blob: 7cc89beee87fa09dd576802d4590593debb4be63 [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
Ulf Hansson7d72a1d2011-12-13 16:54:55 +010056 * @pwrreg_powerup: power up value for MMCIPOWER register
Rabin Vincent4956e102010-07-21 12:54:40 +010057 */
58struct variant_data {
59 unsigned int clkreg;
Rabin Vincent4380c142010-07-21 12:55:18 +010060 unsigned int clkreg_enable;
Rabin Vincent08458ef2010-07-21 12:55:59 +010061 unsigned int datalength_bits;
Rabin Vincent8301bb62010-08-09 12:57:30 +010062 unsigned int fifosize;
63 unsigned int fifohalfsize;
Linus Walleij34177802010-10-19 12:43:58 +010064 bool sdio;
Linus Walleijb70a67f2010-12-06 09:24:14 +010065 bool st_clkdiv;
Philippe Langlais1784b152011-03-25 08:51:52 +010066 bool blksz_datactrl16;
Ulf Hansson7d72a1d2011-12-13 16:54:55 +010067 u32 pwrreg_powerup;
Rabin Vincent4956e102010-07-21 12:54:40 +010068};
69
70static struct variant_data variant_arm = {
Rabin Vincent8301bb62010-08-09 12:57:30 +010071 .fifosize = 16 * 4,
72 .fifohalfsize = 8 * 4,
Rabin Vincent08458ef2010-07-21 12:55:59 +010073 .datalength_bits = 16,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +010074 .pwrreg_powerup = MCI_PWR_UP,
Rabin Vincent4956e102010-07-21 12:54:40 +010075};
76
Pawel Moll768fbc12011-03-11 17:18:07 +000077static struct variant_data variant_arm_extended_fifo = {
78 .fifosize = 128 * 4,
79 .fifohalfsize = 64 * 4,
80 .datalength_bits = 16,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +010081 .pwrreg_powerup = MCI_PWR_UP,
Pawel Moll768fbc12011-03-11 17:18:07 +000082};
83
Rabin Vincent4956e102010-07-21 12:54:40 +010084static struct variant_data variant_u300 = {
Rabin Vincent8301bb62010-08-09 12:57:30 +010085 .fifosize = 16 * 4,
86 .fifohalfsize = 8 * 4,
Linus Walleij49ac2152011-03-04 14:54:16 +010087 .clkreg_enable = MCI_ST_U300_HWFCEN,
Rabin Vincent08458ef2010-07-21 12:55:59 +010088 .datalength_bits = 16,
Linus Walleij34177802010-10-19 12:43:58 +010089 .sdio = true,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +010090 .pwrreg_powerup = MCI_PWR_ON,
Rabin Vincent4956e102010-07-21 12:54:40 +010091};
92
93static struct variant_data variant_ux500 = {
Rabin Vincent8301bb62010-08-09 12:57:30 +010094 .fifosize = 30 * 4,
95 .fifohalfsize = 8 * 4,
Rabin Vincent4956e102010-07-21 12:54:40 +010096 .clkreg = MCI_CLK_ENABLE,
Linus Walleij49ac2152011-03-04 14:54:16 +010097 .clkreg_enable = MCI_ST_UX500_HWFCEN,
Rabin Vincent08458ef2010-07-21 12:55:59 +010098 .datalength_bits = 24,
Linus Walleij34177802010-10-19 12:43:58 +010099 .sdio = true,
Linus Walleijb70a67f2010-12-06 09:24:14 +0100100 .st_clkdiv = true,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +0100101 .pwrreg_powerup = MCI_PWR_ON,
Rabin Vincent4956e102010-07-21 12:54:40 +0100102};
Linus Walleijb70a67f2010-12-06 09:24:14 +0100103
Philippe Langlais1784b152011-03-25 08:51:52 +0100104static struct variant_data variant_ux500v2 = {
105 .fifosize = 30 * 4,
106 .fifohalfsize = 8 * 4,
107 .clkreg = MCI_CLK_ENABLE,
108 .clkreg_enable = MCI_ST_UX500_HWFCEN,
109 .datalength_bits = 24,
110 .sdio = true,
111 .st_clkdiv = true,
112 .blksz_datactrl16 = true,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +0100113 .pwrreg_powerup = MCI_PWR_ON,
Philippe Langlais1784b152011-03-25 08:51:52 +0100114};
115
Linus Walleija6a64642009-09-14 12:56:14 +0100116/*
117 * This must be called with host->lock held
118 */
119static void mmci_set_clkreg(struct mmci_host *host, unsigned int desired)
120{
Rabin Vincent4956e102010-07-21 12:54:40 +0100121 struct variant_data *variant = host->variant;
122 u32 clk = variant->clkreg;
Linus Walleija6a64642009-09-14 12:56:14 +0100123
124 if (desired) {
125 if (desired >= host->mclk) {
Linus Walleij991a86e2010-12-10 09:35:53 +0100126 clk = MCI_CLK_BYPASS;
Linus Walleij399bc482011-04-01 07:59:17 +0100127 if (variant->st_clkdiv)
128 clk |= MCI_ST_UX500_NEG_EDGE;
Linus Walleija6a64642009-09-14 12:56:14 +0100129 host->cclk = host->mclk;
Linus Walleijb70a67f2010-12-06 09:24:14 +0100130 } else if (variant->st_clkdiv) {
131 /*
132 * DB8500 TRM says f = mclk / (clkdiv + 2)
133 * => clkdiv = (mclk / f) - 2
134 * Round the divider up so we don't exceed the max
135 * frequency
136 */
137 clk = DIV_ROUND_UP(host->mclk, desired) - 2;
138 if (clk >= 256)
139 clk = 255;
140 host->cclk = host->mclk / (clk + 2);
Linus Walleija6a64642009-09-14 12:56:14 +0100141 } else {
Linus Walleijb70a67f2010-12-06 09:24:14 +0100142 /*
143 * PL180 TRM says f = mclk / (2 * (clkdiv + 1))
144 * => clkdiv = mclk / (2 * f) - 1
145 */
Linus Walleija6a64642009-09-14 12:56:14 +0100146 clk = host->mclk / (2 * desired) - 1;
147 if (clk >= 256)
148 clk = 255;
149 host->cclk = host->mclk / (2 * (clk + 1));
150 }
Rabin Vincent4380c142010-07-21 12:55:18 +0100151
152 clk |= variant->clkreg_enable;
Linus Walleija6a64642009-09-14 12:56:14 +0100153 clk |= MCI_CLK_ENABLE;
154 /* This hasn't proven to be worthwhile */
155 /* clk |= MCI_CLK_PWRSAVE; */
156 }
157
Linus Walleij9e6c82c2009-09-14 12:57:11 +0100158 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4)
Linus Walleij771dc152010-04-08 07:38:52 +0100159 clk |= MCI_4BIT_BUS;
160 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_8)
161 clk |= MCI_ST_8BIT_BUS;
Linus Walleij9e6c82c2009-09-14 12:57:11 +0100162
Linus Walleija6a64642009-09-14 12:56:14 +0100163 writel(clk, host->base + MMCICLOCK);
164}
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166static void
167mmci_request_end(struct mmci_host *host, struct mmc_request *mrq)
168{
169 writel(0, host->base + MMCICOMMAND);
170
Russell Kinge47c2222007-01-08 16:42:51 +0000171 BUG_ON(host->data);
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 host->mrq = NULL;
174 host->cmd = NULL;
175
Russell King1c3be362011-08-14 09:17:05 +0100176 pm_runtime_put(mmc_dev(host->mmc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 mmc_request_done(host->mmc, mrq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
Linus Walleij2686b4b2010-10-19 12:39:48 +0100180static void mmci_set_mask1(struct mmci_host *host, unsigned int mask)
181{
182 void __iomem *base = host->base;
183
184 if (host->singleirq) {
185 unsigned int mask0 = readl(base + MMCIMASK0);
186
187 mask0 &= ~MCI_IRQ1MASK;
188 mask0 |= mask;
189
190 writel(mask0, base + MMCIMASK0);
191 }
192
193 writel(mask, base + MMCIMASK1);
194}
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196static void mmci_stop_data(struct mmci_host *host)
197{
198 writel(0, host->base + MMCIDATACTRL);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100199 mmci_set_mask1(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 host->data = NULL;
201}
202
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100203static void mmci_init_sg(struct mmci_host *host, struct mmc_data *data)
204{
205 unsigned int flags = SG_MITER_ATOMIC;
206
207 if (data->flags & MMC_DATA_READ)
208 flags |= SG_MITER_TO_SG;
209 else
210 flags |= SG_MITER_FROM_SG;
211
212 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
213}
214
Russell Kingc8ebae32011-01-11 19:35:53 +0000215/*
216 * All the DMA operation mode stuff goes inside this ifdef.
217 * This assumes that you have a generic DMA device interface,
218 * no custom DMA interfaces are supported.
219 */
220#ifdef CONFIG_DMA_ENGINE
221static void __devinit mmci_dma_setup(struct mmci_host *host)
222{
223 struct mmci_platform_data *plat = host->plat;
224 const char *rxname, *txname;
225 dma_cap_mask_t mask;
226
227 if (!plat || !plat->dma_filter) {
228 dev_info(mmc_dev(host->mmc), "no DMA platform data\n");
229 return;
230 }
231
Per Forlin58c7ccb2011-07-01 18:55:24 +0200232 /* initialize pre request cookie */
233 host->next_data.cookie = 1;
234
Russell Kingc8ebae32011-01-11 19:35:53 +0000235 /* Try to acquire a generic DMA engine slave channel */
236 dma_cap_zero(mask);
237 dma_cap_set(DMA_SLAVE, mask);
238
239 /*
240 * If only an RX channel is specified, the driver will
241 * attempt to use it bidirectionally, however if it is
242 * is specified but cannot be located, DMA will be disabled.
243 */
244 if (plat->dma_rx_param) {
245 host->dma_rx_channel = dma_request_channel(mask,
246 plat->dma_filter,
247 plat->dma_rx_param);
248 /* E.g if no DMA hardware is present */
249 if (!host->dma_rx_channel)
250 dev_err(mmc_dev(host->mmc), "no RX DMA channel\n");
251 }
252
253 if (plat->dma_tx_param) {
254 host->dma_tx_channel = dma_request_channel(mask,
255 plat->dma_filter,
256 plat->dma_tx_param);
257 if (!host->dma_tx_channel)
258 dev_warn(mmc_dev(host->mmc), "no TX DMA channel\n");
259 } else {
260 host->dma_tx_channel = host->dma_rx_channel;
261 }
262
263 if (host->dma_rx_channel)
264 rxname = dma_chan_name(host->dma_rx_channel);
265 else
266 rxname = "none";
267
268 if (host->dma_tx_channel)
269 txname = dma_chan_name(host->dma_tx_channel);
270 else
271 txname = "none";
272
273 dev_info(mmc_dev(host->mmc), "DMA channels RX %s, TX %s\n",
274 rxname, txname);
275
276 /*
277 * Limit the maximum segment size in any SG entry according to
278 * the parameters of the DMA engine device.
279 */
280 if (host->dma_tx_channel) {
281 struct device *dev = host->dma_tx_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 if (host->dma_rx_channel) {
288 struct device *dev = host->dma_rx_channel->device->dev;
289 unsigned int max_seg_size = dma_get_max_seg_size(dev);
290
291 if (max_seg_size < host->mmc->max_seg_size)
292 host->mmc->max_seg_size = max_seg_size;
293 }
294}
295
296/*
297 * This is used in __devinit or __devexit so inline it
298 * so it can be discarded.
299 */
300static inline void mmci_dma_release(struct mmci_host *host)
301{
302 struct mmci_platform_data *plat = host->plat;
303
304 if (host->dma_rx_channel)
305 dma_release_channel(host->dma_rx_channel);
306 if (host->dma_tx_channel && plat->dma_tx_param)
307 dma_release_channel(host->dma_tx_channel);
308 host->dma_rx_channel = host->dma_tx_channel = NULL;
309}
310
311static void mmci_dma_unmap(struct mmci_host *host, struct mmc_data *data)
312{
313 struct dma_chan *chan = host->dma_current;
314 enum dma_data_direction dir;
315 u32 status;
316 int i;
317
318 /* Wait up to 1ms for the DMA to complete */
319 for (i = 0; ; i++) {
320 status = readl(host->base + MMCISTATUS);
321 if (!(status & MCI_RXDATAAVLBLMASK) || i >= 100)
322 break;
323 udelay(10);
324 }
325
326 /*
327 * Check to see whether we still have some data left in the FIFO -
328 * this catches DMA controllers which are unable to monitor the
329 * DMALBREQ and DMALSREQ signals while allowing us to DMA to non-
330 * contiguous buffers. On TX, we'll get a FIFO underrun error.
331 */
332 if (status & MCI_RXDATAAVLBLMASK) {
333 dmaengine_terminate_all(chan);
334 if (!data->error)
335 data->error = -EIO;
336 }
337
338 if (data->flags & MMC_DATA_WRITE) {
339 dir = DMA_TO_DEVICE;
340 } else {
341 dir = DMA_FROM_DEVICE;
342 }
343
Per Forlin58c7ccb2011-07-01 18:55:24 +0200344 if (!data->host_cookie)
345 dma_unmap_sg(chan->device->dev, data->sg, data->sg_len, dir);
Russell Kingc8ebae32011-01-11 19:35:53 +0000346
347 /*
348 * Use of DMA with scatter-gather is impossible.
349 * Give up with DMA and switch back to PIO mode.
350 */
351 if (status & MCI_RXDATAAVLBLMASK) {
352 dev_err(mmc_dev(host->mmc), "buggy DMA detected. Taking evasive action.\n");
353 mmci_dma_release(host);
354 }
355}
356
357static void mmci_dma_data_error(struct mmci_host *host)
358{
359 dev_err(mmc_dev(host->mmc), "error during DMA transfer!\n");
360 dmaengine_terminate_all(host->dma_current);
361}
362
Per Forlin58c7ccb2011-07-01 18:55:24 +0200363static int mmci_dma_prep_data(struct mmci_host *host, struct mmc_data *data,
364 struct mmci_host_next *next)
Russell Kingc8ebae32011-01-11 19:35:53 +0000365{
366 struct variant_data *variant = host->variant;
367 struct dma_slave_config conf = {
368 .src_addr = host->phybase + MMCIFIFO,
369 .dst_addr = host->phybase + MMCIFIFO,
370 .src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
371 .dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
372 .src_maxburst = variant->fifohalfsize >> 2, /* # of words */
373 .dst_maxburst = variant->fifohalfsize >> 2, /* # of words */
374 };
Russell Kingc8ebae32011-01-11 19:35:53 +0000375 struct dma_chan *chan;
376 struct dma_device *device;
377 struct dma_async_tx_descriptor *desc;
Vinod Koul05f57992011-10-14 10:45:11 +0530378 enum dma_data_direction buffer_dirn;
Russell Kingc8ebae32011-01-11 19:35:53 +0000379 int nr_sg;
380
Per Forlin58c7ccb2011-07-01 18:55:24 +0200381 /* Check if next job is already prepared */
382 if (data->host_cookie && !next &&
383 host->dma_current && host->dma_desc_current)
384 return 0;
385
386 if (!next) {
387 host->dma_current = NULL;
388 host->dma_desc_current = NULL;
389 }
Russell Kingc8ebae32011-01-11 19:35:53 +0000390
391 if (data->flags & MMC_DATA_READ) {
Vinod Koul05f57992011-10-14 10:45:11 +0530392 conf.direction = DMA_DEV_TO_MEM;
393 buffer_dirn = DMA_FROM_DEVICE;
Russell Kingc8ebae32011-01-11 19:35:53 +0000394 chan = host->dma_rx_channel;
395 } else {
Vinod Koul05f57992011-10-14 10:45:11 +0530396 conf.direction = DMA_MEM_TO_DEV;
397 buffer_dirn = DMA_TO_DEVICE;
Russell Kingc8ebae32011-01-11 19:35:53 +0000398 chan = host->dma_tx_channel;
399 }
400
401 /* If there's no DMA channel, fall back to PIO */
402 if (!chan)
403 return -EINVAL;
404
405 /* If less than or equal to the fifo size, don't bother with DMA */
Per Forlin58c7ccb2011-07-01 18:55:24 +0200406 if (data->blksz * data->blocks <= variant->fifosize)
Russell Kingc8ebae32011-01-11 19:35:53 +0000407 return -EINVAL;
408
409 device = chan->device;
Vinod Koul05f57992011-10-14 10:45:11 +0530410 nr_sg = dma_map_sg(device->dev, data->sg, data->sg_len, buffer_dirn);
Russell Kingc8ebae32011-01-11 19:35:53 +0000411 if (nr_sg == 0)
412 return -EINVAL;
413
414 dmaengine_slave_config(chan, &conf);
415 desc = device->device_prep_slave_sg(chan, data->sg, nr_sg,
416 conf.direction, DMA_CTRL_ACK);
417 if (!desc)
418 goto unmap_exit;
419
Per Forlin58c7ccb2011-07-01 18:55:24 +0200420 if (next) {
421 next->dma_chan = chan;
422 next->dma_desc = desc;
423 } else {
424 host->dma_current = chan;
425 host->dma_desc_current = desc;
426 }
Russell Kingc8ebae32011-01-11 19:35:53 +0000427
Per Forlin58c7ccb2011-07-01 18:55:24 +0200428 return 0;
429
430 unmap_exit:
431 if (!next)
432 dmaengine_terminate_all(chan);
Vinod Koul05f57992011-10-14 10:45:11 +0530433 dma_unmap_sg(device->dev, data->sg, data->sg_len, buffer_dirn);
Per Forlin58c7ccb2011-07-01 18:55:24 +0200434 return -ENOMEM;
435}
436
437static int mmci_dma_start_data(struct mmci_host *host, unsigned int datactrl)
438{
439 int ret;
440 struct mmc_data *data = host->data;
441
442 ret = mmci_dma_prep_data(host, host->data, NULL);
443 if (ret)
444 return ret;
445
446 /* Okay, go for it. */
Russell Kingc8ebae32011-01-11 19:35:53 +0000447 dev_vdbg(mmc_dev(host->mmc),
448 "Submit MMCI DMA job, sglen %d blksz %04x blks %04x flags %08x\n",
449 data->sg_len, data->blksz, data->blocks, data->flags);
Per Forlin58c7ccb2011-07-01 18:55:24 +0200450 dmaengine_submit(host->dma_desc_current);
451 dma_async_issue_pending(host->dma_current);
Russell Kingc8ebae32011-01-11 19:35:53 +0000452
453 datactrl |= MCI_DPSM_DMAENABLE;
454
455 /* Trigger the DMA transfer */
456 writel(datactrl, host->base + MMCIDATACTRL);
457
458 /*
459 * Let the MMCI say when the data is ended and it's time
460 * to fire next DMA request. When that happens, MMCI will
461 * call mmci_data_end()
462 */
463 writel(readl(host->base + MMCIMASK0) | MCI_DATAENDMASK,
464 host->base + MMCIMASK0);
465 return 0;
Russell Kingc8ebae32011-01-11 19:35:53 +0000466}
Per Forlin58c7ccb2011-07-01 18:55:24 +0200467
468static void mmci_get_next_data(struct mmci_host *host, struct mmc_data *data)
469{
470 struct mmci_host_next *next = &host->next_data;
471
472 if (data->host_cookie && data->host_cookie != next->cookie) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530473 pr_warning("[%s] invalid cookie: data->host_cookie %d"
Per Forlin58c7ccb2011-07-01 18:55:24 +0200474 " host->next_data.cookie %d\n",
475 __func__, data->host_cookie, host->next_data.cookie);
476 data->host_cookie = 0;
477 }
478
479 if (!data->host_cookie)
480 return;
481
482 host->dma_desc_current = next->dma_desc;
483 host->dma_current = next->dma_chan;
484
485 next->dma_desc = NULL;
486 next->dma_chan = NULL;
487}
488
489static void mmci_pre_request(struct mmc_host *mmc, struct mmc_request *mrq,
490 bool is_first_req)
491{
492 struct mmci_host *host = mmc_priv(mmc);
493 struct mmc_data *data = mrq->data;
494 struct mmci_host_next *nd = &host->next_data;
495
496 if (!data)
497 return;
498
499 if (data->host_cookie) {
500 data->host_cookie = 0;
501 return;
502 }
503
504 /* if config for dma */
505 if (((data->flags & MMC_DATA_WRITE) && host->dma_tx_channel) ||
506 ((data->flags & MMC_DATA_READ) && host->dma_rx_channel)) {
507 if (mmci_dma_prep_data(host, data, nd))
508 data->host_cookie = 0;
509 else
510 data->host_cookie = ++nd->cookie < 0 ? 1 : nd->cookie;
511 }
512}
513
514static void mmci_post_request(struct mmc_host *mmc, struct mmc_request *mrq,
515 int err)
516{
517 struct mmci_host *host = mmc_priv(mmc);
518 struct mmc_data *data = mrq->data;
519 struct dma_chan *chan;
520 enum dma_data_direction dir;
521
522 if (!data)
523 return;
524
525 if (data->flags & MMC_DATA_READ) {
526 dir = DMA_FROM_DEVICE;
527 chan = host->dma_rx_channel;
528 } else {
529 dir = DMA_TO_DEVICE;
530 chan = host->dma_tx_channel;
531 }
532
533
534 /* if config for dma */
535 if (chan) {
536 if (err)
537 dmaengine_terminate_all(chan);
Per Forlin8e3336b2011-08-29 15:35:59 +0200538 if (data->host_cookie)
Per Forlin58c7ccb2011-07-01 18:55:24 +0200539 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
540 data->sg_len, dir);
541 mrq->data->host_cookie = 0;
542 }
543}
544
Russell Kingc8ebae32011-01-11 19:35:53 +0000545#else
546/* Blank functions if the DMA engine is not available */
Per Forlin58c7ccb2011-07-01 18:55:24 +0200547static void mmci_get_next_data(struct mmci_host *host, struct mmc_data *data)
548{
549}
Russell Kingc8ebae32011-01-11 19:35:53 +0000550static inline void mmci_dma_setup(struct mmci_host *host)
551{
552}
553
554static inline void mmci_dma_release(struct mmci_host *host)
555{
556}
557
558static inline void mmci_dma_unmap(struct mmci_host *host, struct mmc_data *data)
559{
560}
561
562static inline void mmci_dma_data_error(struct mmci_host *host)
563{
564}
565
566static inline int mmci_dma_start_data(struct mmci_host *host, unsigned int datactrl)
567{
568 return -ENOSYS;
569}
Per Forlin58c7ccb2011-07-01 18:55:24 +0200570
571#define mmci_pre_request NULL
572#define mmci_post_request NULL
573
Russell Kingc8ebae32011-01-11 19:35:53 +0000574#endif
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576static void mmci_start_data(struct mmci_host *host, struct mmc_data *data)
577{
Rabin Vincent8301bb62010-08-09 12:57:30 +0100578 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 unsigned int datactrl, timeout, irqmask;
Russell King7b09cda2005-07-01 12:02:59 +0100580 unsigned long long clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 void __iomem *base;
Russell King3bc87f22006-08-27 13:51:28 +0100582 int blksz_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Linus Walleij64de0282010-02-19 01:09:10 +0100584 dev_dbg(mmc_dev(host->mmc), "blksz %04x blks %04x flags %08x\n",
585 data->blksz, data->blocks, data->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 host->data = data;
Rabin Vincent528320d2010-07-21 12:49:49 +0100588 host->size = data->blksz * data->blocks;
Russell King51d43752011-01-27 10:56:52 +0000589 data->bytes_xfered = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Russell King7b09cda2005-07-01 12:02:59 +0100591 clks = (unsigned long long)data->timeout_ns * host->cclk;
592 do_div(clks, 1000000000UL);
593
594 timeout = data->timeout_clks + (unsigned int)clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 base = host->base;
597 writel(timeout, base + MMCIDATATIMER);
598 writel(host->size, base + MMCIDATALENGTH);
599
Russell King3bc87f22006-08-27 13:51:28 +0100600 blksz_bits = ffs(data->blksz) - 1;
601 BUG_ON(1 << blksz_bits != data->blksz);
602
Philippe Langlais1784b152011-03-25 08:51:52 +0100603 if (variant->blksz_datactrl16)
604 datactrl = MCI_DPSM_ENABLE | (data->blksz << 16);
605 else
606 datactrl = MCI_DPSM_ENABLE | blksz_bits << 4;
Russell Kingc8ebae32011-01-11 19:35:53 +0000607
608 if (data->flags & MMC_DATA_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 datactrl |= MCI_DPSM_DIRECTION;
Russell Kingc8ebae32011-01-11 19:35:53 +0000610
611 /*
612 * Attempt to use DMA operation mode, if this
613 * should fail, fall back to PIO mode
614 */
615 if (!mmci_dma_start_data(host, datactrl))
616 return;
617
618 /* IRQ mode, map the SG list for CPU reading/writing */
619 mmci_init_sg(host, data);
620
621 if (data->flags & MMC_DATA_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 irqmask = MCI_RXFIFOHALFFULLMASK;
Russell King0425a142006-02-16 16:48:31 +0000623
624 /*
Russell Kingc4d877c2011-01-27 09:50:13 +0000625 * If we have less than the fifo 'half-full' threshold to
626 * transfer, trigger a PIO interrupt as soon as any data
627 * is available.
Russell King0425a142006-02-16 16:48:31 +0000628 */
Russell Kingc4d877c2011-01-27 09:50:13 +0000629 if (host->size < variant->fifohalfsize)
Russell King0425a142006-02-16 16:48:31 +0000630 irqmask |= MCI_RXDATAAVLBLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 } else {
632 /*
633 * We don't actually need to include "FIFO empty" here
634 * since its implicit in "FIFO half empty".
635 */
636 irqmask = MCI_TXFIFOHALFEMPTYMASK;
637 }
638
Linus Walleij34177802010-10-19 12:43:58 +0100639 /* The ST Micro variants has a special bit to enable SDIO */
640 if (variant->sdio && host->mmc->card)
641 if (mmc_card_sdio(host->mmc->card))
642 datactrl |= MCI_ST_DPSM_SDIOEN;
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 writel(datactrl, base + MMCIDATACTRL);
645 writel(readl(base + MMCIMASK0) & ~MCI_DATAENDMASK, base + MMCIMASK0);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100646 mmci_set_mask1(host, irqmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
649static void
650mmci_start_command(struct mmci_host *host, struct mmc_command *cmd, u32 c)
651{
652 void __iomem *base = host->base;
653
Linus Walleij64de0282010-02-19 01:09:10 +0100654 dev_dbg(mmc_dev(host->mmc), "op %02x arg %08x flags %08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 cmd->opcode, cmd->arg, cmd->flags);
656
657 if (readl(base + MMCICOMMAND) & MCI_CPSM_ENABLE) {
658 writel(0, base + MMCICOMMAND);
659 udelay(1);
660 }
661
662 c |= cmd->opcode | MCI_CPSM_ENABLE;
Russell Kinge9225172006-02-02 12:23:12 +0000663 if (cmd->flags & MMC_RSP_PRESENT) {
664 if (cmd->flags & MMC_RSP_136)
665 c |= MCI_CPSM_LONGRSP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 c |= MCI_CPSM_RESPONSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
668 if (/*interrupt*/0)
669 c |= MCI_CPSM_INTERRUPT;
670
671 host->cmd = cmd;
672
673 writel(cmd->arg, base + MMCIARGUMENT);
674 writel(c, base + MMCICOMMAND);
675}
676
677static void
678mmci_data_irq(struct mmci_host *host, struct mmc_data *data,
679 unsigned int status)
680{
Linus Walleijf20f8f22010-10-19 13:41:24 +0100681 /* First check for errors */
Ulf Hanssonb63038d2011-12-13 16:51:04 +0100682 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_STARTBITERR|
683 MCI_TXUNDERRUN|MCI_RXOVERRUN)) {
Linus Walleij8cb28152011-01-24 15:22:13 +0100684 u32 remain, success;
Linus Walleijf20f8f22010-10-19 13:41:24 +0100685
Russell Kingc8ebae32011-01-11 19:35:53 +0000686 /* Terminate the DMA transfer */
687 if (dma_inprogress(host))
688 mmci_dma_data_error(host);
689
Russell Kingc8afc9d2011-02-04 09:19:46 +0000690 /*
691 * Calculate how far we are into the transfer. Note that
692 * the data counter gives the number of bytes transferred
693 * on the MMC bus, not on the host side. On reads, this
694 * can be as much as a FIFO-worth of data ahead. This
695 * matters for FIFO overruns only.
696 */
Linus Walleijf5a106d2011-01-27 17:44:34 +0100697 remain = readl(host->base + MMCIDATACNT);
Linus Walleij8cb28152011-01-24 15:22:13 +0100698 success = data->blksz * data->blocks - remain;
699
Russell Kingc8afc9d2011-02-04 09:19:46 +0000700 dev_dbg(mmc_dev(host->mmc), "MCI ERROR IRQ, status 0x%08x at 0x%08x\n",
701 status, success);
Linus Walleij8cb28152011-01-24 15:22:13 +0100702 if (status & MCI_DATACRCFAIL) {
703 /* Last block was not successful */
Russell Kingc8afc9d2011-02-04 09:19:46 +0000704 success -= 1;
Pierre Ossman17b04292007-07-22 22:18:46 +0200705 data->error = -EILSEQ;
Linus Walleij8cb28152011-01-24 15:22:13 +0100706 } else if (status & MCI_DATATIMEOUT) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200707 data->error = -ETIMEDOUT;
Linus Walleij757df742011-06-30 15:10:21 +0100708 } else if (status & MCI_STARTBITERR) {
709 data->error = -ECOMM;
Russell Kingc8afc9d2011-02-04 09:19:46 +0000710 } else if (status & MCI_TXUNDERRUN) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200711 data->error = -EIO;
Russell Kingc8afc9d2011-02-04 09:19:46 +0000712 } else if (status & MCI_RXOVERRUN) {
713 if (success > host->variant->fifosize)
714 success -= host->variant->fifosize;
715 else
716 success = 0;
Linus Walleij8cb28152011-01-24 15:22:13 +0100717 data->error = -EIO;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100718 }
Russell King51d43752011-01-27 10:56:52 +0000719 data->bytes_xfered = round_down(success, data->blksz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 }
Linus Walleijf20f8f22010-10-19 13:41:24 +0100721
Linus Walleij8cb28152011-01-24 15:22:13 +0100722 if (status & MCI_DATABLOCKEND)
723 dev_err(mmc_dev(host->mmc), "stray MCI_DATABLOCKEND interrupt\n");
Linus Walleijf20f8f22010-10-19 13:41:24 +0100724
Russell Kingccff9b52011-01-30 21:03:50 +0000725 if (status & MCI_DATAEND || data->error) {
Russell Kingc8ebae32011-01-11 19:35:53 +0000726 if (dma_inprogress(host))
727 mmci_dma_unmap(host, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 mmci_stop_data(host);
729
Linus Walleij8cb28152011-01-24 15:22:13 +0100730 if (!data->error)
731 /* The error clause is handled above, success! */
Russell King51d43752011-01-27 10:56:52 +0000732 data->bytes_xfered = data->blksz * data->blocks;
Linus Walleijf20f8f22010-10-19 13:41:24 +0100733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (!data->stop) {
735 mmci_request_end(host, data->mrq);
736 } else {
737 mmci_start_command(host, data->stop, 0);
738 }
739 }
740}
741
742static void
743mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
744 unsigned int status)
745{
746 void __iomem *base = host->base;
747
748 host->cmd = NULL;
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (status & MCI_CMDTIMEOUT) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200751 cmd->error = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200753 cmd->error = -EILSEQ;
Russell King - ARM Linux9047b432011-01-11 16:35:56 +0000754 } else {
755 cmd->resp[0] = readl(base + MMCIRESPONSE0);
756 cmd->resp[1] = readl(base + MMCIRESPONSE1);
757 cmd->resp[2] = readl(base + MMCIRESPONSE2);
758 cmd->resp[3] = readl(base + MMCIRESPONSE3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
760
Pierre Ossman17b04292007-07-22 22:18:46 +0200761 if (!cmd->data || cmd->error) {
Ulf Hansson3b6e3c72011-12-13 16:58:43 +0100762 if (host->data) {
763 /* Terminate the DMA transfer */
764 if (dma_inprogress(host))
765 mmci_dma_data_error(host);
Russell Kinge47c2222007-01-08 16:42:51 +0000766 mmci_stop_data(host);
Ulf Hansson3b6e3c72011-12-13 16:58:43 +0100767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 mmci_request_end(host, cmd->mrq);
769 } else if (!(cmd->data->flags & MMC_DATA_READ)) {
770 mmci_start_data(host, cmd->data);
771 }
772}
773
774static int mmci_pio_read(struct mmci_host *host, char *buffer, unsigned int remain)
775{
776 void __iomem *base = host->base;
777 char *ptr = buffer;
778 u32 status;
Linus Walleij26eed9a2008-04-26 23:39:44 +0100779 int host_remain = host->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 do {
Linus Walleij26eed9a2008-04-26 23:39:44 +0100782 int count = host_remain - (readl(base + MMCIFIFOCNT) << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 if (count > remain)
785 count = remain;
786
787 if (count <= 0)
788 break;
789
790 readsl(base + MMCIFIFO, ptr, count >> 2);
791
792 ptr += count;
793 remain -= count;
Linus Walleij26eed9a2008-04-26 23:39:44 +0100794 host_remain -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 if (remain == 0)
797 break;
798
799 status = readl(base + MMCISTATUS);
800 } while (status & MCI_RXDATAAVLBL);
801
802 return ptr - buffer;
803}
804
805static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status)
806{
Rabin Vincent8301bb62010-08-09 12:57:30 +0100807 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 void __iomem *base = host->base;
809 char *ptr = buffer;
810
811 do {
812 unsigned int count, maxcnt;
813
Rabin Vincent8301bb62010-08-09 12:57:30 +0100814 maxcnt = status & MCI_TXFIFOEMPTY ?
815 variant->fifosize : variant->fifohalfsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 count = min(remain, maxcnt);
817
Linus Walleij34177802010-10-19 12:43:58 +0100818 /*
819 * The ST Micro variant for SDIO transfer sizes
820 * less then 8 bytes should have clock H/W flow
821 * control disabled.
822 */
823 if (variant->sdio &&
824 mmc_card_sdio(host->mmc->card)) {
825 if (count < 8)
826 writel(readl(host->base + MMCICLOCK) &
827 ~variant->clkreg_enable,
828 host->base + MMCICLOCK);
829 else
830 writel(readl(host->base + MMCICLOCK) |
831 variant->clkreg_enable,
832 host->base + MMCICLOCK);
833 }
834
835 /*
836 * SDIO especially may want to send something that is
837 * not divisible by 4 (as opposed to card sectors
838 * etc), and the FIFO only accept full 32-bit writes.
839 * So compensate by adding +3 on the count, a single
840 * byte become a 32bit write, 7 bytes will be two
841 * 32bit writes etc.
842 */
843 writesl(base + MMCIFIFO, ptr, (count + 3) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 ptr += count;
846 remain -= count;
847
848 if (remain == 0)
849 break;
850
851 status = readl(base + MMCISTATUS);
852 } while (status & MCI_TXFIFOHALFEMPTY);
853
854 return ptr - buffer;
855}
856
857/*
858 * PIO data transfer IRQ handler.
859 */
David Howells7d12e782006-10-05 14:55:46 +0100860static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
862 struct mmci_host *host = dev_id;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100863 struct sg_mapping_iter *sg_miter = &host->sg_miter;
Rabin Vincent8301bb62010-08-09 12:57:30 +0100864 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 void __iomem *base = host->base;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100866 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 u32 status;
868
869 status = readl(base + MMCISTATUS);
870
Linus Walleij64de0282010-02-19 01:09:10 +0100871 dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100873 local_irq_save(flags);
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 unsigned int remain, len;
877 char *buffer;
878
879 /*
880 * For write, we only need to test the half-empty flag
881 * here - if the FIFO is completely empty, then by
882 * definition it is more than half empty.
883 *
884 * For read, check for data available.
885 */
886 if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL)))
887 break;
888
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100889 if (!sg_miter_next(sg_miter))
890 break;
891
892 buffer = sg_miter->addr;
893 remain = sg_miter->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 len = 0;
896 if (status & MCI_RXACTIVE)
897 len = mmci_pio_read(host, buffer, remain);
898 if (status & MCI_TXACTIVE)
899 len = mmci_pio_write(host, buffer, remain, status);
900
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100901 sg_miter->consumed = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 host->size -= len;
904 remain -= len;
905
906 if (remain)
907 break;
908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 status = readl(base + MMCISTATUS);
910 } while (1);
911
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100912 sg_miter_stop(sg_miter);
913
914 local_irq_restore(flags);
915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 /*
Russell Kingc4d877c2011-01-27 09:50:13 +0000917 * If we have less than the fifo 'half-full' threshold to transfer,
918 * trigger a PIO interrupt as soon as any data is available.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 */
Russell Kingc4d877c2011-01-27 09:50:13 +0000920 if (status & MCI_RXACTIVE && host->size < variant->fifohalfsize)
Linus Walleij2686b4b2010-10-19 12:39:48 +0100921 mmci_set_mask1(host, MCI_RXDATAAVLBLMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 /*
924 * If we run out of data, disable the data IRQs; this
925 * prevents a race where the FIFO becomes empty before
926 * the chip itself has disabled the data path, and
927 * stops us racing with our data end IRQ.
928 */
929 if (host->size == 0) {
Linus Walleij2686b4b2010-10-19 12:39:48 +0100930 mmci_set_mask1(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0);
932 }
933
934 return IRQ_HANDLED;
935}
936
937/*
938 * Handle completion of command and data transfers.
939 */
David Howells7d12e782006-10-05 14:55:46 +0100940static irqreturn_t mmci_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
942 struct mmci_host *host = dev_id;
943 u32 status;
944 int ret = 0;
945
946 spin_lock(&host->lock);
947
948 do {
949 struct mmc_command *cmd;
950 struct mmc_data *data;
951
952 status = readl(host->base + MMCISTATUS);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100953
954 if (host->singleirq) {
955 if (status & readl(host->base + MMCIMASK1))
956 mmci_pio_irq(irq, dev_id);
957
958 status &= ~MCI_IRQ1MASK;
959 }
960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 status &= readl(host->base + MMCIMASK0);
962 writel(status, host->base + MMCICLEAR);
963
Linus Walleij64de0282010-02-19 01:09:10 +0100964 dev_dbg(mmc_dev(host->mmc), "irq0 (data+cmd) %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 data = host->data;
Ulf Hanssonb63038d2011-12-13 16:51:04 +0100967 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_STARTBITERR|
968 MCI_TXUNDERRUN|MCI_RXOVERRUN|MCI_DATAEND|
969 MCI_DATABLOCKEND) && data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 mmci_data_irq(host, data, status);
971
972 cmd = host->cmd;
973 if (status & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT|MCI_CMDSENT|MCI_CMDRESPEND) && cmd)
974 mmci_cmd_irq(host, cmd, status);
975
976 ret = 1;
977 } while (status);
978
979 spin_unlock(&host->lock);
980
981 return IRQ_RETVAL(ret);
982}
983
984static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
985{
986 struct mmci_host *host = mmc_priv(mmc);
Linus Walleij9e943022008-10-24 21:17:50 +0100987 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989 WARN_ON(host->mrq != NULL);
990
Nicolas Pitre019a5f52007-10-11 01:06:03 -0400991 if (mrq->data && !is_power_of_2(mrq->data->blksz)) {
Linus Walleij64de0282010-02-19 01:09:10 +0100992 dev_err(mmc_dev(mmc), "unsupported block size (%d bytes)\n",
993 mrq->data->blksz);
Pierre Ossman255d01a2007-07-24 20:38:53 +0200994 mrq->cmd->error = -EINVAL;
995 mmc_request_done(mmc, mrq);
996 return;
997 }
998
Russell King1c3be362011-08-14 09:17:05 +0100999 pm_runtime_get_sync(mmc_dev(mmc));
1000
Linus Walleij9e943022008-10-24 21:17:50 +01001001 spin_lock_irqsave(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003 host->mrq = mrq;
1004
Per Forlin58c7ccb2011-07-01 18:55:24 +02001005 if (mrq->data)
1006 mmci_get_next_data(host, mrq->data);
1007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 if (mrq->data && mrq->data->flags & MMC_DATA_READ)
1009 mmci_start_data(host, mrq->data);
1010
1011 mmci_start_command(host, mrq->cmd, 0);
1012
Linus Walleij9e943022008-10-24 21:17:50 +01001013 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014}
1015
1016static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1017{
1018 struct mmci_host *host = mmc_priv(mmc);
Ulf Hansson7d72a1d2011-12-13 16:54:55 +01001019 struct variant_data *variant = host->variant;
Linus Walleija6a64642009-09-14 12:56:14 +01001020 u32 pwr = 0;
1021 unsigned long flags;
Linus Walleij99fc5132010-09-29 01:08:27 -04001022 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 switch (ios->power_mode) {
1025 case MMC_POWER_OFF:
Linus Walleij99fc5132010-09-29 01:08:27 -04001026 if (host->vcc)
1027 ret = mmc_regulator_set_ocr(mmc, host->vcc, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 break;
1029 case MMC_POWER_UP:
Linus Walleij99fc5132010-09-29 01:08:27 -04001030 if (host->vcc) {
1031 ret = mmc_regulator_set_ocr(mmc, host->vcc, ios->vdd);
1032 if (ret) {
1033 dev_err(mmc_dev(mmc), "unable to set OCR\n");
1034 /*
1035 * The .set_ios() function in the mmc_host_ops
1036 * struct return void, and failing to set the
1037 * power should be rare so we print an error
1038 * and return here.
1039 */
1040 return;
1041 }
1042 }
Rabin Vincentbb8f5632010-07-21 12:53:57 +01001043 if (host->plat->vdd_handler)
1044 pwr |= host->plat->vdd_handler(mmc_dev(mmc), ios->vdd,
1045 ios->power_mode);
Ulf Hansson7d72a1d2011-12-13 16:54:55 +01001046
1047 /*
1048 * The ST Micro variant doesn't have the PL180s MCI_PWR_UP
1049 * and instead uses MCI_PWR_ON so apply whatever value is
1050 * configured in the variant data.
1051 */
1052 pwr |= variant->pwrreg_powerup;
1053
1054 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 case MMC_POWER_ON:
1056 pwr |= MCI_PWR_ON;
1057 break;
1058 }
1059
Linus Walleijcc30d602009-01-04 15:18:54 +01001060 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN) {
Linus Walleijf17a1f02009-08-04 01:01:02 +01001061 if (host->hw_designer != AMBA_VENDOR_ST)
Linus Walleijcc30d602009-01-04 15:18:54 +01001062 pwr |= MCI_ROD;
1063 else {
1064 /*
1065 * The ST Micro variant use the ROD bit for something
1066 * else and only has OD (Open Drain).
1067 */
1068 pwr |= MCI_OD;
1069 }
1070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Linus Walleija6a64642009-09-14 12:56:14 +01001072 spin_lock_irqsave(&host->lock, flags);
1073
1074 mmci_set_clkreg(host, ios->clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076 if (host->pwr != pwr) {
1077 host->pwr = pwr;
1078 writel(pwr, host->base + MMCIPOWER);
1079 }
Linus Walleija6a64642009-09-14 12:56:14 +01001080
1081 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082}
1083
Russell King89001442009-07-09 15:16:07 +01001084static int mmci_get_ro(struct mmc_host *mmc)
1085{
1086 struct mmci_host *host = mmc_priv(mmc);
1087
1088 if (host->gpio_wp == -ENOSYS)
1089 return -ENOSYS;
1090
Linus Walleij18a063012010-09-12 12:56:44 +01001091 return gpio_get_value_cansleep(host->gpio_wp);
Russell King89001442009-07-09 15:16:07 +01001092}
1093
1094static int mmci_get_cd(struct mmc_host *mmc)
1095{
1096 struct mmci_host *host = mmc_priv(mmc);
Rabin Vincent29719442010-08-09 12:54:43 +01001097 struct mmci_platform_data *plat = host->plat;
Russell King89001442009-07-09 15:16:07 +01001098 unsigned int status;
1099
Rabin Vincent4b8caec2010-08-09 12:56:40 +01001100 if (host->gpio_cd == -ENOSYS) {
1101 if (!plat->status)
1102 return 1; /* Assume always present */
1103
Rabin Vincent29719442010-08-09 12:54:43 +01001104 status = plat->status(mmc_dev(host->mmc));
Rabin Vincent4b8caec2010-08-09 12:56:40 +01001105 } else
Linus Walleij18a063012010-09-12 12:56:44 +01001106 status = !!gpio_get_value_cansleep(host->gpio_cd)
1107 ^ plat->cd_invert;
Russell King89001442009-07-09 15:16:07 +01001108
Russell King74bc8092010-07-29 15:58:59 +01001109 /*
1110 * Use positive logic throughout - status is zero for no card,
1111 * non-zero for card inserted.
1112 */
1113 return status;
Russell King89001442009-07-09 15:16:07 +01001114}
1115
Rabin Vincent148b8b32010-08-09 12:55:48 +01001116static irqreturn_t mmci_cd_irq(int irq, void *dev_id)
1117{
1118 struct mmci_host *host = dev_id;
1119
1120 mmc_detect_change(host->mmc, msecs_to_jiffies(500));
1121
1122 return IRQ_HANDLED;
1123}
1124
David Brownellab7aefd2006-11-12 17:55:30 -08001125static const struct mmc_host_ops mmci_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 .request = mmci_request,
Per Forlin58c7ccb2011-07-01 18:55:24 +02001127 .pre_req = mmci_pre_request,
1128 .post_req = mmci_post_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 .set_ios = mmci_set_ios,
Russell King89001442009-07-09 15:16:07 +01001130 .get_ro = mmci_get_ro,
1131 .get_cd = mmci_get_cd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132};
1133
Russell Kingaa25afa2011-02-19 15:55:00 +00001134static int __devinit mmci_probe(struct amba_device *dev,
1135 const struct amba_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
Linus Walleij6ef297f2009-09-22 14:29:36 +01001137 struct mmci_platform_data *plat = dev->dev.platform_data;
Rabin Vincent4956e102010-07-21 12:54:40 +01001138 struct variant_data *variant = id->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 struct mmci_host *host;
1140 struct mmc_host *mmc;
1141 int ret;
1142
1143 /* must have platform data */
1144 if (!plat) {
1145 ret = -EINVAL;
1146 goto out;
1147 }
1148
1149 ret = amba_request_regions(dev, DRIVER_NAME);
1150 if (ret)
1151 goto out;
1152
1153 mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
1154 if (!mmc) {
1155 ret = -ENOMEM;
1156 goto rel_regions;
1157 }
1158
1159 host = mmc_priv(mmc);
Rabin Vincent4ea580f2009-04-17 08:44:19 +05301160 host->mmc = mmc;
Russell King012b7d32009-07-09 15:13:56 +01001161
Russell King89001442009-07-09 15:16:07 +01001162 host->gpio_wp = -ENOSYS;
1163 host->gpio_cd = -ENOSYS;
Rabin Vincent148b8b32010-08-09 12:55:48 +01001164 host->gpio_cd_irq = -1;
Russell King89001442009-07-09 15:16:07 +01001165
Russell King012b7d32009-07-09 15:13:56 +01001166 host->hw_designer = amba_manf(dev);
1167 host->hw_revision = amba_rev(dev);
Linus Walleij64de0282010-02-19 01:09:10 +01001168 dev_dbg(mmc_dev(mmc), "designer ID = 0x%02x\n", host->hw_designer);
1169 dev_dbg(mmc_dev(mmc), "revision = 0x%01x\n", host->hw_revision);
Russell King012b7d32009-07-09 15:13:56 +01001170
Russell Kingee569c42008-11-30 17:38:14 +00001171 host->clk = clk_get(&dev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 if (IS_ERR(host->clk)) {
1173 ret = PTR_ERR(host->clk);
1174 host->clk = NULL;
1175 goto host_free;
1176 }
1177
Russell King52ca0f32011-09-22 11:36:41 +01001178 ret = clk_prepare(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 if (ret)
Russell Kinga8d35842006-01-03 18:41:37 +00001180 goto clk_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
Russell King52ca0f32011-09-22 11:36:41 +01001182 ret = clk_enable(host->clk);
1183 if (ret)
1184 goto clk_unprep;
1185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 host->plat = plat;
Rabin Vincent4956e102010-07-21 12:54:40 +01001187 host->variant = variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 host->mclk = clk_get_rate(host->clk);
Linus Walleijc8df9a52008-04-29 09:34:07 +01001189 /*
1190 * According to the spec, mclk is max 100 MHz,
1191 * so we try to adjust the clock down to this,
1192 * (if possible).
1193 */
1194 if (host->mclk > 100000000) {
1195 ret = clk_set_rate(host->clk, 100000000);
1196 if (ret < 0)
1197 goto clk_disable;
1198 host->mclk = clk_get_rate(host->clk);
Linus Walleij64de0282010-02-19 01:09:10 +01001199 dev_dbg(mmc_dev(mmc), "eventual mclk rate: %u Hz\n",
1200 host->mclk);
Linus Walleijc8df9a52008-04-29 09:34:07 +01001201 }
Russell Kingc8ebae32011-01-11 19:35:53 +00001202 host->phybase = dev->res.start;
Linus Walleijdc890c22009-06-07 23:27:31 +01001203 host->base = ioremap(dev->res.start, resource_size(&dev->res));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 if (!host->base) {
1205 ret = -ENOMEM;
1206 goto clk_disable;
1207 }
1208
1209 mmc->ops = &mmci_ops;
Linus Walleij7f294e42011-07-08 09:57:15 +01001210 /*
1211 * The ARM and ST versions of the block have slightly different
1212 * clock divider equations which means that the minimum divider
1213 * differs too.
1214 */
1215 if (variant->st_clkdiv)
1216 mmc->f_min = DIV_ROUND_UP(host->mclk, 257);
1217 else
1218 mmc->f_min = DIV_ROUND_UP(host->mclk, 512);
Linus Walleij808d97c2010-04-08 07:39:38 +01001219 /*
1220 * If the platform data supplies a maximum operating
1221 * frequency, this takes precedence. Else, we fall back
1222 * to using the module parameter, which has a (low)
1223 * default value in case it is not specified. Either
1224 * value must not exceed the clock rate into the block,
1225 * of course.
1226 */
1227 if (plat->f_max)
1228 mmc->f_max = min(host->mclk, plat->f_max);
1229 else
1230 mmc->f_max = min(host->mclk, fmax);
Linus Walleij64de0282010-02-19 01:09:10 +01001231 dev_dbg(mmc_dev(mmc), "clocking block at %u Hz\n", mmc->f_max);
1232
Linus Walleij34e84f32009-09-22 14:41:40 +01001233#ifdef CONFIG_REGULATOR
1234 /* If we're using the regulator framework, try to fetch a regulator */
1235 host->vcc = regulator_get(&dev->dev, "vmmc");
1236 if (IS_ERR(host->vcc))
1237 host->vcc = NULL;
1238 else {
1239 int mask = mmc_regulator_get_ocrmask(host->vcc);
1240
1241 if (mask < 0)
1242 dev_err(&dev->dev, "error getting OCR mask (%d)\n",
1243 mask);
1244 else {
1245 host->mmc->ocr_avail = (u32) mask;
1246 if (plat->ocr_mask)
1247 dev_warn(&dev->dev,
1248 "Provided ocr_mask/setpower will not be used "
1249 "(using regulator instead)\n");
1250 }
1251 }
1252#endif
1253 /* Fall back to platform data if no regulator is found */
1254 if (host->vcc == NULL)
1255 mmc->ocr_avail = plat->ocr_mask;
Linus Walleij9e6c82c2009-09-14 12:57:11 +01001256 mmc->caps = plat->capabilities;
Per Forlin5a092622011-11-14 12:02:28 +01001257 mmc->caps2 = plat->capabilities2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259 /*
1260 * We can do SGIO
1261 */
Martin K. Petersena36274e2010-09-10 01:33:59 -04001262 mmc->max_segs = NR_SG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 /*
Rabin Vincent08458ef2010-07-21 12:55:59 +01001265 * Since only a certain number of bits are valid in the data length
1266 * register, we must ensure that we don't exceed 2^num-1 bytes in a
1267 * single request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 */
Rabin Vincent08458ef2010-07-21 12:55:59 +01001269 mmc->max_req_size = (1 << variant->datalength_bits) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 /*
1272 * Set the maximum segment size. Since we aren't doing DMA
1273 * (yet) we are only limited by the data length register.
1274 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001275 mmc->max_seg_size = mmc->max_req_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001277 /*
1278 * Block size can be up to 2048 bytes, but must be a power of two.
1279 */
1280 mmc->max_blk_size = 2048;
1281
Pierre Ossman55db8902006-11-21 17:55:45 +01001282 /*
1283 * No limit on the number of blocks transferred.
1284 */
1285 mmc->max_blk_count = mmc->max_req_size;
1286
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 spin_lock_init(&host->lock);
1288
1289 writel(0, host->base + MMCIMASK0);
1290 writel(0, host->base + MMCIMASK1);
1291 writel(0xfff, host->base + MMCICLEAR);
1292
Russell King89001442009-07-09 15:16:07 +01001293 if (gpio_is_valid(plat->gpio_cd)) {
1294 ret = gpio_request(plat->gpio_cd, DRIVER_NAME " (cd)");
1295 if (ret == 0)
1296 ret = gpio_direction_input(plat->gpio_cd);
1297 if (ret == 0)
1298 host->gpio_cd = plat->gpio_cd;
1299 else if (ret != -ENOSYS)
1300 goto err_gpio_cd;
Rabin Vincent148b8b32010-08-09 12:55:48 +01001301
Linus Walleij17ee0832011-05-05 17:23:10 +01001302 /*
1303 * A gpio pin that will detect cards when inserted and removed
1304 * will most likely want to trigger on the edges if it is
1305 * 0 when ejected and 1 when inserted (or mutatis mutandis
1306 * for the inverted case) so we request triggers on both
1307 * edges.
1308 */
Rabin Vincent148b8b32010-08-09 12:55:48 +01001309 ret = request_any_context_irq(gpio_to_irq(plat->gpio_cd),
Linus Walleij17ee0832011-05-05 17:23:10 +01001310 mmci_cd_irq,
1311 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
1312 DRIVER_NAME " (cd)", host);
Rabin Vincent148b8b32010-08-09 12:55:48 +01001313 if (ret >= 0)
1314 host->gpio_cd_irq = gpio_to_irq(plat->gpio_cd);
Russell King89001442009-07-09 15:16:07 +01001315 }
1316 if (gpio_is_valid(plat->gpio_wp)) {
1317 ret = gpio_request(plat->gpio_wp, DRIVER_NAME " (wp)");
1318 if (ret == 0)
1319 ret = gpio_direction_input(plat->gpio_wp);
1320 if (ret == 0)
1321 host->gpio_wp = plat->gpio_wp;
1322 else if (ret != -ENOSYS)
1323 goto err_gpio_wp;
1324 }
1325
Rabin Vincent4b8caec2010-08-09 12:56:40 +01001326 if ((host->plat->status || host->gpio_cd != -ENOSYS)
1327 && host->gpio_cd_irq < 0)
Rabin Vincent148b8b32010-08-09 12:55:48 +01001328 mmc->caps |= MMC_CAP_NEEDS_POLL;
1329
Thomas Gleixnerdace1452006-07-01 19:29:38 -07001330 ret = request_irq(dev->irq[0], mmci_irq, IRQF_SHARED, DRIVER_NAME " (cmd)", host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 if (ret)
1332 goto unmap;
1333
Linus Walleij2686b4b2010-10-19 12:39:48 +01001334 if (dev->irq[1] == NO_IRQ)
1335 host->singleirq = true;
1336 else {
1337 ret = request_irq(dev->irq[1], mmci_pio_irq, IRQF_SHARED,
1338 DRIVER_NAME " (pio)", host);
1339 if (ret)
1340 goto irq0_free;
1341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Linus Walleij8cb28152011-01-24 15:22:13 +01001343 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 amba_set_drvdata(dev, mmc);
1346
Russell Kingc8ebae32011-01-11 19:35:53 +00001347 dev_info(&dev->dev, "%s: PL%03x manf %x rev%u at 0x%08llx irq %d,%d (pio)\n",
1348 mmc_hostname(mmc), amba_part(dev), amba_manf(dev),
1349 amba_rev(dev), (unsigned long long)dev->res.start,
1350 dev->irq[0], dev->irq[1]);
1351
1352 mmci_dma_setup(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Russell King1c3be362011-08-14 09:17:05 +01001354 pm_runtime_put(&dev->dev);
1355
Russell King8c11a942010-12-28 19:40:40 +00001356 mmc_add_host(mmc);
1357
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 return 0;
1359
1360 irq0_free:
1361 free_irq(dev->irq[0], host);
1362 unmap:
Russell King89001442009-07-09 15:16:07 +01001363 if (host->gpio_wp != -ENOSYS)
1364 gpio_free(host->gpio_wp);
1365 err_gpio_wp:
Rabin Vincent148b8b32010-08-09 12:55:48 +01001366 if (host->gpio_cd_irq >= 0)
1367 free_irq(host->gpio_cd_irq, host);
Russell King89001442009-07-09 15:16:07 +01001368 if (host->gpio_cd != -ENOSYS)
1369 gpio_free(host->gpio_cd);
1370 err_gpio_cd:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 iounmap(host->base);
1372 clk_disable:
1373 clk_disable(host->clk);
Russell King52ca0f32011-09-22 11:36:41 +01001374 clk_unprep:
1375 clk_unprepare(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 clk_free:
1377 clk_put(host->clk);
1378 host_free:
1379 mmc_free_host(mmc);
1380 rel_regions:
1381 amba_release_regions(dev);
1382 out:
1383 return ret;
1384}
1385
Linus Walleij6dc4a472009-03-07 00:23:52 +01001386static int __devexit mmci_remove(struct amba_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
1388 struct mmc_host *mmc = amba_get_drvdata(dev);
1389
1390 amba_set_drvdata(dev, NULL);
1391
1392 if (mmc) {
1393 struct mmci_host *host = mmc_priv(mmc);
1394
Russell King1c3be362011-08-14 09:17:05 +01001395 /*
1396 * Undo pm_runtime_put() in probe. We use the _sync
1397 * version here so that we can access the primecell.
1398 */
1399 pm_runtime_get_sync(&dev->dev);
1400
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 mmc_remove_host(mmc);
1402
1403 writel(0, host->base + MMCIMASK0);
1404 writel(0, host->base + MMCIMASK1);
1405
1406 writel(0, host->base + MMCICOMMAND);
1407 writel(0, host->base + MMCIDATACTRL);
1408
Russell Kingc8ebae32011-01-11 19:35:53 +00001409 mmci_dma_release(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 free_irq(dev->irq[0], host);
Linus Walleij2686b4b2010-10-19 12:39:48 +01001411 if (!host->singleirq)
1412 free_irq(dev->irq[1], host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Russell King89001442009-07-09 15:16:07 +01001414 if (host->gpio_wp != -ENOSYS)
1415 gpio_free(host->gpio_wp);
Rabin Vincent148b8b32010-08-09 12:55:48 +01001416 if (host->gpio_cd_irq >= 0)
1417 free_irq(host->gpio_cd_irq, host);
Russell King89001442009-07-09 15:16:07 +01001418 if (host->gpio_cd != -ENOSYS)
1419 gpio_free(host->gpio_cd);
1420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 iounmap(host->base);
1422 clk_disable(host->clk);
Russell King52ca0f32011-09-22 11:36:41 +01001423 clk_unprepare(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 clk_put(host->clk);
1425
Linus Walleij99fc5132010-09-29 01:08:27 -04001426 if (host->vcc)
1427 mmc_regulator_set_ocr(mmc, host->vcc, 0);
Linus Walleij34e84f32009-09-22 14:41:40 +01001428 regulator_put(host->vcc);
1429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 mmc_free_host(mmc);
1431
1432 amba_release_regions(dev);
1433 }
1434
1435 return 0;
1436}
1437
1438#ifdef CONFIG_PM
Pavel Macheke5378ca2005-04-16 15:25:29 -07001439static int mmci_suspend(struct amba_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
1441 struct mmc_host *mmc = amba_get_drvdata(dev);
1442 int ret = 0;
1443
1444 if (mmc) {
1445 struct mmci_host *host = mmc_priv(mmc);
1446
Matt Fleming1a13f8f2010-05-26 14:42:08 -07001447 ret = mmc_suspend_host(mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 if (ret == 0)
1449 writel(0, host->base + MMCIMASK0);
1450 }
1451
1452 return ret;
1453}
1454
1455static int mmci_resume(struct amba_device *dev)
1456{
1457 struct mmc_host *mmc = amba_get_drvdata(dev);
1458 int ret = 0;
1459
1460 if (mmc) {
1461 struct mmci_host *host = mmc_priv(mmc);
1462
1463 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
1464
1465 ret = mmc_resume_host(mmc);
1466 }
1467
1468 return ret;
1469}
1470#else
1471#define mmci_suspend NULL
1472#define mmci_resume NULL
1473#endif
1474
1475static struct amba_id mmci_ids[] = {
1476 {
1477 .id = 0x00041180,
Pawel Moll768fbc12011-03-11 17:18:07 +00001478 .mask = 0xff0fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001479 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 },
1481 {
Pawel Moll768fbc12011-03-11 17:18:07 +00001482 .id = 0x01041180,
1483 .mask = 0xff0fffff,
1484 .data = &variant_arm_extended_fifo,
1485 },
1486 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 .id = 0x00041181,
1488 .mask = 0x000fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001489 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 },
Linus Walleijcc30d602009-01-04 15:18:54 +01001491 /* ST Micro variants */
1492 {
1493 .id = 0x00180180,
1494 .mask = 0x00ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001495 .data = &variant_u300,
Linus Walleijcc30d602009-01-04 15:18:54 +01001496 },
1497 {
1498 .id = 0x00280180,
1499 .mask = 0x00ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001500 .data = &variant_u300,
1501 },
1502 {
1503 .id = 0x00480180,
Philippe Langlais1784b152011-03-25 08:51:52 +01001504 .mask = 0xf0ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001505 .data = &variant_ux500,
Linus Walleijcc30d602009-01-04 15:18:54 +01001506 },
Philippe Langlais1784b152011-03-25 08:51:52 +01001507 {
1508 .id = 0x10480180,
1509 .mask = 0xf0ffffff,
1510 .data = &variant_ux500v2,
1511 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 { 0, 0 },
1513};
1514
Dave Martin9f998352011-10-05 15:15:21 +01001515MODULE_DEVICE_TABLE(amba, mmci_ids);
1516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517static struct amba_driver mmci_driver = {
1518 .drv = {
1519 .name = DRIVER_NAME,
1520 },
1521 .probe = mmci_probe,
Linus Walleij6dc4a472009-03-07 00:23:52 +01001522 .remove = __devexit_p(mmci_remove),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 .suspend = mmci_suspend,
1524 .resume = mmci_resume,
1525 .id_table = mmci_ids,
1526};
1527
1528static int __init mmci_init(void)
1529{
1530 return amba_driver_register(&mmci_driver);
1531}
1532
1533static void __exit mmci_exit(void)
1534{
1535 amba_driver_unregister(&mmci_driver);
1536}
1537
1538module_init(mmci_init);
1539module_exit(mmci_exit);
1540module_param(fmax, uint, 0444);
1541
1542MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
1543MODULE_LICENSE("GPL");