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"
25 #define DRIVER_VERSION "0.12"
27 #define BUGMAIL "<sdhci-devel@list.drzeus.cx>"
29 #define DBG(f, x...) \
30 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
32 static unsigned int debug_nodma
= 0;
33 static unsigned int debug_forcedma
= 0;
34 static unsigned int debug_quirks
= 0;
36 #define SDHCI_QUIRK_CLOCK_BEFORE_RESET (1<<0)
37 #define SDHCI_QUIRK_FORCE_DMA (1<<1)
38 /* Controller doesn't like some resets when there is no card inserted. */
39 #define SDHCI_QUIRK_NO_CARD_NO_RESET (1<<2)
41 static const struct pci_device_id pci_ids
[] __devinitdata
= {
43 .vendor
= PCI_VENDOR_ID_RICOH
,
44 .device
= PCI_DEVICE_ID_RICOH_R5C822
,
45 .subvendor
= PCI_VENDOR_ID_IBM
,
46 .subdevice
= PCI_ANY_ID
,
47 .driver_data
= SDHCI_QUIRK_CLOCK_BEFORE_RESET
|
48 SDHCI_QUIRK_FORCE_DMA
,
52 .vendor
= PCI_VENDOR_ID_RICOH
,
53 .device
= PCI_DEVICE_ID_RICOH_R5C822
,
54 .subvendor
= PCI_ANY_ID
,
55 .subdevice
= PCI_ANY_ID
,
56 .driver_data
= SDHCI_QUIRK_FORCE_DMA
|
57 SDHCI_QUIRK_NO_CARD_NO_RESET
,
61 .vendor
= PCI_VENDOR_ID_TI
,
62 .device
= PCI_DEVICE_ID_TI_XX21_XX11_SD
,
63 .subvendor
= PCI_ANY_ID
,
64 .subdevice
= PCI_ANY_ID
,
65 .driver_data
= SDHCI_QUIRK_FORCE_DMA
,
68 { /* Generic SD host controller */
69 PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI
<< 8), 0xFFFF00)
72 { /* end: all zeroes */ },
75 MODULE_DEVICE_TABLE(pci
, pci_ids
);
77 static void sdhci_prepare_data(struct sdhci_host
*, struct mmc_data
*);
78 static void sdhci_finish_data(struct sdhci_host
*);
80 static void sdhci_send_command(struct sdhci_host
*, struct mmc_command
*);
81 static void sdhci_finish_command(struct sdhci_host
*);
83 static void sdhci_dumpregs(struct sdhci_host
*host
)
85 printk(KERN_DEBUG DRIVER_NAME
": ============== REGISTER DUMP ==============\n");
87 printk(KERN_DEBUG DRIVER_NAME
": Sys addr: 0x%08x | Version: 0x%08x\n",
88 readl(host
->ioaddr
+ SDHCI_DMA_ADDRESS
),
89 readw(host
->ioaddr
+ SDHCI_HOST_VERSION
));
90 printk(KERN_DEBUG DRIVER_NAME
": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
91 readw(host
->ioaddr
+ SDHCI_BLOCK_SIZE
),
92 readw(host
->ioaddr
+ SDHCI_BLOCK_COUNT
));
93 printk(KERN_DEBUG DRIVER_NAME
": Argument: 0x%08x | Trn mode: 0x%08x\n",
94 readl(host
->ioaddr
+ SDHCI_ARGUMENT
),
95 readw(host
->ioaddr
+ SDHCI_TRANSFER_MODE
));
96 printk(KERN_DEBUG DRIVER_NAME
": Present: 0x%08x | Host ctl: 0x%08x\n",
97 readl(host
->ioaddr
+ SDHCI_PRESENT_STATE
),
98 readb(host
->ioaddr
+ SDHCI_HOST_CONTROL
));
99 printk(KERN_DEBUG DRIVER_NAME
": Power: 0x%08x | Blk gap: 0x%08x\n",
100 readb(host
->ioaddr
+ SDHCI_POWER_CONTROL
),
101 readb(host
->ioaddr
+ SDHCI_BLOCK_GAP_CONTROL
));
102 printk(KERN_DEBUG DRIVER_NAME
": Wake-up: 0x%08x | Clock: 0x%08x\n",
103 readb(host
->ioaddr
+ SDHCI_WALK_UP_CONTROL
),
104 readw(host
->ioaddr
+ SDHCI_CLOCK_CONTROL
));
105 printk(KERN_DEBUG DRIVER_NAME
": Timeout: 0x%08x | Int stat: 0x%08x\n",
106 readb(host
->ioaddr
+ SDHCI_TIMEOUT_CONTROL
),
107 readl(host
->ioaddr
+ SDHCI_INT_STATUS
));
108 printk(KERN_DEBUG DRIVER_NAME
": Int enab: 0x%08x | Sig enab: 0x%08x\n",
109 readl(host
->ioaddr
+ SDHCI_INT_ENABLE
),
110 readl(host
->ioaddr
+ SDHCI_SIGNAL_ENABLE
));
111 printk(KERN_DEBUG DRIVER_NAME
": AC12 err: 0x%08x | Slot int: 0x%08x\n",
112 readw(host
->ioaddr
+ SDHCI_ACMD12_ERR
),
113 readw(host
->ioaddr
+ SDHCI_SLOT_INT_STATUS
));
114 printk(KERN_DEBUG DRIVER_NAME
": Caps: 0x%08x | Max curr: 0x%08x\n",
115 readl(host
->ioaddr
+ SDHCI_CAPABILITIES
),
116 readl(host
->ioaddr
+ SDHCI_MAX_CURRENT
));
118 printk(KERN_DEBUG DRIVER_NAME
": ===========================================\n");
121 /*****************************************************************************\
123 * Low level functions *
125 \*****************************************************************************/
127 static void sdhci_reset(struct sdhci_host
*host
, u8 mask
)
129 unsigned long timeout
;
131 if (host
->chip
->quirks
& SDHCI_QUIRK_NO_CARD_NO_RESET
) {
132 if (!(readl(host
->ioaddr
+ SDHCI_PRESENT_STATE
) &
137 writeb(mask
, host
->ioaddr
+ SDHCI_SOFTWARE_RESET
);
139 if (mask
& SDHCI_RESET_ALL
)
142 /* Wait max 100 ms */
145 /* hw clears the bit when it's done */
146 while (readb(host
->ioaddr
+ SDHCI_SOFTWARE_RESET
) & mask
) {
148 printk(KERN_ERR
"%s: Reset 0x%x never completed. "
149 "Please report this to " BUGMAIL
".\n",
150 mmc_hostname(host
->mmc
), (int)mask
);
151 sdhci_dumpregs(host
);
159 static void sdhci_init(struct sdhci_host
*host
)
163 sdhci_reset(host
, SDHCI_RESET_ALL
);
165 intmask
= SDHCI_INT_BUS_POWER
| SDHCI_INT_DATA_END_BIT
|
166 SDHCI_INT_DATA_CRC
| SDHCI_INT_DATA_TIMEOUT
| SDHCI_INT_INDEX
|
167 SDHCI_INT_END_BIT
| SDHCI_INT_CRC
| SDHCI_INT_TIMEOUT
|
168 SDHCI_INT_CARD_REMOVE
| SDHCI_INT_CARD_INSERT
|
169 SDHCI_INT_DATA_AVAIL
| SDHCI_INT_SPACE_AVAIL
|
170 SDHCI_INT_DMA_END
| SDHCI_INT_DATA_END
| SDHCI_INT_RESPONSE
;
172 writel(intmask
, host
->ioaddr
+ SDHCI_INT_ENABLE
);
173 writel(intmask
, host
->ioaddr
+ SDHCI_SIGNAL_ENABLE
);
176 static void sdhci_activate_led(struct sdhci_host
*host
)
180 ctrl
= readb(host
->ioaddr
+ SDHCI_HOST_CONTROL
);
181 ctrl
|= SDHCI_CTRL_LED
;
182 writeb(ctrl
, host
->ioaddr
+ SDHCI_HOST_CONTROL
);
185 static void sdhci_deactivate_led(struct sdhci_host
*host
)
189 ctrl
= readb(host
->ioaddr
+ SDHCI_HOST_CONTROL
);
190 ctrl
&= ~SDHCI_CTRL_LED
;
191 writeb(ctrl
, host
->ioaddr
+ SDHCI_HOST_CONTROL
);
194 /*****************************************************************************\
198 \*****************************************************************************/
200 static inline char* sdhci_kmap_sg(struct sdhci_host
* host
)
202 host
->mapped_sg
= kmap_atomic(host
->cur_sg
->page
, KM_BIO_SRC_IRQ
);
203 return host
->mapped_sg
+ host
->cur_sg
->offset
;
206 static inline void sdhci_kunmap_sg(struct sdhci_host
* host
)
208 kunmap_atomic(host
->mapped_sg
, KM_BIO_SRC_IRQ
);
211 static inline int sdhci_next_sg(struct sdhci_host
* host
)
214 * Skip to next SG entry.
222 if (host
->num_sg
> 0) {
224 host
->remain
= host
->cur_sg
->length
;
230 static void sdhci_read_block_pio(struct sdhci_host
*host
)
232 int blksize
, chunk_remain
;
237 DBG("PIO reading\n");
239 blksize
= host
->data
->blksz
;
243 buffer
= sdhci_kmap_sg(host
) + host
->offset
;
246 if (chunk_remain
== 0) {
247 data
= readl(host
->ioaddr
+ SDHCI_BUFFER
);
248 chunk_remain
= min(blksize
, 4);
251 size
= min(host
->size
, host
->remain
);
252 size
= min(size
, chunk_remain
);
254 chunk_remain
-= size
;
256 host
->offset
+= size
;
257 host
->remain
-= size
;
260 *buffer
= data
& 0xFF;
266 if (host
->remain
== 0) {
267 sdhci_kunmap_sg(host
);
268 if (sdhci_next_sg(host
) == 0) {
269 BUG_ON(blksize
!= 0);
272 buffer
= sdhci_kmap_sg(host
);
276 sdhci_kunmap_sg(host
);
279 static void sdhci_write_block_pio(struct sdhci_host
*host
)
281 int blksize
, chunk_remain
;
286 DBG("PIO writing\n");
288 blksize
= host
->data
->blksz
;
293 buffer
= sdhci_kmap_sg(host
) + host
->offset
;
296 size
= min(host
->size
, host
->remain
);
297 size
= min(size
, chunk_remain
);
299 chunk_remain
-= size
;
301 host
->offset
+= size
;
302 host
->remain
-= size
;
306 data
|= (u32
)*buffer
<< 24;
311 if (chunk_remain
== 0) {
312 writel(data
, host
->ioaddr
+ SDHCI_BUFFER
);
313 chunk_remain
= min(blksize
, 4);
316 if (host
->remain
== 0) {
317 sdhci_kunmap_sg(host
);
318 if (sdhci_next_sg(host
) == 0) {
319 BUG_ON(blksize
!= 0);
322 buffer
= sdhci_kmap_sg(host
);
326 sdhci_kunmap_sg(host
);
329 static void sdhci_transfer_pio(struct sdhci_host
*host
)
338 if (host
->data
->flags
& MMC_DATA_READ
)
339 mask
= SDHCI_DATA_AVAILABLE
;
341 mask
= SDHCI_SPACE_AVAILABLE
;
343 while (readl(host
->ioaddr
+ SDHCI_PRESENT_STATE
) & mask
) {
344 if (host
->data
->flags
& MMC_DATA_READ
)
345 sdhci_read_block_pio(host
);
347 sdhci_write_block_pio(host
);
352 BUG_ON(host
->num_sg
== 0);
355 DBG("PIO transfer complete.\n");
358 static void sdhci_prepare_data(struct sdhci_host
*host
, struct mmc_data
*data
)
361 unsigned target_timeout
, current_timeout
;
368 DBG("blksz %04x blks %04x flags %08x\n",
369 data
->blksz
, data
->blocks
, data
->flags
);
370 DBG("tsac %d ms nsac %d clk\n",
371 data
->timeout_ns
/ 1000000, data
->timeout_clks
);
374 BUG_ON(data
->blksz
* data
->blocks
> 524288);
375 BUG_ON(data
->blksz
> host
->max_block
);
376 BUG_ON(data
->blocks
> 65535);
379 target_timeout
= data
->timeout_ns
/ 1000 +
380 data
->timeout_clks
/ host
->clock
;
383 * Figure out needed cycles.
384 * We do this in steps in order to fit inside a 32 bit int.
385 * The first step is the minimum timeout, which will have a
386 * minimum resolution of 6 bits:
387 * (1) 2^13*1000 > 2^22,
388 * (2) host->timeout_clk < 2^16
393 current_timeout
= (1 << 13) * 1000 / host
->timeout_clk
;
394 while (current_timeout
< target_timeout
) {
396 current_timeout
<<= 1;
402 printk(KERN_WARNING
"%s: Too large timeout requested!\n",
403 mmc_hostname(host
->mmc
));
407 writeb(count
, host
->ioaddr
+ SDHCI_TIMEOUT_CONTROL
);
409 if (host
->flags
& SDHCI_USE_DMA
) {
412 count
= pci_map_sg(host
->chip
->pdev
, data
->sg
, data
->sg_len
,
413 (data
->flags
& MMC_DATA_READ
)?PCI_DMA_FROMDEVICE
:PCI_DMA_TODEVICE
);
416 writel(sg_dma_address(data
->sg
), host
->ioaddr
+ SDHCI_DMA_ADDRESS
);
418 host
->size
= data
->blksz
* data
->blocks
;
420 host
->cur_sg
= data
->sg
;
421 host
->num_sg
= data
->sg_len
;
424 host
->remain
= host
->cur_sg
->length
;
427 /* We do not handle DMA boundaries, so set it to max (512 KiB) */
428 writew(SDHCI_MAKE_BLKSZ(7, data
->blksz
),
429 host
->ioaddr
+ SDHCI_BLOCK_SIZE
);
430 writew(data
->blocks
, host
->ioaddr
+ SDHCI_BLOCK_COUNT
);
433 static void sdhci_set_transfer_mode(struct sdhci_host
*host
,
434 struct mmc_data
*data
)
443 mode
= SDHCI_TRNS_BLK_CNT_EN
;
444 if (data
->blocks
> 1)
445 mode
|= SDHCI_TRNS_MULTI
;
446 if (data
->flags
& MMC_DATA_READ
)
447 mode
|= SDHCI_TRNS_READ
;
448 if (host
->flags
& SDHCI_USE_DMA
)
449 mode
|= SDHCI_TRNS_DMA
;
451 writew(mode
, host
->ioaddr
+ SDHCI_TRANSFER_MODE
);
454 static void sdhci_finish_data(struct sdhci_host
*host
)
456 struct mmc_data
*data
;
464 if (host
->flags
& SDHCI_USE_DMA
) {
465 pci_unmap_sg(host
->chip
->pdev
, data
->sg
, data
->sg_len
,
466 (data
->flags
& MMC_DATA_READ
)?PCI_DMA_FROMDEVICE
:PCI_DMA_TODEVICE
);
470 * Controller doesn't count down when in single block mode.
472 if ((data
->blocks
== 1) && (data
->error
== MMC_ERR_NONE
))
475 blocks
= readw(host
->ioaddr
+ SDHCI_BLOCK_COUNT
);
476 data
->bytes_xfered
= data
->blksz
* (data
->blocks
- blocks
);
478 if ((data
->error
== MMC_ERR_NONE
) && blocks
) {
479 printk(KERN_ERR
"%s: Controller signalled completion even "
480 "though there were blocks left. Please report this "
481 "to " BUGMAIL
".\n", mmc_hostname(host
->mmc
));
482 data
->error
= MMC_ERR_FAILED
;
483 } else if (host
->size
!= 0) {
484 printk(KERN_ERR
"%s: %d bytes were left untransferred. "
485 "Please report this to " BUGMAIL
".\n",
486 mmc_hostname(host
->mmc
), host
->size
);
487 data
->error
= MMC_ERR_FAILED
;
490 DBG("Ending data transfer (%d bytes)\n", data
->bytes_xfered
);
494 * The controller needs a reset of internal state machines
495 * upon error conditions.
497 if (data
->error
!= MMC_ERR_NONE
) {
498 sdhci_reset(host
, SDHCI_RESET_CMD
);
499 sdhci_reset(host
, SDHCI_RESET_DATA
);
502 sdhci_send_command(host
, data
->stop
);
504 tasklet_schedule(&host
->finish_tasklet
);
507 static void sdhci_send_command(struct sdhci_host
*host
, struct mmc_command
*cmd
)
511 unsigned long timeout
;
515 DBG("Sending cmd (%x)\n", cmd
->opcode
);
520 mask
= SDHCI_CMD_INHIBIT
;
521 if ((cmd
->data
!= NULL
) || (cmd
->flags
& MMC_RSP_BUSY
))
522 mask
|= SDHCI_DATA_INHIBIT
;
524 /* We shouldn't wait for data inihibit for stop commands, even
525 though they might use busy signaling */
526 if (host
->mrq
->data
&& (cmd
== host
->mrq
->data
->stop
))
527 mask
&= ~SDHCI_DATA_INHIBIT
;
529 while (readl(host
->ioaddr
+ SDHCI_PRESENT_STATE
) & mask
) {
531 printk(KERN_ERR
"%s: Controller never released "
532 "inhibit bit(s). Please report this to "
533 BUGMAIL
".\n", mmc_hostname(host
->mmc
));
534 sdhci_dumpregs(host
);
535 cmd
->error
= MMC_ERR_FAILED
;
536 tasklet_schedule(&host
->finish_tasklet
);
543 mod_timer(&host
->timer
, jiffies
+ 10 * HZ
);
547 sdhci_prepare_data(host
, cmd
->data
);
549 writel(cmd
->arg
, host
->ioaddr
+ SDHCI_ARGUMENT
);
551 sdhci_set_transfer_mode(host
, cmd
->data
);
553 if ((cmd
->flags
& MMC_RSP_136
) && (cmd
->flags
& MMC_RSP_BUSY
)) {
554 printk(KERN_ERR
"%s: Unsupported response type! "
555 "Please report this to " BUGMAIL
".\n",
556 mmc_hostname(host
->mmc
));
557 cmd
->error
= MMC_ERR_INVALID
;
558 tasklet_schedule(&host
->finish_tasklet
);
562 if (!(cmd
->flags
& MMC_RSP_PRESENT
))
563 flags
= SDHCI_CMD_RESP_NONE
;
564 else if (cmd
->flags
& MMC_RSP_136
)
565 flags
= SDHCI_CMD_RESP_LONG
;
566 else if (cmd
->flags
& MMC_RSP_BUSY
)
567 flags
= SDHCI_CMD_RESP_SHORT_BUSY
;
569 flags
= SDHCI_CMD_RESP_SHORT
;
571 if (cmd
->flags
& MMC_RSP_CRC
)
572 flags
|= SDHCI_CMD_CRC
;
573 if (cmd
->flags
& MMC_RSP_OPCODE
)
574 flags
|= SDHCI_CMD_INDEX
;
576 flags
|= SDHCI_CMD_DATA
;
578 writew(SDHCI_MAKE_CMD(cmd
->opcode
, flags
),
579 host
->ioaddr
+ SDHCI_COMMAND
);
582 static void sdhci_finish_command(struct sdhci_host
*host
)
586 BUG_ON(host
->cmd
== NULL
);
588 if (host
->cmd
->flags
& MMC_RSP_PRESENT
) {
589 if (host
->cmd
->flags
& MMC_RSP_136
) {
590 /* CRC is stripped so we need to do some shifting. */
591 for (i
= 0;i
< 4;i
++) {
592 host
->cmd
->resp
[i
] = readl(host
->ioaddr
+
593 SDHCI_RESPONSE
+ (3-i
)*4) << 8;
595 host
->cmd
->resp
[i
] |=
597 SDHCI_RESPONSE
+ (3-i
)*4-1);
600 host
->cmd
->resp
[0] = readl(host
->ioaddr
+ SDHCI_RESPONSE
);
604 host
->cmd
->error
= MMC_ERR_NONE
;
606 DBG("Ending cmd (%x)\n", host
->cmd
->opcode
);
609 host
->data
= host
->cmd
->data
;
611 tasklet_schedule(&host
->finish_tasklet
);
616 static void sdhci_set_clock(struct sdhci_host
*host
, unsigned int clock
)
621 unsigned long timeout
;
623 if (clock
== host
->clock
)
626 writew(0, host
->ioaddr
+ SDHCI_CLOCK_CONTROL
);
628 ctrl
= readb(host
->ioaddr
+ SDHCI_HOST_CONTROL
);
629 if (clock
> 25000000)
630 ctrl
|= SDHCI_CTRL_HISPD
;
632 ctrl
&= ~SDHCI_CTRL_HISPD
;
633 writeb(ctrl
, host
->ioaddr
+ SDHCI_HOST_CONTROL
);
638 for (div
= 1;div
< 256;div
*= 2) {
639 if ((host
->max_clk
/ div
) <= clock
)
644 clk
= div
<< SDHCI_DIVIDER_SHIFT
;
645 clk
|= SDHCI_CLOCK_INT_EN
;
646 writew(clk
, host
->ioaddr
+ SDHCI_CLOCK_CONTROL
);
650 while (!((clk
= readw(host
->ioaddr
+ SDHCI_CLOCK_CONTROL
))
651 & SDHCI_CLOCK_INT_STABLE
)) {
653 printk(KERN_ERR
"%s: Internal clock never stabilised. "
654 "Please report this to " BUGMAIL
".\n",
655 mmc_hostname(host
->mmc
));
656 sdhci_dumpregs(host
);
663 clk
|= SDHCI_CLOCK_CARD_EN
;
664 writew(clk
, host
->ioaddr
+ SDHCI_CLOCK_CONTROL
);
670 static void sdhci_set_power(struct sdhci_host
*host
, unsigned short power
)
674 if (host
->power
== power
)
677 writeb(0, host
->ioaddr
+ SDHCI_POWER_CONTROL
);
679 if (power
== (unsigned short)-1)
682 pwr
= SDHCI_POWER_ON
;
688 pwr
|= SDHCI_POWER_180
;
693 pwr
|= SDHCI_POWER_300
;
698 pwr
|= SDHCI_POWER_330
;
704 writeb(pwr
, host
->ioaddr
+ SDHCI_POWER_CONTROL
);
710 /*****************************************************************************\
714 \*****************************************************************************/
716 static void sdhci_request(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
718 struct sdhci_host
*host
;
721 host
= mmc_priv(mmc
);
723 spin_lock_irqsave(&host
->lock
, flags
);
725 WARN_ON(host
->mrq
!= NULL
);
727 sdhci_activate_led(host
);
731 if (!(readl(host
->ioaddr
+ SDHCI_PRESENT_STATE
) & SDHCI_CARD_PRESENT
)) {
732 host
->mrq
->cmd
->error
= MMC_ERR_TIMEOUT
;
733 tasklet_schedule(&host
->finish_tasklet
);
735 sdhci_send_command(host
, mrq
->cmd
);
738 spin_unlock_irqrestore(&host
->lock
, flags
);
741 static void sdhci_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
743 struct sdhci_host
*host
;
747 host
= mmc_priv(mmc
);
749 spin_lock_irqsave(&host
->lock
, flags
);
752 * Reset the chip on each power off.
753 * Should clear out any weird states.
755 if (ios
->power_mode
== MMC_POWER_OFF
) {
756 writel(0, host
->ioaddr
+ SDHCI_SIGNAL_ENABLE
);
760 sdhci_set_clock(host
, ios
->clock
);
762 if (ios
->power_mode
== MMC_POWER_OFF
)
763 sdhci_set_power(host
, -1);
765 sdhci_set_power(host
, ios
->vdd
);
767 ctrl
= readb(host
->ioaddr
+ SDHCI_HOST_CONTROL
);
768 if (ios
->bus_width
== MMC_BUS_WIDTH_4
)
769 ctrl
|= SDHCI_CTRL_4BITBUS
;
771 ctrl
&= ~SDHCI_CTRL_4BITBUS
;
772 writeb(ctrl
, host
->ioaddr
+ SDHCI_HOST_CONTROL
);
775 spin_unlock_irqrestore(&host
->lock
, flags
);
778 static int sdhci_get_ro(struct mmc_host
*mmc
)
780 struct sdhci_host
*host
;
784 host
= mmc_priv(mmc
);
786 spin_lock_irqsave(&host
->lock
, flags
);
788 present
= readl(host
->ioaddr
+ SDHCI_PRESENT_STATE
);
790 spin_unlock_irqrestore(&host
->lock
, flags
);
792 return !(present
& SDHCI_WRITE_PROTECT
);
795 static const struct mmc_host_ops sdhci_ops
= {
796 .request
= sdhci_request
,
797 .set_ios
= sdhci_set_ios
,
798 .get_ro
= sdhci_get_ro
,
801 /*****************************************************************************\
805 \*****************************************************************************/
807 static void sdhci_tasklet_card(unsigned long param
)
809 struct sdhci_host
*host
;
812 host
= (struct sdhci_host
*)param
;
814 spin_lock_irqsave(&host
->lock
, flags
);
816 if (!(readl(host
->ioaddr
+ SDHCI_PRESENT_STATE
) & SDHCI_CARD_PRESENT
)) {
818 printk(KERN_ERR
"%s: Card removed during transfer!\n",
819 mmc_hostname(host
->mmc
));
820 printk(KERN_ERR
"%s: Resetting controller.\n",
821 mmc_hostname(host
->mmc
));
823 sdhci_reset(host
, SDHCI_RESET_CMD
);
824 sdhci_reset(host
, SDHCI_RESET_DATA
);
826 host
->mrq
->cmd
->error
= MMC_ERR_FAILED
;
827 tasklet_schedule(&host
->finish_tasklet
);
831 spin_unlock_irqrestore(&host
->lock
, flags
);
833 mmc_detect_change(host
->mmc
, msecs_to_jiffies(500));
836 static void sdhci_tasklet_finish(unsigned long param
)
838 struct sdhci_host
*host
;
840 struct mmc_request
*mrq
;
842 host
= (struct sdhci_host
*)param
;
844 spin_lock_irqsave(&host
->lock
, flags
);
846 del_timer(&host
->timer
);
850 DBG("Ending request, cmd (%x)\n", mrq
->cmd
->opcode
);
853 * The controller needs a reset of internal state machines
854 * upon error conditions.
856 if ((mrq
->cmd
->error
!= MMC_ERR_NONE
) ||
857 (mrq
->data
&& ((mrq
->data
->error
!= MMC_ERR_NONE
) ||
858 (mrq
->data
->stop
&& (mrq
->data
->stop
->error
!= MMC_ERR_NONE
))))) {
860 /* Some controllers need this kick or reset won't work here */
861 if (host
->chip
->quirks
& SDHCI_QUIRK_CLOCK_BEFORE_RESET
) {
864 /* This is to force an update */
867 sdhci_set_clock(host
, clock
);
870 /* Spec says we should do both at the same time, but Ricoh
871 controllers do not like that. */
872 sdhci_reset(host
, SDHCI_RESET_CMD
);
873 sdhci_reset(host
, SDHCI_RESET_DATA
);
880 sdhci_deactivate_led(host
);
883 spin_unlock_irqrestore(&host
->lock
, flags
);
885 mmc_request_done(host
->mmc
, mrq
);
888 static void sdhci_timeout_timer(unsigned long data
)
890 struct sdhci_host
*host
;
893 host
= (struct sdhci_host
*)data
;
895 spin_lock_irqsave(&host
->lock
, flags
);
898 printk(KERN_ERR
"%s: Timeout waiting for hardware interrupt. "
899 "Please report this to " BUGMAIL
".\n",
900 mmc_hostname(host
->mmc
));
901 sdhci_dumpregs(host
);
904 host
->data
->error
= MMC_ERR_TIMEOUT
;
905 sdhci_finish_data(host
);
908 host
->cmd
->error
= MMC_ERR_TIMEOUT
;
910 host
->mrq
->cmd
->error
= MMC_ERR_TIMEOUT
;
912 tasklet_schedule(&host
->finish_tasklet
);
917 spin_unlock_irqrestore(&host
->lock
, flags
);
920 /*****************************************************************************\
922 * Interrupt handling *
924 \*****************************************************************************/
926 static void sdhci_cmd_irq(struct sdhci_host
*host
, u32 intmask
)
928 BUG_ON(intmask
== 0);
931 printk(KERN_ERR
"%s: Got command interrupt even though no "
932 "command operation was in progress.\n",
933 mmc_hostname(host
->mmc
));
934 printk(KERN_ERR
"%s: Please report this to " BUGMAIL
".\n",
935 mmc_hostname(host
->mmc
));
936 sdhci_dumpregs(host
);
940 if (intmask
& SDHCI_INT_RESPONSE
)
941 sdhci_finish_command(host
);
943 if (intmask
& SDHCI_INT_TIMEOUT
)
944 host
->cmd
->error
= MMC_ERR_TIMEOUT
;
945 else if (intmask
& SDHCI_INT_CRC
)
946 host
->cmd
->error
= MMC_ERR_BADCRC
;
947 else if (intmask
& (SDHCI_INT_END_BIT
| SDHCI_INT_INDEX
))
948 host
->cmd
->error
= MMC_ERR_FAILED
;
950 host
->cmd
->error
= MMC_ERR_INVALID
;
952 tasklet_schedule(&host
->finish_tasklet
);
956 static void sdhci_data_irq(struct sdhci_host
*host
, u32 intmask
)
958 BUG_ON(intmask
== 0);
962 * A data end interrupt is sent together with the response
963 * for the stop command.
965 if (intmask
& SDHCI_INT_DATA_END
)
968 printk(KERN_ERR
"%s: Got data interrupt even though no "
969 "data operation was in progress.\n",
970 mmc_hostname(host
->mmc
));
971 printk(KERN_ERR
"%s: Please report this to " BUGMAIL
".\n",
972 mmc_hostname(host
->mmc
));
973 sdhci_dumpregs(host
);
978 if (intmask
& SDHCI_INT_DATA_TIMEOUT
)
979 host
->data
->error
= MMC_ERR_TIMEOUT
;
980 else if (intmask
& SDHCI_INT_DATA_CRC
)
981 host
->data
->error
= MMC_ERR_BADCRC
;
982 else if (intmask
& SDHCI_INT_DATA_END_BIT
)
983 host
->data
->error
= MMC_ERR_FAILED
;
985 if (host
->data
->error
!= MMC_ERR_NONE
)
986 sdhci_finish_data(host
);
988 if (intmask
& (SDHCI_INT_DATA_AVAIL
| SDHCI_INT_SPACE_AVAIL
))
989 sdhci_transfer_pio(host
);
991 if (intmask
& SDHCI_INT_DATA_END
)
992 sdhci_finish_data(host
);
996 static irqreturn_t
sdhci_irq(int irq
, void *dev_id
)
999 struct sdhci_host
* host
= dev_id
;
1002 spin_lock(&host
->lock
);
1004 intmask
= readl(host
->ioaddr
+ SDHCI_INT_STATUS
);
1011 DBG("*** %s got interrupt: 0x%08x\n", host
->slot_descr
, intmask
);
1013 if (intmask
& (SDHCI_INT_CARD_INSERT
| SDHCI_INT_CARD_REMOVE
)) {
1014 writel(intmask
& (SDHCI_INT_CARD_INSERT
| SDHCI_INT_CARD_REMOVE
),
1015 host
->ioaddr
+ SDHCI_INT_STATUS
);
1016 tasklet_schedule(&host
->card_tasklet
);
1019 intmask
&= ~(SDHCI_INT_CARD_INSERT
| SDHCI_INT_CARD_REMOVE
);
1021 if (intmask
& SDHCI_INT_CMD_MASK
) {
1022 writel(intmask
& SDHCI_INT_CMD_MASK
,
1023 host
->ioaddr
+ SDHCI_INT_STATUS
);
1024 sdhci_cmd_irq(host
, intmask
& SDHCI_INT_CMD_MASK
);
1027 if (intmask
& SDHCI_INT_DATA_MASK
) {
1028 writel(intmask
& SDHCI_INT_DATA_MASK
,
1029 host
->ioaddr
+ SDHCI_INT_STATUS
);
1030 sdhci_data_irq(host
, intmask
& SDHCI_INT_DATA_MASK
);
1033 intmask
&= ~(SDHCI_INT_CMD_MASK
| SDHCI_INT_DATA_MASK
);
1035 if (intmask
& SDHCI_INT_BUS_POWER
) {
1036 printk(KERN_ERR
"%s: Card is consuming too much power!\n",
1037 mmc_hostname(host
->mmc
));
1038 writel(SDHCI_INT_BUS_POWER
, host
->ioaddr
+ SDHCI_INT_STATUS
);
1041 intmask
&= SDHCI_INT_BUS_POWER
;
1044 printk(KERN_ERR
"%s: Unexpected interrupt 0x%08x. Please "
1045 "report this to " BUGMAIL
".\n",
1046 mmc_hostname(host
->mmc
), intmask
);
1047 sdhci_dumpregs(host
);
1049 writel(intmask
, host
->ioaddr
+ SDHCI_INT_STATUS
);
1052 result
= IRQ_HANDLED
;
1056 spin_unlock(&host
->lock
);
1061 /*****************************************************************************\
1065 \*****************************************************************************/
1069 static int sdhci_suspend (struct pci_dev
*pdev
, pm_message_t state
)
1071 struct sdhci_chip
*chip
;
1074 chip
= pci_get_drvdata(pdev
);
1078 DBG("Suspending...\n");
1080 for (i
= 0;i
< chip
->num_slots
;i
++) {
1081 if (!chip
->hosts
[i
])
1083 ret
= mmc_suspend_host(chip
->hosts
[i
]->mmc
, state
);
1085 for (i
--;i
>= 0;i
--)
1086 mmc_resume_host(chip
->hosts
[i
]->mmc
);
1091 pci_save_state(pdev
);
1092 pci_enable_wake(pdev
, pci_choose_state(pdev
, state
), 0);
1093 pci_disable_device(pdev
);
1094 pci_set_power_state(pdev
, pci_choose_state(pdev
, state
));
1099 static int sdhci_resume (struct pci_dev
*pdev
)
1101 struct sdhci_chip
*chip
;
1104 chip
= pci_get_drvdata(pdev
);
1108 DBG("Resuming...\n");
1110 pci_set_power_state(pdev
, PCI_D0
);
1111 pci_restore_state(pdev
);
1112 pci_enable_device(pdev
);
1114 for (i
= 0;i
< chip
->num_slots
;i
++) {
1115 if (!chip
->hosts
[i
])
1117 if (chip
->hosts
[i
]->flags
& SDHCI_USE_DMA
)
1118 pci_set_master(pdev
);
1119 sdhci_init(chip
->hosts
[i
]);
1121 ret
= mmc_resume_host(chip
->hosts
[i
]->mmc
);
1129 #else /* CONFIG_PM */
1131 #define sdhci_suspend NULL
1132 #define sdhci_resume NULL
1134 #endif /* CONFIG_PM */
1136 /*****************************************************************************\
1138 * Device probing/removal *
1140 \*****************************************************************************/
1142 static int __devinit
sdhci_probe_slot(struct pci_dev
*pdev
, int slot
)
1145 unsigned int version
;
1146 struct sdhci_chip
*chip
;
1147 struct mmc_host
*mmc
;
1148 struct sdhci_host
*host
;
1153 chip
= pci_get_drvdata(pdev
);
1156 ret
= pci_read_config_byte(pdev
, PCI_SLOT_INFO
, &first_bar
);
1160 first_bar
&= PCI_SLOT_INFO_FIRST_BAR_MASK
;
1162 if (first_bar
> 5) {
1163 printk(KERN_ERR DRIVER_NAME
": Invalid first BAR. Aborting.\n");
1167 if (!(pci_resource_flags(pdev
, first_bar
+ slot
) & IORESOURCE_MEM
)) {
1168 printk(KERN_ERR DRIVER_NAME
": BAR is not iomem. Aborting.\n");
1172 if (pci_resource_len(pdev
, first_bar
+ slot
) != 0x100) {
1173 printk(KERN_ERR DRIVER_NAME
": Invalid iomem size. Aborting.\n");
1177 if ((pdev
->class & 0x0000FF) == PCI_SDHCI_IFVENDOR
) {
1178 printk(KERN_ERR DRIVER_NAME
": Vendor specific interface. Aborting.\n");
1182 if ((pdev
->class & 0x0000FF) > PCI_SDHCI_IFVENDOR
) {
1183 printk(KERN_ERR DRIVER_NAME
": Unknown interface. Aborting.\n");
1187 mmc
= mmc_alloc_host(sizeof(struct sdhci_host
), &pdev
->dev
);
1191 host
= mmc_priv(mmc
);
1195 chip
->hosts
[slot
] = host
;
1197 host
->bar
= first_bar
+ slot
;
1199 host
->addr
= pci_resource_start(pdev
, host
->bar
);
1200 host
->irq
= pdev
->irq
;
1202 DBG("slot %d at 0x%08lx, irq %d\n", slot
, host
->addr
, host
->irq
);
1204 snprintf(host
->slot_descr
, 20, "sdhci:slot%d", slot
);
1206 ret
= pci_request_region(pdev
, host
->bar
, host
->slot_descr
);
1210 host
->ioaddr
= ioremap_nocache(host
->addr
,
1211 pci_resource_len(pdev
, host
->bar
));
1212 if (!host
->ioaddr
) {
1217 sdhci_reset(host
, SDHCI_RESET_ALL
);
1219 version
= readw(host
->ioaddr
+ SDHCI_HOST_VERSION
);
1220 version
= (version
& SDHCI_SPEC_VER_MASK
) >> SDHCI_SPEC_VER_SHIFT
;
1222 printk(KERN_ERR
"%s: Unknown controller version (%d). "
1223 "You may experience problems.\n", host
->slot_descr
,
1227 caps
= readl(host
->ioaddr
+ SDHCI_CAPABILITIES
);
1230 DBG("DMA forced off\n");
1231 else if (debug_forcedma
) {
1232 DBG("DMA forced on\n");
1233 host
->flags
|= SDHCI_USE_DMA
;
1234 } else if (chip
->quirks
& SDHCI_QUIRK_FORCE_DMA
)
1235 host
->flags
|= SDHCI_USE_DMA
;
1236 else if ((pdev
->class & 0x0000FF) != PCI_SDHCI_IFDMA
)
1237 DBG("Controller doesn't have DMA interface\n");
1238 else if (!(caps
& SDHCI_CAN_DO_DMA
))
1239 DBG("Controller doesn't have DMA capability\n");
1241 host
->flags
|= SDHCI_USE_DMA
;
1243 if (host
->flags
& SDHCI_USE_DMA
) {
1244 if (pci_set_dma_mask(pdev
, DMA_32BIT_MASK
)) {
1245 printk(KERN_WARNING
"%s: No suitable DMA available. "
1246 "Falling back to PIO.\n", host
->slot_descr
);
1247 host
->flags
&= ~SDHCI_USE_DMA
;
1251 if (host
->flags
& SDHCI_USE_DMA
)
1252 pci_set_master(pdev
);
1253 else /* XXX: Hack to get MMC layer to avoid highmem */
1257 (caps
& SDHCI_CLOCK_BASE_MASK
) >> SDHCI_CLOCK_BASE_SHIFT
;
1258 if (host
->max_clk
== 0) {
1259 printk(KERN_ERR
"%s: Hardware doesn't specify base clock "
1260 "frequency.\n", host
->slot_descr
);
1264 host
->max_clk
*= 1000000;
1267 (caps
& SDHCI_TIMEOUT_CLK_MASK
) >> SDHCI_TIMEOUT_CLK_SHIFT
;
1268 if (host
->timeout_clk
== 0) {
1269 printk(KERN_ERR
"%s: Hardware doesn't specify timeout clock "
1270 "frequency.\n", host
->slot_descr
);
1274 if (caps
& SDHCI_TIMEOUT_CLK_UNIT
)
1275 host
->timeout_clk
*= 1000;
1277 host
->max_block
= (caps
& SDHCI_MAX_BLOCK_MASK
) >> SDHCI_MAX_BLOCK_SHIFT
;
1278 if (host
->max_block
>= 3) {
1279 printk(KERN_ERR
"%s: Invalid maximum block size.\n",
1284 host
->max_block
= 512 << host
->max_block
;
1287 * Set host parameters.
1289 mmc
->ops
= &sdhci_ops
;
1290 mmc
->f_min
= host
->max_clk
/ 256;
1291 mmc
->f_max
= host
->max_clk
;
1292 mmc
->caps
= MMC_CAP_4_BIT_DATA
| MMC_CAP_MULTIWRITE
| MMC_CAP_BYTEBLOCK
;
1295 if (caps
& SDHCI_CAN_VDD_330
)
1296 mmc
->ocr_avail
|= MMC_VDD_32_33
|MMC_VDD_33_34
;
1297 else if (caps
& SDHCI_CAN_VDD_300
)
1298 mmc
->ocr_avail
|= MMC_VDD_29_30
|MMC_VDD_30_31
;
1299 else if (caps
& SDHCI_CAN_VDD_180
)
1300 mmc
->ocr_avail
|= MMC_VDD_17_18
|MMC_VDD_18_19
;
1302 if ((host
->max_clk
> 25000000) && !(caps
& SDHCI_CAN_DO_HISPD
)) {
1303 printk(KERN_ERR
"%s: Controller reports > 25 MHz base clock,"
1304 " but no high speed support.\n",
1306 mmc
->f_max
= 25000000;
1309 if (mmc
->ocr_avail
== 0) {
1310 printk(KERN_ERR
"%s: Hardware doesn't report any "
1311 "support voltages.\n", host
->slot_descr
);
1316 spin_lock_init(&host
->lock
);
1319 * Maximum number of segments. Hardware cannot do scatter lists.
1321 if (host
->flags
& SDHCI_USE_DMA
)
1322 mmc
->max_hw_segs
= 1;
1324 mmc
->max_hw_segs
= 16;
1325 mmc
->max_phys_segs
= 16;
1328 * Maximum number of sectors in one transfer. Limited by DMA boundary
1329 * size (512KiB), which means (512 KiB/512=) 1024 entries.
1331 mmc
->max_sectors
= 1024;
1334 * Maximum segment size. Could be one segment with the maximum number
1337 mmc
->max_seg_size
= mmc
->max_sectors
* 512;
1342 tasklet_init(&host
->card_tasklet
,
1343 sdhci_tasklet_card
, (unsigned long)host
);
1344 tasklet_init(&host
->finish_tasklet
,
1345 sdhci_tasklet_finish
, (unsigned long)host
);
1347 setup_timer(&host
->timer
, sdhci_timeout_timer
, (unsigned long)host
);
1349 ret
= request_irq(host
->irq
, sdhci_irq
, IRQF_SHARED
,
1350 host
->slot_descr
, host
);
1356 #ifdef CONFIG_MMC_DEBUG
1357 sdhci_dumpregs(host
);
1364 printk(KERN_INFO
"%s: SDHCI at 0x%08lx irq %d %s\n", mmc_hostname(mmc
),
1365 host
->addr
, host
->irq
,
1366 (host
->flags
& SDHCI_USE_DMA
)?"DMA":"PIO");
1371 tasklet_kill(&host
->card_tasklet
);
1372 tasklet_kill(&host
->finish_tasklet
);
1374 iounmap(host
->ioaddr
);
1376 pci_release_region(pdev
, host
->bar
);
1383 static void sdhci_remove_slot(struct pci_dev
*pdev
, int slot
)
1385 struct sdhci_chip
*chip
;
1386 struct mmc_host
*mmc
;
1387 struct sdhci_host
*host
;
1389 chip
= pci_get_drvdata(pdev
);
1390 host
= chip
->hosts
[slot
];
1393 chip
->hosts
[slot
] = NULL
;
1395 mmc_remove_host(mmc
);
1397 sdhci_reset(host
, SDHCI_RESET_ALL
);
1399 free_irq(host
->irq
, host
);
1401 del_timer_sync(&host
->timer
);
1403 tasklet_kill(&host
->card_tasklet
);
1404 tasklet_kill(&host
->finish_tasklet
);
1406 iounmap(host
->ioaddr
);
1408 pci_release_region(pdev
, host
->bar
);
1413 static int __devinit
sdhci_probe(struct pci_dev
*pdev
,
1414 const struct pci_device_id
*ent
)
1418 struct sdhci_chip
*chip
;
1420 BUG_ON(pdev
== NULL
);
1421 BUG_ON(ent
== NULL
);
1423 pci_read_config_byte(pdev
, PCI_CLASS_REVISION
, &rev
);
1425 printk(KERN_INFO DRIVER_NAME
1426 ": SDHCI controller found at %s [%04x:%04x] (rev %x)\n",
1427 pci_name(pdev
), (int)pdev
->vendor
, (int)pdev
->device
,
1430 ret
= pci_read_config_byte(pdev
, PCI_SLOT_INFO
, &slots
);
1434 slots
= PCI_SLOT_INFO_SLOTS(slots
) + 1;
1435 DBG("found %d slot(s)\n", slots
);
1439 ret
= pci_enable_device(pdev
);
1443 chip
= kzalloc(sizeof(struct sdhci_chip
) +
1444 sizeof(struct sdhci_host
*) * slots
, GFP_KERNEL
);
1451 chip
->quirks
= ent
->driver_data
;
1454 chip
->quirks
= debug_quirks
;
1456 chip
->num_slots
= slots
;
1457 pci_set_drvdata(pdev
, chip
);
1459 for (i
= 0;i
< slots
;i
++) {
1460 ret
= sdhci_probe_slot(pdev
, i
);
1462 for (i
--;i
>= 0;i
--)
1463 sdhci_remove_slot(pdev
, i
);
1471 pci_set_drvdata(pdev
, NULL
);
1475 pci_disable_device(pdev
);
1479 static void __devexit
sdhci_remove(struct pci_dev
*pdev
)
1482 struct sdhci_chip
*chip
;
1484 chip
= pci_get_drvdata(pdev
);
1487 for (i
= 0;i
< chip
->num_slots
;i
++)
1488 sdhci_remove_slot(pdev
, i
);
1490 pci_set_drvdata(pdev
, NULL
);
1495 pci_disable_device(pdev
);
1498 static struct pci_driver sdhci_driver
= {
1499 .name
= DRIVER_NAME
,
1500 .id_table
= pci_ids
,
1501 .probe
= sdhci_probe
,
1502 .remove
= __devexit_p(sdhci_remove
),
1503 .suspend
= sdhci_suspend
,
1504 .resume
= sdhci_resume
,
1507 /*****************************************************************************\
1509 * Driver init/exit *
1511 \*****************************************************************************/
1513 static int __init
sdhci_drv_init(void)
1515 printk(KERN_INFO DRIVER_NAME
1516 ": Secure Digital Host Controller Interface driver, "
1517 DRIVER_VERSION
"\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_VERSION(DRIVER_VERSION
);
1540 MODULE_LICENSE("GPL");
1542 MODULE_PARM_DESC(debug_nodma
, "Forcefully disable DMA transfers. (default 0)");
1543 MODULE_PARM_DESC(debug_forcedma
, "Forcefully enable DMA transfers. (default 0)");
1544 MODULE_PARM_DESC(debug_quirks
, "Force certain quirks.");