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
)
271 dev_vdbg(&host
->pdev
->dev
,
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 #ifdef CONFIG_MMC_DW_IDMAC
299 static void dw_mci_dma_cleanup(struct dw_mci
*host
)
301 struct mmc_data
*data
= host
->data
;
304 dma_unmap_sg(&host
->pdev
->dev
, data
->sg
, data
->sg_len
,
305 ((data
->flags
& MMC_DATA_WRITE
)
306 ? DMA_TO_DEVICE
: DMA_FROM_DEVICE
));
309 static void dw_mci_idmac_stop_dma(struct dw_mci
*host
)
313 /* Disable and reset the IDMAC interface */
314 temp
= mci_readl(host
, CTRL
);
315 temp
&= ~SDMMC_CTRL_USE_IDMAC
;
316 temp
|= SDMMC_CTRL_DMA_RESET
;
317 mci_writel(host
, CTRL
, temp
);
319 /* Stop the IDMAC running */
320 temp
= mci_readl(host
, BMOD
);
321 temp
&= ~(SDMMC_IDMAC_ENABLE
| SDMMC_IDMAC_FB
);
322 mci_writel(host
, BMOD
, temp
);
325 static void dw_mci_idmac_complete_dma(struct dw_mci
*host
)
327 struct mmc_data
*data
= host
->data
;
329 dev_vdbg(&host
->pdev
->dev
, "DMA complete\n");
331 host
->dma_ops
->cleanup(host
);
334 * If the card was removed, data will be NULL. No point in trying to
335 * send the stop command or waiting for NBUSY in this case.
338 set_bit(EVENT_XFER_COMPLETE
, &host
->pending_events
);
339 tasklet_schedule(&host
->tasklet
);
343 static void dw_mci_translate_sglist(struct dw_mci
*host
, struct mmc_data
*data
,
347 struct idmac_desc
*desc
= host
->sg_cpu
;
349 for (i
= 0; i
< sg_len
; i
++, desc
++) {
350 unsigned int length
= sg_dma_len(&data
->sg
[i
]);
351 u32 mem_addr
= sg_dma_address(&data
->sg
[i
]);
353 /* Set the OWN bit and disable interrupts for this descriptor */
354 desc
->des0
= IDMAC_DES0_OWN
| IDMAC_DES0_DIC
| IDMAC_DES0_CH
;
357 IDMAC_SET_BUFFER1_SIZE(desc
, length
);
359 /* Physical address to DMA to/from */
360 desc
->des2
= mem_addr
;
363 /* Set first descriptor */
365 desc
->des0
|= IDMAC_DES0_FD
;
367 /* Set last descriptor */
368 desc
= host
->sg_cpu
+ (i
- 1) * sizeof(struct idmac_desc
);
369 desc
->des0
&= ~(IDMAC_DES0_CH
| IDMAC_DES0_DIC
);
370 desc
->des0
|= IDMAC_DES0_LD
;
375 static void dw_mci_idmac_start_dma(struct dw_mci
*host
, unsigned int sg_len
)
379 dw_mci_translate_sglist(host
, host
->data
, sg_len
);
381 /* Select IDMAC interface */
382 temp
= mci_readl(host
, CTRL
);
383 temp
|= SDMMC_CTRL_USE_IDMAC
;
384 mci_writel(host
, CTRL
, temp
);
388 /* Enable the IDMAC */
389 temp
= mci_readl(host
, BMOD
);
390 temp
|= SDMMC_IDMAC_ENABLE
| SDMMC_IDMAC_FB
;
391 mci_writel(host
, BMOD
, temp
);
393 /* Start it running */
394 mci_writel(host
, PLDMND
, 1);
397 static int dw_mci_idmac_init(struct dw_mci
*host
)
399 struct idmac_desc
*p
;
402 /* Number of descriptors in the ring buffer */
403 host
->ring_size
= PAGE_SIZE
/ sizeof(struct idmac_desc
);
405 /* Forward link the descriptor list */
406 for (i
= 0, p
= host
->sg_cpu
; i
< host
->ring_size
- 1; i
++, p
++)
407 p
->des3
= host
->sg_dma
+ (sizeof(struct idmac_desc
) * (i
+ 1));
409 /* Set the last descriptor as the end-of-ring descriptor */
410 p
->des3
= host
->sg_dma
;
411 p
->des0
= IDMAC_DES0_ER
;
413 /* Mask out interrupts - get Tx & Rx complete only */
414 mci_writel(host
, IDINTEN
, SDMMC_IDMAC_INT_NI
| SDMMC_IDMAC_INT_RI
|
417 /* Set the descriptor base address */
418 mci_writel(host
, DBADDR
, host
->sg_dma
);
422 static struct dw_mci_dma_ops dw_mci_idmac_ops
= {
423 .init
= dw_mci_idmac_init
,
424 .start
= dw_mci_idmac_start_dma
,
425 .stop
= dw_mci_idmac_stop_dma
,
426 .complete
= dw_mci_idmac_complete_dma
,
427 .cleanup
= dw_mci_dma_cleanup
,
429 #endif /* CONFIG_MMC_DW_IDMAC */
431 static int dw_mci_submit_data_dma(struct dw_mci
*host
, struct mmc_data
*data
)
433 struct scatterlist
*sg
;
434 unsigned int i
, direction
, sg_len
;
439 /* If we don't have a channel, we can't do DMA */
444 * We don't do DMA on "complex" transfers, i.e. with
445 * non-word-aligned buffers or lengths. Also, we don't bother
446 * with all the DMA setup overhead for short transfers.
448 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)
460 if (data
->flags
& MMC_DATA_READ
)
461 direction
= DMA_FROM_DEVICE
;
463 direction
= DMA_TO_DEVICE
;
465 sg_len
= dma_map_sg(&host
->pdev
->dev
, data
->sg
, data
->sg_len
,
468 dev_vdbg(&host
->pdev
->dev
,
469 "sd sg_cpu: %#lx sg_dma: %#lx sg_len: %d\n",
470 (unsigned long)host
->sg_cpu
, (unsigned long)host
->sg_dma
,
473 /* Enable the DMA interface */
474 temp
= mci_readl(host
, CTRL
);
475 temp
|= SDMMC_CTRL_DMA_ENABLE
;
476 mci_writel(host
, CTRL
, temp
);
478 /* Disable RX/TX IRQs, let DMA handle it */
479 temp
= mci_readl(host
, INTMASK
);
480 temp
&= ~(SDMMC_INT_RXDR
| SDMMC_INT_TXDR
);
481 mci_writel(host
, INTMASK
, temp
);
483 host
->dma_ops
->start(host
, sg_len
);
488 static void dw_mci_submit_data(struct dw_mci
*host
, struct mmc_data
*data
)
492 data
->error
= -EINPROGRESS
;
498 if (data
->flags
& MMC_DATA_READ
)
499 host
->dir_status
= DW_MCI_RECV_STATUS
;
501 host
->dir_status
= DW_MCI_SEND_STATUS
;
503 if (dw_mci_submit_data_dma(host
, data
)) {
504 int flags
= SG_MITER_ATOMIC
;
505 if (host
->data
->flags
& MMC_DATA_READ
)
506 flags
|= SG_MITER_TO_SG
;
508 flags
|= SG_MITER_FROM_SG
;
510 sg_miter_start(&host
->sg_miter
, data
->sg
, data
->sg_len
, flags
);
512 host
->part_buf_start
= 0;
513 host
->part_buf_count
= 0;
515 mci_writel(host
, RINTSTS
, SDMMC_INT_TXDR
| SDMMC_INT_RXDR
);
516 temp
= mci_readl(host
, INTMASK
);
517 temp
|= SDMMC_INT_TXDR
| SDMMC_INT_RXDR
;
518 mci_writel(host
, INTMASK
, temp
);
520 temp
= mci_readl(host
, CTRL
);
521 temp
&= ~SDMMC_CTRL_DMA_ENABLE
;
522 mci_writel(host
, CTRL
, temp
);
526 static void mci_send_cmd(struct dw_mci_slot
*slot
, u32 cmd
, u32 arg
)
528 struct dw_mci
*host
= slot
->host
;
529 unsigned long timeout
= jiffies
+ msecs_to_jiffies(500);
530 unsigned int cmd_status
= 0;
532 mci_writel(host
, CMDARG
, arg
);
534 mci_writel(host
, CMD
, SDMMC_CMD_START
| cmd
);
536 while (time_before(jiffies
, timeout
)) {
537 cmd_status
= mci_readl(host
, CMD
);
538 if (!(cmd_status
& SDMMC_CMD_START
))
541 dev_err(&slot
->mmc
->class_dev
,
542 "Timeout sending command (cmd %#x arg %#x status %#x)\n",
543 cmd
, arg
, cmd_status
);
546 static void dw_mci_setup_bus(struct dw_mci_slot
*slot
)
548 struct dw_mci
*host
= slot
->host
;
551 if (slot
->clock
!= host
->current_speed
) {
552 if (host
->bus_hz
% slot
->clock
)
554 * move the + 1 after the divide to prevent
555 * over-clocking the card.
557 div
= ((host
->bus_hz
/ slot
->clock
) >> 1) + 1;
559 div
= (host
->bus_hz
/ slot
->clock
) >> 1;
561 dev_info(&slot
->mmc
->class_dev
,
562 "Bus speed (slot %d) = %dHz (slot req %dHz, actual %dHZ"
563 " div = %d)\n", slot
->id
, host
->bus_hz
, slot
->clock
,
564 div
? ((host
->bus_hz
/ div
) >> 1) : host
->bus_hz
, div
);
567 mci_writel(host
, CLKENA
, 0);
568 mci_writel(host
, CLKSRC
, 0);
572 SDMMC_CMD_UPD_CLK
| SDMMC_CMD_PRV_DAT_WAIT
, 0);
574 /* set clock to desired speed */
575 mci_writel(host
, CLKDIV
, div
);
579 SDMMC_CMD_UPD_CLK
| SDMMC_CMD_PRV_DAT_WAIT
, 0);
582 mci_writel(host
, CLKENA
, SDMMC_CLKEN_ENABLE
|
583 SDMMC_CLKEN_LOW_PWR
);
587 SDMMC_CMD_UPD_CLK
| SDMMC_CMD_PRV_DAT_WAIT
, 0);
589 host
->current_speed
= slot
->clock
;
592 /* Set the current slot bus width */
593 mci_writel(host
, CTYPE
, (slot
->ctype
<< slot
->id
));
596 static void __dw_mci_start_request(struct dw_mci
*host
,
597 struct dw_mci_slot
*slot
,
598 struct mmc_command
*cmd
)
600 struct mmc_request
*mrq
;
601 struct mmc_data
*data
;
605 if (host
->pdata
->select_slot
)
606 host
->pdata
->select_slot(slot
->id
);
608 /* Slot specific timing and width adjustment */
609 dw_mci_setup_bus(slot
);
611 host
->cur_slot
= slot
;
614 host
->pending_events
= 0;
615 host
->completed_events
= 0;
616 host
->data_status
= 0;
620 dw_mci_set_timeout(host
);
621 mci_writel(host
, BYTCNT
, data
->blksz
*data
->blocks
);
622 mci_writel(host
, BLKSIZ
, data
->blksz
);
625 cmdflags
= dw_mci_prepare_command(slot
->mmc
, cmd
);
627 /* this is the first command, send the initialization clock */
628 if (test_and_clear_bit(DW_MMC_CARD_NEED_INIT
, &slot
->flags
))
629 cmdflags
|= SDMMC_CMD_INIT
;
632 dw_mci_submit_data(host
, data
);
636 dw_mci_start_command(host
, cmd
, cmdflags
);
639 host
->stop_cmdr
= dw_mci_prepare_command(slot
->mmc
, mrq
->stop
);
642 static void dw_mci_start_request(struct dw_mci
*host
,
643 struct dw_mci_slot
*slot
)
645 struct mmc_request
*mrq
= slot
->mrq
;
646 struct mmc_command
*cmd
;
648 cmd
= mrq
->sbc
? mrq
->sbc
: mrq
->cmd
;
649 __dw_mci_start_request(host
, slot
, cmd
);
652 /* must be called with host->lock held */
653 static void dw_mci_queue_request(struct dw_mci
*host
, struct dw_mci_slot
*slot
,
654 struct mmc_request
*mrq
)
656 dev_vdbg(&slot
->mmc
->class_dev
, "queue request: state=%d\n",
661 if (host
->state
== STATE_IDLE
) {
662 host
->state
= STATE_SENDING_CMD
;
663 dw_mci_start_request(host
, slot
);
665 list_add_tail(&slot
->queue_node
, &host
->queue
);
669 static void dw_mci_request(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
671 struct dw_mci_slot
*slot
= mmc_priv(mmc
);
672 struct dw_mci
*host
= slot
->host
;
677 * The check for card presence and queueing of the request must be
678 * atomic, otherwise the card could be removed in between and the
679 * request wouldn't fail until another card was inserted.
681 spin_lock_bh(&host
->lock
);
683 if (!test_bit(DW_MMC_CARD_PRESENT
, &slot
->flags
)) {
684 spin_unlock_bh(&host
->lock
);
685 mrq
->cmd
->error
= -ENOMEDIUM
;
686 mmc_request_done(mmc
, mrq
);
690 dw_mci_queue_request(host
, slot
, mrq
);
692 spin_unlock_bh(&host
->lock
);
695 static void dw_mci_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
697 struct dw_mci_slot
*slot
= mmc_priv(mmc
);
700 /* set default 1 bit mode */
701 slot
->ctype
= SDMMC_CTYPE_1BIT
;
703 switch (ios
->bus_width
) {
704 case MMC_BUS_WIDTH_1
:
705 slot
->ctype
= SDMMC_CTYPE_1BIT
;
707 case MMC_BUS_WIDTH_4
:
708 slot
->ctype
= SDMMC_CTYPE_4BIT
;
710 case MMC_BUS_WIDTH_8
:
711 slot
->ctype
= SDMMC_CTYPE_8BIT
;
715 regs
= mci_readl(slot
->host
, UHS_REG
);
718 if (ios
->timing
== MMC_TIMING_UHS_DDR50
)
719 regs
|= (0x1 << slot
->id
) << 16;
721 regs
&= ~(0x1 << slot
->id
) << 16;
723 mci_writel(slot
->host
, UHS_REG
, regs
);
727 * Use mirror of ios->clock to prevent race with mmc
728 * core ios update when finding the minimum.
730 slot
->clock
= ios
->clock
;
733 switch (ios
->power_mode
) {
735 set_bit(DW_MMC_CARD_NEED_INIT
, &slot
->flags
);
742 static int dw_mci_get_ro(struct mmc_host
*mmc
)
745 struct dw_mci_slot
*slot
= mmc_priv(mmc
);
746 struct dw_mci_board
*brd
= slot
->host
->pdata
;
748 /* Use platform get_ro function, else try on board write protect */
750 read_only
= brd
->get_ro(slot
->id
);
753 mci_readl(slot
->host
, WRTPRT
) & (1 << slot
->id
) ? 1 : 0;
755 dev_dbg(&mmc
->class_dev
, "card is %s\n",
756 read_only
? "read-only" : "read-write");
761 static int dw_mci_get_cd(struct mmc_host
*mmc
)
764 struct dw_mci_slot
*slot
= mmc_priv(mmc
);
765 struct dw_mci_board
*brd
= slot
->host
->pdata
;
767 /* Use platform get_cd function, else try onboard card detect */
768 if (brd
->quirks
& DW_MCI_QUIRK_BROKEN_CARD_DETECTION
)
770 else if (brd
->get_cd
)
771 present
= !brd
->get_cd(slot
->id
);
773 present
= (mci_readl(slot
->host
, CDETECT
) & (1 << slot
->id
))
777 dev_dbg(&mmc
->class_dev
, "card is present\n");
779 dev_dbg(&mmc
->class_dev
, "card is not present\n");
784 static void dw_mci_enable_sdio_irq(struct mmc_host
*mmc
, int enb
)
786 struct dw_mci_slot
*slot
= mmc_priv(mmc
);
787 struct dw_mci
*host
= slot
->host
;
790 /* Enable/disable Slot Specific SDIO interrupt */
791 int_mask
= mci_readl(host
, INTMASK
);
793 mci_writel(host
, INTMASK
,
794 (int_mask
| (1 << SDMMC_INT_SDIO(slot
->id
))));
796 mci_writel(host
, INTMASK
,
797 (int_mask
& ~(1 << SDMMC_INT_SDIO(slot
->id
))));
801 static const struct mmc_host_ops dw_mci_ops
= {
802 .request
= dw_mci_request
,
803 .set_ios
= dw_mci_set_ios
,
804 .get_ro
= dw_mci_get_ro
,
805 .get_cd
= dw_mci_get_cd
,
806 .enable_sdio_irq
= dw_mci_enable_sdio_irq
,
809 static void dw_mci_request_end(struct dw_mci
*host
, struct mmc_request
*mrq
)
810 __releases(&host
->lock
)
811 __acquires(&host
->lock
)
813 struct dw_mci_slot
*slot
;
814 struct mmc_host
*prev_mmc
= host
->cur_slot
->mmc
;
816 WARN_ON(host
->cmd
|| host
->data
);
818 host
->cur_slot
->mrq
= NULL
;
820 if (!list_empty(&host
->queue
)) {
821 slot
= list_entry(host
->queue
.next
,
822 struct dw_mci_slot
, queue_node
);
823 list_del(&slot
->queue_node
);
824 dev_vdbg(&host
->pdev
->dev
, "list not empty: %s is next\n",
825 mmc_hostname(slot
->mmc
));
826 host
->state
= STATE_SENDING_CMD
;
827 dw_mci_start_request(host
, slot
);
829 dev_vdbg(&host
->pdev
->dev
, "list empty\n");
830 host
->state
= STATE_IDLE
;
833 spin_unlock(&host
->lock
);
834 mmc_request_done(prev_mmc
, mrq
);
835 spin_lock(&host
->lock
);
838 static void dw_mci_command_complete(struct dw_mci
*host
, struct mmc_command
*cmd
)
840 u32 status
= host
->cmd_status
;
842 host
->cmd_status
= 0;
844 /* Read the response from the card (up to 16 bytes) */
845 if (cmd
->flags
& MMC_RSP_PRESENT
) {
846 if (cmd
->flags
& MMC_RSP_136
) {
847 cmd
->resp
[3] = mci_readl(host
, RESP0
);
848 cmd
->resp
[2] = mci_readl(host
, RESP1
);
849 cmd
->resp
[1] = mci_readl(host
, RESP2
);
850 cmd
->resp
[0] = mci_readl(host
, RESP3
);
852 cmd
->resp
[0] = mci_readl(host
, RESP0
);
859 if (status
& SDMMC_INT_RTO
)
860 cmd
->error
= -ETIMEDOUT
;
861 else if ((cmd
->flags
& MMC_RSP_CRC
) && (status
& SDMMC_INT_RCRC
))
862 cmd
->error
= -EILSEQ
;
863 else if (status
& SDMMC_INT_RESP_ERR
)
869 /* newer ip versions need a delay between retries */
870 if (host
->quirks
& DW_MCI_QUIRK_RETRY_DELAY
)
875 dw_mci_stop_dma(host
);
880 static void dw_mci_tasklet_func(unsigned long priv
)
882 struct dw_mci
*host
= (struct dw_mci
*)priv
;
883 struct mmc_data
*data
;
884 struct mmc_command
*cmd
;
885 enum dw_mci_state state
;
886 enum dw_mci_state prev_state
;
889 spin_lock(&host
->lock
);
901 case STATE_SENDING_CMD
:
902 if (!test_and_clear_bit(EVENT_CMD_COMPLETE
,
903 &host
->pending_events
))
908 set_bit(EVENT_CMD_COMPLETE
, &host
->completed_events
);
909 dw_mci_command_complete(host
, cmd
);
910 if (cmd
== host
->mrq
->sbc
&& !cmd
->error
) {
911 prev_state
= state
= STATE_SENDING_CMD
;
912 __dw_mci_start_request(host
, host
->cur_slot
,
917 if (!host
->mrq
->data
|| cmd
->error
) {
918 dw_mci_request_end(host
, host
->mrq
);
922 prev_state
= state
= STATE_SENDING_DATA
;
925 case STATE_SENDING_DATA
:
926 if (test_and_clear_bit(EVENT_DATA_ERROR
,
927 &host
->pending_events
)) {
928 dw_mci_stop_dma(host
);
930 send_stop_cmd(host
, data
);
931 state
= STATE_DATA_ERROR
;
935 if (!test_and_clear_bit(EVENT_XFER_COMPLETE
,
936 &host
->pending_events
))
939 set_bit(EVENT_XFER_COMPLETE
, &host
->completed_events
);
940 prev_state
= state
= STATE_DATA_BUSY
;
943 case STATE_DATA_BUSY
:
944 if (!test_and_clear_bit(EVENT_DATA_COMPLETE
,
945 &host
->pending_events
))
949 set_bit(EVENT_DATA_COMPLETE
, &host
->completed_events
);
950 status
= host
->data_status
;
952 if (status
& DW_MCI_DATA_ERROR_FLAGS
) {
953 if (status
& SDMMC_INT_DTO
) {
954 data
->error
= -ETIMEDOUT
;
955 } else if (status
& SDMMC_INT_DCRC
) {
956 data
->error
= -EILSEQ
;
957 } else if (status
& SDMMC_INT_EBE
&&
959 DW_MCI_SEND_STATUS
) {
961 * No data CRC status was returned.
962 * The number of bytes transferred will
963 * be exaggerated in PIO mode.
965 data
->bytes_xfered
= 0;
966 data
->error
= -ETIMEDOUT
;
968 dev_err(&host
->pdev
->dev
,
975 * After an error, there may be data lingering
976 * in the FIFO, so reset it - doing so
977 * generates a block interrupt, hence setting
978 * the scatter-gather pointer to NULL.
980 sg_miter_stop(&host
->sg_miter
);
982 ctrl
= mci_readl(host
, CTRL
);
983 ctrl
|= SDMMC_CTRL_FIFO_RESET
;
984 mci_writel(host
, CTRL
, ctrl
);
986 data
->bytes_xfered
= data
->blocks
* data
->blksz
;
991 dw_mci_request_end(host
, host
->mrq
);
995 if (host
->mrq
->sbc
&& !data
->error
) {
996 data
->stop
->error
= 0;
997 dw_mci_request_end(host
, host
->mrq
);
1001 prev_state
= state
= STATE_SENDING_STOP
;
1003 send_stop_cmd(host
, data
);
1006 case STATE_SENDING_STOP
:
1007 if (!test_and_clear_bit(EVENT_CMD_COMPLETE
,
1008 &host
->pending_events
))
1012 dw_mci_command_complete(host
, host
->mrq
->stop
);
1013 dw_mci_request_end(host
, host
->mrq
);
1016 case STATE_DATA_ERROR
:
1017 if (!test_and_clear_bit(EVENT_XFER_COMPLETE
,
1018 &host
->pending_events
))
1021 state
= STATE_DATA_BUSY
;
1024 } while (state
!= prev_state
);
1026 host
->state
= state
;
1028 spin_unlock(&host
->lock
);
1032 /* push final bytes to part_buf, only use during push */
1033 static void dw_mci_set_part_bytes(struct dw_mci
*host
, void *buf
, int cnt
)
1035 memcpy((void *)&host
->part_buf
, buf
, cnt
);
1036 host
->part_buf_count
= cnt
;
1039 /* append bytes to part_buf, only use during push */
1040 static int dw_mci_push_part_bytes(struct dw_mci
*host
, void *buf
, int cnt
)
1042 cnt
= min(cnt
, (1 << host
->data_shift
) - host
->part_buf_count
);
1043 memcpy((void *)&host
->part_buf
+ host
->part_buf_count
, buf
, cnt
);
1044 host
->part_buf_count
+= cnt
;
1048 /* pull first bytes from part_buf, only use during pull */
1049 static int dw_mci_pull_part_bytes(struct dw_mci
*host
, void *buf
, int cnt
)
1051 cnt
= min(cnt
, (int)host
->part_buf_count
);
1053 memcpy(buf
, (void *)&host
->part_buf
+ host
->part_buf_start
,
1055 host
->part_buf_count
-= cnt
;
1056 host
->part_buf_start
+= cnt
;
1061 /* pull final bytes from the part_buf, assuming it's just been filled */
1062 static void dw_mci_pull_final_bytes(struct dw_mci
*host
, void *buf
, int cnt
)
1064 memcpy(buf
, &host
->part_buf
, cnt
);
1065 host
->part_buf_start
= cnt
;
1066 host
->part_buf_count
= (1 << host
->data_shift
) - cnt
;
1069 static void dw_mci_push_data16(struct dw_mci
*host
, void *buf
, int cnt
)
1071 /* try and push anything in the part_buf */
1072 if (unlikely(host
->part_buf_count
)) {
1073 int len
= dw_mci_push_part_bytes(host
, buf
, cnt
);
1076 if (!sg_next(host
->sg
) || host
->part_buf_count
== 2) {
1077 mci_writew(host
, DATA(host
->data_offset
),
1079 host
->part_buf_count
= 0;
1082 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1083 if (unlikely((unsigned long)buf
& 0x1)) {
1085 u16 aligned_buf
[64];
1086 int len
= min(cnt
& -2, (int)sizeof(aligned_buf
));
1087 int items
= len
>> 1;
1089 /* memcpy from input buffer into aligned buffer */
1090 memcpy(aligned_buf
, buf
, len
);
1093 /* push data from aligned buffer into fifo */
1094 for (i
= 0; i
< items
; ++i
)
1095 mci_writew(host
, DATA(host
->data_offset
),
1102 for (; cnt
>= 2; cnt
-= 2)
1103 mci_writew(host
, DATA(host
->data_offset
), *pdata
++);
1106 /* put anything remaining in the part_buf */
1108 dw_mci_set_part_bytes(host
, buf
, cnt
);
1109 if (!sg_next(host
->sg
))
1110 mci_writew(host
, DATA(host
->data_offset
),
1115 static void dw_mci_pull_data16(struct dw_mci
*host
, void *buf
, int cnt
)
1117 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1118 if (unlikely((unsigned long)buf
& 0x1)) {
1120 /* pull data from fifo into aligned buffer */
1121 u16 aligned_buf
[64];
1122 int len
= min(cnt
& -2, (int)sizeof(aligned_buf
));
1123 int items
= len
>> 1;
1125 for (i
= 0; i
< items
; ++i
)
1126 aligned_buf
[i
] = mci_readw(host
,
1127 DATA(host
->data_offset
));
1128 /* memcpy from aligned buffer into output buffer */
1129 memcpy(buf
, aligned_buf
, len
);
1137 for (; cnt
>= 2; cnt
-= 2)
1138 *pdata
++ = mci_readw(host
, DATA(host
->data_offset
));
1142 host
->part_buf16
= mci_readw(host
, DATA(host
->data_offset
));
1143 dw_mci_pull_final_bytes(host
, buf
, cnt
);
1147 static void dw_mci_push_data32(struct dw_mci
*host
, void *buf
, int cnt
)
1149 /* try and push anything in the part_buf */
1150 if (unlikely(host
->part_buf_count
)) {
1151 int len
= dw_mci_push_part_bytes(host
, buf
, cnt
);
1154 if (!sg_next(host
->sg
) || host
->part_buf_count
== 4) {
1155 mci_writel(host
, DATA(host
->data_offset
),
1157 host
->part_buf_count
= 0;
1160 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1161 if (unlikely((unsigned long)buf
& 0x3)) {
1163 u32 aligned_buf
[32];
1164 int len
= min(cnt
& -4, (int)sizeof(aligned_buf
));
1165 int items
= len
>> 2;
1167 /* memcpy from input buffer into aligned buffer */
1168 memcpy(aligned_buf
, buf
, len
);
1171 /* push data from aligned buffer into fifo */
1172 for (i
= 0; i
< items
; ++i
)
1173 mci_writel(host
, DATA(host
->data_offset
),
1180 for (; cnt
>= 4; cnt
-= 4)
1181 mci_writel(host
, DATA(host
->data_offset
), *pdata
++);
1184 /* put anything remaining in the part_buf */
1186 dw_mci_set_part_bytes(host
, buf
, cnt
);
1187 if (!sg_next(host
->sg
))
1188 mci_writel(host
, DATA(host
->data_offset
),
1193 static void dw_mci_pull_data32(struct dw_mci
*host
, void *buf
, int cnt
)
1195 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1196 if (unlikely((unsigned long)buf
& 0x3)) {
1198 /* pull data from fifo into aligned buffer */
1199 u32 aligned_buf
[32];
1200 int len
= min(cnt
& -4, (int)sizeof(aligned_buf
));
1201 int items
= len
>> 2;
1203 for (i
= 0; i
< items
; ++i
)
1204 aligned_buf
[i
] = mci_readl(host
,
1205 DATA(host
->data_offset
));
1206 /* memcpy from aligned buffer into output buffer */
1207 memcpy(buf
, aligned_buf
, len
);
1215 for (; cnt
>= 4; cnt
-= 4)
1216 *pdata
++ = mci_readl(host
, DATA(host
->data_offset
));
1220 host
->part_buf32
= mci_readl(host
, DATA(host
->data_offset
));
1221 dw_mci_pull_final_bytes(host
, buf
, cnt
);
1225 static void dw_mci_push_data64(struct dw_mci
*host
, void *buf
, int cnt
)
1227 /* try and push anything in the part_buf */
1228 if (unlikely(host
->part_buf_count
)) {
1229 int len
= dw_mci_push_part_bytes(host
, buf
, cnt
);
1232 if (!sg_next(host
->sg
) || host
->part_buf_count
== 8) {
1233 mci_writew(host
, DATA(host
->data_offset
),
1235 host
->part_buf_count
= 0;
1238 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1239 if (unlikely((unsigned long)buf
& 0x7)) {
1241 u64 aligned_buf
[16];
1242 int len
= min(cnt
& -8, (int)sizeof(aligned_buf
));
1243 int items
= len
>> 3;
1245 /* memcpy from input buffer into aligned buffer */
1246 memcpy(aligned_buf
, buf
, len
);
1249 /* push data from aligned buffer into fifo */
1250 for (i
= 0; i
< items
; ++i
)
1251 mci_writeq(host
, DATA(host
->data_offset
),
1258 for (; cnt
>= 8; cnt
-= 8)
1259 mci_writeq(host
, DATA(host
->data_offset
), *pdata
++);
1262 /* put anything remaining in the part_buf */
1264 dw_mci_set_part_bytes(host
, buf
, cnt
);
1265 if (!sg_next(host
->sg
))
1266 mci_writeq(host
, DATA(host
->data_offset
),
1271 static void dw_mci_pull_data64(struct dw_mci
*host
, void *buf
, int cnt
)
1273 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1274 if (unlikely((unsigned long)buf
& 0x7)) {
1276 /* pull data from fifo into aligned buffer */
1277 u64 aligned_buf
[16];
1278 int len
= min(cnt
& -8, (int)sizeof(aligned_buf
));
1279 int items
= len
>> 3;
1281 for (i
= 0; i
< items
; ++i
)
1282 aligned_buf
[i
] = mci_readq(host
,
1283 DATA(host
->data_offset
));
1284 /* memcpy from aligned buffer into output buffer */
1285 memcpy(buf
, aligned_buf
, len
);
1293 for (; cnt
>= 8; cnt
-= 8)
1294 *pdata
++ = mci_readq(host
, DATA(host
->data_offset
));
1298 host
->part_buf
= mci_readq(host
, DATA(host
->data_offset
));
1299 dw_mci_pull_final_bytes(host
, buf
, cnt
);
1303 static void dw_mci_pull_data(struct dw_mci
*host
, void *buf
, int cnt
)
1307 /* get remaining partial bytes */
1308 len
= dw_mci_pull_part_bytes(host
, buf
, cnt
);
1309 if (unlikely(len
== cnt
))
1314 /* get the rest of the data */
1315 host
->pull_data(host
, buf
, cnt
);
1318 static void dw_mci_read_data_pio(struct dw_mci
*host
)
1320 struct sg_mapping_iter
*sg_miter
= &host
->sg_miter
;
1322 unsigned int offset
;
1323 struct mmc_data
*data
= host
->data
;
1324 int shift
= host
->data_shift
;
1326 unsigned int nbytes
= 0, len
;
1327 unsigned int remain
, fcnt
;
1330 if (!sg_miter_next(sg_miter
))
1333 host
->sg
= sg_miter
->__sg
;
1334 buf
= sg_miter
->addr
;
1335 remain
= sg_miter
->length
;
1339 fcnt
= (SDMMC_GET_FCNT(mci_readl(host
, STATUS
))
1340 << shift
) + host
->part_buf_count
;
1341 len
= min(remain
, fcnt
);
1344 dw_mci_pull_data(host
, (void *)(buf
+ offset
), len
);
1349 sg_miter
->consumed
= offset
;
1351 status
= mci_readl(host
, MINTSTS
);
1352 mci_writel(host
, RINTSTS
, SDMMC_INT_RXDR
);
1353 if (status
& DW_MCI_DATA_ERROR_FLAGS
) {
1354 host
->data_status
= status
;
1355 data
->bytes_xfered
+= nbytes
;
1356 sg_miter_stop(sg_miter
);
1360 set_bit(EVENT_DATA_ERROR
, &host
->pending_events
);
1362 tasklet_schedule(&host
->tasklet
);
1365 } while (status
& SDMMC_INT_RXDR
); /*if the RXDR is ready read again*/
1366 data
->bytes_xfered
+= nbytes
;
1369 if (!sg_miter_next(sg_miter
))
1371 sg_miter
->consumed
= 0;
1373 sg_miter_stop(sg_miter
);
1377 data
->bytes_xfered
+= nbytes
;
1378 sg_miter_stop(sg_miter
);
1381 set_bit(EVENT_XFER_COMPLETE
, &host
->pending_events
);
1384 static void dw_mci_write_data_pio(struct dw_mci
*host
)
1386 struct sg_mapping_iter
*sg_miter
= &host
->sg_miter
;
1388 unsigned int offset
;
1389 struct mmc_data
*data
= host
->data
;
1390 int shift
= host
->data_shift
;
1392 unsigned int nbytes
= 0, len
;
1393 unsigned int fifo_depth
= host
->fifo_depth
;
1394 unsigned int remain
, fcnt
;
1397 if (!sg_miter_next(sg_miter
))
1400 host
->sg
= sg_miter
->__sg
;
1401 buf
= sg_miter
->addr
;
1402 remain
= sg_miter
->length
;
1406 fcnt
= ((fifo_depth
-
1407 SDMMC_GET_FCNT(mci_readl(host
, STATUS
)))
1408 << shift
) - host
->part_buf_count
;
1409 len
= min(remain
, fcnt
);
1412 host
->push_data(host
, (void *)(buf
+ offset
), len
);
1417 sg_miter
->consumed
= offset
;
1419 status
= mci_readl(host
, MINTSTS
);
1420 mci_writel(host
, RINTSTS
, SDMMC_INT_TXDR
);
1421 if (status
& DW_MCI_DATA_ERROR_FLAGS
) {
1422 host
->data_status
= status
;
1423 data
->bytes_xfered
+= nbytes
;
1424 sg_miter_stop(sg_miter
);
1429 set_bit(EVENT_DATA_ERROR
, &host
->pending_events
);
1431 tasklet_schedule(&host
->tasklet
);
1434 } while (status
& SDMMC_INT_TXDR
); /* if TXDR write again */
1435 data
->bytes_xfered
+= nbytes
;
1438 if (!sg_miter_next(sg_miter
))
1440 sg_miter
->consumed
= 0;
1442 sg_miter_stop(sg_miter
);
1446 data
->bytes_xfered
+= nbytes
;
1447 sg_miter_stop(sg_miter
);
1450 set_bit(EVENT_XFER_COMPLETE
, &host
->pending_events
);
1453 static void dw_mci_cmd_interrupt(struct dw_mci
*host
, u32 status
)
1455 if (!host
->cmd_status
)
1456 host
->cmd_status
= status
;
1460 set_bit(EVENT_CMD_COMPLETE
, &host
->pending_events
);
1461 tasklet_schedule(&host
->tasklet
);
1464 static irqreturn_t
dw_mci_interrupt(int irq
, void *dev_id
)
1466 struct dw_mci
*host
= dev_id
;
1467 u32 status
, pending
;
1468 unsigned int pass_count
= 0;
1472 status
= mci_readl(host
, RINTSTS
);
1473 pending
= mci_readl(host
, MINTSTS
); /* read-only mask reg */
1476 * DTO fix - version 2.10a and below, and only if internal DMA
1479 if (host
->quirks
& DW_MCI_QUIRK_IDMAC_DTO
) {
1481 ((mci_readl(host
, STATUS
) >> 17) & 0x1fff))
1482 pending
|= SDMMC_INT_DATA_OVER
;
1488 if (pending
& DW_MCI_CMD_ERROR_FLAGS
) {
1489 mci_writel(host
, RINTSTS
, DW_MCI_CMD_ERROR_FLAGS
);
1490 host
->cmd_status
= status
;
1492 set_bit(EVENT_CMD_COMPLETE
, &host
->pending_events
);
1495 if (pending
& DW_MCI_DATA_ERROR_FLAGS
) {
1496 /* if there is an error report DATA_ERROR */
1497 mci_writel(host
, RINTSTS
, DW_MCI_DATA_ERROR_FLAGS
);
1498 host
->data_status
= status
;
1500 set_bit(EVENT_DATA_ERROR
, &host
->pending_events
);
1501 if (!(pending
& (SDMMC_INT_DTO
| SDMMC_INT_DCRC
|
1502 SDMMC_INT_SBE
| SDMMC_INT_EBE
)))
1503 tasklet_schedule(&host
->tasklet
);
1506 if (pending
& SDMMC_INT_DATA_OVER
) {
1507 mci_writel(host
, RINTSTS
, SDMMC_INT_DATA_OVER
);
1508 if (!host
->data_status
)
1509 host
->data_status
= status
;
1511 if (host
->dir_status
== DW_MCI_RECV_STATUS
) {
1512 if (host
->sg
!= NULL
)
1513 dw_mci_read_data_pio(host
);
1515 set_bit(EVENT_DATA_COMPLETE
, &host
->pending_events
);
1516 tasklet_schedule(&host
->tasklet
);
1519 if (pending
& SDMMC_INT_RXDR
) {
1520 mci_writel(host
, RINTSTS
, SDMMC_INT_RXDR
);
1521 if (host
->dir_status
== DW_MCI_RECV_STATUS
&& host
->sg
)
1522 dw_mci_read_data_pio(host
);
1525 if (pending
& SDMMC_INT_TXDR
) {
1526 mci_writel(host
, RINTSTS
, SDMMC_INT_TXDR
);
1527 if (host
->dir_status
== DW_MCI_SEND_STATUS
&& host
->sg
)
1528 dw_mci_write_data_pio(host
);
1531 if (pending
& SDMMC_INT_CMD_DONE
) {
1532 mci_writel(host
, RINTSTS
, SDMMC_INT_CMD_DONE
);
1533 dw_mci_cmd_interrupt(host
, status
);
1536 if (pending
& SDMMC_INT_CD
) {
1537 mci_writel(host
, RINTSTS
, SDMMC_INT_CD
);
1538 queue_work(dw_mci_card_workqueue
, &host
->card_work
);
1541 /* Handle SDIO Interrupts */
1542 for (i
= 0; i
< host
->num_slots
; i
++) {
1543 struct dw_mci_slot
*slot
= host
->slot
[i
];
1544 if (pending
& SDMMC_INT_SDIO(i
)) {
1545 mci_writel(host
, RINTSTS
, SDMMC_INT_SDIO(i
));
1546 mmc_signal_sdio_irq(slot
->mmc
);
1550 } while (pass_count
++ < 5);
1552 #ifdef CONFIG_MMC_DW_IDMAC
1553 /* Handle DMA interrupts */
1554 pending
= mci_readl(host
, IDSTS
);
1555 if (pending
& (SDMMC_IDMAC_INT_TI
| SDMMC_IDMAC_INT_RI
)) {
1556 mci_writel(host
, IDSTS
, SDMMC_IDMAC_INT_TI
| SDMMC_IDMAC_INT_RI
);
1557 mci_writel(host
, IDSTS
, SDMMC_IDMAC_INT_NI
);
1558 set_bit(EVENT_DATA_COMPLETE
, &host
->pending_events
);
1559 host
->dma_ops
->complete(host
);
1566 static void dw_mci_work_routine_card(struct work_struct
*work
)
1568 struct dw_mci
*host
= container_of(work
, struct dw_mci
, card_work
);
1571 for (i
= 0; i
< host
->num_slots
; i
++) {
1572 struct dw_mci_slot
*slot
= host
->slot
[i
];
1573 struct mmc_host
*mmc
= slot
->mmc
;
1574 struct mmc_request
*mrq
;
1578 present
= dw_mci_get_cd(mmc
);
1579 while (present
!= slot
->last_detect_state
) {
1580 dev_dbg(&slot
->mmc
->class_dev
, "card %s\n",
1581 present
? "inserted" : "removed");
1583 /* Power up slot (before spin_lock, may sleep) */
1584 if (present
!= 0 && host
->pdata
->setpower
)
1585 host
->pdata
->setpower(slot
->id
, mmc
->ocr_avail
);
1587 spin_lock_bh(&host
->lock
);
1589 /* Card change detected */
1590 slot
->last_detect_state
= present
;
1592 /* Mark card as present if applicable */
1594 set_bit(DW_MMC_CARD_PRESENT
, &slot
->flags
);
1596 /* Clean up queue if present */
1599 if (mrq
== host
->mrq
) {
1603 switch (host
->state
) {
1606 case STATE_SENDING_CMD
:
1607 mrq
->cmd
->error
= -ENOMEDIUM
;
1611 case STATE_SENDING_DATA
:
1612 mrq
->data
->error
= -ENOMEDIUM
;
1613 dw_mci_stop_dma(host
);
1615 case STATE_DATA_BUSY
:
1616 case STATE_DATA_ERROR
:
1617 if (mrq
->data
->error
== -EINPROGRESS
)
1618 mrq
->data
->error
= -ENOMEDIUM
;
1622 case STATE_SENDING_STOP
:
1623 mrq
->stop
->error
= -ENOMEDIUM
;
1627 dw_mci_request_end(host
, mrq
);
1629 list_del(&slot
->queue_node
);
1630 mrq
->cmd
->error
= -ENOMEDIUM
;
1632 mrq
->data
->error
= -ENOMEDIUM
;
1634 mrq
->stop
->error
= -ENOMEDIUM
;
1636 spin_unlock(&host
->lock
);
1637 mmc_request_done(slot
->mmc
, mrq
);
1638 spin_lock(&host
->lock
);
1642 /* Power down slot */
1644 clear_bit(DW_MMC_CARD_PRESENT
, &slot
->flags
);
1647 * Clear down the FIFO - doing so generates a
1648 * block interrupt, hence setting the
1649 * scatter-gather pointer to NULL.
1651 sg_miter_stop(&host
->sg_miter
);
1654 ctrl
= mci_readl(host
, CTRL
);
1655 ctrl
|= SDMMC_CTRL_FIFO_RESET
;
1656 mci_writel(host
, CTRL
, ctrl
);
1658 #ifdef CONFIG_MMC_DW_IDMAC
1659 ctrl
= mci_readl(host
, BMOD
);
1660 ctrl
|= 0x01; /* Software reset of DMA */
1661 mci_writel(host
, BMOD
, ctrl
);
1666 spin_unlock_bh(&host
->lock
);
1668 /* Power down slot (after spin_unlock, may sleep) */
1669 if (present
== 0 && host
->pdata
->setpower
)
1670 host
->pdata
->setpower(slot
->id
, 0);
1672 present
= dw_mci_get_cd(mmc
);
1675 mmc_detect_change(slot
->mmc
,
1676 msecs_to_jiffies(host
->pdata
->detect_delay_ms
));
1680 static int __init
dw_mci_init_slot(struct dw_mci
*host
, unsigned int id
)
1682 struct mmc_host
*mmc
;
1683 struct dw_mci_slot
*slot
;
1685 mmc
= mmc_alloc_host(sizeof(struct dw_mci_slot
), &host
->pdev
->dev
);
1689 slot
= mmc_priv(mmc
);
1694 mmc
->ops
= &dw_mci_ops
;
1695 mmc
->f_min
= DIV_ROUND_UP(host
->bus_hz
, 510);
1696 mmc
->f_max
= host
->bus_hz
;
1698 if (host
->pdata
->get_ocr
)
1699 mmc
->ocr_avail
= host
->pdata
->get_ocr(id
);
1701 mmc
->ocr_avail
= MMC_VDD_32_33
| MMC_VDD_33_34
;
1704 * Start with slot power disabled, it will be enabled when a card
1707 if (host
->pdata
->setpower
)
1708 host
->pdata
->setpower(id
, 0);
1710 if (host
->pdata
->caps
)
1711 mmc
->caps
= host
->pdata
->caps
;
1713 if (host
->pdata
->caps2
)
1714 mmc
->caps2
= host
->pdata
->caps2
;
1716 if (host
->pdata
->get_bus_wd
)
1717 if (host
->pdata
->get_bus_wd(slot
->id
) >= 4)
1718 mmc
->caps
|= MMC_CAP_4_BIT_DATA
;
1720 if (host
->pdata
->quirks
& DW_MCI_QUIRK_HIGHSPEED
)
1721 mmc
->caps
|= MMC_CAP_SD_HIGHSPEED
| MMC_CAP_MMC_HIGHSPEED
;
1723 #ifdef CONFIG_MMC_DW_IDMAC
1724 mmc
->max_segs
= host
->ring_size
;
1725 mmc
->max_blk_size
= 65536;
1726 mmc
->max_blk_count
= host
->ring_size
;
1727 mmc
->max_seg_size
= 0x1000;
1728 mmc
->max_req_size
= mmc
->max_seg_size
* mmc
->max_blk_count
;
1730 if (host
->pdata
->blk_settings
) {
1731 mmc
->max_segs
= host
->pdata
->blk_settings
->max_segs
;
1732 mmc
->max_blk_size
= host
->pdata
->blk_settings
->max_blk_size
;
1733 mmc
->max_blk_count
= host
->pdata
->blk_settings
->max_blk_count
;
1734 mmc
->max_req_size
= host
->pdata
->blk_settings
->max_req_size
;
1735 mmc
->max_seg_size
= host
->pdata
->blk_settings
->max_seg_size
;
1737 /* Useful defaults if platform data is unset. */
1739 mmc
->max_blk_size
= 65536; /* BLKSIZ is 16 bits */
1740 mmc
->max_blk_count
= 512;
1741 mmc
->max_req_size
= mmc
->max_blk_size
* mmc
->max_blk_count
;
1742 mmc
->max_seg_size
= mmc
->max_req_size
;
1744 #endif /* CONFIG_MMC_DW_IDMAC */
1746 host
->vmmc
= regulator_get(mmc_dev(mmc
), "vmmc");
1747 if (IS_ERR(host
->vmmc
)) {
1748 pr_info("%s: no vmmc regulator found\n", mmc_hostname(mmc
));
1751 regulator_enable(host
->vmmc
);
1753 if (dw_mci_get_cd(mmc
))
1754 set_bit(DW_MMC_CARD_PRESENT
, &slot
->flags
);
1756 clear_bit(DW_MMC_CARD_PRESENT
, &slot
->flags
);
1758 host
->slot
[id
] = slot
;
1761 #if defined(CONFIG_DEBUG_FS)
1762 dw_mci_init_debugfs(slot
);
1765 /* Card initially undetected */
1766 slot
->last_detect_state
= 0;
1769 * Card may have been plugged in prior to boot so we
1770 * need to run the detect tasklet
1772 queue_work(dw_mci_card_workqueue
, &host
->card_work
);
1777 static void dw_mci_cleanup_slot(struct dw_mci_slot
*slot
, unsigned int id
)
1779 /* Shutdown detect IRQ */
1780 if (slot
->host
->pdata
->exit
)
1781 slot
->host
->pdata
->exit(id
);
1783 /* Debugfs stuff is cleaned up by mmc core */
1784 mmc_remove_host(slot
->mmc
);
1785 slot
->host
->slot
[id
] = NULL
;
1786 mmc_free_host(slot
->mmc
);
1789 static void dw_mci_init_dma(struct dw_mci
*host
)
1791 /* Alloc memory for sg translation */
1792 host
->sg_cpu
= dma_alloc_coherent(&host
->pdev
->dev
, PAGE_SIZE
,
1793 &host
->sg_dma
, GFP_KERNEL
);
1794 if (!host
->sg_cpu
) {
1795 dev_err(&host
->pdev
->dev
, "%s: could not alloc DMA memory\n",
1800 /* Determine which DMA interface to use */
1801 #ifdef CONFIG_MMC_DW_IDMAC
1802 host
->dma_ops
= &dw_mci_idmac_ops
;
1803 dev_info(&host
->pdev
->dev
, "Using internal DMA controller.\n");
1809 if (host
->dma_ops
->init
) {
1810 if (host
->dma_ops
->init(host
)) {
1811 dev_err(&host
->pdev
->dev
, "%s: Unable to initialize "
1812 "DMA Controller.\n", __func__
);
1816 dev_err(&host
->pdev
->dev
, "DMA initialization not found.\n");
1824 dev_info(&host
->pdev
->dev
, "Using PIO mode.\n");
1829 static bool mci_wait_reset(struct device
*dev
, struct dw_mci
*host
)
1831 unsigned long timeout
= jiffies
+ msecs_to_jiffies(500);
1834 mci_writel(host
, CTRL
, (SDMMC_CTRL_RESET
| SDMMC_CTRL_FIFO_RESET
|
1835 SDMMC_CTRL_DMA_RESET
));
1837 /* wait till resets clear */
1839 ctrl
= mci_readl(host
, CTRL
);
1840 if (!(ctrl
& (SDMMC_CTRL_RESET
| SDMMC_CTRL_FIFO_RESET
|
1841 SDMMC_CTRL_DMA_RESET
)))
1843 } while (time_before(jiffies
, timeout
));
1845 dev_err(dev
, "Timeout resetting block (ctrl %#x)\n", ctrl
);
1850 static int dw_mci_probe(struct platform_device
*pdev
)
1852 struct dw_mci
*host
;
1853 struct resource
*regs
;
1854 struct dw_mci_board
*pdata
;
1855 int irq
, ret
, i
, width
;
1858 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1862 irq
= platform_get_irq(pdev
, 0);
1866 host
= kzalloc(sizeof(struct dw_mci
), GFP_KERNEL
);
1871 host
->pdata
= pdata
= pdev
->dev
.platform_data
;
1872 if (!pdata
|| !pdata
->init
) {
1874 "Platform data must supply init function\n");
1879 if (!pdata
->select_slot
&& pdata
->num_slots
> 1) {
1881 "Platform data must supply select_slot function\n");
1886 if (!pdata
->bus_hz
) {
1888 "Platform data must supply bus speed\n");
1893 host
->bus_hz
= pdata
->bus_hz
;
1894 host
->quirks
= pdata
->quirks
;
1896 spin_lock_init(&host
->lock
);
1897 INIT_LIST_HEAD(&host
->queue
);
1900 host
->regs
= ioremap(regs
->start
, resource_size(regs
));
1904 host
->dma_ops
= pdata
->dma_ops
;
1905 dw_mci_init_dma(host
);
1908 * Get the host data width - this assumes that HCON has been set with
1909 * the correct values.
1911 i
= (mci_readl(host
, HCON
) >> 7) & 0x7;
1913 host
->push_data
= dw_mci_push_data16
;
1914 host
->pull_data
= dw_mci_pull_data16
;
1916 host
->data_shift
= 1;
1917 } else if (i
== 2) {
1918 host
->push_data
= dw_mci_push_data64
;
1919 host
->pull_data
= dw_mci_pull_data64
;
1921 host
->data_shift
= 3;
1923 /* Check for a reserved value, and warn if it is */
1925 "HCON reports a reserved host data width!\n"
1926 "Defaulting to 32-bit access.\n");
1927 host
->push_data
= dw_mci_push_data32
;
1928 host
->pull_data
= dw_mci_pull_data32
;
1930 host
->data_shift
= 2;
1933 /* Reset all blocks */
1934 if (!mci_wait_reset(&pdev
->dev
, host
)) {
1939 /* Clear the interrupts for the host controller */
1940 mci_writel(host
, RINTSTS
, 0xFFFFFFFF);
1941 mci_writel(host
, INTMASK
, 0); /* disable all mmc interrupt first */
1943 /* Put in max timeout */
1944 mci_writel(host
, TMOUT
, 0xFFFFFFFF);
1947 * FIFO threshold settings RxMark = fifo_size / 2 - 1,
1948 * Tx Mark = fifo_size / 2 DMA Size = 8
1950 if (!host
->pdata
->fifo_depth
) {
1952 * Power-on value of RX_WMark is FIFO_DEPTH-1, but this may
1953 * have been overwritten by the bootloader, just like we're
1954 * about to do, so if you know the value for your hardware, you
1955 * should put it in the platform data.
1957 fifo_size
= mci_readl(host
, FIFOTH
);
1958 fifo_size
= 1 + ((fifo_size
>> 16) & 0xfff);
1960 fifo_size
= host
->pdata
->fifo_depth
;
1962 host
->fifo_depth
= fifo_size
;
1963 host
->fifoth_val
= ((0x2 << 28) | ((fifo_size
/2 - 1) << 16) |
1964 ((fifo_size
/2) << 0));
1965 mci_writel(host
, FIFOTH
, host
->fifoth_val
);
1967 /* disable clock to CIU */
1968 mci_writel(host
, CLKENA
, 0);
1969 mci_writel(host
, CLKSRC
, 0);
1971 tasklet_init(&host
->tasklet
, dw_mci_tasklet_func
, (unsigned long)host
);
1972 dw_mci_card_workqueue
= alloc_workqueue("dw-mci-card",
1973 WQ_MEM_RECLAIM
| WQ_NON_REENTRANT
, 1);
1974 if (!dw_mci_card_workqueue
)
1976 INIT_WORK(&host
->card_work
, dw_mci_work_routine_card
);
1978 ret
= request_irq(irq
, dw_mci_interrupt
, 0, "dw-mci", host
);
1982 platform_set_drvdata(pdev
, host
);
1984 if (host
->pdata
->num_slots
)
1985 host
->num_slots
= host
->pdata
->num_slots
;
1987 host
->num_slots
= ((mci_readl(host
, HCON
) >> 1) & 0x1F) + 1;
1989 /* We need at least one slot to succeed */
1990 for (i
= 0; i
< host
->num_slots
; i
++) {
1991 ret
= dw_mci_init_slot(host
, i
);
1999 * In 2.40a spec, Data offset is changed.
2000 * Need to check the version-id and set data-offset for DATA register.
2002 host
->verid
= SDMMC_GET_VERID(mci_readl(host
, VERID
));
2003 dev_info(&pdev
->dev
, "Version ID is %04x\n", host
->verid
);
2005 if (host
->verid
< DW_MMC_240A
)
2006 host
->data_offset
= DATA_OFFSET
;
2008 host
->data_offset
= DATA_240A_OFFSET
;
2011 * Enable interrupts for command done, data over, data empty, card det,
2012 * receive ready and error such as transmit, receive timeout, crc error
2014 mci_writel(host
, RINTSTS
, 0xFFFFFFFF);
2015 mci_writel(host
, INTMASK
, SDMMC_INT_CMD_DONE
| SDMMC_INT_DATA_OVER
|
2016 SDMMC_INT_TXDR
| SDMMC_INT_RXDR
|
2017 DW_MCI_ERROR_FLAGS
| SDMMC_INT_CD
);
2018 mci_writel(host
, CTRL
, SDMMC_CTRL_INT_ENABLE
); /* Enable mci interrupt */
2020 dev_info(&pdev
->dev
, "DW MMC controller at irq %d, "
2021 "%d bit host data width, "
2023 irq
, width
, fifo_size
);
2024 if (host
->quirks
& DW_MCI_QUIRK_IDMAC_DTO
)
2025 dev_info(&pdev
->dev
, "Internal DMAC interrupt fix enabled.\n");
2030 /* De-init any initialized slots */
2033 dw_mci_cleanup_slot(host
->slot
[i
], i
);
2036 free_irq(irq
, host
);
2039 destroy_workqueue(dw_mci_card_workqueue
);
2042 if (host
->use_dma
&& host
->dma_ops
->exit
)
2043 host
->dma_ops
->exit(host
);
2044 dma_free_coherent(&host
->pdev
->dev
, PAGE_SIZE
,
2045 host
->sg_cpu
, host
->sg_dma
);
2046 iounmap(host
->regs
);
2049 regulator_disable(host
->vmmc
);
2050 regulator_put(host
->vmmc
);
2059 static int __exit
dw_mci_remove(struct platform_device
*pdev
)
2061 struct dw_mci
*host
= platform_get_drvdata(pdev
);
2064 mci_writel(host
, RINTSTS
, 0xFFFFFFFF);
2065 mci_writel(host
, INTMASK
, 0); /* disable all mmc interrupt first */
2067 platform_set_drvdata(pdev
, NULL
);
2069 for (i
= 0; i
< host
->num_slots
; i
++) {
2070 dev_dbg(&pdev
->dev
, "remove slot %d\n", i
);
2072 dw_mci_cleanup_slot(host
->slot
[i
], i
);
2075 /* disable clock to CIU */
2076 mci_writel(host
, CLKENA
, 0);
2077 mci_writel(host
, CLKSRC
, 0);
2079 free_irq(platform_get_irq(pdev
, 0), host
);
2080 destroy_workqueue(dw_mci_card_workqueue
);
2081 dma_free_coherent(&pdev
->dev
, PAGE_SIZE
, host
->sg_cpu
, host
->sg_dma
);
2083 if (host
->use_dma
&& host
->dma_ops
->exit
)
2084 host
->dma_ops
->exit(host
);
2087 regulator_disable(host
->vmmc
);
2088 regulator_put(host
->vmmc
);
2091 iounmap(host
->regs
);
2097 #ifdef CONFIG_PM_SLEEP
2099 * TODO: we should probably disable the clock to the card in the suspend path.
2101 static int dw_mci_suspend(struct device
*dev
)
2104 struct dw_mci
*host
= dev_get_drvdata(dev
);
2106 for (i
= 0; i
< host
->num_slots
; i
++) {
2107 struct dw_mci_slot
*slot
= host
->slot
[i
];
2110 ret
= mmc_suspend_host(slot
->mmc
);
2113 slot
= host
->slot
[i
];
2115 mmc_resume_host(host
->slot
[i
]->mmc
);
2122 regulator_disable(host
->vmmc
);
2127 static int dw_mci_resume(struct device
*dev
)
2130 struct dw_mci
*host
= dev_get_drvdata(dev
);
2133 regulator_enable(host
->vmmc
);
2135 if (host
->dma_ops
->init
)
2136 host
->dma_ops
->init(host
);
2138 if (!mci_wait_reset(dev
, host
)) {
2143 /* Restore the old value at FIFOTH register */
2144 mci_writel(host
, FIFOTH
, host
->fifoth_val
);
2146 mci_writel(host
, RINTSTS
, 0xFFFFFFFF);
2147 mci_writel(host
, INTMASK
, SDMMC_INT_CMD_DONE
| SDMMC_INT_DATA_OVER
|
2148 SDMMC_INT_TXDR
| SDMMC_INT_RXDR
|
2149 DW_MCI_ERROR_FLAGS
| SDMMC_INT_CD
);
2150 mci_writel(host
, CTRL
, SDMMC_CTRL_INT_ENABLE
);
2152 for (i
= 0; i
< host
->num_slots
; i
++) {
2153 struct dw_mci_slot
*slot
= host
->slot
[i
];
2156 ret
= mmc_resume_host(host
->slot
[i
]->mmc
);
2164 #define dw_mci_suspend NULL
2165 #define dw_mci_resume NULL
2166 #endif /* CONFIG_PM_SLEEP */
2168 static SIMPLE_DEV_PM_OPS(dw_mci_pmops
, dw_mci_suspend
, dw_mci_resume
);
2170 static struct platform_driver dw_mci_driver
= {
2171 .remove
= __exit_p(dw_mci_remove
),
2174 .pm
= &dw_mci_pmops
,
2178 static int __init
dw_mci_init(void)
2180 return platform_driver_probe(&dw_mci_driver
, dw_mci_probe
);
2183 static void __exit
dw_mci_exit(void)
2185 platform_driver_unregister(&dw_mci_driver
);
2188 module_init(dw_mci_init
);
2189 module_exit(dw_mci_exit
);
2191 MODULE_DESCRIPTION("DW Multimedia Card Interface driver");
2192 MODULE_AUTHOR("NXP Semiconductor VietNam");
2193 MODULE_AUTHOR("Imagination Technologies Ltd");
2194 MODULE_LICENSE("GPL v2");