blob: 4a72772f4fe4d414a005ada528c037d4c6db93f4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Pierre Ossman70f10482007-07-11 20:04:50 +02002 * linux/drivers/mmc/host/mmci.c - ARM PrimeCell MMCI PL180/1 driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 2003 Deep Blue Solutions, Ltd, 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/module.h>
11#include <linux/moduleparam.h>
12#include <linux/init.h>
13#include <linux/ioport.h>
14#include <linux/device.h>
15#include <linux/interrupt.h>
16#include <linux/delay.h>
17#include <linux/err.h>
18#include <linux/highmem.h>
19#include <linux/mmc/host.h>
Russell Kinga62c80e2006-01-07 13:52:45 +000020#include <linux/amba/bus.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000021#include <linux/clk.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Russell Kinge9c091b2006-01-04 16:24:05 +000023#include <asm/cacheflush.h>
Russell King7b09cda2005-07-01 12:02:59 +010024#include <asm/div64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/scatterlist.h>
Russell Kingc6b8fda2005-10-28 14:05:16 +010027#include <asm/sizes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/mach/mmc.h>
29
30#include "mmci.h"
31
32#define DRIVER_NAME "mmci-pl18x"
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#define DBG(host,fmt,args...) \
Russell Kingd366b642005-08-19 09:40:08 +010035 pr_debug("%s: %s: " fmt, mmc_hostname(host->mmc), __func__ , args)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37static unsigned int fmax = 515633;
38
39static void
40mmci_request_end(struct mmci_host *host, struct mmc_request *mrq)
41{
42 writel(0, host->base + MMCICOMMAND);
43
Russell Kinge47c2222007-01-08 16:42:51 +000044 BUG_ON(host->data);
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 host->mrq = NULL;
47 host->cmd = NULL;
48
49 if (mrq->data)
50 mrq->data->bytes_xfered = host->data_xfered;
51
52 /*
53 * Need to drop the host lock here; mmc_request_done may call
54 * back into the driver...
55 */
56 spin_unlock(&host->lock);
57 mmc_request_done(host->mmc, mrq);
58 spin_lock(&host->lock);
59}
60
61static void mmci_stop_data(struct mmci_host *host)
62{
63 writel(0, host->base + MMCIDATACTRL);
64 writel(0, host->base + MMCIMASK1);
65 host->data = NULL;
66}
67
68static void mmci_start_data(struct mmci_host *host, struct mmc_data *data)
69{
70 unsigned int datactrl, timeout, irqmask;
Russell King7b09cda2005-07-01 12:02:59 +010071 unsigned long long clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 void __iomem *base;
Russell King3bc87f22006-08-27 13:51:28 +010073 int blksz_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 DBG(host, "blksz %04x blks %04x flags %08x\n",
Russell King3bc87f22006-08-27 13:51:28 +010076 data->blksz, data->blocks, data->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 host->data = data;
Russell King3bc87f22006-08-27 13:51:28 +010079 host->size = data->blksz;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 host->data_xfered = 0;
81
82 mmci_init_sg(host, data);
83
Russell King7b09cda2005-07-01 12:02:59 +010084 clks = (unsigned long long)data->timeout_ns * host->cclk;
85 do_div(clks, 1000000000UL);
86
87 timeout = data->timeout_clks + (unsigned int)clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 base = host->base;
90 writel(timeout, base + MMCIDATATIMER);
91 writel(host->size, base + MMCIDATALENGTH);
92
Russell King3bc87f22006-08-27 13:51:28 +010093 blksz_bits = ffs(data->blksz) - 1;
94 BUG_ON(1 << blksz_bits != data->blksz);
95
96 datactrl = MCI_DPSM_ENABLE | blksz_bits << 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 if (data->flags & MMC_DATA_READ) {
98 datactrl |= MCI_DPSM_DIRECTION;
99 irqmask = MCI_RXFIFOHALFFULLMASK;
Russell King0425a142006-02-16 16:48:31 +0000100
101 /*
102 * If we have less than a FIFOSIZE of bytes to transfer,
103 * trigger a PIO interrupt as soon as any data is available.
104 */
105 if (host->size < MCI_FIFOSIZE)
106 irqmask |= MCI_RXDATAAVLBLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 } else {
108 /*
109 * We don't actually need to include "FIFO empty" here
110 * since its implicit in "FIFO half empty".
111 */
112 irqmask = MCI_TXFIFOHALFEMPTYMASK;
113 }
114
115 writel(datactrl, base + MMCIDATACTRL);
116 writel(readl(base + MMCIMASK0) & ~MCI_DATAENDMASK, base + MMCIMASK0);
117 writel(irqmask, base + MMCIMASK1);
118}
119
120static void
121mmci_start_command(struct mmci_host *host, struct mmc_command *cmd, u32 c)
122{
123 void __iomem *base = host->base;
124
125 DBG(host, "op %02x arg %08x flags %08x\n",
126 cmd->opcode, cmd->arg, cmd->flags);
127
128 if (readl(base + MMCICOMMAND) & MCI_CPSM_ENABLE) {
129 writel(0, base + MMCICOMMAND);
130 udelay(1);
131 }
132
133 c |= cmd->opcode | MCI_CPSM_ENABLE;
Russell Kinge9225172006-02-02 12:23:12 +0000134 if (cmd->flags & MMC_RSP_PRESENT) {
135 if (cmd->flags & MMC_RSP_136)
136 c |= MCI_CPSM_LONGRSP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 c |= MCI_CPSM_RESPONSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 }
139 if (/*interrupt*/0)
140 c |= MCI_CPSM_INTERRUPT;
141
142 host->cmd = cmd;
143
144 writel(cmd->arg, base + MMCIARGUMENT);
145 writel(c, base + MMCICOMMAND);
146}
147
148static void
149mmci_data_irq(struct mmci_host *host, struct mmc_data *data,
150 unsigned int status)
151{
152 if (status & MCI_DATABLOCKEND) {
Russell King3bc87f22006-08-27 13:51:28 +0100153 host->data_xfered += data->blksz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 }
155 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|MCI_RXOVERRUN)) {
156 if (status & MCI_DATACRCFAIL)
Pierre Ossman17b04292007-07-22 22:18:46 +0200157 data->error = -EILSEQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 else if (status & MCI_DATATIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +0200159 data->error = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 else if (status & (MCI_TXUNDERRUN|MCI_RXOVERRUN))
Pierre Ossman17b04292007-07-22 22:18:46 +0200161 data->error = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 status |= MCI_DATAEND;
Russell Kinge9c091b2006-01-04 16:24:05 +0000163
164 /*
165 * We hit an error condition. Ensure that any data
166 * partially written to a page is properly coherent.
167 */
168 if (host->sg_len && data->flags & MMC_DATA_READ)
169 flush_dcache_page(host->sg_ptr->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 }
171 if (status & MCI_DATAEND) {
172 mmci_stop_data(host);
173
174 if (!data->stop) {
175 mmci_request_end(host, data->mrq);
176 } else {
177 mmci_start_command(host, data->stop, 0);
178 }
179 }
180}
181
182static void
183mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
184 unsigned int status)
185{
186 void __iomem *base = host->base;
187
188 host->cmd = NULL;
189
190 cmd->resp[0] = readl(base + MMCIRESPONSE0);
191 cmd->resp[1] = readl(base + MMCIRESPONSE1);
192 cmd->resp[2] = readl(base + MMCIRESPONSE2);
193 cmd->resp[3] = readl(base + MMCIRESPONSE3);
194
195 if (status & MCI_CMDTIMEOUT) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200196 cmd->error = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200198 cmd->error = -EILSEQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
200
Pierre Ossman17b04292007-07-22 22:18:46 +0200201 if (!cmd->data || cmd->error) {
Russell Kinge47c2222007-01-08 16:42:51 +0000202 if (host->data)
203 mmci_stop_data(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 mmci_request_end(host, cmd->mrq);
205 } else if (!(cmd->data->flags & MMC_DATA_READ)) {
206 mmci_start_data(host, cmd->data);
207 }
208}
209
210static int mmci_pio_read(struct mmci_host *host, char *buffer, unsigned int remain)
211{
212 void __iomem *base = host->base;
213 char *ptr = buffer;
214 u32 status;
215
216 do {
217 int count = host->size - (readl(base + MMCIFIFOCNT) << 2);
218
219 if (count > remain)
220 count = remain;
221
222 if (count <= 0)
223 break;
224
225 readsl(base + MMCIFIFO, ptr, count >> 2);
226
227 ptr += count;
228 remain -= count;
229
230 if (remain == 0)
231 break;
232
233 status = readl(base + MMCISTATUS);
234 } while (status & MCI_RXDATAAVLBL);
235
236 return ptr - buffer;
237}
238
239static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status)
240{
241 void __iomem *base = host->base;
242 char *ptr = buffer;
243
244 do {
245 unsigned int count, maxcnt;
246
247 maxcnt = status & MCI_TXFIFOEMPTY ? MCI_FIFOSIZE : MCI_FIFOHALFSIZE;
248 count = min(remain, maxcnt);
249
250 writesl(base + MMCIFIFO, ptr, count >> 2);
251
252 ptr += count;
253 remain -= count;
254
255 if (remain == 0)
256 break;
257
258 status = readl(base + MMCISTATUS);
259 } while (status & MCI_TXFIFOHALFEMPTY);
260
261 return ptr - buffer;
262}
263
264/*
265 * PIO data transfer IRQ handler.
266 */
David Howells7d12e782006-10-05 14:55:46 +0100267static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
269 struct mmci_host *host = dev_id;
270 void __iomem *base = host->base;
271 u32 status;
272
273 status = readl(base + MMCISTATUS);
274
275 DBG(host, "irq1 %08x\n", status);
276
277 do {
278 unsigned long flags;
279 unsigned int remain, len;
280 char *buffer;
281
282 /*
283 * For write, we only need to test the half-empty flag
284 * here - if the FIFO is completely empty, then by
285 * definition it is more than half empty.
286 *
287 * For read, check for data available.
288 */
289 if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL)))
290 break;
291
292 /*
293 * Map the current scatter buffer.
294 */
295 buffer = mmci_kmap_atomic(host, &flags) + host->sg_off;
296 remain = host->sg_ptr->length - host->sg_off;
297
298 len = 0;
299 if (status & MCI_RXACTIVE)
300 len = mmci_pio_read(host, buffer, remain);
301 if (status & MCI_TXACTIVE)
302 len = mmci_pio_write(host, buffer, remain, status);
303
304 /*
305 * Unmap the buffer.
306 */
Evgeniy Polyakovf3e26282006-01-05 10:31:23 +0000307 mmci_kunmap_atomic(host, buffer, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 host->sg_off += len;
310 host->size -= len;
311 remain -= len;
312
313 if (remain)
314 break;
315
Russell Kinge9c091b2006-01-04 16:24:05 +0000316 /*
317 * If we were reading, and we have completed this
318 * page, ensure that the data cache is coherent.
319 */
320 if (status & MCI_RXACTIVE)
321 flush_dcache_page(host->sg_ptr->page);
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 if (!mmci_next_sg(host))
324 break;
325
326 status = readl(base + MMCISTATUS);
327 } while (1);
328
329 /*
330 * If we're nearing the end of the read, switch to
331 * "any data available" mode.
332 */
333 if (status & MCI_RXACTIVE && host->size < MCI_FIFOSIZE)
334 writel(MCI_RXDATAAVLBLMASK, base + MMCIMASK1);
335
336 /*
337 * If we run out of data, disable the data IRQs; this
338 * prevents a race where the FIFO becomes empty before
339 * the chip itself has disabled the data path, and
340 * stops us racing with our data end IRQ.
341 */
342 if (host->size == 0) {
343 writel(0, base + MMCIMASK1);
344 writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0);
345 }
346
347 return IRQ_HANDLED;
348}
349
350/*
351 * Handle completion of command and data transfers.
352 */
David Howells7d12e782006-10-05 14:55:46 +0100353static irqreturn_t mmci_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
355 struct mmci_host *host = dev_id;
356 u32 status;
357 int ret = 0;
358
359 spin_lock(&host->lock);
360
361 do {
362 struct mmc_command *cmd;
363 struct mmc_data *data;
364
365 status = readl(host->base + MMCISTATUS);
366 status &= readl(host->base + MMCIMASK0);
367 writel(status, host->base + MMCICLEAR);
368
369 DBG(host, "irq0 %08x\n", status);
370
371 data = host->data;
372 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|
373 MCI_RXOVERRUN|MCI_DATAEND|MCI_DATABLOCKEND) && data)
374 mmci_data_irq(host, data, status);
375
376 cmd = host->cmd;
377 if (status & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT|MCI_CMDSENT|MCI_CMDRESPEND) && cmd)
378 mmci_cmd_irq(host, cmd, status);
379
380 ret = 1;
381 } while (status);
382
383 spin_unlock(&host->lock);
384
385 return IRQ_RETVAL(ret);
386}
387
388static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
389{
390 struct mmci_host *host = mmc_priv(mmc);
391
392 WARN_ON(host->mrq != NULL);
393
Pierre Ossman255d01a2007-07-24 20:38:53 +0200394 if (mrq->data && (hweight32(mrq->data->blksz) > 1)) {
395 printk(KERN_ERR "%s: Unsupported block size (%d bytes)\n",
396 mmc_hostname(mmc), mrq->data->blksz);
397 mrq->cmd->error = -EINVAL;
398 mmc_request_done(mmc, mrq);
399 return;
400 }
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 spin_lock_irq(&host->lock);
403
404 host->mrq = mrq;
405
406 if (mrq->data && mrq->data->flags & MMC_DATA_READ)
407 mmci_start_data(host, mrq->data);
408
409 mmci_start_command(host, mrq->cmd, 0);
410
411 spin_unlock_irq(&host->lock);
412}
413
414static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
415{
416 struct mmci_host *host = mmc_priv(mmc);
417 u32 clk = 0, pwr = 0;
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (ios->clock) {
420 if (ios->clock >= host->mclk) {
421 clk = MCI_CLK_BYPASS;
422 host->cclk = host->mclk;
423 } else {
424 clk = host->mclk / (2 * ios->clock) - 1;
425 if (clk > 256)
426 clk = 255;
427 host->cclk = host->mclk / (2 * (clk + 1));
428 }
429 clk |= MCI_CLK_ENABLE;
430 }
431
432 if (host->plat->translate_vdd)
433 pwr |= host->plat->translate_vdd(mmc_dev(mmc), ios->vdd);
434
435 switch (ios->power_mode) {
436 case MMC_POWER_OFF:
437 break;
438 case MMC_POWER_UP:
439 pwr |= MCI_PWR_UP;
440 break;
441 case MMC_POWER_ON:
442 pwr |= MCI_PWR_ON;
443 break;
444 }
445
446 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
447 pwr |= MCI_ROD;
448
449 writel(clk, host->base + MMCICLOCK);
450
451 if (host->pwr != pwr) {
452 host->pwr = pwr;
453 writel(pwr, host->base + MMCIPOWER);
454 }
455}
456
David Brownellab7aefd2006-11-12 17:55:30 -0800457static const struct mmc_host_ops mmci_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 .request = mmci_request,
459 .set_ios = mmci_set_ios,
460};
461
462static void mmci_check_status(unsigned long data)
463{
464 struct mmci_host *host = (struct mmci_host *)data;
465 unsigned int status;
466
467 status = host->plat->status(mmc_dev(host->mmc));
468 if (status ^ host->oldstat)
Richard Purdie8dc00332005-09-08 17:53:01 +0100469 mmc_detect_change(host->mmc, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 host->oldstat = status;
472 mod_timer(&host->timer, jiffies + HZ);
473}
474
475static int mmci_probe(struct amba_device *dev, void *id)
476{
477 struct mmc_platform_data *plat = dev->dev.platform_data;
478 struct mmci_host *host;
479 struct mmc_host *mmc;
480 int ret;
481
482 /* must have platform data */
483 if (!plat) {
484 ret = -EINVAL;
485 goto out;
486 }
487
488 ret = amba_request_regions(dev, DRIVER_NAME);
489 if (ret)
490 goto out;
491
492 mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
493 if (!mmc) {
494 ret = -ENOMEM;
495 goto rel_regions;
496 }
497
498 host = mmc_priv(mmc);
499 host->clk = clk_get(&dev->dev, "MCLK");
500 if (IS_ERR(host->clk)) {
501 ret = PTR_ERR(host->clk);
502 host->clk = NULL;
503 goto host_free;
504 }
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 ret = clk_enable(host->clk);
507 if (ret)
Russell Kinga8d35842006-01-03 18:41:37 +0000508 goto clk_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 host->plat = plat;
511 host->mclk = clk_get_rate(host->clk);
512 host->mmc = mmc;
513 host->base = ioremap(dev->res.start, SZ_4K);
514 if (!host->base) {
515 ret = -ENOMEM;
516 goto clk_disable;
517 }
518
519 mmc->ops = &mmci_ops;
520 mmc->f_min = (host->mclk + 511) / 512;
521 mmc->f_max = min(host->mclk, fmax);
522 mmc->ocr_avail = plat->ocr_mask;
Russell Kingdb53f282006-08-30 15:14:56 +0100523 mmc->caps = MMC_CAP_MULTIWRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 /*
526 * We can do SGIO
527 */
528 mmc->max_hw_segs = 16;
529 mmc->max_phys_segs = NR_SG;
530
531 /*
532 * Since we only have a 16-bit data length register, we must
533 * ensure that we don't exceed 2^16-1 bytes in a single request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 */
Pierre Ossman55db8902006-11-21 17:55:45 +0100535 mmc->max_req_size = 65535;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 /*
538 * Set the maximum segment size. Since we aren't doing DMA
539 * (yet) we are only limited by the data length register.
540 */
Pierre Ossman55db8902006-11-21 17:55:45 +0100541 mmc->max_seg_size = mmc->max_req_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100543 /*
544 * Block size can be up to 2048 bytes, but must be a power of two.
545 */
546 mmc->max_blk_size = 2048;
547
Pierre Ossman55db8902006-11-21 17:55:45 +0100548 /*
549 * No limit on the number of blocks transferred.
550 */
551 mmc->max_blk_count = mmc->max_req_size;
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 spin_lock_init(&host->lock);
554
555 writel(0, host->base + MMCIMASK0);
556 writel(0, host->base + MMCIMASK1);
557 writel(0xfff, host->base + MMCICLEAR);
558
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700559 ret = request_irq(dev->irq[0], mmci_irq, IRQF_SHARED, DRIVER_NAME " (cmd)", host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (ret)
561 goto unmap;
562
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700563 ret = request_irq(dev->irq[1], mmci_pio_irq, IRQF_SHARED, DRIVER_NAME " (pio)", host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 if (ret)
565 goto irq0_free;
566
567 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
568
569 amba_set_drvdata(dev, mmc);
570
571 mmc_add_host(mmc);
572
Greg Kroah-Hartmane29419f2006-06-12 15:20:16 -0700573 printk(KERN_INFO "%s: MMCI rev %x cfg %02x at 0x%016llx irq %d,%d\n",
Russell Kingd366b642005-08-19 09:40:08 +0100574 mmc_hostname(mmc), amba_rev(dev), amba_config(dev),
Greg Kroah-Hartmane29419f2006-06-12 15:20:16 -0700575 (unsigned long long)dev->res.start, dev->irq[0], dev->irq[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 init_timer(&host->timer);
578 host->timer.data = (unsigned long)host;
579 host->timer.function = mmci_check_status;
580 host->timer.expires = jiffies + HZ;
581 add_timer(&host->timer);
582
583 return 0;
584
585 irq0_free:
586 free_irq(dev->irq[0], host);
587 unmap:
588 iounmap(host->base);
589 clk_disable:
590 clk_disable(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 clk_free:
592 clk_put(host->clk);
593 host_free:
594 mmc_free_host(mmc);
595 rel_regions:
596 amba_release_regions(dev);
597 out:
598 return ret;
599}
600
601static int mmci_remove(struct amba_device *dev)
602{
603 struct mmc_host *mmc = amba_get_drvdata(dev);
604
605 amba_set_drvdata(dev, NULL);
606
607 if (mmc) {
608 struct mmci_host *host = mmc_priv(mmc);
609
610 del_timer_sync(&host->timer);
611
612 mmc_remove_host(mmc);
613
614 writel(0, host->base + MMCIMASK0);
615 writel(0, host->base + MMCIMASK1);
616
617 writel(0, host->base + MMCICOMMAND);
618 writel(0, host->base + MMCIDATACTRL);
619
620 free_irq(dev->irq[0], host);
621 free_irq(dev->irq[1], host);
622
623 iounmap(host->base);
624 clk_disable(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 clk_put(host->clk);
626
627 mmc_free_host(mmc);
628
629 amba_release_regions(dev);
630 }
631
632 return 0;
633}
634
635#ifdef CONFIG_PM
Pavel Macheke5378ca2005-04-16 15:25:29 -0700636static int mmci_suspend(struct amba_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
638 struct mmc_host *mmc = amba_get_drvdata(dev);
639 int ret = 0;
640
641 if (mmc) {
642 struct mmci_host *host = mmc_priv(mmc);
643
644 ret = mmc_suspend_host(mmc, state);
645 if (ret == 0)
646 writel(0, host->base + MMCIMASK0);
647 }
648
649 return ret;
650}
651
652static int mmci_resume(struct amba_device *dev)
653{
654 struct mmc_host *mmc = amba_get_drvdata(dev);
655 int ret = 0;
656
657 if (mmc) {
658 struct mmci_host *host = mmc_priv(mmc);
659
660 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
661
662 ret = mmc_resume_host(mmc);
663 }
664
665 return ret;
666}
667#else
668#define mmci_suspend NULL
669#define mmci_resume NULL
670#endif
671
672static struct amba_id mmci_ids[] = {
673 {
674 .id = 0x00041180,
675 .mask = 0x000fffff,
676 },
677 {
678 .id = 0x00041181,
679 .mask = 0x000fffff,
680 },
681 { 0, 0 },
682};
683
684static struct amba_driver mmci_driver = {
685 .drv = {
686 .name = DRIVER_NAME,
687 },
688 .probe = mmci_probe,
689 .remove = mmci_remove,
690 .suspend = mmci_suspend,
691 .resume = mmci_resume,
692 .id_table = mmci_ids,
693};
694
695static int __init mmci_init(void)
696{
697 return amba_driver_register(&mmci_driver);
698}
699
700static void __exit mmci_exit(void)
701{
702 amba_driver_unregister(&mmci_driver);
703}
704
705module_init(mmci_init);
706module_exit(mmci_exit);
707module_param(fmax, uint, 0444);
708
709MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
710MODULE_LICENSE("GPL");