2 * Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de>
3 * Copyright (C) 2013, Imagination Technologies
5 * JZ4740 SD/MMC controller driver
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
12 * You should have received a copy of the GNU General Public License along
13 * with this program; if not, write to the Free Software Foundation, Inc.,
14 * 675 Mass Ave, Cambridge, MA 02139, USA.
18 #include <linux/bitops.h>
19 #include <linux/clk.h>
20 #include <linux/delay.h>
21 #include <linux/dmaengine.h>
22 #include <linux/dma-mapping.h>
23 #include <linux/err.h>
24 #include <linux/gpio.h>
25 #include <linux/interrupt.h>
27 #include <linux/irq.h>
28 #include <linux/mmc/host.h>
29 #include <linux/mmc/slot-gpio.h>
30 #include <linux/module.h>
31 #include <linux/of_device.h>
32 #include <linux/pinctrl/consumer.h>
33 #include <linux/platform_device.h>
34 #include <linux/scatterlist.h>
36 #include <asm/cacheflush.h>
38 #include <asm/mach-jz4740/dma.h>
39 #include <asm/mach-jz4740/jz4740_mmc.h>
41 #define JZ_REG_MMC_STRPCL 0x00
42 #define JZ_REG_MMC_STATUS 0x04
43 #define JZ_REG_MMC_CLKRT 0x08
44 #define JZ_REG_MMC_CMDAT 0x0C
45 #define JZ_REG_MMC_RESTO 0x10
46 #define JZ_REG_MMC_RDTO 0x14
47 #define JZ_REG_MMC_BLKLEN 0x18
48 #define JZ_REG_MMC_NOB 0x1C
49 #define JZ_REG_MMC_SNOB 0x20
50 #define JZ_REG_MMC_IMASK 0x24
51 #define JZ_REG_MMC_IREG 0x28
52 #define JZ_REG_MMC_CMD 0x2C
53 #define JZ_REG_MMC_ARG 0x30
54 #define JZ_REG_MMC_RESP_FIFO 0x34
55 #define JZ_REG_MMC_RXFIFO 0x38
56 #define JZ_REG_MMC_TXFIFO 0x3C
57 #define JZ_REG_MMC_DMAC 0x44
59 #define JZ_MMC_STRPCL_EXIT_MULTIPLE BIT(7)
60 #define JZ_MMC_STRPCL_EXIT_TRANSFER BIT(6)
61 #define JZ_MMC_STRPCL_START_READWAIT BIT(5)
62 #define JZ_MMC_STRPCL_STOP_READWAIT BIT(4)
63 #define JZ_MMC_STRPCL_RESET BIT(3)
64 #define JZ_MMC_STRPCL_START_OP BIT(2)
65 #define JZ_MMC_STRPCL_CLOCK_CONTROL (BIT(1) | BIT(0))
66 #define JZ_MMC_STRPCL_CLOCK_STOP BIT(0)
67 #define JZ_MMC_STRPCL_CLOCK_START BIT(1)
70 #define JZ_MMC_STATUS_IS_RESETTING BIT(15)
71 #define JZ_MMC_STATUS_SDIO_INT_ACTIVE BIT(14)
72 #define JZ_MMC_STATUS_PRG_DONE BIT(13)
73 #define JZ_MMC_STATUS_DATA_TRAN_DONE BIT(12)
74 #define JZ_MMC_STATUS_END_CMD_RES BIT(11)
75 #define JZ_MMC_STATUS_DATA_FIFO_AFULL BIT(10)
76 #define JZ_MMC_STATUS_IS_READWAIT BIT(9)
77 #define JZ_MMC_STATUS_CLK_EN BIT(8)
78 #define JZ_MMC_STATUS_DATA_FIFO_FULL BIT(7)
79 #define JZ_MMC_STATUS_DATA_FIFO_EMPTY BIT(6)
80 #define JZ_MMC_STATUS_CRC_RES_ERR BIT(5)
81 #define JZ_MMC_STATUS_CRC_READ_ERROR BIT(4)
82 #define JZ_MMC_STATUS_TIMEOUT_WRITE BIT(3)
83 #define JZ_MMC_STATUS_CRC_WRITE_ERROR BIT(2)
84 #define JZ_MMC_STATUS_TIMEOUT_RES BIT(1)
85 #define JZ_MMC_STATUS_TIMEOUT_READ BIT(0)
87 #define JZ_MMC_STATUS_READ_ERROR_MASK (BIT(4) | BIT(0))
88 #define JZ_MMC_STATUS_WRITE_ERROR_MASK (BIT(3) | BIT(2))
91 #define JZ_MMC_CMDAT_IO_ABORT BIT(11)
92 #define JZ_MMC_CMDAT_BUS_WIDTH_4BIT BIT(10)
93 #define JZ_MMC_CMDAT_DMA_EN BIT(8)
94 #define JZ_MMC_CMDAT_INIT BIT(7)
95 #define JZ_MMC_CMDAT_BUSY BIT(6)
96 #define JZ_MMC_CMDAT_STREAM BIT(5)
97 #define JZ_MMC_CMDAT_WRITE BIT(4)
98 #define JZ_MMC_CMDAT_DATA_EN BIT(3)
99 #define JZ_MMC_CMDAT_RESPONSE_FORMAT (BIT(2) | BIT(1) | BIT(0))
100 #define JZ_MMC_CMDAT_RSP_R1 1
101 #define JZ_MMC_CMDAT_RSP_R2 2
102 #define JZ_MMC_CMDAT_RSP_R3 3
104 #define JZ_MMC_IRQ_SDIO BIT(7)
105 #define JZ_MMC_IRQ_TXFIFO_WR_REQ BIT(6)
106 #define JZ_MMC_IRQ_RXFIFO_RD_REQ BIT(5)
107 #define JZ_MMC_IRQ_END_CMD_RES BIT(2)
108 #define JZ_MMC_IRQ_PRG_DONE BIT(1)
109 #define JZ_MMC_IRQ_DATA_TRAN_DONE BIT(0)
111 #define JZ_MMC_DMAC_DMA_SEL BIT(1)
112 #define JZ_MMC_DMAC_DMA_EN BIT(0)
114 #define JZ_MMC_CLK_RATE 24000000
116 enum jz4740_mmc_version
{
122 enum jz4740_mmc_state
{
123 JZ4740_MMC_STATE_READ_RESPONSE
,
124 JZ4740_MMC_STATE_TRANSFER_DATA
,
125 JZ4740_MMC_STATE_SEND_STOP
,
126 JZ4740_MMC_STATE_DONE
,
129 struct jz4740_mmc_host_next
{
134 struct jz4740_mmc_host
{
135 struct mmc_host
*mmc
;
136 struct platform_device
*pdev
;
137 struct jz4740_mmc_platform_data
*pdata
;
140 enum jz4740_mmc_version version
;
146 struct resource
*mem_res
;
147 struct mmc_request
*req
;
148 struct mmc_command
*cmd
;
150 unsigned long waiting
;
158 struct timer_list timeout_timer
;
159 struct sg_mapping_iter miter
;
160 enum jz4740_mmc_state state
;
163 struct dma_chan
*dma_rx
;
164 struct dma_chan
*dma_tx
;
165 struct jz4740_mmc_host_next next_data
;
169 /* The DMA trigger level is 8 words, that is to say, the DMA read
170 * trigger is when data words in MSC_RXFIFO is >= 8 and the DMA write
171 * trigger is when data words in MSC_TXFIFO is < 8.
173 #define JZ4740_MMC_FIFO_HALF_SIZE 8
176 static void jz4740_mmc_write_irq_mask(struct jz4740_mmc_host
*host
,
179 if (host
->version
>= JZ_MMC_JZ4750
)
180 return writel(val
, host
->base
+ JZ_REG_MMC_IMASK
);
182 return writew(val
, host
->base
+ JZ_REG_MMC_IMASK
);
185 static void jz4740_mmc_write_irq_reg(struct jz4740_mmc_host
*host
,
188 if (host
->version
>= JZ_MMC_JZ4780
)
189 return writel(val
, host
->base
+ JZ_REG_MMC_IREG
);
191 return writew(val
, host
->base
+ JZ_REG_MMC_IREG
);
194 static uint32_t jz4740_mmc_read_irq_reg(struct jz4740_mmc_host
*host
)
196 if (host
->version
>= JZ_MMC_JZ4780
)
197 return readl(host
->base
+ JZ_REG_MMC_IREG
);
199 return readw(host
->base
+ JZ_REG_MMC_IREG
);
202 /*----------------------------------------------------------------------------*/
203 /* DMA infrastructure */
205 static void jz4740_mmc_release_dma_channels(struct jz4740_mmc_host
*host
)
210 dma_release_channel(host
->dma_tx
);
211 dma_release_channel(host
->dma_rx
);
214 static int jz4740_mmc_acquire_dma_channels(struct jz4740_mmc_host
*host
)
216 host
->dma_tx
= dma_request_chan(mmc_dev(host
->mmc
), "tx");
217 if (IS_ERR(host
->dma_tx
)) {
218 dev_err(mmc_dev(host
->mmc
), "Failed to get dma_tx channel\n");
219 return PTR_ERR(host
->dma_tx
);
222 host
->dma_rx
= dma_request_chan(mmc_dev(host
->mmc
), "rx");
223 if (IS_ERR(host
->dma_rx
)) {
224 dev_err(mmc_dev(host
->mmc
), "Failed to get dma_rx channel\n");
225 dma_release_channel(host
->dma_tx
);
226 return PTR_ERR(host
->dma_rx
);
229 /* Initialize DMA pre request cookie */
230 host
->next_data
.cookie
= 1;
235 static inline struct dma_chan
*jz4740_mmc_get_dma_chan(struct jz4740_mmc_host
*host
,
236 struct mmc_data
*data
)
238 return (data
->flags
& MMC_DATA_READ
) ? host
->dma_rx
: host
->dma_tx
;
241 static void jz4740_mmc_dma_unmap(struct jz4740_mmc_host
*host
,
242 struct mmc_data
*data
)
244 struct dma_chan
*chan
= jz4740_mmc_get_dma_chan(host
, data
);
245 enum dma_data_direction dir
= mmc_get_dma_dir(data
);
247 dma_unmap_sg(chan
->device
->dev
, data
->sg
, data
->sg_len
, dir
);
250 /* Prepares DMA data for current/next transfer, returns non-zero on failure */
251 static int jz4740_mmc_prepare_dma_data(struct jz4740_mmc_host
*host
,
252 struct mmc_data
*data
,
253 struct jz4740_mmc_host_next
*next
,
254 struct dma_chan
*chan
)
256 struct jz4740_mmc_host_next
*next_data
= &host
->next_data
;
257 enum dma_data_direction dir
= mmc_get_dma_dir(data
);
260 if (!next
&& data
->host_cookie
&&
261 data
->host_cookie
!= host
->next_data
.cookie
) {
262 dev_warn(mmc_dev(host
->mmc
),
263 "[%s] invalid cookie: data->host_cookie %d host->next_data.cookie %d\n",
266 host
->next_data
.cookie
);
267 data
->host_cookie
= 0;
270 /* Check if next job is already prepared */
271 if (next
|| data
->host_cookie
!= host
->next_data
.cookie
) {
272 sg_len
= dma_map_sg(chan
->device
->dev
,
278 sg_len
= next_data
->sg_len
;
279 next_data
->sg_len
= 0;
283 dev_err(mmc_dev(host
->mmc
),
284 "Failed to map scatterlist for DMA operation\n");
289 next
->sg_len
= sg_len
;
290 data
->host_cookie
= ++next
->cookie
< 0 ? 1 : next
->cookie
;
292 host
->sg_len
= sg_len
;
297 static int jz4740_mmc_start_dma_transfer(struct jz4740_mmc_host
*host
,
298 struct mmc_data
*data
)
301 struct dma_chan
*chan
;
302 struct dma_async_tx_descriptor
*desc
;
303 struct dma_slave_config conf
= {
304 .src_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
,
305 .dst_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
,
306 .src_maxburst
= JZ4740_MMC_FIFO_HALF_SIZE
,
307 .dst_maxburst
= JZ4740_MMC_FIFO_HALF_SIZE
,
310 if (data
->flags
& MMC_DATA_WRITE
) {
311 conf
.direction
= DMA_MEM_TO_DEV
;
312 conf
.dst_addr
= host
->mem_res
->start
+ JZ_REG_MMC_TXFIFO
;
313 conf
.slave_id
= JZ4740_DMA_TYPE_MMC_TRANSMIT
;
316 conf
.direction
= DMA_DEV_TO_MEM
;
317 conf
.src_addr
= host
->mem_res
->start
+ JZ_REG_MMC_RXFIFO
;
318 conf
.slave_id
= JZ4740_DMA_TYPE_MMC_RECEIVE
;
322 ret
= jz4740_mmc_prepare_dma_data(host
, data
, NULL
, chan
);
326 dmaengine_slave_config(chan
, &conf
);
327 desc
= dmaengine_prep_slave_sg(chan
,
331 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
333 dev_err(mmc_dev(host
->mmc
),
334 "Failed to allocate DMA %s descriptor",
335 conf
.direction
== DMA_MEM_TO_DEV
? "TX" : "RX");
339 dmaengine_submit(desc
);
340 dma_async_issue_pending(chan
);
345 jz4740_mmc_dma_unmap(host
, data
);
349 static void jz4740_mmc_pre_request(struct mmc_host
*mmc
,
350 struct mmc_request
*mrq
)
352 struct jz4740_mmc_host
*host
= mmc_priv(mmc
);
353 struct mmc_data
*data
= mrq
->data
;
354 struct jz4740_mmc_host_next
*next_data
= &host
->next_data
;
356 BUG_ON(data
->host_cookie
);
359 struct dma_chan
*chan
= jz4740_mmc_get_dma_chan(host
, data
);
361 if (jz4740_mmc_prepare_dma_data(host
, data
, next_data
, chan
))
362 data
->host_cookie
= 0;
366 static void jz4740_mmc_post_request(struct mmc_host
*mmc
,
367 struct mmc_request
*mrq
,
370 struct jz4740_mmc_host
*host
= mmc_priv(mmc
);
371 struct mmc_data
*data
= mrq
->data
;
373 if (host
->use_dma
&& data
->host_cookie
) {
374 jz4740_mmc_dma_unmap(host
, data
);
375 data
->host_cookie
= 0;
379 struct dma_chan
*chan
= jz4740_mmc_get_dma_chan(host
, data
);
381 dmaengine_terminate_all(chan
);
385 /*----------------------------------------------------------------------------*/
387 static void jz4740_mmc_set_irq_enabled(struct jz4740_mmc_host
*host
,
388 unsigned int irq
, bool enabled
)
392 spin_lock_irqsave(&host
->lock
, flags
);
394 host
->irq_mask
&= ~irq
;
396 host
->irq_mask
|= irq
;
398 jz4740_mmc_write_irq_mask(host
, host
->irq_mask
);
399 spin_unlock_irqrestore(&host
->lock
, flags
);
402 static void jz4740_mmc_clock_enable(struct jz4740_mmc_host
*host
,
405 uint16_t val
= JZ_MMC_STRPCL_CLOCK_START
;
408 val
|= JZ_MMC_STRPCL_START_OP
;
410 writew(val
, host
->base
+ JZ_REG_MMC_STRPCL
);
413 static void jz4740_mmc_clock_disable(struct jz4740_mmc_host
*host
)
416 unsigned int timeout
= 1000;
418 writew(JZ_MMC_STRPCL_CLOCK_STOP
, host
->base
+ JZ_REG_MMC_STRPCL
);
420 status
= readl(host
->base
+ JZ_REG_MMC_STATUS
);
421 } while (status
& JZ_MMC_STATUS_CLK_EN
&& --timeout
);
424 static void jz4740_mmc_reset(struct jz4740_mmc_host
*host
)
427 unsigned int timeout
= 1000;
429 writew(JZ_MMC_STRPCL_RESET
, host
->base
+ JZ_REG_MMC_STRPCL
);
432 status
= readl(host
->base
+ JZ_REG_MMC_STATUS
);
433 } while (status
& JZ_MMC_STATUS_IS_RESETTING
&& --timeout
);
436 static void jz4740_mmc_request_done(struct jz4740_mmc_host
*host
)
438 struct mmc_request
*req
;
443 mmc_request_done(host
->mmc
, req
);
446 static unsigned int jz4740_mmc_poll_irq(struct jz4740_mmc_host
*host
,
449 unsigned int timeout
= 0x800;
453 status
= jz4740_mmc_read_irq_reg(host
);
454 } while (!(status
& irq
) && --timeout
);
457 set_bit(0, &host
->waiting
);
458 mod_timer(&host
->timeout_timer
, jiffies
+ 5*HZ
);
459 jz4740_mmc_set_irq_enabled(host
, irq
, true);
466 static void jz4740_mmc_transfer_check_state(struct jz4740_mmc_host
*host
,
467 struct mmc_data
*data
)
471 status
= readl(host
->base
+ JZ_REG_MMC_STATUS
);
472 if (status
& JZ_MMC_STATUS_WRITE_ERROR_MASK
) {
473 if (status
& (JZ_MMC_STATUS_TIMEOUT_WRITE
)) {
474 host
->req
->cmd
->error
= -ETIMEDOUT
;
475 data
->error
= -ETIMEDOUT
;
477 host
->req
->cmd
->error
= -EIO
;
480 } else if (status
& JZ_MMC_STATUS_READ_ERROR_MASK
) {
481 if (status
& (JZ_MMC_STATUS_TIMEOUT_READ
)) {
482 host
->req
->cmd
->error
= -ETIMEDOUT
;
483 data
->error
= -ETIMEDOUT
;
485 host
->req
->cmd
->error
= -EIO
;
491 static bool jz4740_mmc_write_data(struct jz4740_mmc_host
*host
,
492 struct mmc_data
*data
)
494 struct sg_mapping_iter
*miter
= &host
->miter
;
495 void __iomem
*fifo_addr
= host
->base
+ JZ_REG_MMC_TXFIFO
;
500 while (sg_miter_next(miter
)) {
502 i
= miter
->length
/ 4;
506 timeout
= jz4740_mmc_poll_irq(host
, JZ_MMC_IRQ_TXFIFO_WR_REQ
);
507 if (unlikely(timeout
))
510 writel(buf
[0], fifo_addr
);
511 writel(buf
[1], fifo_addr
);
512 writel(buf
[2], fifo_addr
);
513 writel(buf
[3], fifo_addr
);
514 writel(buf
[4], fifo_addr
);
515 writel(buf
[5], fifo_addr
);
516 writel(buf
[6], fifo_addr
);
517 writel(buf
[7], fifo_addr
);
522 timeout
= jz4740_mmc_poll_irq(host
, JZ_MMC_IRQ_TXFIFO_WR_REQ
);
523 if (unlikely(timeout
))
527 writel(*buf
, fifo_addr
);
532 data
->bytes_xfered
+= miter
->length
;
534 sg_miter_stop(miter
);
539 miter
->consumed
= (void *)buf
- miter
->addr
;
540 data
->bytes_xfered
+= miter
->consumed
;
541 sg_miter_stop(miter
);
546 static bool jz4740_mmc_read_data(struct jz4740_mmc_host
*host
,
547 struct mmc_data
*data
)
549 struct sg_mapping_iter
*miter
= &host
->miter
;
550 void __iomem
*fifo_addr
= host
->base
+ JZ_REG_MMC_RXFIFO
;
555 unsigned int timeout
;
557 while (sg_miter_next(miter
)) {
563 timeout
= jz4740_mmc_poll_irq(host
, JZ_MMC_IRQ_RXFIFO_RD_REQ
);
564 if (unlikely(timeout
))
567 buf
[0] = readl(fifo_addr
);
568 buf
[1] = readl(fifo_addr
);
569 buf
[2] = readl(fifo_addr
);
570 buf
[3] = readl(fifo_addr
);
571 buf
[4] = readl(fifo_addr
);
572 buf
[5] = readl(fifo_addr
);
573 buf
[6] = readl(fifo_addr
);
574 buf
[7] = readl(fifo_addr
);
581 timeout
= jz4740_mmc_poll_irq(host
, JZ_MMC_IRQ_RXFIFO_RD_REQ
);
582 if (unlikely(timeout
))
586 *buf
++ = readl(fifo_addr
);
589 if (unlikely(i
> 0)) {
590 d
= readl(fifo_addr
);
594 data
->bytes_xfered
+= miter
->length
;
596 /* This can go away once MIPS implements
597 * flush_kernel_dcache_page */
598 flush_dcache_page(miter
->page
);
600 sg_miter_stop(miter
);
602 /* For whatever reason there is sometime one word more in the fifo then
605 status
= readl(host
->base
+ JZ_REG_MMC_STATUS
);
606 while (!(status
& JZ_MMC_STATUS_DATA_FIFO_EMPTY
) && --timeout
) {
607 d
= readl(fifo_addr
);
608 status
= readl(host
->base
+ JZ_REG_MMC_STATUS
);
614 miter
->consumed
= (void *)buf
- miter
->addr
;
615 data
->bytes_xfered
+= miter
->consumed
;
616 sg_miter_stop(miter
);
621 static void jz4740_mmc_timeout(struct timer_list
*t
)
623 struct jz4740_mmc_host
*host
= from_timer(host
, t
, timeout_timer
);
625 if (!test_and_clear_bit(0, &host
->waiting
))
628 jz4740_mmc_set_irq_enabled(host
, JZ_MMC_IRQ_END_CMD_RES
, false);
630 host
->req
->cmd
->error
= -ETIMEDOUT
;
631 jz4740_mmc_request_done(host
);
634 static void jz4740_mmc_read_response(struct jz4740_mmc_host
*host
,
635 struct mmc_command
*cmd
)
639 void __iomem
*fifo_addr
= host
->base
+ JZ_REG_MMC_RESP_FIFO
;
641 if (cmd
->flags
& MMC_RSP_136
) {
642 tmp
= readw(fifo_addr
);
643 for (i
= 0; i
< 4; ++i
) {
644 cmd
->resp
[i
] = tmp
<< 24;
645 tmp
= readw(fifo_addr
);
646 cmd
->resp
[i
] |= tmp
<< 8;
647 tmp
= readw(fifo_addr
);
648 cmd
->resp
[i
] |= tmp
>> 8;
651 cmd
->resp
[0] = readw(fifo_addr
) << 24;
652 cmd
->resp
[0] |= readw(fifo_addr
) << 8;
653 cmd
->resp
[0] |= readw(fifo_addr
) & 0xff;
657 static void jz4740_mmc_send_command(struct jz4740_mmc_host
*host
,
658 struct mmc_command
*cmd
)
660 uint32_t cmdat
= host
->cmdat
;
662 host
->cmdat
&= ~JZ_MMC_CMDAT_INIT
;
663 jz4740_mmc_clock_disable(host
);
667 if (cmd
->flags
& MMC_RSP_BUSY
)
668 cmdat
|= JZ_MMC_CMDAT_BUSY
;
670 switch (mmc_resp_type(cmd
)) {
673 cmdat
|= JZ_MMC_CMDAT_RSP_R1
;
676 cmdat
|= JZ_MMC_CMDAT_RSP_R2
;
679 cmdat
|= JZ_MMC_CMDAT_RSP_R3
;
686 cmdat
|= JZ_MMC_CMDAT_DATA_EN
;
687 if (cmd
->data
->flags
& MMC_DATA_WRITE
)
688 cmdat
|= JZ_MMC_CMDAT_WRITE
;
691 * The 4780's MMC controller has integrated DMA ability
692 * in addition to being able to use the external DMA
693 * controller. It moves DMA control bits to a separate
694 * register. The DMA_SEL bit chooses the external
695 * controller over the integrated one. Earlier SoCs
696 * can only use the external controller, and have a
697 * single DMA enable bit in CMDAT.
699 if (host
->version
>= JZ_MMC_JZ4780
) {
700 writel(JZ_MMC_DMAC_DMA_EN
| JZ_MMC_DMAC_DMA_SEL
,
701 host
->base
+ JZ_REG_MMC_DMAC
);
703 cmdat
|= JZ_MMC_CMDAT_DMA_EN
;
705 } else if (host
->version
>= JZ_MMC_JZ4780
) {
706 writel(0, host
->base
+ JZ_REG_MMC_DMAC
);
709 writew(cmd
->data
->blksz
, host
->base
+ JZ_REG_MMC_BLKLEN
);
710 writew(cmd
->data
->blocks
, host
->base
+ JZ_REG_MMC_NOB
);
713 writeb(cmd
->opcode
, host
->base
+ JZ_REG_MMC_CMD
);
714 writel(cmd
->arg
, host
->base
+ JZ_REG_MMC_ARG
);
715 writel(cmdat
, host
->base
+ JZ_REG_MMC_CMDAT
);
717 jz4740_mmc_clock_enable(host
, 1);
720 static void jz_mmc_prepare_data_transfer(struct jz4740_mmc_host
*host
)
722 struct mmc_command
*cmd
= host
->req
->cmd
;
723 struct mmc_data
*data
= cmd
->data
;
726 if (data
->flags
& MMC_DATA_READ
)
727 direction
= SG_MITER_TO_SG
;
729 direction
= SG_MITER_FROM_SG
;
731 sg_miter_start(&host
->miter
, data
->sg
, data
->sg_len
, direction
);
735 static irqreturn_t
jz_mmc_irq_worker(int irq
, void *devid
)
737 struct jz4740_mmc_host
*host
= (struct jz4740_mmc_host
*)devid
;
738 struct mmc_command
*cmd
= host
->req
->cmd
;
739 struct mmc_request
*req
= host
->req
;
740 struct mmc_data
*data
= cmd
->data
;
741 bool timeout
= false;
744 host
->state
= JZ4740_MMC_STATE_DONE
;
746 switch (host
->state
) {
747 case JZ4740_MMC_STATE_READ_RESPONSE
:
748 if (cmd
->flags
& MMC_RSP_PRESENT
)
749 jz4740_mmc_read_response(host
, cmd
);
754 jz_mmc_prepare_data_transfer(host
);
756 case JZ4740_MMC_STATE_TRANSFER_DATA
:
758 /* Use DMA if enabled.
759 * Data transfer direction is defined later by
760 * relying on data flags in
761 * jz4740_mmc_prepare_dma_data() and
762 * jz4740_mmc_start_dma_transfer().
764 timeout
= jz4740_mmc_start_dma_transfer(host
, data
);
765 data
->bytes_xfered
= data
->blocks
* data
->blksz
;
766 } else if (data
->flags
& MMC_DATA_READ
)
767 /* Use PIO if DMA is not enabled.
768 * Data transfer direction was defined before
769 * by relying on data flags in
770 * jz_mmc_prepare_data_transfer().
772 timeout
= jz4740_mmc_read_data(host
, data
);
774 timeout
= jz4740_mmc_write_data(host
, data
);
776 if (unlikely(timeout
)) {
777 host
->state
= JZ4740_MMC_STATE_TRANSFER_DATA
;
781 jz4740_mmc_transfer_check_state(host
, data
);
783 timeout
= jz4740_mmc_poll_irq(host
, JZ_MMC_IRQ_DATA_TRAN_DONE
);
784 if (unlikely(timeout
)) {
785 host
->state
= JZ4740_MMC_STATE_SEND_STOP
;
788 jz4740_mmc_write_irq_reg(host
, JZ_MMC_IRQ_DATA_TRAN_DONE
);
790 case JZ4740_MMC_STATE_SEND_STOP
:
794 jz4740_mmc_send_command(host
, req
->stop
);
796 if (mmc_resp_type(req
->stop
) & MMC_RSP_BUSY
) {
797 timeout
= jz4740_mmc_poll_irq(host
,
798 JZ_MMC_IRQ_PRG_DONE
);
800 host
->state
= JZ4740_MMC_STATE_DONE
;
804 case JZ4740_MMC_STATE_DONE
:
809 jz4740_mmc_request_done(host
);
814 static irqreturn_t
jz_mmc_irq(int irq
, void *devid
)
816 struct jz4740_mmc_host
*host
= devid
;
817 struct mmc_command
*cmd
= host
->cmd
;
818 uint32_t irq_reg
, status
, tmp
;
820 status
= readl(host
->base
+ JZ_REG_MMC_STATUS
);
821 irq_reg
= jz4740_mmc_read_irq_reg(host
);
824 irq_reg
&= ~host
->irq_mask
;
826 tmp
&= ~(JZ_MMC_IRQ_TXFIFO_WR_REQ
| JZ_MMC_IRQ_RXFIFO_RD_REQ
|
827 JZ_MMC_IRQ_PRG_DONE
| JZ_MMC_IRQ_DATA_TRAN_DONE
);
830 jz4740_mmc_write_irq_reg(host
, tmp
& ~irq_reg
);
832 if (irq_reg
& JZ_MMC_IRQ_SDIO
) {
833 jz4740_mmc_write_irq_reg(host
, JZ_MMC_IRQ_SDIO
);
834 mmc_signal_sdio_irq(host
->mmc
);
835 irq_reg
&= ~JZ_MMC_IRQ_SDIO
;
838 if (host
->req
&& cmd
&& irq_reg
) {
839 if (test_and_clear_bit(0, &host
->waiting
)) {
840 del_timer(&host
->timeout_timer
);
842 if (status
& JZ_MMC_STATUS_TIMEOUT_RES
) {
843 cmd
->error
= -ETIMEDOUT
;
844 } else if (status
& JZ_MMC_STATUS_CRC_RES_ERR
) {
846 } else if (status
& (JZ_MMC_STATUS_CRC_READ_ERROR
|
847 JZ_MMC_STATUS_CRC_WRITE_ERROR
)) {
849 cmd
->data
->error
= -EIO
;
853 jz4740_mmc_set_irq_enabled(host
, irq_reg
, false);
854 jz4740_mmc_write_irq_reg(host
, irq_reg
);
856 return IRQ_WAKE_THREAD
;
863 static int jz4740_mmc_set_clock_rate(struct jz4740_mmc_host
*host
, int rate
)
868 jz4740_mmc_clock_disable(host
);
869 clk_set_rate(host
->clk
, host
->mmc
->f_max
);
871 real_rate
= clk_get_rate(host
->clk
);
873 while (real_rate
> rate
&& div
< 7) {
878 writew(div
, host
->base
+ JZ_REG_MMC_CLKRT
);
882 static void jz4740_mmc_request(struct mmc_host
*mmc
, struct mmc_request
*req
)
884 struct jz4740_mmc_host
*host
= mmc_priv(mmc
);
888 jz4740_mmc_write_irq_reg(host
, ~0);
889 jz4740_mmc_set_irq_enabled(host
, JZ_MMC_IRQ_END_CMD_RES
, true);
891 host
->state
= JZ4740_MMC_STATE_READ_RESPONSE
;
892 set_bit(0, &host
->waiting
);
893 mod_timer(&host
->timeout_timer
, jiffies
+ 5*HZ
);
894 jz4740_mmc_send_command(host
, req
->cmd
);
897 static void jz4740_mmc_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
899 struct jz4740_mmc_host
*host
= mmc_priv(mmc
);
901 jz4740_mmc_set_clock_rate(host
, ios
->clock
);
903 switch (ios
->power_mode
) {
905 jz4740_mmc_reset(host
);
906 if (host
->pdata
&& gpio_is_valid(host
->pdata
->gpio_power
))
907 gpio_set_value(host
->pdata
->gpio_power
,
908 !host
->pdata
->power_active_low
);
909 host
->cmdat
|= JZ_MMC_CMDAT_INIT
;
910 clk_prepare_enable(host
->clk
);
915 if (host
->pdata
&& gpio_is_valid(host
->pdata
->gpio_power
))
916 gpio_set_value(host
->pdata
->gpio_power
,
917 host
->pdata
->power_active_low
);
918 clk_disable_unprepare(host
->clk
);
922 switch (ios
->bus_width
) {
923 case MMC_BUS_WIDTH_1
:
924 host
->cmdat
&= ~JZ_MMC_CMDAT_BUS_WIDTH_4BIT
;
926 case MMC_BUS_WIDTH_4
:
927 host
->cmdat
|= JZ_MMC_CMDAT_BUS_WIDTH_4BIT
;
934 static void jz4740_mmc_enable_sdio_irq(struct mmc_host
*mmc
, int enable
)
936 struct jz4740_mmc_host
*host
= mmc_priv(mmc
);
937 jz4740_mmc_set_irq_enabled(host
, JZ_MMC_IRQ_SDIO
, enable
);
940 static const struct mmc_host_ops jz4740_mmc_ops
= {
941 .request
= jz4740_mmc_request
,
942 .pre_req
= jz4740_mmc_pre_request
,
943 .post_req
= jz4740_mmc_post_request
,
944 .set_ios
= jz4740_mmc_set_ios
,
945 .get_ro
= mmc_gpio_get_ro
,
946 .get_cd
= mmc_gpio_get_cd
,
947 .enable_sdio_irq
= jz4740_mmc_enable_sdio_irq
,
950 static int jz4740_mmc_request_gpio(struct device
*dev
, int gpio
,
951 const char *name
, bool output
, int value
)
955 if (!gpio_is_valid(gpio
))
958 ret
= gpio_request(gpio
, name
);
960 dev_err(dev
, "Failed to request %s gpio: %d\n", name
, ret
);
965 gpio_direction_output(gpio
, value
);
967 gpio_direction_input(gpio
);
972 static int jz4740_mmc_request_gpios(struct mmc_host
*mmc
,
973 struct platform_device
*pdev
)
975 struct jz4740_mmc_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
981 if (!pdata
->card_detect_active_low
)
982 mmc
->caps2
|= MMC_CAP2_CD_ACTIVE_HIGH
;
983 if (!pdata
->read_only_active_low
)
984 mmc
->caps2
|= MMC_CAP2_RO_ACTIVE_HIGH
;
986 if (gpio_is_valid(pdata
->gpio_card_detect
)) {
987 ret
= mmc_gpio_request_cd(mmc
, pdata
->gpio_card_detect
, 0);
992 if (gpio_is_valid(pdata
->gpio_read_only
)) {
993 ret
= mmc_gpio_request_ro(mmc
, pdata
->gpio_read_only
);
998 return jz4740_mmc_request_gpio(&pdev
->dev
, pdata
->gpio_power
,
999 "MMC read only", true, pdata
->power_active_low
);
1002 static void jz4740_mmc_free_gpios(struct platform_device
*pdev
)
1004 struct jz4740_mmc_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
1009 if (gpio_is_valid(pdata
->gpio_power
))
1010 gpio_free(pdata
->gpio_power
);
1013 static const struct of_device_id jz4740_mmc_of_match
[] = {
1014 { .compatible
= "ingenic,jz4740-mmc", .data
= (void *) JZ_MMC_JZ4740
},
1015 { .compatible
= "ingenic,jz4780-mmc", .data
= (void *) JZ_MMC_JZ4780
},
1018 MODULE_DEVICE_TABLE(of
, jz4740_mmc_of_match
);
1020 static int jz4740_mmc_probe(struct platform_device
* pdev
)
1023 struct mmc_host
*mmc
;
1024 struct jz4740_mmc_host
*host
;
1025 const struct of_device_id
*match
;
1026 struct jz4740_mmc_platform_data
*pdata
;
1028 pdata
= dev_get_platdata(&pdev
->dev
);
1030 mmc
= mmc_alloc_host(sizeof(struct jz4740_mmc_host
), &pdev
->dev
);
1032 dev_err(&pdev
->dev
, "Failed to alloc mmc host structure\n");
1036 host
= mmc_priv(mmc
);
1037 host
->pdata
= pdata
;
1039 match
= of_match_device(jz4740_mmc_of_match
, &pdev
->dev
);
1041 host
->version
= (enum jz4740_mmc_version
)match
->data
;
1042 ret
= mmc_of_parse(mmc
);
1044 if (ret
!= -EPROBE_DEFER
)
1046 "could not parse of data: %d\n", ret
);
1050 /* JZ4740 should be the only one using legacy probe */
1051 host
->version
= JZ_MMC_JZ4740
;
1052 mmc
->caps
|= MMC_CAP_SDIO_IRQ
;
1053 if (!(pdata
&& pdata
->data_1bit
))
1054 mmc
->caps
|= MMC_CAP_4_BIT_DATA
;
1055 ret
= jz4740_mmc_request_gpios(mmc
, pdev
);
1060 host
->irq
= platform_get_irq(pdev
, 0);
1061 if (host
->irq
< 0) {
1063 dev_err(&pdev
->dev
, "Failed to get platform irq: %d\n", ret
);
1067 host
->clk
= devm_clk_get(&pdev
->dev
, "mmc");
1068 if (IS_ERR(host
->clk
)) {
1069 ret
= PTR_ERR(host
->clk
);
1070 dev_err(&pdev
->dev
, "Failed to get mmc clock\n");
1074 host
->mem_res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1075 host
->base
= devm_ioremap_resource(&pdev
->dev
, host
->mem_res
);
1076 if (IS_ERR(host
->base
)) {
1077 ret
= PTR_ERR(host
->base
);
1078 dev_err(&pdev
->dev
, "Failed to ioremap base memory\n");
1082 mmc
->ops
= &jz4740_mmc_ops
;
1084 mmc
->f_max
= JZ_MMC_CLK_RATE
;
1085 mmc
->f_min
= mmc
->f_max
/ 128;
1086 mmc
->ocr_avail
= MMC_VDD_32_33
| MMC_VDD_33_34
;
1088 mmc
->max_blk_size
= (1 << 10) - 1;
1089 mmc
->max_blk_count
= (1 << 15) - 1;
1090 mmc
->max_req_size
= mmc
->max_blk_size
* mmc
->max_blk_count
;
1092 mmc
->max_segs
= 128;
1093 mmc
->max_seg_size
= mmc
->max_req_size
;
1097 spin_lock_init(&host
->lock
);
1098 host
->irq_mask
= ~0;
1100 jz4740_mmc_reset(host
);
1102 ret
= request_threaded_irq(host
->irq
, jz_mmc_irq
, jz_mmc_irq_worker
, 0,
1103 dev_name(&pdev
->dev
), host
);
1105 dev_err(&pdev
->dev
, "Failed to request irq: %d\n", ret
);
1106 goto err_free_gpios
;
1109 jz4740_mmc_clock_disable(host
);
1110 timer_setup(&host
->timeout_timer
, jz4740_mmc_timeout
, 0);
1112 ret
= jz4740_mmc_acquire_dma_channels(host
);
1113 if (ret
== -EPROBE_DEFER
)
1115 host
->use_dma
= !ret
;
1117 platform_set_drvdata(pdev
, host
);
1118 ret
= mmc_add_host(mmc
);
1121 dev_err(&pdev
->dev
, "Failed to add mmc host: %d\n", ret
);
1122 goto err_release_dma
;
1124 dev_info(&pdev
->dev
, "JZ SD/MMC card driver registered\n");
1126 dev_info(&pdev
->dev
, "Using %s, %d-bit mode\n",
1127 host
->use_dma
? "DMA" : "PIO",
1128 (mmc
->caps
& MMC_CAP_4_BIT_DATA
) ? 4 : 1);
1134 jz4740_mmc_release_dma_channels(host
);
1136 free_irq(host
->irq
, host
);
1138 jz4740_mmc_free_gpios(pdev
);
1145 static int jz4740_mmc_remove(struct platform_device
*pdev
)
1147 struct jz4740_mmc_host
*host
= platform_get_drvdata(pdev
);
1149 del_timer_sync(&host
->timeout_timer
);
1150 jz4740_mmc_set_irq_enabled(host
, 0xff, false);
1151 jz4740_mmc_reset(host
);
1153 mmc_remove_host(host
->mmc
);
1155 free_irq(host
->irq
, host
);
1157 jz4740_mmc_free_gpios(pdev
);
1160 jz4740_mmc_release_dma_channels(host
);
1162 mmc_free_host(host
->mmc
);
1167 #ifdef CONFIG_PM_SLEEP
1169 static int jz4740_mmc_suspend(struct device
*dev
)
1171 return pinctrl_pm_select_sleep_state(dev
);
1174 static int jz4740_mmc_resume(struct device
*dev
)
1176 return pinctrl_pm_select_default_state(dev
);
1179 static SIMPLE_DEV_PM_OPS(jz4740_mmc_pm_ops
, jz4740_mmc_suspend
,
1181 #define JZ4740_MMC_PM_OPS (&jz4740_mmc_pm_ops)
1183 #define JZ4740_MMC_PM_OPS NULL
1186 static struct platform_driver jz4740_mmc_driver
= {
1187 .probe
= jz4740_mmc_probe
,
1188 .remove
= jz4740_mmc_remove
,
1190 .name
= "jz4740-mmc",
1191 .of_match_table
= of_match_ptr(jz4740_mmc_of_match
),
1192 .pm
= JZ4740_MMC_PM_OPS
,
1196 module_platform_driver(jz4740_mmc_driver
);
1198 MODULE_DESCRIPTION("JZ4740 SD/MMC controller driver");
1199 MODULE_LICENSE("GPL");
1200 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");