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.
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>
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 /*****************************************************************************\
91 * Low level functions *
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
) {
106 static void sdhci_init(struct sdhci_host
*host
)
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
)
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
)
134 ctrl
= readb(host
->ioaddr
+ SDHCI_HOST_CONTROL
);
135 ctrl
&= ~SDHCI_CTRL_LED
;
136 writeb(ctrl
, host
->ioaddr
+ SDHCI_HOST_CONTROL
);
139 /*****************************************************************************\
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.
167 if (host
->num_sg
> 0) {
169 host
->remain
= host
->cur_sg
->length
;
175 static void sdhci_transfer_pio(struct sdhci_host
*host
)
180 unsigned long max_jiffies
;
184 if (host
->num_sg
== 0)
188 if (host
->data
->flags
& MMC_DATA_READ
)
189 mask
= SDHCI_DATA_AVAILABLE
;
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
);
212 if (!(readl(host
->ioaddr
+ SDHCI_PRESENT_STATE
) & mask
))
215 size
= min(host
->size
, host
->remain
);
218 if (host
->data
->flags
& MMC_DATA_READ
)
219 *(u32
*)buffer
= readl(host
->ioaddr
+ SDHCI_BUFFER
);
221 writel(*(u32
*)buffer
, host
->ioaddr
+ SDHCI_BUFFER
);
223 } else if (size
>= 2) {
224 if (host
->data
->flags
& MMC_DATA_READ
)
225 *(u16
*)buffer
= readw(host
->ioaddr
+ SDHCI_BUFFER
);
227 writew(*(u16
*)buffer
, host
->ioaddr
+ SDHCI_BUFFER
);
230 if (host
->data
->flags
& MMC_DATA_READ
)
231 *(u8
*)buffer
= readb(host
->ioaddr
+ SDHCI_BUFFER
);
233 writeb(*(u8
*)buffer
, host
->ioaddr
+ SDHCI_BUFFER
);
238 host
->offset
+= size
;
239 host
->remain
-= 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
);
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
)
266 writew(0, host
->ioaddr
+ SDHCI_TRANSFER_MODE
);
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
) {
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
);
295 writel(sg_dma_address(data
->sg
), host
->ioaddr
+ SDHCI_DMA_ADDRESS
);
297 host
->size
= (1 << data
->blksz_bits
) * data
->blocks
;
299 host
->cur_sg
= data
->sg
;
300 host
->num_sg
= data
->sg_len
;
303 host
->remain
= host
->cur_sg
->length
;
307 static void sdhci_finish_data(struct sdhci_host
*host
)
309 struct mmc_data
*data
;
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
);
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
))
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
);
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
);
368 tasklet_schedule(&host
->finish_tasklet
);
371 static void sdhci_send_command(struct sdhci_host
*host
, struct mmc_command
*cmd
)
375 unsigned long max_jiffies
;
379 DBG("Sending cmd (%x)\n", cmd
->opcode
);
382 max_jiffies
= jiffies
+ (HZ
+ 99)/100;
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
);
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
);
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
);
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
;
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
;
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
)
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;
446 host
->cmd
->resp
[i
] |=
448 SDHCI_RESPONSE
+ (3-i
)*4-1);
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
) {
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
);
484 tasklet_schedule(&host
->finish_tasklet
);
489 static void sdhci_set_clock(struct sdhci_host
*host
, unsigned int clock
)
493 unsigned long max_jiffies
;
495 if (clock
== host
->clock
)
498 writew(0, host
->ioaddr
+ SDHCI_CLOCK_CONTROL
);
503 for (div
= 1;div
< 256;div
*= 2) {
504 if ((host
->max_clk
/ div
) <= clock
)
509 clk
= div
<< SDHCI_DIVIDER_SHIFT
;
510 clk
|= SDHCI_CLOCK_INT_EN
;
511 writew(clk
, host
->ioaddr
+ SDHCI_CLOCK_CONTROL
);
514 max_jiffies
= jiffies
+ (HZ
+ 99)/100;
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
);
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
);
533 /*****************************************************************************\
537 \*****************************************************************************/
539 static void sdhci_request(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
541 struct sdhci_host
*host
;
544 host
= mmc_priv(mmc
);
546 spin_lock_irqsave(&host
->lock
, flags
);
548 WARN_ON(host
->mrq
!= NULL
);
550 sdhci_activate_led(host
);
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
);
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
;
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
);
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
);
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
;
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
;
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 /*****************************************************************************\
628 \*****************************************************************************/
630 static void sdhci_tasklet_card(unsigned long param
)
632 struct sdhci_host
*host
;
635 host
= (struct sdhci_host
*)param
;
637 spin_lock_irqsave(&host
->lock
, flags
);
639 if (!(readl(host
->ioaddr
+ SDHCI_PRESENT_STATE
) & SDHCI_CARD_PRESENT
)) {
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
;
663 struct mmc_request
*mrq
;
665 host
= (struct sdhci_host
*)param
;
667 spin_lock_irqsave(&host
->lock
, flags
);
669 del_timer(&host
->timer
);
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
);
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
;
702 host
= (struct sdhci_host
*)data
;
704 spin_lock_irqsave(&host
->lock
, flags
);
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
);
713 host
->data
->error
= MMC_ERR_TIMEOUT
;
714 sdhci_finish_data(host
);
717 host
->cmd
->error
= MMC_ERR_TIMEOUT
;
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);
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
);
748 if (intmask
& SDHCI_INT_RESPONSE
)
749 sdhci_finish_command(host
);
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
;
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);
770 * A data end interrupt is sent together with the response
771 * for the stop command.
773 if (intmask
& SDHCI_INT_DATA_END
)
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
);
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
);
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
)
807 struct sdhci_host
* host
= dev_id
;
810 spin_lock(&host
->lock
);
812 intmask
= readl(host
->ioaddr
+ SDHCI_INT_STATUS
);
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
);
864 writel(intmask
, host
->ioaddr
+ SDHCI_INT_STATUS
);
866 result
= IRQ_HANDLED
;
869 spin_unlock(&host
->lock
);
874 /*****************************************************************************\
878 \*****************************************************************************/
882 static int sdhci_suspend (struct pci_dev
*pdev
, pm_message_t state
)
884 struct sdhci_chip
*chip
;
887 chip
= pci_get_drvdata(pdev
);
891 DBG("Suspending...\n");
893 for (i
= 0;i
< chip
->num_slots
;i
++) {
896 ret
= mmc_suspend_host(chip
->hosts
[i
]->mmc
, state
);
899 mmc_resume_host(chip
->hosts
[i
]->mmc
);
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
));
912 static int sdhci_resume (struct pci_dev
*pdev
)
914 struct sdhci_chip
*chip
;
917 chip
= pci_get_drvdata(pdev
);
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
++) {
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
);
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
)
957 struct sdhci_chip
*chip
;
958 struct mmc_host
*mmc
;
959 struct sdhci_host
*host
;
964 chip
= pci_get_drvdata(pdev
);
967 ret
= pci_read_config_byte(pdev
, PCI_SLOT_INFO
, &first_bar
);
971 first_bar
&= PCI_SLOT_INFO_FIRST_BAR_MASK
;
974 printk(KERN_ERR DRIVER_NAME
": Invalid first BAR. Aborting.\n");
978 if (!(pci_resource_flags(pdev
, first_bar
+ slot
) & IORESOURCE_MEM
)) {
979 printk(KERN_ERR DRIVER_NAME
": BAR is not iomem. Aborting.\n");
983 if (pci_resource_len(pdev
, first_bar
+ slot
) != 0x100) {
984 printk(KERN_ERR DRIVER_NAME
": Invalid iomem size. Aborting.\n");
988 mmc
= mmc_alloc_host(sizeof(struct sdhci_host
), &pdev
->dev
);
992 host
= mmc_priv(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
);
1008 host
->ioaddr
= ioremap_nocache(host
->addr
,
1009 pci_resource_len(pdev
, host
->bar
));
1010 if (!host
->ioaddr
) {
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 */
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;
1053 mmc
->max_hw_segs
= 16;
1054 mmc
->max_phys_segs
= 16;
1057 * Maximum number of sectors in one transfer. Limited by sector
1060 mmc
->max_sectors
= 0x3FFF;
1063 * Maximum segment size. Could be one segment with the maximum number
1066 mmc
->max_seg_size
= mmc
->max_sectors
* 512;
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
);
1085 #ifdef CONFIG_MMC_DEBUG
1086 sdhci_dumpregs(host
);
1090 chip
->hosts
[slot
] = host
;
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");
1101 tasklet_kill(&host
->card_tasklet
);
1102 tasklet_kill(&host
->finish_tasklet
);
1104 iounmap(host
->ioaddr
);
1106 pci_release_region(pdev
, host
->bar
);
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
];
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
);
1143 static int __devinit
sdhci_probe(struct pci_dev
*pdev
,
1144 const struct pci_device_id
*ent
)
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
);
1159 slots
= PCI_SLOT_INFO_SLOTS(slots
) + 1;
1160 DBG("found %d slot(s)\n", slots
);
1164 ret
= pci_enable_device(pdev
);
1168 chip
= kzalloc(sizeof(struct sdhci_chip
) +
1169 sizeof(struct sdhci_host
*) * slots
, GFP_KERNEL
);
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
);
1183 for (i
--;i
>= 0;i
--)
1184 sdhci_remove_slot(pdev
, i
);
1192 pci_set_drvdata(pdev
, NULL
);
1196 pci_disable_device(pdev
);
1200 static void __devexit
sdhci_remove(struct pci_dev
*pdev
)
1203 struct sdhci_chip
*chip
;
1205 chip
= pci_get_drvdata(pdev
);
1208 for (i
= 0;i
< chip
->num_slots
;i
++)
1209 sdhci_remove_slot(pdev
, i
);
1211 pci_set_drvdata(pdev
, NULL
);
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)
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");