2 * linux/drivers/mmc/host/sdhci.c - Secure Digital Host Controller Interface driver
4 * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
11 * Thanks to the following companies for their support:
13 * - JMicron (hardware and technical support)
16 #include <linux/delay.h>
17 #include <linux/highmem.h>
19 #include <linux/dma-mapping.h>
20 #include <linux/slab.h>
21 #include <linux/scatterlist.h>
23 #include <linux/leds.h>
25 #include <linux/mmc/host.h>
29 #define DRIVER_NAME "sdhci"
31 #define DBG(f, x...) \
32 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
34 #if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
35 defined(CONFIG_MMC_SDHCI_MODULE))
36 #define SDHCI_USE_LEDS_CLASS
39 static unsigned int debug_quirks
= 0;
41 static void sdhci_prepare_data(struct sdhci_host
*, struct mmc_data
*);
42 static void sdhci_finish_data(struct sdhci_host
*);
44 static void sdhci_send_command(struct sdhci_host
*, struct mmc_command
*);
45 static void sdhci_finish_command(struct sdhci_host
*);
47 static void sdhci_dumpregs(struct sdhci_host
*host
)
49 printk(KERN_DEBUG DRIVER_NAME
": ============== REGISTER DUMP ==============\n");
51 printk(KERN_DEBUG DRIVER_NAME
": Sys addr: 0x%08x | Version: 0x%08x\n",
52 sdhci_readl(host
, SDHCI_DMA_ADDRESS
),
53 sdhci_readw(host
, SDHCI_HOST_VERSION
));
54 printk(KERN_DEBUG DRIVER_NAME
": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
55 sdhci_readw(host
, SDHCI_BLOCK_SIZE
),
56 sdhci_readw(host
, SDHCI_BLOCK_COUNT
));
57 printk(KERN_DEBUG DRIVER_NAME
": Argument: 0x%08x | Trn mode: 0x%08x\n",
58 sdhci_readl(host
, SDHCI_ARGUMENT
),
59 sdhci_readw(host
, SDHCI_TRANSFER_MODE
));
60 printk(KERN_DEBUG DRIVER_NAME
": Present: 0x%08x | Host ctl: 0x%08x\n",
61 sdhci_readl(host
, SDHCI_PRESENT_STATE
),
62 sdhci_readb(host
, SDHCI_HOST_CONTROL
));
63 printk(KERN_DEBUG DRIVER_NAME
": Power: 0x%08x | Blk gap: 0x%08x\n",
64 sdhci_readb(host
, SDHCI_POWER_CONTROL
),
65 sdhci_readb(host
, SDHCI_BLOCK_GAP_CONTROL
));
66 printk(KERN_DEBUG DRIVER_NAME
": Wake-up: 0x%08x | Clock: 0x%08x\n",
67 sdhci_readb(host
, SDHCI_WAKE_UP_CONTROL
),
68 sdhci_readw(host
, SDHCI_CLOCK_CONTROL
));
69 printk(KERN_DEBUG DRIVER_NAME
": Timeout: 0x%08x | Int stat: 0x%08x\n",
70 sdhci_readb(host
, SDHCI_TIMEOUT_CONTROL
),
71 sdhci_readl(host
, SDHCI_INT_STATUS
));
72 printk(KERN_DEBUG DRIVER_NAME
": Int enab: 0x%08x | Sig enab: 0x%08x\n",
73 sdhci_readl(host
, SDHCI_INT_ENABLE
),
74 sdhci_readl(host
, SDHCI_SIGNAL_ENABLE
));
75 printk(KERN_DEBUG DRIVER_NAME
": AC12 err: 0x%08x | Slot int: 0x%08x\n",
76 sdhci_readw(host
, SDHCI_ACMD12_ERR
),
77 sdhci_readw(host
, SDHCI_SLOT_INT_STATUS
));
78 printk(KERN_DEBUG DRIVER_NAME
": Caps: 0x%08x | Max curr: 0x%08x\n",
79 sdhci_readl(host
, SDHCI_CAPABILITIES
),
80 sdhci_readl(host
, SDHCI_MAX_CURRENT
));
82 if (host
->flags
& SDHCI_USE_ADMA
)
83 printk(KERN_DEBUG DRIVER_NAME
": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
84 readl(host
->ioaddr
+ SDHCI_ADMA_ERROR
),
85 readl(host
->ioaddr
+ SDHCI_ADMA_ADDRESS
));
87 printk(KERN_DEBUG DRIVER_NAME
": ===========================================\n");
90 /*****************************************************************************\
92 * Low level functions *
94 \*****************************************************************************/
96 static void sdhci_clear_set_irqs(struct sdhci_host
*host
, u32 clear
, u32 set
)
100 ier
= sdhci_readl(host
, SDHCI_INT_ENABLE
);
103 sdhci_writel(host
, ier
, SDHCI_INT_ENABLE
);
104 sdhci_writel(host
, ier
, SDHCI_SIGNAL_ENABLE
);
107 static void sdhci_unmask_irqs(struct sdhci_host
*host
, u32 irqs
)
109 sdhci_clear_set_irqs(host
, 0, irqs
);
112 static void sdhci_mask_irqs(struct sdhci_host
*host
, u32 irqs
)
114 sdhci_clear_set_irqs(host
, irqs
, 0);
117 static void sdhci_set_card_detection(struct sdhci_host
*host
, bool enable
)
119 u32 irqs
= SDHCI_INT_CARD_REMOVE
| SDHCI_INT_CARD_INSERT
;
121 if (host
->quirks
& SDHCI_QUIRK_BROKEN_CARD_DETECTION
)
125 sdhci_unmask_irqs(host
, irqs
);
127 sdhci_mask_irqs(host
, irqs
);
130 static void sdhci_enable_card_detection(struct sdhci_host
*host
)
132 sdhci_set_card_detection(host
, true);
135 static void sdhci_disable_card_detection(struct sdhci_host
*host
)
137 sdhci_set_card_detection(host
, false);
140 static void sdhci_reset(struct sdhci_host
*host
, u8 mask
)
142 unsigned long timeout
;
143 u32
uninitialized_var(ier
);
145 if (host
->quirks
& SDHCI_QUIRK_NO_CARD_NO_RESET
) {
146 if (!(sdhci_readl(host
, SDHCI_PRESENT_STATE
) &
151 if (host
->quirks
& SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET
)
152 ier
= sdhci_readl(host
, SDHCI_INT_ENABLE
);
154 sdhci_writeb(host
, mask
, SDHCI_SOFTWARE_RESET
);
156 if (mask
& SDHCI_RESET_ALL
)
159 /* Wait max 100 ms */
162 /* hw clears the bit when it's done */
163 while (sdhci_readb(host
, SDHCI_SOFTWARE_RESET
) & mask
) {
165 printk(KERN_ERR
"%s: Reset 0x%x never completed.\n",
166 mmc_hostname(host
->mmc
), (int)mask
);
167 sdhci_dumpregs(host
);
174 if (host
->quirks
& SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET
)
175 sdhci_clear_set_irqs(host
, SDHCI_INT_ALL_MASK
, ier
);
178 static void sdhci_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
);
180 static void sdhci_init(struct sdhci_host
*host
, int soft
)
183 sdhci_reset(host
, SDHCI_RESET_CMD
|SDHCI_RESET_DATA
);
185 sdhci_reset(host
, SDHCI_RESET_ALL
);
187 sdhci_clear_set_irqs(host
, SDHCI_INT_ALL_MASK
,
188 SDHCI_INT_BUS_POWER
| SDHCI_INT_DATA_END_BIT
|
189 SDHCI_INT_DATA_CRC
| SDHCI_INT_DATA_TIMEOUT
| SDHCI_INT_INDEX
|
190 SDHCI_INT_END_BIT
| SDHCI_INT_CRC
| SDHCI_INT_TIMEOUT
|
191 SDHCI_INT_DATA_END
| SDHCI_INT_RESPONSE
);
194 /* force clock reconfiguration */
196 sdhci_set_ios(host
->mmc
, &host
->mmc
->ios
);
200 static void sdhci_reinit(struct sdhci_host
*host
)
203 sdhci_enable_card_detection(host
);
206 static void sdhci_activate_led(struct sdhci_host
*host
)
210 ctrl
= sdhci_readb(host
, SDHCI_HOST_CONTROL
);
211 ctrl
|= SDHCI_CTRL_LED
;
212 sdhci_writeb(host
, ctrl
, SDHCI_HOST_CONTROL
);
215 static void sdhci_deactivate_led(struct sdhci_host
*host
)
219 ctrl
= sdhci_readb(host
, SDHCI_HOST_CONTROL
);
220 ctrl
&= ~SDHCI_CTRL_LED
;
221 sdhci_writeb(host
, ctrl
, SDHCI_HOST_CONTROL
);
224 #ifdef SDHCI_USE_LEDS_CLASS
225 static void sdhci_led_control(struct led_classdev
*led
,
226 enum led_brightness brightness
)
228 struct sdhci_host
*host
= container_of(led
, struct sdhci_host
, led
);
231 spin_lock_irqsave(&host
->lock
, flags
);
233 if (brightness
== LED_OFF
)
234 sdhci_deactivate_led(host
);
236 sdhci_activate_led(host
);
238 spin_unlock_irqrestore(&host
->lock
, flags
);
242 /*****************************************************************************\
246 \*****************************************************************************/
248 static void sdhci_read_block_pio(struct sdhci_host
*host
)
251 size_t blksize
, len
, chunk
;
252 u32
uninitialized_var(scratch
);
255 DBG("PIO reading\n");
257 blksize
= host
->data
->blksz
;
260 local_irq_save(flags
);
263 if (!sg_miter_next(&host
->sg_miter
))
266 len
= min(host
->sg_miter
.length
, blksize
);
269 host
->sg_miter
.consumed
= len
;
271 buf
= host
->sg_miter
.addr
;
275 scratch
= sdhci_readl(host
, SDHCI_BUFFER
);
279 *buf
= scratch
& 0xFF;
288 sg_miter_stop(&host
->sg_miter
);
290 local_irq_restore(flags
);
293 static void sdhci_write_block_pio(struct sdhci_host
*host
)
296 size_t blksize
, len
, chunk
;
300 DBG("PIO writing\n");
302 blksize
= host
->data
->blksz
;
306 local_irq_save(flags
);
309 if (!sg_miter_next(&host
->sg_miter
))
312 len
= min(host
->sg_miter
.length
, blksize
);
315 host
->sg_miter
.consumed
= len
;
317 buf
= host
->sg_miter
.addr
;
320 scratch
|= (u32
)*buf
<< (chunk
* 8);
326 if ((chunk
== 4) || ((len
== 0) && (blksize
== 0))) {
327 sdhci_writel(host
, scratch
, SDHCI_BUFFER
);
334 sg_miter_stop(&host
->sg_miter
);
336 local_irq_restore(flags
);
339 static void sdhci_transfer_pio(struct sdhci_host
*host
)
345 if (host
->blocks
== 0)
348 if (host
->data
->flags
& MMC_DATA_READ
)
349 mask
= SDHCI_DATA_AVAILABLE
;
351 mask
= SDHCI_SPACE_AVAILABLE
;
354 * Some controllers (JMicron JMB38x) mess up the buffer bits
355 * for transfers < 4 bytes. As long as it is just one block,
356 * we can ignore the bits.
358 if ((host
->quirks
& SDHCI_QUIRK_BROKEN_SMALL_PIO
) &&
359 (host
->data
->blocks
== 1))
362 while (sdhci_readl(host
, SDHCI_PRESENT_STATE
) & mask
) {
363 if (host
->quirks
& SDHCI_QUIRK_PIO_NEEDS_DELAY
)
366 if (host
->data
->flags
& MMC_DATA_READ
)
367 sdhci_read_block_pio(host
);
369 sdhci_write_block_pio(host
);
372 if (host
->blocks
== 0)
376 DBG("PIO transfer complete.\n");
379 static char *sdhci_kmap_atomic(struct scatterlist
*sg
, unsigned long *flags
)
381 local_irq_save(*flags
);
382 return kmap_atomic(sg_page(sg
), KM_BIO_SRC_IRQ
) + sg
->offset
;
385 static void sdhci_kunmap_atomic(void *buffer
, unsigned long *flags
)
387 kunmap_atomic(buffer
, KM_BIO_SRC_IRQ
);
388 local_irq_restore(*flags
);
391 static void sdhci_set_adma_desc(u8
*desc
, u32 addr
, int len
, unsigned cmd
)
393 __le32
*dataddr
= (__le32 __force
*)(desc
+ 4);
394 __le16
*cmdlen
= (__le16 __force
*)desc
;
396 /* SDHCI specification says ADMA descriptors should be 4 byte
397 * aligned, so using 16 or 32bit operations should be safe. */
399 cmdlen
[0] = cpu_to_le16(cmd
);
400 cmdlen
[1] = cpu_to_le16(len
);
402 dataddr
[0] = cpu_to_le32(addr
);
405 static int sdhci_adma_table_pre(struct sdhci_host
*host
,
406 struct mmc_data
*data
)
413 dma_addr_t align_addr
;
416 struct scatterlist
*sg
;
422 * The spec does not specify endianness of descriptor table.
423 * We currently guess that it is LE.
426 if (data
->flags
& MMC_DATA_READ
)
427 direction
= DMA_FROM_DEVICE
;
429 direction
= DMA_TO_DEVICE
;
432 * The ADMA descriptor table is mapped further down as we
433 * need to fill it with data first.
436 host
->align_addr
= dma_map_single(mmc_dev(host
->mmc
),
437 host
->align_buffer
, 128 * 4, direction
);
438 if (dma_mapping_error(mmc_dev(host
->mmc
), host
->align_addr
))
440 BUG_ON(host
->align_addr
& 0x3);
442 host
->sg_count
= dma_map_sg(mmc_dev(host
->mmc
),
443 data
->sg
, data
->sg_len
, direction
);
444 if (host
->sg_count
== 0)
447 desc
= host
->adma_desc
;
448 align
= host
->align_buffer
;
450 align_addr
= host
->align_addr
;
452 for_each_sg(data
->sg
, sg
, host
->sg_count
, i
) {
453 addr
= sg_dma_address(sg
);
454 len
= sg_dma_len(sg
);
457 * The SDHCI specification states that ADMA
458 * addresses must be 32-bit aligned. If they
459 * aren't, then we use a bounce buffer for
460 * the (up to three) bytes that screw up the
463 offset
= (4 - (addr
& 0x3)) & 0x3;
465 if (data
->flags
& MMC_DATA_WRITE
) {
466 buffer
= sdhci_kmap_atomic(sg
, &flags
);
467 WARN_ON(((long)buffer
& PAGE_MASK
) > (PAGE_SIZE
- 3));
468 memcpy(align
, buffer
, offset
);
469 sdhci_kunmap_atomic(buffer
, &flags
);
473 sdhci_set_adma_desc(desc
, align_addr
, offset
, 0x21);
475 BUG_ON(offset
> 65536);
489 sdhci_set_adma_desc(desc
, addr
, len
, 0x21);
493 * If this triggers then we have a calculation bug
496 WARN_ON((desc
- host
->adma_desc
) > (128 * 2 + 1) * 4);
499 if (host
->quirks
& SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
) {
501 * Mark the last descriptor as the terminating descriptor
503 if (desc
!= host
->adma_desc
) {
505 desc
[0] |= 0x2; /* end */
509 * Add a terminating entry.
512 /* nop, end, valid */
513 sdhci_set_adma_desc(desc
, 0, 0, 0x3);
517 * Resync align buffer as we might have changed it.
519 if (data
->flags
& MMC_DATA_WRITE
) {
520 dma_sync_single_for_device(mmc_dev(host
->mmc
),
521 host
->align_addr
, 128 * 4, direction
);
524 host
->adma_addr
= dma_map_single(mmc_dev(host
->mmc
),
525 host
->adma_desc
, (128 * 2 + 1) * 4, DMA_TO_DEVICE
);
526 if (dma_mapping_error(mmc_dev(host
->mmc
), host
->adma_addr
))
528 BUG_ON(host
->adma_addr
& 0x3);
533 dma_unmap_sg(mmc_dev(host
->mmc
), data
->sg
,
534 data
->sg_len
, direction
);
536 dma_unmap_single(mmc_dev(host
->mmc
), host
->align_addr
,
542 static void sdhci_adma_table_post(struct sdhci_host
*host
,
543 struct mmc_data
*data
)
547 struct scatterlist
*sg
;
553 if (data
->flags
& MMC_DATA_READ
)
554 direction
= DMA_FROM_DEVICE
;
556 direction
= DMA_TO_DEVICE
;
558 dma_unmap_single(mmc_dev(host
->mmc
), host
->adma_addr
,
559 (128 * 2 + 1) * 4, DMA_TO_DEVICE
);
561 dma_unmap_single(mmc_dev(host
->mmc
), host
->align_addr
,
564 if (data
->flags
& MMC_DATA_READ
) {
565 dma_sync_sg_for_cpu(mmc_dev(host
->mmc
), data
->sg
,
566 data
->sg_len
, direction
);
568 align
= host
->align_buffer
;
570 for_each_sg(data
->sg
, sg
, host
->sg_count
, i
) {
571 if (sg_dma_address(sg
) & 0x3) {
572 size
= 4 - (sg_dma_address(sg
) & 0x3);
574 buffer
= sdhci_kmap_atomic(sg
, &flags
);
575 WARN_ON(((long)buffer
& PAGE_MASK
) > (PAGE_SIZE
- 3));
576 memcpy(buffer
, align
, size
);
577 sdhci_kunmap_atomic(buffer
, &flags
);
584 dma_unmap_sg(mmc_dev(host
->mmc
), data
->sg
,
585 data
->sg_len
, direction
);
588 static u8
sdhci_calc_timeout(struct sdhci_host
*host
, struct mmc_data
*data
)
591 unsigned target_timeout
, current_timeout
;
594 * If the host controller provides us with an incorrect timeout
595 * value, just skip the check and use 0xE. The hardware may take
596 * longer to time out, but that's much better than having a too-short
599 if (host
->quirks
& SDHCI_QUIRK_BROKEN_TIMEOUT_VAL
)
603 target_timeout
= data
->timeout_ns
/ 1000 +
604 data
->timeout_clks
/ host
->clock
;
606 if (host
->quirks
& SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK
)
607 host
->timeout_clk
= host
->clock
/ 1000;
610 * Figure out needed cycles.
611 * We do this in steps in order to fit inside a 32 bit int.
612 * The first step is the minimum timeout, which will have a
613 * minimum resolution of 6 bits:
614 * (1) 2^13*1000 > 2^22,
615 * (2) host->timeout_clk < 2^16
620 current_timeout
= (1 << 13) * 1000 / host
->timeout_clk
;
621 while (current_timeout
< target_timeout
) {
623 current_timeout
<<= 1;
629 printk(KERN_WARNING
"%s: Too large timeout requested!\n",
630 mmc_hostname(host
->mmc
));
637 static void sdhci_set_transfer_irqs(struct sdhci_host
*host
)
639 u32 pio_irqs
= SDHCI_INT_DATA_AVAIL
| SDHCI_INT_SPACE_AVAIL
;
640 u32 dma_irqs
= SDHCI_INT_DMA_END
| SDHCI_INT_ADMA_ERROR
;
642 if (host
->flags
& SDHCI_REQ_USE_DMA
)
643 sdhci_clear_set_irqs(host
, pio_irqs
, dma_irqs
);
645 sdhci_clear_set_irqs(host
, dma_irqs
, pio_irqs
);
648 static void sdhci_prepare_data(struct sdhci_host
*host
, struct mmc_data
*data
)
660 BUG_ON(data
->blksz
* data
->blocks
> 524288);
661 BUG_ON(data
->blksz
> host
->mmc
->max_blk_size
);
662 BUG_ON(data
->blocks
> 65535);
665 host
->data_early
= 0;
667 count
= sdhci_calc_timeout(host
, data
);
668 sdhci_writeb(host
, count
, SDHCI_TIMEOUT_CONTROL
);
670 if (host
->flags
& (SDHCI_USE_SDMA
| SDHCI_USE_ADMA
))
671 host
->flags
|= SDHCI_REQ_USE_DMA
;
674 * FIXME: This doesn't account for merging when mapping the
677 if (host
->flags
& SDHCI_REQ_USE_DMA
) {
679 struct scatterlist
*sg
;
682 if (host
->flags
& SDHCI_USE_ADMA
) {
683 if (host
->quirks
& SDHCI_QUIRK_32BIT_ADMA_SIZE
)
686 if (host
->quirks
& SDHCI_QUIRK_32BIT_DMA_SIZE
)
690 if (unlikely(broken
)) {
691 for_each_sg(data
->sg
, sg
, data
->sg_len
, i
) {
692 if (sg
->length
& 0x3) {
693 DBG("Reverting to PIO because of "
694 "transfer size (%d)\n",
696 host
->flags
&= ~SDHCI_REQ_USE_DMA
;
704 * The assumption here being that alignment is the same after
705 * translation to device address space.
707 if (host
->flags
& SDHCI_REQ_USE_DMA
) {
709 struct scatterlist
*sg
;
712 if (host
->flags
& SDHCI_USE_ADMA
) {
714 * As we use 3 byte chunks to work around
715 * alignment problems, we need to check this
718 if (host
->quirks
& SDHCI_QUIRK_32BIT_ADMA_SIZE
)
721 if (host
->quirks
& SDHCI_QUIRK_32BIT_DMA_ADDR
)
725 if (unlikely(broken
)) {
726 for_each_sg(data
->sg
, sg
, data
->sg_len
, i
) {
727 if (sg
->offset
& 0x3) {
728 DBG("Reverting to PIO because of "
730 host
->flags
&= ~SDHCI_REQ_USE_DMA
;
737 if (host
->flags
& SDHCI_REQ_USE_DMA
) {
738 if (host
->flags
& SDHCI_USE_ADMA
) {
739 ret
= sdhci_adma_table_pre(host
, data
);
742 * This only happens when someone fed
743 * us an invalid request.
746 host
->flags
&= ~SDHCI_REQ_USE_DMA
;
748 sdhci_writel(host
, host
->adma_addr
,
754 sg_cnt
= dma_map_sg(mmc_dev(host
->mmc
),
755 data
->sg
, data
->sg_len
,
756 (data
->flags
& MMC_DATA_READ
) ?
761 * This only happens when someone fed
762 * us an invalid request.
765 host
->flags
&= ~SDHCI_REQ_USE_DMA
;
767 WARN_ON(sg_cnt
!= 1);
768 sdhci_writel(host
, sg_dma_address(data
->sg
),
775 * Always adjust the DMA selection as some controllers
776 * (e.g. JMicron) can't do PIO properly when the selection
779 if (host
->version
>= SDHCI_SPEC_200
) {
780 ctrl
= sdhci_readb(host
, SDHCI_HOST_CONTROL
);
781 ctrl
&= ~SDHCI_CTRL_DMA_MASK
;
782 if ((host
->flags
& SDHCI_REQ_USE_DMA
) &&
783 (host
->flags
& SDHCI_USE_ADMA
))
784 ctrl
|= SDHCI_CTRL_ADMA32
;
786 ctrl
|= SDHCI_CTRL_SDMA
;
787 sdhci_writeb(host
, ctrl
, SDHCI_HOST_CONTROL
);
790 if (!(host
->flags
& SDHCI_REQ_USE_DMA
)) {
793 flags
= SG_MITER_ATOMIC
;
794 if (host
->data
->flags
& MMC_DATA_READ
)
795 flags
|= SG_MITER_TO_SG
;
797 flags
|= SG_MITER_FROM_SG
;
798 sg_miter_start(&host
->sg_miter
, data
->sg
, data
->sg_len
, flags
);
799 host
->blocks
= data
->blocks
;
802 sdhci_set_transfer_irqs(host
);
804 /* We do not handle DMA boundaries, so set it to max (512 KiB) */
805 sdhci_writew(host
, SDHCI_MAKE_BLKSZ(7, data
->blksz
), SDHCI_BLOCK_SIZE
);
806 sdhci_writew(host
, data
->blocks
, SDHCI_BLOCK_COUNT
);
809 static void sdhci_set_transfer_mode(struct sdhci_host
*host
,
810 struct mmc_data
*data
)
817 WARN_ON(!host
->data
);
819 mode
= SDHCI_TRNS_BLK_CNT_EN
;
820 if (data
->blocks
> 1)
821 mode
|= SDHCI_TRNS_MULTI
;
822 if (data
->flags
& MMC_DATA_READ
)
823 mode
|= SDHCI_TRNS_READ
;
824 if (host
->flags
& SDHCI_REQ_USE_DMA
)
825 mode
|= SDHCI_TRNS_DMA
;
827 sdhci_writew(host
, mode
, SDHCI_TRANSFER_MODE
);
830 static void sdhci_finish_data(struct sdhci_host
*host
)
832 struct mmc_data
*data
;
839 if (host
->flags
& SDHCI_REQ_USE_DMA
) {
840 if (host
->flags
& SDHCI_USE_ADMA
)
841 sdhci_adma_table_post(host
, data
);
843 dma_unmap_sg(mmc_dev(host
->mmc
), data
->sg
,
844 data
->sg_len
, (data
->flags
& MMC_DATA_READ
) ?
845 DMA_FROM_DEVICE
: DMA_TO_DEVICE
);
850 * The specification states that the block count register must
851 * be updated, but it does not specify at what point in the
852 * data flow. That makes the register entirely useless to read
853 * back so we have to assume that nothing made it to the card
854 * in the event of an error.
857 data
->bytes_xfered
= 0;
859 data
->bytes_xfered
= data
->blksz
* data
->blocks
;
863 * The controller needs a reset of internal state machines
864 * upon error conditions.
867 sdhci_reset(host
, SDHCI_RESET_CMD
);
868 sdhci_reset(host
, SDHCI_RESET_DATA
);
871 sdhci_send_command(host
, data
->stop
);
873 tasklet_schedule(&host
->finish_tasklet
);
876 static void sdhci_send_command(struct sdhci_host
*host
, struct mmc_command
*cmd
)
880 unsigned long timeout
;
887 mask
= SDHCI_CMD_INHIBIT
;
888 if ((cmd
->data
!= NULL
) || (cmd
->flags
& MMC_RSP_BUSY
))
889 mask
|= SDHCI_DATA_INHIBIT
;
891 /* We shouldn't wait for data inihibit for stop commands, even
892 though they might use busy signaling */
893 if (host
->mrq
->data
&& (cmd
== host
->mrq
->data
->stop
))
894 mask
&= ~SDHCI_DATA_INHIBIT
;
896 while (sdhci_readl(host
, SDHCI_PRESENT_STATE
) & mask
) {
898 printk(KERN_ERR
"%s: Controller never released "
899 "inhibit bit(s).\n", mmc_hostname(host
->mmc
));
900 sdhci_dumpregs(host
);
902 tasklet_schedule(&host
->finish_tasklet
);
909 mod_timer(&host
->timer
, jiffies
+ 10 * HZ
);
913 sdhci_prepare_data(host
, cmd
->data
);
915 sdhci_writel(host
, cmd
->arg
, SDHCI_ARGUMENT
);
917 sdhci_set_transfer_mode(host
, cmd
->data
);
919 if ((cmd
->flags
& MMC_RSP_136
) && (cmd
->flags
& MMC_RSP_BUSY
)) {
920 printk(KERN_ERR
"%s: Unsupported response type!\n",
921 mmc_hostname(host
->mmc
));
922 cmd
->error
= -EINVAL
;
923 tasklet_schedule(&host
->finish_tasklet
);
927 if (!(cmd
->flags
& MMC_RSP_PRESENT
))
928 flags
= SDHCI_CMD_RESP_NONE
;
929 else if (cmd
->flags
& MMC_RSP_136
)
930 flags
= SDHCI_CMD_RESP_LONG
;
931 else if (cmd
->flags
& MMC_RSP_BUSY
)
932 flags
= SDHCI_CMD_RESP_SHORT_BUSY
;
934 flags
= SDHCI_CMD_RESP_SHORT
;
936 if (cmd
->flags
& MMC_RSP_CRC
)
937 flags
|= SDHCI_CMD_CRC
;
938 if (cmd
->flags
& MMC_RSP_OPCODE
)
939 flags
|= SDHCI_CMD_INDEX
;
941 flags
|= SDHCI_CMD_DATA
;
943 sdhci_writew(host
, SDHCI_MAKE_CMD(cmd
->opcode
, flags
), SDHCI_COMMAND
);
946 static void sdhci_finish_command(struct sdhci_host
*host
)
950 BUG_ON(host
->cmd
== NULL
);
952 if (host
->cmd
->flags
& MMC_RSP_PRESENT
) {
953 if (host
->cmd
->flags
& MMC_RSP_136
) {
954 /* CRC is stripped so we need to do some shifting. */
955 for (i
= 0;i
< 4;i
++) {
956 host
->cmd
->resp
[i
] = sdhci_readl(host
,
957 SDHCI_RESPONSE
+ (3-i
)*4) << 8;
959 host
->cmd
->resp
[i
] |=
961 SDHCI_RESPONSE
+ (3-i
)*4-1);
964 host
->cmd
->resp
[0] = sdhci_readl(host
, SDHCI_RESPONSE
);
968 host
->cmd
->error
= 0;
970 if (host
->data
&& host
->data_early
)
971 sdhci_finish_data(host
);
973 if (!host
->cmd
->data
)
974 tasklet_schedule(&host
->finish_tasklet
);
979 static void sdhci_set_clock(struct sdhci_host
*host
, unsigned int clock
)
983 unsigned long timeout
;
985 if (clock
== host
->clock
)
988 if (host
->ops
->set_clock
) {
989 host
->ops
->set_clock(host
, clock
);
990 if (host
->quirks
& SDHCI_QUIRK_NONSTANDARD_CLOCK
)
994 sdhci_writew(host
, 0, SDHCI_CLOCK_CONTROL
);
999 for (div
= 1;div
< 256;div
*= 2) {
1000 if ((host
->max_clk
/ div
) <= clock
)
1005 clk
= div
<< SDHCI_DIVIDER_SHIFT
;
1006 clk
|= SDHCI_CLOCK_INT_EN
;
1007 sdhci_writew(host
, clk
, SDHCI_CLOCK_CONTROL
);
1009 /* Wait max 20 ms */
1011 while (!((clk
= sdhci_readw(host
, SDHCI_CLOCK_CONTROL
))
1012 & SDHCI_CLOCK_INT_STABLE
)) {
1014 printk(KERN_ERR
"%s: Internal clock never "
1015 "stabilised.\n", mmc_hostname(host
->mmc
));
1016 sdhci_dumpregs(host
);
1023 clk
|= SDHCI_CLOCK_CARD_EN
;
1024 sdhci_writew(host
, clk
, SDHCI_CLOCK_CONTROL
);
1027 host
->clock
= clock
;
1030 static void sdhci_set_power(struct sdhci_host
*host
, unsigned short power
)
1034 if (power
== (unsigned short)-1)
1037 switch (1 << power
) {
1038 case MMC_VDD_165_195
:
1039 pwr
= SDHCI_POWER_180
;
1043 pwr
= SDHCI_POWER_300
;
1047 pwr
= SDHCI_POWER_330
;
1054 if (host
->pwr
== pwr
)
1060 sdhci_writeb(host
, 0, SDHCI_POWER_CONTROL
);
1065 * Spec says that we should clear the power reg before setting
1066 * a new value. Some controllers don't seem to like this though.
1068 if (!(host
->quirks
& SDHCI_QUIRK_SINGLE_POWER_WRITE
))
1069 sdhci_writeb(host
, 0, SDHCI_POWER_CONTROL
);
1072 * At least the Marvell CaFe chip gets confused if we set the voltage
1073 * and set turn on power at the same time, so set the voltage first.
1075 if (host
->quirks
& SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER
)
1076 sdhci_writeb(host
, pwr
, SDHCI_POWER_CONTROL
);
1078 pwr
|= SDHCI_POWER_ON
;
1080 sdhci_writeb(host
, pwr
, SDHCI_POWER_CONTROL
);
1083 * Some controllers need an extra 10ms delay of 10ms before they
1084 * can apply clock after applying power
1086 if (host
->quirks
& SDHCI_QUIRK_DELAY_AFTER_POWER
)
1090 /*****************************************************************************\
1094 \*****************************************************************************/
1096 static void sdhci_request(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
1098 struct sdhci_host
*host
;
1100 unsigned long flags
;
1102 host
= mmc_priv(mmc
);
1104 spin_lock_irqsave(&host
->lock
, flags
);
1106 WARN_ON(host
->mrq
!= NULL
);
1108 #ifndef SDHCI_USE_LEDS_CLASS
1109 sdhci_activate_led(host
);
1114 /* If polling, assume that the card is always present. */
1115 if (host
->quirks
& SDHCI_QUIRK_BROKEN_CARD_DETECTION
)
1118 present
= sdhci_readl(host
, SDHCI_PRESENT_STATE
) &
1121 if (!present
|| host
->flags
& SDHCI_DEVICE_DEAD
) {
1122 host
->mrq
->cmd
->error
= -ENOMEDIUM
;
1123 tasklet_schedule(&host
->finish_tasklet
);
1125 sdhci_send_command(host
, mrq
->cmd
);
1128 spin_unlock_irqrestore(&host
->lock
, flags
);
1131 static void sdhci_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
1133 struct sdhci_host
*host
;
1134 unsigned long flags
;
1137 host
= mmc_priv(mmc
);
1139 spin_lock_irqsave(&host
->lock
, flags
);
1141 if (host
->flags
& SDHCI_DEVICE_DEAD
)
1145 * Reset the chip on each power off.
1146 * Should clear out any weird states.
1148 if (ios
->power_mode
== MMC_POWER_OFF
) {
1149 sdhci_writel(host
, 0, SDHCI_SIGNAL_ENABLE
);
1153 sdhci_set_clock(host
, ios
->clock
);
1155 if (ios
->power_mode
== MMC_POWER_OFF
)
1156 sdhci_set_power(host
, -1);
1158 sdhci_set_power(host
, ios
->vdd
);
1160 ctrl
= sdhci_readb(host
, SDHCI_HOST_CONTROL
);
1162 if (ios
->bus_width
== MMC_BUS_WIDTH_4
)
1163 ctrl
|= SDHCI_CTRL_4BITBUS
;
1165 ctrl
&= ~SDHCI_CTRL_4BITBUS
;
1167 if (ios
->timing
== MMC_TIMING_SD_HS
)
1168 ctrl
|= SDHCI_CTRL_HISPD
;
1170 ctrl
&= ~SDHCI_CTRL_HISPD
;
1172 sdhci_writeb(host
, ctrl
, SDHCI_HOST_CONTROL
);
1175 * Some (ENE) controllers go apeshit on some ios operation,
1176 * signalling timeout and CRC errors even on CMD0. Resetting
1177 * it on each ios seems to solve the problem.
1179 if(host
->quirks
& SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS
)
1180 sdhci_reset(host
, SDHCI_RESET_CMD
| SDHCI_RESET_DATA
);
1184 spin_unlock_irqrestore(&host
->lock
, flags
);
1187 static int sdhci_get_ro(struct mmc_host
*mmc
)
1189 struct sdhci_host
*host
;
1190 unsigned long flags
;
1193 host
= mmc_priv(mmc
);
1195 spin_lock_irqsave(&host
->lock
, flags
);
1197 if (host
->flags
& SDHCI_DEVICE_DEAD
)
1200 present
= sdhci_readl(host
, SDHCI_PRESENT_STATE
);
1202 spin_unlock_irqrestore(&host
->lock
, flags
);
1204 if (host
->quirks
& SDHCI_QUIRK_INVERTED_WRITE_PROTECT
)
1205 return !!(present
& SDHCI_WRITE_PROTECT
);
1206 return !(present
& SDHCI_WRITE_PROTECT
);
1209 static void sdhci_enable_sdio_irq(struct mmc_host
*mmc
, int enable
)
1211 struct sdhci_host
*host
;
1212 unsigned long flags
;
1214 host
= mmc_priv(mmc
);
1216 spin_lock_irqsave(&host
->lock
, flags
);
1218 if (host
->flags
& SDHCI_DEVICE_DEAD
)
1222 sdhci_unmask_irqs(host
, SDHCI_INT_CARD_INT
);
1224 sdhci_mask_irqs(host
, SDHCI_INT_CARD_INT
);
1228 spin_unlock_irqrestore(&host
->lock
, flags
);
1231 static const struct mmc_host_ops sdhci_ops
= {
1232 .request
= sdhci_request
,
1233 .set_ios
= sdhci_set_ios
,
1234 .get_ro
= sdhci_get_ro
,
1235 .enable_sdio_irq
= sdhci_enable_sdio_irq
,
1238 /*****************************************************************************\
1242 \*****************************************************************************/
1244 static void sdhci_tasklet_card(unsigned long param
)
1246 struct sdhci_host
*host
;
1247 unsigned long flags
;
1249 host
= (struct sdhci_host
*)param
;
1251 spin_lock_irqsave(&host
->lock
, flags
);
1253 if (!(sdhci_readl(host
, SDHCI_PRESENT_STATE
) & SDHCI_CARD_PRESENT
)) {
1255 printk(KERN_ERR
"%s: Card removed during transfer!\n",
1256 mmc_hostname(host
->mmc
));
1257 printk(KERN_ERR
"%s: Resetting controller.\n",
1258 mmc_hostname(host
->mmc
));
1260 sdhci_reset(host
, SDHCI_RESET_CMD
);
1261 sdhci_reset(host
, SDHCI_RESET_DATA
);
1263 host
->mrq
->cmd
->error
= -ENOMEDIUM
;
1264 tasklet_schedule(&host
->finish_tasklet
);
1268 spin_unlock_irqrestore(&host
->lock
, flags
);
1270 mmc_detect_change(host
->mmc
, msecs_to_jiffies(200));
1273 static void sdhci_tasklet_finish(unsigned long param
)
1275 struct sdhci_host
*host
;
1276 unsigned long flags
;
1277 struct mmc_request
*mrq
;
1279 host
= (struct sdhci_host
*)param
;
1281 spin_lock_irqsave(&host
->lock
, flags
);
1283 del_timer(&host
->timer
);
1288 * The controller needs a reset of internal state machines
1289 * upon error conditions.
1291 if (!(host
->flags
& SDHCI_DEVICE_DEAD
) &&
1293 (mrq
->data
&& (mrq
->data
->error
||
1294 (mrq
->data
->stop
&& mrq
->data
->stop
->error
))) ||
1295 (host
->quirks
& SDHCI_QUIRK_RESET_AFTER_REQUEST
))) {
1297 /* Some controllers need this kick or reset won't work here */
1298 if (host
->quirks
& SDHCI_QUIRK_CLOCK_BEFORE_RESET
) {
1301 /* This is to force an update */
1302 clock
= host
->clock
;
1304 sdhci_set_clock(host
, clock
);
1307 /* Spec says we should do both at the same time, but Ricoh
1308 controllers do not like that. */
1309 sdhci_reset(host
, SDHCI_RESET_CMD
);
1310 sdhci_reset(host
, SDHCI_RESET_DATA
);
1317 #ifndef SDHCI_USE_LEDS_CLASS
1318 sdhci_deactivate_led(host
);
1322 spin_unlock_irqrestore(&host
->lock
, flags
);
1324 mmc_request_done(host
->mmc
, mrq
);
1327 static void sdhci_timeout_timer(unsigned long data
)
1329 struct sdhci_host
*host
;
1330 unsigned long flags
;
1332 host
= (struct sdhci_host
*)data
;
1334 spin_lock_irqsave(&host
->lock
, flags
);
1337 printk(KERN_ERR
"%s: Timeout waiting for hardware "
1338 "interrupt.\n", mmc_hostname(host
->mmc
));
1339 sdhci_dumpregs(host
);
1342 host
->data
->error
= -ETIMEDOUT
;
1343 sdhci_finish_data(host
);
1346 host
->cmd
->error
= -ETIMEDOUT
;
1348 host
->mrq
->cmd
->error
= -ETIMEDOUT
;
1350 tasklet_schedule(&host
->finish_tasklet
);
1355 spin_unlock_irqrestore(&host
->lock
, flags
);
1358 /*****************************************************************************\
1360 * Interrupt handling *
1362 \*****************************************************************************/
1364 static void sdhci_cmd_irq(struct sdhci_host
*host
, u32 intmask
)
1366 BUG_ON(intmask
== 0);
1369 printk(KERN_ERR
"%s: Got command interrupt 0x%08x even "
1370 "though no command operation was in progress.\n",
1371 mmc_hostname(host
->mmc
), (unsigned)intmask
);
1372 sdhci_dumpregs(host
);
1376 if (intmask
& SDHCI_INT_TIMEOUT
)
1377 host
->cmd
->error
= -ETIMEDOUT
;
1378 else if (intmask
& (SDHCI_INT_CRC
| SDHCI_INT_END_BIT
|
1380 host
->cmd
->error
= -EILSEQ
;
1382 if (host
->cmd
->error
) {
1383 tasklet_schedule(&host
->finish_tasklet
);
1388 * The host can send and interrupt when the busy state has
1389 * ended, allowing us to wait without wasting CPU cycles.
1390 * Unfortunately this is overloaded on the "data complete"
1391 * interrupt, so we need to take some care when handling
1394 * Note: The 1.0 specification is a bit ambiguous about this
1395 * feature so there might be some problems with older
1398 if (host
->cmd
->flags
& MMC_RSP_BUSY
) {
1399 if (host
->cmd
->data
)
1400 DBG("Cannot wait for busy signal when also "
1401 "doing a data transfer");
1402 else if (!(host
->quirks
& SDHCI_QUIRK_NO_BUSY_IRQ
))
1405 /* The controller does not support the end-of-busy IRQ,
1406 * fall through and take the SDHCI_INT_RESPONSE */
1409 if (intmask
& SDHCI_INT_RESPONSE
)
1410 sdhci_finish_command(host
);
1414 static void sdhci_show_adma_error(struct sdhci_host
*host
)
1416 const char *name
= mmc_hostname(host
->mmc
);
1417 u8
*desc
= host
->adma_desc
;
1422 sdhci_dumpregs(host
);
1425 dma
= (__le32
*)(desc
+ 4);
1426 len
= (__le16
*)(desc
+ 2);
1429 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
1430 name
, desc
, le32_to_cpu(*dma
), le16_to_cpu(*len
), attr
);
1439 static void sdhci_show_adma_error(struct sdhci_host
*host
) { }
1442 static void sdhci_data_irq(struct sdhci_host
*host
, u32 intmask
)
1444 BUG_ON(intmask
== 0);
1448 * The "data complete" interrupt is also used to
1449 * indicate that a busy state has ended. See comment
1450 * above in sdhci_cmd_irq().
1452 if (host
->cmd
&& (host
->cmd
->flags
& MMC_RSP_BUSY
)) {
1453 if (intmask
& SDHCI_INT_DATA_END
) {
1454 sdhci_finish_command(host
);
1459 printk(KERN_ERR
"%s: Got data interrupt 0x%08x even "
1460 "though no data operation was in progress.\n",
1461 mmc_hostname(host
->mmc
), (unsigned)intmask
);
1462 sdhci_dumpregs(host
);
1467 if (intmask
& SDHCI_INT_DATA_TIMEOUT
)
1468 host
->data
->error
= -ETIMEDOUT
;
1469 else if (intmask
& (SDHCI_INT_DATA_CRC
| SDHCI_INT_DATA_END_BIT
))
1470 host
->data
->error
= -EILSEQ
;
1471 else if (intmask
& SDHCI_INT_ADMA_ERROR
) {
1472 printk(KERN_ERR
"%s: ADMA error\n", mmc_hostname(host
->mmc
));
1473 sdhci_show_adma_error(host
);
1474 host
->data
->error
= -EIO
;
1477 if (host
->data
->error
)
1478 sdhci_finish_data(host
);
1480 if (intmask
& (SDHCI_INT_DATA_AVAIL
| SDHCI_INT_SPACE_AVAIL
))
1481 sdhci_transfer_pio(host
);
1484 * We currently don't do anything fancy with DMA
1485 * boundaries, but as we can't disable the feature
1486 * we need to at least restart the transfer.
1488 if (intmask
& SDHCI_INT_DMA_END
)
1489 sdhci_writel(host
, sdhci_readl(host
, SDHCI_DMA_ADDRESS
),
1492 if (intmask
& SDHCI_INT_DATA_END
) {
1495 * Data managed to finish before the
1496 * command completed. Make sure we do
1497 * things in the proper order.
1499 host
->data_early
= 1;
1501 sdhci_finish_data(host
);
1507 static irqreturn_t
sdhci_irq(int irq
, void *dev_id
)
1510 struct sdhci_host
* host
= dev_id
;
1514 spin_lock(&host
->lock
);
1516 intmask
= sdhci_readl(host
, SDHCI_INT_STATUS
);
1518 if (!intmask
|| intmask
== 0xffffffff) {
1523 DBG("*** %s got interrupt: 0x%08x\n",
1524 mmc_hostname(host
->mmc
), intmask
);
1526 if (intmask
& (SDHCI_INT_CARD_INSERT
| SDHCI_INT_CARD_REMOVE
)) {
1527 sdhci_writel(host
, intmask
& (SDHCI_INT_CARD_INSERT
|
1528 SDHCI_INT_CARD_REMOVE
), SDHCI_INT_STATUS
);
1529 tasklet_schedule(&host
->card_tasklet
);
1532 intmask
&= ~(SDHCI_INT_CARD_INSERT
| SDHCI_INT_CARD_REMOVE
);
1534 if (intmask
& SDHCI_INT_CMD_MASK
) {
1535 sdhci_writel(host
, intmask
& SDHCI_INT_CMD_MASK
,
1537 sdhci_cmd_irq(host
, intmask
& SDHCI_INT_CMD_MASK
);
1540 if (intmask
& SDHCI_INT_DATA_MASK
) {
1541 sdhci_writel(host
, intmask
& SDHCI_INT_DATA_MASK
,
1543 sdhci_data_irq(host
, intmask
& SDHCI_INT_DATA_MASK
);
1546 intmask
&= ~(SDHCI_INT_CMD_MASK
| SDHCI_INT_DATA_MASK
);
1548 intmask
&= ~SDHCI_INT_ERROR
;
1550 if (intmask
& SDHCI_INT_BUS_POWER
) {
1551 printk(KERN_ERR
"%s: Card is consuming too much power!\n",
1552 mmc_hostname(host
->mmc
));
1553 sdhci_writel(host
, SDHCI_INT_BUS_POWER
, SDHCI_INT_STATUS
);
1556 intmask
&= ~SDHCI_INT_BUS_POWER
;
1558 if (intmask
& SDHCI_INT_CARD_INT
)
1561 intmask
&= ~SDHCI_INT_CARD_INT
;
1564 printk(KERN_ERR
"%s: Unexpected interrupt 0x%08x.\n",
1565 mmc_hostname(host
->mmc
), intmask
);
1566 sdhci_dumpregs(host
);
1568 sdhci_writel(host
, intmask
, SDHCI_INT_STATUS
);
1571 result
= IRQ_HANDLED
;
1575 spin_unlock(&host
->lock
);
1578 * We have to delay this as it calls back into the driver.
1581 mmc_signal_sdio_irq(host
->mmc
);
1586 /*****************************************************************************\
1590 \*****************************************************************************/
1594 int sdhci_suspend_host(struct sdhci_host
*host
, pm_message_t state
)
1598 sdhci_disable_card_detection(host
);
1600 ret
= mmc_suspend_host(host
->mmc
);
1604 free_irq(host
->irq
, host
);
1609 EXPORT_SYMBOL_GPL(sdhci_suspend_host
);
1611 int sdhci_resume_host(struct sdhci_host
*host
)
1615 if (host
->flags
& (SDHCI_USE_SDMA
| SDHCI_USE_ADMA
)) {
1616 if (host
->ops
->enable_dma
)
1617 host
->ops
->enable_dma(host
);
1620 ret
= request_irq(host
->irq
, sdhci_irq
, IRQF_SHARED
,
1621 mmc_hostname(host
->mmc
), host
);
1625 sdhci_init(host
, (host
->mmc
->pm_flags
& MMC_PM_KEEP_POWER
));
1628 ret
= mmc_resume_host(host
->mmc
);
1629 sdhci_enable_card_detection(host
);
1634 EXPORT_SYMBOL_GPL(sdhci_resume_host
);
1636 #endif /* CONFIG_PM */
1638 /*****************************************************************************\
1640 * Device allocation/registration *
1642 \*****************************************************************************/
1644 struct sdhci_host
*sdhci_alloc_host(struct device
*dev
,
1647 struct mmc_host
*mmc
;
1648 struct sdhci_host
*host
;
1650 WARN_ON(dev
== NULL
);
1652 mmc
= mmc_alloc_host(sizeof(struct sdhci_host
) + priv_size
, dev
);
1654 return ERR_PTR(-ENOMEM
);
1656 host
= mmc_priv(mmc
);
1662 EXPORT_SYMBOL_GPL(sdhci_alloc_host
);
1664 int sdhci_add_host(struct sdhci_host
*host
)
1666 struct mmc_host
*mmc
;
1670 WARN_ON(host
== NULL
);
1677 host
->quirks
= debug_quirks
;
1679 sdhci_reset(host
, SDHCI_RESET_ALL
);
1681 host
->version
= sdhci_readw(host
, SDHCI_HOST_VERSION
);
1682 host
->version
= (host
->version
& SDHCI_SPEC_VER_MASK
)
1683 >> SDHCI_SPEC_VER_SHIFT
;
1684 if (host
->version
> SDHCI_SPEC_200
) {
1685 printk(KERN_ERR
"%s: Unknown controller version (%d). "
1686 "You may experience problems.\n", mmc_hostname(mmc
),
1690 caps
= sdhci_readl(host
, SDHCI_CAPABILITIES
);
1692 if (host
->quirks
& SDHCI_QUIRK_FORCE_DMA
)
1693 host
->flags
|= SDHCI_USE_SDMA
;
1694 else if (!(caps
& SDHCI_CAN_DO_SDMA
))
1695 DBG("Controller doesn't have SDMA capability\n");
1697 host
->flags
|= SDHCI_USE_SDMA
;
1699 if ((host
->quirks
& SDHCI_QUIRK_BROKEN_DMA
) &&
1700 (host
->flags
& SDHCI_USE_SDMA
)) {
1701 DBG("Disabling DMA as it is marked broken\n");
1702 host
->flags
&= ~SDHCI_USE_SDMA
;
1705 if ((host
->version
>= SDHCI_SPEC_200
) && (caps
& SDHCI_CAN_DO_ADMA2
))
1706 host
->flags
|= SDHCI_USE_ADMA
;
1708 if ((host
->quirks
& SDHCI_QUIRK_BROKEN_ADMA
) &&
1709 (host
->flags
& SDHCI_USE_ADMA
)) {
1710 DBG("Disabling ADMA as it is marked broken\n");
1711 host
->flags
&= ~SDHCI_USE_ADMA
;
1714 if (host
->flags
& (SDHCI_USE_SDMA
| SDHCI_USE_ADMA
)) {
1715 if (host
->ops
->enable_dma
) {
1716 if (host
->ops
->enable_dma(host
)) {
1717 printk(KERN_WARNING
"%s: No suitable DMA "
1718 "available. Falling back to PIO.\n",
1721 ~(SDHCI_USE_SDMA
| SDHCI_USE_ADMA
);
1726 if (host
->flags
& SDHCI_USE_ADMA
) {
1728 * We need to allocate descriptors for all sg entries
1729 * (128) and potentially one alignment transfer for
1730 * each of those entries.
1732 host
->adma_desc
= kmalloc((128 * 2 + 1) * 4, GFP_KERNEL
);
1733 host
->align_buffer
= kmalloc(128 * 4, GFP_KERNEL
);
1734 if (!host
->adma_desc
|| !host
->align_buffer
) {
1735 kfree(host
->adma_desc
);
1736 kfree(host
->align_buffer
);
1737 printk(KERN_WARNING
"%s: Unable to allocate ADMA "
1738 "buffers. Falling back to standard DMA.\n",
1740 host
->flags
&= ~SDHCI_USE_ADMA
;
1745 * If we use DMA, then it's up to the caller to set the DMA
1746 * mask, but PIO does not need the hw shim so we set a new
1747 * mask here in that case.
1749 if (!(host
->flags
& (SDHCI_USE_SDMA
| SDHCI_USE_ADMA
))) {
1750 host
->dma_mask
= DMA_BIT_MASK(64);
1751 mmc_dev(host
->mmc
)->dma_mask
= &host
->dma_mask
;
1755 (caps
& SDHCI_CLOCK_BASE_MASK
) >> SDHCI_CLOCK_BASE_SHIFT
;
1756 host
->max_clk
*= 1000000;
1757 if (host
->max_clk
== 0 || host
->quirks
&
1758 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN
) {
1759 if (!host
->ops
->get_max_clock
) {
1761 "%s: Hardware doesn't specify base clock "
1762 "frequency.\n", mmc_hostname(mmc
));
1765 host
->max_clk
= host
->ops
->get_max_clock(host
);
1769 (caps
& SDHCI_TIMEOUT_CLK_MASK
) >> SDHCI_TIMEOUT_CLK_SHIFT
;
1770 if (host
->timeout_clk
== 0) {
1771 if (host
->ops
->get_timeout_clock
) {
1772 host
->timeout_clk
= host
->ops
->get_timeout_clock(host
);
1773 } else if (!(host
->quirks
&
1774 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK
)) {
1776 "%s: Hardware doesn't specify timeout clock "
1777 "frequency.\n", mmc_hostname(mmc
));
1781 if (caps
& SDHCI_TIMEOUT_CLK_UNIT
)
1782 host
->timeout_clk
*= 1000;
1785 * Set host parameters.
1787 mmc
->ops
= &sdhci_ops
;
1788 if (host
->quirks
& SDHCI_QUIRK_NONSTANDARD_CLOCK
&&
1789 host
->ops
->set_clock
&& host
->ops
->get_min_clock
)
1790 mmc
->f_min
= host
->ops
->get_min_clock(host
);
1792 mmc
->f_min
= host
->max_clk
/ 256;
1793 mmc
->f_max
= host
->max_clk
;
1794 mmc
->caps
= MMC_CAP_SDIO_IRQ
;
1796 if (!(host
->quirks
& SDHCI_QUIRK_FORCE_1_BIT_DATA
))
1797 mmc
->caps
|= MMC_CAP_4_BIT_DATA
;
1799 if (caps
& SDHCI_CAN_DO_HISPD
)
1800 mmc
->caps
|= MMC_CAP_SD_HIGHSPEED
;
1802 if (host
->quirks
& SDHCI_QUIRK_BROKEN_CARD_DETECTION
)
1803 mmc
->caps
|= MMC_CAP_NEEDS_POLL
;
1806 if (caps
& SDHCI_CAN_VDD_330
)
1807 mmc
->ocr_avail
|= MMC_VDD_32_33
|MMC_VDD_33_34
;
1808 if (caps
& SDHCI_CAN_VDD_300
)
1809 mmc
->ocr_avail
|= MMC_VDD_29_30
|MMC_VDD_30_31
;
1810 if (caps
& SDHCI_CAN_VDD_180
)
1811 mmc
->ocr_avail
|= MMC_VDD_165_195
;
1813 if (mmc
->ocr_avail
== 0) {
1814 printk(KERN_ERR
"%s: Hardware doesn't report any "
1815 "support voltages.\n", mmc_hostname(mmc
));
1819 spin_lock_init(&host
->lock
);
1822 * Maximum number of segments. Depends on if the hardware
1823 * can do scatter/gather or not.
1825 if (host
->flags
& SDHCI_USE_ADMA
)
1826 mmc
->max_hw_segs
= 128;
1827 else if (host
->flags
& SDHCI_USE_SDMA
)
1828 mmc
->max_hw_segs
= 1;
1830 mmc
->max_hw_segs
= 128;
1831 mmc
->max_phys_segs
= 128;
1834 * Maximum number of sectors in one transfer. Limited by DMA boundary
1837 mmc
->max_req_size
= 524288;
1840 * Maximum segment size. Could be one segment with the maximum number
1841 * of bytes. When doing hardware scatter/gather, each entry cannot
1842 * be larger than 64 KiB though.
1844 if (host
->flags
& SDHCI_USE_ADMA
)
1845 mmc
->max_seg_size
= 65536;
1847 mmc
->max_seg_size
= mmc
->max_req_size
;
1850 * Maximum block size. This varies from controller to controller and
1851 * is specified in the capabilities register.
1853 if (host
->quirks
& SDHCI_QUIRK_FORCE_BLK_SZ_2048
) {
1854 mmc
->max_blk_size
= 2;
1856 mmc
->max_blk_size
= (caps
& SDHCI_MAX_BLOCK_MASK
) >>
1857 SDHCI_MAX_BLOCK_SHIFT
;
1858 if (mmc
->max_blk_size
>= 3) {
1859 printk(KERN_WARNING
"%s: Invalid maximum block size, "
1860 "assuming 512 bytes\n", mmc_hostname(mmc
));
1861 mmc
->max_blk_size
= 0;
1865 mmc
->max_blk_size
= 512 << mmc
->max_blk_size
;
1868 * Maximum block count.
1870 mmc
->max_blk_count
= (host
->quirks
& SDHCI_QUIRK_NO_MULTIBLOCK
) ? 1 : 65535;
1875 tasklet_init(&host
->card_tasklet
,
1876 sdhci_tasklet_card
, (unsigned long)host
);
1877 tasklet_init(&host
->finish_tasklet
,
1878 sdhci_tasklet_finish
, (unsigned long)host
);
1880 setup_timer(&host
->timer
, sdhci_timeout_timer
, (unsigned long)host
);
1882 ret
= request_irq(host
->irq
, sdhci_irq
, IRQF_SHARED
,
1883 mmc_hostname(mmc
), host
);
1887 sdhci_init(host
, 0);
1889 #ifdef CONFIG_MMC_DEBUG
1890 sdhci_dumpregs(host
);
1893 #ifdef SDHCI_USE_LEDS_CLASS
1894 snprintf(host
->led_name
, sizeof(host
->led_name
),
1895 "%s::", mmc_hostname(mmc
));
1896 host
->led
.name
= host
->led_name
;
1897 host
->led
.brightness
= LED_OFF
;
1898 host
->led
.default_trigger
= mmc_hostname(mmc
);
1899 host
->led
.brightness_set
= sdhci_led_control
;
1901 ret
= led_classdev_register(mmc_dev(mmc
), &host
->led
);
1910 printk(KERN_INFO
"%s: SDHCI controller on %s [%s] using %s\n",
1911 mmc_hostname(mmc
), host
->hw_name
, dev_name(mmc_dev(mmc
)),
1912 (host
->flags
& SDHCI_USE_ADMA
) ? "ADMA" :
1913 (host
->flags
& SDHCI_USE_SDMA
) ? "DMA" : "PIO");
1915 sdhci_enable_card_detection(host
);
1919 #ifdef SDHCI_USE_LEDS_CLASS
1921 sdhci_reset(host
, SDHCI_RESET_ALL
);
1922 free_irq(host
->irq
, host
);
1925 tasklet_kill(&host
->card_tasklet
);
1926 tasklet_kill(&host
->finish_tasklet
);
1931 EXPORT_SYMBOL_GPL(sdhci_add_host
);
1933 void sdhci_remove_host(struct sdhci_host
*host
, int dead
)
1935 unsigned long flags
;
1938 spin_lock_irqsave(&host
->lock
, flags
);
1940 host
->flags
|= SDHCI_DEVICE_DEAD
;
1943 printk(KERN_ERR
"%s: Controller removed during "
1944 " transfer!\n", mmc_hostname(host
->mmc
));
1946 host
->mrq
->cmd
->error
= -ENOMEDIUM
;
1947 tasklet_schedule(&host
->finish_tasklet
);
1950 spin_unlock_irqrestore(&host
->lock
, flags
);
1953 sdhci_disable_card_detection(host
);
1955 mmc_remove_host(host
->mmc
);
1957 #ifdef SDHCI_USE_LEDS_CLASS
1958 led_classdev_unregister(&host
->led
);
1962 sdhci_reset(host
, SDHCI_RESET_ALL
);
1964 free_irq(host
->irq
, host
);
1966 del_timer_sync(&host
->timer
);
1968 tasklet_kill(&host
->card_tasklet
);
1969 tasklet_kill(&host
->finish_tasklet
);
1971 kfree(host
->adma_desc
);
1972 kfree(host
->align_buffer
);
1974 host
->adma_desc
= NULL
;
1975 host
->align_buffer
= NULL
;
1978 EXPORT_SYMBOL_GPL(sdhci_remove_host
);
1980 void sdhci_free_host(struct sdhci_host
*host
)
1982 mmc_free_host(host
->mmc
);
1985 EXPORT_SYMBOL_GPL(sdhci_free_host
);
1987 /*****************************************************************************\
1989 * Driver init/exit *
1991 \*****************************************************************************/
1993 static int __init
sdhci_drv_init(void)
1995 printk(KERN_INFO DRIVER_NAME
1996 ": Secure Digital Host Controller Interface driver\n");
1997 printk(KERN_INFO DRIVER_NAME
": Copyright(c) Pierre Ossman\n");
2002 static void __exit
sdhci_drv_exit(void)
2006 module_init(sdhci_drv_init
);
2007 module_exit(sdhci_drv_exit
);
2009 module_param(debug_quirks
, uint
, 0444);
2011 MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
2012 MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
2013 MODULE_LICENSE("GPL");
2015 MODULE_PARM_DESC(debug_quirks
, "Force certain quirks.");