blob: d09a2b38eeeb4c8208e03314fd5e569e42592396 [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 Liakhovetskiaa0787a2010-11-24 10:05:12 +000019#include <linux/clk.h>
20#include <linux/completion.h>
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +000021#include <linux/delay.h>
Yusuke Godafdc50a92010-05-26 14:41:59 -070022#include <linux/dma-mapping.h>
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +000023#include <linux/dmaengine.h>
Yusuke Godafdc50a92010-05-26 14:41:59 -070024#include <linux/mmc/card.h>
25#include <linux/mmc/core.h>
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +000026#include <linux/mmc/host.h>
Yusuke Godafdc50a92010-05-26 14:41:59 -070027#include <linux/mmc/mmc.h>
28#include <linux/mmc/sdio.h>
Yusuke Godafdc50a92010-05-26 14:41:59 -070029#include <linux/mmc/sh_mmcif.h>
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +000030#include <linux/pagemap.h>
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +000031#include <linux/platform_device.h>
Yusuke Godafdc50a92010-05-26 14:41:59 -070032
33#define DRIVER_NAME "sh_mmcif"
34#define DRIVER_VERSION "2010-04-28"
35
Yusuke Godafdc50a92010-05-26 14:41:59 -070036/* CE_CMD_SET */
37#define CMD_MASK 0x3f000000
38#define CMD_SET_RTYP_NO ((0 << 23) | (0 << 22))
39#define CMD_SET_RTYP_6B ((0 << 23) | (1 << 22)) /* R1/R1b/R3/R4/R5 */
40#define CMD_SET_RTYP_17B ((1 << 23) | (0 << 22)) /* R2 */
41#define CMD_SET_RBSY (1 << 21) /* R1b */
42#define CMD_SET_CCSEN (1 << 20)
43#define CMD_SET_WDAT (1 << 19) /* 1: on data, 0: no data */
44#define CMD_SET_DWEN (1 << 18) /* 1: write, 0: read */
45#define CMD_SET_CMLTE (1 << 17) /* 1: multi block trans, 0: single */
46#define CMD_SET_CMD12EN (1 << 16) /* 1: CMD12 auto issue */
47#define CMD_SET_RIDXC_INDEX ((0 << 15) | (0 << 14)) /* index check */
48#define CMD_SET_RIDXC_BITS ((0 << 15) | (1 << 14)) /* check bits check */
49#define CMD_SET_RIDXC_NO ((1 << 15) | (0 << 14)) /* no check */
50#define CMD_SET_CRC7C ((0 << 13) | (0 << 12)) /* CRC7 check*/
51#define CMD_SET_CRC7C_BITS ((0 << 13) | (1 << 12)) /* check bits check*/
52#define CMD_SET_CRC7C_INTERNAL ((1 << 13) | (0 << 12)) /* internal CRC7 check*/
53#define CMD_SET_CRC16C (1 << 10) /* 0: CRC16 check*/
54#define CMD_SET_CRCSTE (1 << 8) /* 1: not receive CRC status */
55#define CMD_SET_TBIT (1 << 7) /* 1: tran mission bit "Low" */
56#define CMD_SET_OPDM (1 << 6) /* 1: open/drain */
57#define CMD_SET_CCSH (1 << 5)
58#define CMD_SET_DATW_1 ((0 << 1) | (0 << 0)) /* 1bit */
59#define CMD_SET_DATW_4 ((0 << 1) | (1 << 0)) /* 4bit */
60#define CMD_SET_DATW_8 ((1 << 1) | (0 << 0)) /* 8bit */
61
62/* CE_CMD_CTRL */
63#define CMD_CTRL_BREAK (1 << 0)
64
65/* CE_BLOCK_SET */
66#define BLOCK_SIZE_MASK 0x0000ffff
67
Yusuke Godafdc50a92010-05-26 14:41:59 -070068/* CE_INT */
69#define INT_CCSDE (1 << 29)
70#define INT_CMD12DRE (1 << 26)
71#define INT_CMD12RBE (1 << 25)
72#define INT_CMD12CRE (1 << 24)
73#define INT_DTRANE (1 << 23)
74#define INT_BUFRE (1 << 22)
75#define INT_BUFWEN (1 << 21)
76#define INT_BUFREN (1 << 20)
77#define INT_CCSRCV (1 << 19)
78#define INT_RBSYE (1 << 17)
79#define INT_CRSPE (1 << 16)
80#define INT_CMDVIO (1 << 15)
81#define INT_BUFVIO (1 << 14)
82#define INT_WDATERR (1 << 11)
83#define INT_RDATERR (1 << 10)
84#define INT_RIDXERR (1 << 9)
85#define INT_RSPERR (1 << 8)
86#define INT_CCSTO (1 << 5)
87#define INT_CRCSTO (1 << 4)
88#define INT_WDATTO (1 << 3)
89#define INT_RDATTO (1 << 2)
90#define INT_RBSYTO (1 << 1)
91#define INT_RSPTO (1 << 0)
92#define INT_ERR_STS (INT_CMDVIO | INT_BUFVIO | INT_WDATERR | \
93 INT_RDATERR | INT_RIDXERR | INT_RSPERR | \
94 INT_CCSTO | INT_CRCSTO | INT_WDATTO | \
95 INT_RDATTO | INT_RBSYTO | INT_RSPTO)
96
97/* CE_INT_MASK */
98#define MASK_ALL 0x00000000
99#define MASK_MCCSDE (1 << 29)
100#define MASK_MCMD12DRE (1 << 26)
101#define MASK_MCMD12RBE (1 << 25)
102#define MASK_MCMD12CRE (1 << 24)
103#define MASK_MDTRANE (1 << 23)
104#define MASK_MBUFRE (1 << 22)
105#define MASK_MBUFWEN (1 << 21)
106#define MASK_MBUFREN (1 << 20)
107#define MASK_MCCSRCV (1 << 19)
108#define MASK_MRBSYE (1 << 17)
109#define MASK_MCRSPE (1 << 16)
110#define MASK_MCMDVIO (1 << 15)
111#define MASK_MBUFVIO (1 << 14)
112#define MASK_MWDATERR (1 << 11)
113#define MASK_MRDATERR (1 << 10)
114#define MASK_MRIDXERR (1 << 9)
115#define MASK_MRSPERR (1 << 8)
116#define MASK_MCCSTO (1 << 5)
117#define MASK_MCRCSTO (1 << 4)
118#define MASK_MWDATTO (1 << 3)
119#define MASK_MRDATTO (1 << 2)
120#define MASK_MRBSYTO (1 << 1)
121#define MASK_MRSPTO (1 << 0)
122
123/* CE_HOST_STS1 */
124#define STS1_CMDSEQ (1 << 31)
125
126/* CE_HOST_STS2 */
127#define STS2_CRCSTE (1 << 31)
128#define STS2_CRC16E (1 << 30)
129#define STS2_AC12CRCE (1 << 29)
130#define STS2_RSPCRC7E (1 << 28)
131#define STS2_CRCSTEBE (1 << 27)
132#define STS2_RDATEBE (1 << 26)
133#define STS2_AC12REBE (1 << 25)
134#define STS2_RSPEBE (1 << 24)
135#define STS2_AC12IDXE (1 << 23)
136#define STS2_RSPIDXE (1 << 22)
137#define STS2_CCSTO (1 << 15)
138#define STS2_RDATTO (1 << 14)
139#define STS2_DATBSYTO (1 << 13)
140#define STS2_CRCSTTO (1 << 12)
141#define STS2_AC12BSYTO (1 << 11)
142#define STS2_RSPBSYTO (1 << 10)
143#define STS2_AC12RSPTO (1 << 9)
144#define STS2_RSPTO (1 << 8)
145#define STS2_CRC_ERR (STS2_CRCSTE | STS2_CRC16E | \
146 STS2_AC12CRCE | STS2_RSPCRC7E | STS2_CRCSTEBE)
147#define STS2_TIMEOUT_ERR (STS2_CCSTO | STS2_RDATTO | \
148 STS2_DATBSYTO | STS2_CRCSTTO | \
149 STS2_AC12BSYTO | STS2_RSPBSYTO | \
150 STS2_AC12RSPTO | STS2_RSPTO)
151
Yusuke Godafdc50a92010-05-26 14:41:59 -0700152#define CLKDEV_EMMC_DATA 52000000 /* 52MHz */
153#define CLKDEV_MMC_DATA 20000000 /* 20MHz */
154#define CLKDEV_INIT 400000 /* 400 KHz */
155
156struct sh_mmcif_host {
157 struct mmc_host *mmc;
158 struct mmc_data *data;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700159 struct platform_device *pd;
160 struct clk *hclk;
161 unsigned int clk;
162 int bus_width;
Guennadi Liakhovetskiaa0787a2010-11-24 10:05:12 +0000163 bool sd_error;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700164 long timeout;
165 void __iomem *addr;
Guennadi Liakhovetskiaa0787a2010-11-24 10:05:12 +0000166 struct completion intr_wait;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700167
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000168 /* DMA support */
169 struct dma_chan *chan_rx;
170 struct dma_chan *chan_tx;
171 struct completion dma_complete;
172 unsigned int dma_sglen;
173};
Yusuke Godafdc50a92010-05-26 14:41:59 -0700174
175static inline void sh_mmcif_bitset(struct sh_mmcif_host *host,
176 unsigned int reg, u32 val)
177{
Magnus Damm487d9fc2010-05-18 14:42:51 +0000178 writel(val | readl(host->addr + reg), host->addr + reg);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700179}
180
181static inline void sh_mmcif_bitclr(struct sh_mmcif_host *host,
182 unsigned int reg, u32 val)
183{
Magnus Damm487d9fc2010-05-18 14:42:51 +0000184 writel(~val & readl(host->addr + reg), host->addr + reg);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700185}
186
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000187#ifdef CONFIG_SH_MMCIF_DMA
188static void mmcif_dma_complete(void *arg)
189{
190 struct sh_mmcif_host *host = arg;
191 dev_dbg(&host->pd->dev, "Command completed\n");
192
193 if (WARN(!host->data, "%s: NULL data in DMA completion!\n",
194 dev_name(&host->pd->dev)))
195 return;
196
197 if (host->data->flags & MMC_DATA_READ)
198 dma_unmap_sg(&host->pd->dev, host->data->sg, host->dma_sglen,
199 DMA_FROM_DEVICE);
200 else
201 dma_unmap_sg(&host->pd->dev, host->data->sg, host->dma_sglen,
202 DMA_TO_DEVICE);
203
204 complete(&host->dma_complete);
205}
206
207static void sh_mmcif_start_dma_rx(struct sh_mmcif_host *host)
208{
209 struct scatterlist *sg = host->data->sg;
210 struct dma_async_tx_descriptor *desc = NULL;
211 struct dma_chan *chan = host->chan_rx;
212 dma_cookie_t cookie = -EINVAL;
213 int ret;
214
215 ret = dma_map_sg(&host->pd->dev, sg, host->data->sg_len, DMA_FROM_DEVICE);
216 if (ret > 0) {
217 host->dma_sglen = ret;
218 desc = chan->device->device_prep_slave_sg(chan, sg, ret,
219 DMA_FROM_DEVICE, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
220 }
221
222 if (desc) {
223 desc->callback = mmcif_dma_complete;
224 desc->callback_param = host;
225 cookie = desc->tx_submit(desc);
226 if (cookie < 0) {
227 desc = NULL;
228 ret = cookie;
229 } else {
230 sh_mmcif_bitset(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN);
231 chan->device->device_issue_pending(chan);
232 }
233 }
234 dev_dbg(&host->pd->dev, "%s(): mapped %d -> %d, cookie %d\n",
235 __func__, host->data->sg_len, ret, cookie);
236
237 if (!desc) {
238 /* DMA failed, fall back to PIO */
239 if (ret >= 0)
240 ret = -EIO;
241 host->chan_rx = NULL;
242 host->dma_sglen = 0;
243 dma_release_channel(chan);
244 /* Free the Tx channel too */
245 chan = host->chan_tx;
246 if (chan) {
247 host->chan_tx = NULL;
248 dma_release_channel(chan);
249 }
250 dev_warn(&host->pd->dev,
251 "DMA failed: %d, falling back to PIO\n", ret);
252 sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN | BUF_ACC_DMAWEN);
253 }
254
255 dev_dbg(&host->pd->dev, "%s(): desc %p, cookie %d, sg[%d]\n", __func__,
256 desc, cookie, host->data->sg_len);
257}
258
259static void sh_mmcif_start_dma_tx(struct sh_mmcif_host *host)
260{
261 struct scatterlist *sg = host->data->sg;
262 struct dma_async_tx_descriptor *desc = NULL;
263 struct dma_chan *chan = host->chan_tx;
264 dma_cookie_t cookie = -EINVAL;
265 int ret;
266
267 ret = dma_map_sg(&host->pd->dev, sg, host->data->sg_len, DMA_TO_DEVICE);
268 if (ret > 0) {
269 host->dma_sglen = ret;
270 desc = chan->device->device_prep_slave_sg(chan, sg, ret,
271 DMA_TO_DEVICE, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
272 }
273
274 if (desc) {
275 desc->callback = mmcif_dma_complete;
276 desc->callback_param = host;
277 cookie = desc->tx_submit(desc);
278 if (cookie < 0) {
279 desc = NULL;
280 ret = cookie;
281 } else {
282 sh_mmcif_bitset(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAWEN);
283 chan->device->device_issue_pending(chan);
284 }
285 }
286 dev_dbg(&host->pd->dev, "%s(): mapped %d -> %d, cookie %d\n",
287 __func__, host->data->sg_len, ret, cookie);
288
289 if (!desc) {
290 /* DMA failed, fall back to PIO */
291 if (ret >= 0)
292 ret = -EIO;
293 host->chan_tx = NULL;
294 host->dma_sglen = 0;
295 dma_release_channel(chan);
296 /* Free the Rx channel too */
297 chan = host->chan_rx;
298 if (chan) {
299 host->chan_rx = NULL;
300 dma_release_channel(chan);
301 }
302 dev_warn(&host->pd->dev,
303 "DMA failed: %d, falling back to PIO\n", ret);
304 sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN | BUF_ACC_DMAWEN);
305 }
306
307 dev_dbg(&host->pd->dev, "%s(): desc %p, cookie %d\n", __func__,
308 desc, cookie);
309}
310
311static bool sh_mmcif_filter(struct dma_chan *chan, void *arg)
312{
313 dev_dbg(chan->device->dev, "%s: slave data %p\n", __func__, arg);
314 chan->private = arg;
315 return true;
316}
317
318static void sh_mmcif_request_dma(struct sh_mmcif_host *host,
319 struct sh_mmcif_plat_data *pdata)
320{
321 host->dma_sglen = 0;
322
323 /* We can only either use DMA for both Tx and Rx or not use it at all */
324 if (pdata->dma) {
325 dma_cap_mask_t mask;
326
327 dma_cap_zero(mask);
328 dma_cap_set(DMA_SLAVE, mask);
329
330 host->chan_tx = dma_request_channel(mask, sh_mmcif_filter,
331 &pdata->dma->chan_priv_tx);
332 dev_dbg(&host->pd->dev, "%s: TX: got channel %p\n", __func__,
333 host->chan_tx);
334
335 if (!host->chan_tx)
336 return;
337
338 host->chan_rx = dma_request_channel(mask, sh_mmcif_filter,
339 &pdata->dma->chan_priv_rx);
340 dev_dbg(&host->pd->dev, "%s: RX: got channel %p\n", __func__,
341 host->chan_rx);
342
343 if (!host->chan_rx) {
344 dma_release_channel(host->chan_tx);
345 host->chan_tx = NULL;
346 return;
347 }
348
349 init_completion(&host->dma_complete);
350 }
351}
352
353static void sh_mmcif_release_dma(struct sh_mmcif_host *host)
354{
355 sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN | BUF_ACC_DMAWEN);
356 /* Descriptors are freed automatically */
357 if (host->chan_tx) {
358 struct dma_chan *chan = host->chan_tx;
359 host->chan_tx = NULL;
360 dma_release_channel(chan);
361 }
362 if (host->chan_rx) {
363 struct dma_chan *chan = host->chan_rx;
364 host->chan_rx = NULL;
365 dma_release_channel(chan);
366 }
367
368 host->dma_sglen = 0;
369}
370#else
371static void sh_mmcif_start_dma_tx(struct sh_mmcif_host *host)
372{
373}
374
375static void sh_mmcif_start_dma_rx(struct sh_mmcif_host *host)
376{
377}
378
379static void sh_mmcif_request_dma(struct sh_mmcif_host *host,
380 struct sh_mmcif_plat_data *pdata)
381{
382 /* host->chan_tx, host->chan_tx and host->dma_sglen are all zero */
383}
384
385static void sh_mmcif_release_dma(struct sh_mmcif_host *host)
386{
387}
388#endif
Yusuke Godafdc50a92010-05-26 14:41:59 -0700389
390static void sh_mmcif_clock_control(struct sh_mmcif_host *host, unsigned int clk)
391{
392 struct sh_mmcif_plat_data *p = host->pd->dev.platform_data;
393
394 sh_mmcif_bitclr(host, MMCIF_CE_CLK_CTRL, CLK_ENABLE);
395 sh_mmcif_bitclr(host, MMCIF_CE_CLK_CTRL, CLK_CLEAR);
396
397 if (!clk)
398 return;
399 if (p->sup_pclk && clk == host->clk)
400 sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_SUP_PCLK);
401 else
402 sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_CLEAR &
403 (ilog2(__rounddown_pow_of_two(host->clk / clk)) << 16));
404
405 sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_ENABLE);
406}
407
408static void sh_mmcif_sync_reset(struct sh_mmcif_host *host)
409{
410 u32 tmp;
411
Magnus Damm487d9fc2010-05-18 14:42:51 +0000412 tmp = 0x010f0000 & sh_mmcif_readl(host->addr, MMCIF_CE_CLK_CTRL);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700413
Magnus Damm487d9fc2010-05-18 14:42:51 +0000414 sh_mmcif_writel(host->addr, MMCIF_CE_VERSION, SOFT_RST_ON);
415 sh_mmcif_writel(host->addr, MMCIF_CE_VERSION, SOFT_RST_OFF);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700416 sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, tmp |
417 SRSPTO_256 | SRBSYTO_29 | SRWDTO_29 | SCCSTO_29);
418 /* byte swap on */
419 sh_mmcif_bitset(host, MMCIF_CE_BUF_ACC, BUF_ACC_ATYP);
420}
421
422static int sh_mmcif_error_manage(struct sh_mmcif_host *host)
423{
424 u32 state1, state2;
425 int ret, timeout = 10000000;
426
Guennadi Liakhovetskiaa0787a2010-11-24 10:05:12 +0000427 host->sd_error = false;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700428
Magnus Damm487d9fc2010-05-18 14:42:51 +0000429 state1 = sh_mmcif_readl(host->addr, MMCIF_CE_HOST_STS1);
430 state2 = sh_mmcif_readl(host->addr, MMCIF_CE_HOST_STS2);
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +0000431 dev_dbg(&host->pd->dev, "ERR HOST_STS1 = %08x\n", state1);
432 dev_dbg(&host->pd->dev, "ERR HOST_STS2 = %08x\n", state2);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700433
434 if (state1 & STS1_CMDSEQ) {
435 sh_mmcif_bitset(host, MMCIF_CE_CMD_CTRL, CMD_CTRL_BREAK);
436 sh_mmcif_bitset(host, MMCIF_CE_CMD_CTRL, ~CMD_CTRL_BREAK);
437 while (1) {
438 timeout--;
439 if (timeout < 0) {
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +0000440 dev_err(&host->pd->dev,
441 "Forceed end of command sequence timeout err\n");
Yusuke Godafdc50a92010-05-26 14:41:59 -0700442 return -EIO;
443 }
Magnus Damm487d9fc2010-05-18 14:42:51 +0000444 if (!(sh_mmcif_readl(host->addr, MMCIF_CE_HOST_STS1)
Yusuke Godafdc50a92010-05-26 14:41:59 -0700445 & STS1_CMDSEQ))
446 break;
447 mdelay(1);
448 }
449 sh_mmcif_sync_reset(host);
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +0000450 dev_dbg(&host->pd->dev, "Forced end of command sequence\n");
Yusuke Godafdc50a92010-05-26 14:41:59 -0700451 return -EIO;
452 }
453
454 if (state2 & STS2_CRC_ERR) {
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +0000455 dev_dbg(&host->pd->dev, ": Happened CRC error\n");
Yusuke Godafdc50a92010-05-26 14:41:59 -0700456 ret = -EIO;
457 } else if (state2 & STS2_TIMEOUT_ERR) {
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +0000458 dev_dbg(&host->pd->dev, ": Happened Timeout error\n");
Yusuke Godafdc50a92010-05-26 14:41:59 -0700459 ret = -ETIMEDOUT;
460 } else {
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +0000461 dev_dbg(&host->pd->dev, ": Happened End/Index error\n");
Yusuke Godafdc50a92010-05-26 14:41:59 -0700462 ret = -EIO;
463 }
464 return ret;
465}
466
467static int sh_mmcif_single_read(struct sh_mmcif_host *host,
468 struct mmc_request *mrq)
469{
470 struct mmc_data *data = mrq->data;
471 long time;
472 u32 blocksize, i, *p = sg_virt(data->sg);
473
Yusuke Godafdc50a92010-05-26 14:41:59 -0700474 /* buf read enable */
475 sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFREN);
Guennadi Liakhovetskiaa0787a2010-11-24 10:05:12 +0000476 time = wait_for_completion_interruptible_timeout(&host->intr_wait,
477 host->timeout);
478 if (time <= 0 || host->sd_error)
Yusuke Godafdc50a92010-05-26 14:41:59 -0700479 return sh_mmcif_error_manage(host);
480
Yusuke Godafdc50a92010-05-26 14:41:59 -0700481 blocksize = (BLOCK_SIZE_MASK &
Magnus Damm487d9fc2010-05-18 14:42:51 +0000482 sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET)) + 3;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700483 for (i = 0; i < blocksize / 4; i++)
Magnus Damm487d9fc2010-05-18 14:42:51 +0000484 *p++ = sh_mmcif_readl(host->addr, MMCIF_CE_DATA);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700485
486 /* buffer read end */
487 sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFRE);
Guennadi Liakhovetskiaa0787a2010-11-24 10:05:12 +0000488 time = wait_for_completion_interruptible_timeout(&host->intr_wait,
489 host->timeout);
490 if (time <= 0 || host->sd_error)
Yusuke Godafdc50a92010-05-26 14:41:59 -0700491 return sh_mmcif_error_manage(host);
492
Yusuke Godafdc50a92010-05-26 14:41:59 -0700493 return 0;
494}
495
496static int sh_mmcif_multi_read(struct sh_mmcif_host *host,
497 struct mmc_request *mrq)
498{
499 struct mmc_data *data = mrq->data;
500 long time;
501 u32 blocksize, i, j, sec, *p;
502
Magnus Damm487d9fc2010-05-18 14:42:51 +0000503 blocksize = BLOCK_SIZE_MASK & sh_mmcif_readl(host->addr,
504 MMCIF_CE_BLOCK_SET);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700505 for (j = 0; j < data->sg_len; j++) {
506 p = sg_virt(data->sg);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700507 for (sec = 0; sec < data->sg->length / blocksize; sec++) {
508 sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFREN);
509 /* buf read enable */
Guennadi Liakhovetskiaa0787a2010-11-24 10:05:12 +0000510 time = wait_for_completion_interruptible_timeout(&host->intr_wait,
511 host->timeout);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700512
Guennadi Liakhovetskiaa0787a2010-11-24 10:05:12 +0000513 if (time <= 0 || host->sd_error)
Yusuke Godafdc50a92010-05-26 14:41:59 -0700514 return sh_mmcif_error_manage(host);
515
Yusuke Godafdc50a92010-05-26 14:41:59 -0700516 for (i = 0; i < blocksize / 4; i++)
Magnus Damm487d9fc2010-05-18 14:42:51 +0000517 *p++ = sh_mmcif_readl(host->addr,
518 MMCIF_CE_DATA);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700519 }
520 if (j < data->sg_len - 1)
521 data->sg++;
522 }
523 return 0;
524}
525
526static int sh_mmcif_single_write(struct sh_mmcif_host *host,
527 struct mmc_request *mrq)
528{
529 struct mmc_data *data = mrq->data;
530 long time;
531 u32 blocksize, i, *p = sg_virt(data->sg);
532
Yusuke Godafdc50a92010-05-26 14:41:59 -0700533 sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN);
534
535 /* buf write enable */
Guennadi Liakhovetskiaa0787a2010-11-24 10:05:12 +0000536 time = wait_for_completion_interruptible_timeout(&host->intr_wait,
537 host->timeout);
538 if (time <= 0 || host->sd_error)
Yusuke Godafdc50a92010-05-26 14:41:59 -0700539 return sh_mmcif_error_manage(host);
540
Yusuke Godafdc50a92010-05-26 14:41:59 -0700541 blocksize = (BLOCK_SIZE_MASK &
Magnus Damm487d9fc2010-05-18 14:42:51 +0000542 sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET)) + 3;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700543 for (i = 0; i < blocksize / 4; i++)
Magnus Damm487d9fc2010-05-18 14:42:51 +0000544 sh_mmcif_writel(host->addr, MMCIF_CE_DATA, *p++);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700545
546 /* buffer write end */
547 sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MDTRANE);
548
Guennadi Liakhovetskiaa0787a2010-11-24 10:05:12 +0000549 time = wait_for_completion_interruptible_timeout(&host->intr_wait,
550 host->timeout);
551 if (time <= 0 || host->sd_error)
Yusuke Godafdc50a92010-05-26 14:41:59 -0700552 return sh_mmcif_error_manage(host);
553
Yusuke Godafdc50a92010-05-26 14:41:59 -0700554 return 0;
555}
556
557static int sh_mmcif_multi_write(struct sh_mmcif_host *host,
558 struct mmc_request *mrq)
559{
560 struct mmc_data *data = mrq->data;
561 long time;
562 u32 i, sec, j, blocksize, *p;
563
Magnus Damm487d9fc2010-05-18 14:42:51 +0000564 blocksize = BLOCK_SIZE_MASK & sh_mmcif_readl(host->addr,
565 MMCIF_CE_BLOCK_SET);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700566
567 for (j = 0; j < data->sg_len; j++) {
568 p = sg_virt(data->sg);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700569 for (sec = 0; sec < data->sg->length / blocksize; sec++) {
570 sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN);
571 /* buf write enable*/
Guennadi Liakhovetskiaa0787a2010-11-24 10:05:12 +0000572 time = wait_for_completion_interruptible_timeout(&host->intr_wait,
573 host->timeout);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700574
Guennadi Liakhovetskiaa0787a2010-11-24 10:05:12 +0000575 if (time <= 0 || host->sd_error)
Yusuke Godafdc50a92010-05-26 14:41:59 -0700576 return sh_mmcif_error_manage(host);
577
Yusuke Godafdc50a92010-05-26 14:41:59 -0700578 for (i = 0; i < blocksize / 4; i++)
Magnus Damm487d9fc2010-05-18 14:42:51 +0000579 sh_mmcif_writel(host->addr,
580 MMCIF_CE_DATA, *p++);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700581 }
582 if (j < data->sg_len - 1)
583 data->sg++;
584 }
585 return 0;
586}
587
588static void sh_mmcif_get_response(struct sh_mmcif_host *host,
589 struct mmc_command *cmd)
590{
591 if (cmd->flags & MMC_RSP_136) {
Magnus Damm487d9fc2010-05-18 14:42:51 +0000592 cmd->resp[0] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP3);
593 cmd->resp[1] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP2);
594 cmd->resp[2] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP1);
595 cmd->resp[3] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP0);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700596 } else
Magnus Damm487d9fc2010-05-18 14:42:51 +0000597 cmd->resp[0] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP0);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700598}
599
600static void sh_mmcif_get_cmd12response(struct sh_mmcif_host *host,
601 struct mmc_command *cmd)
602{
Magnus Damm487d9fc2010-05-18 14:42:51 +0000603 cmd->resp[0] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP_CMD12);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700604}
605
606static u32 sh_mmcif_set_cmd(struct sh_mmcif_host *host,
607 struct mmc_request *mrq, struct mmc_command *cmd, u32 opc)
608{
609 u32 tmp = 0;
610
611 /* Response Type check */
612 switch (mmc_resp_type(cmd)) {
613 case MMC_RSP_NONE:
614 tmp |= CMD_SET_RTYP_NO;
615 break;
616 case MMC_RSP_R1:
617 case MMC_RSP_R1B:
618 case MMC_RSP_R3:
619 tmp |= CMD_SET_RTYP_6B;
620 break;
621 case MMC_RSP_R2:
622 tmp |= CMD_SET_RTYP_17B;
623 break;
624 default:
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +0000625 dev_err(&host->pd->dev, "Unsupported response type.\n");
Yusuke Godafdc50a92010-05-26 14:41:59 -0700626 break;
627 }
628 switch (opc) {
629 /* RBSY */
630 case MMC_SWITCH:
631 case MMC_STOP_TRANSMISSION:
632 case MMC_SET_WRITE_PROT:
633 case MMC_CLR_WRITE_PROT:
634 case MMC_ERASE:
635 case MMC_GEN_CMD:
636 tmp |= CMD_SET_RBSY;
637 break;
638 }
639 /* WDAT / DATW */
640 if (host->data) {
641 tmp |= CMD_SET_WDAT;
642 switch (host->bus_width) {
643 case MMC_BUS_WIDTH_1:
644 tmp |= CMD_SET_DATW_1;
645 break;
646 case MMC_BUS_WIDTH_4:
647 tmp |= CMD_SET_DATW_4;
648 break;
649 case MMC_BUS_WIDTH_8:
650 tmp |= CMD_SET_DATW_8;
651 break;
652 default:
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +0000653 dev_err(&host->pd->dev, "Unsupported bus width.\n");
Yusuke Godafdc50a92010-05-26 14:41:59 -0700654 break;
655 }
656 }
657 /* DWEN */
658 if (opc == MMC_WRITE_BLOCK || opc == MMC_WRITE_MULTIPLE_BLOCK)
659 tmp |= CMD_SET_DWEN;
660 /* CMLTE/CMD12EN */
661 if (opc == MMC_READ_MULTIPLE_BLOCK || opc == MMC_WRITE_MULTIPLE_BLOCK) {
662 tmp |= CMD_SET_CMLTE | CMD_SET_CMD12EN;
663 sh_mmcif_bitset(host, MMCIF_CE_BLOCK_SET,
664 mrq->data->blocks << 16);
665 }
666 /* RIDXC[1:0] check bits */
667 if (opc == MMC_SEND_OP_COND || opc == MMC_ALL_SEND_CID ||
668 opc == MMC_SEND_CSD || opc == MMC_SEND_CID)
669 tmp |= CMD_SET_RIDXC_BITS;
670 /* RCRC7C[1:0] check bits */
671 if (opc == MMC_SEND_OP_COND)
672 tmp |= CMD_SET_CRC7C_BITS;
673 /* RCRC7C[1:0] internal CRC7 */
674 if (opc == MMC_ALL_SEND_CID ||
675 opc == MMC_SEND_CSD || opc == MMC_SEND_CID)
676 tmp |= CMD_SET_CRC7C_INTERNAL;
677
678 return opc = ((opc << 24) | tmp);
679}
680
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +0000681static int sh_mmcif_data_trans(struct sh_mmcif_host *host,
Yusuke Godafdc50a92010-05-26 14:41:59 -0700682 struct mmc_request *mrq, u32 opc)
683{
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +0000684 int ret;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700685
686 switch (opc) {
687 case MMC_READ_MULTIPLE_BLOCK:
688 ret = sh_mmcif_multi_read(host, mrq);
689 break;
690 case MMC_WRITE_MULTIPLE_BLOCK:
691 ret = sh_mmcif_multi_write(host, mrq);
692 break;
693 case MMC_WRITE_BLOCK:
694 ret = sh_mmcif_single_write(host, mrq);
695 break;
696 case MMC_READ_SINGLE_BLOCK:
697 case MMC_SEND_EXT_CSD:
698 ret = sh_mmcif_single_read(host, mrq);
699 break;
700 default:
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +0000701 dev_err(&host->pd->dev, "UNSUPPORTED CMD = d'%08d\n", opc);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700702 ret = -EINVAL;
703 break;
704 }
705 return ret;
706}
707
708static void sh_mmcif_start_cmd(struct sh_mmcif_host *host,
709 struct mmc_request *mrq, struct mmc_command *cmd)
710{
711 long time;
712 int ret = 0, mask = 0;
713 u32 opc = cmd->opcode;
714
Yusuke Godafdc50a92010-05-26 14:41:59 -0700715 switch (opc) {
716 /* respons busy check */
717 case MMC_SWITCH:
718 case MMC_STOP_TRANSMISSION:
719 case MMC_SET_WRITE_PROT:
720 case MMC_CLR_WRITE_PROT:
721 case MMC_ERASE:
722 case MMC_GEN_CMD:
723 mask = MASK_MRBSYE;
724 break;
725 default:
726 mask = MASK_MCRSPE;
727 break;
728 }
729 mask |= MASK_MCMDVIO | MASK_MBUFVIO | MASK_MWDATERR |
730 MASK_MRDATERR | MASK_MRIDXERR | MASK_MRSPERR |
731 MASK_MCCSTO | MASK_MCRCSTO | MASK_MWDATTO |
732 MASK_MRDATTO | MASK_MRBSYTO | MASK_MRSPTO;
733
734 if (host->data) {
Magnus Damm487d9fc2010-05-18 14:42:51 +0000735 sh_mmcif_writel(host->addr, MMCIF_CE_BLOCK_SET, 0);
736 sh_mmcif_writel(host->addr, MMCIF_CE_BLOCK_SET,
737 mrq->data->blksz);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700738 }
739 opc = sh_mmcif_set_cmd(host, mrq, cmd, opc);
740
Magnus Damm487d9fc2010-05-18 14:42:51 +0000741 sh_mmcif_writel(host->addr, MMCIF_CE_INT, 0xD80430C0);
742 sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, mask);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700743 /* set arg */
Magnus Damm487d9fc2010-05-18 14:42:51 +0000744 sh_mmcif_writel(host->addr, MMCIF_CE_ARG, cmd->arg);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700745 /* set cmd */
Magnus Damm487d9fc2010-05-18 14:42:51 +0000746 sh_mmcif_writel(host->addr, MMCIF_CE_CMD_SET, opc);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700747
Guennadi Liakhovetskiaa0787a2010-11-24 10:05:12 +0000748 time = wait_for_completion_interruptible_timeout(&host->intr_wait,
749 host->timeout);
750 if (time <= 0) {
Yusuke Godafdc50a92010-05-26 14:41:59 -0700751 cmd->error = sh_mmcif_error_manage(host);
752 return;
753 }
754 if (host->sd_error) {
755 switch (cmd->opcode) {
756 case MMC_ALL_SEND_CID:
757 case MMC_SELECT_CARD:
758 case MMC_APP_CMD:
759 cmd->error = -ETIMEDOUT;
760 break;
761 default:
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +0000762 dev_dbg(&host->pd->dev, "Cmd(d'%d) err\n",
763 cmd->opcode);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700764 cmd->error = sh_mmcif_error_manage(host);
765 break;
766 }
Guennadi Liakhovetskiaa0787a2010-11-24 10:05:12 +0000767 host->sd_error = false;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700768 return;
769 }
770 if (!(cmd->flags & MMC_RSP_PRESENT)) {
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +0000771 cmd->error = 0;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700772 return;
773 }
Guennadi Liakhovetskiaa0787a2010-11-24 10:05:12 +0000774 sh_mmcif_get_response(host, cmd);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700775 if (host->data) {
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000776 if (!host->dma_sglen) {
777 ret = sh_mmcif_data_trans(host, mrq, cmd->opcode);
778 } else {
779 long time =
780 wait_for_completion_interruptible_timeout(&host->dma_complete,
781 host->timeout);
782 if (!time)
783 ret = -ETIMEDOUT;
784 else if (time < 0)
785 ret = time;
786 sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC,
787 BUF_ACC_DMAREN | BUF_ACC_DMAWEN);
788 host->dma_sglen = 0;
789 }
Yusuke Godafdc50a92010-05-26 14:41:59 -0700790 if (ret < 0)
791 mrq->data->bytes_xfered = 0;
792 else
793 mrq->data->bytes_xfered =
794 mrq->data->blocks * mrq->data->blksz;
795 }
796 cmd->error = ret;
797}
798
799static void sh_mmcif_stop_cmd(struct sh_mmcif_host *host,
800 struct mmc_request *mrq, struct mmc_command *cmd)
801{
802 long time;
803
804 if (mrq->cmd->opcode == MMC_READ_MULTIPLE_BLOCK)
805 sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MCMD12DRE);
806 else if (mrq->cmd->opcode == MMC_WRITE_MULTIPLE_BLOCK)
807 sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MCMD12RBE);
808 else {
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +0000809 dev_err(&host->pd->dev, "unsupported stop cmd\n");
Yusuke Godafdc50a92010-05-26 14:41:59 -0700810 cmd->error = sh_mmcif_error_manage(host);
811 return;
812 }
813
Guennadi Liakhovetskiaa0787a2010-11-24 10:05:12 +0000814 time = wait_for_completion_interruptible_timeout(&host->intr_wait,
815 host->timeout);
816 if (time <= 0 || host->sd_error) {
Yusuke Godafdc50a92010-05-26 14:41:59 -0700817 cmd->error = sh_mmcif_error_manage(host);
818 return;
819 }
820 sh_mmcif_get_cmd12response(host, cmd);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700821 cmd->error = 0;
822}
823
824static void sh_mmcif_request(struct mmc_host *mmc, struct mmc_request *mrq)
825{
826 struct sh_mmcif_host *host = mmc_priv(mmc);
827
828 switch (mrq->cmd->opcode) {
829 /* MMCIF does not support SD/SDIO command */
830 case SD_IO_SEND_OP_COND:
831 case MMC_APP_CMD:
832 mrq->cmd->error = -ETIMEDOUT;
833 mmc_request_done(mmc, mrq);
834 return;
835 case MMC_SEND_EXT_CSD: /* = SD_SEND_IF_COND (8) */
836 if (!mrq->data) {
837 /* send_if_cond cmd (not support) */
838 mrq->cmd->error = -ETIMEDOUT;
839 mmc_request_done(mmc, mrq);
840 return;
841 }
842 break;
843 default:
844 break;
845 }
846 host->data = mrq->data;
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000847 if (mrq->data) {
848 if (mrq->data->flags & MMC_DATA_READ) {
849 if (host->chan_rx)
850 sh_mmcif_start_dma_rx(host);
851 } else {
852 if (host->chan_tx)
853 sh_mmcif_start_dma_tx(host);
854 }
855 }
Yusuke Godafdc50a92010-05-26 14:41:59 -0700856 sh_mmcif_start_cmd(host, mrq, mrq->cmd);
857 host->data = NULL;
858
859 if (mrq->cmd->error != 0) {
860 mmc_request_done(mmc, mrq);
861 return;
862 }
863 if (mrq->stop)
864 sh_mmcif_stop_cmd(host, mrq, mrq->stop);
865 mmc_request_done(mmc, mrq);
866}
867
868static void sh_mmcif_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
869{
870 struct sh_mmcif_host *host = mmc_priv(mmc);
871 struct sh_mmcif_plat_data *p = host->pd->dev.platform_data;
872
873 if (ios->power_mode == MMC_POWER_OFF) {
874 /* clock stop */
875 sh_mmcif_clock_control(host, 0);
876 if (p->down_pwr)
877 p->down_pwr(host->pd);
878 return;
879 } else if (ios->power_mode == MMC_POWER_UP) {
880 if (p->set_pwr)
881 p->set_pwr(host->pd, ios->power_mode);
882 }
883
884 if (ios->clock)
885 sh_mmcif_clock_control(host, ios->clock);
886
887 host->bus_width = ios->bus_width;
888}
889
Arnd Hannemann777271d2010-08-24 17:27:01 +0200890static int sh_mmcif_get_cd(struct mmc_host *mmc)
891{
892 struct sh_mmcif_host *host = mmc_priv(mmc);
893 struct sh_mmcif_plat_data *p = host->pd->dev.platform_data;
894
895 if (!p->get_cd)
896 return -ENOSYS;
897 else
898 return p->get_cd(host->pd);
899}
900
Yusuke Godafdc50a92010-05-26 14:41:59 -0700901static struct mmc_host_ops sh_mmcif_ops = {
902 .request = sh_mmcif_request,
903 .set_ios = sh_mmcif_set_ios,
Arnd Hannemann777271d2010-08-24 17:27:01 +0200904 .get_cd = sh_mmcif_get_cd,
Yusuke Godafdc50a92010-05-26 14:41:59 -0700905};
906
907static void sh_mmcif_detect(struct mmc_host *mmc)
908{
909 mmc_detect_change(mmc, 0);
910}
911
912static irqreturn_t sh_mmcif_intr(int irq, void *dev_id)
913{
914 struct sh_mmcif_host *host = dev_id;
Guennadi Liakhovetskiaa0787a2010-11-24 10:05:12 +0000915 u32 state;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700916 int err = 0;
917
Magnus Damm487d9fc2010-05-18 14:42:51 +0000918 state = sh_mmcif_readl(host->addr, MMCIF_CE_INT);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700919
920 if (state & INT_RBSYE) {
Magnus Damm487d9fc2010-05-18 14:42:51 +0000921 sh_mmcif_writel(host->addr, MMCIF_CE_INT,
922 ~(INT_RBSYE | INT_CRSPE));
Yusuke Godafdc50a92010-05-26 14:41:59 -0700923 sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MRBSYE);
924 } else if (state & INT_CRSPE) {
Magnus Damm487d9fc2010-05-18 14:42:51 +0000925 sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~INT_CRSPE);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700926 sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MCRSPE);
927 } else if (state & INT_BUFREN) {
Magnus Damm487d9fc2010-05-18 14:42:51 +0000928 sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~INT_BUFREN);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700929 sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MBUFREN);
930 } else if (state & INT_BUFWEN) {
Magnus Damm487d9fc2010-05-18 14:42:51 +0000931 sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~INT_BUFWEN);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700932 sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN);
933 } else if (state & INT_CMD12DRE) {
Magnus Damm487d9fc2010-05-18 14:42:51 +0000934 sh_mmcif_writel(host->addr, MMCIF_CE_INT,
Yusuke Godafdc50a92010-05-26 14:41:59 -0700935 ~(INT_CMD12DRE | INT_CMD12RBE |
936 INT_CMD12CRE | INT_BUFRE));
937 sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MCMD12DRE);
938 } else if (state & INT_BUFRE) {
Magnus Damm487d9fc2010-05-18 14:42:51 +0000939 sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~INT_BUFRE);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700940 sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MBUFRE);
941 } else if (state & INT_DTRANE) {
Magnus Damm487d9fc2010-05-18 14:42:51 +0000942 sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~INT_DTRANE);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700943 sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MDTRANE);
944 } else if (state & INT_CMD12RBE) {
Magnus Damm487d9fc2010-05-18 14:42:51 +0000945 sh_mmcif_writel(host->addr, MMCIF_CE_INT,
Yusuke Godafdc50a92010-05-26 14:41:59 -0700946 ~(INT_CMD12RBE | INT_CMD12CRE));
947 sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MCMD12RBE);
948 } else if (state & INT_ERR_STS) {
949 /* err interrupts */
Magnus Damm487d9fc2010-05-18 14:42:51 +0000950 sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~state);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700951 sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, state);
952 err = 1;
953 } else {
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +0000954 dev_dbg(&host->pd->dev, "Not support int\n");
Magnus Damm487d9fc2010-05-18 14:42:51 +0000955 sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~state);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700956 sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, state);
957 err = 1;
958 }
959 if (err) {
Guennadi Liakhovetskiaa0787a2010-11-24 10:05:12 +0000960 host->sd_error = true;
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +0000961 dev_dbg(&host->pd->dev, "int err state = %08x\n", state);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700962 }
Guennadi Liakhovetskiaa0787a2010-11-24 10:05:12 +0000963 if (state & ~(INT_CMD12RBE | INT_CMD12CRE))
964 complete(&host->intr_wait);
965 else
966 dev_dbg(&host->pd->dev, "Unexpected IRQ 0x%x\n", state);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700967
968 return IRQ_HANDLED;
969}
970
971static int __devinit sh_mmcif_probe(struct platform_device *pdev)
972{
973 int ret = 0, irq[2];
974 struct mmc_host *mmc;
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +0000975 struct sh_mmcif_host *host;
976 struct sh_mmcif_plat_data *pd;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700977 struct resource *res;
978 void __iomem *reg;
979 char clk_name[8];
980
981 irq[0] = platform_get_irq(pdev, 0);
982 irq[1] = platform_get_irq(pdev, 1);
983 if (irq[0] < 0 || irq[1] < 0) {
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +0000984 dev_err(&pdev->dev, "Get irq error\n");
Yusuke Godafdc50a92010-05-26 14:41:59 -0700985 return -ENXIO;
986 }
987 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
988 if (!res) {
989 dev_err(&pdev->dev, "platform_get_resource error.\n");
990 return -ENXIO;
991 }
992 reg = ioremap(res->start, resource_size(res));
993 if (!reg) {
994 dev_err(&pdev->dev, "ioremap error.\n");
995 return -ENOMEM;
996 }
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +0000997 pd = pdev->dev.platform_data;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700998 if (!pd) {
999 dev_err(&pdev->dev, "sh_mmcif plat data error.\n");
1000 ret = -ENXIO;
1001 goto clean_up;
1002 }
1003 mmc = mmc_alloc_host(sizeof(struct sh_mmcif_host), &pdev->dev);
1004 if (!mmc) {
1005 ret = -ENOMEM;
1006 goto clean_up;
1007 }
1008 host = mmc_priv(mmc);
1009 host->mmc = mmc;
1010 host->addr = reg;
1011 host->timeout = 1000;
1012
1013 snprintf(clk_name, sizeof(clk_name), "mmc%d", pdev->id);
1014 host->hclk = clk_get(&pdev->dev, clk_name);
1015 if (IS_ERR(host->hclk)) {
1016 dev_err(&pdev->dev, "cannot get clock \"%s\"\n", clk_name);
1017 ret = PTR_ERR(host->hclk);
1018 goto clean_up1;
1019 }
1020 clk_enable(host->hclk);
1021 host->clk = clk_get_rate(host->hclk);
1022 host->pd = pdev;
1023
Guennadi Liakhovetskiaa0787a2010-11-24 10:05:12 +00001024 init_completion(&host->intr_wait);
Yusuke Godafdc50a92010-05-26 14:41:59 -07001025
1026 mmc->ops = &sh_mmcif_ops;
1027 mmc->f_max = host->clk;
1028 /* close to 400KHz */
1029 if (mmc->f_max < 51200000)
1030 mmc->f_min = mmc->f_max / 128;
1031 else if (mmc->f_max < 102400000)
1032 mmc->f_min = mmc->f_max / 256;
1033 else
1034 mmc->f_min = mmc->f_max / 512;
1035 if (pd->ocr)
1036 mmc->ocr_avail = pd->ocr;
1037 mmc->caps = MMC_CAP_MMC_HIGHSPEED;
1038 if (pd->caps)
1039 mmc->caps |= pd->caps;
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +00001040 mmc->max_segs = 32;
Yusuke Godafdc50a92010-05-26 14:41:59 -07001041 mmc->max_blk_size = 512;
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +00001042 mmc->max_req_size = PAGE_CACHE_SIZE * mmc->max_segs;
1043 mmc->max_blk_count = mmc->max_req_size / mmc->max_blk_size;
Yusuke Godafdc50a92010-05-26 14:41:59 -07001044 mmc->max_seg_size = mmc->max_req_size;
1045
1046 sh_mmcif_sync_reset(host);
1047 platform_set_drvdata(pdev, host);
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +00001048
1049 /* See if we also get DMA */
1050 sh_mmcif_request_dma(host, pd);
1051
Yusuke Godafdc50a92010-05-26 14:41:59 -07001052 mmc_add_host(mmc);
1053
1054 ret = request_irq(irq[0], sh_mmcif_intr, 0, "sh_mmc:error", host);
1055 if (ret) {
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +00001056 dev_err(&pdev->dev, "request_irq error (sh_mmc:error)\n");
Yusuke Godafdc50a92010-05-26 14:41:59 -07001057 goto clean_up2;
1058 }
1059 ret = request_irq(irq[1], sh_mmcif_intr, 0, "sh_mmc:int", host);
1060 if (ret) {
1061 free_irq(irq[0], host);
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +00001062 dev_err(&pdev->dev, "request_irq error (sh_mmc:int)\n");
Yusuke Godafdc50a92010-05-26 14:41:59 -07001063 goto clean_up2;
1064 }
1065
Magnus Damm487d9fc2010-05-18 14:42:51 +00001066 sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL);
Yusuke Godafdc50a92010-05-26 14:41:59 -07001067 sh_mmcif_detect(host->mmc);
1068
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +00001069 dev_info(&pdev->dev, "driver version %s\n", DRIVER_VERSION);
1070 dev_dbg(&pdev->dev, "chip ver H'%04x\n",
Magnus Damm487d9fc2010-05-18 14:42:51 +00001071 sh_mmcif_readl(host->addr, MMCIF_CE_VERSION) & 0x0000ffff);
Yusuke Godafdc50a92010-05-26 14:41:59 -07001072 return ret;
1073
1074clean_up2:
1075 clk_disable(host->hclk);
1076clean_up1:
1077 mmc_free_host(mmc);
1078clean_up:
1079 if (reg)
1080 iounmap(reg);
1081 return ret;
1082}
1083
1084static int __devexit sh_mmcif_remove(struct platform_device *pdev)
1085{
1086 struct sh_mmcif_host *host = platform_get_drvdata(pdev);
1087 int irq[2];
1088
Guennadi Liakhovetskiaa0787a2010-11-24 10:05:12 +00001089 mmc_remove_host(host->mmc);
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +00001090 sh_mmcif_release_dma(host);
Guennadi Liakhovetskiaa0787a2010-11-24 10:05:12 +00001091
1092 if (host->addr)
1093 iounmap(host->addr);
1094
Magnus Damm487d9fc2010-05-18 14:42:51 +00001095 sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL);
Yusuke Godafdc50a92010-05-26 14:41:59 -07001096
1097 irq[0] = platform_get_irq(pdev, 0);
1098 irq[1] = platform_get_irq(pdev, 1);
1099
Yusuke Godafdc50a92010-05-26 14:41:59 -07001100 free_irq(irq[0], host);
1101 free_irq(irq[1], host);
1102
Guennadi Liakhovetskiaa0787a2010-11-24 10:05:12 +00001103 platform_set_drvdata(pdev, NULL);
1104
Yusuke Godafdc50a92010-05-26 14:41:59 -07001105 clk_disable(host->hclk);
1106 mmc_free_host(host->mmc);
1107
1108 return 0;
1109}
1110
1111static struct platform_driver sh_mmcif_driver = {
1112 .probe = sh_mmcif_probe,
1113 .remove = sh_mmcif_remove,
1114 .driver = {
1115 .name = DRIVER_NAME,
1116 },
1117};
1118
1119static int __init sh_mmcif_init(void)
1120{
1121 return platform_driver_register(&sh_mmcif_driver);
1122}
1123
1124static void __exit sh_mmcif_exit(void)
1125{
1126 platform_driver_unregister(&sh_mmcif_driver);
1127}
1128
1129module_init(sh_mmcif_init);
1130module_exit(sh_mmcif_exit);
1131
1132
1133MODULE_DESCRIPTION("SuperH on-chip MMC/eMMC interface driver");
1134MODULE_LICENSE("GPL");
Guennadi Liakhovetskiaa0787a2010-11-24 10:05:12 +00001135MODULE_ALIAS("platform:" DRIVER_NAME);
Yusuke Godafdc50a92010-05-26 14:41:59 -07001136MODULE_AUTHOR("Yusuke Goda <yusuke.goda.sx@renesas.com>");