blob: 2563792c01d076d84f1d4d051383cf06835862bf [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
Russell Kingc8afc9d2011-02-04 09:19:46 +0000286 /*
287 * Calculate how far we are into the transfer. Note that
288 * the data counter gives the number of bytes transferred
289 * on the MMC bus, not on the host side. On reads, this
290 * can be as much as a FIFO-worth of data ahead. This
291 * matters for FIFO overruns only.
292 */
Linus Walleijf5a106d2011-01-27 17:44:34 +0100293 remain = readl(host->base + MMCIDATACNT);
Linus Walleij8cb28152011-01-24 15:22:13 +0100294 success = data->blksz * data->blocks - remain;
295
Russell Kingc8afc9d2011-02-04 09:19:46 +0000296 dev_dbg(mmc_dev(host->mmc), "MCI ERROR IRQ, status 0x%08x at 0x%08x\n",
297 status, success);
Linus Walleij8cb28152011-01-24 15:22:13 +0100298 if (status & MCI_DATACRCFAIL) {
299 /* Last block was not successful */
Russell Kingc8afc9d2011-02-04 09:19:46 +0000300 success -= 1;
Linus Walleij8cb28152011-01-24 15:22:13 +0100301 data->error = -EILSEQ;
302 } else if (status & MCI_DATATIMEOUT) {
Linus Walleij8cb28152011-01-24 15:22:13 +0100303 data->error = -ETIMEDOUT;
Russell Kingc8afc9d2011-02-04 09:19:46 +0000304 } else if (status & MCI_TXUNDERRUN) {
305 data->error = -EIO;
306 } else if (status & MCI_RXOVERRUN) {
307 if (success > host->variant->fifosize)
308 success -= host->variant->fifosize;
309 else
310 success = 0;
Linus Walleij8cb28152011-01-24 15:22:13 +0100311 data->error = -EIO;
312 }
Russell Kingc8afc9d2011-02-04 09:19:46 +0000313 host->data_xfered = round_down(success, data->blksz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
Linus Walleijf20f8f22010-10-19 13:41:24 +0100315
Linus Walleij8cb28152011-01-24 15:22:13 +0100316 if (status & MCI_DATABLOCKEND)
317 dev_err(mmc_dev(host->mmc), "stray MCI_DATABLOCKEND interrupt\n");
Linus Walleijf20f8f22010-10-19 13:41:24 +0100318
Russell Kingccff9b52011-01-30 21:03:50 +0000319 if (status & MCI_DATAEND || data->error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 mmci_stop_data(host);
321
Linus Walleij8cb28152011-01-24 15:22:13 +0100322 if (!data->error)
323 /* The error clause is handled above, success! */
Linus Walleijf20f8f22010-10-19 13:41:24 +0100324 host->data_xfered += data->blksz * data->blocks;
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (!data->stop) {
327 mmci_request_end(host, data->mrq);
328 } else {
329 mmci_start_command(host, data->stop, 0);
330 }
331 }
332}
333
334static void
335mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
336 unsigned int status)
337{
338 void __iomem *base = host->base;
339
340 host->cmd = NULL;
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 if (status & MCI_CMDTIMEOUT) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200343 cmd->error = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200345 cmd->error = -EILSEQ;
Russell King - ARM Linux9047b432011-01-11 16:35:56 +0000346 } else {
347 cmd->resp[0] = readl(base + MMCIRESPONSE0);
348 cmd->resp[1] = readl(base + MMCIRESPONSE1);
349 cmd->resp[2] = readl(base + MMCIRESPONSE2);
350 cmd->resp[3] = readl(base + MMCIRESPONSE3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
352
Pierre Ossman17b04292007-07-22 22:18:46 +0200353 if (!cmd->data || cmd->error) {
Russell Kinge47c2222007-01-08 16:42:51 +0000354 if (host->data)
355 mmci_stop_data(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 mmci_request_end(host, cmd->mrq);
357 } else if (!(cmd->data->flags & MMC_DATA_READ)) {
358 mmci_start_data(host, cmd->data);
359 }
360}
361
362static int mmci_pio_read(struct mmci_host *host, char *buffer, unsigned int remain)
363{
364 void __iomem *base = host->base;
365 char *ptr = buffer;
366 u32 status;
Linus Walleij26eed9a2008-04-26 23:39:44 +0100367 int host_remain = host->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 do {
Linus Walleij26eed9a2008-04-26 23:39:44 +0100370 int count = host_remain - (readl(base + MMCIFIFOCNT) << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 if (count > remain)
373 count = remain;
374
375 if (count <= 0)
376 break;
377
378 readsl(base + MMCIFIFO, ptr, count >> 2);
379
380 ptr += count;
381 remain -= count;
Linus Walleij26eed9a2008-04-26 23:39:44 +0100382 host_remain -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 if (remain == 0)
385 break;
386
387 status = readl(base + MMCISTATUS);
388 } while (status & MCI_RXDATAAVLBL);
389
390 return ptr - buffer;
391}
392
393static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status)
394{
Rabin Vincent8301bb62010-08-09 12:57:30 +0100395 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 void __iomem *base = host->base;
397 char *ptr = buffer;
398
399 do {
400 unsigned int count, maxcnt;
401
Rabin Vincent8301bb62010-08-09 12:57:30 +0100402 maxcnt = status & MCI_TXFIFOEMPTY ?
403 variant->fifosize : variant->fifohalfsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 count = min(remain, maxcnt);
405
Linus Walleij34177802010-10-19 12:43:58 +0100406 /*
407 * The ST Micro variant for SDIO transfer sizes
408 * less then 8 bytes should have clock H/W flow
409 * control disabled.
410 */
411 if (variant->sdio &&
412 mmc_card_sdio(host->mmc->card)) {
413 if (count < 8)
414 writel(readl(host->base + MMCICLOCK) &
415 ~variant->clkreg_enable,
416 host->base + MMCICLOCK);
417 else
418 writel(readl(host->base + MMCICLOCK) |
419 variant->clkreg_enable,
420 host->base + MMCICLOCK);
421 }
422
423 /*
424 * SDIO especially may want to send something that is
425 * not divisible by 4 (as opposed to card sectors
426 * etc), and the FIFO only accept full 32-bit writes.
427 * So compensate by adding +3 on the count, a single
428 * byte become a 32bit write, 7 bytes will be two
429 * 32bit writes etc.
430 */
431 writesl(base + MMCIFIFO, ptr, (count + 3) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 ptr += count;
434 remain -= count;
435
436 if (remain == 0)
437 break;
438
439 status = readl(base + MMCISTATUS);
440 } while (status & MCI_TXFIFOHALFEMPTY);
441
442 return ptr - buffer;
443}
444
445/*
446 * PIO data transfer IRQ handler.
447 */
David Howells7d12e782006-10-05 14:55:46 +0100448static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
450 struct mmci_host *host = dev_id;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100451 struct sg_mapping_iter *sg_miter = &host->sg_miter;
Rabin Vincent8301bb62010-08-09 12:57:30 +0100452 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 void __iomem *base = host->base;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100454 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 u32 status;
456
457 status = readl(base + MMCISTATUS);
458
Linus Walleij64de0282010-02-19 01:09:10 +0100459 dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100461 local_irq_save(flags);
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 unsigned int remain, len;
465 char *buffer;
466
467 /*
468 * For write, we only need to test the half-empty flag
469 * here - if the FIFO is completely empty, then by
470 * definition it is more than half empty.
471 *
472 * For read, check for data available.
473 */
474 if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL)))
475 break;
476
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100477 if (!sg_miter_next(sg_miter))
478 break;
479
480 buffer = sg_miter->addr;
481 remain = sg_miter->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 len = 0;
484 if (status & MCI_RXACTIVE)
485 len = mmci_pio_read(host, buffer, remain);
486 if (status & MCI_TXACTIVE)
487 len = mmci_pio_write(host, buffer, remain, status);
488
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100489 sg_miter->consumed = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 host->size -= len;
492 remain -= len;
493
494 if (remain)
495 break;
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 status = readl(base + MMCISTATUS);
498 } while (1);
499
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100500 sg_miter_stop(sg_miter);
501
502 local_irq_restore(flags);
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 /*
505 * If we're nearing the end of the read, switch to
506 * "any data available" mode.
507 */
Rabin Vincent8301bb62010-08-09 12:57:30 +0100508 if (status & MCI_RXACTIVE && host->size < variant->fifosize)
Linus Walleij2686b4b2010-10-19 12:39:48 +0100509 mmci_set_mask1(host, MCI_RXDATAAVLBLMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 /*
512 * If we run out of data, disable the data IRQs; this
513 * prevents a race where the FIFO becomes empty before
514 * the chip itself has disabled the data path, and
515 * stops us racing with our data end IRQ.
516 */
517 if (host->size == 0) {
Linus Walleij2686b4b2010-10-19 12:39:48 +0100518 mmci_set_mask1(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0);
520 }
521
522 return IRQ_HANDLED;
523}
524
525/*
526 * Handle completion of command and data transfers.
527 */
David Howells7d12e782006-10-05 14:55:46 +0100528static irqreturn_t mmci_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
530 struct mmci_host *host = dev_id;
531 u32 status;
532 int ret = 0;
533
534 spin_lock(&host->lock);
535
536 do {
537 struct mmc_command *cmd;
538 struct mmc_data *data;
539
540 status = readl(host->base + MMCISTATUS);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100541
542 if (host->singleirq) {
543 if (status & readl(host->base + MMCIMASK1))
544 mmci_pio_irq(irq, dev_id);
545
546 status &= ~MCI_IRQ1MASK;
547 }
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 status &= readl(host->base + MMCIMASK0);
550 writel(status, host->base + MMCICLEAR);
551
Linus Walleij64de0282010-02-19 01:09:10 +0100552 dev_dbg(mmc_dev(host->mmc), "irq0 (data+cmd) %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 data = host->data;
555 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|
556 MCI_RXOVERRUN|MCI_DATAEND|MCI_DATABLOCKEND) && data)
557 mmci_data_irq(host, data, status);
558
559 cmd = host->cmd;
560 if (status & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT|MCI_CMDSENT|MCI_CMDRESPEND) && cmd)
561 mmci_cmd_irq(host, cmd, status);
562
563 ret = 1;
564 } while (status);
565
566 spin_unlock(&host->lock);
567
568 return IRQ_RETVAL(ret);
569}
570
571static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
572{
573 struct mmci_host *host = mmc_priv(mmc);
Linus Walleij9e943022008-10-24 21:17:50 +0100574 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 WARN_ON(host->mrq != NULL);
577
Nicolas Pitre019a5f52007-10-11 01:06:03 -0400578 if (mrq->data && !is_power_of_2(mrq->data->blksz)) {
Linus Walleij64de0282010-02-19 01:09:10 +0100579 dev_err(mmc_dev(mmc), "unsupported block size (%d bytes)\n",
580 mrq->data->blksz);
Pierre Ossman255d01a2007-07-24 20:38:53 +0200581 mrq->cmd->error = -EINVAL;
582 mmc_request_done(mmc, mrq);
583 return;
584 }
585
Linus Walleij9e943022008-10-24 21:17:50 +0100586 spin_lock_irqsave(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 host->mrq = mrq;
589
590 if (mrq->data && mrq->data->flags & MMC_DATA_READ)
591 mmci_start_data(host, mrq->data);
592
593 mmci_start_command(host, mrq->cmd, 0);
594
Linus Walleij9e943022008-10-24 21:17:50 +0100595 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}
597
598static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
599{
600 struct mmci_host *host = mmc_priv(mmc);
Linus Walleija6a64642009-09-14 12:56:14 +0100601 u32 pwr = 0;
602 unsigned long flags;
Linus Walleij99fc5132010-09-29 01:08:27 -0400603 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 switch (ios->power_mode) {
606 case MMC_POWER_OFF:
Linus Walleij99fc5132010-09-29 01:08:27 -0400607 if (host->vcc)
608 ret = mmc_regulator_set_ocr(mmc, host->vcc, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 break;
610 case MMC_POWER_UP:
Linus Walleij99fc5132010-09-29 01:08:27 -0400611 if (host->vcc) {
612 ret = mmc_regulator_set_ocr(mmc, host->vcc, ios->vdd);
613 if (ret) {
614 dev_err(mmc_dev(mmc), "unable to set OCR\n");
615 /*
616 * The .set_ios() function in the mmc_host_ops
617 * struct return void, and failing to set the
618 * power should be rare so we print an error
619 * and return here.
620 */
621 return;
622 }
623 }
Rabin Vincentbb8f5632010-07-21 12:53:57 +0100624 if (host->plat->vdd_handler)
625 pwr |= host->plat->vdd_handler(mmc_dev(mmc), ios->vdd,
626 ios->power_mode);
Linus Walleijcc30d602009-01-04 15:18:54 +0100627 /* The ST version does not have this, fall through to POWER_ON */
Linus Walleijf17a1f02009-08-04 01:01:02 +0100628 if (host->hw_designer != AMBA_VENDOR_ST) {
Linus Walleijcc30d602009-01-04 15:18:54 +0100629 pwr |= MCI_PWR_UP;
630 break;
631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 case MMC_POWER_ON:
633 pwr |= MCI_PWR_ON;
634 break;
635 }
636
Linus Walleijcc30d602009-01-04 15:18:54 +0100637 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN) {
Linus Walleijf17a1f02009-08-04 01:01:02 +0100638 if (host->hw_designer != AMBA_VENDOR_ST)
Linus Walleijcc30d602009-01-04 15:18:54 +0100639 pwr |= MCI_ROD;
640 else {
641 /*
642 * The ST Micro variant use the ROD bit for something
643 * else and only has OD (Open Drain).
644 */
645 pwr |= MCI_OD;
646 }
647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Linus Walleija6a64642009-09-14 12:56:14 +0100649 spin_lock_irqsave(&host->lock, flags);
650
651 mmci_set_clkreg(host, ios->clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 if (host->pwr != pwr) {
654 host->pwr = pwr;
655 writel(pwr, host->base + MMCIPOWER);
656 }
Linus Walleija6a64642009-09-14 12:56:14 +0100657
658 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
Russell King89001442009-07-09 15:16:07 +0100661static int mmci_get_ro(struct mmc_host *mmc)
662{
663 struct mmci_host *host = mmc_priv(mmc);
664
665 if (host->gpio_wp == -ENOSYS)
666 return -ENOSYS;
667
Linus Walleij18a06302010-09-12 12:56:44 +0100668 return gpio_get_value_cansleep(host->gpio_wp);
Russell King89001442009-07-09 15:16:07 +0100669}
670
671static int mmci_get_cd(struct mmc_host *mmc)
672{
673 struct mmci_host *host = mmc_priv(mmc);
Rabin Vincent29719442010-08-09 12:54:43 +0100674 struct mmci_platform_data *plat = host->plat;
Russell King89001442009-07-09 15:16:07 +0100675 unsigned int status;
676
Rabin Vincent4b8caec2010-08-09 12:56:40 +0100677 if (host->gpio_cd == -ENOSYS) {
678 if (!plat->status)
679 return 1; /* Assume always present */
680
Rabin Vincent29719442010-08-09 12:54:43 +0100681 status = plat->status(mmc_dev(host->mmc));
Rabin Vincent4b8caec2010-08-09 12:56:40 +0100682 } else
Linus Walleij18a06302010-09-12 12:56:44 +0100683 status = !!gpio_get_value_cansleep(host->gpio_cd)
684 ^ plat->cd_invert;
Russell King89001442009-07-09 15:16:07 +0100685
Russell King74bc8092010-07-29 15:58:59 +0100686 /*
687 * Use positive logic throughout - status is zero for no card,
688 * non-zero for card inserted.
689 */
690 return status;
Russell King89001442009-07-09 15:16:07 +0100691}
692
Rabin Vincent148b8b32010-08-09 12:55:48 +0100693static irqreturn_t mmci_cd_irq(int irq, void *dev_id)
694{
695 struct mmci_host *host = dev_id;
696
697 mmc_detect_change(host->mmc, msecs_to_jiffies(500));
698
699 return IRQ_HANDLED;
700}
701
David Brownellab7aefd2006-11-12 17:55:30 -0800702static const struct mmc_host_ops mmci_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 .request = mmci_request,
704 .set_ios = mmci_set_ios,
Russell King89001442009-07-09 15:16:07 +0100705 .get_ro = mmci_get_ro,
706 .get_cd = mmci_get_cd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707};
708
Alessandro Rubini03fbdb12009-05-20 22:39:08 +0100709static int __devinit mmci_probe(struct amba_device *dev, struct amba_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Linus Walleij6ef297f2009-09-22 14:29:36 +0100711 struct mmci_platform_data *plat = dev->dev.platform_data;
Rabin Vincent4956e102010-07-21 12:54:40 +0100712 struct variant_data *variant = id->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 struct mmci_host *host;
714 struct mmc_host *mmc;
715 int ret;
716
717 /* must have platform data */
718 if (!plat) {
719 ret = -EINVAL;
720 goto out;
721 }
722
723 ret = amba_request_regions(dev, DRIVER_NAME);
724 if (ret)
725 goto out;
726
727 mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
728 if (!mmc) {
729 ret = -ENOMEM;
730 goto rel_regions;
731 }
732
733 host = mmc_priv(mmc);
Rabin Vincent4ea580f2009-04-17 08:44:19 +0530734 host->mmc = mmc;
Russell King012b7d32009-07-09 15:13:56 +0100735
Russell King89001442009-07-09 15:16:07 +0100736 host->gpio_wp = -ENOSYS;
737 host->gpio_cd = -ENOSYS;
Rabin Vincent148b8b32010-08-09 12:55:48 +0100738 host->gpio_cd_irq = -1;
Russell King89001442009-07-09 15:16:07 +0100739
Russell King012b7d32009-07-09 15:13:56 +0100740 host->hw_designer = amba_manf(dev);
741 host->hw_revision = amba_rev(dev);
Linus Walleij64de0282010-02-19 01:09:10 +0100742 dev_dbg(mmc_dev(mmc), "designer ID = 0x%02x\n", host->hw_designer);
743 dev_dbg(mmc_dev(mmc), "revision = 0x%01x\n", host->hw_revision);
Russell King012b7d32009-07-09 15:13:56 +0100744
Russell Kingee569c42008-11-30 17:38:14 +0000745 host->clk = clk_get(&dev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (IS_ERR(host->clk)) {
747 ret = PTR_ERR(host->clk);
748 host->clk = NULL;
749 goto host_free;
750 }
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 ret = clk_enable(host->clk);
753 if (ret)
Russell Kinga8d35842006-01-03 18:41:37 +0000754 goto clk_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 host->plat = plat;
Rabin Vincent4956e102010-07-21 12:54:40 +0100757 host->variant = variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 host->mclk = clk_get_rate(host->clk);
Linus Walleijc8df9a52008-04-29 09:34:07 +0100759 /*
760 * According to the spec, mclk is max 100 MHz,
761 * so we try to adjust the clock down to this,
762 * (if possible).
763 */
764 if (host->mclk > 100000000) {
765 ret = clk_set_rate(host->clk, 100000000);
766 if (ret < 0)
767 goto clk_disable;
768 host->mclk = clk_get_rate(host->clk);
Linus Walleij64de0282010-02-19 01:09:10 +0100769 dev_dbg(mmc_dev(mmc), "eventual mclk rate: %u Hz\n",
770 host->mclk);
Linus Walleijc8df9a52008-04-29 09:34:07 +0100771 }
Linus Walleijdc890c22009-06-07 23:27:31 +0100772 host->base = ioremap(dev->res.start, resource_size(&dev->res));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 if (!host->base) {
774 ret = -ENOMEM;
775 goto clk_disable;
776 }
777
778 mmc->ops = &mmci_ops;
779 mmc->f_min = (host->mclk + 511) / 512;
Linus Walleij808d97c2010-04-08 07:39:38 +0100780 /*
781 * If the platform data supplies a maximum operating
782 * frequency, this takes precedence. Else, we fall back
783 * to using the module parameter, which has a (low)
784 * default value in case it is not specified. Either
785 * value must not exceed the clock rate into the block,
786 * of course.
787 */
788 if (plat->f_max)
789 mmc->f_max = min(host->mclk, plat->f_max);
790 else
791 mmc->f_max = min(host->mclk, fmax);
Linus Walleij64de0282010-02-19 01:09:10 +0100792 dev_dbg(mmc_dev(mmc), "clocking block at %u Hz\n", mmc->f_max);
793
Linus Walleij34e84f32009-09-22 14:41:40 +0100794#ifdef CONFIG_REGULATOR
795 /* If we're using the regulator framework, try to fetch a regulator */
796 host->vcc = regulator_get(&dev->dev, "vmmc");
797 if (IS_ERR(host->vcc))
798 host->vcc = NULL;
799 else {
800 int mask = mmc_regulator_get_ocrmask(host->vcc);
801
802 if (mask < 0)
803 dev_err(&dev->dev, "error getting OCR mask (%d)\n",
804 mask);
805 else {
806 host->mmc->ocr_avail = (u32) mask;
807 if (plat->ocr_mask)
808 dev_warn(&dev->dev,
809 "Provided ocr_mask/setpower will not be used "
810 "(using regulator instead)\n");
811 }
812 }
813#endif
814 /* Fall back to platform data if no regulator is found */
815 if (host->vcc == NULL)
816 mmc->ocr_avail = plat->ocr_mask;
Linus Walleij9e6c82c2009-09-14 12:57:11 +0100817 mmc->caps = plat->capabilities;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 /*
820 * We can do SGIO
821 */
Martin K. Petersena36274e2010-09-10 01:33:59 -0400822 mmc->max_segs = NR_SG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824 /*
Rabin Vincent08458ef2010-07-21 12:55:59 +0100825 * Since only a certain number of bits are valid in the data length
826 * register, we must ensure that we don't exceed 2^num-1 bytes in a
827 * single request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 */
Rabin Vincent08458ef2010-07-21 12:55:59 +0100829 mmc->max_req_size = (1 << variant->datalength_bits) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831 /*
832 * Set the maximum segment size. Since we aren't doing DMA
833 * (yet) we are only limited by the data length register.
834 */
Pierre Ossman55db8902006-11-21 17:55:45 +0100835 mmc->max_seg_size = mmc->max_req_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100837 /*
838 * Block size can be up to 2048 bytes, but must be a power of two.
839 */
840 mmc->max_blk_size = 2048;
841
Pierre Ossman55db8902006-11-21 17:55:45 +0100842 /*
843 * No limit on the number of blocks transferred.
844 */
845 mmc->max_blk_count = mmc->max_req_size;
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 spin_lock_init(&host->lock);
848
849 writel(0, host->base + MMCIMASK0);
850 writel(0, host->base + MMCIMASK1);
851 writel(0xfff, host->base + MMCICLEAR);
852
Russell King89001442009-07-09 15:16:07 +0100853 if (gpio_is_valid(plat->gpio_cd)) {
854 ret = gpio_request(plat->gpio_cd, DRIVER_NAME " (cd)");
855 if (ret == 0)
856 ret = gpio_direction_input(plat->gpio_cd);
857 if (ret == 0)
858 host->gpio_cd = plat->gpio_cd;
859 else if (ret != -ENOSYS)
860 goto err_gpio_cd;
Rabin Vincent148b8b32010-08-09 12:55:48 +0100861
862 ret = request_any_context_irq(gpio_to_irq(plat->gpio_cd),
863 mmci_cd_irq, 0,
864 DRIVER_NAME " (cd)", host);
865 if (ret >= 0)
866 host->gpio_cd_irq = gpio_to_irq(plat->gpio_cd);
Russell King89001442009-07-09 15:16:07 +0100867 }
868 if (gpio_is_valid(plat->gpio_wp)) {
869 ret = gpio_request(plat->gpio_wp, DRIVER_NAME " (wp)");
870 if (ret == 0)
871 ret = gpio_direction_input(plat->gpio_wp);
872 if (ret == 0)
873 host->gpio_wp = plat->gpio_wp;
874 else if (ret != -ENOSYS)
875 goto err_gpio_wp;
876 }
877
Rabin Vincent4b8caec2010-08-09 12:56:40 +0100878 if ((host->plat->status || host->gpio_cd != -ENOSYS)
879 && host->gpio_cd_irq < 0)
Rabin Vincent148b8b32010-08-09 12:55:48 +0100880 mmc->caps |= MMC_CAP_NEEDS_POLL;
881
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700882 ret = request_irq(dev->irq[0], mmci_irq, IRQF_SHARED, DRIVER_NAME " (cmd)", host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 if (ret)
884 goto unmap;
885
Linus Walleij2686b4b2010-10-19 12:39:48 +0100886 if (dev->irq[1] == NO_IRQ)
887 host->singleirq = true;
888 else {
889 ret = request_irq(dev->irq[1], mmci_pio_irq, IRQF_SHARED,
890 DRIVER_NAME " (pio)", host);
891 if (ret)
892 goto irq0_free;
893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Linus Walleij8cb28152011-01-24 15:22:13 +0100895 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 amba_set_drvdata(dev, mmc);
898
Russell King8c11a942010-12-28 19:40:40 +0000899 dev_info(&dev->dev, "%s: PL%03x rev%u at 0x%08llx irq %d,%d\n",
900 mmc_hostname(mmc), amba_part(dev), amba_rev(dev),
Greg Kroah-Hartmane29419f2006-06-12 15:20:16 -0700901 (unsigned long long)dev->res.start, dev->irq[0], dev->irq[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Russell King8c11a942010-12-28 19:40:40 +0000903 mmc_add_host(mmc);
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 return 0;
906
907 irq0_free:
908 free_irq(dev->irq[0], host);
909 unmap:
Russell King89001442009-07-09 15:16:07 +0100910 if (host->gpio_wp != -ENOSYS)
911 gpio_free(host->gpio_wp);
912 err_gpio_wp:
Rabin Vincent148b8b32010-08-09 12:55:48 +0100913 if (host->gpio_cd_irq >= 0)
914 free_irq(host->gpio_cd_irq, host);
Russell King89001442009-07-09 15:16:07 +0100915 if (host->gpio_cd != -ENOSYS)
916 gpio_free(host->gpio_cd);
917 err_gpio_cd:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 iounmap(host->base);
919 clk_disable:
920 clk_disable(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 clk_free:
922 clk_put(host->clk);
923 host_free:
924 mmc_free_host(mmc);
925 rel_regions:
926 amba_release_regions(dev);
927 out:
928 return ret;
929}
930
Linus Walleij6dc4a472009-03-07 00:23:52 +0100931static int __devexit mmci_remove(struct amba_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
933 struct mmc_host *mmc = amba_get_drvdata(dev);
934
935 amba_set_drvdata(dev, NULL);
936
937 if (mmc) {
938 struct mmci_host *host = mmc_priv(mmc);
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 mmc_remove_host(mmc);
941
942 writel(0, host->base + MMCIMASK0);
943 writel(0, host->base + MMCIMASK1);
944
945 writel(0, host->base + MMCICOMMAND);
946 writel(0, host->base + MMCIDATACTRL);
947
948 free_irq(dev->irq[0], host);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100949 if (!host->singleirq)
950 free_irq(dev->irq[1], host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Russell King89001442009-07-09 15:16:07 +0100952 if (host->gpio_wp != -ENOSYS)
953 gpio_free(host->gpio_wp);
Rabin Vincent148b8b32010-08-09 12:55:48 +0100954 if (host->gpio_cd_irq >= 0)
955 free_irq(host->gpio_cd_irq, host);
Russell King89001442009-07-09 15:16:07 +0100956 if (host->gpio_cd != -ENOSYS)
957 gpio_free(host->gpio_cd);
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 iounmap(host->base);
960 clk_disable(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 clk_put(host->clk);
962
Linus Walleij99fc5132010-09-29 01:08:27 -0400963 if (host->vcc)
964 mmc_regulator_set_ocr(mmc, host->vcc, 0);
Linus Walleij34e84f32009-09-22 14:41:40 +0100965 regulator_put(host->vcc);
966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 mmc_free_host(mmc);
968
969 amba_release_regions(dev);
970 }
971
972 return 0;
973}
974
975#ifdef CONFIG_PM
Pavel Macheke5378ca2005-04-16 15:25:29 -0700976static int mmci_suspend(struct amba_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
978 struct mmc_host *mmc = amba_get_drvdata(dev);
979 int ret = 0;
980
981 if (mmc) {
982 struct mmci_host *host = mmc_priv(mmc);
983
Matt Fleming1a13f8f2010-05-26 14:42:08 -0700984 ret = mmc_suspend_host(mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 if (ret == 0)
986 writel(0, host->base + MMCIMASK0);
987 }
988
989 return ret;
990}
991
992static int mmci_resume(struct amba_device *dev)
993{
994 struct mmc_host *mmc = amba_get_drvdata(dev);
995 int ret = 0;
996
997 if (mmc) {
998 struct mmci_host *host = mmc_priv(mmc);
999
1000 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
1001
1002 ret = mmc_resume_host(mmc);
1003 }
1004
1005 return ret;
1006}
1007#else
1008#define mmci_suspend NULL
1009#define mmci_resume NULL
1010#endif
1011
1012static struct amba_id mmci_ids[] = {
1013 {
1014 .id = 0x00041180,
1015 .mask = 0x000fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001016 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 },
1018 {
1019 .id = 0x00041181,
1020 .mask = 0x000fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001021 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 },
Linus Walleijcc30d602009-01-04 15:18:54 +01001023 /* ST Micro variants */
1024 {
1025 .id = 0x00180180,
1026 .mask = 0x00ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001027 .data = &variant_u300,
Linus Walleijcc30d602009-01-04 15:18:54 +01001028 },
1029 {
1030 .id = 0x00280180,
1031 .mask = 0x00ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001032 .data = &variant_u300,
1033 },
1034 {
1035 .id = 0x00480180,
1036 .mask = 0x00ffffff,
1037 .data = &variant_ux500,
Linus Walleijcc30d602009-01-04 15:18:54 +01001038 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 { 0, 0 },
1040};
1041
1042static struct amba_driver mmci_driver = {
1043 .drv = {
1044 .name = DRIVER_NAME,
1045 },
1046 .probe = mmci_probe,
Linus Walleij6dc4a472009-03-07 00:23:52 +01001047 .remove = __devexit_p(mmci_remove),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 .suspend = mmci_suspend,
1049 .resume = mmci_resume,
1050 .id_table = mmci_ids,
1051};
1052
1053static int __init mmci_init(void)
1054{
1055 return amba_driver_register(&mmci_driver);
1056}
1057
1058static void __exit mmci_exit(void)
1059{
1060 amba_driver_unregister(&mmci_driver);
1061}
1062
1063module_init(mmci_init);
1064module_exit(mmci_exit);
1065module_param(fmax, uint, 0444);
1066
1067MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
1068MODULE_LICENSE("GPL");