blob: 67f17d61b978efc2d15cc3325d77fea2eb026e51 [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>
Russell King613b1522011-01-30 21:06:53 +000017#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/delay.h>
19#include <linux/err.h>
20#include <linux/highmem.h>
Nicolas Pitre019a5f52007-10-11 01:06:03 -040021#include <linux/log2.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/mmc/host.h>
Linus Walleij34177802010-10-19 12:43:58 +010023#include <linux/mmc/card.h>
Russell Kinga62c80e2006-01-07 13:52:45 +000024#include <linux/amba/bus.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000025#include <linux/clk.h>
Jens Axboebd6dee62007-10-24 09:01:09 +020026#include <linux/scatterlist.h>
Russell King89001442009-07-09 15:16:07 +010027#include <linux/gpio.h>
Linus Walleij6ef297f2009-09-22 14:29:36 +010028#include <linux/amba/mmci.h>
Linus Walleij34e84f32009-09-22 14:41:40 +010029#include <linux/regulator/consumer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Russell King7b09cda2005-07-01 12:02:59 +010031#include <asm/div64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <asm/io.h>
Russell Kingc6b8fda2005-10-28 14:05:16 +010033#include <asm/sizes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#include "mmci.h"
36
37#define DRIVER_NAME "mmci-pl18x"
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039static unsigned int fmax = 515633;
40
Rabin Vincent4956e102010-07-21 12:54:40 +010041/**
42 * struct variant_data - MMCI variant-specific quirks
43 * @clkreg: default value for MCICLOCK register
Rabin Vincent4380c142010-07-21 12:55:18 +010044 * @clkreg_enable: enable value for MMCICLOCK register
Rabin Vincent08458ef2010-07-21 12:55:59 +010045 * @datalength_bits: number of bits in the MMCIDATALENGTH register
Rabin Vincent8301bb62010-08-09 12:57:30 +010046 * @fifosize: number of bytes that can be written when MMCI_TXFIFOEMPTY
47 * is asserted (likewise for RX)
48 * @fifohalfsize: number of bytes that can be written when MCI_TXFIFOHALFEMPTY
49 * is asserted (likewise for RX)
Linus Walleij34177802010-10-19 12:43:58 +010050 * @sdio: variant supports SDIO
Linus Walleijb70a67f2010-12-06 09:24:14 +010051 * @st_clkdiv: true if using a ST-specific clock divider algorithm
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 Walleij34177802010-10-19 12:43:58 +010059 bool sdio;
Linus Walleijb70a67f2010-12-06 09:24:14 +010060 bool st_clkdiv;
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 Walleij34177802010-10-19 12:43:58 +010074 .sdio = 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 Walleij34177802010-10-19 12:43:58 +010083 .sdio = true,
Linus Walleijb70a67f2010-12-06 09:24:14 +010084 .st_clkdiv = true,
Rabin Vincent4956e102010-07-21 12:54:40 +010085};
Linus Walleijb70a67f2010-12-06 09:24:14 +010086
Linus Walleija6a64642009-09-14 12:56:14 +010087/*
88 * This must be called with host->lock held
89 */
90static void mmci_set_clkreg(struct mmci_host *host, unsigned int desired)
91{
Rabin Vincent4956e102010-07-21 12:54:40 +010092 struct variant_data *variant = host->variant;
93 u32 clk = variant->clkreg;
Linus Walleija6a64642009-09-14 12:56:14 +010094
95 if (desired) {
96 if (desired >= host->mclk) {
Linus Walleij991a86e2010-12-10 09:35:53 +010097 clk = MCI_CLK_BYPASS;
Linus Walleija6a64642009-09-14 12:56:14 +010098 host->cclk = host->mclk;
Linus Walleijb70a67f2010-12-06 09:24:14 +010099 } else if (variant->st_clkdiv) {
100 /*
101 * DB8500 TRM says f = mclk / (clkdiv + 2)
102 * => clkdiv = (mclk / f) - 2
103 * Round the divider up so we don't exceed the max
104 * frequency
105 */
106 clk = DIV_ROUND_UP(host->mclk, desired) - 2;
107 if (clk >= 256)
108 clk = 255;
109 host->cclk = host->mclk / (clk + 2);
Linus Walleija6a64642009-09-14 12:56:14 +0100110 } else {
Linus Walleijb70a67f2010-12-06 09:24:14 +0100111 /*
112 * PL180 TRM says f = mclk / (2 * (clkdiv + 1))
113 * => clkdiv = mclk / (2 * f) - 1
114 */
Linus Walleija6a64642009-09-14 12:56:14 +0100115 clk = host->mclk / (2 * desired) - 1;
116 if (clk >= 256)
117 clk = 255;
118 host->cclk = host->mclk / (2 * (clk + 1));
119 }
Rabin Vincent4380c142010-07-21 12:55:18 +0100120
121 clk |= variant->clkreg_enable;
Linus Walleija6a64642009-09-14 12:56:14 +0100122 clk |= MCI_CLK_ENABLE;
123 /* This hasn't proven to be worthwhile */
124 /* clk |= MCI_CLK_PWRSAVE; */
125 }
126
Linus Walleij9e6c82c2009-09-14 12:57:11 +0100127 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4)
Linus Walleij771dc152010-04-08 07:38:52 +0100128 clk |= MCI_4BIT_BUS;
129 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_8)
130 clk |= MCI_ST_8BIT_BUS;
Linus Walleij9e6c82c2009-09-14 12:57:11 +0100131
Linus Walleija6a64642009-09-14 12:56:14 +0100132 writel(clk, host->base + MMCICLOCK);
133}
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135static void
136mmci_request_end(struct mmci_host *host, struct mmc_request *mrq)
137{
138 writel(0, host->base + MMCICOMMAND);
139
Russell Kinge47c2222007-01-08 16:42:51 +0000140 BUG_ON(host->data);
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 host->mrq = NULL;
143 host->cmd = NULL;
144
145 if (mrq->data)
146 mrq->data->bytes_xfered = host->data_xfered;
147
148 /*
149 * Need to drop the host lock here; mmc_request_done may call
150 * back into the driver...
151 */
152 spin_unlock(&host->lock);
153 mmc_request_done(host->mmc, mrq);
154 spin_lock(&host->lock);
155}
156
Linus Walleij2686b4b2010-10-19 12:39:48 +0100157static void mmci_set_mask1(struct mmci_host *host, unsigned int mask)
158{
159 void __iomem *base = host->base;
160
161 if (host->singleirq) {
162 unsigned int mask0 = readl(base + MMCIMASK0);
163
164 mask0 &= ~MCI_IRQ1MASK;
165 mask0 |= mask;
166
167 writel(mask0, base + MMCIMASK0);
168 }
169
170 writel(mask, base + MMCIMASK1);
171}
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173static void mmci_stop_data(struct mmci_host *host)
174{
175 writel(0, host->base + MMCIDATACTRL);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100176 mmci_set_mask1(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 host->data = NULL;
178}
179
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100180static void mmci_init_sg(struct mmci_host *host, struct mmc_data *data)
181{
182 unsigned int flags = SG_MITER_ATOMIC;
183
184 if (data->flags & MMC_DATA_READ)
185 flags |= SG_MITER_TO_SG;
186 else
187 flags |= SG_MITER_FROM_SG;
188
189 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
190}
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192static void mmci_start_data(struct mmci_host *host, struct mmc_data *data)
193{
Rabin Vincent8301bb62010-08-09 12:57:30 +0100194 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 unsigned int datactrl, timeout, irqmask;
Russell King7b09cda2005-07-01 12:02:59 +0100196 unsigned long long clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 void __iomem *base;
Russell King3bc87f22006-08-27 13:51:28 +0100198 int blksz_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Linus Walleij64de0282010-02-19 01:09:10 +0100200 dev_dbg(mmc_dev(host->mmc), "blksz %04x blks %04x flags %08x\n",
201 data->blksz, data->blocks, data->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 host->data = data;
Rabin Vincent528320d2010-07-21 12:49:49 +0100204 host->size = data->blksz * data->blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 host->data_xfered = 0;
206
207 mmci_init_sg(host, data);
208
Russell King7b09cda2005-07-01 12:02:59 +0100209 clks = (unsigned long long)data->timeout_ns * host->cclk;
210 do_div(clks, 1000000000UL);
211
212 timeout = data->timeout_clks + (unsigned int)clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 base = host->base;
215 writel(timeout, base + MMCIDATATIMER);
216 writel(host->size, base + MMCIDATALENGTH);
217
Russell King3bc87f22006-08-27 13:51:28 +0100218 blksz_bits = ffs(data->blksz) - 1;
219 BUG_ON(1 << blksz_bits != data->blksz);
220
221 datactrl = MCI_DPSM_ENABLE | blksz_bits << 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 if (data->flags & MMC_DATA_READ) {
223 datactrl |= MCI_DPSM_DIRECTION;
224 irqmask = MCI_RXFIFOHALFFULLMASK;
Russell King0425a142006-02-16 16:48:31 +0000225
226 /*
227 * If we have less than a FIFOSIZE of bytes to transfer,
228 * trigger a PIO interrupt as soon as any data is available.
229 */
Rabin Vincent8301bb62010-08-09 12:57:30 +0100230 if (host->size < variant->fifosize)
Russell King0425a142006-02-16 16:48:31 +0000231 irqmask |= MCI_RXDATAAVLBLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 } else {
233 /*
234 * We don't actually need to include "FIFO empty" here
235 * since its implicit in "FIFO half empty".
236 */
237 irqmask = MCI_TXFIFOHALFEMPTYMASK;
238 }
239
Linus Walleij34177802010-10-19 12:43:58 +0100240 /* The ST Micro variants has a special bit to enable SDIO */
241 if (variant->sdio && host->mmc->card)
242 if (mmc_card_sdio(host->mmc->card))
243 datactrl |= MCI_ST_DPSM_SDIOEN;
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 writel(datactrl, base + MMCIDATACTRL);
246 writel(readl(base + MMCIMASK0) & ~MCI_DATAENDMASK, base + MMCIMASK0);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100247 mmci_set_mask1(host, irqmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
250static void
251mmci_start_command(struct mmci_host *host, struct mmc_command *cmd, u32 c)
252{
253 void __iomem *base = host->base;
254
Linus Walleij64de0282010-02-19 01:09:10 +0100255 dev_dbg(mmc_dev(host->mmc), "op %02x arg %08x flags %08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 cmd->opcode, cmd->arg, cmd->flags);
257
258 if (readl(base + MMCICOMMAND) & MCI_CPSM_ENABLE) {
259 writel(0, base + MMCICOMMAND);
260 udelay(1);
261 }
262
263 c |= cmd->opcode | MCI_CPSM_ENABLE;
Russell Kinge9225172006-02-02 12:23:12 +0000264 if (cmd->flags & MMC_RSP_PRESENT) {
265 if (cmd->flags & MMC_RSP_136)
266 c |= MCI_CPSM_LONGRSP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 c |= MCI_CPSM_RESPONSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
269 if (/*interrupt*/0)
270 c |= MCI_CPSM_INTERRUPT;
271
272 host->cmd = cmd;
273
274 writel(cmd->arg, base + MMCIARGUMENT);
275 writel(c, base + MMCICOMMAND);
276}
277
278static void
279mmci_data_irq(struct mmci_host *host, struct mmc_data *data,
280 unsigned int status)
281{
Linus Walleijf20f8f22010-10-19 13:41:24 +0100282 /* First check for errors */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|MCI_RXOVERRUN)) {
Linus Walleij8cb28152011-01-24 15:22:13 +0100284 u32 remain, success;
Linus Walleijf20f8f22010-10-19 13:41:24 +0100285
Linus Walleij8cb28152011-01-24 15:22:13 +0100286 /* Calculate how far we are into the transfer */
Linus Walleijf5a106d2011-01-27 17:44:34 +0100287 remain = readl(host->base + MMCIDATACNT);
Linus Walleij8cb28152011-01-24 15:22:13 +0100288 success = data->blksz * data->blocks - remain;
289
290 dev_dbg(mmc_dev(host->mmc), "MCI ERROR IRQ (status %08x)\n", status);
291 if (status & MCI_DATACRCFAIL) {
292 /* Last block was not successful */
Russell King613b1522011-01-30 21:06:53 +0000293 host->data_xfered = round_down(success - 1, data->blksz);
Linus Walleij8cb28152011-01-24 15:22:13 +0100294 data->error = -EILSEQ;
295 } else if (status & MCI_DATATIMEOUT) {
Russell King613b1522011-01-30 21:06:53 +0000296 host->data_xfered = round_down(success, data->blksz);
Linus Walleij8cb28152011-01-24 15:22:13 +0100297 data->error = -ETIMEDOUT;
298 } else if (status & (MCI_TXUNDERRUN|MCI_RXOVERRUN)) {
Russell King613b1522011-01-30 21:06:53 +0000299 host->data_xfered = round_down(success, data->blksz);
Linus Walleij8cb28152011-01-24 15:22:13 +0100300 data->error = -EIO;
301 }
Russell Kinge9c091b2006-01-04 16:24:05 +0000302
303 /*
304 * We hit an error condition. Ensure that any data
305 * partially written to a page is properly coherent.
306 */
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100307 if (data->flags & MMC_DATA_READ) {
308 struct sg_mapping_iter *sg_miter = &host->sg_miter;
309 unsigned long flags;
310
311 local_irq_save(flags);
312 if (sg_miter_next(sg_miter)) {
313 flush_dcache_page(sg_miter->page);
314 sg_miter_stop(sg_miter);
315 }
316 local_irq_restore(flags);
317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 }
Linus Walleijf20f8f22010-10-19 13:41:24 +0100319
Linus Walleij8cb28152011-01-24 15:22:13 +0100320 if (status & MCI_DATABLOCKEND)
321 dev_err(mmc_dev(host->mmc), "stray MCI_DATABLOCKEND interrupt\n");
Linus Walleijf20f8f22010-10-19 13:41:24 +0100322
Russell Kingccff9b52011-01-30 21:03:50 +0000323 if (status & MCI_DATAEND || data->error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 mmci_stop_data(host);
325
Linus Walleij8cb28152011-01-24 15:22:13 +0100326 if (!data->error)
327 /* The error clause is handled above, success! */
Linus Walleijf20f8f22010-10-19 13:41:24 +0100328 host->data_xfered += data->blksz * data->blocks;
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (!data->stop) {
331 mmci_request_end(host, data->mrq);
332 } else {
333 mmci_start_command(host, data->stop, 0);
334 }
335 }
336}
337
338static void
339mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
340 unsigned int status)
341{
342 void __iomem *base = host->base;
343
344 host->cmd = NULL;
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (status & MCI_CMDTIMEOUT) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200347 cmd->error = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200349 cmd->error = -EILSEQ;
Russell King - ARM Linux9047b432011-01-11 16:35:56 +0000350 } else {
351 cmd->resp[0] = readl(base + MMCIRESPONSE0);
352 cmd->resp[1] = readl(base + MMCIRESPONSE1);
353 cmd->resp[2] = readl(base + MMCIRESPONSE2);
354 cmd->resp[3] = readl(base + MMCIRESPONSE3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 }
356
Pierre Ossman17b04292007-07-22 22:18:46 +0200357 if (!cmd->data || cmd->error) {
Russell Kinge47c2222007-01-08 16:42:51 +0000358 if (host->data)
359 mmci_stop_data(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 mmci_request_end(host, cmd->mrq);
361 } else if (!(cmd->data->flags & MMC_DATA_READ)) {
362 mmci_start_data(host, cmd->data);
363 }
364}
365
366static int mmci_pio_read(struct mmci_host *host, char *buffer, unsigned int remain)
367{
368 void __iomem *base = host->base;
369 char *ptr = buffer;
370 u32 status;
Linus Walleij26eed9a2008-04-26 23:39:44 +0100371 int host_remain = host->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 do {
Linus Walleij26eed9a2008-04-26 23:39:44 +0100374 int count = host_remain - (readl(base + MMCIFIFOCNT) << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 if (count > remain)
377 count = remain;
378
379 if (count <= 0)
380 break;
381
382 readsl(base + MMCIFIFO, ptr, count >> 2);
383
384 ptr += count;
385 remain -= count;
Linus Walleij26eed9a2008-04-26 23:39:44 +0100386 host_remain -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 if (remain == 0)
389 break;
390
391 status = readl(base + MMCISTATUS);
392 } while (status & MCI_RXDATAAVLBL);
393
394 return ptr - buffer;
395}
396
397static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status)
398{
Rabin Vincent8301bb62010-08-09 12:57:30 +0100399 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 void __iomem *base = host->base;
401 char *ptr = buffer;
402
403 do {
404 unsigned int count, maxcnt;
405
Rabin Vincent8301bb62010-08-09 12:57:30 +0100406 maxcnt = status & MCI_TXFIFOEMPTY ?
407 variant->fifosize : variant->fifohalfsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 count = min(remain, maxcnt);
409
Linus Walleij34177802010-10-19 12:43:58 +0100410 /*
411 * The ST Micro variant for SDIO transfer sizes
412 * less then 8 bytes should have clock H/W flow
413 * control disabled.
414 */
415 if (variant->sdio &&
416 mmc_card_sdio(host->mmc->card)) {
417 if (count < 8)
418 writel(readl(host->base + MMCICLOCK) &
419 ~variant->clkreg_enable,
420 host->base + MMCICLOCK);
421 else
422 writel(readl(host->base + MMCICLOCK) |
423 variant->clkreg_enable,
424 host->base + MMCICLOCK);
425 }
426
427 /*
428 * SDIO especially may want to send something that is
429 * not divisible by 4 (as opposed to card sectors
430 * etc), and the FIFO only accept full 32-bit writes.
431 * So compensate by adding +3 on the count, a single
432 * byte become a 32bit write, 7 bytes will be two
433 * 32bit writes etc.
434 */
435 writesl(base + MMCIFIFO, ptr, (count + 3) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 ptr += count;
438 remain -= count;
439
440 if (remain == 0)
441 break;
442
443 status = readl(base + MMCISTATUS);
444 } while (status & MCI_TXFIFOHALFEMPTY);
445
446 return ptr - buffer;
447}
448
449/*
450 * PIO data transfer IRQ handler.
451 */
David Howells7d12e782006-10-05 14:55:46 +0100452static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
454 struct mmci_host *host = dev_id;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100455 struct sg_mapping_iter *sg_miter = &host->sg_miter;
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;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100458 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 u32 status;
460
461 status = readl(base + MMCISTATUS);
462
Linus Walleij64de0282010-02-19 01:09:10 +0100463 dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100465 local_irq_save(flags);
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 unsigned int remain, len;
469 char *buffer;
470
471 /*
472 * For write, we only need to test the half-empty flag
473 * here - if the FIFO is completely empty, then by
474 * definition it is more than half empty.
475 *
476 * For read, check for data available.
477 */
478 if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL)))
479 break;
480
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100481 if (!sg_miter_next(sg_miter))
482 break;
483
484 buffer = sg_miter->addr;
485 remain = sg_miter->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 len = 0;
488 if (status & MCI_RXACTIVE)
489 len = mmci_pio_read(host, buffer, remain);
490 if (status & MCI_TXACTIVE)
491 len = mmci_pio_write(host, buffer, remain, status);
492
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100493 sg_miter->consumed = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 host->size -= len;
496 remain -= len;
497
498 if (remain)
499 break;
500
Russell Kinge9c091b2006-01-04 16:24:05 +0000501 if (status & MCI_RXACTIVE)
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100502 flush_dcache_page(sg_miter->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 status = readl(base + MMCISTATUS);
505 } while (1);
506
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100507 sg_miter_stop(sg_miter);
508
509 local_irq_restore(flags);
510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 /*
512 * If we're nearing the end of the read, switch to
513 * "any data available" mode.
514 */
Rabin Vincent8301bb62010-08-09 12:57:30 +0100515 if (status & MCI_RXACTIVE && host->size < variant->fifosize)
Linus Walleij2686b4b2010-10-19 12:39:48 +0100516 mmci_set_mask1(host, MCI_RXDATAAVLBLMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 /*
519 * If we run out of data, disable the data IRQs; this
520 * prevents a race where the FIFO becomes empty before
521 * the chip itself has disabled the data path, and
522 * stops us racing with our data end IRQ.
523 */
524 if (host->size == 0) {
Linus Walleij2686b4b2010-10-19 12:39:48 +0100525 mmci_set_mask1(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0);
527 }
528
529 return IRQ_HANDLED;
530}
531
532/*
533 * Handle completion of command and data transfers.
534 */
David Howells7d12e782006-10-05 14:55:46 +0100535static irqreturn_t mmci_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
537 struct mmci_host *host = dev_id;
538 u32 status;
539 int ret = 0;
540
541 spin_lock(&host->lock);
542
543 do {
544 struct mmc_command *cmd;
545 struct mmc_data *data;
546
547 status = readl(host->base + MMCISTATUS);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100548
549 if (host->singleirq) {
550 if (status & readl(host->base + MMCIMASK1))
551 mmci_pio_irq(irq, dev_id);
552
553 status &= ~MCI_IRQ1MASK;
554 }
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 status &= readl(host->base + MMCIMASK0);
557 writel(status, host->base + MMCICLEAR);
558
Linus Walleij64de0282010-02-19 01:09:10 +0100559 dev_dbg(mmc_dev(host->mmc), "irq0 (data+cmd) %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 data = host->data;
562 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|
563 MCI_RXOVERRUN|MCI_DATAEND|MCI_DATABLOCKEND) && data)
564 mmci_data_irq(host, data, status);
565
566 cmd = host->cmd;
567 if (status & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT|MCI_CMDSENT|MCI_CMDRESPEND) && cmd)
568 mmci_cmd_irq(host, cmd, status);
569
570 ret = 1;
571 } while (status);
572
573 spin_unlock(&host->lock);
574
575 return IRQ_RETVAL(ret);
576}
577
578static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
579{
580 struct mmci_host *host = mmc_priv(mmc);
Linus Walleij9e943022008-10-24 21:17:50 +0100581 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 WARN_ON(host->mrq != NULL);
584
Nicolas Pitre019a5f52007-10-11 01:06:03 -0400585 if (mrq->data && !is_power_of_2(mrq->data->blksz)) {
Linus Walleij64de0282010-02-19 01:09:10 +0100586 dev_err(mmc_dev(mmc), "unsupported block size (%d bytes)\n",
587 mrq->data->blksz);
Pierre Ossman255d01a2007-07-24 20:38:53 +0200588 mrq->cmd->error = -EINVAL;
589 mmc_request_done(mmc, mrq);
590 return;
591 }
592
Linus Walleij9e943022008-10-24 21:17:50 +0100593 spin_lock_irqsave(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 host->mrq = mrq;
596
597 if (mrq->data && mrq->data->flags & MMC_DATA_READ)
598 mmci_start_data(host, mrq->data);
599
600 mmci_start_command(host, mrq->cmd, 0);
601
Linus Walleij9e943022008-10-24 21:17:50 +0100602 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
605static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
606{
607 struct mmci_host *host = mmc_priv(mmc);
Linus Walleija6a64642009-09-14 12:56:14 +0100608 u32 pwr = 0;
609 unsigned long flags;
Linus Walleij99fc5132010-09-29 01:08:27 -0400610 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 switch (ios->power_mode) {
613 case MMC_POWER_OFF:
Linus Walleij99fc5132010-09-29 01:08:27 -0400614 if (host->vcc)
615 ret = mmc_regulator_set_ocr(mmc, host->vcc, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 break;
617 case MMC_POWER_UP:
Linus Walleij99fc5132010-09-29 01:08:27 -0400618 if (host->vcc) {
619 ret = mmc_regulator_set_ocr(mmc, host->vcc, ios->vdd);
620 if (ret) {
621 dev_err(mmc_dev(mmc), "unable to set OCR\n");
622 /*
623 * The .set_ios() function in the mmc_host_ops
624 * struct return void, and failing to set the
625 * power should be rare so we print an error
626 * and return here.
627 */
628 return;
629 }
630 }
Rabin Vincentbb8f5632010-07-21 12:53:57 +0100631 if (host->plat->vdd_handler)
632 pwr |= host->plat->vdd_handler(mmc_dev(mmc), ios->vdd,
633 ios->power_mode);
Linus Walleijcc30d602009-01-04 15:18:54 +0100634 /* The ST version does not have this, fall through to POWER_ON */
Linus Walleijf17a1f02009-08-04 01:01:02 +0100635 if (host->hw_designer != AMBA_VENDOR_ST) {
Linus Walleijcc30d602009-01-04 15:18:54 +0100636 pwr |= MCI_PWR_UP;
637 break;
638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 case MMC_POWER_ON:
640 pwr |= MCI_PWR_ON;
641 break;
642 }
643
Linus Walleijcc30d602009-01-04 15:18:54 +0100644 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN) {
Linus Walleijf17a1f02009-08-04 01:01:02 +0100645 if (host->hw_designer != AMBA_VENDOR_ST)
Linus Walleijcc30d602009-01-04 15:18:54 +0100646 pwr |= MCI_ROD;
647 else {
648 /*
649 * The ST Micro variant use the ROD bit for something
650 * else and only has OD (Open Drain).
651 */
652 pwr |= MCI_OD;
653 }
654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Linus Walleija6a64642009-09-14 12:56:14 +0100656 spin_lock_irqsave(&host->lock, flags);
657
658 mmci_set_clkreg(host, ios->clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 if (host->pwr != pwr) {
661 host->pwr = pwr;
662 writel(pwr, host->base + MMCIPOWER);
663 }
Linus Walleija6a64642009-09-14 12:56:14 +0100664
665 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667
Russell King89001442009-07-09 15:16:07 +0100668static int mmci_get_ro(struct mmc_host *mmc)
669{
670 struct mmci_host *host = mmc_priv(mmc);
671
672 if (host->gpio_wp == -ENOSYS)
673 return -ENOSYS;
674
Linus Walleij18a06302010-09-12 12:56:44 +0100675 return gpio_get_value_cansleep(host->gpio_wp);
Russell King89001442009-07-09 15:16:07 +0100676}
677
678static int mmci_get_cd(struct mmc_host *mmc)
679{
680 struct mmci_host *host = mmc_priv(mmc);
Rabin Vincent29719442010-08-09 12:54:43 +0100681 struct mmci_platform_data *plat = host->plat;
Russell King89001442009-07-09 15:16:07 +0100682 unsigned int status;
683
Rabin Vincent4b8caec2010-08-09 12:56:40 +0100684 if (host->gpio_cd == -ENOSYS) {
685 if (!plat->status)
686 return 1; /* Assume always present */
687
Rabin Vincent29719442010-08-09 12:54:43 +0100688 status = plat->status(mmc_dev(host->mmc));
Rabin Vincent4b8caec2010-08-09 12:56:40 +0100689 } else
Linus Walleij18a06302010-09-12 12:56:44 +0100690 status = !!gpio_get_value_cansleep(host->gpio_cd)
691 ^ plat->cd_invert;
Russell King89001442009-07-09 15:16:07 +0100692
Russell King74bc8092010-07-29 15:58:59 +0100693 /*
694 * Use positive logic throughout - status is zero for no card,
695 * non-zero for card inserted.
696 */
697 return status;
Russell King89001442009-07-09 15:16:07 +0100698}
699
Rabin Vincent148b8b32010-08-09 12:55:48 +0100700static irqreturn_t mmci_cd_irq(int irq, void *dev_id)
701{
702 struct mmci_host *host = dev_id;
703
704 mmc_detect_change(host->mmc, msecs_to_jiffies(500));
705
706 return IRQ_HANDLED;
707}
708
David Brownellab7aefd2006-11-12 17:55:30 -0800709static const struct mmc_host_ops mmci_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 .request = mmci_request,
711 .set_ios = mmci_set_ios,
Russell King89001442009-07-09 15:16:07 +0100712 .get_ro = mmci_get_ro,
713 .get_cd = mmci_get_cd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714};
715
Russell Kingaa25afa2011-02-19 15:55:00 +0000716static int __devinit mmci_probe(struct amba_device *dev,
717 const struct amba_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
Linus Walleij6ef297f2009-09-22 14:29:36 +0100719 struct mmci_platform_data *plat = dev->dev.platform_data;
Rabin Vincent4956e102010-07-21 12:54:40 +0100720 struct variant_data *variant = id->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 struct mmci_host *host;
722 struct mmc_host *mmc;
723 int ret;
724
725 /* must have platform data */
726 if (!plat) {
727 ret = -EINVAL;
728 goto out;
729 }
730
731 ret = amba_request_regions(dev, DRIVER_NAME);
732 if (ret)
733 goto out;
734
735 mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
736 if (!mmc) {
737 ret = -ENOMEM;
738 goto rel_regions;
739 }
740
741 host = mmc_priv(mmc);
Rabin Vincent4ea580f2009-04-17 08:44:19 +0530742 host->mmc = mmc;
Russell King012b7d32009-07-09 15:13:56 +0100743
Russell King89001442009-07-09 15:16:07 +0100744 host->gpio_wp = -ENOSYS;
745 host->gpio_cd = -ENOSYS;
Rabin Vincent148b8b32010-08-09 12:55:48 +0100746 host->gpio_cd_irq = -1;
Russell King89001442009-07-09 15:16:07 +0100747
Russell King012b7d32009-07-09 15:13:56 +0100748 host->hw_designer = amba_manf(dev);
749 host->hw_revision = amba_rev(dev);
Linus Walleij64de0282010-02-19 01:09:10 +0100750 dev_dbg(mmc_dev(mmc), "designer ID = 0x%02x\n", host->hw_designer);
751 dev_dbg(mmc_dev(mmc), "revision = 0x%01x\n", host->hw_revision);
Russell King012b7d32009-07-09 15:13:56 +0100752
Russell Kingee569c42008-11-30 17:38:14 +0000753 host->clk = clk_get(&dev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 if (IS_ERR(host->clk)) {
755 ret = PTR_ERR(host->clk);
756 host->clk = NULL;
757 goto host_free;
758 }
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 ret = clk_enable(host->clk);
761 if (ret)
Russell Kinga8d35842006-01-03 18:41:37 +0000762 goto clk_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 host->plat = plat;
Rabin Vincent4956e102010-07-21 12:54:40 +0100765 host->variant = variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 host->mclk = clk_get_rate(host->clk);
Linus Walleijc8df9a52008-04-29 09:34:07 +0100767 /*
768 * According to the spec, mclk is max 100 MHz,
769 * so we try to adjust the clock down to this,
770 * (if possible).
771 */
772 if (host->mclk > 100000000) {
773 ret = clk_set_rate(host->clk, 100000000);
774 if (ret < 0)
775 goto clk_disable;
776 host->mclk = clk_get_rate(host->clk);
Linus Walleij64de0282010-02-19 01:09:10 +0100777 dev_dbg(mmc_dev(mmc), "eventual mclk rate: %u Hz\n",
778 host->mclk);
Linus Walleijc8df9a52008-04-29 09:34:07 +0100779 }
Linus Walleijdc890c22009-06-07 23:27:31 +0100780 host->base = ioremap(dev->res.start, resource_size(&dev->res));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 if (!host->base) {
782 ret = -ENOMEM;
783 goto clk_disable;
784 }
785
786 mmc->ops = &mmci_ops;
787 mmc->f_min = (host->mclk + 511) / 512;
Linus Walleij808d97c2010-04-08 07:39:38 +0100788 /*
789 * If the platform data supplies a maximum operating
790 * frequency, this takes precedence. Else, we fall back
791 * to using the module parameter, which has a (low)
792 * default value in case it is not specified. Either
793 * value must not exceed the clock rate into the block,
794 * of course.
795 */
796 if (plat->f_max)
797 mmc->f_max = min(host->mclk, plat->f_max);
798 else
799 mmc->f_max = min(host->mclk, fmax);
Linus Walleij64de0282010-02-19 01:09:10 +0100800 dev_dbg(mmc_dev(mmc), "clocking block at %u Hz\n", mmc->f_max);
801
Linus Walleij34e84f32009-09-22 14:41:40 +0100802#ifdef CONFIG_REGULATOR
803 /* If we're using the regulator framework, try to fetch a regulator */
804 host->vcc = regulator_get(&dev->dev, "vmmc");
805 if (IS_ERR(host->vcc))
806 host->vcc = NULL;
807 else {
808 int mask = mmc_regulator_get_ocrmask(host->vcc);
809
810 if (mask < 0)
811 dev_err(&dev->dev, "error getting OCR mask (%d)\n",
812 mask);
813 else {
814 host->mmc->ocr_avail = (u32) mask;
815 if (plat->ocr_mask)
816 dev_warn(&dev->dev,
817 "Provided ocr_mask/setpower will not be used "
818 "(using regulator instead)\n");
819 }
820 }
821#endif
822 /* Fall back to platform data if no regulator is found */
823 if (host->vcc == NULL)
824 mmc->ocr_avail = plat->ocr_mask;
Linus Walleij9e6c82c2009-09-14 12:57:11 +0100825 mmc->caps = plat->capabilities;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 /*
828 * We can do SGIO
829 */
Martin K. Petersena36274e2010-09-10 01:33:59 -0400830 mmc->max_segs = NR_SG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 /*
Rabin Vincent08458ef2010-07-21 12:55:59 +0100833 * Since only a certain number of bits are valid in the data length
834 * register, we must ensure that we don't exceed 2^num-1 bytes in a
835 * single request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 */
Rabin Vincent08458ef2010-07-21 12:55:59 +0100837 mmc->max_req_size = (1 << variant->datalength_bits) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 /*
840 * Set the maximum segment size. Since we aren't doing DMA
841 * (yet) we are only limited by the data length register.
842 */
Pierre Ossman55db8902006-11-21 17:55:45 +0100843 mmc->max_seg_size = mmc->max_req_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100845 /*
846 * Block size can be up to 2048 bytes, but must be a power of two.
847 */
848 mmc->max_blk_size = 2048;
849
Pierre Ossman55db8902006-11-21 17:55:45 +0100850 /*
851 * No limit on the number of blocks transferred.
852 */
853 mmc->max_blk_count = mmc->max_req_size;
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 spin_lock_init(&host->lock);
856
857 writel(0, host->base + MMCIMASK0);
858 writel(0, host->base + MMCIMASK1);
859 writel(0xfff, host->base + MMCICLEAR);
860
Russell King89001442009-07-09 15:16:07 +0100861 if (gpio_is_valid(plat->gpio_cd)) {
862 ret = gpio_request(plat->gpio_cd, DRIVER_NAME " (cd)");
863 if (ret == 0)
864 ret = gpio_direction_input(plat->gpio_cd);
865 if (ret == 0)
866 host->gpio_cd = plat->gpio_cd;
867 else if (ret != -ENOSYS)
868 goto err_gpio_cd;
Rabin Vincent148b8b32010-08-09 12:55:48 +0100869
870 ret = request_any_context_irq(gpio_to_irq(plat->gpio_cd),
871 mmci_cd_irq, 0,
872 DRIVER_NAME " (cd)", host);
873 if (ret >= 0)
874 host->gpio_cd_irq = gpio_to_irq(plat->gpio_cd);
Russell King89001442009-07-09 15:16:07 +0100875 }
876 if (gpio_is_valid(plat->gpio_wp)) {
877 ret = gpio_request(plat->gpio_wp, DRIVER_NAME " (wp)");
878 if (ret == 0)
879 ret = gpio_direction_input(plat->gpio_wp);
880 if (ret == 0)
881 host->gpio_wp = plat->gpio_wp;
882 else if (ret != -ENOSYS)
883 goto err_gpio_wp;
884 }
885
Rabin Vincent4b8caec2010-08-09 12:56:40 +0100886 if ((host->plat->status || host->gpio_cd != -ENOSYS)
887 && host->gpio_cd_irq < 0)
Rabin Vincent148b8b32010-08-09 12:55:48 +0100888 mmc->caps |= MMC_CAP_NEEDS_POLL;
889
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700890 ret = request_irq(dev->irq[0], mmci_irq, IRQF_SHARED, DRIVER_NAME " (cmd)", host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 if (ret)
892 goto unmap;
893
Linus Walleij2686b4b2010-10-19 12:39:48 +0100894 if (dev->irq[1] == NO_IRQ)
895 host->singleirq = true;
896 else {
897 ret = request_irq(dev->irq[1], mmci_pio_irq, IRQF_SHARED,
898 DRIVER_NAME " (pio)", host);
899 if (ret)
900 goto irq0_free;
901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Linus Walleij8cb28152011-01-24 15:22:13 +0100903 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 amba_set_drvdata(dev, mmc);
906
Russell King8c11a942010-12-28 19:40:40 +0000907 dev_info(&dev->dev, "%s: PL%03x rev%u at 0x%08llx irq %d,%d\n",
908 mmc_hostname(mmc), amba_part(dev), amba_rev(dev),
Greg Kroah-Hartmane29419f2006-06-12 15:20:16 -0700909 (unsigned long long)dev->res.start, dev->irq[0], dev->irq[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Russell King8c11a942010-12-28 19:40:40 +0000911 mmc_add_host(mmc);
912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 return 0;
914
915 irq0_free:
916 free_irq(dev->irq[0], host);
917 unmap:
Russell King89001442009-07-09 15:16:07 +0100918 if (host->gpio_wp != -ENOSYS)
919 gpio_free(host->gpio_wp);
920 err_gpio_wp:
Rabin Vincent148b8b32010-08-09 12:55:48 +0100921 if (host->gpio_cd_irq >= 0)
922 free_irq(host->gpio_cd_irq, host);
Russell King89001442009-07-09 15:16:07 +0100923 if (host->gpio_cd != -ENOSYS)
924 gpio_free(host->gpio_cd);
925 err_gpio_cd:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 iounmap(host->base);
927 clk_disable:
928 clk_disable(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 clk_free:
930 clk_put(host->clk);
931 host_free:
932 mmc_free_host(mmc);
933 rel_regions:
934 amba_release_regions(dev);
935 out:
936 return ret;
937}
938
Linus Walleij6dc4a472009-03-07 00:23:52 +0100939static int __devexit mmci_remove(struct amba_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
941 struct mmc_host *mmc = amba_get_drvdata(dev);
942
943 amba_set_drvdata(dev, NULL);
944
945 if (mmc) {
946 struct mmci_host *host = mmc_priv(mmc);
947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 mmc_remove_host(mmc);
949
950 writel(0, host->base + MMCIMASK0);
951 writel(0, host->base + MMCIMASK1);
952
953 writel(0, host->base + MMCICOMMAND);
954 writel(0, host->base + MMCIDATACTRL);
955
956 free_irq(dev->irq[0], host);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100957 if (!host->singleirq)
958 free_irq(dev->irq[1], host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Russell King89001442009-07-09 15:16:07 +0100960 if (host->gpio_wp != -ENOSYS)
961 gpio_free(host->gpio_wp);
Rabin Vincent148b8b32010-08-09 12:55:48 +0100962 if (host->gpio_cd_irq >= 0)
963 free_irq(host->gpio_cd_irq, host);
Russell King89001442009-07-09 15:16:07 +0100964 if (host->gpio_cd != -ENOSYS)
965 gpio_free(host->gpio_cd);
966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 iounmap(host->base);
968 clk_disable(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 clk_put(host->clk);
970
Linus Walleij99fc5132010-09-29 01:08:27 -0400971 if (host->vcc)
972 mmc_regulator_set_ocr(mmc, host->vcc, 0);
Linus Walleij34e84f32009-09-22 14:41:40 +0100973 regulator_put(host->vcc);
974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 mmc_free_host(mmc);
976
977 amba_release_regions(dev);
978 }
979
980 return 0;
981}
982
983#ifdef CONFIG_PM
Pavel Macheke5378ca2005-04-16 15:25:29 -0700984static int mmci_suspend(struct amba_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
986 struct mmc_host *mmc = amba_get_drvdata(dev);
987 int ret = 0;
988
989 if (mmc) {
990 struct mmci_host *host = mmc_priv(mmc);
991
Matt Fleming1a13f8f2010-05-26 14:42:08 -0700992 ret = mmc_suspend_host(mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 if (ret == 0)
994 writel(0, host->base + MMCIMASK0);
995 }
996
997 return ret;
998}
999
1000static int mmci_resume(struct amba_device *dev)
1001{
1002 struct mmc_host *mmc = amba_get_drvdata(dev);
1003 int ret = 0;
1004
1005 if (mmc) {
1006 struct mmci_host *host = mmc_priv(mmc);
1007
1008 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
1009
1010 ret = mmc_resume_host(mmc);
1011 }
1012
1013 return ret;
1014}
1015#else
1016#define mmci_suspend NULL
1017#define mmci_resume NULL
1018#endif
1019
1020static struct amba_id mmci_ids[] = {
1021 {
1022 .id = 0x00041180,
1023 .mask = 0x000fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001024 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 },
1026 {
1027 .id = 0x00041181,
1028 .mask = 0x000fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001029 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 },
Linus Walleijcc30d602009-01-04 15:18:54 +01001031 /* ST Micro variants */
1032 {
1033 .id = 0x00180180,
1034 .mask = 0x00ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001035 .data = &variant_u300,
Linus Walleijcc30d602009-01-04 15:18:54 +01001036 },
1037 {
1038 .id = 0x00280180,
1039 .mask = 0x00ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001040 .data = &variant_u300,
1041 },
1042 {
1043 .id = 0x00480180,
1044 .mask = 0x00ffffff,
1045 .data = &variant_ux500,
Linus Walleijcc30d602009-01-04 15:18:54 +01001046 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 { 0, 0 },
1048};
1049
1050static struct amba_driver mmci_driver = {
1051 .drv = {
1052 .name = DRIVER_NAME,
1053 },
1054 .probe = mmci_probe,
Linus Walleij6dc4a472009-03-07 00:23:52 +01001055 .remove = __devexit_p(mmci_remove),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 .suspend = mmci_suspend,
1057 .resume = mmci_resume,
1058 .id_table = mmci_ids,
1059};
1060
1061static int __init mmci_init(void)
1062{
1063 return amba_driver_register(&mmci_driver);
1064}
1065
1066static void __exit mmci_exit(void)
1067{
1068 amba_driver_unregister(&mmci_driver);
1069}
1070
1071module_init(mmci_init);
1072module_exit(mmci_exit);
1073module_param(fmax, uint, 0444);
1074
1075MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
1076MODULE_LICENSE("GPL");