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/host.h>
31 #include <linux/mmc/mmc.h>
32 #include <linux/mmc/dw_mmc.h>
33 #include <linux/bitops.h>
34 #include <linux/regulator/consumer.h>
35 #include <linux/workqueue.h>
39 /* Common flag combinations */
40 #define DW_MCI_DATA_ERROR_FLAGS (SDMMC_INT_DTO | SDMMC_INT_DCRC | \
41 SDMMC_INT_HTO | SDMMC_INT_SBE | \
43 #define DW_MCI_CMD_ERROR_FLAGS (SDMMC_INT_RTO | SDMMC_INT_RCRC | \
45 #define DW_MCI_ERROR_FLAGS (DW_MCI_DATA_ERROR_FLAGS | \
46 DW_MCI_CMD_ERROR_FLAGS | SDMMC_INT_HLE)
47 #define DW_MCI_SEND_STATUS 1
48 #define DW_MCI_RECV_STATUS 2
49 #define DW_MCI_DMA_THRESHOLD 16
51 #ifdef CONFIG_MMC_DW_IDMAC
53 u32 des0
; /* Control Descriptor */
54 #define IDMAC_DES0_DIC BIT(1)
55 #define IDMAC_DES0_LD BIT(2)
56 #define IDMAC_DES0_FD BIT(3)
57 #define IDMAC_DES0_CH BIT(4)
58 #define IDMAC_DES0_ER BIT(5)
59 #define IDMAC_DES0_CES BIT(30)
60 #define IDMAC_DES0_OWN BIT(31)
62 u32 des1
; /* Buffer sizes */
63 #define IDMAC_SET_BUFFER1_SIZE(d, s) \
64 ((d)->des1 = ((d)->des1 & 0x03ffe000) | ((s) & 0x1fff))
66 u32 des2
; /* buffer 1 physical address */
68 u32 des3
; /* buffer 2 physical address */
70 #endif /* CONFIG_MMC_DW_IDMAC */
73 * struct dw_mci_slot - MMC slot state
74 * @mmc: The mmc_host representing this slot.
75 * @host: The MMC controller this slot is using.
76 * @ctype: Card type for this slot.
77 * @mrq: mmc_request currently being processed or waiting to be
78 * processed, or NULL when the slot is idle.
79 * @queue_node: List node for placing this node in the @queue list of
81 * @clock: Clock rate configured by set_ios(). Protected by host->lock.
82 * @flags: Random state bits associated with the slot.
83 * @id: Number of this slot.
84 * @last_detect_state: Most recently observed card detect state.
92 struct mmc_request
*mrq
;
93 struct list_head queue_node
;
97 #define DW_MMC_CARD_PRESENT 0
98 #define DW_MMC_CARD_NEED_INIT 1
100 int last_detect_state
;
103 static struct workqueue_struct
*dw_mci_card_workqueue
;
105 #if defined(CONFIG_DEBUG_FS)
106 static int dw_mci_req_show(struct seq_file
*s
, void *v
)
108 struct dw_mci_slot
*slot
= s
->private;
109 struct mmc_request
*mrq
;
110 struct mmc_command
*cmd
;
111 struct mmc_command
*stop
;
112 struct mmc_data
*data
;
114 /* Make sure we get a consistent snapshot */
115 spin_lock_bh(&slot
->host
->lock
);
125 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
126 cmd
->opcode
, cmd
->arg
, cmd
->flags
,
127 cmd
->resp
[0], cmd
->resp
[1], cmd
->resp
[2],
128 cmd
->resp
[2], cmd
->error
);
130 seq_printf(s
, "DATA %u / %u * %u flg %x err %d\n",
131 data
->bytes_xfered
, data
->blocks
,
132 data
->blksz
, data
->flags
, data
->error
);
135 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
136 stop
->opcode
, stop
->arg
, stop
->flags
,
137 stop
->resp
[0], stop
->resp
[1], stop
->resp
[2],
138 stop
->resp
[2], stop
->error
);
141 spin_unlock_bh(&slot
->host
->lock
);
146 static int dw_mci_req_open(struct inode
*inode
, struct file
*file
)
148 return single_open(file
, dw_mci_req_show
, inode
->i_private
);
151 static const struct file_operations dw_mci_req_fops
= {
152 .owner
= THIS_MODULE
,
153 .open
= dw_mci_req_open
,
156 .release
= single_release
,
159 static int dw_mci_regs_show(struct seq_file
*s
, void *v
)
161 seq_printf(s
, "STATUS:\t0x%08x\n", SDMMC_STATUS
);
162 seq_printf(s
, "RINTSTS:\t0x%08x\n", SDMMC_RINTSTS
);
163 seq_printf(s
, "CMD:\t0x%08x\n", SDMMC_CMD
);
164 seq_printf(s
, "CTRL:\t0x%08x\n", SDMMC_CTRL
);
165 seq_printf(s
, "INTMASK:\t0x%08x\n", SDMMC_INTMASK
);
166 seq_printf(s
, "CLKENA:\t0x%08x\n", SDMMC_CLKENA
);
171 static int dw_mci_regs_open(struct inode
*inode
, struct file
*file
)
173 return single_open(file
, dw_mci_regs_show
, inode
->i_private
);
176 static const struct file_operations dw_mci_regs_fops
= {
177 .owner
= THIS_MODULE
,
178 .open
= dw_mci_regs_open
,
181 .release
= single_release
,
184 static void dw_mci_init_debugfs(struct dw_mci_slot
*slot
)
186 struct mmc_host
*mmc
= slot
->mmc
;
187 struct dw_mci
*host
= slot
->host
;
191 root
= mmc
->debugfs_root
;
195 node
= debugfs_create_file("regs", S_IRUSR
, root
, host
,
200 node
= debugfs_create_file("req", S_IRUSR
, root
, slot
,
205 node
= debugfs_create_u32("state", S_IRUSR
, root
, (u32
*)&host
->state
);
209 node
= debugfs_create_x32("pending_events", S_IRUSR
, root
,
210 (u32
*)&host
->pending_events
);
214 node
= debugfs_create_x32("completed_events", S_IRUSR
, root
,
215 (u32
*)&host
->completed_events
);
222 dev_err(&mmc
->class_dev
, "failed to initialize debugfs for slot\n");
224 #endif /* defined(CONFIG_DEBUG_FS) */
226 static void dw_mci_set_timeout(struct dw_mci
*host
)
228 /* timeout (maximum) */
229 mci_writel(host
, TMOUT
, 0xffffffff);
232 static u32
dw_mci_prepare_command(struct mmc_host
*mmc
, struct mmc_command
*cmd
)
234 struct mmc_data
*data
;
236 cmd
->error
= -EINPROGRESS
;
240 if (cmdr
== MMC_STOP_TRANSMISSION
)
241 cmdr
|= SDMMC_CMD_STOP
;
243 cmdr
|= SDMMC_CMD_PRV_DAT_WAIT
;
245 if (cmd
->flags
& MMC_RSP_PRESENT
) {
246 /* We expect a response, so set this bit */
247 cmdr
|= SDMMC_CMD_RESP_EXP
;
248 if (cmd
->flags
& MMC_RSP_136
)
249 cmdr
|= SDMMC_CMD_RESP_LONG
;
252 if (cmd
->flags
& MMC_RSP_CRC
)
253 cmdr
|= SDMMC_CMD_RESP_CRC
;
257 cmdr
|= SDMMC_CMD_DAT_EXP
;
258 if (data
->flags
& MMC_DATA_STREAM
)
259 cmdr
|= SDMMC_CMD_STRM_MODE
;
260 if (data
->flags
& MMC_DATA_WRITE
)
261 cmdr
|= SDMMC_CMD_DAT_WR
;
267 static void dw_mci_start_command(struct dw_mci
*host
,
268 struct mmc_command
*cmd
, u32 cmd_flags
)
272 "start command: ARGR=0x%08x CMDR=0x%08x\n",
273 cmd
->arg
, cmd_flags
);
275 mci_writel(host
, CMDARG
, cmd
->arg
);
278 mci_writel(host
, CMD
, cmd_flags
| SDMMC_CMD_START
);
281 static void send_stop_cmd(struct dw_mci
*host
, struct mmc_data
*data
)
283 dw_mci_start_command(host
, data
->stop
, host
->stop_cmdr
);
286 /* DMA interface functions */
287 static void dw_mci_stop_dma(struct dw_mci
*host
)
289 if (host
->using_dma
) {
290 host
->dma_ops
->stop(host
);
291 host
->dma_ops
->cleanup(host
);
293 /* Data transfer was stopped by the interrupt handler */
294 set_bit(EVENT_XFER_COMPLETE
, &host
->pending_events
);
298 static int dw_mci_get_dma_dir(struct mmc_data
*data
)
300 if (data
->flags
& MMC_DATA_WRITE
)
301 return DMA_TO_DEVICE
;
303 return DMA_FROM_DEVICE
;
306 #ifdef CONFIG_MMC_DW_IDMAC
307 static void dw_mci_dma_cleanup(struct dw_mci
*host
)
309 struct mmc_data
*data
= host
->data
;
312 if (!data
->host_cookie
)
313 dma_unmap_sg(&host
->dev
,
316 dw_mci_get_dma_dir(data
));
319 static void dw_mci_idmac_stop_dma(struct dw_mci
*host
)
323 /* Disable and reset the IDMAC interface */
324 temp
= mci_readl(host
, CTRL
);
325 temp
&= ~SDMMC_CTRL_USE_IDMAC
;
326 temp
|= SDMMC_CTRL_DMA_RESET
;
327 mci_writel(host
, CTRL
, temp
);
329 /* Stop the IDMAC running */
330 temp
= mci_readl(host
, BMOD
);
331 temp
&= ~(SDMMC_IDMAC_ENABLE
| SDMMC_IDMAC_FB
);
332 mci_writel(host
, BMOD
, temp
);
335 static void dw_mci_idmac_complete_dma(struct dw_mci
*host
)
337 struct mmc_data
*data
= host
->data
;
339 dev_vdbg(&host
->dev
, "DMA complete\n");
341 host
->dma_ops
->cleanup(host
);
344 * If the card was removed, data will be NULL. No point in trying to
345 * send the stop command or waiting for NBUSY in this case.
348 set_bit(EVENT_XFER_COMPLETE
, &host
->pending_events
);
349 tasklet_schedule(&host
->tasklet
);
353 static void dw_mci_translate_sglist(struct dw_mci
*host
, struct mmc_data
*data
,
357 struct idmac_desc
*desc
= host
->sg_cpu
;
359 for (i
= 0; i
< sg_len
; i
++, desc
++) {
360 unsigned int length
= sg_dma_len(&data
->sg
[i
]);
361 u32 mem_addr
= sg_dma_address(&data
->sg
[i
]);
363 /* Set the OWN bit and disable interrupts for this descriptor */
364 desc
->des0
= IDMAC_DES0_OWN
| IDMAC_DES0_DIC
| IDMAC_DES0_CH
;
367 IDMAC_SET_BUFFER1_SIZE(desc
, length
);
369 /* Physical address to DMA to/from */
370 desc
->des2
= mem_addr
;
373 /* Set first descriptor */
375 desc
->des0
|= IDMAC_DES0_FD
;
377 /* Set last descriptor */
378 desc
= host
->sg_cpu
+ (i
- 1) * sizeof(struct idmac_desc
);
379 desc
->des0
&= ~(IDMAC_DES0_CH
| IDMAC_DES0_DIC
);
380 desc
->des0
|= IDMAC_DES0_LD
;
385 static void dw_mci_idmac_start_dma(struct dw_mci
*host
, unsigned int sg_len
)
389 dw_mci_translate_sglist(host
, host
->data
, sg_len
);
391 /* Select IDMAC interface */
392 temp
= mci_readl(host
, CTRL
);
393 temp
|= SDMMC_CTRL_USE_IDMAC
;
394 mci_writel(host
, CTRL
, temp
);
398 /* Enable the IDMAC */
399 temp
= mci_readl(host
, BMOD
);
400 temp
|= SDMMC_IDMAC_ENABLE
| SDMMC_IDMAC_FB
;
401 mci_writel(host
, BMOD
, temp
);
403 /* Start it running */
404 mci_writel(host
, PLDMND
, 1);
407 static int dw_mci_idmac_init(struct dw_mci
*host
)
409 struct idmac_desc
*p
;
412 /* Number of descriptors in the ring buffer */
413 host
->ring_size
= PAGE_SIZE
/ sizeof(struct idmac_desc
);
415 /* Forward link the descriptor list */
416 for (i
= 0, p
= host
->sg_cpu
; i
< host
->ring_size
- 1; i
++, p
++)
417 p
->des3
= host
->sg_dma
+ (sizeof(struct idmac_desc
) * (i
+ 1));
419 /* Set the last descriptor as the end-of-ring descriptor */
420 p
->des3
= host
->sg_dma
;
421 p
->des0
= IDMAC_DES0_ER
;
423 /* Mask out interrupts - get Tx & Rx complete only */
424 mci_writel(host
, IDINTEN
, SDMMC_IDMAC_INT_NI
| SDMMC_IDMAC_INT_RI
|
427 /* Set the descriptor base address */
428 mci_writel(host
, DBADDR
, host
->sg_dma
);
432 static int dw_mci_pre_dma_transfer(struct dw_mci
*host
,
433 struct mmc_data
*data
,
436 struct scatterlist
*sg
;
437 unsigned int i
, sg_len
;
439 if (!next
&& data
->host_cookie
)
440 return data
->host_cookie
;
443 * We don't do DMA on "complex" transfers, i.e. with
444 * non-word-aligned buffers or lengths. Also, we don't bother
445 * with all the DMA setup overhead for short transfers.
447 if (data
->blocks
* data
->blksz
< DW_MCI_DMA_THRESHOLD
)
453 for_each_sg(data
->sg
, sg
, data
->sg_len
, i
) {
454 if (sg
->offset
& 3 || sg
->length
& 3)
458 sg_len
= dma_map_sg(&host
->dev
,
461 dw_mci_get_dma_dir(data
));
466 data
->host_cookie
= sg_len
;
471 static struct dw_mci_dma_ops dw_mci_idmac_ops
= {
472 .init
= dw_mci_idmac_init
,
473 .start
= dw_mci_idmac_start_dma
,
474 .stop
= dw_mci_idmac_stop_dma
,
475 .complete
= dw_mci_idmac_complete_dma
,
476 .cleanup
= dw_mci_dma_cleanup
,
479 static int dw_mci_pre_dma_transfer(struct dw_mci
*host
,
480 struct mmc_data
*data
,
485 #endif /* CONFIG_MMC_DW_IDMAC */
487 static void dw_mci_pre_req(struct mmc_host
*mmc
,
488 struct mmc_request
*mrq
,
491 struct dw_mci_slot
*slot
= mmc_priv(mmc
);
492 struct mmc_data
*data
= mrq
->data
;
494 if (!slot
->host
->use_dma
|| !data
)
497 if (data
->host_cookie
) {
498 data
->host_cookie
= 0;
502 if (dw_mci_pre_dma_transfer(slot
->host
, mrq
->data
, 1) < 0)
503 data
->host_cookie
= 0;
506 static void dw_mci_post_req(struct mmc_host
*mmc
,
507 struct mmc_request
*mrq
,
510 struct dw_mci_slot
*slot
= mmc_priv(mmc
);
511 struct mmc_data
*data
= mrq
->data
;
513 if (!slot
->host
->use_dma
|| !data
)
516 if (data
->host_cookie
)
517 dma_unmap_sg(&slot
->host
->dev
,
520 dw_mci_get_dma_dir(data
));
521 data
->host_cookie
= 0;
524 static int dw_mci_submit_data_dma(struct dw_mci
*host
, struct mmc_data
*data
)
531 /* If we don't have a channel, we can't do DMA */
535 sg_len
= dw_mci_pre_dma_transfer(host
, data
, 0);
542 "sd sg_cpu: %#lx sg_dma: %#lx sg_len: %d\n",
543 (unsigned long)host
->sg_cpu
, (unsigned long)host
->sg_dma
,
546 /* Enable the DMA interface */
547 temp
= mci_readl(host
, CTRL
);
548 temp
|= SDMMC_CTRL_DMA_ENABLE
;
549 mci_writel(host
, CTRL
, temp
);
551 /* Disable RX/TX IRQs, let DMA handle it */
552 temp
= mci_readl(host
, INTMASK
);
553 temp
&= ~(SDMMC_INT_RXDR
| SDMMC_INT_TXDR
);
554 mci_writel(host
, INTMASK
, temp
);
556 host
->dma_ops
->start(host
, sg_len
);
561 static void dw_mci_submit_data(struct dw_mci
*host
, struct mmc_data
*data
)
565 data
->error
= -EINPROGRESS
;
571 if (data
->flags
& MMC_DATA_READ
)
572 host
->dir_status
= DW_MCI_RECV_STATUS
;
574 host
->dir_status
= DW_MCI_SEND_STATUS
;
576 if (dw_mci_submit_data_dma(host
, data
)) {
577 int flags
= SG_MITER_ATOMIC
;
578 if (host
->data
->flags
& MMC_DATA_READ
)
579 flags
|= SG_MITER_TO_SG
;
581 flags
|= SG_MITER_FROM_SG
;
583 sg_miter_start(&host
->sg_miter
, data
->sg
, data
->sg_len
, flags
);
585 host
->part_buf_start
= 0;
586 host
->part_buf_count
= 0;
588 mci_writel(host
, RINTSTS
, SDMMC_INT_TXDR
| SDMMC_INT_RXDR
);
589 temp
= mci_readl(host
, INTMASK
);
590 temp
|= SDMMC_INT_TXDR
| SDMMC_INT_RXDR
;
591 mci_writel(host
, INTMASK
, temp
);
593 temp
= mci_readl(host
, CTRL
);
594 temp
&= ~SDMMC_CTRL_DMA_ENABLE
;
595 mci_writel(host
, CTRL
, temp
);
599 static void mci_send_cmd(struct dw_mci_slot
*slot
, u32 cmd
, u32 arg
)
601 struct dw_mci
*host
= slot
->host
;
602 unsigned long timeout
= jiffies
+ msecs_to_jiffies(500);
603 unsigned int cmd_status
= 0;
605 mci_writel(host
, CMDARG
, arg
);
607 mci_writel(host
, CMD
, SDMMC_CMD_START
| cmd
);
609 while (time_before(jiffies
, timeout
)) {
610 cmd_status
= mci_readl(host
, CMD
);
611 if (!(cmd_status
& SDMMC_CMD_START
))
614 dev_err(&slot
->mmc
->class_dev
,
615 "Timeout sending command (cmd %#x arg %#x status %#x)\n",
616 cmd
, arg
, cmd_status
);
619 static void dw_mci_setup_bus(struct dw_mci_slot
*slot
)
621 struct dw_mci
*host
= slot
->host
;
624 if (slot
->clock
!= host
->current_speed
) {
625 if (host
->bus_hz
% slot
->clock
)
627 * move the + 1 after the divide to prevent
628 * over-clocking the card.
630 div
= ((host
->bus_hz
/ slot
->clock
) >> 1) + 1;
632 div
= (host
->bus_hz
/ slot
->clock
) >> 1;
634 dev_info(&slot
->mmc
->class_dev
,
635 "Bus speed (slot %d) = %dHz (slot req %dHz, actual %dHZ"
636 " div = %d)\n", slot
->id
, host
->bus_hz
, slot
->clock
,
637 div
? ((host
->bus_hz
/ div
) >> 1) : host
->bus_hz
, div
);
640 mci_writel(host
, CLKENA
, 0);
641 mci_writel(host
, CLKSRC
, 0);
645 SDMMC_CMD_UPD_CLK
| SDMMC_CMD_PRV_DAT_WAIT
, 0);
647 /* set clock to desired speed */
648 mci_writel(host
, CLKDIV
, div
);
652 SDMMC_CMD_UPD_CLK
| SDMMC_CMD_PRV_DAT_WAIT
, 0);
655 mci_writel(host
, CLKENA
, ((SDMMC_CLKEN_ENABLE
|
656 SDMMC_CLKEN_LOW_PWR
) << slot
->id
));
660 SDMMC_CMD_UPD_CLK
| SDMMC_CMD_PRV_DAT_WAIT
, 0);
662 host
->current_speed
= slot
->clock
;
665 /* Set the current slot bus width */
666 mci_writel(host
, CTYPE
, (slot
->ctype
<< slot
->id
));
669 static void __dw_mci_start_request(struct dw_mci
*host
,
670 struct dw_mci_slot
*slot
,
671 struct mmc_command
*cmd
)
673 struct mmc_request
*mrq
;
674 struct mmc_data
*data
;
678 if (host
->pdata
->select_slot
)
679 host
->pdata
->select_slot(slot
->id
);
681 /* Slot specific timing and width adjustment */
682 dw_mci_setup_bus(slot
);
684 host
->cur_slot
= slot
;
687 host
->pending_events
= 0;
688 host
->completed_events
= 0;
689 host
->data_status
= 0;
693 dw_mci_set_timeout(host
);
694 mci_writel(host
, BYTCNT
, data
->blksz
*data
->blocks
);
695 mci_writel(host
, BLKSIZ
, data
->blksz
);
698 cmdflags
= dw_mci_prepare_command(slot
->mmc
, cmd
);
700 /* this is the first command, send the initialization clock */
701 if (test_and_clear_bit(DW_MMC_CARD_NEED_INIT
, &slot
->flags
))
702 cmdflags
|= SDMMC_CMD_INIT
;
705 dw_mci_submit_data(host
, data
);
709 dw_mci_start_command(host
, cmd
, cmdflags
);
712 host
->stop_cmdr
= dw_mci_prepare_command(slot
->mmc
, mrq
->stop
);
715 static void dw_mci_start_request(struct dw_mci
*host
,
716 struct dw_mci_slot
*slot
)
718 struct mmc_request
*mrq
= slot
->mrq
;
719 struct mmc_command
*cmd
;
721 cmd
= mrq
->sbc
? mrq
->sbc
: mrq
->cmd
;
722 __dw_mci_start_request(host
, slot
, cmd
);
725 /* must be called with host->lock held */
726 static void dw_mci_queue_request(struct dw_mci
*host
, struct dw_mci_slot
*slot
,
727 struct mmc_request
*mrq
)
729 dev_vdbg(&slot
->mmc
->class_dev
, "queue request: state=%d\n",
734 if (host
->state
== STATE_IDLE
) {
735 host
->state
= STATE_SENDING_CMD
;
736 dw_mci_start_request(host
, slot
);
738 list_add_tail(&slot
->queue_node
, &host
->queue
);
742 static void dw_mci_request(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
744 struct dw_mci_slot
*slot
= mmc_priv(mmc
);
745 struct dw_mci
*host
= slot
->host
;
750 * The check for card presence and queueing of the request must be
751 * atomic, otherwise the card could be removed in between and the
752 * request wouldn't fail until another card was inserted.
754 spin_lock_bh(&host
->lock
);
756 if (!test_bit(DW_MMC_CARD_PRESENT
, &slot
->flags
)) {
757 spin_unlock_bh(&host
->lock
);
758 mrq
->cmd
->error
= -ENOMEDIUM
;
759 mmc_request_done(mmc
, mrq
);
763 dw_mci_queue_request(host
, slot
, mrq
);
765 spin_unlock_bh(&host
->lock
);
768 static void dw_mci_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
770 struct dw_mci_slot
*slot
= mmc_priv(mmc
);
773 /* set default 1 bit mode */
774 slot
->ctype
= SDMMC_CTYPE_1BIT
;
776 switch (ios
->bus_width
) {
777 case MMC_BUS_WIDTH_1
:
778 slot
->ctype
= SDMMC_CTYPE_1BIT
;
780 case MMC_BUS_WIDTH_4
:
781 slot
->ctype
= SDMMC_CTYPE_4BIT
;
783 case MMC_BUS_WIDTH_8
:
784 slot
->ctype
= SDMMC_CTYPE_8BIT
;
788 regs
= mci_readl(slot
->host
, UHS_REG
);
791 if (ios
->timing
== MMC_TIMING_UHS_DDR50
)
792 regs
|= (0x1 << slot
->id
) << 16;
794 regs
&= ~(0x1 << slot
->id
) << 16;
796 mci_writel(slot
->host
, UHS_REG
, regs
);
800 * Use mirror of ios->clock to prevent race with mmc
801 * core ios update when finding the minimum.
803 slot
->clock
= ios
->clock
;
806 switch (ios
->power_mode
) {
808 set_bit(DW_MMC_CARD_NEED_INIT
, &slot
->flags
);
815 static int dw_mci_get_ro(struct mmc_host
*mmc
)
818 struct dw_mci_slot
*slot
= mmc_priv(mmc
);
819 struct dw_mci_board
*brd
= slot
->host
->pdata
;
821 /* Use platform get_ro function, else try on board write protect */
823 read_only
= brd
->get_ro(slot
->id
);
826 mci_readl(slot
->host
, WRTPRT
) & (1 << slot
->id
) ? 1 : 0;
828 dev_dbg(&mmc
->class_dev
, "card is %s\n",
829 read_only
? "read-only" : "read-write");
834 static int dw_mci_get_cd(struct mmc_host
*mmc
)
837 struct dw_mci_slot
*slot
= mmc_priv(mmc
);
838 struct dw_mci_board
*brd
= slot
->host
->pdata
;
840 /* Use platform get_cd function, else try onboard card detect */
841 if (brd
->quirks
& DW_MCI_QUIRK_BROKEN_CARD_DETECTION
)
843 else if (brd
->get_cd
)
844 present
= !brd
->get_cd(slot
->id
);
846 present
= (mci_readl(slot
->host
, CDETECT
) & (1 << slot
->id
))
850 dev_dbg(&mmc
->class_dev
, "card is present\n");
852 dev_dbg(&mmc
->class_dev
, "card is not present\n");
857 static void dw_mci_enable_sdio_irq(struct mmc_host
*mmc
, int enb
)
859 struct dw_mci_slot
*slot
= mmc_priv(mmc
);
860 struct dw_mci
*host
= slot
->host
;
863 /* Enable/disable Slot Specific SDIO interrupt */
864 int_mask
= mci_readl(host
, INTMASK
);
866 mci_writel(host
, INTMASK
,
867 (int_mask
| (1 << SDMMC_INT_SDIO(slot
->id
))));
869 mci_writel(host
, INTMASK
,
870 (int_mask
& ~(1 << SDMMC_INT_SDIO(slot
->id
))));
874 static const struct mmc_host_ops dw_mci_ops
= {
875 .request
= dw_mci_request
,
876 .pre_req
= dw_mci_pre_req
,
877 .post_req
= dw_mci_post_req
,
878 .set_ios
= dw_mci_set_ios
,
879 .get_ro
= dw_mci_get_ro
,
880 .get_cd
= dw_mci_get_cd
,
881 .enable_sdio_irq
= dw_mci_enable_sdio_irq
,
884 static void dw_mci_request_end(struct dw_mci
*host
, struct mmc_request
*mrq
)
885 __releases(&host
->lock
)
886 __acquires(&host
->lock
)
888 struct dw_mci_slot
*slot
;
889 struct mmc_host
*prev_mmc
= host
->cur_slot
->mmc
;
891 WARN_ON(host
->cmd
|| host
->data
);
893 host
->cur_slot
->mrq
= NULL
;
895 if (!list_empty(&host
->queue
)) {
896 slot
= list_entry(host
->queue
.next
,
897 struct dw_mci_slot
, queue_node
);
898 list_del(&slot
->queue_node
);
899 dev_vdbg(&host
->dev
, "list not empty: %s is next\n",
900 mmc_hostname(slot
->mmc
));
901 host
->state
= STATE_SENDING_CMD
;
902 dw_mci_start_request(host
, slot
);
904 dev_vdbg(&host
->dev
, "list empty\n");
905 host
->state
= STATE_IDLE
;
908 spin_unlock(&host
->lock
);
909 mmc_request_done(prev_mmc
, mrq
);
910 spin_lock(&host
->lock
);
913 static void dw_mci_command_complete(struct dw_mci
*host
, struct mmc_command
*cmd
)
915 u32 status
= host
->cmd_status
;
917 host
->cmd_status
= 0;
919 /* Read the response from the card (up to 16 bytes) */
920 if (cmd
->flags
& MMC_RSP_PRESENT
) {
921 if (cmd
->flags
& MMC_RSP_136
) {
922 cmd
->resp
[3] = mci_readl(host
, RESP0
);
923 cmd
->resp
[2] = mci_readl(host
, RESP1
);
924 cmd
->resp
[1] = mci_readl(host
, RESP2
);
925 cmd
->resp
[0] = mci_readl(host
, RESP3
);
927 cmd
->resp
[0] = mci_readl(host
, RESP0
);
934 if (status
& SDMMC_INT_RTO
)
935 cmd
->error
= -ETIMEDOUT
;
936 else if ((cmd
->flags
& MMC_RSP_CRC
) && (status
& SDMMC_INT_RCRC
))
937 cmd
->error
= -EILSEQ
;
938 else if (status
& SDMMC_INT_RESP_ERR
)
944 /* newer ip versions need a delay between retries */
945 if (host
->quirks
& DW_MCI_QUIRK_RETRY_DELAY
)
950 dw_mci_stop_dma(host
);
955 static void dw_mci_tasklet_func(unsigned long priv
)
957 struct dw_mci
*host
= (struct dw_mci
*)priv
;
958 struct mmc_data
*data
;
959 struct mmc_command
*cmd
;
960 enum dw_mci_state state
;
961 enum dw_mci_state prev_state
;
964 spin_lock(&host
->lock
);
976 case STATE_SENDING_CMD
:
977 if (!test_and_clear_bit(EVENT_CMD_COMPLETE
,
978 &host
->pending_events
))
983 set_bit(EVENT_CMD_COMPLETE
, &host
->completed_events
);
984 dw_mci_command_complete(host
, cmd
);
985 if (cmd
== host
->mrq
->sbc
&& !cmd
->error
) {
986 prev_state
= state
= STATE_SENDING_CMD
;
987 __dw_mci_start_request(host
, host
->cur_slot
,
992 if (!host
->mrq
->data
|| cmd
->error
) {
993 dw_mci_request_end(host
, host
->mrq
);
997 prev_state
= state
= STATE_SENDING_DATA
;
1000 case STATE_SENDING_DATA
:
1001 if (test_and_clear_bit(EVENT_DATA_ERROR
,
1002 &host
->pending_events
)) {
1003 dw_mci_stop_dma(host
);
1005 send_stop_cmd(host
, data
);
1006 state
= STATE_DATA_ERROR
;
1010 if (!test_and_clear_bit(EVENT_XFER_COMPLETE
,
1011 &host
->pending_events
))
1014 set_bit(EVENT_XFER_COMPLETE
, &host
->completed_events
);
1015 prev_state
= state
= STATE_DATA_BUSY
;
1018 case STATE_DATA_BUSY
:
1019 if (!test_and_clear_bit(EVENT_DATA_COMPLETE
,
1020 &host
->pending_events
))
1024 set_bit(EVENT_DATA_COMPLETE
, &host
->completed_events
);
1025 status
= host
->data_status
;
1027 if (status
& DW_MCI_DATA_ERROR_FLAGS
) {
1028 if (status
& SDMMC_INT_DTO
) {
1029 data
->error
= -ETIMEDOUT
;
1030 } else if (status
& SDMMC_INT_DCRC
) {
1031 data
->error
= -EILSEQ
;
1032 } else if (status
& SDMMC_INT_EBE
&&
1034 DW_MCI_SEND_STATUS
) {
1036 * No data CRC status was returned.
1037 * The number of bytes transferred will
1038 * be exaggerated in PIO mode.
1040 data
->bytes_xfered
= 0;
1041 data
->error
= -ETIMEDOUT
;
1050 * After an error, there may be data lingering
1051 * in the FIFO, so reset it - doing so
1052 * generates a block interrupt, hence setting
1053 * the scatter-gather pointer to NULL.
1055 sg_miter_stop(&host
->sg_miter
);
1057 ctrl
= mci_readl(host
, CTRL
);
1058 ctrl
|= SDMMC_CTRL_FIFO_RESET
;
1059 mci_writel(host
, CTRL
, ctrl
);
1061 data
->bytes_xfered
= data
->blocks
* data
->blksz
;
1066 dw_mci_request_end(host
, host
->mrq
);
1070 if (host
->mrq
->sbc
&& !data
->error
) {
1071 data
->stop
->error
= 0;
1072 dw_mci_request_end(host
, host
->mrq
);
1076 prev_state
= state
= STATE_SENDING_STOP
;
1078 send_stop_cmd(host
, data
);
1081 case STATE_SENDING_STOP
:
1082 if (!test_and_clear_bit(EVENT_CMD_COMPLETE
,
1083 &host
->pending_events
))
1087 dw_mci_command_complete(host
, host
->mrq
->stop
);
1088 dw_mci_request_end(host
, host
->mrq
);
1091 case STATE_DATA_ERROR
:
1092 if (!test_and_clear_bit(EVENT_XFER_COMPLETE
,
1093 &host
->pending_events
))
1096 state
= STATE_DATA_BUSY
;
1099 } while (state
!= prev_state
);
1101 host
->state
= state
;
1103 spin_unlock(&host
->lock
);
1107 /* push final bytes to part_buf, only use during push */
1108 static void dw_mci_set_part_bytes(struct dw_mci
*host
, void *buf
, int cnt
)
1110 memcpy((void *)&host
->part_buf
, buf
, cnt
);
1111 host
->part_buf_count
= cnt
;
1114 /* append bytes to part_buf, only use during push */
1115 static int dw_mci_push_part_bytes(struct dw_mci
*host
, void *buf
, int cnt
)
1117 cnt
= min(cnt
, (1 << host
->data_shift
) - host
->part_buf_count
);
1118 memcpy((void *)&host
->part_buf
+ host
->part_buf_count
, buf
, cnt
);
1119 host
->part_buf_count
+= cnt
;
1123 /* pull first bytes from part_buf, only use during pull */
1124 static int dw_mci_pull_part_bytes(struct dw_mci
*host
, void *buf
, int cnt
)
1126 cnt
= min(cnt
, (int)host
->part_buf_count
);
1128 memcpy(buf
, (void *)&host
->part_buf
+ host
->part_buf_start
,
1130 host
->part_buf_count
-= cnt
;
1131 host
->part_buf_start
+= cnt
;
1136 /* pull final bytes from the part_buf, assuming it's just been filled */
1137 static void dw_mci_pull_final_bytes(struct dw_mci
*host
, void *buf
, int cnt
)
1139 memcpy(buf
, &host
->part_buf
, cnt
);
1140 host
->part_buf_start
= cnt
;
1141 host
->part_buf_count
= (1 << host
->data_shift
) - cnt
;
1144 static void dw_mci_push_data16(struct dw_mci
*host
, void *buf
, int cnt
)
1146 /* try and push anything in the part_buf */
1147 if (unlikely(host
->part_buf_count
)) {
1148 int len
= dw_mci_push_part_bytes(host
, buf
, cnt
);
1151 if (!sg_next(host
->sg
) || host
->part_buf_count
== 2) {
1152 mci_writew(host
, DATA(host
->data_offset
),
1154 host
->part_buf_count
= 0;
1157 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1158 if (unlikely((unsigned long)buf
& 0x1)) {
1160 u16 aligned_buf
[64];
1161 int len
= min(cnt
& -2, (int)sizeof(aligned_buf
));
1162 int items
= len
>> 1;
1164 /* memcpy from input buffer into aligned buffer */
1165 memcpy(aligned_buf
, buf
, len
);
1168 /* push data from aligned buffer into fifo */
1169 for (i
= 0; i
< items
; ++i
)
1170 mci_writew(host
, DATA(host
->data_offset
),
1177 for (; cnt
>= 2; cnt
-= 2)
1178 mci_writew(host
, DATA(host
->data_offset
), *pdata
++);
1181 /* put anything remaining in the part_buf */
1183 dw_mci_set_part_bytes(host
, buf
, cnt
);
1184 if (!sg_next(host
->sg
))
1185 mci_writew(host
, DATA(host
->data_offset
),
1190 static void dw_mci_pull_data16(struct dw_mci
*host
, void *buf
, int cnt
)
1192 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1193 if (unlikely((unsigned long)buf
& 0x1)) {
1195 /* pull data from fifo into aligned buffer */
1196 u16 aligned_buf
[64];
1197 int len
= min(cnt
& -2, (int)sizeof(aligned_buf
));
1198 int items
= len
>> 1;
1200 for (i
= 0; i
< items
; ++i
)
1201 aligned_buf
[i
] = mci_readw(host
,
1202 DATA(host
->data_offset
));
1203 /* memcpy from aligned buffer into output buffer */
1204 memcpy(buf
, aligned_buf
, len
);
1212 for (; cnt
>= 2; cnt
-= 2)
1213 *pdata
++ = mci_readw(host
, DATA(host
->data_offset
));
1217 host
->part_buf16
= mci_readw(host
, DATA(host
->data_offset
));
1218 dw_mci_pull_final_bytes(host
, buf
, cnt
);
1222 static void dw_mci_push_data32(struct dw_mci
*host
, void *buf
, int cnt
)
1224 /* try and push anything in the part_buf */
1225 if (unlikely(host
->part_buf_count
)) {
1226 int len
= dw_mci_push_part_bytes(host
, buf
, cnt
);
1229 if (!sg_next(host
->sg
) || host
->part_buf_count
== 4) {
1230 mci_writel(host
, DATA(host
->data_offset
),
1232 host
->part_buf_count
= 0;
1235 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1236 if (unlikely((unsigned long)buf
& 0x3)) {
1238 u32 aligned_buf
[32];
1239 int len
= min(cnt
& -4, (int)sizeof(aligned_buf
));
1240 int items
= len
>> 2;
1242 /* memcpy from input buffer into aligned buffer */
1243 memcpy(aligned_buf
, buf
, len
);
1246 /* push data from aligned buffer into fifo */
1247 for (i
= 0; i
< items
; ++i
)
1248 mci_writel(host
, DATA(host
->data_offset
),
1255 for (; cnt
>= 4; cnt
-= 4)
1256 mci_writel(host
, DATA(host
->data_offset
), *pdata
++);
1259 /* put anything remaining in the part_buf */
1261 dw_mci_set_part_bytes(host
, buf
, cnt
);
1262 if (!sg_next(host
->sg
))
1263 mci_writel(host
, DATA(host
->data_offset
),
1268 static void dw_mci_pull_data32(struct dw_mci
*host
, void *buf
, int cnt
)
1270 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1271 if (unlikely((unsigned long)buf
& 0x3)) {
1273 /* pull data from fifo into aligned buffer */
1274 u32 aligned_buf
[32];
1275 int len
= min(cnt
& -4, (int)sizeof(aligned_buf
));
1276 int items
= len
>> 2;
1278 for (i
= 0; i
< items
; ++i
)
1279 aligned_buf
[i
] = mci_readl(host
,
1280 DATA(host
->data_offset
));
1281 /* memcpy from aligned buffer into output buffer */
1282 memcpy(buf
, aligned_buf
, len
);
1290 for (; cnt
>= 4; cnt
-= 4)
1291 *pdata
++ = mci_readl(host
, DATA(host
->data_offset
));
1295 host
->part_buf32
= mci_readl(host
, DATA(host
->data_offset
));
1296 dw_mci_pull_final_bytes(host
, buf
, cnt
);
1300 static void dw_mci_push_data64(struct dw_mci
*host
, void *buf
, int cnt
)
1302 /* try and push anything in the part_buf */
1303 if (unlikely(host
->part_buf_count
)) {
1304 int len
= dw_mci_push_part_bytes(host
, buf
, cnt
);
1307 if (!sg_next(host
->sg
) || host
->part_buf_count
== 8) {
1308 mci_writew(host
, DATA(host
->data_offset
),
1310 host
->part_buf_count
= 0;
1313 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1314 if (unlikely((unsigned long)buf
& 0x7)) {
1316 u64 aligned_buf
[16];
1317 int len
= min(cnt
& -8, (int)sizeof(aligned_buf
));
1318 int items
= len
>> 3;
1320 /* memcpy from input buffer into aligned buffer */
1321 memcpy(aligned_buf
, buf
, len
);
1324 /* push data from aligned buffer into fifo */
1325 for (i
= 0; i
< items
; ++i
)
1326 mci_writeq(host
, DATA(host
->data_offset
),
1333 for (; cnt
>= 8; cnt
-= 8)
1334 mci_writeq(host
, DATA(host
->data_offset
), *pdata
++);
1337 /* put anything remaining in the part_buf */
1339 dw_mci_set_part_bytes(host
, buf
, cnt
);
1340 if (!sg_next(host
->sg
))
1341 mci_writeq(host
, DATA(host
->data_offset
),
1346 static void dw_mci_pull_data64(struct dw_mci
*host
, void *buf
, int cnt
)
1348 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1349 if (unlikely((unsigned long)buf
& 0x7)) {
1351 /* pull data from fifo into aligned buffer */
1352 u64 aligned_buf
[16];
1353 int len
= min(cnt
& -8, (int)sizeof(aligned_buf
));
1354 int items
= len
>> 3;
1356 for (i
= 0; i
< items
; ++i
)
1357 aligned_buf
[i
] = mci_readq(host
,
1358 DATA(host
->data_offset
));
1359 /* memcpy from aligned buffer into output buffer */
1360 memcpy(buf
, aligned_buf
, len
);
1368 for (; cnt
>= 8; cnt
-= 8)
1369 *pdata
++ = mci_readq(host
, DATA(host
->data_offset
));
1373 host
->part_buf
= mci_readq(host
, DATA(host
->data_offset
));
1374 dw_mci_pull_final_bytes(host
, buf
, cnt
);
1378 static void dw_mci_pull_data(struct dw_mci
*host
, void *buf
, int cnt
)
1382 /* get remaining partial bytes */
1383 len
= dw_mci_pull_part_bytes(host
, buf
, cnt
);
1384 if (unlikely(len
== cnt
))
1389 /* get the rest of the data */
1390 host
->pull_data(host
, buf
, cnt
);
1393 static void dw_mci_read_data_pio(struct dw_mci
*host
)
1395 struct sg_mapping_iter
*sg_miter
= &host
->sg_miter
;
1397 unsigned int offset
;
1398 struct mmc_data
*data
= host
->data
;
1399 int shift
= host
->data_shift
;
1401 unsigned int nbytes
= 0, len
;
1402 unsigned int remain
, fcnt
;
1405 if (!sg_miter_next(sg_miter
))
1408 host
->sg
= sg_miter
->__sg
;
1409 buf
= sg_miter
->addr
;
1410 remain
= sg_miter
->length
;
1414 fcnt
= (SDMMC_GET_FCNT(mci_readl(host
, STATUS
))
1415 << shift
) + host
->part_buf_count
;
1416 len
= min(remain
, fcnt
);
1419 dw_mci_pull_data(host
, (void *)(buf
+ offset
), len
);
1424 sg_miter
->consumed
= offset
;
1426 status
= mci_readl(host
, MINTSTS
);
1427 mci_writel(host
, RINTSTS
, SDMMC_INT_RXDR
);
1428 if (status
& DW_MCI_DATA_ERROR_FLAGS
) {
1429 host
->data_status
= status
;
1430 data
->bytes_xfered
+= nbytes
;
1431 sg_miter_stop(sg_miter
);
1435 set_bit(EVENT_DATA_ERROR
, &host
->pending_events
);
1437 tasklet_schedule(&host
->tasklet
);
1440 } while (status
& SDMMC_INT_RXDR
); /*if the RXDR is ready read again*/
1441 data
->bytes_xfered
+= nbytes
;
1444 if (!sg_miter_next(sg_miter
))
1446 sg_miter
->consumed
= 0;
1448 sg_miter_stop(sg_miter
);
1452 data
->bytes_xfered
+= nbytes
;
1453 sg_miter_stop(sg_miter
);
1456 set_bit(EVENT_XFER_COMPLETE
, &host
->pending_events
);
1459 static void dw_mci_write_data_pio(struct dw_mci
*host
)
1461 struct sg_mapping_iter
*sg_miter
= &host
->sg_miter
;
1463 unsigned int offset
;
1464 struct mmc_data
*data
= host
->data
;
1465 int shift
= host
->data_shift
;
1467 unsigned int nbytes
= 0, len
;
1468 unsigned int fifo_depth
= host
->fifo_depth
;
1469 unsigned int remain
, fcnt
;
1472 if (!sg_miter_next(sg_miter
))
1475 host
->sg
= sg_miter
->__sg
;
1476 buf
= sg_miter
->addr
;
1477 remain
= sg_miter
->length
;
1481 fcnt
= ((fifo_depth
-
1482 SDMMC_GET_FCNT(mci_readl(host
, STATUS
)))
1483 << shift
) - host
->part_buf_count
;
1484 len
= min(remain
, fcnt
);
1487 host
->push_data(host
, (void *)(buf
+ offset
), len
);
1492 sg_miter
->consumed
= offset
;
1494 status
= mci_readl(host
, MINTSTS
);
1495 mci_writel(host
, RINTSTS
, SDMMC_INT_TXDR
);
1496 if (status
& DW_MCI_DATA_ERROR_FLAGS
) {
1497 host
->data_status
= status
;
1498 data
->bytes_xfered
+= nbytes
;
1499 sg_miter_stop(sg_miter
);
1504 set_bit(EVENT_DATA_ERROR
, &host
->pending_events
);
1506 tasklet_schedule(&host
->tasklet
);
1509 } while (status
& SDMMC_INT_TXDR
); /* if TXDR write again */
1510 data
->bytes_xfered
+= nbytes
;
1513 if (!sg_miter_next(sg_miter
))
1515 sg_miter
->consumed
= 0;
1517 sg_miter_stop(sg_miter
);
1521 data
->bytes_xfered
+= nbytes
;
1522 sg_miter_stop(sg_miter
);
1525 set_bit(EVENT_XFER_COMPLETE
, &host
->pending_events
);
1528 static void dw_mci_cmd_interrupt(struct dw_mci
*host
, u32 status
)
1530 if (!host
->cmd_status
)
1531 host
->cmd_status
= status
;
1535 set_bit(EVENT_CMD_COMPLETE
, &host
->pending_events
);
1536 tasklet_schedule(&host
->tasklet
);
1539 static irqreturn_t
dw_mci_interrupt(int irq
, void *dev_id
)
1541 struct dw_mci
*host
= dev_id
;
1542 u32 status
, pending
;
1543 unsigned int pass_count
= 0;
1547 status
= mci_readl(host
, RINTSTS
);
1548 pending
= mci_readl(host
, MINTSTS
); /* read-only mask reg */
1551 * DTO fix - version 2.10a and below, and only if internal DMA
1554 if (host
->quirks
& DW_MCI_QUIRK_IDMAC_DTO
) {
1556 ((mci_readl(host
, STATUS
) >> 17) & 0x1fff))
1557 pending
|= SDMMC_INT_DATA_OVER
;
1563 if (pending
& DW_MCI_CMD_ERROR_FLAGS
) {
1564 mci_writel(host
, RINTSTS
, DW_MCI_CMD_ERROR_FLAGS
);
1565 host
->cmd_status
= status
;
1567 set_bit(EVENT_CMD_COMPLETE
, &host
->pending_events
);
1570 if (pending
& DW_MCI_DATA_ERROR_FLAGS
) {
1571 /* if there is an error report DATA_ERROR */
1572 mci_writel(host
, RINTSTS
, DW_MCI_DATA_ERROR_FLAGS
);
1573 host
->data_status
= status
;
1575 set_bit(EVENT_DATA_ERROR
, &host
->pending_events
);
1576 if (!(pending
& (SDMMC_INT_DTO
| SDMMC_INT_DCRC
|
1577 SDMMC_INT_SBE
| SDMMC_INT_EBE
)))
1578 tasklet_schedule(&host
->tasklet
);
1581 if (pending
& SDMMC_INT_DATA_OVER
) {
1582 mci_writel(host
, RINTSTS
, SDMMC_INT_DATA_OVER
);
1583 if (!host
->data_status
)
1584 host
->data_status
= status
;
1586 if (host
->dir_status
== DW_MCI_RECV_STATUS
) {
1587 if (host
->sg
!= NULL
)
1588 dw_mci_read_data_pio(host
);
1590 set_bit(EVENT_DATA_COMPLETE
, &host
->pending_events
);
1591 tasklet_schedule(&host
->tasklet
);
1594 if (pending
& SDMMC_INT_RXDR
) {
1595 mci_writel(host
, RINTSTS
, SDMMC_INT_RXDR
);
1596 if (host
->dir_status
== DW_MCI_RECV_STATUS
&& host
->sg
)
1597 dw_mci_read_data_pio(host
);
1600 if (pending
& SDMMC_INT_TXDR
) {
1601 mci_writel(host
, RINTSTS
, SDMMC_INT_TXDR
);
1602 if (host
->dir_status
== DW_MCI_SEND_STATUS
&& host
->sg
)
1603 dw_mci_write_data_pio(host
);
1606 if (pending
& SDMMC_INT_CMD_DONE
) {
1607 mci_writel(host
, RINTSTS
, SDMMC_INT_CMD_DONE
);
1608 dw_mci_cmd_interrupt(host
, status
);
1611 if (pending
& SDMMC_INT_CD
) {
1612 mci_writel(host
, RINTSTS
, SDMMC_INT_CD
);
1613 queue_work(dw_mci_card_workqueue
, &host
->card_work
);
1616 /* Handle SDIO Interrupts */
1617 for (i
= 0; i
< host
->num_slots
; i
++) {
1618 struct dw_mci_slot
*slot
= host
->slot
[i
];
1619 if (pending
& SDMMC_INT_SDIO(i
)) {
1620 mci_writel(host
, RINTSTS
, SDMMC_INT_SDIO(i
));
1621 mmc_signal_sdio_irq(slot
->mmc
);
1625 } while (pass_count
++ < 5);
1627 #ifdef CONFIG_MMC_DW_IDMAC
1628 /* Handle DMA interrupts */
1629 pending
= mci_readl(host
, IDSTS
);
1630 if (pending
& (SDMMC_IDMAC_INT_TI
| SDMMC_IDMAC_INT_RI
)) {
1631 mci_writel(host
, IDSTS
, SDMMC_IDMAC_INT_TI
| SDMMC_IDMAC_INT_RI
);
1632 mci_writel(host
, IDSTS
, SDMMC_IDMAC_INT_NI
);
1633 set_bit(EVENT_DATA_COMPLETE
, &host
->pending_events
);
1634 host
->dma_ops
->complete(host
);
1641 static void dw_mci_work_routine_card(struct work_struct
*work
)
1643 struct dw_mci
*host
= container_of(work
, struct dw_mci
, card_work
);
1646 for (i
= 0; i
< host
->num_slots
; i
++) {
1647 struct dw_mci_slot
*slot
= host
->slot
[i
];
1648 struct mmc_host
*mmc
= slot
->mmc
;
1649 struct mmc_request
*mrq
;
1653 present
= dw_mci_get_cd(mmc
);
1654 while (present
!= slot
->last_detect_state
) {
1655 dev_dbg(&slot
->mmc
->class_dev
, "card %s\n",
1656 present
? "inserted" : "removed");
1658 /* Power up slot (before spin_lock, may sleep) */
1659 if (present
!= 0 && host
->pdata
->setpower
)
1660 host
->pdata
->setpower(slot
->id
, mmc
->ocr_avail
);
1662 spin_lock_bh(&host
->lock
);
1664 /* Card change detected */
1665 slot
->last_detect_state
= present
;
1667 /* Mark card as present if applicable */
1669 set_bit(DW_MMC_CARD_PRESENT
, &slot
->flags
);
1671 /* Clean up queue if present */
1674 if (mrq
== host
->mrq
) {
1678 switch (host
->state
) {
1681 case STATE_SENDING_CMD
:
1682 mrq
->cmd
->error
= -ENOMEDIUM
;
1686 case STATE_SENDING_DATA
:
1687 mrq
->data
->error
= -ENOMEDIUM
;
1688 dw_mci_stop_dma(host
);
1690 case STATE_DATA_BUSY
:
1691 case STATE_DATA_ERROR
:
1692 if (mrq
->data
->error
== -EINPROGRESS
)
1693 mrq
->data
->error
= -ENOMEDIUM
;
1697 case STATE_SENDING_STOP
:
1698 mrq
->stop
->error
= -ENOMEDIUM
;
1702 dw_mci_request_end(host
, mrq
);
1704 list_del(&slot
->queue_node
);
1705 mrq
->cmd
->error
= -ENOMEDIUM
;
1707 mrq
->data
->error
= -ENOMEDIUM
;
1709 mrq
->stop
->error
= -ENOMEDIUM
;
1711 spin_unlock(&host
->lock
);
1712 mmc_request_done(slot
->mmc
, mrq
);
1713 spin_lock(&host
->lock
);
1717 /* Power down slot */
1719 clear_bit(DW_MMC_CARD_PRESENT
, &slot
->flags
);
1722 * Clear down the FIFO - doing so generates a
1723 * block interrupt, hence setting the
1724 * scatter-gather pointer to NULL.
1726 sg_miter_stop(&host
->sg_miter
);
1729 ctrl
= mci_readl(host
, CTRL
);
1730 ctrl
|= SDMMC_CTRL_FIFO_RESET
;
1731 mci_writel(host
, CTRL
, ctrl
);
1733 #ifdef CONFIG_MMC_DW_IDMAC
1734 ctrl
= mci_readl(host
, BMOD
);
1735 ctrl
|= 0x01; /* Software reset of DMA */
1736 mci_writel(host
, BMOD
, ctrl
);
1741 spin_unlock_bh(&host
->lock
);
1743 /* Power down slot (after spin_unlock, may sleep) */
1744 if (present
== 0 && host
->pdata
->setpower
)
1745 host
->pdata
->setpower(slot
->id
, 0);
1747 present
= dw_mci_get_cd(mmc
);
1750 mmc_detect_change(slot
->mmc
,
1751 msecs_to_jiffies(host
->pdata
->detect_delay_ms
));
1755 static int __init
dw_mci_init_slot(struct dw_mci
*host
, unsigned int id
)
1757 struct mmc_host
*mmc
;
1758 struct dw_mci_slot
*slot
;
1760 mmc
= mmc_alloc_host(sizeof(struct dw_mci_slot
), &host
->dev
);
1764 slot
= mmc_priv(mmc
);
1769 mmc
->ops
= &dw_mci_ops
;
1770 mmc
->f_min
= DIV_ROUND_UP(host
->bus_hz
, 510);
1771 mmc
->f_max
= host
->bus_hz
;
1773 if (host
->pdata
->get_ocr
)
1774 mmc
->ocr_avail
= host
->pdata
->get_ocr(id
);
1776 mmc
->ocr_avail
= MMC_VDD_32_33
| MMC_VDD_33_34
;
1779 * Start with slot power disabled, it will be enabled when a card
1782 if (host
->pdata
->setpower
)
1783 host
->pdata
->setpower(id
, 0);
1785 if (host
->pdata
->caps
)
1786 mmc
->caps
= host
->pdata
->caps
;
1788 if (host
->pdata
->caps2
)
1789 mmc
->caps2
= host
->pdata
->caps2
;
1791 if (host
->pdata
->get_bus_wd
)
1792 if (host
->pdata
->get_bus_wd(slot
->id
) >= 4)
1793 mmc
->caps
|= MMC_CAP_4_BIT_DATA
;
1795 if (host
->pdata
->quirks
& DW_MCI_QUIRK_HIGHSPEED
)
1796 mmc
->caps
|= MMC_CAP_SD_HIGHSPEED
| MMC_CAP_MMC_HIGHSPEED
;
1798 if (mmc
->caps2
& MMC_CAP2_POWEROFF_NOTIFY
)
1799 mmc
->power_notify_type
= MMC_HOST_PW_NOTIFY_SHORT
;
1801 mmc
->power_notify_type
= MMC_HOST_PW_NOTIFY_NONE
;
1803 if (host
->pdata
->blk_settings
) {
1804 mmc
->max_segs
= host
->pdata
->blk_settings
->max_segs
;
1805 mmc
->max_blk_size
= host
->pdata
->blk_settings
->max_blk_size
;
1806 mmc
->max_blk_count
= host
->pdata
->blk_settings
->max_blk_count
;
1807 mmc
->max_req_size
= host
->pdata
->blk_settings
->max_req_size
;
1808 mmc
->max_seg_size
= host
->pdata
->blk_settings
->max_seg_size
;
1810 /* Useful defaults if platform data is unset. */
1811 #ifdef CONFIG_MMC_DW_IDMAC
1812 mmc
->max_segs
= host
->ring_size
;
1813 mmc
->max_blk_size
= 65536;
1814 mmc
->max_blk_count
= host
->ring_size
;
1815 mmc
->max_seg_size
= 0x1000;
1816 mmc
->max_req_size
= mmc
->max_seg_size
* mmc
->max_blk_count
;
1819 mmc
->max_blk_size
= 65536; /* BLKSIZ is 16 bits */
1820 mmc
->max_blk_count
= 512;
1821 mmc
->max_req_size
= mmc
->max_blk_size
* mmc
->max_blk_count
;
1822 mmc
->max_seg_size
= mmc
->max_req_size
;
1823 #endif /* CONFIG_MMC_DW_IDMAC */
1826 host
->vmmc
= regulator_get(mmc_dev(mmc
), "vmmc");
1827 if (IS_ERR(host
->vmmc
)) {
1828 pr_info("%s: no vmmc regulator found\n", mmc_hostname(mmc
));
1831 regulator_enable(host
->vmmc
);
1833 if (dw_mci_get_cd(mmc
))
1834 set_bit(DW_MMC_CARD_PRESENT
, &slot
->flags
);
1836 clear_bit(DW_MMC_CARD_PRESENT
, &slot
->flags
);
1838 host
->slot
[id
] = slot
;
1841 #if defined(CONFIG_DEBUG_FS)
1842 dw_mci_init_debugfs(slot
);
1845 /* Card initially undetected */
1846 slot
->last_detect_state
= 0;
1849 * Card may have been plugged in prior to boot so we
1850 * need to run the detect tasklet
1852 queue_work(dw_mci_card_workqueue
, &host
->card_work
);
1857 static void dw_mci_cleanup_slot(struct dw_mci_slot
*slot
, unsigned int id
)
1859 /* Shutdown detect IRQ */
1860 if (slot
->host
->pdata
->exit
)
1861 slot
->host
->pdata
->exit(id
);
1863 /* Debugfs stuff is cleaned up by mmc core */
1864 mmc_remove_host(slot
->mmc
);
1865 slot
->host
->slot
[id
] = NULL
;
1866 mmc_free_host(slot
->mmc
);
1869 static void dw_mci_init_dma(struct dw_mci
*host
)
1871 /* Alloc memory for sg translation */
1872 host
->sg_cpu
= dma_alloc_coherent(&host
->dev
, PAGE_SIZE
,
1873 &host
->sg_dma
, GFP_KERNEL
);
1874 if (!host
->sg_cpu
) {
1875 dev_err(&host
->dev
, "%s: could not alloc DMA memory\n",
1880 /* Determine which DMA interface to use */
1881 #ifdef CONFIG_MMC_DW_IDMAC
1882 host
->dma_ops
= &dw_mci_idmac_ops
;
1883 dev_info(&host
->dev
, "Using internal DMA controller.\n");
1889 if (host
->dma_ops
->init
) {
1890 if (host
->dma_ops
->init(host
)) {
1891 dev_err(&host
->dev
, "%s: Unable to initialize "
1892 "DMA Controller.\n", __func__
);
1896 dev_err(&host
->dev
, "DMA initialization not found.\n");
1904 dev_info(&host
->dev
, "Using PIO mode.\n");
1909 static bool mci_wait_reset(struct device
*dev
, struct dw_mci
*host
)
1911 unsigned long timeout
= jiffies
+ msecs_to_jiffies(500);
1914 mci_writel(host
, CTRL
, (SDMMC_CTRL_RESET
| SDMMC_CTRL_FIFO_RESET
|
1915 SDMMC_CTRL_DMA_RESET
));
1917 /* wait till resets clear */
1919 ctrl
= mci_readl(host
, CTRL
);
1920 if (!(ctrl
& (SDMMC_CTRL_RESET
| SDMMC_CTRL_FIFO_RESET
|
1921 SDMMC_CTRL_DMA_RESET
)))
1923 } while (time_before(jiffies
, timeout
));
1925 dev_err(dev
, "Timeout resetting block (ctrl %#x)\n", ctrl
);
1930 int dw_mci_probe(struct dw_mci
*host
)
1932 int width
, i
, ret
= 0;
1935 if (!host
->pdata
|| !host
->pdata
->init
) {
1937 "Platform data must supply init function\n");
1941 if (!host
->pdata
->select_slot
&& host
->pdata
->num_slots
> 1) {
1943 "Platform data must supply select_slot function\n");
1947 if (!host
->pdata
->bus_hz
) {
1949 "Platform data must supply bus speed\n");
1953 host
->bus_hz
= host
->pdata
->bus_hz
;
1954 host
->quirks
= host
->pdata
->quirks
;
1956 spin_lock_init(&host
->lock
);
1957 INIT_LIST_HEAD(&host
->queue
);
1960 host
->dma_ops
= host
->pdata
->dma_ops
;
1961 dw_mci_init_dma(host
);
1964 * Get the host data width - this assumes that HCON has been set with
1965 * the correct values.
1967 i
= (mci_readl(host
, HCON
) >> 7) & 0x7;
1969 host
->push_data
= dw_mci_push_data16
;
1970 host
->pull_data
= dw_mci_pull_data16
;
1972 host
->data_shift
= 1;
1973 } else if (i
== 2) {
1974 host
->push_data
= dw_mci_push_data64
;
1975 host
->pull_data
= dw_mci_pull_data64
;
1977 host
->data_shift
= 3;
1979 /* Check for a reserved value, and warn if it is */
1981 "HCON reports a reserved host data width!\n"
1982 "Defaulting to 32-bit access.\n");
1983 host
->push_data
= dw_mci_push_data32
;
1984 host
->pull_data
= dw_mci_pull_data32
;
1986 host
->data_shift
= 2;
1989 /* Reset all blocks */
1990 if (!mci_wait_reset(&host
->dev
, host
)) {
1995 /* Clear the interrupts for the host controller */
1996 mci_writel(host
, RINTSTS
, 0xFFFFFFFF);
1997 mci_writel(host
, INTMASK
, 0); /* disable all mmc interrupt first */
1999 /* Put in max timeout */
2000 mci_writel(host
, TMOUT
, 0xFFFFFFFF);
2003 * FIFO threshold settings RxMark = fifo_size / 2 - 1,
2004 * Tx Mark = fifo_size / 2 DMA Size = 8
2006 if (!host
->pdata
->fifo_depth
) {
2008 * Power-on value of RX_WMark is FIFO_DEPTH-1, but this may
2009 * have been overwritten by the bootloader, just like we're
2010 * about to do, so if you know the value for your hardware, you
2011 * should put it in the platform data.
2013 fifo_size
= mci_readl(host
, FIFOTH
);
2014 fifo_size
= 1 + ((fifo_size
>> 16) & 0xfff);
2016 fifo_size
= host
->pdata
->fifo_depth
;
2018 host
->fifo_depth
= fifo_size
;
2019 host
->fifoth_val
= ((0x2 << 28) | ((fifo_size
/2 - 1) << 16) |
2020 ((fifo_size
/2) << 0));
2021 mci_writel(host
, FIFOTH
, host
->fifoth_val
);
2023 /* disable clock to CIU */
2024 mci_writel(host
, CLKENA
, 0);
2025 mci_writel(host
, CLKSRC
, 0);
2027 tasklet_init(&host
->tasklet
, dw_mci_tasklet_func
, (unsigned long)host
);
2028 dw_mci_card_workqueue
= alloc_workqueue("dw-mci-card",
2029 WQ_MEM_RECLAIM
| WQ_NON_REENTRANT
, 1);
2030 if (!dw_mci_card_workqueue
)
2032 INIT_WORK(&host
->card_work
, dw_mci_work_routine_card
);
2033 ret
= request_irq(host
->irq
, dw_mci_interrupt
, host
->irq_flags
, "dw-mci", host
);
2037 if (host
->pdata
->num_slots
)
2038 host
->num_slots
= host
->pdata
->num_slots
;
2040 host
->num_slots
= ((mci_readl(host
, HCON
) >> 1) & 0x1F) + 1;
2042 /* We need at least one slot to succeed */
2043 for (i
= 0; i
< host
->num_slots
; i
++) {
2044 ret
= dw_mci_init_slot(host
, i
);
2052 * In 2.40a spec, Data offset is changed.
2053 * Need to check the version-id and set data-offset for DATA register.
2055 host
->verid
= SDMMC_GET_VERID(mci_readl(host
, VERID
));
2056 dev_info(&host
->dev
, "Version ID is %04x\n", host
->verid
);
2058 if (host
->verid
< DW_MMC_240A
)
2059 host
->data_offset
= DATA_OFFSET
;
2061 host
->data_offset
= DATA_240A_OFFSET
;
2064 * Enable interrupts for command done, data over, data empty, card det,
2065 * receive ready and error such as transmit, receive timeout, crc error
2067 mci_writel(host
, RINTSTS
, 0xFFFFFFFF);
2068 mci_writel(host
, INTMASK
, SDMMC_INT_CMD_DONE
| SDMMC_INT_DATA_OVER
|
2069 SDMMC_INT_TXDR
| SDMMC_INT_RXDR
|
2070 DW_MCI_ERROR_FLAGS
| SDMMC_INT_CD
);
2071 mci_writel(host
, CTRL
, SDMMC_CTRL_INT_ENABLE
); /* Enable mci interrupt */
2073 dev_info(&host
->dev
, "DW MMC controller at irq %d, "
2074 "%d bit host data width, "
2076 host
->irq
, width
, fifo_size
);
2077 if (host
->quirks
& DW_MCI_QUIRK_IDMAC_DTO
)
2078 dev_info(&host
->dev
, "Internal DMAC interrupt fix enabled.\n");
2083 /* De-init any initialized slots */
2086 dw_mci_cleanup_slot(host
->slot
[i
], i
);
2089 free_irq(host
->irq
, host
);
2092 destroy_workqueue(dw_mci_card_workqueue
);
2095 if (host
->use_dma
&& host
->dma_ops
->exit
)
2096 host
->dma_ops
->exit(host
);
2097 dma_free_coherent(&host
->dev
, PAGE_SIZE
,
2098 host
->sg_cpu
, host
->sg_dma
);
2101 regulator_disable(host
->vmmc
);
2102 regulator_put(host
->vmmc
);
2106 EXPORT_SYMBOL(dw_mci_probe
);
2108 void dw_mci_remove(struct dw_mci
*host
)
2112 mci_writel(host
, RINTSTS
, 0xFFFFFFFF);
2113 mci_writel(host
, INTMASK
, 0); /* disable all mmc interrupt first */
2115 for (i
= 0; i
< host
->num_slots
; i
++) {
2116 dev_dbg(&host
->dev
, "remove slot %d\n", i
);
2118 dw_mci_cleanup_slot(host
->slot
[i
], i
);
2121 /* disable clock to CIU */
2122 mci_writel(host
, CLKENA
, 0);
2123 mci_writel(host
, CLKSRC
, 0);
2125 free_irq(host
->irq
, host
);
2126 destroy_workqueue(dw_mci_card_workqueue
);
2127 dma_free_coherent(&host
->dev
, PAGE_SIZE
, host
->sg_cpu
, host
->sg_dma
);
2129 if (host
->use_dma
&& host
->dma_ops
->exit
)
2130 host
->dma_ops
->exit(host
);
2133 regulator_disable(host
->vmmc
);
2134 regulator_put(host
->vmmc
);
2138 EXPORT_SYMBOL(dw_mci_remove
);
2142 #ifdef CONFIG_PM_SLEEP
2144 * TODO: we should probably disable the clock to the card in the suspend path.
2146 int dw_mci_suspend(struct dw_mci
*host
)
2150 for (i
= 0; i
< host
->num_slots
; i
++) {
2151 struct dw_mci_slot
*slot
= host
->slot
[i
];
2154 ret
= mmc_suspend_host(slot
->mmc
);
2157 slot
= host
->slot
[i
];
2159 mmc_resume_host(host
->slot
[i
]->mmc
);
2166 regulator_disable(host
->vmmc
);
2170 EXPORT_SYMBOL(dw_mci_suspend
);
2172 int dw_mci_resume(struct dw_mci
*host
)
2177 regulator_enable(host
->vmmc
);
2179 if (host
->dma_ops
->init
)
2180 host
->dma_ops
->init(host
);
2182 if (!mci_wait_reset(&host
->dev
, host
)) {
2187 /* Restore the old value at FIFOTH register */
2188 mci_writel(host
, FIFOTH
, host
->fifoth_val
);
2190 mci_writel(host
, RINTSTS
, 0xFFFFFFFF);
2191 mci_writel(host
, INTMASK
, SDMMC_INT_CMD_DONE
| SDMMC_INT_DATA_OVER
|
2192 SDMMC_INT_TXDR
| SDMMC_INT_RXDR
|
2193 DW_MCI_ERROR_FLAGS
| SDMMC_INT_CD
);
2194 mci_writel(host
, CTRL
, SDMMC_CTRL_INT_ENABLE
);
2196 for (i
= 0; i
< host
->num_slots
; i
++) {
2197 struct dw_mci_slot
*slot
= host
->slot
[i
];
2200 ret
= mmc_resume_host(host
->slot
[i
]->mmc
);
2206 EXPORT_SYMBOL(dw_mci_resume
);
2207 #endif /* CONFIG_PM_SLEEP */
2209 static int __init
dw_mci_init(void)
2211 printk(KERN_INFO
"Synopsys Designware Multimedia Card Interface Driver");
2215 static void __exit
dw_mci_exit(void)
2219 module_init(dw_mci_init
);
2220 module_exit(dw_mci_exit
);
2222 MODULE_DESCRIPTION("DW Multimedia Card Interface driver");
2223 MODULE_AUTHOR("NXP Semiconductor VietNam");
2224 MODULE_AUTHOR("Imagination Technologies Ltd");
2225 MODULE_LICENSE("GPL v2");