blob: bde170d8f7234658fd55bf3d9a952153abebc59f [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 /*
Russell Kingc4d877c2011-01-27 09:50:13 +0000227 * If we have less than the fifo 'half-full' threshold to
228 * transfer, trigger a PIO interrupt as soon as any data
229 * is available.
Russell King0425a142006-02-16 16:48:31 +0000230 */
Russell Kingc4d877c2011-01-27 09:50:13 +0000231 if (host->size < variant->fifohalfsize)
Russell King0425a142006-02-16 16:48:31 +0000232 irqmask |= MCI_RXDATAAVLBLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 } else {
234 /*
235 * We don't actually need to include "FIFO empty" here
236 * since its implicit in "FIFO half empty".
237 */
238 irqmask = MCI_TXFIFOHALFEMPTYMASK;
239 }
240
Linus Walleij34177802010-10-19 12:43:58 +0100241 /* The ST Micro variants has a special bit to enable SDIO */
242 if (variant->sdio && host->mmc->card)
243 if (mmc_card_sdio(host->mmc->card))
244 datactrl |= MCI_ST_DPSM_SDIOEN;
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 writel(datactrl, base + MMCIDATACTRL);
247 writel(readl(base + MMCIMASK0) & ~MCI_DATAENDMASK, base + MMCIMASK0);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100248 mmci_set_mask1(host, irqmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
251static void
252mmci_start_command(struct mmci_host *host, struct mmc_command *cmd, u32 c)
253{
254 void __iomem *base = host->base;
255
Linus Walleij64de0282010-02-19 01:09:10 +0100256 dev_dbg(mmc_dev(host->mmc), "op %02x arg %08x flags %08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 cmd->opcode, cmd->arg, cmd->flags);
258
259 if (readl(base + MMCICOMMAND) & MCI_CPSM_ENABLE) {
260 writel(0, base + MMCICOMMAND);
261 udelay(1);
262 }
263
264 c |= cmd->opcode | MCI_CPSM_ENABLE;
Russell Kinge9225172006-02-02 12:23:12 +0000265 if (cmd->flags & MMC_RSP_PRESENT) {
266 if (cmd->flags & MMC_RSP_136)
267 c |= MCI_CPSM_LONGRSP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 c |= MCI_CPSM_RESPONSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
270 if (/*interrupt*/0)
271 c |= MCI_CPSM_INTERRUPT;
272
273 host->cmd = cmd;
274
275 writel(cmd->arg, base + MMCIARGUMENT);
276 writel(c, base + MMCICOMMAND);
277}
278
279static void
280mmci_data_irq(struct mmci_host *host, struct mmc_data *data,
281 unsigned int status)
282{
Linus Walleijf20f8f22010-10-19 13:41:24 +0100283 /* First check for errors */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|MCI_RXOVERRUN)) {
Linus Walleij8cb28152011-01-24 15:22:13 +0100285 u32 remain, success;
Linus Walleijf20f8f22010-10-19 13:41:24 +0100286
Russell Kingc8afc9d2011-02-04 09:19:46 +0000287 /*
288 * Calculate how far we are into the transfer. Note that
289 * the data counter gives the number of bytes transferred
290 * on the MMC bus, not on the host side. On reads, this
291 * can be as much as a FIFO-worth of data ahead. This
292 * matters for FIFO overruns only.
293 */
Linus Walleijf5a106d2011-01-27 17:44:34 +0100294 remain = readl(host->base + MMCIDATACNT);
Linus Walleij8cb28152011-01-24 15:22:13 +0100295 success = data->blksz * data->blocks - remain;
296
Russell Kingc8afc9d2011-02-04 09:19:46 +0000297 dev_dbg(mmc_dev(host->mmc), "MCI ERROR IRQ, status 0x%08x at 0x%08x\n",
298 status, success);
Linus Walleij8cb28152011-01-24 15:22:13 +0100299 if (status & MCI_DATACRCFAIL) {
300 /* Last block was not successful */
Russell Kingc8afc9d2011-02-04 09:19:46 +0000301 success -= 1;
Linus Walleij8cb28152011-01-24 15:22:13 +0100302 data->error = -EILSEQ;
303 } else if (status & MCI_DATATIMEOUT) {
Linus Walleij8cb28152011-01-24 15:22:13 +0100304 data->error = -ETIMEDOUT;
Russell Kingc8afc9d2011-02-04 09:19:46 +0000305 } else if (status & MCI_TXUNDERRUN) {
306 data->error = -EIO;
307 } else if (status & MCI_RXOVERRUN) {
308 if (success > host->variant->fifosize)
309 success -= host->variant->fifosize;
310 else
311 success = 0;
Linus Walleij8cb28152011-01-24 15:22:13 +0100312 data->error = -EIO;
313 }
Russell Kingc8afc9d2011-02-04 09:19:46 +0000314 host->data_xfered = round_down(success, data->blksz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 }
Linus Walleijf20f8f22010-10-19 13:41:24 +0100316
Linus Walleij8cb28152011-01-24 15:22:13 +0100317 if (status & MCI_DATABLOCKEND)
318 dev_err(mmc_dev(host->mmc), "stray MCI_DATABLOCKEND interrupt\n");
Linus Walleijf20f8f22010-10-19 13:41:24 +0100319
Russell Kingccff9b52011-01-30 21:03:50 +0000320 if (status & MCI_DATAEND || data->error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 mmci_stop_data(host);
322
Linus Walleij8cb28152011-01-24 15:22:13 +0100323 if (!data->error)
324 /* The error clause is handled above, success! */
Linus Walleijf20f8f22010-10-19 13:41:24 +0100325 host->data_xfered += data->blksz * data->blocks;
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 if (!data->stop) {
328 mmci_request_end(host, data->mrq);
329 } else {
330 mmci_start_command(host, data->stop, 0);
331 }
332 }
333}
334
335static void
336mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
337 unsigned int status)
338{
339 void __iomem *base = host->base;
340
341 host->cmd = NULL;
342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (status & MCI_CMDTIMEOUT) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200344 cmd->error = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200346 cmd->error = -EILSEQ;
Russell King - ARM Linux9047b432011-01-11 16:35:56 +0000347 } else {
348 cmd->resp[0] = readl(base + MMCIRESPONSE0);
349 cmd->resp[1] = readl(base + MMCIRESPONSE1);
350 cmd->resp[2] = readl(base + MMCIRESPONSE2);
351 cmd->resp[3] = readl(base + MMCIRESPONSE3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
353
Pierre Ossman17b04292007-07-22 22:18:46 +0200354 if (!cmd->data || cmd->error) {
Russell Kinge47c2222007-01-08 16:42:51 +0000355 if (host->data)
356 mmci_stop_data(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 mmci_request_end(host, cmd->mrq);
358 } else if (!(cmd->data->flags & MMC_DATA_READ)) {
359 mmci_start_data(host, cmd->data);
360 }
361}
362
363static int mmci_pio_read(struct mmci_host *host, char *buffer, unsigned int remain)
364{
365 void __iomem *base = host->base;
366 char *ptr = buffer;
367 u32 status;
Linus Walleij26eed9a2008-04-26 23:39:44 +0100368 int host_remain = host->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 do {
Linus Walleij26eed9a2008-04-26 23:39:44 +0100371 int count = host_remain - (readl(base + MMCIFIFOCNT) << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 if (count > remain)
374 count = remain;
375
376 if (count <= 0)
377 break;
378
379 readsl(base + MMCIFIFO, ptr, count >> 2);
380
381 ptr += count;
382 remain -= count;
Linus Walleij26eed9a2008-04-26 23:39:44 +0100383 host_remain -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 if (remain == 0)
386 break;
387
388 status = readl(base + MMCISTATUS);
389 } while (status & MCI_RXDATAAVLBL);
390
391 return ptr - buffer;
392}
393
394static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status)
395{
Rabin Vincent8301bb62010-08-09 12:57:30 +0100396 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 void __iomem *base = host->base;
398 char *ptr = buffer;
399
400 do {
401 unsigned int count, maxcnt;
402
Rabin Vincent8301bb62010-08-09 12:57:30 +0100403 maxcnt = status & MCI_TXFIFOEMPTY ?
404 variant->fifosize : variant->fifohalfsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 count = min(remain, maxcnt);
406
Linus Walleij34177802010-10-19 12:43:58 +0100407 /*
408 * The ST Micro variant for SDIO transfer sizes
409 * less then 8 bytes should have clock H/W flow
410 * control disabled.
411 */
412 if (variant->sdio &&
413 mmc_card_sdio(host->mmc->card)) {
414 if (count < 8)
415 writel(readl(host->base + MMCICLOCK) &
416 ~variant->clkreg_enable,
417 host->base + MMCICLOCK);
418 else
419 writel(readl(host->base + MMCICLOCK) |
420 variant->clkreg_enable,
421 host->base + MMCICLOCK);
422 }
423
424 /*
425 * SDIO especially may want to send something that is
426 * not divisible by 4 (as opposed to card sectors
427 * etc), and the FIFO only accept full 32-bit writes.
428 * So compensate by adding +3 on the count, a single
429 * byte become a 32bit write, 7 bytes will be two
430 * 32bit writes etc.
431 */
432 writesl(base + MMCIFIFO, ptr, (count + 3) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 status = readl(base + MMCISTATUS);
499 } while (1);
500
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100501 sg_miter_stop(sg_miter);
502
503 local_irq_restore(flags);
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 /*
Russell Kingc4d877c2011-01-27 09:50:13 +0000506 * If we have less than the fifo 'half-full' threshold to transfer,
507 * trigger a PIO interrupt as soon as any data is available.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 */
Russell Kingc4d877c2011-01-27 09:50:13 +0000509 if (status & MCI_RXACTIVE && host->size < variant->fifohalfsize)
Linus Walleij2686b4b2010-10-19 12:39:48 +0100510 mmci_set_mask1(host, MCI_RXDATAAVLBLMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 /*
513 * If we run out of data, disable the data IRQs; this
514 * prevents a race where the FIFO becomes empty before
515 * the chip itself has disabled the data path, and
516 * stops us racing with our data end IRQ.
517 */
518 if (host->size == 0) {
Linus Walleij2686b4b2010-10-19 12:39:48 +0100519 mmci_set_mask1(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0);
521 }
522
523 return IRQ_HANDLED;
524}
525
526/*
527 * Handle completion of command and data transfers.
528 */
David Howells7d12e782006-10-05 14:55:46 +0100529static irqreturn_t mmci_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
531 struct mmci_host *host = dev_id;
532 u32 status;
533 int ret = 0;
534
535 spin_lock(&host->lock);
536
537 do {
538 struct mmc_command *cmd;
539 struct mmc_data *data;
540
541 status = readl(host->base + MMCISTATUS);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100542
543 if (host->singleirq) {
544 if (status & readl(host->base + MMCIMASK1))
545 mmci_pio_irq(irq, dev_id);
546
547 status &= ~MCI_IRQ1MASK;
548 }
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 status &= readl(host->base + MMCIMASK0);
551 writel(status, host->base + MMCICLEAR);
552
Linus Walleij64de0282010-02-19 01:09:10 +0100553 dev_dbg(mmc_dev(host->mmc), "irq0 (data+cmd) %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 data = host->data;
556 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|
557 MCI_RXOVERRUN|MCI_DATAEND|MCI_DATABLOCKEND) && data)
558 mmci_data_irq(host, data, status);
559
560 cmd = host->cmd;
561 if (status & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT|MCI_CMDSENT|MCI_CMDRESPEND) && cmd)
562 mmci_cmd_irq(host, cmd, status);
563
564 ret = 1;
565 } while (status);
566
567 spin_unlock(&host->lock);
568
569 return IRQ_RETVAL(ret);
570}
571
572static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
573{
574 struct mmci_host *host = mmc_priv(mmc);
Linus Walleij9e943022008-10-24 21:17:50 +0100575 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 WARN_ON(host->mrq != NULL);
578
Nicolas Pitre019a5f52007-10-11 01:06:03 -0400579 if (mrq->data && !is_power_of_2(mrq->data->blksz)) {
Linus Walleij64de0282010-02-19 01:09:10 +0100580 dev_err(mmc_dev(mmc), "unsupported block size (%d bytes)\n",
581 mrq->data->blksz);
Pierre Ossman255d01a2007-07-24 20:38:53 +0200582 mrq->cmd->error = -EINVAL;
583 mmc_request_done(mmc, mrq);
584 return;
585 }
586
Linus Walleij9e943022008-10-24 21:17:50 +0100587 spin_lock_irqsave(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 host->mrq = mrq;
590
591 if (mrq->data && mrq->data->flags & MMC_DATA_READ)
592 mmci_start_data(host, mrq->data);
593
594 mmci_start_command(host, mrq->cmd, 0);
595
Linus Walleij9e943022008-10-24 21:17:50 +0100596 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
598
599static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
600{
601 struct mmci_host *host = mmc_priv(mmc);
Linus Walleija6a64642009-09-14 12:56:14 +0100602 u32 pwr = 0;
603 unsigned long flags;
Linus Walleij99fc5132010-09-29 01:08:27 -0400604 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 switch (ios->power_mode) {
607 case MMC_POWER_OFF:
Linus Walleij99fc5132010-09-29 01:08:27 -0400608 if (host->vcc)
609 ret = mmc_regulator_set_ocr(mmc, host->vcc, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 break;
611 case MMC_POWER_UP:
Linus Walleij99fc5132010-09-29 01:08:27 -0400612 if (host->vcc) {
613 ret = mmc_regulator_set_ocr(mmc, host->vcc, ios->vdd);
614 if (ret) {
615 dev_err(mmc_dev(mmc), "unable to set OCR\n");
616 /*
617 * The .set_ios() function in the mmc_host_ops
618 * struct return void, and failing to set the
619 * power should be rare so we print an error
620 * and return here.
621 */
622 return;
623 }
624 }
Rabin Vincentbb8f5632010-07-21 12:53:57 +0100625 if (host->plat->vdd_handler)
626 pwr |= host->plat->vdd_handler(mmc_dev(mmc), ios->vdd,
627 ios->power_mode);
Linus Walleijcc30d602009-01-04 15:18:54 +0100628 /* The ST version does not have this, fall through to POWER_ON */
Linus Walleijf17a1f02009-08-04 01:01:02 +0100629 if (host->hw_designer != AMBA_VENDOR_ST) {
Linus Walleijcc30d602009-01-04 15:18:54 +0100630 pwr |= MCI_PWR_UP;
631 break;
632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 case MMC_POWER_ON:
634 pwr |= MCI_PWR_ON;
635 break;
636 }
637
Linus Walleijcc30d602009-01-04 15:18:54 +0100638 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN) {
Linus Walleijf17a1f02009-08-04 01:01:02 +0100639 if (host->hw_designer != AMBA_VENDOR_ST)
Linus Walleijcc30d602009-01-04 15:18:54 +0100640 pwr |= MCI_ROD;
641 else {
642 /*
643 * The ST Micro variant use the ROD bit for something
644 * else and only has OD (Open Drain).
645 */
646 pwr |= MCI_OD;
647 }
648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Linus Walleija6a64642009-09-14 12:56:14 +0100650 spin_lock_irqsave(&host->lock, flags);
651
652 mmci_set_clkreg(host, ios->clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 if (host->pwr != pwr) {
655 host->pwr = pwr;
656 writel(pwr, host->base + MMCIPOWER);
657 }
Linus Walleija6a64642009-09-14 12:56:14 +0100658
659 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
Russell King89001442009-07-09 15:16:07 +0100662static int mmci_get_ro(struct mmc_host *mmc)
663{
664 struct mmci_host *host = mmc_priv(mmc);
665
666 if (host->gpio_wp == -ENOSYS)
667 return -ENOSYS;
668
Linus Walleij18a063012010-09-12 12:56:44 +0100669 return gpio_get_value_cansleep(host->gpio_wp);
Russell King89001442009-07-09 15:16:07 +0100670}
671
672static int mmci_get_cd(struct mmc_host *mmc)
673{
674 struct mmci_host *host = mmc_priv(mmc);
Rabin Vincent29719442010-08-09 12:54:43 +0100675 struct mmci_platform_data *plat = host->plat;
Russell King89001442009-07-09 15:16:07 +0100676 unsigned int status;
677
Rabin Vincent4b8caec2010-08-09 12:56:40 +0100678 if (host->gpio_cd == -ENOSYS) {
679 if (!plat->status)
680 return 1; /* Assume always present */
681
Rabin Vincent29719442010-08-09 12:54:43 +0100682 status = plat->status(mmc_dev(host->mmc));
Rabin Vincent4b8caec2010-08-09 12:56:40 +0100683 } else
Linus Walleij18a063012010-09-12 12:56:44 +0100684 status = !!gpio_get_value_cansleep(host->gpio_cd)
685 ^ plat->cd_invert;
Russell King89001442009-07-09 15:16:07 +0100686
Russell King74bc8092010-07-29 15:58:59 +0100687 /*
688 * Use positive logic throughout - status is zero for no card,
689 * non-zero for card inserted.
690 */
691 return status;
Russell King89001442009-07-09 15:16:07 +0100692}
693
Rabin Vincent148b8b32010-08-09 12:55:48 +0100694static irqreturn_t mmci_cd_irq(int irq, void *dev_id)
695{
696 struct mmci_host *host = dev_id;
697
698 mmc_detect_change(host->mmc, msecs_to_jiffies(500));
699
700 return IRQ_HANDLED;
701}
702
David Brownellab7aefd2006-11-12 17:55:30 -0800703static const struct mmc_host_ops mmci_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 .request = mmci_request,
705 .set_ios = mmci_set_ios,
Russell King89001442009-07-09 15:16:07 +0100706 .get_ro = mmci_get_ro,
707 .get_cd = mmci_get_cd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708};
709
Alessandro Rubini03fbdb12009-05-20 22:39:08 +0100710static int __devinit mmci_probe(struct amba_device *dev, struct amba_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Linus Walleij6ef297f2009-09-22 14:29:36 +0100712 struct mmci_platform_data *plat = dev->dev.platform_data;
Rabin Vincent4956e102010-07-21 12:54:40 +0100713 struct variant_data *variant = id->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 struct mmci_host *host;
715 struct mmc_host *mmc;
716 int ret;
717
718 /* must have platform data */
719 if (!plat) {
720 ret = -EINVAL;
721 goto out;
722 }
723
724 ret = amba_request_regions(dev, DRIVER_NAME);
725 if (ret)
726 goto out;
727
728 mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
729 if (!mmc) {
730 ret = -ENOMEM;
731 goto rel_regions;
732 }
733
734 host = mmc_priv(mmc);
Rabin Vincent4ea580f2009-04-17 08:44:19 +0530735 host->mmc = mmc;
Russell King012b7d32009-07-09 15:13:56 +0100736
Russell King89001442009-07-09 15:16:07 +0100737 host->gpio_wp = -ENOSYS;
738 host->gpio_cd = -ENOSYS;
Rabin Vincent148b8b32010-08-09 12:55:48 +0100739 host->gpio_cd_irq = -1;
Russell King89001442009-07-09 15:16:07 +0100740
Russell King012b7d32009-07-09 15:13:56 +0100741 host->hw_designer = amba_manf(dev);
742 host->hw_revision = amba_rev(dev);
Linus Walleij64de0282010-02-19 01:09:10 +0100743 dev_dbg(mmc_dev(mmc), "designer ID = 0x%02x\n", host->hw_designer);
744 dev_dbg(mmc_dev(mmc), "revision = 0x%01x\n", host->hw_revision);
Russell King012b7d32009-07-09 15:13:56 +0100745
Russell Kingee569c42008-11-30 17:38:14 +0000746 host->clk = clk_get(&dev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (IS_ERR(host->clk)) {
748 ret = PTR_ERR(host->clk);
749 host->clk = NULL;
750 goto host_free;
751 }
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 ret = clk_enable(host->clk);
754 if (ret)
Russell Kinga8d35842006-01-03 18:41:37 +0000755 goto clk_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 host->plat = plat;
Rabin Vincent4956e102010-07-21 12:54:40 +0100758 host->variant = variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 host->mclk = clk_get_rate(host->clk);
Linus Walleijc8df9a52008-04-29 09:34:07 +0100760 /*
761 * According to the spec, mclk is max 100 MHz,
762 * so we try to adjust the clock down to this,
763 * (if possible).
764 */
765 if (host->mclk > 100000000) {
766 ret = clk_set_rate(host->clk, 100000000);
767 if (ret < 0)
768 goto clk_disable;
769 host->mclk = clk_get_rate(host->clk);
Linus Walleij64de0282010-02-19 01:09:10 +0100770 dev_dbg(mmc_dev(mmc), "eventual mclk rate: %u Hz\n",
771 host->mclk);
Linus Walleijc8df9a52008-04-29 09:34:07 +0100772 }
Linus Walleijdc890c22009-06-07 23:27:31 +0100773 host->base = ioremap(dev->res.start, resource_size(&dev->res));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 if (!host->base) {
775 ret = -ENOMEM;
776 goto clk_disable;
777 }
778
779 mmc->ops = &mmci_ops;
780 mmc->f_min = (host->mclk + 511) / 512;
Linus Walleij808d97c2010-04-08 07:39:38 +0100781 /*
782 * If the platform data supplies a maximum operating
783 * frequency, this takes precedence. Else, we fall back
784 * to using the module parameter, which has a (low)
785 * default value in case it is not specified. Either
786 * value must not exceed the clock rate into the block,
787 * of course.
788 */
789 if (plat->f_max)
790 mmc->f_max = min(host->mclk, plat->f_max);
791 else
792 mmc->f_max = min(host->mclk, fmax);
Linus Walleij64de0282010-02-19 01:09:10 +0100793 dev_dbg(mmc_dev(mmc), "clocking block at %u Hz\n", mmc->f_max);
794
Linus Walleij34e84f32009-09-22 14:41:40 +0100795#ifdef CONFIG_REGULATOR
796 /* If we're using the regulator framework, try to fetch a regulator */
797 host->vcc = regulator_get(&dev->dev, "vmmc");
798 if (IS_ERR(host->vcc))
799 host->vcc = NULL;
800 else {
801 int mask = mmc_regulator_get_ocrmask(host->vcc);
802
803 if (mask < 0)
804 dev_err(&dev->dev, "error getting OCR mask (%d)\n",
805 mask);
806 else {
807 host->mmc->ocr_avail = (u32) mask;
808 if (plat->ocr_mask)
809 dev_warn(&dev->dev,
810 "Provided ocr_mask/setpower will not be used "
811 "(using regulator instead)\n");
812 }
813 }
814#endif
815 /* Fall back to platform data if no regulator is found */
816 if (host->vcc == NULL)
817 mmc->ocr_avail = plat->ocr_mask;
Linus Walleij9e6c82c2009-09-14 12:57:11 +0100818 mmc->caps = plat->capabilities;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 /*
821 * We can do SGIO
822 */
Martin K. Petersena36274e2010-09-10 01:33:59 -0400823 mmc->max_segs = NR_SG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 /*
Rabin Vincent08458ef2010-07-21 12:55:59 +0100826 * Since only a certain number of bits are valid in the data length
827 * register, we must ensure that we don't exceed 2^num-1 bytes in a
828 * single request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 */
Rabin Vincent08458ef2010-07-21 12:55:59 +0100830 mmc->max_req_size = (1 << variant->datalength_bits) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 /*
833 * Set the maximum segment size. Since we aren't doing DMA
834 * (yet) we are only limited by the data length register.
835 */
Pierre Ossman55db8902006-11-21 17:55:45 +0100836 mmc->max_seg_size = mmc->max_req_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100838 /*
839 * Block size can be up to 2048 bytes, but must be a power of two.
840 */
841 mmc->max_blk_size = 2048;
842
Pierre Ossman55db8902006-11-21 17:55:45 +0100843 /*
844 * No limit on the number of blocks transferred.
845 */
846 mmc->max_blk_count = mmc->max_req_size;
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 spin_lock_init(&host->lock);
849
850 writel(0, host->base + MMCIMASK0);
851 writel(0, host->base + MMCIMASK1);
852 writel(0xfff, host->base + MMCICLEAR);
853
Russell King89001442009-07-09 15:16:07 +0100854 if (gpio_is_valid(plat->gpio_cd)) {
855 ret = gpio_request(plat->gpio_cd, DRIVER_NAME " (cd)");
856 if (ret == 0)
857 ret = gpio_direction_input(plat->gpio_cd);
858 if (ret == 0)
859 host->gpio_cd = plat->gpio_cd;
860 else if (ret != -ENOSYS)
861 goto err_gpio_cd;
Rabin Vincent148b8b32010-08-09 12:55:48 +0100862
863 ret = request_any_context_irq(gpio_to_irq(plat->gpio_cd),
864 mmci_cd_irq, 0,
865 DRIVER_NAME " (cd)", host);
866 if (ret >= 0)
867 host->gpio_cd_irq = gpio_to_irq(plat->gpio_cd);
Russell King89001442009-07-09 15:16:07 +0100868 }
869 if (gpio_is_valid(plat->gpio_wp)) {
870 ret = gpio_request(plat->gpio_wp, DRIVER_NAME " (wp)");
871 if (ret == 0)
872 ret = gpio_direction_input(plat->gpio_wp);
873 if (ret == 0)
874 host->gpio_wp = plat->gpio_wp;
875 else if (ret != -ENOSYS)
876 goto err_gpio_wp;
877 }
878
Rabin Vincent4b8caec2010-08-09 12:56:40 +0100879 if ((host->plat->status || host->gpio_cd != -ENOSYS)
880 && host->gpio_cd_irq < 0)
Rabin Vincent148b8b32010-08-09 12:55:48 +0100881 mmc->caps |= MMC_CAP_NEEDS_POLL;
882
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700883 ret = request_irq(dev->irq[0], mmci_irq, IRQF_SHARED, DRIVER_NAME " (cmd)", host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 if (ret)
885 goto unmap;
886
Linus Walleij2686b4b2010-10-19 12:39:48 +0100887 if (dev->irq[1] == NO_IRQ)
888 host->singleirq = true;
889 else {
890 ret = request_irq(dev->irq[1], mmci_pio_irq, IRQF_SHARED,
891 DRIVER_NAME " (pio)", host);
892 if (ret)
893 goto irq0_free;
894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Linus Walleij8cb28152011-01-24 15:22:13 +0100896 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898 amba_set_drvdata(dev, mmc);
899
Russell King8c11a942010-12-28 19:40:40 +0000900 dev_info(&dev->dev, "%s: PL%03x rev%u at 0x%08llx irq %d,%d\n",
901 mmc_hostname(mmc), amba_part(dev), amba_rev(dev),
Greg Kroah-Hartmane29419f2006-06-12 15:20:16 -0700902 (unsigned long long)dev->res.start, dev->irq[0], dev->irq[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Russell King8c11a942010-12-28 19:40:40 +0000904 mmc_add_host(mmc);
905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return 0;
907
908 irq0_free:
909 free_irq(dev->irq[0], host);
910 unmap:
Russell King89001442009-07-09 15:16:07 +0100911 if (host->gpio_wp != -ENOSYS)
912 gpio_free(host->gpio_wp);
913 err_gpio_wp:
Rabin Vincent148b8b32010-08-09 12:55:48 +0100914 if (host->gpio_cd_irq >= 0)
915 free_irq(host->gpio_cd_irq, host);
Russell King89001442009-07-09 15:16:07 +0100916 if (host->gpio_cd != -ENOSYS)
917 gpio_free(host->gpio_cd);
918 err_gpio_cd:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 iounmap(host->base);
920 clk_disable:
921 clk_disable(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 clk_free:
923 clk_put(host->clk);
924 host_free:
925 mmc_free_host(mmc);
926 rel_regions:
927 amba_release_regions(dev);
928 out:
929 return ret;
930}
931
Linus Walleij6dc4a472009-03-07 00:23:52 +0100932static int __devexit mmci_remove(struct amba_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
934 struct mmc_host *mmc = amba_get_drvdata(dev);
935
936 amba_set_drvdata(dev, NULL);
937
938 if (mmc) {
939 struct mmci_host *host = mmc_priv(mmc);
940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 mmc_remove_host(mmc);
942
943 writel(0, host->base + MMCIMASK0);
944 writel(0, host->base + MMCIMASK1);
945
946 writel(0, host->base + MMCICOMMAND);
947 writel(0, host->base + MMCIDATACTRL);
948
949 free_irq(dev->irq[0], host);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100950 if (!host->singleirq)
951 free_irq(dev->irq[1], host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Russell King89001442009-07-09 15:16:07 +0100953 if (host->gpio_wp != -ENOSYS)
954 gpio_free(host->gpio_wp);
Rabin Vincent148b8b32010-08-09 12:55:48 +0100955 if (host->gpio_cd_irq >= 0)
956 free_irq(host->gpio_cd_irq, host);
Russell King89001442009-07-09 15:16:07 +0100957 if (host->gpio_cd != -ENOSYS)
958 gpio_free(host->gpio_cd);
959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 iounmap(host->base);
961 clk_disable(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 clk_put(host->clk);
963
Linus Walleij99fc5132010-09-29 01:08:27 -0400964 if (host->vcc)
965 mmc_regulator_set_ocr(mmc, host->vcc, 0);
Linus Walleij34e84f32009-09-22 14:41:40 +0100966 regulator_put(host->vcc);
967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 mmc_free_host(mmc);
969
970 amba_release_regions(dev);
971 }
972
973 return 0;
974}
975
976#ifdef CONFIG_PM
Pavel Macheke5378ca2005-04-16 15:25:29 -0700977static int mmci_suspend(struct amba_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
979 struct mmc_host *mmc = amba_get_drvdata(dev);
980 int ret = 0;
981
982 if (mmc) {
983 struct mmci_host *host = mmc_priv(mmc);
984
Matt Fleming1a13f8f2010-05-26 14:42:08 -0700985 ret = mmc_suspend_host(mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 if (ret == 0)
987 writel(0, host->base + MMCIMASK0);
988 }
989
990 return ret;
991}
992
993static int mmci_resume(struct amba_device *dev)
994{
995 struct mmc_host *mmc = amba_get_drvdata(dev);
996 int ret = 0;
997
998 if (mmc) {
999 struct mmci_host *host = mmc_priv(mmc);
1000
1001 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
1002
1003 ret = mmc_resume_host(mmc);
1004 }
1005
1006 return ret;
1007}
1008#else
1009#define mmci_suspend NULL
1010#define mmci_resume NULL
1011#endif
1012
1013static struct amba_id mmci_ids[] = {
1014 {
1015 .id = 0x00041180,
1016 .mask = 0x000fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001017 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 },
1019 {
1020 .id = 0x00041181,
1021 .mask = 0x000fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001022 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 },
Linus Walleijcc30d602009-01-04 15:18:54 +01001024 /* ST Micro variants */
1025 {
1026 .id = 0x00180180,
1027 .mask = 0x00ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001028 .data = &variant_u300,
Linus Walleijcc30d602009-01-04 15:18:54 +01001029 },
1030 {
1031 .id = 0x00280180,
1032 .mask = 0x00ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001033 .data = &variant_u300,
1034 },
1035 {
1036 .id = 0x00480180,
1037 .mask = 0x00ffffff,
1038 .data = &variant_ux500,
Linus Walleijcc30d602009-01-04 15:18:54 +01001039 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 { 0, 0 },
1041};
1042
1043static struct amba_driver mmci_driver = {
1044 .drv = {
1045 .name = DRIVER_NAME,
1046 },
1047 .probe = mmci_probe,
Linus Walleij6dc4a472009-03-07 00:23:52 +01001048 .remove = __devexit_p(mmci_remove),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 .suspend = mmci_suspend,
1050 .resume = mmci_resume,
1051 .id_table = mmci_ids,
1052};
1053
1054static int __init mmci_init(void)
1055{
1056 return amba_driver_register(&mmci_driver);
1057}
1058
1059static void __exit mmci_exit(void)
1060{
1061 amba_driver_unregister(&mmci_driver);
1062}
1063
1064module_init(mmci_init);
1065module_exit(mmci_exit);
1066module_param(fmax, uint, 0444);
1067
1068MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
1069MODULE_LICENSE("GPL");