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