blob: a25ee71998a9090393b4e20653d90cbed53ab71b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Pierre Ossman70f10482007-07-11 20:04:50 +02002 * linux/drivers/mmc/host/pxa.c - PXA MMCI driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 2003 Russell King, All Rights Reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * This hardware is really sick:
11 * - No way to clear interrupts.
12 * - Have to turn off the clock whenever we touch the device.
13 * - Doesn't tell you how many data blocks were transferred.
14 * Yuck!
15 *
16 * 1 and 3 byte data transfers not supported
17 * max block length up to 1023
18 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/module.h>
20#include <linux/init.h>
21#include <linux/ioport.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010022#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/delay.h>
24#include <linux/interrupt.h>
25#include <linux/dma-mapping.h>
Russell Kingebebd9b2007-08-20 10:20:03 +010026#include <linux/clk.h>
27#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/mmc/host.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include <asm/dma.h>
31#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <asm/sizes.h>
33
34#include <asm/arch/pxa-regs.h>
35#include <asm/arch/mmc.h>
36
37#include "pxamci.h"
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#define DRIVER_NAME "pxa2xx-mci"
40
41#define NR_SG 1
42
43struct pxamci_host {
44 struct mmc_host *mmc;
45 spinlock_t lock;
46 struct resource *res;
47 void __iomem *base;
Russell Kingebebd9b2007-08-20 10:20:03 +010048 struct clk *clk;
49 unsigned long clkrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 int irq;
51 int dma;
52 unsigned int clkrt;
53 unsigned int cmdat;
54 unsigned int imask;
55 unsigned int power_mode;
56 struct pxamci_platform_data *pdata;
57
58 struct mmc_request *mrq;
59 struct mmc_command *cmd;
60 struct mmc_data *data;
61
62 dma_addr_t sg_dma;
63 struct pxa_dma_desc *sg_cpu;
64 unsigned int dma_len;
65
66 unsigned int dma_dir;
67};
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static void pxamci_stop_clock(struct pxamci_host *host)
70{
71 if (readl(host->base + MMC_STAT) & STAT_CLK_EN) {
72 unsigned long timeout = 10000;
73 unsigned int v;
74
75 writel(STOP_CLOCK, host->base + MMC_STRPCL);
76
77 do {
78 v = readl(host->base + MMC_STAT);
79 if (!(v & STAT_CLK_EN))
80 break;
81 udelay(1);
82 } while (timeout--);
83
84 if (v & STAT_CLK_EN)
85 dev_err(mmc_dev(host->mmc), "unable to stop clock\n");
86 }
87}
88
89static void pxamci_enable_irq(struct pxamci_host *host, unsigned int mask)
90{
91 unsigned long flags;
92
93 spin_lock_irqsave(&host->lock, flags);
94 host->imask &= ~mask;
95 writel(host->imask, host->base + MMC_I_MASK);
96 spin_unlock_irqrestore(&host->lock, flags);
97}
98
99static void pxamci_disable_irq(struct pxamci_host *host, unsigned int mask)
100{
101 unsigned long flags;
102
103 spin_lock_irqsave(&host->lock, flags);
104 host->imask |= mask;
105 writel(host->imask, host->base + MMC_I_MASK);
106 spin_unlock_irqrestore(&host->lock, flags);
107}
108
109static void pxamci_setup_data(struct pxamci_host *host, struct mmc_data *data)
110{
111 unsigned int nob = data->blocks;
Russell King3d63abe2006-04-24 11:27:02 +0100112 unsigned long long clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 unsigned int timeout;
114 u32 dcmd;
115 int i;
116
117 host->data = data;
118
119 if (data->flags & MMC_DATA_STREAM)
120 nob = 0xffff;
121
122 writel(nob, host->base + MMC_NOB);
Pavel Pisa2c171bf2006-05-19 21:48:03 +0100123 writel(data->blksz, host->base + MMC_BLKLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Russell Kingebebd9b2007-08-20 10:20:03 +0100125 clks = (unsigned long long)data->timeout_ns * host->clkrate;
Russell King3d63abe2006-04-24 11:27:02 +0100126 do_div(clks, 1000000000UL);
127 timeout = (unsigned int)clks + (data->timeout_clks << host->clkrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 writel((timeout + 255) / 256, host->base + MMC_RDTO);
129
130 if (data->flags & MMC_DATA_READ) {
131 host->dma_dir = DMA_FROM_DEVICE;
132 dcmd = DCMD_INCTRGADDR | DCMD_FLOWTRG;
133 DRCMRTXMMC = 0;
134 DRCMRRXMMC = host->dma | DRCMR_MAPVLD;
135 } else {
136 host->dma_dir = DMA_TO_DEVICE;
137 dcmd = DCMD_INCSRCADDR | DCMD_FLOWSRC;
138 DRCMRRXMMC = 0;
139 DRCMRTXMMC = host->dma | DRCMR_MAPVLD;
140 }
141
142 dcmd |= DCMD_BURST32 | DCMD_WIDTH1;
143
144 host->dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
145 host->dma_dir);
146
147 for (i = 0; i < host->dma_len; i++) {
Nicolas Pitrec7838372007-10-09 17:07:58 -0400148 unsigned int length = sg_dma_len(&data->sg[i]);
149 host->sg_cpu[i].dcmd = dcmd | length;
150 if (length & 31 && !(data->flags & MMC_DATA_READ))
151 host->sg_cpu[i].dcmd |= DCMD_ENDIRQEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 if (data->flags & MMC_DATA_READ) {
153 host->sg_cpu[i].dsadr = host->res->start + MMC_RXFIFO;
154 host->sg_cpu[i].dtadr = sg_dma_address(&data->sg[i]);
155 } else {
156 host->sg_cpu[i].dsadr = sg_dma_address(&data->sg[i]);
157 host->sg_cpu[i].dtadr = host->res->start + MMC_TXFIFO;
158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 host->sg_cpu[i].ddadr = host->sg_dma + (i + 1) *
160 sizeof(struct pxa_dma_desc);
161 }
162 host->sg_cpu[host->dma_len - 1].ddadr = DDADR_STOP;
163 wmb();
164
165 DDADR(host->dma) = host->sg_dma;
166 DCSR(host->dma) = DCSR_RUN;
167}
168
169static void pxamci_start_cmd(struct pxamci_host *host, struct mmc_command *cmd, unsigned int cmdat)
170{
171 WARN_ON(host->cmd != NULL);
172 host->cmd = cmd;
173
174 if (cmd->flags & MMC_RSP_BUSY)
175 cmdat |= CMDAT_BUSY;
176
Russell Kinge9225172006-02-02 12:23:12 +0000177#define RSP_TYPE(x) ((x) & ~(MMC_RSP_BUSY|MMC_RSP_OPCODE))
178 switch (RSP_TYPE(mmc_resp_type(cmd))) {
Philip Langdale6f949902007-01-04 07:04:47 -0800179 case RSP_TYPE(MMC_RSP_R1): /* r1, r1b, r6, r7 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 cmdat |= CMDAT_RESP_SHORT;
181 break;
Russell Kinge9225172006-02-02 12:23:12 +0000182 case RSP_TYPE(MMC_RSP_R3):
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 cmdat |= CMDAT_RESP_R3;
184 break;
Russell Kinge9225172006-02-02 12:23:12 +0000185 case RSP_TYPE(MMC_RSP_R2):
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 cmdat |= CMDAT_RESP_R2;
187 break;
188 default:
189 break;
190 }
191
192 writel(cmd->opcode, host->base + MMC_CMD);
193 writel(cmd->arg >> 16, host->base + MMC_ARGH);
194 writel(cmd->arg & 0xffff, host->base + MMC_ARGL);
195 writel(cmdat, host->base + MMC_CMDAT);
196 writel(host->clkrt, host->base + MMC_CLKRT);
197
198 writel(START_CLOCK, host->base + MMC_STRPCL);
199
200 pxamci_enable_irq(host, END_CMD_RES);
201}
202
203static void pxamci_finish_request(struct pxamci_host *host, struct mmc_request *mrq)
204{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 host->mrq = NULL;
206 host->cmd = NULL;
207 host->data = NULL;
208 mmc_request_done(host->mmc, mrq);
209}
210
211static int pxamci_cmd_done(struct pxamci_host *host, unsigned int stat)
212{
213 struct mmc_command *cmd = host->cmd;
214 int i;
215 u32 v;
216
217 if (!cmd)
218 return 0;
219
220 host->cmd = NULL;
221
222 /*
223 * Did I mention this is Sick. We always need to
224 * discard the upper 8 bits of the first 16-bit word.
225 */
226 v = readl(host->base + MMC_RES) & 0xffff;
227 for (i = 0; i < 4; i++) {
228 u32 w1 = readl(host->base + MMC_RES) & 0xffff;
229 u32 w2 = readl(host->base + MMC_RES) & 0xffff;
230 cmd->resp[i] = v << 24 | w1 << 8 | w2 >> 8;
231 v = w2;
232 }
233
234 if (stat & STAT_TIME_OUT_RESPONSE) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200235 cmd->error = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 } else if (stat & STAT_RES_CRC_ERR && cmd->flags & MMC_RSP_CRC) {
237#ifdef CONFIG_PXA27x
238 /*
239 * workaround for erratum #42:
240 * Intel PXA27x Family Processor Specification Update Rev 001
Nicolas Pitre90e07d92007-05-13 18:03:08 +0200241 * A bogus CRC error can appear if the msb of a 136 bit
242 * response is a one.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 */
Nicolas Pitre90e07d92007-05-13 18:03:08 +0200244 if (cmd->flags & MMC_RSP_136 && cmd->resp[0] & 0x80000000) {
245 pr_debug("ignoring CRC from command %d - *risky*\n", cmd->opcode);
246 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247#endif
Pierre Ossman17b04292007-07-22 22:18:46 +0200248 cmd->error = -EILSEQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
250
251 pxamci_disable_irq(host, END_CMD_RES);
Pierre Ossman17b04292007-07-22 22:18:46 +0200252 if (host->data && !cmd->error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 pxamci_enable_irq(host, DATA_TRAN_DONE);
254 } else {
255 pxamci_finish_request(host, host->mrq);
256 }
257
258 return 1;
259}
260
261static int pxamci_data_done(struct pxamci_host *host, unsigned int stat)
262{
263 struct mmc_data *data = host->data;
264
265 if (!data)
266 return 0;
267
268 DCSR(host->dma) = 0;
269 dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->dma_len,
270 host->dma_dir);
271
272 if (stat & STAT_READ_TIME_OUT)
Pierre Ossman17b04292007-07-22 22:18:46 +0200273 data->error = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 else if (stat & (STAT_CRC_READ_ERROR|STAT_CRC_WRITE_ERROR))
Pierre Ossman17b04292007-07-22 22:18:46 +0200275 data->error = -EILSEQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 /*
278 * There appears to be a hardware design bug here. There seems to
279 * be no way to find out how much data was transferred to the card.
280 * This means that if there was an error on any block, we mark all
281 * data blocks as being in error.
282 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200283 if (!data->error)
Pavel Pisa2c171bf2006-05-19 21:48:03 +0100284 data->bytes_xfered = data->blocks * data->blksz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 else
286 data->bytes_xfered = 0;
287
288 pxamci_disable_irq(host, DATA_TRAN_DONE);
289
290 host->data = NULL;
Russell King58741e82006-05-02 20:02:39 +0100291 if (host->mrq->stop) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 pxamci_stop_clock(host);
Bridge Wudf456f42007-09-25 19:09:19 +0200293 pxamci_start_cmd(host, host->mrq->stop, host->cmdat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 } else {
295 pxamci_finish_request(host, host->mrq);
296 }
297
298 return 1;
299}
300
David Howells7d12e782006-10-05 14:55:46 +0100301static irqreturn_t pxamci_irq(int irq, void *devid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 struct pxamci_host *host = devid;
304 unsigned int ireg;
305 int handled = 0;
306
Bridge Wu81ab570f2007-09-25 18:59:07 +0200307 ireg = readl(host->base + MMC_I_REG) & ~readl(host->base + MMC_I_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (ireg) {
310 unsigned stat = readl(host->base + MMC_STAT);
311
Russell Kingd78e9072006-05-02 20:18:53 +0100312 pr_debug("PXAMCI: irq %08x stat %08x\n", ireg, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 if (ireg & END_CMD_RES)
315 handled |= pxamci_cmd_done(host, stat);
316 if (ireg & DATA_TRAN_DONE)
317 handled |= pxamci_data_done(host, stat);
Bridge Wu5d3ad4e2007-09-25 19:11:00 +0200318 if (ireg & SDIO_INT) {
319 mmc_signal_sdio_irq(host->mmc);
320 handled = 1;
321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 }
323
324 return IRQ_RETVAL(handled);
325}
326
327static void pxamci_request(struct mmc_host *mmc, struct mmc_request *mrq)
328{
329 struct pxamci_host *host = mmc_priv(mmc);
330 unsigned int cmdat;
331
332 WARN_ON(host->mrq != NULL);
333
334 host->mrq = mrq;
335
336 pxamci_stop_clock(host);
337
338 cmdat = host->cmdat;
339 host->cmdat &= ~CMDAT_INIT;
340
341 if (mrq->data) {
342 pxamci_setup_data(host, mrq->data);
343
344 cmdat &= ~CMDAT_BUSY;
345 cmdat |= CMDAT_DATAEN | CMDAT_DMAEN;
346 if (mrq->data->flags & MMC_DATA_WRITE)
347 cmdat |= CMDAT_WRITE;
348
349 if (mrq->data->flags & MMC_DATA_STREAM)
350 cmdat |= CMDAT_STREAM;
351 }
352
353 pxamci_start_cmd(host, mrq->cmd, cmdat);
354}
355
Richard Purdiee6195242005-09-06 15:18:56 -0700356static int pxamci_get_ro(struct mmc_host *mmc)
357{
358 struct pxamci_host *host = mmc_priv(mmc);
359
360 if (host->pdata && host->pdata->get_ro)
Sascha Hauer9e866192006-12-05 07:41:09 +0100361 return host->pdata->get_ro(mmc_dev(mmc));
Richard Purdiee6195242005-09-06 15:18:56 -0700362 /* Host doesn't support read only detection so assume writeable */
363 return 0;
364}
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366static void pxamci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
367{
368 struct pxamci_host *host = mmc_priv(mmc);
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (ios->clock) {
Russell Kingebebd9b2007-08-20 10:20:03 +0100371 unsigned long rate = host->clkrate;
372 unsigned int clk = rate / ios->clock;
373
374 /*
375 * clk might result in a lower divisor than we
376 * desire. check for that condition and adjust
377 * as appropriate.
378 */
379 if (rate / clk > ios->clock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 clk <<= 1;
381 host->clkrt = fls(clk) - 1;
Russell Kingebebd9b2007-08-20 10:20:03 +0100382 clk_enable(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 /*
385 * we write clkrt on the next command
386 */
387 } else {
388 pxamci_stop_clock(host);
Russell Kingebebd9b2007-08-20 10:20:03 +0100389 clk_disable(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
391
392 if (host->power_mode != ios->power_mode) {
393 host->power_mode = ios->power_mode;
394
395 if (host->pdata && host->pdata->setpower)
Sascha Hauer9e866192006-12-05 07:41:09 +0100396 host->pdata->setpower(mmc_dev(mmc), ios->vdd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 if (ios->power_mode == MMC_POWER_ON)
399 host->cmdat |= CMDAT_INIT;
400 }
401
Bridge Wudf456f42007-09-25 19:09:19 +0200402 if (ios->bus_width == MMC_BUS_WIDTH_4)
403 host->cmdat |= CMDAT_SD_4DAT;
404 else
405 host->cmdat &= ~CMDAT_SD_4DAT;
406
Russell Kingd78e9072006-05-02 20:18:53 +0100407 pr_debug("PXAMCI: clkrt = %x cmdat = %x\n",
Russell Kingc6563172006-03-29 09:30:20 +0100408 host->clkrt, host->cmdat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
410
Bridge Wu5d3ad4e2007-09-25 19:11:00 +0200411static void pxamci_enable_sdio_irq(struct mmc_host *host, int enable)
412{
413 struct pxamci_host *pxa_host = mmc_priv(host);
414
415 if (enable)
416 pxamci_enable_irq(pxa_host, SDIO_INT);
417 else
418 pxamci_disable_irq(pxa_host, SDIO_INT);
419}
420
David Brownellab7aefd2006-11-12 17:55:30 -0800421static const struct mmc_host_ops pxamci_ops = {
Bridge Wu5d3ad4e2007-09-25 19:11:00 +0200422 .request = pxamci_request,
423 .get_ro = pxamci_get_ro,
424 .set_ios = pxamci_set_ios,
425 .enable_sdio_irq = pxamci_enable_sdio_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426};
427
David Howells7d12e782006-10-05 14:55:46 +0100428static void pxamci_dma_irq(int dma, void *devid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Nicolas Pitrec7838372007-10-09 17:07:58 -0400430 struct pxamci_host *host = devid;
431 int dcsr = DCSR(dma);
432 DCSR(dma) = dcsr & ~DCSR_STOPIRQEN;
433
434 if (dcsr & DCSR_ENDINTR) {
435 writel(BUF_PART_FULL, host->base + MMC_PRTBUF);
436 } else {
437 printk(KERN_ERR "%s: DMA error on channel %d (DCSR=%#x)\n",
438 mmc_hostname(host->mmc), dma, dcsr);
439 host->data->error = -EIO;
440 pxamci_data_done(host, 0);
441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
David Howells7d12e782006-10-05 14:55:46 +0100444static irqreturn_t pxamci_detect_irq(int irq, void *devid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Richard Purdiec26971c2005-09-08 22:48:16 +0100446 struct pxamci_host *host = mmc_priv(devid);
447
448 mmc_detect_change(devid, host->pdata->detect_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return IRQ_HANDLED;
450}
451
Russell King3ae5eae2005-11-09 22:32:44 +0000452static int pxamci_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 struct mmc_host *mmc;
455 struct pxamci_host *host = NULL;
456 struct resource *r;
457 int ret, irq;
458
459 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
460 irq = platform_get_irq(pdev, 0);
David Vrabel48944732006-01-19 17:56:29 +0000461 if (!r || irq < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return -ENXIO;
463
464 r = request_mem_region(r->start, SZ_4K, DRIVER_NAME);
465 if (!r)
466 return -EBUSY;
467
Russell King3ae5eae2005-11-09 22:32:44 +0000468 mmc = mmc_alloc_host(sizeof(struct pxamci_host), &pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 if (!mmc) {
470 ret = -ENOMEM;
471 goto out;
472 }
473
474 mmc->ops = &pxamci_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 /*
477 * We can do SG-DMA, but we don't because we never know how much
478 * data we successfully wrote to the card.
479 */
480 mmc->max_phys_segs = NR_SG;
481
482 /*
483 * Our hardware DMA can handle a maximum of one page per SG entry.
484 */
485 mmc->max_seg_size = PAGE_SIZE;
486
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100487 /*
Nicolas Pitrefe2dc442007-09-24 15:47:18 -0400488 * Block length register is only 10 bits before PXA27x.
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100489 */
Nicolas Pitrefe2dc442007-09-24 15:47:18 -0400490 mmc->max_blk_size = (cpu_is_pxa21x() || cpu_is_pxa25x()) ? 1023 : 2048;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100491
Pierre Ossman55db8902006-11-21 17:55:45 +0100492 /*
493 * Block count register is 16 bits.
494 */
495 mmc->max_blk_count = 65535;
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 host = mmc_priv(mmc);
498 host->mmc = mmc;
499 host->dma = -1;
500 host->pdata = pdev->dev.platform_data;
Russell Kingebebd9b2007-08-20 10:20:03 +0100501
502 host->clk = clk_get(&pdev->dev, "MMCCLK");
503 if (IS_ERR(host->clk)) {
504 ret = PTR_ERR(host->clk);
505 host->clk = NULL;
506 goto out;
507 }
508
509 host->clkrate = clk_get_rate(host->clk);
510
511 /*
512 * Calculate minimum clock rate, rounding up.
513 */
514 mmc->f_min = (host->clkrate + 63) / 64;
515 mmc->f_max = host->clkrate;
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 mmc->ocr_avail = host->pdata ?
518 host->pdata->ocr_mask :
519 MMC_VDD_32_33|MMC_VDD_33_34;
Bridge Wudf456f42007-09-25 19:09:19 +0200520 mmc->caps = 0;
Bridge Wu5d3ad4e2007-09-25 19:11:00 +0200521 host->cmdat = 0;
522 if (!cpu_is_pxa21x() && !cpu_is_pxa25x()) {
523 mmc->caps |= MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
524 host->cmdat |= CMDAT_SDIO_INT_EN;
525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Russell King3ae5eae2005-11-09 22:32:44 +0000527 host->sg_cpu = dma_alloc_coherent(&pdev->dev, PAGE_SIZE, &host->sg_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if (!host->sg_cpu) {
529 ret = -ENOMEM;
530 goto out;
531 }
532
533 spin_lock_init(&host->lock);
534 host->res = r;
535 host->irq = irq;
536 host->imask = MMC_I_MASK_ALL;
537
538 host->base = ioremap(r->start, SZ_4K);
539 if (!host->base) {
540 ret = -ENOMEM;
541 goto out;
542 }
543
544 /*
545 * Ensure that the host controller is shut down, and setup
546 * with our defaults.
547 */
548 pxamci_stop_clock(host);
549 writel(0, host->base + MMC_SPI);
550 writel(64, host->base + MMC_RESTO);
551 writel(host->imask, host->base + MMC_I_MASK);
552
553 host->dma = pxa_request_dma(DRIVER_NAME, DMA_PRIO_LOW,
554 pxamci_dma_irq, host);
555 if (host->dma < 0) {
556 ret = -EBUSY;
557 goto out;
558 }
559
560 ret = request_irq(host->irq, pxamci_irq, 0, DRIVER_NAME, host);
561 if (ret)
562 goto out;
563
Russell King3ae5eae2005-11-09 22:32:44 +0000564 platform_set_drvdata(pdev, mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 if (host->pdata && host->pdata->init)
Russell King3ae5eae2005-11-09 22:32:44 +0000567 host->pdata->init(&pdev->dev, pxamci_detect_irq, mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 mmc_add_host(mmc);
570
571 return 0;
572
573 out:
574 if (host) {
575 if (host->dma >= 0)
576 pxa_free_dma(host->dma);
577 if (host->base)
578 iounmap(host->base);
579 if (host->sg_cpu)
Russell King3ae5eae2005-11-09 22:32:44 +0000580 dma_free_coherent(&pdev->dev, PAGE_SIZE, host->sg_cpu, host->sg_dma);
Russell Kingebebd9b2007-08-20 10:20:03 +0100581 if (host->clk)
582 clk_put(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
584 if (mmc)
585 mmc_free_host(mmc);
586 release_resource(r);
587 return ret;
588}
589
Russell King3ae5eae2005-11-09 22:32:44 +0000590static int pxamci_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Russell King3ae5eae2005-11-09 22:32:44 +0000592 struct mmc_host *mmc = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Russell King3ae5eae2005-11-09 22:32:44 +0000594 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 if (mmc) {
597 struct pxamci_host *host = mmc_priv(mmc);
598
599 if (host->pdata && host->pdata->exit)
Russell King3ae5eae2005-11-09 22:32:44 +0000600 host->pdata->exit(&pdev->dev, mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 mmc_remove_host(mmc);
603
604 pxamci_stop_clock(host);
605 writel(TXFIFO_WR_REQ|RXFIFO_RD_REQ|CLK_IS_OFF|STOP_CMD|
606 END_CMD_RES|PRG_DONE|DATA_TRAN_DONE,
607 host->base + MMC_I_MASK);
608
609 DRCMRRXMMC = 0;
610 DRCMRTXMMC = 0;
611
612 free_irq(host->irq, host);
613 pxa_free_dma(host->dma);
614 iounmap(host->base);
Russell King3ae5eae2005-11-09 22:32:44 +0000615 dma_free_coherent(&pdev->dev, PAGE_SIZE, host->sg_cpu, host->sg_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Russell Kingebebd9b2007-08-20 10:20:03 +0100617 clk_put(host->clk);
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 release_resource(host->res);
620
621 mmc_free_host(mmc);
622 }
623 return 0;
624}
625
626#ifdef CONFIG_PM
Russell King3ae5eae2005-11-09 22:32:44 +0000627static int pxamci_suspend(struct platform_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Russell King3ae5eae2005-11-09 22:32:44 +0000629 struct mmc_host *mmc = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 int ret = 0;
631
Russell King9480e302005-10-28 09:52:56 -0700632 if (mmc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 ret = mmc_suspend_host(mmc, state);
634
635 return ret;
636}
637
Russell King3ae5eae2005-11-09 22:32:44 +0000638static int pxamci_resume(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
Russell King3ae5eae2005-11-09 22:32:44 +0000640 struct mmc_host *mmc = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 int ret = 0;
642
Russell King9480e302005-10-28 09:52:56 -0700643 if (mmc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 ret = mmc_resume_host(mmc);
645
646 return ret;
647}
648#else
649#define pxamci_suspend NULL
650#define pxamci_resume NULL
651#endif
652
Russell King3ae5eae2005-11-09 22:32:44 +0000653static struct platform_driver pxamci_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 .probe = pxamci_probe,
655 .remove = pxamci_remove,
656 .suspend = pxamci_suspend,
657 .resume = pxamci_resume,
Russell King3ae5eae2005-11-09 22:32:44 +0000658 .driver = {
659 .name = DRIVER_NAME,
660 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661};
662
663static int __init pxamci_init(void)
664{
Russell King3ae5eae2005-11-09 22:32:44 +0000665 return platform_driver_register(&pxamci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667
668static void __exit pxamci_exit(void)
669{
Russell King3ae5eae2005-11-09 22:32:44 +0000670 platform_driver_unregister(&pxamci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
672
673module_init(pxamci_init);
674module_exit(pxamci_exit);
675
676MODULE_DESCRIPTION("PXA Multimedia Card Interface Driver");
677MODULE_LICENSE("GPL");