[MMC] sdhci: fix interrupt handling
[linux-2.6/verdex.git] / drivers / mmc / sdhci.c
blobb9aa60aed7f04666e111432cdca4d9caa0ef64ae
1 /*
2 * linux/drivers/mmc/sdhci.c - Secure Digital Host Controller Interface driver
4 * Copyright (C) 2005-2006 Pierre Ossman, All Rights Reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
12 * Note that PIO transfer is rather crappy atm. The buffer full/empty
13 * interrupts aren't reliable so we currently transfer the entire buffer
14 * directly. Patches to solve the problem are welcome.
17 #include <linux/delay.h>
18 #include <linux/highmem.h>
19 #include <linux/pci.h>
20 #include <linux/dma-mapping.h>
22 #include <linux/mmc/host.h>
23 #include <linux/mmc/protocol.h>
25 #include <asm/scatterlist.h>
27 #include "sdhci.h"
29 #define DRIVER_NAME "sdhci"
30 #define DRIVER_VERSION "0.11"
32 #define BUGMAIL "<sdhci-devel@list.drzeus.cx>"
34 #define DBG(f, x...) \
35 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
37 static const struct pci_device_id pci_ids[] __devinitdata = {
38 /* handle any SD host controller */
39 {PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)},
40 { /* end: all zeroes */ },
43 MODULE_DEVICE_TABLE(pci, pci_ids);
45 static void sdhci_prepare_data(struct sdhci_host *, struct mmc_data *);
46 static void sdhci_finish_data(struct sdhci_host *);
48 static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
49 static void sdhci_finish_command(struct sdhci_host *);
51 static void sdhci_dumpregs(struct sdhci_host *host)
53 printk(KERN_DEBUG DRIVER_NAME ": ============== REGISTER DUMP ==============\n");
55 printk(KERN_DEBUG DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
56 readl(host->ioaddr + SDHCI_DMA_ADDRESS),
57 readw(host->ioaddr + SDHCI_HOST_VERSION));
58 printk(KERN_DEBUG DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
59 readw(host->ioaddr + SDHCI_BLOCK_SIZE),
60 readw(host->ioaddr + SDHCI_BLOCK_COUNT));
61 printk(KERN_DEBUG DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
62 readl(host->ioaddr + SDHCI_ARGUMENT),
63 readw(host->ioaddr + SDHCI_TRANSFER_MODE));
64 printk(KERN_DEBUG DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
65 readl(host->ioaddr + SDHCI_PRESENT_STATE),
66 readb(host->ioaddr + SDHCI_HOST_CONTROL));
67 printk(KERN_DEBUG DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
68 readb(host->ioaddr + SDHCI_POWER_CONTROL),
69 readb(host->ioaddr + SDHCI_BLOCK_GAP_CONTROL));
70 printk(KERN_DEBUG DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
71 readb(host->ioaddr + SDHCI_WALK_UP_CONTROL),
72 readw(host->ioaddr + SDHCI_CLOCK_CONTROL));
73 printk(KERN_DEBUG DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
74 readb(host->ioaddr + SDHCI_TIMEOUT_CONTROL),
75 readl(host->ioaddr + SDHCI_INT_STATUS));
76 printk(KERN_DEBUG DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
77 readl(host->ioaddr + SDHCI_INT_ENABLE),
78 readl(host->ioaddr + SDHCI_SIGNAL_ENABLE));
79 printk(KERN_DEBUG DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
80 readw(host->ioaddr + SDHCI_ACMD12_ERR),
81 readw(host->ioaddr + SDHCI_SLOT_INT_STATUS));
82 printk(KERN_DEBUG DRIVER_NAME ": Caps: 0x%08x | Max curr: 0x%08x\n",
83 readl(host->ioaddr + SDHCI_CAPABILITIES),
84 readl(host->ioaddr + SDHCI_MAX_CURRENT));
86 printk(KERN_DEBUG DRIVER_NAME ": ===========================================\n");
89 /*****************************************************************************\
90 * *
91 * Low level functions *
92 * *
93 \*****************************************************************************/
95 static void sdhci_reset(struct sdhci_host *host, u8 mask)
97 unsigned long timeout;
99 writeb(mask, host->ioaddr + SDHCI_SOFTWARE_RESET);
101 if (mask & SDHCI_RESET_ALL)
102 host->clock = 0;
104 /* Wait max 100 ms */
105 timeout = 100;
107 /* hw clears the bit when it's done */
108 while (readb(host->ioaddr + SDHCI_SOFTWARE_RESET) & mask) {
109 if (timeout == 0) {
110 printk(KERN_ERR "%s: Reset 0x%x never completed. "
111 "Please report this to " BUGMAIL ".\n",
112 mmc_hostname(host->mmc), (int)mask);
113 sdhci_dumpregs(host);
114 return;
116 timeout--;
117 mdelay(1);
121 static void sdhci_init(struct sdhci_host *host)
123 u32 intmask;
125 sdhci_reset(host, SDHCI_RESET_ALL);
127 intmask = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
128 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
129 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
130 SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT |
131 SDHCI_INT_BUF_EMPTY | SDHCI_INT_BUF_FULL |
132 SDHCI_INT_DMA_END | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE;
134 writel(intmask, host->ioaddr + SDHCI_INT_ENABLE);
135 writel(intmask, host->ioaddr + SDHCI_SIGNAL_ENABLE);
138 static void sdhci_activate_led(struct sdhci_host *host)
140 u8 ctrl;
142 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
143 ctrl |= SDHCI_CTRL_LED;
144 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
147 static void sdhci_deactivate_led(struct sdhci_host *host)
149 u8 ctrl;
151 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
152 ctrl &= ~SDHCI_CTRL_LED;
153 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
156 /*****************************************************************************\
158 * Core functions *
160 \*****************************************************************************/
162 static inline char* sdhci_kmap_sg(struct sdhci_host* host)
164 host->mapped_sg = kmap_atomic(host->cur_sg->page, KM_BIO_SRC_IRQ);
165 return host->mapped_sg + host->cur_sg->offset;
168 static inline void sdhci_kunmap_sg(struct sdhci_host* host)
170 kunmap_atomic(host->mapped_sg, KM_BIO_SRC_IRQ);
173 static inline int sdhci_next_sg(struct sdhci_host* host)
176 * Skip to next SG entry.
178 host->cur_sg++;
179 host->num_sg--;
182 * Any entries left?
184 if (host->num_sg > 0) {
185 host->offset = 0;
186 host->remain = host->cur_sg->length;
189 return host->num_sg;
192 static void sdhci_transfer_pio(struct sdhci_host *host)
194 char *buffer;
195 u32 mask;
196 int bytes, size;
197 unsigned long max_jiffies;
199 BUG_ON(!host->data);
201 if (host->num_sg == 0)
202 return;
204 bytes = 0;
205 if (host->data->flags & MMC_DATA_READ)
206 mask = SDHCI_DATA_AVAILABLE;
207 else
208 mask = SDHCI_SPACE_AVAILABLE;
210 buffer = sdhci_kmap_sg(host) + host->offset;
212 /* Transfer shouldn't take more than 5 s */
213 max_jiffies = jiffies + HZ * 5;
215 while (host->size > 0) {
216 if (time_after(jiffies, max_jiffies)) {
217 printk(KERN_ERR "%s: PIO transfer stalled. "
218 "Please report this to "
219 BUGMAIL ".\n", mmc_hostname(host->mmc));
220 sdhci_dumpregs(host);
222 sdhci_kunmap_sg(host);
224 host->data->error = MMC_ERR_FAILED;
225 sdhci_finish_data(host);
226 return;
229 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & mask))
230 continue;
232 size = min(host->size, host->remain);
234 if (size >= 4) {
235 if (host->data->flags & MMC_DATA_READ)
236 *(u32*)buffer = readl(host->ioaddr + SDHCI_BUFFER);
237 else
238 writel(*(u32*)buffer, host->ioaddr + SDHCI_BUFFER);
239 size = 4;
240 } else if (size >= 2) {
241 if (host->data->flags & MMC_DATA_READ)
242 *(u16*)buffer = readw(host->ioaddr + SDHCI_BUFFER);
243 else
244 writew(*(u16*)buffer, host->ioaddr + SDHCI_BUFFER);
245 size = 2;
246 } else {
247 if (host->data->flags & MMC_DATA_READ)
248 *(u8*)buffer = readb(host->ioaddr + SDHCI_BUFFER);
249 else
250 writeb(*(u8*)buffer, host->ioaddr + SDHCI_BUFFER);
251 size = 1;
254 buffer += size;
255 host->offset += size;
256 host->remain -= size;
258 bytes += size;
259 host->size -= size;
261 if (host->remain == 0) {
262 sdhci_kunmap_sg(host);
263 if (sdhci_next_sg(host) == 0) {
264 DBG("PIO transfer: %d bytes\n", bytes);
265 return;
267 buffer = sdhci_kmap_sg(host);
271 sdhci_kunmap_sg(host);
273 DBG("PIO transfer: %d bytes\n", bytes);
276 static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_data *data)
278 u8 count;
279 unsigned target_timeout, current_timeout;
281 WARN_ON(host->data);
283 if (data == NULL)
284 return;
286 DBG("blksz %04x blks %04x flags %08x\n",
287 data->blksz, data->blocks, data->flags);
288 DBG("tsac %d ms nsac %d clk\n",
289 data->timeout_ns / 1000000, data->timeout_clks);
291 /* timeout in us */
292 target_timeout = data->timeout_ns / 1000 +
293 data->timeout_clks / host->clock;
296 * Figure out needed cycles.
297 * We do this in steps in order to fit inside a 32 bit int.
298 * The first step is the minimum timeout, which will have a
299 * minimum resolution of 6 bits:
300 * (1) 2^13*1000 > 2^22,
301 * (2) host->timeout_clk < 2^16
302 * =>
303 * (1) / (2) > 2^6
305 count = 0;
306 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
307 while (current_timeout < target_timeout) {
308 count++;
309 current_timeout <<= 1;
310 if (count >= 0xF)
311 break;
314 if (count >= 0xF) {
315 printk(KERN_WARNING "%s: Too large timeout requested!\n",
316 mmc_hostname(host->mmc));
317 count = 0xE;
320 writeb(count, host->ioaddr + SDHCI_TIMEOUT_CONTROL);
322 if (host->flags & SDHCI_USE_DMA) {
323 int count;
325 count = pci_map_sg(host->chip->pdev, data->sg, data->sg_len,
326 (data->flags & MMC_DATA_READ)?PCI_DMA_FROMDEVICE:PCI_DMA_TODEVICE);
327 BUG_ON(count != 1);
329 writel(sg_dma_address(data->sg), host->ioaddr + SDHCI_DMA_ADDRESS);
330 } else {
331 host->size = data->blksz * data->blocks;
333 host->cur_sg = data->sg;
334 host->num_sg = data->sg_len;
336 host->offset = 0;
337 host->remain = host->cur_sg->length;
340 writew(data->blksz, host->ioaddr + SDHCI_BLOCK_SIZE);
341 writew(data->blocks, host->ioaddr + SDHCI_BLOCK_COUNT);
344 static void sdhci_set_transfer_mode(struct sdhci_host *host,
345 struct mmc_data *data)
347 u16 mode;
349 WARN_ON(host->data);
351 if (data == NULL)
352 return;
354 mode = SDHCI_TRNS_BLK_CNT_EN;
355 if (data->blocks > 1)
356 mode |= SDHCI_TRNS_MULTI;
357 if (data->flags & MMC_DATA_READ)
358 mode |= SDHCI_TRNS_READ;
359 if (host->flags & SDHCI_USE_DMA)
360 mode |= SDHCI_TRNS_DMA;
362 writew(mode, host->ioaddr + SDHCI_TRANSFER_MODE);
365 static void sdhci_finish_data(struct sdhci_host *host)
367 struct mmc_data *data;
368 u16 blocks;
370 BUG_ON(!host->data);
372 data = host->data;
373 host->data = NULL;
375 if (host->flags & SDHCI_USE_DMA) {
376 pci_unmap_sg(host->chip->pdev, data->sg, data->sg_len,
377 (data->flags & MMC_DATA_READ)?PCI_DMA_FROMDEVICE:PCI_DMA_TODEVICE);
381 * Controller doesn't count down when in single block mode.
383 if ((data->blocks == 1) && (data->error == MMC_ERR_NONE))
384 blocks = 0;
385 else
386 blocks = readw(host->ioaddr + SDHCI_BLOCK_COUNT);
387 data->bytes_xfered = data->blksz * (data->blocks - blocks);
389 if ((data->error == MMC_ERR_NONE) && blocks) {
390 printk(KERN_ERR "%s: Controller signalled completion even "
391 "though there were blocks left. Please report this "
392 "to " BUGMAIL ".\n", mmc_hostname(host->mmc));
393 data->error = MMC_ERR_FAILED;
396 if (host->size != 0) {
397 printk(KERN_ERR "%s: %d bytes were left untransferred. "
398 "Please report this to " BUGMAIL ".\n",
399 mmc_hostname(host->mmc), host->size);
400 data->error = MMC_ERR_FAILED;
403 DBG("Ending data transfer (%d bytes)\n", data->bytes_xfered);
405 if (data->stop) {
407 * The controller needs a reset of internal state machines
408 * upon error conditions.
410 if (data->error != MMC_ERR_NONE) {
411 sdhci_reset(host, SDHCI_RESET_CMD);
412 sdhci_reset(host, SDHCI_RESET_DATA);
415 sdhci_send_command(host, data->stop);
416 } else
417 tasklet_schedule(&host->finish_tasklet);
420 static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
422 int flags;
423 unsigned long timeout;
425 WARN_ON(host->cmd);
427 DBG("Sending cmd (%x)\n", cmd->opcode);
429 /* Wait max 10 ms */
430 timeout = 10;
431 while (readl(host->ioaddr + SDHCI_PRESENT_STATE) &
432 (SDHCI_CMD_INHIBIT | SDHCI_DATA_INHIBIT)) {
433 if (timeout == 0) {
434 printk(KERN_ERR "%s: Controller never released "
435 "inhibit bits. Please report this to "
436 BUGMAIL ".\n", mmc_hostname(host->mmc));
437 sdhci_dumpregs(host);
438 cmd->error = MMC_ERR_FAILED;
439 tasklet_schedule(&host->finish_tasklet);
440 return;
442 timeout--;
443 mdelay(1);
446 mod_timer(&host->timer, jiffies + 10 * HZ);
448 host->cmd = cmd;
450 sdhci_prepare_data(host, cmd->data);
452 writel(cmd->arg, host->ioaddr + SDHCI_ARGUMENT);
454 sdhci_set_transfer_mode(host, cmd->data);
456 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
457 printk(KERN_ERR "%s: Unsupported response type! "
458 "Please report this to " BUGMAIL ".\n",
459 mmc_hostname(host->mmc));
460 cmd->error = MMC_ERR_INVALID;
461 tasklet_schedule(&host->finish_tasklet);
462 return;
465 if (!(cmd->flags & MMC_RSP_PRESENT))
466 flags = SDHCI_CMD_RESP_NONE;
467 else if (cmd->flags & MMC_RSP_136)
468 flags = SDHCI_CMD_RESP_LONG;
469 else if (cmd->flags & MMC_RSP_BUSY)
470 flags = SDHCI_CMD_RESP_SHORT_BUSY;
471 else
472 flags = SDHCI_CMD_RESP_SHORT;
474 if (cmd->flags & MMC_RSP_CRC)
475 flags |= SDHCI_CMD_CRC;
476 if (cmd->flags & MMC_RSP_OPCODE)
477 flags |= SDHCI_CMD_INDEX;
478 if (cmd->data)
479 flags |= SDHCI_CMD_DATA;
481 writel(SDHCI_MAKE_CMD(cmd->opcode, flags),
482 host->ioaddr + SDHCI_COMMAND);
485 static void sdhci_finish_command(struct sdhci_host *host)
487 int i;
489 BUG_ON(host->cmd == NULL);
491 if (host->cmd->flags & MMC_RSP_PRESENT) {
492 if (host->cmd->flags & MMC_RSP_136) {
493 /* CRC is stripped so we need to do some shifting. */
494 for (i = 0;i < 4;i++) {
495 host->cmd->resp[i] = readl(host->ioaddr +
496 SDHCI_RESPONSE + (3-i)*4) << 8;
497 if (i != 3)
498 host->cmd->resp[i] |=
499 readb(host->ioaddr +
500 SDHCI_RESPONSE + (3-i)*4-1);
502 } else {
503 host->cmd->resp[0] = readl(host->ioaddr + SDHCI_RESPONSE);
507 host->cmd->error = MMC_ERR_NONE;
509 DBG("Ending cmd (%x)\n", host->cmd->opcode);
511 if (host->cmd->data)
512 host->data = host->cmd->data;
513 else
514 tasklet_schedule(&host->finish_tasklet);
516 host->cmd = NULL;
519 static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
521 int div;
522 u16 clk;
523 unsigned long timeout;
525 if (clock == host->clock)
526 return;
528 writew(0, host->ioaddr + SDHCI_CLOCK_CONTROL);
530 if (clock == 0)
531 goto out;
533 for (div = 1;div < 256;div *= 2) {
534 if ((host->max_clk / div) <= clock)
535 break;
537 div >>= 1;
539 clk = div << SDHCI_DIVIDER_SHIFT;
540 clk |= SDHCI_CLOCK_INT_EN;
541 writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL);
543 /* Wait max 10 ms */
544 timeout = 10;
545 while (!((clk = readw(host->ioaddr + SDHCI_CLOCK_CONTROL))
546 & SDHCI_CLOCK_INT_STABLE)) {
547 if (timeout == 0) {
548 printk(KERN_ERR "%s: Internal clock never stabilised. "
549 "Please report this to " BUGMAIL ".\n",
550 mmc_hostname(host->mmc));
551 sdhci_dumpregs(host);
552 return;
554 timeout--;
555 mdelay(1);
558 clk |= SDHCI_CLOCK_CARD_EN;
559 writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL);
561 out:
562 host->clock = clock;
565 static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
567 u8 pwr;
569 if (host->power == power)
570 return;
572 writeb(0, host->ioaddr + SDHCI_POWER_CONTROL);
574 if (power == (unsigned short)-1)
575 goto out;
577 pwr = SDHCI_POWER_ON;
579 switch (power) {
580 case MMC_VDD_170:
581 case MMC_VDD_180:
582 case MMC_VDD_190:
583 pwr |= SDHCI_POWER_180;
584 break;
585 case MMC_VDD_290:
586 case MMC_VDD_300:
587 case MMC_VDD_310:
588 pwr |= SDHCI_POWER_300;
589 break;
590 case MMC_VDD_320:
591 case MMC_VDD_330:
592 case MMC_VDD_340:
593 pwr |= SDHCI_POWER_330;
594 break;
595 default:
596 BUG();
599 writeb(pwr, host->ioaddr + SDHCI_POWER_CONTROL);
601 out:
602 host->power = power;
605 /*****************************************************************************\
607 * MMC callbacks *
609 \*****************************************************************************/
611 static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
613 struct sdhci_host *host;
614 unsigned long flags;
616 host = mmc_priv(mmc);
618 spin_lock_irqsave(&host->lock, flags);
620 WARN_ON(host->mrq != NULL);
622 sdhci_activate_led(host);
624 host->mrq = mrq;
626 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
627 host->mrq->cmd->error = MMC_ERR_TIMEOUT;
628 tasklet_schedule(&host->finish_tasklet);
629 } else
630 sdhci_send_command(host, mrq->cmd);
632 spin_unlock_irqrestore(&host->lock, flags);
635 static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
637 struct sdhci_host *host;
638 unsigned long flags;
639 u8 ctrl;
641 host = mmc_priv(mmc);
643 spin_lock_irqsave(&host->lock, flags);
646 * Reset the chip on each power off.
647 * Should clear out any weird states.
649 if (ios->power_mode == MMC_POWER_OFF) {
650 writel(0, host->ioaddr + SDHCI_SIGNAL_ENABLE);
651 sdhci_init(host);
654 sdhci_set_clock(host, ios->clock);
656 if (ios->power_mode == MMC_POWER_OFF)
657 sdhci_set_power(host, -1);
658 else
659 sdhci_set_power(host, ios->vdd);
661 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
662 if (ios->bus_width == MMC_BUS_WIDTH_4)
663 ctrl |= SDHCI_CTRL_4BITBUS;
664 else
665 ctrl &= ~SDHCI_CTRL_4BITBUS;
666 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
668 spin_unlock_irqrestore(&host->lock, flags);
671 static int sdhci_get_ro(struct mmc_host *mmc)
673 struct sdhci_host *host;
674 unsigned long flags;
675 int present;
677 host = mmc_priv(mmc);
679 spin_lock_irqsave(&host->lock, flags);
681 present = readl(host->ioaddr + SDHCI_PRESENT_STATE);
683 spin_unlock_irqrestore(&host->lock, flags);
685 return !(present & SDHCI_WRITE_PROTECT);
688 static struct mmc_host_ops sdhci_ops = {
689 .request = sdhci_request,
690 .set_ios = sdhci_set_ios,
691 .get_ro = sdhci_get_ro,
694 /*****************************************************************************\
696 * Tasklets *
698 \*****************************************************************************/
700 static void sdhci_tasklet_card(unsigned long param)
702 struct sdhci_host *host;
703 unsigned long flags;
705 host = (struct sdhci_host*)param;
707 spin_lock_irqsave(&host->lock, flags);
709 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
710 if (host->mrq) {
711 printk(KERN_ERR "%s: Card removed during transfer!\n",
712 mmc_hostname(host->mmc));
713 printk(KERN_ERR "%s: Resetting controller.\n",
714 mmc_hostname(host->mmc));
716 sdhci_reset(host, SDHCI_RESET_CMD);
717 sdhci_reset(host, SDHCI_RESET_DATA);
719 host->mrq->cmd->error = MMC_ERR_FAILED;
720 tasklet_schedule(&host->finish_tasklet);
724 spin_unlock_irqrestore(&host->lock, flags);
726 mmc_detect_change(host->mmc, msecs_to_jiffies(500));
729 static void sdhci_tasklet_finish(unsigned long param)
731 struct sdhci_host *host;
732 unsigned long flags;
733 struct mmc_request *mrq;
735 host = (struct sdhci_host*)param;
737 spin_lock_irqsave(&host->lock, flags);
739 del_timer(&host->timer);
741 mrq = host->mrq;
743 DBG("Ending request, cmd (%x)\n", mrq->cmd->opcode);
746 * The controller needs a reset of internal state machines
747 * upon error conditions.
749 if ((mrq->cmd->error != MMC_ERR_NONE) ||
750 (mrq->data && ((mrq->data->error != MMC_ERR_NONE) ||
751 (mrq->data->stop && (mrq->data->stop->error != MMC_ERR_NONE))))) {
752 sdhci_reset(host, SDHCI_RESET_CMD);
753 sdhci_reset(host, SDHCI_RESET_DATA);
756 host->mrq = NULL;
757 host->cmd = NULL;
758 host->data = NULL;
760 sdhci_deactivate_led(host);
762 spin_unlock_irqrestore(&host->lock, flags);
764 mmc_request_done(host->mmc, mrq);
767 static void sdhci_timeout_timer(unsigned long data)
769 struct sdhci_host *host;
770 unsigned long flags;
772 host = (struct sdhci_host*)data;
774 spin_lock_irqsave(&host->lock, flags);
776 if (host->mrq) {
777 printk(KERN_ERR "%s: Timeout waiting for hardware interrupt. "
778 "Please report this to " BUGMAIL ".\n",
779 mmc_hostname(host->mmc));
780 sdhci_dumpregs(host);
782 if (host->data) {
783 host->data->error = MMC_ERR_TIMEOUT;
784 sdhci_finish_data(host);
785 } else {
786 if (host->cmd)
787 host->cmd->error = MMC_ERR_TIMEOUT;
788 else
789 host->mrq->cmd->error = MMC_ERR_TIMEOUT;
791 tasklet_schedule(&host->finish_tasklet);
795 spin_unlock_irqrestore(&host->lock, flags);
798 /*****************************************************************************\
800 * Interrupt handling *
802 \*****************************************************************************/
804 static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
806 BUG_ON(intmask == 0);
808 if (!host->cmd) {
809 printk(KERN_ERR "%s: Got command interrupt even though no "
810 "command operation was in progress.\n",
811 mmc_hostname(host->mmc));
812 printk(KERN_ERR "%s: Please report this to " BUGMAIL ".\n",
813 mmc_hostname(host->mmc));
814 sdhci_dumpregs(host);
815 return;
818 if (intmask & SDHCI_INT_RESPONSE)
819 sdhci_finish_command(host);
820 else {
821 if (intmask & SDHCI_INT_TIMEOUT)
822 host->cmd->error = MMC_ERR_TIMEOUT;
823 else if (intmask & SDHCI_INT_CRC)
824 host->cmd->error = MMC_ERR_BADCRC;
825 else if (intmask & (SDHCI_INT_END_BIT | SDHCI_INT_INDEX))
826 host->cmd->error = MMC_ERR_FAILED;
827 else
828 host->cmd->error = MMC_ERR_INVALID;
830 tasklet_schedule(&host->finish_tasklet);
834 static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
836 BUG_ON(intmask == 0);
838 if (!host->data) {
840 * A data end interrupt is sent together with the response
841 * for the stop command.
843 if (intmask & SDHCI_INT_DATA_END)
844 return;
846 printk(KERN_ERR "%s: Got data interrupt even though no "
847 "data operation was in progress.\n",
848 mmc_hostname(host->mmc));
849 printk(KERN_ERR "%s: Please report this to " BUGMAIL ".\n",
850 mmc_hostname(host->mmc));
851 sdhci_dumpregs(host);
853 return;
856 if (intmask & SDHCI_INT_DATA_TIMEOUT)
857 host->data->error = MMC_ERR_TIMEOUT;
858 else if (intmask & SDHCI_INT_DATA_CRC)
859 host->data->error = MMC_ERR_BADCRC;
860 else if (intmask & SDHCI_INT_DATA_END_BIT)
861 host->data->error = MMC_ERR_FAILED;
863 if (host->data->error != MMC_ERR_NONE)
864 sdhci_finish_data(host);
865 else {
866 if (intmask & (SDHCI_INT_BUF_FULL | SDHCI_INT_BUF_EMPTY))
867 sdhci_transfer_pio(host);
869 if (intmask & SDHCI_INT_DATA_END)
870 sdhci_finish_data(host);
874 static irqreturn_t sdhci_irq(int irq, void *dev_id, struct pt_regs *regs)
876 irqreturn_t result;
877 struct sdhci_host* host = dev_id;
878 u32 intmask;
880 spin_lock(&host->lock);
882 intmask = readl(host->ioaddr + SDHCI_INT_STATUS);
884 if (!intmask) {
885 result = IRQ_NONE;
886 goto out;
889 DBG("*** %s got interrupt: 0x%08x\n", host->slot_descr, intmask);
891 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
892 writel(intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE),
893 host->ioaddr + SDHCI_INT_STATUS);
894 tasklet_schedule(&host->card_tasklet);
897 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
899 if (intmask & SDHCI_INT_CMD_MASK) {
900 writel(intmask & SDHCI_INT_CMD_MASK,
901 host->ioaddr + SDHCI_INT_STATUS);
902 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
905 if (intmask & SDHCI_INT_DATA_MASK) {
906 writel(intmask & SDHCI_INT_DATA_MASK,
907 host->ioaddr + SDHCI_INT_STATUS);
908 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
911 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
913 if (intmask & SDHCI_INT_BUS_POWER) {
914 printk(KERN_ERR "%s: Card is consuming too much power!\n",
915 mmc_hostname(host->mmc));
916 writel(SDHCI_INT_BUS_POWER, host->ioaddr + SDHCI_INT_STATUS);
919 intmask &= SDHCI_INT_BUS_POWER;
921 if (intmask) {
922 printk(KERN_ERR "%s: Unexpected interrupt 0x%08x. Please "
923 "report this to " BUGMAIL ".\n",
924 mmc_hostname(host->mmc), intmask);
925 sdhci_dumpregs(host);
927 writel(intmask, host->ioaddr + SDHCI_INT_STATUS);
930 result = IRQ_HANDLED;
932 out:
933 spin_unlock(&host->lock);
935 return result;
938 /*****************************************************************************\
940 * Suspend/resume *
942 \*****************************************************************************/
944 #ifdef CONFIG_PM
946 static int sdhci_suspend (struct pci_dev *pdev, pm_message_t state)
948 struct sdhci_chip *chip;
949 int i, ret;
951 chip = pci_get_drvdata(pdev);
952 if (!chip)
953 return 0;
955 DBG("Suspending...\n");
957 for (i = 0;i < chip->num_slots;i++) {
958 if (!chip->hosts[i])
959 continue;
960 ret = mmc_suspend_host(chip->hosts[i]->mmc, state);
961 if (ret) {
962 for (i--;i >= 0;i--)
963 mmc_resume_host(chip->hosts[i]->mmc);
964 return ret;
968 pci_save_state(pdev);
969 pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
970 pci_disable_device(pdev);
971 pci_set_power_state(pdev, pci_choose_state(pdev, state));
973 return 0;
976 static int sdhci_resume (struct pci_dev *pdev)
978 struct sdhci_chip *chip;
979 int i, ret;
981 chip = pci_get_drvdata(pdev);
982 if (!chip)
983 return 0;
985 DBG("Resuming...\n");
987 pci_set_power_state(pdev, PCI_D0);
988 pci_restore_state(pdev);
989 pci_enable_device(pdev);
991 for (i = 0;i < chip->num_slots;i++) {
992 if (!chip->hosts[i])
993 continue;
994 if (chip->hosts[i]->flags & SDHCI_USE_DMA)
995 pci_set_master(pdev);
996 sdhci_init(chip->hosts[i]);
997 ret = mmc_resume_host(chip->hosts[i]->mmc);
998 if (ret)
999 return ret;
1002 return 0;
1005 #else /* CONFIG_PM */
1007 #define sdhci_suspend NULL
1008 #define sdhci_resume NULL
1010 #endif /* CONFIG_PM */
1012 /*****************************************************************************\
1014 * Device probing/removal *
1016 \*****************************************************************************/
1018 static int __devinit sdhci_probe_slot(struct pci_dev *pdev, int slot)
1020 int ret;
1021 struct sdhci_chip *chip;
1022 struct mmc_host *mmc;
1023 struct sdhci_host *host;
1025 u8 first_bar;
1026 unsigned int caps;
1028 chip = pci_get_drvdata(pdev);
1029 BUG_ON(!chip);
1031 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
1032 if (ret)
1033 return ret;
1035 first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
1037 if (first_bar > 5) {
1038 printk(KERN_ERR DRIVER_NAME ": Invalid first BAR. Aborting.\n");
1039 return -ENODEV;
1042 if (!(pci_resource_flags(pdev, first_bar + slot) & IORESOURCE_MEM)) {
1043 printk(KERN_ERR DRIVER_NAME ": BAR is not iomem. Aborting.\n");
1044 return -ENODEV;
1047 if (pci_resource_len(pdev, first_bar + slot) != 0x100) {
1048 printk(KERN_ERR DRIVER_NAME ": Invalid iomem size. Aborting.\n");
1049 return -ENODEV;
1052 mmc = mmc_alloc_host(sizeof(struct sdhci_host), &pdev->dev);
1053 if (!mmc)
1054 return -ENOMEM;
1056 host = mmc_priv(mmc);
1057 host->mmc = mmc;
1059 host->bar = first_bar + slot;
1061 host->addr = pci_resource_start(pdev, host->bar);
1062 host->irq = pdev->irq;
1064 DBG("slot %d at 0x%08lx, irq %d\n", slot, host->addr, host->irq);
1066 snprintf(host->slot_descr, 20, "sdhci:slot%d", slot);
1068 ret = pci_request_region(pdev, host->bar, host->slot_descr);
1069 if (ret)
1070 goto free;
1072 host->ioaddr = ioremap_nocache(host->addr,
1073 pci_resource_len(pdev, host->bar));
1074 if (!host->ioaddr) {
1075 ret = -ENOMEM;
1076 goto release;
1079 caps = readl(host->ioaddr + SDHCI_CAPABILITIES);
1081 if ((caps & SDHCI_CAN_DO_DMA) && ((pdev->class & 0x0000FF) == 0x01))
1082 host->flags |= SDHCI_USE_DMA;
1084 if (host->flags & SDHCI_USE_DMA) {
1085 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)) {
1086 printk(KERN_WARNING "%s: No suitable DMA available. "
1087 "Falling back to PIO.\n", host->slot_descr);
1088 host->flags &= ~SDHCI_USE_DMA;
1092 if (host->flags & SDHCI_USE_DMA)
1093 pci_set_master(pdev);
1094 else /* XXX: Hack to get MMC layer to avoid highmem */
1095 pdev->dma_mask = 0;
1097 host->max_clk =
1098 (caps & SDHCI_CLOCK_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT;
1099 if (host->max_clk == 0) {
1100 printk(KERN_ERR "%s: Hardware doesn't specify base clock "
1101 "frequency.\n", host->slot_descr);
1102 ret = -ENODEV;
1103 goto unmap;
1105 host->max_clk *= 1000000;
1107 host->timeout_clk =
1108 (caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
1109 if (host->timeout_clk == 0) {
1110 printk(KERN_ERR "%s: Hardware doesn't specify timeout clock "
1111 "frequency.\n", host->slot_descr);
1112 ret = -ENODEV;
1113 goto unmap;
1115 if (caps & SDHCI_TIMEOUT_CLK_UNIT)
1116 host->timeout_clk *= 1000;
1119 * Set host parameters.
1121 mmc->ops = &sdhci_ops;
1122 mmc->f_min = host->max_clk / 256;
1123 mmc->f_max = host->max_clk;
1124 mmc->caps = MMC_CAP_4_BIT_DATA;
1126 mmc->ocr_avail = 0;
1127 if (caps & SDHCI_CAN_VDD_330)
1128 mmc->ocr_avail |= MMC_VDD_32_33|MMC_VDD_33_34;
1129 else if (caps & SDHCI_CAN_VDD_300)
1130 mmc->ocr_avail |= MMC_VDD_29_30|MMC_VDD_30_31;
1131 else if (caps & SDHCI_CAN_VDD_180)
1132 mmc->ocr_avail |= MMC_VDD_17_18|MMC_VDD_18_19;
1134 if (mmc->ocr_avail == 0) {
1135 printk(KERN_ERR "%s: Hardware doesn't report any "
1136 "support voltages.\n", host->slot_descr);
1137 ret = -ENODEV;
1138 goto unmap;
1141 spin_lock_init(&host->lock);
1144 * Maximum number of segments. Hardware cannot do scatter lists.
1146 if (host->flags & SDHCI_USE_DMA)
1147 mmc->max_hw_segs = 1;
1148 else
1149 mmc->max_hw_segs = 16;
1150 mmc->max_phys_segs = 16;
1153 * Maximum number of sectors in one transfer. Limited by sector
1154 * count register.
1156 mmc->max_sectors = 0x3FFF;
1159 * Maximum segment size. Could be one segment with the maximum number
1160 * of sectors.
1162 mmc->max_seg_size = mmc->max_sectors * 512;
1165 * Init tasklets.
1167 tasklet_init(&host->card_tasklet,
1168 sdhci_tasklet_card, (unsigned long)host);
1169 tasklet_init(&host->finish_tasklet,
1170 sdhci_tasklet_finish, (unsigned long)host);
1172 setup_timer(&host->timer, sdhci_timeout_timer, (long)host);
1174 ret = request_irq(host->irq, sdhci_irq, SA_SHIRQ,
1175 host->slot_descr, host);
1176 if (ret)
1177 goto untasklet;
1179 sdhci_init(host);
1181 #ifdef CONFIG_MMC_DEBUG
1182 sdhci_dumpregs(host);
1183 #endif
1185 host->chip = chip;
1186 chip->hosts[slot] = host;
1188 mmc_add_host(mmc);
1190 printk(KERN_INFO "%s: SDHCI at 0x%08lx irq %d %s\n", mmc_hostname(mmc),
1191 host->addr, host->irq,
1192 (host->flags & SDHCI_USE_DMA)?"DMA":"PIO");
1194 return 0;
1196 untasklet:
1197 tasklet_kill(&host->card_tasklet);
1198 tasklet_kill(&host->finish_tasklet);
1199 unmap:
1200 iounmap(host->ioaddr);
1201 release:
1202 pci_release_region(pdev, host->bar);
1203 free:
1204 mmc_free_host(mmc);
1206 return ret;
1209 static void sdhci_remove_slot(struct pci_dev *pdev, int slot)
1211 struct sdhci_chip *chip;
1212 struct mmc_host *mmc;
1213 struct sdhci_host *host;
1215 chip = pci_get_drvdata(pdev);
1216 host = chip->hosts[slot];
1217 mmc = host->mmc;
1219 chip->hosts[slot] = NULL;
1221 mmc_remove_host(mmc);
1223 sdhci_reset(host, SDHCI_RESET_ALL);
1225 free_irq(host->irq, host);
1227 del_timer_sync(&host->timer);
1229 tasklet_kill(&host->card_tasklet);
1230 tasklet_kill(&host->finish_tasklet);
1232 iounmap(host->ioaddr);
1234 pci_release_region(pdev, host->bar);
1236 mmc_free_host(mmc);
1239 static int __devinit sdhci_probe(struct pci_dev *pdev,
1240 const struct pci_device_id *ent)
1242 int ret, i;
1243 u8 slots, rev;
1244 struct sdhci_chip *chip;
1246 BUG_ON(pdev == NULL);
1247 BUG_ON(ent == NULL);
1249 pci_read_config_byte(pdev, PCI_CLASS_REVISION, &rev);
1251 printk(KERN_INFO DRIVER_NAME
1252 ": SDHCI controller found at %s [%04x:%04x] (rev %x)\n",
1253 pci_name(pdev), (int)pdev->vendor, (int)pdev->device,
1254 (int)rev);
1256 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
1257 if (ret)
1258 return ret;
1260 slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
1261 DBG("found %d slot(s)\n", slots);
1262 if (slots == 0)
1263 return -ENODEV;
1265 ret = pci_enable_device(pdev);
1266 if (ret)
1267 return ret;
1269 chip = kzalloc(sizeof(struct sdhci_chip) +
1270 sizeof(struct sdhci_host*) * slots, GFP_KERNEL);
1271 if (!chip) {
1272 ret = -ENOMEM;
1273 goto err;
1276 chip->pdev = pdev;
1278 chip->num_slots = slots;
1279 pci_set_drvdata(pdev, chip);
1281 for (i = 0;i < slots;i++) {
1282 ret = sdhci_probe_slot(pdev, i);
1283 if (ret) {
1284 for (i--;i >= 0;i--)
1285 sdhci_remove_slot(pdev, i);
1286 goto free;
1290 return 0;
1292 free:
1293 pci_set_drvdata(pdev, NULL);
1294 kfree(chip);
1296 err:
1297 pci_disable_device(pdev);
1298 return ret;
1301 static void __devexit sdhci_remove(struct pci_dev *pdev)
1303 int i;
1304 struct sdhci_chip *chip;
1306 chip = pci_get_drvdata(pdev);
1308 if (chip) {
1309 for (i = 0;i < chip->num_slots;i++)
1310 sdhci_remove_slot(pdev, i);
1312 pci_set_drvdata(pdev, NULL);
1314 kfree(chip);
1317 pci_disable_device(pdev);
1320 static struct pci_driver sdhci_driver = {
1321 .name = DRIVER_NAME,
1322 .id_table = pci_ids,
1323 .probe = sdhci_probe,
1324 .remove = __devexit_p(sdhci_remove),
1325 .suspend = sdhci_suspend,
1326 .resume = sdhci_resume,
1329 /*****************************************************************************\
1331 * Driver init/exit *
1333 \*****************************************************************************/
1335 static int __init sdhci_drv_init(void)
1337 printk(KERN_INFO DRIVER_NAME
1338 ": Secure Digital Host Controller Interface driver, "
1339 DRIVER_VERSION "\n");
1340 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
1342 return pci_register_driver(&sdhci_driver);
1345 static void __exit sdhci_drv_exit(void)
1347 DBG("Exiting\n");
1349 pci_unregister_driver(&sdhci_driver);
1352 module_init(sdhci_drv_init);
1353 module_exit(sdhci_drv_exit);
1355 MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
1356 MODULE_DESCRIPTION("Secure Digital Host Controller Interface driver");
1357 MODULE_VERSION(DRIVER_VERSION);
1358 MODULE_LICENSE("GPL");