blob: 4941e06fe2e17511b552bd5a605ea12fb86c5880 [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.
Russell Kingc8ebae32011-01-11 19:35:53 +00005 * Copyright (C) 2010 ST-Ericsson SA
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 Walleij34e84f32009-09-22 14:41:40 +010028#include <linux/regulator/consumer.h>
Russell Kingc8ebae32011-01-11 19:35:53 +000029#include <linux/dmaengine.h>
30#include <linux/dma-mapping.h>
31#include <linux/amba/mmci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Russell King7b09cda2005-07-01 12:02:59 +010033#include <asm/div64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/io.h>
Russell Kingc6b8fda2005-10-28 14:05:16 +010035#include <asm/sizes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#include "mmci.h"
38
39#define DRIVER_NAME "mmci-pl18x"
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041static unsigned int fmax = 515633;
42
Rabin Vincent4956e102010-07-21 12:54:40 +010043/**
44 * struct variant_data - MMCI variant-specific quirks
45 * @clkreg: default value for MCICLOCK register
Rabin Vincent4380c142010-07-21 12:55:18 +010046 * @clkreg_enable: enable value for MMCICLOCK register
Rabin Vincent08458ef2010-07-21 12:55:59 +010047 * @datalength_bits: number of bits in the MMCIDATALENGTH register
Rabin Vincent8301bb62010-08-09 12:57:30 +010048 * @fifosize: number of bytes that can be written when MMCI_TXFIFOEMPTY
49 * is asserted (likewise for RX)
50 * @fifohalfsize: number of bytes that can be written when MCI_TXFIFOHALFEMPTY
51 * is asserted (likewise for RX)
Linus Walleij34177802010-10-19 12:43:58 +010052 * @sdio: variant supports SDIO
Linus Walleijb70a67f2010-12-06 09:24:14 +010053 * @st_clkdiv: true if using a ST-specific clock divider algorithm
Rabin Vincent4956e102010-07-21 12:54:40 +010054 */
55struct variant_data {
56 unsigned int clkreg;
Rabin Vincent4380c142010-07-21 12:55:18 +010057 unsigned int clkreg_enable;
Rabin Vincent08458ef2010-07-21 12:55:59 +010058 unsigned int datalength_bits;
Rabin Vincent8301bb62010-08-09 12:57:30 +010059 unsigned int fifosize;
60 unsigned int fifohalfsize;
Linus Walleij34177802010-10-19 12:43:58 +010061 bool sdio;
Linus Walleijb70a67f2010-12-06 09:24:14 +010062 bool st_clkdiv;
Rabin Vincent4956e102010-07-21 12:54:40 +010063};
64
65static struct variant_data variant_arm = {
Rabin Vincent8301bb62010-08-09 12:57:30 +010066 .fifosize = 16 * 4,
67 .fifohalfsize = 8 * 4,
Rabin Vincent08458ef2010-07-21 12:55:59 +010068 .datalength_bits = 16,
Rabin Vincent4956e102010-07-21 12:54:40 +010069};
70
Pawel Moll768fbc12011-03-11 17:18:07 +000071static struct variant_data variant_arm_extended_fifo = {
72 .fifosize = 128 * 4,
73 .fifohalfsize = 64 * 4,
74 .datalength_bits = 16,
75};
76
Rabin Vincent4956e102010-07-21 12:54:40 +010077static struct variant_data variant_u300 = {
Rabin Vincent8301bb62010-08-09 12:57:30 +010078 .fifosize = 16 * 4,
79 .fifohalfsize = 8 * 4,
Linus Walleij49ac2152011-03-04 14:54:16 +010080 .clkreg_enable = MCI_ST_U300_HWFCEN,
Rabin Vincent08458ef2010-07-21 12:55:59 +010081 .datalength_bits = 16,
Linus Walleij34177802010-10-19 12:43:58 +010082 .sdio = true,
Rabin Vincent4956e102010-07-21 12:54:40 +010083};
84
85static struct variant_data variant_ux500 = {
Rabin Vincent8301bb62010-08-09 12:57:30 +010086 .fifosize = 30 * 4,
87 .fifohalfsize = 8 * 4,
Rabin Vincent4956e102010-07-21 12:54:40 +010088 .clkreg = MCI_CLK_ENABLE,
Linus Walleij49ac2152011-03-04 14:54:16 +010089 .clkreg_enable = MCI_ST_UX500_HWFCEN,
Rabin Vincent08458ef2010-07-21 12:55:59 +010090 .datalength_bits = 24,
Linus Walleij34177802010-10-19 12:43:58 +010091 .sdio = true,
Linus Walleijb70a67f2010-12-06 09:24:14 +010092 .st_clkdiv = true,
Rabin Vincent4956e102010-07-21 12:54:40 +010093};
Linus Walleijb70a67f2010-12-06 09:24:14 +010094
Linus Walleija6a64642009-09-14 12:56:14 +010095/*
96 * This must be called with host->lock held
97 */
98static void mmci_set_clkreg(struct mmci_host *host, unsigned int desired)
99{
Rabin Vincent4956e102010-07-21 12:54:40 +0100100 struct variant_data *variant = host->variant;
101 u32 clk = variant->clkreg;
Linus Walleija6a64642009-09-14 12:56:14 +0100102
103 if (desired) {
104 if (desired >= host->mclk) {
Linus Walleij991a86e2010-12-10 09:35:53 +0100105 clk = MCI_CLK_BYPASS;
Linus Walleij399bc482011-04-01 07:59:17 +0100106 if (variant->st_clkdiv)
107 clk |= MCI_ST_UX500_NEG_EDGE;
Linus Walleija6a64642009-09-14 12:56:14 +0100108 host->cclk = host->mclk;
Linus Walleijb70a67f2010-12-06 09:24:14 +0100109 } else if (variant->st_clkdiv) {
110 /*
111 * DB8500 TRM says f = mclk / (clkdiv + 2)
112 * => clkdiv = (mclk / f) - 2
113 * Round the divider up so we don't exceed the max
114 * frequency
115 */
116 clk = DIV_ROUND_UP(host->mclk, desired) - 2;
117 if (clk >= 256)
118 clk = 255;
119 host->cclk = host->mclk / (clk + 2);
Linus Walleija6a64642009-09-14 12:56:14 +0100120 } else {
Linus Walleijb70a67f2010-12-06 09:24:14 +0100121 /*
122 * PL180 TRM says f = mclk / (2 * (clkdiv + 1))
123 * => clkdiv = mclk / (2 * f) - 1
124 */
Linus Walleija6a64642009-09-14 12:56:14 +0100125 clk = host->mclk / (2 * desired) - 1;
126 if (clk >= 256)
127 clk = 255;
128 host->cclk = host->mclk / (2 * (clk + 1));
129 }
Rabin Vincent4380c142010-07-21 12:55:18 +0100130
131 clk |= variant->clkreg_enable;
Linus Walleija6a64642009-09-14 12:56:14 +0100132 clk |= MCI_CLK_ENABLE;
133 /* This hasn't proven to be worthwhile */
134 /* clk |= MCI_CLK_PWRSAVE; */
135 }
136
Linus Walleij9e6c82c2009-09-14 12:57:11 +0100137 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4)
Linus Walleij771dc152010-04-08 07:38:52 +0100138 clk |= MCI_4BIT_BUS;
139 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_8)
140 clk |= MCI_ST_8BIT_BUS;
Linus Walleij9e6c82c2009-09-14 12:57:11 +0100141
Linus Walleija6a64642009-09-14 12:56:14 +0100142 writel(clk, host->base + MMCICLOCK);
143}
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145static void
146mmci_request_end(struct mmci_host *host, struct mmc_request *mrq)
147{
148 writel(0, host->base + MMCICOMMAND);
149
Russell Kinge47c2222007-01-08 16:42:51 +0000150 BUG_ON(host->data);
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 host->mrq = NULL;
153 host->cmd = NULL;
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 /*
156 * Need to drop the host lock here; mmc_request_done may call
157 * back into the driver...
158 */
159 spin_unlock(&host->lock);
160 mmc_request_done(host->mmc, mrq);
161 spin_lock(&host->lock);
162}
163
Linus Walleij2686b4b2010-10-19 12:39:48 +0100164static void mmci_set_mask1(struct mmci_host *host, unsigned int mask)
165{
166 void __iomem *base = host->base;
167
168 if (host->singleirq) {
169 unsigned int mask0 = readl(base + MMCIMASK0);
170
171 mask0 &= ~MCI_IRQ1MASK;
172 mask0 |= mask;
173
174 writel(mask0, base + MMCIMASK0);
175 }
176
177 writel(mask, base + MMCIMASK1);
178}
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180static void mmci_stop_data(struct mmci_host *host)
181{
182 writel(0, host->base + MMCIDATACTRL);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100183 mmci_set_mask1(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 host->data = NULL;
185}
186
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100187static void mmci_init_sg(struct mmci_host *host, struct mmc_data *data)
188{
189 unsigned int flags = SG_MITER_ATOMIC;
190
191 if (data->flags & MMC_DATA_READ)
192 flags |= SG_MITER_TO_SG;
193 else
194 flags |= SG_MITER_FROM_SG;
195
196 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
197}
198
Russell Kingc8ebae32011-01-11 19:35:53 +0000199/*
200 * All the DMA operation mode stuff goes inside this ifdef.
201 * This assumes that you have a generic DMA device interface,
202 * no custom DMA interfaces are supported.
203 */
204#ifdef CONFIG_DMA_ENGINE
205static void __devinit mmci_dma_setup(struct mmci_host *host)
206{
207 struct mmci_platform_data *plat = host->plat;
208 const char *rxname, *txname;
209 dma_cap_mask_t mask;
210
211 if (!plat || !plat->dma_filter) {
212 dev_info(mmc_dev(host->mmc), "no DMA platform data\n");
213 return;
214 }
215
216 /* Try to acquire a generic DMA engine slave channel */
217 dma_cap_zero(mask);
218 dma_cap_set(DMA_SLAVE, mask);
219
220 /*
221 * If only an RX channel is specified, the driver will
222 * attempt to use it bidirectionally, however if it is
223 * is specified but cannot be located, DMA will be disabled.
224 */
225 if (plat->dma_rx_param) {
226 host->dma_rx_channel = dma_request_channel(mask,
227 plat->dma_filter,
228 plat->dma_rx_param);
229 /* E.g if no DMA hardware is present */
230 if (!host->dma_rx_channel)
231 dev_err(mmc_dev(host->mmc), "no RX DMA channel\n");
232 }
233
234 if (plat->dma_tx_param) {
235 host->dma_tx_channel = dma_request_channel(mask,
236 plat->dma_filter,
237 plat->dma_tx_param);
238 if (!host->dma_tx_channel)
239 dev_warn(mmc_dev(host->mmc), "no TX DMA channel\n");
240 } else {
241 host->dma_tx_channel = host->dma_rx_channel;
242 }
243
244 if (host->dma_rx_channel)
245 rxname = dma_chan_name(host->dma_rx_channel);
246 else
247 rxname = "none";
248
249 if (host->dma_tx_channel)
250 txname = dma_chan_name(host->dma_tx_channel);
251 else
252 txname = "none";
253
254 dev_info(mmc_dev(host->mmc), "DMA channels RX %s, TX %s\n",
255 rxname, txname);
256
257 /*
258 * Limit the maximum segment size in any SG entry according to
259 * the parameters of the DMA engine device.
260 */
261 if (host->dma_tx_channel) {
262 struct device *dev = host->dma_tx_channel->device->dev;
263 unsigned int max_seg_size = dma_get_max_seg_size(dev);
264
265 if (max_seg_size < host->mmc->max_seg_size)
266 host->mmc->max_seg_size = max_seg_size;
267 }
268 if (host->dma_rx_channel) {
269 struct device *dev = host->dma_rx_channel->device->dev;
270 unsigned int max_seg_size = dma_get_max_seg_size(dev);
271
272 if (max_seg_size < host->mmc->max_seg_size)
273 host->mmc->max_seg_size = max_seg_size;
274 }
275}
276
277/*
278 * This is used in __devinit or __devexit so inline it
279 * so it can be discarded.
280 */
281static inline void mmci_dma_release(struct mmci_host *host)
282{
283 struct mmci_platform_data *plat = host->plat;
284
285 if (host->dma_rx_channel)
286 dma_release_channel(host->dma_rx_channel);
287 if (host->dma_tx_channel && plat->dma_tx_param)
288 dma_release_channel(host->dma_tx_channel);
289 host->dma_rx_channel = host->dma_tx_channel = NULL;
290}
291
292static void mmci_dma_unmap(struct mmci_host *host, struct mmc_data *data)
293{
294 struct dma_chan *chan = host->dma_current;
295 enum dma_data_direction dir;
296 u32 status;
297 int i;
298
299 /* Wait up to 1ms for the DMA to complete */
300 for (i = 0; ; i++) {
301 status = readl(host->base + MMCISTATUS);
302 if (!(status & MCI_RXDATAAVLBLMASK) || i >= 100)
303 break;
304 udelay(10);
305 }
306
307 /*
308 * Check to see whether we still have some data left in the FIFO -
309 * this catches DMA controllers which are unable to monitor the
310 * DMALBREQ and DMALSREQ signals while allowing us to DMA to non-
311 * contiguous buffers. On TX, we'll get a FIFO underrun error.
312 */
313 if (status & MCI_RXDATAAVLBLMASK) {
314 dmaengine_terminate_all(chan);
315 if (!data->error)
316 data->error = -EIO;
317 }
318
319 if (data->flags & MMC_DATA_WRITE) {
320 dir = DMA_TO_DEVICE;
321 } else {
322 dir = DMA_FROM_DEVICE;
323 }
324
325 dma_unmap_sg(chan->device->dev, data->sg, data->sg_len, dir);
326
327 /*
328 * Use of DMA with scatter-gather is impossible.
329 * Give up with DMA and switch back to PIO mode.
330 */
331 if (status & MCI_RXDATAAVLBLMASK) {
332 dev_err(mmc_dev(host->mmc), "buggy DMA detected. Taking evasive action.\n");
333 mmci_dma_release(host);
334 }
335}
336
337static void mmci_dma_data_error(struct mmci_host *host)
338{
339 dev_err(mmc_dev(host->mmc), "error during DMA transfer!\n");
340 dmaengine_terminate_all(host->dma_current);
341}
342
343static int mmci_dma_start_data(struct mmci_host *host, unsigned int datactrl)
344{
345 struct variant_data *variant = host->variant;
346 struct dma_slave_config conf = {
347 .src_addr = host->phybase + MMCIFIFO,
348 .dst_addr = host->phybase + MMCIFIFO,
349 .src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
350 .dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
351 .src_maxburst = variant->fifohalfsize >> 2, /* # of words */
352 .dst_maxburst = variant->fifohalfsize >> 2, /* # of words */
353 };
354 struct mmc_data *data = host->data;
355 struct dma_chan *chan;
356 struct dma_device *device;
357 struct dma_async_tx_descriptor *desc;
358 int nr_sg;
359
360 host->dma_current = NULL;
361
362 if (data->flags & MMC_DATA_READ) {
363 conf.direction = DMA_FROM_DEVICE;
364 chan = host->dma_rx_channel;
365 } else {
366 conf.direction = DMA_TO_DEVICE;
367 chan = host->dma_tx_channel;
368 }
369
370 /* If there's no DMA channel, fall back to PIO */
371 if (!chan)
372 return -EINVAL;
373
374 /* If less than or equal to the fifo size, don't bother with DMA */
375 if (host->size <= variant->fifosize)
376 return -EINVAL;
377
378 device = chan->device;
379 nr_sg = dma_map_sg(device->dev, data->sg, data->sg_len, conf.direction);
380 if (nr_sg == 0)
381 return -EINVAL;
382
383 dmaengine_slave_config(chan, &conf);
384 desc = device->device_prep_slave_sg(chan, data->sg, nr_sg,
385 conf.direction, DMA_CTRL_ACK);
386 if (!desc)
387 goto unmap_exit;
388
389 /* Okay, go for it. */
390 host->dma_current = chan;
391
392 dev_vdbg(mmc_dev(host->mmc),
393 "Submit MMCI DMA job, sglen %d blksz %04x blks %04x flags %08x\n",
394 data->sg_len, data->blksz, data->blocks, data->flags);
395 dmaengine_submit(desc);
396 dma_async_issue_pending(chan);
397
398 datactrl |= MCI_DPSM_DMAENABLE;
399
400 /* Trigger the DMA transfer */
401 writel(datactrl, host->base + MMCIDATACTRL);
402
403 /*
404 * Let the MMCI say when the data is ended and it's time
405 * to fire next DMA request. When that happens, MMCI will
406 * call mmci_data_end()
407 */
408 writel(readl(host->base + MMCIMASK0) | MCI_DATAENDMASK,
409 host->base + MMCIMASK0);
410 return 0;
411
412unmap_exit:
413 dmaengine_terminate_all(chan);
414 dma_unmap_sg(device->dev, data->sg, data->sg_len, conf.direction);
415 return -ENOMEM;
416}
417#else
418/* Blank functions if the DMA engine is not available */
419static inline void mmci_dma_setup(struct mmci_host *host)
420{
421}
422
423static inline void mmci_dma_release(struct mmci_host *host)
424{
425}
426
427static inline void mmci_dma_unmap(struct mmci_host *host, struct mmc_data *data)
428{
429}
430
431static inline void mmci_dma_data_error(struct mmci_host *host)
432{
433}
434
435static inline int mmci_dma_start_data(struct mmci_host *host, unsigned int datactrl)
436{
437 return -ENOSYS;
438}
439#endif
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441static void mmci_start_data(struct mmci_host *host, struct mmc_data *data)
442{
Rabin Vincent8301bb62010-08-09 12:57:30 +0100443 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 unsigned int datactrl, timeout, irqmask;
Russell King7b09cda2005-07-01 12:02:59 +0100445 unsigned long long clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 void __iomem *base;
Russell King3bc87f22006-08-27 13:51:28 +0100447 int blksz_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Linus Walleij64de0282010-02-19 01:09:10 +0100449 dev_dbg(mmc_dev(host->mmc), "blksz %04x blks %04x flags %08x\n",
450 data->blksz, data->blocks, data->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 host->data = data;
Rabin Vincent528320d2010-07-21 12:49:49 +0100453 host->size = data->blksz * data->blocks;
Russell King51d43752011-01-27 10:56:52 +0000454 data->bytes_xfered = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Russell King7b09cda2005-07-01 12:02:59 +0100456 clks = (unsigned long long)data->timeout_ns * host->cclk;
457 do_div(clks, 1000000000UL);
458
459 timeout = data->timeout_clks + (unsigned int)clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 base = host->base;
462 writel(timeout, base + MMCIDATATIMER);
463 writel(host->size, base + MMCIDATALENGTH);
464
Russell King3bc87f22006-08-27 13:51:28 +0100465 blksz_bits = ffs(data->blksz) - 1;
466 BUG_ON(1 << blksz_bits != data->blksz);
467
468 datactrl = MCI_DPSM_ENABLE | blksz_bits << 4;
Russell Kingc8ebae32011-01-11 19:35:53 +0000469
470 if (data->flags & MMC_DATA_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 datactrl |= MCI_DPSM_DIRECTION;
Russell Kingc8ebae32011-01-11 19:35:53 +0000472
473 /*
474 * Attempt to use DMA operation mode, if this
475 * should fail, fall back to PIO mode
476 */
477 if (!mmci_dma_start_data(host, datactrl))
478 return;
479
480 /* IRQ mode, map the SG list for CPU reading/writing */
481 mmci_init_sg(host, data);
482
483 if (data->flags & MMC_DATA_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 irqmask = MCI_RXFIFOHALFFULLMASK;
Russell King0425a142006-02-16 16:48:31 +0000485
486 /*
Russell Kingc4d877c2011-01-27 09:50:13 +0000487 * If we have less than the fifo 'half-full' threshold to
488 * transfer, trigger a PIO interrupt as soon as any data
489 * is available.
Russell King0425a142006-02-16 16:48:31 +0000490 */
Russell Kingc4d877c2011-01-27 09:50:13 +0000491 if (host->size < variant->fifohalfsize)
Russell King0425a142006-02-16 16:48:31 +0000492 irqmask |= MCI_RXDATAAVLBLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 } else {
494 /*
495 * We don't actually need to include "FIFO empty" here
496 * since its implicit in "FIFO half empty".
497 */
498 irqmask = MCI_TXFIFOHALFEMPTYMASK;
499 }
500
Linus Walleij34177802010-10-19 12:43:58 +0100501 /* The ST Micro variants has a special bit to enable SDIO */
502 if (variant->sdio && host->mmc->card)
503 if (mmc_card_sdio(host->mmc->card))
504 datactrl |= MCI_ST_DPSM_SDIOEN;
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 writel(datactrl, base + MMCIDATACTRL);
507 writel(readl(base + MMCIMASK0) & ~MCI_DATAENDMASK, base + MMCIMASK0);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100508 mmci_set_mask1(host, irqmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
510
511static void
512mmci_start_command(struct mmci_host *host, struct mmc_command *cmd, u32 c)
513{
514 void __iomem *base = host->base;
515
Linus Walleij64de0282010-02-19 01:09:10 +0100516 dev_dbg(mmc_dev(host->mmc), "op %02x arg %08x flags %08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 cmd->opcode, cmd->arg, cmd->flags);
518
519 if (readl(base + MMCICOMMAND) & MCI_CPSM_ENABLE) {
520 writel(0, base + MMCICOMMAND);
521 udelay(1);
522 }
523
524 c |= cmd->opcode | MCI_CPSM_ENABLE;
Russell Kinge9225172006-02-02 12:23:12 +0000525 if (cmd->flags & MMC_RSP_PRESENT) {
526 if (cmd->flags & MMC_RSP_136)
527 c |= MCI_CPSM_LONGRSP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 c |= MCI_CPSM_RESPONSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
530 if (/*interrupt*/0)
531 c |= MCI_CPSM_INTERRUPT;
532
533 host->cmd = cmd;
534
535 writel(cmd->arg, base + MMCIARGUMENT);
536 writel(c, base + MMCICOMMAND);
537}
538
539static void
540mmci_data_irq(struct mmci_host *host, struct mmc_data *data,
541 unsigned int status)
542{
Linus Walleijf20f8f22010-10-19 13:41:24 +0100543 /* First check for errors */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|MCI_RXOVERRUN)) {
Linus Walleij8cb28152011-01-24 15:22:13 +0100545 u32 remain, success;
Linus Walleijf20f8f22010-10-19 13:41:24 +0100546
Russell Kingc8ebae32011-01-11 19:35:53 +0000547 /* Terminate the DMA transfer */
548 if (dma_inprogress(host))
549 mmci_dma_data_error(host);
550
Russell Kingc8afc9d2011-02-04 09:19:46 +0000551 /*
552 * Calculate how far we are into the transfer. Note that
553 * the data counter gives the number of bytes transferred
554 * on the MMC bus, not on the host side. On reads, this
555 * can be as much as a FIFO-worth of data ahead. This
556 * matters for FIFO overruns only.
557 */
Linus Walleijf5a106d2011-01-27 17:44:34 +0100558 remain = readl(host->base + MMCIDATACNT);
Linus Walleij8cb28152011-01-24 15:22:13 +0100559 success = data->blksz * data->blocks - remain;
560
Russell Kingc8afc9d2011-02-04 09:19:46 +0000561 dev_dbg(mmc_dev(host->mmc), "MCI ERROR IRQ, status 0x%08x at 0x%08x\n",
562 status, success);
Linus Walleij8cb28152011-01-24 15:22:13 +0100563 if (status & MCI_DATACRCFAIL) {
564 /* Last block was not successful */
Russell Kingc8afc9d2011-02-04 09:19:46 +0000565 success -= 1;
Pierre Ossman17b04292007-07-22 22:18:46 +0200566 data->error = -EILSEQ;
Linus Walleij8cb28152011-01-24 15:22:13 +0100567 } else if (status & MCI_DATATIMEOUT) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200568 data->error = -ETIMEDOUT;
Russell Kingc8afc9d2011-02-04 09:19:46 +0000569 } else if (status & MCI_TXUNDERRUN) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200570 data->error = -EIO;
Russell Kingc8afc9d2011-02-04 09:19:46 +0000571 } else if (status & MCI_RXOVERRUN) {
572 if (success > host->variant->fifosize)
573 success -= host->variant->fifosize;
574 else
575 success = 0;
Linus Walleij8cb28152011-01-24 15:22:13 +0100576 data->error = -EIO;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100577 }
Russell King51d43752011-01-27 10:56:52 +0000578 data->bytes_xfered = round_down(success, data->blksz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
Linus Walleijf20f8f22010-10-19 13:41:24 +0100580
Linus Walleij8cb28152011-01-24 15:22:13 +0100581 if (status & MCI_DATABLOCKEND)
582 dev_err(mmc_dev(host->mmc), "stray MCI_DATABLOCKEND interrupt\n");
Linus Walleijf20f8f22010-10-19 13:41:24 +0100583
Russell Kingccff9b52011-01-30 21:03:50 +0000584 if (status & MCI_DATAEND || data->error) {
Russell Kingc8ebae32011-01-11 19:35:53 +0000585 if (dma_inprogress(host))
586 mmci_dma_unmap(host, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 mmci_stop_data(host);
588
Linus Walleij8cb28152011-01-24 15:22:13 +0100589 if (!data->error)
590 /* The error clause is handled above, success! */
Russell King51d43752011-01-27 10:56:52 +0000591 data->bytes_xfered = data->blksz * data->blocks;
Linus Walleijf20f8f22010-10-19 13:41:24 +0100592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (!data->stop) {
594 mmci_request_end(host, data->mrq);
595 } else {
596 mmci_start_command(host, data->stop, 0);
597 }
598 }
599}
600
601static void
602mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
603 unsigned int status)
604{
605 void __iomem *base = host->base;
606
607 host->cmd = NULL;
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (status & MCI_CMDTIMEOUT) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200610 cmd->error = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200612 cmd->error = -EILSEQ;
Russell King - ARM Linux9047b432011-01-11 16:35:56 +0000613 } else {
614 cmd->resp[0] = readl(base + MMCIRESPONSE0);
615 cmd->resp[1] = readl(base + MMCIRESPONSE1);
616 cmd->resp[2] = readl(base + MMCIRESPONSE2);
617 cmd->resp[3] = readl(base + MMCIRESPONSE3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 }
619
Pierre Ossman17b04292007-07-22 22:18:46 +0200620 if (!cmd->data || cmd->error) {
Russell Kinge47c2222007-01-08 16:42:51 +0000621 if (host->data)
622 mmci_stop_data(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 mmci_request_end(host, cmd->mrq);
624 } else if (!(cmd->data->flags & MMC_DATA_READ)) {
625 mmci_start_data(host, cmd->data);
626 }
627}
628
629static int mmci_pio_read(struct mmci_host *host, char *buffer, unsigned int remain)
630{
631 void __iomem *base = host->base;
632 char *ptr = buffer;
633 u32 status;
Linus Walleij26eed9a2008-04-26 23:39:44 +0100634 int host_remain = host->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636 do {
Linus Walleij26eed9a2008-04-26 23:39:44 +0100637 int count = host_remain - (readl(base + MMCIFIFOCNT) << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 if (count > remain)
640 count = remain;
641
642 if (count <= 0)
643 break;
644
645 readsl(base + MMCIFIFO, ptr, count >> 2);
646
647 ptr += count;
648 remain -= count;
Linus Walleij26eed9a2008-04-26 23:39:44 +0100649 host_remain -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 if (remain == 0)
652 break;
653
654 status = readl(base + MMCISTATUS);
655 } while (status & MCI_RXDATAAVLBL);
656
657 return ptr - buffer;
658}
659
660static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status)
661{
Rabin Vincent8301bb62010-08-09 12:57:30 +0100662 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 void __iomem *base = host->base;
664 char *ptr = buffer;
665
666 do {
667 unsigned int count, maxcnt;
668
Rabin Vincent8301bb62010-08-09 12:57:30 +0100669 maxcnt = status & MCI_TXFIFOEMPTY ?
670 variant->fifosize : variant->fifohalfsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 count = min(remain, maxcnt);
672
Linus Walleij34177802010-10-19 12:43:58 +0100673 /*
674 * The ST Micro variant for SDIO transfer sizes
675 * less then 8 bytes should have clock H/W flow
676 * control disabled.
677 */
678 if (variant->sdio &&
679 mmc_card_sdio(host->mmc->card)) {
680 if (count < 8)
681 writel(readl(host->base + MMCICLOCK) &
682 ~variant->clkreg_enable,
683 host->base + MMCICLOCK);
684 else
685 writel(readl(host->base + MMCICLOCK) |
686 variant->clkreg_enable,
687 host->base + MMCICLOCK);
688 }
689
690 /*
691 * SDIO especially may want to send something that is
692 * not divisible by 4 (as opposed to card sectors
693 * etc), and the FIFO only accept full 32-bit writes.
694 * So compensate by adding +3 on the count, a single
695 * byte become a 32bit write, 7 bytes will be two
696 * 32bit writes etc.
697 */
698 writesl(base + MMCIFIFO, ptr, (count + 3) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 ptr += count;
701 remain -= count;
702
703 if (remain == 0)
704 break;
705
706 status = readl(base + MMCISTATUS);
707 } while (status & MCI_TXFIFOHALFEMPTY);
708
709 return ptr - buffer;
710}
711
712/*
713 * PIO data transfer IRQ handler.
714 */
David Howells7d12e782006-10-05 14:55:46 +0100715static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
717 struct mmci_host *host = dev_id;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100718 struct sg_mapping_iter *sg_miter = &host->sg_miter;
Rabin Vincent8301bb62010-08-09 12:57:30 +0100719 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 void __iomem *base = host->base;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100721 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 u32 status;
723
724 status = readl(base + MMCISTATUS);
725
Linus Walleij64de0282010-02-19 01:09:10 +0100726 dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100728 local_irq_save(flags);
729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 unsigned int remain, len;
732 char *buffer;
733
734 /*
735 * For write, we only need to test the half-empty flag
736 * here - if the FIFO is completely empty, then by
737 * definition it is more than half empty.
738 *
739 * For read, check for data available.
740 */
741 if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL)))
742 break;
743
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100744 if (!sg_miter_next(sg_miter))
745 break;
746
747 buffer = sg_miter->addr;
748 remain = sg_miter->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 len = 0;
751 if (status & MCI_RXACTIVE)
752 len = mmci_pio_read(host, buffer, remain);
753 if (status & MCI_TXACTIVE)
754 len = mmci_pio_write(host, buffer, remain, status);
755
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100756 sg_miter->consumed = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 host->size -= len;
759 remain -= len;
760
761 if (remain)
762 break;
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 status = readl(base + MMCISTATUS);
765 } while (1);
766
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100767 sg_miter_stop(sg_miter);
768
769 local_irq_restore(flags);
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 /*
Russell Kingc4d877c2011-01-27 09:50:13 +0000772 * If we have less than the fifo 'half-full' threshold to transfer,
773 * trigger a PIO interrupt as soon as any data is available.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 */
Russell Kingc4d877c2011-01-27 09:50:13 +0000775 if (status & MCI_RXACTIVE && host->size < variant->fifohalfsize)
Linus Walleij2686b4b2010-10-19 12:39:48 +0100776 mmci_set_mask1(host, MCI_RXDATAAVLBLMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 /*
779 * If we run out of data, disable the data IRQs; this
780 * prevents a race where the FIFO becomes empty before
781 * the chip itself has disabled the data path, and
782 * stops us racing with our data end IRQ.
783 */
784 if (host->size == 0) {
Linus Walleij2686b4b2010-10-19 12:39:48 +0100785 mmci_set_mask1(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0);
787 }
788
789 return IRQ_HANDLED;
790}
791
792/*
793 * Handle completion of command and data transfers.
794 */
David Howells7d12e782006-10-05 14:55:46 +0100795static irqreturn_t mmci_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
797 struct mmci_host *host = dev_id;
798 u32 status;
799 int ret = 0;
800
801 spin_lock(&host->lock);
802
803 do {
804 struct mmc_command *cmd;
805 struct mmc_data *data;
806
807 status = readl(host->base + MMCISTATUS);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100808
809 if (host->singleirq) {
810 if (status & readl(host->base + MMCIMASK1))
811 mmci_pio_irq(irq, dev_id);
812
813 status &= ~MCI_IRQ1MASK;
814 }
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 status &= readl(host->base + MMCIMASK0);
817 writel(status, host->base + MMCICLEAR);
818
Linus Walleij64de0282010-02-19 01:09:10 +0100819 dev_dbg(mmc_dev(host->mmc), "irq0 (data+cmd) %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 data = host->data;
822 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|
823 MCI_RXOVERRUN|MCI_DATAEND|MCI_DATABLOCKEND) && data)
824 mmci_data_irq(host, data, status);
825
826 cmd = host->cmd;
827 if (status & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT|MCI_CMDSENT|MCI_CMDRESPEND) && cmd)
828 mmci_cmd_irq(host, cmd, status);
829
830 ret = 1;
831 } while (status);
832
833 spin_unlock(&host->lock);
834
835 return IRQ_RETVAL(ret);
836}
837
838static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
839{
840 struct mmci_host *host = mmc_priv(mmc);
Linus Walleij9e943022008-10-24 21:17:50 +0100841 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 WARN_ON(host->mrq != NULL);
844
Nicolas Pitre019a5f52007-10-11 01:06:03 -0400845 if (mrq->data && !is_power_of_2(mrq->data->blksz)) {
Linus Walleij64de0282010-02-19 01:09:10 +0100846 dev_err(mmc_dev(mmc), "unsupported block size (%d bytes)\n",
847 mrq->data->blksz);
Pierre Ossman255d01a2007-07-24 20:38:53 +0200848 mrq->cmd->error = -EINVAL;
849 mmc_request_done(mmc, mrq);
850 return;
851 }
852
Linus Walleij9e943022008-10-24 21:17:50 +0100853 spin_lock_irqsave(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855 host->mrq = mrq;
856
857 if (mrq->data && mrq->data->flags & MMC_DATA_READ)
858 mmci_start_data(host, mrq->data);
859
860 mmci_start_command(host, mrq->cmd, 0);
861
Linus Walleij9e943022008-10-24 21:17:50 +0100862 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863}
864
865static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
866{
867 struct mmci_host *host = mmc_priv(mmc);
Linus Walleija6a64642009-09-14 12:56:14 +0100868 u32 pwr = 0;
869 unsigned long flags;
Linus Walleij99fc5132010-09-29 01:08:27 -0400870 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 switch (ios->power_mode) {
873 case MMC_POWER_OFF:
Linus Walleij99fc5132010-09-29 01:08:27 -0400874 if (host->vcc)
875 ret = mmc_regulator_set_ocr(mmc, host->vcc, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 break;
877 case MMC_POWER_UP:
Linus Walleij99fc5132010-09-29 01:08:27 -0400878 if (host->vcc) {
879 ret = mmc_regulator_set_ocr(mmc, host->vcc, ios->vdd);
880 if (ret) {
881 dev_err(mmc_dev(mmc), "unable to set OCR\n");
882 /*
883 * The .set_ios() function in the mmc_host_ops
884 * struct return void, and failing to set the
885 * power should be rare so we print an error
886 * and return here.
887 */
888 return;
889 }
890 }
Rabin Vincentbb8f5632010-07-21 12:53:57 +0100891 if (host->plat->vdd_handler)
892 pwr |= host->plat->vdd_handler(mmc_dev(mmc), ios->vdd,
893 ios->power_mode);
Linus Walleijcc30d602009-01-04 15:18:54 +0100894 /* The ST version does not have this, fall through to POWER_ON */
Linus Walleijf17a1f02009-08-04 01:01:02 +0100895 if (host->hw_designer != AMBA_VENDOR_ST) {
Linus Walleijcc30d602009-01-04 15:18:54 +0100896 pwr |= MCI_PWR_UP;
897 break;
898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 case MMC_POWER_ON:
900 pwr |= MCI_PWR_ON;
901 break;
902 }
903
Linus Walleijcc30d602009-01-04 15:18:54 +0100904 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN) {
Linus Walleijf17a1f02009-08-04 01:01:02 +0100905 if (host->hw_designer != AMBA_VENDOR_ST)
Linus Walleijcc30d602009-01-04 15:18:54 +0100906 pwr |= MCI_ROD;
907 else {
908 /*
909 * The ST Micro variant use the ROD bit for something
910 * else and only has OD (Open Drain).
911 */
912 pwr |= MCI_OD;
913 }
914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Linus Walleija6a64642009-09-14 12:56:14 +0100916 spin_lock_irqsave(&host->lock, flags);
917
918 mmci_set_clkreg(host, ios->clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 if (host->pwr != pwr) {
921 host->pwr = pwr;
922 writel(pwr, host->base + MMCIPOWER);
923 }
Linus Walleija6a64642009-09-14 12:56:14 +0100924
925 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926}
927
Russell King89001442009-07-09 15:16:07 +0100928static int mmci_get_ro(struct mmc_host *mmc)
929{
930 struct mmci_host *host = mmc_priv(mmc);
931
932 if (host->gpio_wp == -ENOSYS)
933 return -ENOSYS;
934
Linus Walleij18a06302010-09-12 12:56:44 +0100935 return gpio_get_value_cansleep(host->gpio_wp);
Russell King89001442009-07-09 15:16:07 +0100936}
937
938static int mmci_get_cd(struct mmc_host *mmc)
939{
940 struct mmci_host *host = mmc_priv(mmc);
Rabin Vincent29719442010-08-09 12:54:43 +0100941 struct mmci_platform_data *plat = host->plat;
Russell King89001442009-07-09 15:16:07 +0100942 unsigned int status;
943
Rabin Vincent4b8caec2010-08-09 12:56:40 +0100944 if (host->gpio_cd == -ENOSYS) {
945 if (!plat->status)
946 return 1; /* Assume always present */
947
Rabin Vincent29719442010-08-09 12:54:43 +0100948 status = plat->status(mmc_dev(host->mmc));
Rabin Vincent4b8caec2010-08-09 12:56:40 +0100949 } else
Linus Walleij18a06302010-09-12 12:56:44 +0100950 status = !!gpio_get_value_cansleep(host->gpio_cd)
951 ^ plat->cd_invert;
Russell King89001442009-07-09 15:16:07 +0100952
Russell King74bc8092010-07-29 15:58:59 +0100953 /*
954 * Use positive logic throughout - status is zero for no card,
955 * non-zero for card inserted.
956 */
957 return status;
Russell King89001442009-07-09 15:16:07 +0100958}
959
Rabin Vincent148b8b32010-08-09 12:55:48 +0100960static irqreturn_t mmci_cd_irq(int irq, void *dev_id)
961{
962 struct mmci_host *host = dev_id;
963
964 mmc_detect_change(host->mmc, msecs_to_jiffies(500));
965
966 return IRQ_HANDLED;
967}
968
David Brownellab7aefd2006-11-12 17:55:30 -0800969static const struct mmc_host_ops mmci_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 .request = mmci_request,
971 .set_ios = mmci_set_ios,
Russell King89001442009-07-09 15:16:07 +0100972 .get_ro = mmci_get_ro,
973 .get_cd = mmci_get_cd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974};
975
Russell Kingaa25afa2011-02-19 15:55:00 +0000976static int __devinit mmci_probe(struct amba_device *dev,
977 const struct amba_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
Linus Walleij6ef297f2009-09-22 14:29:36 +0100979 struct mmci_platform_data *plat = dev->dev.platform_data;
Rabin Vincent4956e102010-07-21 12:54:40 +0100980 struct variant_data *variant = id->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 struct mmci_host *host;
982 struct mmc_host *mmc;
983 int ret;
984
985 /* must have platform data */
986 if (!plat) {
987 ret = -EINVAL;
988 goto out;
989 }
990
991 ret = amba_request_regions(dev, DRIVER_NAME);
992 if (ret)
993 goto out;
994
995 mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
996 if (!mmc) {
997 ret = -ENOMEM;
998 goto rel_regions;
999 }
1000
1001 host = mmc_priv(mmc);
Rabin Vincent4ea580f2009-04-17 08:44:19 +05301002 host->mmc = mmc;
Russell King012b7d32009-07-09 15:13:56 +01001003
Russell King89001442009-07-09 15:16:07 +01001004 host->gpio_wp = -ENOSYS;
1005 host->gpio_cd = -ENOSYS;
Rabin Vincent148b8b32010-08-09 12:55:48 +01001006 host->gpio_cd_irq = -1;
Russell King89001442009-07-09 15:16:07 +01001007
Russell King012b7d32009-07-09 15:13:56 +01001008 host->hw_designer = amba_manf(dev);
1009 host->hw_revision = amba_rev(dev);
Linus Walleij64de0282010-02-19 01:09:10 +01001010 dev_dbg(mmc_dev(mmc), "designer ID = 0x%02x\n", host->hw_designer);
1011 dev_dbg(mmc_dev(mmc), "revision = 0x%01x\n", host->hw_revision);
Russell King012b7d32009-07-09 15:13:56 +01001012
Russell Kingee569c42008-11-30 17:38:14 +00001013 host->clk = clk_get(&dev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 if (IS_ERR(host->clk)) {
1015 ret = PTR_ERR(host->clk);
1016 host->clk = NULL;
1017 goto host_free;
1018 }
1019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 ret = clk_enable(host->clk);
1021 if (ret)
Russell Kinga8d35842006-01-03 18:41:37 +00001022 goto clk_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 host->plat = plat;
Rabin Vincent4956e102010-07-21 12:54:40 +01001025 host->variant = variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 host->mclk = clk_get_rate(host->clk);
Linus Walleijc8df9a52008-04-29 09:34:07 +01001027 /*
1028 * According to the spec, mclk is max 100 MHz,
1029 * so we try to adjust the clock down to this,
1030 * (if possible).
1031 */
1032 if (host->mclk > 100000000) {
1033 ret = clk_set_rate(host->clk, 100000000);
1034 if (ret < 0)
1035 goto clk_disable;
1036 host->mclk = clk_get_rate(host->clk);
Linus Walleij64de0282010-02-19 01:09:10 +01001037 dev_dbg(mmc_dev(mmc), "eventual mclk rate: %u Hz\n",
1038 host->mclk);
Linus Walleijc8df9a52008-04-29 09:34:07 +01001039 }
Russell Kingc8ebae32011-01-11 19:35:53 +00001040 host->phybase = dev->res.start;
Linus Walleijdc890c22009-06-07 23:27:31 +01001041 host->base = ioremap(dev->res.start, resource_size(&dev->res));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if (!host->base) {
1043 ret = -ENOMEM;
1044 goto clk_disable;
1045 }
1046
1047 mmc->ops = &mmci_ops;
1048 mmc->f_min = (host->mclk + 511) / 512;
Linus Walleij808d97c2010-04-08 07:39:38 +01001049 /*
1050 * If the platform data supplies a maximum operating
1051 * frequency, this takes precedence. Else, we fall back
1052 * to using the module parameter, which has a (low)
1053 * default value in case it is not specified. Either
1054 * value must not exceed the clock rate into the block,
1055 * of course.
1056 */
1057 if (plat->f_max)
1058 mmc->f_max = min(host->mclk, plat->f_max);
1059 else
1060 mmc->f_max = min(host->mclk, fmax);
Linus Walleij64de0282010-02-19 01:09:10 +01001061 dev_dbg(mmc_dev(mmc), "clocking block at %u Hz\n", mmc->f_max);
1062
Linus Walleij34e84f32009-09-22 14:41:40 +01001063#ifdef CONFIG_REGULATOR
1064 /* If we're using the regulator framework, try to fetch a regulator */
1065 host->vcc = regulator_get(&dev->dev, "vmmc");
1066 if (IS_ERR(host->vcc))
1067 host->vcc = NULL;
1068 else {
1069 int mask = mmc_regulator_get_ocrmask(host->vcc);
1070
1071 if (mask < 0)
1072 dev_err(&dev->dev, "error getting OCR mask (%d)\n",
1073 mask);
1074 else {
1075 host->mmc->ocr_avail = (u32) mask;
1076 if (plat->ocr_mask)
1077 dev_warn(&dev->dev,
1078 "Provided ocr_mask/setpower will not be used "
1079 "(using regulator instead)\n");
1080 }
1081 }
1082#endif
1083 /* Fall back to platform data if no regulator is found */
1084 if (host->vcc == NULL)
1085 mmc->ocr_avail = plat->ocr_mask;
Linus Walleij9e6c82c2009-09-14 12:57:11 +01001086 mmc->caps = plat->capabilities;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
1088 /*
1089 * We can do SGIO
1090 */
Martin K. Petersena36274e2010-09-10 01:33:59 -04001091 mmc->max_segs = NR_SG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
1093 /*
Rabin Vincent08458ef2010-07-21 12:55:59 +01001094 * Since only a certain number of bits are valid in the data length
1095 * register, we must ensure that we don't exceed 2^num-1 bytes in a
1096 * single request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 */
Rabin Vincent08458ef2010-07-21 12:55:59 +01001098 mmc->max_req_size = (1 << variant->datalength_bits) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 /*
1101 * Set the maximum segment size. Since we aren't doing DMA
1102 * (yet) we are only limited by the data length register.
1103 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001104 mmc->max_seg_size = mmc->max_req_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001106 /*
1107 * Block size can be up to 2048 bytes, but must be a power of two.
1108 */
1109 mmc->max_blk_size = 2048;
1110
Pierre Ossman55db8902006-11-21 17:55:45 +01001111 /*
1112 * No limit on the number of blocks transferred.
1113 */
1114 mmc->max_blk_count = mmc->max_req_size;
1115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 spin_lock_init(&host->lock);
1117
1118 writel(0, host->base + MMCIMASK0);
1119 writel(0, host->base + MMCIMASK1);
1120 writel(0xfff, host->base + MMCICLEAR);
1121
Russell King89001442009-07-09 15:16:07 +01001122 if (gpio_is_valid(plat->gpio_cd)) {
1123 ret = gpio_request(plat->gpio_cd, DRIVER_NAME " (cd)");
1124 if (ret == 0)
1125 ret = gpio_direction_input(plat->gpio_cd);
1126 if (ret == 0)
1127 host->gpio_cd = plat->gpio_cd;
1128 else if (ret != -ENOSYS)
1129 goto err_gpio_cd;
Rabin Vincent148b8b32010-08-09 12:55:48 +01001130
1131 ret = request_any_context_irq(gpio_to_irq(plat->gpio_cd),
1132 mmci_cd_irq, 0,
1133 DRIVER_NAME " (cd)", host);
1134 if (ret >= 0)
1135 host->gpio_cd_irq = gpio_to_irq(plat->gpio_cd);
Russell King89001442009-07-09 15:16:07 +01001136 }
1137 if (gpio_is_valid(plat->gpio_wp)) {
1138 ret = gpio_request(plat->gpio_wp, DRIVER_NAME " (wp)");
1139 if (ret == 0)
1140 ret = gpio_direction_input(plat->gpio_wp);
1141 if (ret == 0)
1142 host->gpio_wp = plat->gpio_wp;
1143 else if (ret != -ENOSYS)
1144 goto err_gpio_wp;
1145 }
1146
Rabin Vincent4b8caec2010-08-09 12:56:40 +01001147 if ((host->plat->status || host->gpio_cd != -ENOSYS)
1148 && host->gpio_cd_irq < 0)
Rabin Vincent148b8b32010-08-09 12:55:48 +01001149 mmc->caps |= MMC_CAP_NEEDS_POLL;
1150
Thomas Gleixnerdace1452006-07-01 19:29:38 -07001151 ret = request_irq(dev->irq[0], mmci_irq, IRQF_SHARED, DRIVER_NAME " (cmd)", host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 if (ret)
1153 goto unmap;
1154
Linus Walleij2686b4b2010-10-19 12:39:48 +01001155 if (dev->irq[1] == NO_IRQ)
1156 host->singleirq = true;
1157 else {
1158 ret = request_irq(dev->irq[1], mmci_pio_irq, IRQF_SHARED,
1159 DRIVER_NAME " (pio)", host);
1160 if (ret)
1161 goto irq0_free;
1162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Linus Walleij8cb28152011-01-24 15:22:13 +01001164 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
1166 amba_set_drvdata(dev, mmc);
1167
Russell Kingc8ebae32011-01-11 19:35:53 +00001168 dev_info(&dev->dev, "%s: PL%03x manf %x rev%u at 0x%08llx irq %d,%d (pio)\n",
1169 mmc_hostname(mmc), amba_part(dev), amba_manf(dev),
1170 amba_rev(dev), (unsigned long long)dev->res.start,
1171 dev->irq[0], dev->irq[1]);
1172
1173 mmci_dma_setup(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Russell King8c11a942010-12-28 19:40:40 +00001175 mmc_add_host(mmc);
1176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 return 0;
1178
1179 irq0_free:
1180 free_irq(dev->irq[0], host);
1181 unmap:
Russell King89001442009-07-09 15:16:07 +01001182 if (host->gpio_wp != -ENOSYS)
1183 gpio_free(host->gpio_wp);
1184 err_gpio_wp:
Rabin Vincent148b8b32010-08-09 12:55:48 +01001185 if (host->gpio_cd_irq >= 0)
1186 free_irq(host->gpio_cd_irq, host);
Russell King89001442009-07-09 15:16:07 +01001187 if (host->gpio_cd != -ENOSYS)
1188 gpio_free(host->gpio_cd);
1189 err_gpio_cd:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 iounmap(host->base);
1191 clk_disable:
1192 clk_disable(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 clk_free:
1194 clk_put(host->clk);
1195 host_free:
1196 mmc_free_host(mmc);
1197 rel_regions:
1198 amba_release_regions(dev);
1199 out:
1200 return ret;
1201}
1202
Linus Walleij6dc4a472009-03-07 00:23:52 +01001203static int __devexit mmci_remove(struct amba_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204{
1205 struct mmc_host *mmc = amba_get_drvdata(dev);
1206
1207 amba_set_drvdata(dev, NULL);
1208
1209 if (mmc) {
1210 struct mmci_host *host = mmc_priv(mmc);
1211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 mmc_remove_host(mmc);
1213
1214 writel(0, host->base + MMCIMASK0);
1215 writel(0, host->base + MMCIMASK1);
1216
1217 writel(0, host->base + MMCICOMMAND);
1218 writel(0, host->base + MMCIDATACTRL);
1219
Russell Kingc8ebae32011-01-11 19:35:53 +00001220 mmci_dma_release(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 free_irq(dev->irq[0], host);
Linus Walleij2686b4b2010-10-19 12:39:48 +01001222 if (!host->singleirq)
1223 free_irq(dev->irq[1], host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Russell King89001442009-07-09 15:16:07 +01001225 if (host->gpio_wp != -ENOSYS)
1226 gpio_free(host->gpio_wp);
Rabin Vincent148b8b32010-08-09 12:55:48 +01001227 if (host->gpio_cd_irq >= 0)
1228 free_irq(host->gpio_cd_irq, host);
Russell King89001442009-07-09 15:16:07 +01001229 if (host->gpio_cd != -ENOSYS)
1230 gpio_free(host->gpio_cd);
1231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 iounmap(host->base);
1233 clk_disable(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 clk_put(host->clk);
1235
Linus Walleij99fc5132010-09-29 01:08:27 -04001236 if (host->vcc)
1237 mmc_regulator_set_ocr(mmc, host->vcc, 0);
Linus Walleij34e84f32009-09-22 14:41:40 +01001238 regulator_put(host->vcc);
1239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 mmc_free_host(mmc);
1241
1242 amba_release_regions(dev);
1243 }
1244
1245 return 0;
1246}
1247
1248#ifdef CONFIG_PM
Pavel Macheke5378ca2005-04-16 15:25:29 -07001249static int mmci_suspend(struct amba_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
1251 struct mmc_host *mmc = amba_get_drvdata(dev);
1252 int ret = 0;
1253
1254 if (mmc) {
1255 struct mmci_host *host = mmc_priv(mmc);
1256
Matt Fleming1a13f8f2010-05-26 14:42:08 -07001257 ret = mmc_suspend_host(mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 if (ret == 0)
1259 writel(0, host->base + MMCIMASK0);
1260 }
1261
1262 return ret;
1263}
1264
1265static int mmci_resume(struct amba_device *dev)
1266{
1267 struct mmc_host *mmc = amba_get_drvdata(dev);
1268 int ret = 0;
1269
1270 if (mmc) {
1271 struct mmci_host *host = mmc_priv(mmc);
1272
1273 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
1274
1275 ret = mmc_resume_host(mmc);
1276 }
1277
1278 return ret;
1279}
1280#else
1281#define mmci_suspend NULL
1282#define mmci_resume NULL
1283#endif
1284
1285static struct amba_id mmci_ids[] = {
1286 {
1287 .id = 0x00041180,
Pawel Moll768fbc12011-03-11 17:18:07 +00001288 .mask = 0xff0fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001289 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 },
1291 {
Pawel Moll768fbc12011-03-11 17:18:07 +00001292 .id = 0x01041180,
1293 .mask = 0xff0fffff,
1294 .data = &variant_arm_extended_fifo,
1295 },
1296 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 .id = 0x00041181,
1298 .mask = 0x000fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001299 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 },
Linus Walleijcc30d602009-01-04 15:18:54 +01001301 /* ST Micro variants */
1302 {
1303 .id = 0x00180180,
1304 .mask = 0x00ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001305 .data = &variant_u300,
Linus Walleijcc30d602009-01-04 15:18:54 +01001306 },
1307 {
1308 .id = 0x00280180,
1309 .mask = 0x00ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001310 .data = &variant_u300,
1311 },
1312 {
1313 .id = 0x00480180,
1314 .mask = 0x00ffffff,
1315 .data = &variant_ux500,
Linus Walleijcc30d602009-01-04 15:18:54 +01001316 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 { 0, 0 },
1318};
1319
1320static struct amba_driver mmci_driver = {
1321 .drv = {
1322 .name = DRIVER_NAME,
1323 },
1324 .probe = mmci_probe,
Linus Walleij6dc4a472009-03-07 00:23:52 +01001325 .remove = __devexit_p(mmci_remove),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 .suspend = mmci_suspend,
1327 .resume = mmci_resume,
1328 .id_table = mmci_ids,
1329};
1330
1331static int __init mmci_init(void)
1332{
1333 return amba_driver_register(&mmci_driver);
1334}
1335
1336static void __exit mmci_exit(void)
1337{
1338 amba_driver_unregister(&mmci_driver);
1339}
1340
1341module_init(mmci_init);
1342module_exit(mmci_exit);
1343module_param(fmax, uint, 0444);
1344
1345MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
1346MODULE_LICENSE("GPL");