blob: 0f07d2878c4919e0f0c30decb0d11d54a162a5f3 [file] [log] [blame]
Yusuke Godafdc50a92010-05-26 14:41:59 -07001/*
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
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +010019/*
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
Guennadi Liakhovetski86df1742011-11-23 15:52:30 +010045#include <linux/bitops.h>
Guennadi Liakhovetskiaa0787a2010-11-24 10:05:12 +000046#include <linux/clk.h>
47#include <linux/completion.h>
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +000048#include <linux/delay.h>
Yusuke Godafdc50a92010-05-26 14:41:59 -070049#include <linux/dma-mapping.h>
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +000050#include <linux/dmaengine.h>
Yusuke Godafdc50a92010-05-26 14:41:59 -070051#include <linux/mmc/card.h>
52#include <linux/mmc/core.h>
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +000053#include <linux/mmc/host.h>
Yusuke Godafdc50a92010-05-26 14:41:59 -070054#include <linux/mmc/mmc.h>
55#include <linux/mmc/sdio.h>
Yusuke Godafdc50a92010-05-26 14:41:59 -070056#include <linux/mmc/sh_mmcif.h>
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +000057#include <linux/pagemap.h>
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +000058#include <linux/platform_device.h>
Rafael J. Wysockiefe6a8a2012-03-13 01:02:15 +010059#include <linux/pm_qos.h>
Guennadi Liakhovetskifaca6642011-05-05 16:20:48 +000060#include <linux/pm_runtime.h>
Guennadi Liakhovetski3b0beaf2011-04-15 18:30:47 +000061#include <linux/spinlock.h>
Paul Gortmaker88b47672011-07-03 15:15:51 -040062#include <linux/module.h>
Yusuke Godafdc50a92010-05-26 14:41:59 -070063
64#define DRIVER_NAME "sh_mmcif"
65#define DRIVER_VERSION "2010-04-28"
66
Yusuke Godafdc50a92010-05-26 14:41:59 -070067/* CE_CMD_SET */
68#define CMD_MASK 0x3f000000
69#define CMD_SET_RTYP_NO ((0 << 23) | (0 << 22))
70#define CMD_SET_RTYP_6B ((0 << 23) | (1 << 22)) /* R1/R1b/R3/R4/R5 */
71#define CMD_SET_RTYP_17B ((1 << 23) | (0 << 22)) /* R2 */
72#define CMD_SET_RBSY (1 << 21) /* R1b */
73#define CMD_SET_CCSEN (1 << 20)
74#define CMD_SET_WDAT (1 << 19) /* 1: on data, 0: no data */
75#define CMD_SET_DWEN (1 << 18) /* 1: write, 0: read */
76#define CMD_SET_CMLTE (1 << 17) /* 1: multi block trans, 0: single */
77#define CMD_SET_CMD12EN (1 << 16) /* 1: CMD12 auto issue */
78#define CMD_SET_RIDXC_INDEX ((0 << 15) | (0 << 14)) /* index check */
79#define CMD_SET_RIDXC_BITS ((0 << 15) | (1 << 14)) /* check bits check */
80#define CMD_SET_RIDXC_NO ((1 << 15) | (0 << 14)) /* no check */
81#define CMD_SET_CRC7C ((0 << 13) | (0 << 12)) /* CRC7 check*/
82#define CMD_SET_CRC7C_BITS ((0 << 13) | (1 << 12)) /* check bits check*/
83#define CMD_SET_CRC7C_INTERNAL ((1 << 13) | (0 << 12)) /* internal CRC7 check*/
84#define CMD_SET_CRC16C (1 << 10) /* 0: CRC16 check*/
85#define CMD_SET_CRCSTE (1 << 8) /* 1: not receive CRC status */
86#define CMD_SET_TBIT (1 << 7) /* 1: tran mission bit "Low" */
87#define CMD_SET_OPDM (1 << 6) /* 1: open/drain */
88#define CMD_SET_CCSH (1 << 5)
89#define CMD_SET_DATW_1 ((0 << 1) | (0 << 0)) /* 1bit */
90#define CMD_SET_DATW_4 ((0 << 1) | (1 << 0)) /* 4bit */
91#define CMD_SET_DATW_8 ((1 << 1) | (0 << 0)) /* 8bit */
92
93/* CE_CMD_CTRL */
94#define CMD_CTRL_BREAK (1 << 0)
95
96/* CE_BLOCK_SET */
97#define BLOCK_SIZE_MASK 0x0000ffff
98
Yusuke Godafdc50a92010-05-26 14:41:59 -070099/* CE_INT */
100#define INT_CCSDE (1 << 29)
101#define INT_CMD12DRE (1 << 26)
102#define INT_CMD12RBE (1 << 25)
103#define INT_CMD12CRE (1 << 24)
104#define INT_DTRANE (1 << 23)
105#define INT_BUFRE (1 << 22)
106#define INT_BUFWEN (1 << 21)
107#define INT_BUFREN (1 << 20)
108#define INT_CCSRCV (1 << 19)
109#define INT_RBSYE (1 << 17)
110#define INT_CRSPE (1 << 16)
111#define INT_CMDVIO (1 << 15)
112#define INT_BUFVIO (1 << 14)
113#define INT_WDATERR (1 << 11)
114#define INT_RDATERR (1 << 10)
115#define INT_RIDXERR (1 << 9)
116#define INT_RSPERR (1 << 8)
117#define INT_CCSTO (1 << 5)
118#define INT_CRCSTO (1 << 4)
119#define INT_WDATTO (1 << 3)
120#define INT_RDATTO (1 << 2)
121#define INT_RBSYTO (1 << 1)
122#define INT_RSPTO (1 << 0)
123#define INT_ERR_STS (INT_CMDVIO | INT_BUFVIO | INT_WDATERR | \
124 INT_RDATERR | INT_RIDXERR | INT_RSPERR | \
125 INT_CCSTO | INT_CRCSTO | INT_WDATTO | \
126 INT_RDATTO | INT_RBSYTO | INT_RSPTO)
127
128/* CE_INT_MASK */
129#define MASK_ALL 0x00000000
130#define MASK_MCCSDE (1 << 29)
131#define MASK_MCMD12DRE (1 << 26)
132#define MASK_MCMD12RBE (1 << 25)
133#define MASK_MCMD12CRE (1 << 24)
134#define MASK_MDTRANE (1 << 23)
135#define MASK_MBUFRE (1 << 22)
136#define MASK_MBUFWEN (1 << 21)
137#define MASK_MBUFREN (1 << 20)
138#define MASK_MCCSRCV (1 << 19)
139#define MASK_MRBSYE (1 << 17)
140#define MASK_MCRSPE (1 << 16)
141#define MASK_MCMDVIO (1 << 15)
142#define MASK_MBUFVIO (1 << 14)
143#define MASK_MWDATERR (1 << 11)
144#define MASK_MRDATERR (1 << 10)
145#define MASK_MRIDXERR (1 << 9)
146#define MASK_MRSPERR (1 << 8)
147#define MASK_MCCSTO (1 << 5)
148#define MASK_MCRCSTO (1 << 4)
149#define MASK_MWDATTO (1 << 3)
150#define MASK_MRDATTO (1 << 2)
151#define MASK_MRBSYTO (1 << 1)
152#define MASK_MRSPTO (1 << 0)
153
Guennadi Liakhovetskiee4b8882011-12-14 19:31:52 +0100154#define MASK_START_CMD (MASK_MCMDVIO | MASK_MBUFVIO | MASK_MWDATERR | \
155 MASK_MRDATERR | MASK_MRIDXERR | MASK_MRSPERR | \
156 MASK_MCCSTO | MASK_MCRCSTO | MASK_MWDATTO | \
157 MASK_MRDATTO | MASK_MRBSYTO | MASK_MRSPTO)
158
Yusuke Godafdc50a92010-05-26 14:41:59 -0700159/* CE_HOST_STS1 */
160#define STS1_CMDSEQ (1 << 31)
161
162/* CE_HOST_STS2 */
163#define STS2_CRCSTE (1 << 31)
164#define STS2_CRC16E (1 << 30)
165#define STS2_AC12CRCE (1 << 29)
166#define STS2_RSPCRC7E (1 << 28)
167#define STS2_CRCSTEBE (1 << 27)
168#define STS2_RDATEBE (1 << 26)
169#define STS2_AC12REBE (1 << 25)
170#define STS2_RSPEBE (1 << 24)
171#define STS2_AC12IDXE (1 << 23)
172#define STS2_RSPIDXE (1 << 22)
173#define STS2_CCSTO (1 << 15)
174#define STS2_RDATTO (1 << 14)
175#define STS2_DATBSYTO (1 << 13)
176#define STS2_CRCSTTO (1 << 12)
177#define STS2_AC12BSYTO (1 << 11)
178#define STS2_RSPBSYTO (1 << 10)
179#define STS2_AC12RSPTO (1 << 9)
180#define STS2_RSPTO (1 << 8)
181#define STS2_CRC_ERR (STS2_CRCSTE | STS2_CRC16E | \
182 STS2_AC12CRCE | STS2_RSPCRC7E | STS2_CRCSTEBE)
183#define STS2_TIMEOUT_ERR (STS2_CCSTO | STS2_RDATTO | \
184 STS2_DATBSYTO | STS2_CRCSTTO | \
185 STS2_AC12BSYTO | STS2_RSPBSYTO | \
186 STS2_AC12RSPTO | STS2_RSPTO)
187
Yusuke Godafdc50a92010-05-26 14:41:59 -0700188#define CLKDEV_EMMC_DATA 52000000 /* 52MHz */
189#define CLKDEV_MMC_DATA 20000000 /* 20MHz */
190#define CLKDEV_INIT 400000 /* 400 KHz */
191
Guennadi Liakhovetski3b0beaf2011-04-15 18:30:47 +0000192enum mmcif_state {
193 STATE_IDLE,
194 STATE_REQUEST,
195 STATE_IOS,
196};
197
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100198enum mmcif_wait_for {
199 MMCIF_WAIT_FOR_REQUEST,
200 MMCIF_WAIT_FOR_CMD,
201 MMCIF_WAIT_FOR_MREAD,
202 MMCIF_WAIT_FOR_MWRITE,
203 MMCIF_WAIT_FOR_READ,
204 MMCIF_WAIT_FOR_WRITE,
205 MMCIF_WAIT_FOR_READ_END,
206 MMCIF_WAIT_FOR_WRITE_END,
207 MMCIF_WAIT_FOR_STOP,
208};
209
Yusuke Godafdc50a92010-05-26 14:41:59 -0700210struct sh_mmcif_host {
211 struct mmc_host *mmc;
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100212 struct mmc_request *mrq;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700213 struct platform_device *pd;
214 struct clk *hclk;
215 unsigned int clk;
216 int bus_width;
Guennadi Liakhovetskiaa0787a2010-11-24 10:05:12 +0000217 bool sd_error;
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100218 bool dying;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700219 long timeout;
220 void __iomem *addr;
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100221 u32 *pio_ptr;
Guennadi Liakhovetskiee4b8882011-12-14 19:31:52 +0100222 spinlock_t lock; /* protect sh_mmcif_host::state */
Guennadi Liakhovetski3b0beaf2011-04-15 18:30:47 +0000223 enum mmcif_state state;
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100224 enum mmcif_wait_for wait_for;
225 struct delayed_work timeout_work;
226 size_t blocksize;
227 int sg_idx;
228 int sg_blkidx;
Guennadi Liakhovetskifaca6642011-05-05 16:20:48 +0000229 bool power;
Guennadi Liakhovetskic9b0cef2011-05-26 15:33:30 +0200230 bool card_present;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700231
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000232 /* DMA support */
233 struct dma_chan *chan_rx;
234 struct dma_chan *chan_tx;
235 struct completion dma_complete;
Linus Walleijf38f94c2011-02-10 16:09:50 +0100236 bool dma_active;
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000237};
Yusuke Godafdc50a92010-05-26 14:41:59 -0700238
239static inline void sh_mmcif_bitset(struct sh_mmcif_host *host,
240 unsigned int reg, u32 val)
241{
Magnus Damm487d9fc2010-05-18 14:42:51 +0000242 writel(val | readl(host->addr + reg), host->addr + reg);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700243}
244
245static inline void sh_mmcif_bitclr(struct sh_mmcif_host *host,
246 unsigned int reg, u32 val)
247{
Magnus Damm487d9fc2010-05-18 14:42:51 +0000248 writel(~val & readl(host->addr + reg), host->addr + reg);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700249}
250
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000251static void mmcif_dma_complete(void *arg)
252{
253 struct sh_mmcif_host *host = arg;
Guennadi Liakhovetski69983402011-12-26 12:52:13 -0500254 struct mmc_data *data = host->mrq->data;
255
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000256 dev_dbg(&host->pd->dev, "Command completed\n");
257
Guennadi Liakhovetski69983402011-12-26 12:52:13 -0500258 if (WARN(!data, "%s: NULL data in DMA completion!\n",
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000259 dev_name(&host->pd->dev)))
260 return;
261
Guennadi Liakhovetski69983402011-12-26 12:52:13 -0500262 if (data->flags & MMC_DATA_READ)
Linus Walleij1ed828d2011-02-10 16:09:29 +0100263 dma_unmap_sg(host->chan_rx->device->dev,
Guennadi Liakhovetski69983402011-12-26 12:52:13 -0500264 data->sg, data->sg_len,
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000265 DMA_FROM_DEVICE);
266 else
Linus Walleij1ed828d2011-02-10 16:09:29 +0100267 dma_unmap_sg(host->chan_tx->device->dev,
Guennadi Liakhovetski69983402011-12-26 12:52:13 -0500268 data->sg, data->sg_len,
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000269 DMA_TO_DEVICE);
270
271 complete(&host->dma_complete);
272}
273
274static void sh_mmcif_start_dma_rx(struct sh_mmcif_host *host)
275{
Guennadi Liakhovetski69983402011-12-26 12:52:13 -0500276 struct mmc_data *data = host->mrq->data;
277 struct scatterlist *sg = data->sg;
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000278 struct dma_async_tx_descriptor *desc = NULL;
279 struct dma_chan *chan = host->chan_rx;
280 dma_cookie_t cookie = -EINVAL;
281 int ret;
282
Guennadi Liakhovetski69983402011-12-26 12:52:13 -0500283 ret = dma_map_sg(chan->device->dev, sg, data->sg_len,
Linus Walleij1ed828d2011-02-10 16:09:29 +0100284 DMA_FROM_DEVICE);
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000285 if (ret > 0) {
Linus Walleijf38f94c2011-02-10 16:09:50 +0100286 host->dma_active = true;
Alexandre Bounine16052822012-03-08 16:11:18 -0500287 desc = dmaengine_prep_slave_sg(chan, sg, ret,
Vinod Koul05f57992011-10-14 10:45:11 +0530288 DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000289 }
290
291 if (desc) {
292 desc->callback = mmcif_dma_complete;
293 desc->callback_param = host;
Linus Walleija5ece7d2011-02-10 16:10:00 +0100294 cookie = dmaengine_submit(desc);
295 sh_mmcif_bitset(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN);
296 dma_async_issue_pending(chan);
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000297 }
298 dev_dbg(&host->pd->dev, "%s(): mapped %d -> %d, cookie %d\n",
Guennadi Liakhovetski69983402011-12-26 12:52:13 -0500299 __func__, data->sg_len, ret, cookie);
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000300
301 if (!desc) {
302 /* DMA failed, fall back to PIO */
303 if (ret >= 0)
304 ret = -EIO;
305 host->chan_rx = NULL;
Linus Walleijf38f94c2011-02-10 16:09:50 +0100306 host->dma_active = false;
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000307 dma_release_channel(chan);
308 /* Free the Tx channel too */
309 chan = host->chan_tx;
310 if (chan) {
311 host->chan_tx = NULL;
312 dma_release_channel(chan);
313 }
314 dev_warn(&host->pd->dev,
315 "DMA failed: %d, falling back to PIO\n", ret);
316 sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN | BUF_ACC_DMAWEN);
317 }
318
319 dev_dbg(&host->pd->dev, "%s(): desc %p, cookie %d, sg[%d]\n", __func__,
Guennadi Liakhovetski69983402011-12-26 12:52:13 -0500320 desc, cookie, data->sg_len);
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000321}
322
323static void sh_mmcif_start_dma_tx(struct sh_mmcif_host *host)
324{
Guennadi Liakhovetski69983402011-12-26 12:52:13 -0500325 struct mmc_data *data = host->mrq->data;
326 struct scatterlist *sg = data->sg;
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000327 struct dma_async_tx_descriptor *desc = NULL;
328 struct dma_chan *chan = host->chan_tx;
329 dma_cookie_t cookie = -EINVAL;
330 int ret;
331
Guennadi Liakhovetski69983402011-12-26 12:52:13 -0500332 ret = dma_map_sg(chan->device->dev, sg, data->sg_len,
Linus Walleij1ed828d2011-02-10 16:09:29 +0100333 DMA_TO_DEVICE);
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000334 if (ret > 0) {
Linus Walleijf38f94c2011-02-10 16:09:50 +0100335 host->dma_active = true;
Alexandre Bounine16052822012-03-08 16:11:18 -0500336 desc = dmaengine_prep_slave_sg(chan, sg, ret,
Vinod Koul05f57992011-10-14 10:45:11 +0530337 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000338 }
339
340 if (desc) {
341 desc->callback = mmcif_dma_complete;
342 desc->callback_param = host;
Linus Walleija5ece7d2011-02-10 16:10:00 +0100343 cookie = dmaengine_submit(desc);
344 sh_mmcif_bitset(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAWEN);
345 dma_async_issue_pending(chan);
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000346 }
347 dev_dbg(&host->pd->dev, "%s(): mapped %d -> %d, cookie %d\n",
Guennadi Liakhovetski69983402011-12-26 12:52:13 -0500348 __func__, data->sg_len, ret, cookie);
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000349
350 if (!desc) {
351 /* DMA failed, fall back to PIO */
352 if (ret >= 0)
353 ret = -EIO;
354 host->chan_tx = NULL;
Linus Walleijf38f94c2011-02-10 16:09:50 +0100355 host->dma_active = false;
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000356 dma_release_channel(chan);
357 /* Free the Rx channel too */
358 chan = host->chan_rx;
359 if (chan) {
360 host->chan_rx = NULL;
361 dma_release_channel(chan);
362 }
363 dev_warn(&host->pd->dev,
364 "DMA failed: %d, falling back to PIO\n", ret);
365 sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN | BUF_ACC_DMAWEN);
366 }
367
368 dev_dbg(&host->pd->dev, "%s(): desc %p, cookie %d\n", __func__,
369 desc, cookie);
370}
371
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000372static void sh_mmcif_request_dma(struct sh_mmcif_host *host,
373 struct sh_mmcif_plat_data *pdata)
374{
Guennadi Liakhovetski0e79f9a2012-07-05 12:29:43 +0200375 struct resource *res = platform_get_resource(host->pd, IORESOURCE_MEM, 0);
376 struct dma_slave_config cfg;
377 dma_cap_mask_t mask;
378 int ret;
379
Linus Walleijf38f94c2011-02-10 16:09:50 +0100380 host->dma_active = false;
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000381
Guennadi Liakhovetski0e79f9a2012-07-05 12:29:43 +0200382 if (pdata->slave_id_tx <= 0 || pdata->slave_id_rx <= 0)
383 return;
384
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000385 /* We can only either use DMA for both Tx and Rx or not use it at all */
Guennadi Liakhovetski0e79f9a2012-07-05 12:29:43 +0200386 dma_cap_zero(mask);
387 dma_cap_set(DMA_SLAVE, mask);
Guennadi Liakhovetski916001f2012-05-09 17:09:15 +0200388
Guennadi Liakhovetski0e79f9a2012-07-05 12:29:43 +0200389 host->chan_tx = dma_request_channel(mask, shdma_chan_filter,
390 (void *)pdata->slave_id_tx);
391 dev_dbg(&host->pd->dev, "%s: TX: got channel %p\n", __func__,
392 host->chan_tx);
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000393
Guennadi Liakhovetski0e79f9a2012-07-05 12:29:43 +0200394 if (!host->chan_tx)
395 return;
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000396
Guennadi Liakhovetski0e79f9a2012-07-05 12:29:43 +0200397 cfg.slave_id = pdata->slave_id_tx;
398 cfg.direction = DMA_MEM_TO_DEV;
399 cfg.dst_addr = res->start + MMCIF_CE_DATA;
400 cfg.src_addr = 0;
401 ret = dmaengine_slave_config(host->chan_tx, &cfg);
402 if (ret < 0)
403 goto ecfgtx;
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000404
Guennadi Liakhovetski0e79f9a2012-07-05 12:29:43 +0200405 host->chan_rx = dma_request_channel(mask, shdma_chan_filter,
406 (void *)pdata->slave_id_rx);
407 dev_dbg(&host->pd->dev, "%s: RX: got channel %p\n", __func__,
408 host->chan_rx);
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000409
Guennadi Liakhovetski0e79f9a2012-07-05 12:29:43 +0200410 if (!host->chan_rx)
411 goto erqrx;
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000412
Guennadi Liakhovetski0e79f9a2012-07-05 12:29:43 +0200413 cfg.slave_id = pdata->slave_id_rx;
414 cfg.direction = DMA_DEV_TO_MEM;
415 cfg.dst_addr = 0;
416 cfg.src_addr = res->start + MMCIF_CE_DATA;
417 ret = dmaengine_slave_config(host->chan_rx, &cfg);
418 if (ret < 0)
419 goto ecfgrx;
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000420
Guennadi Liakhovetski0e79f9a2012-07-05 12:29:43 +0200421 init_completion(&host->dma_complete);
422
423 return;
424
425ecfgrx:
426 dma_release_channel(host->chan_rx);
427 host->chan_rx = NULL;
428erqrx:
429ecfgtx:
430 dma_release_channel(host->chan_tx);
431 host->chan_tx = NULL;
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000432}
433
434static void sh_mmcif_release_dma(struct sh_mmcif_host *host)
435{
436 sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN | BUF_ACC_DMAWEN);
437 /* Descriptors are freed automatically */
438 if (host->chan_tx) {
439 struct dma_chan *chan = host->chan_tx;
440 host->chan_tx = NULL;
441 dma_release_channel(chan);
442 }
443 if (host->chan_rx) {
444 struct dma_chan *chan = host->chan_rx;
445 host->chan_rx = NULL;
446 dma_release_channel(chan);
447 }
448
Linus Walleijf38f94c2011-02-10 16:09:50 +0100449 host->dma_active = false;
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000450}
Yusuke Godafdc50a92010-05-26 14:41:59 -0700451
452static void sh_mmcif_clock_control(struct sh_mmcif_host *host, unsigned int clk)
453{
454 struct sh_mmcif_plat_data *p = host->pd->dev.platform_data;
455
456 sh_mmcif_bitclr(host, MMCIF_CE_CLK_CTRL, CLK_ENABLE);
457 sh_mmcif_bitclr(host, MMCIF_CE_CLK_CTRL, CLK_CLEAR);
458
459 if (!clk)
460 return;
461 if (p->sup_pclk && clk == host->clk)
462 sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_SUP_PCLK);
463 else
464 sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_CLEAR &
Simon Hormanf9388252012-03-28 18:01:09 +0900465 ((fls(DIV_ROUND_UP(host->clk,
466 clk) - 1) - 1) << 16));
Yusuke Godafdc50a92010-05-26 14:41:59 -0700467
468 sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_ENABLE);
469}
470
471static void sh_mmcif_sync_reset(struct sh_mmcif_host *host)
472{
473 u32 tmp;
474
Magnus Damm487d9fc2010-05-18 14:42:51 +0000475 tmp = 0x010f0000 & sh_mmcif_readl(host->addr, MMCIF_CE_CLK_CTRL);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700476
Magnus Damm487d9fc2010-05-18 14:42:51 +0000477 sh_mmcif_writel(host->addr, MMCIF_CE_VERSION, SOFT_RST_ON);
478 sh_mmcif_writel(host->addr, MMCIF_CE_VERSION, SOFT_RST_OFF);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700479 sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, tmp |
480 SRSPTO_256 | SRBSYTO_29 | SRWDTO_29 | SCCSTO_29);
481 /* byte swap on */
482 sh_mmcif_bitset(host, MMCIF_CE_BUF_ACC, BUF_ACC_ATYP);
483}
484
485static int sh_mmcif_error_manage(struct sh_mmcif_host *host)
486{
487 u32 state1, state2;
Guennadi Liakhovetskiee4b8882011-12-14 19:31:52 +0100488 int ret, timeout;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700489
Guennadi Liakhovetskiaa0787a2010-11-24 10:05:12 +0000490 host->sd_error = false;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700491
Magnus Damm487d9fc2010-05-18 14:42:51 +0000492 state1 = sh_mmcif_readl(host->addr, MMCIF_CE_HOST_STS1);
493 state2 = sh_mmcif_readl(host->addr, MMCIF_CE_HOST_STS2);
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +0000494 dev_dbg(&host->pd->dev, "ERR HOST_STS1 = %08x\n", state1);
495 dev_dbg(&host->pd->dev, "ERR HOST_STS2 = %08x\n", state2);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700496
497 if (state1 & STS1_CMDSEQ) {
498 sh_mmcif_bitset(host, MMCIF_CE_CMD_CTRL, CMD_CTRL_BREAK);
499 sh_mmcif_bitset(host, MMCIF_CE_CMD_CTRL, ~CMD_CTRL_BREAK);
Guennadi Liakhovetskiee4b8882011-12-14 19:31:52 +0100500 for (timeout = 10000000; timeout; timeout--) {
Magnus Damm487d9fc2010-05-18 14:42:51 +0000501 if (!(sh_mmcif_readl(host->addr, MMCIF_CE_HOST_STS1)
Guennadi Liakhovetskiee4b8882011-12-14 19:31:52 +0100502 & STS1_CMDSEQ))
Yusuke Godafdc50a92010-05-26 14:41:59 -0700503 break;
504 mdelay(1);
505 }
Guennadi Liakhovetskiee4b8882011-12-14 19:31:52 +0100506 if (!timeout) {
507 dev_err(&host->pd->dev,
508 "Forced end of command sequence timeout err\n");
509 return -EIO;
510 }
Yusuke Godafdc50a92010-05-26 14:41:59 -0700511 sh_mmcif_sync_reset(host);
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +0000512 dev_dbg(&host->pd->dev, "Forced end of command sequence\n");
Yusuke Godafdc50a92010-05-26 14:41:59 -0700513 return -EIO;
514 }
515
516 if (state2 & STS2_CRC_ERR) {
Guennadi Liakhovetskiee4b8882011-12-14 19:31:52 +0100517 dev_dbg(&host->pd->dev, ": CRC error\n");
Yusuke Godafdc50a92010-05-26 14:41:59 -0700518 ret = -EIO;
519 } else if (state2 & STS2_TIMEOUT_ERR) {
Guennadi Liakhovetskiee4b8882011-12-14 19:31:52 +0100520 dev_dbg(&host->pd->dev, ": Timeout\n");
Yusuke Godafdc50a92010-05-26 14:41:59 -0700521 ret = -ETIMEDOUT;
522 } else {
Guennadi Liakhovetskiee4b8882011-12-14 19:31:52 +0100523 dev_dbg(&host->pd->dev, ": End/Index error\n");
Yusuke Godafdc50a92010-05-26 14:41:59 -0700524 ret = -EIO;
525 }
526 return ret;
527}
528
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100529static bool sh_mmcif_next_block(struct sh_mmcif_host *host, u32 *p)
Yusuke Godafdc50a92010-05-26 14:41:59 -0700530{
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100531 struct mmc_data *data = host->mrq->data;
532
533 host->sg_blkidx += host->blocksize;
534
535 /* data->sg->length must be a multiple of host->blocksize? */
536 BUG_ON(host->sg_blkidx > data->sg->length);
537
538 if (host->sg_blkidx == data->sg->length) {
539 host->sg_blkidx = 0;
540 if (++host->sg_idx < data->sg_len)
541 host->pio_ptr = sg_virt(++data->sg);
542 } else {
543 host->pio_ptr = p;
544 }
545
546 if (host->sg_idx == data->sg_len)
547 return false;
548
549 return true;
550}
551
552static void sh_mmcif_single_read(struct sh_mmcif_host *host,
553 struct mmc_request *mrq)
554{
555 host->blocksize = (sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) &
556 BLOCK_SIZE_MASK) + 3;
557
558 host->wait_for = MMCIF_WAIT_FOR_READ;
559 schedule_delayed_work(&host->timeout_work, host->timeout);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700560
Yusuke Godafdc50a92010-05-26 14:41:59 -0700561 /* buf read enable */
562 sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFREN);
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100563}
Yusuke Godafdc50a92010-05-26 14:41:59 -0700564
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100565static bool sh_mmcif_read_block(struct sh_mmcif_host *host)
566{
567 struct mmc_data *data = host->mrq->data;
568 u32 *p = sg_virt(data->sg);
569 int i;
570
571 if (host->sd_error) {
572 data->error = sh_mmcif_error_manage(host);
573 return false;
574 }
575
576 for (i = 0; i < host->blocksize / 4; i++)
Magnus Damm487d9fc2010-05-18 14:42:51 +0000577 *p++ = sh_mmcif_readl(host->addr, MMCIF_CE_DATA);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700578
579 /* buffer read end */
580 sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFRE);
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100581 host->wait_for = MMCIF_WAIT_FOR_READ_END;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700582
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100583 return true;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700584}
585
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100586static void sh_mmcif_multi_read(struct sh_mmcif_host *host,
587 struct mmc_request *mrq)
Yusuke Godafdc50a92010-05-26 14:41:59 -0700588{
589 struct mmc_data *data = mrq->data;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700590
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100591 if (!data->sg_len || !data->sg->length)
592 return;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700593
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100594 host->blocksize = sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) &
595 BLOCK_SIZE_MASK;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700596
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100597 host->wait_for = MMCIF_WAIT_FOR_MREAD;
598 host->sg_idx = 0;
599 host->sg_blkidx = 0;
600 host->pio_ptr = sg_virt(data->sg);
601 schedule_delayed_work(&host->timeout_work, host->timeout);
602 sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFREN);
603}
604
605static bool sh_mmcif_mread_block(struct sh_mmcif_host *host)
606{
607 struct mmc_data *data = host->mrq->data;
608 u32 *p = host->pio_ptr;
609 int i;
610
611 if (host->sd_error) {
612 data->error = sh_mmcif_error_manage(host);
613 return false;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700614 }
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100615
616 BUG_ON(!data->sg->length);
617
618 for (i = 0; i < host->blocksize / 4; i++)
619 *p++ = sh_mmcif_readl(host->addr, MMCIF_CE_DATA);
620
621 if (!sh_mmcif_next_block(host, p))
622 return false;
623
624 schedule_delayed_work(&host->timeout_work, host->timeout);
625 sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFREN);
626
627 return true;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700628}
629
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100630static void sh_mmcif_single_write(struct sh_mmcif_host *host,
Yusuke Godafdc50a92010-05-26 14:41:59 -0700631 struct mmc_request *mrq)
632{
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100633 host->blocksize = (sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) &
634 BLOCK_SIZE_MASK) + 3;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700635
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100636 host->wait_for = MMCIF_WAIT_FOR_WRITE;
637 schedule_delayed_work(&host->timeout_work, host->timeout);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700638
639 /* buf write enable */
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100640 sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN);
641}
Yusuke Godafdc50a92010-05-26 14:41:59 -0700642
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100643static bool sh_mmcif_write_block(struct sh_mmcif_host *host)
644{
645 struct mmc_data *data = host->mrq->data;
646 u32 *p = sg_virt(data->sg);
647 int i;
648
649 if (host->sd_error) {
650 data->error = sh_mmcif_error_manage(host);
651 return false;
652 }
653
654 for (i = 0; i < host->blocksize / 4; i++)
Magnus Damm487d9fc2010-05-18 14:42:51 +0000655 sh_mmcif_writel(host->addr, MMCIF_CE_DATA, *p++);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700656
657 /* buffer write end */
658 sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MDTRANE);
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100659 host->wait_for = MMCIF_WAIT_FOR_WRITE_END;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700660
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100661 return true;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700662}
663
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100664static void sh_mmcif_multi_write(struct sh_mmcif_host *host,
665 struct mmc_request *mrq)
Yusuke Godafdc50a92010-05-26 14:41:59 -0700666{
667 struct mmc_data *data = mrq->data;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700668
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100669 if (!data->sg_len || !data->sg->length)
670 return;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700671
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100672 host->blocksize = sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) &
673 BLOCK_SIZE_MASK;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700674
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100675 host->wait_for = MMCIF_WAIT_FOR_MWRITE;
676 host->sg_idx = 0;
677 host->sg_blkidx = 0;
678 host->pio_ptr = sg_virt(data->sg);
679 schedule_delayed_work(&host->timeout_work, host->timeout);
680 sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN);
681}
Yusuke Godafdc50a92010-05-26 14:41:59 -0700682
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100683static bool sh_mmcif_mwrite_block(struct sh_mmcif_host *host)
684{
685 struct mmc_data *data = host->mrq->data;
686 u32 *p = host->pio_ptr;
687 int i;
688
689 if (host->sd_error) {
690 data->error = sh_mmcif_error_manage(host);
691 return false;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700692 }
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100693
694 BUG_ON(!data->sg->length);
695
696 for (i = 0; i < host->blocksize / 4; i++)
697 sh_mmcif_writel(host->addr, MMCIF_CE_DATA, *p++);
698
699 if (!sh_mmcif_next_block(host, p))
700 return false;
701
702 schedule_delayed_work(&host->timeout_work, host->timeout);
703 sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN);
704
705 return true;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700706}
707
708static void sh_mmcif_get_response(struct sh_mmcif_host *host,
709 struct mmc_command *cmd)
710{
711 if (cmd->flags & MMC_RSP_136) {
Magnus Damm487d9fc2010-05-18 14:42:51 +0000712 cmd->resp[0] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP3);
713 cmd->resp[1] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP2);
714 cmd->resp[2] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP1);
715 cmd->resp[3] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP0);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700716 } else
Magnus Damm487d9fc2010-05-18 14:42:51 +0000717 cmd->resp[0] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP0);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700718}
719
720static void sh_mmcif_get_cmd12response(struct sh_mmcif_host *host,
721 struct mmc_command *cmd)
722{
Magnus Damm487d9fc2010-05-18 14:42:51 +0000723 cmd->resp[0] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP_CMD12);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700724}
725
726static u32 sh_mmcif_set_cmd(struct sh_mmcif_host *host,
Guennadi Liakhovetski69983402011-12-26 12:52:13 -0500727 struct mmc_request *mrq)
Yusuke Godafdc50a92010-05-26 14:41:59 -0700728{
Guennadi Liakhovetski69983402011-12-26 12:52:13 -0500729 struct mmc_data *data = mrq->data;
730 struct mmc_command *cmd = mrq->cmd;
731 u32 opc = cmd->opcode;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700732 u32 tmp = 0;
733
734 /* Response Type check */
735 switch (mmc_resp_type(cmd)) {
736 case MMC_RSP_NONE:
737 tmp |= CMD_SET_RTYP_NO;
738 break;
739 case MMC_RSP_R1:
740 case MMC_RSP_R1B:
741 case MMC_RSP_R3:
742 tmp |= CMD_SET_RTYP_6B;
743 break;
744 case MMC_RSP_R2:
745 tmp |= CMD_SET_RTYP_17B;
746 break;
747 default:
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +0000748 dev_err(&host->pd->dev, "Unsupported response type.\n");
Yusuke Godafdc50a92010-05-26 14:41:59 -0700749 break;
750 }
751 switch (opc) {
752 /* RBSY */
753 case MMC_SWITCH:
754 case MMC_STOP_TRANSMISSION:
755 case MMC_SET_WRITE_PROT:
756 case MMC_CLR_WRITE_PROT:
757 case MMC_ERASE:
Yusuke Godafdc50a92010-05-26 14:41:59 -0700758 tmp |= CMD_SET_RBSY;
759 break;
760 }
761 /* WDAT / DATW */
Guennadi Liakhovetski69983402011-12-26 12:52:13 -0500762 if (data) {
Yusuke Godafdc50a92010-05-26 14:41:59 -0700763 tmp |= CMD_SET_WDAT;
764 switch (host->bus_width) {
765 case MMC_BUS_WIDTH_1:
766 tmp |= CMD_SET_DATW_1;
767 break;
768 case MMC_BUS_WIDTH_4:
769 tmp |= CMD_SET_DATW_4;
770 break;
771 case MMC_BUS_WIDTH_8:
772 tmp |= CMD_SET_DATW_8;
773 break;
774 default:
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +0000775 dev_err(&host->pd->dev, "Unsupported bus width.\n");
Yusuke Godafdc50a92010-05-26 14:41:59 -0700776 break;
777 }
778 }
779 /* DWEN */
780 if (opc == MMC_WRITE_BLOCK || opc == MMC_WRITE_MULTIPLE_BLOCK)
781 tmp |= CMD_SET_DWEN;
782 /* CMLTE/CMD12EN */
783 if (opc == MMC_READ_MULTIPLE_BLOCK || opc == MMC_WRITE_MULTIPLE_BLOCK) {
784 tmp |= CMD_SET_CMLTE | CMD_SET_CMD12EN;
785 sh_mmcif_bitset(host, MMCIF_CE_BLOCK_SET,
Guennadi Liakhovetski69983402011-12-26 12:52:13 -0500786 data->blocks << 16);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700787 }
788 /* RIDXC[1:0] check bits */
789 if (opc == MMC_SEND_OP_COND || opc == MMC_ALL_SEND_CID ||
790 opc == MMC_SEND_CSD || opc == MMC_SEND_CID)
791 tmp |= CMD_SET_RIDXC_BITS;
792 /* RCRC7C[1:0] check bits */
793 if (opc == MMC_SEND_OP_COND)
794 tmp |= CMD_SET_CRC7C_BITS;
795 /* RCRC7C[1:0] internal CRC7 */
796 if (opc == MMC_ALL_SEND_CID ||
797 opc == MMC_SEND_CSD || opc == MMC_SEND_CID)
798 tmp |= CMD_SET_CRC7C_INTERNAL;
799
Guennadi Liakhovetski69983402011-12-26 12:52:13 -0500800 return (opc << 24) | tmp;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700801}
802
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +0000803static int sh_mmcif_data_trans(struct sh_mmcif_host *host,
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100804 struct mmc_request *mrq, u32 opc)
Yusuke Godafdc50a92010-05-26 14:41:59 -0700805{
Yusuke Godafdc50a92010-05-26 14:41:59 -0700806 switch (opc) {
807 case MMC_READ_MULTIPLE_BLOCK:
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100808 sh_mmcif_multi_read(host, mrq);
809 return 0;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700810 case MMC_WRITE_MULTIPLE_BLOCK:
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100811 sh_mmcif_multi_write(host, mrq);
812 return 0;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700813 case MMC_WRITE_BLOCK:
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100814 sh_mmcif_single_write(host, mrq);
815 return 0;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700816 case MMC_READ_SINGLE_BLOCK:
817 case MMC_SEND_EXT_CSD:
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100818 sh_mmcif_single_read(host, mrq);
819 return 0;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700820 default:
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +0000821 dev_err(&host->pd->dev, "UNSUPPORTED CMD = d'%08d\n", opc);
Guennadi Liakhovetskiee4b8882011-12-14 19:31:52 +0100822 return -EINVAL;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700823 }
Yusuke Godafdc50a92010-05-26 14:41:59 -0700824}
825
826static void sh_mmcif_start_cmd(struct sh_mmcif_host *host,
Guennadi Liakhovetskiee4b8882011-12-14 19:31:52 +0100827 struct mmc_request *mrq)
Yusuke Godafdc50a92010-05-26 14:41:59 -0700828{
Guennadi Liakhovetskiee4b8882011-12-14 19:31:52 +0100829 struct mmc_command *cmd = mrq->cmd;
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100830 u32 opc = cmd->opcode;
831 u32 mask;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700832
Yusuke Godafdc50a92010-05-26 14:41:59 -0700833 switch (opc) {
Guennadi Liakhovetskiee4b8882011-12-14 19:31:52 +0100834 /* response busy check */
Yusuke Godafdc50a92010-05-26 14:41:59 -0700835 case MMC_SWITCH:
836 case MMC_STOP_TRANSMISSION:
837 case MMC_SET_WRITE_PROT:
838 case MMC_CLR_WRITE_PROT:
839 case MMC_ERASE:
Guennadi Liakhovetskiee4b8882011-12-14 19:31:52 +0100840 mask = MASK_START_CMD | MASK_MRBSYE;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700841 break;
842 default:
Guennadi Liakhovetskiee4b8882011-12-14 19:31:52 +0100843 mask = MASK_START_CMD | MASK_MCRSPE;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700844 break;
845 }
Yusuke Godafdc50a92010-05-26 14:41:59 -0700846
Guennadi Liakhovetski69983402011-12-26 12:52:13 -0500847 if (mrq->data) {
Magnus Damm487d9fc2010-05-18 14:42:51 +0000848 sh_mmcif_writel(host->addr, MMCIF_CE_BLOCK_SET, 0);
849 sh_mmcif_writel(host->addr, MMCIF_CE_BLOCK_SET,
850 mrq->data->blksz);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700851 }
Guennadi Liakhovetski69983402011-12-26 12:52:13 -0500852 opc = sh_mmcif_set_cmd(host, mrq);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700853
Magnus Damm487d9fc2010-05-18 14:42:51 +0000854 sh_mmcif_writel(host->addr, MMCIF_CE_INT, 0xD80430C0);
855 sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, mask);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700856 /* set arg */
Magnus Damm487d9fc2010-05-18 14:42:51 +0000857 sh_mmcif_writel(host->addr, MMCIF_CE_ARG, cmd->arg);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700858 /* set cmd */
Magnus Damm487d9fc2010-05-18 14:42:51 +0000859 sh_mmcif_writel(host->addr, MMCIF_CE_CMD_SET, opc);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700860
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100861 host->wait_for = MMCIF_WAIT_FOR_CMD;
862 schedule_delayed_work(&host->timeout_work, host->timeout);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700863}
864
865static void sh_mmcif_stop_cmd(struct sh_mmcif_host *host,
Guennadi Liakhovetskiee4b8882011-12-14 19:31:52 +0100866 struct mmc_request *mrq)
Yusuke Godafdc50a92010-05-26 14:41:59 -0700867{
Guennadi Liakhovetski69983402011-12-26 12:52:13 -0500868 switch (mrq->cmd->opcode) {
869 case MMC_READ_MULTIPLE_BLOCK:
Yusuke Godafdc50a92010-05-26 14:41:59 -0700870 sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MCMD12DRE);
Guennadi Liakhovetski69983402011-12-26 12:52:13 -0500871 break;
872 case MMC_WRITE_MULTIPLE_BLOCK:
Yusuke Godafdc50a92010-05-26 14:41:59 -0700873 sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MCMD12RBE);
Guennadi Liakhovetski69983402011-12-26 12:52:13 -0500874 break;
875 default:
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +0000876 dev_err(&host->pd->dev, "unsupported stop cmd\n");
Guennadi Liakhovetski69983402011-12-26 12:52:13 -0500877 mrq->stop->error = sh_mmcif_error_manage(host);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700878 return;
879 }
880
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100881 host->wait_for = MMCIF_WAIT_FOR_STOP;
882 schedule_delayed_work(&host->timeout_work, host->timeout);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700883}
884
885static void sh_mmcif_request(struct mmc_host *mmc, struct mmc_request *mrq)
886{
887 struct sh_mmcif_host *host = mmc_priv(mmc);
Guennadi Liakhovetski3b0beaf2011-04-15 18:30:47 +0000888 unsigned long flags;
889
890 spin_lock_irqsave(&host->lock, flags);
891 if (host->state != STATE_IDLE) {
892 spin_unlock_irqrestore(&host->lock, flags);
893 mrq->cmd->error = -EAGAIN;
894 mmc_request_done(mmc, mrq);
895 return;
896 }
897
898 host->state = STATE_REQUEST;
899 spin_unlock_irqrestore(&host->lock, flags);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700900
901 switch (mrq->cmd->opcode) {
902 /* MMCIF does not support SD/SDIO command */
903 case SD_IO_SEND_OP_COND:
904 case MMC_APP_CMD:
Guennadi Liakhovetski3b0beaf2011-04-15 18:30:47 +0000905 host->state = STATE_IDLE;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700906 mrq->cmd->error = -ETIMEDOUT;
907 mmc_request_done(mmc, mrq);
908 return;
909 case MMC_SEND_EXT_CSD: /* = SD_SEND_IF_COND (8) */
910 if (!mrq->data) {
911 /* send_if_cond cmd (not support) */
Guennadi Liakhovetski3b0beaf2011-04-15 18:30:47 +0000912 host->state = STATE_IDLE;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700913 mrq->cmd->error = -ETIMEDOUT;
914 mmc_request_done(mmc, mrq);
915 return;
916 }
917 break;
918 default:
919 break;
920 }
Yusuke Godafdc50a92010-05-26 14:41:59 -0700921
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100922 host->mrq = mrq;
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100923
924 sh_mmcif_start_cmd(host, mrq);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700925}
926
927static void sh_mmcif_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
928{
929 struct sh_mmcif_host *host = mmc_priv(mmc);
930 struct sh_mmcif_plat_data *p = host->pd->dev.platform_data;
Guennadi Liakhovetski3b0beaf2011-04-15 18:30:47 +0000931 unsigned long flags;
932
933 spin_lock_irqsave(&host->lock, flags);
934 if (host->state != STATE_IDLE) {
935 spin_unlock_irqrestore(&host->lock, flags);
936 return;
937 }
938
939 host->state = STATE_IOS;
940 spin_unlock_irqrestore(&host->lock, flags);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700941
Guennadi Liakhovetskif5e0cec2011-02-25 16:58:38 +0100942 if (ios->power_mode == MMC_POWER_UP) {
Guennadi Liakhovetskic9b0cef2011-05-26 15:33:30 +0200943 if (!host->card_present) {
Guennadi Liakhovetskifaca6642011-05-05 16:20:48 +0000944 /* See if we also get DMA */
945 sh_mmcif_request_dma(host, host->pd->dev.platform_data);
Guennadi Liakhovetskic9b0cef2011-05-26 15:33:30 +0200946 host->card_present = true;
Guennadi Liakhovetskifaca6642011-05-05 16:20:48 +0000947 }
Guennadi Liakhovetskif5e0cec2011-02-25 16:58:38 +0100948 } else if (ios->power_mode == MMC_POWER_OFF || !ios->clock) {
949 /* clock stop */
950 sh_mmcif_clock_control(host, 0);
Guennadi Liakhovetskifaca6642011-05-05 16:20:48 +0000951 if (ios->power_mode == MMC_POWER_OFF) {
Guennadi Liakhovetskic9b0cef2011-05-26 15:33:30 +0200952 if (host->card_present) {
Guennadi Liakhovetskifaca6642011-05-05 16:20:48 +0000953 sh_mmcif_release_dma(host);
Guennadi Liakhovetskic9b0cef2011-05-26 15:33:30 +0200954 host->card_present = false;
Guennadi Liakhovetskifaca6642011-05-05 16:20:48 +0000955 }
Guennadi Liakhovetskic9b0cef2011-05-26 15:33:30 +0200956 }
957 if (host->power) {
958 pm_runtime_put(&host->pd->dev);
959 host->power = false;
Guennadi Liakhovetskif6bc41f2011-11-16 10:10:41 +0100960 if (p->down_pwr && ios->power_mode == MMC_POWER_OFF)
Guennadi Liakhovetskifaca6642011-05-05 16:20:48 +0000961 p->down_pwr(host->pd);
962 }
Guennadi Liakhovetski3b0beaf2011-04-15 18:30:47 +0000963 host->state = STATE_IDLE;
Guennadi Liakhovetskif5e0cec2011-02-25 16:58:38 +0100964 return;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700965 }
966
Guennadi Liakhovetskic9b0cef2011-05-26 15:33:30 +0200967 if (ios->clock) {
968 if (!host->power) {
969 if (p->set_pwr)
970 p->set_pwr(host->pd, ios->power_mode);
971 pm_runtime_get_sync(&host->pd->dev);
972 host->power = true;
973 sh_mmcif_sync_reset(host);
974 }
Yusuke Godafdc50a92010-05-26 14:41:59 -0700975 sh_mmcif_clock_control(host, ios->clock);
Guennadi Liakhovetskic9b0cef2011-05-26 15:33:30 +0200976 }
Yusuke Godafdc50a92010-05-26 14:41:59 -0700977
978 host->bus_width = ios->bus_width;
Guennadi Liakhovetski3b0beaf2011-04-15 18:30:47 +0000979 host->state = STATE_IDLE;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700980}
981
Arnd Hannemann777271d2010-08-24 17:27:01 +0200982static int sh_mmcif_get_cd(struct mmc_host *mmc)
983{
984 struct sh_mmcif_host *host = mmc_priv(mmc);
985 struct sh_mmcif_plat_data *p = host->pd->dev.platform_data;
986
987 if (!p->get_cd)
988 return -ENOSYS;
989 else
990 return p->get_cd(host->pd);
991}
992
Yusuke Godafdc50a92010-05-26 14:41:59 -0700993static struct mmc_host_ops sh_mmcif_ops = {
994 .request = sh_mmcif_request,
995 .set_ios = sh_mmcif_set_ios,
Arnd Hannemann777271d2010-08-24 17:27:01 +0200996 .get_cd = sh_mmcif_get_cd,
Yusuke Godafdc50a92010-05-26 14:41:59 -0700997};
998
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100999static bool sh_mmcif_end_cmd(struct sh_mmcif_host *host)
1000{
1001 struct mmc_command *cmd = host->mrq->cmd;
Guennadi Liakhovetski69983402011-12-26 12:52:13 -05001002 struct mmc_data *data = host->mrq->data;
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001003 long time;
1004
1005 if (host->sd_error) {
1006 switch (cmd->opcode) {
1007 case MMC_ALL_SEND_CID:
1008 case MMC_SELECT_CARD:
1009 case MMC_APP_CMD:
1010 cmd->error = -ETIMEDOUT;
1011 host->sd_error = false;
1012 break;
1013 default:
1014 cmd->error = sh_mmcif_error_manage(host);
1015 dev_dbg(&host->pd->dev, "Cmd(d'%d) error %d\n",
1016 cmd->opcode, cmd->error);
1017 break;
1018 }
1019 return false;
1020 }
1021 if (!(cmd->flags & MMC_RSP_PRESENT)) {
1022 cmd->error = 0;
1023 return false;
1024 }
1025
1026 sh_mmcif_get_response(host, cmd);
1027
Guennadi Liakhovetski69983402011-12-26 12:52:13 -05001028 if (!data)
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001029 return false;
1030
Guennadi Liakhovetski69983402011-12-26 12:52:13 -05001031 if (data->flags & MMC_DATA_READ) {
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001032 if (host->chan_rx)
1033 sh_mmcif_start_dma_rx(host);
1034 } else {
1035 if (host->chan_tx)
1036 sh_mmcif_start_dma_tx(host);
1037 }
1038
1039 if (!host->dma_active) {
Guennadi Liakhovetski69983402011-12-26 12:52:13 -05001040 data->error = sh_mmcif_data_trans(host, host->mrq, cmd->opcode);
1041 if (!data->error)
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001042 return true;
1043 return false;
1044 }
1045
1046 /* Running in the IRQ thread, can sleep */
1047 time = wait_for_completion_interruptible_timeout(&host->dma_complete,
1048 host->timeout);
1049 if (host->sd_error) {
1050 dev_err(host->mmc->parent,
1051 "Error IRQ while waiting for DMA completion!\n");
1052 /* Woken up by an error IRQ: abort DMA */
Guennadi Liakhovetski69983402011-12-26 12:52:13 -05001053 if (data->flags & MMC_DATA_READ)
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001054 dmaengine_terminate_all(host->chan_rx);
1055 else
1056 dmaengine_terminate_all(host->chan_tx);
Guennadi Liakhovetski69983402011-12-26 12:52:13 -05001057 data->error = sh_mmcif_error_manage(host);
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001058 } else if (!time) {
Guennadi Liakhovetski69983402011-12-26 12:52:13 -05001059 data->error = -ETIMEDOUT;
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001060 } else if (time < 0) {
Guennadi Liakhovetski69983402011-12-26 12:52:13 -05001061 data->error = time;
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001062 }
1063 sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC,
1064 BUF_ACC_DMAREN | BUF_ACC_DMAWEN);
1065 host->dma_active = false;
1066
Guennadi Liakhovetski69983402011-12-26 12:52:13 -05001067 if (data->error)
1068 data->bytes_xfered = 0;
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001069
1070 return false;
1071}
1072
1073static irqreturn_t sh_mmcif_irqt(int irq, void *dev_id)
1074{
1075 struct sh_mmcif_host *host = dev_id;
1076 struct mmc_request *mrq = host->mrq;
Guennadi Liakhovetski69983402011-12-26 12:52:13 -05001077 struct mmc_data *data = mrq->data;
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001078
1079 cancel_delayed_work_sync(&host->timeout_work);
1080
1081 /*
1082 * All handlers return true, if processing continues, and false, if the
1083 * request has to be completed - successfully or not
1084 */
1085 switch (host->wait_for) {
1086 case MMCIF_WAIT_FOR_REQUEST:
1087 /* We're too late, the timeout has already kicked in */
1088 return IRQ_HANDLED;
1089 case MMCIF_WAIT_FOR_CMD:
1090 if (sh_mmcif_end_cmd(host))
1091 /* Wait for data */
1092 return IRQ_HANDLED;
1093 break;
1094 case MMCIF_WAIT_FOR_MREAD:
1095 if (sh_mmcif_mread_block(host))
1096 /* Wait for more data */
1097 return IRQ_HANDLED;
1098 break;
1099 case MMCIF_WAIT_FOR_READ:
1100 if (sh_mmcif_read_block(host))
1101 /* Wait for data end */
1102 return IRQ_HANDLED;
1103 break;
1104 case MMCIF_WAIT_FOR_MWRITE:
1105 if (sh_mmcif_mwrite_block(host))
1106 /* Wait data to write */
1107 return IRQ_HANDLED;
1108 break;
1109 case MMCIF_WAIT_FOR_WRITE:
1110 if (sh_mmcif_write_block(host))
1111 /* Wait for data end */
1112 return IRQ_HANDLED;
1113 break;
1114 case MMCIF_WAIT_FOR_STOP:
1115 if (host->sd_error) {
1116 mrq->stop->error = sh_mmcif_error_manage(host);
1117 break;
1118 }
1119 sh_mmcif_get_cmd12response(host, mrq->stop);
1120 mrq->stop->error = 0;
1121 break;
1122 case MMCIF_WAIT_FOR_READ_END:
1123 case MMCIF_WAIT_FOR_WRITE_END:
1124 if (host->sd_error)
Guennadi Liakhovetski69983402011-12-26 12:52:13 -05001125 data->error = sh_mmcif_error_manage(host);
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001126 break;
1127 default:
1128 BUG();
1129 }
1130
1131 if (host->wait_for != MMCIF_WAIT_FOR_STOP) {
Guennadi Liakhovetski69983402011-12-26 12:52:13 -05001132 if (!mrq->cmd->error && data && !data->error)
1133 data->bytes_xfered =
1134 data->blocks * data->blksz;
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001135
Guennadi Liakhovetski69983402011-12-26 12:52:13 -05001136 if (mrq->stop && !mrq->cmd->error && (!data || !data->error)) {
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001137 sh_mmcif_stop_cmd(host, mrq);
1138 if (!mrq->stop->error)
1139 return IRQ_HANDLED;
1140 }
1141 }
1142
1143 host->wait_for = MMCIF_WAIT_FOR_REQUEST;
1144 host->state = STATE_IDLE;
Guennadi Liakhovetski69983402011-12-26 12:52:13 -05001145 host->mrq = NULL;
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001146 mmc_request_done(host->mmc, mrq);
1147
1148 return IRQ_HANDLED;
1149}
1150
Yusuke Godafdc50a92010-05-26 14:41:59 -07001151static irqreturn_t sh_mmcif_intr(int irq, void *dev_id)
1152{
1153 struct sh_mmcif_host *host = dev_id;
Guennadi Liakhovetskiaa0787a2010-11-24 10:05:12 +00001154 u32 state;
Yusuke Godafdc50a92010-05-26 14:41:59 -07001155 int err = 0;
1156
Magnus Damm487d9fc2010-05-18 14:42:51 +00001157 state = sh_mmcif_readl(host->addr, MMCIF_CE_INT);
Yusuke Godafdc50a92010-05-26 14:41:59 -07001158
Guennadi Liakhovetski8a8284a2011-12-14 19:31:51 +01001159 if (state & INT_ERR_STS) {
1160 /* error interrupts - process first */
1161 sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~state);
1162 sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, state);
1163 err = 1;
1164 } else if (state & INT_RBSYE) {
Magnus Damm487d9fc2010-05-18 14:42:51 +00001165 sh_mmcif_writel(host->addr, MMCIF_CE_INT,
1166 ~(INT_RBSYE | INT_CRSPE));
Yusuke Godafdc50a92010-05-26 14:41:59 -07001167 sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MRBSYE);
1168 } else if (state & INT_CRSPE) {
Magnus Damm487d9fc2010-05-18 14:42:51 +00001169 sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~INT_CRSPE);
Yusuke Godafdc50a92010-05-26 14:41:59 -07001170 sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MCRSPE);
1171 } else if (state & INT_BUFREN) {
Magnus Damm487d9fc2010-05-18 14:42:51 +00001172 sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~INT_BUFREN);
Yusuke Godafdc50a92010-05-26 14:41:59 -07001173 sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MBUFREN);
1174 } else if (state & INT_BUFWEN) {
Magnus Damm487d9fc2010-05-18 14:42:51 +00001175 sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~INT_BUFWEN);
Yusuke Godafdc50a92010-05-26 14:41:59 -07001176 sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN);
1177 } else if (state & INT_CMD12DRE) {
Magnus Damm487d9fc2010-05-18 14:42:51 +00001178 sh_mmcif_writel(host->addr, MMCIF_CE_INT,
Yusuke Godafdc50a92010-05-26 14:41:59 -07001179 ~(INT_CMD12DRE | INT_CMD12RBE |
1180 INT_CMD12CRE | INT_BUFRE));
1181 sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MCMD12DRE);
1182 } else if (state & INT_BUFRE) {
Magnus Damm487d9fc2010-05-18 14:42:51 +00001183 sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~INT_BUFRE);
Yusuke Godafdc50a92010-05-26 14:41:59 -07001184 sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MBUFRE);
1185 } else if (state & INT_DTRANE) {
Magnus Damm487d9fc2010-05-18 14:42:51 +00001186 sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~INT_DTRANE);
Yusuke Godafdc50a92010-05-26 14:41:59 -07001187 sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MDTRANE);
1188 } else if (state & INT_CMD12RBE) {
Magnus Damm487d9fc2010-05-18 14:42:51 +00001189 sh_mmcif_writel(host->addr, MMCIF_CE_INT,
Yusuke Godafdc50a92010-05-26 14:41:59 -07001190 ~(INT_CMD12RBE | INT_CMD12CRE));
1191 sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MCMD12RBE);
Yusuke Godafdc50a92010-05-26 14:41:59 -07001192 } else {
Guennadi Liakhovetskifaca6642011-05-05 16:20:48 +00001193 dev_dbg(&host->pd->dev, "Unsupported interrupt: 0x%x\n", state);
Magnus Damm487d9fc2010-05-18 14:42:51 +00001194 sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~state);
Yusuke Godafdc50a92010-05-26 14:41:59 -07001195 sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, state);
1196 err = 1;
1197 }
1198 if (err) {
Guennadi Liakhovetskiaa0787a2010-11-24 10:05:12 +00001199 host->sd_error = true;
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +00001200 dev_dbg(&host->pd->dev, "int err state = %08x\n", state);
Yusuke Godafdc50a92010-05-26 14:41:59 -07001201 }
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001202 if (state & ~(INT_CMD12RBE | INT_CMD12CRE)) {
1203 if (!host->dma_active)
1204 return IRQ_WAKE_THREAD;
1205 else if (host->sd_error)
1206 mmcif_dma_complete(host);
1207 } else {
Guennadi Liakhovetskiaa0787a2010-11-24 10:05:12 +00001208 dev_dbg(&host->pd->dev, "Unexpected IRQ 0x%x\n", state);
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001209 }
Yusuke Godafdc50a92010-05-26 14:41:59 -07001210
1211 return IRQ_HANDLED;
1212}
1213
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001214static void mmcif_timeout_work(struct work_struct *work)
1215{
1216 struct delayed_work *d = container_of(work, struct delayed_work, work);
1217 struct sh_mmcif_host *host = container_of(d, struct sh_mmcif_host, timeout_work);
1218 struct mmc_request *mrq = host->mrq;
1219
1220 if (host->dying)
1221 /* Don't run after mmc_remove_host() */
1222 return;
1223
1224 /*
1225 * Handle races with cancel_delayed_work(), unless
1226 * cancel_delayed_work_sync() is used
1227 */
1228 switch (host->wait_for) {
1229 case MMCIF_WAIT_FOR_CMD:
1230 mrq->cmd->error = sh_mmcif_error_manage(host);
1231 break;
1232 case MMCIF_WAIT_FOR_STOP:
1233 mrq->stop->error = sh_mmcif_error_manage(host);
1234 break;
1235 case MMCIF_WAIT_FOR_MREAD:
1236 case MMCIF_WAIT_FOR_MWRITE:
1237 case MMCIF_WAIT_FOR_READ:
1238 case MMCIF_WAIT_FOR_WRITE:
1239 case MMCIF_WAIT_FOR_READ_END:
1240 case MMCIF_WAIT_FOR_WRITE_END:
Guennadi Liakhovetski69983402011-12-26 12:52:13 -05001241 mrq->data->error = sh_mmcif_error_manage(host);
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001242 break;
1243 default:
1244 BUG();
1245 }
1246
1247 host->state = STATE_IDLE;
1248 host->wait_for = MMCIF_WAIT_FOR_REQUEST;
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001249 host->mrq = NULL;
1250 mmc_request_done(host->mmc, mrq);
1251}
1252
Yusuke Godafdc50a92010-05-26 14:41:59 -07001253static int __devinit sh_mmcif_probe(struct platform_device *pdev)
1254{
1255 int ret = 0, irq[2];
1256 struct mmc_host *mmc;
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +00001257 struct sh_mmcif_host *host;
1258 struct sh_mmcif_plat_data *pd;
Yusuke Godafdc50a92010-05-26 14:41:59 -07001259 struct resource *res;
1260 void __iomem *reg;
1261 char clk_name[8];
1262
1263 irq[0] = platform_get_irq(pdev, 0);
1264 irq[1] = platform_get_irq(pdev, 1);
1265 if (irq[0] < 0 || irq[1] < 0) {
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +00001266 dev_err(&pdev->dev, "Get irq error\n");
Yusuke Godafdc50a92010-05-26 14:41:59 -07001267 return -ENXIO;
1268 }
1269 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1270 if (!res) {
1271 dev_err(&pdev->dev, "platform_get_resource error.\n");
1272 return -ENXIO;
1273 }
1274 reg = ioremap(res->start, resource_size(res));
1275 if (!reg) {
1276 dev_err(&pdev->dev, "ioremap error.\n");
1277 return -ENOMEM;
1278 }
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +00001279 pd = pdev->dev.platform_data;
Yusuke Godafdc50a92010-05-26 14:41:59 -07001280 if (!pd) {
1281 dev_err(&pdev->dev, "sh_mmcif plat data error.\n");
1282 ret = -ENXIO;
1283 goto clean_up;
1284 }
1285 mmc = mmc_alloc_host(sizeof(struct sh_mmcif_host), &pdev->dev);
1286 if (!mmc) {
1287 ret = -ENOMEM;
1288 goto clean_up;
1289 }
1290 host = mmc_priv(mmc);
1291 host->mmc = mmc;
1292 host->addr = reg;
1293 host->timeout = 1000;
1294
1295 snprintf(clk_name, sizeof(clk_name), "mmc%d", pdev->id);
1296 host->hclk = clk_get(&pdev->dev, clk_name);
1297 if (IS_ERR(host->hclk)) {
1298 dev_err(&pdev->dev, "cannot get clock \"%s\"\n", clk_name);
1299 ret = PTR_ERR(host->hclk);
1300 goto clean_up1;
1301 }
1302 clk_enable(host->hclk);
1303 host->clk = clk_get_rate(host->hclk);
1304 host->pd = pdev;
1305
Guennadi Liakhovetski3b0beaf2011-04-15 18:30:47 +00001306 spin_lock_init(&host->lock);
Yusuke Godafdc50a92010-05-26 14:41:59 -07001307
1308 mmc->ops = &sh_mmcif_ops;
Simon Horman930f1522012-03-28 18:01:10 +09001309 mmc->f_max = host->clk / 2;
Simon Hormaneb91b912012-03-28 18:01:11 +09001310 mmc->f_min = host->clk / 512;
Yusuke Godafdc50a92010-05-26 14:41:59 -07001311 if (pd->ocr)
1312 mmc->ocr_avail = pd->ocr;
1313 mmc->caps = MMC_CAP_MMC_HIGHSPEED;
1314 if (pd->caps)
1315 mmc->caps |= pd->caps;
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +00001316 mmc->max_segs = 32;
Yusuke Godafdc50a92010-05-26 14:41:59 -07001317 mmc->max_blk_size = 512;
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +00001318 mmc->max_req_size = PAGE_CACHE_SIZE * mmc->max_segs;
1319 mmc->max_blk_count = mmc->max_req_size / mmc->max_blk_size;
Yusuke Godafdc50a92010-05-26 14:41:59 -07001320 mmc->max_seg_size = mmc->max_req_size;
1321
1322 sh_mmcif_sync_reset(host);
1323 platform_set_drvdata(pdev, host);
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +00001324
Guennadi Liakhovetskifaca6642011-05-05 16:20:48 +00001325 pm_runtime_enable(&pdev->dev);
1326 host->power = false;
1327
1328 ret = pm_runtime_resume(&pdev->dev);
1329 if (ret < 0)
1330 goto clean_up2;
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +00001331
Guennadi Liakhovetski5ba85d92012-01-21 00:41:28 +01001332 INIT_DELAYED_WORK(&host->timeout_work, mmcif_timeout_work);
Yusuke Godafdc50a92010-05-26 14:41:59 -07001333
Guennadi Liakhovetski3b0beaf2011-04-15 18:30:47 +00001334 sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL);
1335
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001336 ret = request_threaded_irq(irq[0], sh_mmcif_intr, sh_mmcif_irqt, 0, "sh_mmc:error", host);
Yusuke Godafdc50a92010-05-26 14:41:59 -07001337 if (ret) {
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +00001338 dev_err(&pdev->dev, "request_irq error (sh_mmc:error)\n");
Guennadi Liakhovetskifaca6642011-05-05 16:20:48 +00001339 goto clean_up3;
Yusuke Godafdc50a92010-05-26 14:41:59 -07001340 }
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001341 ret = request_threaded_irq(irq[1], sh_mmcif_intr, sh_mmcif_irqt, 0, "sh_mmc:int", host);
Yusuke Godafdc50a92010-05-26 14:41:59 -07001342 if (ret) {
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +00001343 dev_err(&pdev->dev, "request_irq error (sh_mmc:int)\n");
Guennadi Liakhovetski5ba85d92012-01-21 00:41:28 +01001344 goto clean_up4;
Yusuke Godafdc50a92010-05-26 14:41:59 -07001345 }
1346
Guennadi Liakhovetski5ba85d92012-01-21 00:41:28 +01001347 ret = mmc_add_host(mmc);
1348 if (ret < 0)
1349 goto clean_up5;
Yusuke Godafdc50a92010-05-26 14:41:59 -07001350
Rafael J. Wysockiefe6a8a2012-03-13 01:02:15 +01001351 dev_pm_qos_expose_latency_limit(&pdev->dev, 100);
1352
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +00001353 dev_info(&pdev->dev, "driver version %s\n", DRIVER_VERSION);
1354 dev_dbg(&pdev->dev, "chip ver H'%04x\n",
Magnus Damm487d9fc2010-05-18 14:42:51 +00001355 sh_mmcif_readl(host->addr, MMCIF_CE_VERSION) & 0x0000ffff);
Yusuke Godafdc50a92010-05-26 14:41:59 -07001356 return ret;
1357
Guennadi Liakhovetski5ba85d92012-01-21 00:41:28 +01001358clean_up5:
1359 free_irq(irq[1], host);
1360clean_up4:
1361 free_irq(irq[0], host);
Guennadi Liakhovetskifaca6642011-05-05 16:20:48 +00001362clean_up3:
Guennadi Liakhovetskifaca6642011-05-05 16:20:48 +00001363 pm_runtime_suspend(&pdev->dev);
Yusuke Godafdc50a92010-05-26 14:41:59 -07001364clean_up2:
Guennadi Liakhovetskifaca6642011-05-05 16:20:48 +00001365 pm_runtime_disable(&pdev->dev);
Yusuke Godafdc50a92010-05-26 14:41:59 -07001366 clk_disable(host->hclk);
1367clean_up1:
1368 mmc_free_host(mmc);
1369clean_up:
1370 if (reg)
1371 iounmap(reg);
1372 return ret;
1373}
1374
1375static int __devexit sh_mmcif_remove(struct platform_device *pdev)
1376{
1377 struct sh_mmcif_host *host = platform_get_drvdata(pdev);
1378 int irq[2];
1379
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001380 host->dying = true;
Guennadi Liakhovetskifaca6642011-05-05 16:20:48 +00001381 pm_runtime_get_sync(&pdev->dev);
Guennadi Liakhovetskiaa0787a2010-11-24 10:05:12 +00001382
Rafael J. Wysockiefe6a8a2012-03-13 01:02:15 +01001383 dev_pm_qos_hide_latency_limit(&pdev->dev);
1384
Guennadi Liakhovetskifaca6642011-05-05 16:20:48 +00001385 mmc_remove_host(host->mmc);
Guennadi Liakhovetski3b0beaf2011-04-15 18:30:47 +00001386 sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL);
1387
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001388 /*
1389 * FIXME: cancel_delayed_work(_sync)() and free_irq() race with the
1390 * mmc_remove_host() call above. But swapping order doesn't help either
1391 * (a query on the linux-mmc mailing list didn't bring any replies).
1392 */
1393 cancel_delayed_work_sync(&host->timeout_work);
1394
Guennadi Liakhovetskiaa0787a2010-11-24 10:05:12 +00001395 if (host->addr)
1396 iounmap(host->addr);
1397
Yusuke Godafdc50a92010-05-26 14:41:59 -07001398 irq[0] = platform_get_irq(pdev, 0);
1399 irq[1] = platform_get_irq(pdev, 1);
1400
Yusuke Godafdc50a92010-05-26 14:41:59 -07001401 free_irq(irq[0], host);
1402 free_irq(irq[1], host);
1403
Guennadi Liakhovetskiaa0787a2010-11-24 10:05:12 +00001404 platform_set_drvdata(pdev, NULL);
1405
Yusuke Godafdc50a92010-05-26 14:41:59 -07001406 clk_disable(host->hclk);
1407 mmc_free_host(host->mmc);
Guennadi Liakhovetskifaca6642011-05-05 16:20:48 +00001408 pm_runtime_put_sync(&pdev->dev);
1409 pm_runtime_disable(&pdev->dev);
Yusuke Godafdc50a92010-05-26 14:41:59 -07001410
1411 return 0;
1412}
1413
Guennadi Liakhovetskifaca6642011-05-05 16:20:48 +00001414#ifdef CONFIG_PM
1415static int sh_mmcif_suspend(struct device *dev)
1416{
1417 struct platform_device *pdev = to_platform_device(dev);
1418 struct sh_mmcif_host *host = platform_get_drvdata(pdev);
1419 int ret = mmc_suspend_host(host->mmc);
1420
1421 if (!ret) {
1422 sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL);
1423 clk_disable(host->hclk);
1424 }
1425
1426 return ret;
1427}
1428
1429static int sh_mmcif_resume(struct device *dev)
1430{
1431 struct platform_device *pdev = to_platform_device(dev);
1432 struct sh_mmcif_host *host = platform_get_drvdata(pdev);
1433
1434 clk_enable(host->hclk);
1435
1436 return mmc_resume_host(host->mmc);
1437}
1438#else
1439#define sh_mmcif_suspend NULL
1440#define sh_mmcif_resume NULL
1441#endif /* CONFIG_PM */
1442
1443static const struct dev_pm_ops sh_mmcif_dev_pm_ops = {
1444 .suspend = sh_mmcif_suspend,
1445 .resume = sh_mmcif_resume,
1446};
1447
Yusuke Godafdc50a92010-05-26 14:41:59 -07001448static struct platform_driver sh_mmcif_driver = {
1449 .probe = sh_mmcif_probe,
1450 .remove = sh_mmcif_remove,
1451 .driver = {
1452 .name = DRIVER_NAME,
Guennadi Liakhovetskifaca6642011-05-05 16:20:48 +00001453 .pm = &sh_mmcif_dev_pm_ops,
Yusuke Godafdc50a92010-05-26 14:41:59 -07001454 },
1455};
1456
Axel Lind1f81a62011-11-26 12:55:43 +08001457module_platform_driver(sh_mmcif_driver);
Yusuke Godafdc50a92010-05-26 14:41:59 -07001458
1459MODULE_DESCRIPTION("SuperH on-chip MMC/eMMC interface driver");
1460MODULE_LICENSE("GPL");
Guennadi Liakhovetskiaa0787a2010-11-24 10:05:12 +00001461MODULE_ALIAS("platform:" DRIVER_NAME);
Yusuke Godafdc50a92010-05-26 14:41:59 -07001462MODULE_AUTHOR("Yusuke Goda <yusuke.goda.sx@renesas.com>");