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 as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
12 #include <linux/delay.h>
13 #include <linux/highmem.h>
14 #include <linux/pci.h>
15 #include <linux/dma-mapping.h>
17 #include <linux/mmc/host.h>
18 #include <linux/mmc/protocol.h>
20 #include <asm/scatterlist.h>
24 #define DRIVER_NAME "sdhci"
26 #define DBG(f, x...) \
27 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
29 static unsigned int debug_nodma
= 0;
30 static unsigned int debug_forcedma
= 0;
31 static unsigned int debug_quirks
= 0;
33 #define SDHCI_QUIRK_CLOCK_BEFORE_RESET (1<<0)
34 #define SDHCI_QUIRK_FORCE_DMA (1<<1)
35 /* Controller doesn't like some resets when there is no card inserted. */
36 #define SDHCI_QUIRK_NO_CARD_NO_RESET (1<<2)
37 #define SDHCI_QUIRK_SINGLE_POWER_WRITE (1<<3)
39 static const struct pci_device_id pci_ids
[] __devinitdata
= {
41 .vendor
= PCI_VENDOR_ID_RICOH
,
42 .device
= PCI_DEVICE_ID_RICOH_R5C822
,
43 .subvendor
= PCI_VENDOR_ID_IBM
,
44 .subdevice
= PCI_ANY_ID
,
45 .driver_data
= SDHCI_QUIRK_CLOCK_BEFORE_RESET
|
46 SDHCI_QUIRK_FORCE_DMA
,
50 .vendor
= PCI_VENDOR_ID_RICOH
,
51 .device
= PCI_DEVICE_ID_RICOH_R5C822
,
52 .subvendor
= PCI_ANY_ID
,
53 .subdevice
= PCI_ANY_ID
,
54 .driver_data
= SDHCI_QUIRK_FORCE_DMA
|
55 SDHCI_QUIRK_NO_CARD_NO_RESET
,
59 .vendor
= PCI_VENDOR_ID_TI
,
60 .device
= PCI_DEVICE_ID_TI_XX21_XX11_SD
,
61 .subvendor
= PCI_ANY_ID
,
62 .subdevice
= PCI_ANY_ID
,
63 .driver_data
= SDHCI_QUIRK_FORCE_DMA
,
67 .vendor
= PCI_VENDOR_ID_ENE
,
68 .device
= PCI_DEVICE_ID_ENE_CB712_SD
,
69 .subvendor
= PCI_ANY_ID
,
70 .subdevice
= PCI_ANY_ID
,
71 .driver_data
= SDHCI_QUIRK_SINGLE_POWER_WRITE
,
74 { /* Generic SD host controller */
75 PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI
<< 8), 0xFFFF00)
78 { /* end: all zeroes */ },
81 MODULE_DEVICE_TABLE(pci
, pci_ids
);
83 static void sdhci_prepare_data(struct sdhci_host
*, struct mmc_data
*);
84 static void sdhci_finish_data(struct sdhci_host
*);
86 static void sdhci_send_command(struct sdhci_host
*, struct mmc_command
*);
87 static void sdhci_finish_command(struct sdhci_host
*);
89 static void sdhci_dumpregs(struct sdhci_host
*host
)
91 printk(KERN_DEBUG DRIVER_NAME
": ============== REGISTER DUMP ==============\n");
93 printk(KERN_DEBUG DRIVER_NAME
": Sys addr: 0x%08x | Version: 0x%08x\n",
94 readl(host
->ioaddr
+ SDHCI_DMA_ADDRESS
),
95 readw(host
->ioaddr
+ SDHCI_HOST_VERSION
));
96 printk(KERN_DEBUG DRIVER_NAME
": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
97 readw(host
->ioaddr
+ SDHCI_BLOCK_SIZE
),
98 readw(host
->ioaddr
+ SDHCI_BLOCK_COUNT
));
99 printk(KERN_DEBUG DRIVER_NAME
": Argument: 0x%08x | Trn mode: 0x%08x\n",
100 readl(host
->ioaddr
+ SDHCI_ARGUMENT
),
101 readw(host
->ioaddr
+ SDHCI_TRANSFER_MODE
));
102 printk(KERN_DEBUG DRIVER_NAME
": Present: 0x%08x | Host ctl: 0x%08x\n",
103 readl(host
->ioaddr
+ SDHCI_PRESENT_STATE
),
104 readb(host
->ioaddr
+ SDHCI_HOST_CONTROL
));
105 printk(KERN_DEBUG DRIVER_NAME
": Power: 0x%08x | Blk gap: 0x%08x\n",
106 readb(host
->ioaddr
+ SDHCI_POWER_CONTROL
),
107 readb(host
->ioaddr
+ SDHCI_BLOCK_GAP_CONTROL
));
108 printk(KERN_DEBUG DRIVER_NAME
": Wake-up: 0x%08x | Clock: 0x%08x\n",
109 readb(host
->ioaddr
+ SDHCI_WALK_UP_CONTROL
),
110 readw(host
->ioaddr
+ SDHCI_CLOCK_CONTROL
));
111 printk(KERN_DEBUG DRIVER_NAME
": Timeout: 0x%08x | Int stat: 0x%08x\n",
112 readb(host
->ioaddr
+ SDHCI_TIMEOUT_CONTROL
),
113 readl(host
->ioaddr
+ SDHCI_INT_STATUS
));
114 printk(KERN_DEBUG DRIVER_NAME
": Int enab: 0x%08x | Sig enab: 0x%08x\n",
115 readl(host
->ioaddr
+ SDHCI_INT_ENABLE
),
116 readl(host
->ioaddr
+ SDHCI_SIGNAL_ENABLE
));
117 printk(KERN_DEBUG DRIVER_NAME
": AC12 err: 0x%08x | Slot int: 0x%08x\n",
118 readw(host
->ioaddr
+ SDHCI_ACMD12_ERR
),
119 readw(host
->ioaddr
+ SDHCI_SLOT_INT_STATUS
));
120 printk(KERN_DEBUG DRIVER_NAME
": Caps: 0x%08x | Max curr: 0x%08x\n",
121 readl(host
->ioaddr
+ SDHCI_CAPABILITIES
),
122 readl(host
->ioaddr
+ SDHCI_MAX_CURRENT
));
124 printk(KERN_DEBUG DRIVER_NAME
": ===========================================\n");
127 /*****************************************************************************\
129 * Low level functions *
131 \*****************************************************************************/
133 static void sdhci_reset(struct sdhci_host
*host
, u8 mask
)
135 unsigned long timeout
;
137 if (host
->chip
->quirks
& SDHCI_QUIRK_NO_CARD_NO_RESET
) {
138 if (!(readl(host
->ioaddr
+ SDHCI_PRESENT_STATE
) &
143 writeb(mask
, host
->ioaddr
+ SDHCI_SOFTWARE_RESET
);
145 if (mask
& SDHCI_RESET_ALL
)
148 /* Wait max 100 ms */
151 /* hw clears the bit when it's done */
152 while (readb(host
->ioaddr
+ SDHCI_SOFTWARE_RESET
) & mask
) {
154 printk(KERN_ERR
"%s: Reset 0x%x never completed.\n",
155 mmc_hostname(host
->mmc
), (int)mask
);
156 sdhci_dumpregs(host
);
164 static void sdhci_init(struct sdhci_host
*host
)
168 sdhci_reset(host
, SDHCI_RESET_ALL
);
170 intmask
= SDHCI_INT_BUS_POWER
| SDHCI_INT_DATA_END_BIT
|
171 SDHCI_INT_DATA_CRC
| SDHCI_INT_DATA_TIMEOUT
| SDHCI_INT_INDEX
|
172 SDHCI_INT_END_BIT
| SDHCI_INT_CRC
| SDHCI_INT_TIMEOUT
|
173 SDHCI_INT_CARD_REMOVE
| SDHCI_INT_CARD_INSERT
|
174 SDHCI_INT_DATA_AVAIL
| SDHCI_INT_SPACE_AVAIL
|
175 SDHCI_INT_DMA_END
| SDHCI_INT_DATA_END
| SDHCI_INT_RESPONSE
;
177 writel(intmask
, host
->ioaddr
+ SDHCI_INT_ENABLE
);
178 writel(intmask
, host
->ioaddr
+ SDHCI_SIGNAL_ENABLE
);
181 static void sdhci_activate_led(struct sdhci_host
*host
)
185 ctrl
= readb(host
->ioaddr
+ SDHCI_HOST_CONTROL
);
186 ctrl
|= SDHCI_CTRL_LED
;
187 writeb(ctrl
, host
->ioaddr
+ SDHCI_HOST_CONTROL
);
190 static void sdhci_deactivate_led(struct sdhci_host
*host
)
194 ctrl
= readb(host
->ioaddr
+ SDHCI_HOST_CONTROL
);
195 ctrl
&= ~SDHCI_CTRL_LED
;
196 writeb(ctrl
, host
->ioaddr
+ SDHCI_HOST_CONTROL
);
199 /*****************************************************************************\
203 \*****************************************************************************/
205 static inline char* sdhci_sg_to_buffer(struct sdhci_host
* host
)
207 return page_address(host
->cur_sg
->page
) + host
->cur_sg
->offset
;
210 static inline int sdhci_next_sg(struct sdhci_host
* host
)
213 * Skip to next SG entry.
221 if (host
->num_sg
> 0) {
223 host
->remain
= host
->cur_sg
->length
;
229 static void sdhci_read_block_pio(struct sdhci_host
*host
)
231 int blksize
, chunk_remain
;
236 DBG("PIO reading\n");
238 blksize
= host
->data
->blksz
;
242 buffer
= sdhci_sg_to_buffer(host
) + host
->offset
;
245 if (chunk_remain
== 0) {
246 data
= readl(host
->ioaddr
+ SDHCI_BUFFER
);
247 chunk_remain
= min(blksize
, 4);
250 size
= min(host
->size
, host
->remain
);
251 size
= min(size
, chunk_remain
);
253 chunk_remain
-= size
;
255 host
->offset
+= size
;
256 host
->remain
-= size
;
259 *buffer
= data
& 0xFF;
265 if (host
->remain
== 0) {
266 if (sdhci_next_sg(host
) == 0) {
267 BUG_ON(blksize
!= 0);
270 buffer
= sdhci_sg_to_buffer(host
);
275 static void sdhci_write_block_pio(struct sdhci_host
*host
)
277 int blksize
, chunk_remain
;
282 DBG("PIO writing\n");
284 blksize
= host
->data
->blksz
;
289 buffer
= sdhci_sg_to_buffer(host
) + host
->offset
;
292 size
= min(host
->size
, host
->remain
);
293 size
= min(size
, chunk_remain
);
295 chunk_remain
-= size
;
297 host
->offset
+= size
;
298 host
->remain
-= size
;
302 data
|= (u32
)*buffer
<< 24;
307 if (chunk_remain
== 0) {
308 writel(data
, host
->ioaddr
+ SDHCI_BUFFER
);
309 chunk_remain
= min(blksize
, 4);
312 if (host
->remain
== 0) {
313 if (sdhci_next_sg(host
) == 0) {
314 BUG_ON(blksize
!= 0);
317 buffer
= sdhci_sg_to_buffer(host
);
322 static void sdhci_transfer_pio(struct sdhci_host
*host
)
331 if (host
->data
->flags
& MMC_DATA_READ
)
332 mask
= SDHCI_DATA_AVAILABLE
;
334 mask
= SDHCI_SPACE_AVAILABLE
;
336 while (readl(host
->ioaddr
+ SDHCI_PRESENT_STATE
) & mask
) {
337 if (host
->data
->flags
& MMC_DATA_READ
)
338 sdhci_read_block_pio(host
);
340 sdhci_write_block_pio(host
);
345 BUG_ON(host
->num_sg
== 0);
348 DBG("PIO transfer complete.\n");
351 static void sdhci_prepare_data(struct sdhci_host
*host
, struct mmc_data
*data
)
354 unsigned target_timeout
, current_timeout
;
361 DBG("blksz %04x blks %04x flags %08x\n",
362 data
->blksz
, data
->blocks
, data
->flags
);
363 DBG("tsac %d ms nsac %d clk\n",
364 data
->timeout_ns
/ 1000000, data
->timeout_clks
);
367 BUG_ON(data
->blksz
* data
->blocks
> 524288);
368 BUG_ON(data
->blksz
> host
->mmc
->max_blk_size
);
369 BUG_ON(data
->blocks
> 65535);
372 target_timeout
= data
->timeout_ns
/ 1000 +
373 data
->timeout_clks
/ host
->clock
;
376 * Figure out needed cycles.
377 * We do this in steps in order to fit inside a 32 bit int.
378 * The first step is the minimum timeout, which will have a
379 * minimum resolution of 6 bits:
380 * (1) 2^13*1000 > 2^22,
381 * (2) host->timeout_clk < 2^16
386 current_timeout
= (1 << 13) * 1000 / host
->timeout_clk
;
387 while (current_timeout
< target_timeout
) {
389 current_timeout
<<= 1;
395 printk(KERN_WARNING
"%s: Too large timeout requested!\n",
396 mmc_hostname(host
->mmc
));
400 writeb(count
, host
->ioaddr
+ SDHCI_TIMEOUT_CONTROL
);
402 if (host
->flags
& SDHCI_USE_DMA
) {
405 count
= pci_map_sg(host
->chip
->pdev
, data
->sg
, data
->sg_len
,
406 (data
->flags
& MMC_DATA_READ
)?PCI_DMA_FROMDEVICE
:PCI_DMA_TODEVICE
);
409 writel(sg_dma_address(data
->sg
), host
->ioaddr
+ SDHCI_DMA_ADDRESS
);
411 host
->size
= data
->blksz
* data
->blocks
;
413 host
->cur_sg
= data
->sg
;
414 host
->num_sg
= data
->sg_len
;
417 host
->remain
= host
->cur_sg
->length
;
420 /* We do not handle DMA boundaries, so set it to max (512 KiB) */
421 writew(SDHCI_MAKE_BLKSZ(7, data
->blksz
),
422 host
->ioaddr
+ SDHCI_BLOCK_SIZE
);
423 writew(data
->blocks
, host
->ioaddr
+ SDHCI_BLOCK_COUNT
);
426 static void sdhci_set_transfer_mode(struct sdhci_host
*host
,
427 struct mmc_data
*data
)
436 mode
= SDHCI_TRNS_BLK_CNT_EN
;
437 if (data
->blocks
> 1)
438 mode
|= SDHCI_TRNS_MULTI
;
439 if (data
->flags
& MMC_DATA_READ
)
440 mode
|= SDHCI_TRNS_READ
;
441 if (host
->flags
& SDHCI_USE_DMA
)
442 mode
|= SDHCI_TRNS_DMA
;
444 writew(mode
, host
->ioaddr
+ SDHCI_TRANSFER_MODE
);
447 static void sdhci_finish_data(struct sdhci_host
*host
)
449 struct mmc_data
*data
;
457 if (host
->flags
& SDHCI_USE_DMA
) {
458 pci_unmap_sg(host
->chip
->pdev
, data
->sg
, data
->sg_len
,
459 (data
->flags
& MMC_DATA_READ
)?PCI_DMA_FROMDEVICE
:PCI_DMA_TODEVICE
);
463 * Controller doesn't count down when in single block mode.
465 if ((data
->blocks
== 1) && (data
->error
== MMC_ERR_NONE
))
468 blocks
= readw(host
->ioaddr
+ SDHCI_BLOCK_COUNT
);
469 data
->bytes_xfered
= data
->blksz
* (data
->blocks
- blocks
);
471 if ((data
->error
== MMC_ERR_NONE
) && blocks
) {
472 printk(KERN_ERR
"%s: Controller signalled completion even "
473 "though there were blocks left.\n",
474 mmc_hostname(host
->mmc
));
475 data
->error
= MMC_ERR_FAILED
;
476 } else if (host
->size
!= 0) {
477 printk(KERN_ERR
"%s: %d bytes were left untransferred.\n",
478 mmc_hostname(host
->mmc
), host
->size
);
479 data
->error
= MMC_ERR_FAILED
;
482 DBG("Ending data transfer (%d bytes)\n", data
->bytes_xfered
);
486 * The controller needs a reset of internal state machines
487 * upon error conditions.
489 if (data
->error
!= MMC_ERR_NONE
) {
490 sdhci_reset(host
, SDHCI_RESET_CMD
);
491 sdhci_reset(host
, SDHCI_RESET_DATA
);
494 sdhci_send_command(host
, data
->stop
);
496 tasklet_schedule(&host
->finish_tasklet
);
499 static void sdhci_send_command(struct sdhci_host
*host
, struct mmc_command
*cmd
)
503 unsigned long timeout
;
507 DBG("Sending cmd (%x)\n", cmd
->opcode
);
512 mask
= SDHCI_CMD_INHIBIT
;
513 if ((cmd
->data
!= NULL
) || (cmd
->flags
& MMC_RSP_BUSY
))
514 mask
|= SDHCI_DATA_INHIBIT
;
516 /* We shouldn't wait for data inihibit for stop commands, even
517 though they might use busy signaling */
518 if (host
->mrq
->data
&& (cmd
== host
->mrq
->data
->stop
))
519 mask
&= ~SDHCI_DATA_INHIBIT
;
521 while (readl(host
->ioaddr
+ SDHCI_PRESENT_STATE
) & mask
) {
523 printk(KERN_ERR
"%s: Controller never released "
524 "inhibit bit(s).\n", mmc_hostname(host
->mmc
));
525 sdhci_dumpregs(host
);
526 cmd
->error
= MMC_ERR_FAILED
;
527 tasklet_schedule(&host
->finish_tasklet
);
534 mod_timer(&host
->timer
, jiffies
+ 10 * HZ
);
538 sdhci_prepare_data(host
, cmd
->data
);
540 writel(cmd
->arg
, host
->ioaddr
+ SDHCI_ARGUMENT
);
542 sdhci_set_transfer_mode(host
, cmd
->data
);
544 if ((cmd
->flags
& MMC_RSP_136
) && (cmd
->flags
& MMC_RSP_BUSY
)) {
545 printk(KERN_ERR
"%s: Unsupported response type!\n",
546 mmc_hostname(host
->mmc
));
547 cmd
->error
= MMC_ERR_INVALID
;
548 tasklet_schedule(&host
->finish_tasklet
);
552 if (!(cmd
->flags
& MMC_RSP_PRESENT
))
553 flags
= SDHCI_CMD_RESP_NONE
;
554 else if (cmd
->flags
& MMC_RSP_136
)
555 flags
= SDHCI_CMD_RESP_LONG
;
556 else if (cmd
->flags
& MMC_RSP_BUSY
)
557 flags
= SDHCI_CMD_RESP_SHORT_BUSY
;
559 flags
= SDHCI_CMD_RESP_SHORT
;
561 if (cmd
->flags
& MMC_RSP_CRC
)
562 flags
|= SDHCI_CMD_CRC
;
563 if (cmd
->flags
& MMC_RSP_OPCODE
)
564 flags
|= SDHCI_CMD_INDEX
;
566 flags
|= SDHCI_CMD_DATA
;
568 writew(SDHCI_MAKE_CMD(cmd
->opcode
, flags
),
569 host
->ioaddr
+ SDHCI_COMMAND
);
572 static void sdhci_finish_command(struct sdhci_host
*host
)
576 BUG_ON(host
->cmd
== NULL
);
578 if (host
->cmd
->flags
& MMC_RSP_PRESENT
) {
579 if (host
->cmd
->flags
& MMC_RSP_136
) {
580 /* CRC is stripped so we need to do some shifting. */
581 for (i
= 0;i
< 4;i
++) {
582 host
->cmd
->resp
[i
] = readl(host
->ioaddr
+
583 SDHCI_RESPONSE
+ (3-i
)*4) << 8;
585 host
->cmd
->resp
[i
] |=
587 SDHCI_RESPONSE
+ (3-i
)*4-1);
590 host
->cmd
->resp
[0] = readl(host
->ioaddr
+ SDHCI_RESPONSE
);
594 host
->cmd
->error
= MMC_ERR_NONE
;
596 DBG("Ending cmd (%x)\n", host
->cmd
->opcode
);
599 host
->data
= host
->cmd
->data
;
601 tasklet_schedule(&host
->finish_tasklet
);
606 static void sdhci_set_clock(struct sdhci_host
*host
, unsigned int clock
)
611 unsigned long timeout
;
613 if (clock
== host
->clock
)
616 writew(0, host
->ioaddr
+ SDHCI_CLOCK_CONTROL
);
618 ctrl
= readb(host
->ioaddr
+ SDHCI_HOST_CONTROL
);
619 if (clock
> 25000000)
620 ctrl
|= SDHCI_CTRL_HISPD
;
622 ctrl
&= ~SDHCI_CTRL_HISPD
;
623 writeb(ctrl
, host
->ioaddr
+ SDHCI_HOST_CONTROL
);
628 for (div
= 1;div
< 256;div
*= 2) {
629 if ((host
->max_clk
/ div
) <= clock
)
634 clk
= div
<< SDHCI_DIVIDER_SHIFT
;
635 clk
|= SDHCI_CLOCK_INT_EN
;
636 writew(clk
, host
->ioaddr
+ SDHCI_CLOCK_CONTROL
);
640 while (!((clk
= readw(host
->ioaddr
+ SDHCI_CLOCK_CONTROL
))
641 & SDHCI_CLOCK_INT_STABLE
)) {
643 printk(KERN_ERR
"%s: Internal clock never "
644 "stabilised.\n", mmc_hostname(host
->mmc
));
645 sdhci_dumpregs(host
);
652 clk
|= SDHCI_CLOCK_CARD_EN
;
653 writew(clk
, host
->ioaddr
+ SDHCI_CLOCK_CONTROL
);
659 static void sdhci_set_power(struct sdhci_host
*host
, unsigned short power
)
663 if (host
->power
== power
)
666 if (power
== (unsigned short)-1) {
667 writeb(0, host
->ioaddr
+ SDHCI_POWER_CONTROL
);
672 * Spec says that we should clear the power reg before setting
673 * a new value. Some controllers don't seem to like this though.
675 if (!(host
->chip
->quirks
& SDHCI_QUIRK_SINGLE_POWER_WRITE
))
676 writeb(0, host
->ioaddr
+ SDHCI_POWER_CONTROL
);
678 pwr
= SDHCI_POWER_ON
;
684 pwr
|= SDHCI_POWER_180
;
689 pwr
|= SDHCI_POWER_300
;
694 pwr
|= SDHCI_POWER_330
;
700 writeb(pwr
, host
->ioaddr
+ SDHCI_POWER_CONTROL
);
706 /*****************************************************************************\
710 \*****************************************************************************/
712 static void sdhci_request(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
714 struct sdhci_host
*host
;
717 host
= mmc_priv(mmc
);
719 spin_lock_irqsave(&host
->lock
, flags
);
721 WARN_ON(host
->mrq
!= NULL
);
723 sdhci_activate_led(host
);
727 if (!(readl(host
->ioaddr
+ SDHCI_PRESENT_STATE
) & SDHCI_CARD_PRESENT
)) {
728 host
->mrq
->cmd
->error
= MMC_ERR_TIMEOUT
;
729 tasklet_schedule(&host
->finish_tasklet
);
731 sdhci_send_command(host
, mrq
->cmd
);
734 spin_unlock_irqrestore(&host
->lock
, flags
);
737 static void sdhci_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
739 struct sdhci_host
*host
;
743 host
= mmc_priv(mmc
);
745 spin_lock_irqsave(&host
->lock
, flags
);
748 * Reset the chip on each power off.
749 * Should clear out any weird states.
751 if (ios
->power_mode
== MMC_POWER_OFF
) {
752 writel(0, host
->ioaddr
+ SDHCI_SIGNAL_ENABLE
);
756 sdhci_set_clock(host
, ios
->clock
);
758 if (ios
->power_mode
== MMC_POWER_OFF
)
759 sdhci_set_power(host
, -1);
761 sdhci_set_power(host
, ios
->vdd
);
763 ctrl
= readb(host
->ioaddr
+ SDHCI_HOST_CONTROL
);
764 if (ios
->bus_width
== MMC_BUS_WIDTH_4
)
765 ctrl
|= SDHCI_CTRL_4BITBUS
;
767 ctrl
&= ~SDHCI_CTRL_4BITBUS
;
768 writeb(ctrl
, host
->ioaddr
+ SDHCI_HOST_CONTROL
);
771 spin_unlock_irqrestore(&host
->lock
, flags
);
774 static int sdhci_get_ro(struct mmc_host
*mmc
)
776 struct sdhci_host
*host
;
780 host
= mmc_priv(mmc
);
782 spin_lock_irqsave(&host
->lock
, flags
);
784 present
= readl(host
->ioaddr
+ SDHCI_PRESENT_STATE
);
786 spin_unlock_irqrestore(&host
->lock
, flags
);
788 return !(present
& SDHCI_WRITE_PROTECT
);
791 static const struct mmc_host_ops sdhci_ops
= {
792 .request
= sdhci_request
,
793 .set_ios
= sdhci_set_ios
,
794 .get_ro
= sdhci_get_ro
,
797 /*****************************************************************************\
801 \*****************************************************************************/
803 static void sdhci_tasklet_card(unsigned long param
)
805 struct sdhci_host
*host
;
808 host
= (struct sdhci_host
*)param
;
810 spin_lock_irqsave(&host
->lock
, flags
);
812 if (!(readl(host
->ioaddr
+ SDHCI_PRESENT_STATE
) & SDHCI_CARD_PRESENT
)) {
814 printk(KERN_ERR
"%s: Card removed during transfer!\n",
815 mmc_hostname(host
->mmc
));
816 printk(KERN_ERR
"%s: Resetting controller.\n",
817 mmc_hostname(host
->mmc
));
819 sdhci_reset(host
, SDHCI_RESET_CMD
);
820 sdhci_reset(host
, SDHCI_RESET_DATA
);
822 host
->mrq
->cmd
->error
= MMC_ERR_FAILED
;
823 tasklet_schedule(&host
->finish_tasklet
);
827 spin_unlock_irqrestore(&host
->lock
, flags
);
829 mmc_detect_change(host
->mmc
, msecs_to_jiffies(500));
832 static void sdhci_tasklet_finish(unsigned long param
)
834 struct sdhci_host
*host
;
836 struct mmc_request
*mrq
;
838 host
= (struct sdhci_host
*)param
;
840 spin_lock_irqsave(&host
->lock
, flags
);
842 del_timer(&host
->timer
);
846 DBG("Ending request, cmd (%x)\n", mrq
->cmd
->opcode
);
849 * The controller needs a reset of internal state machines
850 * upon error conditions.
852 if ((mrq
->cmd
->error
!= MMC_ERR_NONE
) ||
853 (mrq
->data
&& ((mrq
->data
->error
!= MMC_ERR_NONE
) ||
854 (mrq
->data
->stop
&& (mrq
->data
->stop
->error
!= MMC_ERR_NONE
))))) {
856 /* Some controllers need this kick or reset won't work here */
857 if (host
->chip
->quirks
& SDHCI_QUIRK_CLOCK_BEFORE_RESET
) {
860 /* This is to force an update */
863 sdhci_set_clock(host
, clock
);
866 /* Spec says we should do both at the same time, but Ricoh
867 controllers do not like that. */
868 sdhci_reset(host
, SDHCI_RESET_CMD
);
869 sdhci_reset(host
, SDHCI_RESET_DATA
);
876 sdhci_deactivate_led(host
);
879 spin_unlock_irqrestore(&host
->lock
, flags
);
881 mmc_request_done(host
->mmc
, mrq
);
884 static void sdhci_timeout_timer(unsigned long data
)
886 struct sdhci_host
*host
;
889 host
= (struct sdhci_host
*)data
;
891 spin_lock_irqsave(&host
->lock
, flags
);
894 printk(KERN_ERR
"%s: Timeout waiting for hardware "
895 "interrupt.\n", mmc_hostname(host
->mmc
));
896 sdhci_dumpregs(host
);
899 host
->data
->error
= MMC_ERR_TIMEOUT
;
900 sdhci_finish_data(host
);
903 host
->cmd
->error
= MMC_ERR_TIMEOUT
;
905 host
->mrq
->cmd
->error
= MMC_ERR_TIMEOUT
;
907 tasklet_schedule(&host
->finish_tasklet
);
912 spin_unlock_irqrestore(&host
->lock
, flags
);
915 /*****************************************************************************\
917 * Interrupt handling *
919 \*****************************************************************************/
921 static void sdhci_cmd_irq(struct sdhci_host
*host
, u32 intmask
)
923 BUG_ON(intmask
== 0);
926 printk(KERN_ERR
"%s: Got command interrupt even though no "
927 "command operation was in progress.\n",
928 mmc_hostname(host
->mmc
));
929 sdhci_dumpregs(host
);
933 if (intmask
& SDHCI_INT_RESPONSE
)
934 sdhci_finish_command(host
);
936 if (intmask
& SDHCI_INT_TIMEOUT
)
937 host
->cmd
->error
= MMC_ERR_TIMEOUT
;
938 else if (intmask
& SDHCI_INT_CRC
)
939 host
->cmd
->error
= MMC_ERR_BADCRC
;
940 else if (intmask
& (SDHCI_INT_END_BIT
| SDHCI_INT_INDEX
))
941 host
->cmd
->error
= MMC_ERR_FAILED
;
943 host
->cmd
->error
= MMC_ERR_INVALID
;
945 tasklet_schedule(&host
->finish_tasklet
);
949 static void sdhci_data_irq(struct sdhci_host
*host
, u32 intmask
)
951 BUG_ON(intmask
== 0);
955 * A data end interrupt is sent together with the response
956 * for the stop command.
958 if (intmask
& SDHCI_INT_DATA_END
)
961 printk(KERN_ERR
"%s: Got data interrupt even though no "
962 "data operation was in progress.\n",
963 mmc_hostname(host
->mmc
));
964 sdhci_dumpregs(host
);
969 if (intmask
& SDHCI_INT_DATA_TIMEOUT
)
970 host
->data
->error
= MMC_ERR_TIMEOUT
;
971 else if (intmask
& SDHCI_INT_DATA_CRC
)
972 host
->data
->error
= MMC_ERR_BADCRC
;
973 else if (intmask
& SDHCI_INT_DATA_END_BIT
)
974 host
->data
->error
= MMC_ERR_FAILED
;
976 if (host
->data
->error
!= MMC_ERR_NONE
)
977 sdhci_finish_data(host
);
979 if (intmask
& (SDHCI_INT_DATA_AVAIL
| SDHCI_INT_SPACE_AVAIL
))
980 sdhci_transfer_pio(host
);
982 if (intmask
& SDHCI_INT_DATA_END
)
983 sdhci_finish_data(host
);
987 static irqreturn_t
sdhci_irq(int irq
, void *dev_id
)
990 struct sdhci_host
* host
= dev_id
;
993 spin_lock(&host
->lock
);
995 intmask
= readl(host
->ioaddr
+ SDHCI_INT_STATUS
);
1002 DBG("*** %s got interrupt: 0x%08x\n", host
->slot_descr
, intmask
);
1004 if (intmask
& (SDHCI_INT_CARD_INSERT
| SDHCI_INT_CARD_REMOVE
)) {
1005 writel(intmask
& (SDHCI_INT_CARD_INSERT
| SDHCI_INT_CARD_REMOVE
),
1006 host
->ioaddr
+ SDHCI_INT_STATUS
);
1007 tasklet_schedule(&host
->card_tasklet
);
1010 intmask
&= ~(SDHCI_INT_CARD_INSERT
| SDHCI_INT_CARD_REMOVE
);
1012 if (intmask
& SDHCI_INT_CMD_MASK
) {
1013 writel(intmask
& SDHCI_INT_CMD_MASK
,
1014 host
->ioaddr
+ SDHCI_INT_STATUS
);
1015 sdhci_cmd_irq(host
, intmask
& SDHCI_INT_CMD_MASK
);
1018 if (intmask
& SDHCI_INT_DATA_MASK
) {
1019 writel(intmask
& SDHCI_INT_DATA_MASK
,
1020 host
->ioaddr
+ SDHCI_INT_STATUS
);
1021 sdhci_data_irq(host
, intmask
& SDHCI_INT_DATA_MASK
);
1024 intmask
&= ~(SDHCI_INT_CMD_MASK
| SDHCI_INT_DATA_MASK
);
1026 if (intmask
& SDHCI_INT_BUS_POWER
) {
1027 printk(KERN_ERR
"%s: Card is consuming too much power!\n",
1028 mmc_hostname(host
->mmc
));
1029 writel(SDHCI_INT_BUS_POWER
, host
->ioaddr
+ SDHCI_INT_STATUS
);
1032 intmask
&= SDHCI_INT_BUS_POWER
;
1035 printk(KERN_ERR
"%s: Unexpected interrupt 0x%08x.\n",
1036 mmc_hostname(host
->mmc
), intmask
);
1037 sdhci_dumpregs(host
);
1039 writel(intmask
, host
->ioaddr
+ SDHCI_INT_STATUS
);
1042 result
= IRQ_HANDLED
;
1046 spin_unlock(&host
->lock
);
1051 /*****************************************************************************\
1055 \*****************************************************************************/
1059 static int sdhci_suspend (struct pci_dev
*pdev
, pm_message_t state
)
1061 struct sdhci_chip
*chip
;
1064 chip
= pci_get_drvdata(pdev
);
1068 DBG("Suspending...\n");
1070 for (i
= 0;i
< chip
->num_slots
;i
++) {
1071 if (!chip
->hosts
[i
])
1073 ret
= mmc_suspend_host(chip
->hosts
[i
]->mmc
, state
);
1075 for (i
--;i
>= 0;i
--)
1076 mmc_resume_host(chip
->hosts
[i
]->mmc
);
1081 pci_save_state(pdev
);
1082 pci_enable_wake(pdev
, pci_choose_state(pdev
, state
), 0);
1083 pci_disable_device(pdev
);
1084 pci_set_power_state(pdev
, pci_choose_state(pdev
, state
));
1089 static int sdhci_resume (struct pci_dev
*pdev
)
1091 struct sdhci_chip
*chip
;
1094 chip
= pci_get_drvdata(pdev
);
1098 DBG("Resuming...\n");
1100 pci_set_power_state(pdev
, PCI_D0
);
1101 pci_restore_state(pdev
);
1102 ret
= pci_enable_device(pdev
);
1106 for (i
= 0;i
< chip
->num_slots
;i
++) {
1107 if (!chip
->hosts
[i
])
1109 if (chip
->hosts
[i
]->flags
& SDHCI_USE_DMA
)
1110 pci_set_master(pdev
);
1111 sdhci_init(chip
->hosts
[i
]);
1113 ret
= mmc_resume_host(chip
->hosts
[i
]->mmc
);
1121 #else /* CONFIG_PM */
1123 #define sdhci_suspend NULL
1124 #define sdhci_resume NULL
1126 #endif /* CONFIG_PM */
1128 /*****************************************************************************\
1130 * Device probing/removal *
1132 \*****************************************************************************/
1134 static int __devinit
sdhci_probe_slot(struct pci_dev
*pdev
, int slot
)
1137 unsigned int version
;
1138 struct sdhci_chip
*chip
;
1139 struct mmc_host
*mmc
;
1140 struct sdhci_host
*host
;
1145 chip
= pci_get_drvdata(pdev
);
1148 ret
= pci_read_config_byte(pdev
, PCI_SLOT_INFO
, &first_bar
);
1152 first_bar
&= PCI_SLOT_INFO_FIRST_BAR_MASK
;
1154 if (first_bar
> 5) {
1155 printk(KERN_ERR DRIVER_NAME
": Invalid first BAR. Aborting.\n");
1159 if (!(pci_resource_flags(pdev
, first_bar
+ slot
) & IORESOURCE_MEM
)) {
1160 printk(KERN_ERR DRIVER_NAME
": BAR is not iomem. Aborting.\n");
1164 if (pci_resource_len(pdev
, first_bar
+ slot
) != 0x100) {
1165 printk(KERN_ERR DRIVER_NAME
": Invalid iomem size. "
1166 "You may experience problems.\n");
1169 if ((pdev
->class & 0x0000FF) == PCI_SDHCI_IFVENDOR
) {
1170 printk(KERN_ERR DRIVER_NAME
": Vendor specific interface. Aborting.\n");
1174 if ((pdev
->class & 0x0000FF) > PCI_SDHCI_IFVENDOR
) {
1175 printk(KERN_ERR DRIVER_NAME
": Unknown interface. Aborting.\n");
1179 mmc
= mmc_alloc_host(sizeof(struct sdhci_host
), &pdev
->dev
);
1183 host
= mmc_priv(mmc
);
1187 chip
->hosts
[slot
] = host
;
1189 host
->bar
= first_bar
+ slot
;
1191 host
->addr
= pci_resource_start(pdev
, host
->bar
);
1192 host
->irq
= pdev
->irq
;
1194 DBG("slot %d at 0x%08lx, irq %d\n", slot
, host
->addr
, host
->irq
);
1196 snprintf(host
->slot_descr
, 20, "sdhci:slot%d", slot
);
1198 ret
= pci_request_region(pdev
, host
->bar
, host
->slot_descr
);
1202 host
->ioaddr
= ioremap_nocache(host
->addr
,
1203 pci_resource_len(pdev
, host
->bar
));
1204 if (!host
->ioaddr
) {
1209 sdhci_reset(host
, SDHCI_RESET_ALL
);
1211 version
= readw(host
->ioaddr
+ SDHCI_HOST_VERSION
);
1212 version
= (version
& SDHCI_SPEC_VER_MASK
) >> SDHCI_SPEC_VER_SHIFT
;
1214 printk(KERN_ERR
"%s: Unknown controller version (%d). "
1215 "You may experience problems.\n", host
->slot_descr
,
1219 caps
= readl(host
->ioaddr
+ SDHCI_CAPABILITIES
);
1222 DBG("DMA forced off\n");
1223 else if (debug_forcedma
) {
1224 DBG("DMA forced on\n");
1225 host
->flags
|= SDHCI_USE_DMA
;
1226 } else if (chip
->quirks
& SDHCI_QUIRK_FORCE_DMA
)
1227 host
->flags
|= SDHCI_USE_DMA
;
1228 else if ((pdev
->class & 0x0000FF) != PCI_SDHCI_IFDMA
)
1229 DBG("Controller doesn't have DMA interface\n");
1230 else if (!(caps
& SDHCI_CAN_DO_DMA
))
1231 DBG("Controller doesn't have DMA capability\n");
1233 host
->flags
|= SDHCI_USE_DMA
;
1235 if (host
->flags
& SDHCI_USE_DMA
) {
1236 if (pci_set_dma_mask(pdev
, DMA_32BIT_MASK
)) {
1237 printk(KERN_WARNING
"%s: No suitable DMA available. "
1238 "Falling back to PIO.\n", host
->slot_descr
);
1239 host
->flags
&= ~SDHCI_USE_DMA
;
1243 if (host
->flags
& SDHCI_USE_DMA
)
1244 pci_set_master(pdev
);
1245 else /* XXX: Hack to get MMC layer to avoid highmem */
1249 (caps
& SDHCI_CLOCK_BASE_MASK
) >> SDHCI_CLOCK_BASE_SHIFT
;
1250 if (host
->max_clk
== 0) {
1251 printk(KERN_ERR
"%s: Hardware doesn't specify base clock "
1252 "frequency.\n", host
->slot_descr
);
1256 host
->max_clk
*= 1000000;
1259 (caps
& SDHCI_TIMEOUT_CLK_MASK
) >> SDHCI_TIMEOUT_CLK_SHIFT
;
1260 if (host
->timeout_clk
== 0) {
1261 printk(KERN_ERR
"%s: Hardware doesn't specify timeout clock "
1262 "frequency.\n", host
->slot_descr
);
1266 if (caps
& SDHCI_TIMEOUT_CLK_UNIT
)
1267 host
->timeout_clk
*= 1000;
1270 * Set host parameters.
1272 mmc
->ops
= &sdhci_ops
;
1273 mmc
->f_min
= host
->max_clk
/ 256;
1274 mmc
->f_max
= host
->max_clk
;
1275 mmc
->caps
= MMC_CAP_4_BIT_DATA
| MMC_CAP_MULTIWRITE
| MMC_CAP_BYTEBLOCK
;
1278 if (caps
& SDHCI_CAN_VDD_330
)
1279 mmc
->ocr_avail
|= MMC_VDD_32_33
|MMC_VDD_33_34
;
1280 if (caps
& SDHCI_CAN_VDD_300
)
1281 mmc
->ocr_avail
|= MMC_VDD_29_30
|MMC_VDD_30_31
;
1282 if (caps
& SDHCI_CAN_VDD_180
)
1283 mmc
->ocr_avail
|= MMC_VDD_17_18
|MMC_VDD_18_19
;
1285 if ((host
->max_clk
> 25000000) && !(caps
& SDHCI_CAN_DO_HISPD
)) {
1286 printk(KERN_ERR
"%s: Controller reports > 25 MHz base clock,"
1287 " but no high speed support.\n",
1289 mmc
->f_max
= 25000000;
1292 if (mmc
->ocr_avail
== 0) {
1293 printk(KERN_ERR
"%s: Hardware doesn't report any "
1294 "support voltages.\n", host
->slot_descr
);
1299 spin_lock_init(&host
->lock
);
1302 * Maximum number of segments. Hardware cannot do scatter lists.
1304 if (host
->flags
& SDHCI_USE_DMA
)
1305 mmc
->max_hw_segs
= 1;
1307 mmc
->max_hw_segs
= 16;
1308 mmc
->max_phys_segs
= 16;
1311 * Maximum number of sectors in one transfer. Limited by DMA boundary
1314 mmc
->max_req_size
= 524288;
1317 * Maximum segment size. Could be one segment with the maximum number
1320 mmc
->max_seg_size
= mmc
->max_req_size
;
1323 * Maximum block size. This varies from controller to controller and
1324 * is specified in the capabilities register.
1326 mmc
->max_blk_size
= (caps
& SDHCI_MAX_BLOCK_MASK
) >> SDHCI_MAX_BLOCK_SHIFT
;
1327 if (mmc
->max_blk_size
>= 3) {
1328 printk(KERN_ERR
"%s: Invalid maximum block size.\n",
1333 mmc
->max_blk_size
= 512 << mmc
->max_blk_size
;
1336 * Maximum block count.
1338 mmc
->max_blk_count
= 65535;
1343 tasklet_init(&host
->card_tasklet
,
1344 sdhci_tasklet_card
, (unsigned long)host
);
1345 tasklet_init(&host
->finish_tasklet
,
1346 sdhci_tasklet_finish
, (unsigned long)host
);
1348 setup_timer(&host
->timer
, sdhci_timeout_timer
, (unsigned long)host
);
1350 ret
= request_irq(host
->irq
, sdhci_irq
, IRQF_SHARED
,
1351 host
->slot_descr
, host
);
1357 #ifdef CONFIG_MMC_DEBUG
1358 sdhci_dumpregs(host
);
1365 printk(KERN_INFO
"%s: SDHCI at 0x%08lx irq %d %s\n", mmc_hostname(mmc
),
1366 host
->addr
, host
->irq
,
1367 (host
->flags
& SDHCI_USE_DMA
)?"DMA":"PIO");
1372 tasklet_kill(&host
->card_tasklet
);
1373 tasklet_kill(&host
->finish_tasklet
);
1375 iounmap(host
->ioaddr
);
1377 pci_release_region(pdev
, host
->bar
);
1384 static void sdhci_remove_slot(struct pci_dev
*pdev
, int slot
)
1386 struct sdhci_chip
*chip
;
1387 struct mmc_host
*mmc
;
1388 struct sdhci_host
*host
;
1390 chip
= pci_get_drvdata(pdev
);
1391 host
= chip
->hosts
[slot
];
1394 chip
->hosts
[slot
] = NULL
;
1396 mmc_remove_host(mmc
);
1398 sdhci_reset(host
, SDHCI_RESET_ALL
);
1400 free_irq(host
->irq
, host
);
1402 del_timer_sync(&host
->timer
);
1404 tasklet_kill(&host
->card_tasklet
);
1405 tasklet_kill(&host
->finish_tasklet
);
1407 iounmap(host
->ioaddr
);
1409 pci_release_region(pdev
, host
->bar
);
1414 static int __devinit
sdhci_probe(struct pci_dev
*pdev
,
1415 const struct pci_device_id
*ent
)
1419 struct sdhci_chip
*chip
;
1421 BUG_ON(pdev
== NULL
);
1422 BUG_ON(ent
== NULL
);
1424 pci_read_config_byte(pdev
, PCI_CLASS_REVISION
, &rev
);
1426 printk(KERN_INFO DRIVER_NAME
1427 ": SDHCI controller found at %s [%04x:%04x] (rev %x)\n",
1428 pci_name(pdev
), (int)pdev
->vendor
, (int)pdev
->device
,
1431 ret
= pci_read_config_byte(pdev
, PCI_SLOT_INFO
, &slots
);
1435 slots
= PCI_SLOT_INFO_SLOTS(slots
) + 1;
1436 DBG("found %d slot(s)\n", slots
);
1440 ret
= pci_enable_device(pdev
);
1444 chip
= kzalloc(sizeof(struct sdhci_chip
) +
1445 sizeof(struct sdhci_host
*) * slots
, GFP_KERNEL
);
1452 chip
->quirks
= ent
->driver_data
;
1455 chip
->quirks
= debug_quirks
;
1457 chip
->num_slots
= slots
;
1458 pci_set_drvdata(pdev
, chip
);
1460 for (i
= 0;i
< slots
;i
++) {
1461 ret
= sdhci_probe_slot(pdev
, i
);
1463 for (i
--;i
>= 0;i
--)
1464 sdhci_remove_slot(pdev
, i
);
1472 pci_set_drvdata(pdev
, NULL
);
1476 pci_disable_device(pdev
);
1480 static void __devexit
sdhci_remove(struct pci_dev
*pdev
)
1483 struct sdhci_chip
*chip
;
1485 chip
= pci_get_drvdata(pdev
);
1488 for (i
= 0;i
< chip
->num_slots
;i
++)
1489 sdhci_remove_slot(pdev
, i
);
1491 pci_set_drvdata(pdev
, NULL
);
1496 pci_disable_device(pdev
);
1499 static struct pci_driver sdhci_driver
= {
1500 .name
= DRIVER_NAME
,
1501 .id_table
= pci_ids
,
1502 .probe
= sdhci_probe
,
1503 .remove
= __devexit_p(sdhci_remove
),
1504 .suspend
= sdhci_suspend
,
1505 .resume
= sdhci_resume
,
1508 /*****************************************************************************\
1510 * Driver init/exit *
1512 \*****************************************************************************/
1514 static int __init
sdhci_drv_init(void)
1516 printk(KERN_INFO DRIVER_NAME
1517 ": Secure Digital Host Controller Interface driver\n");
1518 printk(KERN_INFO DRIVER_NAME
": Copyright(c) Pierre Ossman\n");
1520 return pci_register_driver(&sdhci_driver
);
1523 static void __exit
sdhci_drv_exit(void)
1527 pci_unregister_driver(&sdhci_driver
);
1530 module_init(sdhci_drv_init
);
1531 module_exit(sdhci_drv_exit
);
1533 module_param(debug_nodma
, uint
, 0444);
1534 module_param(debug_forcedma
, uint
, 0444);
1535 module_param(debug_quirks
, uint
, 0444);
1537 MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
1538 MODULE_DESCRIPTION("Secure Digital Host Controller Interface driver");
1539 MODULE_LICENSE("GPL");
1541 MODULE_PARM_DESC(debug_nodma
, "Forcefully disable DMA transfers. (default 0)");
1542 MODULE_PARM_DESC(debug_forcedma
, "Forcefully enable DMA transfers. (default 0)");
1543 MODULE_PARM_DESC(debug_quirks
, "Force certain quirks.");