Linux 2.6.17.7
[linux/fpc-iii.git] / drivers / mmc / sdhci.c
blobb0053280ff2db61725a7bddba23287e36907428f
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 writeb(mask, host->ioaddr + SDHCI_SOFTWARE_RESET);
99 if (mask & SDHCI_RESET_ALL) {
100 host->clock = 0;
102 mdelay(50);
106 static void sdhci_init(struct sdhci_host *host)
108 u32 intmask;
110 sdhci_reset(host, SDHCI_RESET_ALL);
112 intmask = ~(SDHCI_INT_CARD_INT | SDHCI_INT_BUF_EMPTY | SDHCI_INT_BUF_FULL);
114 writel(intmask, host->ioaddr + SDHCI_INT_ENABLE);
115 writel(intmask, host->ioaddr + SDHCI_SIGNAL_ENABLE);
117 /* This is unknown magic. */
118 writeb(0xE, host->ioaddr + SDHCI_TIMEOUT_CONTROL);
121 static void sdhci_activate_led(struct sdhci_host *host)
123 u8 ctrl;
125 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
126 ctrl |= SDHCI_CTRL_LED;
127 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
130 static void sdhci_deactivate_led(struct sdhci_host *host)
132 u8 ctrl;
134 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
135 ctrl &= ~SDHCI_CTRL_LED;
136 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
139 /*****************************************************************************\
141 * Core functions *
143 \*****************************************************************************/
145 static inline char* sdhci_kmap_sg(struct sdhci_host* host)
147 host->mapped_sg = kmap_atomic(host->cur_sg->page, KM_BIO_SRC_IRQ);
148 return host->mapped_sg + host->cur_sg->offset;
151 static inline void sdhci_kunmap_sg(struct sdhci_host* host)
153 kunmap_atomic(host->mapped_sg, KM_BIO_SRC_IRQ);
156 static inline int sdhci_next_sg(struct sdhci_host* host)
159 * Skip to next SG entry.
161 host->cur_sg++;
162 host->num_sg--;
165 * Any entries left?
167 if (host->num_sg > 0) {
168 host->offset = 0;
169 host->remain = host->cur_sg->length;
172 return host->num_sg;
175 static void sdhci_transfer_pio(struct sdhci_host *host)
177 char *buffer;
178 u32 mask;
179 int bytes, size;
180 unsigned long max_jiffies;
182 BUG_ON(!host->data);
184 if (host->num_sg == 0)
185 return;
187 bytes = 0;
188 if (host->data->flags & MMC_DATA_READ)
189 mask = SDHCI_DATA_AVAILABLE;
190 else
191 mask = SDHCI_SPACE_AVAILABLE;
193 buffer = sdhci_kmap_sg(host) + host->offset;
195 /* Transfer shouldn't take more than 5 s */
196 max_jiffies = jiffies + HZ * 5;
198 while (host->size > 0) {
199 if (time_after(jiffies, max_jiffies)) {
200 printk(KERN_ERR "%s: PIO transfer stalled. "
201 "Please report this to "
202 BUGMAIL ".\n", mmc_hostname(host->mmc));
203 sdhci_dumpregs(host);
205 sdhci_kunmap_sg(host);
207 host->data->error = MMC_ERR_FAILED;
208 sdhci_finish_data(host);
209 return;
212 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & mask))
213 continue;
215 size = min(host->size, host->remain);
217 if (size >= 4) {
218 if (host->data->flags & MMC_DATA_READ)
219 *(u32*)buffer = readl(host->ioaddr + SDHCI_BUFFER);
220 else
221 writel(*(u32*)buffer, host->ioaddr + SDHCI_BUFFER);
222 size = 4;
223 } else if (size >= 2) {
224 if (host->data->flags & MMC_DATA_READ)
225 *(u16*)buffer = readw(host->ioaddr + SDHCI_BUFFER);
226 else
227 writew(*(u16*)buffer, host->ioaddr + SDHCI_BUFFER);
228 size = 2;
229 } else {
230 if (host->data->flags & MMC_DATA_READ)
231 *(u8*)buffer = readb(host->ioaddr + SDHCI_BUFFER);
232 else
233 writeb(*(u8*)buffer, host->ioaddr + SDHCI_BUFFER);
234 size = 1;
237 buffer += size;
238 host->offset += size;
239 host->remain -= size;
241 bytes += size;
242 host->size -= size;
244 if (host->remain == 0) {
245 sdhci_kunmap_sg(host);
246 if (sdhci_next_sg(host) == 0) {
247 DBG("PIO transfer: %d bytes\n", bytes);
248 return;
250 buffer = sdhci_kmap_sg(host);
254 sdhci_kunmap_sg(host);
256 DBG("PIO transfer: %d bytes\n", bytes);
259 static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_data *data)
261 u16 mode;
263 WARN_ON(host->data);
265 if (data == NULL) {
266 writew(0, host->ioaddr + SDHCI_TRANSFER_MODE);
267 return;
270 DBG("blksz %04x blks %04x flags %08x\n",
271 1 << data->blksz_bits, data->blocks, data->flags);
272 DBG("tsac %d ms nsac %d clk\n",
273 data->timeout_ns / 1000000, data->timeout_clks);
275 mode = SDHCI_TRNS_BLK_CNT_EN;
276 if (data->blocks > 1)
277 mode |= SDHCI_TRNS_MULTI;
278 if (data->flags & MMC_DATA_READ)
279 mode |= SDHCI_TRNS_READ;
280 if (host->flags & SDHCI_USE_DMA)
281 mode |= SDHCI_TRNS_DMA;
283 writew(mode, host->ioaddr + SDHCI_TRANSFER_MODE);
285 writew(1 << data->blksz_bits, host->ioaddr + SDHCI_BLOCK_SIZE);
286 writew(data->blocks, host->ioaddr + SDHCI_BLOCK_COUNT);
288 if (host->flags & SDHCI_USE_DMA) {
289 int count;
291 count = pci_map_sg(host->chip->pdev, data->sg, data->sg_len,
292 (data->flags & MMC_DATA_READ)?PCI_DMA_FROMDEVICE:PCI_DMA_TODEVICE);
293 BUG_ON(count != 1);
295 writel(sg_dma_address(data->sg), host->ioaddr + SDHCI_DMA_ADDRESS);
296 } else {
297 host->size = (1 << data->blksz_bits) * data->blocks;
299 host->cur_sg = data->sg;
300 host->num_sg = data->sg_len;
302 host->offset = 0;
303 host->remain = host->cur_sg->length;
307 static void sdhci_finish_data(struct sdhci_host *host)
309 struct mmc_data *data;
310 u32 intmask;
311 u16 blocks;
313 BUG_ON(!host->data);
315 data = host->data;
316 host->data = NULL;
318 if (host->flags & SDHCI_USE_DMA) {
319 pci_unmap_sg(host->chip->pdev, data->sg, data->sg_len,
320 (data->flags & MMC_DATA_READ)?PCI_DMA_FROMDEVICE:PCI_DMA_TODEVICE);
321 } else {
322 intmask = readl(host->ioaddr + SDHCI_SIGNAL_ENABLE);
323 intmask &= ~(SDHCI_INT_BUF_EMPTY | SDHCI_INT_BUF_FULL);
324 writel(intmask, host->ioaddr + SDHCI_SIGNAL_ENABLE);
326 intmask = readl(host->ioaddr + SDHCI_INT_ENABLE);
327 intmask &= ~(SDHCI_INT_BUF_EMPTY | SDHCI_INT_BUF_FULL);
328 writel(intmask, host->ioaddr + SDHCI_INT_ENABLE);
332 * Controller doesn't count down when in single block mode.
334 if ((data->blocks == 1) && (data->error == MMC_ERR_NONE))
335 blocks = 0;
336 else
337 blocks = readw(host->ioaddr + SDHCI_BLOCK_COUNT);
338 data->bytes_xfered = (1 << data->blksz_bits) * (data->blocks - blocks);
340 if ((data->error == MMC_ERR_NONE) && blocks) {
341 printk(KERN_ERR "%s: Controller signalled completion even "
342 "though there were blocks left. Please report this "
343 "to " BUGMAIL ".\n", mmc_hostname(host->mmc));
344 data->error = MMC_ERR_FAILED;
347 if (host->size != 0) {
348 printk(KERN_ERR "%s: %d bytes were left untransferred. "
349 "Please report this to " BUGMAIL ".\n",
350 mmc_hostname(host->mmc), host->size);
351 data->error = MMC_ERR_FAILED;
354 DBG("Ending data transfer (%d bytes)\n", data->bytes_xfered);
356 if (data->stop) {
358 * The controller needs a reset of internal state machines
359 * upon error conditions.
361 if (data->error != MMC_ERR_NONE) {
362 sdhci_reset(host, SDHCI_RESET_CMD);
363 sdhci_reset(host, SDHCI_RESET_DATA);
366 sdhci_send_command(host, data->stop);
367 } else
368 tasklet_schedule(&host->finish_tasklet);
371 static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
373 int flags;
374 u32 present;
375 unsigned long max_jiffies;
377 WARN_ON(host->cmd);
379 DBG("Sending cmd (%x)\n", cmd->opcode);
381 /* Wait max 10 ms */
382 max_jiffies = jiffies + (HZ + 99)/100;
383 do {
384 if (time_after(jiffies, max_jiffies)) {
385 printk(KERN_ERR "%s: Controller never released "
386 "inhibit bits. Please report this to "
387 BUGMAIL ".\n", mmc_hostname(host->mmc));
388 sdhci_dumpregs(host);
389 cmd->error = MMC_ERR_FAILED;
390 tasklet_schedule(&host->finish_tasklet);
391 return;
393 present = readl(host->ioaddr + SDHCI_PRESENT_STATE);
394 } while (present & (SDHCI_CMD_INHIBIT | SDHCI_DATA_INHIBIT));
396 mod_timer(&host->timer, jiffies + 10 * HZ);
398 host->cmd = cmd;
400 sdhci_prepare_data(host, cmd->data);
402 writel(cmd->arg, host->ioaddr + SDHCI_ARGUMENT);
404 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
405 printk(KERN_ERR "%s: Unsupported response type! "
406 "Please report this to " BUGMAIL ".\n",
407 mmc_hostname(host->mmc));
408 cmd->error = MMC_ERR_INVALID;
409 tasklet_schedule(&host->finish_tasklet);
410 return;
413 if (!(cmd->flags & MMC_RSP_PRESENT))
414 flags = SDHCI_CMD_RESP_NONE;
415 else if (cmd->flags & MMC_RSP_136)
416 flags = SDHCI_CMD_RESP_LONG;
417 else if (cmd->flags & MMC_RSP_BUSY)
418 flags = SDHCI_CMD_RESP_SHORT_BUSY;
419 else
420 flags = SDHCI_CMD_RESP_SHORT;
422 if (cmd->flags & MMC_RSP_CRC)
423 flags |= SDHCI_CMD_CRC;
424 if (cmd->flags & MMC_RSP_OPCODE)
425 flags |= SDHCI_CMD_INDEX;
426 if (cmd->data)
427 flags |= SDHCI_CMD_DATA;
429 writel(SDHCI_MAKE_CMD(cmd->opcode, flags),
430 host->ioaddr + SDHCI_COMMAND);
433 static void sdhci_finish_command(struct sdhci_host *host)
435 int i;
437 BUG_ON(host->cmd == NULL);
439 if (host->cmd->flags & MMC_RSP_PRESENT) {
440 if (host->cmd->flags & MMC_RSP_136) {
441 /* CRC is stripped so we need to do some shifting. */
442 for (i = 0;i < 4;i++) {
443 host->cmd->resp[i] = readl(host->ioaddr +
444 SDHCI_RESPONSE + (3-i)*4) << 8;
445 if (i != 3)
446 host->cmd->resp[i] |=
447 readb(host->ioaddr +
448 SDHCI_RESPONSE + (3-i)*4-1);
450 } else {
451 host->cmd->resp[0] = readl(host->ioaddr + SDHCI_RESPONSE);
455 host->cmd->error = MMC_ERR_NONE;
457 DBG("Ending cmd (%x)\n", host->cmd->opcode);
459 if (host->cmd->data) {
460 u32 intmask;
462 host->data = host->cmd->data;
464 if (!(host->flags & SDHCI_USE_DMA)) {
466 * Don't enable the interrupts until now to make sure we
467 * get stable handling of the FIFO.
469 intmask = readl(host->ioaddr + SDHCI_INT_ENABLE);
470 intmask |= SDHCI_INT_BUF_EMPTY | SDHCI_INT_BUF_FULL;
471 writel(intmask, host->ioaddr + SDHCI_INT_ENABLE);
473 intmask = readl(host->ioaddr + SDHCI_SIGNAL_ENABLE);
474 intmask |= SDHCI_INT_BUF_EMPTY | SDHCI_INT_BUF_FULL;
475 writel(intmask, host->ioaddr + SDHCI_SIGNAL_ENABLE);
478 * The buffer interrupts are to unreliable so we
479 * start the transfer immediatly.
481 sdhci_transfer_pio(host);
483 } else
484 tasklet_schedule(&host->finish_tasklet);
486 host->cmd = NULL;
489 static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
491 int div;
492 u16 clk;
493 unsigned long max_jiffies;
495 if (clock == host->clock)
496 return;
498 writew(0, host->ioaddr + SDHCI_CLOCK_CONTROL);
500 if (clock == 0)
501 goto out;
503 for (div = 1;div < 256;div *= 2) {
504 if ((host->max_clk / div) <= clock)
505 break;
507 div >>= 1;
509 clk = div << SDHCI_DIVIDER_SHIFT;
510 clk |= SDHCI_CLOCK_INT_EN;
511 writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL);
513 /* Wait max 10 ms */
514 max_jiffies = jiffies + (HZ + 99)/100;
515 do {
516 if (time_after(jiffies, max_jiffies)) {
517 printk(KERN_ERR "%s: Internal clock never stabilised. "
518 "Please report this to " BUGMAIL ".\n",
519 mmc_hostname(host->mmc));
520 sdhci_dumpregs(host);
521 return;
523 clk = readw(host->ioaddr + SDHCI_CLOCK_CONTROL);
524 } while (!(clk & SDHCI_CLOCK_INT_STABLE));
526 clk |= SDHCI_CLOCK_CARD_EN;
527 writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL);
529 out:
530 host->clock = clock;
533 /*****************************************************************************\
535 * MMC callbacks *
537 \*****************************************************************************/
539 static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
541 struct sdhci_host *host;
542 unsigned long flags;
544 host = mmc_priv(mmc);
546 spin_lock_irqsave(&host->lock, flags);
548 WARN_ON(host->mrq != NULL);
550 sdhci_activate_led(host);
552 host->mrq = mrq;
554 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
555 host->mrq->cmd->error = MMC_ERR_TIMEOUT;
556 tasklet_schedule(&host->finish_tasklet);
557 } else
558 sdhci_send_command(host, mrq->cmd);
560 spin_unlock_irqrestore(&host->lock, flags);
563 static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
565 struct sdhci_host *host;
566 unsigned long flags;
567 u8 ctrl;
569 host = mmc_priv(mmc);
571 spin_lock_irqsave(&host->lock, flags);
574 * Reset the chip on each power off.
575 * Should clear out any weird states.
577 if (ios->power_mode == MMC_POWER_OFF) {
578 writel(0, host->ioaddr + SDHCI_SIGNAL_ENABLE);
579 spin_unlock_irqrestore(&host->lock, flags);
580 sdhci_init(host);
581 spin_lock_irqsave(&host->lock, flags);
584 sdhci_set_clock(host, ios->clock);
586 if (ios->power_mode == MMC_POWER_OFF)
587 writeb(0, host->ioaddr + SDHCI_POWER_CONTROL);
588 else
589 writeb(0xFF, host->ioaddr + SDHCI_POWER_CONTROL);
591 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
592 if (ios->bus_width == MMC_BUS_WIDTH_4)
593 ctrl |= SDHCI_CTRL_4BITBUS;
594 else
595 ctrl &= ~SDHCI_CTRL_4BITBUS;
596 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
598 spin_unlock_irqrestore(&host->lock, flags);
601 static int sdhci_get_ro(struct mmc_host *mmc)
603 struct sdhci_host *host;
604 unsigned long flags;
605 int present;
607 host = mmc_priv(mmc);
609 spin_lock_irqsave(&host->lock, flags);
611 present = readl(host->ioaddr + SDHCI_PRESENT_STATE);
613 spin_unlock_irqrestore(&host->lock, flags);
615 return !(present & SDHCI_WRITE_PROTECT);
618 static struct mmc_host_ops sdhci_ops = {
619 .request = sdhci_request,
620 .set_ios = sdhci_set_ios,
621 .get_ro = sdhci_get_ro,
624 /*****************************************************************************\
626 * Tasklets *
628 \*****************************************************************************/
630 static void sdhci_tasklet_card(unsigned long param)
632 struct sdhci_host *host;
633 unsigned long flags;
635 host = (struct sdhci_host*)param;
637 spin_lock_irqsave(&host->lock, flags);
639 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
640 if (host->mrq) {
641 printk(KERN_ERR "%s: Card removed during transfer!\n",
642 mmc_hostname(host->mmc));
643 printk(KERN_ERR "%s: Resetting controller.\n",
644 mmc_hostname(host->mmc));
646 sdhci_reset(host, SDHCI_RESET_CMD);
647 sdhci_reset(host, SDHCI_RESET_DATA);
649 host->mrq->cmd->error = MMC_ERR_FAILED;
650 tasklet_schedule(&host->finish_tasklet);
654 spin_unlock_irqrestore(&host->lock, flags);
656 mmc_detect_change(host->mmc, msecs_to_jiffies(500));
659 static void sdhci_tasklet_finish(unsigned long param)
661 struct sdhci_host *host;
662 unsigned long flags;
663 struct mmc_request *mrq;
665 host = (struct sdhci_host*)param;
667 spin_lock_irqsave(&host->lock, flags);
669 del_timer(&host->timer);
671 mrq = host->mrq;
673 DBG("Ending request, cmd (%x)\n", mrq->cmd->opcode);
676 * The controller needs a reset of internal state machines
677 * upon error conditions.
679 if ((mrq->cmd->error != MMC_ERR_NONE) ||
680 (mrq->data && ((mrq->data->error != MMC_ERR_NONE) ||
681 (mrq->data->stop && (mrq->data->stop->error != MMC_ERR_NONE))))) {
682 sdhci_reset(host, SDHCI_RESET_CMD);
683 sdhci_reset(host, SDHCI_RESET_DATA);
686 host->mrq = NULL;
687 host->cmd = NULL;
688 host->data = NULL;
690 sdhci_deactivate_led(host);
692 spin_unlock_irqrestore(&host->lock, flags);
694 mmc_request_done(host->mmc, mrq);
697 static void sdhci_timeout_timer(unsigned long data)
699 struct sdhci_host *host;
700 unsigned long flags;
702 host = (struct sdhci_host*)data;
704 spin_lock_irqsave(&host->lock, flags);
706 if (host->mrq) {
707 printk(KERN_ERR "%s: Timeout waiting for hardware interrupt. "
708 "Please report this to " BUGMAIL ".\n",
709 mmc_hostname(host->mmc));
710 sdhci_dumpregs(host);
712 if (host->data) {
713 host->data->error = MMC_ERR_TIMEOUT;
714 sdhci_finish_data(host);
715 } else {
716 if (host->cmd)
717 host->cmd->error = MMC_ERR_TIMEOUT;
718 else
719 host->mrq->cmd->error = MMC_ERR_TIMEOUT;
721 tasklet_schedule(&host->finish_tasklet);
725 spin_unlock_irqrestore(&host->lock, flags);
728 /*****************************************************************************\
730 * Interrupt handling *
732 \*****************************************************************************/
734 static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
736 BUG_ON(intmask == 0);
738 if (!host->cmd) {
739 printk(KERN_ERR "%s: Got command interrupt even though no "
740 "command operation was in progress.\n",
741 mmc_hostname(host->mmc));
742 printk(KERN_ERR "%s: Please report this to " BUGMAIL ".\n",
743 mmc_hostname(host->mmc));
744 sdhci_dumpregs(host);
745 return;
748 if (intmask & SDHCI_INT_RESPONSE)
749 sdhci_finish_command(host);
750 else {
751 if (intmask & SDHCI_INT_TIMEOUT)
752 host->cmd->error = MMC_ERR_TIMEOUT;
753 else if (intmask & SDHCI_INT_CRC)
754 host->cmd->error = MMC_ERR_BADCRC;
755 else if (intmask & (SDHCI_INT_END_BIT | SDHCI_INT_INDEX))
756 host->cmd->error = MMC_ERR_FAILED;
757 else
758 host->cmd->error = MMC_ERR_INVALID;
760 tasklet_schedule(&host->finish_tasklet);
764 static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
766 BUG_ON(intmask == 0);
768 if (!host->data) {
770 * A data end interrupt is sent together with the response
771 * for the stop command.
773 if (intmask & SDHCI_INT_DATA_END)
774 return;
776 printk(KERN_ERR "%s: Got data interrupt even though no "
777 "data operation was in progress.\n",
778 mmc_hostname(host->mmc));
779 printk(KERN_ERR "%s: Please report this to " BUGMAIL ".\n",
780 mmc_hostname(host->mmc));
781 sdhci_dumpregs(host);
783 return;
786 if (intmask & SDHCI_INT_DATA_TIMEOUT)
787 host->data->error = MMC_ERR_TIMEOUT;
788 else if (intmask & SDHCI_INT_DATA_CRC)
789 host->data->error = MMC_ERR_BADCRC;
790 else if (intmask & SDHCI_INT_DATA_END_BIT)
791 host->data->error = MMC_ERR_FAILED;
793 if (host->data->error != MMC_ERR_NONE)
794 sdhci_finish_data(host);
795 else {
796 if (intmask & (SDHCI_INT_BUF_FULL | SDHCI_INT_BUF_EMPTY))
797 sdhci_transfer_pio(host);
799 if (intmask & SDHCI_INT_DATA_END)
800 sdhci_finish_data(host);
804 static irqreturn_t sdhci_irq(int irq, void *dev_id, struct pt_regs *regs)
806 irqreturn_t result;
807 struct sdhci_host* host = dev_id;
808 u32 intmask;
810 spin_lock(&host->lock);
812 intmask = readl(host->ioaddr + SDHCI_INT_STATUS);
814 if (!intmask) {
815 result = IRQ_NONE;
816 goto out;
819 DBG("*** %s got interrupt: 0x%08x\n", host->slot_descr, intmask);
821 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE))
822 tasklet_schedule(&host->card_tasklet);
824 if (intmask & SDHCI_INT_CMD_MASK) {
825 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
827 writel(intmask & SDHCI_INT_CMD_MASK,
828 host->ioaddr + SDHCI_INT_STATUS);
831 if (intmask & SDHCI_INT_DATA_MASK) {
832 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
834 writel(intmask & SDHCI_INT_DATA_MASK,
835 host->ioaddr + SDHCI_INT_STATUS);
838 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
840 if (intmask & SDHCI_INT_CARD_INT) {
841 printk(KERN_ERR "%s: Unexpected card interrupt. Please "
842 "report this to " BUGMAIL ".\n",
843 mmc_hostname(host->mmc));
844 sdhci_dumpregs(host);
847 if (intmask & SDHCI_INT_BUS_POWER) {
848 printk(KERN_ERR "%s: Unexpected bus power interrupt. Please "
849 "report this to " BUGMAIL ".\n",
850 mmc_hostname(host->mmc));
851 sdhci_dumpregs(host);
854 if (intmask & SDHCI_INT_ACMD12ERR) {
855 printk(KERN_ERR "%s: Unexpected auto CMD12 error. Please "
856 "report this to " BUGMAIL ".\n",
857 mmc_hostname(host->mmc));
858 sdhci_dumpregs(host);
860 writew(~0, host->ioaddr + SDHCI_ACMD12_ERR);
863 if (intmask)
864 writel(intmask, host->ioaddr + SDHCI_INT_STATUS);
866 result = IRQ_HANDLED;
868 out:
869 spin_unlock(&host->lock);
871 return result;
874 /*****************************************************************************\
876 * Suspend/resume *
878 \*****************************************************************************/
880 #ifdef CONFIG_PM
882 static int sdhci_suspend (struct pci_dev *pdev, pm_message_t state)
884 struct sdhci_chip *chip;
885 int i, ret;
887 chip = pci_get_drvdata(pdev);
888 if (!chip)
889 return 0;
891 DBG("Suspending...\n");
893 for (i = 0;i < chip->num_slots;i++) {
894 if (!chip->hosts[i])
895 continue;
896 ret = mmc_suspend_host(chip->hosts[i]->mmc, state);
897 if (ret) {
898 for (i--;i >= 0;i--)
899 mmc_resume_host(chip->hosts[i]->mmc);
900 return ret;
904 pci_save_state(pdev);
905 pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
906 pci_disable_device(pdev);
907 pci_set_power_state(pdev, pci_choose_state(pdev, state));
909 return 0;
912 static int sdhci_resume (struct pci_dev *pdev)
914 struct sdhci_chip *chip;
915 int i, ret;
917 chip = pci_get_drvdata(pdev);
918 if (!chip)
919 return 0;
921 DBG("Resuming...\n");
923 pci_set_power_state(pdev, PCI_D0);
924 pci_restore_state(pdev);
925 pci_enable_device(pdev);
927 for (i = 0;i < chip->num_slots;i++) {
928 if (!chip->hosts[i])
929 continue;
930 if (chip->hosts[i]->flags & SDHCI_USE_DMA)
931 pci_set_master(pdev);
932 sdhci_init(chip->hosts[i]);
933 ret = mmc_resume_host(chip->hosts[i]->mmc);
934 if (ret)
935 return ret;
938 return 0;
941 #else /* CONFIG_PM */
943 #define sdhci_suspend NULL
944 #define sdhci_resume NULL
946 #endif /* CONFIG_PM */
948 /*****************************************************************************\
950 * Device probing/removal *
952 \*****************************************************************************/
954 static int __devinit sdhci_probe_slot(struct pci_dev *pdev, int slot)
956 int ret;
957 struct sdhci_chip *chip;
958 struct mmc_host *mmc;
959 struct sdhci_host *host;
961 u8 first_bar;
962 unsigned int caps;
964 chip = pci_get_drvdata(pdev);
965 BUG_ON(!chip);
967 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
968 if (ret)
969 return ret;
971 first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
973 if (first_bar > 5) {
974 printk(KERN_ERR DRIVER_NAME ": Invalid first BAR. Aborting.\n");
975 return -ENODEV;
978 if (!(pci_resource_flags(pdev, first_bar + slot) & IORESOURCE_MEM)) {
979 printk(KERN_ERR DRIVER_NAME ": BAR is not iomem. Aborting.\n");
980 return -ENODEV;
983 if (pci_resource_len(pdev, first_bar + slot) != 0x100) {
984 printk(KERN_ERR DRIVER_NAME ": Invalid iomem size. Aborting.\n");
985 return -ENODEV;
988 mmc = mmc_alloc_host(sizeof(struct sdhci_host), &pdev->dev);
989 if (!mmc)
990 return -ENOMEM;
992 host = mmc_priv(mmc);
993 host->mmc = mmc;
995 host->bar = first_bar + slot;
997 host->addr = pci_resource_start(pdev, host->bar);
998 host->irq = pdev->irq;
1000 DBG("slot %d at 0x%08lx, irq %d\n", slot, host->addr, host->irq);
1002 snprintf(host->slot_descr, 20, "sdhci:slot%d", slot);
1004 ret = pci_request_region(pdev, host->bar, host->slot_descr);
1005 if (ret)
1006 goto free;
1008 host->ioaddr = ioremap_nocache(host->addr,
1009 pci_resource_len(pdev, host->bar));
1010 if (!host->ioaddr) {
1011 ret = -ENOMEM;
1012 goto release;
1015 caps = readl(host->ioaddr + SDHCI_CAPABILITIES);
1017 if ((caps & SDHCI_CAN_DO_DMA) && ((pdev->class & 0x0000FF) == 0x01))
1018 host->flags |= SDHCI_USE_DMA;
1020 if (host->flags & SDHCI_USE_DMA) {
1021 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)) {
1022 printk(KERN_WARNING "%s: No suitable DMA available. "
1023 "Falling back to PIO.\n", host->slot_descr);
1024 host->flags &= ~SDHCI_USE_DMA;
1028 if (host->flags & SDHCI_USE_DMA)
1029 pci_set_master(pdev);
1030 else /* XXX: Hack to get MMC layer to avoid highmem */
1031 pdev->dma_mask = 0;
1033 host->max_clk = (caps & SDHCI_CLOCK_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT;
1034 host->max_clk *= 1000000;
1037 * Set host parameters.
1039 mmc->ops = &sdhci_ops;
1040 mmc->f_min = host->max_clk / 256;
1041 mmc->f_max = host->max_clk;
1042 mmc->ocr_avail = MMC_VDD_32_33|MMC_VDD_33_34;
1043 mmc->caps = MMC_CAP_4_BIT_DATA;
1045 spin_lock_init(&host->lock);
1048 * Maximum number of segments. Hardware cannot do scatter lists.
1050 if (host->flags & SDHCI_USE_DMA)
1051 mmc->max_hw_segs = 1;
1052 else
1053 mmc->max_hw_segs = 16;
1054 mmc->max_phys_segs = 16;
1057 * Maximum number of sectors in one transfer. Limited by sector
1058 * count register.
1060 mmc->max_sectors = 0x3FFF;
1063 * Maximum segment size. Could be one segment with the maximum number
1064 * of sectors.
1066 mmc->max_seg_size = mmc->max_sectors * 512;
1069 * Init tasklets.
1071 tasklet_init(&host->card_tasklet,
1072 sdhci_tasklet_card, (unsigned long)host);
1073 tasklet_init(&host->finish_tasklet,
1074 sdhci_tasklet_finish, (unsigned long)host);
1076 setup_timer(&host->timer, sdhci_timeout_timer, (int)host);
1078 ret = request_irq(host->irq, sdhci_irq, SA_SHIRQ,
1079 host->slot_descr, host);
1080 if (ret)
1081 goto unmap;
1083 sdhci_init(host);
1085 #ifdef CONFIG_MMC_DEBUG
1086 sdhci_dumpregs(host);
1087 #endif
1089 host->chip = chip;
1090 chip->hosts[slot] = host;
1092 mmc_add_host(mmc);
1094 printk(KERN_INFO "%s: SDHCI at 0x%08lx irq %d %s\n", mmc_hostname(mmc),
1095 host->addr, host->irq,
1096 (host->flags & SDHCI_USE_DMA)?"DMA":"PIO");
1098 return 0;
1100 unmap:
1101 tasklet_kill(&host->card_tasklet);
1102 tasklet_kill(&host->finish_tasklet);
1104 iounmap(host->ioaddr);
1105 release:
1106 pci_release_region(pdev, host->bar);
1107 free:
1108 mmc_free_host(mmc);
1110 return ret;
1113 static void sdhci_remove_slot(struct pci_dev *pdev, int slot)
1115 struct sdhci_chip *chip;
1116 struct mmc_host *mmc;
1117 struct sdhci_host *host;
1119 chip = pci_get_drvdata(pdev);
1120 host = chip->hosts[slot];
1121 mmc = host->mmc;
1123 chip->hosts[slot] = NULL;
1125 mmc_remove_host(mmc);
1127 sdhci_reset(host, SDHCI_RESET_ALL);
1129 free_irq(host->irq, host);
1131 del_timer_sync(&host->timer);
1133 tasklet_kill(&host->card_tasklet);
1134 tasklet_kill(&host->finish_tasklet);
1136 iounmap(host->ioaddr);
1138 pci_release_region(pdev, host->bar);
1140 mmc_free_host(mmc);
1143 static int __devinit sdhci_probe(struct pci_dev *pdev,
1144 const struct pci_device_id *ent)
1146 int ret, i;
1147 u8 slots;
1148 struct sdhci_chip *chip;
1150 BUG_ON(pdev == NULL);
1151 BUG_ON(ent == NULL);
1153 DBG("found at %s\n", pci_name(pdev));
1155 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
1156 if (ret)
1157 return ret;
1159 slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
1160 DBG("found %d slot(s)\n", slots);
1161 if (slots == 0)
1162 return -ENODEV;
1164 ret = pci_enable_device(pdev);
1165 if (ret)
1166 return ret;
1168 chip = kzalloc(sizeof(struct sdhci_chip) +
1169 sizeof(struct sdhci_host*) * slots, GFP_KERNEL);
1170 if (!chip) {
1171 ret = -ENOMEM;
1172 goto err;
1175 chip->pdev = pdev;
1177 chip->num_slots = slots;
1178 pci_set_drvdata(pdev, chip);
1180 for (i = 0;i < slots;i++) {
1181 ret = sdhci_probe_slot(pdev, i);
1182 if (ret) {
1183 for (i--;i >= 0;i--)
1184 sdhci_remove_slot(pdev, i);
1185 goto free;
1189 return 0;
1191 free:
1192 pci_set_drvdata(pdev, NULL);
1193 kfree(chip);
1195 err:
1196 pci_disable_device(pdev);
1197 return ret;
1200 static void __devexit sdhci_remove(struct pci_dev *pdev)
1202 int i;
1203 struct sdhci_chip *chip;
1205 chip = pci_get_drvdata(pdev);
1207 if (chip) {
1208 for (i = 0;i < chip->num_slots;i++)
1209 sdhci_remove_slot(pdev, i);
1211 pci_set_drvdata(pdev, NULL);
1213 kfree(chip);
1216 pci_disable_device(pdev);
1219 static struct pci_driver sdhci_driver = {
1220 .name = DRIVER_NAME,
1221 .id_table = pci_ids,
1222 .probe = sdhci_probe,
1223 .remove = __devexit_p(sdhci_remove),
1224 .suspend = sdhci_suspend,
1225 .resume = sdhci_resume,
1228 /*****************************************************************************\
1230 * Driver init/exit *
1232 \*****************************************************************************/
1234 static int __init sdhci_drv_init(void)
1236 printk(KERN_INFO DRIVER_NAME
1237 ": Secure Digital Host Controller Interface driver, "
1238 DRIVER_VERSION "\n");
1239 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
1241 return pci_register_driver(&sdhci_driver);
1244 static void __exit sdhci_drv_exit(void)
1246 DBG("Exiting\n");
1248 pci_unregister_driver(&sdhci_driver);
1251 module_init(sdhci_drv_init);
1252 module_exit(sdhci_drv_exit);
1254 MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
1255 MODULE_DESCRIPTION("Secure Digital Host Controller Interface driver");
1256 MODULE_VERSION(DRIVER_VERSION);
1257 MODULE_LICENSE("GPL");