ARM: MXC: mxcmmc: Teach the driver SDIO operations
[safe/jmp/linux-2.6] / drivers / mmc / host / mxcmmc.c
1 /*
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
7  *  not need all the quirks found in imxmmc.c, hence the separate driver.
8  *
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>
34
35 #include <asm/dma.h>
36 #include <asm/irq.h>
37 #include <asm/sizes.h>
38 #include <mach/mmc.h>
39
40 #ifdef CONFIG_ARCH_MX2
41 #include <mach/dma-mx1-mx2.h>
42 #define HAS_DMA
43 #endif
44
45 #define DRIVER_NAME "mxc-mmc"
46
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
114 struct mxcmci_host {
115         struct mmc_host         *mmc;
116         struct resource         *res;
117         void __iomem            *base;
118         int                     irq;
119         int                     detect_irq;
120         int                     dma;
121         int                     do_dma;
122         int                     use_sdio;
123         unsigned int            power_mode;
124         struct imxmmc_platform_data *pdata;
125
126         struct mmc_request      *req;
127         struct mmc_command      *cmd;
128         struct mmc_data         *data;
129
130         unsigned int            dma_nents;
131         unsigned int            datasize;
132         unsigned int            dma_dir;
133
134         u16                     rev_no;
135         unsigned int            cmdat;
136
137         struct clk              *clk;
138
139         int                     clock;
140
141         struct work_struct      datawork;
142         spinlock_t              lock;
143 };
144
145 static void mxcmci_set_clk_rate(struct mxcmci_host *host, unsigned int clk_ios);
146
147 static inline int mxcmci_use_dma(struct mxcmci_host *host)
148 {
149         return host->do_dma;
150 }
151
152 static void mxcmci_softreset(struct mxcmci_host *host)
153 {
154         int i;
155
156         dev_dbg(mmc_dev(host->mmc), "mxcmci_softreset\n");
157
158         /* reset sequence */
159         writew(STR_STP_CLK_RESET, host->base + MMC_REG_STR_STP_CLK);
160         writew(STR_STP_CLK_RESET | STR_STP_CLK_START_CLK,
161                         host->base + MMC_REG_STR_STP_CLK);
162
163         for (i = 0; i < 8; i++)
164                 writew(STR_STP_CLK_START_CLK, host->base + MMC_REG_STR_STP_CLK);
165
166         writew(0xff, host->base + MMC_REG_RES_TO);
167 }
168
169 static int mxcmci_setup_data(struct mxcmci_host *host, struct mmc_data *data)
170 {
171         unsigned int nob = data->blocks;
172         unsigned int blksz = data->blksz;
173         unsigned int datasize = nob * blksz;
174 #ifdef HAS_DMA
175         struct scatterlist *sg;
176         int i;
177         int ret;
178 #endif
179         if (data->flags & MMC_DATA_STREAM)
180                 nob = 0xffff;
181
182         host->data = data;
183         data->bytes_xfered = 0;
184
185         writew(nob, host->base + MMC_REG_NOB);
186         writew(blksz, host->base + MMC_REG_BLK_LEN);
187         host->datasize = datasize;
188
189 #ifdef HAS_DMA
190         for_each_sg(data->sg, sg, data->sg_len, i) {
191                 if (sg->offset & 3 || sg->length & 3) {
192                         host->do_dma = 0;
193                         return 0;
194                 }
195         }
196
197         if (data->flags & MMC_DATA_READ) {
198                 host->dma_dir = DMA_FROM_DEVICE;
199                 host->dma_nents = dma_map_sg(mmc_dev(host->mmc), data->sg,
200                                              data->sg_len,  host->dma_dir);
201
202                 ret = imx_dma_setup_sg(host->dma, data->sg, host->dma_nents,
203                                 datasize,
204                                 host->res->start + MMC_REG_BUFFER_ACCESS,
205                                 DMA_MODE_READ);
206         } else {
207                 host->dma_dir = DMA_TO_DEVICE;
208                 host->dma_nents = dma_map_sg(mmc_dev(host->mmc), data->sg,
209                                              data->sg_len,  host->dma_dir);
210
211                 ret = imx_dma_setup_sg(host->dma, data->sg, host->dma_nents,
212                                 datasize,
213                                 host->res->start + MMC_REG_BUFFER_ACCESS,
214                                 DMA_MODE_WRITE);
215         }
216
217         if (ret) {
218                 dev_err(mmc_dev(host->mmc), "failed to setup DMA : %d\n", ret);
219                 return ret;
220         }
221         wmb();
222
223         imx_dma_enable(host->dma);
224 #endif /* HAS_DMA */
225         return 0;
226 }
227
228 static int mxcmci_start_cmd(struct mxcmci_host *host, struct mmc_command *cmd,
229                 unsigned int cmdat)
230 {
231         u32 int_cntr;
232         unsigned long flags;
233
234         WARN_ON(host->cmd != NULL);
235         host->cmd = cmd;
236
237         switch (mmc_resp_type(cmd)) {
238         case MMC_RSP_R1: /* short CRC, OPCODE */
239         case MMC_RSP_R1B:/* short CRC, OPCODE, BUSY */
240                 cmdat |= CMD_DAT_CONT_RESPONSE_48BIT_CRC;
241                 break;
242         case MMC_RSP_R2: /* long 136 bit + CRC */
243                 cmdat |= CMD_DAT_CONT_RESPONSE_136BIT;
244                 break;
245         case MMC_RSP_R3: /* short */
246                 cmdat |= CMD_DAT_CONT_RESPONSE_48BIT;
247                 break;
248         case MMC_RSP_NONE:
249                 break;
250         default:
251                 dev_err(mmc_dev(host->mmc), "unhandled response type 0x%x\n",
252                                 mmc_resp_type(cmd));
253                 cmd->error = -EINVAL;
254                 return -EINVAL;
255         }
256
257         int_cntr = INT_END_CMD_RES_EN;
258
259         if (mxcmci_use_dma(host))
260                 int_cntr |= INT_READ_OP_EN | INT_WRITE_OP_DONE_EN;
261
262         spin_lock_irqsave(&host->lock, flags);
263         if (host->use_sdio)
264                 int_cntr |= INT_SDIO_IRQ_EN;
265         writel(int_cntr, host->base + MMC_REG_INT_CNTR);
266         spin_unlock_irqrestore(&host->lock, flags);
267
268         writew(cmd->opcode, host->base + MMC_REG_CMD);
269         writel(cmd->arg, host->base + MMC_REG_ARG);
270         writew(cmdat, host->base + MMC_REG_CMD_DAT_CONT);
271
272         return 0;
273 }
274
275 static void mxcmci_finish_request(struct mxcmci_host *host,
276                 struct mmc_request *req)
277 {
278         u32 int_cntr = 0;
279         unsigned long flags;
280
281         spin_lock_irqsave(&host->lock, flags);
282         if (host->use_sdio)
283                 int_cntr |= INT_SDIO_IRQ_EN;
284         writel(int_cntr, host->base + MMC_REG_INT_CNTR);
285         spin_unlock_irqrestore(&host->lock, flags);
286
287         host->req = NULL;
288         host->cmd = NULL;
289         host->data = NULL;
290
291         mmc_request_done(host->mmc, req);
292 }
293
294 static int mxcmci_finish_data(struct mxcmci_host *host, unsigned int stat)
295 {
296         struct mmc_data *data = host->data;
297         int data_error;
298
299 #ifdef HAS_DMA
300         if (mxcmci_use_dma(host)) {
301                 imx_dma_disable(host->dma);
302                 dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->dma_nents,
303                                 host->dma_dir);
304         }
305 #endif
306
307         if (stat & STATUS_ERR_MASK) {
308                 dev_dbg(mmc_dev(host->mmc), "request failed. status: 0x%08x\n",
309                                 stat);
310                 if (stat & STATUS_CRC_READ_ERR) {
311                         dev_err(mmc_dev(host->mmc), "%s: -EILSEQ\n", __func__);
312                         data->error = -EILSEQ;
313                 } else if (stat & STATUS_CRC_WRITE_ERR) {
314                         u32 err_code = (stat >> 9) & 0x3;
315                         if (err_code == 2) { /* No CRC response */
316                                 dev_err(mmc_dev(host->mmc),
317                                         "%s: No CRC -ETIMEDOUT\n", __func__);
318                                 data->error = -ETIMEDOUT;
319                         } else {
320                                 dev_err(mmc_dev(host->mmc),
321                                         "%s: -EILSEQ\n", __func__);
322                                 data->error = -EILSEQ;
323                         }
324                 } else if (stat & STATUS_TIME_OUT_READ) {
325                         dev_err(mmc_dev(host->mmc),
326                                 "%s: read -ETIMEDOUT\n", __func__);
327                         data->error = -ETIMEDOUT;
328                 } else {
329                         dev_err(mmc_dev(host->mmc), "%s: -EIO\n", __func__);
330                         data->error = -EIO;
331                 }
332         } else {
333                 data->bytes_xfered = host->datasize;
334         }
335
336         data_error = data->error;
337
338         host->data = NULL;
339
340         return data_error;
341 }
342
343 static void mxcmci_read_response(struct mxcmci_host *host, unsigned int stat)
344 {
345         struct mmc_command *cmd = host->cmd;
346         int i;
347         u32 a, b, c;
348
349         if (!cmd)
350                 return;
351
352         if (stat & STATUS_TIME_OUT_RESP) {
353                 dev_dbg(mmc_dev(host->mmc), "CMD TIMEOUT\n");
354                 cmd->error = -ETIMEDOUT;
355         } else if (stat & STATUS_RESP_CRC_ERR && cmd->flags & MMC_RSP_CRC) {
356                 dev_dbg(mmc_dev(host->mmc), "cmd crc error\n");
357                 cmd->error = -EILSEQ;
358         }
359
360         if (cmd->flags & MMC_RSP_PRESENT) {
361                 if (cmd->flags & MMC_RSP_136) {
362                         for (i = 0; i < 4; i++) {
363                                 a = readw(host->base + MMC_REG_RES_FIFO);
364                                 b = readw(host->base + MMC_REG_RES_FIFO);
365                                 cmd->resp[i] = a << 16 | b;
366                         }
367                 } else {
368                         a = readw(host->base + MMC_REG_RES_FIFO);
369                         b = readw(host->base + MMC_REG_RES_FIFO);
370                         c = readw(host->base + MMC_REG_RES_FIFO);
371                         cmd->resp[0] = a << 24 | b << 8 | c >> 8;
372                 }
373         }
374 }
375
376 static int mxcmci_poll_status(struct mxcmci_host *host, u32 mask)
377 {
378         u32 stat;
379         unsigned long timeout = jiffies + HZ;
380
381         do {
382                 stat = readl(host->base + MMC_REG_STATUS);
383                 if (stat & STATUS_ERR_MASK)
384                         return stat;
385                 if (time_after(jiffies, timeout)) {
386                         mxcmci_softreset(host);
387                         mxcmci_set_clk_rate(host, host->clock);
388                         return STATUS_TIME_OUT_READ;
389                 }
390                 if (stat & mask)
391                         return 0;
392                 cpu_relax();
393         } while (1);
394 }
395
396 static int mxcmci_pull(struct mxcmci_host *host, void *_buf, int bytes)
397 {
398         unsigned int stat;
399         u32 *buf = _buf;
400
401         while (bytes > 3) {
402                 stat = mxcmci_poll_status(host,
403                                 STATUS_BUF_READ_RDY | STATUS_READ_OP_DONE);
404                 if (stat)
405                         return stat;
406                 *buf++ = readl(host->base + MMC_REG_BUFFER_ACCESS);
407                 bytes -= 4;
408         }
409
410         if (bytes) {
411                 u8 *b = (u8 *)buf;
412                 u32 tmp;
413
414                 stat = mxcmci_poll_status(host,
415                                 STATUS_BUF_READ_RDY | STATUS_READ_OP_DONE);
416                 if (stat)
417                         return stat;
418                 tmp = readl(host->base + MMC_REG_BUFFER_ACCESS);
419                 memcpy(b, &tmp, bytes);
420         }
421
422         return 0;
423 }
424
425 static int mxcmci_push(struct mxcmci_host *host, void *_buf, int bytes)
426 {
427         unsigned int stat;
428         u32 *buf = _buf;
429
430         while (bytes > 3) {
431                 stat = mxcmci_poll_status(host, STATUS_BUF_WRITE_RDY);
432                 if (stat)
433                         return stat;
434                 writel(*buf++, host->base + MMC_REG_BUFFER_ACCESS);
435                 bytes -= 4;
436         }
437
438         if (bytes) {
439                 u8 *b = (u8 *)buf;
440                 u32 tmp;
441
442                 stat = mxcmci_poll_status(host, STATUS_BUF_WRITE_RDY);
443                 if (stat)
444                         return stat;
445
446                 memcpy(&tmp, b, bytes);
447                 writel(tmp, host->base + MMC_REG_BUFFER_ACCESS);
448         }
449
450         stat = mxcmci_poll_status(host, STATUS_BUF_WRITE_RDY);
451         if (stat)
452                 return stat;
453
454         return 0;
455 }
456
457 static int mxcmci_transfer_data(struct mxcmci_host *host)
458 {
459         struct mmc_data *data = host->req->data;
460         struct scatterlist *sg;
461         int stat, i;
462
463         host->data = data;
464         host->datasize = 0;
465
466         if (data->flags & MMC_DATA_READ) {
467                 for_each_sg(data->sg, sg, data->sg_len, i) {
468                         stat = mxcmci_pull(host, sg_virt(sg), sg->length);
469                         if (stat)
470                                 return stat;
471                         host->datasize += sg->length;
472                 }
473         } else {
474                 for_each_sg(data->sg, sg, data->sg_len, i) {
475                         stat = mxcmci_push(host, sg_virt(sg), sg->length);
476                         if (stat)
477                                 return stat;
478                         host->datasize += sg->length;
479                 }
480                 stat = mxcmci_poll_status(host, STATUS_WRITE_OP_DONE);
481                 if (stat)
482                         return stat;
483         }
484         return 0;
485 }
486
487 static void mxcmci_datawork(struct work_struct *work)
488 {
489         struct mxcmci_host *host = container_of(work, struct mxcmci_host,
490                                                   datawork);
491         int datastat = mxcmci_transfer_data(host);
492         mxcmci_finish_data(host, datastat);
493
494         if (host->req->stop) {
495                 if (mxcmci_start_cmd(host, host->req->stop, 0)) {
496                         mxcmci_finish_request(host, host->req);
497                         return;
498                 }
499         } else {
500                 mxcmci_finish_request(host, host->req);
501         }
502 }
503
504 #ifdef HAS_DMA
505 static void mxcmci_data_done(struct mxcmci_host *host, unsigned int stat)
506 {
507         struct mmc_data *data = host->data;
508         int data_error;
509
510         if (!data)
511                 return;
512
513         data_error = mxcmci_finish_data(host, stat);
514
515         mxcmci_read_response(host, stat);
516         host->cmd = NULL;
517
518         if (host->req->stop) {
519                 if (mxcmci_start_cmd(host, host->req->stop, 0)) {
520                         mxcmci_finish_request(host, host->req);
521                         return;
522                 }
523         } else {
524                 mxcmci_finish_request(host, host->req);
525         }
526 }
527 #endif /* HAS_DMA */
528
529 static void mxcmci_cmd_done(struct mxcmci_host *host, unsigned int stat)
530 {
531         mxcmci_read_response(host, stat);
532         host->cmd = NULL;
533
534         if (!host->data && host->req) {
535                 mxcmci_finish_request(host, host->req);
536                 return;
537         }
538
539         /* For the DMA case the DMA engine handles the data transfer
540          * automatically. For non DMA we have to do it ourselves.
541          * Don't do it in interrupt context though.
542          */
543         if (!mxcmci_use_dma(host) && host->data)
544                 schedule_work(&host->datawork);
545
546 }
547
548 static irqreturn_t mxcmci_irq(int irq, void *devid)
549 {
550         struct mxcmci_host *host = devid;
551         unsigned long flags;
552         bool sdio_irq;
553         u32 stat;
554
555         stat = readl(host->base + MMC_REG_STATUS);
556         writel(stat & ~STATUS_SDIO_INT_ACTIVE, host->base + MMC_REG_STATUS);
557
558         dev_dbg(mmc_dev(host->mmc), "%s: 0x%08x\n", __func__, stat);
559
560         spin_lock_irqsave(&host->lock, flags);
561         sdio_irq = (stat & STATUS_SDIO_INT_ACTIVE) && host->use_sdio;
562         spin_unlock_irqrestore(&host->lock, flags);
563
564         if (sdio_irq) {
565                 writel(STATUS_SDIO_INT_ACTIVE, host->base + MMC_REG_STATUS);
566                 mmc_signal_sdio_irq(host->mmc);
567         }
568
569         if (stat & STATUS_END_CMD_RESP)
570                 mxcmci_cmd_done(host, stat);
571
572 #ifdef HAS_DMA
573         if (mxcmci_use_dma(host) &&
574                   (stat & (STATUS_DATA_TRANS_DONE | STATUS_WRITE_OP_DONE)))
575                 mxcmci_data_done(host, stat);
576 #endif
577         return IRQ_HANDLED;
578 }
579
580 static void mxcmci_request(struct mmc_host *mmc, struct mmc_request *req)
581 {
582         struct mxcmci_host *host = mmc_priv(mmc);
583         unsigned int cmdat = host->cmdat;
584         int error;
585
586         WARN_ON(host->req != NULL);
587
588         host->req = req;
589         host->cmdat &= ~CMD_DAT_CONT_INIT;
590 #ifdef HAS_DMA
591         host->do_dma = 1;
592 #endif
593         if (req->data) {
594                 error = mxcmci_setup_data(host, req->data);
595                 if (error) {
596                         req->cmd->error = error;
597                         goto out;
598                 }
599
600
601                 cmdat |= CMD_DAT_CONT_DATA_ENABLE;
602
603                 if (req->data->flags & MMC_DATA_WRITE)
604                         cmdat |= CMD_DAT_CONT_WRITE;
605         }
606
607         error = mxcmci_start_cmd(host, req->cmd, cmdat);
608 out:
609         if (error)
610                 mxcmci_finish_request(host, req);
611 }
612
613 static void mxcmci_set_clk_rate(struct mxcmci_host *host, unsigned int clk_ios)
614 {
615         unsigned int divider;
616         int prescaler = 0;
617         unsigned int clk_in = clk_get_rate(host->clk);
618
619         while (prescaler <= 0x800) {
620                 for (divider = 1; divider <= 0xF; divider++) {
621                         int x;
622
623                         x = (clk_in / (divider + 1));
624
625                         if (prescaler)
626                                 x /= (prescaler * 2);
627
628                         if (x <= clk_ios)
629                                 break;
630                 }
631                 if (divider < 0x10)
632                         break;
633
634                 if (prescaler == 0)
635                         prescaler = 1;
636                 else
637                         prescaler <<= 1;
638         }
639
640         writew((prescaler << 4) | divider, host->base + MMC_REG_CLK_RATE);
641
642         dev_dbg(mmc_dev(host->mmc), "scaler: %d divider: %d in: %d out: %d\n",
643                         prescaler, divider, clk_in, clk_ios);
644 }
645
646 static void mxcmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
647 {
648         struct mxcmci_host *host = mmc_priv(mmc);
649 #ifdef HAS_DMA
650         unsigned int blen;
651         /*
652          * use burstlen of 64 in 4 bit mode (--> reg value  0)
653          * use burstlen of 16 in 1 bit mode (--> reg value 16)
654          */
655         if (ios->bus_width == MMC_BUS_WIDTH_4)
656                 blen = 0;
657         else
658                 blen = 16;
659
660         imx_dma_config_burstlen(host->dma, blen);
661 #endif
662         if (ios->bus_width == MMC_BUS_WIDTH_4)
663                 host->cmdat |= CMD_DAT_CONT_BUS_WIDTH_4;
664         else
665                 host->cmdat &= ~CMD_DAT_CONT_BUS_WIDTH_4;
666
667         if (host->power_mode != ios->power_mode) {
668                 if (host->pdata && host->pdata->setpower)
669                         host->pdata->setpower(mmc_dev(mmc), ios->vdd);
670                 host->power_mode = ios->power_mode;
671                 if (ios->power_mode == MMC_POWER_ON)
672                         host->cmdat |= CMD_DAT_CONT_INIT;
673         }
674
675         if (ios->clock) {
676                 mxcmci_set_clk_rate(host, ios->clock);
677                 writew(STR_STP_CLK_START_CLK, host->base + MMC_REG_STR_STP_CLK);
678         } else {
679                 writew(STR_STP_CLK_STOP_CLK, host->base + MMC_REG_STR_STP_CLK);
680         }
681
682         host->clock = ios->clock;
683 }
684
685 static irqreturn_t mxcmci_detect_irq(int irq, void *data)
686 {
687         struct mmc_host *mmc = data;
688
689         dev_dbg(mmc_dev(mmc), "%s\n", __func__);
690
691         mmc_detect_change(mmc, msecs_to_jiffies(250));
692         return IRQ_HANDLED;
693 }
694
695 static int mxcmci_get_ro(struct mmc_host *mmc)
696 {
697         struct mxcmci_host *host = mmc_priv(mmc);
698
699         if (host->pdata && host->pdata->get_ro)
700                 return !!host->pdata->get_ro(mmc_dev(mmc));
701         /*
702          * Board doesn't support read only detection; let the mmc core
703          * decide what to do.
704          */
705         return -ENOSYS;
706 }
707
708 static void mxcmci_enable_sdio_irq(struct mmc_host *mmc, int enable)
709 {
710         struct mxcmci_host *host = mmc_priv(mmc);
711         unsigned long flags;
712         u32 int_cntr;
713
714         spin_lock_irqsave(&host->lock, flags);
715         host->use_sdio = enable;
716         int_cntr = readl(host->base + MMC_REG_INT_CNTR);
717
718         if (enable)
719                 int_cntr |= INT_SDIO_IRQ_EN;
720         else
721                 int_cntr &= ~INT_SDIO_IRQ_EN;
722
723         writel(int_cntr, host->base + MMC_REG_INT_CNTR);
724         spin_unlock_irqrestore(&host->lock, flags);
725 }
726
727 static const struct mmc_host_ops mxcmci_ops = {
728         .request                = mxcmci_request,
729         .set_ios                = mxcmci_set_ios,
730         .get_ro                 = mxcmci_get_ro,
731         .enable_sdio_irq        = mxcmci_enable_sdio_irq,
732 };
733
734 static int mxcmci_probe(struct platform_device *pdev)
735 {
736         struct mmc_host *mmc;
737         struct mxcmci_host *host = NULL;
738         struct resource *iores, *r;
739         int ret = 0, irq;
740
741         printk(KERN_INFO "i.MX SDHC driver\n");
742
743         iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
744         irq = platform_get_irq(pdev, 0);
745         if (!iores || irq < 0)
746                 return -EINVAL;
747
748         r = request_mem_region(iores->start, resource_size(iores), pdev->name);
749         if (!r)
750                 return -EBUSY;
751
752         mmc = mmc_alloc_host(sizeof(struct mxcmci_host), &pdev->dev);
753         if (!mmc) {
754                 ret = -ENOMEM;
755                 goto out_release_mem;
756         }
757
758         mmc->ops = &mxcmci_ops;
759         mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
760
761         /* MMC core transfer sizes tunable parameters */
762         mmc->max_hw_segs = 64;
763         mmc->max_phys_segs = 64;
764         mmc->max_blk_size = 2048;
765         mmc->max_blk_count = 65535;
766         mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
767         mmc->max_seg_size = mmc->max_req_size;
768
769         host = mmc_priv(mmc);
770         host->base = ioremap(r->start, resource_size(r));
771         if (!host->base) {
772                 ret = -ENOMEM;
773                 goto out_free;
774         }
775
776         host->mmc = mmc;
777         host->pdata = pdev->dev.platform_data;
778         spin_lock_init(&host->lock);
779
780         if (host->pdata && host->pdata->ocr_avail)
781                 mmc->ocr_avail = host->pdata->ocr_avail;
782         else
783                 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
784
785         host->res = r;
786         host->irq = irq;
787
788         host->clk = clk_get(&pdev->dev, NULL);
789         if (IS_ERR(host->clk)) {
790                 ret = PTR_ERR(host->clk);
791                 goto out_iounmap;
792         }
793         clk_enable(host->clk);
794
795         mxcmci_softreset(host);
796
797         host->rev_no = readw(host->base + MMC_REG_REV_NO);
798         if (host->rev_no != 0x400) {
799                 ret = -ENODEV;
800                 dev_err(mmc_dev(host->mmc), "wrong rev.no. 0x%08x. aborting.\n",
801                         host->rev_no);
802                 goto out_clk_put;
803         }
804
805         mmc->f_min = clk_get_rate(host->clk) >> 16;
806         mmc->f_max = clk_get_rate(host->clk) >> 1;
807
808         /* recommended in data sheet */
809         writew(0x2db4, host->base + MMC_REG_READ_TO);
810
811         writel(0, host->base + MMC_REG_INT_CNTR);
812
813 #ifdef HAS_DMA
814         host->dma = imx_dma_request_by_prio(DRIVER_NAME, DMA_PRIO_LOW);
815         if (host->dma < 0) {
816                 dev_err(mmc_dev(host->mmc), "imx_dma_request_by_prio failed\n");
817                 ret = -EBUSY;
818                 goto out_clk_put;
819         }
820
821         r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
822         if (!r) {
823                 ret = -EINVAL;
824                 goto out_free_dma;
825         }
826
827         ret = imx_dma_config_channel(host->dma,
828                                      IMX_DMA_MEMSIZE_32 | IMX_DMA_TYPE_FIFO,
829                                      IMX_DMA_MEMSIZE_32 | IMX_DMA_TYPE_LINEAR,
830                                      r->start, 0);
831         if (ret) {
832                 dev_err(mmc_dev(host->mmc), "failed to config DMA channel\n");
833                 goto out_free_dma;
834         }
835 #endif
836         INIT_WORK(&host->datawork, mxcmci_datawork);
837
838         ret = request_irq(host->irq, mxcmci_irq, 0, DRIVER_NAME, host);
839         if (ret)
840                 goto out_free_dma;
841
842         platform_set_drvdata(pdev, mmc);
843
844         if (host->pdata && host->pdata->init) {
845                 ret = host->pdata->init(&pdev->dev, mxcmci_detect_irq,
846                                 host->mmc);
847                 if (ret)
848                         goto out_free_irq;
849         }
850
851         mmc_add_host(mmc);
852
853         return 0;
854
855 out_free_irq:
856         free_irq(host->irq, host);
857 out_free_dma:
858 #ifdef HAS_DMA
859         imx_dma_free(host->dma);
860 #endif
861 out_clk_put:
862         clk_disable(host->clk);
863         clk_put(host->clk);
864 out_iounmap:
865         iounmap(host->base);
866 out_free:
867         mmc_free_host(mmc);
868 out_release_mem:
869         release_mem_region(iores->start, resource_size(iores));
870         return ret;
871 }
872
873 static int mxcmci_remove(struct platform_device *pdev)
874 {
875         struct mmc_host *mmc = platform_get_drvdata(pdev);
876         struct mxcmci_host *host = mmc_priv(mmc);
877
878         platform_set_drvdata(pdev, NULL);
879
880         mmc_remove_host(mmc);
881
882         if (host->pdata && host->pdata->exit)
883                 host->pdata->exit(&pdev->dev, mmc);
884
885         free_irq(host->irq, host);
886         iounmap(host->base);
887 #ifdef HAS_DMA
888         imx_dma_free(host->dma);
889 #endif
890         clk_disable(host->clk);
891         clk_put(host->clk);
892
893         release_mem_region(host->res->start, resource_size(host->res));
894         release_resource(host->res);
895
896         mmc_free_host(mmc);
897
898         return 0;
899 }
900
901 #ifdef CONFIG_PM
902 static int mxcmci_suspend(struct platform_device *dev, pm_message_t state)
903 {
904         struct mmc_host *mmc = platform_get_drvdata(dev);
905         int ret = 0;
906
907         if (mmc)
908                 ret = mmc_suspend_host(mmc, state);
909
910         return ret;
911 }
912
913 static int mxcmci_resume(struct platform_device *dev)
914 {
915         struct mmc_host *mmc = platform_get_drvdata(dev);
916         struct mxcmci_host *host;
917         int ret = 0;
918
919         if (mmc) {
920                 host = mmc_priv(mmc);
921                 ret = mmc_resume_host(mmc);
922         }
923
924         return ret;
925 }
926 #else
927 #define mxcmci_suspend  NULL
928 #define mxcmci_resume   NULL
929 #endif /* CONFIG_PM */
930
931 static struct platform_driver mxcmci_driver = {
932         .probe          = mxcmci_probe,
933         .remove         = mxcmci_remove,
934         .suspend        = mxcmci_suspend,
935         .resume         = mxcmci_resume,
936         .driver         = {
937                 .name           = DRIVER_NAME,
938                 .owner          = THIS_MODULE,
939         }
940 };
941
942 static int __init mxcmci_init(void)
943 {
944         return platform_driver_register(&mxcmci_driver);
945 }
946
947 static void __exit mxcmci_exit(void)
948 {
949         platform_driver_unregister(&mxcmci_driver);
950 }
951
952 module_init(mxcmci_init);
953 module_exit(mxcmci_exit);
954
955 MODULE_DESCRIPTION("i.MX Multimedia Card Interface Driver");
956 MODULE_AUTHOR("Sascha Hauer, Pengutronix");
957 MODULE_LICENSE("GPL");
958 MODULE_ALIAS("platform:imx-mmc");