blob: d7b83a8b35341039158b9be695a33be5ea93a2c8 [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);
Russell Kinge9c091b2006-01-04 16:24:05 +0000314
315 /*
316 * We hit an error condition. Ensure that any data
317 * partially written to a page is properly coherent.
318 */
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100319 if (data->flags & MMC_DATA_READ) {
320 struct sg_mapping_iter *sg_miter = &host->sg_miter;
321 unsigned long flags;
322
323 local_irq_save(flags);
324 if (sg_miter_next(sg_miter)) {
325 flush_dcache_page(sg_miter->page);
326 sg_miter_stop(sg_miter);
327 }
328 local_irq_restore(flags);
329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
Linus Walleijf20f8f22010-10-19 13:41:24 +0100331
Linus Walleij8cb28152011-01-24 15:22:13 +0100332 if (status & MCI_DATABLOCKEND)
333 dev_err(mmc_dev(host->mmc), "stray MCI_DATABLOCKEND interrupt\n");
Linus Walleijf20f8f22010-10-19 13:41:24 +0100334
Russell Kingccff9b52011-01-30 21:03:50 +0000335 if (status & MCI_DATAEND || data->error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 mmci_stop_data(host);
337
Linus Walleij8cb28152011-01-24 15:22:13 +0100338 if (!data->error)
339 /* The error clause is handled above, success! */
Linus Walleijf20f8f22010-10-19 13:41:24 +0100340 host->data_xfered += data->blksz * data->blocks;
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 if (!data->stop) {
343 mmci_request_end(host, data->mrq);
344 } else {
345 mmci_start_command(host, data->stop, 0);
346 }
347 }
348}
349
350static void
351mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
352 unsigned int status)
353{
354 void __iomem *base = host->base;
355
356 host->cmd = NULL;
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (status & MCI_CMDTIMEOUT) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200359 cmd->error = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200361 cmd->error = -EILSEQ;
Russell King - ARM Linux9047b432011-01-11 16:35:56 +0000362 } else {
363 cmd->resp[0] = readl(base + MMCIRESPONSE0);
364 cmd->resp[1] = readl(base + MMCIRESPONSE1);
365 cmd->resp[2] = readl(base + MMCIRESPONSE2);
366 cmd->resp[3] = readl(base + MMCIRESPONSE3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
368
Pierre Ossman17b04292007-07-22 22:18:46 +0200369 if (!cmd->data || cmd->error) {
Russell Kinge47c2222007-01-08 16:42:51 +0000370 if (host->data)
371 mmci_stop_data(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 mmci_request_end(host, cmd->mrq);
373 } else if (!(cmd->data->flags & MMC_DATA_READ)) {
374 mmci_start_data(host, cmd->data);
375 }
376}
377
378static int mmci_pio_read(struct mmci_host *host, char *buffer, unsigned int remain)
379{
380 void __iomem *base = host->base;
381 char *ptr = buffer;
382 u32 status;
Linus Walleij26eed9a2008-04-26 23:39:44 +0100383 int host_remain = host->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 do {
Linus Walleij26eed9a2008-04-26 23:39:44 +0100386 int count = host_remain - (readl(base + MMCIFIFOCNT) << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 if (count > remain)
389 count = remain;
390
391 if (count <= 0)
392 break;
393
394 readsl(base + MMCIFIFO, ptr, count >> 2);
395
396 ptr += count;
397 remain -= count;
Linus Walleij26eed9a2008-04-26 23:39:44 +0100398 host_remain -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 if (remain == 0)
401 break;
402
403 status = readl(base + MMCISTATUS);
404 } while (status & MCI_RXDATAAVLBL);
405
406 return ptr - buffer;
407}
408
409static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status)
410{
Rabin Vincent8301bb62010-08-09 12:57:30 +0100411 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 void __iomem *base = host->base;
413 char *ptr = buffer;
414
415 do {
416 unsigned int count, maxcnt;
417
Rabin Vincent8301bb62010-08-09 12:57:30 +0100418 maxcnt = status & MCI_TXFIFOEMPTY ?
419 variant->fifosize : variant->fifohalfsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 count = min(remain, maxcnt);
421
Linus Walleij34177802010-10-19 12:43:58 +0100422 /*
423 * The ST Micro variant for SDIO transfer sizes
424 * less then 8 bytes should have clock H/W flow
425 * control disabled.
426 */
427 if (variant->sdio &&
428 mmc_card_sdio(host->mmc->card)) {
429 if (count < 8)
430 writel(readl(host->base + MMCICLOCK) &
431 ~variant->clkreg_enable,
432 host->base + MMCICLOCK);
433 else
434 writel(readl(host->base + MMCICLOCK) |
435 variant->clkreg_enable,
436 host->base + MMCICLOCK);
437 }
438
439 /*
440 * SDIO especially may want to send something that is
441 * not divisible by 4 (as opposed to card sectors
442 * etc), and the FIFO only accept full 32-bit writes.
443 * So compensate by adding +3 on the count, a single
444 * byte become a 32bit write, 7 bytes will be two
445 * 32bit writes etc.
446 */
447 writesl(base + MMCIFIFO, ptr, (count + 3) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 ptr += count;
450 remain -= count;
451
452 if (remain == 0)
453 break;
454
455 status = readl(base + MMCISTATUS);
456 } while (status & MCI_TXFIFOHALFEMPTY);
457
458 return ptr - buffer;
459}
460
461/*
462 * PIO data transfer IRQ handler.
463 */
David Howells7d12e782006-10-05 14:55:46 +0100464static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
466 struct mmci_host *host = dev_id;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100467 struct sg_mapping_iter *sg_miter = &host->sg_miter;
Rabin Vincent8301bb62010-08-09 12:57:30 +0100468 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 void __iomem *base = host->base;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100470 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 u32 status;
472
473 status = readl(base + MMCISTATUS);
474
Linus Walleij64de0282010-02-19 01:09:10 +0100475 dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100477 local_irq_save(flags);
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 unsigned int remain, len;
481 char *buffer;
482
483 /*
484 * For write, we only need to test the half-empty flag
485 * here - if the FIFO is completely empty, then by
486 * definition it is more than half empty.
487 *
488 * For read, check for data available.
489 */
490 if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL)))
491 break;
492
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100493 if (!sg_miter_next(sg_miter))
494 break;
495
496 buffer = sg_miter->addr;
497 remain = sg_miter->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 len = 0;
500 if (status & MCI_RXACTIVE)
501 len = mmci_pio_read(host, buffer, remain);
502 if (status & MCI_TXACTIVE)
503 len = mmci_pio_write(host, buffer, remain, status);
504
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100505 sg_miter->consumed = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 host->size -= len;
508 remain -= len;
509
510 if (remain)
511 break;
512
Russell Kinge9c091b2006-01-04 16:24:05 +0000513 if (status & MCI_RXACTIVE)
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100514 flush_dcache_page(sg_miter->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 status = readl(base + MMCISTATUS);
517 } while (1);
518
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100519 sg_miter_stop(sg_miter);
520
521 local_irq_restore(flags);
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 /*
524 * If we're nearing the end of the read, switch to
525 * "any data available" mode.
526 */
Rabin Vincent8301bb62010-08-09 12:57:30 +0100527 if (status & MCI_RXACTIVE && host->size < variant->fifosize)
Linus Walleij2686b4b2010-10-19 12:39:48 +0100528 mmci_set_mask1(host, MCI_RXDATAAVLBLMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 /*
531 * If we run out of data, disable the data IRQs; this
532 * prevents a race where the FIFO becomes empty before
533 * the chip itself has disabled the data path, and
534 * stops us racing with our data end IRQ.
535 */
536 if (host->size == 0) {
Linus Walleij2686b4b2010-10-19 12:39:48 +0100537 mmci_set_mask1(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0);
539 }
540
541 return IRQ_HANDLED;
542}
543
544/*
545 * Handle completion of command and data transfers.
546 */
David Howells7d12e782006-10-05 14:55:46 +0100547static irqreturn_t mmci_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
549 struct mmci_host *host = dev_id;
550 u32 status;
551 int ret = 0;
552
553 spin_lock(&host->lock);
554
555 do {
556 struct mmc_command *cmd;
557 struct mmc_data *data;
558
559 status = readl(host->base + MMCISTATUS);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100560
561 if (host->singleirq) {
562 if (status & readl(host->base + MMCIMASK1))
563 mmci_pio_irq(irq, dev_id);
564
565 status &= ~MCI_IRQ1MASK;
566 }
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 status &= readl(host->base + MMCIMASK0);
569 writel(status, host->base + MMCICLEAR);
570
Linus Walleij64de0282010-02-19 01:09:10 +0100571 dev_dbg(mmc_dev(host->mmc), "irq0 (data+cmd) %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 data = host->data;
574 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|
575 MCI_RXOVERRUN|MCI_DATAEND|MCI_DATABLOCKEND) && data)
576 mmci_data_irq(host, data, status);
577
578 cmd = host->cmd;
579 if (status & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT|MCI_CMDSENT|MCI_CMDRESPEND) && cmd)
580 mmci_cmd_irq(host, cmd, status);
581
582 ret = 1;
583 } while (status);
584
585 spin_unlock(&host->lock);
586
587 return IRQ_RETVAL(ret);
588}
589
590static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
591{
592 struct mmci_host *host = mmc_priv(mmc);
Linus Walleij9e943022008-10-24 21:17:50 +0100593 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 WARN_ON(host->mrq != NULL);
596
Nicolas Pitre019a5f52007-10-11 01:06:03 -0400597 if (mrq->data && !is_power_of_2(mrq->data->blksz)) {
Linus Walleij64de0282010-02-19 01:09:10 +0100598 dev_err(mmc_dev(mmc), "unsupported block size (%d bytes)\n",
599 mrq->data->blksz);
Pierre Ossman255d01a2007-07-24 20:38:53 +0200600 mrq->cmd->error = -EINVAL;
601 mmc_request_done(mmc, mrq);
602 return;
603 }
604
Linus Walleij9e943022008-10-24 21:17:50 +0100605 spin_lock_irqsave(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 host->mrq = mrq;
608
609 if (mrq->data && mrq->data->flags & MMC_DATA_READ)
610 mmci_start_data(host, mrq->data);
611
612 mmci_start_command(host, mrq->cmd, 0);
613
Linus Walleij9e943022008-10-24 21:17:50 +0100614 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616
617static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
618{
619 struct mmci_host *host = mmc_priv(mmc);
Linus Walleija6a64642009-09-14 12:56:14 +0100620 u32 pwr = 0;
621 unsigned long flags;
Linus Walleij99fc5132010-09-29 01:08:27 -0400622 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 switch (ios->power_mode) {
625 case MMC_POWER_OFF:
Linus Walleij99fc5132010-09-29 01:08:27 -0400626 if (host->vcc)
627 ret = mmc_regulator_set_ocr(mmc, host->vcc, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 break;
629 case MMC_POWER_UP:
Linus Walleij99fc5132010-09-29 01:08:27 -0400630 if (host->vcc) {
631 ret = mmc_regulator_set_ocr(mmc, host->vcc, ios->vdd);
632 if (ret) {
633 dev_err(mmc_dev(mmc), "unable to set OCR\n");
634 /*
635 * The .set_ios() function in the mmc_host_ops
636 * struct return void, and failing to set the
637 * power should be rare so we print an error
638 * and return here.
639 */
640 return;
641 }
642 }
Rabin Vincentbb8f5632010-07-21 12:53:57 +0100643 if (host->plat->vdd_handler)
644 pwr |= host->plat->vdd_handler(mmc_dev(mmc), ios->vdd,
645 ios->power_mode);
Linus Walleijcc30d602009-01-04 15:18:54 +0100646 /* The ST version does not have this, fall through to POWER_ON */
Linus Walleijf17a1f02009-08-04 01:01:02 +0100647 if (host->hw_designer != AMBA_VENDOR_ST) {
Linus Walleijcc30d602009-01-04 15:18:54 +0100648 pwr |= MCI_PWR_UP;
649 break;
650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 case MMC_POWER_ON:
652 pwr |= MCI_PWR_ON;
653 break;
654 }
655
Linus Walleijcc30d602009-01-04 15:18:54 +0100656 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN) {
Linus Walleijf17a1f02009-08-04 01:01:02 +0100657 if (host->hw_designer != AMBA_VENDOR_ST)
Linus Walleijcc30d602009-01-04 15:18:54 +0100658 pwr |= MCI_ROD;
659 else {
660 /*
661 * The ST Micro variant use the ROD bit for something
662 * else and only has OD (Open Drain).
663 */
664 pwr |= MCI_OD;
665 }
666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Linus Walleija6a64642009-09-14 12:56:14 +0100668 spin_lock_irqsave(&host->lock, flags);
669
670 mmci_set_clkreg(host, ios->clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 if (host->pwr != pwr) {
673 host->pwr = pwr;
674 writel(pwr, host->base + MMCIPOWER);
675 }
Linus Walleija6a64642009-09-14 12:56:14 +0100676
677 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678}
679
Russell King89001442009-07-09 15:16:07 +0100680static int mmci_get_ro(struct mmc_host *mmc)
681{
682 struct mmci_host *host = mmc_priv(mmc);
683
684 if (host->gpio_wp == -ENOSYS)
685 return -ENOSYS;
686
Linus Walleij18a06302010-09-12 12:56:44 +0100687 return gpio_get_value_cansleep(host->gpio_wp);
Russell King89001442009-07-09 15:16:07 +0100688}
689
690static int mmci_get_cd(struct mmc_host *mmc)
691{
692 struct mmci_host *host = mmc_priv(mmc);
Rabin Vincent29719442010-08-09 12:54:43 +0100693 struct mmci_platform_data *plat = host->plat;
Russell King89001442009-07-09 15:16:07 +0100694 unsigned int status;
695
Rabin Vincent4b8caec2010-08-09 12:56:40 +0100696 if (host->gpio_cd == -ENOSYS) {
697 if (!plat->status)
698 return 1; /* Assume always present */
699
Rabin Vincent29719442010-08-09 12:54:43 +0100700 status = plat->status(mmc_dev(host->mmc));
Rabin Vincent4b8caec2010-08-09 12:56:40 +0100701 } else
Linus Walleij18a06302010-09-12 12:56:44 +0100702 status = !!gpio_get_value_cansleep(host->gpio_cd)
703 ^ plat->cd_invert;
Russell King89001442009-07-09 15:16:07 +0100704
Russell King74bc8092010-07-29 15:58:59 +0100705 /*
706 * Use positive logic throughout - status is zero for no card,
707 * non-zero for card inserted.
708 */
709 return status;
Russell King89001442009-07-09 15:16:07 +0100710}
711
Rabin Vincent148b8b32010-08-09 12:55:48 +0100712static irqreturn_t mmci_cd_irq(int irq, void *dev_id)
713{
714 struct mmci_host *host = dev_id;
715
716 mmc_detect_change(host->mmc, msecs_to_jiffies(500));
717
718 return IRQ_HANDLED;
719}
720
David Brownellab7aefd2006-11-12 17:55:30 -0800721static const struct mmc_host_ops mmci_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 .request = mmci_request,
723 .set_ios = mmci_set_ios,
Russell King89001442009-07-09 15:16:07 +0100724 .get_ro = mmci_get_ro,
725 .get_cd = mmci_get_cd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726};
727
Alessandro Rubini03fbdb12009-05-20 22:39:08 +0100728static int __devinit mmci_probe(struct amba_device *dev, struct amba_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
Linus Walleij6ef297f2009-09-22 14:29:36 +0100730 struct mmci_platform_data *plat = dev->dev.platform_data;
Rabin Vincent4956e102010-07-21 12:54:40 +0100731 struct variant_data *variant = id->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 struct mmci_host *host;
733 struct mmc_host *mmc;
734 int ret;
735
736 /* must have platform data */
737 if (!plat) {
738 ret = -EINVAL;
739 goto out;
740 }
741
742 ret = amba_request_regions(dev, DRIVER_NAME);
743 if (ret)
744 goto out;
745
746 mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
747 if (!mmc) {
748 ret = -ENOMEM;
749 goto rel_regions;
750 }
751
752 host = mmc_priv(mmc);
Rabin Vincent4ea580f2009-04-17 08:44:19 +0530753 host->mmc = mmc;
Russell King012b7d32009-07-09 15:13:56 +0100754
Russell King89001442009-07-09 15:16:07 +0100755 host->gpio_wp = -ENOSYS;
756 host->gpio_cd = -ENOSYS;
Rabin Vincent148b8b32010-08-09 12:55:48 +0100757 host->gpio_cd_irq = -1;
Russell King89001442009-07-09 15:16:07 +0100758
Russell King012b7d32009-07-09 15:13:56 +0100759 host->hw_designer = amba_manf(dev);
760 host->hw_revision = amba_rev(dev);
Linus Walleij64de0282010-02-19 01:09:10 +0100761 dev_dbg(mmc_dev(mmc), "designer ID = 0x%02x\n", host->hw_designer);
762 dev_dbg(mmc_dev(mmc), "revision = 0x%01x\n", host->hw_revision);
Russell King012b7d32009-07-09 15:13:56 +0100763
Russell Kingee569c42008-11-30 17:38:14 +0000764 host->clk = clk_get(&dev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 if (IS_ERR(host->clk)) {
766 ret = PTR_ERR(host->clk);
767 host->clk = NULL;
768 goto host_free;
769 }
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 ret = clk_enable(host->clk);
772 if (ret)
Russell Kinga8d35842006-01-03 18:41:37 +0000773 goto clk_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 host->plat = plat;
Rabin Vincent4956e102010-07-21 12:54:40 +0100776 host->variant = variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 host->mclk = clk_get_rate(host->clk);
Linus Walleijc8df9a52008-04-29 09:34:07 +0100778 /*
779 * According to the spec, mclk is max 100 MHz,
780 * so we try to adjust the clock down to this,
781 * (if possible).
782 */
783 if (host->mclk > 100000000) {
784 ret = clk_set_rate(host->clk, 100000000);
785 if (ret < 0)
786 goto clk_disable;
787 host->mclk = clk_get_rate(host->clk);
Linus Walleij64de0282010-02-19 01:09:10 +0100788 dev_dbg(mmc_dev(mmc), "eventual mclk rate: %u Hz\n",
789 host->mclk);
Linus Walleijc8df9a52008-04-29 09:34:07 +0100790 }
Linus Walleijdc890c22009-06-07 23:27:31 +0100791 host->base = ioremap(dev->res.start, resource_size(&dev->res));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (!host->base) {
793 ret = -ENOMEM;
794 goto clk_disable;
795 }
796
797 mmc->ops = &mmci_ops;
798 mmc->f_min = (host->mclk + 511) / 512;
Linus Walleij808d97c2010-04-08 07:39:38 +0100799 /*
800 * If the platform data supplies a maximum operating
801 * frequency, this takes precedence. Else, we fall back
802 * to using the module parameter, which has a (low)
803 * default value in case it is not specified. Either
804 * value must not exceed the clock rate into the block,
805 * of course.
806 */
807 if (plat->f_max)
808 mmc->f_max = min(host->mclk, plat->f_max);
809 else
810 mmc->f_max = min(host->mclk, fmax);
Linus Walleij64de0282010-02-19 01:09:10 +0100811 dev_dbg(mmc_dev(mmc), "clocking block at %u Hz\n", mmc->f_max);
812
Linus Walleij34e84f32009-09-22 14:41:40 +0100813#ifdef CONFIG_REGULATOR
814 /* If we're using the regulator framework, try to fetch a regulator */
815 host->vcc = regulator_get(&dev->dev, "vmmc");
816 if (IS_ERR(host->vcc))
817 host->vcc = NULL;
818 else {
819 int mask = mmc_regulator_get_ocrmask(host->vcc);
820
821 if (mask < 0)
822 dev_err(&dev->dev, "error getting OCR mask (%d)\n",
823 mask);
824 else {
825 host->mmc->ocr_avail = (u32) mask;
826 if (plat->ocr_mask)
827 dev_warn(&dev->dev,
828 "Provided ocr_mask/setpower will not be used "
829 "(using regulator instead)\n");
830 }
831 }
832#endif
833 /* Fall back to platform data if no regulator is found */
834 if (host->vcc == NULL)
835 mmc->ocr_avail = plat->ocr_mask;
Linus Walleij9e6c82c2009-09-14 12:57:11 +0100836 mmc->caps = plat->capabilities;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 /*
839 * We can do SGIO
840 */
Martin K. Petersena36274e2010-09-10 01:33:59 -0400841 mmc->max_segs = NR_SG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 /*
Rabin Vincent08458ef2010-07-21 12:55:59 +0100844 * Since only a certain number of bits are valid in the data length
845 * register, we must ensure that we don't exceed 2^num-1 bytes in a
846 * single request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 */
Rabin Vincent08458ef2010-07-21 12:55:59 +0100848 mmc->max_req_size = (1 << variant->datalength_bits) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 /*
851 * Set the maximum segment size. Since we aren't doing DMA
852 * (yet) we are only limited by the data length register.
853 */
Pierre Ossman55db8902006-11-21 17:55:45 +0100854 mmc->max_seg_size = mmc->max_req_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100856 /*
857 * Block size can be up to 2048 bytes, but must be a power of two.
858 */
859 mmc->max_blk_size = 2048;
860
Pierre Ossman55db8902006-11-21 17:55:45 +0100861 /*
862 * No limit on the number of blocks transferred.
863 */
864 mmc->max_blk_count = mmc->max_req_size;
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 spin_lock_init(&host->lock);
867
868 writel(0, host->base + MMCIMASK0);
869 writel(0, host->base + MMCIMASK1);
870 writel(0xfff, host->base + MMCICLEAR);
871
Russell King89001442009-07-09 15:16:07 +0100872 if (gpio_is_valid(plat->gpio_cd)) {
873 ret = gpio_request(plat->gpio_cd, DRIVER_NAME " (cd)");
874 if (ret == 0)
875 ret = gpio_direction_input(plat->gpio_cd);
876 if (ret == 0)
877 host->gpio_cd = plat->gpio_cd;
878 else if (ret != -ENOSYS)
879 goto err_gpio_cd;
Rabin Vincent148b8b32010-08-09 12:55:48 +0100880
881 ret = request_any_context_irq(gpio_to_irq(plat->gpio_cd),
882 mmci_cd_irq, 0,
883 DRIVER_NAME " (cd)", host);
884 if (ret >= 0)
885 host->gpio_cd_irq = gpio_to_irq(plat->gpio_cd);
Russell King89001442009-07-09 15:16:07 +0100886 }
887 if (gpio_is_valid(plat->gpio_wp)) {
888 ret = gpio_request(plat->gpio_wp, DRIVER_NAME " (wp)");
889 if (ret == 0)
890 ret = gpio_direction_input(plat->gpio_wp);
891 if (ret == 0)
892 host->gpio_wp = plat->gpio_wp;
893 else if (ret != -ENOSYS)
894 goto err_gpio_wp;
895 }
896
Rabin Vincent4b8caec2010-08-09 12:56:40 +0100897 if ((host->plat->status || host->gpio_cd != -ENOSYS)
898 && host->gpio_cd_irq < 0)
Rabin Vincent148b8b32010-08-09 12:55:48 +0100899 mmc->caps |= MMC_CAP_NEEDS_POLL;
900
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700901 ret = request_irq(dev->irq[0], mmci_irq, IRQF_SHARED, DRIVER_NAME " (cmd)", host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 if (ret)
903 goto unmap;
904
Linus Walleij2686b4b2010-10-19 12:39:48 +0100905 if (dev->irq[1] == NO_IRQ)
906 host->singleirq = true;
907 else {
908 ret = request_irq(dev->irq[1], mmci_pio_irq, IRQF_SHARED,
909 DRIVER_NAME " (pio)", host);
910 if (ret)
911 goto irq0_free;
912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Linus Walleij8cb28152011-01-24 15:22:13 +0100914 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 amba_set_drvdata(dev, mmc);
917
Russell King8c11a942010-12-28 19:40:40 +0000918 dev_info(&dev->dev, "%s: PL%03x rev%u at 0x%08llx irq %d,%d\n",
919 mmc_hostname(mmc), amba_part(dev), amba_rev(dev),
Greg Kroah-Hartmane29419f2006-06-12 15:20:16 -0700920 (unsigned long long)dev->res.start, dev->irq[0], dev->irq[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Russell King8c11a942010-12-28 19:40:40 +0000922 mmc_add_host(mmc);
923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 return 0;
925
926 irq0_free:
927 free_irq(dev->irq[0], host);
928 unmap:
Russell King89001442009-07-09 15:16:07 +0100929 if (host->gpio_wp != -ENOSYS)
930 gpio_free(host->gpio_wp);
931 err_gpio_wp:
Rabin Vincent148b8b32010-08-09 12:55:48 +0100932 if (host->gpio_cd_irq >= 0)
933 free_irq(host->gpio_cd_irq, host);
Russell King89001442009-07-09 15:16:07 +0100934 if (host->gpio_cd != -ENOSYS)
935 gpio_free(host->gpio_cd);
936 err_gpio_cd:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 iounmap(host->base);
938 clk_disable:
939 clk_disable(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 clk_free:
941 clk_put(host->clk);
942 host_free:
943 mmc_free_host(mmc);
944 rel_regions:
945 amba_release_regions(dev);
946 out:
947 return ret;
948}
949
Linus Walleij6dc4a472009-03-07 00:23:52 +0100950static int __devexit mmci_remove(struct amba_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
952 struct mmc_host *mmc = amba_get_drvdata(dev);
953
954 amba_set_drvdata(dev, NULL);
955
956 if (mmc) {
957 struct mmci_host *host = mmc_priv(mmc);
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 mmc_remove_host(mmc);
960
961 writel(0, host->base + MMCIMASK0);
962 writel(0, host->base + MMCIMASK1);
963
964 writel(0, host->base + MMCICOMMAND);
965 writel(0, host->base + MMCIDATACTRL);
966
967 free_irq(dev->irq[0], host);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100968 if (!host->singleirq)
969 free_irq(dev->irq[1], host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Russell King89001442009-07-09 15:16:07 +0100971 if (host->gpio_wp != -ENOSYS)
972 gpio_free(host->gpio_wp);
Rabin Vincent148b8b32010-08-09 12:55:48 +0100973 if (host->gpio_cd_irq >= 0)
974 free_irq(host->gpio_cd_irq, host);
Russell King89001442009-07-09 15:16:07 +0100975 if (host->gpio_cd != -ENOSYS)
976 gpio_free(host->gpio_cd);
977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 iounmap(host->base);
979 clk_disable(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 clk_put(host->clk);
981
Linus Walleij99fc5132010-09-29 01:08:27 -0400982 if (host->vcc)
983 mmc_regulator_set_ocr(mmc, host->vcc, 0);
Linus Walleij34e84f32009-09-22 14:41:40 +0100984 regulator_put(host->vcc);
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 mmc_free_host(mmc);
987
988 amba_release_regions(dev);
989 }
990
991 return 0;
992}
993
994#ifdef CONFIG_PM
Pavel Macheke5378ca2005-04-16 15:25:29 -0700995static int mmci_suspend(struct amba_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
997 struct mmc_host *mmc = amba_get_drvdata(dev);
998 int ret = 0;
999
1000 if (mmc) {
1001 struct mmci_host *host = mmc_priv(mmc);
1002
Matt Fleming1a13f8f2010-05-26 14:42:08 -07001003 ret = mmc_suspend_host(mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 if (ret == 0)
1005 writel(0, host->base + MMCIMASK0);
1006 }
1007
1008 return ret;
1009}
1010
1011static int mmci_resume(struct amba_device *dev)
1012{
1013 struct mmc_host *mmc = amba_get_drvdata(dev);
1014 int ret = 0;
1015
1016 if (mmc) {
1017 struct mmci_host *host = mmc_priv(mmc);
1018
1019 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
1020
1021 ret = mmc_resume_host(mmc);
1022 }
1023
1024 return ret;
1025}
1026#else
1027#define mmci_suspend NULL
1028#define mmci_resume NULL
1029#endif
1030
1031static struct amba_id mmci_ids[] = {
1032 {
1033 .id = 0x00041180,
1034 .mask = 0x000fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001035 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 },
1037 {
1038 .id = 0x00041181,
1039 .mask = 0x000fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001040 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 },
Linus Walleijcc30d602009-01-04 15:18:54 +01001042 /* ST Micro variants */
1043 {
1044 .id = 0x00180180,
1045 .mask = 0x00ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001046 .data = &variant_u300,
Linus Walleijcc30d602009-01-04 15:18:54 +01001047 },
1048 {
1049 .id = 0x00280180,
1050 .mask = 0x00ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001051 .data = &variant_u300,
1052 },
1053 {
1054 .id = 0x00480180,
1055 .mask = 0x00ffffff,
1056 .data = &variant_ux500,
Linus Walleijcc30d602009-01-04 15:18:54 +01001057 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 { 0, 0 },
1059};
1060
1061static struct amba_driver mmci_driver = {
1062 .drv = {
1063 .name = DRIVER_NAME,
1064 },
1065 .probe = mmci_probe,
Linus Walleij6dc4a472009-03-07 00:23:52 +01001066 .remove = __devexit_p(mmci_remove),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 .suspend = mmci_suspend,
1068 .resume = mmci_resume,
1069 .id_table = mmci_ids,
1070};
1071
1072static int __init mmci_init(void)
1073{
1074 return amba_driver_register(&mmci_driver);
1075}
1076
1077static void __exit mmci_exit(void)
1078{
1079 amba_driver_unregister(&mmci_driver);
1080}
1081
1082module_init(mmci_init);
1083module_exit(mmci_exit);
1084module_param(fmax, uint, 0444);
1085
1086MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
1087MODULE_LICENSE("GPL");