blob: 2b9f7c8567003f0a9a2279798b82286e548c875d [file] [log] [blame]
Sascha Hauerd96be872009-01-06 17:04:14 +01001/*
2 * linux/drivers/mmc/host/mxcmmc.c - Freescale i.MX MMCI driver
3 *
4 * This is a driver for the SDHC controller found in Freescale MX2/MX3
5 * SoCs. It is basically the same hardware as found on MX1 (imxmmc.c).
6 * Unlike the hardware found on MX1, this hardware just works and does
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08007 * not need all the quirks found in imxmmc.c, hence the separate driver.
Sascha Hauerd96be872009-01-06 17:04:14 +01008 *
9 * Copyright (C) 2008 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
10 * Copyright (C) 2006 Pavel Pisa, PiKRON <ppisa@pikron.com>
11 *
12 * derived from pxamci.c by Russell King
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2 as
16 * published by the Free Software Foundation.
17 *
18 */
19
20#include <linux/module.h>
21#include <linux/init.h>
22#include <linux/ioport.h>
23#include <linux/platform_device.h>
24#include <linux/interrupt.h>
25#include <linux/irq.h>
26#include <linux/blkdev.h>
27#include <linux/dma-mapping.h>
28#include <linux/mmc/host.h>
29#include <linux/mmc/card.h>
30#include <linux/delay.h>
31#include <linux/clk.h>
32#include <linux/io.h>
33#include <linux/gpio.h>
Alberto Panizzo74b66952010-11-02 01:05:37 +010034#include <linux/regulator/consumer.h>
Sascha Hauerd96be872009-01-06 17:04:14 +010035
36#include <asm/dma.h>
37#include <asm/irq.h>
38#include <asm/sizes.h>
39#include <mach/mmc.h>
40
41#ifdef CONFIG_ARCH_MX2
42#include <mach/dma-mx1-mx2.h>
43#define HAS_DMA
44#endif
45
Sascha Hauer9563b1d2009-02-23 13:08:06 +010046#define DRIVER_NAME "mxc-mmc"
Sascha Hauerd96be872009-01-06 17:04:14 +010047
48#define MMC_REG_STR_STP_CLK 0x00
49#define MMC_REG_STATUS 0x04
50#define MMC_REG_CLK_RATE 0x08
51#define MMC_REG_CMD_DAT_CONT 0x0C
52#define MMC_REG_RES_TO 0x10
53#define MMC_REG_READ_TO 0x14
54#define MMC_REG_BLK_LEN 0x18
55#define MMC_REG_NOB 0x1C
56#define MMC_REG_REV_NO 0x20
57#define MMC_REG_INT_CNTR 0x24
58#define MMC_REG_CMD 0x28
59#define MMC_REG_ARG 0x2C
60#define MMC_REG_RES_FIFO 0x34
61#define MMC_REG_BUFFER_ACCESS 0x38
62
63#define STR_STP_CLK_RESET (1 << 3)
64#define STR_STP_CLK_START_CLK (1 << 1)
65#define STR_STP_CLK_STOP_CLK (1 << 0)
66
67#define STATUS_CARD_INSERTION (1 << 31)
68#define STATUS_CARD_REMOVAL (1 << 30)
69#define STATUS_YBUF_EMPTY (1 << 29)
70#define STATUS_XBUF_EMPTY (1 << 28)
71#define STATUS_YBUF_FULL (1 << 27)
72#define STATUS_XBUF_FULL (1 << 26)
73#define STATUS_BUF_UND_RUN (1 << 25)
74#define STATUS_BUF_OVFL (1 << 24)
75#define STATUS_SDIO_INT_ACTIVE (1 << 14)
76#define STATUS_END_CMD_RESP (1 << 13)
77#define STATUS_WRITE_OP_DONE (1 << 12)
78#define STATUS_DATA_TRANS_DONE (1 << 11)
79#define STATUS_READ_OP_DONE (1 << 11)
80#define STATUS_WR_CRC_ERROR_CODE_MASK (3 << 10)
81#define STATUS_CARD_BUS_CLK_RUN (1 << 8)
82#define STATUS_BUF_READ_RDY (1 << 7)
83#define STATUS_BUF_WRITE_RDY (1 << 6)
84#define STATUS_RESP_CRC_ERR (1 << 5)
85#define STATUS_CRC_READ_ERR (1 << 3)
86#define STATUS_CRC_WRITE_ERR (1 << 2)
87#define STATUS_TIME_OUT_RESP (1 << 1)
88#define STATUS_TIME_OUT_READ (1 << 0)
89#define STATUS_ERR_MASK 0x2f
90
91#define CMD_DAT_CONT_CMD_RESP_LONG_OFF (1 << 12)
92#define CMD_DAT_CONT_STOP_READWAIT (1 << 11)
93#define CMD_DAT_CONT_START_READWAIT (1 << 10)
94#define CMD_DAT_CONT_BUS_WIDTH_4 (2 << 8)
95#define CMD_DAT_CONT_INIT (1 << 7)
96#define CMD_DAT_CONT_WRITE (1 << 4)
97#define CMD_DAT_CONT_DATA_ENABLE (1 << 3)
98#define CMD_DAT_CONT_RESPONSE_48BIT_CRC (1 << 0)
99#define CMD_DAT_CONT_RESPONSE_136BIT (2 << 0)
100#define CMD_DAT_CONT_RESPONSE_48BIT (3 << 0)
101
102#define INT_SDIO_INT_WKP_EN (1 << 18)
103#define INT_CARD_INSERTION_WKP_EN (1 << 17)
104#define INT_CARD_REMOVAL_WKP_EN (1 << 16)
105#define INT_CARD_INSERTION_EN (1 << 15)
106#define INT_CARD_REMOVAL_EN (1 << 14)
107#define INT_SDIO_IRQ_EN (1 << 13)
108#define INT_DAT0_EN (1 << 12)
109#define INT_BUF_READ_EN (1 << 4)
110#define INT_BUF_WRITE_EN (1 << 3)
111#define INT_END_CMD_RES_EN (1 << 2)
112#define INT_WRITE_OP_DONE_EN (1 << 1)
113#define INT_READ_OP_EN (1 << 0)
114
115struct mxcmci_host {
116 struct mmc_host *mmc;
117 struct resource *res;
118 void __iomem *base;
119 int irq;
120 int detect_irq;
121 int dma;
122 int do_dma;
Eric Bénard16b3bf82010-05-19 18:46:04 +0200123 int default_irq_mask;
Daniel Mackf441b992010-04-01 10:03:24 +0200124 int use_sdio;
Sascha Hauerd96be872009-01-06 17:04:14 +0100125 unsigned int power_mode;
126 struct imxmmc_platform_data *pdata;
127
128 struct mmc_request *req;
129 struct mmc_command *cmd;
130 struct mmc_data *data;
131
132 unsigned int dma_nents;
133 unsigned int datasize;
134 unsigned int dma_dir;
135
136 u16 rev_no;
137 unsigned int cmdat;
138
139 struct clk *clk;
140
141 int clock;
142
143 struct work_struct datawork;
Daniel Mackf441b992010-04-01 10:03:24 +0200144 spinlock_t lock;
Alberto Panizzo74b66952010-11-02 01:05:37 +0100145
146 struct regulator *vcc;
Sascha Hauerd96be872009-01-06 17:04:14 +0100147};
148
Martin Fuzzey18489fa2009-04-16 22:00:36 +0200149static void mxcmci_set_clk_rate(struct mxcmci_host *host, unsigned int clk_ios);
150
Alberto Panizzo74b66952010-11-02 01:05:37 +0100151static inline void mxcmci_init_ocr(struct mxcmci_host *host)
152{
Alberto Panizzo74b66952010-11-02 01:05:37 +0100153 host->vcc = regulator_get(mmc_dev(host->mmc), "vmmc");
154
155 if (IS_ERR(host->vcc)) {
156 host->vcc = NULL;
157 } else {
158 host->mmc->ocr_avail = mmc_regulator_get_ocrmask(host->vcc);
159 if (host->pdata && host->pdata->ocr_avail)
160 dev_warn(mmc_dev(host->mmc),
161 "pdata->ocr_avail will not be used\n");
162 }
Alberto Panizzod078d242010-11-09 11:35:37 +0100163
Alberto Panizzo74b66952010-11-02 01:05:37 +0100164 if (host->vcc == NULL) {
165 /* fall-back to platform data */
166 if (host->pdata && host->pdata->ocr_avail)
167 host->mmc->ocr_avail = host->pdata->ocr_avail;
168 else
169 host->mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
170 }
171}
172
Alberto Panizzod078d242010-11-09 11:35:37 +0100173static inline void mxcmci_set_power(struct mxcmci_host *host,
174 unsigned char power_mode,
175 unsigned int vdd)
Alberto Panizzo74b66952010-11-02 01:05:37 +0100176{
Alberto Panizzod078d242010-11-09 11:35:37 +0100177 if (host->vcc) {
178 if (power_mode == MMC_POWER_UP)
179 mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
180 else if (power_mode == MMC_POWER_OFF)
181 mmc_regulator_set_ocr(host->mmc, host->vcc, 0);
182 }
183
Alberto Panizzo74b66952010-11-02 01:05:37 +0100184 if (host->pdata && host->pdata->setpower)
185 host->pdata->setpower(mmc_dev(host->mmc), vdd);
186}
187
Sascha Hauerd96be872009-01-06 17:04:14 +0100188static inline int mxcmci_use_dma(struct mxcmci_host *host)
189{
190 return host->do_dma;
191}
192
193static void mxcmci_softreset(struct mxcmci_host *host)
194{
195 int i;
196
Daniel Mack4725f6f2010-04-01 10:03:23 +0200197 dev_dbg(mmc_dev(host->mmc), "mxcmci_softreset\n");
198
Sascha Hauerd96be872009-01-06 17:04:14 +0100199 /* reset sequence */
200 writew(STR_STP_CLK_RESET, host->base + MMC_REG_STR_STP_CLK);
201 writew(STR_STP_CLK_RESET | STR_STP_CLK_START_CLK,
202 host->base + MMC_REG_STR_STP_CLK);
203
204 for (i = 0; i < 8; i++)
205 writew(STR_STP_CLK_START_CLK, host->base + MMC_REG_STR_STP_CLK);
206
207 writew(0xff, host->base + MMC_REG_RES_TO);
208}
209
Martin Fuzzey656217d2009-04-16 22:00:41 +0200210static int mxcmci_setup_data(struct mxcmci_host *host, struct mmc_data *data)
Sascha Hauerd96be872009-01-06 17:04:14 +0100211{
212 unsigned int nob = data->blocks;
213 unsigned int blksz = data->blksz;
214 unsigned int datasize = nob * blksz;
215#ifdef HAS_DMA
216 struct scatterlist *sg;
217 int i;
Martin Fuzzey656217d2009-04-16 22:00:41 +0200218 int ret;
Sascha Hauerd96be872009-01-06 17:04:14 +0100219#endif
220 if (data->flags & MMC_DATA_STREAM)
221 nob = 0xffff;
222
223 host->data = data;
224 data->bytes_xfered = 0;
225
226 writew(nob, host->base + MMC_REG_NOB);
227 writew(blksz, host->base + MMC_REG_BLK_LEN);
228 host->datasize = datasize;
229
230#ifdef HAS_DMA
231 for_each_sg(data->sg, sg, data->sg_len, i) {
232 if (sg->offset & 3 || sg->length & 3) {
233 host->do_dma = 0;
Martin Fuzzey656217d2009-04-16 22:00:41 +0200234 return 0;
Sascha Hauerd96be872009-01-06 17:04:14 +0100235 }
236 }
237
238 if (data->flags & MMC_DATA_READ) {
239 host->dma_dir = DMA_FROM_DEVICE;
240 host->dma_nents = dma_map_sg(mmc_dev(host->mmc), data->sg,
241 data->sg_len, host->dma_dir);
242
Martin Fuzzey656217d2009-04-16 22:00:41 +0200243 ret = imx_dma_setup_sg(host->dma, data->sg, host->dma_nents,
244 datasize,
245 host->res->start + MMC_REG_BUFFER_ACCESS,
246 DMA_MODE_READ);
Sascha Hauerd96be872009-01-06 17:04:14 +0100247 } else {
248 host->dma_dir = DMA_TO_DEVICE;
249 host->dma_nents = dma_map_sg(mmc_dev(host->mmc), data->sg,
250 data->sg_len, host->dma_dir);
251
Martin Fuzzey656217d2009-04-16 22:00:41 +0200252 ret = imx_dma_setup_sg(host->dma, data->sg, host->dma_nents,
253 datasize,
254 host->res->start + MMC_REG_BUFFER_ACCESS,
255 DMA_MODE_WRITE);
Sascha Hauerd96be872009-01-06 17:04:14 +0100256 }
257
Martin Fuzzey656217d2009-04-16 22:00:41 +0200258 if (ret) {
259 dev_err(mmc_dev(host->mmc), "failed to setup DMA : %d\n", ret);
260 return ret;
261 }
Sascha Hauerd96be872009-01-06 17:04:14 +0100262 wmb();
263
264 imx_dma_enable(host->dma);
265#endif /* HAS_DMA */
Martin Fuzzey656217d2009-04-16 22:00:41 +0200266 return 0;
Sascha Hauerd96be872009-01-06 17:04:14 +0100267}
268
269static int mxcmci_start_cmd(struct mxcmci_host *host, struct mmc_command *cmd,
270 unsigned int cmdat)
271{
Eric Bénard16b3bf82010-05-19 18:46:04 +0200272 u32 int_cntr = host->default_irq_mask;
Daniel Mackf441b992010-04-01 10:03:24 +0200273 unsigned long flags;
274
Sascha Hauerd96be872009-01-06 17:04:14 +0100275 WARN_ON(host->cmd != NULL);
276 host->cmd = cmd;
277
278 switch (mmc_resp_type(cmd)) {
279 case MMC_RSP_R1: /* short CRC, OPCODE */
280 case MMC_RSP_R1B:/* short CRC, OPCODE, BUSY */
281 cmdat |= CMD_DAT_CONT_RESPONSE_48BIT_CRC;
282 break;
283 case MMC_RSP_R2: /* long 136 bit + CRC */
284 cmdat |= CMD_DAT_CONT_RESPONSE_136BIT;
285 break;
286 case MMC_RSP_R3: /* short */
287 cmdat |= CMD_DAT_CONT_RESPONSE_48BIT;
288 break;
289 case MMC_RSP_NONE:
290 break;
291 default:
292 dev_err(mmc_dev(host->mmc), "unhandled response type 0x%x\n",
293 mmc_resp_type(cmd));
294 cmd->error = -EINVAL;
295 return -EINVAL;
296 }
297
Daniel Mackf441b992010-04-01 10:03:24 +0200298 int_cntr = INT_END_CMD_RES_EN;
299
Sascha Hauerd96be872009-01-06 17:04:14 +0100300 if (mxcmci_use_dma(host))
Daniel Mackf441b992010-04-01 10:03:24 +0200301 int_cntr |= INT_READ_OP_EN | INT_WRITE_OP_DONE_EN;
302
303 spin_lock_irqsave(&host->lock, flags);
304 if (host->use_sdio)
305 int_cntr |= INT_SDIO_IRQ_EN;
306 writel(int_cntr, host->base + MMC_REG_INT_CNTR);
307 spin_unlock_irqrestore(&host->lock, flags);
Sascha Hauerd96be872009-01-06 17:04:14 +0100308
309 writew(cmd->opcode, host->base + MMC_REG_CMD);
310 writel(cmd->arg, host->base + MMC_REG_ARG);
311 writew(cmdat, host->base + MMC_REG_CMD_DAT_CONT);
312
313 return 0;
314}
315
316static void mxcmci_finish_request(struct mxcmci_host *host,
317 struct mmc_request *req)
318{
Eric Bénard16b3bf82010-05-19 18:46:04 +0200319 u32 int_cntr = host->default_irq_mask;
Daniel Mackf441b992010-04-01 10:03:24 +0200320 unsigned long flags;
321
322 spin_lock_irqsave(&host->lock, flags);
323 if (host->use_sdio)
324 int_cntr |= INT_SDIO_IRQ_EN;
325 writel(int_cntr, host->base + MMC_REG_INT_CNTR);
326 spin_unlock_irqrestore(&host->lock, flags);
Sascha Hauerd96be872009-01-06 17:04:14 +0100327
328 host->req = NULL;
329 host->cmd = NULL;
330 host->data = NULL;
331
332 mmc_request_done(host->mmc, req);
333}
334
335static int mxcmci_finish_data(struct mxcmci_host *host, unsigned int stat)
336{
337 struct mmc_data *data = host->data;
338 int data_error;
339
340#ifdef HAS_DMA
341 if (mxcmci_use_dma(host)) {
342 imx_dma_disable(host->dma);
343 dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->dma_nents,
344 host->dma_dir);
345 }
346#endif
347
348 if (stat & STATUS_ERR_MASK) {
349 dev_dbg(mmc_dev(host->mmc), "request failed. status: 0x%08x\n",
350 stat);
351 if (stat & STATUS_CRC_READ_ERR) {
Daniel Mack4725f6f2010-04-01 10:03:23 +0200352 dev_err(mmc_dev(host->mmc), "%s: -EILSEQ\n", __func__);
Sascha Hauerd96be872009-01-06 17:04:14 +0100353 data->error = -EILSEQ;
354 } else if (stat & STATUS_CRC_WRITE_ERR) {
355 u32 err_code = (stat >> 9) & 0x3;
Daniel Mack4725f6f2010-04-01 10:03:23 +0200356 if (err_code == 2) { /* No CRC response */
357 dev_err(mmc_dev(host->mmc),
358 "%s: No CRC -ETIMEDOUT\n", __func__);
Sascha Hauerd96be872009-01-06 17:04:14 +0100359 data->error = -ETIMEDOUT;
Daniel Mack4725f6f2010-04-01 10:03:23 +0200360 } else {
361 dev_err(mmc_dev(host->mmc),
362 "%s: -EILSEQ\n", __func__);
Sascha Hauerd96be872009-01-06 17:04:14 +0100363 data->error = -EILSEQ;
Daniel Mack4725f6f2010-04-01 10:03:23 +0200364 }
Sascha Hauerd96be872009-01-06 17:04:14 +0100365 } else if (stat & STATUS_TIME_OUT_READ) {
Daniel Mack4725f6f2010-04-01 10:03:23 +0200366 dev_err(mmc_dev(host->mmc),
367 "%s: read -ETIMEDOUT\n", __func__);
Sascha Hauerd96be872009-01-06 17:04:14 +0100368 data->error = -ETIMEDOUT;
369 } else {
Daniel Mack4725f6f2010-04-01 10:03:23 +0200370 dev_err(mmc_dev(host->mmc), "%s: -EIO\n", __func__);
Sascha Hauerd96be872009-01-06 17:04:14 +0100371 data->error = -EIO;
372 }
373 } else {
374 data->bytes_xfered = host->datasize;
375 }
376
377 data_error = data->error;
378
379 host->data = NULL;
380
381 return data_error;
382}
383
384static void mxcmci_read_response(struct mxcmci_host *host, unsigned int stat)
385{
386 struct mmc_command *cmd = host->cmd;
387 int i;
388 u32 a, b, c;
389
390 if (!cmd)
391 return;
392
393 if (stat & STATUS_TIME_OUT_RESP) {
394 dev_dbg(mmc_dev(host->mmc), "CMD TIMEOUT\n");
395 cmd->error = -ETIMEDOUT;
396 } else if (stat & STATUS_RESP_CRC_ERR && cmd->flags & MMC_RSP_CRC) {
397 dev_dbg(mmc_dev(host->mmc), "cmd crc error\n");
398 cmd->error = -EILSEQ;
399 }
400
401 if (cmd->flags & MMC_RSP_PRESENT) {
402 if (cmd->flags & MMC_RSP_136) {
403 for (i = 0; i < 4; i++) {
404 a = readw(host->base + MMC_REG_RES_FIFO);
405 b = readw(host->base + MMC_REG_RES_FIFO);
406 cmd->resp[i] = a << 16 | b;
407 }
408 } else {
409 a = readw(host->base + MMC_REG_RES_FIFO);
410 b = readw(host->base + MMC_REG_RES_FIFO);
411 c = readw(host->base + MMC_REG_RES_FIFO);
412 cmd->resp[0] = a << 24 | b << 8 | c >> 8;
413 }
414 }
415}
416
417static int mxcmci_poll_status(struct mxcmci_host *host, u32 mask)
418{
419 u32 stat;
420 unsigned long timeout = jiffies + HZ;
421
422 do {
423 stat = readl(host->base + MMC_REG_STATUS);
424 if (stat & STATUS_ERR_MASK)
425 return stat;
Martin Fuzzey18489fa2009-04-16 22:00:36 +0200426 if (time_after(jiffies, timeout)) {
427 mxcmci_softreset(host);
428 mxcmci_set_clk_rate(host, host->clock);
Sascha Hauerd96be872009-01-06 17:04:14 +0100429 return STATUS_TIME_OUT_READ;
Martin Fuzzey18489fa2009-04-16 22:00:36 +0200430 }
Sascha Hauerd96be872009-01-06 17:04:14 +0100431 if (stat & mask)
432 return 0;
433 cpu_relax();
434 } while (1);
435}
436
437static int mxcmci_pull(struct mxcmci_host *host, void *_buf, int bytes)
438{
439 unsigned int stat;
440 u32 *buf = _buf;
441
442 while (bytes > 3) {
443 stat = mxcmci_poll_status(host,
444 STATUS_BUF_READ_RDY | STATUS_READ_OP_DONE);
445 if (stat)
446 return stat;
447 *buf++ = readl(host->base + MMC_REG_BUFFER_ACCESS);
448 bytes -= 4;
449 }
450
451 if (bytes) {
452 u8 *b = (u8 *)buf;
453 u32 tmp;
454
455 stat = mxcmci_poll_status(host,
456 STATUS_BUF_READ_RDY | STATUS_READ_OP_DONE);
457 if (stat)
458 return stat;
459 tmp = readl(host->base + MMC_REG_BUFFER_ACCESS);
460 memcpy(b, &tmp, bytes);
461 }
462
463 return 0;
464}
465
466static int mxcmci_push(struct mxcmci_host *host, void *_buf, int bytes)
467{
468 unsigned int stat;
469 u32 *buf = _buf;
470
471 while (bytes > 3) {
472 stat = mxcmci_poll_status(host, STATUS_BUF_WRITE_RDY);
473 if (stat)
474 return stat;
475 writel(*buf++, host->base + MMC_REG_BUFFER_ACCESS);
476 bytes -= 4;
477 }
478
479 if (bytes) {
480 u8 *b = (u8 *)buf;
481 u32 tmp;
482
483 stat = mxcmci_poll_status(host, STATUS_BUF_WRITE_RDY);
484 if (stat)
485 return stat;
486
487 memcpy(&tmp, b, bytes);
488 writel(tmp, host->base + MMC_REG_BUFFER_ACCESS);
489 }
490
491 stat = mxcmci_poll_status(host, STATUS_BUF_WRITE_RDY);
492 if (stat)
493 return stat;
494
495 return 0;
496}
497
498static int mxcmci_transfer_data(struct mxcmci_host *host)
499{
500 struct mmc_data *data = host->req->data;
501 struct scatterlist *sg;
502 int stat, i;
503
Sascha Hauerd96be872009-01-06 17:04:14 +0100504 host->data = data;
505 host->datasize = 0;
506
507 if (data->flags & MMC_DATA_READ) {
508 for_each_sg(data->sg, sg, data->sg_len, i) {
509 stat = mxcmci_pull(host, sg_virt(sg), sg->length);
510 if (stat)
511 return stat;
512 host->datasize += sg->length;
513 }
514 } else {
515 for_each_sg(data->sg, sg, data->sg_len, i) {
516 stat = mxcmci_push(host, sg_virt(sg), sg->length);
517 if (stat)
518 return stat;
519 host->datasize += sg->length;
520 }
521 stat = mxcmci_poll_status(host, STATUS_WRITE_OP_DONE);
522 if (stat)
523 return stat;
524 }
525 return 0;
526}
527
528static void mxcmci_datawork(struct work_struct *work)
529{
530 struct mxcmci_host *host = container_of(work, struct mxcmci_host,
531 datawork);
532 int datastat = mxcmci_transfer_data(host);
Daniel Mack4a31f2e2010-04-27 12:24:42 +0200533
534 writel(STATUS_READ_OP_DONE | STATUS_WRITE_OP_DONE,
535 host->base + MMC_REG_STATUS);
Sascha Hauerd96be872009-01-06 17:04:14 +0100536 mxcmci_finish_data(host, datastat);
537
538 if (host->req->stop) {
539 if (mxcmci_start_cmd(host, host->req->stop, 0)) {
540 mxcmci_finish_request(host, host->req);
541 return;
542 }
543 } else {
544 mxcmci_finish_request(host, host->req);
545 }
546}
547
548#ifdef HAS_DMA
549static void mxcmci_data_done(struct mxcmci_host *host, unsigned int stat)
550{
551 struct mmc_data *data = host->data;
552 int data_error;
553
554 if (!data)
555 return;
556
557 data_error = mxcmci_finish_data(host, stat);
558
559 mxcmci_read_response(host, stat);
560 host->cmd = NULL;
561
562 if (host->req->stop) {
563 if (mxcmci_start_cmd(host, host->req->stop, 0)) {
564 mxcmci_finish_request(host, host->req);
565 return;
566 }
567 } else {
568 mxcmci_finish_request(host, host->req);
569 }
570}
571#endif /* HAS_DMA */
572
573static void mxcmci_cmd_done(struct mxcmci_host *host, unsigned int stat)
574{
575 mxcmci_read_response(host, stat);
576 host->cmd = NULL;
577
578 if (!host->data && host->req) {
579 mxcmci_finish_request(host, host->req);
580 return;
581 }
582
583 /* For the DMA case the DMA engine handles the data transfer
Anand Gadiyarfd589a82009-07-16 17:13:03 +0200584 * automatically. For non DMA we have to do it ourselves.
Sascha Hauerd96be872009-01-06 17:04:14 +0100585 * Don't do it in interrupt context though.
586 */
587 if (!mxcmci_use_dma(host) && host->data)
588 schedule_work(&host->datawork);
589
590}
591
592static irqreturn_t mxcmci_irq(int irq, void *devid)
593{
594 struct mxcmci_host *host = devid;
Daniel Mackf441b992010-04-01 10:03:24 +0200595 unsigned long flags;
596 bool sdio_irq;
Sascha Hauerd96be872009-01-06 17:04:14 +0100597 u32 stat;
598
599 stat = readl(host->base + MMC_REG_STATUS);
Daniel Mack4a31f2e2010-04-27 12:24:42 +0200600 writel(stat & ~(STATUS_SDIO_INT_ACTIVE | STATUS_DATA_TRANS_DONE |
601 STATUS_WRITE_OP_DONE), host->base + MMC_REG_STATUS);
Sascha Hauerd96be872009-01-06 17:04:14 +0100602
603 dev_dbg(mmc_dev(host->mmc), "%s: 0x%08x\n", __func__, stat);
604
Daniel Mackf441b992010-04-01 10:03:24 +0200605 spin_lock_irqsave(&host->lock, flags);
606 sdio_irq = (stat & STATUS_SDIO_INT_ACTIVE) && host->use_sdio;
607 spin_unlock_irqrestore(&host->lock, flags);
608
Daniel Mack4a31f2e2010-04-27 12:24:42 +0200609#ifdef HAS_DMA
610 if (mxcmci_use_dma(host) &&
611 (stat & (STATUS_READ_OP_DONE | STATUS_WRITE_OP_DONE)))
612 writel(STATUS_READ_OP_DONE | STATUS_WRITE_OP_DONE,
613 host->base + MMC_REG_STATUS);
614#endif
615
Daniel Mackf441b992010-04-01 10:03:24 +0200616 if (sdio_irq) {
617 writel(STATUS_SDIO_INT_ACTIVE, host->base + MMC_REG_STATUS);
618 mmc_signal_sdio_irq(host->mmc);
619 }
620
Sascha Hauerd96be872009-01-06 17:04:14 +0100621 if (stat & STATUS_END_CMD_RESP)
622 mxcmci_cmd_done(host, stat);
Daniel Mackf441b992010-04-01 10:03:24 +0200623
Sascha Hauerd96be872009-01-06 17:04:14 +0100624#ifdef HAS_DMA
625 if (mxcmci_use_dma(host) &&
626 (stat & (STATUS_DATA_TRANS_DONE | STATUS_WRITE_OP_DONE)))
627 mxcmci_data_done(host, stat);
628#endif
Eric Bénard16b3bf82010-05-19 18:46:04 +0200629 if (host->default_irq_mask &&
630 (stat & (STATUS_CARD_INSERTION | STATUS_CARD_REMOVAL)))
631 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Sascha Hauerd96be872009-01-06 17:04:14 +0100632 return IRQ_HANDLED;
633}
634
635static void mxcmci_request(struct mmc_host *mmc, struct mmc_request *req)
636{
637 struct mxcmci_host *host = mmc_priv(mmc);
638 unsigned int cmdat = host->cmdat;
Martin Fuzzey656217d2009-04-16 22:00:41 +0200639 int error;
Sascha Hauerd96be872009-01-06 17:04:14 +0100640
641 WARN_ON(host->req != NULL);
642
643 host->req = req;
644 host->cmdat &= ~CMD_DAT_CONT_INIT;
645#ifdef HAS_DMA
646 host->do_dma = 1;
647#endif
648 if (req->data) {
Martin Fuzzey656217d2009-04-16 22:00:41 +0200649 error = mxcmci_setup_data(host, req->data);
650 if (error) {
651 req->cmd->error = error;
652 goto out;
653 }
654
Sascha Hauerd96be872009-01-06 17:04:14 +0100655
656 cmdat |= CMD_DAT_CONT_DATA_ENABLE;
657
658 if (req->data->flags & MMC_DATA_WRITE)
659 cmdat |= CMD_DAT_CONT_WRITE;
660 }
661
Martin Fuzzey656217d2009-04-16 22:00:41 +0200662 error = mxcmci_start_cmd(host, req->cmd, cmdat);
663out:
664 if (error)
Sascha Hauerd96be872009-01-06 17:04:14 +0100665 mxcmci_finish_request(host, req);
666}
667
668static void mxcmci_set_clk_rate(struct mxcmci_host *host, unsigned int clk_ios)
669{
670 unsigned int divider;
671 int prescaler = 0;
672 unsigned int clk_in = clk_get_rate(host->clk);
673
674 while (prescaler <= 0x800) {
675 for (divider = 1; divider <= 0xF; divider++) {
676 int x;
677
678 x = (clk_in / (divider + 1));
679
680 if (prescaler)
681 x /= (prescaler * 2);
682
683 if (x <= clk_ios)
684 break;
685 }
686 if (divider < 0x10)
687 break;
688
689 if (prescaler == 0)
690 prescaler = 1;
691 else
692 prescaler <<= 1;
693 }
694
695 writew((prescaler << 4) | divider, host->base + MMC_REG_CLK_RATE);
696
697 dev_dbg(mmc_dev(host->mmc), "scaler: %d divider: %d in: %d out: %d\n",
698 prescaler, divider, clk_in, clk_ios);
699}
700
701static void mxcmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
702{
703 struct mxcmci_host *host = mmc_priv(mmc);
704#ifdef HAS_DMA
705 unsigned int blen;
706 /*
707 * use burstlen of 64 in 4 bit mode (--> reg value 0)
708 * use burstlen of 16 in 1 bit mode (--> reg value 16)
709 */
710 if (ios->bus_width == MMC_BUS_WIDTH_4)
711 blen = 0;
712 else
713 blen = 16;
714
715 imx_dma_config_burstlen(host->dma, blen);
716#endif
717 if (ios->bus_width == MMC_BUS_WIDTH_4)
718 host->cmdat |= CMD_DAT_CONT_BUS_WIDTH_4;
719 else
720 host->cmdat &= ~CMD_DAT_CONT_BUS_WIDTH_4;
721
722 if (host->power_mode != ios->power_mode) {
Alberto Panizzod078d242010-11-09 11:35:37 +0100723 mxcmci_set_power(host, ios->power_mode, ios->vdd);
Sascha Hauerd96be872009-01-06 17:04:14 +0100724 host->power_mode = ios->power_mode;
Alberto Panizzo74b66952010-11-02 01:05:37 +0100725
Sascha Hauerd96be872009-01-06 17:04:14 +0100726 if (ios->power_mode == MMC_POWER_ON)
727 host->cmdat |= CMD_DAT_CONT_INIT;
728 }
729
730 if (ios->clock) {
731 mxcmci_set_clk_rate(host, ios->clock);
732 writew(STR_STP_CLK_START_CLK, host->base + MMC_REG_STR_STP_CLK);
733 } else {
734 writew(STR_STP_CLK_STOP_CLK, host->base + MMC_REG_STR_STP_CLK);
735 }
736
737 host->clock = ios->clock;
738}
739
740static irqreturn_t mxcmci_detect_irq(int irq, void *data)
741{
742 struct mmc_host *mmc = data;
743
744 dev_dbg(mmc_dev(mmc), "%s\n", __func__);
745
746 mmc_detect_change(mmc, msecs_to_jiffies(250));
747 return IRQ_HANDLED;
748}
749
750static int mxcmci_get_ro(struct mmc_host *mmc)
751{
752 struct mxcmci_host *host = mmc_priv(mmc);
753
754 if (host->pdata && host->pdata->get_ro)
755 return !!host->pdata->get_ro(mmc_dev(mmc));
756 /*
757 * Board doesn't support read only detection; let the mmc core
758 * decide what to do.
759 */
760 return -ENOSYS;
761}
762
Daniel Mackf441b992010-04-01 10:03:24 +0200763static void mxcmci_enable_sdio_irq(struct mmc_host *mmc, int enable)
764{
765 struct mxcmci_host *host = mmc_priv(mmc);
766 unsigned long flags;
767 u32 int_cntr;
768
769 spin_lock_irqsave(&host->lock, flags);
770 host->use_sdio = enable;
771 int_cntr = readl(host->base + MMC_REG_INT_CNTR);
772
773 if (enable)
774 int_cntr |= INT_SDIO_IRQ_EN;
775 else
776 int_cntr &= ~INT_SDIO_IRQ_EN;
777
778 writel(int_cntr, host->base + MMC_REG_INT_CNTR);
779 spin_unlock_irqrestore(&host->lock, flags);
780}
Sascha Hauerd96be872009-01-06 17:04:14 +0100781
Daniel Mack3fcb0272010-04-01 10:03:25 +0200782static void mxcmci_init_card(struct mmc_host *host, struct mmc_card *card)
783{
784 /*
785 * MX3 SoCs have a silicon bug which corrupts CRC calculation of
786 * multi-block transfers when connected SDIO peripheral doesn't
787 * drive the BUSY line as required by the specs.
788 * One way to prevent this is to only allow 1-bit transfers.
789 */
790
791 if (cpu_is_mx3() && card->type == MMC_TYPE_SDIO)
792 host->caps &= ~MMC_CAP_4_BIT_DATA;
793 else
794 host->caps |= MMC_CAP_4_BIT_DATA;
795}
796
Sascha Hauerd96be872009-01-06 17:04:14 +0100797static const struct mmc_host_ops mxcmci_ops = {
Daniel Mackf441b992010-04-01 10:03:24 +0200798 .request = mxcmci_request,
799 .set_ios = mxcmci_set_ios,
800 .get_ro = mxcmci_get_ro,
801 .enable_sdio_irq = mxcmci_enable_sdio_irq,
Daniel Mack3fcb0272010-04-01 10:03:25 +0200802 .init_card = mxcmci_init_card,
Sascha Hauerd96be872009-01-06 17:04:14 +0100803};
804
805static int mxcmci_probe(struct platform_device *pdev)
806{
807 struct mmc_host *mmc;
808 struct mxcmci_host *host = NULL;
Uwe Kleine-Königc0521ba2009-12-14 18:01:17 -0800809 struct resource *iores, *r;
Sascha Hauerd96be872009-01-06 17:04:14 +0100810 int ret = 0, irq;
811
812 printk(KERN_INFO "i.MX SDHC driver\n");
813
Uwe Kleine-Königc0521ba2009-12-14 18:01:17 -0800814 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Sascha Hauerd96be872009-01-06 17:04:14 +0100815 irq = platform_get_irq(pdev, 0);
Uwe Kleine-Königc0521ba2009-12-14 18:01:17 -0800816 if (!iores || irq < 0)
Sascha Hauerd96be872009-01-06 17:04:14 +0100817 return -EINVAL;
818
Uwe Kleine-Königc0521ba2009-12-14 18:01:17 -0800819 r = request_mem_region(iores->start, resource_size(iores), pdev->name);
Sascha Hauerd96be872009-01-06 17:04:14 +0100820 if (!r)
821 return -EBUSY;
822
823 mmc = mmc_alloc_host(sizeof(struct mxcmci_host), &pdev->dev);
824 if (!mmc) {
825 ret = -ENOMEM;
826 goto out_release_mem;
827 }
828
829 mmc->ops = &mxcmci_ops;
Daniel Mackf441b992010-04-01 10:03:24 +0200830 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
Sascha Hauerd96be872009-01-06 17:04:14 +0100831
832 /* MMC core transfer sizes tunable parameters */
Martin K. Petersena36274e2010-09-10 01:33:59 -0400833 mmc->max_segs = 64;
Sascha Hauerd96be872009-01-06 17:04:14 +0100834 mmc->max_blk_size = 2048;
835 mmc->max_blk_count = 65535;
836 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
Vladimir Zapolskiyd759c372010-03-10 15:23:57 -0800837 mmc->max_seg_size = mmc->max_req_size;
Sascha Hauerd96be872009-01-06 17:04:14 +0100838
839 host = mmc_priv(mmc);
840 host->base = ioremap(r->start, resource_size(r));
841 if (!host->base) {
842 ret = -ENOMEM;
843 goto out_free;
844 }
845
846 host->mmc = mmc;
847 host->pdata = pdev->dev.platform_data;
Daniel Mackf441b992010-04-01 10:03:24 +0200848 spin_lock_init(&host->lock);
Sascha Hauerd96be872009-01-06 17:04:14 +0100849
Alberto Panizzo74b66952010-11-02 01:05:37 +0100850 mxcmci_init_ocr(host);
Sascha Hauerd96be872009-01-06 17:04:14 +0100851
Eric Bénard16b3bf82010-05-19 18:46:04 +0200852 if (host->pdata && host->pdata->dat3_card_detect)
853 host->default_irq_mask =
854 INT_CARD_INSERTION_EN | INT_CARD_REMOVAL_EN;
855 else
856 host->default_irq_mask = 0;
857
Sascha Hauerd96be872009-01-06 17:04:14 +0100858 host->res = r;
859 host->irq = irq;
860
Sascha Hauer06277b52009-02-23 13:33:31 +0100861 host->clk = clk_get(&pdev->dev, NULL);
Sascha Hauerd96be872009-01-06 17:04:14 +0100862 if (IS_ERR(host->clk)) {
863 ret = PTR_ERR(host->clk);
864 goto out_iounmap;
865 }
866 clk_enable(host->clk);
867
868 mxcmci_softreset(host);
869
870 host->rev_no = readw(host->base + MMC_REG_REV_NO);
871 if (host->rev_no != 0x400) {
872 ret = -ENODEV;
873 dev_err(mmc_dev(host->mmc), "wrong rev.no. 0x%08x. aborting.\n",
874 host->rev_no);
875 goto out_clk_put;
876 }
877
Sascha Hauerc499b062009-04-03 14:41:56 +0200878 mmc->f_min = clk_get_rate(host->clk) >> 16;
Sascha Hauerd96be872009-01-06 17:04:14 +0100879 mmc->f_max = clk_get_rate(host->clk) >> 1;
880
881 /* recommended in data sheet */
882 writew(0x2db4, host->base + MMC_REG_READ_TO);
883
Eric Bénard16b3bf82010-05-19 18:46:04 +0200884 writel(host->default_irq_mask, host->base + MMC_REG_INT_CNTR);
Sascha Hauerd96be872009-01-06 17:04:14 +0100885
886#ifdef HAS_DMA
887 host->dma = imx_dma_request_by_prio(DRIVER_NAME, DMA_PRIO_LOW);
888 if (host->dma < 0) {
889 dev_err(mmc_dev(host->mmc), "imx_dma_request_by_prio failed\n");
890 ret = -EBUSY;
891 goto out_clk_put;
892 }
893
894 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
895 if (!r) {
896 ret = -EINVAL;
897 goto out_free_dma;
898 }
899
900 ret = imx_dma_config_channel(host->dma,
901 IMX_DMA_MEMSIZE_32 | IMX_DMA_TYPE_FIFO,
902 IMX_DMA_MEMSIZE_32 | IMX_DMA_TYPE_LINEAR,
903 r->start, 0);
904 if (ret) {
905 dev_err(mmc_dev(host->mmc), "failed to config DMA channel\n");
906 goto out_free_dma;
907 }
908#endif
909 INIT_WORK(&host->datawork, mxcmci_datawork);
910
911 ret = request_irq(host->irq, mxcmci_irq, 0, DRIVER_NAME, host);
912 if (ret)
913 goto out_free_dma;
914
915 platform_set_drvdata(pdev, mmc);
916
917 if (host->pdata && host->pdata->init) {
918 ret = host->pdata->init(&pdev->dev, mxcmci_detect_irq,
919 host->mmc);
920 if (ret)
921 goto out_free_irq;
922 }
923
924 mmc_add_host(mmc);
925
926 return 0;
927
928out_free_irq:
929 free_irq(host->irq, host);
930out_free_dma:
931#ifdef HAS_DMA
932 imx_dma_free(host->dma);
933#endif
934out_clk_put:
935 clk_disable(host->clk);
936 clk_put(host->clk);
937out_iounmap:
938 iounmap(host->base);
939out_free:
940 mmc_free_host(mmc);
941out_release_mem:
Uwe Kleine-Königc0521ba2009-12-14 18:01:17 -0800942 release_mem_region(iores->start, resource_size(iores));
Sascha Hauerd96be872009-01-06 17:04:14 +0100943 return ret;
944}
945
946static int mxcmci_remove(struct platform_device *pdev)
947{
948 struct mmc_host *mmc = platform_get_drvdata(pdev);
949 struct mxcmci_host *host = mmc_priv(mmc);
950
951 platform_set_drvdata(pdev, NULL);
952
953 mmc_remove_host(mmc);
954
Alberto Panizzo74b66952010-11-02 01:05:37 +0100955 if (host->vcc)
956 regulator_put(host->vcc);
957
Sascha Hauerd96be872009-01-06 17:04:14 +0100958 if (host->pdata && host->pdata->exit)
959 host->pdata->exit(&pdev->dev, mmc);
960
961 free_irq(host->irq, host);
962 iounmap(host->base);
963#ifdef HAS_DMA
964 imx_dma_free(host->dma);
965#endif
966 clk_disable(host->clk);
967 clk_put(host->clk);
968
969 release_mem_region(host->res->start, resource_size(host->res));
970 release_resource(host->res);
971
972 mmc_free_host(mmc);
973
974 return 0;
975}
976
977#ifdef CONFIG_PM
Eric Bénarda7d403c2010-06-17 20:59:07 +0200978static int mxcmci_suspend(struct device *dev)
Sascha Hauerd96be872009-01-06 17:04:14 +0100979{
Eric Bénarda7d403c2010-06-17 20:59:07 +0200980 struct mmc_host *mmc = dev_get_drvdata(dev);
981 struct mxcmci_host *host = mmc_priv(mmc);
Sascha Hauerd96be872009-01-06 17:04:14 +0100982 int ret = 0;
983
984 if (mmc)
Matt Fleming1a13f8f2010-05-26 14:42:08 -0700985 ret = mmc_suspend_host(mmc);
Eric Bénarda7d403c2010-06-17 20:59:07 +0200986 clk_disable(host->clk);
Sascha Hauerd96be872009-01-06 17:04:14 +0100987
988 return ret;
989}
990
Eric Bénarda7d403c2010-06-17 20:59:07 +0200991static int mxcmci_resume(struct device *dev)
Sascha Hauerd96be872009-01-06 17:04:14 +0100992{
Eric Bénarda7d403c2010-06-17 20:59:07 +0200993 struct mmc_host *mmc = dev_get_drvdata(dev);
994 struct mxcmci_host *host = mmc_priv(mmc);
Sascha Hauerd96be872009-01-06 17:04:14 +0100995 int ret = 0;
996
Eric Bénarda7d403c2010-06-17 20:59:07 +0200997 clk_enable(host->clk);
998 if (mmc)
Sascha Hauerd96be872009-01-06 17:04:14 +0100999 ret = mmc_resume_host(mmc);
Sascha Hauerd96be872009-01-06 17:04:14 +01001000
1001 return ret;
1002}
Eric Bénarda7d403c2010-06-17 20:59:07 +02001003
1004static const struct dev_pm_ops mxcmci_pm_ops = {
1005 .suspend = mxcmci_suspend,
1006 .resume = mxcmci_resume,
1007};
1008#endif
Sascha Hauerd96be872009-01-06 17:04:14 +01001009
1010static struct platform_driver mxcmci_driver = {
1011 .probe = mxcmci_probe,
1012 .remove = mxcmci_remove,
Sascha Hauerd96be872009-01-06 17:04:14 +01001013 .driver = {
1014 .name = DRIVER_NAME,
1015 .owner = THIS_MODULE,
Eric Bénarda7d403c2010-06-17 20:59:07 +02001016#ifdef CONFIG_PM
1017 .pm = &mxcmci_pm_ops,
1018#endif
Sascha Hauerd96be872009-01-06 17:04:14 +01001019 }
1020};
1021
1022static int __init mxcmci_init(void)
1023{
1024 return platform_driver_register(&mxcmci_driver);
1025}
1026
1027static void __exit mxcmci_exit(void)
1028{
1029 platform_driver_unregister(&mxcmci_driver);
1030}
1031
1032module_init(mxcmci_init);
1033module_exit(mxcmci_exit);
1034
1035MODULE_DESCRIPTION("i.MX Multimedia Card Interface Driver");
1036MODULE_AUTHOR("Sascha Hauer, Pengutronix");
1037MODULE_LICENSE("GPL");
1038MODULE_ALIAS("platform:imx-mmc");