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/slab.h>
21 #include <linux/scatterlist.h>
22 #include <linux/regulator/consumer.h>
24 #include <linux/leds.h>
26 #include <linux/mmc/mmc.h>
27 #include <linux/mmc/host.h>
31 #define DRIVER_NAME "sdhci"
33 #define DBG(f, x...) \
34 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
36 #if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
37 defined(CONFIG_MMC_SDHCI_MODULE))
38 #define SDHCI_USE_LEDS_CLASS
41 static unsigned int debug_quirks
= 0;
43 static void sdhci_prepare_data(struct sdhci_host
*, struct mmc_data
*);
44 static void sdhci_finish_data(struct sdhci_host
*);
46 static void sdhci_send_command(struct sdhci_host
*, struct mmc_command
*);
47 static void sdhci_finish_command(struct sdhci_host
*);
49 static void sdhci_dumpregs(struct sdhci_host
*host
)
51 printk(KERN_DEBUG DRIVER_NAME
": =========== REGISTER DUMP (%s)===========\n",
52 mmc_hostname(host
->mmc
));
54 printk(KERN_DEBUG DRIVER_NAME
": Sys addr: 0x%08x | Version: 0x%08x\n",
55 sdhci_readl(host
, SDHCI_DMA_ADDRESS
),
56 sdhci_readw(host
, SDHCI_HOST_VERSION
));
57 printk(KERN_DEBUG DRIVER_NAME
": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
58 sdhci_readw(host
, SDHCI_BLOCK_SIZE
),
59 sdhci_readw(host
, SDHCI_BLOCK_COUNT
));
60 printk(KERN_DEBUG DRIVER_NAME
": Argument: 0x%08x | Trn mode: 0x%08x\n",
61 sdhci_readl(host
, SDHCI_ARGUMENT
),
62 sdhci_readw(host
, SDHCI_TRANSFER_MODE
));
63 printk(KERN_DEBUG DRIVER_NAME
": Present: 0x%08x | Host ctl: 0x%08x\n",
64 sdhci_readl(host
, SDHCI_PRESENT_STATE
),
65 sdhci_readb(host
, SDHCI_HOST_CONTROL
));
66 printk(KERN_DEBUG DRIVER_NAME
": Power: 0x%08x | Blk gap: 0x%08x\n",
67 sdhci_readb(host
, SDHCI_POWER_CONTROL
),
68 sdhci_readb(host
, SDHCI_BLOCK_GAP_CONTROL
));
69 printk(KERN_DEBUG DRIVER_NAME
": Wake-up: 0x%08x | Clock: 0x%08x\n",
70 sdhci_readb(host
, SDHCI_WAKE_UP_CONTROL
),
71 sdhci_readw(host
, SDHCI_CLOCK_CONTROL
));
72 printk(KERN_DEBUG DRIVER_NAME
": Timeout: 0x%08x | Int stat: 0x%08x\n",
73 sdhci_readb(host
, SDHCI_TIMEOUT_CONTROL
),
74 sdhci_readl(host
, SDHCI_INT_STATUS
));
75 printk(KERN_DEBUG DRIVER_NAME
": Int enab: 0x%08x | Sig enab: 0x%08x\n",
76 sdhci_readl(host
, SDHCI_INT_ENABLE
),
77 sdhci_readl(host
, SDHCI_SIGNAL_ENABLE
));
78 printk(KERN_DEBUG DRIVER_NAME
": AC12 err: 0x%08x | Slot int: 0x%08x\n",
79 sdhci_readw(host
, SDHCI_ACMD12_ERR
),
80 sdhci_readw(host
, SDHCI_SLOT_INT_STATUS
));
81 printk(KERN_DEBUG DRIVER_NAME
": Caps: 0x%08x | Caps_1: 0x%08x\n",
82 sdhci_readl(host
, SDHCI_CAPABILITIES
),
83 sdhci_readl(host
, SDHCI_CAPABILITIES_1
));
84 printk(KERN_DEBUG DRIVER_NAME
": Cmd: 0x%08x | Max curr: 0x%08x\n",
85 sdhci_readw(host
, SDHCI_COMMAND
),
86 sdhci_readl(host
, SDHCI_MAX_CURRENT
));
88 if (host
->flags
& SDHCI_USE_ADMA
)
89 printk(KERN_DEBUG DRIVER_NAME
": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
90 readl(host
->ioaddr
+ SDHCI_ADMA_ERROR
),
91 readl(host
->ioaddr
+ SDHCI_ADMA_ADDRESS
));
93 printk(KERN_DEBUG DRIVER_NAME
": ===========================================\n");
96 /*****************************************************************************\
98 * Low level functions *
100 \*****************************************************************************/
102 static void sdhci_clear_set_irqs(struct sdhci_host
*host
, u32 clear
, u32 set
)
106 ier
= sdhci_readl(host
, SDHCI_INT_ENABLE
);
109 sdhci_writel(host
, ier
, SDHCI_INT_ENABLE
);
110 sdhci_writel(host
, ier
, SDHCI_SIGNAL_ENABLE
);
113 static void sdhci_unmask_irqs(struct sdhci_host
*host
, u32 irqs
)
115 sdhci_clear_set_irqs(host
, 0, irqs
);
118 static void sdhci_mask_irqs(struct sdhci_host
*host
, u32 irqs
)
120 sdhci_clear_set_irqs(host
, irqs
, 0);
123 static void sdhci_set_card_detection(struct sdhci_host
*host
, bool enable
)
125 u32 irqs
= SDHCI_INT_CARD_REMOVE
| SDHCI_INT_CARD_INSERT
;
127 if (host
->quirks
& SDHCI_QUIRK_BROKEN_CARD_DETECTION
)
131 sdhci_unmask_irqs(host
, irqs
);
133 sdhci_mask_irqs(host
, irqs
);
136 static void sdhci_enable_card_detection(struct sdhci_host
*host
)
138 sdhci_set_card_detection(host
, true);
141 static void sdhci_disable_card_detection(struct sdhci_host
*host
)
143 sdhci_set_card_detection(host
, false);
146 static void sdhci_reset(struct sdhci_host
*host
, u8 mask
)
148 unsigned long timeout
;
149 u32
uninitialized_var(ier
);
151 if (host
->quirks
& SDHCI_QUIRK_NO_CARD_NO_RESET
) {
152 if (!(sdhci_readl(host
, SDHCI_PRESENT_STATE
) &
157 if (host
->quirks
& SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET
)
158 ier
= sdhci_readl(host
, SDHCI_INT_ENABLE
);
160 sdhci_writeb(host
, mask
, SDHCI_SOFTWARE_RESET
);
162 if (mask
& SDHCI_RESET_ALL
)
165 /* Wait max 100 ms */
168 /* hw clears the bit when it's done */
169 while (sdhci_readb(host
, SDHCI_SOFTWARE_RESET
) & mask
) {
171 printk(KERN_ERR
"%s: Reset 0x%x never completed.\n",
172 mmc_hostname(host
->mmc
), (int)mask
);
173 sdhci_dumpregs(host
);
180 if (host
->quirks
& SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET
)
181 sdhci_clear_set_irqs(host
, SDHCI_INT_ALL_MASK
, ier
);
184 static void sdhci_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
);
186 static void sdhci_init(struct sdhci_host
*host
, int soft
)
189 sdhci_reset(host
, SDHCI_RESET_CMD
|SDHCI_RESET_DATA
);
191 sdhci_reset(host
, SDHCI_RESET_ALL
);
193 sdhci_clear_set_irqs(host
, SDHCI_INT_ALL_MASK
,
194 SDHCI_INT_BUS_POWER
| SDHCI_INT_DATA_END_BIT
|
195 SDHCI_INT_DATA_CRC
| SDHCI_INT_DATA_TIMEOUT
| SDHCI_INT_INDEX
|
196 SDHCI_INT_END_BIT
| SDHCI_INT_CRC
| SDHCI_INT_TIMEOUT
|
197 SDHCI_INT_DATA_END
| SDHCI_INT_RESPONSE
);
200 /* force clock reconfiguration */
202 sdhci_set_ios(host
->mmc
, &host
->mmc
->ios
);
206 static void sdhci_reinit(struct sdhci_host
*host
)
209 sdhci_enable_card_detection(host
);
212 static void sdhci_activate_led(struct sdhci_host
*host
)
216 ctrl
= sdhci_readb(host
, SDHCI_HOST_CONTROL
);
217 ctrl
|= SDHCI_CTRL_LED
;
218 sdhci_writeb(host
, ctrl
, SDHCI_HOST_CONTROL
);
221 static void sdhci_deactivate_led(struct sdhci_host
*host
)
225 ctrl
= sdhci_readb(host
, SDHCI_HOST_CONTROL
);
226 ctrl
&= ~SDHCI_CTRL_LED
;
227 sdhci_writeb(host
, ctrl
, SDHCI_HOST_CONTROL
);
230 #ifdef SDHCI_USE_LEDS_CLASS
231 static void sdhci_led_control(struct led_classdev
*led
,
232 enum led_brightness brightness
)
234 struct sdhci_host
*host
= container_of(led
, struct sdhci_host
, led
);
237 spin_lock_irqsave(&host
->lock
, flags
);
239 if (brightness
== LED_OFF
)
240 sdhci_deactivate_led(host
);
242 sdhci_activate_led(host
);
244 spin_unlock_irqrestore(&host
->lock
, flags
);
248 /*****************************************************************************\
252 \*****************************************************************************/
254 static void sdhci_read_block_pio(struct sdhci_host
*host
)
257 size_t blksize
, len
, chunk
;
258 u32
uninitialized_var(scratch
);
261 DBG("PIO reading\n");
263 blksize
= host
->data
->blksz
;
266 local_irq_save(flags
);
269 if (!sg_miter_next(&host
->sg_miter
))
272 len
= min(host
->sg_miter
.length
, blksize
);
275 host
->sg_miter
.consumed
= len
;
277 buf
= host
->sg_miter
.addr
;
281 scratch
= sdhci_readl(host
, SDHCI_BUFFER
);
285 *buf
= scratch
& 0xFF;
294 sg_miter_stop(&host
->sg_miter
);
296 local_irq_restore(flags
);
299 static void sdhci_write_block_pio(struct sdhci_host
*host
)
302 size_t blksize
, len
, chunk
;
306 DBG("PIO writing\n");
308 blksize
= host
->data
->blksz
;
312 local_irq_save(flags
);
315 if (!sg_miter_next(&host
->sg_miter
))
318 len
= min(host
->sg_miter
.length
, blksize
);
321 host
->sg_miter
.consumed
= len
;
323 buf
= host
->sg_miter
.addr
;
326 scratch
|= (u32
)*buf
<< (chunk
* 8);
332 if ((chunk
== 4) || ((len
== 0) && (blksize
== 0))) {
333 sdhci_writel(host
, scratch
, SDHCI_BUFFER
);
340 sg_miter_stop(&host
->sg_miter
);
342 local_irq_restore(flags
);
345 static void sdhci_transfer_pio(struct sdhci_host
*host
)
351 if (host
->blocks
== 0)
354 if (host
->data
->flags
& MMC_DATA_READ
)
355 mask
= SDHCI_DATA_AVAILABLE
;
357 mask
= SDHCI_SPACE_AVAILABLE
;
360 * Some controllers (JMicron JMB38x) mess up the buffer bits
361 * for transfers < 4 bytes. As long as it is just one block,
362 * we can ignore the bits.
364 if ((host
->quirks
& SDHCI_QUIRK_BROKEN_SMALL_PIO
) &&
365 (host
->data
->blocks
== 1))
368 while (sdhci_readl(host
, SDHCI_PRESENT_STATE
) & mask
) {
369 if (host
->quirks
& SDHCI_QUIRK_PIO_NEEDS_DELAY
)
372 if (host
->data
->flags
& MMC_DATA_READ
)
373 sdhci_read_block_pio(host
);
375 sdhci_write_block_pio(host
);
378 if (host
->blocks
== 0)
382 DBG("PIO transfer complete.\n");
385 static char *sdhci_kmap_atomic(struct scatterlist
*sg
, unsigned long *flags
)
387 local_irq_save(*flags
);
388 return kmap_atomic(sg_page(sg
), KM_BIO_SRC_IRQ
) + sg
->offset
;
391 static void sdhci_kunmap_atomic(void *buffer
, unsigned long *flags
)
393 kunmap_atomic(buffer
, KM_BIO_SRC_IRQ
);
394 local_irq_restore(*flags
);
397 static void sdhci_set_adma_desc(u8
*desc
, u32 addr
, int len
, unsigned cmd
)
399 __le32
*dataddr
= (__le32 __force
*)(desc
+ 4);
400 __le16
*cmdlen
= (__le16 __force
*)desc
;
402 /* SDHCI specification says ADMA descriptors should be 4 byte
403 * aligned, so using 16 or 32bit operations should be safe. */
405 cmdlen
[0] = cpu_to_le16(cmd
);
406 cmdlen
[1] = cpu_to_le16(len
);
408 dataddr
[0] = cpu_to_le32(addr
);
411 static int sdhci_adma_table_pre(struct sdhci_host
*host
,
412 struct mmc_data
*data
)
419 dma_addr_t align_addr
;
422 struct scatterlist
*sg
;
428 * The spec does not specify endianness of descriptor table.
429 * We currently guess that it is LE.
432 if (data
->flags
& MMC_DATA_READ
)
433 direction
= DMA_FROM_DEVICE
;
435 direction
= DMA_TO_DEVICE
;
438 * The ADMA descriptor table is mapped further down as we
439 * need to fill it with data first.
442 host
->align_addr
= dma_map_single(mmc_dev(host
->mmc
),
443 host
->align_buffer
, 128 * 4, direction
);
444 if (dma_mapping_error(mmc_dev(host
->mmc
), host
->align_addr
))
446 BUG_ON(host
->align_addr
& 0x3);
448 host
->sg_count
= dma_map_sg(mmc_dev(host
->mmc
),
449 data
->sg
, data
->sg_len
, direction
);
450 if (host
->sg_count
== 0)
453 desc
= host
->adma_desc
;
454 align
= host
->align_buffer
;
456 align_addr
= host
->align_addr
;
458 for_each_sg(data
->sg
, sg
, host
->sg_count
, i
) {
459 addr
= sg_dma_address(sg
);
460 len
= sg_dma_len(sg
);
463 * The SDHCI specification states that ADMA
464 * addresses must be 32-bit aligned. If they
465 * aren't, then we use a bounce buffer for
466 * the (up to three) bytes that screw up the
469 offset
= (4 - (addr
& 0x3)) & 0x3;
471 if (data
->flags
& MMC_DATA_WRITE
) {
472 buffer
= sdhci_kmap_atomic(sg
, &flags
);
473 WARN_ON(((long)buffer
& PAGE_MASK
) > (PAGE_SIZE
- 3));
474 memcpy(align
, buffer
, offset
);
475 sdhci_kunmap_atomic(buffer
, &flags
);
479 sdhci_set_adma_desc(desc
, align_addr
, offset
, 0x21);
481 BUG_ON(offset
> 65536);
495 sdhci_set_adma_desc(desc
, addr
, len
, 0x21);
499 * If this triggers then we have a calculation bug
502 WARN_ON((desc
- host
->adma_desc
) > (128 * 2 + 1) * 4);
505 if (host
->quirks
& SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
) {
507 * Mark the last descriptor as the terminating descriptor
509 if (desc
!= host
->adma_desc
) {
511 desc
[0] |= 0x2; /* end */
515 * Add a terminating entry.
518 /* nop, end, valid */
519 sdhci_set_adma_desc(desc
, 0, 0, 0x3);
523 * Resync align buffer as we might have changed it.
525 if (data
->flags
& MMC_DATA_WRITE
) {
526 dma_sync_single_for_device(mmc_dev(host
->mmc
),
527 host
->align_addr
, 128 * 4, direction
);
530 host
->adma_addr
= dma_map_single(mmc_dev(host
->mmc
),
531 host
->adma_desc
, (128 * 2 + 1) * 4, DMA_TO_DEVICE
);
532 if (dma_mapping_error(mmc_dev(host
->mmc
), host
->adma_addr
))
534 BUG_ON(host
->adma_addr
& 0x3);
539 dma_unmap_sg(mmc_dev(host
->mmc
), data
->sg
,
540 data
->sg_len
, direction
);
542 dma_unmap_single(mmc_dev(host
->mmc
), host
->align_addr
,
548 static void sdhci_adma_table_post(struct sdhci_host
*host
,
549 struct mmc_data
*data
)
553 struct scatterlist
*sg
;
559 if (data
->flags
& MMC_DATA_READ
)
560 direction
= DMA_FROM_DEVICE
;
562 direction
= DMA_TO_DEVICE
;
564 dma_unmap_single(mmc_dev(host
->mmc
), host
->adma_addr
,
565 (128 * 2 + 1) * 4, DMA_TO_DEVICE
);
567 dma_unmap_single(mmc_dev(host
->mmc
), host
->align_addr
,
570 if (data
->flags
& MMC_DATA_READ
) {
571 dma_sync_sg_for_cpu(mmc_dev(host
->mmc
), data
->sg
,
572 data
->sg_len
, direction
);
574 align
= host
->align_buffer
;
576 for_each_sg(data
->sg
, sg
, host
->sg_count
, i
) {
577 if (sg_dma_address(sg
) & 0x3) {
578 size
= 4 - (sg_dma_address(sg
) & 0x3);
580 buffer
= sdhci_kmap_atomic(sg
, &flags
);
581 WARN_ON(((long)buffer
& PAGE_MASK
) > (PAGE_SIZE
- 3));
582 memcpy(buffer
, align
, size
);
583 sdhci_kunmap_atomic(buffer
, &flags
);
590 dma_unmap_sg(mmc_dev(host
->mmc
), data
->sg
,
591 data
->sg_len
, direction
);
594 static u8
sdhci_calc_timeout(struct sdhci_host
*host
, struct mmc_data
*data
)
597 unsigned target_timeout
, current_timeout
;
600 * If the host controller provides us with an incorrect timeout
601 * value, just skip the check and use 0xE. The hardware may take
602 * longer to time out, but that's much better than having a too-short
605 if (host
->quirks
& SDHCI_QUIRK_BROKEN_TIMEOUT_VAL
)
609 target_timeout
= data
->timeout_ns
/ 1000 +
610 data
->timeout_clks
/ host
->clock
;
612 if (host
->quirks
& SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK
)
613 host
->timeout_clk
= host
->clock
/ 1000;
616 * Figure out needed cycles.
617 * We do this in steps in order to fit inside a 32 bit int.
618 * The first step is the minimum timeout, which will have a
619 * minimum resolution of 6 bits:
620 * (1) 2^13*1000 > 2^22,
621 * (2) host->timeout_clk < 2^16
626 current_timeout
= (1 << 13) * 1000 / host
->timeout_clk
;
627 while (current_timeout
< target_timeout
) {
629 current_timeout
<<= 1;
635 printk(KERN_WARNING
"%s: Too large timeout requested!\n",
636 mmc_hostname(host
->mmc
));
643 static void sdhci_set_transfer_irqs(struct sdhci_host
*host
)
645 u32 pio_irqs
= SDHCI_INT_DATA_AVAIL
| SDHCI_INT_SPACE_AVAIL
;
646 u32 dma_irqs
= SDHCI_INT_DMA_END
| SDHCI_INT_ADMA_ERROR
;
648 if (host
->flags
& SDHCI_REQ_USE_DMA
)
649 sdhci_clear_set_irqs(host
, pio_irqs
, dma_irqs
);
651 sdhci_clear_set_irqs(host
, dma_irqs
, pio_irqs
);
654 static void sdhci_prepare_data(struct sdhci_host
*host
, struct mmc_data
*data
)
666 BUG_ON(data
->blksz
* data
->blocks
> 524288);
667 BUG_ON(data
->blksz
> host
->mmc
->max_blk_size
);
668 BUG_ON(data
->blocks
> 65535);
671 host
->data_early
= 0;
673 count
= sdhci_calc_timeout(host
, data
);
674 sdhci_writeb(host
, count
, SDHCI_TIMEOUT_CONTROL
);
676 if (host
->flags
& (SDHCI_USE_SDMA
| SDHCI_USE_ADMA
))
677 host
->flags
|= SDHCI_REQ_USE_DMA
;
680 * FIXME: This doesn't account for merging when mapping the
683 if (host
->flags
& SDHCI_REQ_USE_DMA
) {
685 struct scatterlist
*sg
;
688 if (host
->flags
& SDHCI_USE_ADMA
) {
689 if (host
->quirks
& SDHCI_QUIRK_32BIT_ADMA_SIZE
)
692 if (host
->quirks
& SDHCI_QUIRK_32BIT_DMA_SIZE
)
696 if (unlikely(broken
)) {
697 for_each_sg(data
->sg
, sg
, data
->sg_len
, i
) {
698 if (sg
->length
& 0x3) {
699 DBG("Reverting to PIO because of "
700 "transfer size (%d)\n",
702 host
->flags
&= ~SDHCI_REQ_USE_DMA
;
710 * The assumption here being that alignment is the same after
711 * translation to device address space.
713 if (host
->flags
& SDHCI_REQ_USE_DMA
) {
715 struct scatterlist
*sg
;
718 if (host
->flags
& SDHCI_USE_ADMA
) {
720 * As we use 3 byte chunks to work around
721 * alignment problems, we need to check this
724 if (host
->quirks
& SDHCI_QUIRK_32BIT_ADMA_SIZE
)
727 if (host
->quirks
& SDHCI_QUIRK_32BIT_DMA_ADDR
)
731 if (unlikely(broken
)) {
732 for_each_sg(data
->sg
, sg
, data
->sg_len
, i
) {
733 if (sg
->offset
& 0x3) {
734 DBG("Reverting to PIO because of "
736 host
->flags
&= ~SDHCI_REQ_USE_DMA
;
743 if (host
->flags
& SDHCI_REQ_USE_DMA
) {
744 if (host
->flags
& SDHCI_USE_ADMA
) {
745 ret
= sdhci_adma_table_pre(host
, data
);
748 * This only happens when someone fed
749 * us an invalid request.
752 host
->flags
&= ~SDHCI_REQ_USE_DMA
;
754 sdhci_writel(host
, host
->adma_addr
,
760 sg_cnt
= dma_map_sg(mmc_dev(host
->mmc
),
761 data
->sg
, data
->sg_len
,
762 (data
->flags
& MMC_DATA_READ
) ?
767 * This only happens when someone fed
768 * us an invalid request.
771 host
->flags
&= ~SDHCI_REQ_USE_DMA
;
773 WARN_ON(sg_cnt
!= 1);
774 sdhci_writel(host
, sg_dma_address(data
->sg
),
781 * Always adjust the DMA selection as some controllers
782 * (e.g. JMicron) can't do PIO properly when the selection
785 if (host
->version
>= SDHCI_SPEC_200
) {
786 ctrl
= sdhci_readb(host
, SDHCI_HOST_CONTROL
);
787 ctrl
&= ~SDHCI_CTRL_DMA_MASK
;
788 if ((host
->flags
& SDHCI_REQ_USE_DMA
) &&
789 (host
->flags
& SDHCI_USE_ADMA
))
790 ctrl
|= SDHCI_CTRL_ADMA32
;
792 ctrl
|= SDHCI_CTRL_SDMA
;
793 sdhci_writeb(host
, ctrl
, SDHCI_HOST_CONTROL
);
796 if (!(host
->flags
& SDHCI_REQ_USE_DMA
)) {
799 flags
= SG_MITER_ATOMIC
;
800 if (host
->data
->flags
& MMC_DATA_READ
)
801 flags
|= SG_MITER_TO_SG
;
803 flags
|= SG_MITER_FROM_SG
;
804 sg_miter_start(&host
->sg_miter
, data
->sg
, data
->sg_len
, flags
);
805 host
->blocks
= data
->blocks
;
808 sdhci_set_transfer_irqs(host
);
810 /* We do not handle DMA boundaries, so set it to max (512 KiB) */
811 sdhci_writew(host
, SDHCI_MAKE_BLKSZ(7, data
->blksz
), SDHCI_BLOCK_SIZE
);
812 sdhci_writew(host
, data
->blocks
, SDHCI_BLOCK_COUNT
);
815 static void sdhci_set_transfer_mode(struct sdhci_host
*host
,
816 struct mmc_data
*data
)
823 WARN_ON(!host
->data
);
825 mode
= SDHCI_TRNS_BLK_CNT_EN
;
826 if (data
->blocks
> 1) {
827 if (host
->quirks
& SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12
)
828 mode
|= SDHCI_TRNS_MULTI
| SDHCI_TRNS_ACMD12
;
830 mode
|= SDHCI_TRNS_MULTI
;
832 if (data
->flags
& MMC_DATA_READ
)
833 mode
|= SDHCI_TRNS_READ
;
834 if (host
->flags
& SDHCI_REQ_USE_DMA
)
835 mode
|= SDHCI_TRNS_DMA
;
837 sdhci_writew(host
, mode
, SDHCI_TRANSFER_MODE
);
840 static void sdhci_finish_data(struct sdhci_host
*host
)
842 struct mmc_data
*data
;
849 if (host
->flags
& SDHCI_REQ_USE_DMA
) {
850 if (host
->flags
& SDHCI_USE_ADMA
)
851 sdhci_adma_table_post(host
, data
);
853 dma_unmap_sg(mmc_dev(host
->mmc
), data
->sg
,
854 data
->sg_len
, (data
->flags
& MMC_DATA_READ
) ?
855 DMA_FROM_DEVICE
: DMA_TO_DEVICE
);
860 * The specification states that the block count register must
861 * be updated, but it does not specify at what point in the
862 * data flow. That makes the register entirely useless to read
863 * back so we have to assume that nothing made it to the card
864 * in the event of an error.
867 data
->bytes_xfered
= 0;
869 data
->bytes_xfered
= data
->blksz
* data
->blocks
;
873 * The controller needs a reset of internal state machines
874 * upon error conditions.
877 sdhci_reset(host
, SDHCI_RESET_CMD
);
878 sdhci_reset(host
, SDHCI_RESET_DATA
);
881 sdhci_send_command(host
, data
->stop
);
883 tasklet_schedule(&host
->finish_tasklet
);
886 static void sdhci_send_command(struct sdhci_host
*host
, struct mmc_command
*cmd
)
890 unsigned long timeout
;
897 mask
= SDHCI_CMD_INHIBIT
;
898 if ((cmd
->data
!= NULL
) || (cmd
->flags
& MMC_RSP_BUSY
))
899 mask
|= SDHCI_DATA_INHIBIT
;
901 /* We shouldn't wait for data inihibit for stop commands, even
902 though they might use busy signaling */
903 if (host
->mrq
->data
&& (cmd
== host
->mrq
->data
->stop
))
904 mask
&= ~SDHCI_DATA_INHIBIT
;
906 while (sdhci_readl(host
, SDHCI_PRESENT_STATE
) & mask
) {
908 printk(KERN_ERR
"%s: Controller never released "
909 "inhibit bit(s).\n", mmc_hostname(host
->mmc
));
910 sdhci_dumpregs(host
);
912 tasklet_schedule(&host
->finish_tasklet
);
919 mod_timer(&host
->timer
, jiffies
+ 10 * HZ
);
923 sdhci_prepare_data(host
, cmd
->data
);
925 sdhci_writel(host
, cmd
->arg
, SDHCI_ARGUMENT
);
927 sdhci_set_transfer_mode(host
, cmd
->data
);
929 if ((cmd
->flags
& MMC_RSP_136
) && (cmd
->flags
& MMC_RSP_BUSY
)) {
930 printk(KERN_ERR
"%s: Unsupported response type!\n",
931 mmc_hostname(host
->mmc
));
932 cmd
->error
= -EINVAL
;
933 tasklet_schedule(&host
->finish_tasklet
);
937 if (!(cmd
->flags
& MMC_RSP_PRESENT
))
938 flags
= SDHCI_CMD_RESP_NONE
;
939 else if (cmd
->flags
& MMC_RSP_136
)
940 flags
= SDHCI_CMD_RESP_LONG
;
941 else if (cmd
->flags
& MMC_RSP_BUSY
)
942 flags
= SDHCI_CMD_RESP_SHORT_BUSY
;
944 flags
= SDHCI_CMD_RESP_SHORT
;
946 if (cmd
->flags
& MMC_RSP_CRC
)
947 flags
|= SDHCI_CMD_CRC
;
948 if (cmd
->flags
& MMC_RSP_OPCODE
)
949 flags
|= SDHCI_CMD_INDEX
;
951 flags
|= SDHCI_CMD_DATA
;
953 sdhci_writew(host
, SDHCI_MAKE_CMD(cmd
->opcode
, flags
), SDHCI_COMMAND
);
956 static void sdhci_finish_command(struct sdhci_host
*host
)
960 BUG_ON(host
->cmd
== NULL
);
962 if (host
->cmd
->flags
& MMC_RSP_PRESENT
) {
963 if (host
->cmd
->flags
& MMC_RSP_136
) {
964 /* CRC is stripped so we need to do some shifting. */
965 for (i
= 0;i
< 4;i
++) {
966 host
->cmd
->resp
[i
] = sdhci_readl(host
,
967 SDHCI_RESPONSE
+ (3-i
)*4) << 8;
969 host
->cmd
->resp
[i
] |=
971 SDHCI_RESPONSE
+ (3-i
)*4-1);
974 host
->cmd
->resp
[0] = sdhci_readl(host
, SDHCI_RESPONSE
);
978 host
->cmd
->error
= 0;
980 if (host
->data
&& host
->data_early
)
981 sdhci_finish_data(host
);
983 if (!host
->cmd
->data
)
984 tasklet_schedule(&host
->finish_tasklet
);
989 static void sdhci_set_clock(struct sdhci_host
*host
, unsigned int clock
)
993 unsigned long timeout
;
995 if (clock
== host
->clock
)
998 if (host
->ops
->set_clock
) {
999 host
->ops
->set_clock(host
, clock
);
1000 if (host
->quirks
& SDHCI_QUIRK_NONSTANDARD_CLOCK
)
1004 sdhci_writew(host
, 0, SDHCI_CLOCK_CONTROL
);
1009 if (host
->version
>= SDHCI_SPEC_300
) {
1010 /* Version 3.00 divisors must be a multiple of 2. */
1011 if (host
->max_clk
<= clock
)
1014 for (div
= 2; div
< SDHCI_MAX_DIV_SPEC_300
; div
+= 2) {
1015 if ((host
->max_clk
/ div
) <= clock
)
1020 /* Version 2.00 divisors must be a power of 2. */
1021 for (div
= 1; div
< SDHCI_MAX_DIV_SPEC_200
; div
*= 2) {
1022 if ((host
->max_clk
/ div
) <= clock
)
1028 clk
= (div
& SDHCI_DIV_MASK
) << SDHCI_DIVIDER_SHIFT
;
1029 clk
|= ((div
& SDHCI_DIV_HI_MASK
) >> SDHCI_DIV_MASK_LEN
)
1030 << SDHCI_DIVIDER_HI_SHIFT
;
1031 clk
|= SDHCI_CLOCK_INT_EN
;
1032 sdhci_writew(host
, clk
, SDHCI_CLOCK_CONTROL
);
1034 /* Wait max 20 ms */
1036 while (!((clk
= sdhci_readw(host
, SDHCI_CLOCK_CONTROL
))
1037 & SDHCI_CLOCK_INT_STABLE
)) {
1039 printk(KERN_ERR
"%s: Internal clock never "
1040 "stabilised.\n", mmc_hostname(host
->mmc
));
1041 sdhci_dumpregs(host
);
1048 clk
|= SDHCI_CLOCK_CARD_EN
;
1049 sdhci_writew(host
, clk
, SDHCI_CLOCK_CONTROL
);
1052 host
->clock
= clock
;
1055 static void sdhci_set_power(struct sdhci_host
*host
, unsigned short power
)
1059 if (power
!= (unsigned short)-1) {
1060 switch (1 << power
) {
1061 case MMC_VDD_165_195
:
1062 pwr
= SDHCI_POWER_180
;
1066 pwr
= SDHCI_POWER_300
;
1070 pwr
= SDHCI_POWER_330
;
1077 if (host
->pwr
== pwr
)
1083 sdhci_writeb(host
, 0, SDHCI_POWER_CONTROL
);
1088 * Spec says that we should clear the power reg before setting
1089 * a new value. Some controllers don't seem to like this though.
1091 if (!(host
->quirks
& SDHCI_QUIRK_SINGLE_POWER_WRITE
))
1092 sdhci_writeb(host
, 0, SDHCI_POWER_CONTROL
);
1095 * At least the Marvell CaFe chip gets confused if we set the voltage
1096 * and set turn on power at the same time, so set the voltage first.
1098 if (host
->quirks
& SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER
)
1099 sdhci_writeb(host
, pwr
, SDHCI_POWER_CONTROL
);
1101 pwr
|= SDHCI_POWER_ON
;
1103 sdhci_writeb(host
, pwr
, SDHCI_POWER_CONTROL
);
1106 * Some controllers need an extra 10ms delay of 10ms before they
1107 * can apply clock after applying power
1109 if (host
->quirks
& SDHCI_QUIRK_DELAY_AFTER_POWER
)
1113 /*****************************************************************************\
1117 \*****************************************************************************/
1119 static void sdhci_request(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
1121 struct sdhci_host
*host
;
1123 unsigned long flags
;
1125 host
= mmc_priv(mmc
);
1127 spin_lock_irqsave(&host
->lock
, flags
);
1129 WARN_ON(host
->mrq
!= NULL
);
1131 #ifndef SDHCI_USE_LEDS_CLASS
1132 sdhci_activate_led(host
);
1134 if (host
->quirks
& SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12
) {
1136 mrq
->data
->stop
= NULL
;
1143 /* If polling, assume that the card is always present. */
1144 if (host
->quirks
& SDHCI_QUIRK_BROKEN_CARD_DETECTION
)
1147 present
= sdhci_readl(host
, SDHCI_PRESENT_STATE
) &
1150 if (!present
|| host
->flags
& SDHCI_DEVICE_DEAD
) {
1151 host
->mrq
->cmd
->error
= -ENOMEDIUM
;
1152 tasklet_schedule(&host
->finish_tasklet
);
1154 sdhci_send_command(host
, mrq
->cmd
);
1157 spin_unlock_irqrestore(&host
->lock
, flags
);
1160 static void sdhci_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
1162 struct sdhci_host
*host
;
1163 unsigned long flags
;
1166 host
= mmc_priv(mmc
);
1168 spin_lock_irqsave(&host
->lock
, flags
);
1170 if (host
->flags
& SDHCI_DEVICE_DEAD
)
1174 * Reset the chip on each power off.
1175 * Should clear out any weird states.
1177 if (ios
->power_mode
== MMC_POWER_OFF
) {
1178 sdhci_writel(host
, 0, SDHCI_SIGNAL_ENABLE
);
1182 sdhci_set_clock(host
, ios
->clock
);
1184 if (ios
->power_mode
== MMC_POWER_OFF
)
1185 sdhci_set_power(host
, -1);
1187 sdhci_set_power(host
, ios
->vdd
);
1189 if (host
->ops
->platform_send_init_74_clocks
)
1190 host
->ops
->platform_send_init_74_clocks(host
, ios
->power_mode
);
1193 * If your platform has 8-bit width support but is not a v3 controller,
1194 * or if it requires special setup code, you should implement that in
1195 * platform_8bit_width().
1197 if (host
->ops
->platform_8bit_width
)
1198 host
->ops
->platform_8bit_width(host
, ios
->bus_width
);
1200 ctrl
= sdhci_readb(host
, SDHCI_HOST_CONTROL
);
1201 if (ios
->bus_width
== MMC_BUS_WIDTH_8
) {
1202 ctrl
&= ~SDHCI_CTRL_4BITBUS
;
1203 if (host
->version
>= SDHCI_SPEC_300
)
1204 ctrl
|= SDHCI_CTRL_8BITBUS
;
1206 if (host
->version
>= SDHCI_SPEC_300
)
1207 ctrl
&= ~SDHCI_CTRL_8BITBUS
;
1208 if (ios
->bus_width
== MMC_BUS_WIDTH_4
)
1209 ctrl
|= SDHCI_CTRL_4BITBUS
;
1211 ctrl
&= ~SDHCI_CTRL_4BITBUS
;
1213 sdhci_writeb(host
, ctrl
, SDHCI_HOST_CONTROL
);
1216 ctrl
= sdhci_readb(host
, SDHCI_HOST_CONTROL
);
1218 if ((ios
->timing
== MMC_TIMING_SD_HS
||
1219 ios
->timing
== MMC_TIMING_MMC_HS
)
1220 && !(host
->quirks
& SDHCI_QUIRK_NO_HISPD_BIT
))
1221 ctrl
|= SDHCI_CTRL_HISPD
;
1223 ctrl
&= ~SDHCI_CTRL_HISPD
;
1225 sdhci_writeb(host
, ctrl
, SDHCI_HOST_CONTROL
);
1228 * Some (ENE) controllers go apeshit on some ios operation,
1229 * signalling timeout and CRC errors even on CMD0. Resetting
1230 * it on each ios seems to solve the problem.
1232 if(host
->quirks
& SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS
)
1233 sdhci_reset(host
, SDHCI_RESET_CMD
| SDHCI_RESET_DATA
);
1237 spin_unlock_irqrestore(&host
->lock
, flags
);
1240 static int sdhci_get_ro(struct mmc_host
*mmc
)
1242 struct sdhci_host
*host
;
1243 unsigned long flags
;
1246 host
= mmc_priv(mmc
);
1248 spin_lock_irqsave(&host
->lock
, flags
);
1250 if (host
->flags
& SDHCI_DEVICE_DEAD
)
1252 else if (host
->ops
->get_ro
)
1253 is_readonly
= host
->ops
->get_ro(host
);
1255 is_readonly
= !(sdhci_readl(host
, SDHCI_PRESENT_STATE
)
1256 & SDHCI_WRITE_PROTECT
);
1258 spin_unlock_irqrestore(&host
->lock
, flags
);
1260 /* This quirk needs to be replaced by a callback-function later */
1261 return host
->quirks
& SDHCI_QUIRK_INVERTED_WRITE_PROTECT
?
1262 !is_readonly
: is_readonly
;
1265 static void sdhci_enable_sdio_irq(struct mmc_host
*mmc
, int enable
)
1267 struct sdhci_host
*host
;
1268 unsigned long flags
;
1270 host
= mmc_priv(mmc
);
1272 spin_lock_irqsave(&host
->lock
, flags
);
1274 if (host
->flags
& SDHCI_DEVICE_DEAD
)
1278 sdhci_unmask_irqs(host
, SDHCI_INT_CARD_INT
);
1280 sdhci_mask_irqs(host
, SDHCI_INT_CARD_INT
);
1284 spin_unlock_irqrestore(&host
->lock
, flags
);
1287 static const struct mmc_host_ops sdhci_ops
= {
1288 .request
= sdhci_request
,
1289 .set_ios
= sdhci_set_ios
,
1290 .get_ro
= sdhci_get_ro
,
1291 .enable_sdio_irq
= sdhci_enable_sdio_irq
,
1294 /*****************************************************************************\
1298 \*****************************************************************************/
1300 static void sdhci_tasklet_card(unsigned long param
)
1302 struct sdhci_host
*host
;
1303 unsigned long flags
;
1305 host
= (struct sdhci_host
*)param
;
1307 spin_lock_irqsave(&host
->lock
, flags
);
1309 if (!(sdhci_readl(host
, SDHCI_PRESENT_STATE
) & SDHCI_CARD_PRESENT
)) {
1311 printk(KERN_ERR
"%s: Card removed during transfer!\n",
1312 mmc_hostname(host
->mmc
));
1313 printk(KERN_ERR
"%s: Resetting controller.\n",
1314 mmc_hostname(host
->mmc
));
1316 sdhci_reset(host
, SDHCI_RESET_CMD
);
1317 sdhci_reset(host
, SDHCI_RESET_DATA
);
1319 host
->mrq
->cmd
->error
= -ENOMEDIUM
;
1320 tasklet_schedule(&host
->finish_tasklet
);
1324 spin_unlock_irqrestore(&host
->lock
, flags
);
1326 mmc_detect_change(host
->mmc
, msecs_to_jiffies(200));
1329 static void sdhci_tasklet_finish(unsigned long param
)
1331 struct sdhci_host
*host
;
1332 unsigned long flags
;
1333 struct mmc_request
*mrq
;
1335 host
= (struct sdhci_host
*)param
;
1338 * If this tasklet gets rescheduled while running, it will
1339 * be run again afterwards but without any active request.
1344 spin_lock_irqsave(&host
->lock
, flags
);
1346 del_timer(&host
->timer
);
1351 * The controller needs a reset of internal state machines
1352 * upon error conditions.
1354 if (!(host
->flags
& SDHCI_DEVICE_DEAD
) &&
1355 ((mrq
->cmd
&& mrq
->cmd
->error
) ||
1356 (mrq
->data
&& (mrq
->data
->error
||
1357 (mrq
->data
->stop
&& mrq
->data
->stop
->error
))) ||
1358 (host
->quirks
& SDHCI_QUIRK_RESET_AFTER_REQUEST
))) {
1360 /* Some controllers need this kick or reset won't work here */
1361 if (host
->quirks
& SDHCI_QUIRK_CLOCK_BEFORE_RESET
) {
1364 /* This is to force an update */
1365 clock
= host
->clock
;
1367 sdhci_set_clock(host
, clock
);
1370 /* Spec says we should do both at the same time, but Ricoh
1371 controllers do not like that. */
1372 sdhci_reset(host
, SDHCI_RESET_CMD
);
1373 sdhci_reset(host
, SDHCI_RESET_DATA
);
1380 #ifndef SDHCI_USE_LEDS_CLASS
1381 sdhci_deactivate_led(host
);
1385 spin_unlock_irqrestore(&host
->lock
, flags
);
1387 mmc_request_done(host
->mmc
, mrq
);
1390 static void sdhci_timeout_timer(unsigned long data
)
1392 struct sdhci_host
*host
;
1393 unsigned long flags
;
1395 host
= (struct sdhci_host
*)data
;
1397 spin_lock_irqsave(&host
->lock
, flags
);
1400 printk(KERN_ERR
"%s: Timeout waiting for hardware "
1401 "interrupt.\n", mmc_hostname(host
->mmc
));
1402 sdhci_dumpregs(host
);
1405 host
->data
->error
= -ETIMEDOUT
;
1406 sdhci_finish_data(host
);
1409 host
->cmd
->error
= -ETIMEDOUT
;
1411 host
->mrq
->cmd
->error
= -ETIMEDOUT
;
1413 tasklet_schedule(&host
->finish_tasklet
);
1418 spin_unlock_irqrestore(&host
->lock
, flags
);
1421 /*****************************************************************************\
1423 * Interrupt handling *
1425 \*****************************************************************************/
1427 static void sdhci_cmd_irq(struct sdhci_host
*host
, u32 intmask
)
1429 BUG_ON(intmask
== 0);
1432 printk(KERN_ERR
"%s: Got command interrupt 0x%08x even "
1433 "though no command operation was in progress.\n",
1434 mmc_hostname(host
->mmc
), (unsigned)intmask
);
1435 sdhci_dumpregs(host
);
1439 if (intmask
& SDHCI_INT_TIMEOUT
)
1440 host
->cmd
->error
= -ETIMEDOUT
;
1441 else if (intmask
& (SDHCI_INT_CRC
| SDHCI_INT_END_BIT
|
1443 host
->cmd
->error
= -EILSEQ
;
1445 if (host
->cmd
->error
) {
1446 tasklet_schedule(&host
->finish_tasklet
);
1451 * The host can send and interrupt when the busy state has
1452 * ended, allowing us to wait without wasting CPU cycles.
1453 * Unfortunately this is overloaded on the "data complete"
1454 * interrupt, so we need to take some care when handling
1457 * Note: The 1.0 specification is a bit ambiguous about this
1458 * feature so there might be some problems with older
1461 if (host
->cmd
->flags
& MMC_RSP_BUSY
) {
1462 if (host
->cmd
->data
)
1463 DBG("Cannot wait for busy signal when also "
1464 "doing a data transfer");
1465 else if (!(host
->quirks
& SDHCI_QUIRK_NO_BUSY_IRQ
))
1468 /* The controller does not support the end-of-busy IRQ,
1469 * fall through and take the SDHCI_INT_RESPONSE */
1472 if (intmask
& SDHCI_INT_RESPONSE
)
1473 sdhci_finish_command(host
);
1476 #ifdef CONFIG_MMC_DEBUG
1477 static void sdhci_show_adma_error(struct sdhci_host
*host
)
1479 const char *name
= mmc_hostname(host
->mmc
);
1480 u8
*desc
= host
->adma_desc
;
1485 sdhci_dumpregs(host
);
1488 dma
= (__le32
*)(desc
+ 4);
1489 len
= (__le16
*)(desc
+ 2);
1492 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
1493 name
, desc
, le32_to_cpu(*dma
), le16_to_cpu(*len
), attr
);
1502 static void sdhci_show_adma_error(struct sdhci_host
*host
) { }
1505 static void sdhci_data_irq(struct sdhci_host
*host
, u32 intmask
)
1507 BUG_ON(intmask
== 0);
1511 * The "data complete" interrupt is also used to
1512 * indicate that a busy state has ended. See comment
1513 * above in sdhci_cmd_irq().
1515 if (host
->cmd
&& (host
->cmd
->flags
& MMC_RSP_BUSY
)) {
1516 if (intmask
& SDHCI_INT_DATA_END
) {
1517 sdhci_finish_command(host
);
1522 printk(KERN_ERR
"%s: Got data interrupt 0x%08x even "
1523 "though no data operation was in progress.\n",
1524 mmc_hostname(host
->mmc
), (unsigned)intmask
);
1525 sdhci_dumpregs(host
);
1530 if (intmask
& SDHCI_INT_DATA_TIMEOUT
)
1531 host
->data
->error
= -ETIMEDOUT
;
1532 else if (intmask
& SDHCI_INT_DATA_END_BIT
)
1533 host
->data
->error
= -EILSEQ
;
1534 else if ((intmask
& SDHCI_INT_DATA_CRC
) &&
1535 SDHCI_GET_CMD(sdhci_readw(host
, SDHCI_COMMAND
))
1537 host
->data
->error
= -EILSEQ
;
1538 else if (intmask
& SDHCI_INT_ADMA_ERROR
) {
1539 printk(KERN_ERR
"%s: ADMA error\n", mmc_hostname(host
->mmc
));
1540 sdhci_show_adma_error(host
);
1541 host
->data
->error
= -EIO
;
1544 if (host
->data
->error
)
1545 sdhci_finish_data(host
);
1547 if (intmask
& (SDHCI_INT_DATA_AVAIL
| SDHCI_INT_SPACE_AVAIL
))
1548 sdhci_transfer_pio(host
);
1551 * We currently don't do anything fancy with DMA
1552 * boundaries, but as we can't disable the feature
1553 * we need to at least restart the transfer.
1555 if (intmask
& SDHCI_INT_DMA_END
)
1556 sdhci_writel(host
, sdhci_readl(host
, SDHCI_DMA_ADDRESS
),
1559 if (intmask
& SDHCI_INT_DATA_END
) {
1562 * Data managed to finish before the
1563 * command completed. Make sure we do
1564 * things in the proper order.
1566 host
->data_early
= 1;
1568 sdhci_finish_data(host
);
1574 static irqreturn_t
sdhci_irq(int irq
, void *dev_id
)
1577 struct sdhci_host
* host
= dev_id
;
1581 spin_lock(&host
->lock
);
1583 intmask
= sdhci_readl(host
, SDHCI_INT_STATUS
);
1585 if (!intmask
|| intmask
== 0xffffffff) {
1590 DBG("*** %s got interrupt: 0x%08x\n",
1591 mmc_hostname(host
->mmc
), intmask
);
1593 if (intmask
& (SDHCI_INT_CARD_INSERT
| SDHCI_INT_CARD_REMOVE
)) {
1594 sdhci_writel(host
, intmask
& (SDHCI_INT_CARD_INSERT
|
1595 SDHCI_INT_CARD_REMOVE
), SDHCI_INT_STATUS
);
1596 tasklet_schedule(&host
->card_tasklet
);
1599 intmask
&= ~(SDHCI_INT_CARD_INSERT
| SDHCI_INT_CARD_REMOVE
);
1601 if (intmask
& SDHCI_INT_CMD_MASK
) {
1602 sdhci_writel(host
, intmask
& SDHCI_INT_CMD_MASK
,
1604 sdhci_cmd_irq(host
, intmask
& SDHCI_INT_CMD_MASK
);
1607 if (intmask
& SDHCI_INT_DATA_MASK
) {
1608 sdhci_writel(host
, intmask
& SDHCI_INT_DATA_MASK
,
1610 sdhci_data_irq(host
, intmask
& SDHCI_INT_DATA_MASK
);
1613 intmask
&= ~(SDHCI_INT_CMD_MASK
| SDHCI_INT_DATA_MASK
);
1615 intmask
&= ~SDHCI_INT_ERROR
;
1617 if (intmask
& SDHCI_INT_BUS_POWER
) {
1618 printk(KERN_ERR
"%s: Card is consuming too much power!\n",
1619 mmc_hostname(host
->mmc
));
1620 sdhci_writel(host
, SDHCI_INT_BUS_POWER
, SDHCI_INT_STATUS
);
1623 intmask
&= ~SDHCI_INT_BUS_POWER
;
1625 if (intmask
& SDHCI_INT_CARD_INT
)
1628 intmask
&= ~SDHCI_INT_CARD_INT
;
1631 printk(KERN_ERR
"%s: Unexpected interrupt 0x%08x.\n",
1632 mmc_hostname(host
->mmc
), intmask
);
1633 sdhci_dumpregs(host
);
1635 sdhci_writel(host
, intmask
, SDHCI_INT_STATUS
);
1638 result
= IRQ_HANDLED
;
1642 spin_unlock(&host
->lock
);
1645 * We have to delay this as it calls back into the driver.
1648 mmc_signal_sdio_irq(host
->mmc
);
1653 /*****************************************************************************\
1657 \*****************************************************************************/
1661 int sdhci_suspend_host(struct sdhci_host
*host
, pm_message_t state
)
1665 sdhci_disable_card_detection(host
);
1667 ret
= mmc_suspend_host(host
->mmc
);
1671 free_irq(host
->irq
, host
);
1674 ret
= regulator_disable(host
->vmmc
);
1679 EXPORT_SYMBOL_GPL(sdhci_suspend_host
);
1681 int sdhci_resume_host(struct sdhci_host
*host
)
1686 int ret
= regulator_enable(host
->vmmc
);
1692 if (host
->flags
& (SDHCI_USE_SDMA
| SDHCI_USE_ADMA
)) {
1693 if (host
->ops
->enable_dma
)
1694 host
->ops
->enable_dma(host
);
1697 ret
= request_irq(host
->irq
, sdhci_irq
, IRQF_SHARED
,
1698 mmc_hostname(host
->mmc
), host
);
1702 sdhci_init(host
, (host
->mmc
->pm_flags
& MMC_PM_KEEP_POWER
));
1705 ret
= mmc_resume_host(host
->mmc
);
1706 sdhci_enable_card_detection(host
);
1711 EXPORT_SYMBOL_GPL(sdhci_resume_host
);
1713 void sdhci_enable_irq_wakeups(struct sdhci_host
*host
)
1716 val
= sdhci_readb(host
, SDHCI_WAKE_UP_CONTROL
);
1717 val
|= SDHCI_WAKE_ON_INT
;
1718 sdhci_writeb(host
, val
, SDHCI_WAKE_UP_CONTROL
);
1721 EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups
);
1723 #endif /* CONFIG_PM */
1725 /*****************************************************************************\
1727 * Device allocation/registration *
1729 \*****************************************************************************/
1731 struct sdhci_host
*sdhci_alloc_host(struct device
*dev
,
1734 struct mmc_host
*mmc
;
1735 struct sdhci_host
*host
;
1737 WARN_ON(dev
== NULL
);
1739 mmc
= mmc_alloc_host(sizeof(struct sdhci_host
) + priv_size
, dev
);
1741 return ERR_PTR(-ENOMEM
);
1743 host
= mmc_priv(mmc
);
1749 EXPORT_SYMBOL_GPL(sdhci_alloc_host
);
1751 int sdhci_add_host(struct sdhci_host
*host
)
1753 struct mmc_host
*mmc
;
1754 unsigned int caps
, ocr_avail
;
1757 WARN_ON(host
== NULL
);
1764 host
->quirks
= debug_quirks
;
1766 sdhci_reset(host
, SDHCI_RESET_ALL
);
1768 host
->version
= sdhci_readw(host
, SDHCI_HOST_VERSION
);
1769 host
->version
= (host
->version
& SDHCI_SPEC_VER_MASK
)
1770 >> SDHCI_SPEC_VER_SHIFT
;
1771 if (host
->version
> SDHCI_SPEC_300
) {
1772 printk(KERN_ERR
"%s: Unknown controller version (%d). "
1773 "You may experience problems.\n", mmc_hostname(mmc
),
1777 caps
= (host
->quirks
& SDHCI_QUIRK_MISSING_CAPS
) ? host
->caps
:
1778 sdhci_readl(host
, SDHCI_CAPABILITIES
);
1780 if (host
->quirks
& SDHCI_QUIRK_FORCE_DMA
)
1781 host
->flags
|= SDHCI_USE_SDMA
;
1782 else if (!(caps
& SDHCI_CAN_DO_SDMA
))
1783 DBG("Controller doesn't have SDMA capability\n");
1785 host
->flags
|= SDHCI_USE_SDMA
;
1787 if ((host
->quirks
& SDHCI_QUIRK_BROKEN_DMA
) &&
1788 (host
->flags
& SDHCI_USE_SDMA
)) {
1789 DBG("Disabling DMA as it is marked broken\n");
1790 host
->flags
&= ~SDHCI_USE_SDMA
;
1793 if ((host
->version
>= SDHCI_SPEC_200
) && (caps
& SDHCI_CAN_DO_ADMA2
))
1794 host
->flags
|= SDHCI_USE_ADMA
;
1796 if ((host
->quirks
& SDHCI_QUIRK_BROKEN_ADMA
) &&
1797 (host
->flags
& SDHCI_USE_ADMA
)) {
1798 DBG("Disabling ADMA as it is marked broken\n");
1799 host
->flags
&= ~SDHCI_USE_ADMA
;
1802 if (host
->flags
& (SDHCI_USE_SDMA
| SDHCI_USE_ADMA
)) {
1803 if (host
->ops
->enable_dma
) {
1804 if (host
->ops
->enable_dma(host
)) {
1805 printk(KERN_WARNING
"%s: No suitable DMA "
1806 "available. Falling back to PIO.\n",
1809 ~(SDHCI_USE_SDMA
| SDHCI_USE_ADMA
);
1814 if (host
->flags
& SDHCI_USE_ADMA
) {
1816 * We need to allocate descriptors for all sg entries
1817 * (128) and potentially one alignment transfer for
1818 * each of those entries.
1820 host
->adma_desc
= kmalloc((128 * 2 + 1) * 4, GFP_KERNEL
);
1821 host
->align_buffer
= kmalloc(128 * 4, GFP_KERNEL
);
1822 if (!host
->adma_desc
|| !host
->align_buffer
) {
1823 kfree(host
->adma_desc
);
1824 kfree(host
->align_buffer
);
1825 printk(KERN_WARNING
"%s: Unable to allocate ADMA "
1826 "buffers. Falling back to standard DMA.\n",
1828 host
->flags
&= ~SDHCI_USE_ADMA
;
1833 * If we use DMA, then it's up to the caller to set the DMA
1834 * mask, but PIO does not need the hw shim so we set a new
1835 * mask here in that case.
1837 if (!(host
->flags
& (SDHCI_USE_SDMA
| SDHCI_USE_ADMA
))) {
1838 host
->dma_mask
= DMA_BIT_MASK(64);
1839 mmc_dev(host
->mmc
)->dma_mask
= &host
->dma_mask
;
1842 if (host
->version
>= SDHCI_SPEC_300
)
1843 host
->max_clk
= (caps
& SDHCI_CLOCK_V3_BASE_MASK
)
1844 >> SDHCI_CLOCK_BASE_SHIFT
;
1846 host
->max_clk
= (caps
& SDHCI_CLOCK_BASE_MASK
)
1847 >> SDHCI_CLOCK_BASE_SHIFT
;
1849 host
->max_clk
*= 1000000;
1850 if (host
->max_clk
== 0 || host
->quirks
&
1851 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN
) {
1852 if (!host
->ops
->get_max_clock
) {
1854 "%s: Hardware doesn't specify base clock "
1855 "frequency.\n", mmc_hostname(mmc
));
1858 host
->max_clk
= host
->ops
->get_max_clock(host
);
1862 (caps
& SDHCI_TIMEOUT_CLK_MASK
) >> SDHCI_TIMEOUT_CLK_SHIFT
;
1863 if (host
->timeout_clk
== 0) {
1864 if (host
->ops
->get_timeout_clock
) {
1865 host
->timeout_clk
= host
->ops
->get_timeout_clock(host
);
1866 } else if (!(host
->quirks
&
1867 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK
)) {
1869 "%s: Hardware doesn't specify timeout clock "
1870 "frequency.\n", mmc_hostname(mmc
));
1874 if (caps
& SDHCI_TIMEOUT_CLK_UNIT
)
1875 host
->timeout_clk
*= 1000;
1878 * Set host parameters.
1880 mmc
->ops
= &sdhci_ops
;
1881 if (host
->ops
->get_min_clock
)
1882 mmc
->f_min
= host
->ops
->get_min_clock(host
);
1883 else if (host
->version
>= SDHCI_SPEC_300
)
1884 mmc
->f_min
= host
->max_clk
/ SDHCI_MAX_DIV_SPEC_300
;
1886 mmc
->f_min
= host
->max_clk
/ SDHCI_MAX_DIV_SPEC_200
;
1888 mmc
->f_max
= host
->max_clk
;
1889 mmc
->caps
|= MMC_CAP_SDIO_IRQ
;
1892 * A controller may support 8-bit width, but the board itself
1893 * might not have the pins brought out. Boards that support
1894 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
1895 * their platform code before calling sdhci_add_host(), and we
1896 * won't assume 8-bit width for hosts without that CAP.
1898 if (!(host
->quirks
& SDHCI_QUIRK_FORCE_1_BIT_DATA
))
1899 mmc
->caps
|= MMC_CAP_4_BIT_DATA
;
1901 if (caps
& SDHCI_CAN_DO_HISPD
)
1902 mmc
->caps
|= MMC_CAP_SD_HIGHSPEED
| MMC_CAP_MMC_HIGHSPEED
;
1904 if ((host
->quirks
& SDHCI_QUIRK_BROKEN_CARD_DETECTION
) &&
1905 mmc_card_is_removable(mmc
))
1906 mmc
->caps
|= MMC_CAP_NEEDS_POLL
;
1909 if (caps
& SDHCI_CAN_VDD_330
)
1910 ocr_avail
|= MMC_VDD_32_33
| MMC_VDD_33_34
;
1911 if (caps
& SDHCI_CAN_VDD_300
)
1912 ocr_avail
|= MMC_VDD_29_30
| MMC_VDD_30_31
;
1913 if (caps
& SDHCI_CAN_VDD_180
)
1914 ocr_avail
|= MMC_VDD_165_195
;
1916 mmc
->ocr_avail
= ocr_avail
;
1917 mmc
->ocr_avail_sdio
= ocr_avail
;
1918 if (host
->ocr_avail_sdio
)
1919 mmc
->ocr_avail_sdio
&= host
->ocr_avail_sdio
;
1920 mmc
->ocr_avail_sd
= ocr_avail
;
1921 if (host
->ocr_avail_sd
)
1922 mmc
->ocr_avail_sd
&= host
->ocr_avail_sd
;
1923 else /* normal SD controllers don't support 1.8V */
1924 mmc
->ocr_avail_sd
&= ~MMC_VDD_165_195
;
1925 mmc
->ocr_avail_mmc
= ocr_avail
;
1926 if (host
->ocr_avail_mmc
)
1927 mmc
->ocr_avail_mmc
&= host
->ocr_avail_mmc
;
1929 if (mmc
->ocr_avail
== 0) {
1930 printk(KERN_ERR
"%s: Hardware doesn't report any "
1931 "support voltages.\n", mmc_hostname(mmc
));
1935 spin_lock_init(&host
->lock
);
1938 * Maximum number of segments. Depends on if the hardware
1939 * can do scatter/gather or not.
1941 if (host
->flags
& SDHCI_USE_ADMA
)
1942 mmc
->max_segs
= 128;
1943 else if (host
->flags
& SDHCI_USE_SDMA
)
1946 mmc
->max_segs
= 128;
1949 * Maximum number of sectors in one transfer. Limited by DMA boundary
1952 mmc
->max_req_size
= 524288;
1955 * Maximum segment size. Could be one segment with the maximum number
1956 * of bytes. When doing hardware scatter/gather, each entry cannot
1957 * be larger than 64 KiB though.
1959 if (host
->flags
& SDHCI_USE_ADMA
) {
1960 if (host
->quirks
& SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC
)
1961 mmc
->max_seg_size
= 65535;
1963 mmc
->max_seg_size
= 65536;
1965 mmc
->max_seg_size
= mmc
->max_req_size
;
1969 * Maximum block size. This varies from controller to controller and
1970 * is specified in the capabilities register.
1972 if (host
->quirks
& SDHCI_QUIRK_FORCE_BLK_SZ_2048
) {
1973 mmc
->max_blk_size
= 2;
1975 mmc
->max_blk_size
= (caps
& SDHCI_MAX_BLOCK_MASK
) >>
1976 SDHCI_MAX_BLOCK_SHIFT
;
1977 if (mmc
->max_blk_size
>= 3) {
1978 printk(KERN_WARNING
"%s: Invalid maximum block size, "
1979 "assuming 512 bytes\n", mmc_hostname(mmc
));
1980 mmc
->max_blk_size
= 0;
1984 mmc
->max_blk_size
= 512 << mmc
->max_blk_size
;
1987 * Maximum block count.
1989 mmc
->max_blk_count
= (host
->quirks
& SDHCI_QUIRK_NO_MULTIBLOCK
) ? 1 : 65535;
1994 tasklet_init(&host
->card_tasklet
,
1995 sdhci_tasklet_card
, (unsigned long)host
);
1996 tasklet_init(&host
->finish_tasklet
,
1997 sdhci_tasklet_finish
, (unsigned long)host
);
1999 setup_timer(&host
->timer
, sdhci_timeout_timer
, (unsigned long)host
);
2001 ret
= request_irq(host
->irq
, sdhci_irq
, IRQF_SHARED
,
2002 mmc_hostname(mmc
), host
);
2006 host
->vmmc
= regulator_get(mmc_dev(mmc
), "vmmc");
2007 if (IS_ERR(host
->vmmc
)) {
2008 printk(KERN_INFO
"%s: no vmmc regulator found\n", mmc_hostname(mmc
));
2011 regulator_enable(host
->vmmc
);
2014 sdhci_init(host
, 0);
2016 #ifdef CONFIG_MMC_DEBUG
2017 sdhci_dumpregs(host
);
2020 #ifdef SDHCI_USE_LEDS_CLASS
2021 snprintf(host
->led_name
, sizeof(host
->led_name
),
2022 "%s::", mmc_hostname(mmc
));
2023 host
->led
.name
= host
->led_name
;
2024 host
->led
.brightness
= LED_OFF
;
2025 host
->led
.default_trigger
= mmc_hostname(mmc
);
2026 host
->led
.brightness_set
= sdhci_led_control
;
2028 ret
= led_classdev_register(mmc_dev(mmc
), &host
->led
);
2037 printk(KERN_INFO
"%s: SDHCI controller on %s [%s] using %s\n",
2038 mmc_hostname(mmc
), host
->hw_name
, dev_name(mmc_dev(mmc
)),
2039 (host
->flags
& SDHCI_USE_ADMA
) ? "ADMA" :
2040 (host
->flags
& SDHCI_USE_SDMA
) ? "DMA" : "PIO");
2042 sdhci_enable_card_detection(host
);
2046 #ifdef SDHCI_USE_LEDS_CLASS
2048 sdhci_reset(host
, SDHCI_RESET_ALL
);
2049 free_irq(host
->irq
, host
);
2052 tasklet_kill(&host
->card_tasklet
);
2053 tasklet_kill(&host
->finish_tasklet
);
2058 EXPORT_SYMBOL_GPL(sdhci_add_host
);
2060 void sdhci_remove_host(struct sdhci_host
*host
, int dead
)
2062 unsigned long flags
;
2065 spin_lock_irqsave(&host
->lock
, flags
);
2067 host
->flags
|= SDHCI_DEVICE_DEAD
;
2070 printk(KERN_ERR
"%s: Controller removed during "
2071 " transfer!\n", mmc_hostname(host
->mmc
));
2073 host
->mrq
->cmd
->error
= -ENOMEDIUM
;
2074 tasklet_schedule(&host
->finish_tasklet
);
2077 spin_unlock_irqrestore(&host
->lock
, flags
);
2080 sdhci_disable_card_detection(host
);
2082 mmc_remove_host(host
->mmc
);
2084 #ifdef SDHCI_USE_LEDS_CLASS
2085 led_classdev_unregister(&host
->led
);
2089 sdhci_reset(host
, SDHCI_RESET_ALL
);
2091 free_irq(host
->irq
, host
);
2093 del_timer_sync(&host
->timer
);
2095 tasklet_kill(&host
->card_tasklet
);
2096 tasklet_kill(&host
->finish_tasklet
);
2099 regulator_disable(host
->vmmc
);
2100 regulator_put(host
->vmmc
);
2103 kfree(host
->adma_desc
);
2104 kfree(host
->align_buffer
);
2106 host
->adma_desc
= NULL
;
2107 host
->align_buffer
= NULL
;
2110 EXPORT_SYMBOL_GPL(sdhci_remove_host
);
2112 void sdhci_free_host(struct sdhci_host
*host
)
2114 mmc_free_host(host
->mmc
);
2117 EXPORT_SYMBOL_GPL(sdhci_free_host
);
2119 /*****************************************************************************\
2121 * Driver init/exit *
2123 \*****************************************************************************/
2125 static int __init
sdhci_drv_init(void)
2127 printk(KERN_INFO DRIVER_NAME
2128 ": Secure Digital Host Controller Interface driver\n");
2129 printk(KERN_INFO DRIVER_NAME
": Copyright(c) Pierre Ossman\n");
2134 static void __exit
sdhci_drv_exit(void)
2138 module_init(sdhci_drv_init
);
2139 module_exit(sdhci_drv_exit
);
2141 module_param(debug_quirks
, uint
, 0444);
2143 MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
2144 MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
2145 MODULE_LICENSE("GPL");
2147 MODULE_PARM_DESC(debug_quirks
, "Force certain quirks.");