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>
34 #define DRIVER_NAME "sdhci"
36 #define DBG(f, x...) \
37 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
39 #if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
40 defined(CONFIG_MMC_SDHCI_MODULE))
41 #define SDHCI_USE_LEDS_CLASS
44 #define MAX_TUNING_LOOP 40
46 static unsigned int debug_quirks
= 0;
47 static unsigned int debug_quirks2
;
49 static void sdhci_finish_data(struct sdhci_host
*);
51 static void sdhci_send_command(struct sdhci_host
*, struct mmc_command
*);
52 static void sdhci_finish_command(struct sdhci_host
*);
53 static int sdhci_execute_tuning(struct mmc_host
*mmc
, u32 opcode
);
54 static void sdhci_tuning_timer(unsigned long data
);
56 #ifdef CONFIG_PM_RUNTIME
57 static int sdhci_runtime_pm_get(struct sdhci_host
*host
);
58 static int sdhci_runtime_pm_put(struct sdhci_host
*host
);
60 static inline int sdhci_runtime_pm_get(struct sdhci_host
*host
)
64 static inline int sdhci_runtime_pm_put(struct sdhci_host
*host
)
70 static void sdhci_dumpregs(struct sdhci_host
*host
)
72 pr_debug(DRIVER_NAME
": =========== REGISTER DUMP (%s)===========\n",
73 mmc_hostname(host
->mmc
));
75 pr_debug(DRIVER_NAME
": Sys addr: 0x%08x | Version: 0x%08x\n",
76 sdhci_readl(host
, SDHCI_DMA_ADDRESS
),
77 sdhci_readw(host
, SDHCI_HOST_VERSION
));
78 pr_debug(DRIVER_NAME
": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
79 sdhci_readw(host
, SDHCI_BLOCK_SIZE
),
80 sdhci_readw(host
, SDHCI_BLOCK_COUNT
));
81 pr_debug(DRIVER_NAME
": Argument: 0x%08x | Trn mode: 0x%08x\n",
82 sdhci_readl(host
, SDHCI_ARGUMENT
),
83 sdhci_readw(host
, SDHCI_TRANSFER_MODE
));
84 pr_debug(DRIVER_NAME
": Present: 0x%08x | Host ctl: 0x%08x\n",
85 sdhci_readl(host
, SDHCI_PRESENT_STATE
),
86 sdhci_readb(host
, SDHCI_HOST_CONTROL
));
87 pr_debug(DRIVER_NAME
": Power: 0x%08x | Blk gap: 0x%08x\n",
88 sdhci_readb(host
, SDHCI_POWER_CONTROL
),
89 sdhci_readb(host
, SDHCI_BLOCK_GAP_CONTROL
));
90 pr_debug(DRIVER_NAME
": Wake-up: 0x%08x | Clock: 0x%08x\n",
91 sdhci_readb(host
, SDHCI_WAKE_UP_CONTROL
),
92 sdhci_readw(host
, SDHCI_CLOCK_CONTROL
));
93 pr_debug(DRIVER_NAME
": Timeout: 0x%08x | Int stat: 0x%08x\n",
94 sdhci_readb(host
, SDHCI_TIMEOUT_CONTROL
),
95 sdhci_readl(host
, SDHCI_INT_STATUS
));
96 pr_debug(DRIVER_NAME
": Int enab: 0x%08x | Sig enab: 0x%08x\n",
97 sdhci_readl(host
, SDHCI_INT_ENABLE
),
98 sdhci_readl(host
, SDHCI_SIGNAL_ENABLE
));
99 pr_debug(DRIVER_NAME
": AC12 err: 0x%08x | Slot int: 0x%08x\n",
100 sdhci_readw(host
, SDHCI_ACMD12_ERR
),
101 sdhci_readw(host
, SDHCI_SLOT_INT_STATUS
));
102 pr_debug(DRIVER_NAME
": Caps: 0x%08x | Caps_1: 0x%08x\n",
103 sdhci_readl(host
, SDHCI_CAPABILITIES
),
104 sdhci_readl(host
, SDHCI_CAPABILITIES_1
));
105 pr_debug(DRIVER_NAME
": Cmd: 0x%08x | Max curr: 0x%08x\n",
106 sdhci_readw(host
, SDHCI_COMMAND
),
107 sdhci_readl(host
, SDHCI_MAX_CURRENT
));
108 pr_debug(DRIVER_NAME
": Host ctl2: 0x%08x\n",
109 sdhci_readw(host
, SDHCI_HOST_CONTROL2
));
111 if (host
->flags
& SDHCI_USE_ADMA
)
112 pr_debug(DRIVER_NAME
": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
113 readl(host
->ioaddr
+ SDHCI_ADMA_ERROR
),
114 readl(host
->ioaddr
+ SDHCI_ADMA_ADDRESS
));
116 pr_debug(DRIVER_NAME
": ===========================================\n");
119 /*****************************************************************************\
121 * Low level functions *
123 \*****************************************************************************/
125 static void sdhci_clear_set_irqs(struct sdhci_host
*host
, u32 clear
, u32 set
)
129 ier
= sdhci_readl(host
, SDHCI_INT_ENABLE
);
132 sdhci_writel(host
, ier
, SDHCI_INT_ENABLE
);
133 sdhci_writel(host
, ier
, SDHCI_SIGNAL_ENABLE
);
136 static void sdhci_unmask_irqs(struct sdhci_host
*host
, u32 irqs
)
138 sdhci_clear_set_irqs(host
, 0, irqs
);
141 static void sdhci_mask_irqs(struct sdhci_host
*host
, u32 irqs
)
143 sdhci_clear_set_irqs(host
, irqs
, 0);
146 static void sdhci_set_card_detection(struct sdhci_host
*host
, bool enable
)
150 if ((host
->quirks
& SDHCI_QUIRK_BROKEN_CARD_DETECTION
) ||
151 (host
->mmc
->caps
& MMC_CAP_NONREMOVABLE
))
154 present
= sdhci_readl(host
, SDHCI_PRESENT_STATE
) &
156 irqs
= present
? SDHCI_INT_CARD_REMOVE
: SDHCI_INT_CARD_INSERT
;
159 sdhci_unmask_irqs(host
, irqs
);
161 sdhci_mask_irqs(host
, irqs
);
164 static void sdhci_enable_card_detection(struct sdhci_host
*host
)
166 sdhci_set_card_detection(host
, true);
169 static void sdhci_disable_card_detection(struct sdhci_host
*host
)
171 sdhci_set_card_detection(host
, false);
174 static void sdhci_reset(struct sdhci_host
*host
, u8 mask
)
176 unsigned long timeout
;
177 u32
uninitialized_var(ier
);
179 if (host
->quirks
& SDHCI_QUIRK_NO_CARD_NO_RESET
) {
180 if (!(sdhci_readl(host
, SDHCI_PRESENT_STATE
) &
185 if (host
->quirks
& SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET
)
186 ier
= sdhci_readl(host
, SDHCI_INT_ENABLE
);
188 if (host
->ops
->platform_reset_enter
)
189 host
->ops
->platform_reset_enter(host
, mask
);
191 sdhci_writeb(host
, mask
, SDHCI_SOFTWARE_RESET
);
193 if (mask
& SDHCI_RESET_ALL
)
196 /* Wait max 100 ms */
199 /* hw clears the bit when it's done */
200 while (sdhci_readb(host
, SDHCI_SOFTWARE_RESET
) & mask
) {
202 pr_err("%s: Reset 0x%x never completed.\n",
203 mmc_hostname(host
->mmc
), (int)mask
);
204 sdhci_dumpregs(host
);
211 if (host
->ops
->platform_reset_exit
)
212 host
->ops
->platform_reset_exit(host
, mask
);
214 if (host
->quirks
& SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET
)
215 sdhci_clear_set_irqs(host
, SDHCI_INT_ALL_MASK
, ier
);
217 if (host
->flags
& (SDHCI_USE_SDMA
| SDHCI_USE_ADMA
)) {
218 if ((host
->ops
->enable_dma
) && (mask
& SDHCI_RESET_ALL
))
219 host
->ops
->enable_dma(host
);
223 static void sdhci_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
);
225 static void sdhci_init(struct sdhci_host
*host
, int soft
)
228 sdhci_reset(host
, SDHCI_RESET_CMD
|SDHCI_RESET_DATA
);
230 sdhci_reset(host
, SDHCI_RESET_ALL
);
232 sdhci_clear_set_irqs(host
, SDHCI_INT_ALL_MASK
,
233 SDHCI_INT_BUS_POWER
| SDHCI_INT_DATA_END_BIT
|
234 SDHCI_INT_DATA_CRC
| SDHCI_INT_DATA_TIMEOUT
| SDHCI_INT_INDEX
|
235 SDHCI_INT_END_BIT
| SDHCI_INT_CRC
| SDHCI_INT_TIMEOUT
|
236 SDHCI_INT_DATA_END
| SDHCI_INT_RESPONSE
);
239 /* force clock reconfiguration */
241 sdhci_set_ios(host
->mmc
, &host
->mmc
->ios
);
245 static void sdhci_reinit(struct sdhci_host
*host
)
248 sdhci_enable_card_detection(host
);
251 static void sdhci_activate_led(struct sdhci_host
*host
)
255 ctrl
= sdhci_readb(host
, SDHCI_HOST_CONTROL
);
256 ctrl
|= SDHCI_CTRL_LED
;
257 sdhci_writeb(host
, ctrl
, SDHCI_HOST_CONTROL
);
260 static void sdhci_deactivate_led(struct sdhci_host
*host
)
264 ctrl
= sdhci_readb(host
, SDHCI_HOST_CONTROL
);
265 ctrl
&= ~SDHCI_CTRL_LED
;
266 sdhci_writeb(host
, ctrl
, SDHCI_HOST_CONTROL
);
269 #ifdef SDHCI_USE_LEDS_CLASS
270 static void sdhci_led_control(struct led_classdev
*led
,
271 enum led_brightness brightness
)
273 struct sdhci_host
*host
= container_of(led
, struct sdhci_host
, led
);
276 spin_lock_irqsave(&host
->lock
, flags
);
278 if (host
->runtime_suspended
)
281 if (brightness
== LED_OFF
)
282 sdhci_deactivate_led(host
);
284 sdhci_activate_led(host
);
286 spin_unlock_irqrestore(&host
->lock
, flags
);
290 /*****************************************************************************\
294 \*****************************************************************************/
296 static void sdhci_read_block_pio(struct sdhci_host
*host
)
299 size_t blksize
, len
, chunk
;
300 u32
uninitialized_var(scratch
);
303 DBG("PIO reading\n");
305 blksize
= host
->data
->blksz
;
308 local_irq_save(flags
);
311 if (!sg_miter_next(&host
->sg_miter
))
314 len
= min(host
->sg_miter
.length
, blksize
);
317 host
->sg_miter
.consumed
= len
;
319 buf
= host
->sg_miter
.addr
;
323 scratch
= sdhci_readl(host
, SDHCI_BUFFER
);
327 *buf
= scratch
& 0xFF;
336 sg_miter_stop(&host
->sg_miter
);
338 local_irq_restore(flags
);
341 static void sdhci_write_block_pio(struct sdhci_host
*host
)
344 size_t blksize
, len
, chunk
;
348 DBG("PIO writing\n");
350 blksize
= host
->data
->blksz
;
354 local_irq_save(flags
);
357 if (!sg_miter_next(&host
->sg_miter
))
360 len
= min(host
->sg_miter
.length
, blksize
);
363 host
->sg_miter
.consumed
= len
;
365 buf
= host
->sg_miter
.addr
;
368 scratch
|= (u32
)*buf
<< (chunk
* 8);
374 if ((chunk
== 4) || ((len
== 0) && (blksize
== 0))) {
375 sdhci_writel(host
, scratch
, SDHCI_BUFFER
);
382 sg_miter_stop(&host
->sg_miter
);
384 local_irq_restore(flags
);
387 static void sdhci_transfer_pio(struct sdhci_host
*host
)
393 if (host
->blocks
== 0)
396 if (host
->data
->flags
& MMC_DATA_READ
)
397 mask
= SDHCI_DATA_AVAILABLE
;
399 mask
= SDHCI_SPACE_AVAILABLE
;
402 * Some controllers (JMicron JMB38x) mess up the buffer bits
403 * for transfers < 4 bytes. As long as it is just one block,
404 * we can ignore the bits.
406 if ((host
->quirks
& SDHCI_QUIRK_BROKEN_SMALL_PIO
) &&
407 (host
->data
->blocks
== 1))
410 while (sdhci_readl(host
, SDHCI_PRESENT_STATE
) & mask
) {
411 if (host
->quirks
& SDHCI_QUIRK_PIO_NEEDS_DELAY
)
414 if (host
->data
->flags
& MMC_DATA_READ
)
415 sdhci_read_block_pio(host
);
417 sdhci_write_block_pio(host
);
420 if (host
->blocks
== 0)
424 DBG("PIO transfer complete.\n");
427 static char *sdhci_kmap_atomic(struct scatterlist
*sg
, unsigned long *flags
)
429 local_irq_save(*flags
);
430 return kmap_atomic(sg_page(sg
)) + sg
->offset
;
433 static void sdhci_kunmap_atomic(void *buffer
, unsigned long *flags
)
435 kunmap_atomic(buffer
);
436 local_irq_restore(*flags
);
439 static void sdhci_set_adma_desc(u8
*desc
, u32 addr
, int len
, unsigned cmd
)
441 __le32
*dataddr
= (__le32 __force
*)(desc
+ 4);
442 __le16
*cmdlen
= (__le16 __force
*)desc
;
444 /* SDHCI specification says ADMA descriptors should be 4 byte
445 * aligned, so using 16 or 32bit operations should be safe. */
447 cmdlen
[0] = cpu_to_le16(cmd
);
448 cmdlen
[1] = cpu_to_le16(len
);
450 dataddr
[0] = cpu_to_le32(addr
);
453 static int sdhci_adma_table_pre(struct sdhci_host
*host
,
454 struct mmc_data
*data
)
461 dma_addr_t align_addr
;
464 struct scatterlist
*sg
;
470 * The spec does not specify endianness of descriptor table.
471 * We currently guess that it is LE.
474 if (data
->flags
& MMC_DATA_READ
)
475 direction
= DMA_FROM_DEVICE
;
477 direction
= DMA_TO_DEVICE
;
480 * The ADMA descriptor table is mapped further down as we
481 * need to fill it with data first.
484 host
->align_addr
= dma_map_single(mmc_dev(host
->mmc
),
485 host
->align_buffer
, 128 * 4, direction
);
486 if (dma_mapping_error(mmc_dev(host
->mmc
), host
->align_addr
))
488 BUG_ON(host
->align_addr
& 0x3);
490 host
->sg_count
= dma_map_sg(mmc_dev(host
->mmc
),
491 data
->sg
, data
->sg_len
, direction
);
492 if (host
->sg_count
== 0)
495 desc
= host
->adma_desc
;
496 align
= host
->align_buffer
;
498 align_addr
= host
->align_addr
;
500 for_each_sg(data
->sg
, sg
, host
->sg_count
, i
) {
501 addr
= sg_dma_address(sg
);
502 len
= sg_dma_len(sg
);
505 * The SDHCI specification states that ADMA
506 * addresses must be 32-bit aligned. If they
507 * aren't, then we use a bounce buffer for
508 * the (up to three) bytes that screw up the
511 offset
= (4 - (addr
& 0x3)) & 0x3;
513 if (data
->flags
& MMC_DATA_WRITE
) {
514 buffer
= sdhci_kmap_atomic(sg
, &flags
);
515 WARN_ON(((long)buffer
& PAGE_MASK
) > (PAGE_SIZE
- 3));
516 memcpy(align
, buffer
, offset
);
517 sdhci_kunmap_atomic(buffer
, &flags
);
521 sdhci_set_adma_desc(desc
, align_addr
, offset
, 0x21);
523 BUG_ON(offset
> 65536);
537 sdhci_set_adma_desc(desc
, addr
, len
, 0x21);
541 * If this triggers then we have a calculation bug
544 WARN_ON((desc
- host
->adma_desc
) > (128 * 2 + 1) * 4);
547 if (host
->quirks
& SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
) {
549 * Mark the last descriptor as the terminating descriptor
551 if (desc
!= host
->adma_desc
) {
553 desc
[0] |= 0x2; /* end */
557 * Add a terminating entry.
560 /* nop, end, valid */
561 sdhci_set_adma_desc(desc
, 0, 0, 0x3);
565 * Resync align buffer as we might have changed it.
567 if (data
->flags
& MMC_DATA_WRITE
) {
568 dma_sync_single_for_device(mmc_dev(host
->mmc
),
569 host
->align_addr
, 128 * 4, direction
);
572 host
->adma_addr
= dma_map_single(mmc_dev(host
->mmc
),
573 host
->adma_desc
, (128 * 2 + 1) * 4, DMA_TO_DEVICE
);
574 if (dma_mapping_error(mmc_dev(host
->mmc
), host
->adma_addr
))
576 BUG_ON(host
->adma_addr
& 0x3);
581 dma_unmap_sg(mmc_dev(host
->mmc
), data
->sg
,
582 data
->sg_len
, direction
);
584 dma_unmap_single(mmc_dev(host
->mmc
), host
->align_addr
,
590 static void sdhci_adma_table_post(struct sdhci_host
*host
,
591 struct mmc_data
*data
)
595 struct scatterlist
*sg
;
601 if (data
->flags
& MMC_DATA_READ
)
602 direction
= DMA_FROM_DEVICE
;
604 direction
= DMA_TO_DEVICE
;
606 dma_unmap_single(mmc_dev(host
->mmc
), host
->adma_addr
,
607 (128 * 2 + 1) * 4, DMA_TO_DEVICE
);
609 dma_unmap_single(mmc_dev(host
->mmc
), host
->align_addr
,
612 if (data
->flags
& MMC_DATA_READ
) {
613 dma_sync_sg_for_cpu(mmc_dev(host
->mmc
), data
->sg
,
614 data
->sg_len
, direction
);
616 align
= host
->align_buffer
;
618 for_each_sg(data
->sg
, sg
, host
->sg_count
, i
) {
619 if (sg_dma_address(sg
) & 0x3) {
620 size
= 4 - (sg_dma_address(sg
) & 0x3);
622 buffer
= sdhci_kmap_atomic(sg
, &flags
);
623 WARN_ON(((long)buffer
& PAGE_MASK
) > (PAGE_SIZE
- 3));
624 memcpy(buffer
, align
, size
);
625 sdhci_kunmap_atomic(buffer
, &flags
);
632 dma_unmap_sg(mmc_dev(host
->mmc
), data
->sg
,
633 data
->sg_len
, direction
);
636 static u8
sdhci_calc_timeout(struct sdhci_host
*host
, struct mmc_command
*cmd
)
639 struct mmc_data
*data
= cmd
->data
;
640 unsigned target_timeout
, current_timeout
;
643 * If the host controller provides us with an incorrect timeout
644 * value, just skip the check and use 0xE. The hardware may take
645 * longer to time out, but that's much better than having a too-short
648 if (host
->quirks
& SDHCI_QUIRK_BROKEN_TIMEOUT_VAL
)
651 /* Unspecified timeout, assume max */
652 if (!data
&& !cmd
->cmd_timeout_ms
)
657 target_timeout
= cmd
->cmd_timeout_ms
* 1000;
659 target_timeout
= data
->timeout_ns
/ 1000;
661 target_timeout
+= data
->timeout_clks
/ host
->clock
;
665 * Figure out needed cycles.
666 * We do this in steps in order to fit inside a 32 bit int.
667 * The first step is the minimum timeout, which will have a
668 * minimum resolution of 6 bits:
669 * (1) 2^13*1000 > 2^22,
670 * (2) host->timeout_clk < 2^16
675 current_timeout
= (1 << 13) * 1000 / host
->timeout_clk
;
676 while (current_timeout
< target_timeout
) {
678 current_timeout
<<= 1;
684 pr_warning("%s: Too large timeout requested for CMD%d!\n",
685 mmc_hostname(host
->mmc
), cmd
->opcode
);
692 static void sdhci_set_transfer_irqs(struct sdhci_host
*host
)
694 u32 pio_irqs
= SDHCI_INT_DATA_AVAIL
| SDHCI_INT_SPACE_AVAIL
;
695 u32 dma_irqs
= SDHCI_INT_DMA_END
| SDHCI_INT_ADMA_ERROR
;
697 if (host
->flags
& SDHCI_REQ_USE_DMA
)
698 sdhci_clear_set_irqs(host
, pio_irqs
, dma_irqs
);
700 sdhci_clear_set_irqs(host
, dma_irqs
, pio_irqs
);
703 static void sdhci_prepare_data(struct sdhci_host
*host
, struct mmc_command
*cmd
)
707 struct mmc_data
*data
= cmd
->data
;
712 if (data
|| (cmd
->flags
& MMC_RSP_BUSY
)) {
713 count
= sdhci_calc_timeout(host
, cmd
);
714 sdhci_writeb(host
, count
, SDHCI_TIMEOUT_CONTROL
);
721 BUG_ON(data
->blksz
* data
->blocks
> 524288);
722 BUG_ON(data
->blksz
> host
->mmc
->max_blk_size
);
723 BUG_ON(data
->blocks
> 65535);
726 host
->data_early
= 0;
727 host
->data
->bytes_xfered
= 0;
729 if (host
->flags
& (SDHCI_USE_SDMA
| SDHCI_USE_ADMA
))
730 host
->flags
|= SDHCI_REQ_USE_DMA
;
733 * FIXME: This doesn't account for merging when mapping the
736 if (host
->flags
& SDHCI_REQ_USE_DMA
) {
738 struct scatterlist
*sg
;
741 if (host
->flags
& SDHCI_USE_ADMA
) {
742 if (host
->quirks
& SDHCI_QUIRK_32BIT_ADMA_SIZE
)
745 if (host
->quirks
& SDHCI_QUIRK_32BIT_DMA_SIZE
)
749 if (unlikely(broken
)) {
750 for_each_sg(data
->sg
, sg
, data
->sg_len
, i
) {
751 if (sg
->length
& 0x3) {
752 DBG("Reverting to PIO because of "
753 "transfer size (%d)\n",
755 host
->flags
&= ~SDHCI_REQ_USE_DMA
;
763 * The assumption here being that alignment is the same after
764 * translation to device address space.
766 if (host
->flags
& SDHCI_REQ_USE_DMA
) {
768 struct scatterlist
*sg
;
771 if (host
->flags
& SDHCI_USE_ADMA
) {
773 * As we use 3 byte chunks to work around
774 * alignment problems, we need to check this
777 if (host
->quirks
& SDHCI_QUIRK_32BIT_ADMA_SIZE
)
780 if (host
->quirks
& SDHCI_QUIRK_32BIT_DMA_ADDR
)
784 if (unlikely(broken
)) {
785 for_each_sg(data
->sg
, sg
, data
->sg_len
, i
) {
786 if (sg
->offset
& 0x3) {
787 DBG("Reverting to PIO because of "
789 host
->flags
&= ~SDHCI_REQ_USE_DMA
;
796 if (host
->flags
& SDHCI_REQ_USE_DMA
) {
797 if (host
->flags
& SDHCI_USE_ADMA
) {
798 ret
= sdhci_adma_table_pre(host
, data
);
801 * This only happens when someone fed
802 * us an invalid request.
805 host
->flags
&= ~SDHCI_REQ_USE_DMA
;
807 sdhci_writel(host
, host
->adma_addr
,
813 sg_cnt
= dma_map_sg(mmc_dev(host
->mmc
),
814 data
->sg
, data
->sg_len
,
815 (data
->flags
& MMC_DATA_READ
) ?
820 * This only happens when someone fed
821 * us an invalid request.
824 host
->flags
&= ~SDHCI_REQ_USE_DMA
;
826 WARN_ON(sg_cnt
!= 1);
827 sdhci_writel(host
, sg_dma_address(data
->sg
),
834 * Always adjust the DMA selection as some controllers
835 * (e.g. JMicron) can't do PIO properly when the selection
838 if (host
->version
>= SDHCI_SPEC_200
) {
839 ctrl
= sdhci_readb(host
, SDHCI_HOST_CONTROL
);
840 ctrl
&= ~SDHCI_CTRL_DMA_MASK
;
841 if ((host
->flags
& SDHCI_REQ_USE_DMA
) &&
842 (host
->flags
& SDHCI_USE_ADMA
))
843 ctrl
|= SDHCI_CTRL_ADMA32
;
845 ctrl
|= SDHCI_CTRL_SDMA
;
846 sdhci_writeb(host
, ctrl
, SDHCI_HOST_CONTROL
);
849 if (!(host
->flags
& SDHCI_REQ_USE_DMA
)) {
852 flags
= SG_MITER_ATOMIC
;
853 if (host
->data
->flags
& MMC_DATA_READ
)
854 flags
|= SG_MITER_TO_SG
;
856 flags
|= SG_MITER_FROM_SG
;
857 sg_miter_start(&host
->sg_miter
, data
->sg
, data
->sg_len
, flags
);
858 host
->blocks
= data
->blocks
;
861 sdhci_set_transfer_irqs(host
);
863 /* Set the DMA boundary value and block size */
864 sdhci_writew(host
, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG
,
865 data
->blksz
), SDHCI_BLOCK_SIZE
);
866 sdhci_writew(host
, data
->blocks
, SDHCI_BLOCK_COUNT
);
869 static void sdhci_set_transfer_mode(struct sdhci_host
*host
,
870 struct mmc_command
*cmd
)
873 struct mmc_data
*data
= cmd
->data
;
878 WARN_ON(!host
->data
);
880 mode
= SDHCI_TRNS_BLK_CNT_EN
;
881 if (mmc_op_multi(cmd
->opcode
) || data
->blocks
> 1) {
882 mode
|= SDHCI_TRNS_MULTI
;
884 * If we are sending CMD23, CMD12 never gets sent
885 * on successful completion (so no Auto-CMD12).
887 if (!host
->mrq
->sbc
&& (host
->flags
& SDHCI_AUTO_CMD12
))
888 mode
|= SDHCI_TRNS_AUTO_CMD12
;
889 else if (host
->mrq
->sbc
&& (host
->flags
& SDHCI_AUTO_CMD23
)) {
890 mode
|= SDHCI_TRNS_AUTO_CMD23
;
891 sdhci_writel(host
, host
->mrq
->sbc
->arg
, SDHCI_ARGUMENT2
);
895 if (data
->flags
& MMC_DATA_READ
)
896 mode
|= SDHCI_TRNS_READ
;
897 if (host
->flags
& SDHCI_REQ_USE_DMA
)
898 mode
|= SDHCI_TRNS_DMA
;
900 sdhci_writew(host
, mode
, SDHCI_TRANSFER_MODE
);
903 static void sdhci_finish_data(struct sdhci_host
*host
)
905 struct mmc_data
*data
;
912 if (host
->flags
& SDHCI_REQ_USE_DMA
) {
913 if (host
->flags
& SDHCI_USE_ADMA
)
914 sdhci_adma_table_post(host
, data
);
916 dma_unmap_sg(mmc_dev(host
->mmc
), data
->sg
,
917 data
->sg_len
, (data
->flags
& MMC_DATA_READ
) ?
918 DMA_FROM_DEVICE
: DMA_TO_DEVICE
);
923 * The specification states that the block count register must
924 * be updated, but it does not specify at what point in the
925 * data flow. That makes the register entirely useless to read
926 * back so we have to assume that nothing made it to the card
927 * in the event of an error.
930 data
->bytes_xfered
= 0;
932 data
->bytes_xfered
= data
->blksz
* data
->blocks
;
935 * Need to send CMD12 if -
936 * a) open-ended multiblock transfer (no CMD23)
937 * b) error in multiblock transfer
944 * The controller needs a reset of internal state machines
945 * upon error conditions.
948 sdhci_reset(host
, SDHCI_RESET_CMD
);
949 sdhci_reset(host
, SDHCI_RESET_DATA
);
952 sdhci_send_command(host
, data
->stop
);
954 tasklet_schedule(&host
->finish_tasklet
);
957 static void sdhci_send_command(struct sdhci_host
*host
, struct mmc_command
*cmd
)
961 unsigned long timeout
;
968 mask
= SDHCI_CMD_INHIBIT
;
969 if ((cmd
->data
!= NULL
) || (cmd
->flags
& MMC_RSP_BUSY
))
970 mask
|= SDHCI_DATA_INHIBIT
;
972 /* We shouldn't wait for data inihibit for stop commands, even
973 though they might use busy signaling */
974 if (host
->mrq
->data
&& (cmd
== host
->mrq
->data
->stop
))
975 mask
&= ~SDHCI_DATA_INHIBIT
;
977 while (sdhci_readl(host
, SDHCI_PRESENT_STATE
) & mask
) {
979 pr_err("%s: Controller never released "
980 "inhibit bit(s).\n", mmc_hostname(host
->mmc
));
981 sdhci_dumpregs(host
);
983 tasklet_schedule(&host
->finish_tasklet
);
990 mod_timer(&host
->timer
, jiffies
+ 10 * HZ
);
994 sdhci_prepare_data(host
, cmd
);
996 sdhci_writel(host
, cmd
->arg
, SDHCI_ARGUMENT
);
998 sdhci_set_transfer_mode(host
, cmd
);
1000 if ((cmd
->flags
& MMC_RSP_136
) && (cmd
->flags
& MMC_RSP_BUSY
)) {
1001 pr_err("%s: Unsupported response type!\n",
1002 mmc_hostname(host
->mmc
));
1003 cmd
->error
= -EINVAL
;
1004 tasklet_schedule(&host
->finish_tasklet
);
1008 if (!(cmd
->flags
& MMC_RSP_PRESENT
))
1009 flags
= SDHCI_CMD_RESP_NONE
;
1010 else if (cmd
->flags
& MMC_RSP_136
)
1011 flags
= SDHCI_CMD_RESP_LONG
;
1012 else if (cmd
->flags
& MMC_RSP_BUSY
)
1013 flags
= SDHCI_CMD_RESP_SHORT_BUSY
;
1015 flags
= SDHCI_CMD_RESP_SHORT
;
1017 if (cmd
->flags
& MMC_RSP_CRC
)
1018 flags
|= SDHCI_CMD_CRC
;
1019 if (cmd
->flags
& MMC_RSP_OPCODE
)
1020 flags
|= SDHCI_CMD_INDEX
;
1022 /* CMD19 is special in that the Data Present Select should be set */
1023 if (cmd
->data
|| cmd
->opcode
== MMC_SEND_TUNING_BLOCK
||
1024 cmd
->opcode
== MMC_SEND_TUNING_BLOCK_HS200
)
1025 flags
|= SDHCI_CMD_DATA
;
1027 sdhci_writew(host
, SDHCI_MAKE_CMD(cmd
->opcode
, flags
), SDHCI_COMMAND
);
1030 static void sdhci_finish_command(struct sdhci_host
*host
)
1034 BUG_ON(host
->cmd
== NULL
);
1036 if (host
->cmd
->flags
& MMC_RSP_PRESENT
) {
1037 if (host
->cmd
->flags
& MMC_RSP_136
) {
1038 /* CRC is stripped so we need to do some shifting. */
1039 for (i
= 0;i
< 4;i
++) {
1040 host
->cmd
->resp
[i
] = sdhci_readl(host
,
1041 SDHCI_RESPONSE
+ (3-i
)*4) << 8;
1043 host
->cmd
->resp
[i
] |=
1045 SDHCI_RESPONSE
+ (3-i
)*4-1);
1048 host
->cmd
->resp
[0] = sdhci_readl(host
, SDHCI_RESPONSE
);
1052 host
->cmd
->error
= 0;
1054 /* Finished CMD23, now send actual command. */
1055 if (host
->cmd
== host
->mrq
->sbc
) {
1057 sdhci_send_command(host
, host
->mrq
->cmd
);
1060 /* Processed actual command. */
1061 if (host
->data
&& host
->data_early
)
1062 sdhci_finish_data(host
);
1064 if (!host
->cmd
->data
)
1065 tasklet_schedule(&host
->finish_tasklet
);
1071 static void sdhci_set_clock(struct sdhci_host
*host
, unsigned int clock
)
1073 int div
= 0; /* Initialized for compiler warning */
1074 int real_div
= div
, clk_mul
= 1;
1076 unsigned long timeout
;
1078 if (clock
&& clock
== host
->clock
)
1081 host
->mmc
->actual_clock
= 0;
1083 if (host
->ops
->set_clock
) {
1084 host
->ops
->set_clock(host
, clock
);
1085 if (host
->quirks
& SDHCI_QUIRK_NONSTANDARD_CLOCK
)
1089 sdhci_writew(host
, 0, SDHCI_CLOCK_CONTROL
);
1094 if (host
->version
>= SDHCI_SPEC_300
) {
1096 * Check if the Host Controller supports Programmable Clock
1099 if (host
->clk_mul
) {
1103 * We need to figure out whether the Host Driver needs
1104 * to select Programmable Clock Mode, or the value can
1105 * be set automatically by the Host Controller based on
1106 * the Preset Value registers.
1108 ctrl
= sdhci_readw(host
, SDHCI_HOST_CONTROL2
);
1109 if (!(ctrl
& SDHCI_CTRL_PRESET_VAL_ENABLE
)) {
1110 for (div
= 1; div
<= 1024; div
++) {
1111 if (((host
->max_clk
* host
->clk_mul
) /
1116 * Set Programmable Clock Mode in the Clock
1119 clk
= SDHCI_PROG_CLOCK_MODE
;
1121 clk_mul
= host
->clk_mul
;
1125 /* Version 3.00 divisors must be a multiple of 2. */
1126 if (host
->max_clk
<= clock
)
1129 for (div
= 2; div
< SDHCI_MAX_DIV_SPEC_300
;
1131 if ((host
->max_clk
/ div
) <= clock
)
1139 /* Version 2.00 divisors must be a power of 2. */
1140 for (div
= 1; div
< SDHCI_MAX_DIV_SPEC_200
; div
*= 2) {
1141 if ((host
->max_clk
/ div
) <= clock
)
1149 host
->mmc
->actual_clock
= (host
->max_clk
* clk_mul
) / real_div
;
1151 clk
|= (div
& SDHCI_DIV_MASK
) << SDHCI_DIVIDER_SHIFT
;
1152 clk
|= ((div
& SDHCI_DIV_HI_MASK
) >> SDHCI_DIV_MASK_LEN
)
1153 << SDHCI_DIVIDER_HI_SHIFT
;
1154 clk
|= SDHCI_CLOCK_INT_EN
;
1155 sdhci_writew(host
, clk
, SDHCI_CLOCK_CONTROL
);
1157 /* Wait max 20 ms */
1159 while (!((clk
= sdhci_readw(host
, SDHCI_CLOCK_CONTROL
))
1160 & SDHCI_CLOCK_INT_STABLE
)) {
1162 pr_err("%s: Internal clock never "
1163 "stabilised.\n", mmc_hostname(host
->mmc
));
1164 sdhci_dumpregs(host
);
1171 clk
|= SDHCI_CLOCK_CARD_EN
;
1172 sdhci_writew(host
, clk
, SDHCI_CLOCK_CONTROL
);
1175 host
->clock
= clock
;
1178 static int sdhci_set_power(struct sdhci_host
*host
, unsigned short power
)
1182 if (power
!= (unsigned short)-1) {
1183 switch (1 << power
) {
1184 case MMC_VDD_165_195
:
1185 pwr
= SDHCI_POWER_180
;
1189 pwr
= SDHCI_POWER_300
;
1193 pwr
= SDHCI_POWER_330
;
1200 if (host
->pwr
== pwr
)
1206 sdhci_writeb(host
, 0, SDHCI_POWER_CONTROL
);
1211 * Spec says that we should clear the power reg before setting
1212 * a new value. Some controllers don't seem to like this though.
1214 if (!(host
->quirks
& SDHCI_QUIRK_SINGLE_POWER_WRITE
))
1215 sdhci_writeb(host
, 0, SDHCI_POWER_CONTROL
);
1218 * At least the Marvell CaFe chip gets confused if we set the voltage
1219 * and set turn on power at the same time, so set the voltage first.
1221 if (host
->quirks
& SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER
)
1222 sdhci_writeb(host
, pwr
, SDHCI_POWER_CONTROL
);
1224 pwr
|= SDHCI_POWER_ON
;
1226 sdhci_writeb(host
, pwr
, SDHCI_POWER_CONTROL
);
1229 * Some controllers need an extra 10ms delay of 10ms before they
1230 * can apply clock after applying power
1232 if (host
->quirks
& SDHCI_QUIRK_DELAY_AFTER_POWER
)
1238 /*****************************************************************************\
1242 \*****************************************************************************/
1244 static void sdhci_request(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
1246 struct sdhci_host
*host
;
1248 unsigned long flags
;
1251 host
= mmc_priv(mmc
);
1253 sdhci_runtime_pm_get(host
);
1255 spin_lock_irqsave(&host
->lock
, flags
);
1257 WARN_ON(host
->mrq
!= NULL
);
1259 #ifndef SDHCI_USE_LEDS_CLASS
1260 sdhci_activate_led(host
);
1264 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1265 * requests if Auto-CMD12 is enabled.
1267 if (!mrq
->sbc
&& (host
->flags
& SDHCI_AUTO_CMD12
)) {
1269 mrq
->data
->stop
= NULL
;
1276 /* If polling, assume that the card is always present. */
1277 if (host
->quirks
& SDHCI_QUIRK_BROKEN_CARD_DETECTION
)
1280 present
= sdhci_readl(host
, SDHCI_PRESENT_STATE
) &
1283 if (!present
|| host
->flags
& SDHCI_DEVICE_DEAD
) {
1284 host
->mrq
->cmd
->error
= -ENOMEDIUM
;
1285 tasklet_schedule(&host
->finish_tasklet
);
1289 present_state
= sdhci_readl(host
, SDHCI_PRESENT_STATE
);
1291 * Check if the re-tuning timer has already expired and there
1292 * is no on-going data transfer. If so, we need to execute
1293 * tuning procedure before sending command.
1295 if ((host
->flags
& SDHCI_NEEDS_RETUNING
) &&
1296 !(present_state
& (SDHCI_DOING_WRITE
| SDHCI_DOING_READ
))) {
1298 /* eMMC uses cmd21 but sd and sdio use cmd19 */
1300 mmc
->card
->type
== MMC_TYPE_MMC
?
1301 MMC_SEND_TUNING_BLOCK_HS200
:
1302 MMC_SEND_TUNING_BLOCK
;
1303 spin_unlock_irqrestore(&host
->lock
, flags
);
1304 sdhci_execute_tuning(mmc
, tuning_opcode
);
1305 spin_lock_irqsave(&host
->lock
, flags
);
1307 /* Restore original mmc_request structure */
1312 if (mrq
->sbc
&& !(host
->flags
& SDHCI_AUTO_CMD23
))
1313 sdhci_send_command(host
, mrq
->sbc
);
1315 sdhci_send_command(host
, mrq
->cmd
);
1319 spin_unlock_irqrestore(&host
->lock
, flags
);
1322 static void sdhci_do_set_ios(struct sdhci_host
*host
, struct mmc_ios
*ios
)
1324 unsigned long flags
;
1328 spin_lock_irqsave(&host
->lock
, flags
);
1330 if (host
->flags
& SDHCI_DEVICE_DEAD
) {
1331 spin_unlock_irqrestore(&host
->lock
, flags
);
1332 if (host
->vmmc
&& ios
->power_mode
== MMC_POWER_OFF
)
1333 mmc_regulator_set_ocr(host
->mmc
, host
->vmmc
, 0);
1338 * Reset the chip on each power off.
1339 * Should clear out any weird states.
1341 if (ios
->power_mode
== MMC_POWER_OFF
) {
1342 sdhci_writel(host
, 0, SDHCI_SIGNAL_ENABLE
);
1346 sdhci_set_clock(host
, ios
->clock
);
1348 if (ios
->power_mode
== MMC_POWER_OFF
)
1349 vdd_bit
= sdhci_set_power(host
, -1);
1351 vdd_bit
= sdhci_set_power(host
, ios
->vdd
);
1353 if (host
->vmmc
&& vdd_bit
!= -1) {
1354 spin_unlock_irqrestore(&host
->lock
, flags
);
1355 mmc_regulator_set_ocr(host
->mmc
, host
->vmmc
, vdd_bit
);
1356 spin_lock_irqsave(&host
->lock
, flags
);
1359 if (host
->ops
->platform_send_init_74_clocks
)
1360 host
->ops
->platform_send_init_74_clocks(host
, ios
->power_mode
);
1363 * If your platform has 8-bit width support but is not a v3 controller,
1364 * or if it requires special setup code, you should implement that in
1365 * platform_8bit_width().
1367 if (host
->ops
->platform_8bit_width
)
1368 host
->ops
->platform_8bit_width(host
, ios
->bus_width
);
1370 ctrl
= sdhci_readb(host
, SDHCI_HOST_CONTROL
);
1371 if (ios
->bus_width
== MMC_BUS_WIDTH_8
) {
1372 ctrl
&= ~SDHCI_CTRL_4BITBUS
;
1373 if (host
->version
>= SDHCI_SPEC_300
)
1374 ctrl
|= SDHCI_CTRL_8BITBUS
;
1376 if (host
->version
>= SDHCI_SPEC_300
)
1377 ctrl
&= ~SDHCI_CTRL_8BITBUS
;
1378 if (ios
->bus_width
== MMC_BUS_WIDTH_4
)
1379 ctrl
|= SDHCI_CTRL_4BITBUS
;
1381 ctrl
&= ~SDHCI_CTRL_4BITBUS
;
1383 sdhci_writeb(host
, ctrl
, SDHCI_HOST_CONTROL
);
1386 ctrl
= sdhci_readb(host
, SDHCI_HOST_CONTROL
);
1388 if ((ios
->timing
== MMC_TIMING_SD_HS
||
1389 ios
->timing
== MMC_TIMING_MMC_HS
)
1390 && !(host
->quirks
& SDHCI_QUIRK_NO_HISPD_BIT
))
1391 ctrl
|= SDHCI_CTRL_HISPD
;
1393 ctrl
&= ~SDHCI_CTRL_HISPD
;
1395 if (host
->version
>= SDHCI_SPEC_300
) {
1399 /* In case of UHS-I modes, set High Speed Enable */
1400 if ((ios
->timing
== MMC_TIMING_MMC_HS200
) ||
1401 (ios
->timing
== MMC_TIMING_UHS_SDR50
) ||
1402 (ios
->timing
== MMC_TIMING_UHS_SDR104
) ||
1403 (ios
->timing
== MMC_TIMING_UHS_DDR50
) ||
1404 (ios
->timing
== MMC_TIMING_UHS_SDR25
))
1405 ctrl
|= SDHCI_CTRL_HISPD
;
1407 ctrl_2
= sdhci_readw(host
, SDHCI_HOST_CONTROL2
);
1408 if (!(ctrl_2
& SDHCI_CTRL_PRESET_VAL_ENABLE
)) {
1409 sdhci_writeb(host
, ctrl
, SDHCI_HOST_CONTROL
);
1411 * We only need to set Driver Strength if the
1412 * preset value enable is not set.
1414 ctrl_2
&= ~SDHCI_CTRL_DRV_TYPE_MASK
;
1415 if (ios
->drv_type
== MMC_SET_DRIVER_TYPE_A
)
1416 ctrl_2
|= SDHCI_CTRL_DRV_TYPE_A
;
1417 else if (ios
->drv_type
== MMC_SET_DRIVER_TYPE_C
)
1418 ctrl_2
|= SDHCI_CTRL_DRV_TYPE_C
;
1420 sdhci_writew(host
, ctrl_2
, SDHCI_HOST_CONTROL2
);
1423 * According to SDHC Spec v3.00, if the Preset Value
1424 * Enable in the Host Control 2 register is set, we
1425 * need to reset SD Clock Enable before changing High
1426 * Speed Enable to avoid generating clock gliches.
1429 /* Reset SD Clock Enable */
1430 clk
= sdhci_readw(host
, SDHCI_CLOCK_CONTROL
);
1431 clk
&= ~SDHCI_CLOCK_CARD_EN
;
1432 sdhci_writew(host
, clk
, SDHCI_CLOCK_CONTROL
);
1434 sdhci_writeb(host
, ctrl
, SDHCI_HOST_CONTROL
);
1436 /* Re-enable SD Clock */
1437 clock
= host
->clock
;
1439 sdhci_set_clock(host
, clock
);
1443 /* Reset SD Clock Enable */
1444 clk
= sdhci_readw(host
, SDHCI_CLOCK_CONTROL
);
1445 clk
&= ~SDHCI_CLOCK_CARD_EN
;
1446 sdhci_writew(host
, clk
, SDHCI_CLOCK_CONTROL
);
1448 if (host
->ops
->set_uhs_signaling
)
1449 host
->ops
->set_uhs_signaling(host
, ios
->timing
);
1451 ctrl_2
= sdhci_readw(host
, SDHCI_HOST_CONTROL2
);
1452 /* Select Bus Speed Mode for host */
1453 ctrl_2
&= ~SDHCI_CTRL_UHS_MASK
;
1454 if (ios
->timing
== MMC_TIMING_MMC_HS200
)
1455 ctrl_2
|= SDHCI_CTRL_HS_SDR200
;
1456 else if (ios
->timing
== MMC_TIMING_UHS_SDR12
)
1457 ctrl_2
|= SDHCI_CTRL_UHS_SDR12
;
1458 else if (ios
->timing
== MMC_TIMING_UHS_SDR25
)
1459 ctrl_2
|= SDHCI_CTRL_UHS_SDR25
;
1460 else if (ios
->timing
== MMC_TIMING_UHS_SDR50
)
1461 ctrl_2
|= SDHCI_CTRL_UHS_SDR50
;
1462 else if (ios
->timing
== MMC_TIMING_UHS_SDR104
)
1463 ctrl_2
|= SDHCI_CTRL_UHS_SDR104
;
1464 else if (ios
->timing
== MMC_TIMING_UHS_DDR50
)
1465 ctrl_2
|= SDHCI_CTRL_UHS_DDR50
;
1466 sdhci_writew(host
, ctrl_2
, SDHCI_HOST_CONTROL2
);
1469 /* Re-enable SD Clock */
1470 clock
= host
->clock
;
1472 sdhci_set_clock(host
, clock
);
1474 sdhci_writeb(host
, ctrl
, SDHCI_HOST_CONTROL
);
1477 * Some (ENE) controllers go apeshit on some ios operation,
1478 * signalling timeout and CRC errors even on CMD0. Resetting
1479 * it on each ios seems to solve the problem.
1481 if(host
->quirks
& SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS
)
1482 sdhci_reset(host
, SDHCI_RESET_CMD
| SDHCI_RESET_DATA
);
1485 spin_unlock_irqrestore(&host
->lock
, flags
);
1488 static void sdhci_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
1490 struct sdhci_host
*host
= mmc_priv(mmc
);
1492 sdhci_runtime_pm_get(host
);
1493 sdhci_do_set_ios(host
, ios
);
1494 sdhci_runtime_pm_put(host
);
1497 static int sdhci_check_ro(struct sdhci_host
*host
)
1499 unsigned long flags
;
1502 spin_lock_irqsave(&host
->lock
, flags
);
1504 if (host
->flags
& SDHCI_DEVICE_DEAD
)
1506 else if (host
->ops
->get_ro
)
1507 is_readonly
= host
->ops
->get_ro(host
);
1509 is_readonly
= !(sdhci_readl(host
, SDHCI_PRESENT_STATE
)
1510 & SDHCI_WRITE_PROTECT
);
1512 spin_unlock_irqrestore(&host
->lock
, flags
);
1514 /* This quirk needs to be replaced by a callback-function later */
1515 return host
->quirks
& SDHCI_QUIRK_INVERTED_WRITE_PROTECT
?
1516 !is_readonly
: is_readonly
;
1519 #define SAMPLE_COUNT 5
1521 static int sdhci_do_get_ro(struct sdhci_host
*host
)
1525 if (!(host
->quirks
& SDHCI_QUIRK_UNSTABLE_RO_DETECT
))
1526 return sdhci_check_ro(host
);
1529 for (i
= 0; i
< SAMPLE_COUNT
; i
++) {
1530 if (sdhci_check_ro(host
)) {
1531 if (++ro_count
> SAMPLE_COUNT
/ 2)
1539 static void sdhci_hw_reset(struct mmc_host
*mmc
)
1541 struct sdhci_host
*host
= mmc_priv(mmc
);
1543 if (host
->ops
&& host
->ops
->hw_reset
)
1544 host
->ops
->hw_reset(host
);
1547 static int sdhci_get_ro(struct mmc_host
*mmc
)
1549 struct sdhci_host
*host
= mmc_priv(mmc
);
1552 sdhci_runtime_pm_get(host
);
1553 ret
= sdhci_do_get_ro(host
);
1554 sdhci_runtime_pm_put(host
);
1558 static void sdhci_enable_sdio_irq_nolock(struct sdhci_host
*host
, int enable
)
1560 if (host
->flags
& SDHCI_DEVICE_DEAD
)
1564 host
->flags
|= SDHCI_SDIO_IRQ_ENABLED
;
1566 host
->flags
&= ~SDHCI_SDIO_IRQ_ENABLED
;
1568 /* SDIO IRQ will be enabled as appropriate in runtime resume */
1569 if (host
->runtime_suspended
)
1573 sdhci_unmask_irqs(host
, SDHCI_INT_CARD_INT
);
1575 sdhci_mask_irqs(host
, SDHCI_INT_CARD_INT
);
1580 static void sdhci_enable_sdio_irq(struct mmc_host
*mmc
, int enable
)
1582 struct sdhci_host
*host
= mmc_priv(mmc
);
1583 unsigned long flags
;
1585 spin_lock_irqsave(&host
->lock
, flags
);
1586 sdhci_enable_sdio_irq_nolock(host
, enable
);
1587 spin_unlock_irqrestore(&host
->lock
, flags
);
1590 static int sdhci_do_start_signal_voltage_switch(struct sdhci_host
*host
,
1591 struct mmc_ios
*ios
)
1598 * Signal Voltage Switching is only applicable for Host Controllers
1601 if (host
->version
< SDHCI_SPEC_300
)
1605 * We first check whether the request is to set signalling voltage
1606 * to 3.3V. If so, we change the voltage to 3.3V and return quickly.
1608 ctrl
= sdhci_readw(host
, SDHCI_HOST_CONTROL2
);
1609 if (ios
->signal_voltage
== MMC_SIGNAL_VOLTAGE_330
) {
1610 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1611 ctrl
&= ~SDHCI_CTRL_VDD_180
;
1612 sdhci_writew(host
, ctrl
, SDHCI_HOST_CONTROL2
);
1615 usleep_range(5000, 5500);
1617 /* 3.3V regulator output should be stable within 5 ms */
1618 ctrl
= sdhci_readw(host
, SDHCI_HOST_CONTROL2
);
1619 if (!(ctrl
& SDHCI_CTRL_VDD_180
))
1622 pr_info(DRIVER_NAME
": Switching to 3.3V "
1623 "signalling voltage failed\n");
1626 } else if (!(ctrl
& SDHCI_CTRL_VDD_180
) &&
1627 (ios
->signal_voltage
== MMC_SIGNAL_VOLTAGE_180
)) {
1629 clk
= sdhci_readw(host
, SDHCI_CLOCK_CONTROL
);
1630 clk
&= ~SDHCI_CLOCK_CARD_EN
;
1631 sdhci_writew(host
, clk
, SDHCI_CLOCK_CONTROL
);
1633 /* Check whether DAT[3:0] is 0000 */
1634 present_state
= sdhci_readl(host
, SDHCI_PRESENT_STATE
);
1635 if (!((present_state
& SDHCI_DATA_LVL_MASK
) >>
1636 SDHCI_DATA_LVL_SHIFT
)) {
1638 * Enable 1.8V Signal Enable in the Host Control2
1641 ctrl
|= SDHCI_CTRL_VDD_180
;
1642 sdhci_writew(host
, ctrl
, SDHCI_HOST_CONTROL2
);
1645 usleep_range(5000, 5500);
1647 ctrl
= sdhci_readw(host
, SDHCI_HOST_CONTROL2
);
1648 if (ctrl
& SDHCI_CTRL_VDD_180
) {
1649 /* Provide SDCLK again and wait for 1ms*/
1650 clk
= sdhci_readw(host
, SDHCI_CLOCK_CONTROL
);
1651 clk
|= SDHCI_CLOCK_CARD_EN
;
1652 sdhci_writew(host
, clk
, SDHCI_CLOCK_CONTROL
);
1653 usleep_range(1000, 1500);
1656 * If DAT[3:0] level is 1111b, then the card
1657 * was successfully switched to 1.8V signaling.
1659 present_state
= sdhci_readl(host
,
1660 SDHCI_PRESENT_STATE
);
1661 if ((present_state
& SDHCI_DATA_LVL_MASK
) ==
1662 SDHCI_DATA_LVL_MASK
)
1668 * If we are here, that means the switch to 1.8V signaling
1669 * failed. We power cycle the card, and retry initialization
1670 * sequence by setting S18R to 0.
1672 pwr
= sdhci_readb(host
, SDHCI_POWER_CONTROL
);
1673 pwr
&= ~SDHCI_POWER_ON
;
1674 sdhci_writeb(host
, pwr
, SDHCI_POWER_CONTROL
);
1676 /* Wait for 1ms as per the spec */
1677 usleep_range(1000, 1500);
1678 pwr
|= SDHCI_POWER_ON
;
1679 sdhci_writeb(host
, pwr
, SDHCI_POWER_CONTROL
);
1681 pr_info(DRIVER_NAME
": Switching to 1.8V signalling "
1682 "voltage failed, retrying with S18R set to 0\n");
1685 /* No signal voltage switch required */
1689 static int sdhci_start_signal_voltage_switch(struct mmc_host
*mmc
,
1690 struct mmc_ios
*ios
)
1692 struct sdhci_host
*host
= mmc_priv(mmc
);
1695 if (host
->version
< SDHCI_SPEC_300
)
1697 sdhci_runtime_pm_get(host
);
1698 err
= sdhci_do_start_signal_voltage_switch(host
, ios
);
1699 sdhci_runtime_pm_put(host
);
1703 static int sdhci_execute_tuning(struct mmc_host
*mmc
, u32 opcode
)
1705 struct sdhci_host
*host
;
1708 int tuning_loop_counter
= MAX_TUNING_LOOP
;
1709 unsigned long timeout
;
1711 bool requires_tuning_nonuhs
= false;
1713 host
= mmc_priv(mmc
);
1715 sdhci_runtime_pm_get(host
);
1716 disable_irq(host
->irq
);
1717 spin_lock(&host
->lock
);
1719 ctrl
= sdhci_readw(host
, SDHCI_HOST_CONTROL2
);
1722 * The Host Controller needs tuning only in case of SDR104 mode
1723 * and for SDR50 mode when Use Tuning for SDR50 is set in the
1724 * Capabilities register.
1725 * If the Host Controller supports the HS200 mode then the
1726 * tuning function has to be executed.
1728 if (((ctrl
& SDHCI_CTRL_UHS_MASK
) == SDHCI_CTRL_UHS_SDR50
) &&
1729 (host
->flags
& SDHCI_SDR50_NEEDS_TUNING
||
1730 host
->flags
& SDHCI_HS200_NEEDS_TUNING
))
1731 requires_tuning_nonuhs
= true;
1733 if (((ctrl
& SDHCI_CTRL_UHS_MASK
) == SDHCI_CTRL_UHS_SDR104
) ||
1734 requires_tuning_nonuhs
)
1735 ctrl
|= SDHCI_CTRL_EXEC_TUNING
;
1737 spin_unlock(&host
->lock
);
1738 enable_irq(host
->irq
);
1739 sdhci_runtime_pm_put(host
);
1743 sdhci_writew(host
, ctrl
, SDHCI_HOST_CONTROL2
);
1746 * As per the Host Controller spec v3.00, tuning command
1747 * generates Buffer Read Ready interrupt, so enable that.
1749 * Note: The spec clearly says that when tuning sequence
1750 * is being performed, the controller does not generate
1751 * interrupts other than Buffer Read Ready interrupt. But
1752 * to make sure we don't hit a controller bug, we _only_
1753 * enable Buffer Read Ready interrupt here.
1755 ier
= sdhci_readl(host
, SDHCI_INT_ENABLE
);
1756 sdhci_clear_set_irqs(host
, ier
, SDHCI_INT_DATA_AVAIL
);
1759 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1760 * of loops reaches 40 times or a timeout of 150ms occurs.
1764 struct mmc_command cmd
= {0};
1765 struct mmc_request mrq
= {NULL
};
1767 if (!tuning_loop_counter
&& !timeout
)
1770 cmd
.opcode
= opcode
;
1772 cmd
.flags
= MMC_RSP_R1
| MMC_CMD_ADTC
;
1781 * In response to CMD19, the card sends 64 bytes of tuning
1782 * block to the Host Controller. So we set the block size
1785 if (cmd
.opcode
== MMC_SEND_TUNING_BLOCK_HS200
) {
1786 if (mmc
->ios
.bus_width
== MMC_BUS_WIDTH_8
)
1787 sdhci_writew(host
, SDHCI_MAKE_BLKSZ(7, 128),
1789 else if (mmc
->ios
.bus_width
== MMC_BUS_WIDTH_4
)
1790 sdhci_writew(host
, SDHCI_MAKE_BLKSZ(7, 64),
1793 sdhci_writew(host
, SDHCI_MAKE_BLKSZ(7, 64),
1798 * The tuning block is sent by the card to the host controller.
1799 * So we set the TRNS_READ bit in the Transfer Mode register.
1800 * This also takes care of setting DMA Enable and Multi Block
1801 * Select in the same register to 0.
1803 sdhci_writew(host
, SDHCI_TRNS_READ
, SDHCI_TRANSFER_MODE
);
1805 sdhci_send_command(host
, &cmd
);
1810 spin_unlock(&host
->lock
);
1811 enable_irq(host
->irq
);
1813 /* Wait for Buffer Read Ready interrupt */
1814 wait_event_interruptible_timeout(host
->buf_ready_int
,
1815 (host
->tuning_done
== 1),
1816 msecs_to_jiffies(50));
1817 disable_irq(host
->irq
);
1818 spin_lock(&host
->lock
);
1820 if (!host
->tuning_done
) {
1821 pr_info(DRIVER_NAME
": Timeout waiting for "
1822 "Buffer Read Ready interrupt during tuning "
1823 "procedure, falling back to fixed sampling "
1825 ctrl
= sdhci_readw(host
, SDHCI_HOST_CONTROL2
);
1826 ctrl
&= ~SDHCI_CTRL_TUNED_CLK
;
1827 ctrl
&= ~SDHCI_CTRL_EXEC_TUNING
;
1828 sdhci_writew(host
, ctrl
, SDHCI_HOST_CONTROL2
);
1834 host
->tuning_done
= 0;
1836 ctrl
= sdhci_readw(host
, SDHCI_HOST_CONTROL2
);
1837 tuning_loop_counter
--;
1840 } while (ctrl
& SDHCI_CTRL_EXEC_TUNING
);
1843 * The Host Driver has exhausted the maximum number of loops allowed,
1844 * so use fixed sampling frequency.
1846 if (!tuning_loop_counter
|| !timeout
) {
1847 ctrl
&= ~SDHCI_CTRL_TUNED_CLK
;
1848 sdhci_writew(host
, ctrl
, SDHCI_HOST_CONTROL2
);
1850 if (!(ctrl
& SDHCI_CTRL_TUNED_CLK
)) {
1851 pr_info(DRIVER_NAME
": Tuning procedure"
1852 " failed, falling back to fixed sampling"
1860 * If this is the very first time we are here, we start the retuning
1861 * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
1862 * flag won't be set, we check this condition before actually starting
1865 if (!(host
->flags
& SDHCI_NEEDS_RETUNING
) && host
->tuning_count
&&
1866 (host
->tuning_mode
== SDHCI_TUNING_MODE_1
)) {
1867 mod_timer(&host
->tuning_timer
, jiffies
+
1868 host
->tuning_count
* HZ
);
1869 /* Tuning mode 1 limits the maximum data length to 4MB */
1870 mmc
->max_blk_count
= (4 * 1024 * 1024) / mmc
->max_blk_size
;
1872 host
->flags
&= ~SDHCI_NEEDS_RETUNING
;
1873 /* Reload the new initial value for timer */
1874 if (host
->tuning_mode
== SDHCI_TUNING_MODE_1
)
1875 mod_timer(&host
->tuning_timer
, jiffies
+
1876 host
->tuning_count
* HZ
);
1880 * In case tuning fails, host controllers which support re-tuning can
1881 * try tuning again at a later time, when the re-tuning timer expires.
1882 * So for these controllers, we return 0. Since there might be other
1883 * controllers who do not have this capability, we return error for
1886 if (err
&& host
->tuning_count
&&
1887 host
->tuning_mode
== SDHCI_TUNING_MODE_1
)
1890 sdhci_clear_set_irqs(host
, SDHCI_INT_DATA_AVAIL
, ier
);
1891 spin_unlock(&host
->lock
);
1892 enable_irq(host
->irq
);
1893 sdhci_runtime_pm_put(host
);
1898 static void sdhci_do_enable_preset_value(struct sdhci_host
*host
, bool enable
)
1901 unsigned long flags
;
1903 /* Host Controller v3.00 defines preset value registers */
1904 if (host
->version
< SDHCI_SPEC_300
)
1907 spin_lock_irqsave(&host
->lock
, flags
);
1909 ctrl
= sdhci_readw(host
, SDHCI_HOST_CONTROL2
);
1912 * We only enable or disable Preset Value if they are not already
1913 * enabled or disabled respectively. Otherwise, we bail out.
1915 if (enable
&& !(ctrl
& SDHCI_CTRL_PRESET_VAL_ENABLE
)) {
1916 ctrl
|= SDHCI_CTRL_PRESET_VAL_ENABLE
;
1917 sdhci_writew(host
, ctrl
, SDHCI_HOST_CONTROL2
);
1918 host
->flags
|= SDHCI_PV_ENABLED
;
1919 } else if (!enable
&& (ctrl
& SDHCI_CTRL_PRESET_VAL_ENABLE
)) {
1920 ctrl
&= ~SDHCI_CTRL_PRESET_VAL_ENABLE
;
1921 sdhci_writew(host
, ctrl
, SDHCI_HOST_CONTROL2
);
1922 host
->flags
&= ~SDHCI_PV_ENABLED
;
1925 spin_unlock_irqrestore(&host
->lock
, flags
);
1928 static void sdhci_enable_preset_value(struct mmc_host
*mmc
, bool enable
)
1930 struct sdhci_host
*host
= mmc_priv(mmc
);
1932 sdhci_runtime_pm_get(host
);
1933 sdhci_do_enable_preset_value(host
, enable
);
1934 sdhci_runtime_pm_put(host
);
1937 static const struct mmc_host_ops sdhci_ops
= {
1938 .request
= sdhci_request
,
1939 .set_ios
= sdhci_set_ios
,
1940 .get_ro
= sdhci_get_ro
,
1941 .hw_reset
= sdhci_hw_reset
,
1942 .enable_sdio_irq
= sdhci_enable_sdio_irq
,
1943 .start_signal_voltage_switch
= sdhci_start_signal_voltage_switch
,
1944 .execute_tuning
= sdhci_execute_tuning
,
1945 .enable_preset_value
= sdhci_enable_preset_value
,
1948 /*****************************************************************************\
1952 \*****************************************************************************/
1954 static void sdhci_tasklet_card(unsigned long param
)
1956 struct sdhci_host
*host
;
1957 unsigned long flags
;
1959 host
= (struct sdhci_host
*)param
;
1961 spin_lock_irqsave(&host
->lock
, flags
);
1963 /* Check host->mrq first in case we are runtime suspended */
1965 !(sdhci_readl(host
, SDHCI_PRESENT_STATE
) & SDHCI_CARD_PRESENT
)) {
1966 pr_err("%s: Card removed during transfer!\n",
1967 mmc_hostname(host
->mmc
));
1968 pr_err("%s: Resetting controller.\n",
1969 mmc_hostname(host
->mmc
));
1971 sdhci_reset(host
, SDHCI_RESET_CMD
);
1972 sdhci_reset(host
, SDHCI_RESET_DATA
);
1974 host
->mrq
->cmd
->error
= -ENOMEDIUM
;
1975 tasklet_schedule(&host
->finish_tasklet
);
1978 spin_unlock_irqrestore(&host
->lock
, flags
);
1980 mmc_detect_change(host
->mmc
, msecs_to_jiffies(200));
1983 static void sdhci_tasklet_finish(unsigned long param
)
1985 struct sdhci_host
*host
;
1986 unsigned long flags
;
1987 struct mmc_request
*mrq
;
1989 host
= (struct sdhci_host
*)param
;
1991 spin_lock_irqsave(&host
->lock
, flags
);
1994 * If this tasklet gets rescheduled while running, it will
1995 * be run again afterwards but without any active request.
1998 spin_unlock_irqrestore(&host
->lock
, flags
);
2002 del_timer(&host
->timer
);
2007 * The controller needs a reset of internal state machines
2008 * upon error conditions.
2010 if (!(host
->flags
& SDHCI_DEVICE_DEAD
) &&
2011 ((mrq
->cmd
&& mrq
->cmd
->error
) ||
2012 (mrq
->data
&& (mrq
->data
->error
||
2013 (mrq
->data
->stop
&& mrq
->data
->stop
->error
))) ||
2014 (host
->quirks
& SDHCI_QUIRK_RESET_AFTER_REQUEST
))) {
2016 /* Some controllers need this kick or reset won't work here */
2017 if (host
->quirks
& SDHCI_QUIRK_CLOCK_BEFORE_RESET
) {
2020 /* This is to force an update */
2021 clock
= host
->clock
;
2023 sdhci_set_clock(host
, clock
);
2026 /* Spec says we should do both at the same time, but Ricoh
2027 controllers do not like that. */
2028 sdhci_reset(host
, SDHCI_RESET_CMD
);
2029 sdhci_reset(host
, SDHCI_RESET_DATA
);
2036 #ifndef SDHCI_USE_LEDS_CLASS
2037 sdhci_deactivate_led(host
);
2041 spin_unlock_irqrestore(&host
->lock
, flags
);
2043 mmc_request_done(host
->mmc
, mrq
);
2044 sdhci_runtime_pm_put(host
);
2047 static void sdhci_timeout_timer(unsigned long data
)
2049 struct sdhci_host
*host
;
2050 unsigned long flags
;
2052 host
= (struct sdhci_host
*)data
;
2054 spin_lock_irqsave(&host
->lock
, flags
);
2057 pr_err("%s: Timeout waiting for hardware "
2058 "interrupt.\n", mmc_hostname(host
->mmc
));
2059 sdhci_dumpregs(host
);
2062 host
->data
->error
= -ETIMEDOUT
;
2063 sdhci_finish_data(host
);
2066 host
->cmd
->error
= -ETIMEDOUT
;
2068 host
->mrq
->cmd
->error
= -ETIMEDOUT
;
2070 tasklet_schedule(&host
->finish_tasklet
);
2075 spin_unlock_irqrestore(&host
->lock
, flags
);
2078 static void sdhci_tuning_timer(unsigned long data
)
2080 struct sdhci_host
*host
;
2081 unsigned long flags
;
2083 host
= (struct sdhci_host
*)data
;
2085 spin_lock_irqsave(&host
->lock
, flags
);
2087 host
->flags
|= SDHCI_NEEDS_RETUNING
;
2089 spin_unlock_irqrestore(&host
->lock
, flags
);
2092 /*****************************************************************************\
2094 * Interrupt handling *
2096 \*****************************************************************************/
2098 static void sdhci_cmd_irq(struct sdhci_host
*host
, u32 intmask
)
2100 BUG_ON(intmask
== 0);
2103 pr_err("%s: Got command interrupt 0x%08x even "
2104 "though no command operation was in progress.\n",
2105 mmc_hostname(host
->mmc
), (unsigned)intmask
);
2106 sdhci_dumpregs(host
);
2110 if (intmask
& SDHCI_INT_TIMEOUT
)
2111 host
->cmd
->error
= -ETIMEDOUT
;
2112 else if (intmask
& (SDHCI_INT_CRC
| SDHCI_INT_END_BIT
|
2114 host
->cmd
->error
= -EILSEQ
;
2116 if (host
->cmd
->error
) {
2117 tasklet_schedule(&host
->finish_tasklet
);
2122 * The host can send and interrupt when the busy state has
2123 * ended, allowing us to wait without wasting CPU cycles.
2124 * Unfortunately this is overloaded on the "data complete"
2125 * interrupt, so we need to take some care when handling
2128 * Note: The 1.0 specification is a bit ambiguous about this
2129 * feature so there might be some problems with older
2132 if (host
->cmd
->flags
& MMC_RSP_BUSY
) {
2133 if (host
->cmd
->data
)
2134 DBG("Cannot wait for busy signal when also "
2135 "doing a data transfer");
2136 else if (!(host
->quirks
& SDHCI_QUIRK_NO_BUSY_IRQ
))
2139 /* The controller does not support the end-of-busy IRQ,
2140 * fall through and take the SDHCI_INT_RESPONSE */
2143 if (intmask
& SDHCI_INT_RESPONSE
)
2144 sdhci_finish_command(host
);
2147 #ifdef CONFIG_MMC_DEBUG
2148 static void sdhci_show_adma_error(struct sdhci_host
*host
)
2150 const char *name
= mmc_hostname(host
->mmc
);
2151 u8
*desc
= host
->adma_desc
;
2156 sdhci_dumpregs(host
);
2159 dma
= (__le32
*)(desc
+ 4);
2160 len
= (__le16
*)(desc
+ 2);
2163 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2164 name
, desc
, le32_to_cpu(*dma
), le16_to_cpu(*len
), attr
);
2173 static void sdhci_show_adma_error(struct sdhci_host
*host
) { }
2176 static void sdhci_data_irq(struct sdhci_host
*host
, u32 intmask
)
2179 BUG_ON(intmask
== 0);
2181 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2182 if (intmask
& SDHCI_INT_DATA_AVAIL
) {
2183 command
= SDHCI_GET_CMD(sdhci_readw(host
, SDHCI_COMMAND
));
2184 if (command
== MMC_SEND_TUNING_BLOCK
||
2185 command
== MMC_SEND_TUNING_BLOCK_HS200
) {
2186 host
->tuning_done
= 1;
2187 wake_up(&host
->buf_ready_int
);
2194 * The "data complete" interrupt is also used to
2195 * indicate that a busy state has ended. See comment
2196 * above in sdhci_cmd_irq().
2198 if (host
->cmd
&& (host
->cmd
->flags
& MMC_RSP_BUSY
)) {
2199 if (intmask
& SDHCI_INT_DATA_END
) {
2200 sdhci_finish_command(host
);
2205 pr_err("%s: Got data interrupt 0x%08x even "
2206 "though no data operation was in progress.\n",
2207 mmc_hostname(host
->mmc
), (unsigned)intmask
);
2208 sdhci_dumpregs(host
);
2213 if (intmask
& SDHCI_INT_DATA_TIMEOUT
)
2214 host
->data
->error
= -ETIMEDOUT
;
2215 else if (intmask
& SDHCI_INT_DATA_END_BIT
)
2216 host
->data
->error
= -EILSEQ
;
2217 else if ((intmask
& SDHCI_INT_DATA_CRC
) &&
2218 SDHCI_GET_CMD(sdhci_readw(host
, SDHCI_COMMAND
))
2220 host
->data
->error
= -EILSEQ
;
2221 else if (intmask
& SDHCI_INT_ADMA_ERROR
) {
2222 pr_err("%s: ADMA error\n", mmc_hostname(host
->mmc
));
2223 sdhci_show_adma_error(host
);
2224 host
->data
->error
= -EIO
;
2227 if (host
->data
->error
)
2228 sdhci_finish_data(host
);
2230 if (intmask
& (SDHCI_INT_DATA_AVAIL
| SDHCI_INT_SPACE_AVAIL
))
2231 sdhci_transfer_pio(host
);
2234 * We currently don't do anything fancy with DMA
2235 * boundaries, but as we can't disable the feature
2236 * we need to at least restart the transfer.
2238 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2239 * should return a valid address to continue from, but as
2240 * some controllers are faulty, don't trust them.
2242 if (intmask
& SDHCI_INT_DMA_END
) {
2243 u32 dmastart
, dmanow
;
2244 dmastart
= sg_dma_address(host
->data
->sg
);
2245 dmanow
= dmastart
+ host
->data
->bytes_xfered
;
2247 * Force update to the next DMA block boundary.
2250 ~(SDHCI_DEFAULT_BOUNDARY_SIZE
- 1)) +
2251 SDHCI_DEFAULT_BOUNDARY_SIZE
;
2252 host
->data
->bytes_xfered
= dmanow
- dmastart
;
2253 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2255 mmc_hostname(host
->mmc
), dmastart
,
2256 host
->data
->bytes_xfered
, dmanow
);
2257 sdhci_writel(host
, dmanow
, SDHCI_DMA_ADDRESS
);
2260 if (intmask
& SDHCI_INT_DATA_END
) {
2263 * Data managed to finish before the
2264 * command completed. Make sure we do
2265 * things in the proper order.
2267 host
->data_early
= 1;
2269 sdhci_finish_data(host
);
2275 static irqreturn_t
sdhci_irq(int irq
, void *dev_id
)
2278 struct sdhci_host
*host
= dev_id
;
2279 u32 intmask
, unexpected
= 0;
2280 int cardint
= 0, max_loops
= 16;
2282 spin_lock(&host
->lock
);
2284 if (host
->runtime_suspended
) {
2285 spin_unlock(&host
->lock
);
2286 pr_warning("%s: got irq while runtime suspended\n",
2287 mmc_hostname(host
->mmc
));
2291 intmask
= sdhci_readl(host
, SDHCI_INT_STATUS
);
2293 if (!intmask
|| intmask
== 0xffffffff) {
2299 DBG("*** %s got interrupt: 0x%08x\n",
2300 mmc_hostname(host
->mmc
), intmask
);
2302 if (intmask
& (SDHCI_INT_CARD_INSERT
| SDHCI_INT_CARD_REMOVE
)) {
2303 u32 present
= sdhci_readl(host
, SDHCI_PRESENT_STATE
) &
2307 * There is a observation on i.mx esdhc. INSERT bit will be
2308 * immediately set again when it gets cleared, if a card is
2309 * inserted. We have to mask the irq to prevent interrupt
2310 * storm which will freeze the system. And the REMOVE gets
2311 * the same situation.
2313 * More testing are needed here to ensure it works for other
2316 sdhci_mask_irqs(host
, present
? SDHCI_INT_CARD_INSERT
:
2317 SDHCI_INT_CARD_REMOVE
);
2318 sdhci_unmask_irqs(host
, present
? SDHCI_INT_CARD_REMOVE
:
2319 SDHCI_INT_CARD_INSERT
);
2321 sdhci_writel(host
, intmask
& (SDHCI_INT_CARD_INSERT
|
2322 SDHCI_INT_CARD_REMOVE
), SDHCI_INT_STATUS
);
2323 intmask
&= ~(SDHCI_INT_CARD_INSERT
| SDHCI_INT_CARD_REMOVE
);
2324 tasklet_schedule(&host
->card_tasklet
);
2327 if (intmask
& SDHCI_INT_CMD_MASK
) {
2328 sdhci_writel(host
, intmask
& SDHCI_INT_CMD_MASK
,
2330 sdhci_cmd_irq(host
, intmask
& SDHCI_INT_CMD_MASK
);
2333 if (intmask
& SDHCI_INT_DATA_MASK
) {
2334 sdhci_writel(host
, intmask
& SDHCI_INT_DATA_MASK
,
2336 sdhci_data_irq(host
, intmask
& SDHCI_INT_DATA_MASK
);
2339 intmask
&= ~(SDHCI_INT_CMD_MASK
| SDHCI_INT_DATA_MASK
);
2341 intmask
&= ~SDHCI_INT_ERROR
;
2343 if (intmask
& SDHCI_INT_BUS_POWER
) {
2344 pr_err("%s: Card is consuming too much power!\n",
2345 mmc_hostname(host
->mmc
));
2346 sdhci_writel(host
, SDHCI_INT_BUS_POWER
, SDHCI_INT_STATUS
);
2349 intmask
&= ~SDHCI_INT_BUS_POWER
;
2351 if (intmask
& SDHCI_INT_CARD_INT
)
2354 intmask
&= ~SDHCI_INT_CARD_INT
;
2357 unexpected
|= intmask
;
2358 sdhci_writel(host
, intmask
, SDHCI_INT_STATUS
);
2361 result
= IRQ_HANDLED
;
2363 intmask
= sdhci_readl(host
, SDHCI_INT_STATUS
);
2364 if (intmask
&& --max_loops
)
2367 spin_unlock(&host
->lock
);
2370 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2371 mmc_hostname(host
->mmc
), unexpected
);
2372 sdhci_dumpregs(host
);
2375 * We have to delay this as it calls back into the driver.
2378 mmc_signal_sdio_irq(host
->mmc
);
2383 /*****************************************************************************\
2387 \*****************************************************************************/
2391 int sdhci_suspend_host(struct sdhci_host
*host
)
2394 bool has_tuning_timer
;
2396 if (host
->ops
->platform_suspend
)
2397 host
->ops
->platform_suspend(host
);
2399 sdhci_disable_card_detection(host
);
2401 /* Disable tuning since we are suspending */
2402 has_tuning_timer
= host
->version
>= SDHCI_SPEC_300
&&
2403 host
->tuning_count
&& host
->tuning_mode
== SDHCI_TUNING_MODE_1
;
2404 if (has_tuning_timer
) {
2405 del_timer_sync(&host
->tuning_timer
);
2406 host
->flags
&= ~SDHCI_NEEDS_RETUNING
;
2409 ret
= mmc_suspend_host(host
->mmc
);
2411 if (has_tuning_timer
) {
2412 host
->flags
|= SDHCI_NEEDS_RETUNING
;
2413 mod_timer(&host
->tuning_timer
, jiffies
+
2414 host
->tuning_count
* HZ
);
2417 sdhci_enable_card_detection(host
);
2422 free_irq(host
->irq
, host
);
2427 EXPORT_SYMBOL_GPL(sdhci_suspend_host
);
2429 int sdhci_resume_host(struct sdhci_host
*host
)
2433 if (host
->flags
& (SDHCI_USE_SDMA
| SDHCI_USE_ADMA
)) {
2434 if (host
->ops
->enable_dma
)
2435 host
->ops
->enable_dma(host
);
2438 ret
= request_irq(host
->irq
, sdhci_irq
, IRQF_SHARED
,
2439 mmc_hostname(host
->mmc
), host
);
2443 if ((host
->mmc
->pm_flags
& MMC_PM_KEEP_POWER
) &&
2444 (host
->quirks2
& SDHCI_QUIRK2_HOST_OFF_CARD_ON
)) {
2445 /* Card keeps power but host controller does not */
2446 sdhci_init(host
, 0);
2449 sdhci_do_set_ios(host
, &host
->mmc
->ios
);
2451 sdhci_init(host
, (host
->mmc
->pm_flags
& MMC_PM_KEEP_POWER
));
2455 ret
= mmc_resume_host(host
->mmc
);
2456 sdhci_enable_card_detection(host
);
2458 if (host
->ops
->platform_resume
)
2459 host
->ops
->platform_resume(host
);
2461 /* Set the re-tuning expiration flag */
2462 if ((host
->version
>= SDHCI_SPEC_300
) && host
->tuning_count
&&
2463 (host
->tuning_mode
== SDHCI_TUNING_MODE_1
))
2464 host
->flags
|= SDHCI_NEEDS_RETUNING
;
2469 EXPORT_SYMBOL_GPL(sdhci_resume_host
);
2471 void sdhci_enable_irq_wakeups(struct sdhci_host
*host
)
2474 val
= sdhci_readb(host
, SDHCI_WAKE_UP_CONTROL
);
2475 val
|= SDHCI_WAKE_ON_INT
;
2476 sdhci_writeb(host
, val
, SDHCI_WAKE_UP_CONTROL
);
2479 EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups
);
2481 #endif /* CONFIG_PM */
2483 #ifdef CONFIG_PM_RUNTIME
2485 static int sdhci_runtime_pm_get(struct sdhci_host
*host
)
2487 return pm_runtime_get_sync(host
->mmc
->parent
);
2490 static int sdhci_runtime_pm_put(struct sdhci_host
*host
)
2492 pm_runtime_mark_last_busy(host
->mmc
->parent
);
2493 return pm_runtime_put_autosuspend(host
->mmc
->parent
);
2496 int sdhci_runtime_suspend_host(struct sdhci_host
*host
)
2498 unsigned long flags
;
2501 /* Disable tuning since we are suspending */
2502 if (host
->version
>= SDHCI_SPEC_300
&&
2503 host
->tuning_mode
== SDHCI_TUNING_MODE_1
) {
2504 del_timer_sync(&host
->tuning_timer
);
2505 host
->flags
&= ~SDHCI_NEEDS_RETUNING
;
2508 spin_lock_irqsave(&host
->lock
, flags
);
2509 sdhci_mask_irqs(host
, SDHCI_INT_ALL_MASK
);
2510 spin_unlock_irqrestore(&host
->lock
, flags
);
2512 synchronize_irq(host
->irq
);
2514 spin_lock_irqsave(&host
->lock
, flags
);
2515 host
->runtime_suspended
= true;
2516 spin_unlock_irqrestore(&host
->lock
, flags
);
2520 EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host
);
2522 int sdhci_runtime_resume_host(struct sdhci_host
*host
)
2524 unsigned long flags
;
2525 int ret
= 0, host_flags
= host
->flags
;
2527 if (host_flags
& (SDHCI_USE_SDMA
| SDHCI_USE_ADMA
)) {
2528 if (host
->ops
->enable_dma
)
2529 host
->ops
->enable_dma(host
);
2532 sdhci_init(host
, 0);
2534 /* Force clock and power re-program */
2537 sdhci_do_set_ios(host
, &host
->mmc
->ios
);
2539 sdhci_do_start_signal_voltage_switch(host
, &host
->mmc
->ios
);
2540 if (host_flags
& SDHCI_PV_ENABLED
)
2541 sdhci_do_enable_preset_value(host
, true);
2543 /* Set the re-tuning expiration flag */
2544 if ((host
->version
>= SDHCI_SPEC_300
) && host
->tuning_count
&&
2545 (host
->tuning_mode
== SDHCI_TUNING_MODE_1
))
2546 host
->flags
|= SDHCI_NEEDS_RETUNING
;
2548 spin_lock_irqsave(&host
->lock
, flags
);
2550 host
->runtime_suspended
= false;
2552 /* Enable SDIO IRQ */
2553 if ((host
->flags
& SDHCI_SDIO_IRQ_ENABLED
))
2554 sdhci_enable_sdio_irq_nolock(host
, true);
2556 /* Enable Card Detection */
2557 sdhci_enable_card_detection(host
);
2559 spin_unlock_irqrestore(&host
->lock
, flags
);
2563 EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host
);
2567 /*****************************************************************************\
2569 * Device allocation/registration *
2571 \*****************************************************************************/
2573 struct sdhci_host
*sdhci_alloc_host(struct device
*dev
,
2576 struct mmc_host
*mmc
;
2577 struct sdhci_host
*host
;
2579 WARN_ON(dev
== NULL
);
2581 mmc
= mmc_alloc_host(sizeof(struct sdhci_host
) + priv_size
, dev
);
2583 return ERR_PTR(-ENOMEM
);
2585 host
= mmc_priv(mmc
);
2591 EXPORT_SYMBOL_GPL(sdhci_alloc_host
);
2593 int sdhci_add_host(struct sdhci_host
*host
)
2595 struct mmc_host
*mmc
;
2597 u32 max_current_caps
;
2598 unsigned int ocr_avail
;
2601 WARN_ON(host
== NULL
);
2608 host
->quirks
= debug_quirks
;
2610 host
->quirks2
= debug_quirks2
;
2612 sdhci_reset(host
, SDHCI_RESET_ALL
);
2614 host
->version
= sdhci_readw(host
, SDHCI_HOST_VERSION
);
2615 host
->version
= (host
->version
& SDHCI_SPEC_VER_MASK
)
2616 >> SDHCI_SPEC_VER_SHIFT
;
2617 if (host
->version
> SDHCI_SPEC_300
) {
2618 pr_err("%s: Unknown controller version (%d). "
2619 "You may experience problems.\n", mmc_hostname(mmc
),
2623 caps
[0] = (host
->quirks
& SDHCI_QUIRK_MISSING_CAPS
) ? host
->caps
:
2624 sdhci_readl(host
, SDHCI_CAPABILITIES
);
2626 caps
[1] = (host
->version
>= SDHCI_SPEC_300
) ?
2627 sdhci_readl(host
, SDHCI_CAPABILITIES_1
) : 0;
2629 if (host
->quirks
& SDHCI_QUIRK_FORCE_DMA
)
2630 host
->flags
|= SDHCI_USE_SDMA
;
2631 else if (!(caps
[0] & SDHCI_CAN_DO_SDMA
))
2632 DBG("Controller doesn't have SDMA capability\n");
2634 host
->flags
|= SDHCI_USE_SDMA
;
2636 if ((host
->quirks
& SDHCI_QUIRK_BROKEN_DMA
) &&
2637 (host
->flags
& SDHCI_USE_SDMA
)) {
2638 DBG("Disabling DMA as it is marked broken\n");
2639 host
->flags
&= ~SDHCI_USE_SDMA
;
2642 if ((host
->version
>= SDHCI_SPEC_200
) &&
2643 (caps
[0] & SDHCI_CAN_DO_ADMA2
))
2644 host
->flags
|= SDHCI_USE_ADMA
;
2646 if ((host
->quirks
& SDHCI_QUIRK_BROKEN_ADMA
) &&
2647 (host
->flags
& SDHCI_USE_ADMA
)) {
2648 DBG("Disabling ADMA as it is marked broken\n");
2649 host
->flags
&= ~SDHCI_USE_ADMA
;
2652 if (host
->flags
& (SDHCI_USE_SDMA
| SDHCI_USE_ADMA
)) {
2653 if (host
->ops
->enable_dma
) {
2654 if (host
->ops
->enable_dma(host
)) {
2655 pr_warning("%s: No suitable DMA "
2656 "available. Falling back to PIO.\n",
2659 ~(SDHCI_USE_SDMA
| SDHCI_USE_ADMA
);
2664 if (host
->flags
& SDHCI_USE_ADMA
) {
2666 * We need to allocate descriptors for all sg entries
2667 * (128) and potentially one alignment transfer for
2668 * each of those entries.
2670 host
->adma_desc
= kmalloc((128 * 2 + 1) * 4, GFP_KERNEL
);
2671 host
->align_buffer
= kmalloc(128 * 4, GFP_KERNEL
);
2672 if (!host
->adma_desc
|| !host
->align_buffer
) {
2673 kfree(host
->adma_desc
);
2674 kfree(host
->align_buffer
);
2675 pr_warning("%s: Unable to allocate ADMA "
2676 "buffers. Falling back to standard DMA.\n",
2678 host
->flags
&= ~SDHCI_USE_ADMA
;
2683 * If we use DMA, then it's up to the caller to set the DMA
2684 * mask, but PIO does not need the hw shim so we set a new
2685 * mask here in that case.
2687 if (!(host
->flags
& (SDHCI_USE_SDMA
| SDHCI_USE_ADMA
))) {
2688 host
->dma_mask
= DMA_BIT_MASK(64);
2689 mmc_dev(host
->mmc
)->dma_mask
= &host
->dma_mask
;
2692 if (host
->version
>= SDHCI_SPEC_300
)
2693 host
->max_clk
= (caps
[0] & SDHCI_CLOCK_V3_BASE_MASK
)
2694 >> SDHCI_CLOCK_BASE_SHIFT
;
2696 host
->max_clk
= (caps
[0] & SDHCI_CLOCK_BASE_MASK
)
2697 >> SDHCI_CLOCK_BASE_SHIFT
;
2699 host
->max_clk
*= 1000000;
2700 if (host
->max_clk
== 0 || host
->quirks
&
2701 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN
) {
2702 if (!host
->ops
->get_max_clock
) {
2703 pr_err("%s: Hardware doesn't specify base clock "
2704 "frequency.\n", mmc_hostname(mmc
));
2707 host
->max_clk
= host
->ops
->get_max_clock(host
);
2711 * In case of Host Controller v3.00, find out whether clock
2712 * multiplier is supported.
2714 host
->clk_mul
= (caps
[1] & SDHCI_CLOCK_MUL_MASK
) >>
2715 SDHCI_CLOCK_MUL_SHIFT
;
2718 * In case the value in Clock Multiplier is 0, then programmable
2719 * clock mode is not supported, otherwise the actual clock
2720 * multiplier is one more than the value of Clock Multiplier
2721 * in the Capabilities Register.
2727 * Set host parameters.
2729 mmc
->ops
= &sdhci_ops
;
2730 mmc
->f_max
= host
->max_clk
;
2731 if (host
->ops
->get_min_clock
)
2732 mmc
->f_min
= host
->ops
->get_min_clock(host
);
2733 else if (host
->version
>= SDHCI_SPEC_300
) {
2734 if (host
->clk_mul
) {
2735 mmc
->f_min
= (host
->max_clk
* host
->clk_mul
) / 1024;
2736 mmc
->f_max
= host
->max_clk
* host
->clk_mul
;
2738 mmc
->f_min
= host
->max_clk
/ SDHCI_MAX_DIV_SPEC_300
;
2740 mmc
->f_min
= host
->max_clk
/ SDHCI_MAX_DIV_SPEC_200
;
2743 (caps
[0] & SDHCI_TIMEOUT_CLK_MASK
) >> SDHCI_TIMEOUT_CLK_SHIFT
;
2744 if (host
->timeout_clk
== 0) {
2745 if (host
->ops
->get_timeout_clock
) {
2746 host
->timeout_clk
= host
->ops
->get_timeout_clock(host
);
2747 } else if (!(host
->quirks
&
2748 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK
)) {
2749 pr_err("%s: Hardware doesn't specify timeout clock "
2750 "frequency.\n", mmc_hostname(mmc
));
2754 if (caps
[0] & SDHCI_TIMEOUT_CLK_UNIT
)
2755 host
->timeout_clk
*= 1000;
2757 if (host
->quirks
& SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK
)
2758 host
->timeout_clk
= mmc
->f_max
/ 1000;
2760 mmc
->max_discard_to
= (1 << 27) / host
->timeout_clk
;
2762 mmc
->caps
|= MMC_CAP_SDIO_IRQ
| MMC_CAP_ERASE
| MMC_CAP_CMD23
;
2764 if (host
->quirks
& SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12
)
2765 host
->flags
|= SDHCI_AUTO_CMD12
;
2767 /* Auto-CMD23 stuff only works in ADMA or PIO. */
2768 if ((host
->version
>= SDHCI_SPEC_300
) &&
2769 ((host
->flags
& SDHCI_USE_ADMA
) ||
2770 !(host
->flags
& SDHCI_USE_SDMA
))) {
2771 host
->flags
|= SDHCI_AUTO_CMD23
;
2772 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc
));
2774 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc
));
2778 * A controller may support 8-bit width, but the board itself
2779 * might not have the pins brought out. Boards that support
2780 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
2781 * their platform code before calling sdhci_add_host(), and we
2782 * won't assume 8-bit width for hosts without that CAP.
2784 if (!(host
->quirks
& SDHCI_QUIRK_FORCE_1_BIT_DATA
))
2785 mmc
->caps
|= MMC_CAP_4_BIT_DATA
;
2787 if (caps
[0] & SDHCI_CAN_DO_HISPD
)
2788 mmc
->caps
|= MMC_CAP_SD_HIGHSPEED
| MMC_CAP_MMC_HIGHSPEED
;
2790 if ((host
->quirks
& SDHCI_QUIRK_BROKEN_CARD_DETECTION
) &&
2791 mmc_card_is_removable(mmc
))
2792 mmc
->caps
|= MMC_CAP_NEEDS_POLL
;
2794 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
2795 if (caps
[1] & (SDHCI_SUPPORT_SDR104
| SDHCI_SUPPORT_SDR50
|
2796 SDHCI_SUPPORT_DDR50
))
2797 mmc
->caps
|= MMC_CAP_UHS_SDR12
| MMC_CAP_UHS_SDR25
;
2799 /* SDR104 supports also implies SDR50 support */
2800 if (caps
[1] & SDHCI_SUPPORT_SDR104
)
2801 mmc
->caps
|= MMC_CAP_UHS_SDR104
| MMC_CAP_UHS_SDR50
;
2802 else if (caps
[1] & SDHCI_SUPPORT_SDR50
)
2803 mmc
->caps
|= MMC_CAP_UHS_SDR50
;
2805 if (caps
[1] & SDHCI_SUPPORT_DDR50
)
2806 mmc
->caps
|= MMC_CAP_UHS_DDR50
;
2808 /* Does the host need tuning for SDR50? */
2809 if (caps
[1] & SDHCI_USE_SDR50_TUNING
)
2810 host
->flags
|= SDHCI_SDR50_NEEDS_TUNING
;
2812 /* Does the host need tuning for HS200? */
2813 if (mmc
->caps2
& MMC_CAP2_HS200
)
2814 host
->flags
|= SDHCI_HS200_NEEDS_TUNING
;
2816 /* Driver Type(s) (A, C, D) supported by the host */
2817 if (caps
[1] & SDHCI_DRIVER_TYPE_A
)
2818 mmc
->caps
|= MMC_CAP_DRIVER_TYPE_A
;
2819 if (caps
[1] & SDHCI_DRIVER_TYPE_C
)
2820 mmc
->caps
|= MMC_CAP_DRIVER_TYPE_C
;
2821 if (caps
[1] & SDHCI_DRIVER_TYPE_D
)
2822 mmc
->caps
|= MMC_CAP_DRIVER_TYPE_D
;
2825 * If Power Off Notify capability is enabled by the host,
2826 * set notify to short power off notify timeout value.
2828 if (mmc
->caps2
& MMC_CAP2_POWEROFF_NOTIFY
)
2829 mmc
->power_notify_type
= MMC_HOST_PW_NOTIFY_SHORT
;
2831 mmc
->power_notify_type
= MMC_HOST_PW_NOTIFY_NONE
;
2833 /* Initial value for re-tuning timer count */
2834 host
->tuning_count
= (caps
[1] & SDHCI_RETUNING_TIMER_COUNT_MASK
) >>
2835 SDHCI_RETUNING_TIMER_COUNT_SHIFT
;
2838 * In case Re-tuning Timer is not disabled, the actual value of
2839 * re-tuning timer will be 2 ^ (n - 1).
2841 if (host
->tuning_count
)
2842 host
->tuning_count
= 1 << (host
->tuning_count
- 1);
2844 /* Re-tuning mode supported by the Host Controller */
2845 host
->tuning_mode
= (caps
[1] & SDHCI_RETUNING_MODE_MASK
) >>
2846 SDHCI_RETUNING_MODE_SHIFT
;
2850 * According to SD Host Controller spec v3.00, if the Host System
2851 * can afford more than 150mA, Host Driver should set XPC to 1. Also
2852 * the value is meaningful only if Voltage Support in the Capabilities
2853 * register is set. The actual current value is 4 times the register
2856 max_current_caps
= sdhci_readl(host
, SDHCI_MAX_CURRENT
);
2858 if (caps
[0] & SDHCI_CAN_VDD_330
) {
2859 int max_current_330
;
2861 ocr_avail
|= MMC_VDD_32_33
| MMC_VDD_33_34
;
2863 max_current_330
= ((max_current_caps
&
2864 SDHCI_MAX_CURRENT_330_MASK
) >>
2865 SDHCI_MAX_CURRENT_330_SHIFT
) *
2866 SDHCI_MAX_CURRENT_MULTIPLIER
;
2868 if (max_current_330
> 150)
2869 mmc
->caps
|= MMC_CAP_SET_XPC_330
;
2871 if (caps
[0] & SDHCI_CAN_VDD_300
) {
2872 int max_current_300
;
2874 ocr_avail
|= MMC_VDD_29_30
| MMC_VDD_30_31
;
2876 max_current_300
= ((max_current_caps
&
2877 SDHCI_MAX_CURRENT_300_MASK
) >>
2878 SDHCI_MAX_CURRENT_300_SHIFT
) *
2879 SDHCI_MAX_CURRENT_MULTIPLIER
;
2881 if (max_current_300
> 150)
2882 mmc
->caps
|= MMC_CAP_SET_XPC_300
;
2884 if (caps
[0] & SDHCI_CAN_VDD_180
) {
2885 int max_current_180
;
2887 ocr_avail
|= MMC_VDD_165_195
;
2889 max_current_180
= ((max_current_caps
&
2890 SDHCI_MAX_CURRENT_180_MASK
) >>
2891 SDHCI_MAX_CURRENT_180_SHIFT
) *
2892 SDHCI_MAX_CURRENT_MULTIPLIER
;
2894 if (max_current_180
> 150)
2895 mmc
->caps
|= MMC_CAP_SET_XPC_180
;
2897 /* Maximum current capabilities of the host at 1.8V */
2898 if (max_current_180
>= 800)
2899 mmc
->caps
|= MMC_CAP_MAX_CURRENT_800
;
2900 else if (max_current_180
>= 600)
2901 mmc
->caps
|= MMC_CAP_MAX_CURRENT_600
;
2902 else if (max_current_180
>= 400)
2903 mmc
->caps
|= MMC_CAP_MAX_CURRENT_400
;
2905 mmc
->caps
|= MMC_CAP_MAX_CURRENT_200
;
2908 mmc
->ocr_avail
= ocr_avail
;
2909 mmc
->ocr_avail_sdio
= ocr_avail
;
2910 if (host
->ocr_avail_sdio
)
2911 mmc
->ocr_avail_sdio
&= host
->ocr_avail_sdio
;
2912 mmc
->ocr_avail_sd
= ocr_avail
;
2913 if (host
->ocr_avail_sd
)
2914 mmc
->ocr_avail_sd
&= host
->ocr_avail_sd
;
2915 else /* normal SD controllers don't support 1.8V */
2916 mmc
->ocr_avail_sd
&= ~MMC_VDD_165_195
;
2917 mmc
->ocr_avail_mmc
= ocr_avail
;
2918 if (host
->ocr_avail_mmc
)
2919 mmc
->ocr_avail_mmc
&= host
->ocr_avail_mmc
;
2921 if (mmc
->ocr_avail
== 0) {
2922 pr_err("%s: Hardware doesn't report any "
2923 "support voltages.\n", mmc_hostname(mmc
));
2927 spin_lock_init(&host
->lock
);
2930 * Maximum number of segments. Depends on if the hardware
2931 * can do scatter/gather or not.
2933 if (host
->flags
& SDHCI_USE_ADMA
)
2934 mmc
->max_segs
= 128;
2935 else if (host
->flags
& SDHCI_USE_SDMA
)
2938 mmc
->max_segs
= 128;
2941 * Maximum number of sectors in one transfer. Limited by DMA boundary
2944 mmc
->max_req_size
= 524288;
2947 * Maximum segment size. Could be one segment with the maximum number
2948 * of bytes. When doing hardware scatter/gather, each entry cannot
2949 * be larger than 64 KiB though.
2951 if (host
->flags
& SDHCI_USE_ADMA
) {
2952 if (host
->quirks
& SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC
)
2953 mmc
->max_seg_size
= 65535;
2955 mmc
->max_seg_size
= 65536;
2957 mmc
->max_seg_size
= mmc
->max_req_size
;
2961 * Maximum block size. This varies from controller to controller and
2962 * is specified in the capabilities register.
2964 if (host
->quirks
& SDHCI_QUIRK_FORCE_BLK_SZ_2048
) {
2965 mmc
->max_blk_size
= 2;
2967 mmc
->max_blk_size
= (caps
[0] & SDHCI_MAX_BLOCK_MASK
) >>
2968 SDHCI_MAX_BLOCK_SHIFT
;
2969 if (mmc
->max_blk_size
>= 3) {
2970 pr_warning("%s: Invalid maximum block size, "
2971 "assuming 512 bytes\n", mmc_hostname(mmc
));
2972 mmc
->max_blk_size
= 0;
2976 mmc
->max_blk_size
= 512 << mmc
->max_blk_size
;
2979 * Maximum block count.
2981 mmc
->max_blk_count
= (host
->quirks
& SDHCI_QUIRK_NO_MULTIBLOCK
) ? 1 : 65535;
2986 tasklet_init(&host
->card_tasklet
,
2987 sdhci_tasklet_card
, (unsigned long)host
);
2988 tasklet_init(&host
->finish_tasklet
,
2989 sdhci_tasklet_finish
, (unsigned long)host
);
2991 setup_timer(&host
->timer
, sdhci_timeout_timer
, (unsigned long)host
);
2993 if (host
->version
>= SDHCI_SPEC_300
) {
2994 init_waitqueue_head(&host
->buf_ready_int
);
2996 /* Initialize re-tuning timer */
2997 init_timer(&host
->tuning_timer
);
2998 host
->tuning_timer
.data
= (unsigned long)host
;
2999 host
->tuning_timer
.function
= sdhci_tuning_timer
;
3002 ret
= request_irq(host
->irq
, sdhci_irq
, IRQF_SHARED
,
3003 mmc_hostname(mmc
), host
);
3007 host
->vmmc
= regulator_get(mmc_dev(mmc
), "vmmc");
3008 if (IS_ERR(host
->vmmc
)) {
3009 pr_info("%s: no vmmc regulator found\n", mmc_hostname(mmc
));
3013 sdhci_init(host
, 0);
3015 #ifdef CONFIG_MMC_DEBUG
3016 sdhci_dumpregs(host
);
3019 #ifdef SDHCI_USE_LEDS_CLASS
3020 snprintf(host
->led_name
, sizeof(host
->led_name
),
3021 "%s::", mmc_hostname(mmc
));
3022 host
->led
.name
= host
->led_name
;
3023 host
->led
.brightness
= LED_OFF
;
3024 host
->led
.default_trigger
= mmc_hostname(mmc
);
3025 host
->led
.brightness_set
= sdhci_led_control
;
3027 ret
= led_classdev_register(mmc_dev(mmc
), &host
->led
);
3036 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
3037 mmc_hostname(mmc
), host
->hw_name
, dev_name(mmc_dev(mmc
)),
3038 (host
->flags
& SDHCI_USE_ADMA
) ? "ADMA" :
3039 (host
->flags
& SDHCI_USE_SDMA
) ? "DMA" : "PIO");
3041 sdhci_enable_card_detection(host
);
3045 #ifdef SDHCI_USE_LEDS_CLASS
3047 sdhci_reset(host
, SDHCI_RESET_ALL
);
3048 free_irq(host
->irq
, host
);
3051 tasklet_kill(&host
->card_tasklet
);
3052 tasklet_kill(&host
->finish_tasklet
);
3057 EXPORT_SYMBOL_GPL(sdhci_add_host
);
3059 void sdhci_remove_host(struct sdhci_host
*host
, int dead
)
3061 unsigned long flags
;
3064 spin_lock_irqsave(&host
->lock
, flags
);
3066 host
->flags
|= SDHCI_DEVICE_DEAD
;
3069 pr_err("%s: Controller removed during "
3070 " transfer!\n", mmc_hostname(host
->mmc
));
3072 host
->mrq
->cmd
->error
= -ENOMEDIUM
;
3073 tasklet_schedule(&host
->finish_tasklet
);
3076 spin_unlock_irqrestore(&host
->lock
, flags
);
3079 sdhci_disable_card_detection(host
);
3081 mmc_remove_host(host
->mmc
);
3083 #ifdef SDHCI_USE_LEDS_CLASS
3084 led_classdev_unregister(&host
->led
);
3088 sdhci_reset(host
, SDHCI_RESET_ALL
);
3090 free_irq(host
->irq
, host
);
3092 del_timer_sync(&host
->timer
);
3093 if (host
->version
>= SDHCI_SPEC_300
)
3094 del_timer_sync(&host
->tuning_timer
);
3096 tasklet_kill(&host
->card_tasklet
);
3097 tasklet_kill(&host
->finish_tasklet
);
3100 regulator_put(host
->vmmc
);
3102 kfree(host
->adma_desc
);
3103 kfree(host
->align_buffer
);
3105 host
->adma_desc
= NULL
;
3106 host
->align_buffer
= NULL
;
3109 EXPORT_SYMBOL_GPL(sdhci_remove_host
);
3111 void sdhci_free_host(struct sdhci_host
*host
)
3113 mmc_free_host(host
->mmc
);
3116 EXPORT_SYMBOL_GPL(sdhci_free_host
);
3118 /*****************************************************************************\
3120 * Driver init/exit *
3122 \*****************************************************************************/
3124 static int __init
sdhci_drv_init(void)
3127 ": Secure Digital Host Controller Interface driver\n");
3128 pr_info(DRIVER_NAME
": Copyright(c) Pierre Ossman\n");
3133 static void __exit
sdhci_drv_exit(void)
3137 module_init(sdhci_drv_init
);
3138 module_exit(sdhci_drv_exit
);
3140 module_param(debug_quirks
, uint
, 0444);
3141 module_param(debug_quirks2
, uint
, 0444);
3143 MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
3144 MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
3145 MODULE_LICENSE("GPL");
3147 MODULE_PARM_DESC(debug_quirks
, "Force certain quirks.");
3148 MODULE_PARM_DESC(debug_quirks2
, "Force certain other quirks.");