blob: aa635b458d2c9e024a0ae1b7758c8d72d067cebc [file] [log] [blame]
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +00001/*
2 * Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de>
3 * JZ4740 SD/MMC controller driver
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 *
10 * You should have received a copy of the GNU General Public License along
11 * with this program; if not, write to the Free Software Foundation, Inc.,
12 * 675 Mass Ave, Cambridge, MA 02139, USA.
13 *
14 */
15
Ezequiel Garcia7e7845f2018-03-28 18:00:45 -030016#include <linux/bitops.h>
17#include <linux/clk.h>
18#include <linux/delay.h>
19#include <linux/dmaengine.h>
20#include <linux/dma-mapping.h>
Jamie Iles3119cbd2011-01-11 12:43:50 +000021#include <linux/err.h>
Ezequiel Garcia7e7845f2018-03-28 18:00:45 -030022#include <linux/gpio.h>
23#include <linux/interrupt.h>
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +000024#include <linux/io.h>
25#include <linux/irq.h>
Ezequiel Garcia7e7845f2018-03-28 18:00:45 -030026#include <linux/mmc/host.h>
27#include <linux/mmc/slot-gpio.h>
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +000028#include <linux/module.h>
Ezequiel Garcia61e11eb2018-03-28 18:00:48 -030029#include <linux/of_device.h>
Paul Cercueilfa5ed6b2017-05-12 18:53:03 +020030#include <linux/pinctrl/consumer.h>
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +000031#include <linux/platform_device.h>
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +000032#include <linux/scatterlist.h>
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +000033
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +000034#include <asm/cacheflush.h>
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +000035
Apelete Seketeli7ca27a62014-07-21 06:37:44 +020036#include <asm/mach-jz4740/dma.h>
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +000037#include <asm/mach-jz4740/jz4740_mmc.h>
38
39#define JZ_REG_MMC_STRPCL 0x00
40#define JZ_REG_MMC_STATUS 0x04
41#define JZ_REG_MMC_CLKRT 0x08
42#define JZ_REG_MMC_CMDAT 0x0C
43#define JZ_REG_MMC_RESTO 0x10
44#define JZ_REG_MMC_RDTO 0x14
45#define JZ_REG_MMC_BLKLEN 0x18
46#define JZ_REG_MMC_NOB 0x1C
47#define JZ_REG_MMC_SNOB 0x20
48#define JZ_REG_MMC_IMASK 0x24
49#define JZ_REG_MMC_IREG 0x28
50#define JZ_REG_MMC_CMD 0x2C
51#define JZ_REG_MMC_ARG 0x30
52#define JZ_REG_MMC_RESP_FIFO 0x34
53#define JZ_REG_MMC_RXFIFO 0x38
54#define JZ_REG_MMC_TXFIFO 0x3C
55
56#define JZ_MMC_STRPCL_EXIT_MULTIPLE BIT(7)
57#define JZ_MMC_STRPCL_EXIT_TRANSFER BIT(6)
58#define JZ_MMC_STRPCL_START_READWAIT BIT(5)
59#define JZ_MMC_STRPCL_STOP_READWAIT BIT(4)
60#define JZ_MMC_STRPCL_RESET BIT(3)
61#define JZ_MMC_STRPCL_START_OP BIT(2)
62#define JZ_MMC_STRPCL_CLOCK_CONTROL (BIT(1) | BIT(0))
63#define JZ_MMC_STRPCL_CLOCK_STOP BIT(0)
64#define JZ_MMC_STRPCL_CLOCK_START BIT(1)
65
66
67#define JZ_MMC_STATUS_IS_RESETTING BIT(15)
68#define JZ_MMC_STATUS_SDIO_INT_ACTIVE BIT(14)
69#define JZ_MMC_STATUS_PRG_DONE BIT(13)
70#define JZ_MMC_STATUS_DATA_TRAN_DONE BIT(12)
71#define JZ_MMC_STATUS_END_CMD_RES BIT(11)
72#define JZ_MMC_STATUS_DATA_FIFO_AFULL BIT(10)
73#define JZ_MMC_STATUS_IS_READWAIT BIT(9)
74#define JZ_MMC_STATUS_CLK_EN BIT(8)
75#define JZ_MMC_STATUS_DATA_FIFO_FULL BIT(7)
76#define JZ_MMC_STATUS_DATA_FIFO_EMPTY BIT(6)
77#define JZ_MMC_STATUS_CRC_RES_ERR BIT(5)
78#define JZ_MMC_STATUS_CRC_READ_ERROR BIT(4)
79#define JZ_MMC_STATUS_TIMEOUT_WRITE BIT(3)
80#define JZ_MMC_STATUS_CRC_WRITE_ERROR BIT(2)
81#define JZ_MMC_STATUS_TIMEOUT_RES BIT(1)
82#define JZ_MMC_STATUS_TIMEOUT_READ BIT(0)
83
84#define JZ_MMC_STATUS_READ_ERROR_MASK (BIT(4) | BIT(0))
85#define JZ_MMC_STATUS_WRITE_ERROR_MASK (BIT(3) | BIT(2))
86
87
88#define JZ_MMC_CMDAT_IO_ABORT BIT(11)
89#define JZ_MMC_CMDAT_BUS_WIDTH_4BIT BIT(10)
90#define JZ_MMC_CMDAT_DMA_EN BIT(8)
91#define JZ_MMC_CMDAT_INIT BIT(7)
92#define JZ_MMC_CMDAT_BUSY BIT(6)
93#define JZ_MMC_CMDAT_STREAM BIT(5)
94#define JZ_MMC_CMDAT_WRITE BIT(4)
95#define JZ_MMC_CMDAT_DATA_EN BIT(3)
96#define JZ_MMC_CMDAT_RESPONSE_FORMAT (BIT(2) | BIT(1) | BIT(0))
97#define JZ_MMC_CMDAT_RSP_R1 1
98#define JZ_MMC_CMDAT_RSP_R2 2
99#define JZ_MMC_CMDAT_RSP_R3 3
100
101#define JZ_MMC_IRQ_SDIO BIT(7)
102#define JZ_MMC_IRQ_TXFIFO_WR_REQ BIT(6)
103#define JZ_MMC_IRQ_RXFIFO_RD_REQ BIT(5)
104#define JZ_MMC_IRQ_END_CMD_RES BIT(2)
105#define JZ_MMC_IRQ_PRG_DONE BIT(1)
106#define JZ_MMC_IRQ_DATA_TRAN_DONE BIT(0)
107
108
109#define JZ_MMC_CLK_RATE 24000000
110
Ezequiel Garcia61e11eb2018-03-28 18:00:48 -0300111enum jz4740_mmc_version {
112 JZ_MMC_JZ4740,
113};
114
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +0000115enum jz4740_mmc_state {
116 JZ4740_MMC_STATE_READ_RESPONSE,
117 JZ4740_MMC_STATE_TRANSFER_DATA,
118 JZ4740_MMC_STATE_SEND_STOP,
119 JZ4740_MMC_STATE_DONE,
120};
121
Apelete Seketelibb2f4592014-07-21 06:37:45 +0200122struct jz4740_mmc_host_next {
123 int sg_len;
124 s32 cookie;
125};
126
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +0000127struct jz4740_mmc_host {
128 struct mmc_host *mmc;
129 struct platform_device *pdev;
130 struct jz4740_mmc_platform_data *pdata;
131 struct clk *clk;
132
Ezequiel Garcia61e11eb2018-03-28 18:00:48 -0300133 enum jz4740_mmc_version version;
134
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +0000135 int irq;
136 int card_detect_irq;
137
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +0000138 void __iomem *base;
Apelete Seketeli7ca27a62014-07-21 06:37:44 +0200139 struct resource *mem_res;
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +0000140 struct mmc_request *req;
141 struct mmc_command *cmd;
142
143 unsigned long waiting;
144
145 uint32_t cmdat;
146
147 uint16_t irq_mask;
148
149 spinlock_t lock;
150
151 struct timer_list timeout_timer;
152 struct sg_mapping_iter miter;
153 enum jz4740_mmc_state state;
Apelete Seketeli7ca27a62014-07-21 06:37:44 +0200154
155 /* DMA support */
156 struct dma_chan *dma_rx;
157 struct dma_chan *dma_tx;
Apelete Seketelibb2f4592014-07-21 06:37:45 +0200158 struct jz4740_mmc_host_next next_data;
Apelete Seketeli7ca27a62014-07-21 06:37:44 +0200159 bool use_dma;
160 int sg_len;
161
162/* The DMA trigger level is 8 words, that is to say, the DMA read
163 * trigger is when data words in MSC_RXFIFO is >= 8 and the DMA write
164 * trigger is when data words in MSC_TXFIFO is < 8.
165 */
166#define JZ4740_MMC_FIFO_HALF_SIZE 8
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +0000167};
168
Apelete Seketeli7ca27a62014-07-21 06:37:44 +0200169/*----------------------------------------------------------------------------*/
170/* DMA infrastructure */
171
172static void jz4740_mmc_release_dma_channels(struct jz4740_mmc_host *host)
173{
174 if (!host->use_dma)
175 return;
176
177 dma_release_channel(host->dma_tx);
178 dma_release_channel(host->dma_rx);
179}
180
181static int jz4740_mmc_acquire_dma_channels(struct jz4740_mmc_host *host)
182{
183 dma_cap_mask_t mask;
184
185 dma_cap_zero(mask);
186 dma_cap_set(DMA_SLAVE, mask);
187
188 host->dma_tx = dma_request_channel(mask, NULL, host);
189 if (!host->dma_tx) {
190 dev_err(mmc_dev(host->mmc), "Failed to get dma_tx channel\n");
191 return -ENODEV;
192 }
193
194 host->dma_rx = dma_request_channel(mask, NULL, host);
195 if (!host->dma_rx) {
196 dev_err(mmc_dev(host->mmc), "Failed to get dma_rx channel\n");
197 goto free_master_write;
198 }
199
Apelete Seketelibb2f4592014-07-21 06:37:45 +0200200 /* Initialize DMA pre request cookie */
201 host->next_data.cookie = 1;
202
Apelete Seketeli7ca27a62014-07-21 06:37:44 +0200203 return 0;
204
205free_master_write:
206 dma_release_channel(host->dma_tx);
207 return -ENODEV;
208}
209
Apelete Seketelibb2f4592014-07-21 06:37:45 +0200210static inline struct dma_chan *jz4740_mmc_get_dma_chan(struct jz4740_mmc_host *host,
211 struct mmc_data *data)
212{
213 return (data->flags & MMC_DATA_READ) ? host->dma_rx : host->dma_tx;
214}
215
Apelete Seketeli7ca27a62014-07-21 06:37:44 +0200216static void jz4740_mmc_dma_unmap(struct jz4740_mmc_host *host,
217 struct mmc_data *data)
218{
Apelete Seketelibb2f4592014-07-21 06:37:45 +0200219 struct dma_chan *chan = jz4740_mmc_get_dma_chan(host, data);
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200220 enum dma_data_direction dir = mmc_get_dma_dir(data);
Apelete Seketeli7ca27a62014-07-21 06:37:44 +0200221
Apelete Seketeli7ca27a62014-07-21 06:37:44 +0200222 dma_unmap_sg(chan->device->dev, data->sg, data->sg_len, dir);
223}
224
Apelete Seketelibb2f4592014-07-21 06:37:45 +0200225/* Prepares DMA data for current/next transfer, returns non-zero on failure */
226static int jz4740_mmc_prepare_dma_data(struct jz4740_mmc_host *host,
227 struct mmc_data *data,
228 struct jz4740_mmc_host_next *next,
229 struct dma_chan *chan)
230{
231 struct jz4740_mmc_host_next *next_data = &host->next_data;
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200232 enum dma_data_direction dir = mmc_get_dma_dir(data);
Apelete Seketelibb2f4592014-07-21 06:37:45 +0200233 int sg_len;
234
235 if (!next && data->host_cookie &&
236 data->host_cookie != host->next_data.cookie) {
237 dev_warn(mmc_dev(host->mmc),
238 "[%s] invalid cookie: data->host_cookie %d host->next_data.cookie %d\n",
239 __func__,
240 data->host_cookie,
241 host->next_data.cookie);
242 data->host_cookie = 0;
243 }
244
245 /* Check if next job is already prepared */
246 if (next || data->host_cookie != host->next_data.cookie) {
247 sg_len = dma_map_sg(chan->device->dev,
248 data->sg,
249 data->sg_len,
250 dir);
251
252 } else {
253 sg_len = next_data->sg_len;
254 next_data->sg_len = 0;
255 }
256
257 if (sg_len <= 0) {
258 dev_err(mmc_dev(host->mmc),
259 "Failed to map scatterlist for DMA operation\n");
260 return -EINVAL;
261 }
262
263 if (next) {
264 next->sg_len = sg_len;
265 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
266 } else
267 host->sg_len = sg_len;
268
269 return 0;
270}
271
Apelete Seketeli7ca27a62014-07-21 06:37:44 +0200272static int jz4740_mmc_start_dma_transfer(struct jz4740_mmc_host *host,
273 struct mmc_data *data)
274{
Apelete Seketelibb2f4592014-07-21 06:37:45 +0200275 int ret;
Apelete Seketeli7ca27a62014-07-21 06:37:44 +0200276 struct dma_chan *chan;
277 struct dma_async_tx_descriptor *desc;
278 struct dma_slave_config conf = {
279 .src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
280 .dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
281 .src_maxburst = JZ4740_MMC_FIFO_HALF_SIZE,
282 .dst_maxburst = JZ4740_MMC_FIFO_HALF_SIZE,
283 };
Apelete Seketeli7ca27a62014-07-21 06:37:44 +0200284
Apelete Seketelibb2f4592014-07-21 06:37:45 +0200285 if (data->flags & MMC_DATA_WRITE) {
Apelete Seketeli7ca27a62014-07-21 06:37:44 +0200286 conf.direction = DMA_MEM_TO_DEV;
287 conf.dst_addr = host->mem_res->start + JZ_REG_MMC_TXFIFO;
288 conf.slave_id = JZ4740_DMA_TYPE_MMC_TRANSMIT;
289 chan = host->dma_tx;
290 } else {
291 conf.direction = DMA_DEV_TO_MEM;
292 conf.src_addr = host->mem_res->start + JZ_REG_MMC_RXFIFO;
293 conf.slave_id = JZ4740_DMA_TYPE_MMC_RECEIVE;
294 chan = host->dma_rx;
295 }
296
Apelete Seketelibb2f4592014-07-21 06:37:45 +0200297 ret = jz4740_mmc_prepare_dma_data(host, data, NULL, chan);
298 if (ret)
299 return ret;
Apelete Seketeli7ca27a62014-07-21 06:37:44 +0200300
301 dmaengine_slave_config(chan, &conf);
302 desc = dmaengine_prep_slave_sg(chan,
303 data->sg,
304 host->sg_len,
305 conf.direction,
306 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
307 if (!desc) {
308 dev_err(mmc_dev(host->mmc),
309 "Failed to allocate DMA %s descriptor",
310 conf.direction == DMA_MEM_TO_DEV ? "TX" : "RX");
311 goto dma_unmap;
312 }
313
314 dmaengine_submit(desc);
315 dma_async_issue_pending(chan);
316
317 return 0;
318
319dma_unmap:
320 jz4740_mmc_dma_unmap(host, data);
321 return -ENOMEM;
322}
323
Apelete Seketelibb2f4592014-07-21 06:37:45 +0200324static void jz4740_mmc_pre_request(struct mmc_host *mmc,
Linus Walleijd3c6aac2016-11-23 11:02:24 +0100325 struct mmc_request *mrq)
Apelete Seketelibb2f4592014-07-21 06:37:45 +0200326{
327 struct jz4740_mmc_host *host = mmc_priv(mmc);
328 struct mmc_data *data = mrq->data;
329 struct jz4740_mmc_host_next *next_data = &host->next_data;
330
331 BUG_ON(data->host_cookie);
332
333 if (host->use_dma) {
334 struct dma_chan *chan = jz4740_mmc_get_dma_chan(host, data);
335
336 if (jz4740_mmc_prepare_dma_data(host, data, next_data, chan))
337 data->host_cookie = 0;
338 }
339}
340
341static void jz4740_mmc_post_request(struct mmc_host *mmc,
342 struct mmc_request *mrq,
343 int err)
344{
345 struct jz4740_mmc_host *host = mmc_priv(mmc);
346 struct mmc_data *data = mrq->data;
347
348 if (host->use_dma && data->host_cookie) {
349 jz4740_mmc_dma_unmap(host, data);
350 data->host_cookie = 0;
351 }
352
353 if (err) {
354 struct dma_chan *chan = jz4740_mmc_get_dma_chan(host, data);
355
356 dmaengine_terminate_all(chan);
357 }
358}
359
Apelete Seketeli7ca27a62014-07-21 06:37:44 +0200360/*----------------------------------------------------------------------------*/
361
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +0000362static void jz4740_mmc_set_irq_enabled(struct jz4740_mmc_host *host,
363 unsigned int irq, bool enabled)
364{
365 unsigned long flags;
366
367 spin_lock_irqsave(&host->lock, flags);
368 if (enabled)
369 host->irq_mask &= ~irq;
370 else
371 host->irq_mask |= irq;
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +0000372
373 writew(host->irq_mask, host->base + JZ_REG_MMC_IMASK);
Alex Smitha04f0012018-03-28 18:00:43 -0300374 spin_unlock_irqrestore(&host->lock, flags);
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +0000375}
376
377static void jz4740_mmc_clock_enable(struct jz4740_mmc_host *host,
378 bool start_transfer)
379{
380 uint16_t val = JZ_MMC_STRPCL_CLOCK_START;
381
382 if (start_transfer)
383 val |= JZ_MMC_STRPCL_START_OP;
384
385 writew(val, host->base + JZ_REG_MMC_STRPCL);
386}
387
388static void jz4740_mmc_clock_disable(struct jz4740_mmc_host *host)
389{
390 uint32_t status;
391 unsigned int timeout = 1000;
392
393 writew(JZ_MMC_STRPCL_CLOCK_STOP, host->base + JZ_REG_MMC_STRPCL);
394 do {
395 status = readl(host->base + JZ_REG_MMC_STATUS);
396 } while (status & JZ_MMC_STATUS_CLK_EN && --timeout);
397}
398
399static void jz4740_mmc_reset(struct jz4740_mmc_host *host)
400{
401 uint32_t status;
402 unsigned int timeout = 1000;
403
404 writew(JZ_MMC_STRPCL_RESET, host->base + JZ_REG_MMC_STRPCL);
405 udelay(10);
406 do {
407 status = readl(host->base + JZ_REG_MMC_STATUS);
408 } while (status & JZ_MMC_STATUS_IS_RESETTING && --timeout);
409}
410
411static void jz4740_mmc_request_done(struct jz4740_mmc_host *host)
412{
413 struct mmc_request *req;
414
415 req = host->req;
416 host->req = NULL;
417
418 mmc_request_done(host->mmc, req);
419}
420
421static unsigned int jz4740_mmc_poll_irq(struct jz4740_mmc_host *host,
422 unsigned int irq)
423{
424 unsigned int timeout = 0x800;
425 uint16_t status;
426
427 do {
428 status = readw(host->base + JZ_REG_MMC_IREG);
429 } while (!(status & irq) && --timeout);
430
431 if (timeout == 0) {
432 set_bit(0, &host->waiting);
433 mod_timer(&host->timeout_timer, jiffies + 5*HZ);
434 jz4740_mmc_set_irq_enabled(host, irq, true);
435 return true;
436 }
437
438 return false;
439}
440
441static void jz4740_mmc_transfer_check_state(struct jz4740_mmc_host *host,
442 struct mmc_data *data)
443{
444 int status;
445
446 status = readl(host->base + JZ_REG_MMC_STATUS);
447 if (status & JZ_MMC_STATUS_WRITE_ERROR_MASK) {
448 if (status & (JZ_MMC_STATUS_TIMEOUT_WRITE)) {
449 host->req->cmd->error = -ETIMEDOUT;
450 data->error = -ETIMEDOUT;
451 } else {
452 host->req->cmd->error = -EIO;
453 data->error = -EIO;
454 }
Paul Cercueil8a489aa2013-06-09 21:10:02 +0200455 } else if (status & JZ_MMC_STATUS_READ_ERROR_MASK) {
456 if (status & (JZ_MMC_STATUS_TIMEOUT_READ)) {
457 host->req->cmd->error = -ETIMEDOUT;
458 data->error = -ETIMEDOUT;
459 } else {
460 host->req->cmd->error = -EIO;
461 data->error = -EIO;
462 }
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +0000463 }
464}
465
466static bool jz4740_mmc_write_data(struct jz4740_mmc_host *host,
467 struct mmc_data *data)
468{
469 struct sg_mapping_iter *miter = &host->miter;
470 void __iomem *fifo_addr = host->base + JZ_REG_MMC_TXFIFO;
471 uint32_t *buf;
472 bool timeout;
473 size_t i, j;
474
475 while (sg_miter_next(miter)) {
476 buf = miter->addr;
477 i = miter->length / 4;
478 j = i / 8;
479 i = i & 0x7;
480 while (j) {
481 timeout = jz4740_mmc_poll_irq(host, JZ_MMC_IRQ_TXFIFO_WR_REQ);
482 if (unlikely(timeout))
483 goto poll_timeout;
484
485 writel(buf[0], fifo_addr);
486 writel(buf[1], fifo_addr);
487 writel(buf[2], fifo_addr);
488 writel(buf[3], fifo_addr);
489 writel(buf[4], fifo_addr);
490 writel(buf[5], fifo_addr);
491 writel(buf[6], fifo_addr);
492 writel(buf[7], fifo_addr);
493 buf += 8;
494 --j;
495 }
496 if (unlikely(i)) {
497 timeout = jz4740_mmc_poll_irq(host, JZ_MMC_IRQ_TXFIFO_WR_REQ);
498 if (unlikely(timeout))
499 goto poll_timeout;
500
501 while (i) {
502 writel(*buf, fifo_addr);
503 ++buf;
504 --i;
505 }
506 }
507 data->bytes_xfered += miter->length;
508 }
509 sg_miter_stop(miter);
510
511 return false;
512
513poll_timeout:
514 miter->consumed = (void *)buf - miter->addr;
515 data->bytes_xfered += miter->consumed;
516 sg_miter_stop(miter);
517
518 return true;
519}
520
521static bool jz4740_mmc_read_data(struct jz4740_mmc_host *host,
522 struct mmc_data *data)
523{
524 struct sg_mapping_iter *miter = &host->miter;
525 void __iomem *fifo_addr = host->base + JZ_REG_MMC_RXFIFO;
526 uint32_t *buf;
527 uint32_t d;
528 uint16_t status;
529 size_t i, j;
530 unsigned int timeout;
531
532 while (sg_miter_next(miter)) {
533 buf = miter->addr;
534 i = miter->length;
535 j = i / 32;
536 i = i & 0x1f;
537 while (j) {
538 timeout = jz4740_mmc_poll_irq(host, JZ_MMC_IRQ_RXFIFO_RD_REQ);
539 if (unlikely(timeout))
540 goto poll_timeout;
541
542 buf[0] = readl(fifo_addr);
543 buf[1] = readl(fifo_addr);
544 buf[2] = readl(fifo_addr);
545 buf[3] = readl(fifo_addr);
546 buf[4] = readl(fifo_addr);
547 buf[5] = readl(fifo_addr);
548 buf[6] = readl(fifo_addr);
549 buf[7] = readl(fifo_addr);
550
551 buf += 8;
552 --j;
553 }
554
555 if (unlikely(i)) {
556 timeout = jz4740_mmc_poll_irq(host, JZ_MMC_IRQ_RXFIFO_RD_REQ);
557 if (unlikely(timeout))
558 goto poll_timeout;
559
560 while (i >= 4) {
561 *buf++ = readl(fifo_addr);
562 i -= 4;
563 }
564 if (unlikely(i > 0)) {
565 d = readl(fifo_addr);
566 memcpy(buf, &d, i);
567 }
568 }
569 data->bytes_xfered += miter->length;
570
571 /* This can go away once MIPS implements
572 * flush_kernel_dcache_page */
573 flush_dcache_page(miter->page);
574 }
575 sg_miter_stop(miter);
576
577 /* For whatever reason there is sometime one word more in the fifo then
578 * requested */
579 timeout = 1000;
580 status = readl(host->base + JZ_REG_MMC_STATUS);
581 while (!(status & JZ_MMC_STATUS_DATA_FIFO_EMPTY) && --timeout) {
582 d = readl(fifo_addr);
583 status = readl(host->base + JZ_REG_MMC_STATUS);
584 }
585
586 return false;
587
588poll_timeout:
589 miter->consumed = (void *)buf - miter->addr;
590 data->bytes_xfered += miter->consumed;
591 sg_miter_stop(miter);
592
593 return true;
594}
595
Kees Cook2ee4f622017-10-24 08:03:45 -0700596static void jz4740_mmc_timeout(struct timer_list *t)
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +0000597{
Kees Cook2ee4f622017-10-24 08:03:45 -0700598 struct jz4740_mmc_host *host = from_timer(host, t, timeout_timer);
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +0000599
600 if (!test_and_clear_bit(0, &host->waiting))
601 return;
602
603 jz4740_mmc_set_irq_enabled(host, JZ_MMC_IRQ_END_CMD_RES, false);
604
605 host->req->cmd->error = -ETIMEDOUT;
606 jz4740_mmc_request_done(host);
607}
608
609static void jz4740_mmc_read_response(struct jz4740_mmc_host *host,
610 struct mmc_command *cmd)
611{
612 int i;
613 uint16_t tmp;
614 void __iomem *fifo_addr = host->base + JZ_REG_MMC_RESP_FIFO;
615
616 if (cmd->flags & MMC_RSP_136) {
617 tmp = readw(fifo_addr);
618 for (i = 0; i < 4; ++i) {
619 cmd->resp[i] = tmp << 24;
620 tmp = readw(fifo_addr);
621 cmd->resp[i] |= tmp << 8;
622 tmp = readw(fifo_addr);
623 cmd->resp[i] |= tmp >> 8;
624 }
625 } else {
626 cmd->resp[0] = readw(fifo_addr) << 24;
627 cmd->resp[0] |= readw(fifo_addr) << 8;
628 cmd->resp[0] |= readw(fifo_addr) & 0xff;
629 }
630}
631
632static void jz4740_mmc_send_command(struct jz4740_mmc_host *host,
633 struct mmc_command *cmd)
634{
635 uint32_t cmdat = host->cmdat;
636
637 host->cmdat &= ~JZ_MMC_CMDAT_INIT;
638 jz4740_mmc_clock_disable(host);
639
640 host->cmd = cmd;
641
642 if (cmd->flags & MMC_RSP_BUSY)
643 cmdat |= JZ_MMC_CMDAT_BUSY;
644
645 switch (mmc_resp_type(cmd)) {
646 case MMC_RSP_R1B:
647 case MMC_RSP_R1:
648 cmdat |= JZ_MMC_CMDAT_RSP_R1;
649 break;
650 case MMC_RSP_R2:
651 cmdat |= JZ_MMC_CMDAT_RSP_R2;
652 break;
653 case MMC_RSP_R3:
654 cmdat |= JZ_MMC_CMDAT_RSP_R3;
655 break;
656 default:
657 break;
658 }
659
660 if (cmd->data) {
661 cmdat |= JZ_MMC_CMDAT_DATA_EN;
662 if (cmd->data->flags & MMC_DATA_WRITE)
663 cmdat |= JZ_MMC_CMDAT_WRITE;
Apelete Seketeli7ca27a62014-07-21 06:37:44 +0200664 if (host->use_dma)
665 cmdat |= JZ_MMC_CMDAT_DMA_EN;
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +0000666
667 writew(cmd->data->blksz, host->base + JZ_REG_MMC_BLKLEN);
668 writew(cmd->data->blocks, host->base + JZ_REG_MMC_NOB);
669 }
670
671 writeb(cmd->opcode, host->base + JZ_REG_MMC_CMD);
672 writel(cmd->arg, host->base + JZ_REG_MMC_ARG);
673 writel(cmdat, host->base + JZ_REG_MMC_CMDAT);
674
675 jz4740_mmc_clock_enable(host, 1);
676}
677
678static void jz_mmc_prepare_data_transfer(struct jz4740_mmc_host *host)
679{
680 struct mmc_command *cmd = host->req->cmd;
681 struct mmc_data *data = cmd->data;
682 int direction;
683
684 if (data->flags & MMC_DATA_READ)
685 direction = SG_MITER_TO_SG;
686 else
687 direction = SG_MITER_FROM_SG;
688
689 sg_miter_start(&host->miter, data->sg, data->sg_len, direction);
690}
691
692
693static irqreturn_t jz_mmc_irq_worker(int irq, void *devid)
694{
695 struct jz4740_mmc_host *host = (struct jz4740_mmc_host *)devid;
696 struct mmc_command *cmd = host->req->cmd;
697 struct mmc_request *req = host->req;
Apelete Seketeli7ca27a62014-07-21 06:37:44 +0200698 struct mmc_data *data = cmd->data;
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +0000699 bool timeout = false;
700
701 if (cmd->error)
702 host->state = JZ4740_MMC_STATE_DONE;
703
704 switch (host->state) {
705 case JZ4740_MMC_STATE_READ_RESPONSE:
706 if (cmd->flags & MMC_RSP_PRESENT)
707 jz4740_mmc_read_response(host, cmd);
708
Apelete Seketeli7ca27a62014-07-21 06:37:44 +0200709 if (!data)
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +0000710 break;
711
712 jz_mmc_prepare_data_transfer(host);
713
714 case JZ4740_MMC_STATE_TRANSFER_DATA:
Apelete Seketeli7ca27a62014-07-21 06:37:44 +0200715 if (host->use_dma) {
Apelete Seketelibb2f4592014-07-21 06:37:45 +0200716 /* Use DMA if enabled.
717 * Data transfer direction is defined later by
718 * relying on data flags in
719 * jz4740_mmc_prepare_dma_data() and
720 * jz4740_mmc_start_dma_transfer().
Apelete Seketeli7ca27a62014-07-21 06:37:44 +0200721 */
722 timeout = jz4740_mmc_start_dma_transfer(host, data);
723 data->bytes_xfered = data->blocks * data->blksz;
724 } else if (data->flags & MMC_DATA_READ)
Apelete Seketelibb2f4592014-07-21 06:37:45 +0200725 /* Use PIO if DMA is not enabled.
726 * Data transfer direction was defined before
727 * by relying on data flags in
728 * jz_mmc_prepare_data_transfer().
Apelete Seketeli7ca27a62014-07-21 06:37:44 +0200729 */
730 timeout = jz4740_mmc_read_data(host, data);
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +0000731 else
Apelete Seketeli7ca27a62014-07-21 06:37:44 +0200732 timeout = jz4740_mmc_write_data(host, data);
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +0000733
734 if (unlikely(timeout)) {
735 host->state = JZ4740_MMC_STATE_TRANSFER_DATA;
736 break;
737 }
738
Apelete Seketeli7ca27a62014-07-21 06:37:44 +0200739 jz4740_mmc_transfer_check_state(host, data);
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +0000740
741 timeout = jz4740_mmc_poll_irq(host, JZ_MMC_IRQ_DATA_TRAN_DONE);
742 if (unlikely(timeout)) {
743 host->state = JZ4740_MMC_STATE_SEND_STOP;
744 break;
745 }
746 writew(JZ_MMC_IRQ_DATA_TRAN_DONE, host->base + JZ_REG_MMC_IREG);
747
748 case JZ4740_MMC_STATE_SEND_STOP:
749 if (!req->stop)
750 break;
751
752 jz4740_mmc_send_command(host, req->stop);
753
Alex Smith1acee842014-04-29 13:54:54 +0100754 if (mmc_resp_type(req->stop) & MMC_RSP_BUSY) {
755 timeout = jz4740_mmc_poll_irq(host,
756 JZ_MMC_IRQ_PRG_DONE);
757 if (timeout) {
758 host->state = JZ4740_MMC_STATE_DONE;
759 break;
760 }
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +0000761 }
762 case JZ4740_MMC_STATE_DONE:
763 break;
764 }
765
766 if (!timeout)
767 jz4740_mmc_request_done(host);
768
769 return IRQ_HANDLED;
770}
771
772static irqreturn_t jz_mmc_irq(int irq, void *devid)
773{
774 struct jz4740_mmc_host *host = devid;
775 struct mmc_command *cmd = host->cmd;
776 uint16_t irq_reg, status, tmp;
777
778 irq_reg = readw(host->base + JZ_REG_MMC_IREG);
779
780 tmp = irq_reg;
781 irq_reg &= ~host->irq_mask;
782
783 tmp &= ~(JZ_MMC_IRQ_TXFIFO_WR_REQ | JZ_MMC_IRQ_RXFIFO_RD_REQ |
784 JZ_MMC_IRQ_PRG_DONE | JZ_MMC_IRQ_DATA_TRAN_DONE);
785
786 if (tmp != irq_reg)
787 writew(tmp & ~irq_reg, host->base + JZ_REG_MMC_IREG);
788
789 if (irq_reg & JZ_MMC_IRQ_SDIO) {
790 writew(JZ_MMC_IRQ_SDIO, host->base + JZ_REG_MMC_IREG);
791 mmc_signal_sdio_irq(host->mmc);
792 irq_reg &= ~JZ_MMC_IRQ_SDIO;
793 }
794
795 if (host->req && cmd && irq_reg) {
796 if (test_and_clear_bit(0, &host->waiting)) {
797 del_timer(&host->timeout_timer);
798
799 status = readl(host->base + JZ_REG_MMC_STATUS);
800
801 if (status & JZ_MMC_STATUS_TIMEOUT_RES) {
802 cmd->error = -ETIMEDOUT;
803 } else if (status & JZ_MMC_STATUS_CRC_RES_ERR) {
804 cmd->error = -EIO;
805 } else if (status & (JZ_MMC_STATUS_CRC_READ_ERROR |
806 JZ_MMC_STATUS_CRC_WRITE_ERROR)) {
807 if (cmd->data)
808 cmd->data->error = -EIO;
809 cmd->error = -EIO;
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +0000810 }
811
812 jz4740_mmc_set_irq_enabled(host, irq_reg, false);
813 writew(irq_reg, host->base + JZ_REG_MMC_IREG);
814
815 return IRQ_WAKE_THREAD;
816 }
817 }
818
819 return IRQ_HANDLED;
820}
821
822static int jz4740_mmc_set_clock_rate(struct jz4740_mmc_host *host, int rate)
823{
824 int div = 0;
825 int real_rate;
826
827 jz4740_mmc_clock_disable(host);
Alex Smith6861fce2018-03-28 18:00:50 -0300828 clk_set_rate(host->clk, host->mmc->f_max);
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +0000829
830 real_rate = clk_get_rate(host->clk);
831
832 while (real_rate > rate && div < 7) {
833 ++div;
834 real_rate >>= 1;
835 }
836
837 writew(div, host->base + JZ_REG_MMC_CLKRT);
838 return real_rate;
839}
840
841static void jz4740_mmc_request(struct mmc_host *mmc, struct mmc_request *req)
842{
843 struct jz4740_mmc_host *host = mmc_priv(mmc);
844
845 host->req = req;
846
847 writew(0xffff, host->base + JZ_REG_MMC_IREG);
848
849 writew(JZ_MMC_IRQ_END_CMD_RES, host->base + JZ_REG_MMC_IREG);
850 jz4740_mmc_set_irq_enabled(host, JZ_MMC_IRQ_END_CMD_RES, true);
851
852 host->state = JZ4740_MMC_STATE_READ_RESPONSE;
853 set_bit(0, &host->waiting);
854 mod_timer(&host->timeout_timer, jiffies + 5*HZ);
855 jz4740_mmc_send_command(host, req->cmd);
856}
857
858static void jz4740_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
859{
860 struct jz4740_mmc_host *host = mmc_priv(mmc);
861 if (ios->clock)
862 jz4740_mmc_set_clock_rate(host, ios->clock);
863
864 switch (ios->power_mode) {
865 case MMC_POWER_UP:
866 jz4740_mmc_reset(host);
Ezequiel Garcia61e11eb2018-03-28 18:00:48 -0300867 if (host->pdata && gpio_is_valid(host->pdata->gpio_power))
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +0000868 gpio_set_value(host->pdata->gpio_power,
869 !host->pdata->power_active_low);
870 host->cmdat |= JZ_MMC_CMDAT_INIT;
Lars-Peter Clausenfca96612013-05-12 20:12:38 +0200871 clk_prepare_enable(host->clk);
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +0000872 break;
873 case MMC_POWER_ON:
874 break;
875 default:
Ezequiel Garcia61e11eb2018-03-28 18:00:48 -0300876 if (host->pdata && gpio_is_valid(host->pdata->gpio_power))
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +0000877 gpio_set_value(host->pdata->gpio_power,
878 host->pdata->power_active_low);
Lars-Peter Clausenfca96612013-05-12 20:12:38 +0200879 clk_disable_unprepare(host->clk);
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +0000880 break;
881 }
882
883 switch (ios->bus_width) {
884 case MMC_BUS_WIDTH_1:
885 host->cmdat &= ~JZ_MMC_CMDAT_BUS_WIDTH_4BIT;
886 break;
887 case MMC_BUS_WIDTH_4:
888 host->cmdat |= JZ_MMC_CMDAT_BUS_WIDTH_4BIT;
889 break;
890 default:
891 break;
892 }
893}
894
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +0000895static void jz4740_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
896{
897 struct jz4740_mmc_host *host = mmc_priv(mmc);
898 jz4740_mmc_set_irq_enabled(host, JZ_MMC_IRQ_SDIO, enable);
899}
900
901static const struct mmc_host_ops jz4740_mmc_ops = {
902 .request = jz4740_mmc_request,
Apelete Seketelibb2f4592014-07-21 06:37:45 +0200903 .pre_req = jz4740_mmc_pre_request,
904 .post_req = jz4740_mmc_post_request,
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +0000905 .set_ios = jz4740_mmc_set_ios,
Lars-Peter Clausen58e300a2013-06-09 21:10:04 +0200906 .get_ro = mmc_gpio_get_ro,
907 .get_cd = mmc_gpio_get_cd,
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +0000908 .enable_sdio_irq = jz4740_mmc_enable_sdio_irq,
909};
910
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500911static int jz4740_mmc_request_gpio(struct device *dev, int gpio,
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +0000912 const char *name, bool output, int value)
913{
914 int ret;
915
916 if (!gpio_is_valid(gpio))
917 return 0;
918
919 ret = gpio_request(gpio, name);
920 if (ret) {
921 dev_err(dev, "Failed to request %s gpio: %d\n", name, ret);
922 return ret;
923 }
924
925 if (output)
926 gpio_direction_output(gpio, value);
927 else
928 gpio_direction_input(gpio);
929
930 return 0;
931}
932
Lars-Peter Clausen58e300a2013-06-09 21:10:04 +0200933static int jz4740_mmc_request_gpios(struct mmc_host *mmc,
934 struct platform_device *pdev)
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +0000935{
Ezequiel Garcia39e9ef12018-03-28 18:00:46 -0300936 struct jz4740_mmc_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lars-Peter Clausen58e300a2013-06-09 21:10:04 +0200937 int ret = 0;
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +0000938
939 if (!pdata)
940 return 0;
941
Lars-Peter Clausen58e300a2013-06-09 21:10:04 +0200942 if (!pdata->card_detect_active_low)
943 mmc->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
944 if (!pdata->read_only_active_low)
945 mmc->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +0000946
Lars-Peter Clausen58e300a2013-06-09 21:10:04 +0200947 if (gpio_is_valid(pdata->gpio_card_detect)) {
Laurent Pinchart214fc302013-08-08 12:38:31 +0200948 ret = mmc_gpio_request_cd(mmc, pdata->gpio_card_detect, 0);
Lars-Peter Clausen58e300a2013-06-09 21:10:04 +0200949 if (ret)
950 return ret;
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +0000951 }
952
Lars-Peter Clausen58e300a2013-06-09 21:10:04 +0200953 if (gpio_is_valid(pdata->gpio_read_only)) {
954 ret = mmc_gpio_request_ro(mmc, pdata->gpio_read_only);
955 if (ret)
956 return ret;
957 }
958
959 return jz4740_mmc_request_gpio(&pdev->dev, pdata->gpio_power,
960 "MMC read only", true, pdata->power_active_low);
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +0000961}
962
963static void jz4740_mmc_free_gpios(struct platform_device *pdev)
964{
Ezequiel Garcia39e9ef12018-03-28 18:00:46 -0300965 struct jz4740_mmc_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +0000966
967 if (!pdata)
968 return;
969
970 if (gpio_is_valid(pdata->gpio_power))
971 gpio_free(pdata->gpio_power);
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +0000972}
973
Ezequiel Garcia61e11eb2018-03-28 18:00:48 -0300974static const struct of_device_id jz4740_mmc_of_match[] = {
975 { .compatible = "ingenic,jz4740-mmc", .data = (void *) JZ_MMC_JZ4740 },
976 {},
977};
978MODULE_DEVICE_TABLE(of, jz4740_mmc_of_match);
979
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500980static int jz4740_mmc_probe(struct platform_device* pdev)
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +0000981{
982 int ret;
983 struct mmc_host *mmc;
984 struct jz4740_mmc_host *host;
Ezequiel Garcia61e11eb2018-03-28 18:00:48 -0300985 const struct of_device_id *match;
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +0000986 struct jz4740_mmc_platform_data *pdata;
987
Ezequiel Garcia39e9ef12018-03-28 18:00:46 -0300988 pdata = dev_get_platdata(&pdev->dev);
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +0000989
990 mmc = mmc_alloc_host(sizeof(struct jz4740_mmc_host), &pdev->dev);
991 if (!mmc) {
992 dev_err(&pdev->dev, "Failed to alloc mmc host structure\n");
993 return -ENOMEM;
994 }
995
996 host = mmc_priv(mmc);
997 host->pdata = pdata;
998
Ezequiel Garcia61e11eb2018-03-28 18:00:48 -0300999 match = of_match_device(jz4740_mmc_of_match, &pdev->dev);
1000 if (match) {
1001 host->version = (enum jz4740_mmc_version)match->data;
1002 ret = mmc_of_parse(mmc);
1003 if (ret) {
1004 if (ret != -EPROBE_DEFER)
1005 dev_err(&pdev->dev,
1006 "could not parse of data: %d\n", ret);
1007 goto err_free_host;
1008 }
1009 } else {
1010 /* JZ4740 should be the only one using legacy probe */
1011 host->version = JZ_MMC_JZ4740;
1012 mmc->caps |= MMC_CAP_SDIO_IRQ;
1013 if (!(pdata && pdata->data_1bit))
1014 mmc->caps |= MMC_CAP_4_BIT_DATA;
1015 ret = jz4740_mmc_request_gpios(mmc, pdev);
1016 if (ret)
1017 goto err_free_host;
1018 }
1019
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +00001020 host->irq = platform_get_irq(pdev, 0);
1021 if (host->irq < 0) {
1022 ret = host->irq;
1023 dev_err(&pdev->dev, "Failed to get platform irq: %d\n", ret);
1024 goto err_free_host;
1025 }
1026
Lars-Peter Clausen017d84b2013-06-09 21:10:05 +02001027 host->clk = devm_clk_get(&pdev->dev, "mmc");
Jamie Iles3119cbd2011-01-11 12:43:50 +00001028 if (IS_ERR(host->clk)) {
1029 ret = PTR_ERR(host->clk);
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +00001030 dev_err(&pdev->dev, "Failed to get mmc clock\n");
1031 goto err_free_host;
1032 }
1033
Apelete Seketeli7ca27a62014-07-21 06:37:44 +02001034 host->mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1035 host->base = devm_ioremap_resource(&pdev->dev, host->mem_res);
Wei Yongjun3e7e8c12013-06-29 08:44:53 +08001036 if (IS_ERR(host->base)) {
1037 ret = PTR_ERR(host->base);
Apelete Seketeli7ca27a62014-07-21 06:37:44 +02001038 dev_err(&pdev->dev, "Failed to ioremap base memory\n");
Lars-Peter Clausen017d84b2013-06-09 21:10:05 +02001039 goto err_free_host;
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +00001040 }
1041
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +00001042 mmc->ops = &jz4740_mmc_ops;
Ezequiel Garcia61e11eb2018-03-28 18:00:48 -03001043 if (!mmc->f_max)
1044 mmc->f_max = JZ_MMC_CLK_RATE;
1045 mmc->f_min = mmc->f_max / 128;
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +00001046 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +00001047
1048 mmc->max_blk_size = (1 << 10) - 1;
1049 mmc->max_blk_count = (1 << 15) - 1;
1050 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1051
Martin K. Petersena36274e2010-09-10 01:33:59 -04001052 mmc->max_segs = 128;
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +00001053 mmc->max_seg_size = mmc->max_req_size;
1054
1055 host->mmc = mmc;
1056 host->pdev = pdev;
1057 spin_lock_init(&host->lock);
1058 host->irq_mask = 0xffff;
1059
Zubair Lutfullah Kakakhel436a3cf2018-03-28 18:00:47 -03001060 jz4740_mmc_reset(host);
1061
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +00001062 ret = request_threaded_irq(host->irq, jz_mmc_irq, jz_mmc_irq_worker, 0,
1063 dev_name(&pdev->dev), host);
1064 if (ret) {
1065 dev_err(&pdev->dev, "Failed to request irq: %d\n", ret);
Lars-Peter Clausen58e300a2013-06-09 21:10:04 +02001066 goto err_free_gpios;
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +00001067 }
1068
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +00001069 jz4740_mmc_clock_disable(host);
Kees Cook2ee4f622017-10-24 08:03:45 -07001070 timer_setup(&host->timeout_timer, jz4740_mmc_timeout, 0);
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +00001071
Paul Cercueil7e8466e2018-03-28 18:00:44 -03001072 ret = jz4740_mmc_acquire_dma_channels(host);
1073 if (ret == -EPROBE_DEFER)
1074 goto err_free_irq;
1075 host->use_dma = !ret;
Apelete Seketeli7ca27a62014-07-21 06:37:44 +02001076
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +00001077 platform_set_drvdata(pdev, host);
1078 ret = mmc_add_host(mmc);
1079
1080 if (ret) {
1081 dev_err(&pdev->dev, "Failed to add mmc host: %d\n", ret);
Paul Cercueil7e8466e2018-03-28 18:00:44 -03001082 goto err_release_dma;
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +00001083 }
1084 dev_info(&pdev->dev, "JZ SD/MMC card driver registered\n");
1085
Apelete Seketeli7ca27a62014-07-21 06:37:44 +02001086 dev_info(&pdev->dev, "Using %s, %d-bit mode\n",
1087 host->use_dma ? "DMA" : "PIO",
1088 (mmc->caps & MMC_CAP_4_BIT_DATA) ? 4 : 1);
1089
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +00001090 return 0;
1091
Paul Cercueil7e8466e2018-03-28 18:00:44 -03001092err_release_dma:
1093 if (host->use_dma)
1094 jz4740_mmc_release_dma_channels(host);
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +00001095err_free_irq:
1096 free_irq(host->irq, host);
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +00001097err_free_gpios:
1098 jz4740_mmc_free_gpios(pdev);
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +00001099err_free_host:
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +00001100 mmc_free_host(mmc);
1101
1102 return ret;
1103}
1104
Bill Pemberton6e0ee712012-11-19 13:26:03 -05001105static int jz4740_mmc_remove(struct platform_device *pdev)
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +00001106{
1107 struct jz4740_mmc_host *host = platform_get_drvdata(pdev);
1108
1109 del_timer_sync(&host->timeout_timer);
1110 jz4740_mmc_set_irq_enabled(host, 0xff, false);
1111 jz4740_mmc_reset(host);
1112
1113 mmc_remove_host(host->mmc);
1114
1115 free_irq(host->irq, host);
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +00001116
1117 jz4740_mmc_free_gpios(pdev);
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +00001118
Apelete Seketeli7ca27a62014-07-21 06:37:44 +02001119 if (host->use_dma)
1120 jz4740_mmc_release_dma_channels(host);
1121
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +00001122 mmc_free_host(host->mmc);
1123
1124 return 0;
1125}
1126
Lars-Peter Clausen5d5c0352013-06-09 21:10:03 +02001127#ifdef CONFIG_PM_SLEEP
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +00001128
1129static int jz4740_mmc_suspend(struct device *dev)
1130{
Paul Cercueilfa5ed6b2017-05-12 18:53:03 +02001131 return pinctrl_pm_select_sleep_state(dev);
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +00001132}
1133
1134static int jz4740_mmc_resume(struct device *dev)
1135{
Paul Cercueilfa5ed6b2017-05-12 18:53:03 +02001136 return pinctrl_pm_select_default_state(dev);
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +00001137}
1138
Lars-Peter Clausen5d5c0352013-06-09 21:10:03 +02001139static SIMPLE_DEV_PM_OPS(jz4740_mmc_pm_ops, jz4740_mmc_suspend,
1140 jz4740_mmc_resume);
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +00001141#define JZ4740_MMC_PM_OPS (&jz4740_mmc_pm_ops)
1142#else
1143#define JZ4740_MMC_PM_OPS NULL
1144#endif
1145
1146static struct platform_driver jz4740_mmc_driver = {
1147 .probe = jz4740_mmc_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05001148 .remove = jz4740_mmc_remove,
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +00001149 .driver = {
1150 .name = "jz4740-mmc",
Ezequiel Garcia61e11eb2018-03-28 18:00:48 -03001151 .of_match_table = of_match_ptr(jz4740_mmc_of_match),
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +00001152 .pm = JZ4740_MMC_PM_OPS,
1153 },
1154};
1155
Axel Lind1f81a62011-11-26 12:55:43 +08001156module_platform_driver(jz4740_mmc_driver);
Lars-Peter Clausen61bfbdb2010-07-15 20:06:04 +00001157
1158MODULE_DESCRIPTION("JZ4740 SD/MMC controller driver");
1159MODULE_LICENSE("GPL");
1160MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");