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/pm_runtime.h>
26 #include <linux/seq_file.h>
27 #include <linux/slab.h>
28 #include <linux/stat.h>
29 #include <linux/delay.h>
30 #include <linux/irq.h>
31 #include <linux/mmc/card.h>
32 #include <linux/mmc/host.h>
33 #include <linux/mmc/mmc.h>
34 #include <linux/mmc/sd.h>
35 #include <linux/mmc/sdio.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 | \
47 SDMMC_INT_EBE | SDMMC_INT_HLE)
48 #define DW_MCI_CMD_ERROR_FLAGS (SDMMC_INT_RTO | SDMMC_INT_RCRC | \
49 SDMMC_INT_RESP_ERR | SDMMC_INT_HLE)
50 #define DW_MCI_ERROR_FLAGS (DW_MCI_DATA_ERROR_FLAGS | \
51 DW_MCI_CMD_ERROR_FLAGS)
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 100000 /* unit: HZ */
59 #define IDMAC_INT_CLR (SDMMC_IDMAC_INT_AI | SDMMC_IDMAC_INT_NI | \
60 SDMMC_IDMAC_INT_CES | SDMMC_IDMAC_INT_DU | \
61 SDMMC_IDMAC_INT_FBE | SDMMC_IDMAC_INT_RI | \
64 #define DESC_RING_BUF_SZ PAGE_SIZE
66 struct idmac_desc_64addr
{
67 u32 des0
; /* Control Descriptor */
69 u32 des1
; /* Reserved */
71 u32 des2
; /*Buffer sizes */
72 #define IDMAC_64ADDR_SET_BUFFER1_SIZE(d, s) \
73 ((d)->des2 = ((d)->des2 & cpu_to_le32(0x03ffe000)) | \
74 ((cpu_to_le32(s)) & cpu_to_le32(0x1fff)))
76 u32 des3
; /* Reserved */
78 u32 des4
; /* Lower 32-bits of Buffer Address Pointer 1*/
79 u32 des5
; /* Upper 32-bits of Buffer Address Pointer 1*/
81 u32 des6
; /* Lower 32-bits of Next Descriptor Address */
82 u32 des7
; /* Upper 32-bits of Next Descriptor Address */
86 __le32 des0
; /* Control Descriptor */
87 #define IDMAC_DES0_DIC BIT(1)
88 #define IDMAC_DES0_LD BIT(2)
89 #define IDMAC_DES0_FD BIT(3)
90 #define IDMAC_DES0_CH BIT(4)
91 #define IDMAC_DES0_ER BIT(5)
92 #define IDMAC_DES0_CES BIT(30)
93 #define IDMAC_DES0_OWN BIT(31)
95 __le32 des1
; /* Buffer sizes */
96 #define IDMAC_SET_BUFFER1_SIZE(d, s) \
97 ((d)->des1 = ((d)->des1 & cpu_to_le32(0x03ffe000)) | (cpu_to_le32((s) & 0x1fff)))
99 __le32 des2
; /* buffer 1 physical address */
101 __le32 des3
; /* buffer 2 physical address */
104 /* Each descriptor can transfer up to 4KB of data in chained mode */
105 #define DW_MCI_DESC_DATA_LENGTH 0x1000
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
);
110 static int dw_mci_get_cd(struct mmc_host
*mmc
);
112 #if defined(CONFIG_DEBUG_FS)
113 static int dw_mci_req_show(struct seq_file
*s
, void *v
)
115 struct dw_mci_slot
*slot
= s
->private;
116 struct mmc_request
*mrq
;
117 struct mmc_command
*cmd
;
118 struct mmc_command
*stop
;
119 struct mmc_data
*data
;
121 /* Make sure we get a consistent snapshot */
122 spin_lock_bh(&slot
->host
->lock
);
132 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
133 cmd
->opcode
, cmd
->arg
, cmd
->flags
,
134 cmd
->resp
[0], cmd
->resp
[1], cmd
->resp
[2],
135 cmd
->resp
[2], cmd
->error
);
137 seq_printf(s
, "DATA %u / %u * %u flg %x err %d\n",
138 data
->bytes_xfered
, data
->blocks
,
139 data
->blksz
, data
->flags
, data
->error
);
142 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
143 stop
->opcode
, stop
->arg
, stop
->flags
,
144 stop
->resp
[0], stop
->resp
[1], stop
->resp
[2],
145 stop
->resp
[2], stop
->error
);
148 spin_unlock_bh(&slot
->host
->lock
);
153 static int dw_mci_req_open(struct inode
*inode
, struct file
*file
)
155 return single_open(file
, dw_mci_req_show
, inode
->i_private
);
158 static const struct file_operations dw_mci_req_fops
= {
159 .owner
= THIS_MODULE
,
160 .open
= dw_mci_req_open
,
163 .release
= single_release
,
166 static int dw_mci_regs_show(struct seq_file
*s
, void *v
)
168 struct dw_mci
*host
= s
->private;
170 seq_printf(s
, "STATUS:\t0x%08x\n", mci_readl(host
, STATUS
));
171 seq_printf(s
, "RINTSTS:\t0x%08x\n", mci_readl(host
, RINTSTS
));
172 seq_printf(s
, "CMD:\t0x%08x\n", mci_readl(host
, CMD
));
173 seq_printf(s
, "CTRL:\t0x%08x\n", mci_readl(host
, CTRL
));
174 seq_printf(s
, "INTMASK:\t0x%08x\n", mci_readl(host
, INTMASK
));
175 seq_printf(s
, "CLKENA:\t0x%08x\n", mci_readl(host
, CLKENA
));
180 static int dw_mci_regs_open(struct inode
*inode
, struct file
*file
)
182 return single_open(file
, dw_mci_regs_show
, inode
->i_private
);
185 static const struct file_operations dw_mci_regs_fops
= {
186 .owner
= THIS_MODULE
,
187 .open
= dw_mci_regs_open
,
190 .release
= single_release
,
193 static void dw_mci_init_debugfs(struct dw_mci_slot
*slot
)
195 struct mmc_host
*mmc
= slot
->mmc
;
196 struct dw_mci
*host
= slot
->host
;
200 root
= mmc
->debugfs_root
;
204 node
= debugfs_create_file("regs", S_IRUSR
, root
, host
,
209 node
= debugfs_create_file("req", S_IRUSR
, root
, slot
,
214 node
= debugfs_create_u32("state", S_IRUSR
, root
, (u32
*)&host
->state
);
218 node
= debugfs_create_x32("pending_events", S_IRUSR
, root
,
219 (u32
*)&host
->pending_events
);
223 node
= debugfs_create_x32("completed_events", S_IRUSR
, root
,
224 (u32
*)&host
->completed_events
);
231 dev_err(&mmc
->class_dev
, "failed to initialize debugfs for slot\n");
233 #endif /* defined(CONFIG_DEBUG_FS) */
235 static void mci_send_cmd(struct dw_mci_slot
*slot
, u32 cmd
, u32 arg
);
237 static u32
dw_mci_prepare_command(struct mmc_host
*mmc
, struct mmc_command
*cmd
)
239 struct dw_mci_slot
*slot
= mmc_priv(mmc
);
240 struct dw_mci
*host
= slot
->host
;
243 cmd
->error
= -EINPROGRESS
;
246 if (cmd
->opcode
== MMC_STOP_TRANSMISSION
||
247 cmd
->opcode
== MMC_GO_IDLE_STATE
||
248 cmd
->opcode
== MMC_GO_INACTIVE_STATE
||
249 (cmd
->opcode
== SD_IO_RW_DIRECT
&&
250 ((cmd
->arg
>> 9) & 0x1FFFF) == SDIO_CCCR_ABORT
))
251 cmdr
|= SDMMC_CMD_STOP
;
252 else if (cmd
->opcode
!= MMC_SEND_STATUS
&& cmd
->data
)
253 cmdr
|= SDMMC_CMD_PRV_DAT_WAIT
;
255 if (cmd
->opcode
== SD_SWITCH_VOLTAGE
) {
258 /* Special bit makes CMD11 not die */
259 cmdr
|= SDMMC_CMD_VOLT_SWITCH
;
261 /* Change state to continue to handle CMD11 weirdness */
262 WARN_ON(slot
->host
->state
!= STATE_SENDING_CMD
);
263 slot
->host
->state
= STATE_SENDING_CMD11
;
266 * We need to disable low power mode (automatic clock stop)
267 * while doing voltage switch so we don't confuse the card,
268 * since stopping the clock is a specific part of the UHS
269 * voltage change dance.
271 * Note that low power mode (SDMMC_CLKEN_LOW_PWR) will be
272 * unconditionally turned back on in dw_mci_setup_bus() if it's
273 * ever called with a non-zero clock. That shouldn't happen
274 * until the voltage change is all done.
276 clk_en_a
= mci_readl(host
, CLKENA
);
277 clk_en_a
&= ~(SDMMC_CLKEN_LOW_PWR
<< slot
->id
);
278 mci_writel(host
, CLKENA
, clk_en_a
);
279 mci_send_cmd(slot
, SDMMC_CMD_UPD_CLK
|
280 SDMMC_CMD_PRV_DAT_WAIT
, 0);
283 if (cmd
->flags
& MMC_RSP_PRESENT
) {
284 /* We expect a response, so set this bit */
285 cmdr
|= SDMMC_CMD_RESP_EXP
;
286 if (cmd
->flags
& MMC_RSP_136
)
287 cmdr
|= SDMMC_CMD_RESP_LONG
;
290 if (cmd
->flags
& MMC_RSP_CRC
)
291 cmdr
|= SDMMC_CMD_RESP_CRC
;
294 cmdr
|= SDMMC_CMD_DAT_EXP
;
295 if (cmd
->data
->flags
& MMC_DATA_WRITE
)
296 cmdr
|= SDMMC_CMD_DAT_WR
;
299 if (!test_bit(DW_MMC_CARD_NO_USE_HOLD
, &slot
->flags
))
300 cmdr
|= SDMMC_CMD_USE_HOLD_REG
;
305 static u32
dw_mci_prep_stop_abort(struct dw_mci
*host
, struct mmc_command
*cmd
)
307 struct mmc_command
*stop
;
313 stop
= &host
->stop_abort
;
315 memset(stop
, 0, sizeof(struct mmc_command
));
317 if (cmdr
== MMC_READ_SINGLE_BLOCK
||
318 cmdr
== MMC_READ_MULTIPLE_BLOCK
||
319 cmdr
== MMC_WRITE_BLOCK
||
320 cmdr
== MMC_WRITE_MULTIPLE_BLOCK
||
321 cmdr
== MMC_SEND_TUNING_BLOCK
||
322 cmdr
== MMC_SEND_TUNING_BLOCK_HS200
) {
323 stop
->opcode
= MMC_STOP_TRANSMISSION
;
325 stop
->flags
= MMC_RSP_R1B
| MMC_CMD_AC
;
326 } else if (cmdr
== SD_IO_RW_EXTENDED
) {
327 stop
->opcode
= SD_IO_RW_DIRECT
;
328 stop
->arg
|= (1 << 31) | (0 << 28) | (SDIO_CCCR_ABORT
<< 9) |
329 ((cmd
->arg
>> 28) & 0x7);
330 stop
->flags
= MMC_RSP_SPI_R5
| MMC_RSP_R5
| MMC_CMD_AC
;
335 cmdr
= stop
->opcode
| SDMMC_CMD_STOP
|
336 SDMMC_CMD_RESP_CRC
| SDMMC_CMD_RESP_EXP
;
338 if (!test_bit(DW_MMC_CARD_NO_USE_HOLD
, &host
->cur_slot
->flags
))
339 cmdr
|= SDMMC_CMD_USE_HOLD_REG
;
344 static void dw_mci_wait_while_busy(struct dw_mci
*host
, u32 cmd_flags
)
346 unsigned long timeout
= jiffies
+ msecs_to_jiffies(500);
349 * Databook says that before issuing a new data transfer command
350 * we need to check to see if the card is busy. Data transfer commands
351 * all have SDMMC_CMD_PRV_DAT_WAIT set, so we'll key off that.
353 * ...also allow sending for SDMMC_CMD_VOLT_SWITCH where busy is
356 if ((cmd_flags
& SDMMC_CMD_PRV_DAT_WAIT
) &&
357 !(cmd_flags
& SDMMC_CMD_VOLT_SWITCH
)) {
358 while (mci_readl(host
, STATUS
) & SDMMC_STATUS_BUSY
) {
359 if (time_after(jiffies
, timeout
)) {
360 /* Command will fail; we'll pass error then */
361 dev_err(host
->dev
, "Busy; trying anyway\n");
369 static void dw_mci_start_command(struct dw_mci
*host
,
370 struct mmc_command
*cmd
, u32 cmd_flags
)
374 "start command: ARGR=0x%08x CMDR=0x%08x\n",
375 cmd
->arg
, cmd_flags
);
377 mci_writel(host
, CMDARG
, cmd
->arg
);
378 wmb(); /* drain writebuffer */
379 dw_mci_wait_while_busy(host
, cmd_flags
);
381 mci_writel(host
, CMD
, cmd_flags
| SDMMC_CMD_START
);
384 static inline void send_stop_abort(struct dw_mci
*host
, struct mmc_data
*data
)
386 struct mmc_command
*stop
= &host
->stop_abort
;
388 dw_mci_start_command(host
, stop
, host
->stop_cmdr
);
391 /* DMA interface functions */
392 static void dw_mci_stop_dma(struct dw_mci
*host
)
394 if (host
->using_dma
) {
395 host
->dma_ops
->stop(host
);
396 host
->dma_ops
->cleanup(host
);
399 /* Data transfer was stopped by the interrupt handler */
400 set_bit(EVENT_XFER_COMPLETE
, &host
->pending_events
);
403 static int dw_mci_get_dma_dir(struct mmc_data
*data
)
405 if (data
->flags
& MMC_DATA_WRITE
)
406 return DMA_TO_DEVICE
;
408 return DMA_FROM_DEVICE
;
411 static void dw_mci_dma_cleanup(struct dw_mci
*host
)
413 struct mmc_data
*data
= host
->data
;
415 if (data
&& data
->host_cookie
== COOKIE_MAPPED
) {
416 dma_unmap_sg(host
->dev
,
419 dw_mci_get_dma_dir(data
));
420 data
->host_cookie
= COOKIE_UNMAPPED
;
424 static void dw_mci_idmac_reset(struct dw_mci
*host
)
426 u32 bmod
= mci_readl(host
, BMOD
);
427 /* Software reset of DMA */
428 bmod
|= SDMMC_IDMAC_SWRESET
;
429 mci_writel(host
, BMOD
, bmod
);
432 static void dw_mci_idmac_stop_dma(struct dw_mci
*host
)
436 /* Disable and reset the IDMAC interface */
437 temp
= mci_readl(host
, CTRL
);
438 temp
&= ~SDMMC_CTRL_USE_IDMAC
;
439 temp
|= SDMMC_CTRL_DMA_RESET
;
440 mci_writel(host
, CTRL
, temp
);
442 /* Stop the IDMAC running */
443 temp
= mci_readl(host
, BMOD
);
444 temp
&= ~(SDMMC_IDMAC_ENABLE
| SDMMC_IDMAC_FB
);
445 temp
|= SDMMC_IDMAC_SWRESET
;
446 mci_writel(host
, BMOD
, temp
);
449 static void dw_mci_dmac_complete_dma(void *arg
)
451 struct dw_mci
*host
= arg
;
452 struct mmc_data
*data
= host
->data
;
454 dev_vdbg(host
->dev
, "DMA complete\n");
456 if ((host
->use_dma
== TRANS_MODE_EDMAC
) &&
457 data
&& (data
->flags
& MMC_DATA_READ
))
458 /* Invalidate cache after read */
459 dma_sync_sg_for_cpu(mmc_dev(host
->cur_slot
->mmc
),
464 host
->dma_ops
->cleanup(host
);
467 * If the card was removed, data will be NULL. No point in trying to
468 * send the stop command or waiting for NBUSY in this case.
471 set_bit(EVENT_XFER_COMPLETE
, &host
->pending_events
);
472 tasklet_schedule(&host
->tasklet
);
476 static int dw_mci_idmac_init(struct dw_mci
*host
)
480 if (host
->dma_64bit_address
== 1) {
481 struct idmac_desc_64addr
*p
;
482 /* Number of descriptors in the ring buffer */
484 DESC_RING_BUF_SZ
/ sizeof(struct idmac_desc_64addr
);
486 /* Forward link the descriptor list */
487 for (i
= 0, p
= host
->sg_cpu
; i
< host
->ring_size
- 1;
489 p
->des6
= (host
->sg_dma
+
490 (sizeof(struct idmac_desc_64addr
) *
491 (i
+ 1))) & 0xffffffff;
493 p
->des7
= (u64
)(host
->sg_dma
+
494 (sizeof(struct idmac_desc_64addr
) *
496 /* Initialize reserved and buffer size fields to "0" */
502 /* Set the last descriptor as the end-of-ring descriptor */
503 p
->des6
= host
->sg_dma
& 0xffffffff;
504 p
->des7
= (u64
)host
->sg_dma
>> 32;
505 p
->des0
= IDMAC_DES0_ER
;
508 struct idmac_desc
*p
;
509 /* Number of descriptors in the ring buffer */
511 DESC_RING_BUF_SZ
/ sizeof(struct idmac_desc
);
513 /* Forward link the descriptor list */
514 for (i
= 0, p
= host
->sg_cpu
;
515 i
< host
->ring_size
- 1;
517 p
->des3
= cpu_to_le32(host
->sg_dma
+
518 (sizeof(struct idmac_desc
) * (i
+ 1)));
522 /* Set the last descriptor as the end-of-ring descriptor */
523 p
->des3
= cpu_to_le32(host
->sg_dma
);
524 p
->des0
= cpu_to_le32(IDMAC_DES0_ER
);
527 dw_mci_idmac_reset(host
);
529 if (host
->dma_64bit_address
== 1) {
530 /* Mask out interrupts - get Tx & Rx complete only */
531 mci_writel(host
, IDSTS64
, IDMAC_INT_CLR
);
532 mci_writel(host
, IDINTEN64
, SDMMC_IDMAC_INT_NI
|
533 SDMMC_IDMAC_INT_RI
| SDMMC_IDMAC_INT_TI
);
535 /* Set the descriptor base address */
536 mci_writel(host
, DBADDRL
, host
->sg_dma
& 0xffffffff);
537 mci_writel(host
, DBADDRU
, (u64
)host
->sg_dma
>> 32);
540 /* Mask out interrupts - get Tx & Rx complete only */
541 mci_writel(host
, IDSTS
, IDMAC_INT_CLR
);
542 mci_writel(host
, IDINTEN
, SDMMC_IDMAC_INT_NI
|
543 SDMMC_IDMAC_INT_RI
| SDMMC_IDMAC_INT_TI
);
545 /* Set the descriptor base address */
546 mci_writel(host
, DBADDR
, host
->sg_dma
);
552 static inline int dw_mci_prepare_desc64(struct dw_mci
*host
,
553 struct mmc_data
*data
,
556 unsigned int desc_len
;
557 struct idmac_desc_64addr
*desc_first
, *desc_last
, *desc
;
558 unsigned long timeout
;
561 desc_first
= desc_last
= desc
= host
->sg_cpu
;
563 for (i
= 0; i
< sg_len
; i
++) {
564 unsigned int length
= sg_dma_len(&data
->sg
[i
]);
566 u64 mem_addr
= sg_dma_address(&data
->sg
[i
]);
568 for ( ; length
; desc
++) {
569 desc_len
= (length
<= DW_MCI_DESC_DATA_LENGTH
) ?
570 length
: DW_MCI_DESC_DATA_LENGTH
;
575 * Wait for the former clear OWN bit operation
576 * of IDMAC to make sure that this descriptor
577 * isn't still owned by IDMAC as IDMAC's write
578 * ops and CPU's read ops are asynchronous.
580 timeout
= jiffies
+ msecs_to_jiffies(100);
581 while (readl(&desc
->des0
) & IDMAC_DES0_OWN
) {
582 if (time_after(jiffies
, timeout
))
588 * Set the OWN bit and disable interrupts
589 * for this descriptor
591 desc
->des0
= IDMAC_DES0_OWN
| IDMAC_DES0_DIC
|
595 IDMAC_64ADDR_SET_BUFFER1_SIZE(desc
, desc_len
);
597 /* Physical address to DMA to/from */
598 desc
->des4
= mem_addr
& 0xffffffff;
599 desc
->des5
= mem_addr
>> 32;
601 /* Update physical address for the next desc */
602 mem_addr
+= desc_len
;
604 /* Save pointer to the last descriptor */
609 /* Set first descriptor */
610 desc_first
->des0
|= IDMAC_DES0_FD
;
612 /* Set last descriptor */
613 desc_last
->des0
&= ~(IDMAC_DES0_CH
| IDMAC_DES0_DIC
);
614 desc_last
->des0
|= IDMAC_DES0_LD
;
618 /* restore the descriptor chain as it's polluted */
619 dev_dbg(host
->dev
, "descriptor is still owned by IDMAC.\n");
620 memset(host
->sg_cpu
, 0, DESC_RING_BUF_SZ
);
621 dw_mci_idmac_init(host
);
626 static inline int dw_mci_prepare_desc32(struct dw_mci
*host
,
627 struct mmc_data
*data
,
630 unsigned int desc_len
;
631 struct idmac_desc
*desc_first
, *desc_last
, *desc
;
632 unsigned long timeout
;
635 desc_first
= desc_last
= desc
= host
->sg_cpu
;
637 for (i
= 0; i
< sg_len
; i
++) {
638 unsigned int length
= sg_dma_len(&data
->sg
[i
]);
640 u32 mem_addr
= sg_dma_address(&data
->sg
[i
]);
642 for ( ; length
; desc
++) {
643 desc_len
= (length
<= DW_MCI_DESC_DATA_LENGTH
) ?
644 length
: DW_MCI_DESC_DATA_LENGTH
;
649 * Wait for the former clear OWN bit operation
650 * of IDMAC to make sure that this descriptor
651 * isn't still owned by IDMAC as IDMAC's write
652 * ops and CPU's read ops are asynchronous.
654 timeout
= jiffies
+ msecs_to_jiffies(100);
655 while (readl(&desc
->des0
) &
656 cpu_to_le32(IDMAC_DES0_OWN
)) {
657 if (time_after(jiffies
, timeout
))
663 * Set the OWN bit and disable interrupts
664 * for this descriptor
666 desc
->des0
= cpu_to_le32(IDMAC_DES0_OWN
|
671 IDMAC_SET_BUFFER1_SIZE(desc
, desc_len
);
673 /* Physical address to DMA to/from */
674 desc
->des2
= cpu_to_le32(mem_addr
);
676 /* Update physical address for the next desc */
677 mem_addr
+= desc_len
;
679 /* Save pointer to the last descriptor */
684 /* Set first descriptor */
685 desc_first
->des0
|= cpu_to_le32(IDMAC_DES0_FD
);
687 /* Set last descriptor */
688 desc_last
->des0
&= cpu_to_le32(~(IDMAC_DES0_CH
|
690 desc_last
->des0
|= cpu_to_le32(IDMAC_DES0_LD
);
694 /* restore the descriptor chain as it's polluted */
695 dev_dbg(host
->dev
, "descriptor is still owned by IDMAC.\n");
696 memset(host
->sg_cpu
, 0, DESC_RING_BUF_SZ
);
697 dw_mci_idmac_init(host
);
701 static int dw_mci_idmac_start_dma(struct dw_mci
*host
, unsigned int sg_len
)
706 if (host
->dma_64bit_address
== 1)
707 ret
= dw_mci_prepare_desc64(host
, host
->data
, sg_len
);
709 ret
= dw_mci_prepare_desc32(host
, host
->data
, sg_len
);
714 /* drain writebuffer */
717 /* Make sure to reset DMA in case we did PIO before this */
718 dw_mci_ctrl_reset(host
, SDMMC_CTRL_DMA_RESET
);
719 dw_mci_idmac_reset(host
);
721 /* Select IDMAC interface */
722 temp
= mci_readl(host
, CTRL
);
723 temp
|= SDMMC_CTRL_USE_IDMAC
;
724 mci_writel(host
, CTRL
, temp
);
726 /* drain writebuffer */
729 /* Enable the IDMAC */
730 temp
= mci_readl(host
, BMOD
);
731 temp
|= SDMMC_IDMAC_ENABLE
| SDMMC_IDMAC_FB
;
732 mci_writel(host
, BMOD
, temp
);
734 /* Start it running */
735 mci_writel(host
, PLDMND
, 1);
741 static const struct dw_mci_dma_ops dw_mci_idmac_ops
= {
742 .init
= dw_mci_idmac_init
,
743 .start
= dw_mci_idmac_start_dma
,
744 .stop
= dw_mci_idmac_stop_dma
,
745 .complete
= dw_mci_dmac_complete_dma
,
746 .cleanup
= dw_mci_dma_cleanup
,
749 static void dw_mci_edmac_stop_dma(struct dw_mci
*host
)
751 dmaengine_terminate_async(host
->dms
->ch
);
754 static int dw_mci_edmac_start_dma(struct dw_mci
*host
,
757 struct dma_slave_config cfg
;
758 struct dma_async_tx_descriptor
*desc
= NULL
;
759 struct scatterlist
*sgl
= host
->data
->sg
;
760 const u32 mszs
[] = {1, 4, 8, 16, 32, 64, 128, 256};
761 u32 sg_elems
= host
->data
->sg_len
;
763 u32 fifo_offset
= host
->fifo_reg
- host
->regs
;
766 /* Set external dma config: burst size, burst width */
767 cfg
.dst_addr
= host
->phy_regs
+ fifo_offset
;
768 cfg
.src_addr
= cfg
.dst_addr
;
769 cfg
.dst_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
770 cfg
.src_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
772 /* Match burst msize with external dma config */
773 fifoth_val
= mci_readl(host
, FIFOTH
);
774 cfg
.dst_maxburst
= mszs
[(fifoth_val
>> 28) & 0x7];
775 cfg
.src_maxburst
= cfg
.dst_maxburst
;
777 if (host
->data
->flags
& MMC_DATA_WRITE
)
778 cfg
.direction
= DMA_MEM_TO_DEV
;
780 cfg
.direction
= DMA_DEV_TO_MEM
;
782 ret
= dmaengine_slave_config(host
->dms
->ch
, &cfg
);
784 dev_err(host
->dev
, "Failed to config edmac.\n");
788 desc
= dmaengine_prep_slave_sg(host
->dms
->ch
, sgl
,
789 sg_len
, cfg
.direction
,
790 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
792 dev_err(host
->dev
, "Can't prepare slave sg.\n");
796 /* Set dw_mci_dmac_complete_dma as callback */
797 desc
->callback
= dw_mci_dmac_complete_dma
;
798 desc
->callback_param
= (void *)host
;
799 dmaengine_submit(desc
);
801 /* Flush cache before write */
802 if (host
->data
->flags
& MMC_DATA_WRITE
)
803 dma_sync_sg_for_device(mmc_dev(host
->cur_slot
->mmc
), sgl
,
804 sg_elems
, DMA_TO_DEVICE
);
806 dma_async_issue_pending(host
->dms
->ch
);
811 static int dw_mci_edmac_init(struct dw_mci
*host
)
813 /* Request external dma channel */
814 host
->dms
= kzalloc(sizeof(struct dw_mci_dma_slave
), GFP_KERNEL
);
818 host
->dms
->ch
= dma_request_slave_channel(host
->dev
, "rx-tx");
819 if (!host
->dms
->ch
) {
820 dev_err(host
->dev
, "Failed to get external DMA channel.\n");
829 static void dw_mci_edmac_exit(struct dw_mci
*host
)
833 dma_release_channel(host
->dms
->ch
);
834 host
->dms
->ch
= NULL
;
841 static const struct dw_mci_dma_ops dw_mci_edmac_ops
= {
842 .init
= dw_mci_edmac_init
,
843 .exit
= dw_mci_edmac_exit
,
844 .start
= dw_mci_edmac_start_dma
,
845 .stop
= dw_mci_edmac_stop_dma
,
846 .complete
= dw_mci_dmac_complete_dma
,
847 .cleanup
= dw_mci_dma_cleanup
,
850 static int dw_mci_pre_dma_transfer(struct dw_mci
*host
,
851 struct mmc_data
*data
,
854 struct scatterlist
*sg
;
855 unsigned int i
, sg_len
;
857 if (data
->host_cookie
== COOKIE_PRE_MAPPED
)
861 * We don't do DMA on "complex" transfers, i.e. with
862 * non-word-aligned buffers or lengths. Also, we don't bother
863 * with all the DMA setup overhead for short transfers.
865 if (data
->blocks
* data
->blksz
< DW_MCI_DMA_THRESHOLD
)
871 for_each_sg(data
->sg
, sg
, data
->sg_len
, i
) {
872 if (sg
->offset
& 3 || sg
->length
& 3)
876 sg_len
= dma_map_sg(host
->dev
,
879 dw_mci_get_dma_dir(data
));
883 data
->host_cookie
= cookie
;
888 static void dw_mci_pre_req(struct mmc_host
*mmc
,
889 struct mmc_request
*mrq
)
891 struct dw_mci_slot
*slot
= mmc_priv(mmc
);
892 struct mmc_data
*data
= mrq
->data
;
894 if (!slot
->host
->use_dma
|| !data
)
897 /* This data might be unmapped at this time */
898 data
->host_cookie
= COOKIE_UNMAPPED
;
900 if (dw_mci_pre_dma_transfer(slot
->host
, mrq
->data
,
901 COOKIE_PRE_MAPPED
) < 0)
902 data
->host_cookie
= COOKIE_UNMAPPED
;
905 static void dw_mci_post_req(struct mmc_host
*mmc
,
906 struct mmc_request
*mrq
,
909 struct dw_mci_slot
*slot
= mmc_priv(mmc
);
910 struct mmc_data
*data
= mrq
->data
;
912 if (!slot
->host
->use_dma
|| !data
)
915 if (data
->host_cookie
!= COOKIE_UNMAPPED
)
916 dma_unmap_sg(slot
->host
->dev
,
919 dw_mci_get_dma_dir(data
));
920 data
->host_cookie
= COOKIE_UNMAPPED
;
923 static void dw_mci_adjust_fifoth(struct dw_mci
*host
, struct mmc_data
*data
)
925 unsigned int blksz
= data
->blksz
;
926 const u32 mszs
[] = {1, 4, 8, 16, 32, 64, 128, 256};
927 u32 fifo_width
= 1 << host
->data_shift
;
928 u32 blksz_depth
= blksz
/ fifo_width
, fifoth_val
;
929 u32 msize
= 0, rx_wmark
= 1, tx_wmark
, tx_wmark_invers
;
930 int idx
= ARRAY_SIZE(mszs
) - 1;
932 /* pio should ship this scenario */
936 tx_wmark
= (host
->fifo_depth
) / 2;
937 tx_wmark_invers
= host
->fifo_depth
- tx_wmark
;
941 * if blksz is not a multiple of the FIFO width
943 if (blksz
% fifo_width
)
947 if (!((blksz_depth
% mszs
[idx
]) ||
948 (tx_wmark_invers
% mszs
[idx
]))) {
950 rx_wmark
= mszs
[idx
] - 1;
955 * If idx is '0', it won't be tried
956 * Thus, initial values are uesed
959 fifoth_val
= SDMMC_SET_FIFOTH(msize
, rx_wmark
, tx_wmark
);
960 mci_writel(host
, FIFOTH
, fifoth_val
);
963 static void dw_mci_ctrl_thld(struct dw_mci
*host
, struct mmc_data
*data
)
965 unsigned int blksz
= data
->blksz
;
966 u32 blksz_depth
, fifo_depth
;
971 * CDTHRCTL doesn't exist prior to 240A (in fact that register offset is
972 * in the FIFO region, so we really shouldn't access it).
974 if (host
->verid
< DW_MMC_240A
||
975 (host
->verid
< DW_MMC_280A
&& data
->flags
& MMC_DATA_WRITE
))
979 * Card write Threshold is introduced since 2.80a
980 * It's used when HS400 mode is enabled.
982 if (data
->flags
& MMC_DATA_WRITE
&&
983 !(host
->timing
!= MMC_TIMING_MMC_HS400
))
986 if (data
->flags
& MMC_DATA_WRITE
)
987 enable
= SDMMC_CARD_WR_THR_EN
;
989 enable
= SDMMC_CARD_RD_THR_EN
;
991 if (host
->timing
!= MMC_TIMING_MMC_HS200
&&
992 host
->timing
!= MMC_TIMING_UHS_SDR104
)
995 blksz_depth
= blksz
/ (1 << host
->data_shift
);
996 fifo_depth
= host
->fifo_depth
;
998 if (blksz_depth
> fifo_depth
)
1002 * If (blksz_depth) >= (fifo_depth >> 1), should be 'thld_size <= blksz'
1003 * If (blksz_depth) < (fifo_depth >> 1), should be thld_size = blksz
1004 * Currently just choose blksz.
1007 mci_writel(host
, CDTHRCTL
, SDMMC_SET_THLD(thld_size
, enable
));
1011 mci_writel(host
, CDTHRCTL
, 0);
1014 static int dw_mci_submit_data_dma(struct dw_mci
*host
, struct mmc_data
*data
)
1016 unsigned long irqflags
;
1020 host
->using_dma
= 0;
1022 /* If we don't have a channel, we can't do DMA */
1026 sg_len
= dw_mci_pre_dma_transfer(host
, data
, COOKIE_MAPPED
);
1028 host
->dma_ops
->stop(host
);
1032 host
->using_dma
= 1;
1034 if (host
->use_dma
== TRANS_MODE_IDMAC
)
1036 "sd sg_cpu: %#lx sg_dma: %#lx sg_len: %d\n",
1037 (unsigned long)host
->sg_cpu
,
1038 (unsigned long)host
->sg_dma
,
1042 * Decide the MSIZE and RX/TX Watermark.
1043 * If current block size is same with previous size,
1044 * no need to update fifoth.
1046 if (host
->prev_blksz
!= data
->blksz
)
1047 dw_mci_adjust_fifoth(host
, data
);
1049 /* Enable the DMA interface */
1050 temp
= mci_readl(host
, CTRL
);
1051 temp
|= SDMMC_CTRL_DMA_ENABLE
;
1052 mci_writel(host
, CTRL
, temp
);
1054 /* Disable RX/TX IRQs, let DMA handle it */
1055 spin_lock_irqsave(&host
->irq_lock
, irqflags
);
1056 temp
= mci_readl(host
, INTMASK
);
1057 temp
&= ~(SDMMC_INT_RXDR
| SDMMC_INT_TXDR
);
1058 mci_writel(host
, INTMASK
, temp
);
1059 spin_unlock_irqrestore(&host
->irq_lock
, irqflags
);
1061 if (host
->dma_ops
->start(host
, sg_len
)) {
1062 host
->dma_ops
->stop(host
);
1063 /* We can't do DMA, try PIO for this one */
1065 "%s: fall back to PIO mode for current transfer\n",
1073 static void dw_mci_submit_data(struct dw_mci
*host
, struct mmc_data
*data
)
1075 unsigned long irqflags
;
1076 int flags
= SG_MITER_ATOMIC
;
1079 data
->error
= -EINPROGRESS
;
1081 WARN_ON(host
->data
);
1085 if (data
->flags
& MMC_DATA_READ
)
1086 host
->dir_status
= DW_MCI_RECV_STATUS
;
1088 host
->dir_status
= DW_MCI_SEND_STATUS
;
1090 dw_mci_ctrl_thld(host
, data
);
1092 if (dw_mci_submit_data_dma(host
, data
)) {
1093 if (host
->data
->flags
& MMC_DATA_READ
)
1094 flags
|= SG_MITER_TO_SG
;
1096 flags
|= SG_MITER_FROM_SG
;
1098 sg_miter_start(&host
->sg_miter
, data
->sg
, data
->sg_len
, flags
);
1099 host
->sg
= data
->sg
;
1100 host
->part_buf_start
= 0;
1101 host
->part_buf_count
= 0;
1103 mci_writel(host
, RINTSTS
, SDMMC_INT_TXDR
| SDMMC_INT_RXDR
);
1105 spin_lock_irqsave(&host
->irq_lock
, irqflags
);
1106 temp
= mci_readl(host
, INTMASK
);
1107 temp
|= SDMMC_INT_TXDR
| SDMMC_INT_RXDR
;
1108 mci_writel(host
, INTMASK
, temp
);
1109 spin_unlock_irqrestore(&host
->irq_lock
, irqflags
);
1111 temp
= mci_readl(host
, CTRL
);
1112 temp
&= ~SDMMC_CTRL_DMA_ENABLE
;
1113 mci_writel(host
, CTRL
, temp
);
1116 * Use the initial fifoth_val for PIO mode. If wm_algined
1117 * is set, we set watermark same as data size.
1118 * If next issued data may be transfered by DMA mode,
1119 * prev_blksz should be invalidated.
1121 if (host
->wm_aligned
)
1122 dw_mci_adjust_fifoth(host
, data
);
1124 mci_writel(host
, FIFOTH
, host
->fifoth_val
);
1125 host
->prev_blksz
= 0;
1128 * Keep the current block size.
1129 * It will be used to decide whether to update
1130 * fifoth register next time.
1132 host
->prev_blksz
= data
->blksz
;
1136 static void mci_send_cmd(struct dw_mci_slot
*slot
, u32 cmd
, u32 arg
)
1138 struct dw_mci
*host
= slot
->host
;
1139 unsigned long timeout
= jiffies
+ msecs_to_jiffies(500);
1140 unsigned int cmd_status
= 0;
1142 mci_writel(host
, CMDARG
, arg
);
1143 wmb(); /* drain writebuffer */
1144 dw_mci_wait_while_busy(host
, cmd
);
1145 mci_writel(host
, CMD
, SDMMC_CMD_START
| cmd
);
1147 while (time_before(jiffies
, timeout
)) {
1148 cmd_status
= mci_readl(host
, CMD
);
1149 if (!(cmd_status
& SDMMC_CMD_START
))
1152 dev_err(&slot
->mmc
->class_dev
,
1153 "Timeout sending command (cmd %#x arg %#x status %#x)\n",
1154 cmd
, arg
, cmd_status
);
1157 static void dw_mci_setup_bus(struct dw_mci_slot
*slot
, bool force_clkinit
)
1159 struct dw_mci
*host
= slot
->host
;
1160 unsigned int clock
= slot
->clock
;
1163 u32 sdmmc_cmd_bits
= SDMMC_CMD_UPD_CLK
| SDMMC_CMD_PRV_DAT_WAIT
;
1165 /* We must continue to set bit 28 in CMD until the change is complete */
1166 if (host
->state
== STATE_WAITING_CMD11_DONE
)
1167 sdmmc_cmd_bits
|= SDMMC_CMD_VOLT_SWITCH
;
1170 mci_writel(host
, CLKENA
, 0);
1171 mci_send_cmd(slot
, sdmmc_cmd_bits
, 0);
1172 } else if (clock
!= host
->current_speed
|| force_clkinit
) {
1173 div
= host
->bus_hz
/ clock
;
1174 if (host
->bus_hz
% clock
&& host
->bus_hz
> clock
)
1176 * move the + 1 after the divide to prevent
1177 * over-clocking the card.
1181 div
= (host
->bus_hz
!= clock
) ? DIV_ROUND_UP(div
, 2) : 0;
1183 if ((clock
!= slot
->__clk_old
&&
1184 !test_bit(DW_MMC_CARD_NEEDS_POLL
, &slot
->flags
)) ||
1186 /* Silent the verbose log if calling from PM context */
1188 dev_info(&slot
->mmc
->class_dev
,
1189 "Bus speed (slot %d) = %dHz (slot req %dHz, actual %dHZ div = %d)\n",
1190 slot
->id
, host
->bus_hz
, clock
,
1191 div
? ((host
->bus_hz
/ div
) >> 1) :
1195 * If card is polling, display the message only
1196 * one time at boot time.
1198 if (slot
->mmc
->caps
& MMC_CAP_NEEDS_POLL
&&
1199 slot
->mmc
->f_min
== clock
)
1200 set_bit(DW_MMC_CARD_NEEDS_POLL
, &slot
->flags
);
1204 mci_writel(host
, CLKENA
, 0);
1205 mci_writel(host
, CLKSRC
, 0);
1208 mci_send_cmd(slot
, sdmmc_cmd_bits
, 0);
1210 /* set clock to desired speed */
1211 mci_writel(host
, CLKDIV
, div
);
1214 mci_send_cmd(slot
, sdmmc_cmd_bits
, 0);
1216 /* enable clock; only low power if no SDIO */
1217 clk_en_a
= SDMMC_CLKEN_ENABLE
<< slot
->id
;
1218 if (!test_bit(DW_MMC_CARD_NO_LOW_PWR
, &slot
->flags
))
1219 clk_en_a
|= SDMMC_CLKEN_LOW_PWR
<< slot
->id
;
1220 mci_writel(host
, CLKENA
, clk_en_a
);
1223 mci_send_cmd(slot
, sdmmc_cmd_bits
, 0);
1225 /* keep the last clock value that was requested from core */
1226 slot
->__clk_old
= clock
;
1229 host
->current_speed
= clock
;
1231 /* Set the current slot bus width */
1232 mci_writel(host
, CTYPE
, (slot
->ctype
<< slot
->id
));
1235 static void __dw_mci_start_request(struct dw_mci
*host
,
1236 struct dw_mci_slot
*slot
,
1237 struct mmc_command
*cmd
)
1239 struct mmc_request
*mrq
;
1240 struct mmc_data
*data
;
1245 host
->cur_slot
= slot
;
1248 host
->pending_events
= 0;
1249 host
->completed_events
= 0;
1250 host
->cmd_status
= 0;
1251 host
->data_status
= 0;
1252 host
->dir_status
= 0;
1256 mci_writel(host
, TMOUT
, 0xFFFFFFFF);
1257 mci_writel(host
, BYTCNT
, data
->blksz
*data
->blocks
);
1258 mci_writel(host
, BLKSIZ
, data
->blksz
);
1261 cmdflags
= dw_mci_prepare_command(slot
->mmc
, cmd
);
1263 /* this is the first command, send the initialization clock */
1264 if (test_and_clear_bit(DW_MMC_CARD_NEED_INIT
, &slot
->flags
))
1265 cmdflags
|= SDMMC_CMD_INIT
;
1268 dw_mci_submit_data(host
, data
);
1269 wmb(); /* drain writebuffer */
1272 dw_mci_start_command(host
, cmd
, cmdflags
);
1274 if (cmd
->opcode
== SD_SWITCH_VOLTAGE
) {
1275 unsigned long irqflags
;
1278 * Databook says to fail after 2ms w/ no response, but evidence
1279 * shows that sometimes the cmd11 interrupt takes over 130ms.
1280 * We'll set to 500ms, plus an extra jiffy just in case jiffies
1281 * is just about to roll over.
1283 * We do this whole thing under spinlock and only if the
1284 * command hasn't already completed (indicating the the irq
1285 * already ran so we don't want the timeout).
1287 spin_lock_irqsave(&host
->irq_lock
, irqflags
);
1288 if (!test_bit(EVENT_CMD_COMPLETE
, &host
->pending_events
))
1289 mod_timer(&host
->cmd11_timer
,
1290 jiffies
+ msecs_to_jiffies(500) + 1);
1291 spin_unlock_irqrestore(&host
->irq_lock
, irqflags
);
1294 host
->stop_cmdr
= dw_mci_prep_stop_abort(host
, cmd
);
1297 static void dw_mci_start_request(struct dw_mci
*host
,
1298 struct dw_mci_slot
*slot
)
1300 struct mmc_request
*mrq
= slot
->mrq
;
1301 struct mmc_command
*cmd
;
1303 cmd
= mrq
->sbc
? mrq
->sbc
: mrq
->cmd
;
1304 __dw_mci_start_request(host
, slot
, cmd
);
1307 /* must be called with host->lock held */
1308 static void dw_mci_queue_request(struct dw_mci
*host
, struct dw_mci_slot
*slot
,
1309 struct mmc_request
*mrq
)
1311 dev_vdbg(&slot
->mmc
->class_dev
, "queue request: state=%d\n",
1316 if (host
->state
== STATE_WAITING_CMD11_DONE
) {
1317 dev_warn(&slot
->mmc
->class_dev
,
1318 "Voltage change didn't complete\n");
1320 * this case isn't expected to happen, so we can
1321 * either crash here or just try to continue on
1322 * in the closest possible state
1324 host
->state
= STATE_IDLE
;
1327 if (host
->state
== STATE_IDLE
) {
1328 host
->state
= STATE_SENDING_CMD
;
1329 dw_mci_start_request(host
, slot
);
1331 list_add_tail(&slot
->queue_node
, &host
->queue
);
1335 static void dw_mci_request(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
1337 struct dw_mci_slot
*slot
= mmc_priv(mmc
);
1338 struct dw_mci
*host
= slot
->host
;
1343 * The check for card presence and queueing of the request must be
1344 * atomic, otherwise the card could be removed in between and the
1345 * request wouldn't fail until another card was inserted.
1348 if (!dw_mci_get_cd(mmc
)) {
1349 mrq
->cmd
->error
= -ENOMEDIUM
;
1350 mmc_request_done(mmc
, mrq
);
1354 spin_lock_bh(&host
->lock
);
1356 dw_mci_queue_request(host
, slot
, mrq
);
1358 spin_unlock_bh(&host
->lock
);
1361 static void dw_mci_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
1363 struct dw_mci_slot
*slot
= mmc_priv(mmc
);
1364 const struct dw_mci_drv_data
*drv_data
= slot
->host
->drv_data
;
1368 switch (ios
->bus_width
) {
1369 case MMC_BUS_WIDTH_4
:
1370 slot
->ctype
= SDMMC_CTYPE_4BIT
;
1372 case MMC_BUS_WIDTH_8
:
1373 slot
->ctype
= SDMMC_CTYPE_8BIT
;
1376 /* set default 1 bit mode */
1377 slot
->ctype
= SDMMC_CTYPE_1BIT
;
1380 regs
= mci_readl(slot
->host
, UHS_REG
);
1383 if (ios
->timing
== MMC_TIMING_MMC_DDR52
||
1384 ios
->timing
== MMC_TIMING_UHS_DDR50
||
1385 ios
->timing
== MMC_TIMING_MMC_HS400
)
1386 regs
|= ((0x1 << slot
->id
) << 16);
1388 regs
&= ~((0x1 << slot
->id
) << 16);
1390 mci_writel(slot
->host
, UHS_REG
, regs
);
1391 slot
->host
->timing
= ios
->timing
;
1394 * Use mirror of ios->clock to prevent race with mmc
1395 * core ios update when finding the minimum.
1397 slot
->clock
= ios
->clock
;
1399 if (drv_data
&& drv_data
->set_ios
)
1400 drv_data
->set_ios(slot
->host
, ios
);
1402 switch (ios
->power_mode
) {
1404 if (!IS_ERR(mmc
->supply
.vmmc
)) {
1405 ret
= mmc_regulator_set_ocr(mmc
, mmc
->supply
.vmmc
,
1408 dev_err(slot
->host
->dev
,
1409 "failed to enable vmmc regulator\n");
1410 /*return, if failed turn on vmmc*/
1414 set_bit(DW_MMC_CARD_NEED_INIT
, &slot
->flags
);
1415 regs
= mci_readl(slot
->host
, PWREN
);
1416 regs
|= (1 << slot
->id
);
1417 mci_writel(slot
->host
, PWREN
, regs
);
1420 if (!slot
->host
->vqmmc_enabled
) {
1421 if (!IS_ERR(mmc
->supply
.vqmmc
)) {
1422 ret
= regulator_enable(mmc
->supply
.vqmmc
);
1424 dev_err(slot
->host
->dev
,
1425 "failed to enable vqmmc\n");
1427 slot
->host
->vqmmc_enabled
= true;
1430 /* Keep track so we don't reset again */
1431 slot
->host
->vqmmc_enabled
= true;
1434 /* Reset our state machine after powering on */
1435 dw_mci_ctrl_reset(slot
->host
,
1436 SDMMC_CTRL_ALL_RESET_FLAGS
);
1439 /* Adjust clock / bus width after power is up */
1440 dw_mci_setup_bus(slot
, false);
1444 /* Turn clock off before power goes down */
1445 dw_mci_setup_bus(slot
, false);
1447 if (!IS_ERR(mmc
->supply
.vmmc
))
1448 mmc_regulator_set_ocr(mmc
, mmc
->supply
.vmmc
, 0);
1450 if (!IS_ERR(mmc
->supply
.vqmmc
) && slot
->host
->vqmmc_enabled
)
1451 regulator_disable(mmc
->supply
.vqmmc
);
1452 slot
->host
->vqmmc_enabled
= false;
1454 regs
= mci_readl(slot
->host
, PWREN
);
1455 regs
&= ~(1 << slot
->id
);
1456 mci_writel(slot
->host
, PWREN
, regs
);
1462 if (slot
->host
->state
== STATE_WAITING_CMD11_DONE
&& ios
->clock
!= 0)
1463 slot
->host
->state
= STATE_IDLE
;
1466 static int dw_mci_card_busy(struct mmc_host
*mmc
)
1468 struct dw_mci_slot
*slot
= mmc_priv(mmc
);
1472 * Check the busy bit which is low when DAT[3:0]
1473 * (the data lines) are 0000
1475 status
= mci_readl(slot
->host
, STATUS
);
1477 return !!(status
& SDMMC_STATUS_BUSY
);
1480 static int dw_mci_switch_voltage(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
1482 struct dw_mci_slot
*slot
= mmc_priv(mmc
);
1483 struct dw_mci
*host
= slot
->host
;
1484 const struct dw_mci_drv_data
*drv_data
= host
->drv_data
;
1486 u32 v18
= SDMMC_UHS_18V
<< slot
->id
;
1489 if (drv_data
&& drv_data
->switch_voltage
)
1490 return drv_data
->switch_voltage(mmc
, ios
);
1493 * Program the voltage. Note that some instances of dw_mmc may use
1494 * the UHS_REG for this. For other instances (like exynos) the UHS_REG
1495 * does no harm but you need to set the regulator directly. Try both.
1497 uhs
= mci_readl(host
, UHS_REG
);
1498 if (ios
->signal_voltage
== MMC_SIGNAL_VOLTAGE_330
)
1503 if (!IS_ERR(mmc
->supply
.vqmmc
)) {
1504 ret
= mmc_regulator_set_vqmmc(mmc
, ios
);
1507 dev_dbg(&mmc
->class_dev
,
1508 "Regulator set error %d - %s V\n",
1509 ret
, uhs
& v18
? "1.8" : "3.3");
1513 mci_writel(host
, UHS_REG
, uhs
);
1518 static int dw_mci_get_ro(struct mmc_host
*mmc
)
1521 struct dw_mci_slot
*slot
= mmc_priv(mmc
);
1522 int gpio_ro
= mmc_gpio_get_ro(mmc
);
1524 /* Use platform get_ro function, else try on board write protect */
1526 read_only
= gpio_ro
;
1529 mci_readl(slot
->host
, WRTPRT
) & (1 << slot
->id
) ? 1 : 0;
1531 dev_dbg(&mmc
->class_dev
, "card is %s\n",
1532 read_only
? "read-only" : "read-write");
1537 static int dw_mci_get_cd(struct mmc_host
*mmc
)
1540 struct dw_mci_slot
*slot
= mmc_priv(mmc
);
1541 struct dw_mci
*host
= slot
->host
;
1542 int gpio_cd
= mmc_gpio_get_cd(mmc
);
1544 /* Use platform get_cd function, else try onboard card detect */
1545 if (((mmc
->caps
& MMC_CAP_NEEDS_POLL
)
1546 || !mmc_card_is_removable(mmc
))) {
1549 if (!test_bit(DW_MMC_CARD_PRESENT
, &slot
->flags
)) {
1550 if (mmc
->caps
& MMC_CAP_NEEDS_POLL
) {
1551 dev_info(&mmc
->class_dev
,
1552 "card is polling.\n");
1554 dev_info(&mmc
->class_dev
,
1555 "card is non-removable.\n");
1557 set_bit(DW_MMC_CARD_PRESENT
, &slot
->flags
);
1561 } else if (gpio_cd
>= 0)
1564 present
= (mci_readl(slot
->host
, CDETECT
) & (1 << slot
->id
))
1567 spin_lock_bh(&host
->lock
);
1568 if (present
&& !test_and_set_bit(DW_MMC_CARD_PRESENT
, &slot
->flags
))
1569 dev_dbg(&mmc
->class_dev
, "card is present\n");
1570 else if (!present
&&
1571 !test_and_clear_bit(DW_MMC_CARD_PRESENT
, &slot
->flags
))
1572 dev_dbg(&mmc
->class_dev
, "card is not present\n");
1573 spin_unlock_bh(&host
->lock
);
1578 static void dw_mci_hw_reset(struct mmc_host
*mmc
)
1580 struct dw_mci_slot
*slot
= mmc_priv(mmc
);
1581 struct dw_mci
*host
= slot
->host
;
1584 if (host
->use_dma
== TRANS_MODE_IDMAC
)
1585 dw_mci_idmac_reset(host
);
1587 if (!dw_mci_ctrl_reset(host
, SDMMC_CTRL_DMA_RESET
|
1588 SDMMC_CTRL_FIFO_RESET
))
1592 * According to eMMC spec, card reset procedure:
1593 * tRstW >= 1us: RST_n pulse width
1594 * tRSCA >= 200us: RST_n to Command time
1595 * tRSTH >= 1us: RST_n high period
1597 reset
= mci_readl(host
, RST_N
);
1598 reset
&= ~(SDMMC_RST_HWACTIVE
<< slot
->id
);
1599 mci_writel(host
, RST_N
, reset
);
1601 reset
|= SDMMC_RST_HWACTIVE
<< slot
->id
;
1602 mci_writel(host
, RST_N
, reset
);
1603 usleep_range(200, 300);
1606 static void dw_mci_init_card(struct mmc_host
*mmc
, struct mmc_card
*card
)
1608 struct dw_mci_slot
*slot
= mmc_priv(mmc
);
1609 struct dw_mci
*host
= slot
->host
;
1612 * Low power mode will stop the card clock when idle. According to the
1613 * description of the CLKENA register we should disable low power mode
1614 * for SDIO cards if we need SDIO interrupts to work.
1616 if (mmc
->caps
& MMC_CAP_SDIO_IRQ
) {
1617 const u32 clken_low_pwr
= SDMMC_CLKEN_LOW_PWR
<< slot
->id
;
1621 clk_en_a_old
= mci_readl(host
, CLKENA
);
1623 if (card
->type
== MMC_TYPE_SDIO
||
1624 card
->type
== MMC_TYPE_SD_COMBO
) {
1625 if (!test_bit(DW_MMC_CARD_NO_LOW_PWR
, &slot
->flags
)) {
1626 pm_runtime_get_noresume(mmc
->parent
);
1627 set_bit(DW_MMC_CARD_NO_LOW_PWR
, &slot
->flags
);
1629 clk_en_a
= clk_en_a_old
& ~clken_low_pwr
;
1631 if (test_bit(DW_MMC_CARD_NO_LOW_PWR
, &slot
->flags
)) {
1632 pm_runtime_put_noidle(mmc
->parent
);
1633 clear_bit(DW_MMC_CARD_NO_LOW_PWR
, &slot
->flags
);
1635 clk_en_a
= clk_en_a_old
| clken_low_pwr
;
1638 if (clk_en_a
!= clk_en_a_old
) {
1639 mci_writel(host
, CLKENA
, clk_en_a
);
1640 mci_send_cmd(slot
, SDMMC_CMD_UPD_CLK
|
1641 SDMMC_CMD_PRV_DAT_WAIT
, 0);
1646 static void dw_mci_enable_sdio_irq(struct mmc_host
*mmc
, int enb
)
1648 struct dw_mci_slot
*slot
= mmc_priv(mmc
);
1649 struct dw_mci
*host
= slot
->host
;
1650 unsigned long irqflags
;
1653 spin_lock_irqsave(&host
->irq_lock
, irqflags
);
1655 /* Enable/disable Slot Specific SDIO interrupt */
1656 int_mask
= mci_readl(host
, INTMASK
);
1658 int_mask
|= SDMMC_INT_SDIO(slot
->sdio_id
);
1660 int_mask
&= ~SDMMC_INT_SDIO(slot
->sdio_id
);
1661 mci_writel(host
, INTMASK
, int_mask
);
1663 spin_unlock_irqrestore(&host
->irq_lock
, irqflags
);
1666 static int dw_mci_execute_tuning(struct mmc_host
*mmc
, u32 opcode
)
1668 struct dw_mci_slot
*slot
= mmc_priv(mmc
);
1669 struct dw_mci
*host
= slot
->host
;
1670 const struct dw_mci_drv_data
*drv_data
= host
->drv_data
;
1673 if (drv_data
&& drv_data
->execute_tuning
)
1674 err
= drv_data
->execute_tuning(slot
, opcode
);
1678 static int dw_mci_prepare_hs400_tuning(struct mmc_host
*mmc
,
1679 struct mmc_ios
*ios
)
1681 struct dw_mci_slot
*slot
= mmc_priv(mmc
);
1682 struct dw_mci
*host
= slot
->host
;
1683 const struct dw_mci_drv_data
*drv_data
= host
->drv_data
;
1685 if (drv_data
&& drv_data
->prepare_hs400_tuning
)
1686 return drv_data
->prepare_hs400_tuning(host
, ios
);
1691 static const struct mmc_host_ops dw_mci_ops
= {
1692 .request
= dw_mci_request
,
1693 .pre_req
= dw_mci_pre_req
,
1694 .post_req
= dw_mci_post_req
,
1695 .set_ios
= dw_mci_set_ios
,
1696 .get_ro
= dw_mci_get_ro
,
1697 .get_cd
= dw_mci_get_cd
,
1698 .hw_reset
= dw_mci_hw_reset
,
1699 .enable_sdio_irq
= dw_mci_enable_sdio_irq
,
1700 .execute_tuning
= dw_mci_execute_tuning
,
1701 .card_busy
= dw_mci_card_busy
,
1702 .start_signal_voltage_switch
= dw_mci_switch_voltage
,
1703 .init_card
= dw_mci_init_card
,
1704 .prepare_hs400_tuning
= dw_mci_prepare_hs400_tuning
,
1707 static void dw_mci_request_end(struct dw_mci
*host
, struct mmc_request
*mrq
)
1708 __releases(&host
->lock
)
1709 __acquires(&host
->lock
)
1711 struct dw_mci_slot
*slot
;
1712 struct mmc_host
*prev_mmc
= host
->cur_slot
->mmc
;
1714 WARN_ON(host
->cmd
|| host
->data
);
1716 host
->cur_slot
->mrq
= NULL
;
1718 if (!list_empty(&host
->queue
)) {
1719 slot
= list_entry(host
->queue
.next
,
1720 struct dw_mci_slot
, queue_node
);
1721 list_del(&slot
->queue_node
);
1722 dev_vdbg(host
->dev
, "list not empty: %s is next\n",
1723 mmc_hostname(slot
->mmc
));
1724 host
->state
= STATE_SENDING_CMD
;
1725 dw_mci_start_request(host
, slot
);
1727 dev_vdbg(host
->dev
, "list empty\n");
1729 if (host
->state
== STATE_SENDING_CMD11
)
1730 host
->state
= STATE_WAITING_CMD11_DONE
;
1732 host
->state
= STATE_IDLE
;
1735 spin_unlock(&host
->lock
);
1736 mmc_request_done(prev_mmc
, mrq
);
1737 spin_lock(&host
->lock
);
1740 static int dw_mci_command_complete(struct dw_mci
*host
, struct mmc_command
*cmd
)
1742 u32 status
= host
->cmd_status
;
1744 host
->cmd_status
= 0;
1746 /* Read the response from the card (up to 16 bytes) */
1747 if (cmd
->flags
& MMC_RSP_PRESENT
) {
1748 if (cmd
->flags
& MMC_RSP_136
) {
1749 cmd
->resp
[3] = mci_readl(host
, RESP0
);
1750 cmd
->resp
[2] = mci_readl(host
, RESP1
);
1751 cmd
->resp
[1] = mci_readl(host
, RESP2
);
1752 cmd
->resp
[0] = mci_readl(host
, RESP3
);
1754 cmd
->resp
[0] = mci_readl(host
, RESP0
);
1761 if (status
& SDMMC_INT_RTO
)
1762 cmd
->error
= -ETIMEDOUT
;
1763 else if ((cmd
->flags
& MMC_RSP_CRC
) && (status
& SDMMC_INT_RCRC
))
1764 cmd
->error
= -EILSEQ
;
1765 else if (status
& SDMMC_INT_RESP_ERR
)
1773 static int dw_mci_data_complete(struct dw_mci
*host
, struct mmc_data
*data
)
1775 u32 status
= host
->data_status
;
1777 if (status
& DW_MCI_DATA_ERROR_FLAGS
) {
1778 if (status
& SDMMC_INT_DRTO
) {
1779 data
->error
= -ETIMEDOUT
;
1780 } else if (status
& SDMMC_INT_DCRC
) {
1781 data
->error
= -EILSEQ
;
1782 } else if (status
& SDMMC_INT_EBE
) {
1783 if (host
->dir_status
==
1784 DW_MCI_SEND_STATUS
) {
1786 * No data CRC status was returned.
1787 * The number of bytes transferred
1788 * will be exaggerated in PIO mode.
1790 data
->bytes_xfered
= 0;
1791 data
->error
= -ETIMEDOUT
;
1792 } else if (host
->dir_status
==
1793 DW_MCI_RECV_STATUS
) {
1794 data
->error
= -EILSEQ
;
1797 /* SDMMC_INT_SBE is included */
1798 data
->error
= -EILSEQ
;
1801 dev_dbg(host
->dev
, "data error, status 0x%08x\n", status
);
1804 * After an error, there may be data lingering
1809 data
->bytes_xfered
= data
->blocks
* data
->blksz
;
1816 static void dw_mci_set_drto(struct dw_mci
*host
)
1818 unsigned int drto_clks
;
1819 unsigned int drto_ms
;
1821 drto_clks
= mci_readl(host
, TMOUT
) >> 8;
1822 drto_ms
= DIV_ROUND_UP(drto_clks
, host
->bus_hz
/ 1000);
1824 /* add a bit spare time */
1827 mod_timer(&host
->dto_timer
, jiffies
+ msecs_to_jiffies(drto_ms
));
1830 static void dw_mci_tasklet_func(unsigned long priv
)
1832 struct dw_mci
*host
= (struct dw_mci
*)priv
;
1833 struct mmc_data
*data
;
1834 struct mmc_command
*cmd
;
1835 struct mmc_request
*mrq
;
1836 enum dw_mci_state state
;
1837 enum dw_mci_state prev_state
;
1840 spin_lock(&host
->lock
);
1842 state
= host
->state
;
1851 case STATE_WAITING_CMD11_DONE
:
1854 case STATE_SENDING_CMD11
:
1855 case STATE_SENDING_CMD
:
1856 if (!test_and_clear_bit(EVENT_CMD_COMPLETE
,
1857 &host
->pending_events
))
1862 set_bit(EVENT_CMD_COMPLETE
, &host
->completed_events
);
1863 err
= dw_mci_command_complete(host
, cmd
);
1864 if (cmd
== mrq
->sbc
&& !err
) {
1865 prev_state
= state
= STATE_SENDING_CMD
;
1866 __dw_mci_start_request(host
, host
->cur_slot
,
1871 if (cmd
->data
&& err
) {
1873 * During UHS tuning sequence, sending the stop
1874 * command after the response CRC error would
1875 * throw the system into a confused state
1876 * causing all future tuning phases to report
1879 * In such case controller will move into a data
1880 * transfer state after a response error or
1881 * response CRC error. Let's let that finish
1882 * before trying to send a stop, so we'll go to
1883 * STATE_SENDING_DATA.
1885 * Although letting the data transfer take place
1886 * will waste a bit of time (we already know
1887 * the command was bad), it can't cause any
1888 * errors since it's possible it would have
1889 * taken place anyway if this tasklet got
1890 * delayed. Allowing the transfer to take place
1891 * avoids races and keeps things simple.
1893 if ((err
!= -ETIMEDOUT
) &&
1894 (cmd
->opcode
== MMC_SEND_TUNING_BLOCK
)) {
1895 state
= STATE_SENDING_DATA
;
1899 dw_mci_stop_dma(host
);
1900 send_stop_abort(host
, data
);
1901 state
= STATE_SENDING_STOP
;
1905 if (!cmd
->data
|| err
) {
1906 dw_mci_request_end(host
, mrq
);
1910 prev_state
= state
= STATE_SENDING_DATA
;
1913 case STATE_SENDING_DATA
:
1915 * We could get a data error and never a transfer
1916 * complete so we'd better check for it here.
1918 * Note that we don't really care if we also got a
1919 * transfer complete; stopping the DMA and sending an
1922 if (test_and_clear_bit(EVENT_DATA_ERROR
,
1923 &host
->pending_events
)) {
1924 dw_mci_stop_dma(host
);
1925 if (!(host
->data_status
& (SDMMC_INT_DRTO
|
1927 send_stop_abort(host
, data
);
1928 state
= STATE_DATA_ERROR
;
1932 if (!test_and_clear_bit(EVENT_XFER_COMPLETE
,
1933 &host
->pending_events
)) {
1935 * If all data-related interrupts don't come
1936 * within the given time in reading data state.
1938 if (host
->dir_status
== DW_MCI_RECV_STATUS
)
1939 dw_mci_set_drto(host
);
1943 set_bit(EVENT_XFER_COMPLETE
, &host
->completed_events
);
1946 * Handle an EVENT_DATA_ERROR that might have shown up
1947 * before the transfer completed. This might not have
1948 * been caught by the check above because the interrupt
1949 * could have gone off between the previous check and
1950 * the check for transfer complete.
1952 * Technically this ought not be needed assuming we
1953 * get a DATA_COMPLETE eventually (we'll notice the
1954 * error and end the request), but it shouldn't hurt.
1956 * This has the advantage of sending the stop command.
1958 if (test_and_clear_bit(EVENT_DATA_ERROR
,
1959 &host
->pending_events
)) {
1960 dw_mci_stop_dma(host
);
1961 if (!(host
->data_status
& (SDMMC_INT_DRTO
|
1963 send_stop_abort(host
, data
);
1964 state
= STATE_DATA_ERROR
;
1967 prev_state
= state
= STATE_DATA_BUSY
;
1971 case STATE_DATA_BUSY
:
1972 if (!test_and_clear_bit(EVENT_DATA_COMPLETE
,
1973 &host
->pending_events
)) {
1975 * If data error interrupt comes but data over
1976 * interrupt doesn't come within the given time.
1977 * in reading data state.
1979 if (host
->dir_status
== DW_MCI_RECV_STATUS
)
1980 dw_mci_set_drto(host
);
1985 set_bit(EVENT_DATA_COMPLETE
, &host
->completed_events
);
1986 err
= dw_mci_data_complete(host
, data
);
1989 if (!data
->stop
|| mrq
->sbc
) {
1990 if (mrq
->sbc
&& data
->stop
)
1991 data
->stop
->error
= 0;
1992 dw_mci_request_end(host
, mrq
);
1996 /* stop command for open-ended transfer*/
1998 send_stop_abort(host
, data
);
2001 * If we don't have a command complete now we'll
2002 * never get one since we just reset everything;
2003 * better end the request.
2005 * If we do have a command complete we'll fall
2006 * through to the SENDING_STOP command and
2007 * everything will be peachy keen.
2009 if (!test_bit(EVENT_CMD_COMPLETE
,
2010 &host
->pending_events
)) {
2012 dw_mci_request_end(host
, mrq
);
2018 * If err has non-zero,
2019 * stop-abort command has been already issued.
2021 prev_state
= state
= STATE_SENDING_STOP
;
2025 case STATE_SENDING_STOP
:
2026 if (!test_and_clear_bit(EVENT_CMD_COMPLETE
,
2027 &host
->pending_events
))
2030 /* CMD error in data command */
2031 if (mrq
->cmd
->error
&& mrq
->data
)
2037 if (!mrq
->sbc
&& mrq
->stop
)
2038 dw_mci_command_complete(host
, mrq
->stop
);
2040 host
->cmd_status
= 0;
2042 dw_mci_request_end(host
, mrq
);
2045 case STATE_DATA_ERROR
:
2046 if (!test_and_clear_bit(EVENT_XFER_COMPLETE
,
2047 &host
->pending_events
))
2050 state
= STATE_DATA_BUSY
;
2053 } while (state
!= prev_state
);
2055 host
->state
= state
;
2057 spin_unlock(&host
->lock
);
2061 /* push final bytes to part_buf, only use during push */
2062 static void dw_mci_set_part_bytes(struct dw_mci
*host
, void *buf
, int cnt
)
2064 memcpy((void *)&host
->part_buf
, buf
, cnt
);
2065 host
->part_buf_count
= cnt
;
2068 /* append bytes to part_buf, only use during push */
2069 static int dw_mci_push_part_bytes(struct dw_mci
*host
, void *buf
, int cnt
)
2071 cnt
= min(cnt
, (1 << host
->data_shift
) - host
->part_buf_count
);
2072 memcpy((void *)&host
->part_buf
+ host
->part_buf_count
, buf
, cnt
);
2073 host
->part_buf_count
+= cnt
;
2077 /* pull first bytes from part_buf, only use during pull */
2078 static int dw_mci_pull_part_bytes(struct dw_mci
*host
, void *buf
, int cnt
)
2080 cnt
= min_t(int, cnt
, host
->part_buf_count
);
2082 memcpy(buf
, (void *)&host
->part_buf
+ host
->part_buf_start
,
2084 host
->part_buf_count
-= cnt
;
2085 host
->part_buf_start
+= cnt
;
2090 /* pull final bytes from the part_buf, assuming it's just been filled */
2091 static void dw_mci_pull_final_bytes(struct dw_mci
*host
, void *buf
, int cnt
)
2093 memcpy(buf
, &host
->part_buf
, cnt
);
2094 host
->part_buf_start
= cnt
;
2095 host
->part_buf_count
= (1 << host
->data_shift
) - cnt
;
2098 static void dw_mci_push_data16(struct dw_mci
*host
, void *buf
, int cnt
)
2100 struct mmc_data
*data
= host
->data
;
2103 /* try and push anything in the part_buf */
2104 if (unlikely(host
->part_buf_count
)) {
2105 int len
= dw_mci_push_part_bytes(host
, buf
, cnt
);
2109 if (host
->part_buf_count
== 2) {
2110 mci_fifo_writew(host
->fifo_reg
, host
->part_buf16
);
2111 host
->part_buf_count
= 0;
2114 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2115 if (unlikely((unsigned long)buf
& 0x1)) {
2117 u16 aligned_buf
[64];
2118 int len
= min(cnt
& -2, (int)sizeof(aligned_buf
));
2119 int items
= len
>> 1;
2121 /* memcpy from input buffer into aligned buffer */
2122 memcpy(aligned_buf
, buf
, len
);
2125 /* push data from aligned buffer into fifo */
2126 for (i
= 0; i
< items
; ++i
)
2127 mci_fifo_writew(host
->fifo_reg
, aligned_buf
[i
]);
2134 for (; cnt
>= 2; cnt
-= 2)
2135 mci_fifo_writew(host
->fifo_reg
, *pdata
++);
2138 /* put anything remaining in the part_buf */
2140 dw_mci_set_part_bytes(host
, buf
, cnt
);
2141 /* Push data if we have reached the expected data length */
2142 if ((data
->bytes_xfered
+ init_cnt
) ==
2143 (data
->blksz
* data
->blocks
))
2144 mci_fifo_writew(host
->fifo_reg
, host
->part_buf16
);
2148 static void dw_mci_pull_data16(struct dw_mci
*host
, void *buf
, int cnt
)
2150 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2151 if (unlikely((unsigned long)buf
& 0x1)) {
2153 /* pull data from fifo into aligned buffer */
2154 u16 aligned_buf
[64];
2155 int len
= min(cnt
& -2, (int)sizeof(aligned_buf
));
2156 int items
= len
>> 1;
2159 for (i
= 0; i
< items
; ++i
)
2160 aligned_buf
[i
] = mci_fifo_readw(host
->fifo_reg
);
2161 /* memcpy from aligned buffer into output buffer */
2162 memcpy(buf
, aligned_buf
, len
);
2171 for (; cnt
>= 2; cnt
-= 2)
2172 *pdata
++ = mci_fifo_readw(host
->fifo_reg
);
2176 host
->part_buf16
= mci_fifo_readw(host
->fifo_reg
);
2177 dw_mci_pull_final_bytes(host
, buf
, cnt
);
2181 static void dw_mci_push_data32(struct dw_mci
*host
, void *buf
, int cnt
)
2183 struct mmc_data
*data
= host
->data
;
2186 /* try and push anything in the part_buf */
2187 if (unlikely(host
->part_buf_count
)) {
2188 int len
= dw_mci_push_part_bytes(host
, buf
, cnt
);
2192 if (host
->part_buf_count
== 4) {
2193 mci_fifo_writel(host
->fifo_reg
, host
->part_buf32
);
2194 host
->part_buf_count
= 0;
2197 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2198 if (unlikely((unsigned long)buf
& 0x3)) {
2200 u32 aligned_buf
[32];
2201 int len
= min(cnt
& -4, (int)sizeof(aligned_buf
));
2202 int items
= len
>> 2;
2204 /* memcpy from input buffer into aligned buffer */
2205 memcpy(aligned_buf
, buf
, len
);
2208 /* push data from aligned buffer into fifo */
2209 for (i
= 0; i
< items
; ++i
)
2210 mci_fifo_writel(host
->fifo_reg
, aligned_buf
[i
]);
2217 for (; cnt
>= 4; cnt
-= 4)
2218 mci_fifo_writel(host
->fifo_reg
, *pdata
++);
2221 /* put anything remaining in the part_buf */
2223 dw_mci_set_part_bytes(host
, buf
, cnt
);
2224 /* Push data if we have reached the expected data length */
2225 if ((data
->bytes_xfered
+ init_cnt
) ==
2226 (data
->blksz
* data
->blocks
))
2227 mci_fifo_writel(host
->fifo_reg
, host
->part_buf32
);
2231 static void dw_mci_pull_data32(struct dw_mci
*host
, void *buf
, int cnt
)
2233 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2234 if (unlikely((unsigned long)buf
& 0x3)) {
2236 /* pull data from fifo into aligned buffer */
2237 u32 aligned_buf
[32];
2238 int len
= min(cnt
& -4, (int)sizeof(aligned_buf
));
2239 int items
= len
>> 2;
2242 for (i
= 0; i
< items
; ++i
)
2243 aligned_buf
[i
] = mci_fifo_readl(host
->fifo_reg
);
2244 /* memcpy from aligned buffer into output buffer */
2245 memcpy(buf
, aligned_buf
, len
);
2254 for (; cnt
>= 4; cnt
-= 4)
2255 *pdata
++ = mci_fifo_readl(host
->fifo_reg
);
2259 host
->part_buf32
= mci_fifo_readl(host
->fifo_reg
);
2260 dw_mci_pull_final_bytes(host
, buf
, cnt
);
2264 static void dw_mci_push_data64(struct dw_mci
*host
, void *buf
, int cnt
)
2266 struct mmc_data
*data
= host
->data
;
2269 /* try and push anything in the part_buf */
2270 if (unlikely(host
->part_buf_count
)) {
2271 int len
= dw_mci_push_part_bytes(host
, buf
, cnt
);
2276 if (host
->part_buf_count
== 8) {
2277 mci_fifo_writeq(host
->fifo_reg
, host
->part_buf
);
2278 host
->part_buf_count
= 0;
2281 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2282 if (unlikely((unsigned long)buf
& 0x7)) {
2284 u64 aligned_buf
[16];
2285 int len
= min(cnt
& -8, (int)sizeof(aligned_buf
));
2286 int items
= len
>> 3;
2288 /* memcpy from input buffer into aligned buffer */
2289 memcpy(aligned_buf
, buf
, len
);
2292 /* push data from aligned buffer into fifo */
2293 for (i
= 0; i
< items
; ++i
)
2294 mci_fifo_writeq(host
->fifo_reg
, aligned_buf
[i
]);
2301 for (; cnt
>= 8; cnt
-= 8)
2302 mci_fifo_writeq(host
->fifo_reg
, *pdata
++);
2305 /* put anything remaining in the part_buf */
2307 dw_mci_set_part_bytes(host
, buf
, cnt
);
2308 /* Push data if we have reached the expected data length */
2309 if ((data
->bytes_xfered
+ init_cnt
) ==
2310 (data
->blksz
* data
->blocks
))
2311 mci_fifo_writeq(host
->fifo_reg
, host
->part_buf
);
2315 static void dw_mci_pull_data64(struct dw_mci
*host
, void *buf
, int cnt
)
2317 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2318 if (unlikely((unsigned long)buf
& 0x7)) {
2320 /* pull data from fifo into aligned buffer */
2321 u64 aligned_buf
[16];
2322 int len
= min(cnt
& -8, (int)sizeof(aligned_buf
));
2323 int items
= len
>> 3;
2326 for (i
= 0; i
< items
; ++i
)
2327 aligned_buf
[i
] = mci_fifo_readq(host
->fifo_reg
);
2329 /* memcpy from aligned buffer into output buffer */
2330 memcpy(buf
, aligned_buf
, len
);
2339 for (; cnt
>= 8; cnt
-= 8)
2340 *pdata
++ = mci_fifo_readq(host
->fifo_reg
);
2344 host
->part_buf
= mci_fifo_readq(host
->fifo_reg
);
2345 dw_mci_pull_final_bytes(host
, buf
, cnt
);
2349 static void dw_mci_pull_data(struct dw_mci
*host
, void *buf
, int cnt
)
2353 /* get remaining partial bytes */
2354 len
= dw_mci_pull_part_bytes(host
, buf
, cnt
);
2355 if (unlikely(len
== cnt
))
2360 /* get the rest of the data */
2361 host
->pull_data(host
, buf
, cnt
);
2364 static void dw_mci_read_data_pio(struct dw_mci
*host
, bool dto
)
2366 struct sg_mapping_iter
*sg_miter
= &host
->sg_miter
;
2368 unsigned int offset
;
2369 struct mmc_data
*data
= host
->data
;
2370 int shift
= host
->data_shift
;
2373 unsigned int remain
, fcnt
;
2376 if (!sg_miter_next(sg_miter
))
2379 host
->sg
= sg_miter
->piter
.sg
;
2380 buf
= sg_miter
->addr
;
2381 remain
= sg_miter
->length
;
2385 fcnt
= (SDMMC_GET_FCNT(mci_readl(host
, STATUS
))
2386 << shift
) + host
->part_buf_count
;
2387 len
= min(remain
, fcnt
);
2390 dw_mci_pull_data(host
, (void *)(buf
+ offset
), len
);
2391 data
->bytes_xfered
+= len
;
2396 sg_miter
->consumed
= offset
;
2397 status
= mci_readl(host
, MINTSTS
);
2398 mci_writel(host
, RINTSTS
, SDMMC_INT_RXDR
);
2399 /* if the RXDR is ready read again */
2400 } while ((status
& SDMMC_INT_RXDR
) ||
2401 (dto
&& SDMMC_GET_FCNT(mci_readl(host
, STATUS
))));
2404 if (!sg_miter_next(sg_miter
))
2406 sg_miter
->consumed
= 0;
2408 sg_miter_stop(sg_miter
);
2412 sg_miter_stop(sg_miter
);
2414 smp_wmb(); /* drain writebuffer */
2415 set_bit(EVENT_XFER_COMPLETE
, &host
->pending_events
);
2418 static void dw_mci_write_data_pio(struct dw_mci
*host
)
2420 struct sg_mapping_iter
*sg_miter
= &host
->sg_miter
;
2422 unsigned int offset
;
2423 struct mmc_data
*data
= host
->data
;
2424 int shift
= host
->data_shift
;
2427 unsigned int fifo_depth
= host
->fifo_depth
;
2428 unsigned int remain
, fcnt
;
2431 if (!sg_miter_next(sg_miter
))
2434 host
->sg
= sg_miter
->piter
.sg
;
2435 buf
= sg_miter
->addr
;
2436 remain
= sg_miter
->length
;
2440 fcnt
= ((fifo_depth
-
2441 SDMMC_GET_FCNT(mci_readl(host
, STATUS
)))
2442 << shift
) - host
->part_buf_count
;
2443 len
= min(remain
, fcnt
);
2446 host
->push_data(host
, (void *)(buf
+ offset
), len
);
2447 data
->bytes_xfered
+= len
;
2452 sg_miter
->consumed
= offset
;
2453 status
= mci_readl(host
, MINTSTS
);
2454 mci_writel(host
, RINTSTS
, SDMMC_INT_TXDR
);
2455 } while (status
& SDMMC_INT_TXDR
); /* if TXDR write again */
2458 if (!sg_miter_next(sg_miter
))
2460 sg_miter
->consumed
= 0;
2462 sg_miter_stop(sg_miter
);
2466 sg_miter_stop(sg_miter
);
2468 smp_wmb(); /* drain writebuffer */
2469 set_bit(EVENT_XFER_COMPLETE
, &host
->pending_events
);
2472 static void dw_mci_cmd_interrupt(struct dw_mci
*host
, u32 status
)
2474 if (!host
->cmd_status
)
2475 host
->cmd_status
= status
;
2477 smp_wmb(); /* drain writebuffer */
2479 set_bit(EVENT_CMD_COMPLETE
, &host
->pending_events
);
2480 tasklet_schedule(&host
->tasklet
);
2483 static void dw_mci_handle_cd(struct dw_mci
*host
)
2487 for (i
= 0; i
< host
->num_slots
; i
++) {
2488 struct dw_mci_slot
*slot
= host
->slot
[i
];
2493 if (slot
->mmc
->ops
->card_event
)
2494 slot
->mmc
->ops
->card_event(slot
->mmc
);
2495 mmc_detect_change(slot
->mmc
,
2496 msecs_to_jiffies(host
->pdata
->detect_delay_ms
));
2500 static irqreturn_t
dw_mci_interrupt(int irq
, void *dev_id
)
2502 struct dw_mci
*host
= dev_id
;
2506 pending
= mci_readl(host
, MINTSTS
); /* read-only mask reg */
2509 /* Check volt switch first, since it can look like an error */
2510 if ((host
->state
== STATE_SENDING_CMD11
) &&
2511 (pending
& SDMMC_INT_VOLT_SWITCH
)) {
2512 unsigned long irqflags
;
2514 mci_writel(host
, RINTSTS
, SDMMC_INT_VOLT_SWITCH
);
2515 pending
&= ~SDMMC_INT_VOLT_SWITCH
;
2518 * Hold the lock; we know cmd11_timer can't be kicked
2519 * off after the lock is released, so safe to delete.
2521 spin_lock_irqsave(&host
->irq_lock
, irqflags
);
2522 dw_mci_cmd_interrupt(host
, pending
);
2523 spin_unlock_irqrestore(&host
->irq_lock
, irqflags
);
2525 del_timer(&host
->cmd11_timer
);
2528 if (pending
& DW_MCI_CMD_ERROR_FLAGS
) {
2529 mci_writel(host
, RINTSTS
, DW_MCI_CMD_ERROR_FLAGS
);
2530 host
->cmd_status
= pending
;
2531 smp_wmb(); /* drain writebuffer */
2532 set_bit(EVENT_CMD_COMPLETE
, &host
->pending_events
);
2535 if (pending
& DW_MCI_DATA_ERROR_FLAGS
) {
2536 /* if there is an error report DATA_ERROR */
2537 mci_writel(host
, RINTSTS
, DW_MCI_DATA_ERROR_FLAGS
);
2538 host
->data_status
= pending
;
2539 smp_wmb(); /* drain writebuffer */
2540 set_bit(EVENT_DATA_ERROR
, &host
->pending_events
);
2541 tasklet_schedule(&host
->tasklet
);
2544 if (pending
& SDMMC_INT_DATA_OVER
) {
2545 del_timer(&host
->dto_timer
);
2547 mci_writel(host
, RINTSTS
, SDMMC_INT_DATA_OVER
);
2548 if (!host
->data_status
)
2549 host
->data_status
= pending
;
2550 smp_wmb(); /* drain writebuffer */
2551 if (host
->dir_status
== DW_MCI_RECV_STATUS
) {
2552 if (host
->sg
!= NULL
)
2553 dw_mci_read_data_pio(host
, true);
2555 set_bit(EVENT_DATA_COMPLETE
, &host
->pending_events
);
2556 tasklet_schedule(&host
->tasklet
);
2559 if (pending
& SDMMC_INT_RXDR
) {
2560 mci_writel(host
, RINTSTS
, SDMMC_INT_RXDR
);
2561 if (host
->dir_status
== DW_MCI_RECV_STATUS
&& host
->sg
)
2562 dw_mci_read_data_pio(host
, false);
2565 if (pending
& SDMMC_INT_TXDR
) {
2566 mci_writel(host
, RINTSTS
, SDMMC_INT_TXDR
);
2567 if (host
->dir_status
== DW_MCI_SEND_STATUS
&& host
->sg
)
2568 dw_mci_write_data_pio(host
);
2571 if (pending
& SDMMC_INT_CMD_DONE
) {
2572 mci_writel(host
, RINTSTS
, SDMMC_INT_CMD_DONE
);
2573 dw_mci_cmd_interrupt(host
, pending
);
2576 if (pending
& SDMMC_INT_CD
) {
2577 mci_writel(host
, RINTSTS
, SDMMC_INT_CD
);
2578 dw_mci_handle_cd(host
);
2581 /* Handle SDIO Interrupts */
2582 for (i
= 0; i
< host
->num_slots
; i
++) {
2583 struct dw_mci_slot
*slot
= host
->slot
[i
];
2588 if (pending
& SDMMC_INT_SDIO(slot
->sdio_id
)) {
2589 mci_writel(host
, RINTSTS
,
2590 SDMMC_INT_SDIO(slot
->sdio_id
));
2591 mmc_signal_sdio_irq(slot
->mmc
);
2597 if (host
->use_dma
!= TRANS_MODE_IDMAC
)
2600 /* Handle IDMA interrupts */
2601 if (host
->dma_64bit_address
== 1) {
2602 pending
= mci_readl(host
, IDSTS64
);
2603 if (pending
& (SDMMC_IDMAC_INT_TI
| SDMMC_IDMAC_INT_RI
)) {
2604 mci_writel(host
, IDSTS64
, SDMMC_IDMAC_INT_TI
|
2605 SDMMC_IDMAC_INT_RI
);
2606 mci_writel(host
, IDSTS64
, SDMMC_IDMAC_INT_NI
);
2607 if (!test_bit(EVENT_DATA_ERROR
, &host
->pending_events
))
2608 host
->dma_ops
->complete((void *)host
);
2611 pending
= mci_readl(host
, IDSTS
);
2612 if (pending
& (SDMMC_IDMAC_INT_TI
| SDMMC_IDMAC_INT_RI
)) {
2613 mci_writel(host
, IDSTS
, SDMMC_IDMAC_INT_TI
|
2614 SDMMC_IDMAC_INT_RI
);
2615 mci_writel(host
, IDSTS
, SDMMC_IDMAC_INT_NI
);
2616 if (!test_bit(EVENT_DATA_ERROR
, &host
->pending_events
))
2617 host
->dma_ops
->complete((void *)host
);
2624 static int dw_mci_init_slot(struct dw_mci
*host
, unsigned int id
)
2626 struct mmc_host
*mmc
;
2627 struct dw_mci_slot
*slot
;
2628 const struct dw_mci_drv_data
*drv_data
= host
->drv_data
;
2632 mmc
= mmc_alloc_host(sizeof(struct dw_mci_slot
), host
->dev
);
2636 slot
= mmc_priv(mmc
);
2638 slot
->sdio_id
= host
->sdio_id0
+ id
;
2641 host
->slot
[id
] = slot
;
2643 mmc
->ops
= &dw_mci_ops
;
2644 if (of_property_read_u32_array(host
->dev
->of_node
,
2645 "clock-freq-min-max", freq
, 2)) {
2646 mmc
->f_min
= DW_MCI_FREQ_MIN
;
2647 mmc
->f_max
= DW_MCI_FREQ_MAX
;
2650 "'clock-freq-min-max' property was deprecated.\n");
2651 mmc
->f_min
= freq
[0];
2652 mmc
->f_max
= freq
[1];
2655 /*if there are external regulators, get them*/
2656 ret
= mmc_regulator_get_supply(mmc
);
2657 if (ret
== -EPROBE_DEFER
)
2658 goto err_host_allocated
;
2660 if (!mmc
->ocr_avail
)
2661 mmc
->ocr_avail
= MMC_VDD_32_33
| MMC_VDD_33_34
;
2663 if (host
->pdata
->caps
)
2664 mmc
->caps
= host
->pdata
->caps
;
2667 * Support MMC_CAP_ERASE by default.
2668 * It needs to use trim/discard/erase commands.
2670 mmc
->caps
|= MMC_CAP_ERASE
;
2672 if (host
->pdata
->pm_caps
)
2673 mmc
->pm_caps
= host
->pdata
->pm_caps
;
2675 if (host
->dev
->of_node
) {
2676 ctrl_id
= of_alias_get_id(host
->dev
->of_node
, "mshc");
2680 ctrl_id
= to_platform_device(host
->dev
)->id
;
2682 if (drv_data
&& drv_data
->caps
)
2683 mmc
->caps
|= drv_data
->caps
[ctrl_id
];
2685 if (host
->pdata
->caps2
)
2686 mmc
->caps2
= host
->pdata
->caps2
;
2688 ret
= mmc_of_parse(mmc
);
2690 goto err_host_allocated
;
2692 /* Useful defaults if platform data is unset. */
2693 if (host
->use_dma
== TRANS_MODE_IDMAC
) {
2694 mmc
->max_segs
= host
->ring_size
;
2695 mmc
->max_blk_size
= 65535;
2696 mmc
->max_seg_size
= 0x1000;
2697 mmc
->max_req_size
= mmc
->max_seg_size
* host
->ring_size
;
2698 mmc
->max_blk_count
= mmc
->max_req_size
/ 512;
2699 } else if (host
->use_dma
== TRANS_MODE_EDMAC
) {
2701 mmc
->max_blk_size
= 65535;
2702 mmc
->max_blk_count
= 65535;
2704 mmc
->max_blk_size
* mmc
->max_blk_count
;
2705 mmc
->max_seg_size
= mmc
->max_req_size
;
2707 /* TRANS_MODE_PIO */
2709 mmc
->max_blk_size
= 65535; /* BLKSIZ is 16 bits */
2710 mmc
->max_blk_count
= 512;
2711 mmc
->max_req_size
= mmc
->max_blk_size
*
2713 mmc
->max_seg_size
= mmc
->max_req_size
;
2718 ret
= mmc_add_host(mmc
);
2720 goto err_host_allocated
;
2722 #if defined(CONFIG_DEBUG_FS)
2723 dw_mci_init_debugfs(slot
);
2733 static void dw_mci_cleanup_slot(struct dw_mci_slot
*slot
, unsigned int id
)
2735 /* Debugfs stuff is cleaned up by mmc core */
2736 mmc_remove_host(slot
->mmc
);
2737 slot
->host
->slot
[id
] = NULL
;
2738 mmc_free_host(slot
->mmc
);
2741 static void dw_mci_init_dma(struct dw_mci
*host
)
2744 struct device
*dev
= host
->dev
;
2745 struct device_node
*np
= dev
->of_node
;
2748 * Check tansfer mode from HCON[17:16]
2749 * Clear the ambiguous description of dw_mmc databook:
2750 * 2b'00: No DMA Interface -> Actually means using Internal DMA block
2751 * 2b'01: DesignWare DMA Interface -> Synopsys DW-DMA block
2752 * 2b'10: Generic DMA Interface -> non-Synopsys generic DMA block
2753 * 2b'11: Non DW DMA Interface -> pio only
2754 * Compared to DesignWare DMA Interface, Generic DMA Interface has a
2755 * simpler request/acknowledge handshake mechanism and both of them
2756 * are regarded as external dma master for dw_mmc.
2758 host
->use_dma
= SDMMC_GET_TRANS_MODE(mci_readl(host
, HCON
));
2759 if (host
->use_dma
== DMA_INTERFACE_IDMA
) {
2760 host
->use_dma
= TRANS_MODE_IDMAC
;
2761 } else if (host
->use_dma
== DMA_INTERFACE_DWDMA
||
2762 host
->use_dma
== DMA_INTERFACE_GDMA
) {
2763 host
->use_dma
= TRANS_MODE_EDMAC
;
2768 /* Determine which DMA interface to use */
2769 if (host
->use_dma
== TRANS_MODE_IDMAC
) {
2771 * Check ADDR_CONFIG bit in HCON to find
2772 * IDMAC address bus width
2774 addr_config
= SDMMC_GET_ADDR_CONFIG(mci_readl(host
, HCON
));
2776 if (addr_config
== 1) {
2777 /* host supports IDMAC in 64-bit address mode */
2778 host
->dma_64bit_address
= 1;
2780 "IDMAC supports 64-bit address mode.\n");
2781 if (!dma_set_mask(host
->dev
, DMA_BIT_MASK(64)))
2782 dma_set_coherent_mask(host
->dev
,
2785 /* host supports IDMAC in 32-bit address mode */
2786 host
->dma_64bit_address
= 0;
2788 "IDMAC supports 32-bit address mode.\n");
2791 /* Alloc memory for sg translation */
2792 host
->sg_cpu
= dmam_alloc_coherent(host
->dev
,
2794 &host
->sg_dma
, GFP_KERNEL
);
2795 if (!host
->sg_cpu
) {
2797 "%s: could not alloc DMA memory\n",
2802 host
->dma_ops
= &dw_mci_idmac_ops
;
2803 dev_info(host
->dev
, "Using internal DMA controller.\n");
2805 /* TRANS_MODE_EDMAC: check dma bindings again */
2806 if ((of_property_count_strings(np
, "dma-names") < 0) ||
2807 (!of_find_property(np
, "dmas", NULL
))) {
2810 host
->dma_ops
= &dw_mci_edmac_ops
;
2811 dev_info(host
->dev
, "Using external DMA controller.\n");
2814 if (host
->dma_ops
->init
&& host
->dma_ops
->start
&&
2815 host
->dma_ops
->stop
&& host
->dma_ops
->cleanup
) {
2816 if (host
->dma_ops
->init(host
)) {
2817 dev_err(host
->dev
, "%s: Unable to initialize DMA Controller.\n",
2822 dev_err(host
->dev
, "DMA initialization not found.\n");
2829 dev_info(host
->dev
, "Using PIO mode.\n");
2830 host
->use_dma
= TRANS_MODE_PIO
;
2833 static bool dw_mci_ctrl_reset(struct dw_mci
*host
, u32 reset
)
2835 unsigned long timeout
= jiffies
+ msecs_to_jiffies(500);
2838 ctrl
= mci_readl(host
, CTRL
);
2840 mci_writel(host
, CTRL
, ctrl
);
2842 /* wait till resets clear */
2844 ctrl
= mci_readl(host
, CTRL
);
2845 if (!(ctrl
& reset
))
2847 } while (time_before(jiffies
, timeout
));
2850 "Timeout resetting block (ctrl reset %#x)\n",
2856 static bool dw_mci_reset(struct dw_mci
*host
)
2858 u32 flags
= SDMMC_CTRL_RESET
| SDMMC_CTRL_FIFO_RESET
;
2862 * Reseting generates a block interrupt, hence setting
2863 * the scatter-gather pointer to NULL.
2866 sg_miter_stop(&host
->sg_miter
);
2871 flags
|= SDMMC_CTRL_DMA_RESET
;
2873 if (dw_mci_ctrl_reset(host
, flags
)) {
2875 * In all cases we clear the RAWINTS register to clear any
2878 mci_writel(host
, RINTSTS
, 0xFFFFFFFF);
2880 /* if using dma we wait for dma_req to clear */
2881 if (host
->use_dma
) {
2882 unsigned long timeout
= jiffies
+ msecs_to_jiffies(500);
2886 status
= mci_readl(host
, STATUS
);
2887 if (!(status
& SDMMC_STATUS_DMA_REQ
))
2890 } while (time_before(jiffies
, timeout
));
2892 if (status
& SDMMC_STATUS_DMA_REQ
) {
2894 "%s: Timeout waiting for dma_req to clear during reset\n",
2899 /* when using DMA next we reset the fifo again */
2900 if (!dw_mci_ctrl_reset(host
, SDMMC_CTRL_FIFO_RESET
))
2904 /* if the controller reset bit did clear, then set clock regs */
2905 if (!(mci_readl(host
, CTRL
) & SDMMC_CTRL_RESET
)) {
2907 "%s: fifo/dma reset bits didn't clear but ciu was reset, doing clock update\n",
2913 if (host
->use_dma
== TRANS_MODE_IDMAC
)
2914 /* It is also recommended that we reset and reprogram idmac */
2915 dw_mci_idmac_reset(host
);
2920 /* After a CTRL reset we need to have CIU set clock registers */
2921 mci_send_cmd(host
->cur_slot
, SDMMC_CMD_UPD_CLK
, 0);
2926 static void dw_mci_cmd11_timer(unsigned long arg
)
2928 struct dw_mci
*host
= (struct dw_mci
*)arg
;
2930 if (host
->state
!= STATE_SENDING_CMD11
) {
2931 dev_warn(host
->dev
, "Unexpected CMD11 timeout\n");
2935 host
->cmd_status
= SDMMC_INT_RTO
;
2936 set_bit(EVENT_CMD_COMPLETE
, &host
->pending_events
);
2937 tasklet_schedule(&host
->tasklet
);
2940 static void dw_mci_dto_timer(unsigned long arg
)
2942 struct dw_mci
*host
= (struct dw_mci
*)arg
;
2944 switch (host
->state
) {
2945 case STATE_SENDING_DATA
:
2946 case STATE_DATA_BUSY
:
2948 * If DTO interrupt does NOT come in sending data state,
2949 * we should notify the driver to terminate current transfer
2950 * and report a data timeout to the core.
2952 host
->data_status
= SDMMC_INT_DRTO
;
2953 set_bit(EVENT_DATA_ERROR
, &host
->pending_events
);
2954 set_bit(EVENT_DATA_COMPLETE
, &host
->pending_events
);
2955 tasklet_schedule(&host
->tasklet
);
2963 static struct dw_mci_board
*dw_mci_parse_dt(struct dw_mci
*host
)
2965 struct dw_mci_board
*pdata
;
2966 struct device
*dev
= host
->dev
;
2967 struct device_node
*np
= dev
->of_node
;
2968 const struct dw_mci_drv_data
*drv_data
= host
->drv_data
;
2970 u32 clock_frequency
;
2972 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
2974 return ERR_PTR(-ENOMEM
);
2976 /* find reset controller when exist */
2977 pdata
->rstc
= devm_reset_control_get_optional(dev
, "reset");
2978 if (IS_ERR(pdata
->rstc
)) {
2979 if (PTR_ERR(pdata
->rstc
) == -EPROBE_DEFER
)
2980 return ERR_PTR(-EPROBE_DEFER
);
2983 /* find out number of slots supported */
2984 of_property_read_u32(np
, "num-slots", &pdata
->num_slots
);
2986 if (of_property_read_u32(np
, "fifo-depth", &pdata
->fifo_depth
))
2988 "fifo-depth property not found, using value of FIFOTH register as default\n");
2990 of_property_read_u32(np
, "card-detect-delay", &pdata
->detect_delay_ms
);
2992 of_property_read_u32(np
, "data-addr", &host
->data_addr_override
);
2994 if (of_get_property(np
, "fifo-watermark-aligned", NULL
))
2995 host
->wm_aligned
= true;
2997 if (!of_property_read_u32(np
, "clock-frequency", &clock_frequency
))
2998 pdata
->bus_hz
= clock_frequency
;
3000 if (drv_data
&& drv_data
->parse_dt
) {
3001 ret
= drv_data
->parse_dt(host
);
3003 return ERR_PTR(ret
);
3009 #else /* CONFIG_OF */
3010 static struct dw_mci_board
*dw_mci_parse_dt(struct dw_mci
*host
)
3012 return ERR_PTR(-EINVAL
);
3014 #endif /* CONFIG_OF */
3016 static void dw_mci_enable_cd(struct dw_mci
*host
)
3018 unsigned long irqflags
;
3021 struct dw_mci_slot
*slot
;
3024 * No need for CD if all slots have a non-error GPIO
3025 * as well as broken card detection is found.
3027 for (i
= 0; i
< host
->num_slots
; i
++) {
3028 slot
= host
->slot
[i
];
3029 if (slot
->mmc
->caps
& MMC_CAP_NEEDS_POLL
)
3032 if (mmc_gpio_get_cd(slot
->mmc
) < 0)
3035 if (i
== host
->num_slots
)
3038 spin_lock_irqsave(&host
->irq_lock
, irqflags
);
3039 temp
= mci_readl(host
, INTMASK
);
3040 temp
|= SDMMC_INT_CD
;
3041 mci_writel(host
, INTMASK
, temp
);
3042 spin_unlock_irqrestore(&host
->irq_lock
, irqflags
);
3045 int dw_mci_probe(struct dw_mci
*host
)
3047 const struct dw_mci_drv_data
*drv_data
= host
->drv_data
;
3048 int width
, i
, ret
= 0;
3053 host
->pdata
= dw_mci_parse_dt(host
);
3054 if (PTR_ERR(host
->pdata
) == -EPROBE_DEFER
) {
3055 return -EPROBE_DEFER
;
3056 } else if (IS_ERR(host
->pdata
)) {
3057 dev_err(host
->dev
, "platform data not available\n");
3062 host
->biu_clk
= devm_clk_get(host
->dev
, "biu");
3063 if (IS_ERR(host
->biu_clk
)) {
3064 dev_dbg(host
->dev
, "biu clock not available\n");
3066 ret
= clk_prepare_enable(host
->biu_clk
);
3068 dev_err(host
->dev
, "failed to enable biu clock\n");
3073 host
->ciu_clk
= devm_clk_get(host
->dev
, "ciu");
3074 if (IS_ERR(host
->ciu_clk
)) {
3075 dev_dbg(host
->dev
, "ciu clock not available\n");
3076 host
->bus_hz
= host
->pdata
->bus_hz
;
3078 ret
= clk_prepare_enable(host
->ciu_clk
);
3080 dev_err(host
->dev
, "failed to enable ciu clock\n");
3084 if (host
->pdata
->bus_hz
) {
3085 ret
= clk_set_rate(host
->ciu_clk
, host
->pdata
->bus_hz
);
3088 "Unable to set bus rate to %uHz\n",
3089 host
->pdata
->bus_hz
);
3091 host
->bus_hz
= clk_get_rate(host
->ciu_clk
);
3094 if (!host
->bus_hz
) {
3096 "Platform data must supply bus speed\n");
3101 if (drv_data
&& drv_data
->init
) {
3102 ret
= drv_data
->init(host
);
3105 "implementation specific init failed\n");
3110 if (!IS_ERR(host
->pdata
->rstc
)) {
3111 reset_control_assert(host
->pdata
->rstc
);
3112 usleep_range(10, 50);
3113 reset_control_deassert(host
->pdata
->rstc
);
3116 setup_timer(&host
->cmd11_timer
,
3117 dw_mci_cmd11_timer
, (unsigned long)host
);
3119 setup_timer(&host
->dto_timer
,
3120 dw_mci_dto_timer
, (unsigned long)host
);
3122 spin_lock_init(&host
->lock
);
3123 spin_lock_init(&host
->irq_lock
);
3124 INIT_LIST_HEAD(&host
->queue
);
3127 * Get the host data width - this assumes that HCON has been set with
3128 * the correct values.
3130 i
= SDMMC_GET_HDATA_WIDTH(mci_readl(host
, HCON
));
3132 host
->push_data
= dw_mci_push_data16
;
3133 host
->pull_data
= dw_mci_pull_data16
;
3135 host
->data_shift
= 1;
3136 } else if (i
== 2) {
3137 host
->push_data
= dw_mci_push_data64
;
3138 host
->pull_data
= dw_mci_pull_data64
;
3140 host
->data_shift
= 3;
3142 /* Check for a reserved value, and warn if it is */
3144 "HCON reports a reserved host data width!\n"
3145 "Defaulting to 32-bit access.\n");
3146 host
->push_data
= dw_mci_push_data32
;
3147 host
->pull_data
= dw_mci_pull_data32
;
3149 host
->data_shift
= 2;
3152 /* Reset all blocks */
3153 if (!dw_mci_ctrl_reset(host
, SDMMC_CTRL_ALL_RESET_FLAGS
)) {
3158 host
->dma_ops
= host
->pdata
->dma_ops
;
3159 dw_mci_init_dma(host
);
3161 /* Clear the interrupts for the host controller */
3162 mci_writel(host
, RINTSTS
, 0xFFFFFFFF);
3163 mci_writel(host
, INTMASK
, 0); /* disable all mmc interrupt first */
3165 /* Put in max timeout */
3166 mci_writel(host
, TMOUT
, 0xFFFFFFFF);
3169 * FIFO threshold settings RxMark = fifo_size / 2 - 1,
3170 * Tx Mark = fifo_size / 2 DMA Size = 8
3172 if (!host
->pdata
->fifo_depth
) {
3174 * Power-on value of RX_WMark is FIFO_DEPTH-1, but this may
3175 * have been overwritten by the bootloader, just like we're
3176 * about to do, so if you know the value for your hardware, you
3177 * should put it in the platform data.
3179 fifo_size
= mci_readl(host
, FIFOTH
);
3180 fifo_size
= 1 + ((fifo_size
>> 16) & 0xfff);
3182 fifo_size
= host
->pdata
->fifo_depth
;
3184 host
->fifo_depth
= fifo_size
;
3186 SDMMC_SET_FIFOTH(0x2, fifo_size
/ 2 - 1, fifo_size
/ 2);
3187 mci_writel(host
, FIFOTH
, host
->fifoth_val
);
3189 /* disable clock to CIU */
3190 mci_writel(host
, CLKENA
, 0);
3191 mci_writel(host
, CLKSRC
, 0);
3194 * In 2.40a spec, Data offset is changed.
3195 * Need to check the version-id and set data-offset for DATA register.
3197 host
->verid
= SDMMC_GET_VERID(mci_readl(host
, VERID
));
3198 dev_info(host
->dev
, "Version ID is %04x\n", host
->verid
);
3200 if (host
->data_addr_override
)
3201 host
->fifo_reg
= host
->regs
+ host
->data_addr_override
;
3202 else if (host
->verid
< DW_MMC_240A
)
3203 host
->fifo_reg
= host
->regs
+ DATA_OFFSET
;
3205 host
->fifo_reg
= host
->regs
+ DATA_240A_OFFSET
;
3207 tasklet_init(&host
->tasklet
, dw_mci_tasklet_func
, (unsigned long)host
);
3208 ret
= devm_request_irq(host
->dev
, host
->irq
, dw_mci_interrupt
,
3209 host
->irq_flags
, "dw-mci", host
);
3213 if (host
->pdata
->num_slots
)
3214 host
->num_slots
= host
->pdata
->num_slots
;
3216 host
->num_slots
= 1;
3218 if (host
->num_slots
< 1 ||
3219 host
->num_slots
> SDMMC_GET_SLOT_NUM(mci_readl(host
, HCON
))) {
3221 "Platform data must supply correct num_slots.\n");
3227 * Enable interrupts for command done, data over, data empty,
3228 * receive ready and error such as transmit, receive timeout, crc error
3230 mci_writel(host
, INTMASK
, SDMMC_INT_CMD_DONE
| SDMMC_INT_DATA_OVER
|
3231 SDMMC_INT_TXDR
| SDMMC_INT_RXDR
|
3232 DW_MCI_ERROR_FLAGS
);
3233 /* Enable mci interrupt */
3234 mci_writel(host
, CTRL
, SDMMC_CTRL_INT_ENABLE
);
3237 "DW MMC controller at irq %d,%d bit host data width,%u deep fifo\n",
3238 host
->irq
, width
, fifo_size
);
3240 /* We need at least one slot to succeed */
3241 for (i
= 0; i
< host
->num_slots
; i
++) {
3242 ret
= dw_mci_init_slot(host
, i
);
3244 dev_dbg(host
->dev
, "slot %d init failed\n", i
);
3250 dev_info(host
->dev
, "%d slots initialized\n", init_slots
);
3253 "attempted to initialize %d slots, but failed on all\n",
3258 /* Now that slots are all setup, we can enable card detect */
3259 dw_mci_enable_cd(host
);
3264 if (host
->use_dma
&& host
->dma_ops
->exit
)
3265 host
->dma_ops
->exit(host
);
3267 if (!IS_ERR(host
->pdata
->rstc
))
3268 reset_control_assert(host
->pdata
->rstc
);
3271 clk_disable_unprepare(host
->ciu_clk
);
3274 clk_disable_unprepare(host
->biu_clk
);
3278 EXPORT_SYMBOL(dw_mci_probe
);
3280 void dw_mci_remove(struct dw_mci
*host
)
3284 for (i
= 0; i
< host
->num_slots
; i
++) {
3285 dev_dbg(host
->dev
, "remove slot %d\n", i
);
3287 dw_mci_cleanup_slot(host
->slot
[i
], i
);
3290 mci_writel(host
, RINTSTS
, 0xFFFFFFFF);
3291 mci_writel(host
, INTMASK
, 0); /* disable all mmc interrupt first */
3293 /* disable clock to CIU */
3294 mci_writel(host
, CLKENA
, 0);
3295 mci_writel(host
, CLKSRC
, 0);
3297 if (host
->use_dma
&& host
->dma_ops
->exit
)
3298 host
->dma_ops
->exit(host
);
3300 if (!IS_ERR(host
->pdata
->rstc
))
3301 reset_control_assert(host
->pdata
->rstc
);
3303 clk_disable_unprepare(host
->ciu_clk
);
3304 clk_disable_unprepare(host
->biu_clk
);
3306 EXPORT_SYMBOL(dw_mci_remove
);
3311 int dw_mci_runtime_suspend(struct device
*dev
)
3313 struct dw_mci
*host
= dev_get_drvdata(dev
);
3315 if (host
->use_dma
&& host
->dma_ops
->exit
)
3316 host
->dma_ops
->exit(host
);
3318 clk_disable_unprepare(host
->ciu_clk
);
3320 if (host
->cur_slot
&&
3321 (mmc_can_gpio_cd(host
->cur_slot
->mmc
) ||
3322 !mmc_card_is_removable(host
->cur_slot
->mmc
)))
3323 clk_disable_unprepare(host
->biu_clk
);
3327 EXPORT_SYMBOL(dw_mci_runtime_suspend
);
3329 int dw_mci_runtime_resume(struct device
*dev
)
3332 struct dw_mci
*host
= dev_get_drvdata(dev
);
3334 if (host
->cur_slot
&&
3335 (mmc_can_gpio_cd(host
->cur_slot
->mmc
) ||
3336 !mmc_card_is_removable(host
->cur_slot
->mmc
))) {
3337 ret
= clk_prepare_enable(host
->biu_clk
);
3342 ret
= clk_prepare_enable(host
->ciu_clk
);
3346 if (!dw_mci_ctrl_reset(host
, SDMMC_CTRL_ALL_RESET_FLAGS
)) {
3347 clk_disable_unprepare(host
->ciu_clk
);
3352 if (host
->use_dma
&& host
->dma_ops
->init
)
3353 host
->dma_ops
->init(host
);
3356 * Restore the initial value at FIFOTH register
3357 * And Invalidate the prev_blksz with zero
3359 mci_writel(host
, FIFOTH
, host
->fifoth_val
);
3360 host
->prev_blksz
= 0;
3362 /* Put in max timeout */
3363 mci_writel(host
, TMOUT
, 0xFFFFFFFF);
3365 mci_writel(host
, RINTSTS
, 0xFFFFFFFF);
3366 mci_writel(host
, INTMASK
, SDMMC_INT_CMD_DONE
| SDMMC_INT_DATA_OVER
|
3367 SDMMC_INT_TXDR
| SDMMC_INT_RXDR
|
3368 DW_MCI_ERROR_FLAGS
);
3369 mci_writel(host
, CTRL
, SDMMC_CTRL_INT_ENABLE
);
3371 for (i
= 0; i
< host
->num_slots
; i
++) {
3372 struct dw_mci_slot
*slot
= host
->slot
[i
];
3376 if (slot
->mmc
->pm_flags
& MMC_PM_KEEP_POWER
)
3377 dw_mci_set_ios(slot
->mmc
, &slot
->mmc
->ios
);
3379 /* Force setup bus to guarantee available clock output */
3380 dw_mci_setup_bus(slot
, true);
3383 /* Now that slots are all setup, we can enable card detect */
3384 dw_mci_enable_cd(host
);
3389 if (host
->cur_slot
&&
3390 (mmc_can_gpio_cd(host
->cur_slot
->mmc
) ||
3391 !mmc_card_is_removable(host
->cur_slot
->mmc
)))
3392 clk_disable_unprepare(host
->biu_clk
);
3396 EXPORT_SYMBOL(dw_mci_runtime_resume
);
3397 #endif /* CONFIG_PM */
3399 static int __init
dw_mci_init(void)
3401 pr_info("Synopsys Designware Multimedia Card Interface Driver\n");
3405 static void __exit
dw_mci_exit(void)
3409 module_init(dw_mci_init
);
3410 module_exit(dw_mci_exit
);
3412 MODULE_DESCRIPTION("DW Multimedia Card Interface driver");
3413 MODULE_AUTHOR("NXP Semiconductor VietNam");
3414 MODULE_AUTHOR("Imagination Technologies Ltd");
3415 MODULE_LICENSE("GPL v2");