blob: f67fd4f2ab485133b45ff5c424d1854cf62fabb0 [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>
Linus Walleij34177802010-10-19 12:43:58 +010022#include <linux/mmc/card.h>
Russell Kinga62c80e2006-01-07 13:52:45 +000023#include <linux/amba/bus.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000024#include <linux/clk.h>
Jens Axboebd6dee62007-10-24 09:01:09 +020025#include <linux/scatterlist.h>
Russell King89001442009-07-09 15:16:07 +010026#include <linux/gpio.h>
Linus Walleij6ef297f2009-09-22 14:29:36 +010027#include <linux/amba/mmci.h>
Linus Walleij34e84f32009-09-22 14:41:40 +010028#include <linux/regulator/consumer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Russell King7b09cda2005-07-01 12:02:59 +010030#include <asm/div64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/io.h>
Russell Kingc6b8fda2005-10-28 14:05:16 +010032#include <asm/sizes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34#include "mmci.h"
35
36#define DRIVER_NAME "mmci-pl18x"
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038static unsigned int fmax = 515633;
39
Rabin Vincent4956e102010-07-21 12:54:40 +010040/**
41 * struct variant_data - MMCI variant-specific quirks
42 * @clkreg: default value for MCICLOCK register
Rabin Vincent4380c142010-07-21 12:55:18 +010043 * @clkreg_enable: enable value for MMCICLOCK register
Rabin Vincent08458ef2010-07-21 12:55:59 +010044 * @datalength_bits: number of bits in the MMCIDATALENGTH register
Rabin Vincent8301bb62010-08-09 12:57:30 +010045 * @fifosize: number of bytes that can be written when MMCI_TXFIFOEMPTY
46 * is asserted (likewise for RX)
47 * @fifohalfsize: number of bytes that can be written when MCI_TXFIFOHALFEMPTY
48 * is asserted (likewise for RX)
Linus Walleijf20f8f22010-10-19 13:41:24 +010049 * @broken_blockend: the MCI_DATABLOCKEND is broken on the hardware
50 * and will not work at all.
51 * @broken_blockend_dma: the MCI_DATABLOCKEND is broken on the hardware when
52 * using DMA.
Linus Walleij34177802010-10-19 12:43:58 +010053 * @sdio: variant supports SDIO
Linus Walleijb70a67f2010-12-06 09:24:14 +010054 * @st_clkdiv: true if using a ST-specific clock divider algorithm
Rabin Vincent4956e102010-07-21 12:54:40 +010055 */
56struct variant_data {
57 unsigned int clkreg;
Rabin Vincent4380c142010-07-21 12:55:18 +010058 unsigned int clkreg_enable;
Rabin Vincent08458ef2010-07-21 12:55:59 +010059 unsigned int datalength_bits;
Rabin Vincent8301bb62010-08-09 12:57:30 +010060 unsigned int fifosize;
61 unsigned int fifohalfsize;
Linus Walleijf20f8f22010-10-19 13:41:24 +010062 bool broken_blockend;
63 bool broken_blockend_dma;
Linus Walleij34177802010-10-19 12:43:58 +010064 bool sdio;
Linus Walleijb70a67f2010-12-06 09:24:14 +010065 bool st_clkdiv;
Rabin Vincent4956e102010-07-21 12:54:40 +010066};
67
68static struct variant_data variant_arm = {
Rabin Vincent8301bb62010-08-09 12:57:30 +010069 .fifosize = 16 * 4,
70 .fifohalfsize = 8 * 4,
Rabin Vincent08458ef2010-07-21 12:55:59 +010071 .datalength_bits = 16,
Rabin Vincent4956e102010-07-21 12:54:40 +010072};
73
74static struct variant_data variant_u300 = {
Rabin Vincent8301bb62010-08-09 12:57:30 +010075 .fifosize = 16 * 4,
76 .fifohalfsize = 8 * 4,
Rabin Vincent4380c142010-07-21 12:55:18 +010077 .clkreg_enable = 1 << 13, /* HWFCEN */
Rabin Vincent08458ef2010-07-21 12:55:59 +010078 .datalength_bits = 16,
Linus Walleijf20f8f22010-10-19 13:41:24 +010079 .broken_blockend_dma = true,
Linus Walleij34177802010-10-19 12:43:58 +010080 .sdio = true,
Rabin Vincent4956e102010-07-21 12:54:40 +010081};
82
83static struct variant_data variant_ux500 = {
Rabin Vincent8301bb62010-08-09 12:57:30 +010084 .fifosize = 30 * 4,
85 .fifohalfsize = 8 * 4,
Rabin Vincent4956e102010-07-21 12:54:40 +010086 .clkreg = MCI_CLK_ENABLE,
Rabin Vincent4380c142010-07-21 12:55:18 +010087 .clkreg_enable = 1 << 14, /* HWFCEN */
Rabin Vincent08458ef2010-07-21 12:55:59 +010088 .datalength_bits = 24,
Linus Walleijf20f8f22010-10-19 13:41:24 +010089 .broken_blockend = true,
Linus Walleij34177802010-10-19 12:43:58 +010090 .sdio = true,
Linus Walleijb70a67f2010-12-06 09:24:14 +010091 .st_clkdiv = true,
Rabin Vincent4956e102010-07-21 12:54:40 +010092};
Linus Walleijb70a67f2010-12-06 09:24:14 +010093
Linus Walleija6a64642009-09-14 12:56:14 +010094/*
95 * This must be called with host->lock held
96 */
97static void mmci_set_clkreg(struct mmci_host *host, unsigned int desired)
98{
Rabin Vincent4956e102010-07-21 12:54:40 +010099 struct variant_data *variant = host->variant;
100 u32 clk = variant->clkreg;
Linus Walleija6a64642009-09-14 12:56:14 +0100101
102 if (desired) {
103 if (desired >= host->mclk) {
Linus Walleijb70a67f2010-12-06 09:24:14 +0100104 /*
105 * The ST clock divider does not like the bypass bit,
106 * even though it's available. Instead the datasheet
107 * recommends setting the divider to zero.
108 */
109 if (!variant->st_clkdiv)
110 clk = MCI_CLK_BYPASS;
Linus Walleija6a64642009-09-14 12:56:14 +0100111 host->cclk = host->mclk;
Linus Walleijb70a67f2010-12-06 09:24:14 +0100112 } else if (variant->st_clkdiv) {
113 /*
114 * DB8500 TRM says f = mclk / (clkdiv + 2)
115 * => clkdiv = (mclk / f) - 2
116 * Round the divider up so we don't exceed the max
117 * frequency
118 */
119 clk = DIV_ROUND_UP(host->mclk, desired) - 2;
120 if (clk >= 256)
121 clk = 255;
122 host->cclk = host->mclk / (clk + 2);
Linus Walleija6a64642009-09-14 12:56:14 +0100123 } else {
Linus Walleijb70a67f2010-12-06 09:24:14 +0100124 /*
125 * PL180 TRM says f = mclk / (2 * (clkdiv + 1))
126 * => clkdiv = mclk / (2 * f) - 1
127 */
Linus Walleija6a64642009-09-14 12:56:14 +0100128 clk = host->mclk / (2 * desired) - 1;
129 if (clk >= 256)
130 clk = 255;
131 host->cclk = host->mclk / (2 * (clk + 1));
132 }
Rabin Vincent4380c142010-07-21 12:55:18 +0100133
134 clk |= variant->clkreg_enable;
Linus Walleija6a64642009-09-14 12:56:14 +0100135 clk |= MCI_CLK_ENABLE;
136 /* This hasn't proven to be worthwhile */
137 /* clk |= MCI_CLK_PWRSAVE; */
138 }
139
Linus Walleij9e6c82c2009-09-14 12:57:11 +0100140 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4)
Linus Walleij771dc152010-04-08 07:38:52 +0100141 clk |= MCI_4BIT_BUS;
142 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_8)
143 clk |= MCI_ST_8BIT_BUS;
Linus Walleij9e6c82c2009-09-14 12:57:11 +0100144
Linus Walleija6a64642009-09-14 12:56:14 +0100145 writel(clk, host->base + MMCICLOCK);
146}
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148static void
149mmci_request_end(struct mmci_host *host, struct mmc_request *mrq)
150{
151 writel(0, host->base + MMCICOMMAND);
152
Russell Kinge47c2222007-01-08 16:42:51 +0000153 BUG_ON(host->data);
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 host->mrq = NULL;
156 host->cmd = NULL;
157
158 if (mrq->data)
159 mrq->data->bytes_xfered = host->data_xfered;
160
161 /*
162 * Need to drop the host lock here; mmc_request_done may call
163 * back into the driver...
164 */
165 spin_unlock(&host->lock);
166 mmc_request_done(host->mmc, mrq);
167 spin_lock(&host->lock);
168}
169
Linus Walleij2686b4b2010-10-19 12:39:48 +0100170static void mmci_set_mask1(struct mmci_host *host, unsigned int mask)
171{
172 void __iomem *base = host->base;
173
174 if (host->singleirq) {
175 unsigned int mask0 = readl(base + MMCIMASK0);
176
177 mask0 &= ~MCI_IRQ1MASK;
178 mask0 |= mask;
179
180 writel(mask0, base + MMCIMASK0);
181 }
182
183 writel(mask, base + MMCIMASK1);
184}
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186static void mmci_stop_data(struct mmci_host *host)
187{
188 writel(0, host->base + MMCIDATACTRL);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100189 mmci_set_mask1(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 host->data = NULL;
191}
192
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100193static void mmci_init_sg(struct mmci_host *host, struct mmc_data *data)
194{
195 unsigned int flags = SG_MITER_ATOMIC;
196
197 if (data->flags & MMC_DATA_READ)
198 flags |= SG_MITER_TO_SG;
199 else
200 flags |= SG_MITER_FROM_SG;
201
202 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
203}
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205static void mmci_start_data(struct mmci_host *host, struct mmc_data *data)
206{
Rabin Vincent8301bb62010-08-09 12:57:30 +0100207 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 unsigned int datactrl, timeout, irqmask;
Russell King7b09cda2005-07-01 12:02:59 +0100209 unsigned long long clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 void __iomem *base;
Russell King3bc87f22006-08-27 13:51:28 +0100211 int blksz_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Linus Walleij64de0282010-02-19 01:09:10 +0100213 dev_dbg(mmc_dev(host->mmc), "blksz %04x blks %04x flags %08x\n",
214 data->blksz, data->blocks, data->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 host->data = data;
Rabin Vincent528320d2010-07-21 12:49:49 +0100217 host->size = data->blksz * data->blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 host->data_xfered = 0;
Linus Walleijf20f8f22010-10-19 13:41:24 +0100219 host->blockend = false;
220 host->dataend = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 mmci_init_sg(host, data);
223
Russell King7b09cda2005-07-01 12:02:59 +0100224 clks = (unsigned long long)data->timeout_ns * host->cclk;
225 do_div(clks, 1000000000UL);
226
227 timeout = data->timeout_clks + (unsigned int)clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 base = host->base;
230 writel(timeout, base + MMCIDATATIMER);
231 writel(host->size, base + MMCIDATALENGTH);
232
Russell King3bc87f22006-08-27 13:51:28 +0100233 blksz_bits = ffs(data->blksz) - 1;
234 BUG_ON(1 << blksz_bits != data->blksz);
235
236 datactrl = MCI_DPSM_ENABLE | blksz_bits << 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 if (data->flags & MMC_DATA_READ) {
238 datactrl |= MCI_DPSM_DIRECTION;
239 irqmask = MCI_RXFIFOHALFFULLMASK;
Russell King0425a142006-02-16 16:48:31 +0000240
241 /*
242 * If we have less than a FIFOSIZE of bytes to transfer,
243 * trigger a PIO interrupt as soon as any data is available.
244 */
Rabin Vincent8301bb62010-08-09 12:57:30 +0100245 if (host->size < variant->fifosize)
Russell King0425a142006-02-16 16:48:31 +0000246 irqmask |= MCI_RXDATAAVLBLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 } else {
248 /*
249 * We don't actually need to include "FIFO empty" here
250 * since its implicit in "FIFO half empty".
251 */
252 irqmask = MCI_TXFIFOHALFEMPTYMASK;
253 }
254
Linus Walleij34177802010-10-19 12:43:58 +0100255 /* The ST Micro variants has a special bit to enable SDIO */
256 if (variant->sdio && host->mmc->card)
257 if (mmc_card_sdio(host->mmc->card))
258 datactrl |= MCI_ST_DPSM_SDIOEN;
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 writel(datactrl, base + MMCIDATACTRL);
261 writel(readl(base + MMCIMASK0) & ~MCI_DATAENDMASK, base + MMCIMASK0);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100262 mmci_set_mask1(host, irqmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
265static void
266mmci_start_command(struct mmci_host *host, struct mmc_command *cmd, u32 c)
267{
268 void __iomem *base = host->base;
269
Linus Walleij64de0282010-02-19 01:09:10 +0100270 dev_dbg(mmc_dev(host->mmc), "op %02x arg %08x flags %08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 cmd->opcode, cmd->arg, cmd->flags);
272
273 if (readl(base + MMCICOMMAND) & MCI_CPSM_ENABLE) {
274 writel(0, base + MMCICOMMAND);
275 udelay(1);
276 }
277
278 c |= cmd->opcode | MCI_CPSM_ENABLE;
Russell Kinge9225172006-02-02 12:23:12 +0000279 if (cmd->flags & MMC_RSP_PRESENT) {
280 if (cmd->flags & MMC_RSP_136)
281 c |= MCI_CPSM_LONGRSP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 c |= MCI_CPSM_RESPONSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
284 if (/*interrupt*/0)
285 c |= MCI_CPSM_INTERRUPT;
286
287 host->cmd = cmd;
288
289 writel(cmd->arg, base + MMCIARGUMENT);
290 writel(c, base + MMCICOMMAND);
291}
292
293static void
294mmci_data_irq(struct mmci_host *host, struct mmc_data *data,
295 unsigned int status)
296{
Linus Walleijf20f8f22010-10-19 13:41:24 +0100297 struct variant_data *variant = host->variant;
298
299 /* First check for errors */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|MCI_RXOVERRUN)) {
Linus Walleij64de0282010-02-19 01:09:10 +0100301 dev_dbg(mmc_dev(host->mmc), "MCI ERROR IRQ (status %08x)\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 if (status & MCI_DATACRCFAIL)
Pierre Ossman17b04292007-07-22 22:18:46 +0200303 data->error = -EILSEQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 else if (status & MCI_DATATIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +0200305 data->error = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 else if (status & (MCI_TXUNDERRUN|MCI_RXOVERRUN))
Pierre Ossman17b04292007-07-22 22:18:46 +0200307 data->error = -EIO;
Linus Walleijf20f8f22010-10-19 13:41:24 +0100308
309 /* Force-complete the transaction */
310 host->blockend = true;
311 host->dataend = true;
Russell Kinge9c091b2006-01-04 16:24:05 +0000312
313 /*
314 * We hit an error condition. Ensure that any data
315 * partially written to a page is properly coherent.
316 */
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100317 if (data->flags & MMC_DATA_READ) {
318 struct sg_mapping_iter *sg_miter = &host->sg_miter;
319 unsigned long flags;
320
321 local_irq_save(flags);
322 if (sg_miter_next(sg_miter)) {
323 flush_dcache_page(sg_miter->page);
324 sg_miter_stop(sg_miter);
325 }
326 local_irq_restore(flags);
327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
Linus Walleijf20f8f22010-10-19 13:41:24 +0100329
330 /*
331 * On ARM variants in PIO mode, MCI_DATABLOCKEND
332 * is always sent first, and we increase the
333 * transfered number of bytes for that IRQ. Then
334 * MCI_DATAEND follows and we conclude the transaction.
335 *
336 * On the Ux500 single-IRQ variant MCI_DATABLOCKEND
337 * doesn't seem to immediately clear from the status,
338 * so we can't use it keep count when only one irq is
339 * used because the irq will hit for other reasons, and
340 * then the flag is still up. So we use the MCI_DATAEND
341 * IRQ at the end of the entire transfer because
342 * MCI_DATABLOCKEND is broken.
343 *
344 * In the U300, the IRQs can arrive out-of-order,
345 * e.g. MCI_DATABLOCKEND sometimes arrives after MCI_DATAEND,
346 * so for this case we use the flags "blockend" and
347 * "dataend" to make sure both IRQs have arrived before
348 * concluding the transaction. (This does not apply
349 * to the Ux500 which doesn't fire MCI_DATABLOCKEND
350 * at all.) In DMA mode it suffers from the same problem
351 * as the Ux500.
352 */
353 if (status & MCI_DATABLOCKEND) {
354 /*
355 * Just being a little over-cautious, we do not
356 * use this progressive update if the hardware blockend
357 * flag is unreliable: since it can stay high between
358 * IRQs it will corrupt the transfer counter.
359 */
360 if (!variant->broken_blockend)
361 host->data_xfered += data->blksz;
362 host->blockend = true;
363 }
364
365 if (status & MCI_DATAEND)
366 host->dataend = true;
367
368 /*
369 * On variants with broken blockend we shall only wait for dataend,
370 * on others we must sync with the blockend signal since they can
371 * appear out-of-order.
372 */
373 if (host->dataend && (host->blockend || variant->broken_blockend)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 mmci_stop_data(host);
375
Linus Walleijf20f8f22010-10-19 13:41:24 +0100376 /* Reset these flags */
377 host->blockend = false;
378 host->dataend = false;
379
380 /*
381 * Variants with broken blockend flags need to handle the
382 * end of the entire transfer here.
383 */
384 if (variant->broken_blockend && !data->error)
385 host->data_xfered += data->blksz * data->blocks;
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (!data->stop) {
388 mmci_request_end(host, data->mrq);
389 } else {
390 mmci_start_command(host, data->stop, 0);
391 }
392 }
393}
394
395static void
396mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
397 unsigned int status)
398{
399 void __iomem *base = host->base;
400
401 host->cmd = NULL;
402
403 cmd->resp[0] = readl(base + MMCIRESPONSE0);
404 cmd->resp[1] = readl(base + MMCIRESPONSE1);
405 cmd->resp[2] = readl(base + MMCIRESPONSE2);
406 cmd->resp[3] = readl(base + MMCIRESPONSE3);
407
408 if (status & MCI_CMDTIMEOUT) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200409 cmd->error = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200411 cmd->error = -EILSEQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413
Pierre Ossman17b04292007-07-22 22:18:46 +0200414 if (!cmd->data || cmd->error) {
Russell Kinge47c2222007-01-08 16:42:51 +0000415 if (host->data)
416 mmci_stop_data(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 mmci_request_end(host, cmd->mrq);
418 } else if (!(cmd->data->flags & MMC_DATA_READ)) {
419 mmci_start_data(host, cmd->data);
420 }
421}
422
423static int mmci_pio_read(struct mmci_host *host, char *buffer, unsigned int remain)
424{
425 void __iomem *base = host->base;
426 char *ptr = buffer;
427 u32 status;
Linus Walleij26eed9a2008-04-26 23:39:44 +0100428 int host_remain = host->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 do {
Linus Walleij26eed9a2008-04-26 23:39:44 +0100431 int count = host_remain - (readl(base + MMCIFIFOCNT) << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 if (count > remain)
434 count = remain;
435
436 if (count <= 0)
437 break;
438
439 readsl(base + MMCIFIFO, ptr, count >> 2);
440
441 ptr += count;
442 remain -= count;
Linus Walleij26eed9a2008-04-26 23:39:44 +0100443 host_remain -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 if (remain == 0)
446 break;
447
448 status = readl(base + MMCISTATUS);
449 } while (status & MCI_RXDATAAVLBL);
450
451 return ptr - buffer;
452}
453
454static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status)
455{
Rabin Vincent8301bb62010-08-09 12:57:30 +0100456 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 void __iomem *base = host->base;
458 char *ptr = buffer;
459
460 do {
461 unsigned int count, maxcnt;
462
Rabin Vincent8301bb62010-08-09 12:57:30 +0100463 maxcnt = status & MCI_TXFIFOEMPTY ?
464 variant->fifosize : variant->fifohalfsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 count = min(remain, maxcnt);
466
Linus Walleij34177802010-10-19 12:43:58 +0100467 /*
468 * The ST Micro variant for SDIO transfer sizes
469 * less then 8 bytes should have clock H/W flow
470 * control disabled.
471 */
472 if (variant->sdio &&
473 mmc_card_sdio(host->mmc->card)) {
474 if (count < 8)
475 writel(readl(host->base + MMCICLOCK) &
476 ~variant->clkreg_enable,
477 host->base + MMCICLOCK);
478 else
479 writel(readl(host->base + MMCICLOCK) |
480 variant->clkreg_enable,
481 host->base + MMCICLOCK);
482 }
483
484 /*
485 * SDIO especially may want to send something that is
486 * not divisible by 4 (as opposed to card sectors
487 * etc), and the FIFO only accept full 32-bit writes.
488 * So compensate by adding +3 on the count, a single
489 * byte become a 32bit write, 7 bytes will be two
490 * 32bit writes etc.
491 */
492 writesl(base + MMCIFIFO, ptr, (count + 3) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 ptr += count;
495 remain -= count;
496
497 if (remain == 0)
498 break;
499
500 status = readl(base + MMCISTATUS);
501 } while (status & MCI_TXFIFOHALFEMPTY);
502
503 return ptr - buffer;
504}
505
506/*
507 * PIO data transfer IRQ handler.
508 */
David Howells7d12e782006-10-05 14:55:46 +0100509static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
511 struct mmci_host *host = dev_id;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100512 struct sg_mapping_iter *sg_miter = &host->sg_miter;
Rabin Vincent8301bb62010-08-09 12:57:30 +0100513 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 void __iomem *base = host->base;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100515 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 u32 status;
517
518 status = readl(base + MMCISTATUS);
519
Linus Walleij64de0282010-02-19 01:09:10 +0100520 dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100522 local_irq_save(flags);
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 unsigned int remain, len;
526 char *buffer;
527
528 /*
529 * For write, we only need to test the half-empty flag
530 * here - if the FIFO is completely empty, then by
531 * definition it is more than half empty.
532 *
533 * For read, check for data available.
534 */
535 if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL)))
536 break;
537
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100538 if (!sg_miter_next(sg_miter))
539 break;
540
541 buffer = sg_miter->addr;
542 remain = sg_miter->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 len = 0;
545 if (status & MCI_RXACTIVE)
546 len = mmci_pio_read(host, buffer, remain);
547 if (status & MCI_TXACTIVE)
548 len = mmci_pio_write(host, buffer, remain, status);
549
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100550 sg_miter->consumed = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 host->size -= len;
553 remain -= len;
554
555 if (remain)
556 break;
557
Russell Kinge9c091b2006-01-04 16:24:05 +0000558 if (status & MCI_RXACTIVE)
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100559 flush_dcache_page(sg_miter->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 status = readl(base + MMCISTATUS);
562 } while (1);
563
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100564 sg_miter_stop(sg_miter);
565
566 local_irq_restore(flags);
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 /*
569 * If we're nearing the end of the read, switch to
570 * "any data available" mode.
571 */
Rabin Vincent8301bb62010-08-09 12:57:30 +0100572 if (status & MCI_RXACTIVE && host->size < variant->fifosize)
Linus Walleij2686b4b2010-10-19 12:39:48 +0100573 mmci_set_mask1(host, MCI_RXDATAAVLBLMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 /*
576 * If we run out of data, disable the data IRQs; this
577 * prevents a race where the FIFO becomes empty before
578 * the chip itself has disabled the data path, and
579 * stops us racing with our data end IRQ.
580 */
581 if (host->size == 0) {
Linus Walleij2686b4b2010-10-19 12:39:48 +0100582 mmci_set_mask1(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0);
584 }
585
586 return IRQ_HANDLED;
587}
588
589/*
590 * Handle completion of command and data transfers.
591 */
David Howells7d12e782006-10-05 14:55:46 +0100592static irqreturn_t mmci_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
594 struct mmci_host *host = dev_id;
595 u32 status;
596 int ret = 0;
597
598 spin_lock(&host->lock);
599
600 do {
601 struct mmc_command *cmd;
602 struct mmc_data *data;
603
604 status = readl(host->base + MMCISTATUS);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100605
606 if (host->singleirq) {
607 if (status & readl(host->base + MMCIMASK1))
608 mmci_pio_irq(irq, dev_id);
609
610 status &= ~MCI_IRQ1MASK;
611 }
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 status &= readl(host->base + MMCIMASK0);
614 writel(status, host->base + MMCICLEAR);
615
Linus Walleij64de0282010-02-19 01:09:10 +0100616 dev_dbg(mmc_dev(host->mmc), "irq0 (data+cmd) %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 data = host->data;
619 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|
620 MCI_RXOVERRUN|MCI_DATAEND|MCI_DATABLOCKEND) && data)
621 mmci_data_irq(host, data, status);
622
623 cmd = host->cmd;
624 if (status & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT|MCI_CMDSENT|MCI_CMDRESPEND) && cmd)
625 mmci_cmd_irq(host, cmd, status);
626
627 ret = 1;
628 } while (status);
629
630 spin_unlock(&host->lock);
631
632 return IRQ_RETVAL(ret);
633}
634
635static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
636{
637 struct mmci_host *host = mmc_priv(mmc);
Linus Walleij9e943022008-10-24 21:17:50 +0100638 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 WARN_ON(host->mrq != NULL);
641
Nicolas Pitre019a5f52007-10-11 01:06:03 -0400642 if (mrq->data && !is_power_of_2(mrq->data->blksz)) {
Linus Walleij64de0282010-02-19 01:09:10 +0100643 dev_err(mmc_dev(mmc), "unsupported block size (%d bytes)\n",
644 mrq->data->blksz);
Pierre Ossman255d01a2007-07-24 20:38:53 +0200645 mrq->cmd->error = -EINVAL;
646 mmc_request_done(mmc, mrq);
647 return;
648 }
649
Linus Walleij9e943022008-10-24 21:17:50 +0100650 spin_lock_irqsave(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 host->mrq = mrq;
653
654 if (mrq->data && mrq->data->flags & MMC_DATA_READ)
655 mmci_start_data(host, mrq->data);
656
657 mmci_start_command(host, mrq->cmd, 0);
658
Linus Walleij9e943022008-10-24 21:17:50 +0100659 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
662static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
663{
664 struct mmci_host *host = mmc_priv(mmc);
Linus Walleija6a64642009-09-14 12:56:14 +0100665 u32 pwr = 0;
666 unsigned long flags;
Linus Walleij99fc5132010-09-29 01:08:27 -0400667 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 switch (ios->power_mode) {
670 case MMC_POWER_OFF:
Linus Walleij99fc5132010-09-29 01:08:27 -0400671 if (host->vcc)
672 ret = mmc_regulator_set_ocr(mmc, host->vcc, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 break;
674 case MMC_POWER_UP:
Linus Walleij99fc5132010-09-29 01:08:27 -0400675 if (host->vcc) {
676 ret = mmc_regulator_set_ocr(mmc, host->vcc, ios->vdd);
677 if (ret) {
678 dev_err(mmc_dev(mmc), "unable to set OCR\n");
679 /*
680 * The .set_ios() function in the mmc_host_ops
681 * struct return void, and failing to set the
682 * power should be rare so we print an error
683 * and return here.
684 */
685 return;
686 }
687 }
Rabin Vincentbb8f5632010-07-21 12:53:57 +0100688 if (host->plat->vdd_handler)
689 pwr |= host->plat->vdd_handler(mmc_dev(mmc), ios->vdd,
690 ios->power_mode);
Linus Walleijcc30d602009-01-04 15:18:54 +0100691 /* The ST version does not have this, fall through to POWER_ON */
Linus Walleijf17a1f02009-08-04 01:01:02 +0100692 if (host->hw_designer != AMBA_VENDOR_ST) {
Linus Walleijcc30d602009-01-04 15:18:54 +0100693 pwr |= MCI_PWR_UP;
694 break;
695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 case MMC_POWER_ON:
697 pwr |= MCI_PWR_ON;
698 break;
699 }
700
Linus Walleijcc30d602009-01-04 15:18:54 +0100701 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN) {
Linus Walleijf17a1f02009-08-04 01:01:02 +0100702 if (host->hw_designer != AMBA_VENDOR_ST)
Linus Walleijcc30d602009-01-04 15:18:54 +0100703 pwr |= MCI_ROD;
704 else {
705 /*
706 * The ST Micro variant use the ROD bit for something
707 * else and only has OD (Open Drain).
708 */
709 pwr |= MCI_OD;
710 }
711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Linus Walleija6a64642009-09-14 12:56:14 +0100713 spin_lock_irqsave(&host->lock, flags);
714
715 mmci_set_clkreg(host, ios->clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 if (host->pwr != pwr) {
718 host->pwr = pwr;
719 writel(pwr, host->base + MMCIPOWER);
720 }
Linus Walleija6a64642009-09-14 12:56:14 +0100721
722 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723}
724
Russell King89001442009-07-09 15:16:07 +0100725static int mmci_get_ro(struct mmc_host *mmc)
726{
727 struct mmci_host *host = mmc_priv(mmc);
728
729 if (host->gpio_wp == -ENOSYS)
730 return -ENOSYS;
731
Linus Walleij18a063012010-09-12 12:56:44 +0100732 return gpio_get_value_cansleep(host->gpio_wp);
Russell King89001442009-07-09 15:16:07 +0100733}
734
735static int mmci_get_cd(struct mmc_host *mmc)
736{
737 struct mmci_host *host = mmc_priv(mmc);
Rabin Vincent29719442010-08-09 12:54:43 +0100738 struct mmci_platform_data *plat = host->plat;
Russell King89001442009-07-09 15:16:07 +0100739 unsigned int status;
740
Rabin Vincent4b8caec2010-08-09 12:56:40 +0100741 if (host->gpio_cd == -ENOSYS) {
742 if (!plat->status)
743 return 1; /* Assume always present */
744
Rabin Vincent29719442010-08-09 12:54:43 +0100745 status = plat->status(mmc_dev(host->mmc));
Rabin Vincent4b8caec2010-08-09 12:56:40 +0100746 } else
Linus Walleij18a063012010-09-12 12:56:44 +0100747 status = !!gpio_get_value_cansleep(host->gpio_cd)
748 ^ plat->cd_invert;
Russell King89001442009-07-09 15:16:07 +0100749
Russell King74bc8092010-07-29 15:58:59 +0100750 /*
751 * Use positive logic throughout - status is zero for no card,
752 * non-zero for card inserted.
753 */
754 return status;
Russell King89001442009-07-09 15:16:07 +0100755}
756
Rabin Vincent148b8b32010-08-09 12:55:48 +0100757static irqreturn_t mmci_cd_irq(int irq, void *dev_id)
758{
759 struct mmci_host *host = dev_id;
760
761 mmc_detect_change(host->mmc, msecs_to_jiffies(500));
762
763 return IRQ_HANDLED;
764}
765
David Brownellab7aefd2006-11-12 17:55:30 -0800766static const struct mmc_host_ops mmci_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 .request = mmci_request,
768 .set_ios = mmci_set_ios,
Russell King89001442009-07-09 15:16:07 +0100769 .get_ro = mmci_get_ro,
770 .get_cd = mmci_get_cd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771};
772
Alessandro Rubini03fbdb12009-05-20 22:39:08 +0100773static int __devinit mmci_probe(struct amba_device *dev, struct amba_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
Linus Walleij6ef297f2009-09-22 14:29:36 +0100775 struct mmci_platform_data *plat = dev->dev.platform_data;
Rabin Vincent4956e102010-07-21 12:54:40 +0100776 struct variant_data *variant = id->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 struct mmci_host *host;
778 struct mmc_host *mmc;
Linus Walleij2686b4b2010-10-19 12:39:48 +0100779 unsigned int mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 int ret;
781
782 /* must have platform data */
783 if (!plat) {
784 ret = -EINVAL;
785 goto out;
786 }
787
788 ret = amba_request_regions(dev, DRIVER_NAME);
789 if (ret)
790 goto out;
791
792 mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
793 if (!mmc) {
794 ret = -ENOMEM;
795 goto rel_regions;
796 }
797
798 host = mmc_priv(mmc);
Rabin Vincent4ea580f2009-04-17 08:44:19 +0530799 host->mmc = mmc;
Russell King012b7d32009-07-09 15:13:56 +0100800
Russell King89001442009-07-09 15:16:07 +0100801 host->gpio_wp = -ENOSYS;
802 host->gpio_cd = -ENOSYS;
Rabin Vincent148b8b32010-08-09 12:55:48 +0100803 host->gpio_cd_irq = -1;
Russell King89001442009-07-09 15:16:07 +0100804
Russell King012b7d32009-07-09 15:13:56 +0100805 host->hw_designer = amba_manf(dev);
806 host->hw_revision = amba_rev(dev);
Linus Walleij64de0282010-02-19 01:09:10 +0100807 dev_dbg(mmc_dev(mmc), "designer ID = 0x%02x\n", host->hw_designer);
808 dev_dbg(mmc_dev(mmc), "revision = 0x%01x\n", host->hw_revision);
Russell King012b7d32009-07-09 15:13:56 +0100809
Russell Kingee569c42008-11-30 17:38:14 +0000810 host->clk = clk_get(&dev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if (IS_ERR(host->clk)) {
812 ret = PTR_ERR(host->clk);
813 host->clk = NULL;
814 goto host_free;
815 }
816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 ret = clk_enable(host->clk);
818 if (ret)
Russell Kinga8d35842006-01-03 18:41:37 +0000819 goto clk_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 host->plat = plat;
Rabin Vincent4956e102010-07-21 12:54:40 +0100822 host->variant = variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 host->mclk = clk_get_rate(host->clk);
Linus Walleijc8df9a52008-04-29 09:34:07 +0100824 /*
825 * According to the spec, mclk is max 100 MHz,
826 * so we try to adjust the clock down to this,
827 * (if possible).
828 */
829 if (host->mclk > 100000000) {
830 ret = clk_set_rate(host->clk, 100000000);
831 if (ret < 0)
832 goto clk_disable;
833 host->mclk = clk_get_rate(host->clk);
Linus Walleij64de0282010-02-19 01:09:10 +0100834 dev_dbg(mmc_dev(mmc), "eventual mclk rate: %u Hz\n",
835 host->mclk);
Linus Walleijc8df9a52008-04-29 09:34:07 +0100836 }
Linus Walleijdc890c22009-06-07 23:27:31 +0100837 host->base = ioremap(dev->res.start, resource_size(&dev->res));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (!host->base) {
839 ret = -ENOMEM;
840 goto clk_disable;
841 }
842
843 mmc->ops = &mmci_ops;
844 mmc->f_min = (host->mclk + 511) / 512;
Linus Walleij808d97c2010-04-08 07:39:38 +0100845 /*
846 * If the platform data supplies a maximum operating
847 * frequency, this takes precedence. Else, we fall back
848 * to using the module parameter, which has a (low)
849 * default value in case it is not specified. Either
850 * value must not exceed the clock rate into the block,
851 * of course.
852 */
853 if (plat->f_max)
854 mmc->f_max = min(host->mclk, plat->f_max);
855 else
856 mmc->f_max = min(host->mclk, fmax);
Linus Walleij64de0282010-02-19 01:09:10 +0100857 dev_dbg(mmc_dev(mmc), "clocking block at %u Hz\n", mmc->f_max);
858
Linus Walleij34e84f32009-09-22 14:41:40 +0100859#ifdef CONFIG_REGULATOR
860 /* If we're using the regulator framework, try to fetch a regulator */
861 host->vcc = regulator_get(&dev->dev, "vmmc");
862 if (IS_ERR(host->vcc))
863 host->vcc = NULL;
864 else {
865 int mask = mmc_regulator_get_ocrmask(host->vcc);
866
867 if (mask < 0)
868 dev_err(&dev->dev, "error getting OCR mask (%d)\n",
869 mask);
870 else {
871 host->mmc->ocr_avail = (u32) mask;
872 if (plat->ocr_mask)
873 dev_warn(&dev->dev,
874 "Provided ocr_mask/setpower will not be used "
875 "(using regulator instead)\n");
876 }
877 }
878#endif
879 /* Fall back to platform data if no regulator is found */
880 if (host->vcc == NULL)
881 mmc->ocr_avail = plat->ocr_mask;
Linus Walleij9e6c82c2009-09-14 12:57:11 +0100882 mmc->caps = plat->capabilities;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884 /*
885 * We can do SGIO
886 */
Martin K. Petersena36274e2010-09-10 01:33:59 -0400887 mmc->max_segs = NR_SG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 /*
Rabin Vincent08458ef2010-07-21 12:55:59 +0100890 * Since only a certain number of bits are valid in the data length
891 * register, we must ensure that we don't exceed 2^num-1 bytes in a
892 * single request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 */
Rabin Vincent08458ef2010-07-21 12:55:59 +0100894 mmc->max_req_size = (1 << variant->datalength_bits) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 /*
897 * Set the maximum segment size. Since we aren't doing DMA
898 * (yet) we are only limited by the data length register.
899 */
Pierre Ossman55db8902006-11-21 17:55:45 +0100900 mmc->max_seg_size = mmc->max_req_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100902 /*
903 * Block size can be up to 2048 bytes, but must be a power of two.
904 */
905 mmc->max_blk_size = 2048;
906
Pierre Ossman55db8902006-11-21 17:55:45 +0100907 /*
908 * No limit on the number of blocks transferred.
909 */
910 mmc->max_blk_count = mmc->max_req_size;
911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 spin_lock_init(&host->lock);
913
914 writel(0, host->base + MMCIMASK0);
915 writel(0, host->base + MMCIMASK1);
916 writel(0xfff, host->base + MMCICLEAR);
917
Russell King89001442009-07-09 15:16:07 +0100918 if (gpio_is_valid(plat->gpio_cd)) {
919 ret = gpio_request(plat->gpio_cd, DRIVER_NAME " (cd)");
920 if (ret == 0)
921 ret = gpio_direction_input(plat->gpio_cd);
922 if (ret == 0)
923 host->gpio_cd = plat->gpio_cd;
924 else if (ret != -ENOSYS)
925 goto err_gpio_cd;
Rabin Vincent148b8b32010-08-09 12:55:48 +0100926
927 ret = request_any_context_irq(gpio_to_irq(plat->gpio_cd),
928 mmci_cd_irq, 0,
929 DRIVER_NAME " (cd)", host);
930 if (ret >= 0)
931 host->gpio_cd_irq = gpio_to_irq(plat->gpio_cd);
Russell King89001442009-07-09 15:16:07 +0100932 }
933 if (gpio_is_valid(plat->gpio_wp)) {
934 ret = gpio_request(plat->gpio_wp, DRIVER_NAME " (wp)");
935 if (ret == 0)
936 ret = gpio_direction_input(plat->gpio_wp);
937 if (ret == 0)
938 host->gpio_wp = plat->gpio_wp;
939 else if (ret != -ENOSYS)
940 goto err_gpio_wp;
941 }
942
Rabin Vincent4b8caec2010-08-09 12:56:40 +0100943 if ((host->plat->status || host->gpio_cd != -ENOSYS)
944 && host->gpio_cd_irq < 0)
Rabin Vincent148b8b32010-08-09 12:55:48 +0100945 mmc->caps |= MMC_CAP_NEEDS_POLL;
946
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700947 ret = request_irq(dev->irq[0], mmci_irq, IRQF_SHARED, DRIVER_NAME " (cmd)", host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 if (ret)
949 goto unmap;
950
Linus Walleij2686b4b2010-10-19 12:39:48 +0100951 if (dev->irq[1] == NO_IRQ)
952 host->singleirq = true;
953 else {
954 ret = request_irq(dev->irq[1], mmci_pio_irq, IRQF_SHARED,
955 DRIVER_NAME " (pio)", host);
956 if (ret)
957 goto irq0_free;
958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Linus Walleij2686b4b2010-10-19 12:39:48 +0100960 mask = MCI_IRQENABLE;
Linus Walleijf20f8f22010-10-19 13:41:24 +0100961 /* Don't use the datablockend flag if it's broken */
962 if (variant->broken_blockend)
963 mask &= ~MCI_DATABLOCKEND;
964
Linus Walleij2686b4b2010-10-19 12:39:48 +0100965 writel(mask, host->base + MMCIMASK0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 amba_set_drvdata(dev, mmc);
968
969 mmc_add_host(mmc);
970
Linus Walleij64de0282010-02-19 01:09:10 +0100971 dev_info(&dev->dev, "%s: MMCI rev %x cfg %02x at 0x%016llx irq %d,%d\n",
Russell Kingd366b642005-08-19 09:40:08 +0100972 mmc_hostname(mmc), amba_rev(dev), amba_config(dev),
Greg Kroah-Hartmane29419f2006-06-12 15:20:16 -0700973 (unsigned long long)dev->res.start, dev->irq[0], dev->irq[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 return 0;
976
977 irq0_free:
978 free_irq(dev->irq[0], host);
979 unmap:
Russell King89001442009-07-09 15:16:07 +0100980 if (host->gpio_wp != -ENOSYS)
981 gpio_free(host->gpio_wp);
982 err_gpio_wp:
Rabin Vincent148b8b32010-08-09 12:55:48 +0100983 if (host->gpio_cd_irq >= 0)
984 free_irq(host->gpio_cd_irq, host);
Russell King89001442009-07-09 15:16:07 +0100985 if (host->gpio_cd != -ENOSYS)
986 gpio_free(host->gpio_cd);
987 err_gpio_cd:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 iounmap(host->base);
989 clk_disable:
990 clk_disable(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 clk_free:
992 clk_put(host->clk);
993 host_free:
994 mmc_free_host(mmc);
995 rel_regions:
996 amba_release_regions(dev);
997 out:
998 return ret;
999}
1000
Linus Walleij6dc4a472009-03-07 00:23:52 +01001001static int __devexit mmci_remove(struct amba_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
1003 struct mmc_host *mmc = amba_get_drvdata(dev);
1004
1005 amba_set_drvdata(dev, NULL);
1006
1007 if (mmc) {
1008 struct mmci_host *host = mmc_priv(mmc);
1009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 mmc_remove_host(mmc);
1011
1012 writel(0, host->base + MMCIMASK0);
1013 writel(0, host->base + MMCIMASK1);
1014
1015 writel(0, host->base + MMCICOMMAND);
1016 writel(0, host->base + MMCIDATACTRL);
1017
1018 free_irq(dev->irq[0], host);
Linus Walleij2686b4b2010-10-19 12:39:48 +01001019 if (!host->singleirq)
1020 free_irq(dev->irq[1], host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Russell King89001442009-07-09 15:16:07 +01001022 if (host->gpio_wp != -ENOSYS)
1023 gpio_free(host->gpio_wp);
Rabin Vincent148b8b32010-08-09 12:55:48 +01001024 if (host->gpio_cd_irq >= 0)
1025 free_irq(host->gpio_cd_irq, host);
Russell King89001442009-07-09 15:16:07 +01001026 if (host->gpio_cd != -ENOSYS)
1027 gpio_free(host->gpio_cd);
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 iounmap(host->base);
1030 clk_disable(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 clk_put(host->clk);
1032
Linus Walleij99fc5132010-09-29 01:08:27 -04001033 if (host->vcc)
1034 mmc_regulator_set_ocr(mmc, host->vcc, 0);
Linus Walleij34e84f32009-09-22 14:41:40 +01001035 regulator_put(host->vcc);
1036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 mmc_free_host(mmc);
1038
1039 amba_release_regions(dev);
1040 }
1041
1042 return 0;
1043}
1044
1045#ifdef CONFIG_PM
Pavel Macheke5378ca2005-04-16 15:25:29 -07001046static int mmci_suspend(struct amba_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
1048 struct mmc_host *mmc = amba_get_drvdata(dev);
1049 int ret = 0;
1050
1051 if (mmc) {
1052 struct mmci_host *host = mmc_priv(mmc);
1053
Matt Fleming1a13f8f2010-05-26 14:42:08 -07001054 ret = mmc_suspend_host(mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 if (ret == 0)
1056 writel(0, host->base + MMCIMASK0);
1057 }
1058
1059 return ret;
1060}
1061
1062static int mmci_resume(struct amba_device *dev)
1063{
1064 struct mmc_host *mmc = amba_get_drvdata(dev);
1065 int ret = 0;
1066
1067 if (mmc) {
1068 struct mmci_host *host = mmc_priv(mmc);
1069
1070 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
1071
1072 ret = mmc_resume_host(mmc);
1073 }
1074
1075 return ret;
1076}
1077#else
1078#define mmci_suspend NULL
1079#define mmci_resume NULL
1080#endif
1081
1082static struct amba_id mmci_ids[] = {
1083 {
1084 .id = 0x00041180,
1085 .mask = 0x000fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001086 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 },
1088 {
1089 .id = 0x00041181,
1090 .mask = 0x000fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001091 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 },
Linus Walleijcc30d602009-01-04 15:18:54 +01001093 /* ST Micro variants */
1094 {
1095 .id = 0x00180180,
1096 .mask = 0x00ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001097 .data = &variant_u300,
Linus Walleijcc30d602009-01-04 15:18:54 +01001098 },
1099 {
1100 .id = 0x00280180,
1101 .mask = 0x00ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001102 .data = &variant_u300,
1103 },
1104 {
1105 .id = 0x00480180,
1106 .mask = 0x00ffffff,
1107 .data = &variant_ux500,
Linus Walleijcc30d602009-01-04 15:18:54 +01001108 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 { 0, 0 },
1110};
1111
1112static struct amba_driver mmci_driver = {
1113 .drv = {
1114 .name = DRIVER_NAME,
1115 },
1116 .probe = mmci_probe,
Linus Walleij6dc4a472009-03-07 00:23:52 +01001117 .remove = __devexit_p(mmci_remove),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 .suspend = mmci_suspend,
1119 .resume = mmci_resume,
1120 .id_table = mmci_ids,
1121};
1122
1123static int __init mmci_init(void)
1124{
1125 return amba_driver_register(&mmci_driver);
1126}
1127
1128static void __exit mmci_exit(void)
1129{
1130 amba_driver_unregister(&mmci_driver);
1131}
1132
1133module_init(mmci_init);
1134module_exit(mmci_exit);
1135module_param(fmax, uint, 0444);
1136
1137MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
1138MODULE_LICENSE("GPL");