2 * Synopsys DesignWare Multimedia Card Interface driver
3 * (Based on NXP driver for lpc 31xx)
5 * Copyright (C) 2009 NXP Semiconductors
6 * Copyright (C) 2009, 2010 Imagination Technologies Ltd.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
14 #include <linux/blkdev.h>
15 #include <linux/clk.h>
16 #include <linux/debugfs.h>
17 #include <linux/device.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/err.h>
20 #include <linux/init.h>
21 #include <linux/interrupt.h>
22 #include <linux/ioport.h>
23 #include <linux/module.h>
24 #include <linux/platform_device.h>
25 #include <linux/seq_file.h>
26 #include <linux/slab.h>
27 #include <linux/stat.h>
28 #include <linux/delay.h>
29 #include <linux/irq.h>
30 #include <linux/mmc/card.h>
31 #include <linux/mmc/host.h>
32 #include <linux/mmc/mmc.h>
33 #include <linux/mmc/sd.h>
34 #include <linux/mmc/sdio.h>
35 #include <linux/mmc/dw_mmc.h>
36 #include <linux/bitops.h>
37 #include <linux/regulator/consumer.h>
39 #include <linux/of_gpio.h>
40 #include <linux/mmc/slot-gpio.h>
44 /* Common flag combinations */
45 #define DW_MCI_DATA_ERROR_FLAGS (SDMMC_INT_DRTO | SDMMC_INT_DCRC | \
46 SDMMC_INT_HTO | SDMMC_INT_SBE | \
48 #define DW_MCI_CMD_ERROR_FLAGS (SDMMC_INT_RTO | SDMMC_INT_RCRC | \
50 #define DW_MCI_ERROR_FLAGS (DW_MCI_DATA_ERROR_FLAGS | \
51 DW_MCI_CMD_ERROR_FLAGS | SDMMC_INT_HLE)
52 #define DW_MCI_SEND_STATUS 1
53 #define DW_MCI_RECV_STATUS 2
54 #define DW_MCI_DMA_THRESHOLD 16
56 #define DW_MCI_FREQ_MAX 200000000 /* unit: HZ */
57 #define DW_MCI_FREQ_MIN 400000 /* unit: HZ */
59 #ifdef CONFIG_MMC_DW_IDMAC
60 #define IDMAC_INT_CLR (SDMMC_IDMAC_INT_AI | SDMMC_IDMAC_INT_NI | \
61 SDMMC_IDMAC_INT_CES | SDMMC_IDMAC_INT_DU | \
62 SDMMC_IDMAC_INT_FBE | SDMMC_IDMAC_INT_RI | \
65 struct idmac_desc_64addr
{
66 u32 des0
; /* Control Descriptor */
68 u32 des1
; /* Reserved */
70 u32 des2
; /*Buffer sizes */
71 #define IDMAC_64ADDR_SET_BUFFER1_SIZE(d, s) \
72 ((d)->des2 = ((d)->des2 & cpu_to_le32(0x03ffe000)) | \
73 ((cpu_to_le32(s)) & cpu_to_le32(0x1fff)))
75 u32 des3
; /* Reserved */
77 u32 des4
; /* Lower 32-bits of Buffer Address Pointer 1*/
78 u32 des5
; /* Upper 32-bits of Buffer Address Pointer 1*/
80 u32 des6
; /* Lower 32-bits of Next Descriptor Address */
81 u32 des7
; /* Upper 32-bits of Next Descriptor Address */
85 __le32 des0
; /* Control Descriptor */
86 #define IDMAC_DES0_DIC BIT(1)
87 #define IDMAC_DES0_LD BIT(2)
88 #define IDMAC_DES0_FD BIT(3)
89 #define IDMAC_DES0_CH BIT(4)
90 #define IDMAC_DES0_ER BIT(5)
91 #define IDMAC_DES0_CES BIT(30)
92 #define IDMAC_DES0_OWN BIT(31)
94 __le32 des1
; /* Buffer sizes */
95 #define IDMAC_SET_BUFFER1_SIZE(d, s) \
96 ((d)->des1 = ((d)->des1 & 0x03ffe000) | ((s) & 0x1fff))
98 __le32 des2
; /* buffer 1 physical address */
100 __le32 des3
; /* buffer 2 physical address */
103 /* Each descriptor can transfer up to 4KB of data in chained mode */
104 #define DW_MCI_DESC_DATA_LENGTH 0x1000
105 #endif /* CONFIG_MMC_DW_IDMAC */
107 static bool dw_mci_reset(struct dw_mci
*host
);
108 static bool dw_mci_ctrl_reset(struct dw_mci
*host
, u32 reset
);
109 static int dw_mci_card_busy(struct mmc_host
*mmc
);
111 #if defined(CONFIG_DEBUG_FS)
112 static int dw_mci_req_show(struct seq_file
*s
, void *v
)
114 struct dw_mci_slot
*slot
= s
->private;
115 struct mmc_request
*mrq
;
116 struct mmc_command
*cmd
;
117 struct mmc_command
*stop
;
118 struct mmc_data
*data
;
120 /* Make sure we get a consistent snapshot */
121 spin_lock_bh(&slot
->host
->lock
);
131 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
132 cmd
->opcode
, cmd
->arg
, cmd
->flags
,
133 cmd
->resp
[0], cmd
->resp
[1], cmd
->resp
[2],
134 cmd
->resp
[2], cmd
->error
);
136 seq_printf(s
, "DATA %u / %u * %u flg %x err %d\n",
137 data
->bytes_xfered
, data
->blocks
,
138 data
->blksz
, data
->flags
, data
->error
);
141 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
142 stop
->opcode
, stop
->arg
, stop
->flags
,
143 stop
->resp
[0], stop
->resp
[1], stop
->resp
[2],
144 stop
->resp
[2], stop
->error
);
147 spin_unlock_bh(&slot
->host
->lock
);
152 static int dw_mci_req_open(struct inode
*inode
, struct file
*file
)
154 return single_open(file
, dw_mci_req_show
, inode
->i_private
);
157 static const struct file_operations dw_mci_req_fops
= {
158 .owner
= THIS_MODULE
,
159 .open
= dw_mci_req_open
,
162 .release
= single_release
,
165 static int dw_mci_regs_show(struct seq_file
*s
, void *v
)
167 seq_printf(s
, "STATUS:\t0x%08x\n", SDMMC_STATUS
);
168 seq_printf(s
, "RINTSTS:\t0x%08x\n", SDMMC_RINTSTS
);
169 seq_printf(s
, "CMD:\t0x%08x\n", SDMMC_CMD
);
170 seq_printf(s
, "CTRL:\t0x%08x\n", SDMMC_CTRL
);
171 seq_printf(s
, "INTMASK:\t0x%08x\n", SDMMC_INTMASK
);
172 seq_printf(s
, "CLKENA:\t0x%08x\n", SDMMC_CLKENA
);
177 static int dw_mci_regs_open(struct inode
*inode
, struct file
*file
)
179 return single_open(file
, dw_mci_regs_show
, inode
->i_private
);
182 static const struct file_operations dw_mci_regs_fops
= {
183 .owner
= THIS_MODULE
,
184 .open
= dw_mci_regs_open
,
187 .release
= single_release
,
190 static void dw_mci_init_debugfs(struct dw_mci_slot
*slot
)
192 struct mmc_host
*mmc
= slot
->mmc
;
193 struct dw_mci
*host
= slot
->host
;
197 root
= mmc
->debugfs_root
;
201 node
= debugfs_create_file("regs", S_IRUSR
, root
, host
,
206 node
= debugfs_create_file("req", S_IRUSR
, root
, slot
,
211 node
= debugfs_create_u32("state", S_IRUSR
, root
, (u32
*)&host
->state
);
215 node
= debugfs_create_x32("pending_events", S_IRUSR
, root
,
216 (u32
*)&host
->pending_events
);
220 node
= debugfs_create_x32("completed_events", S_IRUSR
, root
,
221 (u32
*)&host
->completed_events
);
228 dev_err(&mmc
->class_dev
, "failed to initialize debugfs for slot\n");
230 #endif /* defined(CONFIG_DEBUG_FS) */
232 static void mci_send_cmd(struct dw_mci_slot
*slot
, u32 cmd
, u32 arg
);
234 static u32
dw_mci_prepare_command(struct mmc_host
*mmc
, struct mmc_command
*cmd
)
236 struct mmc_data
*data
;
237 struct dw_mci_slot
*slot
= mmc_priv(mmc
);
238 struct dw_mci
*host
= slot
->host
;
239 const struct dw_mci_drv_data
*drv_data
= slot
->host
->drv_data
;
242 cmd
->error
= -EINPROGRESS
;
245 if (cmd
->opcode
== MMC_STOP_TRANSMISSION
||
246 cmd
->opcode
== MMC_GO_IDLE_STATE
||
247 cmd
->opcode
== MMC_GO_INACTIVE_STATE
||
248 (cmd
->opcode
== SD_IO_RW_DIRECT
&&
249 ((cmd
->arg
>> 9) & 0x1FFFF) == SDIO_CCCR_ABORT
))
250 cmdr
|= SDMMC_CMD_STOP
;
251 else if (cmd
->opcode
!= MMC_SEND_STATUS
&& cmd
->data
)
252 cmdr
|= SDMMC_CMD_PRV_DAT_WAIT
;
254 if (cmd
->opcode
== SD_SWITCH_VOLTAGE
) {
257 /* Special bit makes CMD11 not die */
258 cmdr
|= SDMMC_CMD_VOLT_SWITCH
;
260 /* Change state to continue to handle CMD11 weirdness */
261 WARN_ON(slot
->host
->state
!= STATE_SENDING_CMD
);
262 slot
->host
->state
= STATE_SENDING_CMD11
;
265 * We need to disable low power mode (automatic clock stop)
266 * while doing voltage switch so we don't confuse the card,
267 * since stopping the clock is a specific part of the UHS
268 * voltage change dance.
270 * Note that low power mode (SDMMC_CLKEN_LOW_PWR) will be
271 * unconditionally turned back on in dw_mci_setup_bus() if it's
272 * ever called with a non-zero clock. That shouldn't happen
273 * until the voltage change is all done.
275 clk_en_a
= mci_readl(host
, CLKENA
);
276 clk_en_a
&= ~(SDMMC_CLKEN_LOW_PWR
<< slot
->id
);
277 mci_writel(host
, CLKENA
, clk_en_a
);
278 mci_send_cmd(slot
, SDMMC_CMD_UPD_CLK
|
279 SDMMC_CMD_PRV_DAT_WAIT
, 0);
282 if (cmd
->flags
& MMC_RSP_PRESENT
) {
283 /* We expect a response, so set this bit */
284 cmdr
|= SDMMC_CMD_RESP_EXP
;
285 if (cmd
->flags
& MMC_RSP_136
)
286 cmdr
|= SDMMC_CMD_RESP_LONG
;
289 if (cmd
->flags
& MMC_RSP_CRC
)
290 cmdr
|= SDMMC_CMD_RESP_CRC
;
294 cmdr
|= SDMMC_CMD_DAT_EXP
;
295 if (data
->flags
& MMC_DATA_STREAM
)
296 cmdr
|= SDMMC_CMD_STRM_MODE
;
297 if (data
->flags
& MMC_DATA_WRITE
)
298 cmdr
|= SDMMC_CMD_DAT_WR
;
301 if (drv_data
&& drv_data
->prepare_command
)
302 drv_data
->prepare_command(slot
->host
, &cmdr
);
307 static u32
dw_mci_prep_stop_abort(struct dw_mci
*host
, struct mmc_command
*cmd
)
309 struct mmc_command
*stop
;
315 stop
= &host
->stop_abort
;
317 memset(stop
, 0, sizeof(struct mmc_command
));
319 if (cmdr
== MMC_READ_SINGLE_BLOCK
||
320 cmdr
== MMC_READ_MULTIPLE_BLOCK
||
321 cmdr
== MMC_WRITE_BLOCK
||
322 cmdr
== MMC_WRITE_MULTIPLE_BLOCK
||
323 cmdr
== MMC_SEND_TUNING_BLOCK
||
324 cmdr
== MMC_SEND_TUNING_BLOCK_HS200
) {
325 stop
->opcode
= MMC_STOP_TRANSMISSION
;
327 stop
->flags
= MMC_RSP_R1B
| MMC_CMD_AC
;
328 } else if (cmdr
== SD_IO_RW_EXTENDED
) {
329 stop
->opcode
= SD_IO_RW_DIRECT
;
330 stop
->arg
|= (1 << 31) | (0 << 28) | (SDIO_CCCR_ABORT
<< 9) |
331 ((cmd
->arg
>> 28) & 0x7);
332 stop
->flags
= MMC_RSP_SPI_R5
| MMC_RSP_R5
| MMC_CMD_AC
;
337 cmdr
= stop
->opcode
| SDMMC_CMD_STOP
|
338 SDMMC_CMD_RESP_CRC
| SDMMC_CMD_RESP_EXP
;
343 static void dw_mci_wait_while_busy(struct dw_mci
*host
, u32 cmd_flags
)
345 unsigned long timeout
= jiffies
+ msecs_to_jiffies(500);
348 * Databook says that before issuing a new data transfer command
349 * we need to check to see if the card is busy. Data transfer commands
350 * all have SDMMC_CMD_PRV_DAT_WAIT set, so we'll key off that.
352 * ...also allow sending for SDMMC_CMD_VOLT_SWITCH where busy is
355 if ((cmd_flags
& SDMMC_CMD_PRV_DAT_WAIT
) &&
356 !(cmd_flags
& SDMMC_CMD_VOLT_SWITCH
)) {
357 while (mci_readl(host
, STATUS
) & SDMMC_STATUS_BUSY
) {
358 if (time_after(jiffies
, timeout
)) {
359 /* Command will fail; we'll pass error then */
360 dev_err(host
->dev
, "Busy; trying anyway\n");
368 static void dw_mci_start_command(struct dw_mci
*host
,
369 struct mmc_command
*cmd
, u32 cmd_flags
)
373 "start command: ARGR=0x%08x CMDR=0x%08x\n",
374 cmd
->arg
, cmd_flags
);
376 mci_writel(host
, CMDARG
, cmd
->arg
);
377 wmb(); /* drain writebuffer */
378 dw_mci_wait_while_busy(host
, cmd_flags
);
380 mci_writel(host
, CMD
, cmd_flags
| SDMMC_CMD_START
);
383 static inline void send_stop_abort(struct dw_mci
*host
, struct mmc_data
*data
)
385 struct mmc_command
*stop
= data
->stop
? data
->stop
: &host
->stop_abort
;
387 dw_mci_start_command(host
, stop
, host
->stop_cmdr
);
390 /* DMA interface functions */
391 static void dw_mci_stop_dma(struct dw_mci
*host
)
393 if (host
->using_dma
) {
394 host
->dma_ops
->stop(host
);
395 host
->dma_ops
->cleanup(host
);
398 /* Data transfer was stopped by the interrupt handler */
399 set_bit(EVENT_XFER_COMPLETE
, &host
->pending_events
);
402 static int dw_mci_get_dma_dir(struct mmc_data
*data
)
404 if (data
->flags
& MMC_DATA_WRITE
)
405 return DMA_TO_DEVICE
;
407 return DMA_FROM_DEVICE
;
410 #ifdef CONFIG_MMC_DW_IDMAC
411 static void dw_mci_dma_cleanup(struct dw_mci
*host
)
413 struct mmc_data
*data
= host
->data
;
416 if (!data
->host_cookie
)
417 dma_unmap_sg(host
->dev
,
420 dw_mci_get_dma_dir(data
));
423 static void dw_mci_idmac_reset(struct dw_mci
*host
)
425 u32 bmod
= mci_readl(host
, BMOD
);
426 /* Software reset of DMA */
427 bmod
|= SDMMC_IDMAC_SWRESET
;
428 mci_writel(host
, BMOD
, bmod
);
431 static void dw_mci_idmac_stop_dma(struct dw_mci
*host
)
435 /* Disable and reset the IDMAC interface */
436 temp
= mci_readl(host
, CTRL
);
437 temp
&= ~SDMMC_CTRL_USE_IDMAC
;
438 temp
|= SDMMC_CTRL_DMA_RESET
;
439 mci_writel(host
, CTRL
, temp
);
441 /* Stop the IDMAC running */
442 temp
= mci_readl(host
, BMOD
);
443 temp
&= ~(SDMMC_IDMAC_ENABLE
| SDMMC_IDMAC_FB
);
444 temp
|= SDMMC_IDMAC_SWRESET
;
445 mci_writel(host
, BMOD
, temp
);
448 static void dw_mci_idmac_complete_dma(struct dw_mci
*host
)
450 struct mmc_data
*data
= host
->data
;
452 dev_vdbg(host
->dev
, "DMA complete\n");
454 host
->dma_ops
->cleanup(host
);
457 * If the card was removed, data will be NULL. No point in trying to
458 * send the stop command or waiting for NBUSY in this case.
461 set_bit(EVENT_XFER_COMPLETE
, &host
->pending_events
);
462 tasklet_schedule(&host
->tasklet
);
466 static void dw_mci_translate_sglist(struct dw_mci
*host
, struct mmc_data
*data
,
469 unsigned int desc_len
;
472 if (host
->dma_64bit_address
== 1) {
473 struct idmac_desc_64addr
*desc_first
, *desc_last
, *desc
;
475 desc_first
= desc_last
= desc
= host
->sg_cpu
;
477 for (i
= 0; i
< sg_len
; i
++) {
478 unsigned int length
= sg_dma_len(&data
->sg
[i
]);
480 u64 mem_addr
= sg_dma_address(&data
->sg
[i
]);
482 for ( ; length
; desc
++) {
483 desc_len
= (length
<= DW_MCI_DESC_DATA_LENGTH
) ?
484 length
: DW_MCI_DESC_DATA_LENGTH
;
489 * Set the OWN bit and disable interrupts
490 * for this descriptor
492 desc
->des0
= IDMAC_DES0_OWN
| IDMAC_DES0_DIC
|
496 IDMAC_64ADDR_SET_BUFFER1_SIZE(desc
, desc_len
);
498 /* Physical address to DMA to/from */
499 desc
->des4
= mem_addr
& 0xffffffff;
500 desc
->des5
= mem_addr
>> 32;
502 /* Update physical address for the next desc */
503 mem_addr
+= desc_len
;
505 /* Save pointer to the last descriptor */
510 /* Set first descriptor */
511 desc_first
->des0
|= IDMAC_DES0_FD
;
513 /* Set last descriptor */
514 desc_last
->des0
&= ~(IDMAC_DES0_CH
| IDMAC_DES0_DIC
);
515 desc_last
->des0
|= IDMAC_DES0_LD
;
518 struct idmac_desc
*desc_first
, *desc_last
, *desc
;
520 desc_first
= desc_last
= desc
= host
->sg_cpu
;
522 for (i
= 0; i
< sg_len
; i
++) {
523 unsigned int length
= sg_dma_len(&data
->sg
[i
]);
525 u32 mem_addr
= sg_dma_address(&data
->sg
[i
]);
527 for ( ; length
; desc
++) {
528 desc_len
= (length
<= DW_MCI_DESC_DATA_LENGTH
) ?
529 length
: DW_MCI_DESC_DATA_LENGTH
;
534 * Set the OWN bit and disable interrupts
535 * for this descriptor
537 desc
->des0
= cpu_to_le32(IDMAC_DES0_OWN
|
542 IDMAC_SET_BUFFER1_SIZE(desc
, desc_len
);
544 /* Physical address to DMA to/from */
545 desc
->des2
= cpu_to_le32(mem_addr
);
547 /* Update physical address for the next desc */
548 mem_addr
+= desc_len
;
550 /* Save pointer to the last descriptor */
555 /* Set first descriptor */
556 desc_first
->des0
|= cpu_to_le32(IDMAC_DES0_FD
);
558 /* Set last descriptor */
559 desc_last
->des0
&= cpu_to_le32(~(IDMAC_DES0_CH
|
561 desc_last
->des0
|= cpu_to_le32(IDMAC_DES0_LD
);
564 wmb(); /* drain writebuffer */
567 static void dw_mci_idmac_start_dma(struct dw_mci
*host
, unsigned int sg_len
)
571 dw_mci_translate_sglist(host
, host
->data
, sg_len
);
573 /* Make sure to reset DMA in case we did PIO before this */
574 dw_mci_ctrl_reset(host
, SDMMC_CTRL_DMA_RESET
);
575 dw_mci_idmac_reset(host
);
577 /* Select IDMAC interface */
578 temp
= mci_readl(host
, CTRL
);
579 temp
|= SDMMC_CTRL_USE_IDMAC
;
580 mci_writel(host
, CTRL
, temp
);
582 /* drain writebuffer */
585 /* Enable the IDMAC */
586 temp
= mci_readl(host
, BMOD
);
587 temp
|= SDMMC_IDMAC_ENABLE
| SDMMC_IDMAC_FB
;
588 mci_writel(host
, BMOD
, temp
);
590 /* Start it running */
591 mci_writel(host
, PLDMND
, 1);
594 static int dw_mci_idmac_init(struct dw_mci
*host
)
598 if (host
->dma_64bit_address
== 1) {
599 struct idmac_desc_64addr
*p
;
600 /* Number of descriptors in the ring buffer */
601 host
->ring_size
= PAGE_SIZE
/ sizeof(struct idmac_desc_64addr
);
603 /* Forward link the descriptor list */
604 for (i
= 0, p
= host
->sg_cpu
; i
< host
->ring_size
- 1;
606 p
->des6
= (host
->sg_dma
+
607 (sizeof(struct idmac_desc_64addr
) *
608 (i
+ 1))) & 0xffffffff;
610 p
->des7
= (u64
)(host
->sg_dma
+
611 (sizeof(struct idmac_desc_64addr
) *
613 /* Initialize reserved and buffer size fields to "0" */
619 /* Set the last descriptor as the end-of-ring descriptor */
620 p
->des6
= host
->sg_dma
& 0xffffffff;
621 p
->des7
= (u64
)host
->sg_dma
>> 32;
622 p
->des0
= IDMAC_DES0_ER
;
625 struct idmac_desc
*p
;
626 /* Number of descriptors in the ring buffer */
627 host
->ring_size
= PAGE_SIZE
/ sizeof(struct idmac_desc
);
629 /* Forward link the descriptor list */
630 for (i
= 0, p
= host
->sg_cpu
;
631 i
< host
->ring_size
- 1;
633 p
->des3
= cpu_to_le32(host
->sg_dma
+
634 (sizeof(struct idmac_desc
) * (i
+ 1)));
638 /* Set the last descriptor as the end-of-ring descriptor */
639 p
->des3
= cpu_to_le32(host
->sg_dma
);
640 p
->des0
= cpu_to_le32(IDMAC_DES0_ER
);
643 dw_mci_idmac_reset(host
);
645 if (host
->dma_64bit_address
== 1) {
646 /* Mask out interrupts - get Tx & Rx complete only */
647 mci_writel(host
, IDSTS64
, IDMAC_INT_CLR
);
648 mci_writel(host
, IDINTEN64
, SDMMC_IDMAC_INT_NI
|
649 SDMMC_IDMAC_INT_RI
| SDMMC_IDMAC_INT_TI
);
651 /* Set the descriptor base address */
652 mci_writel(host
, DBADDRL
, host
->sg_dma
& 0xffffffff);
653 mci_writel(host
, DBADDRU
, (u64
)host
->sg_dma
>> 32);
656 /* Mask out interrupts - get Tx & Rx complete only */
657 mci_writel(host
, IDSTS
, IDMAC_INT_CLR
);
658 mci_writel(host
, IDINTEN
, SDMMC_IDMAC_INT_NI
|
659 SDMMC_IDMAC_INT_RI
| SDMMC_IDMAC_INT_TI
);
661 /* Set the descriptor base address */
662 mci_writel(host
, DBADDR
, host
->sg_dma
);
668 static const struct dw_mci_dma_ops dw_mci_idmac_ops
= {
669 .init
= dw_mci_idmac_init
,
670 .start
= dw_mci_idmac_start_dma
,
671 .stop
= dw_mci_idmac_stop_dma
,
672 .complete
= dw_mci_idmac_complete_dma
,
673 .cleanup
= dw_mci_dma_cleanup
,
675 #endif /* CONFIG_MMC_DW_IDMAC */
677 static int dw_mci_pre_dma_transfer(struct dw_mci
*host
,
678 struct mmc_data
*data
,
681 struct scatterlist
*sg
;
682 unsigned int i
, sg_len
;
684 if (!next
&& data
->host_cookie
)
685 return data
->host_cookie
;
688 * We don't do DMA on "complex" transfers, i.e. with
689 * non-word-aligned buffers or lengths. Also, we don't bother
690 * with all the DMA setup overhead for short transfers.
692 if (data
->blocks
* data
->blksz
< DW_MCI_DMA_THRESHOLD
)
698 for_each_sg(data
->sg
, sg
, data
->sg_len
, i
) {
699 if (sg
->offset
& 3 || sg
->length
& 3)
703 sg_len
= dma_map_sg(host
->dev
,
706 dw_mci_get_dma_dir(data
));
711 data
->host_cookie
= sg_len
;
716 static void dw_mci_pre_req(struct mmc_host
*mmc
,
717 struct mmc_request
*mrq
,
720 struct dw_mci_slot
*slot
= mmc_priv(mmc
);
721 struct mmc_data
*data
= mrq
->data
;
723 if (!slot
->host
->use_dma
|| !data
)
726 if (data
->host_cookie
) {
727 data
->host_cookie
= 0;
731 if (dw_mci_pre_dma_transfer(slot
->host
, mrq
->data
, 1) < 0)
732 data
->host_cookie
= 0;
735 static void dw_mci_post_req(struct mmc_host
*mmc
,
736 struct mmc_request
*mrq
,
739 struct dw_mci_slot
*slot
= mmc_priv(mmc
);
740 struct mmc_data
*data
= mrq
->data
;
742 if (!slot
->host
->use_dma
|| !data
)
745 if (data
->host_cookie
)
746 dma_unmap_sg(slot
->host
->dev
,
749 dw_mci_get_dma_dir(data
));
750 data
->host_cookie
= 0;
753 static void dw_mci_adjust_fifoth(struct dw_mci
*host
, struct mmc_data
*data
)
755 #ifdef CONFIG_MMC_DW_IDMAC
756 unsigned int blksz
= data
->blksz
;
757 const u32 mszs
[] = {1, 4, 8, 16, 32, 64, 128, 256};
758 u32 fifo_width
= 1 << host
->data_shift
;
759 u32 blksz_depth
= blksz
/ fifo_width
, fifoth_val
;
760 u32 msize
= 0, rx_wmark
= 1, tx_wmark
, tx_wmark_invers
;
761 int idx
= ARRAY_SIZE(mszs
) - 1;
763 tx_wmark
= (host
->fifo_depth
) / 2;
764 tx_wmark_invers
= host
->fifo_depth
- tx_wmark
;
768 * if blksz is not a multiple of the FIFO width
770 if (blksz
% fifo_width
) {
777 if (!((blksz_depth
% mszs
[idx
]) ||
778 (tx_wmark_invers
% mszs
[idx
]))) {
780 rx_wmark
= mszs
[idx
] - 1;
785 * If idx is '0', it won't be tried
786 * Thus, initial values are uesed
789 fifoth_val
= SDMMC_SET_FIFOTH(msize
, rx_wmark
, tx_wmark
);
790 mci_writel(host
, FIFOTH
, fifoth_val
);
794 static void dw_mci_ctrl_rd_thld(struct dw_mci
*host
, struct mmc_data
*data
)
796 unsigned int blksz
= data
->blksz
;
797 u32 blksz_depth
, fifo_depth
;
800 WARN_ON(!(data
->flags
& MMC_DATA_READ
));
803 * CDTHRCTL doesn't exist prior to 240A (in fact that register offset is
804 * in the FIFO region, so we really shouldn't access it).
806 if (host
->verid
< DW_MMC_240A
)
809 if (host
->timing
!= MMC_TIMING_MMC_HS200
&&
810 host
->timing
!= MMC_TIMING_MMC_HS400
&&
811 host
->timing
!= MMC_TIMING_UHS_SDR104
)
814 blksz_depth
= blksz
/ (1 << host
->data_shift
);
815 fifo_depth
= host
->fifo_depth
;
817 if (blksz_depth
> fifo_depth
)
821 * If (blksz_depth) >= (fifo_depth >> 1), should be 'thld_size <= blksz'
822 * If (blksz_depth) < (fifo_depth >> 1), should be thld_size = blksz
823 * Currently just choose blksz.
826 mci_writel(host
, CDTHRCTL
, SDMMC_SET_RD_THLD(thld_size
, 1));
830 mci_writel(host
, CDTHRCTL
, SDMMC_SET_RD_THLD(0, 0));
833 static int dw_mci_submit_data_dma(struct dw_mci
*host
, struct mmc_data
*data
)
835 unsigned long irqflags
;
841 /* If we don't have a channel, we can't do DMA */
845 sg_len
= dw_mci_pre_dma_transfer(host
, data
, 0);
847 host
->dma_ops
->stop(host
);
854 "sd sg_cpu: %#lx sg_dma: %#lx sg_len: %d\n",
855 (unsigned long)host
->sg_cpu
, (unsigned long)host
->sg_dma
,
859 * Decide the MSIZE and RX/TX Watermark.
860 * If current block size is same with previous size,
861 * no need to update fifoth.
863 if (host
->prev_blksz
!= data
->blksz
)
864 dw_mci_adjust_fifoth(host
, data
);
866 /* Enable the DMA interface */
867 temp
= mci_readl(host
, CTRL
);
868 temp
|= SDMMC_CTRL_DMA_ENABLE
;
869 mci_writel(host
, CTRL
, temp
);
871 /* Disable RX/TX IRQs, let DMA handle it */
872 spin_lock_irqsave(&host
->irq_lock
, irqflags
);
873 temp
= mci_readl(host
, INTMASK
);
874 temp
&= ~(SDMMC_INT_RXDR
| SDMMC_INT_TXDR
);
875 mci_writel(host
, INTMASK
, temp
);
876 spin_unlock_irqrestore(&host
->irq_lock
, irqflags
);
878 host
->dma_ops
->start(host
, sg_len
);
883 static void dw_mci_submit_data(struct dw_mci
*host
, struct mmc_data
*data
)
885 unsigned long irqflags
;
886 int flags
= SG_MITER_ATOMIC
;
889 data
->error
= -EINPROGRESS
;
895 if (data
->flags
& MMC_DATA_READ
) {
896 host
->dir_status
= DW_MCI_RECV_STATUS
;
897 dw_mci_ctrl_rd_thld(host
, data
);
899 host
->dir_status
= DW_MCI_SEND_STATUS
;
902 if (dw_mci_submit_data_dma(host
, data
)) {
903 if (host
->data
->flags
& MMC_DATA_READ
)
904 flags
|= SG_MITER_TO_SG
;
906 flags
|= SG_MITER_FROM_SG
;
908 sg_miter_start(&host
->sg_miter
, data
->sg
, data
->sg_len
, flags
);
910 host
->part_buf_start
= 0;
911 host
->part_buf_count
= 0;
913 mci_writel(host
, RINTSTS
, SDMMC_INT_TXDR
| SDMMC_INT_RXDR
);
915 spin_lock_irqsave(&host
->irq_lock
, irqflags
);
916 temp
= mci_readl(host
, INTMASK
);
917 temp
|= SDMMC_INT_TXDR
| SDMMC_INT_RXDR
;
918 mci_writel(host
, INTMASK
, temp
);
919 spin_unlock_irqrestore(&host
->irq_lock
, irqflags
);
921 temp
= mci_readl(host
, CTRL
);
922 temp
&= ~SDMMC_CTRL_DMA_ENABLE
;
923 mci_writel(host
, CTRL
, temp
);
926 * Use the initial fifoth_val for PIO mode.
927 * If next issued data may be transfered by DMA mode,
928 * prev_blksz should be invalidated.
930 mci_writel(host
, FIFOTH
, host
->fifoth_val
);
931 host
->prev_blksz
= 0;
934 * Keep the current block size.
935 * It will be used to decide whether to update
936 * fifoth register next time.
938 host
->prev_blksz
= data
->blksz
;
942 static void mci_send_cmd(struct dw_mci_slot
*slot
, u32 cmd
, u32 arg
)
944 struct dw_mci
*host
= slot
->host
;
945 unsigned long timeout
= jiffies
+ msecs_to_jiffies(500);
946 unsigned int cmd_status
= 0;
948 mci_writel(host
, CMDARG
, arg
);
949 wmb(); /* drain writebuffer */
950 dw_mci_wait_while_busy(host
, cmd
);
951 mci_writel(host
, CMD
, SDMMC_CMD_START
| cmd
);
953 while (time_before(jiffies
, timeout
)) {
954 cmd_status
= mci_readl(host
, CMD
);
955 if (!(cmd_status
& SDMMC_CMD_START
))
958 dev_err(&slot
->mmc
->class_dev
,
959 "Timeout sending command (cmd %#x arg %#x status %#x)\n",
960 cmd
, arg
, cmd_status
);
963 static void dw_mci_setup_bus(struct dw_mci_slot
*slot
, bool force_clkinit
)
965 struct dw_mci
*host
= slot
->host
;
966 unsigned int clock
= slot
->clock
;
969 u32 sdmmc_cmd_bits
= SDMMC_CMD_UPD_CLK
| SDMMC_CMD_PRV_DAT_WAIT
;
971 /* We must continue to set bit 28 in CMD until the change is complete */
972 if (host
->state
== STATE_WAITING_CMD11_DONE
)
973 sdmmc_cmd_bits
|= SDMMC_CMD_VOLT_SWITCH
;
976 mci_writel(host
, CLKENA
, 0);
977 mci_send_cmd(slot
, sdmmc_cmd_bits
, 0);
978 } else if (clock
!= host
->current_speed
|| force_clkinit
) {
979 div
= host
->bus_hz
/ clock
;
980 if (host
->bus_hz
% clock
&& host
->bus_hz
> clock
)
982 * move the + 1 after the divide to prevent
983 * over-clocking the card.
987 div
= (host
->bus_hz
!= clock
) ? DIV_ROUND_UP(div
, 2) : 0;
989 if ((clock
<< div
) != slot
->__clk_old
|| force_clkinit
)
990 dev_info(&slot
->mmc
->class_dev
,
991 "Bus speed (slot %d) = %dHz (slot req %dHz, actual %dHZ div = %d)\n",
992 slot
->id
, host
->bus_hz
, clock
,
993 div
? ((host
->bus_hz
/ div
) >> 1) :
997 mci_writel(host
, CLKENA
, 0);
998 mci_writel(host
, CLKSRC
, 0);
1001 mci_send_cmd(slot
, sdmmc_cmd_bits
, 0);
1003 /* set clock to desired speed */
1004 mci_writel(host
, CLKDIV
, div
);
1007 mci_send_cmd(slot
, sdmmc_cmd_bits
, 0);
1009 /* enable clock; only low power if no SDIO */
1010 clk_en_a
= SDMMC_CLKEN_ENABLE
<< slot
->id
;
1011 if (!test_bit(DW_MMC_CARD_NO_LOW_PWR
, &slot
->flags
))
1012 clk_en_a
|= SDMMC_CLKEN_LOW_PWR
<< slot
->id
;
1013 mci_writel(host
, CLKENA
, clk_en_a
);
1016 mci_send_cmd(slot
, sdmmc_cmd_bits
, 0);
1018 /* keep the clock with reflecting clock dividor */
1019 slot
->__clk_old
= clock
<< div
;
1022 host
->current_speed
= clock
;
1024 /* Set the current slot bus width */
1025 mci_writel(host
, CTYPE
, (slot
->ctype
<< slot
->id
));
1028 static void __dw_mci_start_request(struct dw_mci
*host
,
1029 struct dw_mci_slot
*slot
,
1030 struct mmc_command
*cmd
)
1032 struct mmc_request
*mrq
;
1033 struct mmc_data
*data
;
1038 host
->cur_slot
= slot
;
1041 host
->pending_events
= 0;
1042 host
->completed_events
= 0;
1043 host
->cmd_status
= 0;
1044 host
->data_status
= 0;
1045 host
->dir_status
= 0;
1049 mci_writel(host
, TMOUT
, 0xFFFFFFFF);
1050 mci_writel(host
, BYTCNT
, data
->blksz
*data
->blocks
);
1051 mci_writel(host
, BLKSIZ
, data
->blksz
);
1054 cmdflags
= dw_mci_prepare_command(slot
->mmc
, cmd
);
1056 /* this is the first command, send the initialization clock */
1057 if (test_and_clear_bit(DW_MMC_CARD_NEED_INIT
, &slot
->flags
))
1058 cmdflags
|= SDMMC_CMD_INIT
;
1061 dw_mci_submit_data(host
, data
);
1062 wmb(); /* drain writebuffer */
1065 dw_mci_start_command(host
, cmd
, cmdflags
);
1067 if (cmd
->opcode
== SD_SWITCH_VOLTAGE
) {
1068 unsigned long irqflags
;
1071 * Databook says to fail after 2ms w/ no response, but evidence
1072 * shows that sometimes the cmd11 interrupt takes over 130ms.
1073 * We'll set to 500ms, plus an extra jiffy just in case jiffies
1074 * is just about to roll over.
1076 * We do this whole thing under spinlock and only if the
1077 * command hasn't already completed (indicating the the irq
1078 * already ran so we don't want the timeout).
1080 spin_lock_irqsave(&host
->irq_lock
, irqflags
);
1081 if (!test_bit(EVENT_CMD_COMPLETE
, &host
->pending_events
))
1082 mod_timer(&host
->cmd11_timer
,
1083 jiffies
+ msecs_to_jiffies(500) + 1);
1084 spin_unlock_irqrestore(&host
->irq_lock
, irqflags
);
1088 host
->stop_cmdr
= dw_mci_prepare_command(slot
->mmc
, mrq
->stop
);
1090 host
->stop_cmdr
= dw_mci_prep_stop_abort(host
, cmd
);
1093 static void dw_mci_start_request(struct dw_mci
*host
,
1094 struct dw_mci_slot
*slot
)
1096 struct mmc_request
*mrq
= slot
->mrq
;
1097 struct mmc_command
*cmd
;
1099 cmd
= mrq
->sbc
? mrq
->sbc
: mrq
->cmd
;
1100 __dw_mci_start_request(host
, slot
, cmd
);
1103 /* must be called with host->lock held */
1104 static void dw_mci_queue_request(struct dw_mci
*host
, struct dw_mci_slot
*slot
,
1105 struct mmc_request
*mrq
)
1107 dev_vdbg(&slot
->mmc
->class_dev
, "queue request: state=%d\n",
1112 if (host
->state
== STATE_WAITING_CMD11_DONE
) {
1113 dev_warn(&slot
->mmc
->class_dev
,
1114 "Voltage change didn't complete\n");
1116 * this case isn't expected to happen, so we can
1117 * either crash here or just try to continue on
1118 * in the closest possible state
1120 host
->state
= STATE_IDLE
;
1123 if (host
->state
== STATE_IDLE
) {
1124 host
->state
= STATE_SENDING_CMD
;
1125 dw_mci_start_request(host
, slot
);
1127 list_add_tail(&slot
->queue_node
, &host
->queue
);
1131 static void dw_mci_request(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
1133 struct dw_mci_slot
*slot
= mmc_priv(mmc
);
1134 struct dw_mci
*host
= slot
->host
;
1139 * The check for card presence and queueing of the request must be
1140 * atomic, otherwise the card could be removed in between and the
1141 * request wouldn't fail until another card was inserted.
1143 spin_lock_bh(&host
->lock
);
1145 if (!test_bit(DW_MMC_CARD_PRESENT
, &slot
->flags
)) {
1146 spin_unlock_bh(&host
->lock
);
1147 mrq
->cmd
->error
= -ENOMEDIUM
;
1148 mmc_request_done(mmc
, mrq
);
1152 dw_mci_queue_request(host
, slot
, mrq
);
1154 spin_unlock_bh(&host
->lock
);
1157 static void dw_mci_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
1159 struct dw_mci_slot
*slot
= mmc_priv(mmc
);
1160 const struct dw_mci_drv_data
*drv_data
= slot
->host
->drv_data
;
1164 switch (ios
->bus_width
) {
1165 case MMC_BUS_WIDTH_4
:
1166 slot
->ctype
= SDMMC_CTYPE_4BIT
;
1168 case MMC_BUS_WIDTH_8
:
1169 slot
->ctype
= SDMMC_CTYPE_8BIT
;
1172 /* set default 1 bit mode */
1173 slot
->ctype
= SDMMC_CTYPE_1BIT
;
1176 regs
= mci_readl(slot
->host
, UHS_REG
);
1179 if (ios
->timing
== MMC_TIMING_MMC_DDR52
||
1180 ios
->timing
== MMC_TIMING_MMC_HS400
)
1181 regs
|= ((0x1 << slot
->id
) << 16);
1183 regs
&= ~((0x1 << slot
->id
) << 16);
1185 mci_writel(slot
->host
, UHS_REG
, regs
);
1186 slot
->host
->timing
= ios
->timing
;
1189 * Use mirror of ios->clock to prevent race with mmc
1190 * core ios update when finding the minimum.
1192 slot
->clock
= ios
->clock
;
1194 if (drv_data
&& drv_data
->set_ios
)
1195 drv_data
->set_ios(slot
->host
, ios
);
1197 switch (ios
->power_mode
) {
1199 if (!IS_ERR(mmc
->supply
.vmmc
)) {
1200 ret
= mmc_regulator_set_ocr(mmc
, mmc
->supply
.vmmc
,
1203 dev_err(slot
->host
->dev
,
1204 "failed to enable vmmc regulator\n");
1205 /*return, if failed turn on vmmc*/
1209 set_bit(DW_MMC_CARD_NEED_INIT
, &slot
->flags
);
1210 regs
= mci_readl(slot
->host
, PWREN
);
1211 regs
|= (1 << slot
->id
);
1212 mci_writel(slot
->host
, PWREN
, regs
);
1215 if (!slot
->host
->vqmmc_enabled
) {
1216 if (!IS_ERR(mmc
->supply
.vqmmc
)) {
1217 ret
= regulator_enable(mmc
->supply
.vqmmc
);
1219 dev_err(slot
->host
->dev
,
1220 "failed to enable vqmmc\n");
1222 slot
->host
->vqmmc_enabled
= true;
1225 /* Keep track so we don't reset again */
1226 slot
->host
->vqmmc_enabled
= true;
1229 /* Reset our state machine after powering on */
1230 dw_mci_ctrl_reset(slot
->host
,
1231 SDMMC_CTRL_ALL_RESET_FLAGS
);
1234 /* Adjust clock / bus width after power is up */
1235 dw_mci_setup_bus(slot
, false);
1239 /* Turn clock off before power goes down */
1240 dw_mci_setup_bus(slot
, false);
1242 if (!IS_ERR(mmc
->supply
.vmmc
))
1243 mmc_regulator_set_ocr(mmc
, mmc
->supply
.vmmc
, 0);
1245 if (!IS_ERR(mmc
->supply
.vqmmc
) && slot
->host
->vqmmc_enabled
)
1246 regulator_disable(mmc
->supply
.vqmmc
);
1247 slot
->host
->vqmmc_enabled
= false;
1249 regs
= mci_readl(slot
->host
, PWREN
);
1250 regs
&= ~(1 << slot
->id
);
1251 mci_writel(slot
->host
, PWREN
, regs
);
1257 if (slot
->host
->state
== STATE_WAITING_CMD11_DONE
&& ios
->clock
!= 0)
1258 slot
->host
->state
= STATE_IDLE
;
1261 static int dw_mci_card_busy(struct mmc_host
*mmc
)
1263 struct dw_mci_slot
*slot
= mmc_priv(mmc
);
1267 * Check the busy bit which is low when DAT[3:0]
1268 * (the data lines) are 0000
1270 status
= mci_readl(slot
->host
, STATUS
);
1272 return !!(status
& SDMMC_STATUS_BUSY
);
1275 static int dw_mci_switch_voltage(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
1277 struct dw_mci_slot
*slot
= mmc_priv(mmc
);
1278 struct dw_mci
*host
= slot
->host
;
1279 const struct dw_mci_drv_data
*drv_data
= host
->drv_data
;
1281 u32 v18
= SDMMC_UHS_18V
<< slot
->id
;
1285 if (drv_data
&& drv_data
->switch_voltage
)
1286 return drv_data
->switch_voltage(mmc
, ios
);
1289 * Program the voltage. Note that some instances of dw_mmc may use
1290 * the UHS_REG for this. For other instances (like exynos) the UHS_REG
1291 * does no harm but you need to set the regulator directly. Try both.
1293 uhs
= mci_readl(host
, UHS_REG
);
1294 if (ios
->signal_voltage
== MMC_SIGNAL_VOLTAGE_330
) {
1303 if (!IS_ERR(mmc
->supply
.vqmmc
)) {
1304 ret
= regulator_set_voltage(mmc
->supply
.vqmmc
, min_uv
, max_uv
);
1307 dev_dbg(&mmc
->class_dev
,
1308 "Regulator set error %d: %d - %d\n",
1309 ret
, min_uv
, max_uv
);
1313 mci_writel(host
, UHS_REG
, uhs
);
1318 static int dw_mci_get_ro(struct mmc_host
*mmc
)
1321 struct dw_mci_slot
*slot
= mmc_priv(mmc
);
1322 int gpio_ro
= mmc_gpio_get_ro(mmc
);
1324 /* Use platform get_ro function, else try on board write protect */
1325 if (!IS_ERR_VALUE(gpio_ro
))
1326 read_only
= gpio_ro
;
1329 mci_readl(slot
->host
, WRTPRT
) & (1 << slot
->id
) ? 1 : 0;
1331 dev_dbg(&mmc
->class_dev
, "card is %s\n",
1332 read_only
? "read-only" : "read-write");
1337 static int dw_mci_get_cd(struct mmc_host
*mmc
)
1340 struct dw_mci_slot
*slot
= mmc_priv(mmc
);
1341 struct dw_mci_board
*brd
= slot
->host
->pdata
;
1342 struct dw_mci
*host
= slot
->host
;
1343 int gpio_cd
= mmc_gpio_get_cd(mmc
);
1345 /* Use platform get_cd function, else try onboard card detect */
1346 if ((brd
->quirks
& DW_MCI_QUIRK_BROKEN_CARD_DETECTION
) ||
1347 (mmc
->caps
& MMC_CAP_NONREMOVABLE
))
1349 else if (!IS_ERR_VALUE(gpio_cd
))
1352 present
= (mci_readl(slot
->host
, CDETECT
) & (1 << slot
->id
))
1355 spin_lock_bh(&host
->lock
);
1357 set_bit(DW_MMC_CARD_PRESENT
, &slot
->flags
);
1358 dev_dbg(&mmc
->class_dev
, "card is present\n");
1360 clear_bit(DW_MMC_CARD_PRESENT
, &slot
->flags
);
1361 dev_dbg(&mmc
->class_dev
, "card is not present\n");
1363 spin_unlock_bh(&host
->lock
);
1368 static void dw_mci_init_card(struct mmc_host
*mmc
, struct mmc_card
*card
)
1370 struct dw_mci_slot
*slot
= mmc_priv(mmc
);
1371 struct dw_mci
*host
= slot
->host
;
1374 * Low power mode will stop the card clock when idle. According to the
1375 * description of the CLKENA register we should disable low power mode
1376 * for SDIO cards if we need SDIO interrupts to work.
1378 if (mmc
->caps
& MMC_CAP_SDIO_IRQ
) {
1379 const u32 clken_low_pwr
= SDMMC_CLKEN_LOW_PWR
<< slot
->id
;
1383 clk_en_a_old
= mci_readl(host
, CLKENA
);
1385 if (card
->type
== MMC_TYPE_SDIO
||
1386 card
->type
== MMC_TYPE_SD_COMBO
) {
1387 set_bit(DW_MMC_CARD_NO_LOW_PWR
, &slot
->flags
);
1388 clk_en_a
= clk_en_a_old
& ~clken_low_pwr
;
1390 clear_bit(DW_MMC_CARD_NO_LOW_PWR
, &slot
->flags
);
1391 clk_en_a
= clk_en_a_old
| clken_low_pwr
;
1394 if (clk_en_a
!= clk_en_a_old
) {
1395 mci_writel(host
, CLKENA
, clk_en_a
);
1396 mci_send_cmd(slot
, SDMMC_CMD_UPD_CLK
|
1397 SDMMC_CMD_PRV_DAT_WAIT
, 0);
1402 static void dw_mci_enable_sdio_irq(struct mmc_host
*mmc
, int enb
)
1404 struct dw_mci_slot
*slot
= mmc_priv(mmc
);
1405 struct dw_mci
*host
= slot
->host
;
1406 unsigned long irqflags
;
1409 spin_lock_irqsave(&host
->irq_lock
, irqflags
);
1411 /* Enable/disable Slot Specific SDIO interrupt */
1412 int_mask
= mci_readl(host
, INTMASK
);
1414 int_mask
|= SDMMC_INT_SDIO(slot
->sdio_id
);
1416 int_mask
&= ~SDMMC_INT_SDIO(slot
->sdio_id
);
1417 mci_writel(host
, INTMASK
, int_mask
);
1419 spin_unlock_irqrestore(&host
->irq_lock
, irqflags
);
1422 static int dw_mci_execute_tuning(struct mmc_host
*mmc
, u32 opcode
)
1424 struct dw_mci_slot
*slot
= mmc_priv(mmc
);
1425 struct dw_mci
*host
= slot
->host
;
1426 const struct dw_mci_drv_data
*drv_data
= host
->drv_data
;
1429 if (drv_data
&& drv_data
->execute_tuning
)
1430 err
= drv_data
->execute_tuning(slot
);
1434 static int dw_mci_prepare_hs400_tuning(struct mmc_host
*mmc
,
1435 struct mmc_ios
*ios
)
1437 struct dw_mci_slot
*slot
= mmc_priv(mmc
);
1438 struct dw_mci
*host
= slot
->host
;
1439 const struct dw_mci_drv_data
*drv_data
= host
->drv_data
;
1441 if (drv_data
&& drv_data
->prepare_hs400_tuning
)
1442 return drv_data
->prepare_hs400_tuning(host
, ios
);
1447 static const struct mmc_host_ops dw_mci_ops
= {
1448 .request
= dw_mci_request
,
1449 .pre_req
= dw_mci_pre_req
,
1450 .post_req
= dw_mci_post_req
,
1451 .set_ios
= dw_mci_set_ios
,
1452 .get_ro
= dw_mci_get_ro
,
1453 .get_cd
= dw_mci_get_cd
,
1454 .enable_sdio_irq
= dw_mci_enable_sdio_irq
,
1455 .execute_tuning
= dw_mci_execute_tuning
,
1456 .card_busy
= dw_mci_card_busy
,
1457 .start_signal_voltage_switch
= dw_mci_switch_voltage
,
1458 .init_card
= dw_mci_init_card
,
1459 .prepare_hs400_tuning
= dw_mci_prepare_hs400_tuning
,
1462 static void dw_mci_request_end(struct dw_mci
*host
, struct mmc_request
*mrq
)
1463 __releases(&host
->lock
)
1464 __acquires(&host
->lock
)
1466 struct dw_mci_slot
*slot
;
1467 struct mmc_host
*prev_mmc
= host
->cur_slot
->mmc
;
1469 WARN_ON(host
->cmd
|| host
->data
);
1471 host
->cur_slot
->mrq
= NULL
;
1473 if (!list_empty(&host
->queue
)) {
1474 slot
= list_entry(host
->queue
.next
,
1475 struct dw_mci_slot
, queue_node
);
1476 list_del(&slot
->queue_node
);
1477 dev_vdbg(host
->dev
, "list not empty: %s is next\n",
1478 mmc_hostname(slot
->mmc
));
1479 host
->state
= STATE_SENDING_CMD
;
1480 dw_mci_start_request(host
, slot
);
1482 dev_vdbg(host
->dev
, "list empty\n");
1484 if (host
->state
== STATE_SENDING_CMD11
)
1485 host
->state
= STATE_WAITING_CMD11_DONE
;
1487 host
->state
= STATE_IDLE
;
1490 spin_unlock(&host
->lock
);
1491 mmc_request_done(prev_mmc
, mrq
);
1492 spin_lock(&host
->lock
);
1495 static int dw_mci_command_complete(struct dw_mci
*host
, struct mmc_command
*cmd
)
1497 u32 status
= host
->cmd_status
;
1499 host
->cmd_status
= 0;
1501 /* Read the response from the card (up to 16 bytes) */
1502 if (cmd
->flags
& MMC_RSP_PRESENT
) {
1503 if (cmd
->flags
& MMC_RSP_136
) {
1504 cmd
->resp
[3] = mci_readl(host
, RESP0
);
1505 cmd
->resp
[2] = mci_readl(host
, RESP1
);
1506 cmd
->resp
[1] = mci_readl(host
, RESP2
);
1507 cmd
->resp
[0] = mci_readl(host
, RESP3
);
1509 cmd
->resp
[0] = mci_readl(host
, RESP0
);
1516 if (status
& SDMMC_INT_RTO
)
1517 cmd
->error
= -ETIMEDOUT
;
1518 else if ((cmd
->flags
& MMC_RSP_CRC
) && (status
& SDMMC_INT_RCRC
))
1519 cmd
->error
= -EILSEQ
;
1520 else if (status
& SDMMC_INT_RESP_ERR
)
1526 /* newer ip versions need a delay between retries */
1527 if (host
->quirks
& DW_MCI_QUIRK_RETRY_DELAY
)
1534 static int dw_mci_data_complete(struct dw_mci
*host
, struct mmc_data
*data
)
1536 u32 status
= host
->data_status
;
1538 if (status
& DW_MCI_DATA_ERROR_FLAGS
) {
1539 if (status
& SDMMC_INT_DRTO
) {
1540 data
->error
= -ETIMEDOUT
;
1541 } else if (status
& SDMMC_INT_DCRC
) {
1542 data
->error
= -EILSEQ
;
1543 } else if (status
& SDMMC_INT_EBE
) {
1544 if (host
->dir_status
==
1545 DW_MCI_SEND_STATUS
) {
1547 * No data CRC status was returned.
1548 * The number of bytes transferred
1549 * will be exaggerated in PIO mode.
1551 data
->bytes_xfered
= 0;
1552 data
->error
= -ETIMEDOUT
;
1553 } else if (host
->dir_status
==
1554 DW_MCI_RECV_STATUS
) {
1558 /* SDMMC_INT_SBE is included */
1562 dev_dbg(host
->dev
, "data error, status 0x%08x\n", status
);
1565 * After an error, there may be data lingering
1570 data
->bytes_xfered
= data
->blocks
* data
->blksz
;
1577 static void dw_mci_tasklet_func(unsigned long priv
)
1579 struct dw_mci
*host
= (struct dw_mci
*)priv
;
1580 struct mmc_data
*data
;
1581 struct mmc_command
*cmd
;
1582 struct mmc_request
*mrq
;
1583 enum dw_mci_state state
;
1584 enum dw_mci_state prev_state
;
1587 spin_lock(&host
->lock
);
1589 state
= host
->state
;
1598 case STATE_WAITING_CMD11_DONE
:
1601 case STATE_SENDING_CMD11
:
1602 case STATE_SENDING_CMD
:
1603 if (!test_and_clear_bit(EVENT_CMD_COMPLETE
,
1604 &host
->pending_events
))
1609 set_bit(EVENT_CMD_COMPLETE
, &host
->completed_events
);
1610 err
= dw_mci_command_complete(host
, cmd
);
1611 if (cmd
== mrq
->sbc
&& !err
) {
1612 prev_state
= state
= STATE_SENDING_CMD
;
1613 __dw_mci_start_request(host
, host
->cur_slot
,
1618 if (cmd
->data
&& err
) {
1619 dw_mci_stop_dma(host
);
1620 send_stop_abort(host
, data
);
1621 state
= STATE_SENDING_STOP
;
1625 if (!cmd
->data
|| err
) {
1626 dw_mci_request_end(host
, mrq
);
1630 prev_state
= state
= STATE_SENDING_DATA
;
1633 case STATE_SENDING_DATA
:
1635 * We could get a data error and never a transfer
1636 * complete so we'd better check for it here.
1638 * Note that we don't really care if we also got a
1639 * transfer complete; stopping the DMA and sending an
1642 if (test_and_clear_bit(EVENT_DATA_ERROR
,
1643 &host
->pending_events
)) {
1644 dw_mci_stop_dma(host
);
1646 !(host
->data_status
& (SDMMC_INT_DRTO
|
1648 send_stop_abort(host
, data
);
1649 state
= STATE_DATA_ERROR
;
1653 if (!test_and_clear_bit(EVENT_XFER_COMPLETE
,
1654 &host
->pending_events
))
1657 set_bit(EVENT_XFER_COMPLETE
, &host
->completed_events
);
1660 * Handle an EVENT_DATA_ERROR that might have shown up
1661 * before the transfer completed. This might not have
1662 * been caught by the check above because the interrupt
1663 * could have gone off between the previous check and
1664 * the check for transfer complete.
1666 * Technically this ought not be needed assuming we
1667 * get a DATA_COMPLETE eventually (we'll notice the
1668 * error and end the request), but it shouldn't hurt.
1670 * This has the advantage of sending the stop command.
1672 if (test_and_clear_bit(EVENT_DATA_ERROR
,
1673 &host
->pending_events
)) {
1674 dw_mci_stop_dma(host
);
1676 !(host
->data_status
& (SDMMC_INT_DRTO
|
1678 send_stop_abort(host
, data
);
1679 state
= STATE_DATA_ERROR
;
1682 prev_state
= state
= STATE_DATA_BUSY
;
1686 case STATE_DATA_BUSY
:
1687 if (!test_and_clear_bit(EVENT_DATA_COMPLETE
,
1688 &host
->pending_events
))
1692 set_bit(EVENT_DATA_COMPLETE
, &host
->completed_events
);
1693 err
= dw_mci_data_complete(host
, data
);
1696 if (!data
->stop
|| mrq
->sbc
) {
1697 if (mrq
->sbc
&& data
->stop
)
1698 data
->stop
->error
= 0;
1699 dw_mci_request_end(host
, mrq
);
1703 /* stop command for open-ended transfer*/
1705 send_stop_abort(host
, data
);
1708 * If we don't have a command complete now we'll
1709 * never get one since we just reset everything;
1710 * better end the request.
1712 * If we do have a command complete we'll fall
1713 * through to the SENDING_STOP command and
1714 * everything will be peachy keen.
1716 if (!test_bit(EVENT_CMD_COMPLETE
,
1717 &host
->pending_events
)) {
1719 dw_mci_request_end(host
, mrq
);
1725 * If err has non-zero,
1726 * stop-abort command has been already issued.
1728 prev_state
= state
= STATE_SENDING_STOP
;
1732 case STATE_SENDING_STOP
:
1733 if (!test_and_clear_bit(EVENT_CMD_COMPLETE
,
1734 &host
->pending_events
))
1737 /* CMD error in data command */
1738 if (mrq
->cmd
->error
&& mrq
->data
)
1745 dw_mci_command_complete(host
, mrq
->stop
);
1747 host
->cmd_status
= 0;
1749 dw_mci_request_end(host
, mrq
);
1752 case STATE_DATA_ERROR
:
1753 if (!test_and_clear_bit(EVENT_XFER_COMPLETE
,
1754 &host
->pending_events
))
1757 state
= STATE_DATA_BUSY
;
1760 } while (state
!= prev_state
);
1762 host
->state
= state
;
1764 spin_unlock(&host
->lock
);
1768 /* push final bytes to part_buf, only use during push */
1769 static void dw_mci_set_part_bytes(struct dw_mci
*host
, void *buf
, int cnt
)
1771 memcpy((void *)&host
->part_buf
, buf
, cnt
);
1772 host
->part_buf_count
= cnt
;
1775 /* append bytes to part_buf, only use during push */
1776 static int dw_mci_push_part_bytes(struct dw_mci
*host
, void *buf
, int cnt
)
1778 cnt
= min(cnt
, (1 << host
->data_shift
) - host
->part_buf_count
);
1779 memcpy((void *)&host
->part_buf
+ host
->part_buf_count
, buf
, cnt
);
1780 host
->part_buf_count
+= cnt
;
1784 /* pull first bytes from part_buf, only use during pull */
1785 static int dw_mci_pull_part_bytes(struct dw_mci
*host
, void *buf
, int cnt
)
1787 cnt
= min_t(int, cnt
, host
->part_buf_count
);
1789 memcpy(buf
, (void *)&host
->part_buf
+ host
->part_buf_start
,
1791 host
->part_buf_count
-= cnt
;
1792 host
->part_buf_start
+= cnt
;
1797 /* pull final bytes from the part_buf, assuming it's just been filled */
1798 static void dw_mci_pull_final_bytes(struct dw_mci
*host
, void *buf
, int cnt
)
1800 memcpy(buf
, &host
->part_buf
, cnt
);
1801 host
->part_buf_start
= cnt
;
1802 host
->part_buf_count
= (1 << host
->data_shift
) - cnt
;
1805 static void dw_mci_push_data16(struct dw_mci
*host
, void *buf
, int cnt
)
1807 struct mmc_data
*data
= host
->data
;
1810 /* try and push anything in the part_buf */
1811 if (unlikely(host
->part_buf_count
)) {
1812 int len
= dw_mci_push_part_bytes(host
, buf
, cnt
);
1816 if (host
->part_buf_count
== 2) {
1817 mci_fifo_writew(host
->fifo_reg
, host
->part_buf16
);
1818 host
->part_buf_count
= 0;
1821 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1822 if (unlikely((unsigned long)buf
& 0x1)) {
1824 u16 aligned_buf
[64];
1825 int len
= min(cnt
& -2, (int)sizeof(aligned_buf
));
1826 int items
= len
>> 1;
1828 /* memcpy from input buffer into aligned buffer */
1829 memcpy(aligned_buf
, buf
, len
);
1832 /* push data from aligned buffer into fifo */
1833 for (i
= 0; i
< items
; ++i
)
1834 mci_fifo_writew(host
->fifo_reg
, aligned_buf
[i
]);
1841 for (; cnt
>= 2; cnt
-= 2)
1842 mci_fifo_writew(host
->fifo_reg
, *pdata
++);
1845 /* put anything remaining in the part_buf */
1847 dw_mci_set_part_bytes(host
, buf
, cnt
);
1848 /* Push data if we have reached the expected data length */
1849 if ((data
->bytes_xfered
+ init_cnt
) ==
1850 (data
->blksz
* data
->blocks
))
1851 mci_fifo_writew(host
->fifo_reg
, host
->part_buf16
);
1855 static void dw_mci_pull_data16(struct dw_mci
*host
, void *buf
, int cnt
)
1857 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1858 if (unlikely((unsigned long)buf
& 0x1)) {
1860 /* pull data from fifo into aligned buffer */
1861 u16 aligned_buf
[64];
1862 int len
= min(cnt
& -2, (int)sizeof(aligned_buf
));
1863 int items
= len
>> 1;
1866 for (i
= 0; i
< items
; ++i
)
1867 aligned_buf
[i
] = mci_fifo_readw(host
->fifo_reg
);
1868 /* memcpy from aligned buffer into output buffer */
1869 memcpy(buf
, aligned_buf
, len
);
1878 for (; cnt
>= 2; cnt
-= 2)
1879 *pdata
++ = mci_fifo_readw(host
->fifo_reg
);
1883 host
->part_buf16
= mci_fifo_readw(host
->fifo_reg
);
1884 dw_mci_pull_final_bytes(host
, buf
, cnt
);
1888 static void dw_mci_push_data32(struct dw_mci
*host
, void *buf
, int cnt
)
1890 struct mmc_data
*data
= host
->data
;
1893 /* try and push anything in the part_buf */
1894 if (unlikely(host
->part_buf_count
)) {
1895 int len
= dw_mci_push_part_bytes(host
, buf
, cnt
);
1899 if (host
->part_buf_count
== 4) {
1900 mci_fifo_writel(host
->fifo_reg
, host
->part_buf32
);
1901 host
->part_buf_count
= 0;
1904 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1905 if (unlikely((unsigned long)buf
& 0x3)) {
1907 u32 aligned_buf
[32];
1908 int len
= min(cnt
& -4, (int)sizeof(aligned_buf
));
1909 int items
= len
>> 2;
1911 /* memcpy from input buffer into aligned buffer */
1912 memcpy(aligned_buf
, buf
, len
);
1915 /* push data from aligned buffer into fifo */
1916 for (i
= 0; i
< items
; ++i
)
1917 mci_fifo_writel(host
->fifo_reg
, aligned_buf
[i
]);
1924 for (; cnt
>= 4; cnt
-= 4)
1925 mci_fifo_writel(host
->fifo_reg
, *pdata
++);
1928 /* put anything remaining in the part_buf */
1930 dw_mci_set_part_bytes(host
, buf
, cnt
);
1931 /* Push data if we have reached the expected data length */
1932 if ((data
->bytes_xfered
+ init_cnt
) ==
1933 (data
->blksz
* data
->blocks
))
1934 mci_fifo_writel(host
->fifo_reg
, host
->part_buf32
);
1938 static void dw_mci_pull_data32(struct dw_mci
*host
, void *buf
, int cnt
)
1940 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1941 if (unlikely((unsigned long)buf
& 0x3)) {
1943 /* pull data from fifo into aligned buffer */
1944 u32 aligned_buf
[32];
1945 int len
= min(cnt
& -4, (int)sizeof(aligned_buf
));
1946 int items
= len
>> 2;
1949 for (i
= 0; i
< items
; ++i
)
1950 aligned_buf
[i
] = mci_fifo_readl(host
->fifo_reg
);
1951 /* memcpy from aligned buffer into output buffer */
1952 memcpy(buf
, aligned_buf
, len
);
1961 for (; cnt
>= 4; cnt
-= 4)
1962 *pdata
++ = mci_fifo_readl(host
->fifo_reg
);
1966 host
->part_buf32
= mci_fifo_readl(host
->fifo_reg
);
1967 dw_mci_pull_final_bytes(host
, buf
, cnt
);
1971 static void dw_mci_push_data64(struct dw_mci
*host
, void *buf
, int cnt
)
1973 struct mmc_data
*data
= host
->data
;
1976 /* try and push anything in the part_buf */
1977 if (unlikely(host
->part_buf_count
)) {
1978 int len
= dw_mci_push_part_bytes(host
, buf
, cnt
);
1983 if (host
->part_buf_count
== 8) {
1984 mci_fifo_writeq(host
->fifo_reg
, host
->part_buf
);
1985 host
->part_buf_count
= 0;
1988 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1989 if (unlikely((unsigned long)buf
& 0x7)) {
1991 u64 aligned_buf
[16];
1992 int len
= min(cnt
& -8, (int)sizeof(aligned_buf
));
1993 int items
= len
>> 3;
1995 /* memcpy from input buffer into aligned buffer */
1996 memcpy(aligned_buf
, buf
, len
);
1999 /* push data from aligned buffer into fifo */
2000 for (i
= 0; i
< items
; ++i
)
2001 mci_fifo_writeq(host
->fifo_reg
, aligned_buf
[i
]);
2008 for (; cnt
>= 8; cnt
-= 8)
2009 mci_fifo_writeq(host
->fifo_reg
, *pdata
++);
2012 /* put anything remaining in the part_buf */
2014 dw_mci_set_part_bytes(host
, buf
, cnt
);
2015 /* Push data if we have reached the expected data length */
2016 if ((data
->bytes_xfered
+ init_cnt
) ==
2017 (data
->blksz
* data
->blocks
))
2018 mci_fifo_writeq(host
->fifo_reg
, host
->part_buf
);
2022 static void dw_mci_pull_data64(struct dw_mci
*host
, void *buf
, int cnt
)
2024 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2025 if (unlikely((unsigned long)buf
& 0x7)) {
2027 /* pull data from fifo into aligned buffer */
2028 u64 aligned_buf
[16];
2029 int len
= min(cnt
& -8, (int)sizeof(aligned_buf
));
2030 int items
= len
>> 3;
2033 for (i
= 0; i
< items
; ++i
)
2034 aligned_buf
[i
] = mci_fifo_readq(host
->fifo_reg
);
2036 /* memcpy from aligned buffer into output buffer */
2037 memcpy(buf
, aligned_buf
, len
);
2046 for (; cnt
>= 8; cnt
-= 8)
2047 *pdata
++ = mci_fifo_readq(host
->fifo_reg
);
2051 host
->part_buf
= mci_fifo_readq(host
->fifo_reg
);
2052 dw_mci_pull_final_bytes(host
, buf
, cnt
);
2056 static void dw_mci_pull_data(struct dw_mci
*host
, void *buf
, int cnt
)
2060 /* get remaining partial bytes */
2061 len
= dw_mci_pull_part_bytes(host
, buf
, cnt
);
2062 if (unlikely(len
== cnt
))
2067 /* get the rest of the data */
2068 host
->pull_data(host
, buf
, cnt
);
2071 static void dw_mci_read_data_pio(struct dw_mci
*host
, bool dto
)
2073 struct sg_mapping_iter
*sg_miter
= &host
->sg_miter
;
2075 unsigned int offset
;
2076 struct mmc_data
*data
= host
->data
;
2077 int shift
= host
->data_shift
;
2080 unsigned int remain
, fcnt
;
2083 if (!sg_miter_next(sg_miter
))
2086 host
->sg
= sg_miter
->piter
.sg
;
2087 buf
= sg_miter
->addr
;
2088 remain
= sg_miter
->length
;
2092 fcnt
= (SDMMC_GET_FCNT(mci_readl(host
, STATUS
))
2093 << shift
) + host
->part_buf_count
;
2094 len
= min(remain
, fcnt
);
2097 dw_mci_pull_data(host
, (void *)(buf
+ offset
), len
);
2098 data
->bytes_xfered
+= len
;
2103 sg_miter
->consumed
= offset
;
2104 status
= mci_readl(host
, MINTSTS
);
2105 mci_writel(host
, RINTSTS
, SDMMC_INT_RXDR
);
2106 /* if the RXDR is ready read again */
2107 } while ((status
& SDMMC_INT_RXDR
) ||
2108 (dto
&& SDMMC_GET_FCNT(mci_readl(host
, STATUS
))));
2111 if (!sg_miter_next(sg_miter
))
2113 sg_miter
->consumed
= 0;
2115 sg_miter_stop(sg_miter
);
2119 sg_miter_stop(sg_miter
);
2121 smp_wmb(); /* drain writebuffer */
2122 set_bit(EVENT_XFER_COMPLETE
, &host
->pending_events
);
2125 static void dw_mci_write_data_pio(struct dw_mci
*host
)
2127 struct sg_mapping_iter
*sg_miter
= &host
->sg_miter
;
2129 unsigned int offset
;
2130 struct mmc_data
*data
= host
->data
;
2131 int shift
= host
->data_shift
;
2134 unsigned int fifo_depth
= host
->fifo_depth
;
2135 unsigned int remain
, fcnt
;
2138 if (!sg_miter_next(sg_miter
))
2141 host
->sg
= sg_miter
->piter
.sg
;
2142 buf
= sg_miter
->addr
;
2143 remain
= sg_miter
->length
;
2147 fcnt
= ((fifo_depth
-
2148 SDMMC_GET_FCNT(mci_readl(host
, STATUS
)))
2149 << shift
) - host
->part_buf_count
;
2150 len
= min(remain
, fcnt
);
2153 host
->push_data(host
, (void *)(buf
+ offset
), len
);
2154 data
->bytes_xfered
+= len
;
2159 sg_miter
->consumed
= offset
;
2160 status
= mci_readl(host
, MINTSTS
);
2161 mci_writel(host
, RINTSTS
, SDMMC_INT_TXDR
);
2162 } while (status
& SDMMC_INT_TXDR
); /* if TXDR write again */
2165 if (!sg_miter_next(sg_miter
))
2167 sg_miter
->consumed
= 0;
2169 sg_miter_stop(sg_miter
);
2173 sg_miter_stop(sg_miter
);
2175 smp_wmb(); /* drain writebuffer */
2176 set_bit(EVENT_XFER_COMPLETE
, &host
->pending_events
);
2179 static void dw_mci_cmd_interrupt(struct dw_mci
*host
, u32 status
)
2181 if (!host
->cmd_status
)
2182 host
->cmd_status
= status
;
2184 smp_wmb(); /* drain writebuffer */
2186 set_bit(EVENT_CMD_COMPLETE
, &host
->pending_events
);
2187 tasklet_schedule(&host
->tasklet
);
2190 static void dw_mci_handle_cd(struct dw_mci
*host
)
2194 for (i
= 0; i
< host
->num_slots
; i
++) {
2195 struct dw_mci_slot
*slot
= host
->slot
[i
];
2200 if (slot
->mmc
->ops
->card_event
)
2201 slot
->mmc
->ops
->card_event(slot
->mmc
);
2202 mmc_detect_change(slot
->mmc
,
2203 msecs_to_jiffies(host
->pdata
->detect_delay_ms
));
2207 static irqreturn_t
dw_mci_interrupt(int irq
, void *dev_id
)
2209 struct dw_mci
*host
= dev_id
;
2213 pending
= mci_readl(host
, MINTSTS
); /* read-only mask reg */
2216 * DTO fix - version 2.10a and below, and only if internal DMA
2219 if (host
->quirks
& DW_MCI_QUIRK_IDMAC_DTO
) {
2221 ((mci_readl(host
, STATUS
) >> 17) & 0x1fff))
2222 pending
|= SDMMC_INT_DATA_OVER
;
2226 /* Check volt switch first, since it can look like an error */
2227 if ((host
->state
== STATE_SENDING_CMD11
) &&
2228 (pending
& SDMMC_INT_VOLT_SWITCH
)) {
2229 unsigned long irqflags
;
2231 mci_writel(host
, RINTSTS
, SDMMC_INT_VOLT_SWITCH
);
2232 pending
&= ~SDMMC_INT_VOLT_SWITCH
;
2235 * Hold the lock; we know cmd11_timer can't be kicked
2236 * off after the lock is released, so safe to delete.
2238 spin_lock_irqsave(&host
->irq_lock
, irqflags
);
2239 dw_mci_cmd_interrupt(host
, pending
);
2240 spin_unlock_irqrestore(&host
->irq_lock
, irqflags
);
2242 del_timer(&host
->cmd11_timer
);
2245 if (pending
& DW_MCI_CMD_ERROR_FLAGS
) {
2246 mci_writel(host
, RINTSTS
, DW_MCI_CMD_ERROR_FLAGS
);
2247 host
->cmd_status
= pending
;
2248 smp_wmb(); /* drain writebuffer */
2249 set_bit(EVENT_CMD_COMPLETE
, &host
->pending_events
);
2252 if (pending
& DW_MCI_DATA_ERROR_FLAGS
) {
2253 /* if there is an error report DATA_ERROR */
2254 mci_writel(host
, RINTSTS
, DW_MCI_DATA_ERROR_FLAGS
);
2255 host
->data_status
= pending
;
2256 smp_wmb(); /* drain writebuffer */
2257 set_bit(EVENT_DATA_ERROR
, &host
->pending_events
);
2258 tasklet_schedule(&host
->tasklet
);
2261 if (pending
& SDMMC_INT_DATA_OVER
) {
2262 mci_writel(host
, RINTSTS
, SDMMC_INT_DATA_OVER
);
2263 if (!host
->data_status
)
2264 host
->data_status
= pending
;
2265 smp_wmb(); /* drain writebuffer */
2266 if (host
->dir_status
== DW_MCI_RECV_STATUS
) {
2267 if (host
->sg
!= NULL
)
2268 dw_mci_read_data_pio(host
, true);
2270 set_bit(EVENT_DATA_COMPLETE
, &host
->pending_events
);
2271 tasklet_schedule(&host
->tasklet
);
2274 if (pending
& SDMMC_INT_RXDR
) {
2275 mci_writel(host
, RINTSTS
, SDMMC_INT_RXDR
);
2276 if (host
->dir_status
== DW_MCI_RECV_STATUS
&& host
->sg
)
2277 dw_mci_read_data_pio(host
, false);
2280 if (pending
& SDMMC_INT_TXDR
) {
2281 mci_writel(host
, RINTSTS
, SDMMC_INT_TXDR
);
2282 if (host
->dir_status
== DW_MCI_SEND_STATUS
&& host
->sg
)
2283 dw_mci_write_data_pio(host
);
2286 if (pending
& SDMMC_INT_CMD_DONE
) {
2287 mci_writel(host
, RINTSTS
, SDMMC_INT_CMD_DONE
);
2288 dw_mci_cmd_interrupt(host
, pending
);
2291 if (pending
& SDMMC_INT_CD
) {
2292 mci_writel(host
, RINTSTS
, SDMMC_INT_CD
);
2293 dw_mci_handle_cd(host
);
2296 /* Handle SDIO Interrupts */
2297 for (i
= 0; i
< host
->num_slots
; i
++) {
2298 struct dw_mci_slot
*slot
= host
->slot
[i
];
2303 if (pending
& SDMMC_INT_SDIO(slot
->sdio_id
)) {
2304 mci_writel(host
, RINTSTS
,
2305 SDMMC_INT_SDIO(slot
->sdio_id
));
2306 mmc_signal_sdio_irq(slot
->mmc
);
2312 #ifdef CONFIG_MMC_DW_IDMAC
2313 /* Handle DMA interrupts */
2314 if (host
->dma_64bit_address
== 1) {
2315 pending
= mci_readl(host
, IDSTS64
);
2316 if (pending
& (SDMMC_IDMAC_INT_TI
| SDMMC_IDMAC_INT_RI
)) {
2317 mci_writel(host
, IDSTS64
, SDMMC_IDMAC_INT_TI
|
2318 SDMMC_IDMAC_INT_RI
);
2319 mci_writel(host
, IDSTS64
, SDMMC_IDMAC_INT_NI
);
2320 host
->dma_ops
->complete(host
);
2323 pending
= mci_readl(host
, IDSTS
);
2324 if (pending
& (SDMMC_IDMAC_INT_TI
| SDMMC_IDMAC_INT_RI
)) {
2325 mci_writel(host
, IDSTS
, SDMMC_IDMAC_INT_TI
|
2326 SDMMC_IDMAC_INT_RI
);
2327 mci_writel(host
, IDSTS
, SDMMC_IDMAC_INT_NI
);
2328 host
->dma_ops
->complete(host
);
2337 /* given a slot, find out the device node representing that slot */
2338 static struct device_node
*dw_mci_of_find_slot_node(struct dw_mci_slot
*slot
)
2340 struct device
*dev
= slot
->mmc
->parent
;
2341 struct device_node
*np
;
2345 if (!dev
|| !dev
->of_node
)
2348 for_each_child_of_node(dev
->of_node
, np
) {
2349 addr
= of_get_property(np
, "reg", &len
);
2350 if (!addr
|| (len
< sizeof(int)))
2352 if (be32_to_cpup(addr
) == slot
->id
)
2358 static void dw_mci_slot_of_parse(struct dw_mci_slot
*slot
)
2360 struct device_node
*np
= dw_mci_of_find_slot_node(slot
);
2365 if (of_property_read_bool(np
, "disable-wp")) {
2366 slot
->mmc
->caps2
|= MMC_CAP2_NO_WRITE_PROTECT
;
2367 dev_warn(slot
->mmc
->parent
,
2368 "Slot quirk 'disable-wp' is deprecated\n");
2371 #else /* CONFIG_OF */
2372 static void dw_mci_slot_of_parse(struct dw_mci_slot
*slot
)
2375 #endif /* CONFIG_OF */
2377 static int dw_mci_init_slot(struct dw_mci
*host
, unsigned int id
)
2379 struct mmc_host
*mmc
;
2380 struct dw_mci_slot
*slot
;
2381 const struct dw_mci_drv_data
*drv_data
= host
->drv_data
;
2385 mmc
= mmc_alloc_host(sizeof(struct dw_mci_slot
), host
->dev
);
2389 slot
= mmc_priv(mmc
);
2391 slot
->sdio_id
= host
->sdio_id0
+ id
;
2394 host
->slot
[id
] = slot
;
2396 mmc
->ops
= &dw_mci_ops
;
2397 if (of_property_read_u32_array(host
->dev
->of_node
,
2398 "clock-freq-min-max", freq
, 2)) {
2399 mmc
->f_min
= DW_MCI_FREQ_MIN
;
2400 mmc
->f_max
= DW_MCI_FREQ_MAX
;
2402 mmc
->f_min
= freq
[0];
2403 mmc
->f_max
= freq
[1];
2406 /*if there are external regulators, get them*/
2407 ret
= mmc_regulator_get_supply(mmc
);
2408 if (ret
== -EPROBE_DEFER
)
2409 goto err_host_allocated
;
2411 if (!mmc
->ocr_avail
)
2412 mmc
->ocr_avail
= MMC_VDD_32_33
| MMC_VDD_33_34
;
2414 if (host
->pdata
->caps
)
2415 mmc
->caps
= host
->pdata
->caps
;
2417 if (host
->pdata
->pm_caps
)
2418 mmc
->pm_caps
= host
->pdata
->pm_caps
;
2420 if (host
->dev
->of_node
) {
2421 ctrl_id
= of_alias_get_id(host
->dev
->of_node
, "mshc");
2425 ctrl_id
= to_platform_device(host
->dev
)->id
;
2427 if (drv_data
&& drv_data
->caps
)
2428 mmc
->caps
|= drv_data
->caps
[ctrl_id
];
2430 if (host
->pdata
->caps2
)
2431 mmc
->caps2
= host
->pdata
->caps2
;
2433 dw_mci_slot_of_parse(slot
);
2435 ret
= mmc_of_parse(mmc
);
2437 goto err_host_allocated
;
2439 if (host
->pdata
->blk_settings
) {
2440 mmc
->max_segs
= host
->pdata
->blk_settings
->max_segs
;
2441 mmc
->max_blk_size
= host
->pdata
->blk_settings
->max_blk_size
;
2442 mmc
->max_blk_count
= host
->pdata
->blk_settings
->max_blk_count
;
2443 mmc
->max_req_size
= host
->pdata
->blk_settings
->max_req_size
;
2444 mmc
->max_seg_size
= host
->pdata
->blk_settings
->max_seg_size
;
2446 /* Useful defaults if platform data is unset. */
2447 #ifdef CONFIG_MMC_DW_IDMAC
2448 mmc
->max_segs
= host
->ring_size
;
2449 mmc
->max_blk_size
= 65536;
2450 mmc
->max_seg_size
= DW_MCI_DESC_DATA_LENGTH
;
2451 mmc
->max_req_size
= mmc
->max_seg_size
* host
->ring_size
;
2452 mmc
->max_blk_count
= mmc
->max_req_size
/ 512;
2455 mmc
->max_blk_size
= 65536; /* BLKSIZ is 16 bits */
2456 mmc
->max_blk_count
= 512;
2457 mmc
->max_req_size
= mmc
->max_blk_size
* mmc
->max_blk_count
;
2458 mmc
->max_seg_size
= mmc
->max_req_size
;
2459 #endif /* CONFIG_MMC_DW_IDMAC */
2462 if (dw_mci_get_cd(mmc
))
2463 set_bit(DW_MMC_CARD_PRESENT
, &slot
->flags
);
2465 clear_bit(DW_MMC_CARD_PRESENT
, &slot
->flags
);
2467 ret
= mmc_add_host(mmc
);
2469 goto err_host_allocated
;
2471 #if defined(CONFIG_DEBUG_FS)
2472 dw_mci_init_debugfs(slot
);
2482 static void dw_mci_cleanup_slot(struct dw_mci_slot
*slot
, unsigned int id
)
2484 /* Debugfs stuff is cleaned up by mmc core */
2485 mmc_remove_host(slot
->mmc
);
2486 slot
->host
->slot
[id
] = NULL
;
2487 mmc_free_host(slot
->mmc
);
2490 static void dw_mci_init_dma(struct dw_mci
*host
)
2493 /* Check ADDR_CONFIG bit in HCON to find IDMAC address bus width */
2494 addr_config
= (mci_readl(host
, HCON
) >> 27) & 0x01;
2496 if (addr_config
== 1) {
2497 /* host supports IDMAC in 64-bit address mode */
2498 host
->dma_64bit_address
= 1;
2499 dev_info(host
->dev
, "IDMAC supports 64-bit address mode.\n");
2500 if (!dma_set_mask(host
->dev
, DMA_BIT_MASK(64)))
2501 dma_set_coherent_mask(host
->dev
, DMA_BIT_MASK(64));
2503 /* host supports IDMAC in 32-bit address mode */
2504 host
->dma_64bit_address
= 0;
2505 dev_info(host
->dev
, "IDMAC supports 32-bit address mode.\n");
2508 /* Alloc memory for sg translation */
2509 host
->sg_cpu
= dmam_alloc_coherent(host
->dev
, PAGE_SIZE
,
2510 &host
->sg_dma
, GFP_KERNEL
);
2511 if (!host
->sg_cpu
) {
2512 dev_err(host
->dev
, "%s: could not alloc DMA memory\n",
2517 /* Determine which DMA interface to use */
2518 #ifdef CONFIG_MMC_DW_IDMAC
2519 host
->dma_ops
= &dw_mci_idmac_ops
;
2520 dev_info(host
->dev
, "Using internal DMA controller.\n");
2526 if (host
->dma_ops
->init
&& host
->dma_ops
->start
&&
2527 host
->dma_ops
->stop
&& host
->dma_ops
->cleanup
) {
2528 if (host
->dma_ops
->init(host
)) {
2529 dev_err(host
->dev
, "%s: Unable to initialize DMA Controller.\n",
2534 dev_err(host
->dev
, "DMA initialization not found.\n");
2542 dev_info(host
->dev
, "Using PIO mode.\n");
2546 static bool dw_mci_ctrl_reset(struct dw_mci
*host
, u32 reset
)
2548 unsigned long timeout
= jiffies
+ msecs_to_jiffies(500);
2551 ctrl
= mci_readl(host
, CTRL
);
2553 mci_writel(host
, CTRL
, ctrl
);
2555 /* wait till resets clear */
2557 ctrl
= mci_readl(host
, CTRL
);
2558 if (!(ctrl
& reset
))
2560 } while (time_before(jiffies
, timeout
));
2563 "Timeout resetting block (ctrl reset %#x)\n",
2569 static bool dw_mci_reset(struct dw_mci
*host
)
2571 u32 flags
= SDMMC_CTRL_RESET
| SDMMC_CTRL_FIFO_RESET
;
2575 * Reseting generates a block interrupt, hence setting
2576 * the scatter-gather pointer to NULL.
2579 sg_miter_stop(&host
->sg_miter
);
2584 flags
|= SDMMC_CTRL_DMA_RESET
;
2586 if (dw_mci_ctrl_reset(host
, flags
)) {
2588 * In all cases we clear the RAWINTS register to clear any
2591 mci_writel(host
, RINTSTS
, 0xFFFFFFFF);
2593 /* if using dma we wait for dma_req to clear */
2594 if (host
->use_dma
) {
2595 unsigned long timeout
= jiffies
+ msecs_to_jiffies(500);
2599 status
= mci_readl(host
, STATUS
);
2600 if (!(status
& SDMMC_STATUS_DMA_REQ
))
2603 } while (time_before(jiffies
, timeout
));
2605 if (status
& SDMMC_STATUS_DMA_REQ
) {
2607 "%s: Timeout waiting for dma_req to clear during reset\n",
2612 /* when using DMA next we reset the fifo again */
2613 if (!dw_mci_ctrl_reset(host
, SDMMC_CTRL_FIFO_RESET
))
2617 /* if the controller reset bit did clear, then set clock regs */
2618 if (!(mci_readl(host
, CTRL
) & SDMMC_CTRL_RESET
)) {
2620 "%s: fifo/dma reset bits didn't clear but ciu was reset, doing clock update\n",
2626 #if IS_ENABLED(CONFIG_MMC_DW_IDMAC)
2627 /* It is also recommended that we reset and reprogram idmac */
2628 dw_mci_idmac_reset(host
);
2634 /* After a CTRL reset we need to have CIU set clock registers */
2635 mci_send_cmd(host
->cur_slot
, SDMMC_CMD_UPD_CLK
, 0);
2640 static void dw_mci_cmd11_timer(unsigned long arg
)
2642 struct dw_mci
*host
= (struct dw_mci
*)arg
;
2644 if (host
->state
!= STATE_SENDING_CMD11
) {
2645 dev_warn(host
->dev
, "Unexpected CMD11 timeout\n");
2649 host
->cmd_status
= SDMMC_INT_RTO
;
2650 set_bit(EVENT_CMD_COMPLETE
, &host
->pending_events
);
2651 tasklet_schedule(&host
->tasklet
);
2655 static struct dw_mci_of_quirks
{
2660 .quirk
= "broken-cd",
2661 .id
= DW_MCI_QUIRK_BROKEN_CARD_DETECTION
,
2665 static struct dw_mci_board
*dw_mci_parse_dt(struct dw_mci
*host
)
2667 struct dw_mci_board
*pdata
;
2668 struct device
*dev
= host
->dev
;
2669 struct device_node
*np
= dev
->of_node
;
2670 const struct dw_mci_drv_data
*drv_data
= host
->drv_data
;
2672 u32 clock_frequency
;
2674 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
2676 return ERR_PTR(-ENOMEM
);
2678 /* find out number of slots supported */
2679 if (of_property_read_u32(dev
->of_node
, "num-slots",
2680 &pdata
->num_slots
)) {
2682 "num-slots property not found, assuming 1 slot is available\n");
2683 pdata
->num_slots
= 1;
2687 for (idx
= 0; idx
< ARRAY_SIZE(of_quirks
); idx
++)
2688 if (of_get_property(np
, of_quirks
[idx
].quirk
, NULL
))
2689 pdata
->quirks
|= of_quirks
[idx
].id
;
2691 if (of_property_read_u32(np
, "fifo-depth", &pdata
->fifo_depth
))
2693 "fifo-depth property not found, using value of FIFOTH register as default\n");
2695 of_property_read_u32(np
, "card-detect-delay", &pdata
->detect_delay_ms
);
2697 if (!of_property_read_u32(np
, "clock-frequency", &clock_frequency
))
2698 pdata
->bus_hz
= clock_frequency
;
2700 if (drv_data
&& drv_data
->parse_dt
) {
2701 ret
= drv_data
->parse_dt(host
);
2703 return ERR_PTR(ret
);
2706 if (of_find_property(np
, "supports-highspeed", NULL
))
2707 pdata
->caps
|= MMC_CAP_SD_HIGHSPEED
| MMC_CAP_MMC_HIGHSPEED
;
2712 #else /* CONFIG_OF */
2713 static struct dw_mci_board
*dw_mci_parse_dt(struct dw_mci
*host
)
2715 return ERR_PTR(-EINVAL
);
2717 #endif /* CONFIG_OF */
2719 static void dw_mci_enable_cd(struct dw_mci
*host
)
2721 struct dw_mci_board
*brd
= host
->pdata
;
2722 unsigned long irqflags
;
2726 /* No need for CD if broken card detection */
2727 if (brd
->quirks
& DW_MCI_QUIRK_BROKEN_CARD_DETECTION
)
2730 /* No need for CD if all slots have a non-error GPIO */
2731 for (i
= 0; i
< host
->num_slots
; i
++) {
2732 struct dw_mci_slot
*slot
= host
->slot
[i
];
2734 if (IS_ERR_VALUE(mmc_gpio_get_cd(slot
->mmc
)))
2737 if (i
== host
->num_slots
)
2740 spin_lock_irqsave(&host
->irq_lock
, irqflags
);
2741 temp
= mci_readl(host
, INTMASK
);
2742 temp
|= SDMMC_INT_CD
;
2743 mci_writel(host
, INTMASK
, temp
);
2744 spin_unlock_irqrestore(&host
->irq_lock
, irqflags
);
2747 int dw_mci_probe(struct dw_mci
*host
)
2749 const struct dw_mci_drv_data
*drv_data
= host
->drv_data
;
2750 int width
, i
, ret
= 0;
2755 host
->pdata
= dw_mci_parse_dt(host
);
2756 if (IS_ERR(host
->pdata
)) {
2757 dev_err(host
->dev
, "platform data not available\n");
2762 if (host
->pdata
->num_slots
> 1) {
2764 "Platform data must supply num_slots.\n");
2768 host
->biu_clk
= devm_clk_get(host
->dev
, "biu");
2769 if (IS_ERR(host
->biu_clk
)) {
2770 dev_dbg(host
->dev
, "biu clock not available\n");
2772 ret
= clk_prepare_enable(host
->biu_clk
);
2774 dev_err(host
->dev
, "failed to enable biu clock\n");
2779 host
->ciu_clk
= devm_clk_get(host
->dev
, "ciu");
2780 if (IS_ERR(host
->ciu_clk
)) {
2781 dev_dbg(host
->dev
, "ciu clock not available\n");
2782 host
->bus_hz
= host
->pdata
->bus_hz
;
2784 ret
= clk_prepare_enable(host
->ciu_clk
);
2786 dev_err(host
->dev
, "failed to enable ciu clock\n");
2790 if (host
->pdata
->bus_hz
) {
2791 ret
= clk_set_rate(host
->ciu_clk
, host
->pdata
->bus_hz
);
2794 "Unable to set bus rate to %uHz\n",
2795 host
->pdata
->bus_hz
);
2797 host
->bus_hz
= clk_get_rate(host
->ciu_clk
);
2800 if (!host
->bus_hz
) {
2802 "Platform data must supply bus speed\n");
2807 if (drv_data
&& drv_data
->init
) {
2808 ret
= drv_data
->init(host
);
2811 "implementation specific init failed\n");
2816 if (drv_data
&& drv_data
->setup_clock
) {
2817 ret
= drv_data
->setup_clock(host
);
2820 "implementation specific clock setup failed\n");
2825 setup_timer(&host
->cmd11_timer
,
2826 dw_mci_cmd11_timer
, (unsigned long)host
);
2828 host
->quirks
= host
->pdata
->quirks
;
2830 spin_lock_init(&host
->lock
);
2831 spin_lock_init(&host
->irq_lock
);
2832 INIT_LIST_HEAD(&host
->queue
);
2835 * Get the host data width - this assumes that HCON has been set with
2836 * the correct values.
2838 i
= (mci_readl(host
, HCON
) >> 7) & 0x7;
2840 host
->push_data
= dw_mci_push_data16
;
2841 host
->pull_data
= dw_mci_pull_data16
;
2843 host
->data_shift
= 1;
2844 } else if (i
== 2) {
2845 host
->push_data
= dw_mci_push_data64
;
2846 host
->pull_data
= dw_mci_pull_data64
;
2848 host
->data_shift
= 3;
2850 /* Check for a reserved value, and warn if it is */
2852 "HCON reports a reserved host data width!\n"
2853 "Defaulting to 32-bit access.\n");
2854 host
->push_data
= dw_mci_push_data32
;
2855 host
->pull_data
= dw_mci_pull_data32
;
2857 host
->data_shift
= 2;
2860 /* Reset all blocks */
2861 if (!dw_mci_ctrl_reset(host
, SDMMC_CTRL_ALL_RESET_FLAGS
))
2864 host
->dma_ops
= host
->pdata
->dma_ops
;
2865 dw_mci_init_dma(host
);
2867 /* Clear the interrupts for the host controller */
2868 mci_writel(host
, RINTSTS
, 0xFFFFFFFF);
2869 mci_writel(host
, INTMASK
, 0); /* disable all mmc interrupt first */
2871 /* Put in max timeout */
2872 mci_writel(host
, TMOUT
, 0xFFFFFFFF);
2875 * FIFO threshold settings RxMark = fifo_size / 2 - 1,
2876 * Tx Mark = fifo_size / 2 DMA Size = 8
2878 if (!host
->pdata
->fifo_depth
) {
2880 * Power-on value of RX_WMark is FIFO_DEPTH-1, but this may
2881 * have been overwritten by the bootloader, just like we're
2882 * about to do, so if you know the value for your hardware, you
2883 * should put it in the platform data.
2885 fifo_size
= mci_readl(host
, FIFOTH
);
2886 fifo_size
= 1 + ((fifo_size
>> 16) & 0xfff);
2888 fifo_size
= host
->pdata
->fifo_depth
;
2890 host
->fifo_depth
= fifo_size
;
2892 SDMMC_SET_FIFOTH(0x2, fifo_size
/ 2 - 1, fifo_size
/ 2);
2893 mci_writel(host
, FIFOTH
, host
->fifoth_val
);
2895 /* disable clock to CIU */
2896 mci_writel(host
, CLKENA
, 0);
2897 mci_writel(host
, CLKSRC
, 0);
2900 * In 2.40a spec, Data offset is changed.
2901 * Need to check the version-id and set data-offset for DATA register.
2903 host
->verid
= SDMMC_GET_VERID(mci_readl(host
, VERID
));
2904 dev_info(host
->dev
, "Version ID is %04x\n", host
->verid
);
2906 if (host
->verid
< DW_MMC_240A
)
2907 host
->fifo_reg
= host
->regs
+ DATA_OFFSET
;
2909 host
->fifo_reg
= host
->regs
+ DATA_240A_OFFSET
;
2911 tasklet_init(&host
->tasklet
, dw_mci_tasklet_func
, (unsigned long)host
);
2912 ret
= devm_request_irq(host
->dev
, host
->irq
, dw_mci_interrupt
,
2913 host
->irq_flags
, "dw-mci", host
);
2917 if (host
->pdata
->num_slots
)
2918 host
->num_slots
= host
->pdata
->num_slots
;
2920 host
->num_slots
= ((mci_readl(host
, HCON
) >> 1) & 0x1F) + 1;
2923 * Enable interrupts for command done, data over, data empty,
2924 * receive ready and error such as transmit, receive timeout, crc error
2926 mci_writel(host
, RINTSTS
, 0xFFFFFFFF);
2927 mci_writel(host
, INTMASK
, SDMMC_INT_CMD_DONE
| SDMMC_INT_DATA_OVER
|
2928 SDMMC_INT_TXDR
| SDMMC_INT_RXDR
|
2929 DW_MCI_ERROR_FLAGS
);
2930 /* Enable mci interrupt */
2931 mci_writel(host
, CTRL
, SDMMC_CTRL_INT_ENABLE
);
2934 "DW MMC controller at irq %d,%d bit host data width,%u deep fifo\n",
2935 host
->irq
, width
, fifo_size
);
2937 /* We need at least one slot to succeed */
2938 for (i
= 0; i
< host
->num_slots
; i
++) {
2939 ret
= dw_mci_init_slot(host
, i
);
2941 dev_dbg(host
->dev
, "slot %d init failed\n", i
);
2947 dev_info(host
->dev
, "%d slots initialized\n", init_slots
);
2950 "attempted to initialize %d slots, but failed on all\n",
2955 /* Now that slots are all setup, we can enable card detect */
2956 dw_mci_enable_cd(host
);
2958 if (host
->quirks
& DW_MCI_QUIRK_IDMAC_DTO
)
2959 dev_info(host
->dev
, "Internal DMAC interrupt fix enabled.\n");
2964 if (host
->use_dma
&& host
->dma_ops
->exit
)
2965 host
->dma_ops
->exit(host
);
2968 if (!IS_ERR(host
->ciu_clk
))
2969 clk_disable_unprepare(host
->ciu_clk
);
2972 if (!IS_ERR(host
->biu_clk
))
2973 clk_disable_unprepare(host
->biu_clk
);
2977 EXPORT_SYMBOL(dw_mci_probe
);
2979 void dw_mci_remove(struct dw_mci
*host
)
2983 for (i
= 0; i
< host
->num_slots
; i
++) {
2984 dev_dbg(host
->dev
, "remove slot %d\n", i
);
2986 dw_mci_cleanup_slot(host
->slot
[i
], i
);
2989 mci_writel(host
, RINTSTS
, 0xFFFFFFFF);
2990 mci_writel(host
, INTMASK
, 0); /* disable all mmc interrupt first */
2992 /* disable clock to CIU */
2993 mci_writel(host
, CLKENA
, 0);
2994 mci_writel(host
, CLKSRC
, 0);
2996 if (host
->use_dma
&& host
->dma_ops
->exit
)
2997 host
->dma_ops
->exit(host
);
2999 if (!IS_ERR(host
->ciu_clk
))
3000 clk_disable_unprepare(host
->ciu_clk
);
3002 if (!IS_ERR(host
->biu_clk
))
3003 clk_disable_unprepare(host
->biu_clk
);
3005 EXPORT_SYMBOL(dw_mci_remove
);
3009 #ifdef CONFIG_PM_SLEEP
3011 * TODO: we should probably disable the clock to the card in the suspend path.
3013 int dw_mci_suspend(struct dw_mci
*host
)
3017 EXPORT_SYMBOL(dw_mci_suspend
);
3019 int dw_mci_resume(struct dw_mci
*host
)
3023 if (!dw_mci_ctrl_reset(host
, SDMMC_CTRL_ALL_RESET_FLAGS
)) {
3028 if (host
->use_dma
&& host
->dma_ops
->init
)
3029 host
->dma_ops
->init(host
);
3032 * Restore the initial value at FIFOTH register
3033 * And Invalidate the prev_blksz with zero
3035 mci_writel(host
, FIFOTH
, host
->fifoth_val
);
3036 host
->prev_blksz
= 0;
3038 /* Put in max timeout */
3039 mci_writel(host
, TMOUT
, 0xFFFFFFFF);
3041 mci_writel(host
, RINTSTS
, 0xFFFFFFFF);
3042 mci_writel(host
, INTMASK
, SDMMC_INT_CMD_DONE
| SDMMC_INT_DATA_OVER
|
3043 SDMMC_INT_TXDR
| SDMMC_INT_RXDR
|
3044 DW_MCI_ERROR_FLAGS
);
3045 mci_writel(host
, CTRL
, SDMMC_CTRL_INT_ENABLE
);
3047 for (i
= 0; i
< host
->num_slots
; i
++) {
3048 struct dw_mci_slot
*slot
= host
->slot
[i
];
3052 if (slot
->mmc
->pm_flags
& MMC_PM_KEEP_POWER
) {
3053 dw_mci_set_ios(slot
->mmc
, &slot
->mmc
->ios
);
3054 dw_mci_setup_bus(slot
, true);
3058 /* Now that slots are all setup, we can enable card detect */
3059 dw_mci_enable_cd(host
);
3063 EXPORT_SYMBOL(dw_mci_resume
);
3064 #endif /* CONFIG_PM_SLEEP */
3066 static int __init
dw_mci_init(void)
3068 pr_info("Synopsys Designware Multimedia Card Interface Driver\n");
3072 static void __exit
dw_mci_exit(void)
3076 module_init(dw_mci_init
);
3077 module_exit(dw_mci_exit
);
3079 MODULE_DESCRIPTION("DW Multimedia Card Interface driver");
3080 MODULE_AUTHOR("NXP Semiconductor VietNam");
3081 MODULE_AUTHOR("Imagination Technologies Ltd");
3082 MODULE_LICENSE("GPL v2");