blob: 976c9d0e8080a0059bca72cc0e4b3bdd0d801fc5 [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.
Linus Walleij64de0282010-02-19 01:09:10 +01005 * Copyright (C) 2010 ST-Ericsson AB.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
12#include <linux/moduleparam.h>
13#include <linux/init.h>
14#include <linux/ioport.h>
15#include <linux/device.h>
16#include <linux/interrupt.h>
17#include <linux/delay.h>
18#include <linux/err.h>
19#include <linux/highmem.h>
Nicolas Pitre019a5f52007-10-11 01:06:03 -040020#include <linux/log2.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/mmc/host.h>
Russell Kinga62c80e2006-01-07 13:52:45 +000022#include <linux/amba/bus.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000023#include <linux/clk.h>
Jens Axboebd6dee62007-10-24 09:01:09 +020024#include <linux/scatterlist.h>
Russell King89001442009-07-09 15:16:07 +010025#include <linux/gpio.h>
Linus Walleij6ef297f2009-09-22 14:29:36 +010026#include <linux/amba/mmci.h>
Linus Walleij34e84f32009-09-22 14:41:40 +010027#include <linux/regulator/consumer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Russell King7b09cda2005-07-01 12:02:59 +010029#include <asm/div64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/io.h>
Russell Kingc6b8fda2005-10-28 14:05:16 +010031#include <asm/sizes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include "mmci.h"
34
35#define DRIVER_NAME "mmci-pl18x"
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037static unsigned int fmax = 515633;
38
Rabin Vincent4956e102010-07-21 12:54:40 +010039/**
40 * struct variant_data - MMCI variant-specific quirks
41 * @clkreg: default value for MCICLOCK register
Rabin Vincent4380c142010-07-21 12:55:18 +010042 * @clkreg_enable: enable value for MMCICLOCK register
Rabin Vincent08458ef2010-07-21 12:55:59 +010043 * @datalength_bits: number of bits in the MMCIDATALENGTH register
Rabin Vincent8301bb62010-08-09 12:57:30 +010044 * @fifosize: number of bytes that can be written when MMCI_TXFIFOEMPTY
45 * is asserted (likewise for RX)
46 * @fifohalfsize: number of bytes that can be written when MCI_TXFIFOHALFEMPTY
47 * is asserted (likewise for RX)
Linus Walleijf20f8f22010-10-19 13:41:24 +010048 * @broken_blockend: the MCI_DATABLOCKEND is broken on the hardware
49 * and will not work at all.
50 * @broken_blockend_dma: the MCI_DATABLOCKEND is broken on the hardware when
51 * using DMA.
Rabin Vincent4956e102010-07-21 12:54:40 +010052 */
53struct variant_data {
54 unsigned int clkreg;
Rabin Vincent4380c142010-07-21 12:55:18 +010055 unsigned int clkreg_enable;
Rabin Vincent08458ef2010-07-21 12:55:59 +010056 unsigned int datalength_bits;
Rabin Vincent8301bb62010-08-09 12:57:30 +010057 unsigned int fifosize;
58 unsigned int fifohalfsize;
Linus Walleijf20f8f22010-10-19 13:41:24 +010059 bool broken_blockend;
60 bool broken_blockend_dma;
Rabin Vincent4956e102010-07-21 12:54:40 +010061};
62
63static struct variant_data variant_arm = {
Rabin Vincent8301bb62010-08-09 12:57:30 +010064 .fifosize = 16 * 4,
65 .fifohalfsize = 8 * 4,
Rabin Vincent08458ef2010-07-21 12:55:59 +010066 .datalength_bits = 16,
Rabin Vincent4956e102010-07-21 12:54:40 +010067};
68
69static struct variant_data variant_u300 = {
Rabin Vincent8301bb62010-08-09 12:57:30 +010070 .fifosize = 16 * 4,
71 .fifohalfsize = 8 * 4,
Rabin Vincent4380c142010-07-21 12:55:18 +010072 .clkreg_enable = 1 << 13, /* HWFCEN */
Rabin Vincent08458ef2010-07-21 12:55:59 +010073 .datalength_bits = 16,
Linus Walleijf20f8f22010-10-19 13:41:24 +010074 .broken_blockend_dma = true,
Rabin Vincent4956e102010-07-21 12:54:40 +010075};
76
77static struct variant_data variant_ux500 = {
Rabin Vincent8301bb62010-08-09 12:57:30 +010078 .fifosize = 30 * 4,
79 .fifohalfsize = 8 * 4,
Rabin Vincent4956e102010-07-21 12:54:40 +010080 .clkreg = MCI_CLK_ENABLE,
Rabin Vincent4380c142010-07-21 12:55:18 +010081 .clkreg_enable = 1 << 14, /* HWFCEN */
Rabin Vincent08458ef2010-07-21 12:55:59 +010082 .datalength_bits = 24,
Linus Walleijf20f8f22010-10-19 13:41:24 +010083 .broken_blockend = true,
Rabin Vincent4956e102010-07-21 12:54:40 +010084};
Linus Walleija6a64642009-09-14 12:56:14 +010085/*
86 * This must be called with host->lock held
87 */
88static void mmci_set_clkreg(struct mmci_host *host, unsigned int desired)
89{
Rabin Vincent4956e102010-07-21 12:54:40 +010090 struct variant_data *variant = host->variant;
91 u32 clk = variant->clkreg;
Linus Walleija6a64642009-09-14 12:56:14 +010092
93 if (desired) {
94 if (desired >= host->mclk) {
95 clk = MCI_CLK_BYPASS;
96 host->cclk = host->mclk;
97 } else {
98 clk = host->mclk / (2 * desired) - 1;
99 if (clk >= 256)
100 clk = 255;
101 host->cclk = host->mclk / (2 * (clk + 1));
102 }
Rabin Vincent4380c142010-07-21 12:55:18 +0100103
104 clk |= variant->clkreg_enable;
Linus Walleija6a64642009-09-14 12:56:14 +0100105 clk |= MCI_CLK_ENABLE;
106 /* This hasn't proven to be worthwhile */
107 /* clk |= MCI_CLK_PWRSAVE; */
108 }
109
Linus Walleij9e6c82c2009-09-14 12:57:11 +0100110 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4)
Linus Walleij771dc152010-04-08 07:38:52 +0100111 clk |= MCI_4BIT_BUS;
112 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_8)
113 clk |= MCI_ST_8BIT_BUS;
Linus Walleij9e6c82c2009-09-14 12:57:11 +0100114
Linus Walleija6a64642009-09-14 12:56:14 +0100115 writel(clk, host->base + MMCICLOCK);
116}
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118static void
119mmci_request_end(struct mmci_host *host, struct mmc_request *mrq)
120{
121 writel(0, host->base + MMCICOMMAND);
122
Russell Kinge47c2222007-01-08 16:42:51 +0000123 BUG_ON(host->data);
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 host->mrq = NULL;
126 host->cmd = NULL;
127
128 if (mrq->data)
129 mrq->data->bytes_xfered = host->data_xfered;
130
131 /*
132 * Need to drop the host lock here; mmc_request_done may call
133 * back into the driver...
134 */
135 spin_unlock(&host->lock);
136 mmc_request_done(host->mmc, mrq);
137 spin_lock(&host->lock);
138}
139
Linus Walleij2686b4b2010-10-19 12:39:48 +0100140static void mmci_set_mask1(struct mmci_host *host, unsigned int mask)
141{
142 void __iomem *base = host->base;
143
144 if (host->singleirq) {
145 unsigned int mask0 = readl(base + MMCIMASK0);
146
147 mask0 &= ~MCI_IRQ1MASK;
148 mask0 |= mask;
149
150 writel(mask0, base + MMCIMASK0);
151 }
152
153 writel(mask, base + MMCIMASK1);
154}
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156static void mmci_stop_data(struct mmci_host *host)
157{
158 writel(0, host->base + MMCIDATACTRL);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100159 mmci_set_mask1(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 host->data = NULL;
161}
162
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100163static void mmci_init_sg(struct mmci_host *host, struct mmc_data *data)
164{
165 unsigned int flags = SG_MITER_ATOMIC;
166
167 if (data->flags & MMC_DATA_READ)
168 flags |= SG_MITER_TO_SG;
169 else
170 flags |= SG_MITER_FROM_SG;
171
172 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
173}
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175static void mmci_start_data(struct mmci_host *host, struct mmc_data *data)
176{
Rabin Vincent8301bb62010-08-09 12:57:30 +0100177 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 unsigned int datactrl, timeout, irqmask;
Russell King7b09cda2005-07-01 12:02:59 +0100179 unsigned long long clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 void __iomem *base;
Russell King3bc87f22006-08-27 13:51:28 +0100181 int blksz_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Linus Walleij64de0282010-02-19 01:09:10 +0100183 dev_dbg(mmc_dev(host->mmc), "blksz %04x blks %04x flags %08x\n",
184 data->blksz, data->blocks, data->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 host->data = data;
Rabin Vincent528320d2010-07-21 12:49:49 +0100187 host->size = data->blksz * data->blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 host->data_xfered = 0;
Linus Walleijf20f8f22010-10-19 13:41:24 +0100189 host->blockend = false;
190 host->dataend = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 mmci_init_sg(host, data);
193
Russell King7b09cda2005-07-01 12:02:59 +0100194 clks = (unsigned long long)data->timeout_ns * host->cclk;
195 do_div(clks, 1000000000UL);
196
197 timeout = data->timeout_clks + (unsigned int)clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 base = host->base;
200 writel(timeout, base + MMCIDATATIMER);
201 writel(host->size, base + MMCIDATALENGTH);
202
Russell King3bc87f22006-08-27 13:51:28 +0100203 blksz_bits = ffs(data->blksz) - 1;
204 BUG_ON(1 << blksz_bits != data->blksz);
205
206 datactrl = MCI_DPSM_ENABLE | blksz_bits << 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if (data->flags & MMC_DATA_READ) {
208 datactrl |= MCI_DPSM_DIRECTION;
209 irqmask = MCI_RXFIFOHALFFULLMASK;
Russell King0425a142006-02-16 16:48:31 +0000210
211 /*
212 * If we have less than a FIFOSIZE of bytes to transfer,
213 * trigger a PIO interrupt as soon as any data is available.
214 */
Rabin Vincent8301bb62010-08-09 12:57:30 +0100215 if (host->size < variant->fifosize)
Russell King0425a142006-02-16 16:48:31 +0000216 irqmask |= MCI_RXDATAAVLBLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 } else {
218 /*
219 * We don't actually need to include "FIFO empty" here
220 * since its implicit in "FIFO half empty".
221 */
222 irqmask = MCI_TXFIFOHALFEMPTYMASK;
223 }
224
225 writel(datactrl, base + MMCIDATACTRL);
226 writel(readl(base + MMCIMASK0) & ~MCI_DATAENDMASK, base + MMCIMASK0);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100227 mmci_set_mask1(host, irqmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
230static void
231mmci_start_command(struct mmci_host *host, struct mmc_command *cmd, u32 c)
232{
233 void __iomem *base = host->base;
234
Linus Walleij64de0282010-02-19 01:09:10 +0100235 dev_dbg(mmc_dev(host->mmc), "op %02x arg %08x flags %08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 cmd->opcode, cmd->arg, cmd->flags);
237
238 if (readl(base + MMCICOMMAND) & MCI_CPSM_ENABLE) {
239 writel(0, base + MMCICOMMAND);
240 udelay(1);
241 }
242
243 c |= cmd->opcode | MCI_CPSM_ENABLE;
Russell Kinge9225172006-02-02 12:23:12 +0000244 if (cmd->flags & MMC_RSP_PRESENT) {
245 if (cmd->flags & MMC_RSP_136)
246 c |= MCI_CPSM_LONGRSP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 c |= MCI_CPSM_RESPONSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
249 if (/*interrupt*/0)
250 c |= MCI_CPSM_INTERRUPT;
251
252 host->cmd = cmd;
253
254 writel(cmd->arg, base + MMCIARGUMENT);
255 writel(c, base + MMCICOMMAND);
256}
257
258static void
259mmci_data_irq(struct mmci_host *host, struct mmc_data *data,
260 unsigned int status)
261{
Linus Walleijf20f8f22010-10-19 13:41:24 +0100262 struct variant_data *variant = host->variant;
263
264 /* First check for errors */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|MCI_RXOVERRUN)) {
Linus Walleij64de0282010-02-19 01:09:10 +0100266 dev_dbg(mmc_dev(host->mmc), "MCI ERROR IRQ (status %08x)\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if (status & MCI_DATACRCFAIL)
Pierre Ossman17b04292007-07-22 22:18:46 +0200268 data->error = -EILSEQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 else if (status & MCI_DATATIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +0200270 data->error = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 else if (status & (MCI_TXUNDERRUN|MCI_RXOVERRUN))
Pierre Ossman17b04292007-07-22 22:18:46 +0200272 data->error = -EIO;
Linus Walleijf20f8f22010-10-19 13:41:24 +0100273
274 /* Force-complete the transaction */
275 host->blockend = true;
276 host->dataend = true;
Russell Kinge9c091b2006-01-04 16:24:05 +0000277
278 /*
279 * We hit an error condition. Ensure that any data
280 * partially written to a page is properly coherent.
281 */
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100282 if (data->flags & MMC_DATA_READ) {
283 struct sg_mapping_iter *sg_miter = &host->sg_miter;
284 unsigned long flags;
285
286 local_irq_save(flags);
287 if (sg_miter_next(sg_miter)) {
288 flush_dcache_page(sg_miter->page);
289 sg_miter_stop(sg_miter);
290 }
291 local_irq_restore(flags);
292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
Linus Walleijf20f8f22010-10-19 13:41:24 +0100294
295 /*
296 * On ARM variants in PIO mode, MCI_DATABLOCKEND
297 * is always sent first, and we increase the
298 * transfered number of bytes for that IRQ. Then
299 * MCI_DATAEND follows and we conclude the transaction.
300 *
301 * On the Ux500 single-IRQ variant MCI_DATABLOCKEND
302 * doesn't seem to immediately clear from the status,
303 * so we can't use it keep count when only one irq is
304 * used because the irq will hit for other reasons, and
305 * then the flag is still up. So we use the MCI_DATAEND
306 * IRQ at the end of the entire transfer because
307 * MCI_DATABLOCKEND is broken.
308 *
309 * In the U300, the IRQs can arrive out-of-order,
310 * e.g. MCI_DATABLOCKEND sometimes arrives after MCI_DATAEND,
311 * so for this case we use the flags "blockend" and
312 * "dataend" to make sure both IRQs have arrived before
313 * concluding the transaction. (This does not apply
314 * to the Ux500 which doesn't fire MCI_DATABLOCKEND
315 * at all.) In DMA mode it suffers from the same problem
316 * as the Ux500.
317 */
318 if (status & MCI_DATABLOCKEND) {
319 /*
320 * Just being a little over-cautious, we do not
321 * use this progressive update if the hardware blockend
322 * flag is unreliable: since it can stay high between
323 * IRQs it will corrupt the transfer counter.
324 */
325 if (!variant->broken_blockend)
326 host->data_xfered += data->blksz;
327 host->blockend = true;
328 }
329
330 if (status & MCI_DATAEND)
331 host->dataend = true;
332
333 /*
334 * On variants with broken blockend we shall only wait for dataend,
335 * on others we must sync with the blockend signal since they can
336 * appear out-of-order.
337 */
338 if (host->dataend && (host->blockend || variant->broken_blockend)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 mmci_stop_data(host);
340
Linus Walleijf20f8f22010-10-19 13:41:24 +0100341 /* Reset these flags */
342 host->blockend = false;
343 host->dataend = false;
344
345 /*
346 * Variants with broken blockend flags need to handle the
347 * end of the entire transfer here.
348 */
349 if (variant->broken_blockend && !data->error)
350 host->data_xfered += data->blksz * data->blocks;
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (!data->stop) {
353 mmci_request_end(host, data->mrq);
354 } else {
355 mmci_start_command(host, data->stop, 0);
356 }
357 }
358}
359
360static void
361mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
362 unsigned int status)
363{
364 void __iomem *base = host->base;
365
366 host->cmd = NULL;
367
368 cmd->resp[0] = readl(base + MMCIRESPONSE0);
369 cmd->resp[1] = readl(base + MMCIRESPONSE1);
370 cmd->resp[2] = readl(base + MMCIRESPONSE2);
371 cmd->resp[3] = readl(base + MMCIRESPONSE3);
372
373 if (status & MCI_CMDTIMEOUT) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200374 cmd->error = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200376 cmd->error = -EILSEQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 }
378
Pierre Ossman17b04292007-07-22 22:18:46 +0200379 if (!cmd->data || cmd->error) {
Russell Kinge47c2222007-01-08 16:42:51 +0000380 if (host->data)
381 mmci_stop_data(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 mmci_request_end(host, cmd->mrq);
383 } else if (!(cmd->data->flags & MMC_DATA_READ)) {
384 mmci_start_data(host, cmd->data);
385 }
386}
387
388static int mmci_pio_read(struct mmci_host *host, char *buffer, unsigned int remain)
389{
390 void __iomem *base = host->base;
391 char *ptr = buffer;
392 u32 status;
Linus Walleij26eed9a2008-04-26 23:39:44 +0100393 int host_remain = host->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 do {
Linus Walleij26eed9a2008-04-26 23:39:44 +0100396 int count = host_remain - (readl(base + MMCIFIFOCNT) << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 if (count > remain)
399 count = remain;
400
401 if (count <= 0)
402 break;
403
404 readsl(base + MMCIFIFO, ptr, count >> 2);
405
406 ptr += count;
407 remain -= count;
Linus Walleij26eed9a2008-04-26 23:39:44 +0100408 host_remain -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 if (remain == 0)
411 break;
412
413 status = readl(base + MMCISTATUS);
414 } while (status & MCI_RXDATAAVLBL);
415
416 return ptr - buffer;
417}
418
419static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status)
420{
Rabin Vincent8301bb62010-08-09 12:57:30 +0100421 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 void __iomem *base = host->base;
423 char *ptr = buffer;
424
425 do {
426 unsigned int count, maxcnt;
427
Rabin Vincent8301bb62010-08-09 12:57:30 +0100428 maxcnt = status & MCI_TXFIFOEMPTY ?
429 variant->fifosize : variant->fifohalfsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 count = min(remain, maxcnt);
431
432 writesl(base + MMCIFIFO, ptr, count >> 2);
433
434 ptr += count;
435 remain -= count;
436
437 if (remain == 0)
438 break;
439
440 status = readl(base + MMCISTATUS);
441 } while (status & MCI_TXFIFOHALFEMPTY);
442
443 return ptr - buffer;
444}
445
446/*
447 * PIO data transfer IRQ handler.
448 */
David Howells7d12e782006-10-05 14:55:46 +0100449static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
451 struct mmci_host *host = dev_id;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100452 struct sg_mapping_iter *sg_miter = &host->sg_miter;
Rabin Vincent8301bb62010-08-09 12:57:30 +0100453 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 void __iomem *base = host->base;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100455 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 u32 status;
457
458 status = readl(base + MMCISTATUS);
459
Linus Walleij64de0282010-02-19 01:09:10 +0100460 dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100462 local_irq_save(flags);
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 unsigned int remain, len;
466 char *buffer;
467
468 /*
469 * For write, we only need to test the half-empty flag
470 * here - if the FIFO is completely empty, then by
471 * definition it is more than half empty.
472 *
473 * For read, check for data available.
474 */
475 if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL)))
476 break;
477
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100478 if (!sg_miter_next(sg_miter))
479 break;
480
481 buffer = sg_miter->addr;
482 remain = sg_miter->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 len = 0;
485 if (status & MCI_RXACTIVE)
486 len = mmci_pio_read(host, buffer, remain);
487 if (status & MCI_TXACTIVE)
488 len = mmci_pio_write(host, buffer, remain, status);
489
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100490 sg_miter->consumed = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 host->size -= len;
493 remain -= len;
494
495 if (remain)
496 break;
497
Russell Kinge9c091b2006-01-04 16:24:05 +0000498 if (status & MCI_RXACTIVE)
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100499 flush_dcache_page(sg_miter->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 status = readl(base + MMCISTATUS);
502 } while (1);
503
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100504 sg_miter_stop(sg_miter);
505
506 local_irq_restore(flags);
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 /*
509 * If we're nearing the end of the read, switch to
510 * "any data available" mode.
511 */
Rabin Vincent8301bb62010-08-09 12:57:30 +0100512 if (status & MCI_RXACTIVE && host->size < variant->fifosize)
Linus Walleij2686b4b2010-10-19 12:39:48 +0100513 mmci_set_mask1(host, MCI_RXDATAAVLBLMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 /*
516 * If we run out of data, disable the data IRQs; this
517 * prevents a race where the FIFO becomes empty before
518 * the chip itself has disabled the data path, and
519 * stops us racing with our data end IRQ.
520 */
521 if (host->size == 0) {
Linus Walleij2686b4b2010-10-19 12:39:48 +0100522 mmci_set_mask1(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0);
524 }
525
526 return IRQ_HANDLED;
527}
528
529/*
530 * Handle completion of command and data transfers.
531 */
David Howells7d12e782006-10-05 14:55:46 +0100532static irqreturn_t mmci_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
534 struct mmci_host *host = dev_id;
535 u32 status;
536 int ret = 0;
537
538 spin_lock(&host->lock);
539
540 do {
541 struct mmc_command *cmd;
542 struct mmc_data *data;
543
544 status = readl(host->base + MMCISTATUS);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100545
546 if (host->singleirq) {
547 if (status & readl(host->base + MMCIMASK1))
548 mmci_pio_irq(irq, dev_id);
549
550 status &= ~MCI_IRQ1MASK;
551 }
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 status &= readl(host->base + MMCIMASK0);
554 writel(status, host->base + MMCICLEAR);
555
Linus Walleij64de0282010-02-19 01:09:10 +0100556 dev_dbg(mmc_dev(host->mmc), "irq0 (data+cmd) %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 data = host->data;
559 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|
560 MCI_RXOVERRUN|MCI_DATAEND|MCI_DATABLOCKEND) && data)
561 mmci_data_irq(host, data, status);
562
563 cmd = host->cmd;
564 if (status & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT|MCI_CMDSENT|MCI_CMDRESPEND) && cmd)
565 mmci_cmd_irq(host, cmd, status);
566
567 ret = 1;
568 } while (status);
569
570 spin_unlock(&host->lock);
571
572 return IRQ_RETVAL(ret);
573}
574
575static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
576{
577 struct mmci_host *host = mmc_priv(mmc);
Linus Walleij9e943022008-10-24 21:17:50 +0100578 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 WARN_ON(host->mrq != NULL);
581
Nicolas Pitre019a5f52007-10-11 01:06:03 -0400582 if (mrq->data && !is_power_of_2(mrq->data->blksz)) {
Linus Walleij64de0282010-02-19 01:09:10 +0100583 dev_err(mmc_dev(mmc), "unsupported block size (%d bytes)\n",
584 mrq->data->blksz);
Pierre Ossman255d01a2007-07-24 20:38:53 +0200585 mrq->cmd->error = -EINVAL;
586 mmc_request_done(mmc, mrq);
587 return;
588 }
589
Linus Walleij9e943022008-10-24 21:17:50 +0100590 spin_lock_irqsave(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 host->mrq = mrq;
593
594 if (mrq->data && mrq->data->flags & MMC_DATA_READ)
595 mmci_start_data(host, mrq->data);
596
597 mmci_start_command(host, mrq->cmd, 0);
598
Linus Walleij9e943022008-10-24 21:17:50 +0100599 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601
602static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
603{
604 struct mmci_host *host = mmc_priv(mmc);
Linus Walleija6a64642009-09-14 12:56:14 +0100605 u32 pwr = 0;
606 unsigned long flags;
Linus Walleij99fc5132010-09-29 01:08:27 -0400607 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 switch (ios->power_mode) {
610 case MMC_POWER_OFF:
Linus Walleij99fc5132010-09-29 01:08:27 -0400611 if (host->vcc)
612 ret = mmc_regulator_set_ocr(mmc, host->vcc, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 break;
614 case MMC_POWER_UP:
Linus Walleij99fc5132010-09-29 01:08:27 -0400615 if (host->vcc) {
616 ret = mmc_regulator_set_ocr(mmc, host->vcc, ios->vdd);
617 if (ret) {
618 dev_err(mmc_dev(mmc), "unable to set OCR\n");
619 /*
620 * The .set_ios() function in the mmc_host_ops
621 * struct return void, and failing to set the
622 * power should be rare so we print an error
623 * and return here.
624 */
625 return;
626 }
627 }
Rabin Vincentbb8f5632010-07-21 12:53:57 +0100628 if (host->plat->vdd_handler)
629 pwr |= host->plat->vdd_handler(mmc_dev(mmc), ios->vdd,
630 ios->power_mode);
Linus Walleijcc30d602009-01-04 15:18:54 +0100631 /* The ST version does not have this, fall through to POWER_ON */
Linus Walleijf17a1f02009-08-04 01:01:02 +0100632 if (host->hw_designer != AMBA_VENDOR_ST) {
Linus Walleijcc30d602009-01-04 15:18:54 +0100633 pwr |= MCI_PWR_UP;
634 break;
635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 case MMC_POWER_ON:
637 pwr |= MCI_PWR_ON;
638 break;
639 }
640
Linus Walleijcc30d602009-01-04 15:18:54 +0100641 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN) {
Linus Walleijf17a1f02009-08-04 01:01:02 +0100642 if (host->hw_designer != AMBA_VENDOR_ST)
Linus Walleijcc30d602009-01-04 15:18:54 +0100643 pwr |= MCI_ROD;
644 else {
645 /*
646 * The ST Micro variant use the ROD bit for something
647 * else and only has OD (Open Drain).
648 */
649 pwr |= MCI_OD;
650 }
651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Linus Walleija6a64642009-09-14 12:56:14 +0100653 spin_lock_irqsave(&host->lock, flags);
654
655 mmci_set_clkreg(host, ios->clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657 if (host->pwr != pwr) {
658 host->pwr = pwr;
659 writel(pwr, host->base + MMCIPOWER);
660 }
Linus Walleija6a64642009-09-14 12:56:14 +0100661
662 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663}
664
Russell King89001442009-07-09 15:16:07 +0100665static int mmci_get_ro(struct mmc_host *mmc)
666{
667 struct mmci_host *host = mmc_priv(mmc);
668
669 if (host->gpio_wp == -ENOSYS)
670 return -ENOSYS;
671
Linus Walleij18a06302010-09-12 12:56:44 +0100672 return gpio_get_value_cansleep(host->gpio_wp);
Russell King89001442009-07-09 15:16:07 +0100673}
674
675static int mmci_get_cd(struct mmc_host *mmc)
676{
677 struct mmci_host *host = mmc_priv(mmc);
Rabin Vincent29719442010-08-09 12:54:43 +0100678 struct mmci_platform_data *plat = host->plat;
Russell King89001442009-07-09 15:16:07 +0100679 unsigned int status;
680
Rabin Vincent4b8caec2010-08-09 12:56:40 +0100681 if (host->gpio_cd == -ENOSYS) {
682 if (!plat->status)
683 return 1; /* Assume always present */
684
Rabin Vincent29719442010-08-09 12:54:43 +0100685 status = plat->status(mmc_dev(host->mmc));
Rabin Vincent4b8caec2010-08-09 12:56:40 +0100686 } else
Linus Walleij18a06302010-09-12 12:56:44 +0100687 status = !!gpio_get_value_cansleep(host->gpio_cd)
688 ^ plat->cd_invert;
Russell King89001442009-07-09 15:16:07 +0100689
Russell King74bc8092010-07-29 15:58:59 +0100690 /*
691 * Use positive logic throughout - status is zero for no card,
692 * non-zero for card inserted.
693 */
694 return status;
Russell King89001442009-07-09 15:16:07 +0100695}
696
Rabin Vincent148b8b32010-08-09 12:55:48 +0100697static irqreturn_t mmci_cd_irq(int irq, void *dev_id)
698{
699 struct mmci_host *host = dev_id;
700
701 mmc_detect_change(host->mmc, msecs_to_jiffies(500));
702
703 return IRQ_HANDLED;
704}
705
David Brownellab7aefd2006-11-12 17:55:30 -0800706static const struct mmc_host_ops mmci_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 .request = mmci_request,
708 .set_ios = mmci_set_ios,
Russell King89001442009-07-09 15:16:07 +0100709 .get_ro = mmci_get_ro,
710 .get_cd = mmci_get_cd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711};
712
Alessandro Rubini03fbdb12009-05-20 22:39:08 +0100713static int __devinit mmci_probe(struct amba_device *dev, struct amba_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
Linus Walleij6ef297f2009-09-22 14:29:36 +0100715 struct mmci_platform_data *plat = dev->dev.platform_data;
Rabin Vincent4956e102010-07-21 12:54:40 +0100716 struct variant_data *variant = id->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 struct mmci_host *host;
718 struct mmc_host *mmc;
Linus Walleij2686b4b2010-10-19 12:39:48 +0100719 unsigned int mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 int ret;
721
722 /* must have platform data */
723 if (!plat) {
724 ret = -EINVAL;
725 goto out;
726 }
727
728 ret = amba_request_regions(dev, DRIVER_NAME);
729 if (ret)
730 goto out;
731
732 mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
733 if (!mmc) {
734 ret = -ENOMEM;
735 goto rel_regions;
736 }
737
738 host = mmc_priv(mmc);
Rabin Vincent4ea580f2009-04-17 08:44:19 +0530739 host->mmc = mmc;
Russell King012b7d32009-07-09 15:13:56 +0100740
Russell King89001442009-07-09 15:16:07 +0100741 host->gpio_wp = -ENOSYS;
742 host->gpio_cd = -ENOSYS;
Rabin Vincent148b8b32010-08-09 12:55:48 +0100743 host->gpio_cd_irq = -1;
Russell King89001442009-07-09 15:16:07 +0100744
Russell King012b7d32009-07-09 15:13:56 +0100745 host->hw_designer = amba_manf(dev);
746 host->hw_revision = amba_rev(dev);
Linus Walleij64de0282010-02-19 01:09:10 +0100747 dev_dbg(mmc_dev(mmc), "designer ID = 0x%02x\n", host->hw_designer);
748 dev_dbg(mmc_dev(mmc), "revision = 0x%01x\n", host->hw_revision);
Russell King012b7d32009-07-09 15:13:56 +0100749
Russell Kingee569c42008-11-30 17:38:14 +0000750 host->clk = clk_get(&dev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if (IS_ERR(host->clk)) {
752 ret = PTR_ERR(host->clk);
753 host->clk = NULL;
754 goto host_free;
755 }
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 ret = clk_enable(host->clk);
758 if (ret)
Russell Kinga8d35842006-01-03 18:41:37 +0000759 goto clk_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 host->plat = plat;
Rabin Vincent4956e102010-07-21 12:54:40 +0100762 host->variant = variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 host->mclk = clk_get_rate(host->clk);
Linus Walleijc8df9a52008-04-29 09:34:07 +0100764 /*
765 * According to the spec, mclk is max 100 MHz,
766 * so we try to adjust the clock down to this,
767 * (if possible).
768 */
769 if (host->mclk > 100000000) {
770 ret = clk_set_rate(host->clk, 100000000);
771 if (ret < 0)
772 goto clk_disable;
773 host->mclk = clk_get_rate(host->clk);
Linus Walleij64de0282010-02-19 01:09:10 +0100774 dev_dbg(mmc_dev(mmc), "eventual mclk rate: %u Hz\n",
775 host->mclk);
Linus Walleijc8df9a52008-04-29 09:34:07 +0100776 }
Linus Walleijdc890c22009-06-07 23:27:31 +0100777 host->base = ioremap(dev->res.start, resource_size(&dev->res));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 if (!host->base) {
779 ret = -ENOMEM;
780 goto clk_disable;
781 }
782
783 mmc->ops = &mmci_ops;
784 mmc->f_min = (host->mclk + 511) / 512;
Linus Walleij808d97c2010-04-08 07:39:38 +0100785 /*
786 * If the platform data supplies a maximum operating
787 * frequency, this takes precedence. Else, we fall back
788 * to using the module parameter, which has a (low)
789 * default value in case it is not specified. Either
790 * value must not exceed the clock rate into the block,
791 * of course.
792 */
793 if (plat->f_max)
794 mmc->f_max = min(host->mclk, plat->f_max);
795 else
796 mmc->f_max = min(host->mclk, fmax);
Linus Walleij64de0282010-02-19 01:09:10 +0100797 dev_dbg(mmc_dev(mmc), "clocking block at %u Hz\n", mmc->f_max);
798
Linus Walleij34e84f32009-09-22 14:41:40 +0100799#ifdef CONFIG_REGULATOR
800 /* If we're using the regulator framework, try to fetch a regulator */
801 host->vcc = regulator_get(&dev->dev, "vmmc");
802 if (IS_ERR(host->vcc))
803 host->vcc = NULL;
804 else {
805 int mask = mmc_regulator_get_ocrmask(host->vcc);
806
807 if (mask < 0)
808 dev_err(&dev->dev, "error getting OCR mask (%d)\n",
809 mask);
810 else {
811 host->mmc->ocr_avail = (u32) mask;
812 if (plat->ocr_mask)
813 dev_warn(&dev->dev,
814 "Provided ocr_mask/setpower will not be used "
815 "(using regulator instead)\n");
816 }
817 }
818#endif
819 /* Fall back to platform data if no regulator is found */
820 if (host->vcc == NULL)
821 mmc->ocr_avail = plat->ocr_mask;
Linus Walleij9e6c82c2009-09-14 12:57:11 +0100822 mmc->caps = plat->capabilities;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824 /*
825 * We can do SGIO
826 */
Martin K. Petersena36274e2010-09-10 01:33:59 -0400827 mmc->max_segs = NR_SG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 /*
Rabin Vincent08458ef2010-07-21 12:55:59 +0100830 * Since only a certain number of bits are valid in the data length
831 * register, we must ensure that we don't exceed 2^num-1 bytes in a
832 * single request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 */
Rabin Vincent08458ef2010-07-21 12:55:59 +0100834 mmc->max_req_size = (1 << variant->datalength_bits) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836 /*
837 * Set the maximum segment size. Since we aren't doing DMA
838 * (yet) we are only limited by the data length register.
839 */
Pierre Ossman55db8902006-11-21 17:55:45 +0100840 mmc->max_seg_size = mmc->max_req_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100842 /*
843 * Block size can be up to 2048 bytes, but must be a power of two.
844 */
845 mmc->max_blk_size = 2048;
846
Pierre Ossman55db8902006-11-21 17:55:45 +0100847 /*
848 * No limit on the number of blocks transferred.
849 */
850 mmc->max_blk_count = mmc->max_req_size;
851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 spin_lock_init(&host->lock);
853
854 writel(0, host->base + MMCIMASK0);
855 writel(0, host->base + MMCIMASK1);
856 writel(0xfff, host->base + MMCICLEAR);
857
Russell King89001442009-07-09 15:16:07 +0100858 if (gpio_is_valid(plat->gpio_cd)) {
859 ret = gpio_request(plat->gpio_cd, DRIVER_NAME " (cd)");
860 if (ret == 0)
861 ret = gpio_direction_input(plat->gpio_cd);
862 if (ret == 0)
863 host->gpio_cd = plat->gpio_cd;
864 else if (ret != -ENOSYS)
865 goto err_gpio_cd;
Rabin Vincent148b8b32010-08-09 12:55:48 +0100866
867 ret = request_any_context_irq(gpio_to_irq(plat->gpio_cd),
868 mmci_cd_irq, 0,
869 DRIVER_NAME " (cd)", host);
870 if (ret >= 0)
871 host->gpio_cd_irq = gpio_to_irq(plat->gpio_cd);
Russell King89001442009-07-09 15:16:07 +0100872 }
873 if (gpio_is_valid(plat->gpio_wp)) {
874 ret = gpio_request(plat->gpio_wp, DRIVER_NAME " (wp)");
875 if (ret == 0)
876 ret = gpio_direction_input(plat->gpio_wp);
877 if (ret == 0)
878 host->gpio_wp = plat->gpio_wp;
879 else if (ret != -ENOSYS)
880 goto err_gpio_wp;
881 }
882
Rabin Vincent4b8caec2010-08-09 12:56:40 +0100883 if ((host->plat->status || host->gpio_cd != -ENOSYS)
884 && host->gpio_cd_irq < 0)
Rabin Vincent148b8b32010-08-09 12:55:48 +0100885 mmc->caps |= MMC_CAP_NEEDS_POLL;
886
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700887 ret = request_irq(dev->irq[0], mmci_irq, IRQF_SHARED, DRIVER_NAME " (cmd)", host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 if (ret)
889 goto unmap;
890
Linus Walleij2686b4b2010-10-19 12:39:48 +0100891 if (dev->irq[1] == NO_IRQ)
892 host->singleirq = true;
893 else {
894 ret = request_irq(dev->irq[1], mmci_pio_irq, IRQF_SHARED,
895 DRIVER_NAME " (pio)", host);
896 if (ret)
897 goto irq0_free;
898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Linus Walleij2686b4b2010-10-19 12:39:48 +0100900 mask = MCI_IRQENABLE;
Linus Walleijf20f8f22010-10-19 13:41:24 +0100901 /* Don't use the datablockend flag if it's broken */
902 if (variant->broken_blockend)
903 mask &= ~MCI_DATABLOCKEND;
904
Linus Walleij2686b4b2010-10-19 12:39:48 +0100905 writel(mask, host->base + MMCIMASK0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 amba_set_drvdata(dev, mmc);
908
909 mmc_add_host(mmc);
910
Linus Walleij64de0282010-02-19 01:09:10 +0100911 dev_info(&dev->dev, "%s: MMCI rev %x cfg %02x at 0x%016llx irq %d,%d\n",
Russell Kingd366b642005-08-19 09:40:08 +0100912 mmc_hostname(mmc), amba_rev(dev), amba_config(dev),
Greg Kroah-Hartmane29419f2006-06-12 15:20:16 -0700913 (unsigned long long)dev->res.start, dev->irq[0], dev->irq[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return 0;
916
917 irq0_free:
918 free_irq(dev->irq[0], host);
919 unmap:
Russell King89001442009-07-09 15:16:07 +0100920 if (host->gpio_wp != -ENOSYS)
921 gpio_free(host->gpio_wp);
922 err_gpio_wp:
Rabin Vincent148b8b32010-08-09 12:55:48 +0100923 if (host->gpio_cd_irq >= 0)
924 free_irq(host->gpio_cd_irq, host);
Russell King89001442009-07-09 15:16:07 +0100925 if (host->gpio_cd != -ENOSYS)
926 gpio_free(host->gpio_cd);
927 err_gpio_cd:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 iounmap(host->base);
929 clk_disable:
930 clk_disable(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 clk_free:
932 clk_put(host->clk);
933 host_free:
934 mmc_free_host(mmc);
935 rel_regions:
936 amba_release_regions(dev);
937 out:
938 return ret;
939}
940
Linus Walleij6dc4a472009-03-07 00:23:52 +0100941static int __devexit mmci_remove(struct amba_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
943 struct mmc_host *mmc = amba_get_drvdata(dev);
944
945 amba_set_drvdata(dev, NULL);
946
947 if (mmc) {
948 struct mmci_host *host = mmc_priv(mmc);
949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 mmc_remove_host(mmc);
951
952 writel(0, host->base + MMCIMASK0);
953 writel(0, host->base + MMCIMASK1);
954
955 writel(0, host->base + MMCICOMMAND);
956 writel(0, host->base + MMCIDATACTRL);
957
958 free_irq(dev->irq[0], host);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100959 if (!host->singleirq)
960 free_irq(dev->irq[1], host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Russell King89001442009-07-09 15:16:07 +0100962 if (host->gpio_wp != -ENOSYS)
963 gpio_free(host->gpio_wp);
Rabin Vincent148b8b32010-08-09 12:55:48 +0100964 if (host->gpio_cd_irq >= 0)
965 free_irq(host->gpio_cd_irq, host);
Russell King89001442009-07-09 15:16:07 +0100966 if (host->gpio_cd != -ENOSYS)
967 gpio_free(host->gpio_cd);
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 iounmap(host->base);
970 clk_disable(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 clk_put(host->clk);
972
Linus Walleij99fc5132010-09-29 01:08:27 -0400973 if (host->vcc)
974 mmc_regulator_set_ocr(mmc, host->vcc, 0);
Linus Walleij34e84f32009-09-22 14:41:40 +0100975 regulator_put(host->vcc);
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 mmc_free_host(mmc);
978
979 amba_release_regions(dev);
980 }
981
982 return 0;
983}
984
985#ifdef CONFIG_PM
Pavel Macheke5378ca2005-04-16 15:25:29 -0700986static int mmci_suspend(struct amba_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
988 struct mmc_host *mmc = amba_get_drvdata(dev);
989 int ret = 0;
990
991 if (mmc) {
992 struct mmci_host *host = mmc_priv(mmc);
993
Matt Fleming1a13f8f2010-05-26 14:42:08 -0700994 ret = mmc_suspend_host(mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 if (ret == 0)
996 writel(0, host->base + MMCIMASK0);
997 }
998
999 return ret;
1000}
1001
1002static int mmci_resume(struct amba_device *dev)
1003{
1004 struct mmc_host *mmc = amba_get_drvdata(dev);
1005 int ret = 0;
1006
1007 if (mmc) {
1008 struct mmci_host *host = mmc_priv(mmc);
1009
1010 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
1011
1012 ret = mmc_resume_host(mmc);
1013 }
1014
1015 return ret;
1016}
1017#else
1018#define mmci_suspend NULL
1019#define mmci_resume NULL
1020#endif
1021
1022static struct amba_id mmci_ids[] = {
1023 {
1024 .id = 0x00041180,
1025 .mask = 0x000fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001026 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 },
1028 {
1029 .id = 0x00041181,
1030 .mask = 0x000fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001031 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 },
Linus Walleijcc30d602009-01-04 15:18:54 +01001033 /* ST Micro variants */
1034 {
1035 .id = 0x00180180,
1036 .mask = 0x00ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001037 .data = &variant_u300,
Linus Walleijcc30d602009-01-04 15:18:54 +01001038 },
1039 {
1040 .id = 0x00280180,
1041 .mask = 0x00ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001042 .data = &variant_u300,
1043 },
1044 {
1045 .id = 0x00480180,
1046 .mask = 0x00ffffff,
1047 .data = &variant_ux500,
Linus Walleijcc30d602009-01-04 15:18:54 +01001048 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 { 0, 0 },
1050};
1051
1052static struct amba_driver mmci_driver = {
1053 .drv = {
1054 .name = DRIVER_NAME,
1055 },
1056 .probe = mmci_probe,
Linus Walleij6dc4a472009-03-07 00:23:52 +01001057 .remove = __devexit_p(mmci_remove),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 .suspend = mmci_suspend,
1059 .resume = mmci_resume,
1060 .id_table = mmci_ids,
1061};
1062
1063static int __init mmci_init(void)
1064{
1065 return amba_driver_register(&mmci_driver);
1066}
1067
1068static void __exit mmci_exit(void)
1069{
1070 amba_driver_unregister(&mmci_driver);
1071}
1072
1073module_init(mmci_init);
1074module_exit(mmci_exit);
1075module_param(fmax, uint, 0444);
1076
1077MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
1078MODULE_LICENSE("GPL");