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/module.h>
20 #include <linux/dma-mapping.h>
21 #include <linux/slab.h>
22 #include <linux/scatterlist.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/pm_runtime.h>
26 #include <linux/leds.h>
28 #include <linux/mmc/mmc.h>
29 #include <linux/mmc/host.h>
30 #include <linux/mmc/card.h>
31 #include <linux/mmc/slot-gpio.h>
35 #define DRIVER_NAME "sdhci"
37 #define DBG(f, x...) \
38 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
40 #if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
41 defined(CONFIG_MMC_SDHCI_MODULE))
42 #define SDHCI_USE_LEDS_CLASS
45 #define MAX_TUNING_LOOP 40
47 static unsigned int debug_quirks
= 0;
48 static unsigned int debug_quirks2
;
50 static void sdhci_finish_data(struct sdhci_host
*);
52 static void sdhci_send_command(struct sdhci_host
*, struct mmc_command
*);
53 static void sdhci_finish_command(struct sdhci_host
*);
54 static int sdhci_execute_tuning(struct mmc_host
*mmc
, u32 opcode
);
55 static void sdhci_tuning_timer(unsigned long data
);
56 static void sdhci_enable_preset_value(struct sdhci_host
*host
, bool enable
);
58 #ifdef CONFIG_PM_RUNTIME
59 static int sdhci_runtime_pm_get(struct sdhci_host
*host
);
60 static int sdhci_runtime_pm_put(struct sdhci_host
*host
);
62 static inline int sdhci_runtime_pm_get(struct sdhci_host
*host
)
66 static inline int sdhci_runtime_pm_put(struct sdhci_host
*host
)
72 static void sdhci_dumpregs(struct sdhci_host
*host
)
74 pr_debug(DRIVER_NAME
": =========== REGISTER DUMP (%s)===========\n",
75 mmc_hostname(host
->mmc
));
77 pr_debug(DRIVER_NAME
": Sys addr: 0x%08x | Version: 0x%08x\n",
78 sdhci_readl(host
, SDHCI_DMA_ADDRESS
),
79 sdhci_readw(host
, SDHCI_HOST_VERSION
));
80 pr_debug(DRIVER_NAME
": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
81 sdhci_readw(host
, SDHCI_BLOCK_SIZE
),
82 sdhci_readw(host
, SDHCI_BLOCK_COUNT
));
83 pr_debug(DRIVER_NAME
": Argument: 0x%08x | Trn mode: 0x%08x\n",
84 sdhci_readl(host
, SDHCI_ARGUMENT
),
85 sdhci_readw(host
, SDHCI_TRANSFER_MODE
));
86 pr_debug(DRIVER_NAME
": Present: 0x%08x | Host ctl: 0x%08x\n",
87 sdhci_readl(host
, SDHCI_PRESENT_STATE
),
88 sdhci_readb(host
, SDHCI_HOST_CONTROL
));
89 pr_debug(DRIVER_NAME
": Power: 0x%08x | Blk gap: 0x%08x\n",
90 sdhci_readb(host
, SDHCI_POWER_CONTROL
),
91 sdhci_readb(host
, SDHCI_BLOCK_GAP_CONTROL
));
92 pr_debug(DRIVER_NAME
": Wake-up: 0x%08x | Clock: 0x%08x\n",
93 sdhci_readb(host
, SDHCI_WAKE_UP_CONTROL
),
94 sdhci_readw(host
, SDHCI_CLOCK_CONTROL
));
95 pr_debug(DRIVER_NAME
": Timeout: 0x%08x | Int stat: 0x%08x\n",
96 sdhci_readb(host
, SDHCI_TIMEOUT_CONTROL
),
97 sdhci_readl(host
, SDHCI_INT_STATUS
));
98 pr_debug(DRIVER_NAME
": Int enab: 0x%08x | Sig enab: 0x%08x\n",
99 sdhci_readl(host
, SDHCI_INT_ENABLE
),
100 sdhci_readl(host
, SDHCI_SIGNAL_ENABLE
));
101 pr_debug(DRIVER_NAME
": AC12 err: 0x%08x | Slot int: 0x%08x\n",
102 sdhci_readw(host
, SDHCI_ACMD12_ERR
),
103 sdhci_readw(host
, SDHCI_SLOT_INT_STATUS
));
104 pr_debug(DRIVER_NAME
": Caps: 0x%08x | Caps_1: 0x%08x\n",
105 sdhci_readl(host
, SDHCI_CAPABILITIES
),
106 sdhci_readl(host
, SDHCI_CAPABILITIES_1
));
107 pr_debug(DRIVER_NAME
": Cmd: 0x%08x | Max curr: 0x%08x\n",
108 sdhci_readw(host
, SDHCI_COMMAND
),
109 sdhci_readl(host
, SDHCI_MAX_CURRENT
));
110 pr_debug(DRIVER_NAME
": Host ctl2: 0x%08x\n",
111 sdhci_readw(host
, SDHCI_HOST_CONTROL2
));
113 if (host
->flags
& SDHCI_USE_ADMA
)
114 pr_debug(DRIVER_NAME
": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
115 readl(host
->ioaddr
+ SDHCI_ADMA_ERROR
),
116 readl(host
->ioaddr
+ SDHCI_ADMA_ADDRESS
));
118 pr_debug(DRIVER_NAME
": ===========================================\n");
121 /*****************************************************************************\
123 * Low level functions *
125 \*****************************************************************************/
127 static void sdhci_clear_set_irqs(struct sdhci_host
*host
, u32 clear
, u32 set
)
131 ier
= sdhci_readl(host
, SDHCI_INT_ENABLE
);
134 sdhci_writel(host
, ier
, SDHCI_INT_ENABLE
);
135 sdhci_writel(host
, ier
, SDHCI_SIGNAL_ENABLE
);
138 static void sdhci_unmask_irqs(struct sdhci_host
*host
, u32 irqs
)
140 sdhci_clear_set_irqs(host
, 0, irqs
);
143 static void sdhci_mask_irqs(struct sdhci_host
*host
, u32 irqs
)
145 sdhci_clear_set_irqs(host
, irqs
, 0);
148 static void sdhci_set_card_detection(struct sdhci_host
*host
, bool enable
)
152 if ((host
->quirks
& SDHCI_QUIRK_BROKEN_CARD_DETECTION
) ||
153 (host
->mmc
->caps
& MMC_CAP_NONREMOVABLE
))
156 present
= sdhci_readl(host
, SDHCI_PRESENT_STATE
) &
158 irqs
= present
? SDHCI_INT_CARD_REMOVE
: SDHCI_INT_CARD_INSERT
;
161 sdhci_unmask_irqs(host
, irqs
);
163 sdhci_mask_irqs(host
, irqs
);
166 static void sdhci_enable_card_detection(struct sdhci_host
*host
)
168 sdhci_set_card_detection(host
, true);
171 static void sdhci_disable_card_detection(struct sdhci_host
*host
)
173 sdhci_set_card_detection(host
, false);
176 static void sdhci_reset(struct sdhci_host
*host
, u8 mask
)
178 unsigned long timeout
;
179 u32
uninitialized_var(ier
);
181 if (host
->quirks
& SDHCI_QUIRK_NO_CARD_NO_RESET
) {
182 if (!(sdhci_readl(host
, SDHCI_PRESENT_STATE
) &
187 if (host
->quirks
& SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET
)
188 ier
= sdhci_readl(host
, SDHCI_INT_ENABLE
);
190 if (host
->ops
->platform_reset_enter
)
191 host
->ops
->platform_reset_enter(host
, mask
);
193 sdhci_writeb(host
, mask
, SDHCI_SOFTWARE_RESET
);
195 if (mask
& SDHCI_RESET_ALL
)
198 /* Wait max 100 ms */
201 /* hw clears the bit when it's done */
202 while (sdhci_readb(host
, SDHCI_SOFTWARE_RESET
) & mask
) {
204 pr_err("%s: Reset 0x%x never completed.\n",
205 mmc_hostname(host
->mmc
), (int)mask
);
206 sdhci_dumpregs(host
);
213 if (host
->ops
->platform_reset_exit
)
214 host
->ops
->platform_reset_exit(host
, mask
);
216 if (host
->quirks
& SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET
)
217 sdhci_clear_set_irqs(host
, SDHCI_INT_ALL_MASK
, ier
);
219 if (host
->flags
& (SDHCI_USE_SDMA
| SDHCI_USE_ADMA
)) {
220 if ((host
->ops
->enable_dma
) && (mask
& SDHCI_RESET_ALL
))
221 host
->ops
->enable_dma(host
);
225 static void sdhci_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
);
227 static void sdhci_init(struct sdhci_host
*host
, int soft
)
230 sdhci_reset(host
, SDHCI_RESET_CMD
|SDHCI_RESET_DATA
);
232 sdhci_reset(host
, SDHCI_RESET_ALL
);
234 sdhci_clear_set_irqs(host
, SDHCI_INT_ALL_MASK
,
235 SDHCI_INT_BUS_POWER
| SDHCI_INT_DATA_END_BIT
|
236 SDHCI_INT_DATA_CRC
| SDHCI_INT_DATA_TIMEOUT
| SDHCI_INT_INDEX
|
237 SDHCI_INT_END_BIT
| SDHCI_INT_CRC
| SDHCI_INT_TIMEOUT
|
238 SDHCI_INT_DATA_END
| SDHCI_INT_RESPONSE
);
241 /* force clock reconfiguration */
243 sdhci_set_ios(host
->mmc
, &host
->mmc
->ios
);
247 static void sdhci_reinit(struct sdhci_host
*host
)
251 * Retuning stuffs are affected by different cards inserted and only
252 * applicable to UHS-I cards. So reset these fields to their initial
253 * value when card is removed.
255 if (host
->flags
& SDHCI_USING_RETUNING_TIMER
) {
256 host
->flags
&= ~SDHCI_USING_RETUNING_TIMER
;
258 del_timer_sync(&host
->tuning_timer
);
259 host
->flags
&= ~SDHCI_NEEDS_RETUNING
;
260 host
->mmc
->max_blk_count
=
261 (host
->quirks
& SDHCI_QUIRK_NO_MULTIBLOCK
) ? 1 : 65535;
263 sdhci_enable_card_detection(host
);
266 static void sdhci_activate_led(struct sdhci_host
*host
)
270 ctrl
= sdhci_readb(host
, SDHCI_HOST_CONTROL
);
271 ctrl
|= SDHCI_CTRL_LED
;
272 sdhci_writeb(host
, ctrl
, SDHCI_HOST_CONTROL
);
275 static void sdhci_deactivate_led(struct sdhci_host
*host
)
279 ctrl
= sdhci_readb(host
, SDHCI_HOST_CONTROL
);
280 ctrl
&= ~SDHCI_CTRL_LED
;
281 sdhci_writeb(host
, ctrl
, SDHCI_HOST_CONTROL
);
284 #ifdef SDHCI_USE_LEDS_CLASS
285 static void sdhci_led_control(struct led_classdev
*led
,
286 enum led_brightness brightness
)
288 struct sdhci_host
*host
= container_of(led
, struct sdhci_host
, led
);
291 spin_lock_irqsave(&host
->lock
, flags
);
293 if (host
->runtime_suspended
)
296 if (brightness
== LED_OFF
)
297 sdhci_deactivate_led(host
);
299 sdhci_activate_led(host
);
301 spin_unlock_irqrestore(&host
->lock
, flags
);
305 /*****************************************************************************\
309 \*****************************************************************************/
311 static void sdhci_read_block_pio(struct sdhci_host
*host
)
314 size_t blksize
, len
, chunk
;
315 u32
uninitialized_var(scratch
);
318 DBG("PIO reading\n");
320 blksize
= host
->data
->blksz
;
323 local_irq_save(flags
);
326 if (!sg_miter_next(&host
->sg_miter
))
329 len
= min(host
->sg_miter
.length
, blksize
);
332 host
->sg_miter
.consumed
= len
;
334 buf
= host
->sg_miter
.addr
;
338 scratch
= sdhci_readl(host
, SDHCI_BUFFER
);
342 *buf
= scratch
& 0xFF;
351 sg_miter_stop(&host
->sg_miter
);
353 local_irq_restore(flags
);
356 static void sdhci_write_block_pio(struct sdhci_host
*host
)
359 size_t blksize
, len
, chunk
;
363 DBG("PIO writing\n");
365 blksize
= host
->data
->blksz
;
369 local_irq_save(flags
);
372 if (!sg_miter_next(&host
->sg_miter
))
375 len
= min(host
->sg_miter
.length
, blksize
);
378 host
->sg_miter
.consumed
= len
;
380 buf
= host
->sg_miter
.addr
;
383 scratch
|= (u32
)*buf
<< (chunk
* 8);
389 if ((chunk
== 4) || ((len
== 0) && (blksize
== 0))) {
390 sdhci_writel(host
, scratch
, SDHCI_BUFFER
);
397 sg_miter_stop(&host
->sg_miter
);
399 local_irq_restore(flags
);
402 static void sdhci_transfer_pio(struct sdhci_host
*host
)
408 if (host
->blocks
== 0)
411 if (host
->data
->flags
& MMC_DATA_READ
)
412 mask
= SDHCI_DATA_AVAILABLE
;
414 mask
= SDHCI_SPACE_AVAILABLE
;
417 * Some controllers (JMicron JMB38x) mess up the buffer bits
418 * for transfers < 4 bytes. As long as it is just one block,
419 * we can ignore the bits.
421 if ((host
->quirks
& SDHCI_QUIRK_BROKEN_SMALL_PIO
) &&
422 (host
->data
->blocks
== 1))
425 while (sdhci_readl(host
, SDHCI_PRESENT_STATE
) & mask
) {
426 if (host
->quirks
& SDHCI_QUIRK_PIO_NEEDS_DELAY
)
429 if (host
->data
->flags
& MMC_DATA_READ
)
430 sdhci_read_block_pio(host
);
432 sdhci_write_block_pio(host
);
435 if (host
->blocks
== 0)
439 DBG("PIO transfer complete.\n");
442 static char *sdhci_kmap_atomic(struct scatterlist
*sg
, unsigned long *flags
)
444 local_irq_save(*flags
);
445 return kmap_atomic(sg_page(sg
)) + sg
->offset
;
448 static void sdhci_kunmap_atomic(void *buffer
, unsigned long *flags
)
450 kunmap_atomic(buffer
);
451 local_irq_restore(*flags
);
454 static void sdhci_set_adma_desc(u8
*desc
, u32 addr
, int len
, unsigned cmd
)
456 __le32
*dataddr
= (__le32 __force
*)(desc
+ 4);
457 __le16
*cmdlen
= (__le16 __force
*)desc
;
459 /* SDHCI specification says ADMA descriptors should be 4 byte
460 * aligned, so using 16 or 32bit operations should be safe. */
462 cmdlen
[0] = cpu_to_le16(cmd
);
463 cmdlen
[1] = cpu_to_le16(len
);
465 dataddr
[0] = cpu_to_le32(addr
);
468 static int sdhci_adma_table_pre(struct sdhci_host
*host
,
469 struct mmc_data
*data
)
476 dma_addr_t align_addr
;
479 struct scatterlist
*sg
;
485 * The spec does not specify endianness of descriptor table.
486 * We currently guess that it is LE.
489 if (data
->flags
& MMC_DATA_READ
)
490 direction
= DMA_FROM_DEVICE
;
492 direction
= DMA_TO_DEVICE
;
495 * The ADMA descriptor table is mapped further down as we
496 * need to fill it with data first.
499 host
->align_addr
= dma_map_single(mmc_dev(host
->mmc
),
500 host
->align_buffer
, 128 * 4, direction
);
501 if (dma_mapping_error(mmc_dev(host
->mmc
), host
->align_addr
))
503 BUG_ON(host
->align_addr
& 0x3);
505 host
->sg_count
= dma_map_sg(mmc_dev(host
->mmc
),
506 data
->sg
, data
->sg_len
, direction
);
507 if (host
->sg_count
== 0)
510 desc
= host
->adma_desc
;
511 align
= host
->align_buffer
;
513 align_addr
= host
->align_addr
;
515 for_each_sg(data
->sg
, sg
, host
->sg_count
, i
) {
516 addr
= sg_dma_address(sg
);
517 len
= sg_dma_len(sg
);
520 * The SDHCI specification states that ADMA
521 * addresses must be 32-bit aligned. If they
522 * aren't, then we use a bounce buffer for
523 * the (up to three) bytes that screw up the
526 offset
= (4 - (addr
& 0x3)) & 0x3;
528 if (data
->flags
& MMC_DATA_WRITE
) {
529 buffer
= sdhci_kmap_atomic(sg
, &flags
);
530 WARN_ON(((long)buffer
& PAGE_MASK
) > (PAGE_SIZE
- 3));
531 memcpy(align
, buffer
, offset
);
532 sdhci_kunmap_atomic(buffer
, &flags
);
536 sdhci_set_adma_desc(desc
, align_addr
, offset
, 0x21);
538 BUG_ON(offset
> 65536);
552 sdhci_set_adma_desc(desc
, addr
, len
, 0x21);
556 * If this triggers then we have a calculation bug
559 WARN_ON((desc
- host
->adma_desc
) > (128 * 2 + 1) * 4);
562 if (host
->quirks
& SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
) {
564 * Mark the last descriptor as the terminating descriptor
566 if (desc
!= host
->adma_desc
) {
568 desc
[0] |= 0x2; /* end */
572 * Add a terminating entry.
575 /* nop, end, valid */
576 sdhci_set_adma_desc(desc
, 0, 0, 0x3);
580 * Resync align buffer as we might have changed it.
582 if (data
->flags
& MMC_DATA_WRITE
) {
583 dma_sync_single_for_device(mmc_dev(host
->mmc
),
584 host
->align_addr
, 128 * 4, direction
);
587 host
->adma_addr
= dma_map_single(mmc_dev(host
->mmc
),
588 host
->adma_desc
, (128 * 2 + 1) * 4, DMA_TO_DEVICE
);
589 if (dma_mapping_error(mmc_dev(host
->mmc
), host
->adma_addr
))
591 BUG_ON(host
->adma_addr
& 0x3);
596 dma_unmap_sg(mmc_dev(host
->mmc
), data
->sg
,
597 data
->sg_len
, direction
);
599 dma_unmap_single(mmc_dev(host
->mmc
), host
->align_addr
,
605 static void sdhci_adma_table_post(struct sdhci_host
*host
,
606 struct mmc_data
*data
)
610 struct scatterlist
*sg
;
616 if (data
->flags
& MMC_DATA_READ
)
617 direction
= DMA_FROM_DEVICE
;
619 direction
= DMA_TO_DEVICE
;
621 dma_unmap_single(mmc_dev(host
->mmc
), host
->adma_addr
,
622 (128 * 2 + 1) * 4, DMA_TO_DEVICE
);
624 dma_unmap_single(mmc_dev(host
->mmc
), host
->align_addr
,
627 if (data
->flags
& MMC_DATA_READ
) {
628 dma_sync_sg_for_cpu(mmc_dev(host
->mmc
), data
->sg
,
629 data
->sg_len
, direction
);
631 align
= host
->align_buffer
;
633 for_each_sg(data
->sg
, sg
, host
->sg_count
, i
) {
634 if (sg_dma_address(sg
) & 0x3) {
635 size
= 4 - (sg_dma_address(sg
) & 0x3);
637 buffer
= sdhci_kmap_atomic(sg
, &flags
);
638 WARN_ON(((long)buffer
& PAGE_MASK
) > (PAGE_SIZE
- 3));
639 memcpy(buffer
, align
, size
);
640 sdhci_kunmap_atomic(buffer
, &flags
);
647 dma_unmap_sg(mmc_dev(host
->mmc
), data
->sg
,
648 data
->sg_len
, direction
);
651 static u8
sdhci_calc_timeout(struct sdhci_host
*host
, struct mmc_command
*cmd
)
654 struct mmc_data
*data
= cmd
->data
;
655 unsigned target_timeout
, current_timeout
;
658 * If the host controller provides us with an incorrect timeout
659 * value, just skip the check and use 0xE. The hardware may take
660 * longer to time out, but that's much better than having a too-short
663 if (host
->quirks
& SDHCI_QUIRK_BROKEN_TIMEOUT_VAL
)
666 /* Unspecified timeout, assume max */
667 if (!data
&& !cmd
->cmd_timeout_ms
)
672 target_timeout
= cmd
->cmd_timeout_ms
* 1000;
674 target_timeout
= data
->timeout_ns
/ 1000;
676 target_timeout
+= data
->timeout_clks
/ host
->clock
;
680 * Figure out needed cycles.
681 * We do this in steps in order to fit inside a 32 bit int.
682 * The first step is the minimum timeout, which will have a
683 * minimum resolution of 6 bits:
684 * (1) 2^13*1000 > 2^22,
685 * (2) host->timeout_clk < 2^16
690 current_timeout
= (1 << 13) * 1000 / host
->timeout_clk
;
691 while (current_timeout
< target_timeout
) {
693 current_timeout
<<= 1;
699 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
700 mmc_hostname(host
->mmc
), count
, cmd
->opcode
);
707 static void sdhci_set_transfer_irqs(struct sdhci_host
*host
)
709 u32 pio_irqs
= SDHCI_INT_DATA_AVAIL
| SDHCI_INT_SPACE_AVAIL
;
710 u32 dma_irqs
= SDHCI_INT_DMA_END
| SDHCI_INT_ADMA_ERROR
;
712 if (host
->flags
& SDHCI_REQ_USE_DMA
)
713 sdhci_clear_set_irqs(host
, pio_irqs
, dma_irqs
);
715 sdhci_clear_set_irqs(host
, dma_irqs
, pio_irqs
);
718 static void sdhci_prepare_data(struct sdhci_host
*host
, struct mmc_command
*cmd
)
722 struct mmc_data
*data
= cmd
->data
;
727 if (data
|| (cmd
->flags
& MMC_RSP_BUSY
)) {
728 count
= sdhci_calc_timeout(host
, cmd
);
729 sdhci_writeb(host
, count
, SDHCI_TIMEOUT_CONTROL
);
736 BUG_ON(data
->blksz
* data
->blocks
> 524288);
737 BUG_ON(data
->blksz
> host
->mmc
->max_blk_size
);
738 BUG_ON(data
->blocks
> 65535);
741 host
->data_early
= 0;
742 host
->data
->bytes_xfered
= 0;
744 if (host
->flags
& (SDHCI_USE_SDMA
| SDHCI_USE_ADMA
))
745 host
->flags
|= SDHCI_REQ_USE_DMA
;
748 * FIXME: This doesn't account for merging when mapping the
751 if (host
->flags
& SDHCI_REQ_USE_DMA
) {
753 struct scatterlist
*sg
;
756 if (host
->flags
& SDHCI_USE_ADMA
) {
757 if (host
->quirks
& SDHCI_QUIRK_32BIT_ADMA_SIZE
)
760 if (host
->quirks
& SDHCI_QUIRK_32BIT_DMA_SIZE
)
764 if (unlikely(broken
)) {
765 for_each_sg(data
->sg
, sg
, data
->sg_len
, i
) {
766 if (sg
->length
& 0x3) {
767 DBG("Reverting to PIO because of "
768 "transfer size (%d)\n",
770 host
->flags
&= ~SDHCI_REQ_USE_DMA
;
778 * The assumption here being that alignment is the same after
779 * translation to device address space.
781 if (host
->flags
& SDHCI_REQ_USE_DMA
) {
783 struct scatterlist
*sg
;
786 if (host
->flags
& SDHCI_USE_ADMA
) {
788 * As we use 3 byte chunks to work around
789 * alignment problems, we need to check this
792 if (host
->quirks
& SDHCI_QUIRK_32BIT_ADMA_SIZE
)
795 if (host
->quirks
& SDHCI_QUIRK_32BIT_DMA_ADDR
)
799 if (unlikely(broken
)) {
800 for_each_sg(data
->sg
, sg
, data
->sg_len
, i
) {
801 if (sg
->offset
& 0x3) {
802 DBG("Reverting to PIO because of "
804 host
->flags
&= ~SDHCI_REQ_USE_DMA
;
811 if (host
->flags
& SDHCI_REQ_USE_DMA
) {
812 if (host
->flags
& SDHCI_USE_ADMA
) {
813 ret
= sdhci_adma_table_pre(host
, data
);
816 * This only happens when someone fed
817 * us an invalid request.
820 host
->flags
&= ~SDHCI_REQ_USE_DMA
;
822 sdhci_writel(host
, host
->adma_addr
,
828 sg_cnt
= dma_map_sg(mmc_dev(host
->mmc
),
829 data
->sg
, data
->sg_len
,
830 (data
->flags
& MMC_DATA_READ
) ?
835 * This only happens when someone fed
836 * us an invalid request.
839 host
->flags
&= ~SDHCI_REQ_USE_DMA
;
841 WARN_ON(sg_cnt
!= 1);
842 sdhci_writel(host
, sg_dma_address(data
->sg
),
849 * Always adjust the DMA selection as some controllers
850 * (e.g. JMicron) can't do PIO properly when the selection
853 if (host
->version
>= SDHCI_SPEC_200
) {
854 ctrl
= sdhci_readb(host
, SDHCI_HOST_CONTROL
);
855 ctrl
&= ~SDHCI_CTRL_DMA_MASK
;
856 if ((host
->flags
& SDHCI_REQ_USE_DMA
) &&
857 (host
->flags
& SDHCI_USE_ADMA
))
858 ctrl
|= SDHCI_CTRL_ADMA32
;
860 ctrl
|= SDHCI_CTRL_SDMA
;
861 sdhci_writeb(host
, ctrl
, SDHCI_HOST_CONTROL
);
864 if (!(host
->flags
& SDHCI_REQ_USE_DMA
)) {
867 flags
= SG_MITER_ATOMIC
;
868 if (host
->data
->flags
& MMC_DATA_READ
)
869 flags
|= SG_MITER_TO_SG
;
871 flags
|= SG_MITER_FROM_SG
;
872 sg_miter_start(&host
->sg_miter
, data
->sg
, data
->sg_len
, flags
);
873 host
->blocks
= data
->blocks
;
876 sdhci_set_transfer_irqs(host
);
878 /* Set the DMA boundary value and block size */
879 sdhci_writew(host
, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG
,
880 data
->blksz
), SDHCI_BLOCK_SIZE
);
881 sdhci_writew(host
, data
->blocks
, SDHCI_BLOCK_COUNT
);
884 static void sdhci_set_transfer_mode(struct sdhci_host
*host
,
885 struct mmc_command
*cmd
)
888 struct mmc_data
*data
= cmd
->data
;
893 WARN_ON(!host
->data
);
895 mode
= SDHCI_TRNS_BLK_CNT_EN
;
896 if (mmc_op_multi(cmd
->opcode
) || data
->blocks
> 1) {
897 mode
|= SDHCI_TRNS_MULTI
;
899 * If we are sending CMD23, CMD12 never gets sent
900 * on successful completion (so no Auto-CMD12).
902 if (!host
->mrq
->sbc
&& (host
->flags
& SDHCI_AUTO_CMD12
))
903 mode
|= SDHCI_TRNS_AUTO_CMD12
;
904 else if (host
->mrq
->sbc
&& (host
->flags
& SDHCI_AUTO_CMD23
)) {
905 mode
|= SDHCI_TRNS_AUTO_CMD23
;
906 sdhci_writel(host
, host
->mrq
->sbc
->arg
, SDHCI_ARGUMENT2
);
910 if (data
->flags
& MMC_DATA_READ
)
911 mode
|= SDHCI_TRNS_READ
;
912 if (host
->flags
& SDHCI_REQ_USE_DMA
)
913 mode
|= SDHCI_TRNS_DMA
;
915 sdhci_writew(host
, mode
, SDHCI_TRANSFER_MODE
);
918 static void sdhci_finish_data(struct sdhci_host
*host
)
920 struct mmc_data
*data
;
927 if (host
->flags
& SDHCI_REQ_USE_DMA
) {
928 if (host
->flags
& SDHCI_USE_ADMA
)
929 sdhci_adma_table_post(host
, data
);
931 dma_unmap_sg(mmc_dev(host
->mmc
), data
->sg
,
932 data
->sg_len
, (data
->flags
& MMC_DATA_READ
) ?
933 DMA_FROM_DEVICE
: DMA_TO_DEVICE
);
938 * The specification states that the block count register must
939 * be updated, but it does not specify at what point in the
940 * data flow. That makes the register entirely useless to read
941 * back so we have to assume that nothing made it to the card
942 * in the event of an error.
945 data
->bytes_xfered
= 0;
947 data
->bytes_xfered
= data
->blksz
* data
->blocks
;
950 * Need to send CMD12 if -
951 * a) open-ended multiblock transfer (no CMD23)
952 * b) error in multiblock transfer
959 * The controller needs a reset of internal state machines
960 * upon error conditions.
963 sdhci_reset(host
, SDHCI_RESET_CMD
);
964 sdhci_reset(host
, SDHCI_RESET_DATA
);
967 sdhci_send_command(host
, data
->stop
);
969 tasklet_schedule(&host
->finish_tasklet
);
972 static void sdhci_send_command(struct sdhci_host
*host
, struct mmc_command
*cmd
)
976 unsigned long timeout
;
983 mask
= SDHCI_CMD_INHIBIT
;
984 if ((cmd
->data
!= NULL
) || (cmd
->flags
& MMC_RSP_BUSY
))
985 mask
|= SDHCI_DATA_INHIBIT
;
987 /* We shouldn't wait for data inihibit for stop commands, even
988 though they might use busy signaling */
989 if (host
->mrq
->data
&& (cmd
== host
->mrq
->data
->stop
))
990 mask
&= ~SDHCI_DATA_INHIBIT
;
992 while (sdhci_readl(host
, SDHCI_PRESENT_STATE
) & mask
) {
994 pr_err("%s: Controller never released "
995 "inhibit bit(s).\n", mmc_hostname(host
->mmc
));
996 sdhci_dumpregs(host
);
998 tasklet_schedule(&host
->finish_tasklet
);
1005 mod_timer(&host
->timer
, jiffies
+ 10 * HZ
);
1009 sdhci_prepare_data(host
, cmd
);
1011 sdhci_writel(host
, cmd
->arg
, SDHCI_ARGUMENT
);
1013 sdhci_set_transfer_mode(host
, cmd
);
1015 if ((cmd
->flags
& MMC_RSP_136
) && (cmd
->flags
& MMC_RSP_BUSY
)) {
1016 pr_err("%s: Unsupported response type!\n",
1017 mmc_hostname(host
->mmc
));
1018 cmd
->error
= -EINVAL
;
1019 tasklet_schedule(&host
->finish_tasklet
);
1023 if (!(cmd
->flags
& MMC_RSP_PRESENT
))
1024 flags
= SDHCI_CMD_RESP_NONE
;
1025 else if (cmd
->flags
& MMC_RSP_136
)
1026 flags
= SDHCI_CMD_RESP_LONG
;
1027 else if (cmd
->flags
& MMC_RSP_BUSY
)
1028 flags
= SDHCI_CMD_RESP_SHORT_BUSY
;
1030 flags
= SDHCI_CMD_RESP_SHORT
;
1032 if (cmd
->flags
& MMC_RSP_CRC
)
1033 flags
|= SDHCI_CMD_CRC
;
1034 if (cmd
->flags
& MMC_RSP_OPCODE
)
1035 flags
|= SDHCI_CMD_INDEX
;
1037 /* CMD19 is special in that the Data Present Select should be set */
1038 if (cmd
->data
|| cmd
->opcode
== MMC_SEND_TUNING_BLOCK
||
1039 cmd
->opcode
== MMC_SEND_TUNING_BLOCK_HS200
)
1040 flags
|= SDHCI_CMD_DATA
;
1042 sdhci_writew(host
, SDHCI_MAKE_CMD(cmd
->opcode
, flags
), SDHCI_COMMAND
);
1045 static void sdhci_finish_command(struct sdhci_host
*host
)
1049 BUG_ON(host
->cmd
== NULL
);
1051 if (host
->cmd
->flags
& MMC_RSP_PRESENT
) {
1052 if (host
->cmd
->flags
& MMC_RSP_136
) {
1053 /* CRC is stripped so we need to do some shifting. */
1054 for (i
= 0;i
< 4;i
++) {
1055 host
->cmd
->resp
[i
] = sdhci_readl(host
,
1056 SDHCI_RESPONSE
+ (3-i
)*4) << 8;
1058 host
->cmd
->resp
[i
] |=
1060 SDHCI_RESPONSE
+ (3-i
)*4-1);
1063 host
->cmd
->resp
[0] = sdhci_readl(host
, SDHCI_RESPONSE
);
1067 host
->cmd
->error
= 0;
1069 /* Finished CMD23, now send actual command. */
1070 if (host
->cmd
== host
->mrq
->sbc
) {
1072 sdhci_send_command(host
, host
->mrq
->cmd
);
1075 /* Processed actual command. */
1076 if (host
->data
&& host
->data_early
)
1077 sdhci_finish_data(host
);
1079 if (!host
->cmd
->data
)
1080 tasklet_schedule(&host
->finish_tasklet
);
1086 static u16
sdhci_get_preset_value(struct sdhci_host
*host
)
1088 u16 ctrl
, preset
= 0;
1090 ctrl
= sdhci_readw(host
, SDHCI_HOST_CONTROL2
);
1092 switch (ctrl
& SDHCI_CTRL_UHS_MASK
) {
1093 case SDHCI_CTRL_UHS_SDR12
:
1094 preset
= sdhci_readw(host
, SDHCI_PRESET_FOR_SDR12
);
1096 case SDHCI_CTRL_UHS_SDR25
:
1097 preset
= sdhci_readw(host
, SDHCI_PRESET_FOR_SDR25
);
1099 case SDHCI_CTRL_UHS_SDR50
:
1100 preset
= sdhci_readw(host
, SDHCI_PRESET_FOR_SDR50
);
1102 case SDHCI_CTRL_UHS_SDR104
:
1103 preset
= sdhci_readw(host
, SDHCI_PRESET_FOR_SDR104
);
1105 case SDHCI_CTRL_UHS_DDR50
:
1106 preset
= sdhci_readw(host
, SDHCI_PRESET_FOR_DDR50
);
1109 pr_warn("%s: Invalid UHS-I mode selected\n",
1110 mmc_hostname(host
->mmc
));
1111 preset
= sdhci_readw(host
, SDHCI_PRESET_FOR_SDR12
);
1117 static void sdhci_set_clock(struct sdhci_host
*host
, unsigned int clock
)
1119 int div
= 0; /* Initialized for compiler warning */
1120 int real_div
= div
, clk_mul
= 1;
1122 unsigned long timeout
;
1124 if (clock
&& clock
== host
->clock
)
1127 host
->mmc
->actual_clock
= 0;
1129 if (host
->ops
->set_clock
) {
1130 host
->ops
->set_clock(host
, clock
);
1131 if (host
->quirks
& SDHCI_QUIRK_NONSTANDARD_CLOCK
)
1135 sdhci_writew(host
, 0, SDHCI_CLOCK_CONTROL
);
1140 if (host
->version
>= SDHCI_SPEC_300
) {
1141 if (sdhci_readw(host
, SDHCI_HOST_CONTROL2
) &
1142 SDHCI_CTRL_PRESET_VAL_ENABLE
) {
1145 clk
= sdhci_readw(host
, SDHCI_CLOCK_CONTROL
);
1146 pre_val
= sdhci_get_preset_value(host
);
1147 div
= (pre_val
& SDHCI_PRESET_SDCLK_FREQ_MASK
)
1148 >> SDHCI_PRESET_SDCLK_FREQ_SHIFT
;
1149 if (host
->clk_mul
&&
1150 (pre_val
& SDHCI_PRESET_CLKGEN_SEL_MASK
)) {
1151 clk
= SDHCI_PROG_CLOCK_MODE
;
1153 clk_mul
= host
->clk_mul
;
1155 real_div
= max_t(int, 1, div
<< 1);
1161 * Check if the Host Controller supports Programmable Clock
1164 if (host
->clk_mul
) {
1165 for (div
= 1; div
<= 1024; div
++) {
1166 if ((host
->max_clk
* host
->clk_mul
/ div
)
1171 * Set Programmable Clock Mode in the Clock
1174 clk
= SDHCI_PROG_CLOCK_MODE
;
1176 clk_mul
= host
->clk_mul
;
1179 /* Version 3.00 divisors must be a multiple of 2. */
1180 if (host
->max_clk
<= clock
)
1183 for (div
= 2; div
< SDHCI_MAX_DIV_SPEC_300
;
1185 if ((host
->max_clk
/ div
) <= clock
)
1193 /* Version 2.00 divisors must be a power of 2. */
1194 for (div
= 1; div
< SDHCI_MAX_DIV_SPEC_200
; div
*= 2) {
1195 if ((host
->max_clk
/ div
) <= clock
)
1204 host
->mmc
->actual_clock
= (host
->max_clk
* clk_mul
) / real_div
;
1206 clk
|= (div
& SDHCI_DIV_MASK
) << SDHCI_DIVIDER_SHIFT
;
1207 clk
|= ((div
& SDHCI_DIV_HI_MASK
) >> SDHCI_DIV_MASK_LEN
)
1208 << SDHCI_DIVIDER_HI_SHIFT
;
1209 clk
|= SDHCI_CLOCK_INT_EN
;
1210 sdhci_writew(host
, clk
, SDHCI_CLOCK_CONTROL
);
1212 /* Wait max 20 ms */
1214 while (!((clk
= sdhci_readw(host
, SDHCI_CLOCK_CONTROL
))
1215 & SDHCI_CLOCK_INT_STABLE
)) {
1217 pr_err("%s: Internal clock never "
1218 "stabilised.\n", mmc_hostname(host
->mmc
));
1219 sdhci_dumpregs(host
);
1226 clk
|= SDHCI_CLOCK_CARD_EN
;
1227 sdhci_writew(host
, clk
, SDHCI_CLOCK_CONTROL
);
1230 host
->clock
= clock
;
1233 static inline void sdhci_update_clock(struct sdhci_host
*host
)
1237 clock
= host
->clock
;
1239 sdhci_set_clock(host
, clock
);
1242 static int sdhci_set_power(struct sdhci_host
*host
, unsigned short power
)
1246 if (power
!= (unsigned short)-1) {
1247 switch (1 << power
) {
1248 case MMC_VDD_165_195
:
1249 pwr
= SDHCI_POWER_180
;
1253 pwr
= SDHCI_POWER_300
;
1257 pwr
= SDHCI_POWER_330
;
1264 if (host
->pwr
== pwr
)
1270 sdhci_writeb(host
, 0, SDHCI_POWER_CONTROL
);
1275 * Spec says that we should clear the power reg before setting
1276 * a new value. Some controllers don't seem to like this though.
1278 if (!(host
->quirks
& SDHCI_QUIRK_SINGLE_POWER_WRITE
))
1279 sdhci_writeb(host
, 0, SDHCI_POWER_CONTROL
);
1282 * At least the Marvell CaFe chip gets confused if we set the voltage
1283 * and set turn on power at the same time, so set the voltage first.
1285 if (host
->quirks
& SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER
)
1286 sdhci_writeb(host
, pwr
, SDHCI_POWER_CONTROL
);
1288 pwr
|= SDHCI_POWER_ON
;
1290 sdhci_writeb(host
, pwr
, SDHCI_POWER_CONTROL
);
1293 * Some controllers need an extra 10ms delay of 10ms before they
1294 * can apply clock after applying power
1296 if (host
->quirks
& SDHCI_QUIRK_DELAY_AFTER_POWER
)
1302 /*****************************************************************************\
1306 \*****************************************************************************/
1308 static void sdhci_request(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
1310 struct sdhci_host
*host
;
1312 unsigned long flags
;
1315 host
= mmc_priv(mmc
);
1317 sdhci_runtime_pm_get(host
);
1319 present
= mmc_gpio_get_cd(host
->mmc
);
1321 spin_lock_irqsave(&host
->lock
, flags
);
1323 WARN_ON(host
->mrq
!= NULL
);
1325 #ifndef SDHCI_USE_LEDS_CLASS
1326 sdhci_activate_led(host
);
1330 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1331 * requests if Auto-CMD12 is enabled.
1333 if (!mrq
->sbc
&& (host
->flags
& SDHCI_AUTO_CMD12
)) {
1335 mrq
->data
->stop
= NULL
;
1343 * Firstly check card presence from cd-gpio. The return could
1344 * be one of the following possibilities:
1345 * negative: cd-gpio is not available
1346 * zero: cd-gpio is used, and card is removed
1347 * one: cd-gpio is used, and card is present
1350 /* If polling, assume that the card is always present. */
1351 if (host
->quirks
& SDHCI_QUIRK_BROKEN_CARD_DETECTION
)
1354 present
= sdhci_readl(host
, SDHCI_PRESENT_STATE
) &
1358 if (!present
|| host
->flags
& SDHCI_DEVICE_DEAD
) {
1359 host
->mrq
->cmd
->error
= -ENOMEDIUM
;
1360 tasklet_schedule(&host
->finish_tasklet
);
1364 present_state
= sdhci_readl(host
, SDHCI_PRESENT_STATE
);
1366 * Check if the re-tuning timer has already expired and there
1367 * is no on-going data transfer. If so, we need to execute
1368 * tuning procedure before sending command.
1370 if ((host
->flags
& SDHCI_NEEDS_RETUNING
) &&
1371 !(present_state
& (SDHCI_DOING_WRITE
| SDHCI_DOING_READ
))) {
1373 /* eMMC uses cmd21 but sd and sdio use cmd19 */
1375 mmc
->card
->type
== MMC_TYPE_MMC
?
1376 MMC_SEND_TUNING_BLOCK_HS200
:
1377 MMC_SEND_TUNING_BLOCK
;
1378 spin_unlock_irqrestore(&host
->lock
, flags
);
1379 sdhci_execute_tuning(mmc
, tuning_opcode
);
1380 spin_lock_irqsave(&host
->lock
, flags
);
1382 /* Restore original mmc_request structure */
1387 if (mrq
->sbc
&& !(host
->flags
& SDHCI_AUTO_CMD23
))
1388 sdhci_send_command(host
, mrq
->sbc
);
1390 sdhci_send_command(host
, mrq
->cmd
);
1394 spin_unlock_irqrestore(&host
->lock
, flags
);
1397 static void sdhci_do_set_ios(struct sdhci_host
*host
, struct mmc_ios
*ios
)
1399 unsigned long flags
;
1403 spin_lock_irqsave(&host
->lock
, flags
);
1405 if (host
->flags
& SDHCI_DEVICE_DEAD
) {
1406 spin_unlock_irqrestore(&host
->lock
, flags
);
1407 if (host
->vmmc
&& ios
->power_mode
== MMC_POWER_OFF
)
1408 mmc_regulator_set_ocr(host
->mmc
, host
->vmmc
, 0);
1413 * Reset the chip on each power off.
1414 * Should clear out any weird states.
1416 if (ios
->power_mode
== MMC_POWER_OFF
) {
1417 sdhci_writel(host
, 0, SDHCI_SIGNAL_ENABLE
);
1421 if (host
->version
>= SDHCI_SPEC_300
&&
1422 (ios
->power_mode
== MMC_POWER_UP
))
1423 sdhci_enable_preset_value(host
, false);
1425 sdhci_set_clock(host
, ios
->clock
);
1427 if (ios
->power_mode
== MMC_POWER_OFF
)
1428 vdd_bit
= sdhci_set_power(host
, -1);
1430 vdd_bit
= sdhci_set_power(host
, ios
->vdd
);
1432 if (host
->vmmc
&& vdd_bit
!= -1) {
1433 spin_unlock_irqrestore(&host
->lock
, flags
);
1434 mmc_regulator_set_ocr(host
->mmc
, host
->vmmc
, vdd_bit
);
1435 spin_lock_irqsave(&host
->lock
, flags
);
1438 if (host
->ops
->platform_send_init_74_clocks
)
1439 host
->ops
->platform_send_init_74_clocks(host
, ios
->power_mode
);
1442 * If your platform has 8-bit width support but is not a v3 controller,
1443 * or if it requires special setup code, you should implement that in
1444 * platform_bus_width().
1446 if (host
->ops
->platform_bus_width
) {
1447 host
->ops
->platform_bus_width(host
, ios
->bus_width
);
1449 ctrl
= sdhci_readb(host
, SDHCI_HOST_CONTROL
);
1450 if (ios
->bus_width
== MMC_BUS_WIDTH_8
) {
1451 ctrl
&= ~SDHCI_CTRL_4BITBUS
;
1452 if (host
->version
>= SDHCI_SPEC_300
)
1453 ctrl
|= SDHCI_CTRL_8BITBUS
;
1455 if (host
->version
>= SDHCI_SPEC_300
)
1456 ctrl
&= ~SDHCI_CTRL_8BITBUS
;
1457 if (ios
->bus_width
== MMC_BUS_WIDTH_4
)
1458 ctrl
|= SDHCI_CTRL_4BITBUS
;
1460 ctrl
&= ~SDHCI_CTRL_4BITBUS
;
1462 sdhci_writeb(host
, ctrl
, SDHCI_HOST_CONTROL
);
1465 ctrl
= sdhci_readb(host
, SDHCI_HOST_CONTROL
);
1467 if ((ios
->timing
== MMC_TIMING_SD_HS
||
1468 ios
->timing
== MMC_TIMING_MMC_HS
)
1469 && !(host
->quirks
& SDHCI_QUIRK_NO_HISPD_BIT
))
1470 ctrl
|= SDHCI_CTRL_HISPD
;
1472 ctrl
&= ~SDHCI_CTRL_HISPD
;
1474 if (host
->version
>= SDHCI_SPEC_300
) {
1477 /* In case of UHS-I modes, set High Speed Enable */
1478 if ((ios
->timing
== MMC_TIMING_MMC_HS200
) ||
1479 (ios
->timing
== MMC_TIMING_UHS_SDR50
) ||
1480 (ios
->timing
== MMC_TIMING_UHS_SDR104
) ||
1481 (ios
->timing
== MMC_TIMING_UHS_DDR50
) ||
1482 (ios
->timing
== MMC_TIMING_UHS_SDR25
))
1483 ctrl
|= SDHCI_CTRL_HISPD
;
1485 ctrl_2
= sdhci_readw(host
, SDHCI_HOST_CONTROL2
);
1486 if (!(ctrl_2
& SDHCI_CTRL_PRESET_VAL_ENABLE
)) {
1487 sdhci_writeb(host
, ctrl
, SDHCI_HOST_CONTROL
);
1489 * We only need to set Driver Strength if the
1490 * preset value enable is not set.
1492 ctrl_2
&= ~SDHCI_CTRL_DRV_TYPE_MASK
;
1493 if (ios
->drv_type
== MMC_SET_DRIVER_TYPE_A
)
1494 ctrl_2
|= SDHCI_CTRL_DRV_TYPE_A
;
1495 else if (ios
->drv_type
== MMC_SET_DRIVER_TYPE_C
)
1496 ctrl_2
|= SDHCI_CTRL_DRV_TYPE_C
;
1498 sdhci_writew(host
, ctrl_2
, SDHCI_HOST_CONTROL2
);
1501 * According to SDHC Spec v3.00, if the Preset Value
1502 * Enable in the Host Control 2 register is set, we
1503 * need to reset SD Clock Enable before changing High
1504 * Speed Enable to avoid generating clock gliches.
1507 /* Reset SD Clock Enable */
1508 clk
= sdhci_readw(host
, SDHCI_CLOCK_CONTROL
);
1509 clk
&= ~SDHCI_CLOCK_CARD_EN
;
1510 sdhci_writew(host
, clk
, SDHCI_CLOCK_CONTROL
);
1512 sdhci_writeb(host
, ctrl
, SDHCI_HOST_CONTROL
);
1514 /* Re-enable SD Clock */
1515 sdhci_update_clock(host
);
1519 /* Reset SD Clock Enable */
1520 clk
= sdhci_readw(host
, SDHCI_CLOCK_CONTROL
);
1521 clk
&= ~SDHCI_CLOCK_CARD_EN
;
1522 sdhci_writew(host
, clk
, SDHCI_CLOCK_CONTROL
);
1524 if (host
->ops
->set_uhs_signaling
)
1525 host
->ops
->set_uhs_signaling(host
, ios
->timing
);
1527 ctrl_2
= sdhci_readw(host
, SDHCI_HOST_CONTROL2
);
1528 /* Select Bus Speed Mode for host */
1529 ctrl_2
&= ~SDHCI_CTRL_UHS_MASK
;
1530 if (ios
->timing
== MMC_TIMING_MMC_HS200
)
1531 ctrl_2
|= SDHCI_CTRL_HS_SDR200
;
1532 else if (ios
->timing
== MMC_TIMING_UHS_SDR12
)
1533 ctrl_2
|= SDHCI_CTRL_UHS_SDR12
;
1534 else if (ios
->timing
== MMC_TIMING_UHS_SDR25
)
1535 ctrl_2
|= SDHCI_CTRL_UHS_SDR25
;
1536 else if (ios
->timing
== MMC_TIMING_UHS_SDR50
)
1537 ctrl_2
|= SDHCI_CTRL_UHS_SDR50
;
1538 else if (ios
->timing
== MMC_TIMING_UHS_SDR104
)
1539 ctrl_2
|= SDHCI_CTRL_UHS_SDR104
;
1540 else if (ios
->timing
== MMC_TIMING_UHS_DDR50
)
1541 ctrl_2
|= SDHCI_CTRL_UHS_DDR50
;
1542 sdhci_writew(host
, ctrl_2
, SDHCI_HOST_CONTROL2
);
1545 if (!(host
->quirks2
& SDHCI_QUIRK2_PRESET_VALUE_BROKEN
) &&
1546 ((ios
->timing
== MMC_TIMING_UHS_SDR12
) ||
1547 (ios
->timing
== MMC_TIMING_UHS_SDR25
) ||
1548 (ios
->timing
== MMC_TIMING_UHS_SDR50
) ||
1549 (ios
->timing
== MMC_TIMING_UHS_SDR104
) ||
1550 (ios
->timing
== MMC_TIMING_UHS_DDR50
))) {
1553 sdhci_enable_preset_value(host
, true);
1554 preset
= sdhci_get_preset_value(host
);
1555 ios
->drv_type
= (preset
& SDHCI_PRESET_DRV_MASK
)
1556 >> SDHCI_PRESET_DRV_SHIFT
;
1559 /* Re-enable SD Clock */
1560 sdhci_update_clock(host
);
1562 sdhci_writeb(host
, ctrl
, SDHCI_HOST_CONTROL
);
1565 * Some (ENE) controllers go apeshit on some ios operation,
1566 * signalling timeout and CRC errors even on CMD0. Resetting
1567 * it on each ios seems to solve the problem.
1569 if(host
->quirks
& SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS
)
1570 sdhci_reset(host
, SDHCI_RESET_CMD
| SDHCI_RESET_DATA
);
1573 spin_unlock_irqrestore(&host
->lock
, flags
);
1576 static void sdhci_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
1578 struct sdhci_host
*host
= mmc_priv(mmc
);
1580 sdhci_runtime_pm_get(host
);
1581 sdhci_do_set_ios(host
, ios
);
1582 sdhci_runtime_pm_put(host
);
1585 static int sdhci_do_get_cd(struct sdhci_host
*host
)
1587 int gpio_cd
= mmc_gpio_get_cd(host
->mmc
);
1589 if (host
->flags
& SDHCI_DEVICE_DEAD
)
1592 /* If polling/nonremovable, assume that the card is always present. */
1593 if ((host
->quirks
& SDHCI_QUIRK_BROKEN_CARD_DETECTION
) ||
1594 (host
->mmc
->caps
& MMC_CAP_NONREMOVABLE
))
1597 /* Try slot gpio detect */
1598 if (!IS_ERR_VALUE(gpio_cd
))
1601 /* Host native card detect */
1602 return !!(sdhci_readl(host
, SDHCI_PRESENT_STATE
) & SDHCI_CARD_PRESENT
);
1605 static int sdhci_get_cd(struct mmc_host
*mmc
)
1607 struct sdhci_host
*host
= mmc_priv(mmc
);
1610 sdhci_runtime_pm_get(host
);
1611 ret
= sdhci_do_get_cd(host
);
1612 sdhci_runtime_pm_put(host
);
1616 static int sdhci_check_ro(struct sdhci_host
*host
)
1618 unsigned long flags
;
1621 spin_lock_irqsave(&host
->lock
, flags
);
1623 if (host
->flags
& SDHCI_DEVICE_DEAD
)
1625 else if (host
->ops
->get_ro
)
1626 is_readonly
= host
->ops
->get_ro(host
);
1628 is_readonly
= !(sdhci_readl(host
, SDHCI_PRESENT_STATE
)
1629 & SDHCI_WRITE_PROTECT
);
1631 spin_unlock_irqrestore(&host
->lock
, flags
);
1633 /* This quirk needs to be replaced by a callback-function later */
1634 return host
->quirks
& SDHCI_QUIRK_INVERTED_WRITE_PROTECT
?
1635 !is_readonly
: is_readonly
;
1638 #define SAMPLE_COUNT 5
1640 static int sdhci_do_get_ro(struct sdhci_host
*host
)
1644 if (!(host
->quirks
& SDHCI_QUIRK_UNSTABLE_RO_DETECT
))
1645 return sdhci_check_ro(host
);
1648 for (i
= 0; i
< SAMPLE_COUNT
; i
++) {
1649 if (sdhci_check_ro(host
)) {
1650 if (++ro_count
> SAMPLE_COUNT
/ 2)
1658 static void sdhci_hw_reset(struct mmc_host
*mmc
)
1660 struct sdhci_host
*host
= mmc_priv(mmc
);
1662 if (host
->ops
&& host
->ops
->hw_reset
)
1663 host
->ops
->hw_reset(host
);
1666 static int sdhci_get_ro(struct mmc_host
*mmc
)
1668 struct sdhci_host
*host
= mmc_priv(mmc
);
1671 sdhci_runtime_pm_get(host
);
1672 ret
= sdhci_do_get_ro(host
);
1673 sdhci_runtime_pm_put(host
);
1677 static void sdhci_enable_sdio_irq_nolock(struct sdhci_host
*host
, int enable
)
1679 if (host
->flags
& SDHCI_DEVICE_DEAD
)
1683 host
->flags
|= SDHCI_SDIO_IRQ_ENABLED
;
1685 host
->flags
&= ~SDHCI_SDIO_IRQ_ENABLED
;
1687 /* SDIO IRQ will be enabled as appropriate in runtime resume */
1688 if (host
->runtime_suspended
)
1692 sdhci_unmask_irqs(host
, SDHCI_INT_CARD_INT
);
1694 sdhci_mask_irqs(host
, SDHCI_INT_CARD_INT
);
1699 static void sdhci_enable_sdio_irq(struct mmc_host
*mmc
, int enable
)
1701 struct sdhci_host
*host
= mmc_priv(mmc
);
1702 unsigned long flags
;
1704 spin_lock_irqsave(&host
->lock
, flags
);
1705 sdhci_enable_sdio_irq_nolock(host
, enable
);
1706 spin_unlock_irqrestore(&host
->lock
, flags
);
1709 static int sdhci_do_start_signal_voltage_switch(struct sdhci_host
*host
,
1710 struct mmc_ios
*ios
)
1716 * Signal Voltage Switching is only applicable for Host Controllers
1719 if (host
->version
< SDHCI_SPEC_300
)
1722 ctrl
= sdhci_readw(host
, SDHCI_HOST_CONTROL2
);
1724 switch (ios
->signal_voltage
) {
1725 case MMC_SIGNAL_VOLTAGE_330
:
1726 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1727 ctrl
&= ~SDHCI_CTRL_VDD_180
;
1728 sdhci_writew(host
, ctrl
, SDHCI_HOST_CONTROL2
);
1731 ret
= regulator_set_voltage(host
->vqmmc
, 2700000, 3600000);
1733 pr_warning("%s: Switching to 3.3V signalling voltage "
1734 " failed\n", mmc_hostname(host
->mmc
));
1739 usleep_range(5000, 5500);
1741 /* 3.3V regulator output should be stable within 5 ms */
1742 ctrl
= sdhci_readw(host
, SDHCI_HOST_CONTROL2
);
1743 if (!(ctrl
& SDHCI_CTRL_VDD_180
))
1746 pr_warning("%s: 3.3V regulator output did not became stable\n",
1747 mmc_hostname(host
->mmc
));
1750 case MMC_SIGNAL_VOLTAGE_180
:
1752 ret
= regulator_set_voltage(host
->vqmmc
,
1755 pr_warning("%s: Switching to 1.8V signalling voltage "
1756 " failed\n", mmc_hostname(host
->mmc
));
1762 * Enable 1.8V Signal Enable in the Host Control2
1765 ctrl
|= SDHCI_CTRL_VDD_180
;
1766 sdhci_writew(host
, ctrl
, SDHCI_HOST_CONTROL2
);
1769 usleep_range(5000, 5500);
1771 /* 1.8V regulator output should be stable within 5 ms */
1772 ctrl
= sdhci_readw(host
, SDHCI_HOST_CONTROL2
);
1773 if (ctrl
& SDHCI_CTRL_VDD_180
)
1776 pr_warning("%s: 1.8V regulator output did not became stable\n",
1777 mmc_hostname(host
->mmc
));
1780 case MMC_SIGNAL_VOLTAGE_120
:
1782 ret
= regulator_set_voltage(host
->vqmmc
, 1100000, 1300000);
1784 pr_warning("%s: Switching to 1.2V signalling voltage "
1785 " failed\n", mmc_hostname(host
->mmc
));
1791 /* No signal voltage switch required */
1796 static int sdhci_start_signal_voltage_switch(struct mmc_host
*mmc
,
1797 struct mmc_ios
*ios
)
1799 struct sdhci_host
*host
= mmc_priv(mmc
);
1802 if (host
->version
< SDHCI_SPEC_300
)
1804 sdhci_runtime_pm_get(host
);
1805 err
= sdhci_do_start_signal_voltage_switch(host
, ios
);
1806 sdhci_runtime_pm_put(host
);
1810 static int sdhci_card_busy(struct mmc_host
*mmc
)
1812 struct sdhci_host
*host
= mmc_priv(mmc
);
1815 sdhci_runtime_pm_get(host
);
1816 /* Check whether DAT[3:0] is 0000 */
1817 present_state
= sdhci_readl(host
, SDHCI_PRESENT_STATE
);
1818 sdhci_runtime_pm_put(host
);
1820 return !(present_state
& SDHCI_DATA_LVL_MASK
);
1823 static int sdhci_execute_tuning(struct mmc_host
*mmc
, u32 opcode
)
1825 struct sdhci_host
*host
;
1828 int tuning_loop_counter
= MAX_TUNING_LOOP
;
1829 unsigned long timeout
;
1831 bool requires_tuning_nonuhs
= false;
1833 host
= mmc_priv(mmc
);
1835 sdhci_runtime_pm_get(host
);
1836 disable_irq(host
->irq
);
1837 spin_lock(&host
->lock
);
1839 ctrl
= sdhci_readw(host
, SDHCI_HOST_CONTROL2
);
1842 * The Host Controller needs tuning only in case of SDR104 mode
1843 * and for SDR50 mode when Use Tuning for SDR50 is set in the
1844 * Capabilities register.
1845 * If the Host Controller supports the HS200 mode then the
1846 * tuning function has to be executed.
1848 if (((ctrl
& SDHCI_CTRL_UHS_MASK
) == SDHCI_CTRL_UHS_SDR50
) &&
1849 (host
->flags
& SDHCI_SDR50_NEEDS_TUNING
||
1850 host
->flags
& SDHCI_HS200_NEEDS_TUNING
))
1851 requires_tuning_nonuhs
= true;
1853 if (((ctrl
& SDHCI_CTRL_UHS_MASK
) == SDHCI_CTRL_UHS_SDR104
) ||
1854 requires_tuning_nonuhs
)
1855 ctrl
|= SDHCI_CTRL_EXEC_TUNING
;
1857 spin_unlock(&host
->lock
);
1858 enable_irq(host
->irq
);
1859 sdhci_runtime_pm_put(host
);
1863 sdhci_writew(host
, ctrl
, SDHCI_HOST_CONTROL2
);
1866 * As per the Host Controller spec v3.00, tuning command
1867 * generates Buffer Read Ready interrupt, so enable that.
1869 * Note: The spec clearly says that when tuning sequence
1870 * is being performed, the controller does not generate
1871 * interrupts other than Buffer Read Ready interrupt. But
1872 * to make sure we don't hit a controller bug, we _only_
1873 * enable Buffer Read Ready interrupt here.
1875 ier
= sdhci_readl(host
, SDHCI_INT_ENABLE
);
1876 sdhci_clear_set_irqs(host
, ier
, SDHCI_INT_DATA_AVAIL
);
1879 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1880 * of loops reaches 40 times or a timeout of 150ms occurs.
1884 struct mmc_command cmd
= {0};
1885 struct mmc_request mrq
= {NULL
};
1887 if (!tuning_loop_counter
&& !timeout
)
1890 cmd
.opcode
= opcode
;
1892 cmd
.flags
= MMC_RSP_R1
| MMC_CMD_ADTC
;
1901 * In response to CMD19, the card sends 64 bytes of tuning
1902 * block to the Host Controller. So we set the block size
1905 if (cmd
.opcode
== MMC_SEND_TUNING_BLOCK_HS200
) {
1906 if (mmc
->ios
.bus_width
== MMC_BUS_WIDTH_8
)
1907 sdhci_writew(host
, SDHCI_MAKE_BLKSZ(7, 128),
1909 else if (mmc
->ios
.bus_width
== MMC_BUS_WIDTH_4
)
1910 sdhci_writew(host
, SDHCI_MAKE_BLKSZ(7, 64),
1913 sdhci_writew(host
, SDHCI_MAKE_BLKSZ(7, 64),
1918 * The tuning block is sent by the card to the host controller.
1919 * So we set the TRNS_READ bit in the Transfer Mode register.
1920 * This also takes care of setting DMA Enable and Multi Block
1921 * Select in the same register to 0.
1923 sdhci_writew(host
, SDHCI_TRNS_READ
, SDHCI_TRANSFER_MODE
);
1925 sdhci_send_command(host
, &cmd
);
1930 spin_unlock(&host
->lock
);
1931 enable_irq(host
->irq
);
1933 /* Wait for Buffer Read Ready interrupt */
1934 wait_event_interruptible_timeout(host
->buf_ready_int
,
1935 (host
->tuning_done
== 1),
1936 msecs_to_jiffies(50));
1937 disable_irq(host
->irq
);
1938 spin_lock(&host
->lock
);
1940 if (!host
->tuning_done
) {
1941 pr_info(DRIVER_NAME
": Timeout waiting for "
1942 "Buffer Read Ready interrupt during tuning "
1943 "procedure, falling back to fixed sampling "
1945 ctrl
= sdhci_readw(host
, SDHCI_HOST_CONTROL2
);
1946 ctrl
&= ~SDHCI_CTRL_TUNED_CLK
;
1947 ctrl
&= ~SDHCI_CTRL_EXEC_TUNING
;
1948 sdhci_writew(host
, ctrl
, SDHCI_HOST_CONTROL2
);
1954 host
->tuning_done
= 0;
1956 ctrl
= sdhci_readw(host
, SDHCI_HOST_CONTROL2
);
1957 tuning_loop_counter
--;
1960 } while (ctrl
& SDHCI_CTRL_EXEC_TUNING
);
1963 * The Host Driver has exhausted the maximum number of loops allowed,
1964 * so use fixed sampling frequency.
1966 if (!tuning_loop_counter
|| !timeout
) {
1967 ctrl
&= ~SDHCI_CTRL_TUNED_CLK
;
1968 sdhci_writew(host
, ctrl
, SDHCI_HOST_CONTROL2
);
1970 if (!(ctrl
& SDHCI_CTRL_TUNED_CLK
)) {
1971 pr_info(DRIVER_NAME
": Tuning procedure"
1972 " failed, falling back to fixed sampling"
1980 * If this is the very first time we are here, we start the retuning
1981 * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
1982 * flag won't be set, we check this condition before actually starting
1985 if (!(host
->flags
& SDHCI_NEEDS_RETUNING
) && host
->tuning_count
&&
1986 (host
->tuning_mode
== SDHCI_TUNING_MODE_1
)) {
1987 host
->flags
|= SDHCI_USING_RETUNING_TIMER
;
1988 mod_timer(&host
->tuning_timer
, jiffies
+
1989 host
->tuning_count
* HZ
);
1990 /* Tuning mode 1 limits the maximum data length to 4MB */
1991 mmc
->max_blk_count
= (4 * 1024 * 1024) / mmc
->max_blk_size
;
1993 host
->flags
&= ~SDHCI_NEEDS_RETUNING
;
1994 /* Reload the new initial value for timer */
1995 if (host
->tuning_mode
== SDHCI_TUNING_MODE_1
)
1996 mod_timer(&host
->tuning_timer
, jiffies
+
1997 host
->tuning_count
* HZ
);
2001 * In case tuning fails, host controllers which support re-tuning can
2002 * try tuning again at a later time, when the re-tuning timer expires.
2003 * So for these controllers, we return 0. Since there might be other
2004 * controllers who do not have this capability, we return error for
2005 * them. SDHCI_USING_RETUNING_TIMER means the host is currently using
2006 * a retuning timer to do the retuning for the card.
2008 if (err
&& (host
->flags
& SDHCI_USING_RETUNING_TIMER
))
2011 sdhci_clear_set_irqs(host
, SDHCI_INT_DATA_AVAIL
, ier
);
2012 spin_unlock(&host
->lock
);
2013 enable_irq(host
->irq
);
2014 sdhci_runtime_pm_put(host
);
2020 static void sdhci_enable_preset_value(struct sdhci_host
*host
, bool enable
)
2024 /* Host Controller v3.00 defines preset value registers */
2025 if (host
->version
< SDHCI_SPEC_300
)
2028 ctrl
= sdhci_readw(host
, SDHCI_HOST_CONTROL2
);
2031 * We only enable or disable Preset Value if they are not already
2032 * enabled or disabled respectively. Otherwise, we bail out.
2034 if (enable
&& !(ctrl
& SDHCI_CTRL_PRESET_VAL_ENABLE
)) {
2035 ctrl
|= SDHCI_CTRL_PRESET_VAL_ENABLE
;
2036 sdhci_writew(host
, ctrl
, SDHCI_HOST_CONTROL2
);
2037 host
->flags
|= SDHCI_PV_ENABLED
;
2038 } else if (!enable
&& (ctrl
& SDHCI_CTRL_PRESET_VAL_ENABLE
)) {
2039 ctrl
&= ~SDHCI_CTRL_PRESET_VAL_ENABLE
;
2040 sdhci_writew(host
, ctrl
, SDHCI_HOST_CONTROL2
);
2041 host
->flags
&= ~SDHCI_PV_ENABLED
;
2045 static void sdhci_card_event(struct mmc_host
*mmc
)
2047 struct sdhci_host
*host
= mmc_priv(mmc
);
2048 unsigned long flags
;
2050 spin_lock_irqsave(&host
->lock
, flags
);
2052 /* Check host->mrq first in case we are runtime suspended */
2054 !(sdhci_readl(host
, SDHCI_PRESENT_STATE
) & SDHCI_CARD_PRESENT
)) {
2055 pr_err("%s: Card removed during transfer!\n",
2056 mmc_hostname(host
->mmc
));
2057 pr_err("%s: Resetting controller.\n",
2058 mmc_hostname(host
->mmc
));
2060 sdhci_reset(host
, SDHCI_RESET_CMD
);
2061 sdhci_reset(host
, SDHCI_RESET_DATA
);
2063 host
->mrq
->cmd
->error
= -ENOMEDIUM
;
2064 tasklet_schedule(&host
->finish_tasklet
);
2067 spin_unlock_irqrestore(&host
->lock
, flags
);
2070 static const struct mmc_host_ops sdhci_ops
= {
2071 .request
= sdhci_request
,
2072 .set_ios
= sdhci_set_ios
,
2073 .get_cd
= sdhci_get_cd
,
2074 .get_ro
= sdhci_get_ro
,
2075 .hw_reset
= sdhci_hw_reset
,
2076 .enable_sdio_irq
= sdhci_enable_sdio_irq
,
2077 .start_signal_voltage_switch
= sdhci_start_signal_voltage_switch
,
2078 .execute_tuning
= sdhci_execute_tuning
,
2079 .card_event
= sdhci_card_event
,
2080 .card_busy
= sdhci_card_busy
,
2083 /*****************************************************************************\
2087 \*****************************************************************************/
2089 static void sdhci_tasklet_card(unsigned long param
)
2091 struct sdhci_host
*host
= (struct sdhci_host
*)param
;
2093 sdhci_card_event(host
->mmc
);
2095 mmc_detect_change(host
->mmc
, msecs_to_jiffies(200));
2098 static void sdhci_tasklet_finish(unsigned long param
)
2100 struct sdhci_host
*host
;
2101 unsigned long flags
;
2102 struct mmc_request
*mrq
;
2104 host
= (struct sdhci_host
*)param
;
2106 spin_lock_irqsave(&host
->lock
, flags
);
2109 * If this tasklet gets rescheduled while running, it will
2110 * be run again afterwards but without any active request.
2113 spin_unlock_irqrestore(&host
->lock
, flags
);
2117 del_timer(&host
->timer
);
2122 * The controller needs a reset of internal state machines
2123 * upon error conditions.
2125 if (!(host
->flags
& SDHCI_DEVICE_DEAD
) &&
2126 ((mrq
->cmd
&& mrq
->cmd
->error
) ||
2127 (mrq
->data
&& (mrq
->data
->error
||
2128 (mrq
->data
->stop
&& mrq
->data
->stop
->error
))) ||
2129 (host
->quirks
& SDHCI_QUIRK_RESET_AFTER_REQUEST
))) {
2131 /* Some controllers need this kick or reset won't work here */
2132 if (host
->quirks
& SDHCI_QUIRK_CLOCK_BEFORE_RESET
)
2133 /* This is to force an update */
2134 sdhci_update_clock(host
);
2136 /* Spec says we should do both at the same time, but Ricoh
2137 controllers do not like that. */
2138 sdhci_reset(host
, SDHCI_RESET_CMD
);
2139 sdhci_reset(host
, SDHCI_RESET_DATA
);
2146 #ifndef SDHCI_USE_LEDS_CLASS
2147 sdhci_deactivate_led(host
);
2151 spin_unlock_irqrestore(&host
->lock
, flags
);
2153 mmc_request_done(host
->mmc
, mrq
);
2154 sdhci_runtime_pm_put(host
);
2157 static void sdhci_timeout_timer(unsigned long data
)
2159 struct sdhci_host
*host
;
2160 unsigned long flags
;
2162 host
= (struct sdhci_host
*)data
;
2164 spin_lock_irqsave(&host
->lock
, flags
);
2167 pr_err("%s: Timeout waiting for hardware "
2168 "interrupt.\n", mmc_hostname(host
->mmc
));
2169 sdhci_dumpregs(host
);
2172 host
->data
->error
= -ETIMEDOUT
;
2173 sdhci_finish_data(host
);
2176 host
->cmd
->error
= -ETIMEDOUT
;
2178 host
->mrq
->cmd
->error
= -ETIMEDOUT
;
2180 tasklet_schedule(&host
->finish_tasklet
);
2185 spin_unlock_irqrestore(&host
->lock
, flags
);
2188 static void sdhci_tuning_timer(unsigned long data
)
2190 struct sdhci_host
*host
;
2191 unsigned long flags
;
2193 host
= (struct sdhci_host
*)data
;
2195 spin_lock_irqsave(&host
->lock
, flags
);
2197 host
->flags
|= SDHCI_NEEDS_RETUNING
;
2199 spin_unlock_irqrestore(&host
->lock
, flags
);
2202 /*****************************************************************************\
2204 * Interrupt handling *
2206 \*****************************************************************************/
2208 static void sdhci_cmd_irq(struct sdhci_host
*host
, u32 intmask
)
2210 BUG_ON(intmask
== 0);
2213 pr_err("%s: Got command interrupt 0x%08x even "
2214 "though no command operation was in progress.\n",
2215 mmc_hostname(host
->mmc
), (unsigned)intmask
);
2216 sdhci_dumpregs(host
);
2220 if (intmask
& SDHCI_INT_TIMEOUT
)
2221 host
->cmd
->error
= -ETIMEDOUT
;
2222 else if (intmask
& (SDHCI_INT_CRC
| SDHCI_INT_END_BIT
|
2224 host
->cmd
->error
= -EILSEQ
;
2226 if (host
->cmd
->error
) {
2227 tasklet_schedule(&host
->finish_tasklet
);
2232 * The host can send and interrupt when the busy state has
2233 * ended, allowing us to wait without wasting CPU cycles.
2234 * Unfortunately this is overloaded on the "data complete"
2235 * interrupt, so we need to take some care when handling
2238 * Note: The 1.0 specification is a bit ambiguous about this
2239 * feature so there might be some problems with older
2242 if (host
->cmd
->flags
& MMC_RSP_BUSY
) {
2243 if (host
->cmd
->data
)
2244 DBG("Cannot wait for busy signal when also "
2245 "doing a data transfer");
2246 else if (!(host
->quirks
& SDHCI_QUIRK_NO_BUSY_IRQ
))
2249 /* The controller does not support the end-of-busy IRQ,
2250 * fall through and take the SDHCI_INT_RESPONSE */
2253 if (intmask
& SDHCI_INT_RESPONSE
)
2254 sdhci_finish_command(host
);
2257 #ifdef CONFIG_MMC_DEBUG
2258 static void sdhci_show_adma_error(struct sdhci_host
*host
)
2260 const char *name
= mmc_hostname(host
->mmc
);
2261 u8
*desc
= host
->adma_desc
;
2266 sdhci_dumpregs(host
);
2269 dma
= (__le32
*)(desc
+ 4);
2270 len
= (__le16
*)(desc
+ 2);
2273 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2274 name
, desc
, le32_to_cpu(*dma
), le16_to_cpu(*len
), attr
);
2283 static void sdhci_show_adma_error(struct sdhci_host
*host
) { }
2286 static void sdhci_data_irq(struct sdhci_host
*host
, u32 intmask
)
2289 BUG_ON(intmask
== 0);
2291 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2292 if (intmask
& SDHCI_INT_DATA_AVAIL
) {
2293 command
= SDHCI_GET_CMD(sdhci_readw(host
, SDHCI_COMMAND
));
2294 if (command
== MMC_SEND_TUNING_BLOCK
||
2295 command
== MMC_SEND_TUNING_BLOCK_HS200
) {
2296 host
->tuning_done
= 1;
2297 wake_up(&host
->buf_ready_int
);
2304 * The "data complete" interrupt is also used to
2305 * indicate that a busy state has ended. See comment
2306 * above in sdhci_cmd_irq().
2308 if (host
->cmd
&& (host
->cmd
->flags
& MMC_RSP_BUSY
)) {
2309 if (intmask
& SDHCI_INT_DATA_END
) {
2310 sdhci_finish_command(host
);
2315 pr_err("%s: Got data interrupt 0x%08x even "
2316 "though no data operation was in progress.\n",
2317 mmc_hostname(host
->mmc
), (unsigned)intmask
);
2318 sdhci_dumpregs(host
);
2323 if (intmask
& SDHCI_INT_DATA_TIMEOUT
)
2324 host
->data
->error
= -ETIMEDOUT
;
2325 else if (intmask
& SDHCI_INT_DATA_END_BIT
)
2326 host
->data
->error
= -EILSEQ
;
2327 else if ((intmask
& SDHCI_INT_DATA_CRC
) &&
2328 SDHCI_GET_CMD(sdhci_readw(host
, SDHCI_COMMAND
))
2330 host
->data
->error
= -EILSEQ
;
2331 else if (intmask
& SDHCI_INT_ADMA_ERROR
) {
2332 pr_err("%s: ADMA error\n", mmc_hostname(host
->mmc
));
2333 sdhci_show_adma_error(host
);
2334 host
->data
->error
= -EIO
;
2335 if (host
->ops
->adma_workaround
)
2336 host
->ops
->adma_workaround(host
, intmask
);
2339 if (host
->data
->error
)
2340 sdhci_finish_data(host
);
2342 if (intmask
& (SDHCI_INT_DATA_AVAIL
| SDHCI_INT_SPACE_AVAIL
))
2343 sdhci_transfer_pio(host
);
2346 * We currently don't do anything fancy with DMA
2347 * boundaries, but as we can't disable the feature
2348 * we need to at least restart the transfer.
2350 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2351 * should return a valid address to continue from, but as
2352 * some controllers are faulty, don't trust them.
2354 if (intmask
& SDHCI_INT_DMA_END
) {
2355 u32 dmastart
, dmanow
;
2356 dmastart
= sg_dma_address(host
->data
->sg
);
2357 dmanow
= dmastart
+ host
->data
->bytes_xfered
;
2359 * Force update to the next DMA block boundary.
2362 ~(SDHCI_DEFAULT_BOUNDARY_SIZE
- 1)) +
2363 SDHCI_DEFAULT_BOUNDARY_SIZE
;
2364 host
->data
->bytes_xfered
= dmanow
- dmastart
;
2365 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2367 mmc_hostname(host
->mmc
), dmastart
,
2368 host
->data
->bytes_xfered
, dmanow
);
2369 sdhci_writel(host
, dmanow
, SDHCI_DMA_ADDRESS
);
2372 if (intmask
& SDHCI_INT_DATA_END
) {
2375 * Data managed to finish before the
2376 * command completed. Make sure we do
2377 * things in the proper order.
2379 host
->data_early
= 1;
2381 sdhci_finish_data(host
);
2387 static irqreturn_t
sdhci_irq(int irq
, void *dev_id
)
2390 struct sdhci_host
*host
= dev_id
;
2391 u32 intmask
, unexpected
= 0;
2392 int cardint
= 0, max_loops
= 16;
2394 spin_lock(&host
->lock
);
2396 if (host
->runtime_suspended
) {
2397 spin_unlock(&host
->lock
);
2398 pr_warning("%s: got irq while runtime suspended\n",
2399 mmc_hostname(host
->mmc
));
2403 intmask
= sdhci_readl(host
, SDHCI_INT_STATUS
);
2405 if (!intmask
|| intmask
== 0xffffffff) {
2411 DBG("*** %s got interrupt: 0x%08x\n",
2412 mmc_hostname(host
->mmc
), intmask
);
2414 if (intmask
& (SDHCI_INT_CARD_INSERT
| SDHCI_INT_CARD_REMOVE
)) {
2415 u32 present
= sdhci_readl(host
, SDHCI_PRESENT_STATE
) &
2419 * There is a observation on i.mx esdhc. INSERT bit will be
2420 * immediately set again when it gets cleared, if a card is
2421 * inserted. We have to mask the irq to prevent interrupt
2422 * storm which will freeze the system. And the REMOVE gets
2423 * the same situation.
2425 * More testing are needed here to ensure it works for other
2428 sdhci_mask_irqs(host
, present
? SDHCI_INT_CARD_INSERT
:
2429 SDHCI_INT_CARD_REMOVE
);
2430 sdhci_unmask_irqs(host
, present
? SDHCI_INT_CARD_REMOVE
:
2431 SDHCI_INT_CARD_INSERT
);
2433 sdhci_writel(host
, intmask
& (SDHCI_INT_CARD_INSERT
|
2434 SDHCI_INT_CARD_REMOVE
), SDHCI_INT_STATUS
);
2435 intmask
&= ~(SDHCI_INT_CARD_INSERT
| SDHCI_INT_CARD_REMOVE
);
2436 tasklet_schedule(&host
->card_tasklet
);
2439 if (intmask
& SDHCI_INT_CMD_MASK
) {
2440 sdhci_writel(host
, intmask
& SDHCI_INT_CMD_MASK
,
2442 sdhci_cmd_irq(host
, intmask
& SDHCI_INT_CMD_MASK
);
2445 if (intmask
& SDHCI_INT_DATA_MASK
) {
2446 sdhci_writel(host
, intmask
& SDHCI_INT_DATA_MASK
,
2448 sdhci_data_irq(host
, intmask
& SDHCI_INT_DATA_MASK
);
2451 intmask
&= ~(SDHCI_INT_CMD_MASK
| SDHCI_INT_DATA_MASK
);
2453 intmask
&= ~SDHCI_INT_ERROR
;
2455 if (intmask
& SDHCI_INT_BUS_POWER
) {
2456 pr_err("%s: Card is consuming too much power!\n",
2457 mmc_hostname(host
->mmc
));
2458 sdhci_writel(host
, SDHCI_INT_BUS_POWER
, SDHCI_INT_STATUS
);
2461 intmask
&= ~SDHCI_INT_BUS_POWER
;
2463 if (intmask
& SDHCI_INT_CARD_INT
)
2466 intmask
&= ~SDHCI_INT_CARD_INT
;
2469 unexpected
|= intmask
;
2470 sdhci_writel(host
, intmask
, SDHCI_INT_STATUS
);
2473 result
= IRQ_HANDLED
;
2475 intmask
= sdhci_readl(host
, SDHCI_INT_STATUS
);
2476 if (intmask
&& --max_loops
)
2479 spin_unlock(&host
->lock
);
2482 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2483 mmc_hostname(host
->mmc
), unexpected
);
2484 sdhci_dumpregs(host
);
2487 * We have to delay this as it calls back into the driver.
2490 mmc_signal_sdio_irq(host
->mmc
);
2495 /*****************************************************************************\
2499 \*****************************************************************************/
2502 void sdhci_enable_irq_wakeups(struct sdhci_host
*host
)
2505 u8 mask
= SDHCI_WAKE_ON_INSERT
| SDHCI_WAKE_ON_REMOVE
2506 | SDHCI_WAKE_ON_INT
;
2508 val
= sdhci_readb(host
, SDHCI_WAKE_UP_CONTROL
);
2510 /* Avoid fake wake up */
2511 if (host
->quirks
& SDHCI_QUIRK_BROKEN_CARD_DETECTION
)
2512 val
&= ~(SDHCI_WAKE_ON_INSERT
| SDHCI_WAKE_ON_REMOVE
);
2513 sdhci_writeb(host
, val
, SDHCI_WAKE_UP_CONTROL
);
2515 EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups
);
2517 void sdhci_disable_irq_wakeups(struct sdhci_host
*host
)
2520 u8 mask
= SDHCI_WAKE_ON_INSERT
| SDHCI_WAKE_ON_REMOVE
2521 | SDHCI_WAKE_ON_INT
;
2523 val
= sdhci_readb(host
, SDHCI_WAKE_UP_CONTROL
);
2525 sdhci_writeb(host
, val
, SDHCI_WAKE_UP_CONTROL
);
2527 EXPORT_SYMBOL_GPL(sdhci_disable_irq_wakeups
);
2529 int sdhci_suspend_host(struct sdhci_host
*host
)
2533 if (host
->ops
->platform_suspend
)
2534 host
->ops
->platform_suspend(host
);
2536 sdhci_disable_card_detection(host
);
2538 /* Disable tuning since we are suspending */
2539 if (host
->flags
& SDHCI_USING_RETUNING_TIMER
) {
2540 del_timer_sync(&host
->tuning_timer
);
2541 host
->flags
&= ~SDHCI_NEEDS_RETUNING
;
2544 ret
= mmc_suspend_host(host
->mmc
);
2546 if (host
->flags
& SDHCI_USING_RETUNING_TIMER
) {
2547 host
->flags
|= SDHCI_NEEDS_RETUNING
;
2548 mod_timer(&host
->tuning_timer
, jiffies
+
2549 host
->tuning_count
* HZ
);
2552 sdhci_enable_card_detection(host
);
2557 if (!device_may_wakeup(mmc_dev(host
->mmc
))) {
2558 sdhci_mask_irqs(host
, SDHCI_INT_ALL_MASK
);
2559 free_irq(host
->irq
, host
);
2561 sdhci_enable_irq_wakeups(host
);
2562 enable_irq_wake(host
->irq
);
2567 EXPORT_SYMBOL_GPL(sdhci_suspend_host
);
2569 int sdhci_resume_host(struct sdhci_host
*host
)
2573 if (host
->flags
& (SDHCI_USE_SDMA
| SDHCI_USE_ADMA
)) {
2574 if (host
->ops
->enable_dma
)
2575 host
->ops
->enable_dma(host
);
2578 if (!device_may_wakeup(mmc_dev(host
->mmc
))) {
2579 ret
= request_irq(host
->irq
, sdhci_irq
, IRQF_SHARED
,
2580 mmc_hostname(host
->mmc
), host
);
2584 sdhci_disable_irq_wakeups(host
);
2585 disable_irq_wake(host
->irq
);
2588 if ((host
->mmc
->pm_flags
& MMC_PM_KEEP_POWER
) &&
2589 (host
->quirks2
& SDHCI_QUIRK2_HOST_OFF_CARD_ON
)) {
2590 /* Card keeps power but host controller does not */
2591 sdhci_init(host
, 0);
2594 sdhci_do_set_ios(host
, &host
->mmc
->ios
);
2596 sdhci_init(host
, (host
->mmc
->pm_flags
& MMC_PM_KEEP_POWER
));
2600 ret
= mmc_resume_host(host
->mmc
);
2601 sdhci_enable_card_detection(host
);
2603 if (host
->ops
->platform_resume
)
2604 host
->ops
->platform_resume(host
);
2606 /* Set the re-tuning expiration flag */
2607 if (host
->flags
& SDHCI_USING_RETUNING_TIMER
)
2608 host
->flags
|= SDHCI_NEEDS_RETUNING
;
2613 EXPORT_SYMBOL_GPL(sdhci_resume_host
);
2614 #endif /* CONFIG_PM */
2616 #ifdef CONFIG_PM_RUNTIME
2618 static int sdhci_runtime_pm_get(struct sdhci_host
*host
)
2620 return pm_runtime_get_sync(host
->mmc
->parent
);
2623 static int sdhci_runtime_pm_put(struct sdhci_host
*host
)
2625 pm_runtime_mark_last_busy(host
->mmc
->parent
);
2626 return pm_runtime_put_autosuspend(host
->mmc
->parent
);
2629 int sdhci_runtime_suspend_host(struct sdhci_host
*host
)
2631 unsigned long flags
;
2634 /* Disable tuning since we are suspending */
2635 if (host
->flags
& SDHCI_USING_RETUNING_TIMER
) {
2636 del_timer_sync(&host
->tuning_timer
);
2637 host
->flags
&= ~SDHCI_NEEDS_RETUNING
;
2640 spin_lock_irqsave(&host
->lock
, flags
);
2641 sdhci_mask_irqs(host
, SDHCI_INT_ALL_MASK
);
2642 spin_unlock_irqrestore(&host
->lock
, flags
);
2644 synchronize_irq(host
->irq
);
2646 spin_lock_irqsave(&host
->lock
, flags
);
2647 host
->runtime_suspended
= true;
2648 spin_unlock_irqrestore(&host
->lock
, flags
);
2652 EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host
);
2654 int sdhci_runtime_resume_host(struct sdhci_host
*host
)
2656 unsigned long flags
;
2657 int ret
= 0, host_flags
= host
->flags
;
2659 if (host_flags
& (SDHCI_USE_SDMA
| SDHCI_USE_ADMA
)) {
2660 if (host
->ops
->enable_dma
)
2661 host
->ops
->enable_dma(host
);
2664 sdhci_init(host
, 0);
2666 /* Force clock and power re-program */
2669 sdhci_do_set_ios(host
, &host
->mmc
->ios
);
2671 sdhci_do_start_signal_voltage_switch(host
, &host
->mmc
->ios
);
2672 if ((host_flags
& SDHCI_PV_ENABLED
) &&
2673 !(host
->quirks2
& SDHCI_QUIRK2_PRESET_VALUE_BROKEN
)) {
2674 spin_lock_irqsave(&host
->lock
, flags
);
2675 sdhci_enable_preset_value(host
, true);
2676 spin_unlock_irqrestore(&host
->lock
, flags
);
2679 /* Set the re-tuning expiration flag */
2680 if (host
->flags
& SDHCI_USING_RETUNING_TIMER
)
2681 host
->flags
|= SDHCI_NEEDS_RETUNING
;
2683 spin_lock_irqsave(&host
->lock
, flags
);
2685 host
->runtime_suspended
= false;
2687 /* Enable SDIO IRQ */
2688 if ((host
->flags
& SDHCI_SDIO_IRQ_ENABLED
))
2689 sdhci_enable_sdio_irq_nolock(host
, true);
2691 /* Enable Card Detection */
2692 sdhci_enable_card_detection(host
);
2694 spin_unlock_irqrestore(&host
->lock
, flags
);
2698 EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host
);
2702 /*****************************************************************************\
2704 * Device allocation/registration *
2706 \*****************************************************************************/
2708 struct sdhci_host
*sdhci_alloc_host(struct device
*dev
,
2711 struct mmc_host
*mmc
;
2712 struct sdhci_host
*host
;
2714 WARN_ON(dev
== NULL
);
2716 mmc
= mmc_alloc_host(sizeof(struct sdhci_host
) + priv_size
, dev
);
2718 return ERR_PTR(-ENOMEM
);
2720 host
= mmc_priv(mmc
);
2726 EXPORT_SYMBOL_GPL(sdhci_alloc_host
);
2728 int sdhci_add_host(struct sdhci_host
*host
)
2730 struct mmc_host
*mmc
;
2731 u32 caps
[2] = {0, 0};
2732 u32 max_current_caps
;
2733 unsigned int ocr_avail
;
2736 WARN_ON(host
== NULL
);
2743 host
->quirks
= debug_quirks
;
2745 host
->quirks2
= debug_quirks2
;
2747 sdhci_reset(host
, SDHCI_RESET_ALL
);
2749 host
->version
= sdhci_readw(host
, SDHCI_HOST_VERSION
);
2750 host
->version
= (host
->version
& SDHCI_SPEC_VER_MASK
)
2751 >> SDHCI_SPEC_VER_SHIFT
;
2752 if (host
->version
> SDHCI_SPEC_300
) {
2753 pr_err("%s: Unknown controller version (%d). "
2754 "You may experience problems.\n", mmc_hostname(mmc
),
2758 caps
[0] = (host
->quirks
& SDHCI_QUIRK_MISSING_CAPS
) ? host
->caps
:
2759 sdhci_readl(host
, SDHCI_CAPABILITIES
);
2761 if (host
->version
>= SDHCI_SPEC_300
)
2762 caps
[1] = (host
->quirks
& SDHCI_QUIRK_MISSING_CAPS
) ?
2764 sdhci_readl(host
, SDHCI_CAPABILITIES_1
);
2766 if (host
->quirks
& SDHCI_QUIRK_FORCE_DMA
)
2767 host
->flags
|= SDHCI_USE_SDMA
;
2768 else if (!(caps
[0] & SDHCI_CAN_DO_SDMA
))
2769 DBG("Controller doesn't have SDMA capability\n");
2771 host
->flags
|= SDHCI_USE_SDMA
;
2773 if ((host
->quirks
& SDHCI_QUIRK_BROKEN_DMA
) &&
2774 (host
->flags
& SDHCI_USE_SDMA
)) {
2775 DBG("Disabling DMA as it is marked broken\n");
2776 host
->flags
&= ~SDHCI_USE_SDMA
;
2779 if ((host
->version
>= SDHCI_SPEC_200
) &&
2780 (caps
[0] & SDHCI_CAN_DO_ADMA2
))
2781 host
->flags
|= SDHCI_USE_ADMA
;
2783 if ((host
->quirks
& SDHCI_QUIRK_BROKEN_ADMA
) &&
2784 (host
->flags
& SDHCI_USE_ADMA
)) {
2785 DBG("Disabling ADMA as it is marked broken\n");
2786 host
->flags
&= ~SDHCI_USE_ADMA
;
2789 if (host
->flags
& (SDHCI_USE_SDMA
| SDHCI_USE_ADMA
)) {
2790 if (host
->ops
->enable_dma
) {
2791 if (host
->ops
->enable_dma(host
)) {
2792 pr_warning("%s: No suitable DMA "
2793 "available. Falling back to PIO.\n",
2796 ~(SDHCI_USE_SDMA
| SDHCI_USE_ADMA
);
2801 if (host
->flags
& SDHCI_USE_ADMA
) {
2803 * We need to allocate descriptors for all sg entries
2804 * (128) and potentially one alignment transfer for
2805 * each of those entries.
2807 host
->adma_desc
= kmalloc((128 * 2 + 1) * 4, GFP_KERNEL
);
2808 host
->align_buffer
= kmalloc(128 * 4, GFP_KERNEL
);
2809 if (!host
->adma_desc
|| !host
->align_buffer
) {
2810 kfree(host
->adma_desc
);
2811 kfree(host
->align_buffer
);
2812 pr_warning("%s: Unable to allocate ADMA "
2813 "buffers. Falling back to standard DMA.\n",
2815 host
->flags
&= ~SDHCI_USE_ADMA
;
2820 * If we use DMA, then it's up to the caller to set the DMA
2821 * mask, but PIO does not need the hw shim so we set a new
2822 * mask here in that case.
2824 if (!(host
->flags
& (SDHCI_USE_SDMA
| SDHCI_USE_ADMA
))) {
2825 host
->dma_mask
= DMA_BIT_MASK(64);
2826 mmc_dev(host
->mmc
)->dma_mask
= &host
->dma_mask
;
2829 if (host
->version
>= SDHCI_SPEC_300
)
2830 host
->max_clk
= (caps
[0] & SDHCI_CLOCK_V3_BASE_MASK
)
2831 >> SDHCI_CLOCK_BASE_SHIFT
;
2833 host
->max_clk
= (caps
[0] & SDHCI_CLOCK_BASE_MASK
)
2834 >> SDHCI_CLOCK_BASE_SHIFT
;
2836 host
->max_clk
*= 1000000;
2837 if (host
->max_clk
== 0 || host
->quirks
&
2838 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN
) {
2839 if (!host
->ops
->get_max_clock
) {
2840 pr_err("%s: Hardware doesn't specify base clock "
2841 "frequency.\n", mmc_hostname(mmc
));
2844 host
->max_clk
= host
->ops
->get_max_clock(host
);
2848 * In case of Host Controller v3.00, find out whether clock
2849 * multiplier is supported.
2851 host
->clk_mul
= (caps
[1] & SDHCI_CLOCK_MUL_MASK
) >>
2852 SDHCI_CLOCK_MUL_SHIFT
;
2855 * In case the value in Clock Multiplier is 0, then programmable
2856 * clock mode is not supported, otherwise the actual clock
2857 * multiplier is one more than the value of Clock Multiplier
2858 * in the Capabilities Register.
2864 * Set host parameters.
2866 mmc
->ops
= &sdhci_ops
;
2867 mmc
->f_max
= host
->max_clk
;
2868 if (host
->ops
->get_min_clock
)
2869 mmc
->f_min
= host
->ops
->get_min_clock(host
);
2870 else if (host
->version
>= SDHCI_SPEC_300
) {
2871 if (host
->clk_mul
) {
2872 mmc
->f_min
= (host
->max_clk
* host
->clk_mul
) / 1024;
2873 mmc
->f_max
= host
->max_clk
* host
->clk_mul
;
2875 mmc
->f_min
= host
->max_clk
/ SDHCI_MAX_DIV_SPEC_300
;
2877 mmc
->f_min
= host
->max_clk
/ SDHCI_MAX_DIV_SPEC_200
;
2880 (caps
[0] & SDHCI_TIMEOUT_CLK_MASK
) >> SDHCI_TIMEOUT_CLK_SHIFT
;
2881 if (host
->timeout_clk
== 0) {
2882 if (host
->ops
->get_timeout_clock
) {
2883 host
->timeout_clk
= host
->ops
->get_timeout_clock(host
);
2884 } else if (!(host
->quirks
&
2885 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK
)) {
2886 pr_err("%s: Hardware doesn't specify timeout clock "
2887 "frequency.\n", mmc_hostname(mmc
));
2891 if (caps
[0] & SDHCI_TIMEOUT_CLK_UNIT
)
2892 host
->timeout_clk
*= 1000;
2894 if (host
->quirks
& SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK
)
2895 host
->timeout_clk
= mmc
->f_max
/ 1000;
2897 mmc
->max_discard_to
= (1 << 27) / host
->timeout_clk
;
2899 mmc
->caps
|= MMC_CAP_SDIO_IRQ
| MMC_CAP_ERASE
| MMC_CAP_CMD23
;
2901 if (host
->quirks
& SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12
)
2902 host
->flags
|= SDHCI_AUTO_CMD12
;
2904 /* Auto-CMD23 stuff only works in ADMA or PIO. */
2905 if ((host
->version
>= SDHCI_SPEC_300
) &&
2906 ((host
->flags
& SDHCI_USE_ADMA
) ||
2907 !(host
->flags
& SDHCI_USE_SDMA
))) {
2908 host
->flags
|= SDHCI_AUTO_CMD23
;
2909 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc
));
2911 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc
));
2915 * A controller may support 8-bit width, but the board itself
2916 * might not have the pins brought out. Boards that support
2917 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
2918 * their platform code before calling sdhci_add_host(), and we
2919 * won't assume 8-bit width for hosts without that CAP.
2921 if (!(host
->quirks
& SDHCI_QUIRK_FORCE_1_BIT_DATA
))
2922 mmc
->caps
|= MMC_CAP_4_BIT_DATA
;
2924 if (host
->quirks2
& SDHCI_QUIRK2_HOST_NO_CMD23
)
2925 mmc
->caps
&= ~MMC_CAP_CMD23
;
2927 if (caps
[0] & SDHCI_CAN_DO_HISPD
)
2928 mmc
->caps
|= MMC_CAP_SD_HIGHSPEED
| MMC_CAP_MMC_HIGHSPEED
;
2930 if ((host
->quirks
& SDHCI_QUIRK_BROKEN_CARD_DETECTION
) &&
2931 !(host
->mmc
->caps
& MMC_CAP_NONREMOVABLE
))
2932 mmc
->caps
|= MMC_CAP_NEEDS_POLL
;
2934 /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
2935 host
->vqmmc
= regulator_get(mmc_dev(mmc
), "vqmmc");
2936 if (IS_ERR_OR_NULL(host
->vqmmc
)) {
2937 if (PTR_ERR(host
->vqmmc
) < 0) {
2938 pr_info("%s: no vqmmc regulator found\n",
2943 ret
= regulator_enable(host
->vqmmc
);
2944 if (!regulator_is_supported_voltage(host
->vqmmc
, 1700000,
2946 caps
[1] &= ~(SDHCI_SUPPORT_SDR104
|
2947 SDHCI_SUPPORT_SDR50
|
2948 SDHCI_SUPPORT_DDR50
);
2950 pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
2951 mmc_hostname(mmc
), ret
);
2956 if (host
->quirks2
& SDHCI_QUIRK2_NO_1_8_V
)
2957 caps
[1] &= ~(SDHCI_SUPPORT_SDR104
| SDHCI_SUPPORT_SDR50
|
2958 SDHCI_SUPPORT_DDR50
);
2960 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
2961 if (caps
[1] & (SDHCI_SUPPORT_SDR104
| SDHCI_SUPPORT_SDR50
|
2962 SDHCI_SUPPORT_DDR50
))
2963 mmc
->caps
|= MMC_CAP_UHS_SDR12
| MMC_CAP_UHS_SDR25
;
2965 /* SDR104 supports also implies SDR50 support */
2966 if (caps
[1] & SDHCI_SUPPORT_SDR104
)
2967 mmc
->caps
|= MMC_CAP_UHS_SDR104
| MMC_CAP_UHS_SDR50
;
2968 else if (caps
[1] & SDHCI_SUPPORT_SDR50
)
2969 mmc
->caps
|= MMC_CAP_UHS_SDR50
;
2971 if (caps
[1] & SDHCI_SUPPORT_DDR50
)
2972 mmc
->caps
|= MMC_CAP_UHS_DDR50
;
2974 /* Does the host need tuning for SDR50? */
2975 if (caps
[1] & SDHCI_USE_SDR50_TUNING
)
2976 host
->flags
|= SDHCI_SDR50_NEEDS_TUNING
;
2978 /* Does the host need tuning for HS200? */
2979 if (mmc
->caps2
& MMC_CAP2_HS200
)
2980 host
->flags
|= SDHCI_HS200_NEEDS_TUNING
;
2982 /* Driver Type(s) (A, C, D) supported by the host */
2983 if (caps
[1] & SDHCI_DRIVER_TYPE_A
)
2984 mmc
->caps
|= MMC_CAP_DRIVER_TYPE_A
;
2985 if (caps
[1] & SDHCI_DRIVER_TYPE_C
)
2986 mmc
->caps
|= MMC_CAP_DRIVER_TYPE_C
;
2987 if (caps
[1] & SDHCI_DRIVER_TYPE_D
)
2988 mmc
->caps
|= MMC_CAP_DRIVER_TYPE_D
;
2990 /* Initial value for re-tuning timer count */
2991 host
->tuning_count
= (caps
[1] & SDHCI_RETUNING_TIMER_COUNT_MASK
) >>
2992 SDHCI_RETUNING_TIMER_COUNT_SHIFT
;
2995 * In case Re-tuning Timer is not disabled, the actual value of
2996 * re-tuning timer will be 2 ^ (n - 1).
2998 if (host
->tuning_count
)
2999 host
->tuning_count
= 1 << (host
->tuning_count
- 1);
3001 /* Re-tuning mode supported by the Host Controller */
3002 host
->tuning_mode
= (caps
[1] & SDHCI_RETUNING_MODE_MASK
) >>
3003 SDHCI_RETUNING_MODE_SHIFT
;
3007 host
->vmmc
= regulator_get(mmc_dev(mmc
), "vmmc");
3008 if (IS_ERR_OR_NULL(host
->vmmc
)) {
3009 if (PTR_ERR(host
->vmmc
) < 0) {
3010 pr_info("%s: no vmmc regulator found\n",
3016 #ifdef CONFIG_REGULATOR
3018 * Voltage range check makes sense only if regulator reports
3019 * any voltage value.
3021 if (host
->vmmc
&& regulator_get_voltage(host
->vmmc
) > 0) {
3022 ret
= regulator_is_supported_voltage(host
->vmmc
, 2700000,
3024 if ((ret
<= 0) || (!(caps
[0] & SDHCI_CAN_VDD_330
)))
3025 caps
[0] &= ~SDHCI_CAN_VDD_330
;
3026 if ((ret
<= 0) || (!(caps
[0] & SDHCI_CAN_VDD_300
)))
3027 caps
[0] &= ~SDHCI_CAN_VDD_300
;
3028 ret
= regulator_is_supported_voltage(host
->vmmc
, 1700000,
3030 if ((ret
<= 0) || (!(caps
[0] & SDHCI_CAN_VDD_180
)))
3031 caps
[0] &= ~SDHCI_CAN_VDD_180
;
3033 #endif /* CONFIG_REGULATOR */
3036 * According to SD Host Controller spec v3.00, if the Host System
3037 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3038 * the value is meaningful only if Voltage Support in the Capabilities
3039 * register is set. The actual current value is 4 times the register
3042 max_current_caps
= sdhci_readl(host
, SDHCI_MAX_CURRENT
);
3043 if (!max_current_caps
&& host
->vmmc
) {
3044 u32 curr
= regulator_get_current_limit(host
->vmmc
);
3047 /* convert to SDHCI_MAX_CURRENT format */
3048 curr
= curr
/1000; /* convert to mA */
3049 curr
= curr
/SDHCI_MAX_CURRENT_MULTIPLIER
;
3051 curr
= min_t(u32
, curr
, SDHCI_MAX_CURRENT_LIMIT
);
3053 (curr
<< SDHCI_MAX_CURRENT_330_SHIFT
) |
3054 (curr
<< SDHCI_MAX_CURRENT_300_SHIFT
) |
3055 (curr
<< SDHCI_MAX_CURRENT_180_SHIFT
);
3059 if (caps
[0] & SDHCI_CAN_VDD_330
) {
3060 ocr_avail
|= MMC_VDD_32_33
| MMC_VDD_33_34
;
3062 mmc
->max_current_330
= ((max_current_caps
&
3063 SDHCI_MAX_CURRENT_330_MASK
) >>
3064 SDHCI_MAX_CURRENT_330_SHIFT
) *
3065 SDHCI_MAX_CURRENT_MULTIPLIER
;
3067 if (caps
[0] & SDHCI_CAN_VDD_300
) {
3068 ocr_avail
|= MMC_VDD_29_30
| MMC_VDD_30_31
;
3070 mmc
->max_current_300
= ((max_current_caps
&
3071 SDHCI_MAX_CURRENT_300_MASK
) >>
3072 SDHCI_MAX_CURRENT_300_SHIFT
) *
3073 SDHCI_MAX_CURRENT_MULTIPLIER
;
3075 if (caps
[0] & SDHCI_CAN_VDD_180
) {
3076 ocr_avail
|= MMC_VDD_165_195
;
3078 mmc
->max_current_180
= ((max_current_caps
&
3079 SDHCI_MAX_CURRENT_180_MASK
) >>
3080 SDHCI_MAX_CURRENT_180_SHIFT
) *
3081 SDHCI_MAX_CURRENT_MULTIPLIER
;
3084 mmc
->ocr_avail
= ocr_avail
;
3085 mmc
->ocr_avail_sdio
= ocr_avail
;
3086 if (host
->ocr_avail_sdio
)
3087 mmc
->ocr_avail_sdio
&= host
->ocr_avail_sdio
;
3088 mmc
->ocr_avail_sd
= ocr_avail
;
3089 if (host
->ocr_avail_sd
)
3090 mmc
->ocr_avail_sd
&= host
->ocr_avail_sd
;
3091 else /* normal SD controllers don't support 1.8V */
3092 mmc
->ocr_avail_sd
&= ~MMC_VDD_165_195
;
3093 mmc
->ocr_avail_mmc
= ocr_avail
;
3094 if (host
->ocr_avail_mmc
)
3095 mmc
->ocr_avail_mmc
&= host
->ocr_avail_mmc
;
3097 if (mmc
->ocr_avail
== 0) {
3098 pr_err("%s: Hardware doesn't report any "
3099 "support voltages.\n", mmc_hostname(mmc
));
3103 spin_lock_init(&host
->lock
);
3106 * Maximum number of segments. Depends on if the hardware
3107 * can do scatter/gather or not.
3109 if (host
->flags
& SDHCI_USE_ADMA
)
3110 mmc
->max_segs
= 128;
3111 else if (host
->flags
& SDHCI_USE_SDMA
)
3114 mmc
->max_segs
= 128;
3117 * Maximum number of sectors in one transfer. Limited by DMA boundary
3120 mmc
->max_req_size
= 524288;
3123 * Maximum segment size. Could be one segment with the maximum number
3124 * of bytes. When doing hardware scatter/gather, each entry cannot
3125 * be larger than 64 KiB though.
3127 if (host
->flags
& SDHCI_USE_ADMA
) {
3128 if (host
->quirks
& SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC
)
3129 mmc
->max_seg_size
= 65535;
3131 mmc
->max_seg_size
= 65536;
3133 mmc
->max_seg_size
= mmc
->max_req_size
;
3137 * Maximum block size. This varies from controller to controller and
3138 * is specified in the capabilities register.
3140 if (host
->quirks
& SDHCI_QUIRK_FORCE_BLK_SZ_2048
) {
3141 mmc
->max_blk_size
= 2;
3143 mmc
->max_blk_size
= (caps
[0] & SDHCI_MAX_BLOCK_MASK
) >>
3144 SDHCI_MAX_BLOCK_SHIFT
;
3145 if (mmc
->max_blk_size
>= 3) {
3146 pr_warning("%s: Invalid maximum block size, "
3147 "assuming 512 bytes\n", mmc_hostname(mmc
));
3148 mmc
->max_blk_size
= 0;
3152 mmc
->max_blk_size
= 512 << mmc
->max_blk_size
;
3155 * Maximum block count.
3157 mmc
->max_blk_count
= (host
->quirks
& SDHCI_QUIRK_NO_MULTIBLOCK
) ? 1 : 65535;
3162 tasklet_init(&host
->card_tasklet
,
3163 sdhci_tasklet_card
, (unsigned long)host
);
3164 tasklet_init(&host
->finish_tasklet
,
3165 sdhci_tasklet_finish
, (unsigned long)host
);
3167 setup_timer(&host
->timer
, sdhci_timeout_timer
, (unsigned long)host
);
3169 if (host
->version
>= SDHCI_SPEC_300
) {
3170 init_waitqueue_head(&host
->buf_ready_int
);
3172 /* Initialize re-tuning timer */
3173 init_timer(&host
->tuning_timer
);
3174 host
->tuning_timer
.data
= (unsigned long)host
;
3175 host
->tuning_timer
.function
= sdhci_tuning_timer
;
3178 ret
= request_irq(host
->irq
, sdhci_irq
, IRQF_SHARED
,
3179 mmc_hostname(mmc
), host
);
3181 pr_err("%s: Failed to request IRQ %d: %d\n",
3182 mmc_hostname(mmc
), host
->irq
, ret
);
3186 sdhci_init(host
, 0);
3188 #ifdef CONFIG_MMC_DEBUG
3189 sdhci_dumpregs(host
);
3192 #ifdef SDHCI_USE_LEDS_CLASS
3193 snprintf(host
->led_name
, sizeof(host
->led_name
),
3194 "%s::", mmc_hostname(mmc
));
3195 host
->led
.name
= host
->led_name
;
3196 host
->led
.brightness
= LED_OFF
;
3197 host
->led
.default_trigger
= mmc_hostname(mmc
);
3198 host
->led
.brightness_set
= sdhci_led_control
;
3200 ret
= led_classdev_register(mmc_dev(mmc
), &host
->led
);
3202 pr_err("%s: Failed to register LED device: %d\n",
3203 mmc_hostname(mmc
), ret
);
3212 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
3213 mmc_hostname(mmc
), host
->hw_name
, dev_name(mmc_dev(mmc
)),
3214 (host
->flags
& SDHCI_USE_ADMA
) ? "ADMA" :
3215 (host
->flags
& SDHCI_USE_SDMA
) ? "DMA" : "PIO");
3217 sdhci_enable_card_detection(host
);
3221 #ifdef SDHCI_USE_LEDS_CLASS
3223 sdhci_reset(host
, SDHCI_RESET_ALL
);
3224 sdhci_mask_irqs(host
, SDHCI_INT_ALL_MASK
);
3225 free_irq(host
->irq
, host
);
3228 tasklet_kill(&host
->card_tasklet
);
3229 tasklet_kill(&host
->finish_tasklet
);
3234 EXPORT_SYMBOL_GPL(sdhci_add_host
);
3236 void sdhci_remove_host(struct sdhci_host
*host
, int dead
)
3238 unsigned long flags
;
3241 spin_lock_irqsave(&host
->lock
, flags
);
3243 host
->flags
|= SDHCI_DEVICE_DEAD
;
3246 pr_err("%s: Controller removed during "
3247 " transfer!\n", mmc_hostname(host
->mmc
));
3249 host
->mrq
->cmd
->error
= -ENOMEDIUM
;
3250 tasklet_schedule(&host
->finish_tasklet
);
3253 spin_unlock_irqrestore(&host
->lock
, flags
);
3256 sdhci_disable_card_detection(host
);
3258 mmc_remove_host(host
->mmc
);
3260 #ifdef SDHCI_USE_LEDS_CLASS
3261 led_classdev_unregister(&host
->led
);
3265 sdhci_reset(host
, SDHCI_RESET_ALL
);
3267 sdhci_mask_irqs(host
, SDHCI_INT_ALL_MASK
);
3268 free_irq(host
->irq
, host
);
3270 del_timer_sync(&host
->timer
);
3272 tasklet_kill(&host
->card_tasklet
);
3273 tasklet_kill(&host
->finish_tasklet
);
3276 regulator_disable(host
->vmmc
);
3277 regulator_put(host
->vmmc
);
3281 regulator_disable(host
->vqmmc
);
3282 regulator_put(host
->vqmmc
);
3285 kfree(host
->adma_desc
);
3286 kfree(host
->align_buffer
);
3288 host
->adma_desc
= NULL
;
3289 host
->align_buffer
= NULL
;
3292 EXPORT_SYMBOL_GPL(sdhci_remove_host
);
3294 void sdhci_free_host(struct sdhci_host
*host
)
3296 mmc_free_host(host
->mmc
);
3299 EXPORT_SYMBOL_GPL(sdhci_free_host
);
3301 /*****************************************************************************\
3303 * Driver init/exit *
3305 \*****************************************************************************/
3307 static int __init
sdhci_drv_init(void)
3310 ": Secure Digital Host Controller Interface driver\n");
3311 pr_info(DRIVER_NAME
": Copyright(c) Pierre Ossman\n");
3316 static void __exit
sdhci_drv_exit(void)
3320 module_init(sdhci_drv_init
);
3321 module_exit(sdhci_drv_exit
);
3323 module_param(debug_quirks
, uint
, 0444);
3324 module_param(debug_quirks2
, uint
, 0444);
3326 MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
3327 MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
3328 MODULE_LICENSE("GPL");
3330 MODULE_PARM_DESC(debug_quirks
, "Force certain quirks.");
3331 MODULE_PARM_DESC(debug_quirks2
, "Force certain other quirks.");