blob: 04efcdfeb5e140973826e3813004dfa6fb446df2 [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>
Ulf Hansson70be2082013-01-07 15:35:06 +010023#include <linux/mmc/pm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/mmc/host.h>
Linus Walleij34177802010-10-19 12:43:58 +010025#include <linux/mmc/card.h>
Russell Kinga62c80e2006-01-07 13:52:45 +000026#include <linux/amba/bus.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000027#include <linux/clk.h>
Jens Axboebd6dee62007-10-24 09:01:09 +020028#include <linux/scatterlist.h>
Russell King89001442009-07-09 15:16:07 +010029#include <linux/gpio.h>
Lee Jones9a597012012-04-12 16:51:13 +010030#include <linux/of_gpio.h>
Linus Walleij34e84f32009-09-22 14:41:40 +010031#include <linux/regulator/consumer.h>
Russell Kingc8ebae32011-01-11 19:35:53 +000032#include <linux/dmaengine.h>
33#include <linux/dma-mapping.h>
34#include <linux/amba/mmci.h>
Russell King1c3be362011-08-14 09:17:05 +010035#include <linux/pm_runtime.h>
Viresh Kumar258aea72012-02-01 16:12:19 +053036#include <linux/types.h>
Linus Walleija9a83782012-10-29 14:39:30 +010037#include <linux/pinctrl/consumer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Russell King7b09cda2005-07-01 12:02:59 +010039#include <asm/div64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/io.h>
Russell Kingc6b8fda2005-10-28 14:05:16 +010041#include <asm/sizes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include "mmci.h"
44
45#define DRIVER_NAME "mmci-pl18x"
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047static unsigned int fmax = 515633;
48
Rabin Vincent4956e102010-07-21 12:54:40 +010049/**
50 * struct variant_data - MMCI variant-specific quirks
51 * @clkreg: default value for MCICLOCK register
Rabin Vincent4380c142010-07-21 12:55:18 +010052 * @clkreg_enable: enable value for MMCICLOCK register
Rabin Vincent08458ef2010-07-21 12:55:59 +010053 * @datalength_bits: number of bits in the MMCIDATALENGTH register
Rabin Vincent8301bb62010-08-09 12:57:30 +010054 * @fifosize: number of bytes that can be written when MMCI_TXFIFOEMPTY
55 * is asserted (likewise for RX)
56 * @fifohalfsize: number of bytes that can be written when MCI_TXFIFOHALFEMPTY
57 * is asserted (likewise for RX)
Linus Walleij34177802010-10-19 12:43:58 +010058 * @sdio: variant supports SDIO
Linus Walleijb70a67f2010-12-06 09:24:14 +010059 * @st_clkdiv: true if using a ST-specific clock divider algorithm
Philippe Langlais1784b152011-03-25 08:51:52 +010060 * @blksz_datactrl16: true if Block size is at b16..b30 position in datactrl register
Ulf Hansson7d72a1d2011-12-13 16:54:55 +010061 * @pwrreg_powerup: power up value for MMCIPOWER register
Ulf Hansson4d1a3a02011-12-13 16:57:07 +010062 * @signal_direction: input/out direction of bus signals can be indicated
Ulf Hanssonf4670da2013-01-09 17:19:54 +010063 * @pwrreg_clkgate: MMCIPOWER register must be used to gate the clock
Rabin Vincent4956e102010-07-21 12:54:40 +010064 */
65struct variant_data {
66 unsigned int clkreg;
Rabin Vincent4380c142010-07-21 12:55:18 +010067 unsigned int clkreg_enable;
Rabin Vincent08458ef2010-07-21 12:55:59 +010068 unsigned int datalength_bits;
Rabin Vincent8301bb62010-08-09 12:57:30 +010069 unsigned int fifosize;
70 unsigned int fifohalfsize;
Linus Walleij34177802010-10-19 12:43:58 +010071 bool sdio;
Linus Walleijb70a67f2010-12-06 09:24:14 +010072 bool st_clkdiv;
Philippe Langlais1784b152011-03-25 08:51:52 +010073 bool blksz_datactrl16;
Ulf Hansson7d72a1d2011-12-13 16:54:55 +010074 u32 pwrreg_powerup;
Ulf Hansson4d1a3a02011-12-13 16:57:07 +010075 bool signal_direction;
Ulf Hanssonf4670da2013-01-09 17:19:54 +010076 bool pwrreg_clkgate;
Rabin Vincent4956e102010-07-21 12:54:40 +010077};
78
79static struct variant_data variant_arm = {
Rabin Vincent8301bb62010-08-09 12:57:30 +010080 .fifosize = 16 * 4,
81 .fifohalfsize = 8 * 4,
Rabin Vincent08458ef2010-07-21 12:55:59 +010082 .datalength_bits = 16,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +010083 .pwrreg_powerup = MCI_PWR_UP,
Rabin Vincent4956e102010-07-21 12:54:40 +010084};
85
Pawel Moll768fbc12011-03-11 17:18:07 +000086static struct variant_data variant_arm_extended_fifo = {
87 .fifosize = 128 * 4,
88 .fifohalfsize = 64 * 4,
89 .datalength_bits = 16,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +010090 .pwrreg_powerup = MCI_PWR_UP,
Pawel Moll768fbc12011-03-11 17:18:07 +000091};
92
Rabin Vincent4956e102010-07-21 12:54:40 +010093static struct variant_data variant_u300 = {
Rabin Vincent8301bb62010-08-09 12:57:30 +010094 .fifosize = 16 * 4,
95 .fifohalfsize = 8 * 4,
Linus Walleij49ac2152011-03-04 14:54:16 +010096 .clkreg_enable = MCI_ST_U300_HWFCEN,
Rabin Vincent08458ef2010-07-21 12:55:59 +010097 .datalength_bits = 16,
Linus Walleij34177802010-10-19 12:43:58 +010098 .sdio = true,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +010099 .pwrreg_powerup = MCI_PWR_ON,
Ulf Hansson4d1a3a02011-12-13 16:57:07 +0100100 .signal_direction = true,
Ulf Hanssonf4670da2013-01-09 17:19:54 +0100101 .pwrreg_clkgate = true,
Rabin Vincent4956e102010-07-21 12:54:40 +0100102};
103
Linus Walleij34fd4212012-04-10 17:43:59 +0100104static struct variant_data variant_nomadik = {
105 .fifosize = 16 * 4,
106 .fifohalfsize = 8 * 4,
107 .clkreg = MCI_CLK_ENABLE,
108 .datalength_bits = 24,
109 .sdio = true,
110 .st_clkdiv = true,
111 .pwrreg_powerup = MCI_PWR_ON,
112 .signal_direction = true,
Ulf Hanssonf4670da2013-01-09 17:19:54 +0100113 .pwrreg_clkgate = true,
Linus Walleij34fd4212012-04-10 17:43:59 +0100114};
115
Rabin Vincent4956e102010-07-21 12:54:40 +0100116static struct variant_data variant_ux500 = {
Rabin Vincent8301bb62010-08-09 12:57:30 +0100117 .fifosize = 30 * 4,
118 .fifohalfsize = 8 * 4,
Rabin Vincent4956e102010-07-21 12:54:40 +0100119 .clkreg = MCI_CLK_ENABLE,
Linus Walleij49ac2152011-03-04 14:54:16 +0100120 .clkreg_enable = MCI_ST_UX500_HWFCEN,
Rabin Vincent08458ef2010-07-21 12:55:59 +0100121 .datalength_bits = 24,
Linus Walleij34177802010-10-19 12:43:58 +0100122 .sdio = true,
Linus Walleijb70a67f2010-12-06 09:24:14 +0100123 .st_clkdiv = true,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +0100124 .pwrreg_powerup = MCI_PWR_ON,
Ulf Hansson4d1a3a02011-12-13 16:57:07 +0100125 .signal_direction = true,
Ulf Hanssonf4670da2013-01-09 17:19:54 +0100126 .pwrreg_clkgate = true,
Rabin Vincent4956e102010-07-21 12:54:40 +0100127};
Linus Walleijb70a67f2010-12-06 09:24:14 +0100128
Philippe Langlais1784b152011-03-25 08:51:52 +0100129static struct variant_data variant_ux500v2 = {
130 .fifosize = 30 * 4,
131 .fifohalfsize = 8 * 4,
132 .clkreg = MCI_CLK_ENABLE,
133 .clkreg_enable = MCI_ST_UX500_HWFCEN,
134 .datalength_bits = 24,
135 .sdio = true,
136 .st_clkdiv = true,
137 .blksz_datactrl16 = true,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +0100138 .pwrreg_powerup = MCI_PWR_ON,
Ulf Hansson4d1a3a02011-12-13 16:57:07 +0100139 .signal_direction = true,
Ulf Hanssonf4670da2013-01-09 17:19:54 +0100140 .pwrreg_clkgate = true,
Philippe Langlais1784b152011-03-25 08:51:52 +0100141};
142
Linus Walleija6a64642009-09-14 12:56:14 +0100143/*
144 * This must be called with host->lock held
145 */
Ulf Hansson7437cfa2012-01-18 09:17:27 +0100146static void mmci_write_clkreg(struct mmci_host *host, u32 clk)
147{
148 if (host->clk_reg != clk) {
149 host->clk_reg = clk;
150 writel(clk, host->base + MMCICLOCK);
151 }
152}
153
154/*
155 * This must be called with host->lock held
156 */
157static void mmci_write_pwrreg(struct mmci_host *host, u32 pwr)
158{
159 if (host->pwr_reg != pwr) {
160 host->pwr_reg = pwr;
161 writel(pwr, host->base + MMCIPOWER);
162 }
163}
164
165/*
166 * This must be called with host->lock held
167 */
Linus Walleija6a64642009-09-14 12:56:14 +0100168static void mmci_set_clkreg(struct mmci_host *host, unsigned int desired)
169{
Rabin Vincent4956e102010-07-21 12:54:40 +0100170 struct variant_data *variant = host->variant;
171 u32 clk = variant->clkreg;
Linus Walleija6a64642009-09-14 12:56:14 +0100172
173 if (desired) {
174 if (desired >= host->mclk) {
Linus Walleij991a86e2010-12-10 09:35:53 +0100175 clk = MCI_CLK_BYPASS;
Linus Walleij399bc482011-04-01 07:59:17 +0100176 if (variant->st_clkdiv)
177 clk |= MCI_ST_UX500_NEG_EDGE;
Linus Walleija6a64642009-09-14 12:56:14 +0100178 host->cclk = host->mclk;
Linus Walleijb70a67f2010-12-06 09:24:14 +0100179 } else if (variant->st_clkdiv) {
180 /*
181 * DB8500 TRM says f = mclk / (clkdiv + 2)
182 * => clkdiv = (mclk / f) - 2
183 * Round the divider up so we don't exceed the max
184 * frequency
185 */
186 clk = DIV_ROUND_UP(host->mclk, desired) - 2;
187 if (clk >= 256)
188 clk = 255;
189 host->cclk = host->mclk / (clk + 2);
Linus Walleija6a64642009-09-14 12:56:14 +0100190 } else {
Linus Walleijb70a67f2010-12-06 09:24:14 +0100191 /*
192 * PL180 TRM says f = mclk / (2 * (clkdiv + 1))
193 * => clkdiv = mclk / (2 * f) - 1
194 */
Linus Walleija6a64642009-09-14 12:56:14 +0100195 clk = host->mclk / (2 * desired) - 1;
196 if (clk >= 256)
197 clk = 255;
198 host->cclk = host->mclk / (2 * (clk + 1));
199 }
Rabin Vincent4380c142010-07-21 12:55:18 +0100200
201 clk |= variant->clkreg_enable;
Linus Walleija6a64642009-09-14 12:56:14 +0100202 clk |= MCI_CLK_ENABLE;
203 /* This hasn't proven to be worthwhile */
204 /* clk |= MCI_CLK_PWRSAVE; */
205 }
206
Linus Walleij9e6c82c2009-09-14 12:57:11 +0100207 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4)
Linus Walleij771dc152010-04-08 07:38:52 +0100208 clk |= MCI_4BIT_BUS;
209 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_8)
210 clk |= MCI_ST_8BIT_BUS;
Linus Walleij9e6c82c2009-09-14 12:57:11 +0100211
Ulf Hansson6dbb6ee2013-01-07 15:30:44 +0100212 if (host->mmc->ios.timing == MMC_TIMING_UHS_DDR50)
213 clk |= MCI_ST_UX500_NEG_EDGE;
214
Ulf Hansson7437cfa2012-01-18 09:17:27 +0100215 mmci_write_clkreg(host, clk);
Linus Walleija6a64642009-09-14 12:56:14 +0100216}
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218static void
219mmci_request_end(struct mmci_host *host, struct mmc_request *mrq)
220{
221 writel(0, host->base + MMCICOMMAND);
222
Russell Kinge47c2222007-01-08 16:42:51 +0000223 BUG_ON(host->data);
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 host->mrq = NULL;
226 host->cmd = NULL;
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 mmc_request_done(host->mmc, mrq);
Ulf Hansson2cd976c2011-12-13 17:01:11 +0100229
230 pm_runtime_mark_last_busy(mmc_dev(host->mmc));
231 pm_runtime_put_autosuspend(mmc_dev(host->mmc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
Linus Walleij2686b4b2010-10-19 12:39:48 +0100234static void mmci_set_mask1(struct mmci_host *host, unsigned int mask)
235{
236 void __iomem *base = host->base;
237
238 if (host->singleirq) {
239 unsigned int mask0 = readl(base + MMCIMASK0);
240
241 mask0 &= ~MCI_IRQ1MASK;
242 mask0 |= mask;
243
244 writel(mask0, base + MMCIMASK0);
245 }
246
247 writel(mask, base + MMCIMASK1);
248}
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250static void mmci_stop_data(struct mmci_host *host)
251{
252 writel(0, host->base + MMCIDATACTRL);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100253 mmci_set_mask1(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 host->data = NULL;
255}
256
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100257static void mmci_init_sg(struct mmci_host *host, struct mmc_data *data)
258{
259 unsigned int flags = SG_MITER_ATOMIC;
260
261 if (data->flags & MMC_DATA_READ)
262 flags |= SG_MITER_TO_SG;
263 else
264 flags |= SG_MITER_FROM_SG;
265
266 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
267}
268
Russell Kingc8ebae32011-01-11 19:35:53 +0000269/*
270 * All the DMA operation mode stuff goes inside this ifdef.
271 * This assumes that you have a generic DMA device interface,
272 * no custom DMA interfaces are supported.
273 */
274#ifdef CONFIG_DMA_ENGINE
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500275static void mmci_dma_setup(struct mmci_host *host)
Russell Kingc8ebae32011-01-11 19:35:53 +0000276{
277 struct mmci_platform_data *plat = host->plat;
278 const char *rxname, *txname;
279 dma_cap_mask_t mask;
280
281 if (!plat || !plat->dma_filter) {
282 dev_info(mmc_dev(host->mmc), "no DMA platform data\n");
283 return;
284 }
285
Per Forlin58c7ccb2011-07-01 18:55:24 +0200286 /* initialize pre request cookie */
287 host->next_data.cookie = 1;
288
Russell Kingc8ebae32011-01-11 19:35:53 +0000289 /* Try to acquire a generic DMA engine slave channel */
290 dma_cap_zero(mask);
291 dma_cap_set(DMA_SLAVE, mask);
292
293 /*
294 * If only an RX channel is specified, the driver will
295 * attempt to use it bidirectionally, however if it is
296 * is specified but cannot be located, DMA will be disabled.
297 */
298 if (plat->dma_rx_param) {
299 host->dma_rx_channel = dma_request_channel(mask,
300 plat->dma_filter,
301 plat->dma_rx_param);
302 /* E.g if no DMA hardware is present */
303 if (!host->dma_rx_channel)
304 dev_err(mmc_dev(host->mmc), "no RX DMA channel\n");
305 }
306
307 if (plat->dma_tx_param) {
308 host->dma_tx_channel = dma_request_channel(mask,
309 plat->dma_filter,
310 plat->dma_tx_param);
311 if (!host->dma_tx_channel)
312 dev_warn(mmc_dev(host->mmc), "no TX DMA channel\n");
313 } else {
314 host->dma_tx_channel = host->dma_rx_channel;
315 }
316
317 if (host->dma_rx_channel)
318 rxname = dma_chan_name(host->dma_rx_channel);
319 else
320 rxname = "none";
321
322 if (host->dma_tx_channel)
323 txname = dma_chan_name(host->dma_tx_channel);
324 else
325 txname = "none";
326
327 dev_info(mmc_dev(host->mmc), "DMA channels RX %s, TX %s\n",
328 rxname, txname);
329
330 /*
331 * Limit the maximum segment size in any SG entry according to
332 * the parameters of the DMA engine device.
333 */
334 if (host->dma_tx_channel) {
335 struct device *dev = host->dma_tx_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 if (host->dma_rx_channel) {
342 struct device *dev = host->dma_rx_channel->device->dev;
343 unsigned int max_seg_size = dma_get_max_seg_size(dev);
344
345 if (max_seg_size < host->mmc->max_seg_size)
346 host->mmc->max_seg_size = max_seg_size;
347 }
348}
349
350/*
Bill Pemberton6e0ee712012-11-19 13:26:03 -0500351 * This is used in or so inline it
Russell Kingc8ebae32011-01-11 19:35:53 +0000352 * so it can be discarded.
353 */
354static inline void mmci_dma_release(struct mmci_host *host)
355{
356 struct mmci_platform_data *plat = host->plat;
357
358 if (host->dma_rx_channel)
359 dma_release_channel(host->dma_rx_channel);
360 if (host->dma_tx_channel && plat->dma_tx_param)
361 dma_release_channel(host->dma_tx_channel);
362 host->dma_rx_channel = host->dma_tx_channel = NULL;
363}
364
365static void mmci_dma_unmap(struct mmci_host *host, struct mmc_data *data)
366{
367 struct dma_chan *chan = host->dma_current;
368 enum dma_data_direction dir;
369 u32 status;
370 int i;
371
372 /* Wait up to 1ms for the DMA to complete */
373 for (i = 0; ; i++) {
374 status = readl(host->base + MMCISTATUS);
375 if (!(status & MCI_RXDATAAVLBLMASK) || i >= 100)
376 break;
377 udelay(10);
378 }
379
380 /*
381 * Check to see whether we still have some data left in the FIFO -
382 * this catches DMA controllers which are unable to monitor the
383 * DMALBREQ and DMALSREQ signals while allowing us to DMA to non-
384 * contiguous buffers. On TX, we'll get a FIFO underrun error.
385 */
386 if (status & MCI_RXDATAAVLBLMASK) {
387 dmaengine_terminate_all(chan);
388 if (!data->error)
389 data->error = -EIO;
390 }
391
392 if (data->flags & MMC_DATA_WRITE) {
393 dir = DMA_TO_DEVICE;
394 } else {
395 dir = DMA_FROM_DEVICE;
396 }
397
Per Forlin58c7ccb2011-07-01 18:55:24 +0200398 if (!data->host_cookie)
399 dma_unmap_sg(chan->device->dev, data->sg, data->sg_len, dir);
Russell Kingc8ebae32011-01-11 19:35:53 +0000400
401 /*
402 * Use of DMA with scatter-gather is impossible.
403 * Give up with DMA and switch back to PIO mode.
404 */
405 if (status & MCI_RXDATAAVLBLMASK) {
406 dev_err(mmc_dev(host->mmc), "buggy DMA detected. Taking evasive action.\n");
407 mmci_dma_release(host);
408 }
409}
410
411static void mmci_dma_data_error(struct mmci_host *host)
412{
413 dev_err(mmc_dev(host->mmc), "error during DMA transfer!\n");
414 dmaengine_terminate_all(host->dma_current);
415}
416
Per Forlin58c7ccb2011-07-01 18:55:24 +0200417static int mmci_dma_prep_data(struct mmci_host *host, struct mmc_data *data,
418 struct mmci_host_next *next)
Russell Kingc8ebae32011-01-11 19:35:53 +0000419{
420 struct variant_data *variant = host->variant;
421 struct dma_slave_config conf = {
422 .src_addr = host->phybase + MMCIFIFO,
423 .dst_addr = host->phybase + MMCIFIFO,
424 .src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
425 .dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
426 .src_maxburst = variant->fifohalfsize >> 2, /* # of words */
427 .dst_maxburst = variant->fifohalfsize >> 2, /* # of words */
Viresh Kumar258aea72012-02-01 16:12:19 +0530428 .device_fc = false,
Russell Kingc8ebae32011-01-11 19:35:53 +0000429 };
Russell Kingc8ebae32011-01-11 19:35:53 +0000430 struct dma_chan *chan;
431 struct dma_device *device;
432 struct dma_async_tx_descriptor *desc;
Vinod Koul05f57992011-10-14 10:45:11 +0530433 enum dma_data_direction buffer_dirn;
Russell Kingc8ebae32011-01-11 19:35:53 +0000434 int nr_sg;
435
Per Forlin58c7ccb2011-07-01 18:55:24 +0200436 /* Check if next job is already prepared */
437 if (data->host_cookie && !next &&
438 host->dma_current && host->dma_desc_current)
439 return 0;
440
441 if (!next) {
442 host->dma_current = NULL;
443 host->dma_desc_current = NULL;
444 }
Russell Kingc8ebae32011-01-11 19:35:53 +0000445
446 if (data->flags & MMC_DATA_READ) {
Vinod Koul05f57992011-10-14 10:45:11 +0530447 conf.direction = DMA_DEV_TO_MEM;
448 buffer_dirn = DMA_FROM_DEVICE;
Russell Kingc8ebae32011-01-11 19:35:53 +0000449 chan = host->dma_rx_channel;
450 } else {
Vinod Koul05f57992011-10-14 10:45:11 +0530451 conf.direction = DMA_MEM_TO_DEV;
452 buffer_dirn = DMA_TO_DEVICE;
Russell Kingc8ebae32011-01-11 19:35:53 +0000453 chan = host->dma_tx_channel;
454 }
455
456 /* If there's no DMA channel, fall back to PIO */
457 if (!chan)
458 return -EINVAL;
459
460 /* If less than or equal to the fifo size, don't bother with DMA */
Per Forlin58c7ccb2011-07-01 18:55:24 +0200461 if (data->blksz * data->blocks <= variant->fifosize)
Russell Kingc8ebae32011-01-11 19:35:53 +0000462 return -EINVAL;
463
464 device = chan->device;
Vinod Koul05f57992011-10-14 10:45:11 +0530465 nr_sg = dma_map_sg(device->dev, data->sg, data->sg_len, buffer_dirn);
Russell Kingc8ebae32011-01-11 19:35:53 +0000466 if (nr_sg == 0)
467 return -EINVAL;
468
469 dmaengine_slave_config(chan, &conf);
Alexandre Bounine16052822012-03-08 16:11:18 -0500470 desc = dmaengine_prep_slave_sg(chan, data->sg, nr_sg,
Russell Kingc8ebae32011-01-11 19:35:53 +0000471 conf.direction, DMA_CTRL_ACK);
472 if (!desc)
473 goto unmap_exit;
474
Per Forlin58c7ccb2011-07-01 18:55:24 +0200475 if (next) {
476 next->dma_chan = chan;
477 next->dma_desc = desc;
478 } else {
479 host->dma_current = chan;
480 host->dma_desc_current = desc;
481 }
Russell Kingc8ebae32011-01-11 19:35:53 +0000482
Per Forlin58c7ccb2011-07-01 18:55:24 +0200483 return 0;
484
485 unmap_exit:
486 if (!next)
487 dmaengine_terminate_all(chan);
Vinod Koul05f57992011-10-14 10:45:11 +0530488 dma_unmap_sg(device->dev, data->sg, data->sg_len, buffer_dirn);
Per Forlin58c7ccb2011-07-01 18:55:24 +0200489 return -ENOMEM;
490}
491
492static int mmci_dma_start_data(struct mmci_host *host, unsigned int datactrl)
493{
494 int ret;
495 struct mmc_data *data = host->data;
496
497 ret = mmci_dma_prep_data(host, host->data, NULL);
498 if (ret)
499 return ret;
500
501 /* Okay, go for it. */
Russell Kingc8ebae32011-01-11 19:35:53 +0000502 dev_vdbg(mmc_dev(host->mmc),
503 "Submit MMCI DMA job, sglen %d blksz %04x blks %04x flags %08x\n",
504 data->sg_len, data->blksz, data->blocks, data->flags);
Per Forlin58c7ccb2011-07-01 18:55:24 +0200505 dmaengine_submit(host->dma_desc_current);
506 dma_async_issue_pending(host->dma_current);
Russell Kingc8ebae32011-01-11 19:35:53 +0000507
508 datactrl |= MCI_DPSM_DMAENABLE;
509
510 /* Trigger the DMA transfer */
511 writel(datactrl, host->base + MMCIDATACTRL);
512
513 /*
514 * Let the MMCI say when the data is ended and it's time
515 * to fire next DMA request. When that happens, MMCI will
516 * call mmci_data_end()
517 */
518 writel(readl(host->base + MMCIMASK0) | MCI_DATAENDMASK,
519 host->base + MMCIMASK0);
520 return 0;
Russell Kingc8ebae32011-01-11 19:35:53 +0000521}
Per Forlin58c7ccb2011-07-01 18:55:24 +0200522
523static void mmci_get_next_data(struct mmci_host *host, struct mmc_data *data)
524{
525 struct mmci_host_next *next = &host->next_data;
526
527 if (data->host_cookie && data->host_cookie != next->cookie) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530528 pr_warning("[%s] invalid cookie: data->host_cookie %d"
Per Forlin58c7ccb2011-07-01 18:55:24 +0200529 " host->next_data.cookie %d\n",
530 __func__, data->host_cookie, host->next_data.cookie);
531 data->host_cookie = 0;
532 }
533
534 if (!data->host_cookie)
535 return;
536
537 host->dma_desc_current = next->dma_desc;
538 host->dma_current = next->dma_chan;
539
540 next->dma_desc = NULL;
541 next->dma_chan = NULL;
542}
543
544static void mmci_pre_request(struct mmc_host *mmc, struct mmc_request *mrq,
545 bool is_first_req)
546{
547 struct mmci_host *host = mmc_priv(mmc);
548 struct mmc_data *data = mrq->data;
549 struct mmci_host_next *nd = &host->next_data;
550
551 if (!data)
552 return;
553
554 if (data->host_cookie) {
555 data->host_cookie = 0;
556 return;
557 }
558
559 /* if config for dma */
560 if (((data->flags & MMC_DATA_WRITE) && host->dma_tx_channel) ||
561 ((data->flags & MMC_DATA_READ) && host->dma_rx_channel)) {
562 if (mmci_dma_prep_data(host, data, nd))
563 data->host_cookie = 0;
564 else
565 data->host_cookie = ++nd->cookie < 0 ? 1 : nd->cookie;
566 }
567}
568
569static void mmci_post_request(struct mmc_host *mmc, struct mmc_request *mrq,
570 int err)
571{
572 struct mmci_host *host = mmc_priv(mmc);
573 struct mmc_data *data = mrq->data;
574 struct dma_chan *chan;
575 enum dma_data_direction dir;
576
577 if (!data)
578 return;
579
580 if (data->flags & MMC_DATA_READ) {
581 dir = DMA_FROM_DEVICE;
582 chan = host->dma_rx_channel;
583 } else {
584 dir = DMA_TO_DEVICE;
585 chan = host->dma_tx_channel;
586 }
587
588
589 /* if config for dma */
590 if (chan) {
591 if (err)
592 dmaengine_terminate_all(chan);
Per Forlin8e3336b2011-08-29 15:35:59 +0200593 if (data->host_cookie)
Per Forlin58c7ccb2011-07-01 18:55:24 +0200594 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
595 data->sg_len, dir);
596 mrq->data->host_cookie = 0;
597 }
598}
599
Russell Kingc8ebae32011-01-11 19:35:53 +0000600#else
601/* Blank functions if the DMA engine is not available */
Per Forlin58c7ccb2011-07-01 18:55:24 +0200602static void mmci_get_next_data(struct mmci_host *host, struct mmc_data *data)
603{
604}
Russell Kingc8ebae32011-01-11 19:35:53 +0000605static inline void mmci_dma_setup(struct mmci_host *host)
606{
607}
608
609static inline void mmci_dma_release(struct mmci_host *host)
610{
611}
612
613static inline void mmci_dma_unmap(struct mmci_host *host, struct mmc_data *data)
614{
615}
616
617static inline void mmci_dma_data_error(struct mmci_host *host)
618{
619}
620
621static inline int mmci_dma_start_data(struct mmci_host *host, unsigned int datactrl)
622{
623 return -ENOSYS;
624}
Per Forlin58c7ccb2011-07-01 18:55:24 +0200625
626#define mmci_pre_request NULL
627#define mmci_post_request NULL
628
Russell Kingc8ebae32011-01-11 19:35:53 +0000629#endif
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631static void mmci_start_data(struct mmci_host *host, struct mmc_data *data)
632{
Rabin Vincent8301bb62010-08-09 12:57:30 +0100633 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 unsigned int datactrl, timeout, irqmask;
Russell King7b09cda2005-07-01 12:02:59 +0100635 unsigned long long clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 void __iomem *base;
Russell King3bc87f22006-08-27 13:51:28 +0100637 int blksz_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Linus Walleij64de0282010-02-19 01:09:10 +0100639 dev_dbg(mmc_dev(host->mmc), "blksz %04x blks %04x flags %08x\n",
640 data->blksz, data->blocks, data->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 host->data = data;
Rabin Vincent528320d2010-07-21 12:49:49 +0100643 host->size = data->blksz * data->blocks;
Russell King51d43752011-01-27 10:56:52 +0000644 data->bytes_xfered = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Russell King7b09cda2005-07-01 12:02:59 +0100646 clks = (unsigned long long)data->timeout_ns * host->cclk;
647 do_div(clks, 1000000000UL);
648
649 timeout = data->timeout_clks + (unsigned int)clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 base = host->base;
652 writel(timeout, base + MMCIDATATIMER);
653 writel(host->size, base + MMCIDATALENGTH);
654
Russell King3bc87f22006-08-27 13:51:28 +0100655 blksz_bits = ffs(data->blksz) - 1;
656 BUG_ON(1 << blksz_bits != data->blksz);
657
Philippe Langlais1784b152011-03-25 08:51:52 +0100658 if (variant->blksz_datactrl16)
659 datactrl = MCI_DPSM_ENABLE | (data->blksz << 16);
660 else
661 datactrl = MCI_DPSM_ENABLE | blksz_bits << 4;
Russell Kingc8ebae32011-01-11 19:35:53 +0000662
663 if (data->flags & MMC_DATA_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 datactrl |= MCI_DPSM_DIRECTION;
Russell Kingc8ebae32011-01-11 19:35:53 +0000665
Ulf Hansson7258db72011-12-13 17:05:28 +0100666 /* The ST Micro variants has a special bit to enable SDIO */
667 if (variant->sdio && host->mmc->card)
Ulf Hansson06c1a122012-10-12 14:01:50 +0100668 if (mmc_card_sdio(host->mmc->card)) {
669 /*
670 * The ST Micro variants has a special bit
671 * to enable SDIO.
672 */
673 u32 clk;
674
Ulf Hansson7258db72011-12-13 17:05:28 +0100675 datactrl |= MCI_ST_DPSM_SDIOEN;
676
Ulf Hansson06c1a122012-10-12 14:01:50 +0100677 /*
Ulf Hansson70ac0932012-10-12 14:07:36 +0100678 * The ST Micro variant for SDIO small write transfers
679 * needs to have clock H/W flow control disabled,
680 * otherwise the transfer will not start. The threshold
681 * depends on the rate of MCLK.
Ulf Hansson06c1a122012-10-12 14:01:50 +0100682 */
Ulf Hansson70ac0932012-10-12 14:07:36 +0100683 if (data->flags & MMC_DATA_WRITE &&
684 (host->size < 8 ||
685 (host->size <= 8 && host->mclk > 50000000)))
Ulf Hansson06c1a122012-10-12 14:01:50 +0100686 clk = host->clk_reg & ~variant->clkreg_enable;
687 else
688 clk = host->clk_reg | variant->clkreg_enable;
689
690 mmci_write_clkreg(host, clk);
691 }
692
Ulf Hansson6dbb6ee2013-01-07 15:30:44 +0100693 if (host->mmc->ios.timing == MMC_TIMING_UHS_DDR50)
694 datactrl |= MCI_ST_DPSM_DDRMODE;
695
Russell Kingc8ebae32011-01-11 19:35:53 +0000696 /*
697 * Attempt to use DMA operation mode, if this
698 * should fail, fall back to PIO mode
699 */
700 if (!mmci_dma_start_data(host, datactrl))
701 return;
702
703 /* IRQ mode, map the SG list for CPU reading/writing */
704 mmci_init_sg(host, data);
705
706 if (data->flags & MMC_DATA_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 irqmask = MCI_RXFIFOHALFFULLMASK;
Russell King0425a142006-02-16 16:48:31 +0000708
709 /*
Russell Kingc4d877c2011-01-27 09:50:13 +0000710 * If we have less than the fifo 'half-full' threshold to
711 * transfer, trigger a PIO interrupt as soon as any data
712 * is available.
Russell King0425a142006-02-16 16:48:31 +0000713 */
Russell Kingc4d877c2011-01-27 09:50:13 +0000714 if (host->size < variant->fifohalfsize)
Russell King0425a142006-02-16 16:48:31 +0000715 irqmask |= MCI_RXDATAAVLBLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 } else {
717 /*
718 * We don't actually need to include "FIFO empty" here
719 * since its implicit in "FIFO half empty".
720 */
721 irqmask = MCI_TXFIFOHALFEMPTYMASK;
722 }
723
724 writel(datactrl, base + MMCIDATACTRL);
725 writel(readl(base + MMCIMASK0) & ~MCI_DATAENDMASK, base + MMCIMASK0);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100726 mmci_set_mask1(host, irqmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727}
728
729static void
730mmci_start_command(struct mmci_host *host, struct mmc_command *cmd, u32 c)
731{
732 void __iomem *base = host->base;
733
Linus Walleij64de0282010-02-19 01:09:10 +0100734 dev_dbg(mmc_dev(host->mmc), "op %02x arg %08x flags %08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 cmd->opcode, cmd->arg, cmd->flags);
736
737 if (readl(base + MMCICOMMAND) & MCI_CPSM_ENABLE) {
738 writel(0, base + MMCICOMMAND);
739 udelay(1);
740 }
741
742 c |= cmd->opcode | MCI_CPSM_ENABLE;
Russell Kinge9225172006-02-02 12:23:12 +0000743 if (cmd->flags & MMC_RSP_PRESENT) {
744 if (cmd->flags & MMC_RSP_136)
745 c |= MCI_CPSM_LONGRSP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 c |= MCI_CPSM_RESPONSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
748 if (/*interrupt*/0)
749 c |= MCI_CPSM_INTERRUPT;
750
751 host->cmd = cmd;
752
753 writel(cmd->arg, base + MMCIARGUMENT);
754 writel(c, base + MMCICOMMAND);
755}
756
757static void
758mmci_data_irq(struct mmci_host *host, struct mmc_data *data,
759 unsigned int status)
760{
Linus Walleijf20f8f22010-10-19 13:41:24 +0100761 /* First check for errors */
Ulf Hanssonb63038d2011-12-13 16:51:04 +0100762 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_STARTBITERR|
763 MCI_TXUNDERRUN|MCI_RXOVERRUN)) {
Linus Walleij8cb28152011-01-24 15:22:13 +0100764 u32 remain, success;
Linus Walleijf20f8f22010-10-19 13:41:24 +0100765
Russell Kingc8ebae32011-01-11 19:35:53 +0000766 /* Terminate the DMA transfer */
767 if (dma_inprogress(host))
768 mmci_dma_data_error(host);
769
Russell Kingc8afc9d2011-02-04 09:19:46 +0000770 /*
771 * Calculate how far we are into the transfer. Note that
772 * the data counter gives the number of bytes transferred
773 * on the MMC bus, not on the host side. On reads, this
774 * can be as much as a FIFO-worth of data ahead. This
775 * matters for FIFO overruns only.
776 */
Linus Walleijf5a106d2011-01-27 17:44:34 +0100777 remain = readl(host->base + MMCIDATACNT);
Linus Walleij8cb28152011-01-24 15:22:13 +0100778 success = data->blksz * data->blocks - remain;
779
Russell Kingc8afc9d2011-02-04 09:19:46 +0000780 dev_dbg(mmc_dev(host->mmc), "MCI ERROR IRQ, status 0x%08x at 0x%08x\n",
781 status, success);
Linus Walleij8cb28152011-01-24 15:22:13 +0100782 if (status & MCI_DATACRCFAIL) {
783 /* Last block was not successful */
Russell Kingc8afc9d2011-02-04 09:19:46 +0000784 success -= 1;
Pierre Ossman17b04292007-07-22 22:18:46 +0200785 data->error = -EILSEQ;
Linus Walleij8cb28152011-01-24 15:22:13 +0100786 } else if (status & MCI_DATATIMEOUT) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200787 data->error = -ETIMEDOUT;
Linus Walleij757df742011-06-30 15:10:21 +0100788 } else if (status & MCI_STARTBITERR) {
789 data->error = -ECOMM;
Russell Kingc8afc9d2011-02-04 09:19:46 +0000790 } else if (status & MCI_TXUNDERRUN) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200791 data->error = -EIO;
Russell Kingc8afc9d2011-02-04 09:19:46 +0000792 } else if (status & MCI_RXOVERRUN) {
793 if (success > host->variant->fifosize)
794 success -= host->variant->fifosize;
795 else
796 success = 0;
Linus Walleij8cb28152011-01-24 15:22:13 +0100797 data->error = -EIO;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100798 }
Russell King51d43752011-01-27 10:56:52 +0000799 data->bytes_xfered = round_down(success, data->blksz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
Linus Walleijf20f8f22010-10-19 13:41:24 +0100801
Linus Walleij8cb28152011-01-24 15:22:13 +0100802 if (status & MCI_DATABLOCKEND)
803 dev_err(mmc_dev(host->mmc), "stray MCI_DATABLOCKEND interrupt\n");
Linus Walleijf20f8f22010-10-19 13:41:24 +0100804
Russell Kingccff9b52011-01-30 21:03:50 +0000805 if (status & MCI_DATAEND || data->error) {
Russell Kingc8ebae32011-01-11 19:35:53 +0000806 if (dma_inprogress(host))
807 mmci_dma_unmap(host, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 mmci_stop_data(host);
809
Linus Walleij8cb28152011-01-24 15:22:13 +0100810 if (!data->error)
811 /* The error clause is handled above, success! */
Russell King51d43752011-01-27 10:56:52 +0000812 data->bytes_xfered = data->blksz * data->blocks;
Linus Walleijf20f8f22010-10-19 13:41:24 +0100813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if (!data->stop) {
815 mmci_request_end(host, data->mrq);
816 } else {
817 mmci_start_command(host, data->stop, 0);
818 }
819 }
820}
821
822static void
823mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
824 unsigned int status)
825{
826 void __iomem *base = host->base;
827
828 host->cmd = NULL;
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (status & MCI_CMDTIMEOUT) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200831 cmd->error = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200833 cmd->error = -EILSEQ;
Russell King - ARM Linux9047b432011-01-11 16:35:56 +0000834 } else {
835 cmd->resp[0] = readl(base + MMCIRESPONSE0);
836 cmd->resp[1] = readl(base + MMCIRESPONSE1);
837 cmd->resp[2] = readl(base + MMCIRESPONSE2);
838 cmd->resp[3] = readl(base + MMCIRESPONSE3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
840
Pierre Ossman17b04292007-07-22 22:18:46 +0200841 if (!cmd->data || cmd->error) {
Ulf Hansson3b6e3c72011-12-13 16:58:43 +0100842 if (host->data) {
843 /* Terminate the DMA transfer */
844 if (dma_inprogress(host))
845 mmci_dma_data_error(host);
Russell Kinge47c2222007-01-08 16:42:51 +0000846 mmci_stop_data(host);
Ulf Hansson3b6e3c72011-12-13 16:58:43 +0100847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 mmci_request_end(host, cmd->mrq);
849 } else if (!(cmd->data->flags & MMC_DATA_READ)) {
850 mmci_start_data(host, cmd->data);
851 }
852}
853
854static int mmci_pio_read(struct mmci_host *host, char *buffer, unsigned int remain)
855{
856 void __iomem *base = host->base;
857 char *ptr = buffer;
858 u32 status;
Linus Walleij26eed9a2008-04-26 23:39:44 +0100859 int host_remain = host->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 do {
Linus Walleij26eed9a2008-04-26 23:39:44 +0100862 int count = host_remain - (readl(base + MMCIFIFOCNT) << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 if (count > remain)
865 count = remain;
866
867 if (count <= 0)
868 break;
869
Ulf Hansson393e5e22011-12-13 17:08:04 +0100870 /*
871 * SDIO especially may want to send something that is
872 * not divisible by 4 (as opposed to card sectors
873 * etc). Therefore make sure to always read the last bytes
874 * while only doing full 32-bit reads towards the FIFO.
875 */
876 if (unlikely(count & 0x3)) {
877 if (count < 4) {
878 unsigned char buf[4];
Davide Ciminaghi4b85da02012-12-10 14:47:21 +0100879 ioread32_rep(base + MMCIFIFO, buf, 1);
Ulf Hansson393e5e22011-12-13 17:08:04 +0100880 memcpy(ptr, buf, count);
881 } else {
Davide Ciminaghi4b85da02012-12-10 14:47:21 +0100882 ioread32_rep(base + MMCIFIFO, ptr, count >> 2);
Ulf Hansson393e5e22011-12-13 17:08:04 +0100883 count &= ~0x3;
884 }
885 } else {
Davide Ciminaghi4b85da02012-12-10 14:47:21 +0100886 ioread32_rep(base + MMCIFIFO, ptr, count >> 2);
Ulf Hansson393e5e22011-12-13 17:08:04 +0100887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 ptr += count;
890 remain -= count;
Linus Walleij26eed9a2008-04-26 23:39:44 +0100891 host_remain -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 if (remain == 0)
894 break;
895
896 status = readl(base + MMCISTATUS);
897 } while (status & MCI_RXDATAAVLBL);
898
899 return ptr - buffer;
900}
901
902static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status)
903{
Rabin Vincent8301bb62010-08-09 12:57:30 +0100904 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 void __iomem *base = host->base;
906 char *ptr = buffer;
907
908 do {
909 unsigned int count, maxcnt;
910
Rabin Vincent8301bb62010-08-09 12:57:30 +0100911 maxcnt = status & MCI_TXFIFOEMPTY ?
912 variant->fifosize : variant->fifohalfsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 count = min(remain, maxcnt);
914
Linus Walleij34177802010-10-19 12:43:58 +0100915 /*
Linus Walleij34177802010-10-19 12:43:58 +0100916 * SDIO especially may want to send something that is
917 * not divisible by 4 (as opposed to card sectors
918 * etc), and the FIFO only accept full 32-bit writes.
919 * So compensate by adding +3 on the count, a single
920 * byte become a 32bit write, 7 bytes will be two
921 * 32bit writes etc.
922 */
Davide Ciminaghi4b85da02012-12-10 14:47:21 +0100923 iowrite32_rep(base + MMCIFIFO, ptr, (count + 3) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925 ptr += count;
926 remain -= count;
927
928 if (remain == 0)
929 break;
930
931 status = readl(base + MMCISTATUS);
932 } while (status & MCI_TXFIFOHALFEMPTY);
933
934 return ptr - buffer;
935}
936
937/*
938 * PIO data transfer IRQ handler.
939 */
David Howells7d12e782006-10-05 14:55:46 +0100940static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
942 struct mmci_host *host = dev_id;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100943 struct sg_mapping_iter *sg_miter = &host->sg_miter;
Rabin Vincent8301bb62010-08-09 12:57:30 +0100944 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 void __iomem *base = host->base;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100946 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 u32 status;
948
949 status = readl(base + MMCISTATUS);
950
Linus Walleij64de0282010-02-19 01:09:10 +0100951 dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100953 local_irq_save(flags);
954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 unsigned int remain, len;
957 char *buffer;
958
959 /*
960 * For write, we only need to test the half-empty flag
961 * here - if the FIFO is completely empty, then by
962 * definition it is more than half empty.
963 *
964 * For read, check for data available.
965 */
966 if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL)))
967 break;
968
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100969 if (!sg_miter_next(sg_miter))
970 break;
971
972 buffer = sg_miter->addr;
973 remain = sg_miter->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975 len = 0;
976 if (status & MCI_RXACTIVE)
977 len = mmci_pio_read(host, buffer, remain);
978 if (status & MCI_TXACTIVE)
979 len = mmci_pio_write(host, buffer, remain, status);
980
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100981 sg_miter->consumed = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 host->size -= len;
984 remain -= len;
985
986 if (remain)
987 break;
988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 status = readl(base + MMCISTATUS);
990 } while (1);
991
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100992 sg_miter_stop(sg_miter);
993
994 local_irq_restore(flags);
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 /*
Russell Kingc4d877c2011-01-27 09:50:13 +0000997 * If we have less than the fifo 'half-full' threshold to transfer,
998 * trigger a PIO interrupt as soon as any data is available.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 */
Russell Kingc4d877c2011-01-27 09:50:13 +00001000 if (status & MCI_RXACTIVE && host->size < variant->fifohalfsize)
Linus Walleij2686b4b2010-10-19 12:39:48 +01001001 mmci_set_mask1(host, MCI_RXDATAAVLBLMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003 /*
1004 * If we run out of data, disable the data IRQs; this
1005 * prevents a race where the FIFO becomes empty before
1006 * the chip itself has disabled the data path, and
1007 * stops us racing with our data end IRQ.
1008 */
1009 if (host->size == 0) {
Linus Walleij2686b4b2010-10-19 12:39:48 +01001010 mmci_set_mask1(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0);
1012 }
1013
1014 return IRQ_HANDLED;
1015}
1016
1017/*
1018 * Handle completion of command and data transfers.
1019 */
David Howells7d12e782006-10-05 14:55:46 +01001020static irqreturn_t mmci_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
1022 struct mmci_host *host = dev_id;
1023 u32 status;
1024 int ret = 0;
1025
1026 spin_lock(&host->lock);
1027
1028 do {
1029 struct mmc_command *cmd;
1030 struct mmc_data *data;
1031
1032 status = readl(host->base + MMCISTATUS);
Linus Walleij2686b4b2010-10-19 12:39:48 +01001033
1034 if (host->singleirq) {
1035 if (status & readl(host->base + MMCIMASK1))
1036 mmci_pio_irq(irq, dev_id);
1037
1038 status &= ~MCI_IRQ1MASK;
1039 }
1040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 status &= readl(host->base + MMCIMASK0);
1042 writel(status, host->base + MMCICLEAR);
1043
Linus Walleij64de0282010-02-19 01:09:10 +01001044 dev_dbg(mmc_dev(host->mmc), "irq0 (data+cmd) %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
1046 data = host->data;
Ulf Hanssonb63038d2011-12-13 16:51:04 +01001047 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_STARTBITERR|
1048 MCI_TXUNDERRUN|MCI_RXOVERRUN|MCI_DATAEND|
1049 MCI_DATABLOCKEND) && data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 mmci_data_irq(host, data, status);
1051
1052 cmd = host->cmd;
1053 if (status & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT|MCI_CMDSENT|MCI_CMDRESPEND) && cmd)
1054 mmci_cmd_irq(host, cmd, status);
1055
1056 ret = 1;
1057 } while (status);
1058
1059 spin_unlock(&host->lock);
1060
1061 return IRQ_RETVAL(ret);
1062}
1063
1064static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1065{
1066 struct mmci_host *host = mmc_priv(mmc);
Linus Walleij9e943022008-10-24 21:17:50 +01001067 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 WARN_ON(host->mrq != NULL);
1070
Nicolas Pitre019a5f52007-10-11 01:06:03 -04001071 if (mrq->data && !is_power_of_2(mrq->data->blksz)) {
Linus Walleij64de0282010-02-19 01:09:10 +01001072 dev_err(mmc_dev(mmc), "unsupported block size (%d bytes)\n",
1073 mrq->data->blksz);
Pierre Ossman255d01a2007-07-24 20:38:53 +02001074 mrq->cmd->error = -EINVAL;
1075 mmc_request_done(mmc, mrq);
1076 return;
1077 }
1078
Russell King1c3be362011-08-14 09:17:05 +01001079 pm_runtime_get_sync(mmc_dev(mmc));
1080
Linus Walleij9e943022008-10-24 21:17:50 +01001081 spin_lock_irqsave(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
1083 host->mrq = mrq;
1084
Per Forlin58c7ccb2011-07-01 18:55:24 +02001085 if (mrq->data)
1086 mmci_get_next_data(host, mrq->data);
1087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 if (mrq->data && mrq->data->flags & MMC_DATA_READ)
1089 mmci_start_data(host, mrq->data);
1090
1091 mmci_start_command(host, mrq->cmd, 0);
1092
Linus Walleij9e943022008-10-24 21:17:50 +01001093 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094}
1095
1096static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1097{
1098 struct mmci_host *host = mmc_priv(mmc);
Ulf Hansson7d72a1d2011-12-13 16:54:55 +01001099 struct variant_data *variant = host->variant;
Linus Walleija6a64642009-09-14 12:56:14 +01001100 u32 pwr = 0;
1101 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Ulf Hansson2cd976c2011-12-13 17:01:11 +01001103 pm_runtime_get_sync(mmc_dev(mmc));
1104
Ulf Hanssonbc521812011-12-13 16:57:55 +01001105 if (host->plat->ios_handler &&
1106 host->plat->ios_handler(mmc_dev(mmc), ios))
1107 dev_err(mmc_dev(mmc), "platform ios_handler failed\n");
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 switch (ios->power_mode) {
1110 case MMC_POWER_OFF:
Ulf Hansson599c1d52013-01-07 16:22:50 +01001111 if (!IS_ERR(mmc->supply.vmmc))
1112 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 break;
1114 case MMC_POWER_UP:
Ulf Hansson599c1d52013-01-07 16:22:50 +01001115 if (!IS_ERR(mmc->supply.vmmc))
1116 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd);
1117
Ulf Hansson7d72a1d2011-12-13 16:54:55 +01001118 /*
1119 * The ST Micro variant doesn't have the PL180s MCI_PWR_UP
1120 * and instead uses MCI_PWR_ON so apply whatever value is
1121 * configured in the variant data.
1122 */
1123 pwr |= variant->pwrreg_powerup;
1124
1125 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 case MMC_POWER_ON:
1127 pwr |= MCI_PWR_ON;
1128 break;
1129 }
1130
Ulf Hansson4d1a3a02011-12-13 16:57:07 +01001131 if (variant->signal_direction && ios->power_mode != MMC_POWER_OFF) {
1132 /*
1133 * The ST Micro variant has some additional bits
1134 * indicating signal direction for the signals in
1135 * the SD/MMC bus and feedback-clock usage.
1136 */
1137 pwr |= host->plat->sigdir;
1138
1139 if (ios->bus_width == MMC_BUS_WIDTH_4)
1140 pwr &= ~MCI_ST_DATA74DIREN;
1141 else if (ios->bus_width == MMC_BUS_WIDTH_1)
1142 pwr &= (~MCI_ST_DATA74DIREN &
1143 ~MCI_ST_DATA31DIREN &
1144 ~MCI_ST_DATA2DIREN);
1145 }
1146
Linus Walleijcc30d602009-01-04 15:18:54 +01001147 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN) {
Linus Walleijf17a1f02009-08-04 01:01:02 +01001148 if (host->hw_designer != AMBA_VENDOR_ST)
Linus Walleijcc30d602009-01-04 15:18:54 +01001149 pwr |= MCI_ROD;
1150 else {
1151 /*
1152 * The ST Micro variant use the ROD bit for something
1153 * else and only has OD (Open Drain).
1154 */
1155 pwr |= MCI_OD;
1156 }
1157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Ulf Hanssonf4670da2013-01-09 17:19:54 +01001159 /*
1160 * If clock = 0 and the variant requires the MMCIPOWER to be used for
1161 * gating the clock, the MCI_PWR_ON bit is cleared.
1162 */
1163 if (!ios->clock && variant->pwrreg_clkgate)
1164 pwr &= ~MCI_PWR_ON;
1165
Linus Walleija6a64642009-09-14 12:56:14 +01001166 spin_lock_irqsave(&host->lock, flags);
1167
1168 mmci_set_clkreg(host, ios->clock);
Ulf Hansson7437cfa2012-01-18 09:17:27 +01001169 mmci_write_pwrreg(host, pwr);
Linus Walleija6a64642009-09-14 12:56:14 +01001170
1171 spin_unlock_irqrestore(&host->lock, flags);
Ulf Hansson2cd976c2011-12-13 17:01:11 +01001172
Ulf Hansson2cd976c2011-12-13 17:01:11 +01001173 pm_runtime_mark_last_busy(mmc_dev(mmc));
1174 pm_runtime_put_autosuspend(mmc_dev(mmc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175}
1176
Russell King89001442009-07-09 15:16:07 +01001177static int mmci_get_ro(struct mmc_host *mmc)
1178{
1179 struct mmci_host *host = mmc_priv(mmc);
1180
1181 if (host->gpio_wp == -ENOSYS)
1182 return -ENOSYS;
1183
Linus Walleij18a063012010-09-12 12:56:44 +01001184 return gpio_get_value_cansleep(host->gpio_wp);
Russell King89001442009-07-09 15:16:07 +01001185}
1186
1187static int mmci_get_cd(struct mmc_host *mmc)
1188{
1189 struct mmci_host *host = mmc_priv(mmc);
Rabin Vincent29719442010-08-09 12:54:43 +01001190 struct mmci_platform_data *plat = host->plat;
Russell King89001442009-07-09 15:16:07 +01001191 unsigned int status;
1192
Rabin Vincent4b8caec2010-08-09 12:56:40 +01001193 if (host->gpio_cd == -ENOSYS) {
1194 if (!plat->status)
1195 return 1; /* Assume always present */
1196
Rabin Vincent29719442010-08-09 12:54:43 +01001197 status = plat->status(mmc_dev(host->mmc));
Rabin Vincent4b8caec2010-08-09 12:56:40 +01001198 } else
Linus Walleij18a063012010-09-12 12:56:44 +01001199 status = !!gpio_get_value_cansleep(host->gpio_cd)
1200 ^ plat->cd_invert;
Russell King89001442009-07-09 15:16:07 +01001201
Russell King74bc8092010-07-29 15:58:59 +01001202 /*
1203 * Use positive logic throughout - status is zero for no card,
1204 * non-zero for card inserted.
1205 */
1206 return status;
Russell King89001442009-07-09 15:16:07 +01001207}
1208
Rabin Vincent148b8b32010-08-09 12:55:48 +01001209static irqreturn_t mmci_cd_irq(int irq, void *dev_id)
1210{
1211 struct mmci_host *host = dev_id;
1212
1213 mmc_detect_change(host->mmc, msecs_to_jiffies(500));
1214
1215 return IRQ_HANDLED;
1216}
1217
David Brownellab7aefd2006-11-12 17:55:30 -08001218static const struct mmc_host_ops mmci_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 .request = mmci_request,
Per Forlin58c7ccb2011-07-01 18:55:24 +02001220 .pre_req = mmci_pre_request,
1221 .post_req = mmci_post_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 .set_ios = mmci_set_ios,
Russell King89001442009-07-09 15:16:07 +01001223 .get_ro = mmci_get_ro,
1224 .get_cd = mmci_get_cd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225};
1226
Lee Jones000bc9d2012-04-16 10:18:43 +01001227#ifdef CONFIG_OF
1228static void mmci_dt_populate_generic_pdata(struct device_node *np,
1229 struct mmci_platform_data *pdata)
1230{
1231 int bus_width = 0;
1232
Lee Jones9a597012012-04-12 16:51:13 +01001233 pdata->gpio_wp = of_get_named_gpio(np, "wp-gpios", 0);
Lee Jones9a597012012-04-12 16:51:13 +01001234 pdata->gpio_cd = of_get_named_gpio(np, "cd-gpios", 0);
Lee Jones000bc9d2012-04-16 10:18:43 +01001235
1236 if (of_get_property(np, "cd-inverted", NULL))
1237 pdata->cd_invert = true;
1238 else
1239 pdata->cd_invert = false;
1240
1241 of_property_read_u32(np, "max-frequency", &pdata->f_max);
1242 if (!pdata->f_max)
1243 pr_warn("%s has no 'max-frequency' property\n", np->full_name);
1244
1245 if (of_get_property(np, "mmc-cap-mmc-highspeed", NULL))
1246 pdata->capabilities |= MMC_CAP_MMC_HIGHSPEED;
1247 if (of_get_property(np, "mmc-cap-sd-highspeed", NULL))
1248 pdata->capabilities |= MMC_CAP_SD_HIGHSPEED;
1249
1250 of_property_read_u32(np, "bus-width", &bus_width);
1251 switch (bus_width) {
1252 case 0 :
1253 /* No bus-width supplied. */
1254 break;
1255 case 4 :
1256 pdata->capabilities |= MMC_CAP_4_BIT_DATA;
1257 break;
1258 case 8 :
1259 pdata->capabilities |= MMC_CAP_8_BIT_DATA;
1260 break;
1261 default :
1262 pr_warn("%s: Unsupported bus width\n", np->full_name);
1263 }
1264}
Lee Jonesc0a120a2012-05-08 13:59:38 +01001265#else
1266static void mmci_dt_populate_generic_pdata(struct device_node *np,
1267 struct mmci_platform_data *pdata)
1268{
1269 return;
1270}
Lee Jones000bc9d2012-04-16 10:18:43 +01001271#endif
1272
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001273static int mmci_probe(struct amba_device *dev,
Russell Kingaa25afa2011-02-19 15:55:00 +00001274 const struct amba_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
Linus Walleij6ef297f2009-09-22 14:29:36 +01001276 struct mmci_platform_data *plat = dev->dev.platform_data;
Lee Jones000bc9d2012-04-16 10:18:43 +01001277 struct device_node *np = dev->dev.of_node;
Rabin Vincent4956e102010-07-21 12:54:40 +01001278 struct variant_data *variant = id->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 struct mmci_host *host;
1280 struct mmc_host *mmc;
1281 int ret;
1282
Lee Jones000bc9d2012-04-16 10:18:43 +01001283 /* Must have platform data or Device Tree. */
1284 if (!plat && !np) {
1285 dev_err(&dev->dev, "No plat data or DT found\n");
1286 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 }
1288
Lee Jonesb9b52912012-06-12 10:49:51 +01001289 if (!plat) {
1290 plat = devm_kzalloc(&dev->dev, sizeof(*plat), GFP_KERNEL);
1291 if (!plat)
1292 return -ENOMEM;
1293 }
1294
Lee Jones000bc9d2012-04-16 10:18:43 +01001295 if (np)
1296 mmci_dt_populate_generic_pdata(np, plat);
1297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 ret = amba_request_regions(dev, DRIVER_NAME);
1299 if (ret)
1300 goto out;
1301
1302 mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
1303 if (!mmc) {
1304 ret = -ENOMEM;
1305 goto rel_regions;
1306 }
1307
1308 host = mmc_priv(mmc);
Rabin Vincent4ea580f2009-04-17 08:44:19 +05301309 host->mmc = mmc;
Russell King012b7d32009-07-09 15:13:56 +01001310
Russell King89001442009-07-09 15:16:07 +01001311 host->gpio_wp = -ENOSYS;
1312 host->gpio_cd = -ENOSYS;
Rabin Vincent148b8b32010-08-09 12:55:48 +01001313 host->gpio_cd_irq = -1;
Russell King89001442009-07-09 15:16:07 +01001314
Russell King012b7d32009-07-09 15:13:56 +01001315 host->hw_designer = amba_manf(dev);
1316 host->hw_revision = amba_rev(dev);
Linus Walleij64de0282010-02-19 01:09:10 +01001317 dev_dbg(mmc_dev(mmc), "designer ID = 0x%02x\n", host->hw_designer);
1318 dev_dbg(mmc_dev(mmc), "revision = 0x%01x\n", host->hw_revision);
Russell King012b7d32009-07-09 15:13:56 +01001319
Russell Kingee569c42008-11-30 17:38:14 +00001320 host->clk = clk_get(&dev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 if (IS_ERR(host->clk)) {
1322 ret = PTR_ERR(host->clk);
1323 host->clk = NULL;
1324 goto host_free;
1325 }
1326
Julia Lawallac940932012-08-26 16:00:59 +00001327 ret = clk_prepare_enable(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 if (ret)
Russell Kinga8d35842006-01-03 18:41:37 +00001329 goto clk_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 host->plat = plat;
Rabin Vincent4956e102010-07-21 12:54:40 +01001332 host->variant = variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 host->mclk = clk_get_rate(host->clk);
Linus Walleijc8df9a52008-04-29 09:34:07 +01001334 /*
1335 * According to the spec, mclk is max 100 MHz,
1336 * so we try to adjust the clock down to this,
1337 * (if possible).
1338 */
1339 if (host->mclk > 100000000) {
1340 ret = clk_set_rate(host->clk, 100000000);
1341 if (ret < 0)
1342 goto clk_disable;
1343 host->mclk = clk_get_rate(host->clk);
Linus Walleij64de0282010-02-19 01:09:10 +01001344 dev_dbg(mmc_dev(mmc), "eventual mclk rate: %u Hz\n",
1345 host->mclk);
Linus Walleijc8df9a52008-04-29 09:34:07 +01001346 }
Russell Kingc8ebae32011-01-11 19:35:53 +00001347 host->phybase = dev->res.start;
Linus Walleijdc890c22009-06-07 23:27:31 +01001348 host->base = ioremap(dev->res.start, resource_size(&dev->res));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 if (!host->base) {
1350 ret = -ENOMEM;
1351 goto clk_disable;
1352 }
1353
1354 mmc->ops = &mmci_ops;
Linus Walleij7f294e42011-07-08 09:57:15 +01001355 /*
1356 * The ARM and ST versions of the block have slightly different
1357 * clock divider equations which means that the minimum divider
1358 * differs too.
1359 */
1360 if (variant->st_clkdiv)
1361 mmc->f_min = DIV_ROUND_UP(host->mclk, 257);
1362 else
1363 mmc->f_min = DIV_ROUND_UP(host->mclk, 512);
Linus Walleij808d97c2010-04-08 07:39:38 +01001364 /*
1365 * If the platform data supplies a maximum operating
1366 * frequency, this takes precedence. Else, we fall back
1367 * to using the module parameter, which has a (low)
1368 * default value in case it is not specified. Either
1369 * value must not exceed the clock rate into the block,
1370 * of course.
1371 */
1372 if (plat->f_max)
1373 mmc->f_max = min(host->mclk, plat->f_max);
1374 else
1375 mmc->f_max = min(host->mclk, fmax);
Linus Walleij64de0282010-02-19 01:09:10 +01001376 dev_dbg(mmc_dev(mmc), "clocking block at %u Hz\n", mmc->f_max);
1377
Linus Walleija9a83782012-10-29 14:39:30 +01001378 host->pinctrl = devm_pinctrl_get(&dev->dev);
1379 if (IS_ERR(host->pinctrl)) {
1380 ret = PTR_ERR(host->pinctrl);
1381 goto clk_disable;
1382 }
1383
1384 host->pins_default = pinctrl_lookup_state(host->pinctrl,
1385 PINCTRL_STATE_DEFAULT);
1386
1387 /* enable pins to be muxed in and configured */
1388 if (!IS_ERR(host->pins_default)) {
1389 ret = pinctrl_select_state(host->pinctrl, host->pins_default);
1390 if (ret)
1391 dev_warn(&dev->dev, "could not set default pins\n");
1392 } else
1393 dev_warn(&dev->dev, "could not get default pinstate\n");
1394
Ulf Hansson599c1d52013-01-07 16:22:50 +01001395 /* Get regulators and the supported OCR mask */
1396 mmc_regulator_get_supply(mmc);
1397 if (!mmc->ocr_avail)
Linus Walleij34e84f32009-09-22 14:41:40 +01001398 mmc->ocr_avail = plat->ocr_mask;
Ulf Hansson599c1d52013-01-07 16:22:50 +01001399 else if (plat->ocr_mask)
1400 dev_warn(mmc_dev(mmc), "Platform OCR mask is ignored\n");
1401
Linus Walleij9e6c82c2009-09-14 12:57:11 +01001402 mmc->caps = plat->capabilities;
Per Forlin5a092622011-11-14 12:02:28 +01001403 mmc->caps2 = plat->capabilities2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Ulf Hansson70be2082013-01-07 15:35:06 +01001405 /* We support these PM capabilities. */
1406 mmc->pm_caps = MMC_PM_KEEP_POWER;
1407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 /*
1409 * We can do SGIO
1410 */
Martin K. Petersena36274e2010-09-10 01:33:59 -04001411 mmc->max_segs = NR_SG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
1413 /*
Rabin Vincent08458ef2010-07-21 12:55:59 +01001414 * Since only a certain number of bits are valid in the data length
1415 * register, we must ensure that we don't exceed 2^num-1 bytes in a
1416 * single request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 */
Rabin Vincent08458ef2010-07-21 12:55:59 +01001418 mmc->max_req_size = (1 << variant->datalength_bits) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
1420 /*
1421 * Set the maximum segment size. Since we aren't doing DMA
1422 * (yet) we are only limited by the data length register.
1423 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001424 mmc->max_seg_size = mmc->max_req_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001426 /*
1427 * Block size can be up to 2048 bytes, but must be a power of two.
1428 */
Will Deacon8f7f6b72012-02-24 11:25:21 +00001429 mmc->max_blk_size = 1 << 11;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001430
Pierre Ossman55db8902006-11-21 17:55:45 +01001431 /*
Will Deacon8f7f6b72012-02-24 11:25:21 +00001432 * Limit the number of blocks transferred so that we don't overflow
1433 * the maximum request size.
Pierre Ossman55db8902006-11-21 17:55:45 +01001434 */
Will Deacon8f7f6b72012-02-24 11:25:21 +00001435 mmc->max_blk_count = mmc->max_req_size >> 11;
Pierre Ossman55db8902006-11-21 17:55:45 +01001436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 spin_lock_init(&host->lock);
1438
1439 writel(0, host->base + MMCIMASK0);
1440 writel(0, host->base + MMCIMASK1);
1441 writel(0xfff, host->base + MMCICLEAR);
1442
Roland Stigge2805b9a2012-06-17 21:14:27 +01001443 if (plat->gpio_cd == -EPROBE_DEFER) {
1444 ret = -EPROBE_DEFER;
1445 goto err_gpio_cd;
1446 }
Russell King89001442009-07-09 15:16:07 +01001447 if (gpio_is_valid(plat->gpio_cd)) {
1448 ret = gpio_request(plat->gpio_cd, DRIVER_NAME " (cd)");
1449 if (ret == 0)
1450 ret = gpio_direction_input(plat->gpio_cd);
1451 if (ret == 0)
1452 host->gpio_cd = plat->gpio_cd;
1453 else if (ret != -ENOSYS)
1454 goto err_gpio_cd;
Rabin Vincent148b8b32010-08-09 12:55:48 +01001455
Linus Walleij17ee0832011-05-05 17:23:10 +01001456 /*
1457 * A gpio pin that will detect cards when inserted and removed
1458 * will most likely want to trigger on the edges if it is
1459 * 0 when ejected and 1 when inserted (or mutatis mutandis
1460 * for the inverted case) so we request triggers on both
1461 * edges.
1462 */
Rabin Vincent148b8b32010-08-09 12:55:48 +01001463 ret = request_any_context_irq(gpio_to_irq(plat->gpio_cd),
Linus Walleij17ee0832011-05-05 17:23:10 +01001464 mmci_cd_irq,
1465 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
1466 DRIVER_NAME " (cd)", host);
Rabin Vincent148b8b32010-08-09 12:55:48 +01001467 if (ret >= 0)
1468 host->gpio_cd_irq = gpio_to_irq(plat->gpio_cd);
Russell King89001442009-07-09 15:16:07 +01001469 }
Roland Stigge2805b9a2012-06-17 21:14:27 +01001470 if (plat->gpio_wp == -EPROBE_DEFER) {
1471 ret = -EPROBE_DEFER;
1472 goto err_gpio_wp;
1473 }
Russell King89001442009-07-09 15:16:07 +01001474 if (gpio_is_valid(plat->gpio_wp)) {
1475 ret = gpio_request(plat->gpio_wp, DRIVER_NAME " (wp)");
1476 if (ret == 0)
1477 ret = gpio_direction_input(plat->gpio_wp);
1478 if (ret == 0)
1479 host->gpio_wp = plat->gpio_wp;
1480 else if (ret != -ENOSYS)
1481 goto err_gpio_wp;
1482 }
1483
Rabin Vincent4b8caec2010-08-09 12:56:40 +01001484 if ((host->plat->status || host->gpio_cd != -ENOSYS)
1485 && host->gpio_cd_irq < 0)
Rabin Vincent148b8b32010-08-09 12:55:48 +01001486 mmc->caps |= MMC_CAP_NEEDS_POLL;
1487
Thomas Gleixnerdace1452006-07-01 19:29:38 -07001488 ret = request_irq(dev->irq[0], mmci_irq, IRQF_SHARED, DRIVER_NAME " (cmd)", host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 if (ret)
1490 goto unmap;
1491
Russell Kingdfb85182012-05-03 11:33:15 +01001492 if (!dev->irq[1])
Linus Walleij2686b4b2010-10-19 12:39:48 +01001493 host->singleirq = true;
1494 else {
1495 ret = request_irq(dev->irq[1], mmci_pio_irq, IRQF_SHARED,
1496 DRIVER_NAME " (pio)", host);
1497 if (ret)
1498 goto irq0_free;
1499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Linus Walleij8cb28152011-01-24 15:22:13 +01001501 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
1503 amba_set_drvdata(dev, mmc);
1504
Russell Kingc8ebae32011-01-11 19:35:53 +00001505 dev_info(&dev->dev, "%s: PL%03x manf %x rev%u at 0x%08llx irq %d,%d (pio)\n",
1506 mmc_hostname(mmc), amba_part(dev), amba_manf(dev),
1507 amba_rev(dev), (unsigned long long)dev->res.start,
1508 dev->irq[0], dev->irq[1]);
1509
1510 mmci_dma_setup(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Ulf Hansson2cd976c2011-12-13 17:01:11 +01001512 pm_runtime_set_autosuspend_delay(&dev->dev, 50);
1513 pm_runtime_use_autosuspend(&dev->dev);
Russell King1c3be362011-08-14 09:17:05 +01001514 pm_runtime_put(&dev->dev);
1515
Russell King8c11a942010-12-28 19:40:40 +00001516 mmc_add_host(mmc);
1517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 return 0;
1519
1520 irq0_free:
1521 free_irq(dev->irq[0], host);
1522 unmap:
Russell King89001442009-07-09 15:16:07 +01001523 if (host->gpio_wp != -ENOSYS)
1524 gpio_free(host->gpio_wp);
1525 err_gpio_wp:
Rabin Vincent148b8b32010-08-09 12:55:48 +01001526 if (host->gpio_cd_irq >= 0)
1527 free_irq(host->gpio_cd_irq, host);
Russell King89001442009-07-09 15:16:07 +01001528 if (host->gpio_cd != -ENOSYS)
1529 gpio_free(host->gpio_cd);
1530 err_gpio_cd:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 iounmap(host->base);
1532 clk_disable:
Julia Lawallac940932012-08-26 16:00:59 +00001533 clk_disable_unprepare(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 clk_free:
1535 clk_put(host->clk);
1536 host_free:
1537 mmc_free_host(mmc);
1538 rel_regions:
1539 amba_release_regions(dev);
1540 out:
1541 return ret;
1542}
1543
Bill Pemberton6e0ee712012-11-19 13:26:03 -05001544static int mmci_remove(struct amba_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545{
1546 struct mmc_host *mmc = amba_get_drvdata(dev);
1547
1548 amba_set_drvdata(dev, NULL);
1549
1550 if (mmc) {
1551 struct mmci_host *host = mmc_priv(mmc);
1552
Russell King1c3be362011-08-14 09:17:05 +01001553 /*
1554 * Undo pm_runtime_put() in probe. We use the _sync
1555 * version here so that we can access the primecell.
1556 */
1557 pm_runtime_get_sync(&dev->dev);
1558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 mmc_remove_host(mmc);
1560
1561 writel(0, host->base + MMCIMASK0);
1562 writel(0, host->base + MMCIMASK1);
1563
1564 writel(0, host->base + MMCICOMMAND);
1565 writel(0, host->base + MMCIDATACTRL);
1566
Russell Kingc8ebae32011-01-11 19:35:53 +00001567 mmci_dma_release(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 free_irq(dev->irq[0], host);
Linus Walleij2686b4b2010-10-19 12:39:48 +01001569 if (!host->singleirq)
1570 free_irq(dev->irq[1], host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Russell King89001442009-07-09 15:16:07 +01001572 if (host->gpio_wp != -ENOSYS)
1573 gpio_free(host->gpio_wp);
Rabin Vincent148b8b32010-08-09 12:55:48 +01001574 if (host->gpio_cd_irq >= 0)
1575 free_irq(host->gpio_cd_irq, host);
Russell King89001442009-07-09 15:16:07 +01001576 if (host->gpio_cd != -ENOSYS)
1577 gpio_free(host->gpio_cd);
1578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 iounmap(host->base);
Julia Lawallac940932012-08-26 16:00:59 +00001580 clk_disable_unprepare(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 clk_put(host->clk);
1582
1583 mmc_free_host(mmc);
1584
1585 amba_release_regions(dev);
1586 }
1587
1588 return 0;
1589}
1590
Ulf Hansson48fa7002011-12-13 16:59:34 +01001591#ifdef CONFIG_SUSPEND
1592static int mmci_suspend(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
Ulf Hansson48fa7002011-12-13 16:59:34 +01001594 struct amba_device *adev = to_amba_device(dev);
1595 struct mmc_host *mmc = amba_get_drvdata(adev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 int ret = 0;
1597
1598 if (mmc) {
1599 struct mmci_host *host = mmc_priv(mmc);
1600
Matt Fleming1a13f8f2010-05-26 14:42:08 -07001601 ret = mmc_suspend_host(mmc);
Ulf Hansson2cd976c2011-12-13 17:01:11 +01001602 if (ret == 0) {
1603 pm_runtime_get_sync(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 writel(0, host->base + MMCIMASK0);
Ulf Hansson2cd976c2011-12-13 17:01:11 +01001605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 }
1607
1608 return ret;
1609}
1610
Ulf Hansson48fa7002011-12-13 16:59:34 +01001611static int mmci_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612{
Ulf Hansson48fa7002011-12-13 16:59:34 +01001613 struct amba_device *adev = to_amba_device(dev);
1614 struct mmc_host *mmc = amba_get_drvdata(adev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 int ret = 0;
1616
1617 if (mmc) {
1618 struct mmci_host *host = mmc_priv(mmc);
1619
1620 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
Ulf Hansson2cd976c2011-12-13 17:01:11 +01001621 pm_runtime_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
1623 ret = mmc_resume_host(mmc);
1624 }
1625
1626 return ret;
1627}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628#endif
1629
Ulf Hansson82592932013-01-09 11:15:26 +01001630#ifdef CONFIG_PM_RUNTIME
1631static int mmci_runtime_suspend(struct device *dev)
1632{
1633 struct amba_device *adev = to_amba_device(dev);
1634 struct mmc_host *mmc = amba_get_drvdata(adev);
1635
1636 if (mmc) {
1637 struct mmci_host *host = mmc_priv(mmc);
1638 clk_disable_unprepare(host->clk);
1639 }
1640
1641 return 0;
1642}
1643
1644static int mmci_runtime_resume(struct device *dev)
1645{
1646 struct amba_device *adev = to_amba_device(dev);
1647 struct mmc_host *mmc = amba_get_drvdata(adev);
1648
1649 if (mmc) {
1650 struct mmci_host *host = mmc_priv(mmc);
1651 clk_prepare_enable(host->clk);
1652 }
1653
1654 return 0;
1655}
1656#endif
1657
Ulf Hansson48fa7002011-12-13 16:59:34 +01001658static const struct dev_pm_ops mmci_dev_pm_ops = {
1659 SET_SYSTEM_SLEEP_PM_OPS(mmci_suspend, mmci_resume)
Ulf Hansson82592932013-01-09 11:15:26 +01001660 SET_RUNTIME_PM_OPS(mmci_runtime_suspend, mmci_runtime_resume, NULL)
Ulf Hansson48fa7002011-12-13 16:59:34 +01001661};
1662
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663static struct amba_id mmci_ids[] = {
1664 {
1665 .id = 0x00041180,
Pawel Moll768fbc12011-03-11 17:18:07 +00001666 .mask = 0xff0fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001667 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 },
1669 {
Pawel Moll768fbc12011-03-11 17:18:07 +00001670 .id = 0x01041180,
1671 .mask = 0xff0fffff,
1672 .data = &variant_arm_extended_fifo,
1673 },
1674 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 .id = 0x00041181,
1676 .mask = 0x000fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001677 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 },
Linus Walleijcc30d602009-01-04 15:18:54 +01001679 /* ST Micro variants */
1680 {
1681 .id = 0x00180180,
1682 .mask = 0x00ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001683 .data = &variant_u300,
Linus Walleijcc30d602009-01-04 15:18:54 +01001684 },
1685 {
Linus Walleij34fd4212012-04-10 17:43:59 +01001686 .id = 0x10180180,
1687 .mask = 0xf0ffffff,
1688 .data = &variant_nomadik,
1689 },
1690 {
Linus Walleijcc30d602009-01-04 15:18:54 +01001691 .id = 0x00280180,
1692 .mask = 0x00ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001693 .data = &variant_u300,
1694 },
1695 {
1696 .id = 0x00480180,
Philippe Langlais1784b152011-03-25 08:51:52 +01001697 .mask = 0xf0ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001698 .data = &variant_ux500,
Linus Walleijcc30d602009-01-04 15:18:54 +01001699 },
Philippe Langlais1784b152011-03-25 08:51:52 +01001700 {
1701 .id = 0x10480180,
1702 .mask = 0xf0ffffff,
1703 .data = &variant_ux500v2,
1704 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 { 0, 0 },
1706};
1707
Dave Martin9f998352011-10-05 15:15:21 +01001708MODULE_DEVICE_TABLE(amba, mmci_ids);
1709
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710static struct amba_driver mmci_driver = {
1711 .drv = {
1712 .name = DRIVER_NAME,
Ulf Hansson48fa7002011-12-13 16:59:34 +01001713 .pm = &mmci_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 },
1715 .probe = mmci_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05001716 .remove = mmci_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 .id_table = mmci_ids,
1718};
1719
viresh kumar9e5ed092012-03-15 10:40:38 +01001720module_amba_driver(mmci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722module_param(fmax, uint, 0444);
1723
1724MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
1725MODULE_LICENSE("GPL");