blob: f35b6bad04767187f971d3de383b53b72f946ecf [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 Mack3ad2f3fb2010-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 Hauerf53fbde2011-02-18 10:21:09 +010035#include <linux/dmaengine.h>
Sascha Hauerd96be872009-01-06 17:04:14 +010036
37#include <asm/dma.h>
38#include <asm/irq.h>
39#include <asm/sizes.h>
40#include <mach/mmc.h>
41
Sascha Hauerf53fbde2011-02-18 10:21:09 +010042#include <mach/dma.h>
Shawn Guoedae8992011-10-17 14:15:10 +080043#include <mach/hardware.h>
Sascha Hauerd96be872009-01-06 17:04:14 +010044
Sascha Hauer9563b1d2009-02-23 13:08:06 +010045#define DRIVER_NAME "mxc-mmc"
Sascha Hauerd96be872009-01-06 17:04:14 +010046
47#define MMC_REG_STR_STP_CLK 0x00
48#define MMC_REG_STATUS 0x04
49#define MMC_REG_CLK_RATE 0x08
50#define MMC_REG_CMD_DAT_CONT 0x0C
51#define MMC_REG_RES_TO 0x10
52#define MMC_REG_READ_TO 0x14
53#define MMC_REG_BLK_LEN 0x18
54#define MMC_REG_NOB 0x1C
55#define MMC_REG_REV_NO 0x20
56#define MMC_REG_INT_CNTR 0x24
57#define MMC_REG_CMD 0x28
58#define MMC_REG_ARG 0x2C
59#define MMC_REG_RES_FIFO 0x34
60#define MMC_REG_BUFFER_ACCESS 0x38
61
62#define STR_STP_CLK_RESET (1 << 3)
63#define STR_STP_CLK_START_CLK (1 << 1)
64#define STR_STP_CLK_STOP_CLK (1 << 0)
65
66#define STATUS_CARD_INSERTION (1 << 31)
67#define STATUS_CARD_REMOVAL (1 << 30)
68#define STATUS_YBUF_EMPTY (1 << 29)
69#define STATUS_XBUF_EMPTY (1 << 28)
70#define STATUS_YBUF_FULL (1 << 27)
71#define STATUS_XBUF_FULL (1 << 26)
72#define STATUS_BUF_UND_RUN (1 << 25)
73#define STATUS_BUF_OVFL (1 << 24)
74#define STATUS_SDIO_INT_ACTIVE (1 << 14)
75#define STATUS_END_CMD_RESP (1 << 13)
76#define STATUS_WRITE_OP_DONE (1 << 12)
77#define STATUS_DATA_TRANS_DONE (1 << 11)
78#define STATUS_READ_OP_DONE (1 << 11)
79#define STATUS_WR_CRC_ERROR_CODE_MASK (3 << 10)
80#define STATUS_CARD_BUS_CLK_RUN (1 << 8)
81#define STATUS_BUF_READ_RDY (1 << 7)
82#define STATUS_BUF_WRITE_RDY (1 << 6)
83#define STATUS_RESP_CRC_ERR (1 << 5)
84#define STATUS_CRC_READ_ERR (1 << 3)
85#define STATUS_CRC_WRITE_ERR (1 << 2)
86#define STATUS_TIME_OUT_RESP (1 << 1)
87#define STATUS_TIME_OUT_READ (1 << 0)
88#define STATUS_ERR_MASK 0x2f
89
90#define CMD_DAT_CONT_CMD_RESP_LONG_OFF (1 << 12)
91#define CMD_DAT_CONT_STOP_READWAIT (1 << 11)
92#define CMD_DAT_CONT_START_READWAIT (1 << 10)
93#define CMD_DAT_CONT_BUS_WIDTH_4 (2 << 8)
94#define CMD_DAT_CONT_INIT (1 << 7)
95#define CMD_DAT_CONT_WRITE (1 << 4)
96#define CMD_DAT_CONT_DATA_ENABLE (1 << 3)
97#define CMD_DAT_CONT_RESPONSE_48BIT_CRC (1 << 0)
98#define CMD_DAT_CONT_RESPONSE_136BIT (2 << 0)
99#define CMD_DAT_CONT_RESPONSE_48BIT (3 << 0)
100
101#define INT_SDIO_INT_WKP_EN (1 << 18)
102#define INT_CARD_INSERTION_WKP_EN (1 << 17)
103#define INT_CARD_REMOVAL_WKP_EN (1 << 16)
104#define INT_CARD_INSERTION_EN (1 << 15)
105#define INT_CARD_REMOVAL_EN (1 << 14)
106#define INT_SDIO_IRQ_EN (1 << 13)
107#define INT_DAT0_EN (1 << 12)
108#define INT_BUF_READ_EN (1 << 4)
109#define INT_BUF_WRITE_EN (1 << 3)
110#define INT_END_CMD_RES_EN (1 << 2)
111#define INT_WRITE_OP_DONE_EN (1 << 1)
112#define INT_READ_OP_EN (1 << 0)
113
114struct mxcmci_host {
115 struct mmc_host *mmc;
116 struct resource *res;
117 void __iomem *base;
118 int irq;
119 int detect_irq;
Sascha Hauerf53fbde2011-02-18 10:21:09 +0100120 struct dma_chan *dma;
121 struct dma_async_tx_descriptor *desc;
Sascha Hauerd96be872009-01-06 17:04:14 +0100122 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
Sascha Hauerd96be872009-01-06 17:04:14 +0100132 unsigned int datasize;
133 unsigned int dma_dir;
134
135 u16 rev_no;
136 unsigned int cmdat;
137
138 struct clk *clk;
139
140 int clock;
141
142 struct work_struct datawork;
Daniel Mackf441b992010-04-01 10:03:24 +0200143 spinlock_t lock;
Alberto Panizzo74b66952010-11-02 01:05:37 +0100144
145 struct regulator *vcc;
Sascha Hauerf53fbde2011-02-18 10:21:09 +0100146
147 int burstlen;
148 int dmareq;
149 struct dma_slave_config dma_slave_config;
150 struct imx_dma_data dma_data;
Sascha Hauerd96be872009-01-06 17:04:14 +0100151};
152
Martin Fuzzey18489fa2009-04-16 22:00:36 +0200153static void mxcmci_set_clk_rate(struct mxcmci_host *host, unsigned int clk_ios);
154
Alberto Panizzo74b66952010-11-02 01:05:37 +0100155static inline void mxcmci_init_ocr(struct mxcmci_host *host)
156{
Alberto Panizzo74b66952010-11-02 01:05:37 +0100157 host->vcc = regulator_get(mmc_dev(host->mmc), "vmmc");
158
159 if (IS_ERR(host->vcc)) {
160 host->vcc = NULL;
161 } else {
162 host->mmc->ocr_avail = mmc_regulator_get_ocrmask(host->vcc);
163 if (host->pdata && host->pdata->ocr_avail)
164 dev_warn(mmc_dev(host->mmc),
165 "pdata->ocr_avail will not be used\n");
166 }
Alberto Panizzod078d242010-11-09 11:35:37 +0100167
Alberto Panizzo74b66952010-11-02 01:05:37 +0100168 if (host->vcc == NULL) {
169 /* fall-back to platform data */
170 if (host->pdata && host->pdata->ocr_avail)
171 host->mmc->ocr_avail = host->pdata->ocr_avail;
172 else
173 host->mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
174 }
175}
176
Alberto Panizzod078d242010-11-09 11:35:37 +0100177static inline void mxcmci_set_power(struct mxcmci_host *host,
178 unsigned char power_mode,
179 unsigned int vdd)
Alberto Panizzo74b66952010-11-02 01:05:37 +0100180{
Alberto Panizzod078d242010-11-09 11:35:37 +0100181 if (host->vcc) {
182 if (power_mode == MMC_POWER_UP)
183 mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
184 else if (power_mode == MMC_POWER_OFF)
185 mmc_regulator_set_ocr(host->mmc, host->vcc, 0);
186 }
187
Alberto Panizzo74b66952010-11-02 01:05:37 +0100188 if (host->pdata && host->pdata->setpower)
189 host->pdata->setpower(mmc_dev(host->mmc), vdd);
190}
191
Sascha Hauerd96be872009-01-06 17:04:14 +0100192static inline int mxcmci_use_dma(struct mxcmci_host *host)
193{
194 return host->do_dma;
195}
196
197static void mxcmci_softreset(struct mxcmci_host *host)
198{
199 int i;
200
Daniel Mack4725f6f2010-04-01 10:03:23 +0200201 dev_dbg(mmc_dev(host->mmc), "mxcmci_softreset\n");
202
Sascha Hauerd96be872009-01-06 17:04:14 +0100203 /* reset sequence */
204 writew(STR_STP_CLK_RESET, host->base + MMC_REG_STR_STP_CLK);
205 writew(STR_STP_CLK_RESET | STR_STP_CLK_START_CLK,
206 host->base + MMC_REG_STR_STP_CLK);
207
208 for (i = 0; i < 8; i++)
209 writew(STR_STP_CLK_START_CLK, host->base + MMC_REG_STR_STP_CLK);
210
211 writew(0xff, host->base + MMC_REG_RES_TO);
212}
Sascha Hauerf53fbde2011-02-18 10:21:09 +0100213static int mxcmci_setup_dma(struct mmc_host *mmc);
Sascha Hauerd96be872009-01-06 17:04:14 +0100214
Martin Fuzzey656217d2009-04-16 22:00:41 +0200215static int mxcmci_setup_data(struct mxcmci_host *host, struct mmc_data *data)
Sascha Hauerd96be872009-01-06 17:04:14 +0100216{
217 unsigned int nob = data->blocks;
218 unsigned int blksz = data->blksz;
219 unsigned int datasize = nob * blksz;
Sascha Hauerd96be872009-01-06 17:04:14 +0100220 struct scatterlist *sg;
Vinod Koul05f57992011-10-14 10:45:11 +0530221 enum dma_transfer_direction slave_dirn;
Sascha Hauerf53fbde2011-02-18 10:21:09 +0100222 int i, nents;
223
Sascha Hauerd96be872009-01-06 17:04:14 +0100224 if (data->flags & MMC_DATA_STREAM)
225 nob = 0xffff;
226
227 host->data = data;
228 data->bytes_xfered = 0;
229
230 writew(nob, host->base + MMC_REG_NOB);
231 writew(blksz, host->base + MMC_REG_BLK_LEN);
232 host->datasize = datasize;
233
Sascha Hauerf53fbde2011-02-18 10:21:09 +0100234 if (!mxcmci_use_dma(host))
235 return 0;
236
Sascha Hauerd96be872009-01-06 17:04:14 +0100237 for_each_sg(data->sg, sg, data->sg_len, i) {
238 if (sg->offset & 3 || sg->length & 3) {
239 host->do_dma = 0;
Martin Fuzzey656217d2009-04-16 22:00:41 +0200240 return 0;
Sascha Hauerd96be872009-01-06 17:04:14 +0100241 }
242 }
243
Vinod Koul05f57992011-10-14 10:45:11 +0530244 if (data->flags & MMC_DATA_READ) {
Sascha Hauerd96be872009-01-06 17:04:14 +0100245 host->dma_dir = DMA_FROM_DEVICE;
Vinod Koul05f57992011-10-14 10:45:11 +0530246 slave_dirn = DMA_DEV_TO_MEM;
247 } else {
Sascha Hauerd96be872009-01-06 17:04:14 +0100248 host->dma_dir = DMA_TO_DEVICE;
Vinod Koul05f57992011-10-14 10:45:11 +0530249 slave_dirn = DMA_MEM_TO_DEV;
250 }
Sascha Hauerd96be872009-01-06 17:04:14 +0100251
Sascha Hauerf53fbde2011-02-18 10:21:09 +0100252 nents = dma_map_sg(host->dma->device->dev, data->sg,
253 data->sg_len, host->dma_dir);
254 if (nents != data->sg_len)
255 return -EINVAL;
Sascha Hauerd96be872009-01-06 17:04:14 +0100256
Sascha Hauerf53fbde2011-02-18 10:21:09 +0100257 host->desc = host->dma->device->device_prep_slave_sg(host->dma,
Vinod Koul05f57992011-10-14 10:45:11 +0530258 data->sg, data->sg_len, slave_dirn,
Sascha Hauerf53fbde2011-02-18 10:21:09 +0100259 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
260
261 if (!host->desc) {
262 dma_unmap_sg(host->dma->device->dev, data->sg, data->sg_len,
263 host->dma_dir);
264 host->do_dma = 0;
265 return 0; /* Fall back to PIO */
Martin Fuzzey656217d2009-04-16 22:00:41 +0200266 }
Sascha Hauerd96be872009-01-06 17:04:14 +0100267 wmb();
268
Sascha Hauerf53fbde2011-02-18 10:21:09 +0100269 dmaengine_submit(host->desc);
Sascha Hauer439aa0e2012-01-09 10:32:47 +0100270 dma_async_issue_pending(host->dma);
Sascha Hauerf53fbde2011-02-18 10:21:09 +0100271
Martin Fuzzey656217d2009-04-16 22:00:41 +0200272 return 0;
Sascha Hauerd96be872009-01-06 17:04:14 +0100273}
274
275static int mxcmci_start_cmd(struct mxcmci_host *host, struct mmc_command *cmd,
276 unsigned int cmdat)
277{
Eric Bénard16b3bf82010-05-19 18:46:04 +0200278 u32 int_cntr = host->default_irq_mask;
Daniel Mackf441b992010-04-01 10:03:24 +0200279 unsigned long flags;
280
Sascha Hauerd96be872009-01-06 17:04:14 +0100281 WARN_ON(host->cmd != NULL);
282 host->cmd = cmd;
283
284 switch (mmc_resp_type(cmd)) {
285 case MMC_RSP_R1: /* short CRC, OPCODE */
286 case MMC_RSP_R1B:/* short CRC, OPCODE, BUSY */
287 cmdat |= CMD_DAT_CONT_RESPONSE_48BIT_CRC;
288 break;
289 case MMC_RSP_R2: /* long 136 bit + CRC */
290 cmdat |= CMD_DAT_CONT_RESPONSE_136BIT;
291 break;
292 case MMC_RSP_R3: /* short */
293 cmdat |= CMD_DAT_CONT_RESPONSE_48BIT;
294 break;
295 case MMC_RSP_NONE:
296 break;
297 default:
298 dev_err(mmc_dev(host->mmc), "unhandled response type 0x%x\n",
299 mmc_resp_type(cmd));
300 cmd->error = -EINVAL;
301 return -EINVAL;
302 }
303
Daniel Mackf441b992010-04-01 10:03:24 +0200304 int_cntr = INT_END_CMD_RES_EN;
305
Sascha Hauerd96be872009-01-06 17:04:14 +0100306 if (mxcmci_use_dma(host))
Daniel Mackf441b992010-04-01 10:03:24 +0200307 int_cntr |= INT_READ_OP_EN | INT_WRITE_OP_DONE_EN;
308
309 spin_lock_irqsave(&host->lock, flags);
310 if (host->use_sdio)
311 int_cntr |= INT_SDIO_IRQ_EN;
312 writel(int_cntr, host->base + MMC_REG_INT_CNTR);
313 spin_unlock_irqrestore(&host->lock, flags);
Sascha Hauerd96be872009-01-06 17:04:14 +0100314
315 writew(cmd->opcode, host->base + MMC_REG_CMD);
316 writel(cmd->arg, host->base + MMC_REG_ARG);
317 writew(cmdat, host->base + MMC_REG_CMD_DAT_CONT);
318
319 return 0;
320}
321
322static void mxcmci_finish_request(struct mxcmci_host *host,
323 struct mmc_request *req)
324{
Eric Bénard16b3bf82010-05-19 18:46:04 +0200325 u32 int_cntr = host->default_irq_mask;
Daniel Mackf441b992010-04-01 10:03:24 +0200326 unsigned long flags;
327
328 spin_lock_irqsave(&host->lock, flags);
329 if (host->use_sdio)
330 int_cntr |= INT_SDIO_IRQ_EN;
331 writel(int_cntr, host->base + MMC_REG_INT_CNTR);
332 spin_unlock_irqrestore(&host->lock, flags);
Sascha Hauerd96be872009-01-06 17:04:14 +0100333
334 host->req = NULL;
335 host->cmd = NULL;
336 host->data = NULL;
337
338 mmc_request_done(host->mmc, req);
339}
340
341static int mxcmci_finish_data(struct mxcmci_host *host, unsigned int stat)
342{
343 struct mmc_data *data = host->data;
344 int data_error;
345
Sascha Hauerd96be872009-01-06 17:04:14 +0100346 if (mxcmci_use_dma(host)) {
Sascha Hauerf53fbde2011-02-18 10:21:09 +0100347 dmaengine_terminate_all(host->dma);
348 dma_unmap_sg(host->dma->device->dev, data->sg, data->sg_len,
Sascha Hauerd96be872009-01-06 17:04:14 +0100349 host->dma_dir);
350 }
Sascha Hauerd96be872009-01-06 17:04:14 +0100351
352 if (stat & STATUS_ERR_MASK) {
353 dev_dbg(mmc_dev(host->mmc), "request failed. status: 0x%08x\n",
354 stat);
355 if (stat & STATUS_CRC_READ_ERR) {
Daniel Mack4725f6f2010-04-01 10:03:23 +0200356 dev_err(mmc_dev(host->mmc), "%s: -EILSEQ\n", __func__);
Sascha Hauerd96be872009-01-06 17:04:14 +0100357 data->error = -EILSEQ;
358 } else if (stat & STATUS_CRC_WRITE_ERR) {
359 u32 err_code = (stat >> 9) & 0x3;
Daniel Mack4725f6f2010-04-01 10:03:23 +0200360 if (err_code == 2) { /* No CRC response */
361 dev_err(mmc_dev(host->mmc),
362 "%s: No CRC -ETIMEDOUT\n", __func__);
Sascha Hauerd96be872009-01-06 17:04:14 +0100363 data->error = -ETIMEDOUT;
Daniel Mack4725f6f2010-04-01 10:03:23 +0200364 } else {
365 dev_err(mmc_dev(host->mmc),
366 "%s: -EILSEQ\n", __func__);
Sascha Hauerd96be872009-01-06 17:04:14 +0100367 data->error = -EILSEQ;
Daniel Mack4725f6f2010-04-01 10:03:23 +0200368 }
Sascha Hauerd96be872009-01-06 17:04:14 +0100369 } else if (stat & STATUS_TIME_OUT_READ) {
Daniel Mack4725f6f2010-04-01 10:03:23 +0200370 dev_err(mmc_dev(host->mmc),
371 "%s: read -ETIMEDOUT\n", __func__);
Sascha Hauerd96be872009-01-06 17:04:14 +0100372 data->error = -ETIMEDOUT;
373 } else {
Daniel Mack4725f6f2010-04-01 10:03:23 +0200374 dev_err(mmc_dev(host->mmc), "%s: -EIO\n", __func__);
Sascha Hauerd96be872009-01-06 17:04:14 +0100375 data->error = -EIO;
376 }
377 } else {
378 data->bytes_xfered = host->datasize;
379 }
380
381 data_error = data->error;
382
383 host->data = NULL;
384
385 return data_error;
386}
387
388static void mxcmci_read_response(struct mxcmci_host *host, unsigned int stat)
389{
390 struct mmc_command *cmd = host->cmd;
391 int i;
392 u32 a, b, c;
393
394 if (!cmd)
395 return;
396
397 if (stat & STATUS_TIME_OUT_RESP) {
398 dev_dbg(mmc_dev(host->mmc), "CMD TIMEOUT\n");
399 cmd->error = -ETIMEDOUT;
400 } else if (stat & STATUS_RESP_CRC_ERR && cmd->flags & MMC_RSP_CRC) {
401 dev_dbg(mmc_dev(host->mmc), "cmd crc error\n");
402 cmd->error = -EILSEQ;
403 }
404
405 if (cmd->flags & MMC_RSP_PRESENT) {
406 if (cmd->flags & MMC_RSP_136) {
407 for (i = 0; i < 4; i++) {
408 a = readw(host->base + MMC_REG_RES_FIFO);
409 b = readw(host->base + MMC_REG_RES_FIFO);
410 cmd->resp[i] = a << 16 | b;
411 }
412 } else {
413 a = readw(host->base + MMC_REG_RES_FIFO);
414 b = readw(host->base + MMC_REG_RES_FIFO);
415 c = readw(host->base + MMC_REG_RES_FIFO);
416 cmd->resp[0] = a << 24 | b << 8 | c >> 8;
417 }
418 }
419}
420
421static int mxcmci_poll_status(struct mxcmci_host *host, u32 mask)
422{
423 u32 stat;
424 unsigned long timeout = jiffies + HZ;
425
426 do {
427 stat = readl(host->base + MMC_REG_STATUS);
428 if (stat & STATUS_ERR_MASK)
429 return stat;
Martin Fuzzey18489fa2009-04-16 22:00:36 +0200430 if (time_after(jiffies, timeout)) {
431 mxcmci_softreset(host);
432 mxcmci_set_clk_rate(host, host->clock);
Sascha Hauerd96be872009-01-06 17:04:14 +0100433 return STATUS_TIME_OUT_READ;
Martin Fuzzey18489fa2009-04-16 22:00:36 +0200434 }
Sascha Hauerd96be872009-01-06 17:04:14 +0100435 if (stat & mask)
436 return 0;
437 cpu_relax();
438 } while (1);
439}
440
441static int mxcmci_pull(struct mxcmci_host *host, void *_buf, int bytes)
442{
443 unsigned int stat;
444 u32 *buf = _buf;
445
446 while (bytes > 3) {
447 stat = mxcmci_poll_status(host,
448 STATUS_BUF_READ_RDY | STATUS_READ_OP_DONE);
449 if (stat)
450 return stat;
451 *buf++ = readl(host->base + MMC_REG_BUFFER_ACCESS);
452 bytes -= 4;
453 }
454
455 if (bytes) {
456 u8 *b = (u8 *)buf;
457 u32 tmp;
458
459 stat = mxcmci_poll_status(host,
460 STATUS_BUF_READ_RDY | STATUS_READ_OP_DONE);
461 if (stat)
462 return stat;
463 tmp = readl(host->base + MMC_REG_BUFFER_ACCESS);
464 memcpy(b, &tmp, bytes);
465 }
466
467 return 0;
468}
469
470static int mxcmci_push(struct mxcmci_host *host, void *_buf, int bytes)
471{
472 unsigned int stat;
473 u32 *buf = _buf;
474
475 while (bytes > 3) {
476 stat = mxcmci_poll_status(host, STATUS_BUF_WRITE_RDY);
477 if (stat)
478 return stat;
479 writel(*buf++, host->base + MMC_REG_BUFFER_ACCESS);
480 bytes -= 4;
481 }
482
483 if (bytes) {
484 u8 *b = (u8 *)buf;
485 u32 tmp;
486
487 stat = mxcmci_poll_status(host, STATUS_BUF_WRITE_RDY);
488 if (stat)
489 return stat;
490
491 memcpy(&tmp, b, bytes);
492 writel(tmp, host->base + MMC_REG_BUFFER_ACCESS);
493 }
494
495 stat = mxcmci_poll_status(host, STATUS_BUF_WRITE_RDY);
496 if (stat)
497 return stat;
498
499 return 0;
500}
501
502static int mxcmci_transfer_data(struct mxcmci_host *host)
503{
504 struct mmc_data *data = host->req->data;
505 struct scatterlist *sg;
506 int stat, i;
507
Sascha Hauerd96be872009-01-06 17:04:14 +0100508 host->data = data;
509 host->datasize = 0;
510
511 if (data->flags & MMC_DATA_READ) {
512 for_each_sg(data->sg, sg, data->sg_len, i) {
513 stat = mxcmci_pull(host, sg_virt(sg), sg->length);
514 if (stat)
515 return stat;
516 host->datasize += sg->length;
517 }
518 } else {
519 for_each_sg(data->sg, sg, data->sg_len, i) {
520 stat = mxcmci_push(host, sg_virt(sg), sg->length);
521 if (stat)
522 return stat;
523 host->datasize += sg->length;
524 }
525 stat = mxcmci_poll_status(host, STATUS_WRITE_OP_DONE);
526 if (stat)
527 return stat;
528 }
529 return 0;
530}
531
532static void mxcmci_datawork(struct work_struct *work)
533{
534 struct mxcmci_host *host = container_of(work, struct mxcmci_host,
535 datawork);
536 int datastat = mxcmci_transfer_data(host);
Daniel Mack4a31f2e2010-04-27 12:24:42 +0200537
538 writel(STATUS_READ_OP_DONE | STATUS_WRITE_OP_DONE,
539 host->base + MMC_REG_STATUS);
Sascha Hauerd96be872009-01-06 17:04:14 +0100540 mxcmci_finish_data(host, datastat);
541
542 if (host->req->stop) {
543 if (mxcmci_start_cmd(host, host->req->stop, 0)) {
544 mxcmci_finish_request(host, host->req);
545 return;
546 }
547 } else {
548 mxcmci_finish_request(host, host->req);
549 }
550}
551
Sascha Hauerd96be872009-01-06 17:04:14 +0100552static void mxcmci_data_done(struct mxcmci_host *host, unsigned int stat)
553{
554 struct mmc_data *data = host->data;
555 int data_error;
556
557 if (!data)
558 return;
559
560 data_error = mxcmci_finish_data(host, stat);
561
562 mxcmci_read_response(host, stat);
563 host->cmd = NULL;
564
565 if (host->req->stop) {
566 if (mxcmci_start_cmd(host, host->req->stop, 0)) {
567 mxcmci_finish_request(host, host->req);
568 return;
569 }
570 } else {
571 mxcmci_finish_request(host, host->req);
572 }
573}
Sascha Hauerd96be872009-01-06 17:04:14 +0100574
575static void mxcmci_cmd_done(struct mxcmci_host *host, unsigned int stat)
576{
577 mxcmci_read_response(host, stat);
578 host->cmd = NULL;
579
580 if (!host->data && host->req) {
581 mxcmci_finish_request(host, host->req);
582 return;
583 }
584
585 /* For the DMA case the DMA engine handles the data transfer
Anand Gadiyarfd589a82009-07-16 17:13:03 +0200586 * automatically. For non DMA we have to do it ourselves.
Sascha Hauerd96be872009-01-06 17:04:14 +0100587 * Don't do it in interrupt context though.
588 */
589 if (!mxcmci_use_dma(host) && host->data)
590 schedule_work(&host->datawork);
591
592}
593
594static irqreturn_t mxcmci_irq(int irq, void *devid)
595{
596 struct mxcmci_host *host = devid;
Daniel Mackf441b992010-04-01 10:03:24 +0200597 unsigned long flags;
598 bool sdio_irq;
Sascha Hauerd96be872009-01-06 17:04:14 +0100599 u32 stat;
600
601 stat = readl(host->base + MMC_REG_STATUS);
Daniel Mack4a31f2e2010-04-27 12:24:42 +0200602 writel(stat & ~(STATUS_SDIO_INT_ACTIVE | STATUS_DATA_TRANS_DONE |
603 STATUS_WRITE_OP_DONE), host->base + MMC_REG_STATUS);
Sascha Hauerd96be872009-01-06 17:04:14 +0100604
605 dev_dbg(mmc_dev(host->mmc), "%s: 0x%08x\n", __func__, stat);
606
Daniel Mackf441b992010-04-01 10:03:24 +0200607 spin_lock_irqsave(&host->lock, flags);
608 sdio_irq = (stat & STATUS_SDIO_INT_ACTIVE) && host->use_sdio;
609 spin_unlock_irqrestore(&host->lock, flags);
610
Daniel Mack4a31f2e2010-04-27 12:24:42 +0200611 if (mxcmci_use_dma(host) &&
612 (stat & (STATUS_READ_OP_DONE | STATUS_WRITE_OP_DONE)))
613 writel(STATUS_READ_OP_DONE | STATUS_WRITE_OP_DONE,
614 host->base + MMC_REG_STATUS);
Daniel Mack4a31f2e2010-04-27 12:24:42 +0200615
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 if (mxcmci_use_dma(host) &&
625 (stat & (STATUS_DATA_TRANS_DONE | STATUS_WRITE_OP_DONE)))
626 mxcmci_data_done(host, stat);
Sascha Hauerf53fbde2011-02-18 10:21:09 +0100627
Eric Bénard16b3bf82010-05-19 18:46:04 +0200628 if (host->default_irq_mask &&
629 (stat & (STATUS_CARD_INSERTION | STATUS_CARD_REMOVAL)))
630 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Sascha Hauerf53fbde2011-02-18 10:21:09 +0100631
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;
Sascha Hauerf53fbde2011-02-18 10:21:09 +0100645
646 if (host->dma)
647 host->do_dma = 1;
648
Sascha Hauerd96be872009-01-06 17:04:14 +0100649 if (req->data) {
Martin Fuzzey656217d2009-04-16 22:00:41 +0200650 error = mxcmci_setup_data(host, req->data);
651 if (error) {
652 req->cmd->error = error;
653 goto out;
654 }
655
Sascha Hauerd96be872009-01-06 17:04:14 +0100656
657 cmdat |= CMD_DAT_CONT_DATA_ENABLE;
658
659 if (req->data->flags & MMC_DATA_WRITE)
660 cmdat |= CMD_DAT_CONT_WRITE;
661 }
662
Martin Fuzzey656217d2009-04-16 22:00:41 +0200663 error = mxcmci_start_cmd(host, req->cmd, cmdat);
Sascha Hauerf53fbde2011-02-18 10:21:09 +0100664
Martin Fuzzey656217d2009-04-16 22:00:41 +0200665out:
666 if (error)
Sascha Hauerd96be872009-01-06 17:04:14 +0100667 mxcmci_finish_request(host, req);
668}
669
670static void mxcmci_set_clk_rate(struct mxcmci_host *host, unsigned int clk_ios)
671{
672 unsigned int divider;
673 int prescaler = 0;
674 unsigned int clk_in = clk_get_rate(host->clk);
675
676 while (prescaler <= 0x800) {
677 for (divider = 1; divider <= 0xF; divider++) {
678 int x;
679
680 x = (clk_in / (divider + 1));
681
682 if (prescaler)
683 x /= (prescaler * 2);
684
685 if (x <= clk_ios)
686 break;
687 }
688 if (divider < 0x10)
689 break;
690
691 if (prescaler == 0)
692 prescaler = 1;
693 else
694 prescaler <<= 1;
695 }
696
697 writew((prescaler << 4) | divider, host->base + MMC_REG_CLK_RATE);
698
699 dev_dbg(mmc_dev(host->mmc), "scaler: %d divider: %d in: %d out: %d\n",
700 prescaler, divider, clk_in, clk_ios);
701}
702
Sascha Hauerf53fbde2011-02-18 10:21:09 +0100703static int mxcmci_setup_dma(struct mmc_host *mmc)
704{
705 struct mxcmci_host *host = mmc_priv(mmc);
706 struct dma_slave_config *config = &host->dma_slave_config;
707
708 config->dst_addr = host->res->start + MMC_REG_BUFFER_ACCESS;
709 config->src_addr = host->res->start + MMC_REG_BUFFER_ACCESS;
710 config->dst_addr_width = 4;
711 config->src_addr_width = 4;
712 config->dst_maxburst = host->burstlen;
713 config->src_maxburst = host->burstlen;
714
715 return dmaengine_slave_config(host->dma, config);
716}
717
Sascha Hauerd96be872009-01-06 17:04:14 +0100718static void mxcmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
719{
720 struct mxcmci_host *host = mmc_priv(mmc);
Sascha Hauerf53fbde2011-02-18 10:21:09 +0100721 int burstlen, ret;
722
Sascha Hauerd96be872009-01-06 17:04:14 +0100723 /*
Sascha Hauer6584cb82011-07-06 11:18:33 +0200724 * use burstlen of 64 (16 words) in 4 bit mode (--> reg value 0)
725 * use burstlen of 16 (4 words) in 1 bit mode (--> reg value 16)
Sascha Hauerd96be872009-01-06 17:04:14 +0100726 */
727 if (ios->bus_width == MMC_BUS_WIDTH_4)
Sascha Hauerf53fbde2011-02-18 10:21:09 +0100728 burstlen = 16;
Sascha Hauer6584cb82011-07-06 11:18:33 +0200729 else
730 burstlen = 4;
Sascha Hauerd96be872009-01-06 17:04:14 +0100731
Sascha Hauerf53fbde2011-02-18 10:21:09 +0100732 if (mxcmci_use_dma(host) && burstlen != host->burstlen) {
733 host->burstlen = burstlen;
734 ret = mxcmci_setup_dma(mmc);
735 if (ret) {
736 dev_err(mmc_dev(host->mmc),
737 "failed to config DMA channel. Falling back to PIO\n");
738 dma_release_channel(host->dma);
739 host->do_dma = 0;
Sascha Hauere58f5162011-11-11 16:28:05 +0100740 host->dma = NULL;
Sascha Hauerf53fbde2011-02-18 10:21:09 +0100741 }
742 }
743
Sascha Hauerd96be872009-01-06 17:04:14 +0100744 if (ios->bus_width == MMC_BUS_WIDTH_4)
745 host->cmdat |= CMD_DAT_CONT_BUS_WIDTH_4;
746 else
747 host->cmdat &= ~CMD_DAT_CONT_BUS_WIDTH_4;
748
749 if (host->power_mode != ios->power_mode) {
Alberto Panizzod078d242010-11-09 11:35:37 +0100750 mxcmci_set_power(host, ios->power_mode, ios->vdd);
Sascha Hauerd96be872009-01-06 17:04:14 +0100751 host->power_mode = ios->power_mode;
Alberto Panizzo74b66952010-11-02 01:05:37 +0100752
Sascha Hauerd96be872009-01-06 17:04:14 +0100753 if (ios->power_mode == MMC_POWER_ON)
754 host->cmdat |= CMD_DAT_CONT_INIT;
755 }
756
757 if (ios->clock) {
758 mxcmci_set_clk_rate(host, ios->clock);
759 writew(STR_STP_CLK_START_CLK, host->base + MMC_REG_STR_STP_CLK);
760 } else {
761 writew(STR_STP_CLK_STOP_CLK, host->base + MMC_REG_STR_STP_CLK);
762 }
763
764 host->clock = ios->clock;
765}
766
767static irqreturn_t mxcmci_detect_irq(int irq, void *data)
768{
769 struct mmc_host *mmc = data;
770
771 dev_dbg(mmc_dev(mmc), "%s\n", __func__);
772
773 mmc_detect_change(mmc, msecs_to_jiffies(250));
774 return IRQ_HANDLED;
775}
776
777static int mxcmci_get_ro(struct mmc_host *mmc)
778{
779 struct mxcmci_host *host = mmc_priv(mmc);
780
781 if (host->pdata && host->pdata->get_ro)
782 return !!host->pdata->get_ro(mmc_dev(mmc));
783 /*
784 * Board doesn't support read only detection; let the mmc core
785 * decide what to do.
786 */
787 return -ENOSYS;
788}
789
Daniel Mackf441b992010-04-01 10:03:24 +0200790static void mxcmci_enable_sdio_irq(struct mmc_host *mmc, int enable)
791{
792 struct mxcmci_host *host = mmc_priv(mmc);
793 unsigned long flags;
794 u32 int_cntr;
795
796 spin_lock_irqsave(&host->lock, flags);
797 host->use_sdio = enable;
798 int_cntr = readl(host->base + MMC_REG_INT_CNTR);
799
800 if (enable)
801 int_cntr |= INT_SDIO_IRQ_EN;
802 else
803 int_cntr &= ~INT_SDIO_IRQ_EN;
804
805 writel(int_cntr, host->base + MMC_REG_INT_CNTR);
806 spin_unlock_irqrestore(&host->lock, flags);
807}
Sascha Hauerd96be872009-01-06 17:04:14 +0100808
Daniel Mack3fcb0272010-04-01 10:03:25 +0200809static void mxcmci_init_card(struct mmc_host *host, struct mmc_card *card)
810{
811 /*
812 * MX3 SoCs have a silicon bug which corrupts CRC calculation of
813 * multi-block transfers when connected SDIO peripheral doesn't
814 * drive the BUSY line as required by the specs.
815 * One way to prevent this is to only allow 1-bit transfers.
816 */
817
818 if (cpu_is_mx3() && card->type == MMC_TYPE_SDIO)
819 host->caps &= ~MMC_CAP_4_BIT_DATA;
820 else
821 host->caps |= MMC_CAP_4_BIT_DATA;
822}
823
Sascha Hauerf53fbde2011-02-18 10:21:09 +0100824static bool filter(struct dma_chan *chan, void *param)
825{
826 struct mxcmci_host *host = param;
827
828 if (!imx_dma_is_general_purpose(chan))
829 return false;
830
831 chan->private = &host->dma_data;
832
833 return true;
834}
835
Sascha Hauerd96be872009-01-06 17:04:14 +0100836static const struct mmc_host_ops mxcmci_ops = {
Daniel Mackf441b992010-04-01 10:03:24 +0200837 .request = mxcmci_request,
838 .set_ios = mxcmci_set_ios,
839 .get_ro = mxcmci_get_ro,
840 .enable_sdio_irq = mxcmci_enable_sdio_irq,
Daniel Mack3fcb0272010-04-01 10:03:25 +0200841 .init_card = mxcmci_init_card,
Sascha Hauerd96be872009-01-06 17:04:14 +0100842};
843
844static int mxcmci_probe(struct platform_device *pdev)
845{
846 struct mmc_host *mmc;
847 struct mxcmci_host *host = NULL;
Uwe Kleine-Königc0521ba2009-12-14 18:01:17 -0800848 struct resource *iores, *r;
Sascha Hauerd96be872009-01-06 17:04:14 +0100849 int ret = 0, irq;
Sascha Hauerf53fbde2011-02-18 10:21:09 +0100850 dma_cap_mask_t mask;
Sascha Hauerd96be872009-01-06 17:04:14 +0100851
Girish K Sa3c76eb2011-10-11 11:44:09 +0530852 pr_info("i.MX SDHC driver\n");
Sascha Hauerd96be872009-01-06 17:04:14 +0100853
Uwe Kleine-Königc0521ba2009-12-14 18:01:17 -0800854 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Sascha Hauerd96be872009-01-06 17:04:14 +0100855 irq = platform_get_irq(pdev, 0);
Uwe Kleine-Königc0521ba2009-12-14 18:01:17 -0800856 if (!iores || irq < 0)
Sascha Hauerd96be872009-01-06 17:04:14 +0100857 return -EINVAL;
858
Uwe Kleine-Königc0521ba2009-12-14 18:01:17 -0800859 r = request_mem_region(iores->start, resource_size(iores), pdev->name);
Sascha Hauerd96be872009-01-06 17:04:14 +0100860 if (!r)
861 return -EBUSY;
862
863 mmc = mmc_alloc_host(sizeof(struct mxcmci_host), &pdev->dev);
864 if (!mmc) {
865 ret = -ENOMEM;
866 goto out_release_mem;
867 }
868
869 mmc->ops = &mxcmci_ops;
Daniel Mackf441b992010-04-01 10:03:24 +0200870 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
Sascha Hauerd96be872009-01-06 17:04:14 +0100871
872 /* MMC core transfer sizes tunable parameters */
Martin K. Petersena36274e2010-09-10 01:33:59 -0400873 mmc->max_segs = 64;
Sascha Hauerd96be872009-01-06 17:04:14 +0100874 mmc->max_blk_size = 2048;
875 mmc->max_blk_count = 65535;
876 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
Vladimir Zapolskiyd759c372010-03-10 15:23:57 -0800877 mmc->max_seg_size = mmc->max_req_size;
Sascha Hauerd96be872009-01-06 17:04:14 +0100878
879 host = mmc_priv(mmc);
880 host->base = ioremap(r->start, resource_size(r));
881 if (!host->base) {
882 ret = -ENOMEM;
883 goto out_free;
884 }
885
886 host->mmc = mmc;
887 host->pdata = pdev->dev.platform_data;
Daniel Mackf441b992010-04-01 10:03:24 +0200888 spin_lock_init(&host->lock);
Sascha Hauerd96be872009-01-06 17:04:14 +0100889
Alberto Panizzo74b66952010-11-02 01:05:37 +0100890 mxcmci_init_ocr(host);
Sascha Hauerd96be872009-01-06 17:04:14 +0100891
Eric Bénard16b3bf82010-05-19 18:46:04 +0200892 if (host->pdata && host->pdata->dat3_card_detect)
893 host->default_irq_mask =
894 INT_CARD_INSERTION_EN | INT_CARD_REMOVAL_EN;
895 else
896 host->default_irq_mask = 0;
897
Sascha Hauerd96be872009-01-06 17:04:14 +0100898 host->res = r;
899 host->irq = irq;
900
Sascha Hauer06277b52009-02-23 13:33:31 +0100901 host->clk = clk_get(&pdev->dev, NULL);
Sascha Hauerd96be872009-01-06 17:04:14 +0100902 if (IS_ERR(host->clk)) {
903 ret = PTR_ERR(host->clk);
904 goto out_iounmap;
905 }
906 clk_enable(host->clk);
907
908 mxcmci_softreset(host);
909
910 host->rev_no = readw(host->base + MMC_REG_REV_NO);
911 if (host->rev_no != 0x400) {
912 ret = -ENODEV;
913 dev_err(mmc_dev(host->mmc), "wrong rev.no. 0x%08x. aborting.\n",
914 host->rev_no);
915 goto out_clk_put;
916 }
917
Sascha Hauerc499b062009-04-03 14:41:56 +0200918 mmc->f_min = clk_get_rate(host->clk) >> 16;
Sascha Hauerd96be872009-01-06 17:04:14 +0100919 mmc->f_max = clk_get_rate(host->clk) >> 1;
920
921 /* recommended in data sheet */
922 writew(0x2db4, host->base + MMC_REG_READ_TO);
923
Eric Bénard16b3bf82010-05-19 18:46:04 +0200924 writel(host->default_irq_mask, host->base + MMC_REG_INT_CNTR);
Sascha Hauerd96be872009-01-06 17:04:14 +0100925
Sascha Hauerd96be872009-01-06 17:04:14 +0100926 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
Sascha Hauerf53fbde2011-02-18 10:21:09 +0100927 if (r) {
928 host->dmareq = r->start;
929 host->dma_data.peripheral_type = IMX_DMATYPE_SDHC;
930 host->dma_data.priority = DMA_PRIO_LOW;
931 host->dma_data.dma_request = host->dmareq;
932 dma_cap_zero(mask);
933 dma_cap_set(DMA_SLAVE, mask);
934 host->dma = dma_request_channel(mask, filter, host);
935 if (host->dma)
936 mmc->max_seg_size = dma_get_max_seg_size(
937 host->dma->device->dev);
Sascha Hauerd96be872009-01-06 17:04:14 +0100938 }
939
Sascha Hauerf53fbde2011-02-18 10:21:09 +0100940 if (!host->dma)
941 dev_info(mmc_dev(host->mmc), "dma not available. Using PIO\n");
942
Sascha Hauerd96be872009-01-06 17:04:14 +0100943 INIT_WORK(&host->datawork, mxcmci_datawork);
944
945 ret = request_irq(host->irq, mxcmci_irq, 0, DRIVER_NAME, host);
946 if (ret)
947 goto out_free_dma;
948
949 platform_set_drvdata(pdev, mmc);
950
951 if (host->pdata && host->pdata->init) {
952 ret = host->pdata->init(&pdev->dev, mxcmci_detect_irq,
953 host->mmc);
954 if (ret)
955 goto out_free_irq;
956 }
957
958 mmc_add_host(mmc);
959
960 return 0;
961
962out_free_irq:
963 free_irq(host->irq, host);
964out_free_dma:
Sascha Hauerf53fbde2011-02-18 10:21:09 +0100965 if (host->dma)
966 dma_release_channel(host->dma);
Sascha Hauerd96be872009-01-06 17:04:14 +0100967out_clk_put:
968 clk_disable(host->clk);
969 clk_put(host->clk);
970out_iounmap:
971 iounmap(host->base);
972out_free:
973 mmc_free_host(mmc);
974out_release_mem:
Uwe Kleine-Königc0521ba2009-12-14 18:01:17 -0800975 release_mem_region(iores->start, resource_size(iores));
Sascha Hauerd96be872009-01-06 17:04:14 +0100976 return ret;
977}
978
979static int mxcmci_remove(struct platform_device *pdev)
980{
981 struct mmc_host *mmc = platform_get_drvdata(pdev);
982 struct mxcmci_host *host = mmc_priv(mmc);
983
984 platform_set_drvdata(pdev, NULL);
985
986 mmc_remove_host(mmc);
987
Alberto Panizzo74b66952010-11-02 01:05:37 +0100988 if (host->vcc)
989 regulator_put(host->vcc);
990
Sascha Hauerd96be872009-01-06 17:04:14 +0100991 if (host->pdata && host->pdata->exit)
992 host->pdata->exit(&pdev->dev, mmc);
993
994 free_irq(host->irq, host);
995 iounmap(host->base);
Sascha Hauerf53fbde2011-02-18 10:21:09 +0100996
997 if (host->dma)
998 dma_release_channel(host->dma);
999
Sascha Hauerd96be872009-01-06 17:04:14 +01001000 clk_disable(host->clk);
1001 clk_put(host->clk);
1002
1003 release_mem_region(host->res->start, resource_size(host->res));
Sascha Hauerd96be872009-01-06 17:04:14 +01001004
1005 mmc_free_host(mmc);
1006
1007 return 0;
1008}
1009
1010#ifdef CONFIG_PM
Eric Bénarda7d403c2010-06-17 20:59:07 +02001011static int mxcmci_suspend(struct device *dev)
Sascha Hauerd96be872009-01-06 17:04:14 +01001012{
Eric Bénarda7d403c2010-06-17 20:59:07 +02001013 struct mmc_host *mmc = dev_get_drvdata(dev);
1014 struct mxcmci_host *host = mmc_priv(mmc);
Sascha Hauerd96be872009-01-06 17:04:14 +01001015 int ret = 0;
1016
1017 if (mmc)
Matt Fleming1a13f8f2010-05-26 14:42:08 -07001018 ret = mmc_suspend_host(mmc);
Eric Bénarda7d403c2010-06-17 20:59:07 +02001019 clk_disable(host->clk);
Sascha Hauerd96be872009-01-06 17:04:14 +01001020
1021 return ret;
1022}
1023
Eric Bénarda7d403c2010-06-17 20:59:07 +02001024static int mxcmci_resume(struct device *dev)
Sascha Hauerd96be872009-01-06 17:04:14 +01001025{
Eric Bénarda7d403c2010-06-17 20:59:07 +02001026 struct mmc_host *mmc = dev_get_drvdata(dev);
1027 struct mxcmci_host *host = mmc_priv(mmc);
Sascha Hauerd96be872009-01-06 17:04:14 +01001028 int ret = 0;
1029
Eric Bénarda7d403c2010-06-17 20:59:07 +02001030 clk_enable(host->clk);
1031 if (mmc)
Sascha Hauerd96be872009-01-06 17:04:14 +01001032 ret = mmc_resume_host(mmc);
Sascha Hauerd96be872009-01-06 17:04:14 +01001033
1034 return ret;
1035}
Eric Bénarda7d403c2010-06-17 20:59:07 +02001036
1037static const struct dev_pm_ops mxcmci_pm_ops = {
1038 .suspend = mxcmci_suspend,
1039 .resume = mxcmci_resume,
1040};
1041#endif
Sascha Hauerd96be872009-01-06 17:04:14 +01001042
1043static struct platform_driver mxcmci_driver = {
1044 .probe = mxcmci_probe,
1045 .remove = mxcmci_remove,
Sascha Hauerd96be872009-01-06 17:04:14 +01001046 .driver = {
1047 .name = DRIVER_NAME,
1048 .owner = THIS_MODULE,
Eric Bénarda7d403c2010-06-17 20:59:07 +02001049#ifdef CONFIG_PM
1050 .pm = &mxcmci_pm_ops,
1051#endif
Sascha Hauerd96be872009-01-06 17:04:14 +01001052 }
1053};
1054
Axel Lind1f81a62011-11-26 12:55:43 +08001055module_platform_driver(mxcmci_driver);
Sascha Hauerd96be872009-01-06 17:04:14 +01001056
1057MODULE_DESCRIPTION("i.MX Multimedia Card Interface Driver");
1058MODULE_AUTHOR("Sascha Hauer, Pengutronix");
1059MODULE_LICENSE("GPL");
1060MODULE_ALIAS("platform:imx-mmc");