blob: 604e41db00db5ed637e44ab7c4f82b3fe3b7dd68 [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
Ulf Hansson01259622013-05-15 20:53:22 +010064 * @busy_detect: true if busy detection on dat0 is supported
Rabin Vincent4956e102010-07-21 12:54:40 +010065 */
66struct variant_data {
67 unsigned int clkreg;
Rabin Vincent4380c142010-07-21 12:55:18 +010068 unsigned int clkreg_enable;
Rabin Vincent08458ef2010-07-21 12:55:59 +010069 unsigned int datalength_bits;
Rabin Vincent8301bb62010-08-09 12:57:30 +010070 unsigned int fifosize;
71 unsigned int fifohalfsize;
Linus Walleij34177802010-10-19 12:43:58 +010072 bool sdio;
Linus Walleijb70a67f2010-12-06 09:24:14 +010073 bool st_clkdiv;
Philippe Langlais1784b152011-03-25 08:51:52 +010074 bool blksz_datactrl16;
Ulf Hansson7d72a1d2011-12-13 16:54:55 +010075 u32 pwrreg_powerup;
Ulf Hansson4d1a3a02011-12-13 16:57:07 +010076 bool signal_direction;
Ulf Hanssonf4670da2013-01-09 17:19:54 +010077 bool pwrreg_clkgate;
Ulf Hansson01259622013-05-15 20:53:22 +010078 bool busy_detect;
Rabin Vincent4956e102010-07-21 12:54:40 +010079};
80
81static struct variant_data variant_arm = {
Rabin Vincent8301bb62010-08-09 12:57:30 +010082 .fifosize = 16 * 4,
83 .fifohalfsize = 8 * 4,
Rabin Vincent08458ef2010-07-21 12:55:59 +010084 .datalength_bits = 16,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +010085 .pwrreg_powerup = MCI_PWR_UP,
Rabin Vincent4956e102010-07-21 12:54:40 +010086};
87
Pawel Moll768fbc12011-03-11 17:18:07 +000088static struct variant_data variant_arm_extended_fifo = {
89 .fifosize = 128 * 4,
90 .fifohalfsize = 64 * 4,
91 .datalength_bits = 16,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +010092 .pwrreg_powerup = MCI_PWR_UP,
Pawel Moll768fbc12011-03-11 17:18:07 +000093};
94
Pawel Moll3a372982013-01-24 14:12:45 +010095static struct variant_data variant_arm_extended_fifo_hwfc = {
96 .fifosize = 128 * 4,
97 .fifohalfsize = 64 * 4,
98 .clkreg_enable = MCI_ARM_HWFCEN,
99 .datalength_bits = 16,
100 .pwrreg_powerup = MCI_PWR_UP,
101};
102
Rabin Vincent4956e102010-07-21 12:54:40 +0100103static struct variant_data variant_u300 = {
Rabin Vincent8301bb62010-08-09 12:57:30 +0100104 .fifosize = 16 * 4,
105 .fifohalfsize = 8 * 4,
Linus Walleij49ac2152011-03-04 14:54:16 +0100106 .clkreg_enable = MCI_ST_U300_HWFCEN,
Rabin Vincent08458ef2010-07-21 12:55:59 +0100107 .datalength_bits = 16,
Linus Walleij34177802010-10-19 12:43:58 +0100108 .sdio = true,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +0100109 .pwrreg_powerup = MCI_PWR_ON,
Ulf Hansson4d1a3a02011-12-13 16:57:07 +0100110 .signal_direction = true,
Ulf Hanssonf4670da2013-01-09 17:19:54 +0100111 .pwrreg_clkgate = true,
Rabin Vincent4956e102010-07-21 12:54:40 +0100112};
113
Linus Walleij34fd4212012-04-10 17:43:59 +0100114static struct variant_data variant_nomadik = {
115 .fifosize = 16 * 4,
116 .fifohalfsize = 8 * 4,
117 .clkreg = MCI_CLK_ENABLE,
118 .datalength_bits = 24,
119 .sdio = true,
120 .st_clkdiv = true,
121 .pwrreg_powerup = MCI_PWR_ON,
122 .signal_direction = true,
Ulf Hanssonf4670da2013-01-09 17:19:54 +0100123 .pwrreg_clkgate = true,
Linus Walleij34fd4212012-04-10 17:43:59 +0100124};
125
Rabin Vincent4956e102010-07-21 12:54:40 +0100126static struct variant_data variant_ux500 = {
Rabin Vincent8301bb62010-08-09 12:57:30 +0100127 .fifosize = 30 * 4,
128 .fifohalfsize = 8 * 4,
Rabin Vincent4956e102010-07-21 12:54:40 +0100129 .clkreg = MCI_CLK_ENABLE,
Linus Walleij49ac2152011-03-04 14:54:16 +0100130 .clkreg_enable = MCI_ST_UX500_HWFCEN,
Rabin Vincent08458ef2010-07-21 12:55:59 +0100131 .datalength_bits = 24,
Linus Walleij34177802010-10-19 12:43:58 +0100132 .sdio = true,
Linus Walleijb70a67f2010-12-06 09:24:14 +0100133 .st_clkdiv = true,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +0100134 .pwrreg_powerup = MCI_PWR_ON,
Ulf Hansson4d1a3a02011-12-13 16:57:07 +0100135 .signal_direction = true,
Ulf Hanssonf4670da2013-01-09 17:19:54 +0100136 .pwrreg_clkgate = true,
Ulf Hansson01259622013-05-15 20:53:22 +0100137 .busy_detect = true,
Rabin Vincent4956e102010-07-21 12:54:40 +0100138};
Linus Walleijb70a67f2010-12-06 09:24:14 +0100139
Philippe Langlais1784b152011-03-25 08:51:52 +0100140static struct variant_data variant_ux500v2 = {
141 .fifosize = 30 * 4,
142 .fifohalfsize = 8 * 4,
143 .clkreg = MCI_CLK_ENABLE,
144 .clkreg_enable = MCI_ST_UX500_HWFCEN,
145 .datalength_bits = 24,
146 .sdio = true,
147 .st_clkdiv = true,
148 .blksz_datactrl16 = true,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +0100149 .pwrreg_powerup = MCI_PWR_ON,
Ulf Hansson4d1a3a02011-12-13 16:57:07 +0100150 .signal_direction = true,
Ulf Hanssonf4670da2013-01-09 17:19:54 +0100151 .pwrreg_clkgate = true,
Ulf Hansson01259622013-05-15 20:53:22 +0100152 .busy_detect = true,
Philippe Langlais1784b152011-03-25 08:51:52 +0100153};
154
Ulf Hansson01259622013-05-15 20:53:22 +0100155static int mmci_card_busy(struct mmc_host *mmc)
156{
157 struct mmci_host *host = mmc_priv(mmc);
158 unsigned long flags;
159 int busy = 0;
160
161 pm_runtime_get_sync(mmc_dev(mmc));
162
163 spin_lock_irqsave(&host->lock, flags);
164 if (readl(host->base + MMCISTATUS) & MCI_ST_CARDBUSY)
165 busy = 1;
166 spin_unlock_irqrestore(&host->lock, flags);
167
168 pm_runtime_mark_last_busy(mmc_dev(mmc));
169 pm_runtime_put_autosuspend(mmc_dev(mmc));
170
171 return busy;
172}
173
Linus Walleija6a64642009-09-14 12:56:14 +0100174/*
Ulf Hansson653a7612013-01-21 21:29:34 +0100175 * Validate mmc prerequisites
176 */
177static int mmci_validate_data(struct mmci_host *host,
178 struct mmc_data *data)
179{
180 if (!data)
181 return 0;
182
183 if (!is_power_of_2(data->blksz)) {
184 dev_err(mmc_dev(host->mmc),
185 "unsupported block size (%d bytes)\n", data->blksz);
186 return -EINVAL;
187 }
188
189 return 0;
190}
191
Ulf Hanssonf829c042013-09-04 09:01:15 +0100192static void mmci_reg_delay(struct mmci_host *host)
193{
194 /*
195 * According to the spec, at least three feedback clock cycles
196 * of max 52 MHz must pass between two writes to the MMCICLOCK reg.
197 * Three MCLK clock cycles must pass between two MMCIPOWER reg writes.
198 * Worst delay time during card init is at 100 kHz => 30 us.
199 * Worst delay time when up and running is at 25 MHz => 120 ns.
200 */
201 if (host->cclk < 25000000)
202 udelay(30);
203 else
204 ndelay(120);
205}
206
Ulf Hansson653a7612013-01-21 21:29:34 +0100207/*
Linus Walleija6a64642009-09-14 12:56:14 +0100208 * This must be called with host->lock held
209 */
Ulf Hansson7437cfa2012-01-18 09:17:27 +0100210static void mmci_write_clkreg(struct mmci_host *host, u32 clk)
211{
212 if (host->clk_reg != clk) {
213 host->clk_reg = clk;
214 writel(clk, host->base + MMCICLOCK);
215 }
216}
217
218/*
219 * This must be called with host->lock held
220 */
221static void mmci_write_pwrreg(struct mmci_host *host, u32 pwr)
222{
223 if (host->pwr_reg != pwr) {
224 host->pwr_reg = pwr;
225 writel(pwr, host->base + MMCIPOWER);
226 }
227}
228
229/*
230 * This must be called with host->lock held
231 */
Ulf Hansson9cc639a2013-05-15 20:48:23 +0100232static void mmci_write_datactrlreg(struct mmci_host *host, u32 datactrl)
233{
Ulf Hansson01259622013-05-15 20:53:22 +0100234 /* Keep ST Micro busy mode if enabled */
235 datactrl |= host->datactrl_reg & MCI_ST_DPSM_BUSYMODE;
236
Ulf Hansson9cc639a2013-05-15 20:48:23 +0100237 if (host->datactrl_reg != datactrl) {
238 host->datactrl_reg = datactrl;
239 writel(datactrl, host->base + MMCIDATACTRL);
240 }
241}
242
243/*
244 * This must be called with host->lock held
245 */
Linus Walleija6a64642009-09-14 12:56:14 +0100246static void mmci_set_clkreg(struct mmci_host *host, unsigned int desired)
247{
Rabin Vincent4956e102010-07-21 12:54:40 +0100248 struct variant_data *variant = host->variant;
249 u32 clk = variant->clkreg;
Linus Walleija6a64642009-09-14 12:56:14 +0100250
Ulf Hanssonc58a8502013-05-13 15:40:03 +0100251 /* Make sure cclk reflects the current calculated clock */
252 host->cclk = 0;
253
Linus Walleija6a64642009-09-14 12:56:14 +0100254 if (desired) {
255 if (desired >= host->mclk) {
Linus Walleij991a86e2010-12-10 09:35:53 +0100256 clk = MCI_CLK_BYPASS;
Linus Walleij399bc482011-04-01 07:59:17 +0100257 if (variant->st_clkdiv)
258 clk |= MCI_ST_UX500_NEG_EDGE;
Linus Walleija6a64642009-09-14 12:56:14 +0100259 host->cclk = host->mclk;
Linus Walleijb70a67f2010-12-06 09:24:14 +0100260 } else if (variant->st_clkdiv) {
261 /*
262 * DB8500 TRM says f = mclk / (clkdiv + 2)
263 * => clkdiv = (mclk / f) - 2
264 * Round the divider up so we don't exceed the max
265 * frequency
266 */
267 clk = DIV_ROUND_UP(host->mclk, desired) - 2;
268 if (clk >= 256)
269 clk = 255;
270 host->cclk = host->mclk / (clk + 2);
Linus Walleija6a64642009-09-14 12:56:14 +0100271 } else {
Linus Walleijb70a67f2010-12-06 09:24:14 +0100272 /*
273 * PL180 TRM says f = mclk / (2 * (clkdiv + 1))
274 * => clkdiv = mclk / (2 * f) - 1
275 */
Linus Walleija6a64642009-09-14 12:56:14 +0100276 clk = host->mclk / (2 * desired) - 1;
277 if (clk >= 256)
278 clk = 255;
279 host->cclk = host->mclk / (2 * (clk + 1));
280 }
Rabin Vincent4380c142010-07-21 12:55:18 +0100281
282 clk |= variant->clkreg_enable;
Linus Walleija6a64642009-09-14 12:56:14 +0100283 clk |= MCI_CLK_ENABLE;
284 /* This hasn't proven to be worthwhile */
285 /* clk |= MCI_CLK_PWRSAVE; */
286 }
287
Ulf Hanssonc58a8502013-05-13 15:40:03 +0100288 /* Set actual clock for debug */
289 host->mmc->actual_clock = host->cclk;
290
Linus Walleij9e6c82c2009-09-14 12:57:11 +0100291 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4)
Linus Walleij771dc152010-04-08 07:38:52 +0100292 clk |= MCI_4BIT_BUS;
293 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_8)
294 clk |= MCI_ST_8BIT_BUS;
Linus Walleij9e6c82c2009-09-14 12:57:11 +0100295
Ulf Hansson6dbb6ee2013-01-07 15:30:44 +0100296 if (host->mmc->ios.timing == MMC_TIMING_UHS_DDR50)
297 clk |= MCI_ST_UX500_NEG_EDGE;
298
Ulf Hansson7437cfa2012-01-18 09:17:27 +0100299 mmci_write_clkreg(host, clk);
Linus Walleija6a64642009-09-14 12:56:14 +0100300}
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302static void
303mmci_request_end(struct mmci_host *host, struct mmc_request *mrq)
304{
305 writel(0, host->base + MMCICOMMAND);
306
Russell Kinge47c2222007-01-08 16:42:51 +0000307 BUG_ON(host->data);
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 host->mrq = NULL;
310 host->cmd = NULL;
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 mmc_request_done(host->mmc, mrq);
Ulf Hansson2cd976c2011-12-13 17:01:11 +0100313
314 pm_runtime_mark_last_busy(mmc_dev(host->mmc));
315 pm_runtime_put_autosuspend(mmc_dev(host->mmc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
317
Linus Walleij2686b4b2010-10-19 12:39:48 +0100318static void mmci_set_mask1(struct mmci_host *host, unsigned int mask)
319{
320 void __iomem *base = host->base;
321
322 if (host->singleirq) {
323 unsigned int mask0 = readl(base + MMCIMASK0);
324
325 mask0 &= ~MCI_IRQ1MASK;
326 mask0 |= mask;
327
328 writel(mask0, base + MMCIMASK0);
329 }
330
331 writel(mask, base + MMCIMASK1);
332}
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334static void mmci_stop_data(struct mmci_host *host)
335{
Ulf Hansson9cc639a2013-05-15 20:48:23 +0100336 mmci_write_datactrlreg(host, 0);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100337 mmci_set_mask1(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 host->data = NULL;
339}
340
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100341static void mmci_init_sg(struct mmci_host *host, struct mmc_data *data)
342{
343 unsigned int flags = SG_MITER_ATOMIC;
344
345 if (data->flags & MMC_DATA_READ)
346 flags |= SG_MITER_TO_SG;
347 else
348 flags |= SG_MITER_FROM_SG;
349
350 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
351}
352
Russell Kingc8ebae32011-01-11 19:35:53 +0000353/*
354 * All the DMA operation mode stuff goes inside this ifdef.
355 * This assumes that you have a generic DMA device interface,
356 * no custom DMA interfaces are supported.
357 */
358#ifdef CONFIG_DMA_ENGINE
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500359static void mmci_dma_setup(struct mmci_host *host)
Russell Kingc8ebae32011-01-11 19:35:53 +0000360{
361 struct mmci_platform_data *plat = host->plat;
362 const char *rxname, *txname;
363 dma_cap_mask_t mask;
364
Lee Jones1fd83f02013-05-03 12:51:17 +0100365 host->dma_rx_channel = dma_request_slave_channel(mmc_dev(host->mmc), "rx");
366 host->dma_tx_channel = dma_request_slave_channel(mmc_dev(host->mmc), "tx");
Russell Kingc8ebae32011-01-11 19:35:53 +0000367
Per Forlin58c7ccb2011-07-01 18:55:24 +0200368 /* initialize pre request cookie */
369 host->next_data.cookie = 1;
370
Russell Kingc8ebae32011-01-11 19:35:53 +0000371 /* Try to acquire a generic DMA engine slave channel */
372 dma_cap_zero(mask);
373 dma_cap_set(DMA_SLAVE, mask);
374
Lee Jones1fd83f02013-05-03 12:51:17 +0100375 if (plat && plat->dma_filter) {
376 if (!host->dma_rx_channel && plat->dma_rx_param) {
377 host->dma_rx_channel = dma_request_channel(mask,
378 plat->dma_filter,
379 plat->dma_rx_param);
380 /* E.g if no DMA hardware is present */
381 if (!host->dma_rx_channel)
382 dev_err(mmc_dev(host->mmc), "no RX DMA channel\n");
383 }
384
385 if (!host->dma_tx_channel && plat->dma_tx_param) {
386 host->dma_tx_channel = dma_request_channel(mask,
387 plat->dma_filter,
388 plat->dma_tx_param);
389 if (!host->dma_tx_channel)
390 dev_warn(mmc_dev(host->mmc), "no TX DMA channel\n");
391 }
392 }
393
Russell Kingc8ebae32011-01-11 19:35:53 +0000394 /*
395 * If only an RX channel is specified, the driver will
396 * attempt to use it bidirectionally, however if it is
397 * is specified but cannot be located, DMA will be disabled.
398 */
Lee Jones1fd83f02013-05-03 12:51:17 +0100399 if (host->dma_rx_channel && !host->dma_tx_channel)
Russell Kingc8ebae32011-01-11 19:35:53 +0000400 host->dma_tx_channel = host->dma_rx_channel;
Russell Kingc8ebae32011-01-11 19:35:53 +0000401
402 if (host->dma_rx_channel)
403 rxname = dma_chan_name(host->dma_rx_channel);
404 else
405 rxname = "none";
406
407 if (host->dma_tx_channel)
408 txname = dma_chan_name(host->dma_tx_channel);
409 else
410 txname = "none";
411
412 dev_info(mmc_dev(host->mmc), "DMA channels RX %s, TX %s\n",
413 rxname, txname);
414
415 /*
416 * Limit the maximum segment size in any SG entry according to
417 * the parameters of the DMA engine device.
418 */
419 if (host->dma_tx_channel) {
420 struct device *dev = host->dma_tx_channel->device->dev;
421 unsigned int max_seg_size = dma_get_max_seg_size(dev);
422
423 if (max_seg_size < host->mmc->max_seg_size)
424 host->mmc->max_seg_size = max_seg_size;
425 }
426 if (host->dma_rx_channel) {
427 struct device *dev = host->dma_rx_channel->device->dev;
428 unsigned int max_seg_size = dma_get_max_seg_size(dev);
429
430 if (max_seg_size < host->mmc->max_seg_size)
431 host->mmc->max_seg_size = max_seg_size;
432 }
433}
434
435/*
Bill Pemberton6e0ee712012-11-19 13:26:03 -0500436 * This is used in or so inline it
Russell Kingc8ebae32011-01-11 19:35:53 +0000437 * so it can be discarded.
438 */
439static inline void mmci_dma_release(struct mmci_host *host)
440{
441 struct mmci_platform_data *plat = host->plat;
442
443 if (host->dma_rx_channel)
444 dma_release_channel(host->dma_rx_channel);
445 if (host->dma_tx_channel && plat->dma_tx_param)
446 dma_release_channel(host->dma_tx_channel);
447 host->dma_rx_channel = host->dma_tx_channel = NULL;
448}
449
Ulf Hansson653a7612013-01-21 21:29:34 +0100450static void mmci_dma_data_error(struct mmci_host *host)
451{
452 dev_err(mmc_dev(host->mmc), "error during DMA transfer!\n");
453 dmaengine_terminate_all(host->dma_current);
454 host->dma_current = NULL;
455 host->dma_desc_current = NULL;
456 host->data->host_cookie = 0;
457}
458
Russell Kingc8ebae32011-01-11 19:35:53 +0000459static void mmci_dma_unmap(struct mmci_host *host, struct mmc_data *data)
460{
Ulf Hansson653a7612013-01-21 21:29:34 +0100461 struct dma_chan *chan;
Russell Kingc8ebae32011-01-11 19:35:53 +0000462 enum dma_data_direction dir;
Ulf Hansson653a7612013-01-21 21:29:34 +0100463
464 if (data->flags & MMC_DATA_READ) {
465 dir = DMA_FROM_DEVICE;
466 chan = host->dma_rx_channel;
467 } else {
468 dir = DMA_TO_DEVICE;
469 chan = host->dma_tx_channel;
470 }
471
472 dma_unmap_sg(chan->device->dev, data->sg, data->sg_len, dir);
473}
474
475static void mmci_dma_finalize(struct mmci_host *host, struct mmc_data *data)
476{
Russell Kingc8ebae32011-01-11 19:35:53 +0000477 u32 status;
478 int i;
479
480 /* Wait up to 1ms for the DMA to complete */
481 for (i = 0; ; i++) {
482 status = readl(host->base + MMCISTATUS);
483 if (!(status & MCI_RXDATAAVLBLMASK) || i >= 100)
484 break;
485 udelay(10);
486 }
487
488 /*
489 * Check to see whether we still have some data left in the FIFO -
490 * this catches DMA controllers which are unable to monitor the
491 * DMALBREQ and DMALSREQ signals while allowing us to DMA to non-
492 * contiguous buffers. On TX, we'll get a FIFO underrun error.
493 */
494 if (status & MCI_RXDATAAVLBLMASK) {
Ulf Hansson653a7612013-01-21 21:29:34 +0100495 mmci_dma_data_error(host);
Russell Kingc8ebae32011-01-11 19:35:53 +0000496 if (!data->error)
497 data->error = -EIO;
498 }
499
Per Forlin58c7ccb2011-07-01 18:55:24 +0200500 if (!data->host_cookie)
Ulf Hansson653a7612013-01-21 21:29:34 +0100501 mmci_dma_unmap(host, data);
Russell Kingc8ebae32011-01-11 19:35:53 +0000502
503 /*
504 * Use of DMA with scatter-gather is impossible.
505 * Give up with DMA and switch back to PIO mode.
506 */
507 if (status & MCI_RXDATAAVLBLMASK) {
508 dev_err(mmc_dev(host->mmc), "buggy DMA detected. Taking evasive action.\n");
509 mmci_dma_release(host);
510 }
Ulf Hansson653a7612013-01-21 21:29:34 +0100511
512 host->dma_current = NULL;
513 host->dma_desc_current = NULL;
Russell Kingc8ebae32011-01-11 19:35:53 +0000514}
515
Ulf Hansson653a7612013-01-21 21:29:34 +0100516/* prepares DMA channel and DMA descriptor, returns non-zero on failure */
517static int __mmci_dma_prep_data(struct mmci_host *host, struct mmc_data *data,
518 struct dma_chan **dma_chan,
519 struct dma_async_tx_descriptor **dma_desc)
Russell Kingc8ebae32011-01-11 19:35:53 +0000520{
521 struct variant_data *variant = host->variant;
522 struct dma_slave_config conf = {
523 .src_addr = host->phybase + MMCIFIFO,
524 .dst_addr = host->phybase + MMCIFIFO,
525 .src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
526 .dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
527 .src_maxburst = variant->fifohalfsize >> 2, /* # of words */
528 .dst_maxburst = variant->fifohalfsize >> 2, /* # of words */
Viresh Kumar258aea72012-02-01 16:12:19 +0530529 .device_fc = false,
Russell Kingc8ebae32011-01-11 19:35:53 +0000530 };
Russell Kingc8ebae32011-01-11 19:35:53 +0000531 struct dma_chan *chan;
532 struct dma_device *device;
533 struct dma_async_tx_descriptor *desc;
Vinod Koul05f57992011-10-14 10:45:11 +0530534 enum dma_data_direction buffer_dirn;
Russell Kingc8ebae32011-01-11 19:35:53 +0000535 int nr_sg;
536
Russell Kingc8ebae32011-01-11 19:35:53 +0000537 if (data->flags & MMC_DATA_READ) {
Vinod Koul05f57992011-10-14 10:45:11 +0530538 conf.direction = DMA_DEV_TO_MEM;
539 buffer_dirn = DMA_FROM_DEVICE;
Russell Kingc8ebae32011-01-11 19:35:53 +0000540 chan = host->dma_rx_channel;
541 } else {
Vinod Koul05f57992011-10-14 10:45:11 +0530542 conf.direction = DMA_MEM_TO_DEV;
543 buffer_dirn = DMA_TO_DEVICE;
Russell Kingc8ebae32011-01-11 19:35:53 +0000544 chan = host->dma_tx_channel;
545 }
546
547 /* If there's no DMA channel, fall back to PIO */
548 if (!chan)
549 return -EINVAL;
550
551 /* If less than or equal to the fifo size, don't bother with DMA */
Per Forlin58c7ccb2011-07-01 18:55:24 +0200552 if (data->blksz * data->blocks <= variant->fifosize)
Russell Kingc8ebae32011-01-11 19:35:53 +0000553 return -EINVAL;
554
555 device = chan->device;
Vinod Koul05f57992011-10-14 10:45:11 +0530556 nr_sg = dma_map_sg(device->dev, data->sg, data->sg_len, buffer_dirn);
Russell Kingc8ebae32011-01-11 19:35:53 +0000557 if (nr_sg == 0)
558 return -EINVAL;
559
560 dmaengine_slave_config(chan, &conf);
Alexandre Bounine16052822012-03-08 16:11:18 -0500561 desc = dmaengine_prep_slave_sg(chan, data->sg, nr_sg,
Russell Kingc8ebae32011-01-11 19:35:53 +0000562 conf.direction, DMA_CTRL_ACK);
563 if (!desc)
564 goto unmap_exit;
565
Ulf Hansson653a7612013-01-21 21:29:34 +0100566 *dma_chan = chan;
567 *dma_desc = desc;
Russell Kingc8ebae32011-01-11 19:35:53 +0000568
Per Forlin58c7ccb2011-07-01 18:55:24 +0200569 return 0;
570
571 unmap_exit:
Vinod Koul05f57992011-10-14 10:45:11 +0530572 dma_unmap_sg(device->dev, data->sg, data->sg_len, buffer_dirn);
Per Forlin58c7ccb2011-07-01 18:55:24 +0200573 return -ENOMEM;
574}
575
Ulf Hansson653a7612013-01-21 21:29:34 +0100576static inline int mmci_dma_prep_data(struct mmci_host *host,
577 struct mmc_data *data)
578{
579 /* Check if next job is already prepared. */
580 if (host->dma_current && host->dma_desc_current)
581 return 0;
582
583 /* No job were prepared thus do it now. */
584 return __mmci_dma_prep_data(host, data, &host->dma_current,
585 &host->dma_desc_current);
586}
587
588static inline int mmci_dma_prep_next(struct mmci_host *host,
589 struct mmc_data *data)
590{
591 struct mmci_host_next *nd = &host->next_data;
592 return __mmci_dma_prep_data(host, data, &nd->dma_chan, &nd->dma_desc);
593}
594
Per Forlin58c7ccb2011-07-01 18:55:24 +0200595static int mmci_dma_start_data(struct mmci_host *host, unsigned int datactrl)
596{
597 int ret;
598 struct mmc_data *data = host->data;
599
Ulf Hansson653a7612013-01-21 21:29:34 +0100600 ret = mmci_dma_prep_data(host, host->data);
Per Forlin58c7ccb2011-07-01 18:55:24 +0200601 if (ret)
602 return ret;
603
604 /* Okay, go for it. */
Russell Kingc8ebae32011-01-11 19:35:53 +0000605 dev_vdbg(mmc_dev(host->mmc),
606 "Submit MMCI DMA job, sglen %d blksz %04x blks %04x flags %08x\n",
607 data->sg_len, data->blksz, data->blocks, data->flags);
Per Forlin58c7ccb2011-07-01 18:55:24 +0200608 dmaengine_submit(host->dma_desc_current);
609 dma_async_issue_pending(host->dma_current);
Russell Kingc8ebae32011-01-11 19:35:53 +0000610
611 datactrl |= MCI_DPSM_DMAENABLE;
612
613 /* Trigger the DMA transfer */
Ulf Hansson9cc639a2013-05-15 20:48:23 +0100614 mmci_write_datactrlreg(host, datactrl);
Russell Kingc8ebae32011-01-11 19:35:53 +0000615
616 /*
617 * Let the MMCI say when the data is ended and it's time
618 * to fire next DMA request. When that happens, MMCI will
619 * call mmci_data_end()
620 */
621 writel(readl(host->base + MMCIMASK0) | MCI_DATAENDMASK,
622 host->base + MMCIMASK0);
623 return 0;
Russell Kingc8ebae32011-01-11 19:35:53 +0000624}
Per Forlin58c7ccb2011-07-01 18:55:24 +0200625
626static void mmci_get_next_data(struct mmci_host *host, struct mmc_data *data)
627{
628 struct mmci_host_next *next = &host->next_data;
629
Ulf Hansson653a7612013-01-21 21:29:34 +0100630 WARN_ON(data->host_cookie && data->host_cookie != next->cookie);
631 WARN_ON(!data->host_cookie && (next->dma_desc || next->dma_chan));
Per Forlin58c7ccb2011-07-01 18:55:24 +0200632
633 host->dma_desc_current = next->dma_desc;
634 host->dma_current = next->dma_chan;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200635 next->dma_desc = NULL;
636 next->dma_chan = NULL;
637}
638
639static void mmci_pre_request(struct mmc_host *mmc, struct mmc_request *mrq,
640 bool is_first_req)
641{
642 struct mmci_host *host = mmc_priv(mmc);
643 struct mmc_data *data = mrq->data;
644 struct mmci_host_next *nd = &host->next_data;
645
646 if (!data)
647 return;
648
Ulf Hansson653a7612013-01-21 21:29:34 +0100649 BUG_ON(data->host_cookie);
Per Forlin58c7ccb2011-07-01 18:55:24 +0200650
Ulf Hansson653a7612013-01-21 21:29:34 +0100651 if (mmci_validate_data(host, data))
652 return;
653
654 if (!mmci_dma_prep_next(host, data))
655 data->host_cookie = ++nd->cookie < 0 ? 1 : nd->cookie;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200656}
657
658static void mmci_post_request(struct mmc_host *mmc, struct mmc_request *mrq,
659 int err)
660{
661 struct mmci_host *host = mmc_priv(mmc);
662 struct mmc_data *data = mrq->data;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200663
Ulf Hansson653a7612013-01-21 21:29:34 +0100664 if (!data || !data->host_cookie)
Per Forlin58c7ccb2011-07-01 18:55:24 +0200665 return;
666
Ulf Hansson653a7612013-01-21 21:29:34 +0100667 mmci_dma_unmap(host, data);
Per Forlin58c7ccb2011-07-01 18:55:24 +0200668
Ulf Hansson653a7612013-01-21 21:29:34 +0100669 if (err) {
670 struct mmci_host_next *next = &host->next_data;
671 struct dma_chan *chan;
672 if (data->flags & MMC_DATA_READ)
673 chan = host->dma_rx_channel;
674 else
675 chan = host->dma_tx_channel;
676 dmaengine_terminate_all(chan);
Per Forlin58c7ccb2011-07-01 18:55:24 +0200677
Ulf Hansson653a7612013-01-21 21:29:34 +0100678 next->dma_desc = NULL;
679 next->dma_chan = NULL;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200680 }
681}
682
Russell Kingc8ebae32011-01-11 19:35:53 +0000683#else
684/* Blank functions if the DMA engine is not available */
Per Forlin58c7ccb2011-07-01 18:55:24 +0200685static void mmci_get_next_data(struct mmci_host *host, struct mmc_data *data)
686{
687}
Russell Kingc8ebae32011-01-11 19:35:53 +0000688static inline void mmci_dma_setup(struct mmci_host *host)
689{
690}
691
692static inline void mmci_dma_release(struct mmci_host *host)
693{
694}
695
696static inline void mmci_dma_unmap(struct mmci_host *host, struct mmc_data *data)
697{
698}
699
Ulf Hansson653a7612013-01-21 21:29:34 +0100700static inline void mmci_dma_finalize(struct mmci_host *host,
701 struct mmc_data *data)
702{
703}
704
Russell Kingc8ebae32011-01-11 19:35:53 +0000705static inline void mmci_dma_data_error(struct mmci_host *host)
706{
707}
708
709static inline int mmci_dma_start_data(struct mmci_host *host, unsigned int datactrl)
710{
711 return -ENOSYS;
712}
Per Forlin58c7ccb2011-07-01 18:55:24 +0200713
714#define mmci_pre_request NULL
715#define mmci_post_request NULL
716
Russell Kingc8ebae32011-01-11 19:35:53 +0000717#endif
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719static void mmci_start_data(struct mmci_host *host, struct mmc_data *data)
720{
Rabin Vincent8301bb62010-08-09 12:57:30 +0100721 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 unsigned int datactrl, timeout, irqmask;
Russell King7b09cda2005-07-01 12:02:59 +0100723 unsigned long long clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 void __iomem *base;
Russell King3bc87f22006-08-27 13:51:28 +0100725 int blksz_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Linus Walleij64de0282010-02-19 01:09:10 +0100727 dev_dbg(mmc_dev(host->mmc), "blksz %04x blks %04x flags %08x\n",
728 data->blksz, data->blocks, data->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 host->data = data;
Rabin Vincent528320d2010-07-21 12:49:49 +0100731 host->size = data->blksz * data->blocks;
Russell King51d43752011-01-27 10:56:52 +0000732 data->bytes_xfered = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Russell King7b09cda2005-07-01 12:02:59 +0100734 clks = (unsigned long long)data->timeout_ns * host->cclk;
735 do_div(clks, 1000000000UL);
736
737 timeout = data->timeout_clks + (unsigned int)clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 base = host->base;
740 writel(timeout, base + MMCIDATATIMER);
741 writel(host->size, base + MMCIDATALENGTH);
742
Russell King3bc87f22006-08-27 13:51:28 +0100743 blksz_bits = ffs(data->blksz) - 1;
744 BUG_ON(1 << blksz_bits != data->blksz);
745
Philippe Langlais1784b152011-03-25 08:51:52 +0100746 if (variant->blksz_datactrl16)
747 datactrl = MCI_DPSM_ENABLE | (data->blksz << 16);
748 else
749 datactrl = MCI_DPSM_ENABLE | blksz_bits << 4;
Russell Kingc8ebae32011-01-11 19:35:53 +0000750
751 if (data->flags & MMC_DATA_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 datactrl |= MCI_DPSM_DIRECTION;
Russell Kingc8ebae32011-01-11 19:35:53 +0000753
Ulf Hansson7258db72011-12-13 17:05:28 +0100754 /* The ST Micro variants has a special bit to enable SDIO */
755 if (variant->sdio && host->mmc->card)
Ulf Hansson06c1a122012-10-12 14:01:50 +0100756 if (mmc_card_sdio(host->mmc->card)) {
757 /*
758 * The ST Micro variants has a special bit
759 * to enable SDIO.
760 */
761 u32 clk;
762
Ulf Hansson7258db72011-12-13 17:05:28 +0100763 datactrl |= MCI_ST_DPSM_SDIOEN;
764
Ulf Hansson06c1a122012-10-12 14:01:50 +0100765 /*
Ulf Hansson70ac0932012-10-12 14:07:36 +0100766 * The ST Micro variant for SDIO small write transfers
767 * needs to have clock H/W flow control disabled,
768 * otherwise the transfer will not start. The threshold
769 * depends on the rate of MCLK.
Ulf Hansson06c1a122012-10-12 14:01:50 +0100770 */
Ulf Hansson70ac0932012-10-12 14:07:36 +0100771 if (data->flags & MMC_DATA_WRITE &&
772 (host->size < 8 ||
773 (host->size <= 8 && host->mclk > 50000000)))
Ulf Hansson06c1a122012-10-12 14:01:50 +0100774 clk = host->clk_reg & ~variant->clkreg_enable;
775 else
776 clk = host->clk_reg | variant->clkreg_enable;
777
778 mmci_write_clkreg(host, clk);
779 }
780
Ulf Hansson6dbb6ee2013-01-07 15:30:44 +0100781 if (host->mmc->ios.timing == MMC_TIMING_UHS_DDR50)
782 datactrl |= MCI_ST_DPSM_DDRMODE;
783
Russell Kingc8ebae32011-01-11 19:35:53 +0000784 /*
785 * Attempt to use DMA operation mode, if this
786 * should fail, fall back to PIO mode
787 */
788 if (!mmci_dma_start_data(host, datactrl))
789 return;
790
791 /* IRQ mode, map the SG list for CPU reading/writing */
792 mmci_init_sg(host, data);
793
794 if (data->flags & MMC_DATA_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 irqmask = MCI_RXFIFOHALFFULLMASK;
Russell King0425a142006-02-16 16:48:31 +0000796
797 /*
Russell Kingc4d877c2011-01-27 09:50:13 +0000798 * If we have less than the fifo 'half-full' threshold to
799 * transfer, trigger a PIO interrupt as soon as any data
800 * is available.
Russell King0425a142006-02-16 16:48:31 +0000801 */
Russell Kingc4d877c2011-01-27 09:50:13 +0000802 if (host->size < variant->fifohalfsize)
Russell King0425a142006-02-16 16:48:31 +0000803 irqmask |= MCI_RXDATAAVLBLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 } else {
805 /*
806 * We don't actually need to include "FIFO empty" here
807 * since its implicit in "FIFO half empty".
808 */
809 irqmask = MCI_TXFIFOHALFEMPTYMASK;
810 }
811
Ulf Hansson9cc639a2013-05-15 20:48:23 +0100812 mmci_write_datactrlreg(host, datactrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 writel(readl(base + MMCIMASK0) & ~MCI_DATAENDMASK, base + MMCIMASK0);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100814 mmci_set_mask1(host, irqmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815}
816
817static void
818mmci_start_command(struct mmci_host *host, struct mmc_command *cmd, u32 c)
819{
820 void __iomem *base = host->base;
821
Linus Walleij64de0282010-02-19 01:09:10 +0100822 dev_dbg(mmc_dev(host->mmc), "op %02x arg %08x flags %08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 cmd->opcode, cmd->arg, cmd->flags);
824
825 if (readl(base + MMCICOMMAND) & MCI_CPSM_ENABLE) {
826 writel(0, base + MMCICOMMAND);
827 udelay(1);
828 }
829
830 c |= cmd->opcode | MCI_CPSM_ENABLE;
Russell Kinge9225172006-02-02 12:23:12 +0000831 if (cmd->flags & MMC_RSP_PRESENT) {
832 if (cmd->flags & MMC_RSP_136)
833 c |= MCI_CPSM_LONGRSP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 c |= MCI_CPSM_RESPONSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
836 if (/*interrupt*/0)
837 c |= MCI_CPSM_INTERRUPT;
838
839 host->cmd = cmd;
840
841 writel(cmd->arg, base + MMCIARGUMENT);
842 writel(c, base + MMCICOMMAND);
843}
844
845static void
846mmci_data_irq(struct mmci_host *host, struct mmc_data *data,
847 unsigned int status)
848{
Linus Walleijf20f8f22010-10-19 13:41:24 +0100849 /* First check for errors */
Ulf Hanssonb63038d2011-12-13 16:51:04 +0100850 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_STARTBITERR|
851 MCI_TXUNDERRUN|MCI_RXOVERRUN)) {
Linus Walleij8cb28152011-01-24 15:22:13 +0100852 u32 remain, success;
Linus Walleijf20f8f22010-10-19 13:41:24 +0100853
Russell Kingc8ebae32011-01-11 19:35:53 +0000854 /* Terminate the DMA transfer */
Ulf Hansson653a7612013-01-21 21:29:34 +0100855 if (dma_inprogress(host)) {
Russell Kingc8ebae32011-01-11 19:35:53 +0000856 mmci_dma_data_error(host);
Ulf Hansson653a7612013-01-21 21:29:34 +0100857 mmci_dma_unmap(host, data);
858 }
Russell Kingc8ebae32011-01-11 19:35:53 +0000859
Russell Kingc8afc9d2011-02-04 09:19:46 +0000860 /*
861 * Calculate how far we are into the transfer. Note that
862 * the data counter gives the number of bytes transferred
863 * on the MMC bus, not on the host side. On reads, this
864 * can be as much as a FIFO-worth of data ahead. This
865 * matters for FIFO overruns only.
866 */
Linus Walleijf5a106d2011-01-27 17:44:34 +0100867 remain = readl(host->base + MMCIDATACNT);
Linus Walleij8cb28152011-01-24 15:22:13 +0100868 success = data->blksz * data->blocks - remain;
869
Russell Kingc8afc9d2011-02-04 09:19:46 +0000870 dev_dbg(mmc_dev(host->mmc), "MCI ERROR IRQ, status 0x%08x at 0x%08x\n",
871 status, success);
Linus Walleij8cb28152011-01-24 15:22:13 +0100872 if (status & MCI_DATACRCFAIL) {
873 /* Last block was not successful */
Russell Kingc8afc9d2011-02-04 09:19:46 +0000874 success -= 1;
Pierre Ossman17b04292007-07-22 22:18:46 +0200875 data->error = -EILSEQ;
Linus Walleij8cb28152011-01-24 15:22:13 +0100876 } else if (status & MCI_DATATIMEOUT) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200877 data->error = -ETIMEDOUT;
Linus Walleij757df742011-06-30 15:10:21 +0100878 } else if (status & MCI_STARTBITERR) {
879 data->error = -ECOMM;
Russell Kingc8afc9d2011-02-04 09:19:46 +0000880 } else if (status & MCI_TXUNDERRUN) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200881 data->error = -EIO;
Russell Kingc8afc9d2011-02-04 09:19:46 +0000882 } else if (status & MCI_RXOVERRUN) {
883 if (success > host->variant->fifosize)
884 success -= host->variant->fifosize;
885 else
886 success = 0;
Linus Walleij8cb28152011-01-24 15:22:13 +0100887 data->error = -EIO;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100888 }
Russell King51d43752011-01-27 10:56:52 +0000889 data->bytes_xfered = round_down(success, data->blksz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
Linus Walleijf20f8f22010-10-19 13:41:24 +0100891
Linus Walleij8cb28152011-01-24 15:22:13 +0100892 if (status & MCI_DATABLOCKEND)
893 dev_err(mmc_dev(host->mmc), "stray MCI_DATABLOCKEND interrupt\n");
Linus Walleijf20f8f22010-10-19 13:41:24 +0100894
Russell Kingccff9b52011-01-30 21:03:50 +0000895 if (status & MCI_DATAEND || data->error) {
Russell Kingc8ebae32011-01-11 19:35:53 +0000896 if (dma_inprogress(host))
Ulf Hansson653a7612013-01-21 21:29:34 +0100897 mmci_dma_finalize(host, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 mmci_stop_data(host);
899
Linus Walleij8cb28152011-01-24 15:22:13 +0100900 if (!data->error)
901 /* The error clause is handled above, success! */
Russell King51d43752011-01-27 10:56:52 +0000902 data->bytes_xfered = data->blksz * data->blocks;
Linus Walleijf20f8f22010-10-19 13:41:24 +0100903
Ulf Hansson024629c2013-05-13 15:40:56 +0100904 if (!data->stop || host->mrq->sbc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 mmci_request_end(host, data->mrq);
906 } else {
907 mmci_start_command(host, data->stop, 0);
908 }
909 }
910}
911
912static void
913mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
914 unsigned int status)
915{
916 void __iomem *base = host->base;
Ulf Hansson024629c2013-05-13 15:40:56 +0100917 bool sbc = (cmd == host->mrq->sbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 host->cmd = NULL;
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 if (status & MCI_CMDTIMEOUT) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200922 cmd->error = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200924 cmd->error = -EILSEQ;
Russell King - ARM Linux9047b432011-01-11 16:35:56 +0000925 } else {
926 cmd->resp[0] = readl(base + MMCIRESPONSE0);
927 cmd->resp[1] = readl(base + MMCIRESPONSE1);
928 cmd->resp[2] = readl(base + MMCIRESPONSE2);
929 cmd->resp[3] = readl(base + MMCIRESPONSE3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 }
931
Ulf Hansson024629c2013-05-13 15:40:56 +0100932 if ((!sbc && !cmd->data) || cmd->error) {
Ulf Hansson3b6e3c72011-12-13 16:58:43 +0100933 if (host->data) {
934 /* Terminate the DMA transfer */
Ulf Hansson653a7612013-01-21 21:29:34 +0100935 if (dma_inprogress(host)) {
Ulf Hansson3b6e3c72011-12-13 16:58:43 +0100936 mmci_dma_data_error(host);
Ulf Hansson653a7612013-01-21 21:29:34 +0100937 mmci_dma_unmap(host, host->data);
938 }
Russell Kinge47c2222007-01-08 16:42:51 +0000939 mmci_stop_data(host);
Ulf Hansson3b6e3c72011-12-13 16:58:43 +0100940 }
Ulf Hansson024629c2013-05-13 15:40:56 +0100941 mmci_request_end(host, host->mrq);
942 } else if (sbc) {
943 mmci_start_command(host, host->mrq->cmd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 } else if (!(cmd->data->flags & MMC_DATA_READ)) {
945 mmci_start_data(host, cmd->data);
946 }
947}
948
949static int mmci_pio_read(struct mmci_host *host, char *buffer, unsigned int remain)
950{
951 void __iomem *base = host->base;
952 char *ptr = buffer;
953 u32 status;
Linus Walleij26eed9a2008-04-26 23:39:44 +0100954 int host_remain = host->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 do {
Linus Walleij26eed9a2008-04-26 23:39:44 +0100957 int count = host_remain - (readl(base + MMCIFIFOCNT) << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 if (count > remain)
960 count = remain;
961
962 if (count <= 0)
963 break;
964
Ulf Hansson393e5e22011-12-13 17:08:04 +0100965 /*
966 * SDIO especially may want to send something that is
967 * not divisible by 4 (as opposed to card sectors
968 * etc). Therefore make sure to always read the last bytes
969 * while only doing full 32-bit reads towards the FIFO.
970 */
971 if (unlikely(count & 0x3)) {
972 if (count < 4) {
973 unsigned char buf[4];
Davide Ciminaghi4b85da02012-12-10 14:47:21 +0100974 ioread32_rep(base + MMCIFIFO, buf, 1);
Ulf Hansson393e5e22011-12-13 17:08:04 +0100975 memcpy(ptr, buf, count);
976 } else {
Davide Ciminaghi4b85da02012-12-10 14:47:21 +0100977 ioread32_rep(base + MMCIFIFO, ptr, count >> 2);
Ulf Hansson393e5e22011-12-13 17:08:04 +0100978 count &= ~0x3;
979 }
980 } else {
Davide Ciminaghi4b85da02012-12-10 14:47:21 +0100981 ioread32_rep(base + MMCIFIFO, ptr, count >> 2);
Ulf Hansson393e5e22011-12-13 17:08:04 +0100982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984 ptr += count;
985 remain -= count;
Linus Walleij26eed9a2008-04-26 23:39:44 +0100986 host_remain -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
988 if (remain == 0)
989 break;
990
991 status = readl(base + MMCISTATUS);
992 } while (status & MCI_RXDATAAVLBL);
993
994 return ptr - buffer;
995}
996
997static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status)
998{
Rabin Vincent8301bb62010-08-09 12:57:30 +0100999 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 void __iomem *base = host->base;
1001 char *ptr = buffer;
1002
1003 do {
1004 unsigned int count, maxcnt;
1005
Rabin Vincent8301bb62010-08-09 12:57:30 +01001006 maxcnt = status & MCI_TXFIFOEMPTY ?
1007 variant->fifosize : variant->fifohalfsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 count = min(remain, maxcnt);
1009
Linus Walleij34177802010-10-19 12:43:58 +01001010 /*
Linus Walleij34177802010-10-19 12:43:58 +01001011 * SDIO especially may want to send something that is
1012 * not divisible by 4 (as opposed to card sectors
1013 * etc), and the FIFO only accept full 32-bit writes.
1014 * So compensate by adding +3 on the count, a single
1015 * byte become a 32bit write, 7 bytes will be two
1016 * 32bit writes etc.
1017 */
Davide Ciminaghi4b85da02012-12-10 14:47:21 +01001018 iowrite32_rep(base + MMCIFIFO, ptr, (count + 3) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 ptr += count;
1021 remain -= count;
1022
1023 if (remain == 0)
1024 break;
1025
1026 status = readl(base + MMCISTATUS);
1027 } while (status & MCI_TXFIFOHALFEMPTY);
1028
1029 return ptr - buffer;
1030}
1031
1032/*
1033 * PIO data transfer IRQ handler.
1034 */
David Howells7d12e782006-10-05 14:55:46 +01001035static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
1037 struct mmci_host *host = dev_id;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001038 struct sg_mapping_iter *sg_miter = &host->sg_miter;
Rabin Vincent8301bb62010-08-09 12:57:30 +01001039 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 void __iomem *base = host->base;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001041 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 u32 status;
1043
1044 status = readl(base + MMCISTATUS);
1045
Linus Walleij64de0282010-02-19 01:09:10 +01001046 dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001048 local_irq_save(flags);
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 unsigned int remain, len;
1052 char *buffer;
1053
1054 /*
1055 * For write, we only need to test the half-empty flag
1056 * here - if the FIFO is completely empty, then by
1057 * definition it is more than half empty.
1058 *
1059 * For read, check for data available.
1060 */
1061 if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL)))
1062 break;
1063
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001064 if (!sg_miter_next(sg_miter))
1065 break;
1066
1067 buffer = sg_miter->addr;
1068 remain = sg_miter->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070 len = 0;
1071 if (status & MCI_RXACTIVE)
1072 len = mmci_pio_read(host, buffer, remain);
1073 if (status & MCI_TXACTIVE)
1074 len = mmci_pio_write(host, buffer, remain, status);
1075
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001076 sg_miter->consumed = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 host->size -= len;
1079 remain -= len;
1080
1081 if (remain)
1082 break;
1083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 status = readl(base + MMCISTATUS);
1085 } while (1);
1086
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001087 sg_miter_stop(sg_miter);
1088
1089 local_irq_restore(flags);
1090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 /*
Russell Kingc4d877c2011-01-27 09:50:13 +00001092 * If we have less than the fifo 'half-full' threshold to transfer,
1093 * trigger a PIO interrupt as soon as any data is available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 */
Russell Kingc4d877c2011-01-27 09:50:13 +00001095 if (status & MCI_RXACTIVE && host->size < variant->fifohalfsize)
Linus Walleij2686b4b2010-10-19 12:39:48 +01001096 mmci_set_mask1(host, MCI_RXDATAAVLBLMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098 /*
1099 * If we run out of data, disable the data IRQs; this
1100 * prevents a race where the FIFO becomes empty before
1101 * the chip itself has disabled the data path, and
1102 * stops us racing with our data end IRQ.
1103 */
1104 if (host->size == 0) {
Linus Walleij2686b4b2010-10-19 12:39:48 +01001105 mmci_set_mask1(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0);
1107 }
1108
1109 return IRQ_HANDLED;
1110}
1111
1112/*
1113 * Handle completion of command and data transfers.
1114 */
David Howells7d12e782006-10-05 14:55:46 +01001115static irqreturn_t mmci_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
1117 struct mmci_host *host = dev_id;
1118 u32 status;
1119 int ret = 0;
1120
1121 spin_lock(&host->lock);
1122
1123 do {
1124 struct mmc_command *cmd;
1125 struct mmc_data *data;
1126
1127 status = readl(host->base + MMCISTATUS);
Linus Walleij2686b4b2010-10-19 12:39:48 +01001128
1129 if (host->singleirq) {
1130 if (status & readl(host->base + MMCIMASK1))
1131 mmci_pio_irq(irq, dev_id);
1132
1133 status &= ~MCI_IRQ1MASK;
1134 }
1135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 status &= readl(host->base + MMCIMASK0);
1137 writel(status, host->base + MMCICLEAR);
1138
Linus Walleij64de0282010-02-19 01:09:10 +01001139 dev_dbg(mmc_dev(host->mmc), "irq0 (data+cmd) %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141 data = host->data;
Ulf Hanssonb63038d2011-12-13 16:51:04 +01001142 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_STARTBITERR|
1143 MCI_TXUNDERRUN|MCI_RXOVERRUN|MCI_DATAEND|
1144 MCI_DATABLOCKEND) && data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 mmci_data_irq(host, data, status);
1146
1147 cmd = host->cmd;
1148 if (status & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT|MCI_CMDSENT|MCI_CMDRESPEND) && cmd)
1149 mmci_cmd_irq(host, cmd, status);
1150
1151 ret = 1;
1152 } while (status);
1153
1154 spin_unlock(&host->lock);
1155
1156 return IRQ_RETVAL(ret);
1157}
1158
1159static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1160{
1161 struct mmci_host *host = mmc_priv(mmc);
Linus Walleij9e943022008-10-24 21:17:50 +01001162 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 WARN_ON(host->mrq != NULL);
1165
Ulf Hansson653a7612013-01-21 21:29:34 +01001166 mrq->cmd->error = mmci_validate_data(host, mrq->data);
1167 if (mrq->cmd->error) {
Pierre Ossman255d01a2007-07-24 20:38:53 +02001168 mmc_request_done(mmc, mrq);
1169 return;
1170 }
1171
Russell King1c3be362011-08-14 09:17:05 +01001172 pm_runtime_get_sync(mmc_dev(mmc));
1173
Linus Walleij9e943022008-10-24 21:17:50 +01001174 spin_lock_irqsave(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 host->mrq = mrq;
1177
Per Forlin58c7ccb2011-07-01 18:55:24 +02001178 if (mrq->data)
1179 mmci_get_next_data(host, mrq->data);
1180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 if (mrq->data && mrq->data->flags & MMC_DATA_READ)
1182 mmci_start_data(host, mrq->data);
1183
Ulf Hansson024629c2013-05-13 15:40:56 +01001184 if (mrq->sbc)
1185 mmci_start_command(host, mrq->sbc, 0);
1186 else
1187 mmci_start_command(host, mrq->cmd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Linus Walleij9e943022008-10-24 21:17:50 +01001189 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190}
1191
1192static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1193{
1194 struct mmci_host *host = mmc_priv(mmc);
Ulf Hansson7d72a1d2011-12-13 16:54:55 +01001195 struct variant_data *variant = host->variant;
Linus Walleija6a64642009-09-14 12:56:14 +01001196 u32 pwr = 0;
1197 unsigned long flags;
Lee Jonesdb90f912013-05-03 12:52:12 +01001198 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Ulf Hansson2cd976c2011-12-13 17:01:11 +01001200 pm_runtime_get_sync(mmc_dev(mmc));
1201
Ulf Hanssonbc521812011-12-13 16:57:55 +01001202 if (host->plat->ios_handler &&
1203 host->plat->ios_handler(mmc_dev(mmc), ios))
1204 dev_err(mmc_dev(mmc), "platform ios_handler failed\n");
1205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 switch (ios->power_mode) {
1207 case MMC_POWER_OFF:
Ulf Hansson599c1d52013-01-07 16:22:50 +01001208 if (!IS_ERR(mmc->supply.vmmc))
1209 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Lee Jones237fb5e2013-01-31 11:27:52 +00001210
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001211 if (!IS_ERR(mmc->supply.vqmmc) && host->vqmmc_enabled) {
Lee Jones237fb5e2013-01-31 11:27:52 +00001212 regulator_disable(mmc->supply.vqmmc);
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001213 host->vqmmc_enabled = false;
1214 }
Lee Jones237fb5e2013-01-31 11:27:52 +00001215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 break;
1217 case MMC_POWER_UP:
Ulf Hansson599c1d52013-01-07 16:22:50 +01001218 if (!IS_ERR(mmc->supply.vmmc))
1219 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd);
1220
Ulf Hansson7d72a1d2011-12-13 16:54:55 +01001221 /*
1222 * The ST Micro variant doesn't have the PL180s MCI_PWR_UP
1223 * and instead uses MCI_PWR_ON so apply whatever value is
1224 * configured in the variant data.
1225 */
1226 pwr |= variant->pwrreg_powerup;
1227
1228 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 case MMC_POWER_ON:
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001230 if (!IS_ERR(mmc->supply.vqmmc) && !host->vqmmc_enabled) {
Lee Jonesdb90f912013-05-03 12:52:12 +01001231 ret = regulator_enable(mmc->supply.vqmmc);
1232 if (ret < 0)
1233 dev_err(mmc_dev(mmc),
1234 "failed to enable vqmmc regulator\n");
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001235 else
1236 host->vqmmc_enabled = true;
Lee Jonesdb90f912013-05-03 12:52:12 +01001237 }
Lee Jones237fb5e2013-01-31 11:27:52 +00001238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 pwr |= MCI_PWR_ON;
1240 break;
1241 }
1242
Ulf Hansson4d1a3a02011-12-13 16:57:07 +01001243 if (variant->signal_direction && ios->power_mode != MMC_POWER_OFF) {
1244 /*
1245 * The ST Micro variant has some additional bits
1246 * indicating signal direction for the signals in
1247 * the SD/MMC bus and feedback-clock usage.
1248 */
1249 pwr |= host->plat->sigdir;
1250
1251 if (ios->bus_width == MMC_BUS_WIDTH_4)
1252 pwr &= ~MCI_ST_DATA74DIREN;
1253 else if (ios->bus_width == MMC_BUS_WIDTH_1)
1254 pwr &= (~MCI_ST_DATA74DIREN &
1255 ~MCI_ST_DATA31DIREN &
1256 ~MCI_ST_DATA2DIREN);
1257 }
1258
Linus Walleijcc30d602009-01-04 15:18:54 +01001259 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN) {
Linus Walleijf17a1f02009-08-04 01:01:02 +01001260 if (host->hw_designer != AMBA_VENDOR_ST)
Linus Walleijcc30d602009-01-04 15:18:54 +01001261 pwr |= MCI_ROD;
1262 else {
1263 /*
1264 * The ST Micro variant use the ROD bit for something
1265 * else and only has OD (Open Drain).
1266 */
1267 pwr |= MCI_OD;
1268 }
1269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Ulf Hanssonf4670da2013-01-09 17:19:54 +01001271 /*
1272 * If clock = 0 and the variant requires the MMCIPOWER to be used for
1273 * gating the clock, the MCI_PWR_ON bit is cleared.
1274 */
1275 if (!ios->clock && variant->pwrreg_clkgate)
1276 pwr &= ~MCI_PWR_ON;
1277
Linus Walleija6a64642009-09-14 12:56:14 +01001278 spin_lock_irqsave(&host->lock, flags);
1279
1280 mmci_set_clkreg(host, ios->clock);
Ulf Hansson7437cfa2012-01-18 09:17:27 +01001281 mmci_write_pwrreg(host, pwr);
Ulf Hanssonf829c042013-09-04 09:01:15 +01001282 mmci_reg_delay(host);
Linus Walleija6a64642009-09-14 12:56:14 +01001283
1284 spin_unlock_irqrestore(&host->lock, flags);
Ulf Hansson2cd976c2011-12-13 17:01:11 +01001285
Ulf Hansson2cd976c2011-12-13 17:01:11 +01001286 pm_runtime_mark_last_busy(mmc_dev(mmc));
1287 pm_runtime_put_autosuspend(mmc_dev(mmc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288}
1289
Russell King89001442009-07-09 15:16:07 +01001290static int mmci_get_ro(struct mmc_host *mmc)
1291{
1292 struct mmci_host *host = mmc_priv(mmc);
1293
1294 if (host->gpio_wp == -ENOSYS)
1295 return -ENOSYS;
1296
Linus Walleij18a063012010-09-12 12:56:44 +01001297 return gpio_get_value_cansleep(host->gpio_wp);
Russell King89001442009-07-09 15:16:07 +01001298}
1299
1300static int mmci_get_cd(struct mmc_host *mmc)
1301{
1302 struct mmci_host *host = mmc_priv(mmc);
Rabin Vincent29719442010-08-09 12:54:43 +01001303 struct mmci_platform_data *plat = host->plat;
Russell King89001442009-07-09 15:16:07 +01001304 unsigned int status;
1305
Rabin Vincent4b8caec2010-08-09 12:56:40 +01001306 if (host->gpio_cd == -ENOSYS) {
1307 if (!plat->status)
1308 return 1; /* Assume always present */
1309
Rabin Vincent29719442010-08-09 12:54:43 +01001310 status = plat->status(mmc_dev(host->mmc));
Rabin Vincent4b8caec2010-08-09 12:56:40 +01001311 } else
Linus Walleij18a063012010-09-12 12:56:44 +01001312 status = !!gpio_get_value_cansleep(host->gpio_cd)
1313 ^ plat->cd_invert;
Russell King89001442009-07-09 15:16:07 +01001314
Russell King74bc8092010-07-29 15:58:59 +01001315 /*
1316 * Use positive logic throughout - status is zero for no card,
1317 * non-zero for card inserted.
1318 */
1319 return status;
Russell King89001442009-07-09 15:16:07 +01001320}
1321
Ulf Hansson0f3ed7f2013-05-15 20:47:33 +01001322static int mmci_sig_volt_switch(struct mmc_host *mmc, struct mmc_ios *ios)
1323{
1324 int ret = 0;
1325
1326 if (!IS_ERR(mmc->supply.vqmmc)) {
1327
1328 pm_runtime_get_sync(mmc_dev(mmc));
1329
1330 switch (ios->signal_voltage) {
1331 case MMC_SIGNAL_VOLTAGE_330:
1332 ret = regulator_set_voltage(mmc->supply.vqmmc,
1333 2700000, 3600000);
1334 break;
1335 case MMC_SIGNAL_VOLTAGE_180:
1336 ret = regulator_set_voltage(mmc->supply.vqmmc,
1337 1700000, 1950000);
1338 break;
1339 case MMC_SIGNAL_VOLTAGE_120:
1340 ret = regulator_set_voltage(mmc->supply.vqmmc,
1341 1100000, 1300000);
1342 break;
1343 }
1344
1345 if (ret)
1346 dev_warn(mmc_dev(mmc), "Voltage switch failed\n");
1347
1348 pm_runtime_mark_last_busy(mmc_dev(mmc));
1349 pm_runtime_put_autosuspend(mmc_dev(mmc));
1350 }
1351
1352 return ret;
1353}
1354
Rabin Vincent148b8b32010-08-09 12:55:48 +01001355static irqreturn_t mmci_cd_irq(int irq, void *dev_id)
1356{
1357 struct mmci_host *host = dev_id;
1358
1359 mmc_detect_change(host->mmc, msecs_to_jiffies(500));
1360
1361 return IRQ_HANDLED;
1362}
1363
Ulf Hansson01259622013-05-15 20:53:22 +01001364static struct mmc_host_ops mmci_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 .request = mmci_request,
Per Forlin58c7ccb2011-07-01 18:55:24 +02001366 .pre_req = mmci_pre_request,
1367 .post_req = mmci_post_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 .set_ios = mmci_set_ios,
Russell King89001442009-07-09 15:16:07 +01001369 .get_ro = mmci_get_ro,
1370 .get_cd = mmci_get_cd,
Ulf Hansson0f3ed7f2013-05-15 20:47:33 +01001371 .start_signal_voltage_switch = mmci_sig_volt_switch,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372};
1373
Lee Jones000bc9d2012-04-16 10:18:43 +01001374#ifdef CONFIG_OF
1375static void mmci_dt_populate_generic_pdata(struct device_node *np,
1376 struct mmci_platform_data *pdata)
1377{
1378 int bus_width = 0;
1379
Lee Jones9a597012012-04-12 16:51:13 +01001380 pdata->gpio_wp = of_get_named_gpio(np, "wp-gpios", 0);
Lee Jones9a597012012-04-12 16:51:13 +01001381 pdata->gpio_cd = of_get_named_gpio(np, "cd-gpios", 0);
Lee Jones000bc9d2012-04-16 10:18:43 +01001382
1383 if (of_get_property(np, "cd-inverted", NULL))
1384 pdata->cd_invert = true;
1385 else
1386 pdata->cd_invert = false;
1387
1388 of_property_read_u32(np, "max-frequency", &pdata->f_max);
1389 if (!pdata->f_max)
1390 pr_warn("%s has no 'max-frequency' property\n", np->full_name);
1391
1392 if (of_get_property(np, "mmc-cap-mmc-highspeed", NULL))
1393 pdata->capabilities |= MMC_CAP_MMC_HIGHSPEED;
1394 if (of_get_property(np, "mmc-cap-sd-highspeed", NULL))
1395 pdata->capabilities |= MMC_CAP_SD_HIGHSPEED;
1396
1397 of_property_read_u32(np, "bus-width", &bus_width);
1398 switch (bus_width) {
1399 case 0 :
1400 /* No bus-width supplied. */
1401 break;
1402 case 4 :
1403 pdata->capabilities |= MMC_CAP_4_BIT_DATA;
1404 break;
1405 case 8 :
1406 pdata->capabilities |= MMC_CAP_8_BIT_DATA;
1407 break;
1408 default :
1409 pr_warn("%s: Unsupported bus width\n", np->full_name);
1410 }
1411}
Lee Jonesc0a120a2012-05-08 13:59:38 +01001412#else
1413static void mmci_dt_populate_generic_pdata(struct device_node *np,
1414 struct mmci_platform_data *pdata)
1415{
1416 return;
1417}
Lee Jones000bc9d2012-04-16 10:18:43 +01001418#endif
1419
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001420static int mmci_probe(struct amba_device *dev,
Russell Kingaa25afa2011-02-19 15:55:00 +00001421 const struct amba_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
Linus Walleij6ef297f2009-09-22 14:29:36 +01001423 struct mmci_platform_data *plat = dev->dev.platform_data;
Lee Jones000bc9d2012-04-16 10:18:43 +01001424 struct device_node *np = dev->dev.of_node;
Rabin Vincent4956e102010-07-21 12:54:40 +01001425 struct variant_data *variant = id->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 struct mmci_host *host;
1427 struct mmc_host *mmc;
1428 int ret;
1429
Lee Jones000bc9d2012-04-16 10:18:43 +01001430 /* Must have platform data or Device Tree. */
1431 if (!plat && !np) {
1432 dev_err(&dev->dev, "No plat data or DT found\n");
1433 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 }
1435
Lee Jonesb9b52912012-06-12 10:49:51 +01001436 if (!plat) {
1437 plat = devm_kzalloc(&dev->dev, sizeof(*plat), GFP_KERNEL);
1438 if (!plat)
1439 return -ENOMEM;
1440 }
1441
Lee Jones000bc9d2012-04-16 10:18:43 +01001442 if (np)
1443 mmci_dt_populate_generic_pdata(np, plat);
1444
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 ret = amba_request_regions(dev, DRIVER_NAME);
1446 if (ret)
1447 goto out;
1448
1449 mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
1450 if (!mmc) {
1451 ret = -ENOMEM;
1452 goto rel_regions;
1453 }
1454
1455 host = mmc_priv(mmc);
Rabin Vincent4ea580f2009-04-17 08:44:19 +05301456 host->mmc = mmc;
Russell King012b7d32009-07-09 15:13:56 +01001457
Russell King89001442009-07-09 15:16:07 +01001458 host->gpio_wp = -ENOSYS;
1459 host->gpio_cd = -ENOSYS;
Rabin Vincent148b8b32010-08-09 12:55:48 +01001460 host->gpio_cd_irq = -1;
Russell King89001442009-07-09 15:16:07 +01001461
Russell King012b7d32009-07-09 15:13:56 +01001462 host->hw_designer = amba_manf(dev);
1463 host->hw_revision = amba_rev(dev);
Linus Walleij64de0282010-02-19 01:09:10 +01001464 dev_dbg(mmc_dev(mmc), "designer ID = 0x%02x\n", host->hw_designer);
1465 dev_dbg(mmc_dev(mmc), "revision = 0x%01x\n", host->hw_revision);
Russell King012b7d32009-07-09 15:13:56 +01001466
Ulf Hansson665ba562013-05-13 15:39:17 +01001467 host->clk = devm_clk_get(&dev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 if (IS_ERR(host->clk)) {
1469 ret = PTR_ERR(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 goto host_free;
1471 }
1472
Julia Lawallac940932012-08-26 16:00:59 +00001473 ret = clk_prepare_enable(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 if (ret)
Ulf Hansson665ba562013-05-13 15:39:17 +01001475 goto host_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477 host->plat = plat;
Rabin Vincent4956e102010-07-21 12:54:40 +01001478 host->variant = variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 host->mclk = clk_get_rate(host->clk);
Linus Walleijc8df9a52008-04-29 09:34:07 +01001480 /*
1481 * According to the spec, mclk is max 100 MHz,
1482 * so we try to adjust the clock down to this,
1483 * (if possible).
1484 */
1485 if (host->mclk > 100000000) {
1486 ret = clk_set_rate(host->clk, 100000000);
1487 if (ret < 0)
1488 goto clk_disable;
1489 host->mclk = clk_get_rate(host->clk);
Linus Walleij64de0282010-02-19 01:09:10 +01001490 dev_dbg(mmc_dev(mmc), "eventual mclk rate: %u Hz\n",
1491 host->mclk);
Linus Walleijc8df9a52008-04-29 09:34:07 +01001492 }
Russell Kingc8ebae32011-01-11 19:35:53 +00001493 host->phybase = dev->res.start;
Linus Walleijdc890c22009-06-07 23:27:31 +01001494 host->base = ioremap(dev->res.start, resource_size(&dev->res));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 if (!host->base) {
1496 ret = -ENOMEM;
1497 goto clk_disable;
1498 }
1499
Ulf Hansson01259622013-05-15 20:53:22 +01001500 if (variant->busy_detect) {
1501 mmci_ops.card_busy = mmci_card_busy;
1502 mmci_write_datactrlreg(host, MCI_ST_DPSM_BUSYMODE);
1503 }
1504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 mmc->ops = &mmci_ops;
Linus Walleij7f294e42011-07-08 09:57:15 +01001506 /*
1507 * The ARM and ST versions of the block have slightly different
1508 * clock divider equations which means that the minimum divider
1509 * differs too.
1510 */
1511 if (variant->st_clkdiv)
1512 mmc->f_min = DIV_ROUND_UP(host->mclk, 257);
1513 else
1514 mmc->f_min = DIV_ROUND_UP(host->mclk, 512);
Linus Walleij808d97c2010-04-08 07:39:38 +01001515 /*
1516 * If the platform data supplies a maximum operating
1517 * frequency, this takes precedence. Else, we fall back
1518 * to using the module parameter, which has a (low)
1519 * default value in case it is not specified. Either
1520 * value must not exceed the clock rate into the block,
1521 * of course.
1522 */
1523 if (plat->f_max)
1524 mmc->f_max = min(host->mclk, plat->f_max);
1525 else
1526 mmc->f_max = min(host->mclk, fmax);
Linus Walleij64de0282010-02-19 01:09:10 +01001527 dev_dbg(mmc_dev(mmc), "clocking block at %u Hz\n", mmc->f_max);
1528
Ulf Hansson599c1d52013-01-07 16:22:50 +01001529 /* Get regulators and the supported OCR mask */
1530 mmc_regulator_get_supply(mmc);
1531 if (!mmc->ocr_avail)
Linus Walleij34e84f32009-09-22 14:41:40 +01001532 mmc->ocr_avail = plat->ocr_mask;
Ulf Hansson599c1d52013-01-07 16:22:50 +01001533 else if (plat->ocr_mask)
1534 dev_warn(mmc_dev(mmc), "Platform OCR mask is ignored\n");
1535
Linus Walleij9e6c82c2009-09-14 12:57:11 +01001536 mmc->caps = plat->capabilities;
Per Forlin5a092622011-11-14 12:02:28 +01001537 mmc->caps2 = plat->capabilities2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
Ulf Hansson70be2082013-01-07 15:35:06 +01001539 /* We support these PM capabilities. */
1540 mmc->pm_caps = MMC_PM_KEEP_POWER;
1541
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 /*
1543 * We can do SGIO
1544 */
Martin K. Petersena36274e2010-09-10 01:33:59 -04001545 mmc->max_segs = NR_SG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
1547 /*
Rabin Vincent08458ef2010-07-21 12:55:59 +01001548 * Since only a certain number of bits are valid in the data length
1549 * register, we must ensure that we don't exceed 2^num-1 bytes in a
1550 * single request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 */
Rabin Vincent08458ef2010-07-21 12:55:59 +01001552 mmc->max_req_size = (1 << variant->datalength_bits) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
1554 /*
1555 * Set the maximum segment size. Since we aren't doing DMA
1556 * (yet) we are only limited by the data length register.
1557 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001558 mmc->max_seg_size = mmc->max_req_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001560 /*
1561 * Block size can be up to 2048 bytes, but must be a power of two.
1562 */
Will Deacon8f7f6b72012-02-24 11:25:21 +00001563 mmc->max_blk_size = 1 << 11;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001564
Pierre Ossman55db8902006-11-21 17:55:45 +01001565 /*
Will Deacon8f7f6b72012-02-24 11:25:21 +00001566 * Limit the number of blocks transferred so that we don't overflow
1567 * the maximum request size.
Pierre Ossman55db8902006-11-21 17:55:45 +01001568 */
Will Deacon8f7f6b72012-02-24 11:25:21 +00001569 mmc->max_blk_count = mmc->max_req_size >> 11;
Pierre Ossman55db8902006-11-21 17:55:45 +01001570
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 spin_lock_init(&host->lock);
1572
1573 writel(0, host->base + MMCIMASK0);
1574 writel(0, host->base + MMCIMASK1);
1575 writel(0xfff, host->base + MMCICLEAR);
1576
Roland Stigge2805b9a2012-06-17 21:14:27 +01001577 if (plat->gpio_cd == -EPROBE_DEFER) {
1578 ret = -EPROBE_DEFER;
1579 goto err_gpio_cd;
1580 }
Russell King89001442009-07-09 15:16:07 +01001581 if (gpio_is_valid(plat->gpio_cd)) {
1582 ret = gpio_request(plat->gpio_cd, DRIVER_NAME " (cd)");
1583 if (ret == 0)
1584 ret = gpio_direction_input(plat->gpio_cd);
1585 if (ret == 0)
1586 host->gpio_cd = plat->gpio_cd;
1587 else if (ret != -ENOSYS)
1588 goto err_gpio_cd;
Rabin Vincent148b8b32010-08-09 12:55:48 +01001589
Linus Walleij17ee0832011-05-05 17:23:10 +01001590 /*
1591 * A gpio pin that will detect cards when inserted and removed
1592 * will most likely want to trigger on the edges if it is
1593 * 0 when ejected and 1 when inserted (or mutatis mutandis
1594 * for the inverted case) so we request triggers on both
1595 * edges.
1596 */
Rabin Vincent148b8b32010-08-09 12:55:48 +01001597 ret = request_any_context_irq(gpio_to_irq(plat->gpio_cd),
Linus Walleij17ee0832011-05-05 17:23:10 +01001598 mmci_cd_irq,
1599 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
1600 DRIVER_NAME " (cd)", host);
Rabin Vincent148b8b32010-08-09 12:55:48 +01001601 if (ret >= 0)
1602 host->gpio_cd_irq = gpio_to_irq(plat->gpio_cd);
Russell King89001442009-07-09 15:16:07 +01001603 }
Roland Stigge2805b9a2012-06-17 21:14:27 +01001604 if (plat->gpio_wp == -EPROBE_DEFER) {
1605 ret = -EPROBE_DEFER;
1606 goto err_gpio_wp;
1607 }
Russell King89001442009-07-09 15:16:07 +01001608 if (gpio_is_valid(plat->gpio_wp)) {
1609 ret = gpio_request(plat->gpio_wp, DRIVER_NAME " (wp)");
1610 if (ret == 0)
1611 ret = gpio_direction_input(plat->gpio_wp);
1612 if (ret == 0)
1613 host->gpio_wp = plat->gpio_wp;
1614 else if (ret != -ENOSYS)
1615 goto err_gpio_wp;
1616 }
1617
Rabin Vincent4b8caec2010-08-09 12:56:40 +01001618 if ((host->plat->status || host->gpio_cd != -ENOSYS)
1619 && host->gpio_cd_irq < 0)
Rabin Vincent148b8b32010-08-09 12:55:48 +01001620 mmc->caps |= MMC_CAP_NEEDS_POLL;
1621
Thomas Gleixnerdace1452006-07-01 19:29:38 -07001622 ret = request_irq(dev->irq[0], mmci_irq, IRQF_SHARED, DRIVER_NAME " (cmd)", host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 if (ret)
1624 goto unmap;
1625
Russell Kingdfb851852012-05-03 11:33:15 +01001626 if (!dev->irq[1])
Linus Walleij2686b4b2010-10-19 12:39:48 +01001627 host->singleirq = true;
1628 else {
1629 ret = request_irq(dev->irq[1], mmci_pio_irq, IRQF_SHARED,
1630 DRIVER_NAME " (pio)", host);
1631 if (ret)
1632 goto irq0_free;
1633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Linus Walleij8cb28152011-01-24 15:22:13 +01001635 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
1637 amba_set_drvdata(dev, mmc);
1638
Russell Kingc8ebae32011-01-11 19:35:53 +00001639 dev_info(&dev->dev, "%s: PL%03x manf %x rev%u at 0x%08llx irq %d,%d (pio)\n",
1640 mmc_hostname(mmc), amba_part(dev), amba_manf(dev),
1641 amba_rev(dev), (unsigned long long)dev->res.start,
1642 dev->irq[0], dev->irq[1]);
1643
1644 mmci_dma_setup(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Ulf Hansson2cd976c2011-12-13 17:01:11 +01001646 pm_runtime_set_autosuspend_delay(&dev->dev, 50);
1647 pm_runtime_use_autosuspend(&dev->dev);
Russell King1c3be362011-08-14 09:17:05 +01001648 pm_runtime_put(&dev->dev);
1649
Russell King8c11a942010-12-28 19:40:40 +00001650 mmc_add_host(mmc);
1651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 return 0;
1653
1654 irq0_free:
1655 free_irq(dev->irq[0], host);
1656 unmap:
Russell King89001442009-07-09 15:16:07 +01001657 if (host->gpio_wp != -ENOSYS)
1658 gpio_free(host->gpio_wp);
1659 err_gpio_wp:
Rabin Vincent148b8b32010-08-09 12:55:48 +01001660 if (host->gpio_cd_irq >= 0)
1661 free_irq(host->gpio_cd_irq, host);
Russell King89001442009-07-09 15:16:07 +01001662 if (host->gpio_cd != -ENOSYS)
1663 gpio_free(host->gpio_cd);
1664 err_gpio_cd:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 iounmap(host->base);
1666 clk_disable:
Julia Lawallac940932012-08-26 16:00:59 +00001667 clk_disable_unprepare(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 host_free:
1669 mmc_free_host(mmc);
1670 rel_regions:
1671 amba_release_regions(dev);
1672 out:
1673 return ret;
1674}
1675
Bill Pemberton6e0ee712012-11-19 13:26:03 -05001676static int mmci_remove(struct amba_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677{
1678 struct mmc_host *mmc = amba_get_drvdata(dev);
1679
1680 amba_set_drvdata(dev, NULL);
1681
1682 if (mmc) {
1683 struct mmci_host *host = mmc_priv(mmc);
1684
Russell King1c3be362011-08-14 09:17:05 +01001685 /*
1686 * Undo pm_runtime_put() in probe. We use the _sync
1687 * version here so that we can access the primecell.
1688 */
1689 pm_runtime_get_sync(&dev->dev);
1690
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 mmc_remove_host(mmc);
1692
1693 writel(0, host->base + MMCIMASK0);
1694 writel(0, host->base + MMCIMASK1);
1695
1696 writel(0, host->base + MMCICOMMAND);
1697 writel(0, host->base + MMCIDATACTRL);
1698
Russell Kingc8ebae32011-01-11 19:35:53 +00001699 mmci_dma_release(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 free_irq(dev->irq[0], host);
Linus Walleij2686b4b2010-10-19 12:39:48 +01001701 if (!host->singleirq)
1702 free_irq(dev->irq[1], host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Russell King89001442009-07-09 15:16:07 +01001704 if (host->gpio_wp != -ENOSYS)
1705 gpio_free(host->gpio_wp);
Rabin Vincent148b8b32010-08-09 12:55:48 +01001706 if (host->gpio_cd_irq >= 0)
1707 free_irq(host->gpio_cd_irq, host);
Russell King89001442009-07-09 15:16:07 +01001708 if (host->gpio_cd != -ENOSYS)
1709 gpio_free(host->gpio_cd);
1710
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 iounmap(host->base);
Julia Lawallac940932012-08-26 16:00:59 +00001712 clk_disable_unprepare(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
1714 mmc_free_host(mmc);
1715
1716 amba_release_regions(dev);
1717 }
1718
1719 return 0;
1720}
1721
Ulf Hansson48fa7002011-12-13 16:59:34 +01001722#ifdef CONFIG_SUSPEND
1723static int mmci_suspend(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724{
Ulf Hansson48fa7002011-12-13 16:59:34 +01001725 struct amba_device *adev = to_amba_device(dev);
1726 struct mmc_host *mmc = amba_get_drvdata(adev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 int ret = 0;
1728
1729 if (mmc) {
1730 struct mmci_host *host = mmc_priv(mmc);
1731
Matt Fleming1a13f8f2010-05-26 14:42:08 -07001732 ret = mmc_suspend_host(mmc);
Ulf Hansson2cd976c2011-12-13 17:01:11 +01001733 if (ret == 0) {
1734 pm_runtime_get_sync(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 writel(0, host->base + MMCIMASK0);
Ulf Hansson2cd976c2011-12-13 17:01:11 +01001736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 }
1738
1739 return ret;
1740}
1741
Ulf Hansson48fa7002011-12-13 16:59:34 +01001742static int mmci_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743{
Ulf Hansson48fa7002011-12-13 16:59:34 +01001744 struct amba_device *adev = to_amba_device(dev);
1745 struct mmc_host *mmc = amba_get_drvdata(adev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 int ret = 0;
1747
1748 if (mmc) {
1749 struct mmci_host *host = mmc_priv(mmc);
1750
1751 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
Ulf Hansson2cd976c2011-12-13 17:01:11 +01001752 pm_runtime_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
1754 ret = mmc_resume_host(mmc);
1755 }
1756
1757 return ret;
1758}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759#endif
1760
Ulf Hansson82592932013-01-09 11:15:26 +01001761#ifdef CONFIG_PM_RUNTIME
1762static int mmci_runtime_suspend(struct device *dev)
1763{
1764 struct amba_device *adev = to_amba_device(dev);
1765 struct mmc_host *mmc = amba_get_drvdata(adev);
1766
1767 if (mmc) {
1768 struct mmci_host *host = mmc_priv(mmc);
Ulf Hanssone36bd9c62013-09-04 09:00:37 +01001769 pinctrl_pm_select_sleep_state(dev);
Ulf Hansson82592932013-01-09 11:15:26 +01001770 clk_disable_unprepare(host->clk);
1771 }
1772
1773 return 0;
1774}
1775
1776static int mmci_runtime_resume(struct device *dev)
1777{
1778 struct amba_device *adev = to_amba_device(dev);
1779 struct mmc_host *mmc = amba_get_drvdata(adev);
1780
1781 if (mmc) {
1782 struct mmci_host *host = mmc_priv(mmc);
1783 clk_prepare_enable(host->clk);
Ulf Hanssone36bd9c62013-09-04 09:00:37 +01001784 pinctrl_pm_select_default_state(dev);
Ulf Hansson82592932013-01-09 11:15:26 +01001785 }
1786
1787 return 0;
1788}
1789#endif
1790
Ulf Hansson48fa7002011-12-13 16:59:34 +01001791static const struct dev_pm_ops mmci_dev_pm_ops = {
1792 SET_SYSTEM_SLEEP_PM_OPS(mmci_suspend, mmci_resume)
Ulf Hansson82592932013-01-09 11:15:26 +01001793 SET_RUNTIME_PM_OPS(mmci_runtime_suspend, mmci_runtime_resume, NULL)
Ulf Hansson48fa7002011-12-13 16:59:34 +01001794};
1795
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796static struct amba_id mmci_ids[] = {
1797 {
1798 .id = 0x00041180,
Pawel Moll768fbc12011-03-11 17:18:07 +00001799 .mask = 0xff0fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001800 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 },
1802 {
Pawel Moll768fbc12011-03-11 17:18:07 +00001803 .id = 0x01041180,
1804 .mask = 0xff0fffff,
1805 .data = &variant_arm_extended_fifo,
1806 },
1807 {
Pawel Moll3a372982013-01-24 14:12:45 +01001808 .id = 0x02041180,
1809 .mask = 0xff0fffff,
1810 .data = &variant_arm_extended_fifo_hwfc,
1811 },
1812 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 .id = 0x00041181,
1814 .mask = 0x000fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001815 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 },
Linus Walleijcc30d602009-01-04 15:18:54 +01001817 /* ST Micro variants */
1818 {
1819 .id = 0x00180180,
1820 .mask = 0x00ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001821 .data = &variant_u300,
Linus Walleijcc30d602009-01-04 15:18:54 +01001822 },
1823 {
Linus Walleij34fd4212012-04-10 17:43:59 +01001824 .id = 0x10180180,
1825 .mask = 0xf0ffffff,
1826 .data = &variant_nomadik,
1827 },
1828 {
Linus Walleijcc30d602009-01-04 15:18:54 +01001829 .id = 0x00280180,
1830 .mask = 0x00ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001831 .data = &variant_u300,
1832 },
1833 {
1834 .id = 0x00480180,
Philippe Langlais1784b152011-03-25 08:51:52 +01001835 .mask = 0xf0ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001836 .data = &variant_ux500,
Linus Walleijcc30d602009-01-04 15:18:54 +01001837 },
Philippe Langlais1784b152011-03-25 08:51:52 +01001838 {
1839 .id = 0x10480180,
1840 .mask = 0xf0ffffff,
1841 .data = &variant_ux500v2,
1842 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 { 0, 0 },
1844};
1845
Dave Martin9f998352011-10-05 15:15:21 +01001846MODULE_DEVICE_TABLE(amba, mmci_ids);
1847
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848static struct amba_driver mmci_driver = {
1849 .drv = {
1850 .name = DRIVER_NAME,
Ulf Hansson48fa7002011-12-13 16:59:34 +01001851 .pm = &mmci_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 },
1853 .probe = mmci_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05001854 .remove = mmci_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 .id_table = mmci_ids,
1856};
1857
viresh kumar9e5ed092012-03-15 10:40:38 +01001858module_amba_driver(mmci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860module_param(fmax, uint, 0444);
1861
1862MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
1863MODULE_LICENSE("GPL");