2 * linux/drivers/mmc/host/sdhci.c - Secure Digital Host Controller Interface driver
4 * Copyright (C) 2005-2008 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.
11 * Thanks to the following companies for their support:
13 * - JMicron (hardware and technical support)
16 #include <linux/delay.h>
17 #include <linux/highmem.h>
19 #include <linux/dma-mapping.h>
20 #include <linux/scatterlist.h>
22 #include <linux/leds.h>
24 #include <linux/mmc/host.h>
28 #define DRIVER_NAME "sdhci"
30 #define DBG(f, x...) \
31 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
33 #if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
34 defined(CONFIG_MMC_SDHCI_MODULE))
35 #define SDHCI_USE_LEDS_CLASS
38 static unsigned int debug_quirks
= 0;
40 static void sdhci_prepare_data(struct sdhci_host
*, struct mmc_data
*);
41 static void sdhci_finish_data(struct sdhci_host
*);
43 static void sdhci_send_command(struct sdhci_host
*, struct mmc_command
*);
44 static void sdhci_finish_command(struct sdhci_host
*);
46 static void sdhci_dumpregs(struct sdhci_host
*host
)
48 printk(KERN_DEBUG DRIVER_NAME
": ============== REGISTER DUMP ==============\n");
50 printk(KERN_DEBUG DRIVER_NAME
": Sys addr: 0x%08x | Version: 0x%08x\n",
51 sdhci_readl(host
, SDHCI_DMA_ADDRESS
),
52 sdhci_readw(host
, SDHCI_HOST_VERSION
));
53 printk(KERN_DEBUG DRIVER_NAME
": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
54 sdhci_readw(host
, SDHCI_BLOCK_SIZE
),
55 sdhci_readw(host
, SDHCI_BLOCK_COUNT
));
56 printk(KERN_DEBUG DRIVER_NAME
": Argument: 0x%08x | Trn mode: 0x%08x\n",
57 sdhci_readl(host
, SDHCI_ARGUMENT
),
58 sdhci_readw(host
, SDHCI_TRANSFER_MODE
));
59 printk(KERN_DEBUG DRIVER_NAME
": Present: 0x%08x | Host ctl: 0x%08x\n",
60 sdhci_readl(host
, SDHCI_PRESENT_STATE
),
61 sdhci_readb(host
, SDHCI_HOST_CONTROL
));
62 printk(KERN_DEBUG DRIVER_NAME
": Power: 0x%08x | Blk gap: 0x%08x\n",
63 sdhci_readb(host
, SDHCI_POWER_CONTROL
),
64 sdhci_readb(host
, SDHCI_BLOCK_GAP_CONTROL
));
65 printk(KERN_DEBUG DRIVER_NAME
": Wake-up: 0x%08x | Clock: 0x%08x\n",
66 sdhci_readb(host
, SDHCI_WAKE_UP_CONTROL
),
67 sdhci_readw(host
, SDHCI_CLOCK_CONTROL
));
68 printk(KERN_DEBUG DRIVER_NAME
": Timeout: 0x%08x | Int stat: 0x%08x\n",
69 sdhci_readb(host
, SDHCI_TIMEOUT_CONTROL
),
70 sdhci_readl(host
, SDHCI_INT_STATUS
));
71 printk(KERN_DEBUG DRIVER_NAME
": Int enab: 0x%08x | Sig enab: 0x%08x\n",
72 sdhci_readl(host
, SDHCI_INT_ENABLE
),
73 sdhci_readl(host
, SDHCI_SIGNAL_ENABLE
));
74 printk(KERN_DEBUG DRIVER_NAME
": AC12 err: 0x%08x | Slot int: 0x%08x\n",
75 sdhci_readw(host
, SDHCI_ACMD12_ERR
),
76 sdhci_readw(host
, SDHCI_SLOT_INT_STATUS
));
77 printk(KERN_DEBUG DRIVER_NAME
": Caps: 0x%08x | Max curr: 0x%08x\n",
78 sdhci_readl(host
, SDHCI_CAPABILITIES
),
79 sdhci_readl(host
, SDHCI_MAX_CURRENT
));
81 if (host
->flags
& SDHCI_USE_ADMA
)
82 printk(KERN_DEBUG DRIVER_NAME
": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
83 readl(host
->ioaddr
+ SDHCI_ADMA_ERROR
),
84 readl(host
->ioaddr
+ SDHCI_ADMA_ADDRESS
));
86 printk(KERN_DEBUG DRIVER_NAME
": ===========================================\n");
89 /*****************************************************************************\
91 * Low level functions *
93 \*****************************************************************************/
95 static void sdhci_clear_set_irqs(struct sdhci_host
*host
, u32 clear
, u32 set
)
99 ier
= sdhci_readl(host
, SDHCI_INT_ENABLE
);
102 sdhci_writel(host
, ier
, SDHCI_INT_ENABLE
);
103 sdhci_writel(host
, ier
, SDHCI_SIGNAL_ENABLE
);
106 static void sdhci_unmask_irqs(struct sdhci_host
*host
, u32 irqs
)
108 sdhci_clear_set_irqs(host
, 0, irqs
);
111 static void sdhci_mask_irqs(struct sdhci_host
*host
, u32 irqs
)
113 sdhci_clear_set_irqs(host
, irqs
, 0);
116 static void sdhci_set_card_detection(struct sdhci_host
*host
, bool enable
)
118 u32 irqs
= SDHCI_INT_CARD_REMOVE
| SDHCI_INT_CARD_INSERT
;
120 if (host
->quirks
& SDHCI_QUIRK_BROKEN_CARD_DETECTION
)
124 sdhci_unmask_irqs(host
, irqs
);
126 sdhci_mask_irqs(host
, irqs
);
129 static void sdhci_enable_card_detection(struct sdhci_host
*host
)
131 sdhci_set_card_detection(host
, true);
134 static void sdhci_disable_card_detection(struct sdhci_host
*host
)
136 sdhci_set_card_detection(host
, false);
139 static void sdhci_reset(struct sdhci_host
*host
, u8 mask
)
141 unsigned long timeout
;
142 u32
uninitialized_var(ier
);
144 if (host
->quirks
& SDHCI_QUIRK_NO_CARD_NO_RESET
) {
145 if (!(sdhci_readl(host
, SDHCI_PRESENT_STATE
) &
150 if (host
->quirks
& SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET
)
151 ier
= sdhci_readl(host
, SDHCI_INT_ENABLE
);
153 sdhci_writeb(host
, mask
, SDHCI_SOFTWARE_RESET
);
155 if (mask
& SDHCI_RESET_ALL
)
158 /* Wait max 100 ms */
161 /* hw clears the bit when it's done */
162 while (sdhci_readb(host
, SDHCI_SOFTWARE_RESET
) & mask
) {
164 printk(KERN_ERR
"%s: Reset 0x%x never completed.\n",
165 mmc_hostname(host
->mmc
), (int)mask
);
166 sdhci_dumpregs(host
);
173 if (host
->quirks
& SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET
)
174 sdhci_clear_set_irqs(host
, SDHCI_INT_ALL_MASK
, ier
);
177 static void sdhci_init(struct sdhci_host
*host
)
179 sdhci_reset(host
, SDHCI_RESET_ALL
);
181 sdhci_clear_set_irqs(host
, SDHCI_INT_ALL_MASK
,
182 SDHCI_INT_BUS_POWER
| SDHCI_INT_DATA_END_BIT
|
183 SDHCI_INT_DATA_CRC
| SDHCI_INT_DATA_TIMEOUT
| SDHCI_INT_INDEX
|
184 SDHCI_INT_END_BIT
| SDHCI_INT_CRC
| SDHCI_INT_TIMEOUT
|
185 SDHCI_INT_DATA_END
| SDHCI_INT_RESPONSE
);
188 static void sdhci_reinit(struct sdhci_host
*host
)
191 sdhci_enable_card_detection(host
);
194 static void sdhci_activate_led(struct sdhci_host
*host
)
198 ctrl
= sdhci_readb(host
, SDHCI_HOST_CONTROL
);
199 ctrl
|= SDHCI_CTRL_LED
;
200 sdhci_writeb(host
, ctrl
, SDHCI_HOST_CONTROL
);
203 static void sdhci_deactivate_led(struct sdhci_host
*host
)
207 ctrl
= sdhci_readb(host
, SDHCI_HOST_CONTROL
);
208 ctrl
&= ~SDHCI_CTRL_LED
;
209 sdhci_writeb(host
, ctrl
, SDHCI_HOST_CONTROL
);
212 #ifdef SDHCI_USE_LEDS_CLASS
213 static void sdhci_led_control(struct led_classdev
*led
,
214 enum led_brightness brightness
)
216 struct sdhci_host
*host
= container_of(led
, struct sdhci_host
, led
);
219 spin_lock_irqsave(&host
->lock
, flags
);
221 if (brightness
== LED_OFF
)
222 sdhci_deactivate_led(host
);
224 sdhci_activate_led(host
);
226 spin_unlock_irqrestore(&host
->lock
, flags
);
230 /*****************************************************************************\
234 \*****************************************************************************/
236 static void sdhci_read_block_pio(struct sdhci_host
*host
)
239 size_t blksize
, len
, chunk
;
240 u32
uninitialized_var(scratch
);
243 DBG("PIO reading\n");
245 blksize
= host
->data
->blksz
;
248 local_irq_save(flags
);
251 if (!sg_miter_next(&host
->sg_miter
))
254 len
= min(host
->sg_miter
.length
, blksize
);
257 host
->sg_miter
.consumed
= len
;
259 buf
= host
->sg_miter
.addr
;
263 scratch
= sdhci_readl(host
, SDHCI_BUFFER
);
267 *buf
= scratch
& 0xFF;
276 sg_miter_stop(&host
->sg_miter
);
278 local_irq_restore(flags
);
281 static void sdhci_write_block_pio(struct sdhci_host
*host
)
284 size_t blksize
, len
, chunk
;
288 DBG("PIO writing\n");
290 blksize
= host
->data
->blksz
;
294 local_irq_save(flags
);
297 if (!sg_miter_next(&host
->sg_miter
))
300 len
= min(host
->sg_miter
.length
, blksize
);
303 host
->sg_miter
.consumed
= len
;
305 buf
= host
->sg_miter
.addr
;
308 scratch
|= (u32
)*buf
<< (chunk
* 8);
314 if ((chunk
== 4) || ((len
== 0) && (blksize
== 0))) {
315 sdhci_writel(host
, scratch
, SDHCI_BUFFER
);
322 sg_miter_stop(&host
->sg_miter
);
324 local_irq_restore(flags
);
327 static void sdhci_transfer_pio(struct sdhci_host
*host
)
333 if (host
->blocks
== 0)
336 if (host
->data
->flags
& MMC_DATA_READ
)
337 mask
= SDHCI_DATA_AVAILABLE
;
339 mask
= SDHCI_SPACE_AVAILABLE
;
342 * Some controllers (JMicron JMB38x) mess up the buffer bits
343 * for transfers < 4 bytes. As long as it is just one block,
344 * we can ignore the bits.
346 if ((host
->quirks
& SDHCI_QUIRK_BROKEN_SMALL_PIO
) &&
347 (host
->data
->blocks
== 1))
350 while (sdhci_readl(host
, SDHCI_PRESENT_STATE
) & mask
) {
351 if (host
->quirks
& SDHCI_QUIRK_PIO_NEEDS_DELAY
)
354 if (host
->data
->flags
& MMC_DATA_READ
)
355 sdhci_read_block_pio(host
);
357 sdhci_write_block_pio(host
);
360 if (host
->blocks
== 0)
364 DBG("PIO transfer complete.\n");
367 static char *sdhci_kmap_atomic(struct scatterlist
*sg
, unsigned long *flags
)
369 local_irq_save(*flags
);
370 return kmap_atomic(sg_page(sg
), KM_BIO_SRC_IRQ
) + sg
->offset
;
373 static void sdhci_kunmap_atomic(void *buffer
, unsigned long *flags
)
375 kunmap_atomic(buffer
, KM_BIO_SRC_IRQ
);
376 local_irq_restore(*flags
);
379 static int sdhci_adma_table_pre(struct sdhci_host
*host
,
380 struct mmc_data
*data
)
387 dma_addr_t align_addr
;
390 struct scatterlist
*sg
;
396 * The spec does not specify endianness of descriptor table.
397 * We currently guess that it is LE.
400 if (data
->flags
& MMC_DATA_READ
)
401 direction
= DMA_FROM_DEVICE
;
403 direction
= DMA_TO_DEVICE
;
406 * The ADMA descriptor table is mapped further down as we
407 * need to fill it with data first.
410 host
->align_addr
= dma_map_single(mmc_dev(host
->mmc
),
411 host
->align_buffer
, 128 * 4, direction
);
412 if (dma_mapping_error(mmc_dev(host
->mmc
), host
->align_addr
))
414 BUG_ON(host
->align_addr
& 0x3);
416 host
->sg_count
= dma_map_sg(mmc_dev(host
->mmc
),
417 data
->sg
, data
->sg_len
, direction
);
418 if (host
->sg_count
== 0)
421 desc
= host
->adma_desc
;
422 align
= host
->align_buffer
;
424 align_addr
= host
->align_addr
;
426 for_each_sg(data
->sg
, sg
, host
->sg_count
, i
) {
427 addr
= sg_dma_address(sg
);
428 len
= sg_dma_len(sg
);
431 * The SDHCI specification states that ADMA
432 * addresses must be 32-bit aligned. If they
433 * aren't, then we use a bounce buffer for
434 * the (up to three) bytes that screw up the
437 offset
= (4 - (addr
& 0x3)) & 0x3;
439 if (data
->flags
& MMC_DATA_WRITE
) {
440 buffer
= sdhci_kmap_atomic(sg
, &flags
);
441 WARN_ON(((long)buffer
& PAGE_MASK
) > (PAGE_SIZE
- 3));
442 memcpy(align
, buffer
, offset
);
443 sdhci_kunmap_atomic(buffer
, &flags
);
446 desc
[7] = (align_addr
>> 24) & 0xff;
447 desc
[6] = (align_addr
>> 16) & 0xff;
448 desc
[5] = (align_addr
>> 8) & 0xff;
449 desc
[4] = (align_addr
>> 0) & 0xff;
451 BUG_ON(offset
> 65536);
453 desc
[3] = (offset
>> 8) & 0xff;
454 desc
[2] = (offset
>> 0) & 0xff;
457 desc
[0] = 0x21; /* tran, valid */
468 desc
[7] = (addr
>> 24) & 0xff;
469 desc
[6] = (addr
>> 16) & 0xff;
470 desc
[5] = (addr
>> 8) & 0xff;
471 desc
[4] = (addr
>> 0) & 0xff;
475 desc
[3] = (len
>> 8) & 0xff;
476 desc
[2] = (len
>> 0) & 0xff;
479 desc
[0] = 0x21; /* tran, valid */
484 * If this triggers then we have a calculation bug
487 WARN_ON((desc
- host
->adma_desc
) > (128 * 2 + 1) * 4);
491 * Add a terminating entry.
502 desc
[0] = 0x03; /* nop, end, valid */
505 * Resync align buffer as we might have changed it.
507 if (data
->flags
& MMC_DATA_WRITE
) {
508 dma_sync_single_for_device(mmc_dev(host
->mmc
),
509 host
->align_addr
, 128 * 4, direction
);
512 host
->adma_addr
= dma_map_single(mmc_dev(host
->mmc
),
513 host
->adma_desc
, (128 * 2 + 1) * 4, DMA_TO_DEVICE
);
514 if (dma_mapping_error(mmc_dev(host
->mmc
), host
->adma_addr
))
516 BUG_ON(host
->adma_addr
& 0x3);
521 dma_unmap_sg(mmc_dev(host
->mmc
), data
->sg
,
522 data
->sg_len
, direction
);
524 dma_unmap_single(mmc_dev(host
->mmc
), host
->align_addr
,
530 static void sdhci_adma_table_post(struct sdhci_host
*host
,
531 struct mmc_data
*data
)
535 struct scatterlist
*sg
;
541 if (data
->flags
& MMC_DATA_READ
)
542 direction
= DMA_FROM_DEVICE
;
544 direction
= DMA_TO_DEVICE
;
546 dma_unmap_single(mmc_dev(host
->mmc
), host
->adma_addr
,
547 (128 * 2 + 1) * 4, DMA_TO_DEVICE
);
549 dma_unmap_single(mmc_dev(host
->mmc
), host
->align_addr
,
552 if (data
->flags
& MMC_DATA_READ
) {
553 dma_sync_sg_for_cpu(mmc_dev(host
->mmc
), data
->sg
,
554 data
->sg_len
, direction
);
556 align
= host
->align_buffer
;
558 for_each_sg(data
->sg
, sg
, host
->sg_count
, i
) {
559 if (sg_dma_address(sg
) & 0x3) {
560 size
= 4 - (sg_dma_address(sg
) & 0x3);
562 buffer
= sdhci_kmap_atomic(sg
, &flags
);
563 WARN_ON(((long)buffer
& PAGE_MASK
) > (PAGE_SIZE
- 3));
564 memcpy(buffer
, align
, size
);
565 sdhci_kunmap_atomic(buffer
, &flags
);
572 dma_unmap_sg(mmc_dev(host
->mmc
), data
->sg
,
573 data
->sg_len
, direction
);
576 static u8
sdhci_calc_timeout(struct sdhci_host
*host
, struct mmc_data
*data
)
579 unsigned target_timeout
, current_timeout
;
582 * If the host controller provides us with an incorrect timeout
583 * value, just skip the check and use 0xE. The hardware may take
584 * longer to time out, but that's much better than having a too-short
587 if (host
->quirks
& SDHCI_QUIRK_BROKEN_TIMEOUT_VAL
)
591 target_timeout
= data
->timeout_ns
/ 1000 +
592 data
->timeout_clks
/ host
->clock
;
595 * Figure out needed cycles.
596 * We do this in steps in order to fit inside a 32 bit int.
597 * The first step is the minimum timeout, which will have a
598 * minimum resolution of 6 bits:
599 * (1) 2^13*1000 > 2^22,
600 * (2) host->timeout_clk < 2^16
605 current_timeout
= (1 << 13) * 1000 / host
->timeout_clk
;
606 while (current_timeout
< target_timeout
) {
608 current_timeout
<<= 1;
614 printk(KERN_WARNING
"%s: Too large timeout requested!\n",
615 mmc_hostname(host
->mmc
));
622 static void sdhci_set_transfer_irqs(struct sdhci_host
*host
)
624 u32 pio_irqs
= SDHCI_INT_DATA_AVAIL
| SDHCI_INT_SPACE_AVAIL
;
625 u32 dma_irqs
= SDHCI_INT_DMA_END
| SDHCI_INT_ADMA_ERROR
;
627 if (host
->flags
& SDHCI_REQ_USE_DMA
)
628 sdhci_clear_set_irqs(host
, pio_irqs
, dma_irqs
);
630 sdhci_clear_set_irqs(host
, dma_irqs
, pio_irqs
);
633 static void sdhci_prepare_data(struct sdhci_host
*host
, struct mmc_data
*data
)
645 BUG_ON(data
->blksz
* data
->blocks
> 524288);
646 BUG_ON(data
->blksz
> host
->mmc
->max_blk_size
);
647 BUG_ON(data
->blocks
> 65535);
650 host
->data_early
= 0;
652 count
= sdhci_calc_timeout(host
, data
);
653 sdhci_writeb(host
, count
, SDHCI_TIMEOUT_CONTROL
);
655 if (host
->flags
& SDHCI_USE_DMA
)
656 host
->flags
|= SDHCI_REQ_USE_DMA
;
659 * FIXME: This doesn't account for merging when mapping the
662 if (host
->flags
& SDHCI_REQ_USE_DMA
) {
664 struct scatterlist
*sg
;
667 if (host
->flags
& SDHCI_USE_ADMA
) {
668 if (host
->quirks
& SDHCI_QUIRK_32BIT_ADMA_SIZE
)
671 if (host
->quirks
& SDHCI_QUIRK_32BIT_DMA_SIZE
)
675 if (unlikely(broken
)) {
676 for_each_sg(data
->sg
, sg
, data
->sg_len
, i
) {
677 if (sg
->length
& 0x3) {
678 DBG("Reverting to PIO because of "
679 "transfer size (%d)\n",
681 host
->flags
&= ~SDHCI_REQ_USE_DMA
;
689 * The assumption here being that alignment is the same after
690 * translation to device address space.
692 if (host
->flags
& SDHCI_REQ_USE_DMA
) {
694 struct scatterlist
*sg
;
697 if (host
->flags
& SDHCI_USE_ADMA
) {
699 * As we use 3 byte chunks to work around
700 * alignment problems, we need to check this
703 if (host
->quirks
& SDHCI_QUIRK_32BIT_ADMA_SIZE
)
706 if (host
->quirks
& SDHCI_QUIRK_32BIT_DMA_ADDR
)
710 if (unlikely(broken
)) {
711 for_each_sg(data
->sg
, sg
, data
->sg_len
, i
) {
712 if (sg
->offset
& 0x3) {
713 DBG("Reverting to PIO because of "
715 host
->flags
&= ~SDHCI_REQ_USE_DMA
;
722 if (host
->flags
& SDHCI_REQ_USE_DMA
) {
723 if (host
->flags
& SDHCI_USE_ADMA
) {
724 ret
= sdhci_adma_table_pre(host
, data
);
727 * This only happens when someone fed
728 * us an invalid request.
731 host
->flags
&= ~SDHCI_REQ_USE_DMA
;
733 sdhci_writel(host
, host
->adma_addr
,
739 sg_cnt
= dma_map_sg(mmc_dev(host
->mmc
),
740 data
->sg
, data
->sg_len
,
741 (data
->flags
& MMC_DATA_READ
) ?
746 * This only happens when someone fed
747 * us an invalid request.
750 host
->flags
&= ~SDHCI_REQ_USE_DMA
;
752 WARN_ON(sg_cnt
!= 1);
753 sdhci_writel(host
, sg_dma_address(data
->sg
),
760 * Always adjust the DMA selection as some controllers
761 * (e.g. JMicron) can't do PIO properly when the selection
764 if (host
->version
>= SDHCI_SPEC_200
) {
765 ctrl
= sdhci_readb(host
, SDHCI_HOST_CONTROL
);
766 ctrl
&= ~SDHCI_CTRL_DMA_MASK
;
767 if ((host
->flags
& SDHCI_REQ_USE_DMA
) &&
768 (host
->flags
& SDHCI_USE_ADMA
))
769 ctrl
|= SDHCI_CTRL_ADMA32
;
771 ctrl
|= SDHCI_CTRL_SDMA
;
772 sdhci_writeb(host
, ctrl
, SDHCI_HOST_CONTROL
);
775 if (!(host
->flags
& SDHCI_REQ_USE_DMA
)) {
778 flags
= SG_MITER_ATOMIC
;
779 if (host
->data
->flags
& MMC_DATA_READ
)
780 flags
|= SG_MITER_TO_SG
;
782 flags
|= SG_MITER_FROM_SG
;
783 sg_miter_start(&host
->sg_miter
, data
->sg
, data
->sg_len
, flags
);
784 host
->blocks
= data
->blocks
;
787 sdhci_set_transfer_irqs(host
);
789 /* We do not handle DMA boundaries, so set it to max (512 KiB) */
790 sdhci_writew(host
, SDHCI_MAKE_BLKSZ(7, data
->blksz
), SDHCI_BLOCK_SIZE
);
791 sdhci_writew(host
, data
->blocks
, SDHCI_BLOCK_COUNT
);
794 static void sdhci_set_transfer_mode(struct sdhci_host
*host
,
795 struct mmc_data
*data
)
802 WARN_ON(!host
->data
);
804 mode
= SDHCI_TRNS_BLK_CNT_EN
;
805 if (data
->blocks
> 1)
806 mode
|= SDHCI_TRNS_MULTI
;
807 if (data
->flags
& MMC_DATA_READ
)
808 mode
|= SDHCI_TRNS_READ
;
809 if (host
->flags
& SDHCI_REQ_USE_DMA
)
810 mode
|= SDHCI_TRNS_DMA
;
812 sdhci_writew(host
, mode
, SDHCI_TRANSFER_MODE
);
815 static void sdhci_finish_data(struct sdhci_host
*host
)
817 struct mmc_data
*data
;
824 if (host
->flags
& SDHCI_REQ_USE_DMA
) {
825 if (host
->flags
& SDHCI_USE_ADMA
)
826 sdhci_adma_table_post(host
, data
);
828 dma_unmap_sg(mmc_dev(host
->mmc
), data
->sg
,
829 data
->sg_len
, (data
->flags
& MMC_DATA_READ
) ?
830 DMA_FROM_DEVICE
: DMA_TO_DEVICE
);
835 * The specification states that the block count register must
836 * be updated, but it does not specify at what point in the
837 * data flow. That makes the register entirely useless to read
838 * back so we have to assume that nothing made it to the card
839 * in the event of an error.
842 data
->bytes_xfered
= 0;
844 data
->bytes_xfered
= data
->blksz
* data
->blocks
;
848 * The controller needs a reset of internal state machines
849 * upon error conditions.
852 sdhci_reset(host
, SDHCI_RESET_CMD
);
853 sdhci_reset(host
, SDHCI_RESET_DATA
);
856 sdhci_send_command(host
, data
->stop
);
858 tasklet_schedule(&host
->finish_tasklet
);
861 static void sdhci_send_command(struct sdhci_host
*host
, struct mmc_command
*cmd
)
865 unsigned long timeout
;
872 mask
= SDHCI_CMD_INHIBIT
;
873 if ((cmd
->data
!= NULL
) || (cmd
->flags
& MMC_RSP_BUSY
))
874 mask
|= SDHCI_DATA_INHIBIT
;
876 /* We shouldn't wait for data inihibit for stop commands, even
877 though they might use busy signaling */
878 if (host
->mrq
->data
&& (cmd
== host
->mrq
->data
->stop
))
879 mask
&= ~SDHCI_DATA_INHIBIT
;
881 while (sdhci_readl(host
, SDHCI_PRESENT_STATE
) & mask
) {
883 printk(KERN_ERR
"%s: Controller never released "
884 "inhibit bit(s).\n", mmc_hostname(host
->mmc
));
885 sdhci_dumpregs(host
);
887 tasklet_schedule(&host
->finish_tasklet
);
894 mod_timer(&host
->timer
, jiffies
+ 10 * HZ
);
898 sdhci_prepare_data(host
, cmd
->data
);
900 sdhci_writel(host
, cmd
->arg
, SDHCI_ARGUMENT
);
902 sdhci_set_transfer_mode(host
, cmd
->data
);
904 if ((cmd
->flags
& MMC_RSP_136
) && (cmd
->flags
& MMC_RSP_BUSY
)) {
905 printk(KERN_ERR
"%s: Unsupported response type!\n",
906 mmc_hostname(host
->mmc
));
907 cmd
->error
= -EINVAL
;
908 tasklet_schedule(&host
->finish_tasklet
);
912 if (!(cmd
->flags
& MMC_RSP_PRESENT
))
913 flags
= SDHCI_CMD_RESP_NONE
;
914 else if (cmd
->flags
& MMC_RSP_136
)
915 flags
= SDHCI_CMD_RESP_LONG
;
916 else if (cmd
->flags
& MMC_RSP_BUSY
)
917 flags
= SDHCI_CMD_RESP_SHORT_BUSY
;
919 flags
= SDHCI_CMD_RESP_SHORT
;
921 if (cmd
->flags
& MMC_RSP_CRC
)
922 flags
|= SDHCI_CMD_CRC
;
923 if (cmd
->flags
& MMC_RSP_OPCODE
)
924 flags
|= SDHCI_CMD_INDEX
;
926 flags
|= SDHCI_CMD_DATA
;
928 sdhci_writew(host
, SDHCI_MAKE_CMD(cmd
->opcode
, flags
), SDHCI_COMMAND
);
931 static void sdhci_finish_command(struct sdhci_host
*host
)
935 BUG_ON(host
->cmd
== NULL
);
937 if (host
->cmd
->flags
& MMC_RSP_PRESENT
) {
938 if (host
->cmd
->flags
& MMC_RSP_136
) {
939 /* CRC is stripped so we need to do some shifting. */
940 for (i
= 0;i
< 4;i
++) {
941 host
->cmd
->resp
[i
] = sdhci_readl(host
,
942 SDHCI_RESPONSE
+ (3-i
)*4) << 8;
944 host
->cmd
->resp
[i
] |=
946 SDHCI_RESPONSE
+ (3-i
)*4-1);
949 host
->cmd
->resp
[0] = sdhci_readl(host
, SDHCI_RESPONSE
);
953 host
->cmd
->error
= 0;
955 if (host
->data
&& host
->data_early
)
956 sdhci_finish_data(host
);
958 if (!host
->cmd
->data
)
959 tasklet_schedule(&host
->finish_tasklet
);
964 static void sdhci_set_clock(struct sdhci_host
*host
, unsigned int clock
)
968 unsigned long timeout
;
970 if (clock
== host
->clock
)
973 if (host
->ops
->set_clock
) {
974 host
->ops
->set_clock(host
, clock
);
975 if (host
->quirks
& SDHCI_QUIRK_NONSTANDARD_CLOCK
)
979 sdhci_writew(host
, 0, SDHCI_CLOCK_CONTROL
);
984 for (div
= 1;div
< 256;div
*= 2) {
985 if ((host
->max_clk
/ div
) <= clock
)
990 clk
= div
<< SDHCI_DIVIDER_SHIFT
;
991 clk
|= SDHCI_CLOCK_INT_EN
;
992 sdhci_writew(host
, clk
, SDHCI_CLOCK_CONTROL
);
996 while (!((clk
= sdhci_readw(host
, SDHCI_CLOCK_CONTROL
))
997 & SDHCI_CLOCK_INT_STABLE
)) {
999 printk(KERN_ERR
"%s: Internal clock never "
1000 "stabilised.\n", mmc_hostname(host
->mmc
));
1001 sdhci_dumpregs(host
);
1008 clk
|= SDHCI_CLOCK_CARD_EN
;
1009 sdhci_writew(host
, clk
, SDHCI_CLOCK_CONTROL
);
1012 host
->clock
= clock
;
1015 static void sdhci_set_power(struct sdhci_host
*host
, unsigned short power
)
1019 if (power
== (unsigned short)-1)
1022 switch (1 << power
) {
1023 case MMC_VDD_165_195
:
1024 pwr
= SDHCI_POWER_180
;
1028 pwr
= SDHCI_POWER_300
;
1032 pwr
= SDHCI_POWER_330
;
1039 if (host
->pwr
== pwr
)
1045 sdhci_writeb(host
, 0, SDHCI_POWER_CONTROL
);
1050 * Spec says that we should clear the power reg before setting
1051 * a new value. Some controllers don't seem to like this though.
1053 if (!(host
->quirks
& SDHCI_QUIRK_SINGLE_POWER_WRITE
))
1054 sdhci_writeb(host
, 0, SDHCI_POWER_CONTROL
);
1057 * At least the Marvell CaFe chip gets confused if we set the voltage
1058 * and set turn on power at the same time, so set the voltage first.
1060 if (host
->quirks
& SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER
)
1061 sdhci_writeb(host
, pwr
, SDHCI_POWER_CONTROL
);
1063 pwr
|= SDHCI_POWER_ON
;
1065 sdhci_writeb(host
, pwr
, SDHCI_POWER_CONTROL
);
1068 * Some controllers need an extra 10ms delay of 10ms before they
1069 * can apply clock after applying power
1071 if (host
->quirks
& SDHCI_QUIRK_DELAY_AFTER_POWER
)
1075 /*****************************************************************************\
1079 \*****************************************************************************/
1081 static void sdhci_request(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
1083 struct sdhci_host
*host
;
1085 unsigned long flags
;
1087 host
= mmc_priv(mmc
);
1089 spin_lock_irqsave(&host
->lock
, flags
);
1091 WARN_ON(host
->mrq
!= NULL
);
1093 #ifndef SDHCI_USE_LEDS_CLASS
1094 sdhci_activate_led(host
);
1099 /* If polling, assume that the card is always present. */
1100 if (host
->quirks
& SDHCI_QUIRK_BROKEN_CARD_DETECTION
)
1103 present
= sdhci_readl(host
, SDHCI_PRESENT_STATE
) &
1106 if (!present
|| host
->flags
& SDHCI_DEVICE_DEAD
) {
1107 host
->mrq
->cmd
->error
= -ENOMEDIUM
;
1108 tasklet_schedule(&host
->finish_tasklet
);
1110 sdhci_send_command(host
, mrq
->cmd
);
1113 spin_unlock_irqrestore(&host
->lock
, flags
);
1116 static void sdhci_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
1118 struct sdhci_host
*host
;
1119 unsigned long flags
;
1122 host
= mmc_priv(mmc
);
1124 spin_lock_irqsave(&host
->lock
, flags
);
1126 if (host
->flags
& SDHCI_DEVICE_DEAD
)
1130 * Reset the chip on each power off.
1131 * Should clear out any weird states.
1133 if (ios
->power_mode
== MMC_POWER_OFF
) {
1134 sdhci_writel(host
, 0, SDHCI_SIGNAL_ENABLE
);
1138 sdhci_set_clock(host
, ios
->clock
);
1140 if (ios
->power_mode
== MMC_POWER_OFF
)
1141 sdhci_set_power(host
, -1);
1143 sdhci_set_power(host
, ios
->vdd
);
1145 ctrl
= sdhci_readb(host
, SDHCI_HOST_CONTROL
);
1147 if (ios
->bus_width
== MMC_BUS_WIDTH_4
)
1148 ctrl
|= SDHCI_CTRL_4BITBUS
;
1150 ctrl
&= ~SDHCI_CTRL_4BITBUS
;
1152 if (ios
->timing
== MMC_TIMING_SD_HS
)
1153 ctrl
|= SDHCI_CTRL_HISPD
;
1155 ctrl
&= ~SDHCI_CTRL_HISPD
;
1157 sdhci_writeb(host
, ctrl
, SDHCI_HOST_CONTROL
);
1160 * Some (ENE) controllers go apeshit on some ios operation,
1161 * signalling timeout and CRC errors even on CMD0. Resetting
1162 * it on each ios seems to solve the problem.
1164 if(host
->quirks
& SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS
)
1165 sdhci_reset(host
, SDHCI_RESET_CMD
| SDHCI_RESET_DATA
);
1169 spin_unlock_irqrestore(&host
->lock
, flags
);
1172 static int sdhci_get_ro(struct mmc_host
*mmc
)
1174 struct sdhci_host
*host
;
1175 unsigned long flags
;
1178 host
= mmc_priv(mmc
);
1180 spin_lock_irqsave(&host
->lock
, flags
);
1182 if (host
->flags
& SDHCI_DEVICE_DEAD
)
1185 present
= sdhci_readl(host
, SDHCI_PRESENT_STATE
);
1187 spin_unlock_irqrestore(&host
->lock
, flags
);
1189 if (host
->quirks
& SDHCI_QUIRK_INVERTED_WRITE_PROTECT
)
1190 return !!(present
& SDHCI_WRITE_PROTECT
);
1191 return !(present
& SDHCI_WRITE_PROTECT
);
1194 static void sdhci_enable_sdio_irq(struct mmc_host
*mmc
, int enable
)
1196 struct sdhci_host
*host
;
1197 unsigned long flags
;
1199 host
= mmc_priv(mmc
);
1201 spin_lock_irqsave(&host
->lock
, flags
);
1203 if (host
->flags
& SDHCI_DEVICE_DEAD
)
1207 sdhci_unmask_irqs(host
, SDHCI_INT_CARD_INT
);
1209 sdhci_mask_irqs(host
, SDHCI_INT_CARD_INT
);
1213 spin_unlock_irqrestore(&host
->lock
, flags
);
1216 static const struct mmc_host_ops sdhci_ops
= {
1217 .request
= sdhci_request
,
1218 .set_ios
= sdhci_set_ios
,
1219 .get_ro
= sdhci_get_ro
,
1220 .enable_sdio_irq
= sdhci_enable_sdio_irq
,
1223 /*****************************************************************************\
1227 \*****************************************************************************/
1229 static void sdhci_tasklet_card(unsigned long param
)
1231 struct sdhci_host
*host
;
1232 unsigned long flags
;
1234 host
= (struct sdhci_host
*)param
;
1236 spin_lock_irqsave(&host
->lock
, flags
);
1238 if (!(sdhci_readl(host
, SDHCI_PRESENT_STATE
) & SDHCI_CARD_PRESENT
)) {
1240 printk(KERN_ERR
"%s: Card removed during transfer!\n",
1241 mmc_hostname(host
->mmc
));
1242 printk(KERN_ERR
"%s: Resetting controller.\n",
1243 mmc_hostname(host
->mmc
));
1245 sdhci_reset(host
, SDHCI_RESET_CMD
);
1246 sdhci_reset(host
, SDHCI_RESET_DATA
);
1248 host
->mrq
->cmd
->error
= -ENOMEDIUM
;
1249 tasklet_schedule(&host
->finish_tasklet
);
1253 spin_unlock_irqrestore(&host
->lock
, flags
);
1255 mmc_detect_change(host
->mmc
, msecs_to_jiffies(200));
1258 static void sdhci_tasklet_finish(unsigned long param
)
1260 struct sdhci_host
*host
;
1261 unsigned long flags
;
1262 struct mmc_request
*mrq
;
1264 host
= (struct sdhci_host
*)param
;
1266 spin_lock_irqsave(&host
->lock
, flags
);
1268 del_timer(&host
->timer
);
1273 * The controller needs a reset of internal state machines
1274 * upon error conditions.
1276 if (!(host
->flags
& SDHCI_DEVICE_DEAD
) &&
1278 (mrq
->data
&& (mrq
->data
->error
||
1279 (mrq
->data
->stop
&& mrq
->data
->stop
->error
))) ||
1280 (host
->quirks
& SDHCI_QUIRK_RESET_AFTER_REQUEST
))) {
1282 /* Some controllers need this kick or reset won't work here */
1283 if (host
->quirks
& SDHCI_QUIRK_CLOCK_BEFORE_RESET
) {
1286 /* This is to force an update */
1287 clock
= host
->clock
;
1289 sdhci_set_clock(host
, clock
);
1292 /* Spec says we should do both at the same time, but Ricoh
1293 controllers do not like that. */
1294 sdhci_reset(host
, SDHCI_RESET_CMD
);
1295 sdhci_reset(host
, SDHCI_RESET_DATA
);
1302 #ifndef SDHCI_USE_LEDS_CLASS
1303 sdhci_deactivate_led(host
);
1307 spin_unlock_irqrestore(&host
->lock
, flags
);
1309 mmc_request_done(host
->mmc
, mrq
);
1312 static void sdhci_timeout_timer(unsigned long data
)
1314 struct sdhci_host
*host
;
1315 unsigned long flags
;
1317 host
= (struct sdhci_host
*)data
;
1319 spin_lock_irqsave(&host
->lock
, flags
);
1322 printk(KERN_ERR
"%s: Timeout waiting for hardware "
1323 "interrupt.\n", mmc_hostname(host
->mmc
));
1324 sdhci_dumpregs(host
);
1327 host
->data
->error
= -ETIMEDOUT
;
1328 sdhci_finish_data(host
);
1331 host
->cmd
->error
= -ETIMEDOUT
;
1333 host
->mrq
->cmd
->error
= -ETIMEDOUT
;
1335 tasklet_schedule(&host
->finish_tasklet
);
1340 spin_unlock_irqrestore(&host
->lock
, flags
);
1343 /*****************************************************************************\
1345 * Interrupt handling *
1347 \*****************************************************************************/
1349 static void sdhci_cmd_irq(struct sdhci_host
*host
, u32 intmask
)
1351 BUG_ON(intmask
== 0);
1354 printk(KERN_ERR
"%s: Got command interrupt 0x%08x even "
1355 "though no command operation was in progress.\n",
1356 mmc_hostname(host
->mmc
), (unsigned)intmask
);
1357 sdhci_dumpregs(host
);
1361 if (intmask
& SDHCI_INT_TIMEOUT
)
1362 host
->cmd
->error
= -ETIMEDOUT
;
1363 else if (intmask
& (SDHCI_INT_CRC
| SDHCI_INT_END_BIT
|
1365 host
->cmd
->error
= -EILSEQ
;
1367 if (host
->cmd
->error
) {
1368 tasklet_schedule(&host
->finish_tasklet
);
1373 * The host can send and interrupt when the busy state has
1374 * ended, allowing us to wait without wasting CPU cycles.
1375 * Unfortunately this is overloaded on the "data complete"
1376 * interrupt, so we need to take some care when handling
1379 * Note: The 1.0 specification is a bit ambiguous about this
1380 * feature so there might be some problems with older
1383 if (host
->cmd
->flags
& MMC_RSP_BUSY
) {
1384 if (host
->cmd
->data
)
1385 DBG("Cannot wait for busy signal when also "
1386 "doing a data transfer");
1387 else if (!(host
->quirks
& SDHCI_QUIRK_NO_BUSY_IRQ
))
1390 /* The controller does not support the end-of-busy IRQ,
1391 * fall through and take the SDHCI_INT_RESPONSE */
1394 if (intmask
& SDHCI_INT_RESPONSE
)
1395 sdhci_finish_command(host
);
1399 static void sdhci_show_adma_error(struct sdhci_host
*host
)
1401 const char *name
= mmc_hostname(host
->mmc
);
1402 u8
*desc
= host
->adma_desc
;
1407 sdhci_dumpregs(host
);
1410 dma
= (__le32
*)(desc
+ 4);
1411 len
= (__le16
*)(desc
+ 2);
1414 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
1415 name
, desc
, le32_to_cpu(*dma
), le16_to_cpu(*len
), attr
);
1424 static void sdhci_show_adma_error(struct sdhci_host
*host
) { }
1427 static void sdhci_data_irq(struct sdhci_host
*host
, u32 intmask
)
1429 BUG_ON(intmask
== 0);
1433 * The "data complete" interrupt is also used to
1434 * indicate that a busy state has ended. See comment
1435 * above in sdhci_cmd_irq().
1437 if (host
->cmd
&& (host
->cmd
->flags
& MMC_RSP_BUSY
)) {
1438 if (intmask
& SDHCI_INT_DATA_END
) {
1439 sdhci_finish_command(host
);
1444 printk(KERN_ERR
"%s: Got data interrupt 0x%08x even "
1445 "though no data operation was in progress.\n",
1446 mmc_hostname(host
->mmc
), (unsigned)intmask
);
1447 sdhci_dumpregs(host
);
1452 if (intmask
& SDHCI_INT_DATA_TIMEOUT
)
1453 host
->data
->error
= -ETIMEDOUT
;
1454 else if (intmask
& (SDHCI_INT_DATA_CRC
| SDHCI_INT_DATA_END_BIT
))
1455 host
->data
->error
= -EILSEQ
;
1456 else if (intmask
& SDHCI_INT_ADMA_ERROR
) {
1457 printk(KERN_ERR
"%s: ADMA error\n", mmc_hostname(host
->mmc
));
1458 sdhci_show_adma_error(host
);
1459 host
->data
->error
= -EIO
;
1462 if (host
->data
->error
)
1463 sdhci_finish_data(host
);
1465 if (intmask
& (SDHCI_INT_DATA_AVAIL
| SDHCI_INT_SPACE_AVAIL
))
1466 sdhci_transfer_pio(host
);
1469 * We currently don't do anything fancy with DMA
1470 * boundaries, but as we can't disable the feature
1471 * we need to at least restart the transfer.
1473 if (intmask
& SDHCI_INT_DMA_END
)
1474 sdhci_writel(host
, sdhci_readl(host
, SDHCI_DMA_ADDRESS
),
1477 if (intmask
& SDHCI_INT_DATA_END
) {
1480 * Data managed to finish before the
1481 * command completed. Make sure we do
1482 * things in the proper order.
1484 host
->data_early
= 1;
1486 sdhci_finish_data(host
);
1492 static irqreturn_t
sdhci_irq(int irq
, void *dev_id
)
1495 struct sdhci_host
* host
= dev_id
;
1499 spin_lock(&host
->lock
);
1501 intmask
= sdhci_readl(host
, SDHCI_INT_STATUS
);
1503 if (!intmask
|| intmask
== 0xffffffff) {
1508 DBG("*** %s got interrupt: 0x%08x\n",
1509 mmc_hostname(host
->mmc
), intmask
);
1511 if (intmask
& (SDHCI_INT_CARD_INSERT
| SDHCI_INT_CARD_REMOVE
)) {
1512 sdhci_writel(host
, intmask
& (SDHCI_INT_CARD_INSERT
|
1513 SDHCI_INT_CARD_REMOVE
), SDHCI_INT_STATUS
);
1514 tasklet_schedule(&host
->card_tasklet
);
1517 intmask
&= ~(SDHCI_INT_CARD_INSERT
| SDHCI_INT_CARD_REMOVE
);
1519 if (intmask
& SDHCI_INT_CMD_MASK
) {
1520 sdhci_writel(host
, intmask
& SDHCI_INT_CMD_MASK
,
1522 sdhci_cmd_irq(host
, intmask
& SDHCI_INT_CMD_MASK
);
1525 if (intmask
& SDHCI_INT_DATA_MASK
) {
1526 sdhci_writel(host
, intmask
& SDHCI_INT_DATA_MASK
,
1528 sdhci_data_irq(host
, intmask
& SDHCI_INT_DATA_MASK
);
1531 intmask
&= ~(SDHCI_INT_CMD_MASK
| SDHCI_INT_DATA_MASK
);
1533 intmask
&= ~SDHCI_INT_ERROR
;
1535 if (intmask
& SDHCI_INT_BUS_POWER
) {
1536 printk(KERN_ERR
"%s: Card is consuming too much power!\n",
1537 mmc_hostname(host
->mmc
));
1538 sdhci_writel(host
, SDHCI_INT_BUS_POWER
, SDHCI_INT_STATUS
);
1541 intmask
&= ~SDHCI_INT_BUS_POWER
;
1543 if (intmask
& SDHCI_INT_CARD_INT
)
1546 intmask
&= ~SDHCI_INT_CARD_INT
;
1549 printk(KERN_ERR
"%s: Unexpected interrupt 0x%08x.\n",
1550 mmc_hostname(host
->mmc
), intmask
);
1551 sdhci_dumpregs(host
);
1553 sdhci_writel(host
, intmask
, SDHCI_INT_STATUS
);
1556 result
= IRQ_HANDLED
;
1560 spin_unlock(&host
->lock
);
1563 * We have to delay this as it calls back into the driver.
1566 mmc_signal_sdio_irq(host
->mmc
);
1571 /*****************************************************************************\
1575 \*****************************************************************************/
1579 int sdhci_suspend_host(struct sdhci_host
*host
, pm_message_t state
)
1583 sdhci_disable_card_detection(host
);
1585 ret
= mmc_suspend_host(host
->mmc
, state
);
1589 free_irq(host
->irq
, host
);
1594 EXPORT_SYMBOL_GPL(sdhci_suspend_host
);
1596 int sdhci_resume_host(struct sdhci_host
*host
)
1600 if (host
->flags
& SDHCI_USE_DMA
) {
1601 if (host
->ops
->enable_dma
)
1602 host
->ops
->enable_dma(host
);
1605 ret
= request_irq(host
->irq
, sdhci_irq
, IRQF_SHARED
,
1606 mmc_hostname(host
->mmc
), host
);
1613 ret
= mmc_resume_host(host
->mmc
);
1617 sdhci_enable_card_detection(host
);
1622 EXPORT_SYMBOL_GPL(sdhci_resume_host
);
1624 #endif /* CONFIG_PM */
1626 /*****************************************************************************\
1628 * Device allocation/registration *
1630 \*****************************************************************************/
1632 struct sdhci_host
*sdhci_alloc_host(struct device
*dev
,
1635 struct mmc_host
*mmc
;
1636 struct sdhci_host
*host
;
1638 WARN_ON(dev
== NULL
);
1640 mmc
= mmc_alloc_host(sizeof(struct sdhci_host
) + priv_size
, dev
);
1642 return ERR_PTR(-ENOMEM
);
1644 host
= mmc_priv(mmc
);
1650 EXPORT_SYMBOL_GPL(sdhci_alloc_host
);
1652 int sdhci_add_host(struct sdhci_host
*host
)
1654 struct mmc_host
*mmc
;
1658 WARN_ON(host
== NULL
);
1665 host
->quirks
= debug_quirks
;
1667 sdhci_reset(host
, SDHCI_RESET_ALL
);
1669 host
->version
= sdhci_readw(host
, SDHCI_HOST_VERSION
);
1670 host
->version
= (host
->version
& SDHCI_SPEC_VER_MASK
)
1671 >> SDHCI_SPEC_VER_SHIFT
;
1672 if (host
->version
> SDHCI_SPEC_200
) {
1673 printk(KERN_ERR
"%s: Unknown controller version (%d). "
1674 "You may experience problems.\n", mmc_hostname(mmc
),
1678 caps
= sdhci_readl(host
, SDHCI_CAPABILITIES
);
1680 if (host
->quirks
& SDHCI_QUIRK_FORCE_DMA
)
1681 host
->flags
|= SDHCI_USE_DMA
;
1682 else if (!(caps
& SDHCI_CAN_DO_DMA
))
1683 DBG("Controller doesn't have DMA capability\n");
1685 host
->flags
|= SDHCI_USE_DMA
;
1687 if ((host
->quirks
& SDHCI_QUIRK_BROKEN_DMA
) &&
1688 (host
->flags
& SDHCI_USE_DMA
)) {
1689 DBG("Disabling DMA as it is marked broken\n");
1690 host
->flags
&= ~SDHCI_USE_DMA
;
1693 if (host
->flags
& SDHCI_USE_DMA
) {
1694 if ((host
->version
>= SDHCI_SPEC_200
) &&
1695 (caps
& SDHCI_CAN_DO_ADMA2
))
1696 host
->flags
|= SDHCI_USE_ADMA
;
1699 if ((host
->quirks
& SDHCI_QUIRK_BROKEN_ADMA
) &&
1700 (host
->flags
& SDHCI_USE_ADMA
)) {
1701 DBG("Disabling ADMA as it is marked broken\n");
1702 host
->flags
&= ~SDHCI_USE_ADMA
;
1705 if (host
->flags
& SDHCI_USE_DMA
) {
1706 if (host
->ops
->enable_dma
) {
1707 if (host
->ops
->enable_dma(host
)) {
1708 printk(KERN_WARNING
"%s: No suitable DMA "
1709 "available. Falling back to PIO.\n",
1711 host
->flags
&= ~(SDHCI_USE_DMA
| SDHCI_USE_ADMA
);
1716 if (host
->flags
& SDHCI_USE_ADMA
) {
1718 * We need to allocate descriptors for all sg entries
1719 * (128) and potentially one alignment transfer for
1720 * each of those entries.
1722 host
->adma_desc
= kmalloc((128 * 2 + 1) * 4, GFP_KERNEL
);
1723 host
->align_buffer
= kmalloc(128 * 4, GFP_KERNEL
);
1724 if (!host
->adma_desc
|| !host
->align_buffer
) {
1725 kfree(host
->adma_desc
);
1726 kfree(host
->align_buffer
);
1727 printk(KERN_WARNING
"%s: Unable to allocate ADMA "
1728 "buffers. Falling back to standard DMA.\n",
1730 host
->flags
&= ~SDHCI_USE_ADMA
;
1735 * If we use DMA, then it's up to the caller to set the DMA
1736 * mask, but PIO does not need the hw shim so we set a new
1737 * mask here in that case.
1739 if (!(host
->flags
& SDHCI_USE_DMA
)) {
1740 host
->dma_mask
= DMA_BIT_MASK(64);
1741 mmc_dev(host
->mmc
)->dma_mask
= &host
->dma_mask
;
1745 (caps
& SDHCI_CLOCK_BASE_MASK
) >> SDHCI_CLOCK_BASE_SHIFT
;
1746 host
->max_clk
*= 1000000;
1747 if (host
->max_clk
== 0) {
1748 if (!host
->ops
->get_max_clock
) {
1750 "%s: Hardware doesn't specify base clock "
1751 "frequency.\n", mmc_hostname(mmc
));
1754 host
->max_clk
= host
->ops
->get_max_clock(host
);
1758 (caps
& SDHCI_TIMEOUT_CLK_MASK
) >> SDHCI_TIMEOUT_CLK_SHIFT
;
1759 if (host
->timeout_clk
== 0) {
1760 if (!host
->ops
->get_timeout_clock
) {
1762 "%s: Hardware doesn't specify timeout clock "
1763 "frequency.\n", mmc_hostname(mmc
));
1766 host
->timeout_clk
= host
->ops
->get_timeout_clock(host
);
1768 if (caps
& SDHCI_TIMEOUT_CLK_UNIT
)
1769 host
->timeout_clk
*= 1000;
1772 * Set host parameters.
1774 mmc
->ops
= &sdhci_ops
;
1775 if (host
->ops
->get_min_clock
)
1776 mmc
->f_min
= host
->ops
->get_min_clock(host
);
1778 mmc
->f_min
= host
->max_clk
/ 256;
1779 mmc
->f_max
= host
->max_clk
;
1780 mmc
->caps
= MMC_CAP_SDIO_IRQ
;
1782 if (!(host
->quirks
& SDHCI_QUIRK_FORCE_1_BIT_DATA
))
1783 mmc
->caps
|= MMC_CAP_4_BIT_DATA
;
1785 if (caps
& SDHCI_CAN_DO_HISPD
)
1786 mmc
->caps
|= MMC_CAP_SD_HIGHSPEED
;
1788 if (host
->quirks
& SDHCI_QUIRK_BROKEN_CARD_DETECTION
)
1789 mmc
->caps
|= MMC_CAP_NEEDS_POLL
;
1792 if (caps
& SDHCI_CAN_VDD_330
)
1793 mmc
->ocr_avail
|= MMC_VDD_32_33
|MMC_VDD_33_34
;
1794 if (caps
& SDHCI_CAN_VDD_300
)
1795 mmc
->ocr_avail
|= MMC_VDD_29_30
|MMC_VDD_30_31
;
1796 if (caps
& SDHCI_CAN_VDD_180
)
1797 mmc
->ocr_avail
|= MMC_VDD_165_195
;
1799 if (mmc
->ocr_avail
== 0) {
1800 printk(KERN_ERR
"%s: Hardware doesn't report any "
1801 "support voltages.\n", mmc_hostname(mmc
));
1805 spin_lock_init(&host
->lock
);
1808 * Maximum number of segments. Depends on if the hardware
1809 * can do scatter/gather or not.
1811 if (host
->flags
& SDHCI_USE_ADMA
)
1812 mmc
->max_hw_segs
= 128;
1813 else if (host
->flags
& SDHCI_USE_DMA
)
1814 mmc
->max_hw_segs
= 1;
1816 mmc
->max_hw_segs
= 128;
1817 mmc
->max_phys_segs
= 128;
1820 * Maximum number of sectors in one transfer. Limited by DMA boundary
1823 mmc
->max_req_size
= 524288;
1826 * Maximum segment size. Could be one segment with the maximum number
1827 * of bytes. When doing hardware scatter/gather, each entry cannot
1828 * be larger than 64 KiB though.
1830 if (host
->flags
& SDHCI_USE_ADMA
)
1831 mmc
->max_seg_size
= 65536;
1833 mmc
->max_seg_size
= mmc
->max_req_size
;
1836 * Maximum block size. This varies from controller to controller and
1837 * is specified in the capabilities register.
1839 if (host
->quirks
& SDHCI_QUIRK_FORCE_BLK_SZ_2048
) {
1840 mmc
->max_blk_size
= 2;
1842 mmc
->max_blk_size
= (caps
& SDHCI_MAX_BLOCK_MASK
) >>
1843 SDHCI_MAX_BLOCK_SHIFT
;
1844 if (mmc
->max_blk_size
>= 3) {
1845 printk(KERN_WARNING
"%s: Invalid maximum block size, "
1846 "assuming 512 bytes\n", mmc_hostname(mmc
));
1847 mmc
->max_blk_size
= 0;
1851 mmc
->max_blk_size
= 512 << mmc
->max_blk_size
;
1854 * Maximum block count.
1856 mmc
->max_blk_count
= (host
->quirks
& SDHCI_QUIRK_NO_MULTIBLOCK
) ? 1 : 65535;
1861 tasklet_init(&host
->card_tasklet
,
1862 sdhci_tasklet_card
, (unsigned long)host
);
1863 tasklet_init(&host
->finish_tasklet
,
1864 sdhci_tasklet_finish
, (unsigned long)host
);
1866 setup_timer(&host
->timer
, sdhci_timeout_timer
, (unsigned long)host
);
1868 ret
= request_irq(host
->irq
, sdhci_irq
, IRQF_SHARED
,
1869 mmc_hostname(mmc
), host
);
1875 #ifdef CONFIG_MMC_DEBUG
1876 sdhci_dumpregs(host
);
1879 #ifdef SDHCI_USE_LEDS_CLASS
1880 snprintf(host
->led_name
, sizeof(host
->led_name
),
1881 "%s::", mmc_hostname(mmc
));
1882 host
->led
.name
= host
->led_name
;
1883 host
->led
.brightness
= LED_OFF
;
1884 host
->led
.default_trigger
= mmc_hostname(mmc
);
1885 host
->led
.brightness_set
= sdhci_led_control
;
1887 ret
= led_classdev_register(mmc_dev(mmc
), &host
->led
);
1896 printk(KERN_INFO
"%s: SDHCI controller on %s [%s] using %s%s\n",
1897 mmc_hostname(mmc
), host
->hw_name
, dev_name(mmc_dev(mmc
)),
1898 (host
->flags
& SDHCI_USE_ADMA
)?"A":"",
1899 (host
->flags
& SDHCI_USE_DMA
)?"DMA":"PIO");
1901 sdhci_enable_card_detection(host
);
1905 #ifdef SDHCI_USE_LEDS_CLASS
1907 sdhci_reset(host
, SDHCI_RESET_ALL
);
1908 free_irq(host
->irq
, host
);
1911 tasklet_kill(&host
->card_tasklet
);
1912 tasklet_kill(&host
->finish_tasklet
);
1917 EXPORT_SYMBOL_GPL(sdhci_add_host
);
1919 void sdhci_remove_host(struct sdhci_host
*host
, int dead
)
1921 unsigned long flags
;
1924 spin_lock_irqsave(&host
->lock
, flags
);
1926 host
->flags
|= SDHCI_DEVICE_DEAD
;
1929 printk(KERN_ERR
"%s: Controller removed during "
1930 " transfer!\n", mmc_hostname(host
->mmc
));
1932 host
->mrq
->cmd
->error
= -ENOMEDIUM
;
1933 tasklet_schedule(&host
->finish_tasklet
);
1936 spin_unlock_irqrestore(&host
->lock
, flags
);
1939 sdhci_disable_card_detection(host
);
1941 mmc_remove_host(host
->mmc
);
1943 #ifdef SDHCI_USE_LEDS_CLASS
1944 led_classdev_unregister(&host
->led
);
1948 sdhci_reset(host
, SDHCI_RESET_ALL
);
1950 free_irq(host
->irq
, host
);
1952 del_timer_sync(&host
->timer
);
1954 tasklet_kill(&host
->card_tasklet
);
1955 tasklet_kill(&host
->finish_tasklet
);
1957 kfree(host
->adma_desc
);
1958 kfree(host
->align_buffer
);
1960 host
->adma_desc
= NULL
;
1961 host
->align_buffer
= NULL
;
1964 EXPORT_SYMBOL_GPL(sdhci_remove_host
);
1966 void sdhci_free_host(struct sdhci_host
*host
)
1968 mmc_free_host(host
->mmc
);
1971 EXPORT_SYMBOL_GPL(sdhci_free_host
);
1973 /*****************************************************************************\
1975 * Driver init/exit *
1977 \*****************************************************************************/
1979 static int __init
sdhci_drv_init(void)
1981 printk(KERN_INFO DRIVER_NAME
1982 ": Secure Digital Host Controller Interface driver\n");
1983 printk(KERN_INFO DRIVER_NAME
": Copyright(c) Pierre Ossman\n");
1988 static void __exit
sdhci_drv_exit(void)
1992 module_init(sdhci_drv_init
);
1993 module_exit(sdhci_drv_exit
);
1995 module_param(debug_quirks
, uint
, 0444);
1997 MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
1998 MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
1999 MODULE_LICENSE("GPL");
2001 MODULE_PARM_DESC(debug_quirks
, "Force certain quirks.");