blob: fba51073e94f2fc255815e93c87fda56e96819e2 [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>
Lee Jones000bc9d2012-04-16 10:18:43 +010018#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/delay.h>
20#include <linux/err.h>
21#include <linux/highmem.h>
Nicolas Pitre019a5f52007-10-11 01:06:03 -040022#include <linux/log2.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/mmc/host.h>
Linus Walleij34177802010-10-19 12:43:58 +010024#include <linux/mmc/card.h>
Russell Kinga62c80e2006-01-07 13:52:45 +000025#include <linux/amba/bus.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000026#include <linux/clk.h>
Jens Axboebd6dee62007-10-24 09:01:09 +020027#include <linux/scatterlist.h>
Russell King89001442009-07-09 15:16:07 +010028#include <linux/gpio.h>
Lee Jones9a597012012-04-12 16:51:13 +010029#include <linux/of_gpio.h>
Linus Walleij34e84f32009-09-22 14:41:40 +010030#include <linux/regulator/consumer.h>
Russell Kingc8ebae32011-01-11 19:35:53 +000031#include <linux/dmaengine.h>
32#include <linux/dma-mapping.h>
33#include <linux/amba/mmci.h>
Russell King1c3be362011-08-14 09:17:05 +010034#include <linux/pm_runtime.h>
Viresh Kumar258aea72012-02-01 16:12:19 +053035#include <linux/types.h>
Linus Walleija9a83782012-10-29 14:39:30 +010036#include <linux/pinctrl/consumer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Russell King7b09cda2005-07-01 12:02:59 +010038#include <asm/div64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <asm/io.h>
Russell Kingc6b8fda2005-10-28 14:05:16 +010040#include <asm/sizes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include "mmci.h"
43
44#define DRIVER_NAME "mmci-pl18x"
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046static unsigned int fmax = 515633;
47
Rabin Vincent4956e102010-07-21 12:54:40 +010048/**
49 * struct variant_data - MMCI variant-specific quirks
50 * @clkreg: default value for MCICLOCK register
Rabin Vincent4380c142010-07-21 12:55:18 +010051 * @clkreg_enable: enable value for MMCICLOCK register
Rabin Vincent08458ef2010-07-21 12:55:59 +010052 * @datalength_bits: number of bits in the MMCIDATALENGTH register
Rabin Vincent8301bb62010-08-09 12:57:30 +010053 * @fifosize: number of bytes that can be written when MMCI_TXFIFOEMPTY
54 * is asserted (likewise for RX)
55 * @fifohalfsize: number of bytes that can be written when MCI_TXFIFOHALFEMPTY
56 * is asserted (likewise for RX)
Linus Walleij34177802010-10-19 12:43:58 +010057 * @sdio: variant supports SDIO
Linus Walleijb70a67f2010-12-06 09:24:14 +010058 * @st_clkdiv: true if using a ST-specific clock divider algorithm
Philippe Langlais1784b152011-03-25 08:51:52 +010059 * @blksz_datactrl16: true if Block size is at b16..b30 position in datactrl register
Ulf Hansson7d72a1d2011-12-13 16:54:55 +010060 * @pwrreg_powerup: power up value for MMCIPOWER register
Ulf Hansson4d1a3a02011-12-13 16:57:07 +010061 * @signal_direction: input/out direction of bus signals can be indicated
Rabin Vincent4956e102010-07-21 12:54:40 +010062 */
63struct variant_data {
64 unsigned int clkreg;
Rabin Vincent4380c142010-07-21 12:55:18 +010065 unsigned int clkreg_enable;
Rabin Vincent08458ef2010-07-21 12:55:59 +010066 unsigned int datalength_bits;
Rabin Vincent8301bb62010-08-09 12:57:30 +010067 unsigned int fifosize;
68 unsigned int fifohalfsize;
Linus Walleij34177802010-10-19 12:43:58 +010069 bool sdio;
Linus Walleijb70a67f2010-12-06 09:24:14 +010070 bool st_clkdiv;
Philippe Langlais1784b152011-03-25 08:51:52 +010071 bool blksz_datactrl16;
Ulf Hansson7d72a1d2011-12-13 16:54:55 +010072 u32 pwrreg_powerup;
Ulf Hansson4d1a3a02011-12-13 16:57:07 +010073 bool signal_direction;
Rabin Vincent4956e102010-07-21 12:54:40 +010074};
75
76static struct variant_data variant_arm = {
Rabin Vincent8301bb62010-08-09 12:57:30 +010077 .fifosize = 16 * 4,
78 .fifohalfsize = 8 * 4,
Rabin Vincent08458ef2010-07-21 12:55:59 +010079 .datalength_bits = 16,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +010080 .pwrreg_powerup = MCI_PWR_UP,
Rabin Vincent4956e102010-07-21 12:54:40 +010081};
82
Pawel Moll768fbc12011-03-11 17:18:07 +000083static struct variant_data variant_arm_extended_fifo = {
84 .fifosize = 128 * 4,
85 .fifohalfsize = 64 * 4,
86 .datalength_bits = 16,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +010087 .pwrreg_powerup = MCI_PWR_UP,
Pawel Moll768fbc12011-03-11 17:18:07 +000088};
89
Rabin Vincent4956e102010-07-21 12:54:40 +010090static struct variant_data variant_u300 = {
Rabin Vincent8301bb62010-08-09 12:57:30 +010091 .fifosize = 16 * 4,
92 .fifohalfsize = 8 * 4,
Linus Walleij49ac2152011-03-04 14:54:16 +010093 .clkreg_enable = MCI_ST_U300_HWFCEN,
Rabin Vincent08458ef2010-07-21 12:55:59 +010094 .datalength_bits = 16,
Linus Walleij34177802010-10-19 12:43:58 +010095 .sdio = true,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +010096 .pwrreg_powerup = MCI_PWR_ON,
Ulf Hansson4d1a3a02011-12-13 16:57:07 +010097 .signal_direction = true,
Rabin Vincent4956e102010-07-21 12:54:40 +010098};
99
Linus Walleij34fd4212012-04-10 17:43:59 +0100100static struct variant_data variant_nomadik = {
101 .fifosize = 16 * 4,
102 .fifohalfsize = 8 * 4,
103 .clkreg = MCI_CLK_ENABLE,
104 .datalength_bits = 24,
105 .sdio = true,
106 .st_clkdiv = true,
107 .pwrreg_powerup = MCI_PWR_ON,
108 .signal_direction = true,
109};
110
Rabin Vincent4956e102010-07-21 12:54:40 +0100111static struct variant_data variant_ux500 = {
Rabin Vincent8301bb62010-08-09 12:57:30 +0100112 .fifosize = 30 * 4,
113 .fifohalfsize = 8 * 4,
Rabin Vincent4956e102010-07-21 12:54:40 +0100114 .clkreg = MCI_CLK_ENABLE,
Linus Walleij49ac2152011-03-04 14:54:16 +0100115 .clkreg_enable = MCI_ST_UX500_HWFCEN,
Rabin Vincent08458ef2010-07-21 12:55:59 +0100116 .datalength_bits = 24,
Linus Walleij34177802010-10-19 12:43:58 +0100117 .sdio = true,
Linus Walleijb70a67f2010-12-06 09:24:14 +0100118 .st_clkdiv = true,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +0100119 .pwrreg_powerup = MCI_PWR_ON,
Ulf Hansson4d1a3a02011-12-13 16:57:07 +0100120 .signal_direction = true,
Rabin Vincent4956e102010-07-21 12:54:40 +0100121};
Linus Walleijb70a67f2010-12-06 09:24:14 +0100122
Philippe Langlais1784b152011-03-25 08:51:52 +0100123static struct variant_data variant_ux500v2 = {
124 .fifosize = 30 * 4,
125 .fifohalfsize = 8 * 4,
126 .clkreg = MCI_CLK_ENABLE,
127 .clkreg_enable = MCI_ST_UX500_HWFCEN,
128 .datalength_bits = 24,
129 .sdio = true,
130 .st_clkdiv = true,
131 .blksz_datactrl16 = true,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +0100132 .pwrreg_powerup = MCI_PWR_ON,
Ulf Hansson4d1a3a02011-12-13 16:57:07 +0100133 .signal_direction = true,
Philippe Langlais1784b152011-03-25 08:51:52 +0100134};
135
Linus Walleija6a64642009-09-14 12:56:14 +0100136/*
137 * This must be called with host->lock held
138 */
Ulf Hansson7437cfa2012-01-18 09:17:27 +0100139static void mmci_write_clkreg(struct mmci_host *host, u32 clk)
140{
141 if (host->clk_reg != clk) {
142 host->clk_reg = clk;
143 writel(clk, host->base + MMCICLOCK);
144 }
145}
146
147/*
148 * This must be called with host->lock held
149 */
150static void mmci_write_pwrreg(struct mmci_host *host, u32 pwr)
151{
152 if (host->pwr_reg != pwr) {
153 host->pwr_reg = pwr;
154 writel(pwr, host->base + MMCIPOWER);
155 }
156}
157
158/*
159 * This must be called with host->lock held
160 */
Linus Walleija6a64642009-09-14 12:56:14 +0100161static void mmci_set_clkreg(struct mmci_host *host, unsigned int desired)
162{
Rabin Vincent4956e102010-07-21 12:54:40 +0100163 struct variant_data *variant = host->variant;
164 u32 clk = variant->clkreg;
Linus Walleija6a64642009-09-14 12:56:14 +0100165
166 if (desired) {
167 if (desired >= host->mclk) {
Linus Walleij991a86e2010-12-10 09:35:53 +0100168 clk = MCI_CLK_BYPASS;
Linus Walleij399bc482011-04-01 07:59:17 +0100169 if (variant->st_clkdiv)
170 clk |= MCI_ST_UX500_NEG_EDGE;
Linus Walleija6a64642009-09-14 12:56:14 +0100171 host->cclk = host->mclk;
Linus Walleijb70a67f2010-12-06 09:24:14 +0100172 } else if (variant->st_clkdiv) {
173 /*
174 * DB8500 TRM says f = mclk / (clkdiv + 2)
175 * => clkdiv = (mclk / f) - 2
176 * Round the divider up so we don't exceed the max
177 * frequency
178 */
179 clk = DIV_ROUND_UP(host->mclk, desired) - 2;
180 if (clk >= 256)
181 clk = 255;
182 host->cclk = host->mclk / (clk + 2);
Linus Walleija6a64642009-09-14 12:56:14 +0100183 } else {
Linus Walleijb70a67f2010-12-06 09:24:14 +0100184 /*
185 * PL180 TRM says f = mclk / (2 * (clkdiv + 1))
186 * => clkdiv = mclk / (2 * f) - 1
187 */
Linus Walleija6a64642009-09-14 12:56:14 +0100188 clk = host->mclk / (2 * desired) - 1;
189 if (clk >= 256)
190 clk = 255;
191 host->cclk = host->mclk / (2 * (clk + 1));
192 }
Rabin Vincent4380c142010-07-21 12:55:18 +0100193
194 clk |= variant->clkreg_enable;
Linus Walleija6a64642009-09-14 12:56:14 +0100195 clk |= MCI_CLK_ENABLE;
196 /* This hasn't proven to be worthwhile */
197 /* clk |= MCI_CLK_PWRSAVE; */
198 }
199
Linus Walleij9e6c82c2009-09-14 12:57:11 +0100200 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4)
Linus Walleij771dc152010-04-08 07:38:52 +0100201 clk |= MCI_4BIT_BUS;
202 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_8)
203 clk |= MCI_ST_8BIT_BUS;
Linus Walleij9e6c82c2009-09-14 12:57:11 +0100204
Ulf Hansson6dbb6ee2013-01-07 15:30:44 +0100205 if (host->mmc->ios.timing == MMC_TIMING_UHS_DDR50)
206 clk |= MCI_ST_UX500_NEG_EDGE;
207
Ulf Hansson7437cfa2012-01-18 09:17:27 +0100208 mmci_write_clkreg(host, clk);
Linus Walleija6a64642009-09-14 12:56:14 +0100209}
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211static void
212mmci_request_end(struct mmci_host *host, struct mmc_request *mrq)
213{
214 writel(0, host->base + MMCICOMMAND);
215
Russell Kinge47c2222007-01-08 16:42:51 +0000216 BUG_ON(host->data);
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 host->mrq = NULL;
219 host->cmd = NULL;
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 mmc_request_done(host->mmc, mrq);
Ulf Hansson2cd976c2011-12-13 17:01:11 +0100222
223 pm_runtime_mark_last_busy(mmc_dev(host->mmc));
224 pm_runtime_put_autosuspend(mmc_dev(host->mmc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
Linus Walleij2686b4b2010-10-19 12:39:48 +0100227static void mmci_set_mask1(struct mmci_host *host, unsigned int mask)
228{
229 void __iomem *base = host->base;
230
231 if (host->singleirq) {
232 unsigned int mask0 = readl(base + MMCIMASK0);
233
234 mask0 &= ~MCI_IRQ1MASK;
235 mask0 |= mask;
236
237 writel(mask0, base + MMCIMASK0);
238 }
239
240 writel(mask, base + MMCIMASK1);
241}
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243static void mmci_stop_data(struct mmci_host *host)
244{
245 writel(0, host->base + MMCIDATACTRL);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100246 mmci_set_mask1(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 host->data = NULL;
248}
249
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100250static void mmci_init_sg(struct mmci_host *host, struct mmc_data *data)
251{
252 unsigned int flags = SG_MITER_ATOMIC;
253
254 if (data->flags & MMC_DATA_READ)
255 flags |= SG_MITER_TO_SG;
256 else
257 flags |= SG_MITER_FROM_SG;
258
259 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
260}
261
Russell Kingc8ebae32011-01-11 19:35:53 +0000262/*
263 * All the DMA operation mode stuff goes inside this ifdef.
264 * This assumes that you have a generic DMA device interface,
265 * no custom DMA interfaces are supported.
266 */
267#ifdef CONFIG_DMA_ENGINE
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500268static void mmci_dma_setup(struct mmci_host *host)
Russell Kingc8ebae32011-01-11 19:35:53 +0000269{
270 struct mmci_platform_data *plat = host->plat;
271 const char *rxname, *txname;
272 dma_cap_mask_t mask;
273
274 if (!plat || !plat->dma_filter) {
275 dev_info(mmc_dev(host->mmc), "no DMA platform data\n");
276 return;
277 }
278
Per Forlin58c7ccb2011-07-01 18:55:24 +0200279 /* initialize pre request cookie */
280 host->next_data.cookie = 1;
281
Russell Kingc8ebae32011-01-11 19:35:53 +0000282 /* Try to acquire a generic DMA engine slave channel */
283 dma_cap_zero(mask);
284 dma_cap_set(DMA_SLAVE, mask);
285
286 /*
287 * If only an RX channel is specified, the driver will
288 * attempt to use it bidirectionally, however if it is
289 * is specified but cannot be located, DMA will be disabled.
290 */
291 if (plat->dma_rx_param) {
292 host->dma_rx_channel = dma_request_channel(mask,
293 plat->dma_filter,
294 plat->dma_rx_param);
295 /* E.g if no DMA hardware is present */
296 if (!host->dma_rx_channel)
297 dev_err(mmc_dev(host->mmc), "no RX DMA channel\n");
298 }
299
300 if (plat->dma_tx_param) {
301 host->dma_tx_channel = dma_request_channel(mask,
302 plat->dma_filter,
303 plat->dma_tx_param);
304 if (!host->dma_tx_channel)
305 dev_warn(mmc_dev(host->mmc), "no TX DMA channel\n");
306 } else {
307 host->dma_tx_channel = host->dma_rx_channel;
308 }
309
310 if (host->dma_rx_channel)
311 rxname = dma_chan_name(host->dma_rx_channel);
312 else
313 rxname = "none";
314
315 if (host->dma_tx_channel)
316 txname = dma_chan_name(host->dma_tx_channel);
317 else
318 txname = "none";
319
320 dev_info(mmc_dev(host->mmc), "DMA channels RX %s, TX %s\n",
321 rxname, txname);
322
323 /*
324 * Limit the maximum segment size in any SG entry according to
325 * the parameters of the DMA engine device.
326 */
327 if (host->dma_tx_channel) {
328 struct device *dev = host->dma_tx_channel->device->dev;
329 unsigned int max_seg_size = dma_get_max_seg_size(dev);
330
331 if (max_seg_size < host->mmc->max_seg_size)
332 host->mmc->max_seg_size = max_seg_size;
333 }
334 if (host->dma_rx_channel) {
335 struct device *dev = host->dma_rx_channel->device->dev;
336 unsigned int max_seg_size = dma_get_max_seg_size(dev);
337
338 if (max_seg_size < host->mmc->max_seg_size)
339 host->mmc->max_seg_size = max_seg_size;
340 }
341}
342
343/*
Bill Pemberton6e0ee712012-11-19 13:26:03 -0500344 * This is used in or so inline it
Russell Kingc8ebae32011-01-11 19:35:53 +0000345 * so it can be discarded.
346 */
347static inline void mmci_dma_release(struct mmci_host *host)
348{
349 struct mmci_platform_data *plat = host->plat;
350
351 if (host->dma_rx_channel)
352 dma_release_channel(host->dma_rx_channel);
353 if (host->dma_tx_channel && plat->dma_tx_param)
354 dma_release_channel(host->dma_tx_channel);
355 host->dma_rx_channel = host->dma_tx_channel = NULL;
356}
357
358static void mmci_dma_unmap(struct mmci_host *host, struct mmc_data *data)
359{
360 struct dma_chan *chan = host->dma_current;
361 enum dma_data_direction dir;
362 u32 status;
363 int i;
364
365 /* Wait up to 1ms for the DMA to complete */
366 for (i = 0; ; i++) {
367 status = readl(host->base + MMCISTATUS);
368 if (!(status & MCI_RXDATAAVLBLMASK) || i >= 100)
369 break;
370 udelay(10);
371 }
372
373 /*
374 * Check to see whether we still have some data left in the FIFO -
375 * this catches DMA controllers which are unable to monitor the
376 * DMALBREQ and DMALSREQ signals while allowing us to DMA to non-
377 * contiguous buffers. On TX, we'll get a FIFO underrun error.
378 */
379 if (status & MCI_RXDATAAVLBLMASK) {
380 dmaengine_terminate_all(chan);
381 if (!data->error)
382 data->error = -EIO;
383 }
384
385 if (data->flags & MMC_DATA_WRITE) {
386 dir = DMA_TO_DEVICE;
387 } else {
388 dir = DMA_FROM_DEVICE;
389 }
390
Per Forlin58c7ccb2011-07-01 18:55:24 +0200391 if (!data->host_cookie)
392 dma_unmap_sg(chan->device->dev, data->sg, data->sg_len, dir);
Russell Kingc8ebae32011-01-11 19:35:53 +0000393
394 /*
395 * Use of DMA with scatter-gather is impossible.
396 * Give up with DMA and switch back to PIO mode.
397 */
398 if (status & MCI_RXDATAAVLBLMASK) {
399 dev_err(mmc_dev(host->mmc), "buggy DMA detected. Taking evasive action.\n");
400 mmci_dma_release(host);
401 }
402}
403
404static void mmci_dma_data_error(struct mmci_host *host)
405{
406 dev_err(mmc_dev(host->mmc), "error during DMA transfer!\n");
407 dmaengine_terminate_all(host->dma_current);
408}
409
Per Forlin58c7ccb2011-07-01 18:55:24 +0200410static int mmci_dma_prep_data(struct mmci_host *host, struct mmc_data *data,
411 struct mmci_host_next *next)
Russell Kingc8ebae32011-01-11 19:35:53 +0000412{
413 struct variant_data *variant = host->variant;
414 struct dma_slave_config conf = {
415 .src_addr = host->phybase + MMCIFIFO,
416 .dst_addr = host->phybase + MMCIFIFO,
417 .src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
418 .dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
419 .src_maxburst = variant->fifohalfsize >> 2, /* # of words */
420 .dst_maxburst = variant->fifohalfsize >> 2, /* # of words */
Viresh Kumar258aea72012-02-01 16:12:19 +0530421 .device_fc = false,
Russell Kingc8ebae32011-01-11 19:35:53 +0000422 };
Russell Kingc8ebae32011-01-11 19:35:53 +0000423 struct dma_chan *chan;
424 struct dma_device *device;
425 struct dma_async_tx_descriptor *desc;
Vinod Koul05f57992011-10-14 10:45:11 +0530426 enum dma_data_direction buffer_dirn;
Russell Kingc8ebae32011-01-11 19:35:53 +0000427 int nr_sg;
428
Per Forlin58c7ccb2011-07-01 18:55:24 +0200429 /* Check if next job is already prepared */
430 if (data->host_cookie && !next &&
431 host->dma_current && host->dma_desc_current)
432 return 0;
433
434 if (!next) {
435 host->dma_current = NULL;
436 host->dma_desc_current = NULL;
437 }
Russell Kingc8ebae32011-01-11 19:35:53 +0000438
439 if (data->flags & MMC_DATA_READ) {
Vinod Koul05f57992011-10-14 10:45:11 +0530440 conf.direction = DMA_DEV_TO_MEM;
441 buffer_dirn = DMA_FROM_DEVICE;
Russell Kingc8ebae32011-01-11 19:35:53 +0000442 chan = host->dma_rx_channel;
443 } else {
Vinod Koul05f57992011-10-14 10:45:11 +0530444 conf.direction = DMA_MEM_TO_DEV;
445 buffer_dirn = DMA_TO_DEVICE;
Russell Kingc8ebae32011-01-11 19:35:53 +0000446 chan = host->dma_tx_channel;
447 }
448
449 /* If there's no DMA channel, fall back to PIO */
450 if (!chan)
451 return -EINVAL;
452
453 /* If less than or equal to the fifo size, don't bother with DMA */
Per Forlin58c7ccb2011-07-01 18:55:24 +0200454 if (data->blksz * data->blocks <= variant->fifosize)
Russell Kingc8ebae32011-01-11 19:35:53 +0000455 return -EINVAL;
456
457 device = chan->device;
Vinod Koul05f57992011-10-14 10:45:11 +0530458 nr_sg = dma_map_sg(device->dev, data->sg, data->sg_len, buffer_dirn);
Russell Kingc8ebae32011-01-11 19:35:53 +0000459 if (nr_sg == 0)
460 return -EINVAL;
461
462 dmaengine_slave_config(chan, &conf);
Alexandre Bounine16052822012-03-08 16:11:18 -0500463 desc = dmaengine_prep_slave_sg(chan, data->sg, nr_sg,
Russell Kingc8ebae32011-01-11 19:35:53 +0000464 conf.direction, DMA_CTRL_ACK);
465 if (!desc)
466 goto unmap_exit;
467
Per Forlin58c7ccb2011-07-01 18:55:24 +0200468 if (next) {
469 next->dma_chan = chan;
470 next->dma_desc = desc;
471 } else {
472 host->dma_current = chan;
473 host->dma_desc_current = desc;
474 }
Russell Kingc8ebae32011-01-11 19:35:53 +0000475
Per Forlin58c7ccb2011-07-01 18:55:24 +0200476 return 0;
477
478 unmap_exit:
479 if (!next)
480 dmaengine_terminate_all(chan);
Vinod Koul05f57992011-10-14 10:45:11 +0530481 dma_unmap_sg(device->dev, data->sg, data->sg_len, buffer_dirn);
Per Forlin58c7ccb2011-07-01 18:55:24 +0200482 return -ENOMEM;
483}
484
485static int mmci_dma_start_data(struct mmci_host *host, unsigned int datactrl)
486{
487 int ret;
488 struct mmc_data *data = host->data;
489
490 ret = mmci_dma_prep_data(host, host->data, NULL);
491 if (ret)
492 return ret;
493
494 /* Okay, go for it. */
Russell Kingc8ebae32011-01-11 19:35:53 +0000495 dev_vdbg(mmc_dev(host->mmc),
496 "Submit MMCI DMA job, sglen %d blksz %04x blks %04x flags %08x\n",
497 data->sg_len, data->blksz, data->blocks, data->flags);
Per Forlin58c7ccb2011-07-01 18:55:24 +0200498 dmaengine_submit(host->dma_desc_current);
499 dma_async_issue_pending(host->dma_current);
Russell Kingc8ebae32011-01-11 19:35:53 +0000500
501 datactrl |= MCI_DPSM_DMAENABLE;
502
503 /* Trigger the DMA transfer */
504 writel(datactrl, host->base + MMCIDATACTRL);
505
506 /*
507 * Let the MMCI say when the data is ended and it's time
508 * to fire next DMA request. When that happens, MMCI will
509 * call mmci_data_end()
510 */
511 writel(readl(host->base + MMCIMASK0) | MCI_DATAENDMASK,
512 host->base + MMCIMASK0);
513 return 0;
Russell Kingc8ebae32011-01-11 19:35:53 +0000514}
Per Forlin58c7ccb2011-07-01 18:55:24 +0200515
516static void mmci_get_next_data(struct mmci_host *host, struct mmc_data *data)
517{
518 struct mmci_host_next *next = &host->next_data;
519
520 if (data->host_cookie && data->host_cookie != next->cookie) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530521 pr_warning("[%s] invalid cookie: data->host_cookie %d"
Per Forlin58c7ccb2011-07-01 18:55:24 +0200522 " host->next_data.cookie %d\n",
523 __func__, data->host_cookie, host->next_data.cookie);
524 data->host_cookie = 0;
525 }
526
527 if (!data->host_cookie)
528 return;
529
530 host->dma_desc_current = next->dma_desc;
531 host->dma_current = next->dma_chan;
532
533 next->dma_desc = NULL;
534 next->dma_chan = NULL;
535}
536
537static void mmci_pre_request(struct mmc_host *mmc, struct mmc_request *mrq,
538 bool is_first_req)
539{
540 struct mmci_host *host = mmc_priv(mmc);
541 struct mmc_data *data = mrq->data;
542 struct mmci_host_next *nd = &host->next_data;
543
544 if (!data)
545 return;
546
547 if (data->host_cookie) {
548 data->host_cookie = 0;
549 return;
550 }
551
552 /* if config for dma */
553 if (((data->flags & MMC_DATA_WRITE) && host->dma_tx_channel) ||
554 ((data->flags & MMC_DATA_READ) && host->dma_rx_channel)) {
555 if (mmci_dma_prep_data(host, data, nd))
556 data->host_cookie = 0;
557 else
558 data->host_cookie = ++nd->cookie < 0 ? 1 : nd->cookie;
559 }
560}
561
562static void mmci_post_request(struct mmc_host *mmc, struct mmc_request *mrq,
563 int err)
564{
565 struct mmci_host *host = mmc_priv(mmc);
566 struct mmc_data *data = mrq->data;
567 struct dma_chan *chan;
568 enum dma_data_direction dir;
569
570 if (!data)
571 return;
572
573 if (data->flags & MMC_DATA_READ) {
574 dir = DMA_FROM_DEVICE;
575 chan = host->dma_rx_channel;
576 } else {
577 dir = DMA_TO_DEVICE;
578 chan = host->dma_tx_channel;
579 }
580
581
582 /* if config for dma */
583 if (chan) {
584 if (err)
585 dmaengine_terminate_all(chan);
Per Forlin8e3336b2011-08-29 15:35:59 +0200586 if (data->host_cookie)
Per Forlin58c7ccb2011-07-01 18:55:24 +0200587 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
588 data->sg_len, dir);
589 mrq->data->host_cookie = 0;
590 }
591}
592
Russell Kingc8ebae32011-01-11 19:35:53 +0000593#else
594/* Blank functions if the DMA engine is not available */
Per Forlin58c7ccb2011-07-01 18:55:24 +0200595static void mmci_get_next_data(struct mmci_host *host, struct mmc_data *data)
596{
597}
Russell Kingc8ebae32011-01-11 19:35:53 +0000598static inline void mmci_dma_setup(struct mmci_host *host)
599{
600}
601
602static inline void mmci_dma_release(struct mmci_host *host)
603{
604}
605
606static inline void mmci_dma_unmap(struct mmci_host *host, struct mmc_data *data)
607{
608}
609
610static inline void mmci_dma_data_error(struct mmci_host *host)
611{
612}
613
614static inline int mmci_dma_start_data(struct mmci_host *host, unsigned int datactrl)
615{
616 return -ENOSYS;
617}
Per Forlin58c7ccb2011-07-01 18:55:24 +0200618
619#define mmci_pre_request NULL
620#define mmci_post_request NULL
621
Russell Kingc8ebae32011-01-11 19:35:53 +0000622#endif
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624static void mmci_start_data(struct mmci_host *host, struct mmc_data *data)
625{
Rabin Vincent8301bb62010-08-09 12:57:30 +0100626 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 unsigned int datactrl, timeout, irqmask;
Russell King7b09cda2005-07-01 12:02:59 +0100628 unsigned long long clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 void __iomem *base;
Russell King3bc87f22006-08-27 13:51:28 +0100630 int blksz_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Linus Walleij64de0282010-02-19 01:09:10 +0100632 dev_dbg(mmc_dev(host->mmc), "blksz %04x blks %04x flags %08x\n",
633 data->blksz, data->blocks, data->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 host->data = data;
Rabin Vincent528320d2010-07-21 12:49:49 +0100636 host->size = data->blksz * data->blocks;
Russell King51d43752011-01-27 10:56:52 +0000637 data->bytes_xfered = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Russell King7b09cda2005-07-01 12:02:59 +0100639 clks = (unsigned long long)data->timeout_ns * host->cclk;
640 do_div(clks, 1000000000UL);
641
642 timeout = data->timeout_clks + (unsigned int)clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 base = host->base;
645 writel(timeout, base + MMCIDATATIMER);
646 writel(host->size, base + MMCIDATALENGTH);
647
Russell King3bc87f22006-08-27 13:51:28 +0100648 blksz_bits = ffs(data->blksz) - 1;
649 BUG_ON(1 << blksz_bits != data->blksz);
650
Philippe Langlais1784b152011-03-25 08:51:52 +0100651 if (variant->blksz_datactrl16)
652 datactrl = MCI_DPSM_ENABLE | (data->blksz << 16);
653 else
654 datactrl = MCI_DPSM_ENABLE | blksz_bits << 4;
Russell Kingc8ebae32011-01-11 19:35:53 +0000655
656 if (data->flags & MMC_DATA_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 datactrl |= MCI_DPSM_DIRECTION;
Russell Kingc8ebae32011-01-11 19:35:53 +0000658
Ulf Hansson7258db72011-12-13 17:05:28 +0100659 /* The ST Micro variants has a special bit to enable SDIO */
660 if (variant->sdio && host->mmc->card)
Ulf Hansson06c1a122012-10-12 14:01:50 +0100661 if (mmc_card_sdio(host->mmc->card)) {
662 /*
663 * The ST Micro variants has a special bit
664 * to enable SDIO.
665 */
666 u32 clk;
667
Ulf Hansson7258db72011-12-13 17:05:28 +0100668 datactrl |= MCI_ST_DPSM_SDIOEN;
669
Ulf Hansson06c1a122012-10-12 14:01:50 +0100670 /*
Ulf Hansson70ac0932012-10-12 14:07:36 +0100671 * The ST Micro variant for SDIO small write transfers
672 * needs to have clock H/W flow control disabled,
673 * otherwise the transfer will not start. The threshold
674 * depends on the rate of MCLK.
Ulf Hansson06c1a122012-10-12 14:01:50 +0100675 */
Ulf Hansson70ac0932012-10-12 14:07:36 +0100676 if (data->flags & MMC_DATA_WRITE &&
677 (host->size < 8 ||
678 (host->size <= 8 && host->mclk > 50000000)))
Ulf Hansson06c1a122012-10-12 14:01:50 +0100679 clk = host->clk_reg & ~variant->clkreg_enable;
680 else
681 clk = host->clk_reg | variant->clkreg_enable;
682
683 mmci_write_clkreg(host, clk);
684 }
685
Ulf Hansson6dbb6ee2013-01-07 15:30:44 +0100686 if (host->mmc->ios.timing == MMC_TIMING_UHS_DDR50)
687 datactrl |= MCI_ST_DPSM_DDRMODE;
688
Russell Kingc8ebae32011-01-11 19:35:53 +0000689 /*
690 * Attempt to use DMA operation mode, if this
691 * should fail, fall back to PIO mode
692 */
693 if (!mmci_dma_start_data(host, datactrl))
694 return;
695
696 /* IRQ mode, map the SG list for CPU reading/writing */
697 mmci_init_sg(host, data);
698
699 if (data->flags & MMC_DATA_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 irqmask = MCI_RXFIFOHALFFULLMASK;
Russell King0425a142006-02-16 16:48:31 +0000701
702 /*
Russell Kingc4d877c2011-01-27 09:50:13 +0000703 * If we have less than the fifo 'half-full' threshold to
704 * transfer, trigger a PIO interrupt as soon as any data
705 * is available.
Russell King0425a142006-02-16 16:48:31 +0000706 */
Russell Kingc4d877c2011-01-27 09:50:13 +0000707 if (host->size < variant->fifohalfsize)
Russell King0425a142006-02-16 16:48:31 +0000708 irqmask |= MCI_RXDATAAVLBLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 } else {
710 /*
711 * We don't actually need to include "FIFO empty" here
712 * since its implicit in "FIFO half empty".
713 */
714 irqmask = MCI_TXFIFOHALFEMPTYMASK;
715 }
716
717 writel(datactrl, base + MMCIDATACTRL);
718 writel(readl(base + MMCIMASK0) & ~MCI_DATAENDMASK, base + MMCIMASK0);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100719 mmci_set_mask1(host, irqmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}
721
722static void
723mmci_start_command(struct mmci_host *host, struct mmc_command *cmd, u32 c)
724{
725 void __iomem *base = host->base;
726
Linus Walleij64de0282010-02-19 01:09:10 +0100727 dev_dbg(mmc_dev(host->mmc), "op %02x arg %08x flags %08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 cmd->opcode, cmd->arg, cmd->flags);
729
730 if (readl(base + MMCICOMMAND) & MCI_CPSM_ENABLE) {
731 writel(0, base + MMCICOMMAND);
732 udelay(1);
733 }
734
735 c |= cmd->opcode | MCI_CPSM_ENABLE;
Russell Kinge9225172006-02-02 12:23:12 +0000736 if (cmd->flags & MMC_RSP_PRESENT) {
737 if (cmd->flags & MMC_RSP_136)
738 c |= MCI_CPSM_LONGRSP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 c |= MCI_CPSM_RESPONSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 }
741 if (/*interrupt*/0)
742 c |= MCI_CPSM_INTERRUPT;
743
744 host->cmd = cmd;
745
746 writel(cmd->arg, base + MMCIARGUMENT);
747 writel(c, base + MMCICOMMAND);
748}
749
750static void
751mmci_data_irq(struct mmci_host *host, struct mmc_data *data,
752 unsigned int status)
753{
Linus Walleijf20f8f22010-10-19 13:41:24 +0100754 /* First check for errors */
Ulf Hanssonb63038d2011-12-13 16:51:04 +0100755 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_STARTBITERR|
756 MCI_TXUNDERRUN|MCI_RXOVERRUN)) {
Linus Walleij8cb28152011-01-24 15:22:13 +0100757 u32 remain, success;
Linus Walleijf20f8f22010-10-19 13:41:24 +0100758
Russell Kingc8ebae32011-01-11 19:35:53 +0000759 /* Terminate the DMA transfer */
760 if (dma_inprogress(host))
761 mmci_dma_data_error(host);
762
Russell Kingc8afc9d2011-02-04 09:19:46 +0000763 /*
764 * Calculate how far we are into the transfer. Note that
765 * the data counter gives the number of bytes transferred
766 * on the MMC bus, not on the host side. On reads, this
767 * can be as much as a FIFO-worth of data ahead. This
768 * matters for FIFO overruns only.
769 */
Linus Walleijf5a106d2011-01-27 17:44:34 +0100770 remain = readl(host->base + MMCIDATACNT);
Linus Walleij8cb28152011-01-24 15:22:13 +0100771 success = data->blksz * data->blocks - remain;
772
Russell Kingc8afc9d2011-02-04 09:19:46 +0000773 dev_dbg(mmc_dev(host->mmc), "MCI ERROR IRQ, status 0x%08x at 0x%08x\n",
774 status, success);
Linus Walleij8cb28152011-01-24 15:22:13 +0100775 if (status & MCI_DATACRCFAIL) {
776 /* Last block was not successful */
Russell Kingc8afc9d2011-02-04 09:19:46 +0000777 success -= 1;
Pierre Ossman17b04292007-07-22 22:18:46 +0200778 data->error = -EILSEQ;
Linus Walleij8cb28152011-01-24 15:22:13 +0100779 } else if (status & MCI_DATATIMEOUT) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200780 data->error = -ETIMEDOUT;
Linus Walleij757df742011-06-30 15:10:21 +0100781 } else if (status & MCI_STARTBITERR) {
782 data->error = -ECOMM;
Russell Kingc8afc9d2011-02-04 09:19:46 +0000783 } else if (status & MCI_TXUNDERRUN) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200784 data->error = -EIO;
Russell Kingc8afc9d2011-02-04 09:19:46 +0000785 } else if (status & MCI_RXOVERRUN) {
786 if (success > host->variant->fifosize)
787 success -= host->variant->fifosize;
788 else
789 success = 0;
Linus Walleij8cb28152011-01-24 15:22:13 +0100790 data->error = -EIO;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100791 }
Russell King51d43752011-01-27 10:56:52 +0000792 data->bytes_xfered = round_down(success, data->blksz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 }
Linus Walleijf20f8f22010-10-19 13:41:24 +0100794
Linus Walleij8cb28152011-01-24 15:22:13 +0100795 if (status & MCI_DATABLOCKEND)
796 dev_err(mmc_dev(host->mmc), "stray MCI_DATABLOCKEND interrupt\n");
Linus Walleijf20f8f22010-10-19 13:41:24 +0100797
Russell Kingccff9b52011-01-30 21:03:50 +0000798 if (status & MCI_DATAEND || data->error) {
Russell Kingc8ebae32011-01-11 19:35:53 +0000799 if (dma_inprogress(host))
800 mmci_dma_unmap(host, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 mmci_stop_data(host);
802
Linus Walleij8cb28152011-01-24 15:22:13 +0100803 if (!data->error)
804 /* The error clause is handled above, success! */
Russell King51d43752011-01-27 10:56:52 +0000805 data->bytes_xfered = data->blksz * data->blocks;
Linus Walleijf20f8f22010-10-19 13:41:24 +0100806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 if (!data->stop) {
808 mmci_request_end(host, data->mrq);
809 } else {
810 mmci_start_command(host, data->stop, 0);
811 }
812 }
813}
814
815static void
816mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
817 unsigned int status)
818{
819 void __iomem *base = host->base;
820
821 host->cmd = NULL;
822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 if (status & MCI_CMDTIMEOUT) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200824 cmd->error = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200826 cmd->error = -EILSEQ;
Russell King - ARM Linux9047b432011-01-11 16:35:56 +0000827 } else {
828 cmd->resp[0] = readl(base + MMCIRESPONSE0);
829 cmd->resp[1] = readl(base + MMCIRESPONSE1);
830 cmd->resp[2] = readl(base + MMCIRESPONSE2);
831 cmd->resp[3] = readl(base + MMCIRESPONSE3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
833
Pierre Ossman17b04292007-07-22 22:18:46 +0200834 if (!cmd->data || cmd->error) {
Ulf Hansson3b6e3c72011-12-13 16:58:43 +0100835 if (host->data) {
836 /* Terminate the DMA transfer */
837 if (dma_inprogress(host))
838 mmci_dma_data_error(host);
Russell Kinge47c2222007-01-08 16:42:51 +0000839 mmci_stop_data(host);
Ulf Hansson3b6e3c72011-12-13 16:58:43 +0100840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 mmci_request_end(host, cmd->mrq);
842 } else if (!(cmd->data->flags & MMC_DATA_READ)) {
843 mmci_start_data(host, cmd->data);
844 }
845}
846
847static int mmci_pio_read(struct mmci_host *host, char *buffer, unsigned int remain)
848{
849 void __iomem *base = host->base;
850 char *ptr = buffer;
851 u32 status;
Linus Walleij26eed9a2008-04-26 23:39:44 +0100852 int host_remain = host->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 do {
Linus Walleij26eed9a2008-04-26 23:39:44 +0100855 int count = host_remain - (readl(base + MMCIFIFOCNT) << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857 if (count > remain)
858 count = remain;
859
860 if (count <= 0)
861 break;
862
Ulf Hansson393e5e22011-12-13 17:08:04 +0100863 /*
864 * SDIO especially may want to send something that is
865 * not divisible by 4 (as opposed to card sectors
866 * etc). Therefore make sure to always read the last bytes
867 * while only doing full 32-bit reads towards the FIFO.
868 */
869 if (unlikely(count & 0x3)) {
870 if (count < 4) {
871 unsigned char buf[4];
Davide Ciminaghi4b85da02012-12-10 14:47:21 +0100872 ioread32_rep(base + MMCIFIFO, buf, 1);
Ulf Hansson393e5e22011-12-13 17:08:04 +0100873 memcpy(ptr, buf, count);
874 } else {
Davide Ciminaghi4b85da02012-12-10 14:47:21 +0100875 ioread32_rep(base + MMCIFIFO, ptr, count >> 2);
Ulf Hansson393e5e22011-12-13 17:08:04 +0100876 count &= ~0x3;
877 }
878 } else {
Davide Ciminaghi4b85da02012-12-10 14:47:21 +0100879 ioread32_rep(base + MMCIFIFO, ptr, count >> 2);
Ulf Hansson393e5e22011-12-13 17:08:04 +0100880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 ptr += count;
883 remain -= count;
Linus Walleij26eed9a2008-04-26 23:39:44 +0100884 host_remain -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886 if (remain == 0)
887 break;
888
889 status = readl(base + MMCISTATUS);
890 } while (status & MCI_RXDATAAVLBL);
891
892 return ptr - buffer;
893}
894
895static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status)
896{
Rabin Vincent8301bb62010-08-09 12:57:30 +0100897 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 void __iomem *base = host->base;
899 char *ptr = buffer;
900
901 do {
902 unsigned int count, maxcnt;
903
Rabin Vincent8301bb62010-08-09 12:57:30 +0100904 maxcnt = status & MCI_TXFIFOEMPTY ?
905 variant->fifosize : variant->fifohalfsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 count = min(remain, maxcnt);
907
Linus Walleij34177802010-10-19 12:43:58 +0100908 /*
Linus Walleij34177802010-10-19 12:43:58 +0100909 * SDIO especially may want to send something that is
910 * not divisible by 4 (as opposed to card sectors
911 * etc), and the FIFO only accept full 32-bit writes.
912 * So compensate by adding +3 on the count, a single
913 * byte become a 32bit write, 7 bytes will be two
914 * 32bit writes etc.
915 */
Davide Ciminaghi4b85da02012-12-10 14:47:21 +0100916 iowrite32_rep(base + MMCIFIFO, ptr, (count + 3) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 ptr += count;
919 remain -= count;
920
921 if (remain == 0)
922 break;
923
924 status = readl(base + MMCISTATUS);
925 } while (status & MCI_TXFIFOHALFEMPTY);
926
927 return ptr - buffer;
928}
929
930/*
931 * PIO data transfer IRQ handler.
932 */
David Howells7d12e782006-10-05 14:55:46 +0100933static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
935 struct mmci_host *host = dev_id;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100936 struct sg_mapping_iter *sg_miter = &host->sg_miter;
Rabin Vincent8301bb62010-08-09 12:57:30 +0100937 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 void __iomem *base = host->base;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100939 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 u32 status;
941
942 status = readl(base + MMCISTATUS);
943
Linus Walleij64de0282010-02-19 01:09:10 +0100944 dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100946 local_irq_save(flags);
947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 unsigned int remain, len;
950 char *buffer;
951
952 /*
953 * For write, we only need to test the half-empty flag
954 * here - if the FIFO is completely empty, then by
955 * definition it is more than half empty.
956 *
957 * For read, check for data available.
958 */
959 if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL)))
960 break;
961
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100962 if (!sg_miter_next(sg_miter))
963 break;
964
965 buffer = sg_miter->addr;
966 remain = sg_miter->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 len = 0;
969 if (status & MCI_RXACTIVE)
970 len = mmci_pio_read(host, buffer, remain);
971 if (status & MCI_TXACTIVE)
972 len = mmci_pio_write(host, buffer, remain, status);
973
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100974 sg_miter->consumed = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 host->size -= len;
977 remain -= len;
978
979 if (remain)
980 break;
981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 status = readl(base + MMCISTATUS);
983 } while (1);
984
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100985 sg_miter_stop(sg_miter);
986
987 local_irq_restore(flags);
988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 /*
Russell Kingc4d877c2011-01-27 09:50:13 +0000990 * If we have less than the fifo 'half-full' threshold to transfer,
991 * trigger a PIO interrupt as soon as any data is available.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 */
Russell Kingc4d877c2011-01-27 09:50:13 +0000993 if (status & MCI_RXACTIVE && host->size < variant->fifohalfsize)
Linus Walleij2686b4b2010-10-19 12:39:48 +0100994 mmci_set_mask1(host, MCI_RXDATAAVLBLMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 /*
997 * If we run out of data, disable the data IRQs; this
998 * prevents a race where the FIFO becomes empty before
999 * the chip itself has disabled the data path, and
1000 * stops us racing with our data end IRQ.
1001 */
1002 if (host->size == 0) {
Linus Walleij2686b4b2010-10-19 12:39:48 +01001003 mmci_set_mask1(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0);
1005 }
1006
1007 return IRQ_HANDLED;
1008}
1009
1010/*
1011 * Handle completion of command and data transfers.
1012 */
David Howells7d12e782006-10-05 14:55:46 +01001013static irqreturn_t mmci_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
1015 struct mmci_host *host = dev_id;
1016 u32 status;
1017 int ret = 0;
1018
1019 spin_lock(&host->lock);
1020
1021 do {
1022 struct mmc_command *cmd;
1023 struct mmc_data *data;
1024
1025 status = readl(host->base + MMCISTATUS);
Linus Walleij2686b4b2010-10-19 12:39:48 +01001026
1027 if (host->singleirq) {
1028 if (status & readl(host->base + MMCIMASK1))
1029 mmci_pio_irq(irq, dev_id);
1030
1031 status &= ~MCI_IRQ1MASK;
1032 }
1033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 status &= readl(host->base + MMCIMASK0);
1035 writel(status, host->base + MMCICLEAR);
1036
Linus Walleij64de0282010-02-19 01:09:10 +01001037 dev_dbg(mmc_dev(host->mmc), "irq0 (data+cmd) %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039 data = host->data;
Ulf Hanssonb63038d2011-12-13 16:51:04 +01001040 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_STARTBITERR|
1041 MCI_TXUNDERRUN|MCI_RXOVERRUN|MCI_DATAEND|
1042 MCI_DATABLOCKEND) && data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 mmci_data_irq(host, data, status);
1044
1045 cmd = host->cmd;
1046 if (status & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT|MCI_CMDSENT|MCI_CMDRESPEND) && cmd)
1047 mmci_cmd_irq(host, cmd, status);
1048
1049 ret = 1;
1050 } while (status);
1051
1052 spin_unlock(&host->lock);
1053
1054 return IRQ_RETVAL(ret);
1055}
1056
1057static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1058{
1059 struct mmci_host *host = mmc_priv(mmc);
Linus Walleij9e943022008-10-24 21:17:50 +01001060 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 WARN_ON(host->mrq != NULL);
1063
Nicolas Pitre019a5f52007-10-11 01:06:03 -04001064 if (mrq->data && !is_power_of_2(mrq->data->blksz)) {
Linus Walleij64de0282010-02-19 01:09:10 +01001065 dev_err(mmc_dev(mmc), "unsupported block size (%d bytes)\n",
1066 mrq->data->blksz);
Pierre Ossman255d01a2007-07-24 20:38:53 +02001067 mrq->cmd->error = -EINVAL;
1068 mmc_request_done(mmc, mrq);
1069 return;
1070 }
1071
Russell King1c3be362011-08-14 09:17:05 +01001072 pm_runtime_get_sync(mmc_dev(mmc));
1073
Linus Walleij9e943022008-10-24 21:17:50 +01001074 spin_lock_irqsave(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076 host->mrq = mrq;
1077
Per Forlin58c7ccb2011-07-01 18:55:24 +02001078 if (mrq->data)
1079 mmci_get_next_data(host, mrq->data);
1080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 if (mrq->data && mrq->data->flags & MMC_DATA_READ)
1082 mmci_start_data(host, mrq->data);
1083
1084 mmci_start_command(host, mrq->cmd, 0);
1085
Linus Walleij9e943022008-10-24 21:17:50 +01001086 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087}
1088
1089static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1090{
1091 struct mmci_host *host = mmc_priv(mmc);
Ulf Hansson7d72a1d2011-12-13 16:54:55 +01001092 struct variant_data *variant = host->variant;
Linus Walleija6a64642009-09-14 12:56:14 +01001093 u32 pwr = 0;
1094 unsigned long flags;
Linus Walleij99fc5132010-09-29 01:08:27 -04001095 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Ulf Hansson2cd976c2011-12-13 17:01:11 +01001097 pm_runtime_get_sync(mmc_dev(mmc));
1098
Ulf Hanssonbc521812011-12-13 16:57:55 +01001099 if (host->plat->ios_handler &&
1100 host->plat->ios_handler(mmc_dev(mmc), ios))
1101 dev_err(mmc_dev(mmc), "platform ios_handler failed\n");
1102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 switch (ios->power_mode) {
1104 case MMC_POWER_OFF:
Linus Walleij99fc5132010-09-29 01:08:27 -04001105 if (host->vcc)
1106 ret = mmc_regulator_set_ocr(mmc, host->vcc, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 break;
1108 case MMC_POWER_UP:
Linus Walleij99fc5132010-09-29 01:08:27 -04001109 if (host->vcc) {
1110 ret = mmc_regulator_set_ocr(mmc, host->vcc, ios->vdd);
1111 if (ret) {
1112 dev_err(mmc_dev(mmc), "unable to set OCR\n");
1113 /*
1114 * The .set_ios() function in the mmc_host_ops
1115 * struct return void, and failing to set the
1116 * power should be rare so we print an error
1117 * and return here.
1118 */
Ulf Hansson2cd976c2011-12-13 17:01:11 +01001119 goto out;
Linus Walleij99fc5132010-09-29 01:08:27 -04001120 }
1121 }
Ulf Hansson7d72a1d2011-12-13 16:54:55 +01001122 /*
1123 * The ST Micro variant doesn't have the PL180s MCI_PWR_UP
1124 * and instead uses MCI_PWR_ON so apply whatever value is
1125 * configured in the variant data.
1126 */
1127 pwr |= variant->pwrreg_powerup;
1128
1129 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 case MMC_POWER_ON:
1131 pwr |= MCI_PWR_ON;
1132 break;
1133 }
1134
Ulf Hansson4d1a3a02011-12-13 16:57:07 +01001135 if (variant->signal_direction && ios->power_mode != MMC_POWER_OFF) {
1136 /*
1137 * The ST Micro variant has some additional bits
1138 * indicating signal direction for the signals in
1139 * the SD/MMC bus and feedback-clock usage.
1140 */
1141 pwr |= host->plat->sigdir;
1142
1143 if (ios->bus_width == MMC_BUS_WIDTH_4)
1144 pwr &= ~MCI_ST_DATA74DIREN;
1145 else if (ios->bus_width == MMC_BUS_WIDTH_1)
1146 pwr &= (~MCI_ST_DATA74DIREN &
1147 ~MCI_ST_DATA31DIREN &
1148 ~MCI_ST_DATA2DIREN);
1149 }
1150
Linus Walleijcc30d602009-01-04 15:18:54 +01001151 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN) {
Linus Walleijf17a1f02009-08-04 01:01:02 +01001152 if (host->hw_designer != AMBA_VENDOR_ST)
Linus Walleijcc30d602009-01-04 15:18:54 +01001153 pwr |= MCI_ROD;
1154 else {
1155 /*
1156 * The ST Micro variant use the ROD bit for something
1157 * else and only has OD (Open Drain).
1158 */
1159 pwr |= MCI_OD;
1160 }
1161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Linus Walleija6a64642009-09-14 12:56:14 +01001163 spin_lock_irqsave(&host->lock, flags);
1164
1165 mmci_set_clkreg(host, ios->clock);
Ulf Hansson7437cfa2012-01-18 09:17:27 +01001166 mmci_write_pwrreg(host, pwr);
Linus Walleija6a64642009-09-14 12:56:14 +01001167
1168 spin_unlock_irqrestore(&host->lock, flags);
Ulf Hansson2cd976c2011-12-13 17:01:11 +01001169
1170 out:
1171 pm_runtime_mark_last_busy(mmc_dev(mmc));
1172 pm_runtime_put_autosuspend(mmc_dev(mmc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173}
1174
Russell King89001442009-07-09 15:16:07 +01001175static int mmci_get_ro(struct mmc_host *mmc)
1176{
1177 struct mmci_host *host = mmc_priv(mmc);
1178
1179 if (host->gpio_wp == -ENOSYS)
1180 return -ENOSYS;
1181
Linus Walleij18a063012010-09-12 12:56:44 +01001182 return gpio_get_value_cansleep(host->gpio_wp);
Russell King89001442009-07-09 15:16:07 +01001183}
1184
1185static int mmci_get_cd(struct mmc_host *mmc)
1186{
1187 struct mmci_host *host = mmc_priv(mmc);
Rabin Vincent29719442010-08-09 12:54:43 +01001188 struct mmci_platform_data *plat = host->plat;
Russell King89001442009-07-09 15:16:07 +01001189 unsigned int status;
1190
Rabin Vincent4b8caec2010-08-09 12:56:40 +01001191 if (host->gpio_cd == -ENOSYS) {
1192 if (!plat->status)
1193 return 1; /* Assume always present */
1194
Rabin Vincent29719442010-08-09 12:54:43 +01001195 status = plat->status(mmc_dev(host->mmc));
Rabin Vincent4b8caec2010-08-09 12:56:40 +01001196 } else
Linus Walleij18a063012010-09-12 12:56:44 +01001197 status = !!gpio_get_value_cansleep(host->gpio_cd)
1198 ^ plat->cd_invert;
Russell King89001442009-07-09 15:16:07 +01001199
Russell King74bc8092010-07-29 15:58:59 +01001200 /*
1201 * Use positive logic throughout - status is zero for no card,
1202 * non-zero for card inserted.
1203 */
1204 return status;
Russell King89001442009-07-09 15:16:07 +01001205}
1206
Rabin Vincent148b8b32010-08-09 12:55:48 +01001207static irqreturn_t mmci_cd_irq(int irq, void *dev_id)
1208{
1209 struct mmci_host *host = dev_id;
1210
1211 mmc_detect_change(host->mmc, msecs_to_jiffies(500));
1212
1213 return IRQ_HANDLED;
1214}
1215
David Brownellab7aefd2006-11-12 17:55:30 -08001216static const struct mmc_host_ops mmci_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 .request = mmci_request,
Per Forlin58c7ccb2011-07-01 18:55:24 +02001218 .pre_req = mmci_pre_request,
1219 .post_req = mmci_post_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 .set_ios = mmci_set_ios,
Russell King89001442009-07-09 15:16:07 +01001221 .get_ro = mmci_get_ro,
1222 .get_cd = mmci_get_cd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223};
1224
Lee Jones000bc9d2012-04-16 10:18:43 +01001225#ifdef CONFIG_OF
1226static void mmci_dt_populate_generic_pdata(struct device_node *np,
1227 struct mmci_platform_data *pdata)
1228{
1229 int bus_width = 0;
1230
Lee Jones9a597012012-04-12 16:51:13 +01001231 pdata->gpio_wp = of_get_named_gpio(np, "wp-gpios", 0);
Lee Jones9a597012012-04-12 16:51:13 +01001232 pdata->gpio_cd = of_get_named_gpio(np, "cd-gpios", 0);
Lee Jones000bc9d2012-04-16 10:18:43 +01001233
1234 if (of_get_property(np, "cd-inverted", NULL))
1235 pdata->cd_invert = true;
1236 else
1237 pdata->cd_invert = false;
1238
1239 of_property_read_u32(np, "max-frequency", &pdata->f_max);
1240 if (!pdata->f_max)
1241 pr_warn("%s has no 'max-frequency' property\n", np->full_name);
1242
1243 if (of_get_property(np, "mmc-cap-mmc-highspeed", NULL))
1244 pdata->capabilities |= MMC_CAP_MMC_HIGHSPEED;
1245 if (of_get_property(np, "mmc-cap-sd-highspeed", NULL))
1246 pdata->capabilities |= MMC_CAP_SD_HIGHSPEED;
1247
1248 of_property_read_u32(np, "bus-width", &bus_width);
1249 switch (bus_width) {
1250 case 0 :
1251 /* No bus-width supplied. */
1252 break;
1253 case 4 :
1254 pdata->capabilities |= MMC_CAP_4_BIT_DATA;
1255 break;
1256 case 8 :
1257 pdata->capabilities |= MMC_CAP_8_BIT_DATA;
1258 break;
1259 default :
1260 pr_warn("%s: Unsupported bus width\n", np->full_name);
1261 }
1262}
Lee Jonesc0a120a2012-05-08 13:59:38 +01001263#else
1264static void mmci_dt_populate_generic_pdata(struct device_node *np,
1265 struct mmci_platform_data *pdata)
1266{
1267 return;
1268}
Lee Jones000bc9d2012-04-16 10:18:43 +01001269#endif
1270
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001271static int mmci_probe(struct amba_device *dev,
Russell Kingaa25afa2011-02-19 15:55:00 +00001272 const struct amba_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
Linus Walleij6ef297f2009-09-22 14:29:36 +01001274 struct mmci_platform_data *plat = dev->dev.platform_data;
Lee Jones000bc9d2012-04-16 10:18:43 +01001275 struct device_node *np = dev->dev.of_node;
Rabin Vincent4956e102010-07-21 12:54:40 +01001276 struct variant_data *variant = id->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 struct mmci_host *host;
1278 struct mmc_host *mmc;
1279 int ret;
1280
Lee Jones000bc9d2012-04-16 10:18:43 +01001281 /* Must have platform data or Device Tree. */
1282 if (!plat && !np) {
1283 dev_err(&dev->dev, "No plat data or DT found\n");
1284 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 }
1286
Lee Jonesb9b52912012-06-12 10:49:51 +01001287 if (!plat) {
1288 plat = devm_kzalloc(&dev->dev, sizeof(*plat), GFP_KERNEL);
1289 if (!plat)
1290 return -ENOMEM;
1291 }
1292
Lee Jones000bc9d2012-04-16 10:18:43 +01001293 if (np)
1294 mmci_dt_populate_generic_pdata(np, plat);
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 ret = amba_request_regions(dev, DRIVER_NAME);
1297 if (ret)
1298 goto out;
1299
1300 mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
1301 if (!mmc) {
1302 ret = -ENOMEM;
1303 goto rel_regions;
1304 }
1305
1306 host = mmc_priv(mmc);
Rabin Vincent4ea580f2009-04-17 08:44:19 +05301307 host->mmc = mmc;
Russell King012b7d32009-07-09 15:13:56 +01001308
Russell King89001442009-07-09 15:16:07 +01001309 host->gpio_wp = -ENOSYS;
1310 host->gpio_cd = -ENOSYS;
Rabin Vincent148b8b32010-08-09 12:55:48 +01001311 host->gpio_cd_irq = -1;
Russell King89001442009-07-09 15:16:07 +01001312
Russell King012b7d32009-07-09 15:13:56 +01001313 host->hw_designer = amba_manf(dev);
1314 host->hw_revision = amba_rev(dev);
Linus Walleij64de0282010-02-19 01:09:10 +01001315 dev_dbg(mmc_dev(mmc), "designer ID = 0x%02x\n", host->hw_designer);
1316 dev_dbg(mmc_dev(mmc), "revision = 0x%01x\n", host->hw_revision);
Russell King012b7d32009-07-09 15:13:56 +01001317
Russell Kingee569c42008-11-30 17:38:14 +00001318 host->clk = clk_get(&dev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 if (IS_ERR(host->clk)) {
1320 ret = PTR_ERR(host->clk);
1321 host->clk = NULL;
1322 goto host_free;
1323 }
1324
Julia Lawallac940932012-08-26 16:00:59 +00001325 ret = clk_prepare_enable(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 if (ret)
Russell Kinga8d35842006-01-03 18:41:37 +00001327 goto clk_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329 host->plat = plat;
Rabin Vincent4956e102010-07-21 12:54:40 +01001330 host->variant = variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 host->mclk = clk_get_rate(host->clk);
Linus Walleijc8df9a52008-04-29 09:34:07 +01001332 /*
1333 * According to the spec, mclk is max 100 MHz,
1334 * so we try to adjust the clock down to this,
1335 * (if possible).
1336 */
1337 if (host->mclk > 100000000) {
1338 ret = clk_set_rate(host->clk, 100000000);
1339 if (ret < 0)
1340 goto clk_disable;
1341 host->mclk = clk_get_rate(host->clk);
Linus Walleij64de0282010-02-19 01:09:10 +01001342 dev_dbg(mmc_dev(mmc), "eventual mclk rate: %u Hz\n",
1343 host->mclk);
Linus Walleijc8df9a52008-04-29 09:34:07 +01001344 }
Russell Kingc8ebae32011-01-11 19:35:53 +00001345 host->phybase = dev->res.start;
Linus Walleijdc890c22009-06-07 23:27:31 +01001346 host->base = ioremap(dev->res.start, resource_size(&dev->res));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 if (!host->base) {
1348 ret = -ENOMEM;
1349 goto clk_disable;
1350 }
1351
1352 mmc->ops = &mmci_ops;
Linus Walleij7f294e42011-07-08 09:57:15 +01001353 /*
1354 * The ARM and ST versions of the block have slightly different
1355 * clock divider equations which means that the minimum divider
1356 * differs too.
1357 */
1358 if (variant->st_clkdiv)
1359 mmc->f_min = DIV_ROUND_UP(host->mclk, 257);
1360 else
1361 mmc->f_min = DIV_ROUND_UP(host->mclk, 512);
Linus Walleij808d97c2010-04-08 07:39:38 +01001362 /*
1363 * If the platform data supplies a maximum operating
1364 * frequency, this takes precedence. Else, we fall back
1365 * to using the module parameter, which has a (low)
1366 * default value in case it is not specified. Either
1367 * value must not exceed the clock rate into the block,
1368 * of course.
1369 */
1370 if (plat->f_max)
1371 mmc->f_max = min(host->mclk, plat->f_max);
1372 else
1373 mmc->f_max = min(host->mclk, fmax);
Linus Walleij64de0282010-02-19 01:09:10 +01001374 dev_dbg(mmc_dev(mmc), "clocking block at %u Hz\n", mmc->f_max);
1375
Linus Walleija9a83782012-10-29 14:39:30 +01001376 host->pinctrl = devm_pinctrl_get(&dev->dev);
1377 if (IS_ERR(host->pinctrl)) {
1378 ret = PTR_ERR(host->pinctrl);
1379 goto clk_disable;
1380 }
1381
1382 host->pins_default = pinctrl_lookup_state(host->pinctrl,
1383 PINCTRL_STATE_DEFAULT);
1384
1385 /* enable pins to be muxed in and configured */
1386 if (!IS_ERR(host->pins_default)) {
1387 ret = pinctrl_select_state(host->pinctrl, host->pins_default);
1388 if (ret)
1389 dev_warn(&dev->dev, "could not set default pins\n");
1390 } else
1391 dev_warn(&dev->dev, "could not get default pinstate\n");
1392
Linus Walleij34e84f32009-09-22 14:41:40 +01001393#ifdef CONFIG_REGULATOR
1394 /* If we're using the regulator framework, try to fetch a regulator */
1395 host->vcc = regulator_get(&dev->dev, "vmmc");
1396 if (IS_ERR(host->vcc))
1397 host->vcc = NULL;
1398 else {
1399 int mask = mmc_regulator_get_ocrmask(host->vcc);
1400
1401 if (mask < 0)
1402 dev_err(&dev->dev, "error getting OCR mask (%d)\n",
1403 mask);
1404 else {
1405 host->mmc->ocr_avail = (u32) mask;
1406 if (plat->ocr_mask)
1407 dev_warn(&dev->dev,
1408 "Provided ocr_mask/setpower will not be used "
1409 "(using regulator instead)\n");
1410 }
1411 }
1412#endif
1413 /* Fall back to platform data if no regulator is found */
1414 if (host->vcc == NULL)
1415 mmc->ocr_avail = plat->ocr_mask;
Linus Walleij9e6c82c2009-09-14 12:57:11 +01001416 mmc->caps = plat->capabilities;
Per Forlin5a092622011-11-14 12:02:28 +01001417 mmc->caps2 = plat->capabilities2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
1419 /*
1420 * We can do SGIO
1421 */
Martin K. Petersena36274e2010-09-10 01:33:59 -04001422 mmc->max_segs = NR_SG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
1424 /*
Rabin Vincent08458ef2010-07-21 12:55:59 +01001425 * Since only a certain number of bits are valid in the data length
1426 * register, we must ensure that we don't exceed 2^num-1 bytes in a
1427 * single request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 */
Rabin Vincent08458ef2010-07-21 12:55:59 +01001429 mmc->max_req_size = (1 << variant->datalength_bits) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
1431 /*
1432 * Set the maximum segment size. Since we aren't doing DMA
1433 * (yet) we are only limited by the data length register.
1434 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001435 mmc->max_seg_size = mmc->max_req_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001437 /*
1438 * Block size can be up to 2048 bytes, but must be a power of two.
1439 */
Will Deacon8f7f6b72012-02-24 11:25:21 +00001440 mmc->max_blk_size = 1 << 11;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001441
Pierre Ossman55db8902006-11-21 17:55:45 +01001442 /*
Will Deacon8f7f6b72012-02-24 11:25:21 +00001443 * Limit the number of blocks transferred so that we don't overflow
1444 * the maximum request size.
Pierre Ossman55db8902006-11-21 17:55:45 +01001445 */
Will Deacon8f7f6b72012-02-24 11:25:21 +00001446 mmc->max_blk_count = mmc->max_req_size >> 11;
Pierre Ossman55db8902006-11-21 17:55:45 +01001447
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 spin_lock_init(&host->lock);
1449
1450 writel(0, host->base + MMCIMASK0);
1451 writel(0, host->base + MMCIMASK1);
1452 writel(0xfff, host->base + MMCICLEAR);
1453
Roland Stigge2805b9a2012-06-17 21:14:27 +01001454 if (plat->gpio_cd == -EPROBE_DEFER) {
1455 ret = -EPROBE_DEFER;
1456 goto err_gpio_cd;
1457 }
Russell King89001442009-07-09 15:16:07 +01001458 if (gpio_is_valid(plat->gpio_cd)) {
1459 ret = gpio_request(plat->gpio_cd, DRIVER_NAME " (cd)");
1460 if (ret == 0)
1461 ret = gpio_direction_input(plat->gpio_cd);
1462 if (ret == 0)
1463 host->gpio_cd = plat->gpio_cd;
1464 else if (ret != -ENOSYS)
1465 goto err_gpio_cd;
Rabin Vincent148b8b32010-08-09 12:55:48 +01001466
Linus Walleij17ee0832011-05-05 17:23:10 +01001467 /*
1468 * A gpio pin that will detect cards when inserted and removed
1469 * will most likely want to trigger on the edges if it is
1470 * 0 when ejected and 1 when inserted (or mutatis mutandis
1471 * for the inverted case) so we request triggers on both
1472 * edges.
1473 */
Rabin Vincent148b8b32010-08-09 12:55:48 +01001474 ret = request_any_context_irq(gpio_to_irq(plat->gpio_cd),
Linus Walleij17ee0832011-05-05 17:23:10 +01001475 mmci_cd_irq,
1476 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
1477 DRIVER_NAME " (cd)", host);
Rabin Vincent148b8b32010-08-09 12:55:48 +01001478 if (ret >= 0)
1479 host->gpio_cd_irq = gpio_to_irq(plat->gpio_cd);
Russell King89001442009-07-09 15:16:07 +01001480 }
Roland Stigge2805b9a2012-06-17 21:14:27 +01001481 if (plat->gpio_wp == -EPROBE_DEFER) {
1482 ret = -EPROBE_DEFER;
1483 goto err_gpio_wp;
1484 }
Russell King89001442009-07-09 15:16:07 +01001485 if (gpio_is_valid(plat->gpio_wp)) {
1486 ret = gpio_request(plat->gpio_wp, DRIVER_NAME " (wp)");
1487 if (ret == 0)
1488 ret = gpio_direction_input(plat->gpio_wp);
1489 if (ret == 0)
1490 host->gpio_wp = plat->gpio_wp;
1491 else if (ret != -ENOSYS)
1492 goto err_gpio_wp;
1493 }
1494
Rabin Vincent4b8caec2010-08-09 12:56:40 +01001495 if ((host->plat->status || host->gpio_cd != -ENOSYS)
1496 && host->gpio_cd_irq < 0)
Rabin Vincent148b8b32010-08-09 12:55:48 +01001497 mmc->caps |= MMC_CAP_NEEDS_POLL;
1498
Thomas Gleixnerdace1452006-07-01 19:29:38 -07001499 ret = request_irq(dev->irq[0], mmci_irq, IRQF_SHARED, DRIVER_NAME " (cmd)", host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 if (ret)
1501 goto unmap;
1502
Russell Kingdfb85182012-05-03 11:33:15 +01001503 if (!dev->irq[1])
Linus Walleij2686b4b2010-10-19 12:39:48 +01001504 host->singleirq = true;
1505 else {
1506 ret = request_irq(dev->irq[1], mmci_pio_irq, IRQF_SHARED,
1507 DRIVER_NAME " (pio)", host);
1508 if (ret)
1509 goto irq0_free;
1510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Linus Walleij8cb28152011-01-24 15:22:13 +01001512 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
1514 amba_set_drvdata(dev, mmc);
1515
Russell Kingc8ebae32011-01-11 19:35:53 +00001516 dev_info(&dev->dev, "%s: PL%03x manf %x rev%u at 0x%08llx irq %d,%d (pio)\n",
1517 mmc_hostname(mmc), amba_part(dev), amba_manf(dev),
1518 amba_rev(dev), (unsigned long long)dev->res.start,
1519 dev->irq[0], dev->irq[1]);
1520
1521 mmci_dma_setup(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Ulf Hansson2cd976c2011-12-13 17:01:11 +01001523 pm_runtime_set_autosuspend_delay(&dev->dev, 50);
1524 pm_runtime_use_autosuspend(&dev->dev);
Russell King1c3be362011-08-14 09:17:05 +01001525 pm_runtime_put(&dev->dev);
1526
Russell King8c11a942010-12-28 19:40:40 +00001527 mmc_add_host(mmc);
1528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 return 0;
1530
1531 irq0_free:
1532 free_irq(dev->irq[0], host);
1533 unmap:
Russell King89001442009-07-09 15:16:07 +01001534 if (host->gpio_wp != -ENOSYS)
1535 gpio_free(host->gpio_wp);
1536 err_gpio_wp:
Rabin Vincent148b8b32010-08-09 12:55:48 +01001537 if (host->gpio_cd_irq >= 0)
1538 free_irq(host->gpio_cd_irq, host);
Russell King89001442009-07-09 15:16:07 +01001539 if (host->gpio_cd != -ENOSYS)
1540 gpio_free(host->gpio_cd);
1541 err_gpio_cd:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 iounmap(host->base);
1543 clk_disable:
Julia Lawallac940932012-08-26 16:00:59 +00001544 clk_disable_unprepare(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 clk_free:
1546 clk_put(host->clk);
1547 host_free:
1548 mmc_free_host(mmc);
1549 rel_regions:
1550 amba_release_regions(dev);
1551 out:
1552 return ret;
1553}
1554
Bill Pemberton6e0ee712012-11-19 13:26:03 -05001555static int mmci_remove(struct amba_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556{
1557 struct mmc_host *mmc = amba_get_drvdata(dev);
1558
1559 amba_set_drvdata(dev, NULL);
1560
1561 if (mmc) {
1562 struct mmci_host *host = mmc_priv(mmc);
1563
Russell King1c3be362011-08-14 09:17:05 +01001564 /*
1565 * Undo pm_runtime_put() in probe. We use the _sync
1566 * version here so that we can access the primecell.
1567 */
1568 pm_runtime_get_sync(&dev->dev);
1569
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 mmc_remove_host(mmc);
1571
1572 writel(0, host->base + MMCIMASK0);
1573 writel(0, host->base + MMCIMASK1);
1574
1575 writel(0, host->base + MMCICOMMAND);
1576 writel(0, host->base + MMCIDATACTRL);
1577
Russell Kingc8ebae32011-01-11 19:35:53 +00001578 mmci_dma_release(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 free_irq(dev->irq[0], host);
Linus Walleij2686b4b2010-10-19 12:39:48 +01001580 if (!host->singleirq)
1581 free_irq(dev->irq[1], host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Russell King89001442009-07-09 15:16:07 +01001583 if (host->gpio_wp != -ENOSYS)
1584 gpio_free(host->gpio_wp);
Rabin Vincent148b8b32010-08-09 12:55:48 +01001585 if (host->gpio_cd_irq >= 0)
1586 free_irq(host->gpio_cd_irq, host);
Russell King89001442009-07-09 15:16:07 +01001587 if (host->gpio_cd != -ENOSYS)
1588 gpio_free(host->gpio_cd);
1589
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 iounmap(host->base);
Julia Lawallac940932012-08-26 16:00:59 +00001591 clk_disable_unprepare(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 clk_put(host->clk);
1593
Linus Walleij99fc5132010-09-29 01:08:27 -04001594 if (host->vcc)
1595 mmc_regulator_set_ocr(mmc, host->vcc, 0);
Linus Walleij34e84f32009-09-22 14:41:40 +01001596 regulator_put(host->vcc);
1597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 mmc_free_host(mmc);
1599
1600 amba_release_regions(dev);
1601 }
1602
1603 return 0;
1604}
1605
Ulf Hansson48fa7002011-12-13 16:59:34 +01001606#ifdef CONFIG_SUSPEND
1607static int mmci_suspend(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608{
Ulf Hansson48fa7002011-12-13 16:59:34 +01001609 struct amba_device *adev = to_amba_device(dev);
1610 struct mmc_host *mmc = amba_get_drvdata(adev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 int ret = 0;
1612
1613 if (mmc) {
1614 struct mmci_host *host = mmc_priv(mmc);
1615
Matt Fleming1a13f8f2010-05-26 14:42:08 -07001616 ret = mmc_suspend_host(mmc);
Ulf Hansson2cd976c2011-12-13 17:01:11 +01001617 if (ret == 0) {
1618 pm_runtime_get_sync(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 writel(0, host->base + MMCIMASK0);
Ulf Hansson2cd976c2011-12-13 17:01:11 +01001620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 }
1622
1623 return ret;
1624}
1625
Ulf Hansson48fa7002011-12-13 16:59:34 +01001626static int mmci_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627{
Ulf Hansson48fa7002011-12-13 16:59:34 +01001628 struct amba_device *adev = to_amba_device(dev);
1629 struct mmc_host *mmc = amba_get_drvdata(adev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 int ret = 0;
1631
1632 if (mmc) {
1633 struct mmci_host *host = mmc_priv(mmc);
1634
1635 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
Ulf Hansson2cd976c2011-12-13 17:01:11 +01001636 pm_runtime_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
1638 ret = mmc_resume_host(mmc);
1639 }
1640
1641 return ret;
1642}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643#endif
1644
Ulf Hansson48fa7002011-12-13 16:59:34 +01001645static const struct dev_pm_ops mmci_dev_pm_ops = {
1646 SET_SYSTEM_SLEEP_PM_OPS(mmci_suspend, mmci_resume)
1647};
1648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649static struct amba_id mmci_ids[] = {
1650 {
1651 .id = 0x00041180,
Pawel Moll768fbc12011-03-11 17:18:07 +00001652 .mask = 0xff0fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001653 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 },
1655 {
Pawel Moll768fbc12011-03-11 17:18:07 +00001656 .id = 0x01041180,
1657 .mask = 0xff0fffff,
1658 .data = &variant_arm_extended_fifo,
1659 },
1660 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 .id = 0x00041181,
1662 .mask = 0x000fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001663 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 },
Linus Walleijcc30d602009-01-04 15:18:54 +01001665 /* ST Micro variants */
1666 {
1667 .id = 0x00180180,
1668 .mask = 0x00ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001669 .data = &variant_u300,
Linus Walleijcc30d602009-01-04 15:18:54 +01001670 },
1671 {
Linus Walleij34fd4212012-04-10 17:43:59 +01001672 .id = 0x10180180,
1673 .mask = 0xf0ffffff,
1674 .data = &variant_nomadik,
1675 },
1676 {
Linus Walleijcc30d602009-01-04 15:18:54 +01001677 .id = 0x00280180,
1678 .mask = 0x00ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001679 .data = &variant_u300,
1680 },
1681 {
1682 .id = 0x00480180,
Philippe Langlais1784b152011-03-25 08:51:52 +01001683 .mask = 0xf0ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001684 .data = &variant_ux500,
Linus Walleijcc30d602009-01-04 15:18:54 +01001685 },
Philippe Langlais1784b152011-03-25 08:51:52 +01001686 {
1687 .id = 0x10480180,
1688 .mask = 0xf0ffffff,
1689 .data = &variant_ux500v2,
1690 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 { 0, 0 },
1692};
1693
Dave Martin9f998352011-10-05 15:15:21 +01001694MODULE_DEVICE_TABLE(amba, mmci_ids);
1695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696static struct amba_driver mmci_driver = {
1697 .drv = {
1698 .name = DRIVER_NAME,
Ulf Hansson48fa7002011-12-13 16:59:34 +01001699 .pm = &mmci_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 },
1701 .probe = mmci_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05001702 .remove = mmci_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 .id_table = mmci_ids,
1704};
1705
viresh kumar9e5ed092012-03-15 10:40:38 +01001706module_amba_driver(mmci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708module_param(fmax, uint, 0444);
1709
1710MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
1711MODULE_LICENSE("GPL");