10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/pci.h>
16 static const u8 cb710_clock_divider_log2[8] = {
18 0, 1, 2, 3, 4, 5, 7, 9
20 #define CB710_MAX_DIVIDER_IDX \
21 (ARRAY_SIZE(cb710_clock_divider_log2) - 1)
23 static const u8 cb710_src_freq_mhz[16] = {
24 33, 10, 20, 25, 30, 35, 40, 45,
25 50, 55, 60, 65, 70, 75, 80, 85
28 static void cb710_mmc_select_clock_divider(
struct mmc_host *mmc,
int hz)
41 pci_read_config_dword(pdev, 0x48, &src_freq_idx);
42 src_freq_idx = (src_freq_idx >> 16) & 0xF;
43 src_hz = cb710_src_freq_mhz[src_freq_idx] * 1000000;
46 if (hz >= src_hz >> cb710_clock_divider_log2[divider_idx])
52 else if (divider_idx == 0)
58 "clock set to %d Hz, wanted %d Hz; src_freq_idx = %d, divider_idx = %d|%d\n",
59 src_hz >> cb710_clock_divider_log2[divider_idx & 7],
60 hz, src_freq_idx, divider_idx & 7, divider_idx & 8);
63 static void __cb710_mmc_enable_irq(
struct cb710_slot *slot,
84 static void cb710_mmc_enable_irq(
struct cb710_slot *slot,
85 unsigned short enable,
unsigned short mask)
92 __cb710_mmc_enable_irq(slot, enable, mask);
93 spin_unlock_irqrestore(&reader->
irq_lock, flags);
96 static void cb710_mmc_reset_events(
struct cb710_slot *slot)
103 static void cb710_mmc_enable_4bit_data(
struct cb710_slot *slot,
int enable)
122 "CHECK : ignoring bit 6 in status %04X\n", status);
124 CB710_MMC_S0_FIFO_UNDERFLOW);
125 status &= ~CB710_MMC_S0_FIFO_UNDERFLOW;
130 "CHECK : returning EIO on status %04X\n", status);
138 if ((status >> 8) & what) {
146 static int cb710_wait_for_event(
struct cb710_slot *slot,
u8 what)
149 unsigned limit = 2000000;
151 #ifdef CONFIG_CB710_DEBUG
156 while (!(err = cb710_check_event(slot, what))) {
166 #ifdef CONFIG_CB710_DEBUG
169 limit = 2000000 -
limit;
172 "WAIT10: waited %d loops, what %d, entry val %08X, exit val %08X\n",
175 return err < 0 ? err : 0;
181 unsigned limit = 500000;
184 #ifdef CONFIG_CB710_DEBUG
199 #ifdef CONFIG_CB710_DEBUG
202 limit = 500000 -
limit;
205 "WAIT12: waited %d loops, mask %02X, entry val %08X, exit val %08X\n",
211 static void cb710_mmc_set_transfer_size(
struct cb710_slot *slot,
212 size_t count,
size_t blocksize)
216 ((count - 1) << 16)|(blocksize - 1));
218 dev_vdbg(cb710_slot_dev(slot),
"set up for %zu block%s of %zu bytes\n",
219 count, count == 1 ?
"" :
"s", blocksize);
222 static void cb710_mmc_fifo_hack(
struct cb710_slot *slot)
231 & CB710_MMC_S0_FIFO_UNDERFLOW) {
233 CB710_MMC_S0_FIFO_UNDERFLOW);
238 "FIFO-read-hack: expected STATUS0 bit was %s\n",
239 ok ?
"set." :
"NOT SET!");
241 "FIFO-read-hack: dwords ignored: %08X %08X - %s\n",
242 r1, r2, (r1|r2) ?
"BAD (NOT ZERO)!" :
"ok");
245 static int cb710_mmc_receive_pio(
struct cb710_slot *slot,
249 int err = cb710_wait_for_event(slot,
255 cb710_sg_dwiter_write_from_io(miter,
261 static bool cb710_is_transfer_size_supported(
struct mmc_data *
data)
282 cb710_mmc_fifo_hack(slot);
284 while (blocks-- > 0) {
288 err = cb710_mmc_receive_pio(slot, &miter, 4);
300 len = (len >= 8) ? 4 : 2;
301 err = cb710_mmc_receive_pio(slot, &miter, len);
313 size_t len, blocks = data->
blocks;
326 while (blocks-- > 0) {
327 len = (data->
blksz + 15) >> 4;
331 err = cb710_wait_for_event(slot,
336 cb710_sg_dwiter_read_to_io(&miter,
396 static void cb710_receive_response(
struct cb710_slot *slot,
399 unsigned rsp_opcode, wanted_opcode;
402 if (cmd->
flags & MMC_RSP_136) {
409 rsp_opcode = resp[0] >> 24;
411 cmd->
resp[0] = (resp[0] << 8)|(resp[1] >> 24);
412 cmd->
resp[1] = (resp[1] << 8)|(resp[2] >> 24);
413 cmd->
resp[2] = (resp[2] << 8)|(resp[3] >> 24);
414 cmd->
resp[3] = (resp[3] << 8);
421 if (rsp_opcode != wanted_opcode)
425 static int cb710_mmc_transfer_data(
struct cb710_slot *slot,
431 error = cb710_mmc_receive(slot, data);
433 error = cb710_mmc_send(slot, data);
446 struct cb710_slot *slot = cb710_mmc_to_slot(mmc);
450 u16 cb_cmd = cb710_encode_cmd_flags(reader, cmd);
451 dev_dbg(cb710_slot_dev(slot),
"cmd request: 0x%04X\n", cb_cmd);
454 if (!cb710_is_transfer_size_supported(data)) {
458 cb710_mmc_set_transfer_size(slot, data->
blocks, data->
blksz);
465 cb710_mmc_reset_events(slot);
473 if (cmd->
flags & MMC_RSP_PRESENT) {
474 cb710_receive_response(slot, cmd);
480 data->
error = cb710_mmc_transfer_data(slot, data);
486 struct cb710_slot *slot = cb710_mmc_to_slot(mmc);
494 if (!cb710_mmc_command(mmc, mrq->
cmd) && mrq->
stop)
495 cb710_mmc_command(mmc, mrq->
stop);
500 static int cb710_mmc_powerup(
struct cb710_slot *slot)
502 #ifdef CONFIG_CB710_DEBUG
508 dev_dbg(cb710_slot_dev(slot),
"bus powerup\n");
517 dev_dbg(cb710_slot_dev(slot),
"after delay 1\n");
525 dev_dbg(cb710_slot_dev(slot),
"after delay 2\n");
533 dev_dbg(cb710_slot_dev(slot),
"after delay 3\n");
550 dev_dbg(cb710_slot_dev(slot),
"bus powerup finished\n");
552 return cb710_check_event(slot, 0);
555 static void cb710_mmc_powerdown(
struct cb710_slot *slot)
561 static void cb710_mmc_set_ios(
struct mmc_host *mmc,
struct mmc_ios *ios)
563 struct cb710_slot *slot = cb710_mmc_to_slot(mmc);
567 cb710_mmc_select_clock_divider(mmc, ios->
clock);
572 err = cb710_mmc_powerup(slot);
575 "powerup failed (%d)- retrying\n", err);
576 cb710_mmc_powerdown(slot);
578 err = cb710_mmc_powerup(slot);
581 "powerup retry failed (%d) - expect errors\n",
587 cb710_mmc_powerdown(slot);
600 static int cb710_mmc_get_ro(
struct mmc_host *mmc)
602 struct cb710_slot *slot = cb710_mmc_to_slot(mmc);
608 static int cb710_mmc_get_cd(
struct mmc_host *mmc)
610 struct cb710_slot *slot = cb710_mmc_to_slot(mmc);
616 static int cb710_mmc_irq_handler(
struct cb710_slot *slot)
618 struct mmc_host *mmc = cb710_slot_to_mmc(slot);
627 dev_dbg(cb710_slot_dev(slot),
"interrupt; status: %08X, "
628 "ie: %08X, c2: %08X, c1: %08X\n",
629 status, irqen, config2, config1);
636 == CB710_MMC_IE_CISTATUS_MASK)
639 dev_dbg(cb710_slot_dev(slot),
"unknown interrupt (test)\n");
648 static void cb710_mmc_finish_request_tasklet(
unsigned long data)
650 struct mmc_host *mmc = (
void *)data;
659 .request = cb710_mmc_request,
660 .set_ios = cb710_mmc_set_ios,
661 .get_ro = cb710_mmc_get_ro,
662 .get_cd = cb710_mmc_get_cd,
669 struct cb710_slot *slot = cb710_pdev_to_slot(pdev);
670 struct mmc_host *mmc = cb710_slot_to_mmc(slot);
677 cb710_mmc_enable_irq(slot, 0, ~0);
683 struct cb710_slot *slot = cb710_pdev_to_slot(pdev);
684 struct mmc_host *mmc = cb710_slot_to_mmc(slot);
686 cb710_mmc_enable_irq(slot, 0, ~0);
695 struct cb710_slot *slot = cb710_pdev_to_slot(pdev);
696 struct cb710_chip *chip = cb710_slot_to_chip(slot);
709 pci_read_config_dword(chip->
pdev, 0x48, &val);
710 val = cb710_src_freq_mhz[(val >> 16) & 0xF];
711 dev_dbg(cb710_slot_dev(slot),
"source frequency: %dMHz\n", val);
714 mmc->
ops = &cb710_mmc_host;
720 reader = mmc_priv(mmc);
723 cb710_mmc_finish_request_tasklet, (
unsigned long)mmc);
727 cb710_mmc_enable_irq(slot, 0, ~0);
734 dev_dbg(cb710_slot_dev(slot),
"mmc_hostname is %s\n",
742 dev_dbg(cb710_slot_dev(slot),
"mmc_add_host() failed: %d\n", err);
751 struct cb710_slot *slot = cb710_pdev_to_slot(pdev);
752 struct mmc_host *mmc = cb710_slot_to_mmc(slot);
760 cb710_mmc_enable_irq(slot, 0, ~0);
774 .driver.name =
"cb710-mmc",
775 .probe = cb710_mmc_init,
778 .suspend = cb710_mmc_suspend,
779 .resume = cb710_mmc_resume,