blob: 0e04138b17590ca5feb23cea1c3e9797dbf375ad [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>
Russell King1c3be362011-08-14 09:17:05 +010032#include <linux/pm_runtime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Russell King7b09cda2005-07-01 12:02:59 +010034#include <asm/div64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/io.h>
Russell Kingc6b8fda2005-10-28 14:05:16 +010036#include <asm/sizes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#include "mmci.h"
39
40#define DRIVER_NAME "mmci-pl18x"
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static unsigned int fmax = 515633;
43
Rabin Vincent4956e102010-07-21 12:54:40 +010044/**
45 * struct variant_data - MMCI variant-specific quirks
46 * @clkreg: default value for MCICLOCK register
Rabin Vincent4380c142010-07-21 12:55:18 +010047 * @clkreg_enable: enable value for MMCICLOCK register
Rabin Vincent08458ef2010-07-21 12:55:59 +010048 * @datalength_bits: number of bits in the MMCIDATALENGTH register
Rabin Vincent8301bb62010-08-09 12:57:30 +010049 * @fifosize: number of bytes that can be written when MMCI_TXFIFOEMPTY
50 * is asserted (likewise for RX)
51 * @fifohalfsize: number of bytes that can be written when MCI_TXFIFOHALFEMPTY
52 * is asserted (likewise for RX)
Linus Walleij34177802010-10-19 12:43:58 +010053 * @sdio: variant supports SDIO
Linus Walleijb70a67f2010-12-06 09:24:14 +010054 * @st_clkdiv: true if using a ST-specific clock divider algorithm
Philippe Langlais1784b152011-03-25 08:51:52 +010055 * @blksz_datactrl16: true if Block size is at b16..b30 position in datactrl register
Rabin Vincent4956e102010-07-21 12:54:40 +010056 */
57struct variant_data {
58 unsigned int clkreg;
Rabin Vincent4380c142010-07-21 12:55:18 +010059 unsigned int clkreg_enable;
Rabin Vincent08458ef2010-07-21 12:55:59 +010060 unsigned int datalength_bits;
Rabin Vincent8301bb62010-08-09 12:57:30 +010061 unsigned int fifosize;
62 unsigned int fifohalfsize;
Linus Walleij34177802010-10-19 12:43:58 +010063 bool sdio;
Linus Walleijb70a67f2010-12-06 09:24:14 +010064 bool st_clkdiv;
Philippe Langlais1784b152011-03-25 08:51:52 +010065 bool blksz_datactrl16;
Rabin Vincent4956e102010-07-21 12:54:40 +010066};
67
68static struct variant_data variant_arm = {
Rabin Vincent8301bb62010-08-09 12:57:30 +010069 .fifosize = 16 * 4,
70 .fifohalfsize = 8 * 4,
Rabin Vincent08458ef2010-07-21 12:55:59 +010071 .datalength_bits = 16,
Rabin Vincent4956e102010-07-21 12:54:40 +010072};
73
Pawel Moll768fbc12011-03-11 17:18:07 +000074static struct variant_data variant_arm_extended_fifo = {
75 .fifosize = 128 * 4,
76 .fifohalfsize = 64 * 4,
77 .datalength_bits = 16,
78};
79
Rabin Vincent4956e102010-07-21 12:54:40 +010080static struct variant_data variant_u300 = {
Rabin Vincent8301bb62010-08-09 12:57:30 +010081 .fifosize = 16 * 4,
82 .fifohalfsize = 8 * 4,
Linus Walleij49ac2152011-03-04 14:54:16 +010083 .clkreg_enable = MCI_ST_U300_HWFCEN,
Rabin Vincent08458ef2010-07-21 12:55:59 +010084 .datalength_bits = 16,
Linus Walleij34177802010-10-19 12:43:58 +010085 .sdio = true,
Rabin Vincent4956e102010-07-21 12:54:40 +010086};
87
88static struct variant_data variant_ux500 = {
Rabin Vincent8301bb62010-08-09 12:57:30 +010089 .fifosize = 30 * 4,
90 .fifohalfsize = 8 * 4,
Rabin Vincent4956e102010-07-21 12:54:40 +010091 .clkreg = MCI_CLK_ENABLE,
Linus Walleij49ac2152011-03-04 14:54:16 +010092 .clkreg_enable = MCI_ST_UX500_HWFCEN,
Rabin Vincent08458ef2010-07-21 12:55:59 +010093 .datalength_bits = 24,
Linus Walleij34177802010-10-19 12:43:58 +010094 .sdio = true,
Linus Walleijb70a67f2010-12-06 09:24:14 +010095 .st_clkdiv = true,
Rabin Vincent4956e102010-07-21 12:54:40 +010096};
Linus Walleijb70a67f2010-12-06 09:24:14 +010097
Philippe Langlais1784b152011-03-25 08:51:52 +010098static struct variant_data variant_ux500v2 = {
99 .fifosize = 30 * 4,
100 .fifohalfsize = 8 * 4,
101 .clkreg = MCI_CLK_ENABLE,
102 .clkreg_enable = MCI_ST_UX500_HWFCEN,
103 .datalength_bits = 24,
104 .sdio = true,
105 .st_clkdiv = true,
106 .blksz_datactrl16 = true,
107};
108
Linus Walleija6a64642009-09-14 12:56:14 +0100109/*
110 * This must be called with host->lock held
111 */
112static void mmci_set_clkreg(struct mmci_host *host, unsigned int desired)
113{
Rabin Vincent4956e102010-07-21 12:54:40 +0100114 struct variant_data *variant = host->variant;
115 u32 clk = variant->clkreg;
Linus Walleija6a64642009-09-14 12:56:14 +0100116
117 if (desired) {
118 if (desired >= host->mclk) {
Linus Walleij991a86e2010-12-10 09:35:53 +0100119 clk = MCI_CLK_BYPASS;
Linus Walleij399bc482011-04-01 07:59:17 +0100120 if (variant->st_clkdiv)
121 clk |= MCI_ST_UX500_NEG_EDGE;
Linus Walleija6a64642009-09-14 12:56:14 +0100122 host->cclk = host->mclk;
Linus Walleijb70a67f2010-12-06 09:24:14 +0100123 } else if (variant->st_clkdiv) {
124 /*
125 * DB8500 TRM says f = mclk / (clkdiv + 2)
126 * => clkdiv = (mclk / f) - 2
127 * Round the divider up so we don't exceed the max
128 * frequency
129 */
130 clk = DIV_ROUND_UP(host->mclk, desired) - 2;
131 if (clk >= 256)
132 clk = 255;
133 host->cclk = host->mclk / (clk + 2);
Linus Walleija6a64642009-09-14 12:56:14 +0100134 } else {
Linus Walleijb70a67f2010-12-06 09:24:14 +0100135 /*
136 * PL180 TRM says f = mclk / (2 * (clkdiv + 1))
137 * => clkdiv = mclk / (2 * f) - 1
138 */
Linus Walleija6a64642009-09-14 12:56:14 +0100139 clk = host->mclk / (2 * desired) - 1;
140 if (clk >= 256)
141 clk = 255;
142 host->cclk = host->mclk / (2 * (clk + 1));
143 }
Rabin Vincent4380c142010-07-21 12:55:18 +0100144
145 clk |= variant->clkreg_enable;
Linus Walleija6a64642009-09-14 12:56:14 +0100146 clk |= MCI_CLK_ENABLE;
147 /* This hasn't proven to be worthwhile */
148 /* clk |= MCI_CLK_PWRSAVE; */
149 }
150
Linus Walleij9e6c82c2009-09-14 12:57:11 +0100151 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4)
Linus Walleij771dc152010-04-08 07:38:52 +0100152 clk |= MCI_4BIT_BUS;
153 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_8)
154 clk |= MCI_ST_8BIT_BUS;
Linus Walleij9e6c82c2009-09-14 12:57:11 +0100155
Linus Walleija6a64642009-09-14 12:56:14 +0100156 writel(clk, host->base + MMCICLOCK);
157}
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159static void
160mmci_request_end(struct mmci_host *host, struct mmc_request *mrq)
161{
162 writel(0, host->base + MMCICOMMAND);
163
Russell Kinge47c2222007-01-08 16:42:51 +0000164 BUG_ON(host->data);
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 host->mrq = NULL;
167 host->cmd = NULL;
168
Russell King1c3be362011-08-14 09:17:05 +0100169 pm_runtime_put(mmc_dev(host->mmc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 mmc_request_done(host->mmc, mrq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
Linus Walleij2686b4b2010-10-19 12:39:48 +0100173static void mmci_set_mask1(struct mmci_host *host, unsigned int mask)
174{
175 void __iomem *base = host->base;
176
177 if (host->singleirq) {
178 unsigned int mask0 = readl(base + MMCIMASK0);
179
180 mask0 &= ~MCI_IRQ1MASK;
181 mask0 |= mask;
182
183 writel(mask0, base + MMCIMASK0);
184 }
185
186 writel(mask, base + MMCIMASK1);
187}
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189static void mmci_stop_data(struct mmci_host *host)
190{
191 writel(0, host->base + MMCIDATACTRL);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100192 mmci_set_mask1(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 host->data = NULL;
194}
195
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100196static void mmci_init_sg(struct mmci_host *host, struct mmc_data *data)
197{
198 unsigned int flags = SG_MITER_ATOMIC;
199
200 if (data->flags & MMC_DATA_READ)
201 flags |= SG_MITER_TO_SG;
202 else
203 flags |= SG_MITER_FROM_SG;
204
205 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
206}
207
Russell Kingc8ebae32011-01-11 19:35:53 +0000208/*
209 * All the DMA operation mode stuff goes inside this ifdef.
210 * This assumes that you have a generic DMA device interface,
211 * no custom DMA interfaces are supported.
212 */
213#ifdef CONFIG_DMA_ENGINE
214static void __devinit mmci_dma_setup(struct mmci_host *host)
215{
216 struct mmci_platform_data *plat = host->plat;
217 const char *rxname, *txname;
218 dma_cap_mask_t mask;
219
220 if (!plat || !plat->dma_filter) {
221 dev_info(mmc_dev(host->mmc), "no DMA platform data\n");
222 return;
223 }
224
Per Forlin58c7ccb2011-07-01 18:55:24 +0200225 /* initialize pre request cookie */
226 host->next_data.cookie = 1;
227
Russell Kingc8ebae32011-01-11 19:35:53 +0000228 /* Try to acquire a generic DMA engine slave channel */
229 dma_cap_zero(mask);
230 dma_cap_set(DMA_SLAVE, mask);
231
232 /*
233 * If only an RX channel is specified, the driver will
234 * attempt to use it bidirectionally, however if it is
235 * is specified but cannot be located, DMA will be disabled.
236 */
237 if (plat->dma_rx_param) {
238 host->dma_rx_channel = dma_request_channel(mask,
239 plat->dma_filter,
240 plat->dma_rx_param);
241 /* E.g if no DMA hardware is present */
242 if (!host->dma_rx_channel)
243 dev_err(mmc_dev(host->mmc), "no RX DMA channel\n");
244 }
245
246 if (plat->dma_tx_param) {
247 host->dma_tx_channel = dma_request_channel(mask,
248 plat->dma_filter,
249 plat->dma_tx_param);
250 if (!host->dma_tx_channel)
251 dev_warn(mmc_dev(host->mmc), "no TX DMA channel\n");
252 } else {
253 host->dma_tx_channel = host->dma_rx_channel;
254 }
255
256 if (host->dma_rx_channel)
257 rxname = dma_chan_name(host->dma_rx_channel);
258 else
259 rxname = "none";
260
261 if (host->dma_tx_channel)
262 txname = dma_chan_name(host->dma_tx_channel);
263 else
264 txname = "none";
265
266 dev_info(mmc_dev(host->mmc), "DMA channels RX %s, TX %s\n",
267 rxname, txname);
268
269 /*
270 * Limit the maximum segment size in any SG entry according to
271 * the parameters of the DMA engine device.
272 */
273 if (host->dma_tx_channel) {
274 struct device *dev = host->dma_tx_channel->device->dev;
275 unsigned int max_seg_size = dma_get_max_seg_size(dev);
276
277 if (max_seg_size < host->mmc->max_seg_size)
278 host->mmc->max_seg_size = max_seg_size;
279 }
280 if (host->dma_rx_channel) {
281 struct device *dev = host->dma_rx_channel->device->dev;
282 unsigned int max_seg_size = dma_get_max_seg_size(dev);
283
284 if (max_seg_size < host->mmc->max_seg_size)
285 host->mmc->max_seg_size = max_seg_size;
286 }
287}
288
289/*
290 * This is used in __devinit or __devexit so inline it
291 * so it can be discarded.
292 */
293static inline void mmci_dma_release(struct mmci_host *host)
294{
295 struct mmci_platform_data *plat = host->plat;
296
297 if (host->dma_rx_channel)
298 dma_release_channel(host->dma_rx_channel);
299 if (host->dma_tx_channel && plat->dma_tx_param)
300 dma_release_channel(host->dma_tx_channel);
301 host->dma_rx_channel = host->dma_tx_channel = NULL;
302}
303
304static void mmci_dma_unmap(struct mmci_host *host, struct mmc_data *data)
305{
306 struct dma_chan *chan = host->dma_current;
307 enum dma_data_direction dir;
308 u32 status;
309 int i;
310
311 /* Wait up to 1ms for the DMA to complete */
312 for (i = 0; ; i++) {
313 status = readl(host->base + MMCISTATUS);
314 if (!(status & MCI_RXDATAAVLBLMASK) || i >= 100)
315 break;
316 udelay(10);
317 }
318
319 /*
320 * Check to see whether we still have some data left in the FIFO -
321 * this catches DMA controllers which are unable to monitor the
322 * DMALBREQ and DMALSREQ signals while allowing us to DMA to non-
323 * contiguous buffers. On TX, we'll get a FIFO underrun error.
324 */
325 if (status & MCI_RXDATAAVLBLMASK) {
326 dmaengine_terminate_all(chan);
327 if (!data->error)
328 data->error = -EIO;
329 }
330
331 if (data->flags & MMC_DATA_WRITE) {
332 dir = DMA_TO_DEVICE;
333 } else {
334 dir = DMA_FROM_DEVICE;
335 }
336
Per Forlin58c7ccb2011-07-01 18:55:24 +0200337 if (!data->host_cookie)
338 dma_unmap_sg(chan->device->dev, data->sg, data->sg_len, dir);
Russell Kingc8ebae32011-01-11 19:35:53 +0000339
340 /*
341 * Use of DMA with scatter-gather is impossible.
342 * Give up with DMA and switch back to PIO mode.
343 */
344 if (status & MCI_RXDATAAVLBLMASK) {
345 dev_err(mmc_dev(host->mmc), "buggy DMA detected. Taking evasive action.\n");
346 mmci_dma_release(host);
347 }
348}
349
350static void mmci_dma_data_error(struct mmci_host *host)
351{
352 dev_err(mmc_dev(host->mmc), "error during DMA transfer!\n");
353 dmaengine_terminate_all(host->dma_current);
354}
355
Per Forlin58c7ccb2011-07-01 18:55:24 +0200356static int mmci_dma_prep_data(struct mmci_host *host, struct mmc_data *data,
357 struct mmci_host_next *next)
Russell Kingc8ebae32011-01-11 19:35:53 +0000358{
359 struct variant_data *variant = host->variant;
360 struct dma_slave_config conf = {
361 .src_addr = host->phybase + MMCIFIFO,
362 .dst_addr = host->phybase + MMCIFIFO,
363 .src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
364 .dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
365 .src_maxburst = variant->fifohalfsize >> 2, /* # of words */
366 .dst_maxburst = variant->fifohalfsize >> 2, /* # of words */
367 };
Russell Kingc8ebae32011-01-11 19:35:53 +0000368 struct dma_chan *chan;
369 struct dma_device *device;
370 struct dma_async_tx_descriptor *desc;
Vinod Koul05f57992011-10-14 10:45:11 +0530371 enum dma_data_direction buffer_dirn;
Russell Kingc8ebae32011-01-11 19:35:53 +0000372 int nr_sg;
373
Per Forlin58c7ccb2011-07-01 18:55:24 +0200374 /* Check if next job is already prepared */
375 if (data->host_cookie && !next &&
376 host->dma_current && host->dma_desc_current)
377 return 0;
378
379 if (!next) {
380 host->dma_current = NULL;
381 host->dma_desc_current = NULL;
382 }
Russell Kingc8ebae32011-01-11 19:35:53 +0000383
384 if (data->flags & MMC_DATA_READ) {
Vinod Koul05f57992011-10-14 10:45:11 +0530385 conf.direction = DMA_DEV_TO_MEM;
386 buffer_dirn = DMA_FROM_DEVICE;
Russell Kingc8ebae32011-01-11 19:35:53 +0000387 chan = host->dma_rx_channel;
388 } else {
Vinod Koul05f57992011-10-14 10:45:11 +0530389 conf.direction = DMA_MEM_TO_DEV;
390 buffer_dirn = DMA_TO_DEVICE;
Russell Kingc8ebae32011-01-11 19:35:53 +0000391 chan = host->dma_tx_channel;
392 }
393
394 /* If there's no DMA channel, fall back to PIO */
395 if (!chan)
396 return -EINVAL;
397
398 /* If less than or equal to the fifo size, don't bother with DMA */
Per Forlin58c7ccb2011-07-01 18:55:24 +0200399 if (data->blksz * data->blocks <= variant->fifosize)
Russell Kingc8ebae32011-01-11 19:35:53 +0000400 return -EINVAL;
401
402 device = chan->device;
Vinod Koul05f57992011-10-14 10:45:11 +0530403 nr_sg = dma_map_sg(device->dev, data->sg, data->sg_len, buffer_dirn);
Russell Kingc8ebae32011-01-11 19:35:53 +0000404 if (nr_sg == 0)
405 return -EINVAL;
406
407 dmaengine_slave_config(chan, &conf);
408 desc = device->device_prep_slave_sg(chan, data->sg, nr_sg,
409 conf.direction, DMA_CTRL_ACK);
410 if (!desc)
411 goto unmap_exit;
412
Per Forlin58c7ccb2011-07-01 18:55:24 +0200413 if (next) {
414 next->dma_chan = chan;
415 next->dma_desc = desc;
416 } else {
417 host->dma_current = chan;
418 host->dma_desc_current = desc;
419 }
Russell Kingc8ebae32011-01-11 19:35:53 +0000420
Per Forlin58c7ccb2011-07-01 18:55:24 +0200421 return 0;
422
423 unmap_exit:
424 if (!next)
425 dmaengine_terminate_all(chan);
Vinod Koul05f57992011-10-14 10:45:11 +0530426 dma_unmap_sg(device->dev, data->sg, data->sg_len, buffer_dirn);
Per Forlin58c7ccb2011-07-01 18:55:24 +0200427 return -ENOMEM;
428}
429
430static int mmci_dma_start_data(struct mmci_host *host, unsigned int datactrl)
431{
432 int ret;
433 struct mmc_data *data = host->data;
434
435 ret = mmci_dma_prep_data(host, host->data, NULL);
436 if (ret)
437 return ret;
438
439 /* Okay, go for it. */
Russell Kingc8ebae32011-01-11 19:35:53 +0000440 dev_vdbg(mmc_dev(host->mmc),
441 "Submit MMCI DMA job, sglen %d blksz %04x blks %04x flags %08x\n",
442 data->sg_len, data->blksz, data->blocks, data->flags);
Per Forlin58c7ccb2011-07-01 18:55:24 +0200443 dmaengine_submit(host->dma_desc_current);
444 dma_async_issue_pending(host->dma_current);
Russell Kingc8ebae32011-01-11 19:35:53 +0000445
446 datactrl |= MCI_DPSM_DMAENABLE;
447
448 /* Trigger the DMA transfer */
449 writel(datactrl, host->base + MMCIDATACTRL);
450
451 /*
452 * Let the MMCI say when the data is ended and it's time
453 * to fire next DMA request. When that happens, MMCI will
454 * call mmci_data_end()
455 */
456 writel(readl(host->base + MMCIMASK0) | MCI_DATAENDMASK,
457 host->base + MMCIMASK0);
458 return 0;
Russell Kingc8ebae32011-01-11 19:35:53 +0000459}
Per Forlin58c7ccb2011-07-01 18:55:24 +0200460
461static void mmci_get_next_data(struct mmci_host *host, struct mmc_data *data)
462{
463 struct mmci_host_next *next = &host->next_data;
464
465 if (data->host_cookie && data->host_cookie != next->cookie) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530466 pr_warning("[%s] invalid cookie: data->host_cookie %d"
Per Forlin58c7ccb2011-07-01 18:55:24 +0200467 " host->next_data.cookie %d\n",
468 __func__, data->host_cookie, host->next_data.cookie);
469 data->host_cookie = 0;
470 }
471
472 if (!data->host_cookie)
473 return;
474
475 host->dma_desc_current = next->dma_desc;
476 host->dma_current = next->dma_chan;
477
478 next->dma_desc = NULL;
479 next->dma_chan = NULL;
480}
481
482static void mmci_pre_request(struct mmc_host *mmc, struct mmc_request *mrq,
483 bool is_first_req)
484{
485 struct mmci_host *host = mmc_priv(mmc);
486 struct mmc_data *data = mrq->data;
487 struct mmci_host_next *nd = &host->next_data;
488
489 if (!data)
490 return;
491
492 if (data->host_cookie) {
493 data->host_cookie = 0;
494 return;
495 }
496
497 /* if config for dma */
498 if (((data->flags & MMC_DATA_WRITE) && host->dma_tx_channel) ||
499 ((data->flags & MMC_DATA_READ) && host->dma_rx_channel)) {
500 if (mmci_dma_prep_data(host, data, nd))
501 data->host_cookie = 0;
502 else
503 data->host_cookie = ++nd->cookie < 0 ? 1 : nd->cookie;
504 }
505}
506
507static void mmci_post_request(struct mmc_host *mmc, struct mmc_request *mrq,
508 int err)
509{
510 struct mmci_host *host = mmc_priv(mmc);
511 struct mmc_data *data = mrq->data;
512 struct dma_chan *chan;
513 enum dma_data_direction dir;
514
515 if (!data)
516 return;
517
518 if (data->flags & MMC_DATA_READ) {
519 dir = DMA_FROM_DEVICE;
520 chan = host->dma_rx_channel;
521 } else {
522 dir = DMA_TO_DEVICE;
523 chan = host->dma_tx_channel;
524 }
525
526
527 /* if config for dma */
528 if (chan) {
529 if (err)
530 dmaengine_terminate_all(chan);
Per Forlin8e3336b2011-08-29 15:35:59 +0200531 if (data->host_cookie)
Per Forlin58c7ccb2011-07-01 18:55:24 +0200532 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
533 data->sg_len, dir);
534 mrq->data->host_cookie = 0;
535 }
536}
537
Russell Kingc8ebae32011-01-11 19:35:53 +0000538#else
539/* Blank functions if the DMA engine is not available */
Per Forlin58c7ccb2011-07-01 18:55:24 +0200540static void mmci_get_next_data(struct mmci_host *host, struct mmc_data *data)
541{
542}
Russell Kingc8ebae32011-01-11 19:35:53 +0000543static inline void mmci_dma_setup(struct mmci_host *host)
544{
545}
546
547static inline void mmci_dma_release(struct mmci_host *host)
548{
549}
550
551static inline void mmci_dma_unmap(struct mmci_host *host, struct mmc_data *data)
552{
553}
554
555static inline void mmci_dma_data_error(struct mmci_host *host)
556{
557}
558
559static inline int mmci_dma_start_data(struct mmci_host *host, unsigned int datactrl)
560{
561 return -ENOSYS;
562}
Per Forlin58c7ccb2011-07-01 18:55:24 +0200563
564#define mmci_pre_request NULL
565#define mmci_post_request NULL
566
Russell Kingc8ebae32011-01-11 19:35:53 +0000567#endif
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569static void mmci_start_data(struct mmci_host *host, struct mmc_data *data)
570{
Rabin Vincent8301bb62010-08-09 12:57:30 +0100571 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 unsigned int datactrl, timeout, irqmask;
Russell King7b09cda2005-07-01 12:02:59 +0100573 unsigned long long clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 void __iomem *base;
Russell King3bc87f22006-08-27 13:51:28 +0100575 int blksz_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Linus Walleij64de0282010-02-19 01:09:10 +0100577 dev_dbg(mmc_dev(host->mmc), "blksz %04x blks %04x flags %08x\n",
578 data->blksz, data->blocks, data->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 host->data = data;
Rabin Vincent528320d2010-07-21 12:49:49 +0100581 host->size = data->blksz * data->blocks;
Russell King51d43752011-01-27 10:56:52 +0000582 data->bytes_xfered = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Russell King7b09cda2005-07-01 12:02:59 +0100584 clks = (unsigned long long)data->timeout_ns * host->cclk;
585 do_div(clks, 1000000000UL);
586
587 timeout = data->timeout_clks + (unsigned int)clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 base = host->base;
590 writel(timeout, base + MMCIDATATIMER);
591 writel(host->size, base + MMCIDATALENGTH);
592
Russell King3bc87f22006-08-27 13:51:28 +0100593 blksz_bits = ffs(data->blksz) - 1;
594 BUG_ON(1 << blksz_bits != data->blksz);
595
Philippe Langlais1784b152011-03-25 08:51:52 +0100596 if (variant->blksz_datactrl16)
597 datactrl = MCI_DPSM_ENABLE | (data->blksz << 16);
598 else
599 datactrl = MCI_DPSM_ENABLE | blksz_bits << 4;
Russell Kingc8ebae32011-01-11 19:35:53 +0000600
601 if (data->flags & MMC_DATA_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 datactrl |= MCI_DPSM_DIRECTION;
Russell Kingc8ebae32011-01-11 19:35:53 +0000603
604 /*
605 * Attempt to use DMA operation mode, if this
606 * should fail, fall back to PIO mode
607 */
608 if (!mmci_dma_start_data(host, datactrl))
609 return;
610
611 /* IRQ mode, map the SG list for CPU reading/writing */
612 mmci_init_sg(host, data);
613
614 if (data->flags & MMC_DATA_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 irqmask = MCI_RXFIFOHALFFULLMASK;
Russell King0425a142006-02-16 16:48:31 +0000616
617 /*
Russell Kingc4d877c2011-01-27 09:50:13 +0000618 * If we have less than the fifo 'half-full' threshold to
619 * transfer, trigger a PIO interrupt as soon as any data
620 * is available.
Russell King0425a142006-02-16 16:48:31 +0000621 */
Russell Kingc4d877c2011-01-27 09:50:13 +0000622 if (host->size < variant->fifohalfsize)
Russell King0425a142006-02-16 16:48:31 +0000623 irqmask |= MCI_RXDATAAVLBLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 } else {
625 /*
626 * We don't actually need to include "FIFO empty" here
627 * since its implicit in "FIFO half empty".
628 */
629 irqmask = MCI_TXFIFOHALFEMPTYMASK;
630 }
631
Linus Walleij34177802010-10-19 12:43:58 +0100632 /* The ST Micro variants has a special bit to enable SDIO */
633 if (variant->sdio && host->mmc->card)
634 if (mmc_card_sdio(host->mmc->card))
635 datactrl |= MCI_ST_DPSM_SDIOEN;
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 writel(datactrl, base + MMCIDATACTRL);
638 writel(readl(base + MMCIMASK0) & ~MCI_DATAENDMASK, base + MMCIMASK0);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100639 mmci_set_mask1(host, irqmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
641
642static void
643mmci_start_command(struct mmci_host *host, struct mmc_command *cmd, u32 c)
644{
645 void __iomem *base = host->base;
646
Linus Walleij64de0282010-02-19 01:09:10 +0100647 dev_dbg(mmc_dev(host->mmc), "op %02x arg %08x flags %08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 cmd->opcode, cmd->arg, cmd->flags);
649
650 if (readl(base + MMCICOMMAND) & MCI_CPSM_ENABLE) {
651 writel(0, base + MMCICOMMAND);
652 udelay(1);
653 }
654
655 c |= cmd->opcode | MCI_CPSM_ENABLE;
Russell Kinge9225172006-02-02 12:23:12 +0000656 if (cmd->flags & MMC_RSP_PRESENT) {
657 if (cmd->flags & MMC_RSP_136)
658 c |= MCI_CPSM_LONGRSP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 c |= MCI_CPSM_RESPONSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
661 if (/*interrupt*/0)
662 c |= MCI_CPSM_INTERRUPT;
663
664 host->cmd = cmd;
665
666 writel(cmd->arg, base + MMCIARGUMENT);
667 writel(c, base + MMCICOMMAND);
668}
669
670static void
671mmci_data_irq(struct mmci_host *host, struct mmc_data *data,
672 unsigned int status)
673{
Linus Walleijf20f8f22010-10-19 13:41:24 +0100674 /* First check for errors */
Ulf Hanssonb63038d2011-12-13 16:51:04 +0100675 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_STARTBITERR|
676 MCI_TXUNDERRUN|MCI_RXOVERRUN)) {
Linus Walleij8cb28152011-01-24 15:22:13 +0100677 u32 remain, success;
Linus Walleijf20f8f22010-10-19 13:41:24 +0100678
Russell Kingc8ebae32011-01-11 19:35:53 +0000679 /* Terminate the DMA transfer */
680 if (dma_inprogress(host))
681 mmci_dma_data_error(host);
682
Russell Kingc8afc9d2011-02-04 09:19:46 +0000683 /*
684 * Calculate how far we are into the transfer. Note that
685 * the data counter gives the number of bytes transferred
686 * on the MMC bus, not on the host side. On reads, this
687 * can be as much as a FIFO-worth of data ahead. This
688 * matters for FIFO overruns only.
689 */
Linus Walleijf5a106d2011-01-27 17:44:34 +0100690 remain = readl(host->base + MMCIDATACNT);
Linus Walleij8cb28152011-01-24 15:22:13 +0100691 success = data->blksz * data->blocks - remain;
692
Russell Kingc8afc9d2011-02-04 09:19:46 +0000693 dev_dbg(mmc_dev(host->mmc), "MCI ERROR IRQ, status 0x%08x at 0x%08x\n",
694 status, success);
Linus Walleij8cb28152011-01-24 15:22:13 +0100695 if (status & MCI_DATACRCFAIL) {
696 /* Last block was not successful */
Russell Kingc8afc9d2011-02-04 09:19:46 +0000697 success -= 1;
Pierre Ossman17b04292007-07-22 22:18:46 +0200698 data->error = -EILSEQ;
Linus Walleij8cb28152011-01-24 15:22:13 +0100699 } else if (status & MCI_DATATIMEOUT) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200700 data->error = -ETIMEDOUT;
Linus Walleij757df742011-06-30 15:10:21 +0100701 } else if (status & MCI_STARTBITERR) {
702 data->error = -ECOMM;
Russell Kingc8afc9d2011-02-04 09:19:46 +0000703 } else if (status & MCI_TXUNDERRUN) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200704 data->error = -EIO;
Russell Kingc8afc9d2011-02-04 09:19:46 +0000705 } else if (status & MCI_RXOVERRUN) {
706 if (success > host->variant->fifosize)
707 success -= host->variant->fifosize;
708 else
709 success = 0;
Linus Walleij8cb28152011-01-24 15:22:13 +0100710 data->error = -EIO;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100711 }
Russell King51d43752011-01-27 10:56:52 +0000712 data->bytes_xfered = round_down(success, data->blksz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 }
Linus Walleijf20f8f22010-10-19 13:41:24 +0100714
Linus Walleij8cb28152011-01-24 15:22:13 +0100715 if (status & MCI_DATABLOCKEND)
716 dev_err(mmc_dev(host->mmc), "stray MCI_DATABLOCKEND interrupt\n");
Linus Walleijf20f8f22010-10-19 13:41:24 +0100717
Russell Kingccff9b52011-01-30 21:03:50 +0000718 if (status & MCI_DATAEND || data->error) {
Russell Kingc8ebae32011-01-11 19:35:53 +0000719 if (dma_inprogress(host))
720 mmci_dma_unmap(host, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 mmci_stop_data(host);
722
Linus Walleij8cb28152011-01-24 15:22:13 +0100723 if (!data->error)
724 /* The error clause is handled above, success! */
Russell King51d43752011-01-27 10:56:52 +0000725 data->bytes_xfered = data->blksz * data->blocks;
Linus Walleijf20f8f22010-10-19 13:41:24 +0100726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (!data->stop) {
728 mmci_request_end(host, data->mrq);
729 } else {
730 mmci_start_command(host, data->stop, 0);
731 }
732 }
733}
734
735static void
736mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
737 unsigned int status)
738{
739 void __iomem *base = host->base;
740
741 host->cmd = NULL;
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 if (status & MCI_CMDTIMEOUT) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200744 cmd->error = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200746 cmd->error = -EILSEQ;
Russell King - ARM Linux9047b432011-01-11 16:35:56 +0000747 } else {
748 cmd->resp[0] = readl(base + MMCIRESPONSE0);
749 cmd->resp[1] = readl(base + MMCIRESPONSE1);
750 cmd->resp[2] = readl(base + MMCIRESPONSE2);
751 cmd->resp[3] = readl(base + MMCIRESPONSE3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
753
Pierre Ossman17b04292007-07-22 22:18:46 +0200754 if (!cmd->data || cmd->error) {
Ulf Hansson3b6e3c72011-12-13 16:58:43 +0100755 if (host->data) {
756 /* Terminate the DMA transfer */
757 if (dma_inprogress(host))
758 mmci_dma_data_error(host);
Russell Kinge47c2222007-01-08 16:42:51 +0000759 mmci_stop_data(host);
Ulf Hansson3b6e3c72011-12-13 16:58:43 +0100760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 mmci_request_end(host, cmd->mrq);
762 } else if (!(cmd->data->flags & MMC_DATA_READ)) {
763 mmci_start_data(host, cmd->data);
764 }
765}
766
767static int mmci_pio_read(struct mmci_host *host, char *buffer, unsigned int remain)
768{
769 void __iomem *base = host->base;
770 char *ptr = buffer;
771 u32 status;
Linus Walleij26eed9a2008-04-26 23:39:44 +0100772 int host_remain = host->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 do {
Linus Walleij26eed9a2008-04-26 23:39:44 +0100775 int count = host_remain - (readl(base + MMCIFIFOCNT) << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 if (count > remain)
778 count = remain;
779
780 if (count <= 0)
781 break;
782
783 readsl(base + MMCIFIFO, ptr, count >> 2);
784
785 ptr += count;
786 remain -= count;
Linus Walleij26eed9a2008-04-26 23:39:44 +0100787 host_remain -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 if (remain == 0)
790 break;
791
792 status = readl(base + MMCISTATUS);
793 } while (status & MCI_RXDATAAVLBL);
794
795 return ptr - buffer;
796}
797
798static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status)
799{
Rabin Vincent8301bb62010-08-09 12:57:30 +0100800 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 void __iomem *base = host->base;
802 char *ptr = buffer;
803
804 do {
805 unsigned int count, maxcnt;
806
Rabin Vincent8301bb62010-08-09 12:57:30 +0100807 maxcnt = status & MCI_TXFIFOEMPTY ?
808 variant->fifosize : variant->fifohalfsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 count = min(remain, maxcnt);
810
Linus Walleij34177802010-10-19 12:43:58 +0100811 /*
812 * The ST Micro variant for SDIO transfer sizes
813 * less then 8 bytes should have clock H/W flow
814 * control disabled.
815 */
816 if (variant->sdio &&
817 mmc_card_sdio(host->mmc->card)) {
818 if (count < 8)
819 writel(readl(host->base + MMCICLOCK) &
820 ~variant->clkreg_enable,
821 host->base + MMCICLOCK);
822 else
823 writel(readl(host->base + MMCICLOCK) |
824 variant->clkreg_enable,
825 host->base + MMCICLOCK);
826 }
827
828 /*
829 * SDIO especially may want to send something that is
830 * not divisible by 4 (as opposed to card sectors
831 * etc), and the FIFO only accept full 32-bit writes.
832 * So compensate by adding +3 on the count, a single
833 * byte become a 32bit write, 7 bytes will be two
834 * 32bit writes etc.
835 */
836 writesl(base + MMCIFIFO, ptr, (count + 3) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 ptr += count;
839 remain -= count;
840
841 if (remain == 0)
842 break;
843
844 status = readl(base + MMCISTATUS);
845 } while (status & MCI_TXFIFOHALFEMPTY);
846
847 return ptr - buffer;
848}
849
850/*
851 * PIO data transfer IRQ handler.
852 */
David Howells7d12e782006-10-05 14:55:46 +0100853static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
855 struct mmci_host *host = dev_id;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100856 struct sg_mapping_iter *sg_miter = &host->sg_miter;
Rabin Vincent8301bb62010-08-09 12:57:30 +0100857 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 void __iomem *base = host->base;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100859 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 u32 status;
861
862 status = readl(base + MMCISTATUS);
863
Linus Walleij64de0282010-02-19 01:09:10 +0100864 dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100866 local_irq_save(flags);
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 unsigned int remain, len;
870 char *buffer;
871
872 /*
873 * For write, we only need to test the half-empty flag
874 * here - if the FIFO is completely empty, then by
875 * definition it is more than half empty.
876 *
877 * For read, check for data available.
878 */
879 if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL)))
880 break;
881
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100882 if (!sg_miter_next(sg_miter))
883 break;
884
885 buffer = sg_miter->addr;
886 remain = sg_miter->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888 len = 0;
889 if (status & MCI_RXACTIVE)
890 len = mmci_pio_read(host, buffer, remain);
891 if (status & MCI_TXACTIVE)
892 len = mmci_pio_write(host, buffer, remain, status);
893
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100894 sg_miter->consumed = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 host->size -= len;
897 remain -= len;
898
899 if (remain)
900 break;
901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 status = readl(base + MMCISTATUS);
903 } while (1);
904
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100905 sg_miter_stop(sg_miter);
906
907 local_irq_restore(flags);
908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 /*
Russell Kingc4d877c2011-01-27 09:50:13 +0000910 * If we have less than the fifo 'half-full' threshold to transfer,
911 * trigger a PIO interrupt as soon as any data is available.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 */
Russell Kingc4d877c2011-01-27 09:50:13 +0000913 if (status & MCI_RXACTIVE && host->size < variant->fifohalfsize)
Linus Walleij2686b4b2010-10-19 12:39:48 +0100914 mmci_set_mask1(host, MCI_RXDATAAVLBLMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 /*
917 * If we run out of data, disable the data IRQs; this
918 * prevents a race where the FIFO becomes empty before
919 * the chip itself has disabled the data path, and
920 * stops us racing with our data end IRQ.
921 */
922 if (host->size == 0) {
Linus Walleij2686b4b2010-10-19 12:39:48 +0100923 mmci_set_mask1(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0);
925 }
926
927 return IRQ_HANDLED;
928}
929
930/*
931 * Handle completion of command and data transfers.
932 */
David Howells7d12e782006-10-05 14:55:46 +0100933static irqreturn_t mmci_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
935 struct mmci_host *host = dev_id;
936 u32 status;
937 int ret = 0;
938
939 spin_lock(&host->lock);
940
941 do {
942 struct mmc_command *cmd;
943 struct mmc_data *data;
944
945 status = readl(host->base + MMCISTATUS);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100946
947 if (host->singleirq) {
948 if (status & readl(host->base + MMCIMASK1))
949 mmci_pio_irq(irq, dev_id);
950
951 status &= ~MCI_IRQ1MASK;
952 }
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 status &= readl(host->base + MMCIMASK0);
955 writel(status, host->base + MMCICLEAR);
956
Linus Walleij64de0282010-02-19 01:09:10 +0100957 dev_dbg(mmc_dev(host->mmc), "irq0 (data+cmd) %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 data = host->data;
Ulf Hanssonb63038d2011-12-13 16:51:04 +0100960 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_STARTBITERR|
961 MCI_TXUNDERRUN|MCI_RXOVERRUN|MCI_DATAEND|
962 MCI_DATABLOCKEND) && data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 mmci_data_irq(host, data, status);
964
965 cmd = host->cmd;
966 if (status & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT|MCI_CMDSENT|MCI_CMDRESPEND) && cmd)
967 mmci_cmd_irq(host, cmd, status);
968
969 ret = 1;
970 } while (status);
971
972 spin_unlock(&host->lock);
973
974 return IRQ_RETVAL(ret);
975}
976
977static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
978{
979 struct mmci_host *host = mmc_priv(mmc);
Linus Walleij9e943022008-10-24 21:17:50 +0100980 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 WARN_ON(host->mrq != NULL);
983
Nicolas Pitre019a5f52007-10-11 01:06:03 -0400984 if (mrq->data && !is_power_of_2(mrq->data->blksz)) {
Linus Walleij64de0282010-02-19 01:09:10 +0100985 dev_err(mmc_dev(mmc), "unsupported block size (%d bytes)\n",
986 mrq->data->blksz);
Pierre Ossman255d01a2007-07-24 20:38:53 +0200987 mrq->cmd->error = -EINVAL;
988 mmc_request_done(mmc, mrq);
989 return;
990 }
991
Russell King1c3be362011-08-14 09:17:05 +0100992 pm_runtime_get_sync(mmc_dev(mmc));
993
Linus Walleij9e943022008-10-24 21:17:50 +0100994 spin_lock_irqsave(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 host->mrq = mrq;
997
Per Forlin58c7ccb2011-07-01 18:55:24 +0200998 if (mrq->data)
999 mmci_get_next_data(host, mrq->data);
1000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 if (mrq->data && mrq->data->flags & MMC_DATA_READ)
1002 mmci_start_data(host, mrq->data);
1003
1004 mmci_start_command(host, mrq->cmd, 0);
1005
Linus Walleij9e943022008-10-24 21:17:50 +01001006 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007}
1008
1009static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1010{
1011 struct mmci_host *host = mmc_priv(mmc);
Linus Walleija6a64642009-09-14 12:56:14 +01001012 u32 pwr = 0;
1013 unsigned long flags;
Linus Walleij99fc5132010-09-29 01:08:27 -04001014 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 switch (ios->power_mode) {
1017 case MMC_POWER_OFF:
Linus Walleij99fc5132010-09-29 01:08:27 -04001018 if (host->vcc)
1019 ret = mmc_regulator_set_ocr(mmc, host->vcc, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 break;
1021 case MMC_POWER_UP:
Linus Walleij99fc5132010-09-29 01:08:27 -04001022 if (host->vcc) {
1023 ret = mmc_regulator_set_ocr(mmc, host->vcc, ios->vdd);
1024 if (ret) {
1025 dev_err(mmc_dev(mmc), "unable to set OCR\n");
1026 /*
1027 * The .set_ios() function in the mmc_host_ops
1028 * struct return void, and failing to set the
1029 * power should be rare so we print an error
1030 * and return here.
1031 */
1032 return;
1033 }
1034 }
Rabin Vincentbb8f5632010-07-21 12:53:57 +01001035 if (host->plat->vdd_handler)
1036 pwr |= host->plat->vdd_handler(mmc_dev(mmc), ios->vdd,
1037 ios->power_mode);
Linus Walleijcc30d602009-01-04 15:18:54 +01001038 /* The ST version does not have this, fall through to POWER_ON */
Linus Walleijf17a1f02009-08-04 01:01:02 +01001039 if (host->hw_designer != AMBA_VENDOR_ST) {
Linus Walleijcc30d602009-01-04 15:18:54 +01001040 pwr |= MCI_PWR_UP;
1041 break;
1042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 case MMC_POWER_ON:
1044 pwr |= MCI_PWR_ON;
1045 break;
1046 }
1047
Linus Walleijcc30d602009-01-04 15:18:54 +01001048 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN) {
Linus Walleijf17a1f02009-08-04 01:01:02 +01001049 if (host->hw_designer != AMBA_VENDOR_ST)
Linus Walleijcc30d602009-01-04 15:18:54 +01001050 pwr |= MCI_ROD;
1051 else {
1052 /*
1053 * The ST Micro variant use the ROD bit for something
1054 * else and only has OD (Open Drain).
1055 */
1056 pwr |= MCI_OD;
1057 }
1058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Linus Walleija6a64642009-09-14 12:56:14 +01001060 spin_lock_irqsave(&host->lock, flags);
1061
1062 mmci_set_clkreg(host, ios->clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 if (host->pwr != pwr) {
1065 host->pwr = pwr;
1066 writel(pwr, host->base + MMCIPOWER);
1067 }
Linus Walleija6a64642009-09-14 12:56:14 +01001068
1069 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070}
1071
Russell King89001442009-07-09 15:16:07 +01001072static int mmci_get_ro(struct mmc_host *mmc)
1073{
1074 struct mmci_host *host = mmc_priv(mmc);
1075
1076 if (host->gpio_wp == -ENOSYS)
1077 return -ENOSYS;
1078
Linus Walleij18a06302010-09-12 12:56:44 +01001079 return gpio_get_value_cansleep(host->gpio_wp);
Russell King89001442009-07-09 15:16:07 +01001080}
1081
1082static int mmci_get_cd(struct mmc_host *mmc)
1083{
1084 struct mmci_host *host = mmc_priv(mmc);
Rabin Vincent29719442010-08-09 12:54:43 +01001085 struct mmci_platform_data *plat = host->plat;
Russell King89001442009-07-09 15:16:07 +01001086 unsigned int status;
1087
Rabin Vincent4b8caec2010-08-09 12:56:40 +01001088 if (host->gpio_cd == -ENOSYS) {
1089 if (!plat->status)
1090 return 1; /* Assume always present */
1091
Rabin Vincent29719442010-08-09 12:54:43 +01001092 status = plat->status(mmc_dev(host->mmc));
Rabin Vincent4b8caec2010-08-09 12:56:40 +01001093 } else
Linus Walleij18a06302010-09-12 12:56:44 +01001094 status = !!gpio_get_value_cansleep(host->gpio_cd)
1095 ^ plat->cd_invert;
Russell King89001442009-07-09 15:16:07 +01001096
Russell King74bc8092010-07-29 15:58:59 +01001097 /*
1098 * Use positive logic throughout - status is zero for no card,
1099 * non-zero for card inserted.
1100 */
1101 return status;
Russell King89001442009-07-09 15:16:07 +01001102}
1103
Rabin Vincent148b8b32010-08-09 12:55:48 +01001104static irqreturn_t mmci_cd_irq(int irq, void *dev_id)
1105{
1106 struct mmci_host *host = dev_id;
1107
1108 mmc_detect_change(host->mmc, msecs_to_jiffies(500));
1109
1110 return IRQ_HANDLED;
1111}
1112
David Brownellab7aefd2006-11-12 17:55:30 -08001113static const struct mmc_host_ops mmci_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 .request = mmci_request,
Per Forlin58c7ccb2011-07-01 18:55:24 +02001115 .pre_req = mmci_pre_request,
1116 .post_req = mmci_post_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 .set_ios = mmci_set_ios,
Russell King89001442009-07-09 15:16:07 +01001118 .get_ro = mmci_get_ro,
1119 .get_cd = mmci_get_cd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120};
1121
Russell Kingaa25afa2011-02-19 15:55:00 +00001122static int __devinit mmci_probe(struct amba_device *dev,
1123 const struct amba_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
Linus Walleij6ef297f2009-09-22 14:29:36 +01001125 struct mmci_platform_data *plat = dev->dev.platform_data;
Rabin Vincent4956e102010-07-21 12:54:40 +01001126 struct variant_data *variant = id->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 struct mmci_host *host;
1128 struct mmc_host *mmc;
1129 int ret;
1130
1131 /* must have platform data */
1132 if (!plat) {
1133 ret = -EINVAL;
1134 goto out;
1135 }
1136
1137 ret = amba_request_regions(dev, DRIVER_NAME);
1138 if (ret)
1139 goto out;
1140
1141 mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
1142 if (!mmc) {
1143 ret = -ENOMEM;
1144 goto rel_regions;
1145 }
1146
1147 host = mmc_priv(mmc);
Rabin Vincent4ea580f2009-04-17 08:44:19 +05301148 host->mmc = mmc;
Russell King012b7d32009-07-09 15:13:56 +01001149
Russell King89001442009-07-09 15:16:07 +01001150 host->gpio_wp = -ENOSYS;
1151 host->gpio_cd = -ENOSYS;
Rabin Vincent148b8b32010-08-09 12:55:48 +01001152 host->gpio_cd_irq = -1;
Russell King89001442009-07-09 15:16:07 +01001153
Russell King012b7d32009-07-09 15:13:56 +01001154 host->hw_designer = amba_manf(dev);
1155 host->hw_revision = amba_rev(dev);
Linus Walleij64de0282010-02-19 01:09:10 +01001156 dev_dbg(mmc_dev(mmc), "designer ID = 0x%02x\n", host->hw_designer);
1157 dev_dbg(mmc_dev(mmc), "revision = 0x%01x\n", host->hw_revision);
Russell King012b7d32009-07-09 15:13:56 +01001158
Russell Kingee569c42008-11-30 17:38:14 +00001159 host->clk = clk_get(&dev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 if (IS_ERR(host->clk)) {
1161 ret = PTR_ERR(host->clk);
1162 host->clk = NULL;
1163 goto host_free;
1164 }
1165
Russell King52ca0f32011-09-22 11:36:41 +01001166 ret = clk_prepare(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 if (ret)
Russell Kinga8d35842006-01-03 18:41:37 +00001168 goto clk_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Russell King52ca0f32011-09-22 11:36:41 +01001170 ret = clk_enable(host->clk);
1171 if (ret)
1172 goto clk_unprep;
1173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 host->plat = plat;
Rabin Vincent4956e102010-07-21 12:54:40 +01001175 host->variant = variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 host->mclk = clk_get_rate(host->clk);
Linus Walleijc8df9a52008-04-29 09:34:07 +01001177 /*
1178 * According to the spec, mclk is max 100 MHz,
1179 * so we try to adjust the clock down to this,
1180 * (if possible).
1181 */
1182 if (host->mclk > 100000000) {
1183 ret = clk_set_rate(host->clk, 100000000);
1184 if (ret < 0)
1185 goto clk_disable;
1186 host->mclk = clk_get_rate(host->clk);
Linus Walleij64de0282010-02-19 01:09:10 +01001187 dev_dbg(mmc_dev(mmc), "eventual mclk rate: %u Hz\n",
1188 host->mclk);
Linus Walleijc8df9a52008-04-29 09:34:07 +01001189 }
Russell Kingc8ebae32011-01-11 19:35:53 +00001190 host->phybase = dev->res.start;
Linus Walleijdc890c22009-06-07 23:27:31 +01001191 host->base = ioremap(dev->res.start, resource_size(&dev->res));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 if (!host->base) {
1193 ret = -ENOMEM;
1194 goto clk_disable;
1195 }
1196
1197 mmc->ops = &mmci_ops;
Linus Walleij7f294e42011-07-08 09:57:15 +01001198 /*
1199 * The ARM and ST versions of the block have slightly different
1200 * clock divider equations which means that the minimum divider
1201 * differs too.
1202 */
1203 if (variant->st_clkdiv)
1204 mmc->f_min = DIV_ROUND_UP(host->mclk, 257);
1205 else
1206 mmc->f_min = DIV_ROUND_UP(host->mclk, 512);
Linus Walleij808d97c2010-04-08 07:39:38 +01001207 /*
1208 * If the platform data supplies a maximum operating
1209 * frequency, this takes precedence. Else, we fall back
1210 * to using the module parameter, which has a (low)
1211 * default value in case it is not specified. Either
1212 * value must not exceed the clock rate into the block,
1213 * of course.
1214 */
1215 if (plat->f_max)
1216 mmc->f_max = min(host->mclk, plat->f_max);
1217 else
1218 mmc->f_max = min(host->mclk, fmax);
Linus Walleij64de0282010-02-19 01:09:10 +01001219 dev_dbg(mmc_dev(mmc), "clocking block at %u Hz\n", mmc->f_max);
1220
Linus Walleij34e84f32009-09-22 14:41:40 +01001221#ifdef CONFIG_REGULATOR
1222 /* If we're using the regulator framework, try to fetch a regulator */
1223 host->vcc = regulator_get(&dev->dev, "vmmc");
1224 if (IS_ERR(host->vcc))
1225 host->vcc = NULL;
1226 else {
1227 int mask = mmc_regulator_get_ocrmask(host->vcc);
1228
1229 if (mask < 0)
1230 dev_err(&dev->dev, "error getting OCR mask (%d)\n",
1231 mask);
1232 else {
1233 host->mmc->ocr_avail = (u32) mask;
1234 if (plat->ocr_mask)
1235 dev_warn(&dev->dev,
1236 "Provided ocr_mask/setpower will not be used "
1237 "(using regulator instead)\n");
1238 }
1239 }
1240#endif
1241 /* Fall back to platform data if no regulator is found */
1242 if (host->vcc == NULL)
1243 mmc->ocr_avail = plat->ocr_mask;
Linus Walleij9e6c82c2009-09-14 12:57:11 +01001244 mmc->caps = plat->capabilities;
Per Forlin5a092622011-11-14 12:02:28 +01001245 mmc->caps2 = plat->capabilities2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247 /*
1248 * We can do SGIO
1249 */
Martin K. Petersena36274e2010-09-10 01:33:59 -04001250 mmc->max_segs = NR_SG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 /*
Rabin Vincent08458ef2010-07-21 12:55:59 +01001253 * Since only a certain number of bits are valid in the data length
1254 * register, we must ensure that we don't exceed 2^num-1 bytes in a
1255 * single request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 */
Rabin Vincent08458ef2010-07-21 12:55:59 +01001257 mmc->max_req_size = (1 << variant->datalength_bits) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259 /*
1260 * Set the maximum segment size. Since we aren't doing DMA
1261 * (yet) we are only limited by the data length register.
1262 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001263 mmc->max_seg_size = mmc->max_req_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001265 /*
1266 * Block size can be up to 2048 bytes, but must be a power of two.
1267 */
1268 mmc->max_blk_size = 2048;
1269
Pierre Ossman55db8902006-11-21 17:55:45 +01001270 /*
1271 * No limit on the number of blocks transferred.
1272 */
1273 mmc->max_blk_count = mmc->max_req_size;
1274
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 spin_lock_init(&host->lock);
1276
1277 writel(0, host->base + MMCIMASK0);
1278 writel(0, host->base + MMCIMASK1);
1279 writel(0xfff, host->base + MMCICLEAR);
1280
Russell King89001442009-07-09 15:16:07 +01001281 if (gpio_is_valid(plat->gpio_cd)) {
1282 ret = gpio_request(plat->gpio_cd, DRIVER_NAME " (cd)");
1283 if (ret == 0)
1284 ret = gpio_direction_input(plat->gpio_cd);
1285 if (ret == 0)
1286 host->gpio_cd = plat->gpio_cd;
1287 else if (ret != -ENOSYS)
1288 goto err_gpio_cd;
Rabin Vincent148b8b32010-08-09 12:55:48 +01001289
Linus Walleij17ee0832011-05-05 17:23:10 +01001290 /*
1291 * A gpio pin that will detect cards when inserted and removed
1292 * will most likely want to trigger on the edges if it is
1293 * 0 when ejected and 1 when inserted (or mutatis mutandis
1294 * for the inverted case) so we request triggers on both
1295 * edges.
1296 */
Rabin Vincent148b8b32010-08-09 12:55:48 +01001297 ret = request_any_context_irq(gpio_to_irq(plat->gpio_cd),
Linus Walleij17ee0832011-05-05 17:23:10 +01001298 mmci_cd_irq,
1299 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
1300 DRIVER_NAME " (cd)", host);
Rabin Vincent148b8b32010-08-09 12:55:48 +01001301 if (ret >= 0)
1302 host->gpio_cd_irq = gpio_to_irq(plat->gpio_cd);
Russell King89001442009-07-09 15:16:07 +01001303 }
1304 if (gpio_is_valid(plat->gpio_wp)) {
1305 ret = gpio_request(plat->gpio_wp, DRIVER_NAME " (wp)");
1306 if (ret == 0)
1307 ret = gpio_direction_input(plat->gpio_wp);
1308 if (ret == 0)
1309 host->gpio_wp = plat->gpio_wp;
1310 else if (ret != -ENOSYS)
1311 goto err_gpio_wp;
1312 }
1313
Rabin Vincent4b8caec2010-08-09 12:56:40 +01001314 if ((host->plat->status || host->gpio_cd != -ENOSYS)
1315 && host->gpio_cd_irq < 0)
Rabin Vincent148b8b32010-08-09 12:55:48 +01001316 mmc->caps |= MMC_CAP_NEEDS_POLL;
1317
Thomas Gleixnerdace1452006-07-01 19:29:38 -07001318 ret = request_irq(dev->irq[0], mmci_irq, IRQF_SHARED, DRIVER_NAME " (cmd)", host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 if (ret)
1320 goto unmap;
1321
Linus Walleij2686b4b2010-10-19 12:39:48 +01001322 if (dev->irq[1] == NO_IRQ)
1323 host->singleirq = true;
1324 else {
1325 ret = request_irq(dev->irq[1], mmci_pio_irq, IRQF_SHARED,
1326 DRIVER_NAME " (pio)", host);
1327 if (ret)
1328 goto irq0_free;
1329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Linus Walleij8cb28152011-01-24 15:22:13 +01001331 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 amba_set_drvdata(dev, mmc);
1334
Russell Kingc8ebae32011-01-11 19:35:53 +00001335 dev_info(&dev->dev, "%s: PL%03x manf %x rev%u at 0x%08llx irq %d,%d (pio)\n",
1336 mmc_hostname(mmc), amba_part(dev), amba_manf(dev),
1337 amba_rev(dev), (unsigned long long)dev->res.start,
1338 dev->irq[0], dev->irq[1]);
1339
1340 mmci_dma_setup(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Russell King1c3be362011-08-14 09:17:05 +01001342 pm_runtime_put(&dev->dev);
1343
Russell King8c11a942010-12-28 19:40:40 +00001344 mmc_add_host(mmc);
1345
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 return 0;
1347
1348 irq0_free:
1349 free_irq(dev->irq[0], host);
1350 unmap:
Russell King89001442009-07-09 15:16:07 +01001351 if (host->gpio_wp != -ENOSYS)
1352 gpio_free(host->gpio_wp);
1353 err_gpio_wp:
Rabin Vincent148b8b32010-08-09 12:55:48 +01001354 if (host->gpio_cd_irq >= 0)
1355 free_irq(host->gpio_cd_irq, host);
Russell King89001442009-07-09 15:16:07 +01001356 if (host->gpio_cd != -ENOSYS)
1357 gpio_free(host->gpio_cd);
1358 err_gpio_cd:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 iounmap(host->base);
1360 clk_disable:
1361 clk_disable(host->clk);
Russell King52ca0f32011-09-22 11:36:41 +01001362 clk_unprep:
1363 clk_unprepare(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 clk_free:
1365 clk_put(host->clk);
1366 host_free:
1367 mmc_free_host(mmc);
1368 rel_regions:
1369 amba_release_regions(dev);
1370 out:
1371 return ret;
1372}
1373
Linus Walleij6dc4a472009-03-07 00:23:52 +01001374static int __devexit mmci_remove(struct amba_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
1376 struct mmc_host *mmc = amba_get_drvdata(dev);
1377
1378 amba_set_drvdata(dev, NULL);
1379
1380 if (mmc) {
1381 struct mmci_host *host = mmc_priv(mmc);
1382
Russell King1c3be362011-08-14 09:17:05 +01001383 /*
1384 * Undo pm_runtime_put() in probe. We use the _sync
1385 * version here so that we can access the primecell.
1386 */
1387 pm_runtime_get_sync(&dev->dev);
1388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 mmc_remove_host(mmc);
1390
1391 writel(0, host->base + MMCIMASK0);
1392 writel(0, host->base + MMCIMASK1);
1393
1394 writel(0, host->base + MMCICOMMAND);
1395 writel(0, host->base + MMCIDATACTRL);
1396
Russell Kingc8ebae32011-01-11 19:35:53 +00001397 mmci_dma_release(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 free_irq(dev->irq[0], host);
Linus Walleij2686b4b2010-10-19 12:39:48 +01001399 if (!host->singleirq)
1400 free_irq(dev->irq[1], host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Russell King89001442009-07-09 15:16:07 +01001402 if (host->gpio_wp != -ENOSYS)
1403 gpio_free(host->gpio_wp);
Rabin Vincent148b8b32010-08-09 12:55:48 +01001404 if (host->gpio_cd_irq >= 0)
1405 free_irq(host->gpio_cd_irq, host);
Russell King89001442009-07-09 15:16:07 +01001406 if (host->gpio_cd != -ENOSYS)
1407 gpio_free(host->gpio_cd);
1408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 iounmap(host->base);
1410 clk_disable(host->clk);
Russell King52ca0f32011-09-22 11:36:41 +01001411 clk_unprepare(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 clk_put(host->clk);
1413
Linus Walleij99fc5132010-09-29 01:08:27 -04001414 if (host->vcc)
1415 mmc_regulator_set_ocr(mmc, host->vcc, 0);
Linus Walleij34e84f32009-09-22 14:41:40 +01001416 regulator_put(host->vcc);
1417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 mmc_free_host(mmc);
1419
1420 amba_release_regions(dev);
1421 }
1422
1423 return 0;
1424}
1425
1426#ifdef CONFIG_PM
Pavel Macheke5378ca2005-04-16 15:25:29 -07001427static int mmci_suspend(struct amba_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
1429 struct mmc_host *mmc = amba_get_drvdata(dev);
1430 int ret = 0;
1431
1432 if (mmc) {
1433 struct mmci_host *host = mmc_priv(mmc);
1434
Matt Fleming1a13f8f2010-05-26 14:42:08 -07001435 ret = mmc_suspend_host(mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 if (ret == 0)
1437 writel(0, host->base + MMCIMASK0);
1438 }
1439
1440 return ret;
1441}
1442
1443static int mmci_resume(struct amba_device *dev)
1444{
1445 struct mmc_host *mmc = amba_get_drvdata(dev);
1446 int ret = 0;
1447
1448 if (mmc) {
1449 struct mmci_host *host = mmc_priv(mmc);
1450
1451 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
1452
1453 ret = mmc_resume_host(mmc);
1454 }
1455
1456 return ret;
1457}
1458#else
1459#define mmci_suspend NULL
1460#define mmci_resume NULL
1461#endif
1462
1463static struct amba_id mmci_ids[] = {
1464 {
1465 .id = 0x00041180,
Pawel Moll768fbc12011-03-11 17:18:07 +00001466 .mask = 0xff0fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001467 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 },
1469 {
Pawel Moll768fbc12011-03-11 17:18:07 +00001470 .id = 0x01041180,
1471 .mask = 0xff0fffff,
1472 .data = &variant_arm_extended_fifo,
1473 },
1474 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 .id = 0x00041181,
1476 .mask = 0x000fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001477 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 },
Linus Walleijcc30d602009-01-04 15:18:54 +01001479 /* ST Micro variants */
1480 {
1481 .id = 0x00180180,
1482 .mask = 0x00ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001483 .data = &variant_u300,
Linus Walleijcc30d602009-01-04 15:18:54 +01001484 },
1485 {
1486 .id = 0x00280180,
1487 .mask = 0x00ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001488 .data = &variant_u300,
1489 },
1490 {
1491 .id = 0x00480180,
Philippe Langlais1784b152011-03-25 08:51:52 +01001492 .mask = 0xf0ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001493 .data = &variant_ux500,
Linus Walleijcc30d602009-01-04 15:18:54 +01001494 },
Philippe Langlais1784b152011-03-25 08:51:52 +01001495 {
1496 .id = 0x10480180,
1497 .mask = 0xf0ffffff,
1498 .data = &variant_ux500v2,
1499 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 { 0, 0 },
1501};
1502
Dave Martin9f998352011-10-05 15:15:21 +01001503MODULE_DEVICE_TABLE(amba, mmci_ids);
1504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505static struct amba_driver mmci_driver = {
1506 .drv = {
1507 .name = DRIVER_NAME,
1508 },
1509 .probe = mmci_probe,
Linus Walleij6dc4a472009-03-07 00:23:52 +01001510 .remove = __devexit_p(mmci_remove),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 .suspend = mmci_suspend,
1512 .resume = mmci_resume,
1513 .id_table = mmci_ids,
1514};
1515
1516static int __init mmci_init(void)
1517{
1518 return amba_driver_register(&mmci_driver);
1519}
1520
1521static void __exit mmci_exit(void)
1522{
1523 amba_driver_unregister(&mmci_driver);
1524}
1525
1526module_init(mmci_init);
1527module_exit(mmci_exit);
1528module_param(fmax, uint, 0444);
1529
1530MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
1531MODULE_LICENSE("GPL");