mmc: SDHI: add DT compatibility strings for further SoCs
[linux-2.6-block.git] / drivers / mmc / host / sh_mmcif.c
CommitLineData
fdc50a94
YG
1/*
2 * MMCIF eMMC driver.
3 *
4 * Copyright (C) 2010 Renesas Solutions Corp.
5 * Yusuke Goda <yusuke.goda.sx@renesas.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License.
10 *
11 *
12 * TODO
13 * 1. DMA
14 * 2. Power management
15 * 3. Handle MMC errors better
16 *
17 */
18
f985da17
GL
19/*
20 * The MMCIF driver is now processing MMC requests asynchronously, according
21 * to the Linux MMC API requirement.
22 *
23 * The MMCIF driver processes MMC requests in up to 3 stages: command, optional
24 * data, and optional stop. To achieve asynchronous processing each of these
25 * stages is split into two halves: a top and a bottom half. The top half
26 * initialises the hardware, installs a timeout handler to handle completion
27 * timeouts, and returns. In case of the command stage this immediately returns
28 * control to the caller, leaving all further processing to run asynchronously.
29 * All further request processing is performed by the bottom halves.
30 *
31 * The bottom half further consists of a "hard" IRQ handler, an IRQ handler
32 * thread, a DMA completion callback, if DMA is used, a timeout work, and
33 * request- and stage-specific handler methods.
34 *
35 * Each bottom half run begins with either a hardware interrupt, a DMA callback
36 * invocation, or a timeout work run. In case of an error or a successful
37 * processing completion, the MMC core is informed and the request processing is
38 * finished. In case processing has to continue, i.e., if data has to be read
39 * from or written to the card, or if a stop command has to be sent, the next
40 * top half is called, which performs the necessary hardware handling and
41 * reschedules the timeout work. This returns the driver state machine into the
42 * bottom half waiting state.
43 */
44
86df1745 45#include <linux/bitops.h>
aa0787a9
GL
46#include <linux/clk.h>
47#include <linux/completion.h>
e47bf32a 48#include <linux/delay.h>
fdc50a94 49#include <linux/dma-mapping.h>
a782d688 50#include <linux/dmaengine.h>
fdc50a94
YG
51#include <linux/mmc/card.h>
52#include <linux/mmc/core.h>
e47bf32a 53#include <linux/mmc/host.h>
fdc50a94
YG
54#include <linux/mmc/mmc.h>
55#include <linux/mmc/sdio.h>
fdc50a94 56#include <linux/mmc/sh_mmcif.h>
e480606a 57#include <linux/mmc/slot-gpio.h>
bf68a812 58#include <linux/mod_devicetable.h>
8047310e 59#include <linux/mutex.h>
a782d688 60#include <linux/pagemap.h>
e47bf32a 61#include <linux/platform_device.h>
efe6a8ad 62#include <linux/pm_qos.h>
faca6648 63#include <linux/pm_runtime.h>
3b0beafc 64#include <linux/spinlock.h>
88b47679 65#include <linux/module.h>
fdc50a94
YG
66
67#define DRIVER_NAME "sh_mmcif"
68#define DRIVER_VERSION "2010-04-28"
69
fdc50a94
YG
70/* CE_CMD_SET */
71#define CMD_MASK 0x3f000000
72#define CMD_SET_RTYP_NO ((0 << 23) | (0 << 22))
73#define CMD_SET_RTYP_6B ((0 << 23) | (1 << 22)) /* R1/R1b/R3/R4/R5 */
74#define CMD_SET_RTYP_17B ((1 << 23) | (0 << 22)) /* R2 */
75#define CMD_SET_RBSY (1 << 21) /* R1b */
76#define CMD_SET_CCSEN (1 << 20)
77#define CMD_SET_WDAT (1 << 19) /* 1: on data, 0: no data */
78#define CMD_SET_DWEN (1 << 18) /* 1: write, 0: read */
79#define CMD_SET_CMLTE (1 << 17) /* 1: multi block trans, 0: single */
80#define CMD_SET_CMD12EN (1 << 16) /* 1: CMD12 auto issue */
81#define CMD_SET_RIDXC_INDEX ((0 << 15) | (0 << 14)) /* index check */
82#define CMD_SET_RIDXC_BITS ((0 << 15) | (1 << 14)) /* check bits check */
83#define CMD_SET_RIDXC_NO ((1 << 15) | (0 << 14)) /* no check */
84#define CMD_SET_CRC7C ((0 << 13) | (0 << 12)) /* CRC7 check*/
85#define CMD_SET_CRC7C_BITS ((0 << 13) | (1 << 12)) /* check bits check*/
86#define CMD_SET_CRC7C_INTERNAL ((1 << 13) | (0 << 12)) /* internal CRC7 check*/
87#define CMD_SET_CRC16C (1 << 10) /* 0: CRC16 check*/
88#define CMD_SET_CRCSTE (1 << 8) /* 1: not receive CRC status */
89#define CMD_SET_TBIT (1 << 7) /* 1: tran mission bit "Low" */
90#define CMD_SET_OPDM (1 << 6) /* 1: open/drain */
91#define CMD_SET_CCSH (1 << 5)
555061f9 92#define CMD_SET_DARS (1 << 2) /* Dual Data Rate */
fdc50a94
YG
93#define CMD_SET_DATW_1 ((0 << 1) | (0 << 0)) /* 1bit */
94#define CMD_SET_DATW_4 ((0 << 1) | (1 << 0)) /* 4bit */
95#define CMD_SET_DATW_8 ((1 << 1) | (0 << 0)) /* 8bit */
96
97/* CE_CMD_CTRL */
98#define CMD_CTRL_BREAK (1 << 0)
99
100/* CE_BLOCK_SET */
101#define BLOCK_SIZE_MASK 0x0000ffff
102
fdc50a94
YG
103/* CE_INT */
104#define INT_CCSDE (1 << 29)
105#define INT_CMD12DRE (1 << 26)
106#define INT_CMD12RBE (1 << 25)
107#define INT_CMD12CRE (1 << 24)
108#define INT_DTRANE (1 << 23)
109#define INT_BUFRE (1 << 22)
110#define INT_BUFWEN (1 << 21)
111#define INT_BUFREN (1 << 20)
112#define INT_CCSRCV (1 << 19)
113#define INT_RBSYE (1 << 17)
114#define INT_CRSPE (1 << 16)
115#define INT_CMDVIO (1 << 15)
116#define INT_BUFVIO (1 << 14)
117#define INT_WDATERR (1 << 11)
118#define INT_RDATERR (1 << 10)
119#define INT_RIDXERR (1 << 9)
120#define INT_RSPERR (1 << 8)
121#define INT_CCSTO (1 << 5)
122#define INT_CRCSTO (1 << 4)
123#define INT_WDATTO (1 << 3)
124#define INT_RDATTO (1 << 2)
125#define INT_RBSYTO (1 << 1)
126#define INT_RSPTO (1 << 0)
127#define INT_ERR_STS (INT_CMDVIO | INT_BUFVIO | INT_WDATERR | \
128 INT_RDATERR | INT_RIDXERR | INT_RSPERR | \
129 INT_CCSTO | INT_CRCSTO | INT_WDATTO | \
130 INT_RDATTO | INT_RBSYTO | INT_RSPTO)
131
8af50750
GL
132#define INT_ALL (INT_RBSYE | INT_CRSPE | INT_BUFREN | \
133 INT_BUFWEN | INT_CMD12DRE | INT_BUFRE | \
134 INT_DTRANE | INT_CMD12RBE | INT_CMD12CRE)
135
fdc50a94
YG
136/* CE_INT_MASK */
137#define MASK_ALL 0x00000000
138#define MASK_MCCSDE (1 << 29)
139#define MASK_MCMD12DRE (1 << 26)
140#define MASK_MCMD12RBE (1 << 25)
141#define MASK_MCMD12CRE (1 << 24)
142#define MASK_MDTRANE (1 << 23)
143#define MASK_MBUFRE (1 << 22)
144#define MASK_MBUFWEN (1 << 21)
145#define MASK_MBUFREN (1 << 20)
146#define MASK_MCCSRCV (1 << 19)
147#define MASK_MRBSYE (1 << 17)
148#define MASK_MCRSPE (1 << 16)
149#define MASK_MCMDVIO (1 << 15)
150#define MASK_MBUFVIO (1 << 14)
151#define MASK_MWDATERR (1 << 11)
152#define MASK_MRDATERR (1 << 10)
153#define MASK_MRIDXERR (1 << 9)
154#define MASK_MRSPERR (1 << 8)
155#define MASK_MCCSTO (1 << 5)
156#define MASK_MCRCSTO (1 << 4)
157#define MASK_MWDATTO (1 << 3)
158#define MASK_MRDATTO (1 << 2)
159#define MASK_MRBSYTO (1 << 1)
160#define MASK_MRSPTO (1 << 0)
161
ee4b8887
GL
162#define MASK_START_CMD (MASK_MCMDVIO | MASK_MBUFVIO | MASK_MWDATERR | \
163 MASK_MRDATERR | MASK_MRIDXERR | MASK_MRSPERR | \
164 MASK_MCCSTO | MASK_MCRCSTO | MASK_MWDATTO | \
165 MASK_MRDATTO | MASK_MRBSYTO | MASK_MRSPTO)
166
8af50750
GL
167#define MASK_CLEAN (INT_ERR_STS | MASK_MRBSYE | MASK_MCRSPE | \
168 MASK_MBUFREN | MASK_MBUFWEN | \
169 MASK_MCMD12DRE | MASK_MBUFRE | MASK_MDTRANE | \
170 MASK_MCMD12RBE | MASK_MCMD12CRE)
171
fdc50a94
YG
172/* CE_HOST_STS1 */
173#define STS1_CMDSEQ (1 << 31)
174
175/* CE_HOST_STS2 */
176#define STS2_CRCSTE (1 << 31)
177#define STS2_CRC16E (1 << 30)
178#define STS2_AC12CRCE (1 << 29)
179#define STS2_RSPCRC7E (1 << 28)
180#define STS2_CRCSTEBE (1 << 27)
181#define STS2_RDATEBE (1 << 26)
182#define STS2_AC12REBE (1 << 25)
183#define STS2_RSPEBE (1 << 24)
184#define STS2_AC12IDXE (1 << 23)
185#define STS2_RSPIDXE (1 << 22)
186#define STS2_CCSTO (1 << 15)
187#define STS2_RDATTO (1 << 14)
188#define STS2_DATBSYTO (1 << 13)
189#define STS2_CRCSTTO (1 << 12)
190#define STS2_AC12BSYTO (1 << 11)
191#define STS2_RSPBSYTO (1 << 10)
192#define STS2_AC12RSPTO (1 << 9)
193#define STS2_RSPTO (1 << 8)
194#define STS2_CRC_ERR (STS2_CRCSTE | STS2_CRC16E | \
195 STS2_AC12CRCE | STS2_RSPCRC7E | STS2_CRCSTEBE)
196#define STS2_TIMEOUT_ERR (STS2_CCSTO | STS2_RDATTO | \
197 STS2_DATBSYTO | STS2_CRCSTTO | \
198 STS2_AC12BSYTO | STS2_RSPBSYTO | \
199 STS2_AC12RSPTO | STS2_RSPTO)
200
fdc50a94
YG
201#define CLKDEV_EMMC_DATA 52000000 /* 52MHz */
202#define CLKDEV_MMC_DATA 20000000 /* 20MHz */
203#define CLKDEV_INIT 400000 /* 400 KHz */
204
3b0beafc
GL
205enum mmcif_state {
206 STATE_IDLE,
207 STATE_REQUEST,
208 STATE_IOS,
8047310e 209 STATE_TIMEOUT,
3b0beafc
GL
210};
211
f985da17
GL
212enum mmcif_wait_for {
213 MMCIF_WAIT_FOR_REQUEST,
214 MMCIF_WAIT_FOR_CMD,
215 MMCIF_WAIT_FOR_MREAD,
216 MMCIF_WAIT_FOR_MWRITE,
217 MMCIF_WAIT_FOR_READ,
218 MMCIF_WAIT_FOR_WRITE,
219 MMCIF_WAIT_FOR_READ_END,
220 MMCIF_WAIT_FOR_WRITE_END,
221 MMCIF_WAIT_FOR_STOP,
222};
223
fdc50a94
YG
224struct sh_mmcif_host {
225 struct mmc_host *mmc;
f985da17 226 struct mmc_request *mrq;
fdc50a94
YG
227 struct platform_device *pd;
228 struct clk *hclk;
229 unsigned int clk;
230 int bus_width;
555061f9 231 unsigned char timing;
aa0787a9 232 bool sd_error;
f985da17 233 bool dying;
fdc50a94
YG
234 long timeout;
235 void __iomem *addr;
f985da17 236 u32 *pio_ptr;
ee4b8887 237 spinlock_t lock; /* protect sh_mmcif_host::state */
3b0beafc 238 enum mmcif_state state;
f985da17
GL
239 enum mmcif_wait_for wait_for;
240 struct delayed_work timeout_work;
241 size_t blocksize;
242 int sg_idx;
243 int sg_blkidx;
faca6648 244 bool power;
c9b0cef2 245 bool card_present;
8047310e 246 struct mutex thread_lock;
fdc50a94 247
a782d688
GL
248 /* DMA support */
249 struct dma_chan *chan_rx;
250 struct dma_chan *chan_tx;
251 struct completion dma_complete;
f38f94c6 252 bool dma_active;
a782d688 253};
fdc50a94
YG
254
255static inline void sh_mmcif_bitset(struct sh_mmcif_host *host,
256 unsigned int reg, u32 val)
257{
487d9fc5 258 writel(val | readl(host->addr + reg), host->addr + reg);
fdc50a94
YG
259}
260
261static inline void sh_mmcif_bitclr(struct sh_mmcif_host *host,
262 unsigned int reg, u32 val)
263{
487d9fc5 264 writel(~val & readl(host->addr + reg), host->addr + reg);
fdc50a94
YG
265}
266
a782d688
GL
267static void mmcif_dma_complete(void *arg)
268{
269 struct sh_mmcif_host *host = arg;
8047310e 270 struct mmc_request *mrq = host->mrq;
69983404 271
a782d688
GL
272 dev_dbg(&host->pd->dev, "Command completed\n");
273
8047310e 274 if (WARN(!mrq || !mrq->data, "%s: NULL data in DMA completion!\n",
a782d688
GL
275 dev_name(&host->pd->dev)))
276 return;
277
a782d688
GL
278 complete(&host->dma_complete);
279}
280
281static void sh_mmcif_start_dma_rx(struct sh_mmcif_host *host)
282{
69983404
GL
283 struct mmc_data *data = host->mrq->data;
284 struct scatterlist *sg = data->sg;
a782d688
GL
285 struct dma_async_tx_descriptor *desc = NULL;
286 struct dma_chan *chan = host->chan_rx;
287 dma_cookie_t cookie = -EINVAL;
288 int ret;
289
69983404 290 ret = dma_map_sg(chan->device->dev, sg, data->sg_len,
1ed828db 291 DMA_FROM_DEVICE);
a782d688 292 if (ret > 0) {
f38f94c6 293 host->dma_active = true;
16052827 294 desc = dmaengine_prep_slave_sg(chan, sg, ret,
05f5799c 295 DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
a782d688
GL
296 }
297
298 if (desc) {
299 desc->callback = mmcif_dma_complete;
300 desc->callback_param = host;
a5ece7d2
LW
301 cookie = dmaengine_submit(desc);
302 sh_mmcif_bitset(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN);
303 dma_async_issue_pending(chan);
a782d688
GL
304 }
305 dev_dbg(&host->pd->dev, "%s(): mapped %d -> %d, cookie %d\n",
69983404 306 __func__, data->sg_len, ret, cookie);
a782d688
GL
307
308 if (!desc) {
309 /* DMA failed, fall back to PIO */
310 if (ret >= 0)
311 ret = -EIO;
312 host->chan_rx = NULL;
f38f94c6 313 host->dma_active = false;
a782d688
GL
314 dma_release_channel(chan);
315 /* Free the Tx channel too */
316 chan = host->chan_tx;
317 if (chan) {
318 host->chan_tx = NULL;
319 dma_release_channel(chan);
320 }
321 dev_warn(&host->pd->dev,
322 "DMA failed: %d, falling back to PIO\n", ret);
323 sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN | BUF_ACC_DMAWEN);
324 }
325
326 dev_dbg(&host->pd->dev, "%s(): desc %p, cookie %d, sg[%d]\n", __func__,
69983404 327 desc, cookie, data->sg_len);
a782d688
GL
328}
329
330static void sh_mmcif_start_dma_tx(struct sh_mmcif_host *host)
331{
69983404
GL
332 struct mmc_data *data = host->mrq->data;
333 struct scatterlist *sg = data->sg;
a782d688
GL
334 struct dma_async_tx_descriptor *desc = NULL;
335 struct dma_chan *chan = host->chan_tx;
336 dma_cookie_t cookie = -EINVAL;
337 int ret;
338
69983404 339 ret = dma_map_sg(chan->device->dev, sg, data->sg_len,
1ed828db 340 DMA_TO_DEVICE);
a782d688 341 if (ret > 0) {
f38f94c6 342 host->dma_active = true;
16052827 343 desc = dmaengine_prep_slave_sg(chan, sg, ret,
05f5799c 344 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
a782d688
GL
345 }
346
347 if (desc) {
348 desc->callback = mmcif_dma_complete;
349 desc->callback_param = host;
a5ece7d2
LW
350 cookie = dmaengine_submit(desc);
351 sh_mmcif_bitset(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAWEN);
352 dma_async_issue_pending(chan);
a782d688
GL
353 }
354 dev_dbg(&host->pd->dev, "%s(): mapped %d -> %d, cookie %d\n",
69983404 355 __func__, data->sg_len, ret, cookie);
a782d688
GL
356
357 if (!desc) {
358 /* DMA failed, fall back to PIO */
359 if (ret >= 0)
360 ret = -EIO;
361 host->chan_tx = NULL;
f38f94c6 362 host->dma_active = false;
a782d688
GL
363 dma_release_channel(chan);
364 /* Free the Rx channel too */
365 chan = host->chan_rx;
366 if (chan) {
367 host->chan_rx = NULL;
368 dma_release_channel(chan);
369 }
370 dev_warn(&host->pd->dev,
371 "DMA failed: %d, falling back to PIO\n", ret);
372 sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN | BUF_ACC_DMAWEN);
373 }
374
375 dev_dbg(&host->pd->dev, "%s(): desc %p, cookie %d\n", __func__,
376 desc, cookie);
377}
378
a782d688
GL
379static void sh_mmcif_request_dma(struct sh_mmcif_host *host,
380 struct sh_mmcif_plat_data *pdata)
381{
0e79f9ae
GL
382 struct resource *res = platform_get_resource(host->pd, IORESOURCE_MEM, 0);
383 struct dma_slave_config cfg;
384 dma_cap_mask_t mask;
385 int ret;
a782d688 386
f38f94c6 387 host->dma_active = false;
a782d688 388
bf68a812
GL
389 if (!pdata)
390 return;
391
0e79f9ae
GL
392 if (pdata->slave_id_tx <= 0 || pdata->slave_id_rx <= 0)
393 return;
a782d688 394
0e79f9ae
GL
395 /* We can only either use DMA for both Tx and Rx or not use it at all */
396 dma_cap_zero(mask);
397 dma_cap_set(DMA_SLAVE, mask);
a782d688 398
0e79f9ae
GL
399 host->chan_tx = dma_request_channel(mask, shdma_chan_filter,
400 (void *)pdata->slave_id_tx);
401 dev_dbg(&host->pd->dev, "%s: TX: got channel %p\n", __func__,
402 host->chan_tx);
a782d688 403
0e79f9ae
GL
404 if (!host->chan_tx)
405 return;
a782d688 406
0e79f9ae
GL
407 cfg.slave_id = pdata->slave_id_tx;
408 cfg.direction = DMA_MEM_TO_DEV;
409 cfg.dst_addr = res->start + MMCIF_CE_DATA;
410 cfg.src_addr = 0;
411 ret = dmaengine_slave_config(host->chan_tx, &cfg);
412 if (ret < 0)
413 goto ecfgtx;
414
415 host->chan_rx = dma_request_channel(mask, shdma_chan_filter,
416 (void *)pdata->slave_id_rx);
417 dev_dbg(&host->pd->dev, "%s: RX: got channel %p\n", __func__,
418 host->chan_rx);
419
420 if (!host->chan_rx)
421 goto erqrx;
422
423 cfg.slave_id = pdata->slave_id_rx;
424 cfg.direction = DMA_DEV_TO_MEM;
425 cfg.dst_addr = 0;
426 cfg.src_addr = res->start + MMCIF_CE_DATA;
427 ret = dmaengine_slave_config(host->chan_rx, &cfg);
428 if (ret < 0)
429 goto ecfgrx;
430
0e79f9ae
GL
431 return;
432
433ecfgrx:
434 dma_release_channel(host->chan_rx);
435 host->chan_rx = NULL;
436erqrx:
437ecfgtx:
438 dma_release_channel(host->chan_tx);
439 host->chan_tx = NULL;
a782d688
GL
440}
441
442static void sh_mmcif_release_dma(struct sh_mmcif_host *host)
443{
444 sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN | BUF_ACC_DMAWEN);
445 /* Descriptors are freed automatically */
446 if (host->chan_tx) {
447 struct dma_chan *chan = host->chan_tx;
448 host->chan_tx = NULL;
449 dma_release_channel(chan);
450 }
451 if (host->chan_rx) {
452 struct dma_chan *chan = host->chan_rx;
453 host->chan_rx = NULL;
454 dma_release_channel(chan);
455 }
456
f38f94c6 457 host->dma_active = false;
a782d688 458}
fdc50a94
YG
459
460static void sh_mmcif_clock_control(struct sh_mmcif_host *host, unsigned int clk)
461{
462 struct sh_mmcif_plat_data *p = host->pd->dev.platform_data;
bf68a812 463 bool sup_pclk = p ? p->sup_pclk : false;
fdc50a94
YG
464
465 sh_mmcif_bitclr(host, MMCIF_CE_CLK_CTRL, CLK_ENABLE);
466 sh_mmcif_bitclr(host, MMCIF_CE_CLK_CTRL, CLK_CLEAR);
467
468 if (!clk)
469 return;
bf68a812 470 if (sup_pclk && clk == host->clk)
fdc50a94
YG
471 sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_SUP_PCLK);
472 else
473 sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_CLEAR &
f9388257
SH
474 ((fls(DIV_ROUND_UP(host->clk,
475 clk) - 1) - 1) << 16));
fdc50a94
YG
476
477 sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_ENABLE);
478}
479
480static void sh_mmcif_sync_reset(struct sh_mmcif_host *host)
481{
482 u32 tmp;
483
487d9fc5 484 tmp = 0x010f0000 & sh_mmcif_readl(host->addr, MMCIF_CE_CLK_CTRL);
fdc50a94 485
487d9fc5
MD
486 sh_mmcif_writel(host->addr, MMCIF_CE_VERSION, SOFT_RST_ON);
487 sh_mmcif_writel(host->addr, MMCIF_CE_VERSION, SOFT_RST_OFF);
fdc50a94
YG
488 sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, tmp |
489 SRSPTO_256 | SRBSYTO_29 | SRWDTO_29 | SCCSTO_29);
490 /* byte swap on */
491 sh_mmcif_bitset(host, MMCIF_CE_BUF_ACC, BUF_ACC_ATYP);
492}
493
494static int sh_mmcif_error_manage(struct sh_mmcif_host *host)
495{
496 u32 state1, state2;
ee4b8887 497 int ret, timeout;
fdc50a94 498
aa0787a9 499 host->sd_error = false;
fdc50a94 500
487d9fc5
MD
501 state1 = sh_mmcif_readl(host->addr, MMCIF_CE_HOST_STS1);
502 state2 = sh_mmcif_readl(host->addr, MMCIF_CE_HOST_STS2);
e47bf32a
GL
503 dev_dbg(&host->pd->dev, "ERR HOST_STS1 = %08x\n", state1);
504 dev_dbg(&host->pd->dev, "ERR HOST_STS2 = %08x\n", state2);
fdc50a94
YG
505
506 if (state1 & STS1_CMDSEQ) {
507 sh_mmcif_bitset(host, MMCIF_CE_CMD_CTRL, CMD_CTRL_BREAK);
508 sh_mmcif_bitset(host, MMCIF_CE_CMD_CTRL, ~CMD_CTRL_BREAK);
ee4b8887 509 for (timeout = 10000000; timeout; timeout--) {
487d9fc5 510 if (!(sh_mmcif_readl(host->addr, MMCIF_CE_HOST_STS1)
ee4b8887 511 & STS1_CMDSEQ))
fdc50a94
YG
512 break;
513 mdelay(1);
514 }
ee4b8887
GL
515 if (!timeout) {
516 dev_err(&host->pd->dev,
517 "Forced end of command sequence timeout err\n");
518 return -EIO;
519 }
fdc50a94 520 sh_mmcif_sync_reset(host);
e47bf32a 521 dev_dbg(&host->pd->dev, "Forced end of command sequence\n");
fdc50a94
YG
522 return -EIO;
523 }
524
525 if (state2 & STS2_CRC_ERR) {
e475b270
TK
526 dev_err(&host->pd->dev, " CRC error: state %u, wait %u\n",
527 host->state, host->wait_for);
fdc50a94
YG
528 ret = -EIO;
529 } else if (state2 & STS2_TIMEOUT_ERR) {
e475b270
TK
530 dev_err(&host->pd->dev, " Timeout: state %u, wait %u\n",
531 host->state, host->wait_for);
fdc50a94
YG
532 ret = -ETIMEDOUT;
533 } else {
e475b270
TK
534 dev_dbg(&host->pd->dev, " End/Index error: state %u, wait %u\n",
535 host->state, host->wait_for);
fdc50a94
YG
536 ret = -EIO;
537 }
538 return ret;
539}
540
f985da17 541static bool sh_mmcif_next_block(struct sh_mmcif_host *host, u32 *p)
fdc50a94 542{
f985da17
GL
543 struct mmc_data *data = host->mrq->data;
544
545 host->sg_blkidx += host->blocksize;
546
547 /* data->sg->length must be a multiple of host->blocksize? */
548 BUG_ON(host->sg_blkidx > data->sg->length);
549
550 if (host->sg_blkidx == data->sg->length) {
551 host->sg_blkidx = 0;
552 if (++host->sg_idx < data->sg_len)
553 host->pio_ptr = sg_virt(++data->sg);
554 } else {
555 host->pio_ptr = p;
556 }
557
99eb9d8d 558 return host->sg_idx != data->sg_len;
f985da17
GL
559}
560
561static void sh_mmcif_single_read(struct sh_mmcif_host *host,
562 struct mmc_request *mrq)
563{
564 host->blocksize = (sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) &
565 BLOCK_SIZE_MASK) + 3;
566
567 host->wait_for = MMCIF_WAIT_FOR_READ;
fdc50a94 568
fdc50a94
YG
569 /* buf read enable */
570 sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFREN);
f985da17
GL
571}
572
573static bool sh_mmcif_read_block(struct sh_mmcif_host *host)
574{
575 struct mmc_data *data = host->mrq->data;
576 u32 *p = sg_virt(data->sg);
577 int i;
578
579 if (host->sd_error) {
580 data->error = sh_mmcif_error_manage(host);
e475b270 581 dev_dbg(&host->pd->dev, "%s(): %d\n", __func__, data->error);
f985da17
GL
582 return false;
583 }
584
585 for (i = 0; i < host->blocksize / 4; i++)
487d9fc5 586 *p++ = sh_mmcif_readl(host->addr, MMCIF_CE_DATA);
fdc50a94
YG
587
588 /* buffer read end */
589 sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFRE);
f985da17 590 host->wait_for = MMCIF_WAIT_FOR_READ_END;
fdc50a94 591
f985da17 592 return true;
fdc50a94
YG
593}
594
f985da17
GL
595static void sh_mmcif_multi_read(struct sh_mmcif_host *host,
596 struct mmc_request *mrq)
fdc50a94
YG
597{
598 struct mmc_data *data = mrq->data;
f985da17
GL
599
600 if (!data->sg_len || !data->sg->length)
601 return;
602
603 host->blocksize = sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) &
604 BLOCK_SIZE_MASK;
605
606 host->wait_for = MMCIF_WAIT_FOR_MREAD;
607 host->sg_idx = 0;
608 host->sg_blkidx = 0;
609 host->pio_ptr = sg_virt(data->sg);
5df460b1 610
f985da17
GL
611 sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFREN);
612}
613
614static bool sh_mmcif_mread_block(struct sh_mmcif_host *host)
615{
616 struct mmc_data *data = host->mrq->data;
617 u32 *p = host->pio_ptr;
618 int i;
619
620 if (host->sd_error) {
621 data->error = sh_mmcif_error_manage(host);
e475b270 622 dev_dbg(&host->pd->dev, "%s(): %d\n", __func__, data->error);
f985da17 623 return false;
fdc50a94 624 }
f985da17
GL
625
626 BUG_ON(!data->sg->length);
627
628 for (i = 0; i < host->blocksize / 4; i++)
629 *p++ = sh_mmcif_readl(host->addr, MMCIF_CE_DATA);
630
631 if (!sh_mmcif_next_block(host, p))
632 return false;
633
f985da17
GL
634 sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFREN);
635
636 return true;
fdc50a94
YG
637}
638
f985da17 639static void sh_mmcif_single_write(struct sh_mmcif_host *host,
fdc50a94
YG
640 struct mmc_request *mrq)
641{
f985da17
GL
642 host->blocksize = (sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) &
643 BLOCK_SIZE_MASK) + 3;
fdc50a94 644
f985da17 645 host->wait_for = MMCIF_WAIT_FOR_WRITE;
fdc50a94
YG
646
647 /* buf write enable */
f985da17
GL
648 sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN);
649}
650
651static bool sh_mmcif_write_block(struct sh_mmcif_host *host)
652{
653 struct mmc_data *data = host->mrq->data;
654 u32 *p = sg_virt(data->sg);
655 int i;
656
657 if (host->sd_error) {
658 data->error = sh_mmcif_error_manage(host);
e475b270 659 dev_dbg(&host->pd->dev, "%s(): %d\n", __func__, data->error);
f985da17
GL
660 return false;
661 }
662
663 for (i = 0; i < host->blocksize / 4; i++)
487d9fc5 664 sh_mmcif_writel(host->addr, MMCIF_CE_DATA, *p++);
fdc50a94
YG
665
666 /* buffer write end */
667 sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MDTRANE);
f985da17 668 host->wait_for = MMCIF_WAIT_FOR_WRITE_END;
fdc50a94 669
f985da17 670 return true;
fdc50a94
YG
671}
672
f985da17
GL
673static void sh_mmcif_multi_write(struct sh_mmcif_host *host,
674 struct mmc_request *mrq)
fdc50a94
YG
675{
676 struct mmc_data *data = mrq->data;
fdc50a94 677
f985da17
GL
678 if (!data->sg_len || !data->sg->length)
679 return;
fdc50a94 680
f985da17
GL
681 host->blocksize = sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) &
682 BLOCK_SIZE_MASK;
fdc50a94 683
f985da17
GL
684 host->wait_for = MMCIF_WAIT_FOR_MWRITE;
685 host->sg_idx = 0;
686 host->sg_blkidx = 0;
687 host->pio_ptr = sg_virt(data->sg);
5df460b1 688
f985da17
GL
689 sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN);
690}
fdc50a94 691
f985da17
GL
692static bool sh_mmcif_mwrite_block(struct sh_mmcif_host *host)
693{
694 struct mmc_data *data = host->mrq->data;
695 u32 *p = host->pio_ptr;
696 int i;
697
698 if (host->sd_error) {
699 data->error = sh_mmcif_error_manage(host);
e475b270 700 dev_dbg(&host->pd->dev, "%s(): %d\n", __func__, data->error);
f985da17 701 return false;
fdc50a94 702 }
f985da17
GL
703
704 BUG_ON(!data->sg->length);
705
706 for (i = 0; i < host->blocksize / 4; i++)
707 sh_mmcif_writel(host->addr, MMCIF_CE_DATA, *p++);
708
709 if (!sh_mmcif_next_block(host, p))
710 return false;
711
f985da17
GL
712 sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN);
713
714 return true;
fdc50a94
YG
715}
716
717static void sh_mmcif_get_response(struct sh_mmcif_host *host,
718 struct mmc_command *cmd)
719{
720 if (cmd->flags & MMC_RSP_136) {
487d9fc5
MD
721 cmd->resp[0] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP3);
722 cmd->resp[1] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP2);
723 cmd->resp[2] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP1);
724 cmd->resp[3] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP0);
fdc50a94 725 } else
487d9fc5 726 cmd->resp[0] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP0);
fdc50a94
YG
727}
728
729static void sh_mmcif_get_cmd12response(struct sh_mmcif_host *host,
730 struct mmc_command *cmd)
731{
487d9fc5 732 cmd->resp[0] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP_CMD12);
fdc50a94
YG
733}
734
735static u32 sh_mmcif_set_cmd(struct sh_mmcif_host *host,
69983404 736 struct mmc_request *mrq)
fdc50a94 737{
69983404
GL
738 struct mmc_data *data = mrq->data;
739 struct mmc_command *cmd = mrq->cmd;
740 u32 opc = cmd->opcode;
fdc50a94
YG
741 u32 tmp = 0;
742
743 /* Response Type check */
744 switch (mmc_resp_type(cmd)) {
745 case MMC_RSP_NONE:
746 tmp |= CMD_SET_RTYP_NO;
747 break;
748 case MMC_RSP_R1:
749 case MMC_RSP_R1B:
750 case MMC_RSP_R3:
751 tmp |= CMD_SET_RTYP_6B;
752 break;
753 case MMC_RSP_R2:
754 tmp |= CMD_SET_RTYP_17B;
755 break;
756 default:
e47bf32a 757 dev_err(&host->pd->dev, "Unsupported response type.\n");
fdc50a94
YG
758 break;
759 }
760 switch (opc) {
761 /* RBSY */
a812ba0f 762 case MMC_SLEEP_AWAKE:
fdc50a94
YG
763 case MMC_SWITCH:
764 case MMC_STOP_TRANSMISSION:
765 case MMC_SET_WRITE_PROT:
766 case MMC_CLR_WRITE_PROT:
767 case MMC_ERASE:
fdc50a94
YG
768 tmp |= CMD_SET_RBSY;
769 break;
770 }
771 /* WDAT / DATW */
69983404 772 if (data) {
fdc50a94
YG
773 tmp |= CMD_SET_WDAT;
774 switch (host->bus_width) {
775 case MMC_BUS_WIDTH_1:
776 tmp |= CMD_SET_DATW_1;
777 break;
778 case MMC_BUS_WIDTH_4:
779 tmp |= CMD_SET_DATW_4;
780 break;
781 case MMC_BUS_WIDTH_8:
782 tmp |= CMD_SET_DATW_8;
783 break;
784 default:
e47bf32a 785 dev_err(&host->pd->dev, "Unsupported bus width.\n");
fdc50a94
YG
786 break;
787 }
555061f9
TK
788 switch (host->timing) {
789 case MMC_TIMING_UHS_DDR50:
790 /*
791 * MMC core will only set this timing, if the host
792 * advertises the MMC_CAP_UHS_DDR50 capability. MMCIF
793 * implementations with this capability, e.g. sh73a0,
794 * will have to set it in their platform data.
795 */
796 tmp |= CMD_SET_DARS;
797 break;
798 }
fdc50a94
YG
799 }
800 /* DWEN */
801 if (opc == MMC_WRITE_BLOCK || opc == MMC_WRITE_MULTIPLE_BLOCK)
802 tmp |= CMD_SET_DWEN;
803 /* CMLTE/CMD12EN */
804 if (opc == MMC_READ_MULTIPLE_BLOCK || opc == MMC_WRITE_MULTIPLE_BLOCK) {
805 tmp |= CMD_SET_CMLTE | CMD_SET_CMD12EN;
806 sh_mmcif_bitset(host, MMCIF_CE_BLOCK_SET,
69983404 807 data->blocks << 16);
fdc50a94
YG
808 }
809 /* RIDXC[1:0] check bits */
810 if (opc == MMC_SEND_OP_COND || opc == MMC_ALL_SEND_CID ||
811 opc == MMC_SEND_CSD || opc == MMC_SEND_CID)
812 tmp |= CMD_SET_RIDXC_BITS;
813 /* RCRC7C[1:0] check bits */
814 if (opc == MMC_SEND_OP_COND)
815 tmp |= CMD_SET_CRC7C_BITS;
816 /* RCRC7C[1:0] internal CRC7 */
817 if (opc == MMC_ALL_SEND_CID ||
818 opc == MMC_SEND_CSD || opc == MMC_SEND_CID)
819 tmp |= CMD_SET_CRC7C_INTERNAL;
820
69983404 821 return (opc << 24) | tmp;
fdc50a94
YG
822}
823
e47bf32a 824static int sh_mmcif_data_trans(struct sh_mmcif_host *host,
f985da17 825 struct mmc_request *mrq, u32 opc)
fdc50a94 826{
fdc50a94
YG
827 switch (opc) {
828 case MMC_READ_MULTIPLE_BLOCK:
f985da17
GL
829 sh_mmcif_multi_read(host, mrq);
830 return 0;
fdc50a94 831 case MMC_WRITE_MULTIPLE_BLOCK:
f985da17
GL
832 sh_mmcif_multi_write(host, mrq);
833 return 0;
fdc50a94 834 case MMC_WRITE_BLOCK:
f985da17
GL
835 sh_mmcif_single_write(host, mrq);
836 return 0;
fdc50a94
YG
837 case MMC_READ_SINGLE_BLOCK:
838 case MMC_SEND_EXT_CSD:
f985da17
GL
839 sh_mmcif_single_read(host, mrq);
840 return 0;
fdc50a94 841 default:
e475b270 842 dev_err(&host->pd->dev, "Unsupported CMD%d\n", opc);
ee4b8887 843 return -EINVAL;
fdc50a94 844 }
fdc50a94
YG
845}
846
847static void sh_mmcif_start_cmd(struct sh_mmcif_host *host,
ee4b8887 848 struct mmc_request *mrq)
fdc50a94 849{
ee4b8887 850 struct mmc_command *cmd = mrq->cmd;
f985da17
GL
851 u32 opc = cmd->opcode;
852 u32 mask;
fdc50a94 853
fdc50a94 854 switch (opc) {
ee4b8887 855 /* response busy check */
a812ba0f 856 case MMC_SLEEP_AWAKE:
fdc50a94
YG
857 case MMC_SWITCH:
858 case MMC_STOP_TRANSMISSION:
859 case MMC_SET_WRITE_PROT:
860 case MMC_CLR_WRITE_PROT:
861 case MMC_ERASE:
ee4b8887 862 mask = MASK_START_CMD | MASK_MRBSYE;
fdc50a94
YG
863 break;
864 default:
ee4b8887 865 mask = MASK_START_CMD | MASK_MCRSPE;
fdc50a94
YG
866 break;
867 }
fdc50a94 868
69983404 869 if (mrq->data) {
487d9fc5
MD
870 sh_mmcif_writel(host->addr, MMCIF_CE_BLOCK_SET, 0);
871 sh_mmcif_writel(host->addr, MMCIF_CE_BLOCK_SET,
872 mrq->data->blksz);
fdc50a94 873 }
69983404 874 opc = sh_mmcif_set_cmd(host, mrq);
fdc50a94 875
487d9fc5
MD
876 sh_mmcif_writel(host->addr, MMCIF_CE_INT, 0xD80430C0);
877 sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, mask);
fdc50a94 878 /* set arg */
487d9fc5 879 sh_mmcif_writel(host->addr, MMCIF_CE_ARG, cmd->arg);
fdc50a94 880 /* set cmd */
487d9fc5 881 sh_mmcif_writel(host->addr, MMCIF_CE_CMD_SET, opc);
fdc50a94 882
f985da17
GL
883 host->wait_for = MMCIF_WAIT_FOR_CMD;
884 schedule_delayed_work(&host->timeout_work, host->timeout);
fdc50a94
YG
885}
886
887static void sh_mmcif_stop_cmd(struct sh_mmcif_host *host,
ee4b8887 888 struct mmc_request *mrq)
fdc50a94 889{
69983404
GL
890 switch (mrq->cmd->opcode) {
891 case MMC_READ_MULTIPLE_BLOCK:
fdc50a94 892 sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MCMD12DRE);
69983404
GL
893 break;
894 case MMC_WRITE_MULTIPLE_BLOCK:
fdc50a94 895 sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MCMD12RBE);
69983404
GL
896 break;
897 default:
e47bf32a 898 dev_err(&host->pd->dev, "unsupported stop cmd\n");
69983404 899 mrq->stop->error = sh_mmcif_error_manage(host);
fdc50a94
YG
900 return;
901 }
902
f985da17 903 host->wait_for = MMCIF_WAIT_FOR_STOP;
fdc50a94
YG
904}
905
906static void sh_mmcif_request(struct mmc_host *mmc, struct mmc_request *mrq)
907{
908 struct sh_mmcif_host *host = mmc_priv(mmc);
3b0beafc
GL
909 unsigned long flags;
910
911 spin_lock_irqsave(&host->lock, flags);
912 if (host->state != STATE_IDLE) {
e475b270 913 dev_dbg(&host->pd->dev, "%s() rejected, state %u\n", __func__, host->state);
3b0beafc
GL
914 spin_unlock_irqrestore(&host->lock, flags);
915 mrq->cmd->error = -EAGAIN;
916 mmc_request_done(mmc, mrq);
917 return;
918 }
919
920 host->state = STATE_REQUEST;
921 spin_unlock_irqrestore(&host->lock, flags);
fdc50a94
YG
922
923 switch (mrq->cmd->opcode) {
924 /* MMCIF does not support SD/SDIO command */
7541ca98
LP
925 case MMC_SLEEP_AWAKE: /* = SD_IO_SEND_OP_COND (5) */
926 case MMC_SEND_EXT_CSD: /* = SD_SEND_IF_COND (8) */
927 if ((mrq->cmd->flags & MMC_CMD_MASK) != MMC_CMD_BCR)
928 break;
fdc50a94 929 case MMC_APP_CMD:
92ff0c5b 930 case SD_IO_RW_DIRECT:
3b0beafc 931 host->state = STATE_IDLE;
fdc50a94
YG
932 mrq->cmd->error = -ETIMEDOUT;
933 mmc_request_done(mmc, mrq);
934 return;
fdc50a94
YG
935 default:
936 break;
937 }
f985da17
GL
938
939 host->mrq = mrq;
fdc50a94 940
f985da17 941 sh_mmcif_start_cmd(host, mrq);
fdc50a94
YG
942}
943
a6609267
GL
944static int sh_mmcif_clk_update(struct sh_mmcif_host *host)
945{
946 int ret = clk_enable(host->hclk);
947
948 if (!ret) {
949 host->clk = clk_get_rate(host->hclk);
950 host->mmc->f_max = host->clk / 2;
951 host->mmc->f_min = host->clk / 512;
952 }
953
954 return ret;
955}
956
7d17baa0
GL
957static void sh_mmcif_set_power(struct sh_mmcif_host *host, struct mmc_ios *ios)
958{
7d17baa0
GL
959 struct mmc_host *mmc = host->mmc;
960
7d17baa0
GL
961 if (!IS_ERR(mmc->supply.vmmc))
962 /* Errors ignored... */
963 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc,
964 ios->power_mode ? ios->vdd : 0);
965}
966
fdc50a94
YG
967static void sh_mmcif_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
968{
969 struct sh_mmcif_host *host = mmc_priv(mmc);
3b0beafc
GL
970 unsigned long flags;
971
972 spin_lock_irqsave(&host->lock, flags);
973 if (host->state != STATE_IDLE) {
e475b270 974 dev_dbg(&host->pd->dev, "%s() rejected, state %u\n", __func__, host->state);
3b0beafc
GL
975 spin_unlock_irqrestore(&host->lock, flags);
976 return;
977 }
978
979 host->state = STATE_IOS;
980 spin_unlock_irqrestore(&host->lock, flags);
fdc50a94 981
f5e0cec4 982 if (ios->power_mode == MMC_POWER_UP) {
c9b0cef2 983 if (!host->card_present) {
faca6648
GL
984 /* See if we also get DMA */
985 sh_mmcif_request_dma(host, host->pd->dev.platform_data);
c9b0cef2 986 host->card_present = true;
faca6648 987 }
7d17baa0 988 sh_mmcif_set_power(host, ios);
f5e0cec4 989 } else if (ios->power_mode == MMC_POWER_OFF || !ios->clock) {
fdc50a94
YG
990 /* clock stop */
991 sh_mmcif_clock_control(host, 0);
faca6648 992 if (ios->power_mode == MMC_POWER_OFF) {
c9b0cef2 993 if (host->card_present) {
faca6648 994 sh_mmcif_release_dma(host);
c9b0cef2 995 host->card_present = false;
faca6648 996 }
c9b0cef2
GL
997 }
998 if (host->power) {
f8a8ced7 999 pm_runtime_put_sync(&host->pd->dev);
b289174f 1000 clk_disable(host->hclk);
c9b0cef2 1001 host->power = false;
7d17baa0
GL
1002 if (ios->power_mode == MMC_POWER_OFF)
1003 sh_mmcif_set_power(host, ios);
faca6648 1004 }
3b0beafc 1005 host->state = STATE_IDLE;
fdc50a94 1006 return;
fdc50a94
YG
1007 }
1008
c9b0cef2
GL
1009 if (ios->clock) {
1010 if (!host->power) {
a6609267 1011 sh_mmcif_clk_update(host);
c9b0cef2
GL
1012 pm_runtime_get_sync(&host->pd->dev);
1013 host->power = true;
1014 sh_mmcif_sync_reset(host);
1015 }
fdc50a94 1016 sh_mmcif_clock_control(host, ios->clock);
c9b0cef2 1017 }
fdc50a94 1018
555061f9 1019 host->timing = ios->timing;
fdc50a94 1020 host->bus_width = ios->bus_width;
3b0beafc 1021 host->state = STATE_IDLE;
fdc50a94
YG
1022}
1023
777271d0
AH
1024static int sh_mmcif_get_cd(struct mmc_host *mmc)
1025{
1026 struct sh_mmcif_host *host = mmc_priv(mmc);
1027 struct sh_mmcif_plat_data *p = host->pd->dev.platform_data;
e480606a
GL
1028 int ret = mmc_gpio_get_cd(mmc);
1029
1030 if (ret >= 0)
1031 return ret;
777271d0 1032
bf68a812 1033 if (!p || !p->get_cd)
777271d0
AH
1034 return -ENOSYS;
1035 else
1036 return p->get_cd(host->pd);
1037}
1038
fdc50a94
YG
1039static struct mmc_host_ops sh_mmcif_ops = {
1040 .request = sh_mmcif_request,
1041 .set_ios = sh_mmcif_set_ios,
777271d0 1042 .get_cd = sh_mmcif_get_cd,
fdc50a94
YG
1043};
1044
f985da17
GL
1045static bool sh_mmcif_end_cmd(struct sh_mmcif_host *host)
1046{
1047 struct mmc_command *cmd = host->mrq->cmd;
69983404 1048 struct mmc_data *data = host->mrq->data;
f985da17
GL
1049 long time;
1050
1051 if (host->sd_error) {
1052 switch (cmd->opcode) {
1053 case MMC_ALL_SEND_CID:
1054 case MMC_SELECT_CARD:
1055 case MMC_APP_CMD:
1056 cmd->error = -ETIMEDOUT;
f985da17
GL
1057 break;
1058 default:
1059 cmd->error = sh_mmcif_error_manage(host);
f985da17
GL
1060 break;
1061 }
e475b270
TK
1062 dev_dbg(&host->pd->dev, "CMD%d error %d\n",
1063 cmd->opcode, cmd->error);
aba9d646 1064 host->sd_error = false;
f985da17
GL
1065 return false;
1066 }
1067 if (!(cmd->flags & MMC_RSP_PRESENT)) {
1068 cmd->error = 0;
1069 return false;
1070 }
1071
1072 sh_mmcif_get_response(host, cmd);
1073
69983404 1074 if (!data)
f985da17
GL
1075 return false;
1076
90f1cb43
GL
1077 /*
1078 * Completion can be signalled from DMA callback and error, so, have to
1079 * reset here, before setting .dma_active
1080 */
1081 init_completion(&host->dma_complete);
1082
69983404 1083 if (data->flags & MMC_DATA_READ) {
f985da17
GL
1084 if (host->chan_rx)
1085 sh_mmcif_start_dma_rx(host);
1086 } else {
1087 if (host->chan_tx)
1088 sh_mmcif_start_dma_tx(host);
1089 }
1090
1091 if (!host->dma_active) {
69983404 1092 data->error = sh_mmcif_data_trans(host, host->mrq, cmd->opcode);
99eb9d8d 1093 return !data->error;
f985da17
GL
1094 }
1095
1096 /* Running in the IRQ thread, can sleep */
1097 time = wait_for_completion_interruptible_timeout(&host->dma_complete,
1098 host->timeout);
eae30983
TK
1099
1100 if (data->flags & MMC_DATA_READ)
1101 dma_unmap_sg(host->chan_rx->device->dev,
1102 data->sg, data->sg_len,
1103 DMA_FROM_DEVICE);
1104 else
1105 dma_unmap_sg(host->chan_tx->device->dev,
1106 data->sg, data->sg_len,
1107 DMA_TO_DEVICE);
1108
f985da17
GL
1109 if (host->sd_error) {
1110 dev_err(host->mmc->parent,
1111 "Error IRQ while waiting for DMA completion!\n");
1112 /* Woken up by an error IRQ: abort DMA */
69983404 1113 data->error = sh_mmcif_error_manage(host);
f985da17 1114 } else if (!time) {
e475b270 1115 dev_err(host->mmc->parent, "DMA timeout!\n");
69983404 1116 data->error = -ETIMEDOUT;
f985da17 1117 } else if (time < 0) {
e475b270
TK
1118 dev_err(host->mmc->parent,
1119 "wait_for_completion_...() error %ld!\n", time);
69983404 1120 data->error = time;
f985da17
GL
1121 }
1122 sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC,
1123 BUF_ACC_DMAREN | BUF_ACC_DMAWEN);
1124 host->dma_active = false;
1125
eae30983 1126 if (data->error) {
69983404 1127 data->bytes_xfered = 0;
eae30983
TK
1128 /* Abort DMA */
1129 if (data->flags & MMC_DATA_READ)
1130 dmaengine_terminate_all(host->chan_rx);
1131 else
1132 dmaengine_terminate_all(host->chan_tx);
1133 }
f985da17
GL
1134
1135 return false;
1136}
1137
1138static irqreturn_t sh_mmcif_irqt(int irq, void *dev_id)
1139{
1140 struct sh_mmcif_host *host = dev_id;
8047310e 1141 struct mmc_request *mrq;
5df460b1 1142 bool wait = false;
f985da17
GL
1143
1144 cancel_delayed_work_sync(&host->timeout_work);
1145
8047310e
GL
1146 mutex_lock(&host->thread_lock);
1147
1148 mrq = host->mrq;
1149 if (!mrq) {
1150 dev_dbg(&host->pd->dev, "IRQ thread state %u, wait %u: NULL mrq!\n",
1151 host->state, host->wait_for);
1152 mutex_unlock(&host->thread_lock);
1153 return IRQ_HANDLED;
1154 }
1155
f985da17
GL
1156 /*
1157 * All handlers return true, if processing continues, and false, if the
1158 * request has to be completed - successfully or not
1159 */
1160 switch (host->wait_for) {
1161 case MMCIF_WAIT_FOR_REQUEST:
1162 /* We're too late, the timeout has already kicked in */
8047310e 1163 mutex_unlock(&host->thread_lock);
f985da17
GL
1164 return IRQ_HANDLED;
1165 case MMCIF_WAIT_FOR_CMD:
5df460b1
GL
1166 /* Wait for data? */
1167 wait = sh_mmcif_end_cmd(host);
f985da17
GL
1168 break;
1169 case MMCIF_WAIT_FOR_MREAD:
5df460b1
GL
1170 /* Wait for more data? */
1171 wait = sh_mmcif_mread_block(host);
f985da17
GL
1172 break;
1173 case MMCIF_WAIT_FOR_READ:
5df460b1
GL
1174 /* Wait for data end? */
1175 wait = sh_mmcif_read_block(host);
f985da17
GL
1176 break;
1177 case MMCIF_WAIT_FOR_MWRITE:
5df460b1
GL
1178 /* Wait data to write? */
1179 wait = sh_mmcif_mwrite_block(host);
f985da17
GL
1180 break;
1181 case MMCIF_WAIT_FOR_WRITE:
5df460b1
GL
1182 /* Wait for data end? */
1183 wait = sh_mmcif_write_block(host);
f985da17
GL
1184 break;
1185 case MMCIF_WAIT_FOR_STOP:
1186 if (host->sd_error) {
1187 mrq->stop->error = sh_mmcif_error_manage(host);
e475b270 1188 dev_dbg(&host->pd->dev, "%s(): %d\n", __func__, mrq->stop->error);
f985da17
GL
1189 break;
1190 }
1191 sh_mmcif_get_cmd12response(host, mrq->stop);
1192 mrq->stop->error = 0;
1193 break;
1194 case MMCIF_WAIT_FOR_READ_END:
1195 case MMCIF_WAIT_FOR_WRITE_END:
e475b270 1196 if (host->sd_error) {
91ab252a 1197 mrq->data->error = sh_mmcif_error_manage(host);
e475b270
TK
1198 dev_dbg(&host->pd->dev, "%s(): %d\n", __func__, mrq->data->error);
1199 }
f985da17
GL
1200 break;
1201 default:
1202 BUG();
1203 }
1204
5df460b1
GL
1205 if (wait) {
1206 schedule_delayed_work(&host->timeout_work, host->timeout);
1207 /* Wait for more data */
8047310e 1208 mutex_unlock(&host->thread_lock);
5df460b1
GL
1209 return IRQ_HANDLED;
1210 }
1211
f985da17 1212 if (host->wait_for != MMCIF_WAIT_FOR_STOP) {
91ab252a 1213 struct mmc_data *data = mrq->data;
69983404
GL
1214 if (!mrq->cmd->error && data && !data->error)
1215 data->bytes_xfered =
1216 data->blocks * data->blksz;
f985da17 1217
69983404 1218 if (mrq->stop && !mrq->cmd->error && (!data || !data->error)) {
f985da17 1219 sh_mmcif_stop_cmd(host, mrq);
5df460b1
GL
1220 if (!mrq->stop->error) {
1221 schedule_delayed_work(&host->timeout_work, host->timeout);
8047310e 1222 mutex_unlock(&host->thread_lock);
f985da17 1223 return IRQ_HANDLED;
5df460b1 1224 }
f985da17
GL
1225 }
1226 }
1227
1228 host->wait_for = MMCIF_WAIT_FOR_REQUEST;
1229 host->state = STATE_IDLE;
69983404 1230 host->mrq = NULL;
f985da17
GL
1231 mmc_request_done(host->mmc, mrq);
1232
8047310e
GL
1233 mutex_unlock(&host->thread_lock);
1234
f985da17
GL
1235 return IRQ_HANDLED;
1236}
1237
fdc50a94
YG
1238static irqreturn_t sh_mmcif_intr(int irq, void *dev_id)
1239{
1240 struct sh_mmcif_host *host = dev_id;
aa0787a9 1241 u32 state;
fdc50a94 1242
487d9fc5 1243 state = sh_mmcif_readl(host->addr, MMCIF_CE_INT);
19f1ba51
GL
1244 sh_mmcif_writel(host->addr, MMCIF_CE_INT,
1245 ~(state & sh_mmcif_readl(host->addr, MMCIF_CE_INT_MASK)));
8af50750 1246 sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, state & MASK_CLEAN);
fdc50a94 1247
8af50750
GL
1248 if (state & ~MASK_CLEAN)
1249 dev_dbg(&host->pd->dev, "IRQ state = 0x%08x incompletely cleared\n",
1250 state);
1251
1252 if (state & INT_ERR_STS || state & ~INT_ALL) {
aa0787a9 1253 host->sd_error = true;
8af50750 1254 dev_dbg(&host->pd->dev, "int err state = 0x%08x\n", state);
fdc50a94 1255 }
f985da17 1256 if (state & ~(INT_CMD12RBE | INT_CMD12CRE)) {
8af50750
GL
1257 if (!host->mrq)
1258 dev_dbg(&host->pd->dev, "NULL IRQ state = 0x%08x\n", state);
f985da17
GL
1259 if (!host->dma_active)
1260 return IRQ_WAKE_THREAD;
1261 else if (host->sd_error)
1262 mmcif_dma_complete(host);
1263 } else {
aa0787a9 1264 dev_dbg(&host->pd->dev, "Unexpected IRQ 0x%x\n", state);
f985da17 1265 }
fdc50a94
YG
1266
1267 return IRQ_HANDLED;
1268}
1269
f985da17
GL
1270static void mmcif_timeout_work(struct work_struct *work)
1271{
1272 struct delayed_work *d = container_of(work, struct delayed_work, work);
1273 struct sh_mmcif_host *host = container_of(d, struct sh_mmcif_host, timeout_work);
1274 struct mmc_request *mrq = host->mrq;
8047310e 1275 unsigned long flags;
f985da17
GL
1276
1277 if (host->dying)
1278 /* Don't run after mmc_remove_host() */
1279 return;
1280
e475b270 1281 dev_err(&host->pd->dev, "Timeout waiting for %u on CMD%u\n",
8047310e
GL
1282 host->wait_for, mrq->cmd->opcode);
1283
1284 spin_lock_irqsave(&host->lock, flags);
1285 if (host->state == STATE_IDLE) {
1286 spin_unlock_irqrestore(&host->lock, flags);
1287 return;
1288 }
1289
1290 host->state = STATE_TIMEOUT;
1291 spin_unlock_irqrestore(&host->lock, flags);
1292
f985da17
GL
1293 /*
1294 * Handle races with cancel_delayed_work(), unless
1295 * cancel_delayed_work_sync() is used
1296 */
1297 switch (host->wait_for) {
1298 case MMCIF_WAIT_FOR_CMD:
1299 mrq->cmd->error = sh_mmcif_error_manage(host);
1300 break;
1301 case MMCIF_WAIT_FOR_STOP:
1302 mrq->stop->error = sh_mmcif_error_manage(host);
1303 break;
1304 case MMCIF_WAIT_FOR_MREAD:
1305 case MMCIF_WAIT_FOR_MWRITE:
1306 case MMCIF_WAIT_FOR_READ:
1307 case MMCIF_WAIT_FOR_WRITE:
1308 case MMCIF_WAIT_FOR_READ_END:
1309 case MMCIF_WAIT_FOR_WRITE_END:
69983404 1310 mrq->data->error = sh_mmcif_error_manage(host);
f985da17
GL
1311 break;
1312 default:
1313 BUG();
1314 }
1315
1316 host->state = STATE_IDLE;
1317 host->wait_for = MMCIF_WAIT_FOR_REQUEST;
f985da17
GL
1318 host->mrq = NULL;
1319 mmc_request_done(host->mmc, mrq);
1320}
1321
7d17baa0
GL
1322static void sh_mmcif_init_ocr(struct sh_mmcif_host *host)
1323{
1324 struct sh_mmcif_plat_data *pd = host->pd->dev.platform_data;
1325 struct mmc_host *mmc = host->mmc;
1326
1327 mmc_regulator_get_supply(mmc);
1328
bf68a812
GL
1329 if (!pd)
1330 return;
1331
7d17baa0
GL
1332 if (!mmc->ocr_avail)
1333 mmc->ocr_avail = pd->ocr;
1334 else if (pd->ocr)
1335 dev_warn(mmc_dev(mmc), "Platform OCR mask is ignored\n");
1336}
1337
c3be1efd 1338static int sh_mmcif_probe(struct platform_device *pdev)
fdc50a94
YG
1339{
1340 int ret = 0, irq[2];
1341 struct mmc_host *mmc;
e47bf32a 1342 struct sh_mmcif_host *host;
e1aae2eb 1343 struct sh_mmcif_plat_data *pd = pdev->dev.platform_data;
fdc50a94
YG
1344 struct resource *res;
1345 void __iomem *reg;
2cd5b3e0 1346 const char *name;
fdc50a94
YG
1347
1348 irq[0] = platform_get_irq(pdev, 0);
1349 irq[1] = platform_get_irq(pdev, 1);
2cd5b3e0 1350 if (irq[0] < 0) {
e47bf32a 1351 dev_err(&pdev->dev, "Get irq error\n");
fdc50a94
YG
1352 return -ENXIO;
1353 }
1354 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1355 if (!res) {
1356 dev_err(&pdev->dev, "platform_get_resource error.\n");
1357 return -ENXIO;
1358 }
1359 reg = ioremap(res->start, resource_size(res));
1360 if (!reg) {
1361 dev_err(&pdev->dev, "ioremap error.\n");
1362 return -ENOMEM;
1363 }
e1aae2eb 1364
fdc50a94
YG
1365 mmc = mmc_alloc_host(sizeof(struct sh_mmcif_host), &pdev->dev);
1366 if (!mmc) {
1367 ret = -ENOMEM;
e1aae2eb 1368 goto ealloch;
fdc50a94 1369 }
2c9054dc
SB
1370
1371 ret = mmc_of_parse(mmc);
1372 if (ret < 0)
1373 goto eofparse;
1374
fdc50a94
YG
1375 host = mmc_priv(mmc);
1376 host->mmc = mmc;
1377 host->addr = reg;
f9fd54f2 1378 host->timeout = msecs_to_jiffies(1000);
fdc50a94 1379
fdc50a94
YG
1380 host->pd = pdev;
1381
3b0beafc 1382 spin_lock_init(&host->lock);
fdc50a94
YG
1383
1384 mmc->ops = &sh_mmcif_ops;
7d17baa0
GL
1385 sh_mmcif_init_ocr(host);
1386
eca889f6 1387 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_WAIT_WHILE_BUSY;
bf68a812 1388 if (pd && pd->caps)
fdc50a94 1389 mmc->caps |= pd->caps;
a782d688 1390 mmc->max_segs = 32;
fdc50a94 1391 mmc->max_blk_size = 512;
a782d688
GL
1392 mmc->max_req_size = PAGE_CACHE_SIZE * mmc->max_segs;
1393 mmc->max_blk_count = mmc->max_req_size / mmc->max_blk_size;
fdc50a94
YG
1394 mmc->max_seg_size = mmc->max_req_size;
1395
fdc50a94 1396 platform_set_drvdata(pdev, host);
a782d688 1397
faca6648
GL
1398 pm_runtime_enable(&pdev->dev);
1399 host->power = false;
1400
047a9ce7 1401 host->hclk = clk_get(&pdev->dev, NULL);
b289174f
GL
1402 if (IS_ERR(host->hclk)) {
1403 ret = PTR_ERR(host->hclk);
047a9ce7 1404 dev_err(&pdev->dev, "cannot get clock: %d\n", ret);
b289174f
GL
1405 goto eclkget;
1406 }
a6609267
GL
1407 ret = sh_mmcif_clk_update(host);
1408 if (ret < 0)
1409 goto eclkupdate;
b289174f 1410
faca6648
GL
1411 ret = pm_runtime_resume(&pdev->dev);
1412 if (ret < 0)
e1aae2eb 1413 goto eresume;
a782d688 1414
5ba85d95 1415 INIT_DELAYED_WORK(&host->timeout_work, mmcif_timeout_work);
fdc50a94 1416
b289174f 1417 sh_mmcif_sync_reset(host);
3b0beafc
GL
1418 sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL);
1419
2cd5b3e0
SK
1420 name = irq[1] < 0 ? dev_name(&pdev->dev) : "sh_mmc:error";
1421 ret = request_threaded_irq(irq[0], sh_mmcif_intr, sh_mmcif_irqt, 0, name, host);
fdc50a94 1422 if (ret) {
2cd5b3e0 1423 dev_err(&pdev->dev, "request_irq error (%s)\n", name);
e1aae2eb 1424 goto ereqirq0;
fdc50a94 1425 }
2cd5b3e0
SK
1426 if (irq[1] >= 0) {
1427 ret = request_threaded_irq(irq[1], sh_mmcif_intr, sh_mmcif_irqt,
1428 0, "sh_mmc:int", host);
1429 if (ret) {
1430 dev_err(&pdev->dev, "request_irq error (sh_mmc:int)\n");
1431 goto ereqirq1;
1432 }
fdc50a94
YG
1433 }
1434
e480606a 1435 if (pd && pd->use_cd_gpio) {
214fc309 1436 ret = mmc_gpio_request_cd(mmc, pd->cd_gpio, 0);
e480606a
GL
1437 if (ret < 0)
1438 goto erqcd;
1439 }
1440
8047310e
GL
1441 mutex_init(&host->thread_lock);
1442
b289174f 1443 clk_disable(host->hclk);
5ba85d95
GL
1444 ret = mmc_add_host(mmc);
1445 if (ret < 0)
e1aae2eb 1446 goto emmcaddh;
fdc50a94 1447
efe6a8ad
RW
1448 dev_pm_qos_expose_latency_limit(&pdev->dev, 100);
1449
e47bf32a
GL
1450 dev_info(&pdev->dev, "driver version %s\n", DRIVER_VERSION);
1451 dev_dbg(&pdev->dev, "chip ver H'%04x\n",
487d9fc5 1452 sh_mmcif_readl(host->addr, MMCIF_CE_VERSION) & 0x0000ffff);
fdc50a94
YG
1453 return ret;
1454
e1aae2eb 1455emmcaddh:
e480606a 1456erqcd:
2cd5b3e0
SK
1457 if (irq[1] >= 0)
1458 free_irq(irq[1], host);
e1aae2eb 1459ereqirq1:
5ba85d95 1460 free_irq(irq[0], host);
e1aae2eb 1461ereqirq0:
faca6648 1462 pm_runtime_suspend(&pdev->dev);
e1aae2eb 1463eresume:
fdc50a94 1464 clk_disable(host->hclk);
a6609267 1465eclkupdate:
b289174f 1466 clk_put(host->hclk);
e1aae2eb 1467eclkget:
b289174f 1468 pm_runtime_disable(&pdev->dev);
2c9054dc 1469eofparse:
fdc50a94 1470 mmc_free_host(mmc);
e1aae2eb
GL
1471ealloch:
1472 iounmap(reg);
fdc50a94
YG
1473 return ret;
1474}
1475
6e0ee714 1476static int sh_mmcif_remove(struct platform_device *pdev)
fdc50a94
YG
1477{
1478 struct sh_mmcif_host *host = platform_get_drvdata(pdev);
1479 int irq[2];
1480
f985da17 1481 host->dying = true;
b289174f 1482 clk_enable(host->hclk);
faca6648 1483 pm_runtime_get_sync(&pdev->dev);
fdc50a94 1484
efe6a8ad
RW
1485 dev_pm_qos_hide_latency_limit(&pdev->dev);
1486
faca6648 1487 mmc_remove_host(host->mmc);
3b0beafc
GL
1488 sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL);
1489
f985da17
GL
1490 /*
1491 * FIXME: cancel_delayed_work(_sync)() and free_irq() race with the
1492 * mmc_remove_host() call above. But swapping order doesn't help either
1493 * (a query on the linux-mmc mailing list didn't bring any replies).
1494 */
1495 cancel_delayed_work_sync(&host->timeout_work);
1496
fdc50a94
YG
1497 if (host->addr)
1498 iounmap(host->addr);
1499
aa0787a9
GL
1500 irq[0] = platform_get_irq(pdev, 0);
1501 irq[1] = platform_get_irq(pdev, 1);
fdc50a94
YG
1502
1503 free_irq(irq[0], host);
2cd5b3e0
SK
1504 if (irq[1] >= 0)
1505 free_irq(irq[1], host);
fdc50a94 1506
a0d28ba0 1507 clk_disable(host->hclk);
fdc50a94 1508 mmc_free_host(host->mmc);
faca6648
GL
1509 pm_runtime_put_sync(&pdev->dev);
1510 pm_runtime_disable(&pdev->dev);
fdc50a94
YG
1511
1512 return 0;
1513}
1514
faca6648
GL
1515#ifdef CONFIG_PM
1516static int sh_mmcif_suspend(struct device *dev)
1517{
b289174f 1518 struct sh_mmcif_host *host = dev_get_drvdata(dev);
faca6648
GL
1519 int ret = mmc_suspend_host(host->mmc);
1520
b289174f 1521 if (!ret)
faca6648 1522 sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL);
faca6648
GL
1523
1524 return ret;
1525}
1526
1527static int sh_mmcif_resume(struct device *dev)
1528{
b289174f 1529 struct sh_mmcif_host *host = dev_get_drvdata(dev);
faca6648
GL
1530
1531 return mmc_resume_host(host->mmc);
1532}
1533#else
1534#define sh_mmcif_suspend NULL
1535#define sh_mmcif_resume NULL
1536#endif /* CONFIG_PM */
1537
bf68a812
GL
1538static const struct of_device_id mmcif_of_match[] = {
1539 { .compatible = "renesas,sh-mmcif" },
1540 { }
1541};
1542MODULE_DEVICE_TABLE(of, mmcif_of_match);
1543
faca6648
GL
1544static const struct dev_pm_ops sh_mmcif_dev_pm_ops = {
1545 .suspend = sh_mmcif_suspend,
1546 .resume = sh_mmcif_resume,
1547};
1548
fdc50a94
YG
1549static struct platform_driver sh_mmcif_driver = {
1550 .probe = sh_mmcif_probe,
1551 .remove = sh_mmcif_remove,
1552 .driver = {
1553 .name = DRIVER_NAME,
faca6648 1554 .pm = &sh_mmcif_dev_pm_ops,
bf68a812
GL
1555 .owner = THIS_MODULE,
1556 .of_match_table = mmcif_of_match,
fdc50a94
YG
1557 },
1558};
1559
d1f81a64 1560module_platform_driver(sh_mmcif_driver);
fdc50a94
YG
1561
1562MODULE_DESCRIPTION("SuperH on-chip MMC/eMMC interface driver");
1563MODULE_LICENSE("GPL");
aa0787a9 1564MODULE_ALIAS("platform:" DRIVER_NAME);
fdc50a94 1565MODULE_AUTHOR("Yusuke Goda <yusuke.goda.sx@renesas.com>");