blob: 063136d89b42e1caa398365f6d21f798b62c7a16 [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>
Ulf Hanssonef289982014-03-17 13:56:32 +010016#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/interrupt.h>
Russell King613b1522011-01-30 21:06:53 +000018#include <linux/kernel.h>
Lee Jones000bc9d2012-04-16 10:18:43 +010019#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/delay.h>
21#include <linux/err.h>
22#include <linux/highmem.h>
Nicolas Pitre019a5f52007-10-11 01:06:03 -040023#include <linux/log2.h>
Ulf Hansson70be2082013-01-07 15:35:06 +010024#include <linux/mmc/pm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/mmc/host.h>
Linus Walleij34177802010-10-19 12:43:58 +010026#include <linux/mmc/card.h>
Ulf Hanssond2762092014-03-17 13:56:19 +010027#include <linux/mmc/slot-gpio.h>
Russell Kinga62c80e2006-01-07 13:52:45 +000028#include <linux/amba/bus.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000029#include <linux/clk.h>
Jens Axboebd6dee62007-10-24 09:01:09 +020030#include <linux/scatterlist.h>
Russell King89001442009-07-09 15:16:07 +010031#include <linux/gpio.h>
Lee Jones9a597012012-04-12 16:51:13 +010032#include <linux/of_gpio.h>
Linus Walleij34e84f32009-09-22 14:41:40 +010033#include <linux/regulator/consumer.h>
Russell Kingc8ebae32011-01-11 19:35:53 +000034#include <linux/dmaengine.h>
35#include <linux/dma-mapping.h>
36#include <linux/amba/mmci.h>
Russell King1c3be362011-08-14 09:17:05 +010037#include <linux/pm_runtime.h>
Viresh Kumar258aea72012-02-01 16:12:19 +053038#include <linux/types.h>
Linus Walleija9a83782012-10-29 14:39:30 +010039#include <linux/pinctrl/consumer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Russell King7b09cda2005-07-01 12:02:59 +010041#include <asm/div64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/io.h>
Russell Kingc6b8fda2005-10-28 14:05:16 +010043#include <asm/sizes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#include "mmci.h"
46
47#define DRIVER_NAME "mmci-pl18x"
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049static unsigned int fmax = 515633;
50
Rabin Vincent4956e102010-07-21 12:54:40 +010051/**
52 * struct variant_data - MMCI variant-specific quirks
53 * @clkreg: default value for MCICLOCK register
Rabin Vincent4380c142010-07-21 12:55:18 +010054 * @clkreg_enable: enable value for MMCICLOCK register
Srinivas Kandagatlae1412d82014-06-02 10:09:23 +010055 * @clkreg_8bit_bus_enable: enable value for 8 bit bus
Srinivas Kandagatlae8740642014-06-02 10:09:30 +010056 * @clkreg_neg_edge_enable: enable value for inverted data/cmd output
Rabin Vincent08458ef2010-07-21 12:55:59 +010057 * @datalength_bits: number of bits in the MMCIDATALENGTH register
Rabin Vincent8301bb62010-08-09 12:57:30 +010058 * @fifosize: number of bytes that can be written when MMCI_TXFIFOEMPTY
59 * is asserted (likewise for RX)
60 * @fifohalfsize: number of bytes that can be written when MCI_TXFIFOHALFEMPTY
61 * is asserted (likewise for RX)
Srinivas Kandagatlaae7b0062014-06-02 10:09:39 +010062 * @data_cmd_enable: enable value for data commands.
Linus Walleij34177802010-10-19 12:43:58 +010063 * @sdio: variant supports SDIO
Linus Walleijb70a67f2010-12-06 09:24:14 +010064 * @st_clkdiv: true if using a ST-specific clock divider algorithm
Srinivas Kandagatlae17dca22014-06-02 10:09:15 +010065 * @datactrl_mask_ddrmode: ddr mode mask in datactrl register.
Philippe Langlais1784b152011-03-25 08:51:52 +010066 * @blksz_datactrl16: true if Block size is at b16..b30 position in datactrl register
Srinivas Kandagatlaff783232014-06-02 10:09:06 +010067 * @blksz_datactrl4: true if Block size is at b4..b16 position in datactrl
68 * register
Ulf Hansson7d72a1d2011-12-13 16:54:55 +010069 * @pwrreg_powerup: power up value for MMCIPOWER register
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +010070 * @f_max: maximum clk frequency supported by the controller.
Ulf Hansson4d1a3a02011-12-13 16:57:07 +010071 * @signal_direction: input/out direction of bus signals can be indicated
Ulf Hanssonf4670da2013-01-09 17:19:54 +010072 * @pwrreg_clkgate: MMCIPOWER register must be used to gate the clock
Ulf Hansson01259622013-05-15 20:53:22 +010073 * @busy_detect: true if busy detection on dat0 is supported
Ulf Hansson1ff44432013-09-04 09:05:17 +010074 * @pwrreg_nopower: bits in MMCIPOWER don't controls ext. power supply
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +010075 * @explicit_mclk_control: enable explicit mclk control in driver.
Srinivas Kandagatla9c34b732014-06-02 10:10:04 +010076 * @qcom_fifo: enables qcom specific fifo pio read logic.
Rabin Vincent4956e102010-07-21 12:54:40 +010077 */
78struct variant_data {
79 unsigned int clkreg;
Rabin Vincent4380c142010-07-21 12:55:18 +010080 unsigned int clkreg_enable;
Srinivas Kandagatlae1412d82014-06-02 10:09:23 +010081 unsigned int clkreg_8bit_bus_enable;
Srinivas Kandagatlae8740642014-06-02 10:09:30 +010082 unsigned int clkreg_neg_edge_enable;
Rabin Vincent08458ef2010-07-21 12:55:59 +010083 unsigned int datalength_bits;
Rabin Vincent8301bb62010-08-09 12:57:30 +010084 unsigned int fifosize;
85 unsigned int fifohalfsize;
Srinivas Kandagatlaae7b0062014-06-02 10:09:39 +010086 unsigned int data_cmd_enable;
Srinivas Kandagatlae17dca22014-06-02 10:09:15 +010087 unsigned int datactrl_mask_ddrmode;
Linus Walleij34177802010-10-19 12:43:58 +010088 bool sdio;
Linus Walleijb70a67f2010-12-06 09:24:14 +010089 bool st_clkdiv;
Philippe Langlais1784b152011-03-25 08:51:52 +010090 bool blksz_datactrl16;
Srinivas Kandagatlaff783232014-06-02 10:09:06 +010091 bool blksz_datactrl4;
Ulf Hansson7d72a1d2011-12-13 16:54:55 +010092 u32 pwrreg_powerup;
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +010093 u32 f_max;
Ulf Hansson4d1a3a02011-12-13 16:57:07 +010094 bool signal_direction;
Ulf Hanssonf4670da2013-01-09 17:19:54 +010095 bool pwrreg_clkgate;
Ulf Hansson01259622013-05-15 20:53:22 +010096 bool busy_detect;
Ulf Hansson1ff44432013-09-04 09:05:17 +010097 bool pwrreg_nopower;
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +010098 bool explicit_mclk_control;
Srinivas Kandagatla9c34b732014-06-02 10:10:04 +010099 bool qcom_fifo;
Rabin Vincent4956e102010-07-21 12:54:40 +0100100};
101
102static struct variant_data variant_arm = {
Rabin Vincent8301bb62010-08-09 12:57:30 +0100103 .fifosize = 16 * 4,
104 .fifohalfsize = 8 * 4,
Rabin Vincent08458ef2010-07-21 12:55:59 +0100105 .datalength_bits = 16,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +0100106 .pwrreg_powerup = MCI_PWR_UP,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +0100107 .f_max = 100000000,
Rabin Vincent4956e102010-07-21 12:54:40 +0100108};
109
Pawel Moll768fbc12011-03-11 17:18:07 +0000110static struct variant_data variant_arm_extended_fifo = {
111 .fifosize = 128 * 4,
112 .fifohalfsize = 64 * 4,
113 .datalength_bits = 16,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +0100114 .pwrreg_powerup = MCI_PWR_UP,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +0100115 .f_max = 100000000,
Pawel Moll768fbc12011-03-11 17:18:07 +0000116};
117
Pawel Moll3a372982013-01-24 14:12:45 +0100118static struct variant_data variant_arm_extended_fifo_hwfc = {
119 .fifosize = 128 * 4,
120 .fifohalfsize = 64 * 4,
121 .clkreg_enable = MCI_ARM_HWFCEN,
122 .datalength_bits = 16,
123 .pwrreg_powerup = MCI_PWR_UP,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +0100124 .f_max = 100000000,
Pawel Moll3a372982013-01-24 14:12:45 +0100125};
126
Rabin Vincent4956e102010-07-21 12:54:40 +0100127static struct variant_data variant_u300 = {
Rabin Vincent8301bb62010-08-09 12:57:30 +0100128 .fifosize = 16 * 4,
129 .fifohalfsize = 8 * 4,
Linus Walleij49ac2152011-03-04 14:54:16 +0100130 .clkreg_enable = MCI_ST_U300_HWFCEN,
Srinivas Kandagatlae1412d82014-06-02 10:09:23 +0100131 .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
Rabin Vincent08458ef2010-07-21 12:55:59 +0100132 .datalength_bits = 16,
Linus Walleij34177802010-10-19 12:43:58 +0100133 .sdio = true,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +0100134 .pwrreg_powerup = MCI_PWR_ON,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +0100135 .f_max = 100000000,
Ulf Hansson4d1a3a02011-12-13 16:57:07 +0100136 .signal_direction = true,
Ulf Hanssonf4670da2013-01-09 17:19:54 +0100137 .pwrreg_clkgate = true,
Ulf Hansson1ff44432013-09-04 09:05:17 +0100138 .pwrreg_nopower = true,
Rabin Vincent4956e102010-07-21 12:54:40 +0100139};
140
Linus Walleij34fd4212012-04-10 17:43:59 +0100141static struct variant_data variant_nomadik = {
142 .fifosize = 16 * 4,
143 .fifohalfsize = 8 * 4,
144 .clkreg = MCI_CLK_ENABLE,
145 .datalength_bits = 24,
146 .sdio = true,
147 .st_clkdiv = true,
148 .pwrreg_powerup = MCI_PWR_ON,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +0100149 .f_max = 100000000,
Linus Walleij34fd4212012-04-10 17:43:59 +0100150 .signal_direction = true,
Ulf Hanssonf4670da2013-01-09 17:19:54 +0100151 .pwrreg_clkgate = true,
Ulf Hansson1ff44432013-09-04 09:05:17 +0100152 .pwrreg_nopower = true,
Linus Walleij34fd4212012-04-10 17:43:59 +0100153};
154
Rabin Vincent4956e102010-07-21 12:54:40 +0100155static struct variant_data variant_ux500 = {
Rabin Vincent8301bb62010-08-09 12:57:30 +0100156 .fifosize = 30 * 4,
157 .fifohalfsize = 8 * 4,
Rabin Vincent4956e102010-07-21 12:54:40 +0100158 .clkreg = MCI_CLK_ENABLE,
Linus Walleij49ac2152011-03-04 14:54:16 +0100159 .clkreg_enable = MCI_ST_UX500_HWFCEN,
Srinivas Kandagatlae1412d82014-06-02 10:09:23 +0100160 .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
Srinivas Kandagatlae8740642014-06-02 10:09:30 +0100161 .clkreg_neg_edge_enable = MCI_ST_UX500_NEG_EDGE,
Rabin Vincent08458ef2010-07-21 12:55:59 +0100162 .datalength_bits = 24,
Linus Walleij34177802010-10-19 12:43:58 +0100163 .sdio = true,
Linus Walleijb70a67f2010-12-06 09:24:14 +0100164 .st_clkdiv = true,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +0100165 .pwrreg_powerup = MCI_PWR_ON,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +0100166 .f_max = 100000000,
Ulf Hansson4d1a3a02011-12-13 16:57:07 +0100167 .signal_direction = true,
Ulf Hanssonf4670da2013-01-09 17:19:54 +0100168 .pwrreg_clkgate = true,
Ulf Hansson01259622013-05-15 20:53:22 +0100169 .busy_detect = true,
Ulf Hansson1ff44432013-09-04 09:05:17 +0100170 .pwrreg_nopower = true,
Rabin Vincent4956e102010-07-21 12:54:40 +0100171};
Linus Walleijb70a67f2010-12-06 09:24:14 +0100172
Philippe Langlais1784b152011-03-25 08:51:52 +0100173static struct variant_data variant_ux500v2 = {
174 .fifosize = 30 * 4,
175 .fifohalfsize = 8 * 4,
176 .clkreg = MCI_CLK_ENABLE,
177 .clkreg_enable = MCI_ST_UX500_HWFCEN,
Srinivas Kandagatlae1412d82014-06-02 10:09:23 +0100178 .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
Srinivas Kandagatlae8740642014-06-02 10:09:30 +0100179 .clkreg_neg_edge_enable = MCI_ST_UX500_NEG_EDGE,
Srinivas Kandagatlae17dca22014-06-02 10:09:15 +0100180 .datactrl_mask_ddrmode = MCI_ST_DPSM_DDRMODE,
Philippe Langlais1784b152011-03-25 08:51:52 +0100181 .datalength_bits = 24,
182 .sdio = true,
183 .st_clkdiv = true,
184 .blksz_datactrl16 = true,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +0100185 .pwrreg_powerup = MCI_PWR_ON,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +0100186 .f_max = 100000000,
Ulf Hansson4d1a3a02011-12-13 16:57:07 +0100187 .signal_direction = true,
Ulf Hanssonf4670da2013-01-09 17:19:54 +0100188 .pwrreg_clkgate = true,
Ulf Hansson01259622013-05-15 20:53:22 +0100189 .busy_detect = true,
Ulf Hansson1ff44432013-09-04 09:05:17 +0100190 .pwrreg_nopower = true,
Philippe Langlais1784b152011-03-25 08:51:52 +0100191};
192
Ulf Hansson01259622013-05-15 20:53:22 +0100193static int mmci_card_busy(struct mmc_host *mmc)
194{
195 struct mmci_host *host = mmc_priv(mmc);
196 unsigned long flags;
197 int busy = 0;
198
199 pm_runtime_get_sync(mmc_dev(mmc));
200
201 spin_lock_irqsave(&host->lock, flags);
202 if (readl(host->base + MMCISTATUS) & MCI_ST_CARDBUSY)
203 busy = 1;
204 spin_unlock_irqrestore(&host->lock, flags);
205
206 pm_runtime_mark_last_busy(mmc_dev(mmc));
207 pm_runtime_put_autosuspend(mmc_dev(mmc));
208
209 return busy;
210}
211
Linus Walleija6a64642009-09-14 12:56:14 +0100212/*
Ulf Hansson653a7612013-01-21 21:29:34 +0100213 * Validate mmc prerequisites
214 */
215static int mmci_validate_data(struct mmci_host *host,
216 struct mmc_data *data)
217{
218 if (!data)
219 return 0;
220
221 if (!is_power_of_2(data->blksz)) {
222 dev_err(mmc_dev(host->mmc),
223 "unsupported block size (%d bytes)\n", data->blksz);
224 return -EINVAL;
225 }
226
227 return 0;
228}
229
Ulf Hanssonf829c042013-09-04 09:01:15 +0100230static void mmci_reg_delay(struct mmci_host *host)
231{
232 /*
233 * According to the spec, at least three feedback clock cycles
234 * of max 52 MHz must pass between two writes to the MMCICLOCK reg.
235 * Three MCLK clock cycles must pass between two MMCIPOWER reg writes.
236 * Worst delay time during card init is at 100 kHz => 30 us.
237 * Worst delay time when up and running is at 25 MHz => 120 ns.
238 */
239 if (host->cclk < 25000000)
240 udelay(30);
241 else
242 ndelay(120);
243}
244
Ulf Hansson653a7612013-01-21 21:29:34 +0100245/*
Linus Walleija6a64642009-09-14 12:56:14 +0100246 * This must be called with host->lock held
247 */
Ulf Hansson7437cfa2012-01-18 09:17:27 +0100248static void mmci_write_clkreg(struct mmci_host *host, u32 clk)
249{
250 if (host->clk_reg != clk) {
251 host->clk_reg = clk;
252 writel(clk, host->base + MMCICLOCK);
253 }
254}
255
256/*
257 * This must be called with host->lock held
258 */
259static void mmci_write_pwrreg(struct mmci_host *host, u32 pwr)
260{
261 if (host->pwr_reg != pwr) {
262 host->pwr_reg = pwr;
263 writel(pwr, host->base + MMCIPOWER);
264 }
265}
266
267/*
268 * This must be called with host->lock held
269 */
Ulf Hansson9cc639a2013-05-15 20:48:23 +0100270static void mmci_write_datactrlreg(struct mmci_host *host, u32 datactrl)
271{
Ulf Hansson01259622013-05-15 20:53:22 +0100272 /* Keep ST Micro busy mode if enabled */
273 datactrl |= host->datactrl_reg & MCI_ST_DPSM_BUSYMODE;
274
Ulf Hansson9cc639a2013-05-15 20:48:23 +0100275 if (host->datactrl_reg != datactrl) {
276 host->datactrl_reg = datactrl;
277 writel(datactrl, host->base + MMCIDATACTRL);
278 }
279}
280
281/*
282 * This must be called with host->lock held
283 */
Linus Walleija6a64642009-09-14 12:56:14 +0100284static void mmci_set_clkreg(struct mmci_host *host, unsigned int desired)
285{
Rabin Vincent4956e102010-07-21 12:54:40 +0100286 struct variant_data *variant = host->variant;
287 u32 clk = variant->clkreg;
Linus Walleija6a64642009-09-14 12:56:14 +0100288
Ulf Hanssonc58a8502013-05-13 15:40:03 +0100289 /* Make sure cclk reflects the current calculated clock */
290 host->cclk = 0;
291
Linus Walleija6a64642009-09-14 12:56:14 +0100292 if (desired) {
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +0100293 if (variant->explicit_mclk_control) {
294 host->cclk = host->mclk;
295 } else if (desired >= host->mclk) {
Linus Walleij991a86e2010-12-10 09:35:53 +0100296 clk = MCI_CLK_BYPASS;
Linus Walleij399bc482011-04-01 07:59:17 +0100297 if (variant->st_clkdiv)
298 clk |= MCI_ST_UX500_NEG_EDGE;
Linus Walleija6a64642009-09-14 12:56:14 +0100299 host->cclk = host->mclk;
Linus Walleijb70a67f2010-12-06 09:24:14 +0100300 } else if (variant->st_clkdiv) {
301 /*
302 * DB8500 TRM says f = mclk / (clkdiv + 2)
303 * => clkdiv = (mclk / f) - 2
304 * Round the divider up so we don't exceed the max
305 * frequency
306 */
307 clk = DIV_ROUND_UP(host->mclk, desired) - 2;
308 if (clk >= 256)
309 clk = 255;
310 host->cclk = host->mclk / (clk + 2);
Linus Walleija6a64642009-09-14 12:56:14 +0100311 } else {
Linus Walleijb70a67f2010-12-06 09:24:14 +0100312 /*
313 * PL180 TRM says f = mclk / (2 * (clkdiv + 1))
314 * => clkdiv = mclk / (2 * f) - 1
315 */
Linus Walleija6a64642009-09-14 12:56:14 +0100316 clk = host->mclk / (2 * desired) - 1;
317 if (clk >= 256)
318 clk = 255;
319 host->cclk = host->mclk / (2 * (clk + 1));
320 }
Rabin Vincent4380c142010-07-21 12:55:18 +0100321
322 clk |= variant->clkreg_enable;
Linus Walleija6a64642009-09-14 12:56:14 +0100323 clk |= MCI_CLK_ENABLE;
324 /* This hasn't proven to be worthwhile */
325 /* clk |= MCI_CLK_PWRSAVE; */
326 }
327
Ulf Hanssonc58a8502013-05-13 15:40:03 +0100328 /* Set actual clock for debug */
329 host->mmc->actual_clock = host->cclk;
330
Linus Walleij9e6c82c2009-09-14 12:57:11 +0100331 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4)
Linus Walleij771dc152010-04-08 07:38:52 +0100332 clk |= MCI_4BIT_BUS;
333 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_8)
Srinivas Kandagatlae1412d82014-06-02 10:09:23 +0100334 clk |= variant->clkreg_8bit_bus_enable;
Linus Walleij9e6c82c2009-09-14 12:57:11 +0100335
Seungwon Jeon6dad6c92014-03-14 21:12:13 +0900336 if (host->mmc->ios.timing == MMC_TIMING_UHS_DDR50 ||
337 host->mmc->ios.timing == MMC_TIMING_MMC_DDR52)
Srinivas Kandagatlae8740642014-06-02 10:09:30 +0100338 clk |= variant->clkreg_neg_edge_enable;
Ulf Hansson6dbb6ee2013-01-07 15:30:44 +0100339
Ulf Hansson7437cfa2012-01-18 09:17:27 +0100340 mmci_write_clkreg(host, clk);
Linus Walleija6a64642009-09-14 12:56:14 +0100341}
342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343static void
344mmci_request_end(struct mmci_host *host, struct mmc_request *mrq)
345{
346 writel(0, host->base + MMCICOMMAND);
347
Russell Kinge47c2222007-01-08 16:42:51 +0000348 BUG_ON(host->data);
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 host->mrq = NULL;
351 host->cmd = NULL;
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 mmc_request_done(host->mmc, mrq);
Ulf Hansson2cd976c2011-12-13 17:01:11 +0100354
355 pm_runtime_mark_last_busy(mmc_dev(host->mmc));
356 pm_runtime_put_autosuspend(mmc_dev(host->mmc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
Linus Walleij2686b4b2010-10-19 12:39:48 +0100359static void mmci_set_mask1(struct mmci_host *host, unsigned int mask)
360{
361 void __iomem *base = host->base;
362
363 if (host->singleirq) {
364 unsigned int mask0 = readl(base + MMCIMASK0);
365
366 mask0 &= ~MCI_IRQ1MASK;
367 mask0 |= mask;
368
369 writel(mask0, base + MMCIMASK0);
370 }
371
372 writel(mask, base + MMCIMASK1);
373}
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375static void mmci_stop_data(struct mmci_host *host)
376{
Ulf Hansson9cc639a2013-05-15 20:48:23 +0100377 mmci_write_datactrlreg(host, 0);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100378 mmci_set_mask1(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 host->data = NULL;
380}
381
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100382static void mmci_init_sg(struct mmci_host *host, struct mmc_data *data)
383{
384 unsigned int flags = SG_MITER_ATOMIC;
385
386 if (data->flags & MMC_DATA_READ)
387 flags |= SG_MITER_TO_SG;
388 else
389 flags |= SG_MITER_FROM_SG;
390
391 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
392}
393
Russell Kingc8ebae32011-01-11 19:35:53 +0000394/*
395 * All the DMA operation mode stuff goes inside this ifdef.
396 * This assumes that you have a generic DMA device interface,
397 * no custom DMA interfaces are supported.
398 */
399#ifdef CONFIG_DMA_ENGINE
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500400static void mmci_dma_setup(struct mmci_host *host)
Russell Kingc8ebae32011-01-11 19:35:53 +0000401{
Russell Kingc8ebae32011-01-11 19:35:53 +0000402 const char *rxname, *txname;
403 dma_cap_mask_t mask;
404
Lee Jones1fd83f02013-05-03 12:51:17 +0100405 host->dma_rx_channel = dma_request_slave_channel(mmc_dev(host->mmc), "rx");
406 host->dma_tx_channel = dma_request_slave_channel(mmc_dev(host->mmc), "tx");
Russell Kingc8ebae32011-01-11 19:35:53 +0000407
Per Forlin58c7ccb2011-07-01 18:55:24 +0200408 /* initialize pre request cookie */
409 host->next_data.cookie = 1;
410
Russell Kingc8ebae32011-01-11 19:35:53 +0000411 /* Try to acquire a generic DMA engine slave channel */
412 dma_cap_zero(mask);
413 dma_cap_set(DMA_SLAVE, mask);
414
415 /*
416 * If only an RX channel is specified, the driver will
417 * attempt to use it bidirectionally, however if it is
418 * is specified but cannot be located, DMA will be disabled.
419 */
Lee Jones1fd83f02013-05-03 12:51:17 +0100420 if (host->dma_rx_channel && !host->dma_tx_channel)
Russell Kingc8ebae32011-01-11 19:35:53 +0000421 host->dma_tx_channel = host->dma_rx_channel;
Russell Kingc8ebae32011-01-11 19:35:53 +0000422
423 if (host->dma_rx_channel)
424 rxname = dma_chan_name(host->dma_rx_channel);
425 else
426 rxname = "none";
427
428 if (host->dma_tx_channel)
429 txname = dma_chan_name(host->dma_tx_channel);
430 else
431 txname = "none";
432
433 dev_info(mmc_dev(host->mmc), "DMA channels RX %s, TX %s\n",
434 rxname, txname);
435
436 /*
437 * Limit the maximum segment size in any SG entry according to
438 * the parameters of the DMA engine device.
439 */
440 if (host->dma_tx_channel) {
441 struct device *dev = host->dma_tx_channel->device->dev;
442 unsigned int max_seg_size = dma_get_max_seg_size(dev);
443
444 if (max_seg_size < host->mmc->max_seg_size)
445 host->mmc->max_seg_size = max_seg_size;
446 }
447 if (host->dma_rx_channel) {
448 struct device *dev = host->dma_rx_channel->device->dev;
449 unsigned int max_seg_size = dma_get_max_seg_size(dev);
450
451 if (max_seg_size < host->mmc->max_seg_size)
452 host->mmc->max_seg_size = max_seg_size;
453 }
454}
455
456/*
Bill Pemberton6e0ee712012-11-19 13:26:03 -0500457 * This is used in or so inline it
Russell Kingc8ebae32011-01-11 19:35:53 +0000458 * so it can be discarded.
459 */
460static inline void mmci_dma_release(struct mmci_host *host)
461{
Russell Kingc8ebae32011-01-11 19:35:53 +0000462 if (host->dma_rx_channel)
463 dma_release_channel(host->dma_rx_channel);
Ulf Hansson8c3a05b2014-05-20 06:45:54 +0200464 if (host->dma_tx_channel)
Russell Kingc8ebae32011-01-11 19:35:53 +0000465 dma_release_channel(host->dma_tx_channel);
466 host->dma_rx_channel = host->dma_tx_channel = NULL;
467}
468
Ulf Hansson653a7612013-01-21 21:29:34 +0100469static void mmci_dma_data_error(struct mmci_host *host)
470{
471 dev_err(mmc_dev(host->mmc), "error during DMA transfer!\n");
472 dmaengine_terminate_all(host->dma_current);
473 host->dma_current = NULL;
474 host->dma_desc_current = NULL;
475 host->data->host_cookie = 0;
476}
477
Russell Kingc8ebae32011-01-11 19:35:53 +0000478static void mmci_dma_unmap(struct mmci_host *host, struct mmc_data *data)
479{
Ulf Hansson653a7612013-01-21 21:29:34 +0100480 struct dma_chan *chan;
Russell Kingc8ebae32011-01-11 19:35:53 +0000481 enum dma_data_direction dir;
Ulf Hansson653a7612013-01-21 21:29:34 +0100482
483 if (data->flags & MMC_DATA_READ) {
484 dir = DMA_FROM_DEVICE;
485 chan = host->dma_rx_channel;
486 } else {
487 dir = DMA_TO_DEVICE;
488 chan = host->dma_tx_channel;
489 }
490
491 dma_unmap_sg(chan->device->dev, data->sg, data->sg_len, dir);
492}
493
494static void mmci_dma_finalize(struct mmci_host *host, struct mmc_data *data)
495{
Russell Kingc8ebae32011-01-11 19:35:53 +0000496 u32 status;
497 int i;
498
499 /* Wait up to 1ms for the DMA to complete */
500 for (i = 0; ; i++) {
501 status = readl(host->base + MMCISTATUS);
502 if (!(status & MCI_RXDATAAVLBLMASK) || i >= 100)
503 break;
504 udelay(10);
505 }
506
507 /*
508 * Check to see whether we still have some data left in the FIFO -
509 * this catches DMA controllers which are unable to monitor the
510 * DMALBREQ and DMALSREQ signals while allowing us to DMA to non-
511 * contiguous buffers. On TX, we'll get a FIFO underrun error.
512 */
513 if (status & MCI_RXDATAAVLBLMASK) {
Ulf Hansson653a7612013-01-21 21:29:34 +0100514 mmci_dma_data_error(host);
Russell Kingc8ebae32011-01-11 19:35:53 +0000515 if (!data->error)
516 data->error = -EIO;
517 }
518
Per Forlin58c7ccb2011-07-01 18:55:24 +0200519 if (!data->host_cookie)
Ulf Hansson653a7612013-01-21 21:29:34 +0100520 mmci_dma_unmap(host, data);
Russell Kingc8ebae32011-01-11 19:35:53 +0000521
522 /*
523 * Use of DMA with scatter-gather is impossible.
524 * Give up with DMA and switch back to PIO mode.
525 */
526 if (status & MCI_RXDATAAVLBLMASK) {
527 dev_err(mmc_dev(host->mmc), "buggy DMA detected. Taking evasive action.\n");
528 mmci_dma_release(host);
529 }
Ulf Hansson653a7612013-01-21 21:29:34 +0100530
531 host->dma_current = NULL;
532 host->dma_desc_current = NULL;
Russell Kingc8ebae32011-01-11 19:35:53 +0000533}
534
Ulf Hansson653a7612013-01-21 21:29:34 +0100535/* prepares DMA channel and DMA descriptor, returns non-zero on failure */
536static int __mmci_dma_prep_data(struct mmci_host *host, struct mmc_data *data,
537 struct dma_chan **dma_chan,
538 struct dma_async_tx_descriptor **dma_desc)
Russell Kingc8ebae32011-01-11 19:35:53 +0000539{
540 struct variant_data *variant = host->variant;
541 struct dma_slave_config conf = {
542 .src_addr = host->phybase + MMCIFIFO,
543 .dst_addr = host->phybase + MMCIFIFO,
544 .src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
545 .dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
546 .src_maxburst = variant->fifohalfsize >> 2, /* # of words */
547 .dst_maxburst = variant->fifohalfsize >> 2, /* # of words */
Viresh Kumar258aea72012-02-01 16:12:19 +0530548 .device_fc = false,
Russell Kingc8ebae32011-01-11 19:35:53 +0000549 };
Russell Kingc8ebae32011-01-11 19:35:53 +0000550 struct dma_chan *chan;
551 struct dma_device *device;
552 struct dma_async_tx_descriptor *desc;
Vinod Koul05f57992011-10-14 10:45:11 +0530553 enum dma_data_direction buffer_dirn;
Russell Kingc8ebae32011-01-11 19:35:53 +0000554 int nr_sg;
555
Russell Kingc8ebae32011-01-11 19:35:53 +0000556 if (data->flags & MMC_DATA_READ) {
Vinod Koul05f57992011-10-14 10:45:11 +0530557 conf.direction = DMA_DEV_TO_MEM;
558 buffer_dirn = DMA_FROM_DEVICE;
Russell Kingc8ebae32011-01-11 19:35:53 +0000559 chan = host->dma_rx_channel;
560 } else {
Vinod Koul05f57992011-10-14 10:45:11 +0530561 conf.direction = DMA_MEM_TO_DEV;
562 buffer_dirn = DMA_TO_DEVICE;
Russell Kingc8ebae32011-01-11 19:35:53 +0000563 chan = host->dma_tx_channel;
564 }
565
566 /* If there's no DMA channel, fall back to PIO */
567 if (!chan)
568 return -EINVAL;
569
570 /* If less than or equal to the fifo size, don't bother with DMA */
Per Forlin58c7ccb2011-07-01 18:55:24 +0200571 if (data->blksz * data->blocks <= variant->fifosize)
Russell Kingc8ebae32011-01-11 19:35:53 +0000572 return -EINVAL;
573
574 device = chan->device;
Vinod Koul05f57992011-10-14 10:45:11 +0530575 nr_sg = dma_map_sg(device->dev, data->sg, data->sg_len, buffer_dirn);
Russell Kingc8ebae32011-01-11 19:35:53 +0000576 if (nr_sg == 0)
577 return -EINVAL;
578
579 dmaengine_slave_config(chan, &conf);
Alexandre Bounine16052822012-03-08 16:11:18 -0500580 desc = dmaengine_prep_slave_sg(chan, data->sg, nr_sg,
Russell Kingc8ebae32011-01-11 19:35:53 +0000581 conf.direction, DMA_CTRL_ACK);
582 if (!desc)
583 goto unmap_exit;
584
Ulf Hansson653a7612013-01-21 21:29:34 +0100585 *dma_chan = chan;
586 *dma_desc = desc;
Russell Kingc8ebae32011-01-11 19:35:53 +0000587
Per Forlin58c7ccb2011-07-01 18:55:24 +0200588 return 0;
589
590 unmap_exit:
Vinod Koul05f57992011-10-14 10:45:11 +0530591 dma_unmap_sg(device->dev, data->sg, data->sg_len, buffer_dirn);
Per Forlin58c7ccb2011-07-01 18:55:24 +0200592 return -ENOMEM;
593}
594
Ulf Hansson653a7612013-01-21 21:29:34 +0100595static inline int mmci_dma_prep_data(struct mmci_host *host,
596 struct mmc_data *data)
597{
598 /* Check if next job is already prepared. */
599 if (host->dma_current && host->dma_desc_current)
600 return 0;
601
602 /* No job were prepared thus do it now. */
603 return __mmci_dma_prep_data(host, data, &host->dma_current,
604 &host->dma_desc_current);
605}
606
607static inline int mmci_dma_prep_next(struct mmci_host *host,
608 struct mmc_data *data)
609{
610 struct mmci_host_next *nd = &host->next_data;
611 return __mmci_dma_prep_data(host, data, &nd->dma_chan, &nd->dma_desc);
612}
613
Per Forlin58c7ccb2011-07-01 18:55:24 +0200614static int mmci_dma_start_data(struct mmci_host *host, unsigned int datactrl)
615{
616 int ret;
617 struct mmc_data *data = host->data;
618
Ulf Hansson653a7612013-01-21 21:29:34 +0100619 ret = mmci_dma_prep_data(host, host->data);
Per Forlin58c7ccb2011-07-01 18:55:24 +0200620 if (ret)
621 return ret;
622
623 /* Okay, go for it. */
Russell Kingc8ebae32011-01-11 19:35:53 +0000624 dev_vdbg(mmc_dev(host->mmc),
625 "Submit MMCI DMA job, sglen %d blksz %04x blks %04x flags %08x\n",
626 data->sg_len, data->blksz, data->blocks, data->flags);
Per Forlin58c7ccb2011-07-01 18:55:24 +0200627 dmaengine_submit(host->dma_desc_current);
628 dma_async_issue_pending(host->dma_current);
Russell Kingc8ebae32011-01-11 19:35:53 +0000629
630 datactrl |= MCI_DPSM_DMAENABLE;
631
632 /* Trigger the DMA transfer */
Ulf Hansson9cc639a2013-05-15 20:48:23 +0100633 mmci_write_datactrlreg(host, datactrl);
Russell Kingc8ebae32011-01-11 19:35:53 +0000634
635 /*
636 * Let the MMCI say when the data is ended and it's time
637 * to fire next DMA request. When that happens, MMCI will
638 * call mmci_data_end()
639 */
640 writel(readl(host->base + MMCIMASK0) | MCI_DATAENDMASK,
641 host->base + MMCIMASK0);
642 return 0;
Russell Kingc8ebae32011-01-11 19:35:53 +0000643}
Per Forlin58c7ccb2011-07-01 18:55:24 +0200644
645static void mmci_get_next_data(struct mmci_host *host, struct mmc_data *data)
646{
647 struct mmci_host_next *next = &host->next_data;
648
Ulf Hansson653a7612013-01-21 21:29:34 +0100649 WARN_ON(data->host_cookie && data->host_cookie != next->cookie);
650 WARN_ON(!data->host_cookie && (next->dma_desc || next->dma_chan));
Per Forlin58c7ccb2011-07-01 18:55:24 +0200651
652 host->dma_desc_current = next->dma_desc;
653 host->dma_current = next->dma_chan;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200654 next->dma_desc = NULL;
655 next->dma_chan = NULL;
656}
657
658static void mmci_pre_request(struct mmc_host *mmc, struct mmc_request *mrq,
659 bool is_first_req)
660{
661 struct mmci_host *host = mmc_priv(mmc);
662 struct mmc_data *data = mrq->data;
663 struct mmci_host_next *nd = &host->next_data;
664
665 if (!data)
666 return;
667
Ulf Hansson653a7612013-01-21 21:29:34 +0100668 BUG_ON(data->host_cookie);
Per Forlin58c7ccb2011-07-01 18:55:24 +0200669
Ulf Hansson653a7612013-01-21 21:29:34 +0100670 if (mmci_validate_data(host, data))
671 return;
672
673 if (!mmci_dma_prep_next(host, data))
674 data->host_cookie = ++nd->cookie < 0 ? 1 : nd->cookie;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200675}
676
677static void mmci_post_request(struct mmc_host *mmc, struct mmc_request *mrq,
678 int err)
679{
680 struct mmci_host *host = mmc_priv(mmc);
681 struct mmc_data *data = mrq->data;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200682
Ulf Hansson653a7612013-01-21 21:29:34 +0100683 if (!data || !data->host_cookie)
Per Forlin58c7ccb2011-07-01 18:55:24 +0200684 return;
685
Ulf Hansson653a7612013-01-21 21:29:34 +0100686 mmci_dma_unmap(host, data);
Per Forlin58c7ccb2011-07-01 18:55:24 +0200687
Ulf Hansson653a7612013-01-21 21:29:34 +0100688 if (err) {
689 struct mmci_host_next *next = &host->next_data;
690 struct dma_chan *chan;
691 if (data->flags & MMC_DATA_READ)
692 chan = host->dma_rx_channel;
693 else
694 chan = host->dma_tx_channel;
695 dmaengine_terminate_all(chan);
Per Forlin58c7ccb2011-07-01 18:55:24 +0200696
Ulf Hansson653a7612013-01-21 21:29:34 +0100697 next->dma_desc = NULL;
698 next->dma_chan = NULL;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200699 }
700}
701
Russell Kingc8ebae32011-01-11 19:35:53 +0000702#else
703/* Blank functions if the DMA engine is not available */
Per Forlin58c7ccb2011-07-01 18:55:24 +0200704static void mmci_get_next_data(struct mmci_host *host, struct mmc_data *data)
705{
706}
Russell Kingc8ebae32011-01-11 19:35:53 +0000707static inline void mmci_dma_setup(struct mmci_host *host)
708{
709}
710
711static inline void mmci_dma_release(struct mmci_host *host)
712{
713}
714
715static inline void mmci_dma_unmap(struct mmci_host *host, struct mmc_data *data)
716{
717}
718
Ulf Hansson653a7612013-01-21 21:29:34 +0100719static inline void mmci_dma_finalize(struct mmci_host *host,
720 struct mmc_data *data)
721{
722}
723
Russell Kingc8ebae32011-01-11 19:35:53 +0000724static inline void mmci_dma_data_error(struct mmci_host *host)
725{
726}
727
728static inline int mmci_dma_start_data(struct mmci_host *host, unsigned int datactrl)
729{
730 return -ENOSYS;
731}
Per Forlin58c7ccb2011-07-01 18:55:24 +0200732
733#define mmci_pre_request NULL
734#define mmci_post_request NULL
735
Russell Kingc8ebae32011-01-11 19:35:53 +0000736#endif
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738static void mmci_start_data(struct mmci_host *host, struct mmc_data *data)
739{
Rabin Vincent8301bb62010-08-09 12:57:30 +0100740 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 unsigned int datactrl, timeout, irqmask;
Russell King7b09cda2005-07-01 12:02:59 +0100742 unsigned long long clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 void __iomem *base;
Russell King3bc87f22006-08-27 13:51:28 +0100744 int blksz_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Linus Walleij64de0282010-02-19 01:09:10 +0100746 dev_dbg(mmc_dev(host->mmc), "blksz %04x blks %04x flags %08x\n",
747 data->blksz, data->blocks, data->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 host->data = data;
Rabin Vincent528320d2010-07-21 12:49:49 +0100750 host->size = data->blksz * data->blocks;
Russell King51d43752011-01-27 10:56:52 +0000751 data->bytes_xfered = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Russell King7b09cda2005-07-01 12:02:59 +0100753 clks = (unsigned long long)data->timeout_ns * host->cclk;
Srinivas Kandagatlac4a35762014-06-02 10:08:39 +0100754 do_div(clks, NSEC_PER_SEC);
Russell King7b09cda2005-07-01 12:02:59 +0100755
756 timeout = data->timeout_clks + (unsigned int)clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 base = host->base;
759 writel(timeout, base + MMCIDATATIMER);
760 writel(host->size, base + MMCIDATALENGTH);
761
Russell King3bc87f22006-08-27 13:51:28 +0100762 blksz_bits = ffs(data->blksz) - 1;
763 BUG_ON(1 << blksz_bits != data->blksz);
764
Philippe Langlais1784b152011-03-25 08:51:52 +0100765 if (variant->blksz_datactrl16)
766 datactrl = MCI_DPSM_ENABLE | (data->blksz << 16);
Srinivas Kandagatlaff783232014-06-02 10:09:06 +0100767 else if (variant->blksz_datactrl4)
768 datactrl = MCI_DPSM_ENABLE | (data->blksz << 4);
Philippe Langlais1784b152011-03-25 08:51:52 +0100769 else
770 datactrl = MCI_DPSM_ENABLE | blksz_bits << 4;
Russell Kingc8ebae32011-01-11 19:35:53 +0000771
772 if (data->flags & MMC_DATA_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 datactrl |= MCI_DPSM_DIRECTION;
Russell Kingc8ebae32011-01-11 19:35:53 +0000774
Ulf Hansson7258db72011-12-13 17:05:28 +0100775 /* The ST Micro variants has a special bit to enable SDIO */
776 if (variant->sdio && host->mmc->card)
Ulf Hansson06c1a122012-10-12 14:01:50 +0100777 if (mmc_card_sdio(host->mmc->card)) {
778 /*
779 * The ST Micro variants has a special bit
780 * to enable SDIO.
781 */
782 u32 clk;
783
Ulf Hansson7258db72011-12-13 17:05:28 +0100784 datactrl |= MCI_ST_DPSM_SDIOEN;
785
Ulf Hansson06c1a122012-10-12 14:01:50 +0100786 /*
Ulf Hansson70ac0932012-10-12 14:07:36 +0100787 * The ST Micro variant for SDIO small write transfers
788 * needs to have clock H/W flow control disabled,
789 * otherwise the transfer will not start. The threshold
790 * depends on the rate of MCLK.
Ulf Hansson06c1a122012-10-12 14:01:50 +0100791 */
Ulf Hansson70ac0932012-10-12 14:07:36 +0100792 if (data->flags & MMC_DATA_WRITE &&
793 (host->size < 8 ||
794 (host->size <= 8 && host->mclk > 50000000)))
Ulf Hansson06c1a122012-10-12 14:01:50 +0100795 clk = host->clk_reg & ~variant->clkreg_enable;
796 else
797 clk = host->clk_reg | variant->clkreg_enable;
798
799 mmci_write_clkreg(host, clk);
800 }
801
Seungwon Jeon6dad6c92014-03-14 21:12:13 +0900802 if (host->mmc->ios.timing == MMC_TIMING_UHS_DDR50 ||
803 host->mmc->ios.timing == MMC_TIMING_MMC_DDR52)
Srinivas Kandagatlae17dca22014-06-02 10:09:15 +0100804 datactrl |= variant->datactrl_mask_ddrmode;
Ulf Hansson6dbb6ee2013-01-07 15:30:44 +0100805
Russell Kingc8ebae32011-01-11 19:35:53 +0000806 /*
807 * Attempt to use DMA operation mode, if this
808 * should fail, fall back to PIO mode
809 */
810 if (!mmci_dma_start_data(host, datactrl))
811 return;
812
813 /* IRQ mode, map the SG list for CPU reading/writing */
814 mmci_init_sg(host, data);
815
816 if (data->flags & MMC_DATA_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 irqmask = MCI_RXFIFOHALFFULLMASK;
Russell King0425a142006-02-16 16:48:31 +0000818
819 /*
Russell Kingc4d877c2011-01-27 09:50:13 +0000820 * If we have less than the fifo 'half-full' threshold to
821 * transfer, trigger a PIO interrupt as soon as any data
822 * is available.
Russell King0425a142006-02-16 16:48:31 +0000823 */
Russell Kingc4d877c2011-01-27 09:50:13 +0000824 if (host->size < variant->fifohalfsize)
Russell King0425a142006-02-16 16:48:31 +0000825 irqmask |= MCI_RXDATAAVLBLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 } else {
827 /*
828 * We don't actually need to include "FIFO empty" here
829 * since its implicit in "FIFO half empty".
830 */
831 irqmask = MCI_TXFIFOHALFEMPTYMASK;
832 }
833
Ulf Hansson9cc639a2013-05-15 20:48:23 +0100834 mmci_write_datactrlreg(host, datactrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 writel(readl(base + MMCIMASK0) & ~MCI_DATAENDMASK, base + MMCIMASK0);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100836 mmci_set_mask1(host, irqmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
838
839static void
840mmci_start_command(struct mmci_host *host, struct mmc_command *cmd, u32 c)
841{
842 void __iomem *base = host->base;
843
Linus Walleij64de0282010-02-19 01:09:10 +0100844 dev_dbg(mmc_dev(host->mmc), "op %02x arg %08x flags %08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 cmd->opcode, cmd->arg, cmd->flags);
846
847 if (readl(base + MMCICOMMAND) & MCI_CPSM_ENABLE) {
848 writel(0, base + MMCICOMMAND);
Srinivas Kandagatla6adb2a82014-06-02 10:08:57 +0100849 mmci_reg_delay(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 }
851
852 c |= cmd->opcode | MCI_CPSM_ENABLE;
Russell Kinge9225172006-02-02 12:23:12 +0000853 if (cmd->flags & MMC_RSP_PRESENT) {
854 if (cmd->flags & MMC_RSP_136)
855 c |= MCI_CPSM_LONGRSP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 c |= MCI_CPSM_RESPONSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 }
858 if (/*interrupt*/0)
859 c |= MCI_CPSM_INTERRUPT;
860
Srinivas Kandagatlaae7b0062014-06-02 10:09:39 +0100861 if (mmc_cmd_type(cmd) == MMC_CMD_ADTC)
862 c |= host->variant->data_cmd_enable;
863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 host->cmd = cmd;
865
866 writel(cmd->arg, base + MMCIARGUMENT);
867 writel(c, base + MMCICOMMAND);
868}
869
870static void
871mmci_data_irq(struct mmci_host *host, struct mmc_data *data,
872 unsigned int status)
873{
Linus Walleijf20f8f212010-10-19 13:41:24 +0100874 /* First check for errors */
Ulf Hanssonb63038d2011-12-13 16:51:04 +0100875 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_STARTBITERR|
876 MCI_TXUNDERRUN|MCI_RXOVERRUN)) {
Linus Walleij8cb28152011-01-24 15:22:13 +0100877 u32 remain, success;
Linus Walleijf20f8f212010-10-19 13:41:24 +0100878
Russell Kingc8ebae32011-01-11 19:35:53 +0000879 /* Terminate the DMA transfer */
Ulf Hansson653a7612013-01-21 21:29:34 +0100880 if (dma_inprogress(host)) {
Russell Kingc8ebae32011-01-11 19:35:53 +0000881 mmci_dma_data_error(host);
Ulf Hansson653a7612013-01-21 21:29:34 +0100882 mmci_dma_unmap(host, data);
883 }
Russell Kingc8ebae32011-01-11 19:35:53 +0000884
Russell Kingc8afc9d2011-02-04 09:19:46 +0000885 /*
886 * Calculate how far we are into the transfer. Note that
887 * the data counter gives the number of bytes transferred
888 * on the MMC bus, not on the host side. On reads, this
889 * can be as much as a FIFO-worth of data ahead. This
890 * matters for FIFO overruns only.
891 */
Linus Walleijf5a106d2011-01-27 17:44:34 +0100892 remain = readl(host->base + MMCIDATACNT);
Linus Walleij8cb28152011-01-24 15:22:13 +0100893 success = data->blksz * data->blocks - remain;
894
Russell Kingc8afc9d2011-02-04 09:19:46 +0000895 dev_dbg(mmc_dev(host->mmc), "MCI ERROR IRQ, status 0x%08x at 0x%08x\n",
896 status, success);
Linus Walleij8cb28152011-01-24 15:22:13 +0100897 if (status & MCI_DATACRCFAIL) {
898 /* Last block was not successful */
Russell Kingc8afc9d2011-02-04 09:19:46 +0000899 success -= 1;
Pierre Ossman17b04292007-07-22 22:18:46 +0200900 data->error = -EILSEQ;
Linus Walleij8cb28152011-01-24 15:22:13 +0100901 } else if (status & MCI_DATATIMEOUT) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200902 data->error = -ETIMEDOUT;
Linus Walleij757df742011-06-30 15:10:21 +0100903 } else if (status & MCI_STARTBITERR) {
904 data->error = -ECOMM;
Russell Kingc8afc9d2011-02-04 09:19:46 +0000905 } else if (status & MCI_TXUNDERRUN) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200906 data->error = -EIO;
Russell Kingc8afc9d2011-02-04 09:19:46 +0000907 } else if (status & MCI_RXOVERRUN) {
908 if (success > host->variant->fifosize)
909 success -= host->variant->fifosize;
910 else
911 success = 0;
Linus Walleij8cb28152011-01-24 15:22:13 +0100912 data->error = -EIO;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100913 }
Russell King51d43752011-01-27 10:56:52 +0000914 data->bytes_xfered = round_down(success, data->blksz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 }
Linus Walleijf20f8f212010-10-19 13:41:24 +0100916
Linus Walleij8cb28152011-01-24 15:22:13 +0100917 if (status & MCI_DATABLOCKEND)
918 dev_err(mmc_dev(host->mmc), "stray MCI_DATABLOCKEND interrupt\n");
Linus Walleijf20f8f212010-10-19 13:41:24 +0100919
Russell Kingccff9b52011-01-30 21:03:50 +0000920 if (status & MCI_DATAEND || data->error) {
Russell Kingc8ebae32011-01-11 19:35:53 +0000921 if (dma_inprogress(host))
Ulf Hansson653a7612013-01-21 21:29:34 +0100922 mmci_dma_finalize(host, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 mmci_stop_data(host);
924
Linus Walleij8cb28152011-01-24 15:22:13 +0100925 if (!data->error)
926 /* The error clause is handled above, success! */
Russell King51d43752011-01-27 10:56:52 +0000927 data->bytes_xfered = data->blksz * data->blocks;
Linus Walleijf20f8f212010-10-19 13:41:24 +0100928
Ulf Hansson024629c2013-05-13 15:40:56 +0100929 if (!data->stop || host->mrq->sbc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 mmci_request_end(host, data->mrq);
931 } else {
932 mmci_start_command(host, data->stop, 0);
933 }
934 }
935}
936
937static void
938mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
939 unsigned int status)
940{
941 void __iomem *base = host->base;
Ulf Hansson024629c2013-05-13 15:40:56 +0100942 bool sbc = (cmd == host->mrq->sbc);
Ulf Hansson8d94b542014-01-13 16:49:31 +0100943 bool busy_resp = host->variant->busy_detect &&
944 (cmd->flags & MMC_RSP_BUSY);
945
946 /* Check if we need to wait for busy completion. */
947 if (host->busy_status && (status & MCI_ST_CARDBUSY))
948 return;
949
950 /* Enable busy completion if needed and supported. */
951 if (!host->busy_status && busy_resp &&
952 !(status & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT)) &&
953 (readl(base + MMCISTATUS) & MCI_ST_CARDBUSY)) {
954 writel(readl(base + MMCIMASK0) | MCI_ST_BUSYEND,
955 base + MMCIMASK0);
956 host->busy_status = status & (MCI_CMDSENT|MCI_CMDRESPEND);
957 return;
958 }
959
960 /* At busy completion, mask the IRQ and complete the request. */
961 if (host->busy_status) {
962 writel(readl(base + MMCIMASK0) & ~MCI_ST_BUSYEND,
963 base + MMCIMASK0);
964 host->busy_status = 0;
965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 host->cmd = NULL;
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (status & MCI_CMDTIMEOUT) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200970 cmd->error = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200972 cmd->error = -EILSEQ;
Russell King - ARM Linux9047b432011-01-11 16:35:56 +0000973 } else {
974 cmd->resp[0] = readl(base + MMCIRESPONSE0);
975 cmd->resp[1] = readl(base + MMCIRESPONSE1);
976 cmd->resp[2] = readl(base + MMCIRESPONSE2);
977 cmd->resp[3] = readl(base + MMCIRESPONSE3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
979
Ulf Hansson024629c2013-05-13 15:40:56 +0100980 if ((!sbc && !cmd->data) || cmd->error) {
Ulf Hansson3b6e3c72011-12-13 16:58:43 +0100981 if (host->data) {
982 /* Terminate the DMA transfer */
Ulf Hansson653a7612013-01-21 21:29:34 +0100983 if (dma_inprogress(host)) {
Ulf Hansson3b6e3c72011-12-13 16:58:43 +0100984 mmci_dma_data_error(host);
Ulf Hansson653a7612013-01-21 21:29:34 +0100985 mmci_dma_unmap(host, host->data);
986 }
Russell Kinge47c2222007-01-08 16:42:51 +0000987 mmci_stop_data(host);
Ulf Hansson3b6e3c72011-12-13 16:58:43 +0100988 }
Ulf Hansson024629c2013-05-13 15:40:56 +0100989 mmci_request_end(host, host->mrq);
990 } else if (sbc) {
991 mmci_start_command(host, host->mrq->cmd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 } else if (!(cmd->data->flags & MMC_DATA_READ)) {
993 mmci_start_data(host, cmd->data);
994 }
995}
996
Srinivas Kandagatla9c34b732014-06-02 10:10:04 +0100997static int mmci_get_rx_fifocnt(struct mmci_host *host, u32 status, int remain)
998{
999 return remain - (readl(host->base + MMCIFIFOCNT) << 2);
1000}
1001
1002static int mmci_qcom_get_rx_fifocnt(struct mmci_host *host, u32 status, int r)
1003{
1004 /*
1005 * on qcom SDCC4 only 8 words are used in each burst so only 8 addresses
1006 * from the fifo range should be used
1007 */
1008 if (status & MCI_RXFIFOHALFFULL)
1009 return host->variant->fifohalfsize;
1010 else if (status & MCI_RXDATAAVLBL)
1011 return 4;
1012
1013 return 0;
1014}
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016static int mmci_pio_read(struct mmci_host *host, char *buffer, unsigned int remain)
1017{
1018 void __iomem *base = host->base;
1019 char *ptr = buffer;
Srinivas Kandagatla9c34b732014-06-02 10:10:04 +01001020 u32 status = readl(host->base + MMCISTATUS);
Linus Walleij26eed9a2008-04-26 23:39:44 +01001021 int host_remain = host->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023 do {
Srinivas Kandagatla9c34b732014-06-02 10:10:04 +01001024 int count = host->get_rx_fifocnt(host, status, host_remain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 if (count > remain)
1027 count = remain;
1028
1029 if (count <= 0)
1030 break;
1031
Ulf Hansson393e5e22011-12-13 17:08:04 +01001032 /*
1033 * SDIO especially may want to send something that is
1034 * not divisible by 4 (as opposed to card sectors
1035 * etc). Therefore make sure to always read the last bytes
1036 * while only doing full 32-bit reads towards the FIFO.
1037 */
1038 if (unlikely(count & 0x3)) {
1039 if (count < 4) {
1040 unsigned char buf[4];
Davide Ciminaghi4b85da02012-12-10 14:47:21 +01001041 ioread32_rep(base + MMCIFIFO, buf, 1);
Ulf Hansson393e5e22011-12-13 17:08:04 +01001042 memcpy(ptr, buf, count);
1043 } else {
Davide Ciminaghi4b85da02012-12-10 14:47:21 +01001044 ioread32_rep(base + MMCIFIFO, ptr, count >> 2);
Ulf Hansson393e5e22011-12-13 17:08:04 +01001045 count &= ~0x3;
1046 }
1047 } else {
Davide Ciminaghi4b85da02012-12-10 14:47:21 +01001048 ioread32_rep(base + MMCIFIFO, ptr, count >> 2);
Ulf Hansson393e5e22011-12-13 17:08:04 +01001049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 ptr += count;
1052 remain -= count;
Linus Walleij26eed9a2008-04-26 23:39:44 +01001053 host_remain -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 if (remain == 0)
1056 break;
1057
1058 status = readl(base + MMCISTATUS);
1059 } while (status & MCI_RXDATAAVLBL);
1060
1061 return ptr - buffer;
1062}
1063
1064static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status)
1065{
Rabin Vincent8301bb62010-08-09 12:57:30 +01001066 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 void __iomem *base = host->base;
1068 char *ptr = buffer;
1069
1070 do {
1071 unsigned int count, maxcnt;
1072
Rabin Vincent8301bb62010-08-09 12:57:30 +01001073 maxcnt = status & MCI_TXFIFOEMPTY ?
1074 variant->fifosize : variant->fifohalfsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 count = min(remain, maxcnt);
1076
Linus Walleij34177802010-10-19 12:43:58 +01001077 /*
Linus Walleij34177802010-10-19 12:43:58 +01001078 * SDIO especially may want to send something that is
1079 * not divisible by 4 (as opposed to card sectors
1080 * etc), and the FIFO only accept full 32-bit writes.
1081 * So compensate by adding +3 on the count, a single
1082 * byte become a 32bit write, 7 bytes will be two
1083 * 32bit writes etc.
1084 */
Davide Ciminaghi4b85da02012-12-10 14:47:21 +01001085 iowrite32_rep(base + MMCIFIFO, ptr, (count + 3) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087 ptr += count;
1088 remain -= count;
1089
1090 if (remain == 0)
1091 break;
1092
1093 status = readl(base + MMCISTATUS);
1094 } while (status & MCI_TXFIFOHALFEMPTY);
1095
1096 return ptr - buffer;
1097}
1098
1099/*
1100 * PIO data transfer IRQ handler.
1101 */
David Howells7d12e782006-10-05 14:55:46 +01001102static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
1104 struct mmci_host *host = dev_id;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001105 struct sg_mapping_iter *sg_miter = &host->sg_miter;
Rabin Vincent8301bb62010-08-09 12:57:30 +01001106 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 void __iomem *base = host->base;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001108 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 u32 status;
1110
1111 status = readl(base + MMCISTATUS);
1112
Linus Walleij64de0282010-02-19 01:09:10 +01001113 dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001115 local_irq_save(flags);
1116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 unsigned int remain, len;
1119 char *buffer;
1120
1121 /*
1122 * For write, we only need to test the half-empty flag
1123 * here - if the FIFO is completely empty, then by
1124 * definition it is more than half empty.
1125 *
1126 * For read, check for data available.
1127 */
1128 if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL)))
1129 break;
1130
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001131 if (!sg_miter_next(sg_miter))
1132 break;
1133
1134 buffer = sg_miter->addr;
1135 remain = sg_miter->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
1137 len = 0;
1138 if (status & MCI_RXACTIVE)
1139 len = mmci_pio_read(host, buffer, remain);
1140 if (status & MCI_TXACTIVE)
1141 len = mmci_pio_write(host, buffer, remain, status);
1142
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001143 sg_miter->consumed = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 host->size -= len;
1146 remain -= len;
1147
1148 if (remain)
1149 break;
1150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 status = readl(base + MMCISTATUS);
1152 } while (1);
1153
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001154 sg_miter_stop(sg_miter);
1155
1156 local_irq_restore(flags);
1157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 /*
Russell Kingc4d877c2011-01-27 09:50:13 +00001159 * If we have less than the fifo 'half-full' threshold to transfer,
1160 * trigger a PIO interrupt as soon as any data is available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 */
Russell Kingc4d877c2011-01-27 09:50:13 +00001162 if (status & MCI_RXACTIVE && host->size < variant->fifohalfsize)
Linus Walleij2686b4b2010-10-19 12:39:48 +01001163 mmci_set_mask1(host, MCI_RXDATAAVLBLMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165 /*
1166 * If we run out of data, disable the data IRQs; this
1167 * prevents a race where the FIFO becomes empty before
1168 * the chip itself has disabled the data path, and
1169 * stops us racing with our data end IRQ.
1170 */
1171 if (host->size == 0) {
Linus Walleij2686b4b2010-10-19 12:39:48 +01001172 mmci_set_mask1(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0);
1174 }
1175
1176 return IRQ_HANDLED;
1177}
1178
1179/*
1180 * Handle completion of command and data transfers.
1181 */
David Howells7d12e782006-10-05 14:55:46 +01001182static irqreturn_t mmci_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
1184 struct mmci_host *host = dev_id;
1185 u32 status;
1186 int ret = 0;
1187
1188 spin_lock(&host->lock);
1189
1190 do {
1191 struct mmc_command *cmd;
1192 struct mmc_data *data;
1193
1194 status = readl(host->base + MMCISTATUS);
Linus Walleij2686b4b2010-10-19 12:39:48 +01001195
1196 if (host->singleirq) {
1197 if (status & readl(host->base + MMCIMASK1))
1198 mmci_pio_irq(irq, dev_id);
1199
1200 status &= ~MCI_IRQ1MASK;
1201 }
1202
Ulf Hansson8d94b542014-01-13 16:49:31 +01001203 /*
1204 * We intentionally clear the MCI_ST_CARDBUSY IRQ here (if it's
1205 * enabled) since the HW seems to be triggering the IRQ on both
1206 * edges while monitoring DAT0 for busy completion.
1207 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 status &= readl(host->base + MMCIMASK0);
1209 writel(status, host->base + MMCICLEAR);
1210
Linus Walleij64de0282010-02-19 01:09:10 +01001211 dev_dbg(mmc_dev(host->mmc), "irq0 (data+cmd) %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Ulf Hanssone7f3d222014-01-10 14:51:42 +01001213 cmd = host->cmd;
Ulf Hansson8d94b542014-01-13 16:49:31 +01001214 if ((status|host->busy_status) & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT|
1215 MCI_CMDSENT|MCI_CMDRESPEND) && cmd)
Ulf Hanssone7f3d222014-01-10 14:51:42 +01001216 mmci_cmd_irq(host, cmd, status);
1217
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 data = host->data;
Ulf Hanssonb63038d2011-12-13 16:51:04 +01001219 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_STARTBITERR|
1220 MCI_TXUNDERRUN|MCI_RXOVERRUN|MCI_DATAEND|
1221 MCI_DATABLOCKEND) && data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 mmci_data_irq(host, data, status);
1223
Ulf Hansson8d94b542014-01-13 16:49:31 +01001224 /* Don't poll for busy completion in irq context. */
1225 if (host->busy_status)
1226 status &= ~MCI_ST_CARDBUSY;
1227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 ret = 1;
1229 } while (status);
1230
1231 spin_unlock(&host->lock);
1232
1233 return IRQ_RETVAL(ret);
1234}
1235
1236static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1237{
1238 struct mmci_host *host = mmc_priv(mmc);
Linus Walleij9e943022008-10-24 21:17:50 +01001239 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 WARN_ON(host->mrq != NULL);
1242
Ulf Hansson653a7612013-01-21 21:29:34 +01001243 mrq->cmd->error = mmci_validate_data(host, mrq->data);
1244 if (mrq->cmd->error) {
Pierre Ossman255d01a2007-07-24 20:38:53 +02001245 mmc_request_done(mmc, mrq);
1246 return;
1247 }
1248
Russell King1c3be362011-08-14 09:17:05 +01001249 pm_runtime_get_sync(mmc_dev(mmc));
1250
Linus Walleij9e943022008-10-24 21:17:50 +01001251 spin_lock_irqsave(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253 host->mrq = mrq;
1254
Per Forlin58c7ccb2011-07-01 18:55:24 +02001255 if (mrq->data)
1256 mmci_get_next_data(host, mrq->data);
1257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 if (mrq->data && mrq->data->flags & MMC_DATA_READ)
1259 mmci_start_data(host, mrq->data);
1260
Ulf Hansson024629c2013-05-13 15:40:56 +01001261 if (mrq->sbc)
1262 mmci_start_command(host, mrq->sbc, 0);
1263 else
1264 mmci_start_command(host, mrq->cmd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Linus Walleij9e943022008-10-24 21:17:50 +01001266 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267}
1268
1269static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1270{
1271 struct mmci_host *host = mmc_priv(mmc);
Ulf Hansson7d72a1d2011-12-13 16:54:55 +01001272 struct variant_data *variant = host->variant;
Linus Walleija6a64642009-09-14 12:56:14 +01001273 u32 pwr = 0;
1274 unsigned long flags;
Lee Jonesdb90f912013-05-03 12:52:12 +01001275 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Ulf Hansson2cd976c2011-12-13 17:01:11 +01001277 pm_runtime_get_sync(mmc_dev(mmc));
1278
Ulf Hanssonbc521812011-12-13 16:57:55 +01001279 if (host->plat->ios_handler &&
1280 host->plat->ios_handler(mmc_dev(mmc), ios))
1281 dev_err(mmc_dev(mmc), "platform ios_handler failed\n");
1282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 switch (ios->power_mode) {
1284 case MMC_POWER_OFF:
Ulf Hansson599c1d52013-01-07 16:22:50 +01001285 if (!IS_ERR(mmc->supply.vmmc))
1286 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Lee Jones237fb5e2013-01-31 11:27:52 +00001287
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001288 if (!IS_ERR(mmc->supply.vqmmc) && host->vqmmc_enabled) {
Lee Jones237fb5e2013-01-31 11:27:52 +00001289 regulator_disable(mmc->supply.vqmmc);
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001290 host->vqmmc_enabled = false;
1291 }
Lee Jones237fb5e2013-01-31 11:27:52 +00001292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 break;
1294 case MMC_POWER_UP:
Ulf Hansson599c1d52013-01-07 16:22:50 +01001295 if (!IS_ERR(mmc->supply.vmmc))
1296 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd);
1297
Ulf Hansson7d72a1d2011-12-13 16:54:55 +01001298 /*
1299 * The ST Micro variant doesn't have the PL180s MCI_PWR_UP
1300 * and instead uses MCI_PWR_ON so apply whatever value is
1301 * configured in the variant data.
1302 */
1303 pwr |= variant->pwrreg_powerup;
1304
1305 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 case MMC_POWER_ON:
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001307 if (!IS_ERR(mmc->supply.vqmmc) && !host->vqmmc_enabled) {
Lee Jonesdb90f912013-05-03 12:52:12 +01001308 ret = regulator_enable(mmc->supply.vqmmc);
1309 if (ret < 0)
1310 dev_err(mmc_dev(mmc),
1311 "failed to enable vqmmc regulator\n");
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001312 else
1313 host->vqmmc_enabled = true;
Lee Jonesdb90f912013-05-03 12:52:12 +01001314 }
Lee Jones237fb5e2013-01-31 11:27:52 +00001315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 pwr |= MCI_PWR_ON;
1317 break;
1318 }
1319
Ulf Hansson4d1a3a02011-12-13 16:57:07 +01001320 if (variant->signal_direction && ios->power_mode != MMC_POWER_OFF) {
1321 /*
1322 * The ST Micro variant has some additional bits
1323 * indicating signal direction for the signals in
1324 * the SD/MMC bus and feedback-clock usage.
1325 */
Ulf Hansson4593df22014-03-21 10:13:05 +01001326 pwr |= host->pwr_reg_add;
Ulf Hansson4d1a3a02011-12-13 16:57:07 +01001327
1328 if (ios->bus_width == MMC_BUS_WIDTH_4)
1329 pwr &= ~MCI_ST_DATA74DIREN;
1330 else if (ios->bus_width == MMC_BUS_WIDTH_1)
1331 pwr &= (~MCI_ST_DATA74DIREN &
1332 ~MCI_ST_DATA31DIREN &
1333 ~MCI_ST_DATA2DIREN);
1334 }
1335
Linus Walleijcc30d602009-01-04 15:18:54 +01001336 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN) {
Linus Walleijf17a1f02009-08-04 01:01:02 +01001337 if (host->hw_designer != AMBA_VENDOR_ST)
Linus Walleijcc30d602009-01-04 15:18:54 +01001338 pwr |= MCI_ROD;
1339 else {
1340 /*
1341 * The ST Micro variant use the ROD bit for something
1342 * else and only has OD (Open Drain).
1343 */
1344 pwr |= MCI_OD;
1345 }
1346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Ulf Hanssonf4670da2013-01-09 17:19:54 +01001348 /*
1349 * If clock = 0 and the variant requires the MMCIPOWER to be used for
1350 * gating the clock, the MCI_PWR_ON bit is cleared.
1351 */
1352 if (!ios->clock && variant->pwrreg_clkgate)
1353 pwr &= ~MCI_PWR_ON;
1354
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001355 if (host->variant->explicit_mclk_control &&
1356 ios->clock != host->clock_cache) {
1357 ret = clk_set_rate(host->clk, ios->clock);
1358 if (ret < 0)
1359 dev_err(mmc_dev(host->mmc),
1360 "Error setting clock rate (%d)\n", ret);
1361 else
1362 host->mclk = clk_get_rate(host->clk);
1363 }
1364 host->clock_cache = ios->clock;
1365
Linus Walleija6a64642009-09-14 12:56:14 +01001366 spin_lock_irqsave(&host->lock, flags);
1367
1368 mmci_set_clkreg(host, ios->clock);
Ulf Hansson7437cfa2012-01-18 09:17:27 +01001369 mmci_write_pwrreg(host, pwr);
Ulf Hanssonf829c042013-09-04 09:01:15 +01001370 mmci_reg_delay(host);
Linus Walleija6a64642009-09-14 12:56:14 +01001371
1372 spin_unlock_irqrestore(&host->lock, flags);
Ulf Hansson2cd976c2011-12-13 17:01:11 +01001373
Ulf Hansson2cd976c2011-12-13 17:01:11 +01001374 pm_runtime_mark_last_busy(mmc_dev(mmc));
1375 pm_runtime_put_autosuspend(mmc_dev(mmc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376}
1377
Russell King89001442009-07-09 15:16:07 +01001378static int mmci_get_cd(struct mmc_host *mmc)
1379{
1380 struct mmci_host *host = mmc_priv(mmc);
Rabin Vincent29719442010-08-09 12:54:43 +01001381 struct mmci_platform_data *plat = host->plat;
Ulf Hanssond2762092014-03-17 13:56:19 +01001382 unsigned int status = mmc_gpio_get_cd(mmc);
Russell King89001442009-07-09 15:16:07 +01001383
Ulf Hanssond2762092014-03-17 13:56:19 +01001384 if (status == -ENOSYS) {
Rabin Vincent4b8caec2010-08-09 12:56:40 +01001385 if (!plat->status)
1386 return 1; /* Assume always present */
1387
Rabin Vincent29719442010-08-09 12:54:43 +01001388 status = plat->status(mmc_dev(host->mmc));
Ulf Hanssond2762092014-03-17 13:56:19 +01001389 }
Russell King74bc8092010-07-29 15:58:59 +01001390 return status;
Russell King89001442009-07-09 15:16:07 +01001391}
1392
Ulf Hansson0f3ed7f2013-05-15 20:47:33 +01001393static int mmci_sig_volt_switch(struct mmc_host *mmc, struct mmc_ios *ios)
1394{
1395 int ret = 0;
1396
1397 if (!IS_ERR(mmc->supply.vqmmc)) {
1398
1399 pm_runtime_get_sync(mmc_dev(mmc));
1400
1401 switch (ios->signal_voltage) {
1402 case MMC_SIGNAL_VOLTAGE_330:
1403 ret = regulator_set_voltage(mmc->supply.vqmmc,
1404 2700000, 3600000);
1405 break;
1406 case MMC_SIGNAL_VOLTAGE_180:
1407 ret = regulator_set_voltage(mmc->supply.vqmmc,
1408 1700000, 1950000);
1409 break;
1410 case MMC_SIGNAL_VOLTAGE_120:
1411 ret = regulator_set_voltage(mmc->supply.vqmmc,
1412 1100000, 1300000);
1413 break;
1414 }
1415
1416 if (ret)
1417 dev_warn(mmc_dev(mmc), "Voltage switch failed\n");
1418
1419 pm_runtime_mark_last_busy(mmc_dev(mmc));
1420 pm_runtime_put_autosuspend(mmc_dev(mmc));
1421 }
1422
1423 return ret;
1424}
1425
Ulf Hansson01259622013-05-15 20:53:22 +01001426static struct mmc_host_ops mmci_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 .request = mmci_request,
Per Forlin58c7ccb2011-07-01 18:55:24 +02001428 .pre_req = mmci_pre_request,
1429 .post_req = mmci_post_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 .set_ios = mmci_set_ios,
Ulf Hanssond2762092014-03-17 13:56:19 +01001431 .get_ro = mmc_gpio_get_ro,
Russell King89001442009-07-09 15:16:07 +01001432 .get_cd = mmci_get_cd,
Ulf Hansson0f3ed7f2013-05-15 20:47:33 +01001433 .start_signal_voltage_switch = mmci_sig_volt_switch,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434};
1435
Ulf Hansson78f87df2014-03-17 15:53:07 +01001436static int mmci_of_parse(struct device_node *np, struct mmc_host *mmc)
1437{
Ulf Hansson4593df22014-03-21 10:13:05 +01001438 struct mmci_host *host = mmc_priv(mmc);
Ulf Hansson78f87df2014-03-17 15:53:07 +01001439 int ret = mmc_of_parse(mmc);
Lee Jones000bc9d2012-04-16 10:18:43 +01001440
Ulf Hansson78f87df2014-03-17 15:53:07 +01001441 if (ret)
1442 return ret;
Lee Jones000bc9d2012-04-16 10:18:43 +01001443
Ulf Hansson4593df22014-03-21 10:13:05 +01001444 if (of_get_property(np, "st,sig-dir-dat0", NULL))
1445 host->pwr_reg_add |= MCI_ST_DATA0DIREN;
1446 if (of_get_property(np, "st,sig-dir-dat2", NULL))
1447 host->pwr_reg_add |= MCI_ST_DATA2DIREN;
1448 if (of_get_property(np, "st,sig-dir-dat31", NULL))
1449 host->pwr_reg_add |= MCI_ST_DATA31DIREN;
1450 if (of_get_property(np, "st,sig-dir-dat74", NULL))
1451 host->pwr_reg_add |= MCI_ST_DATA74DIREN;
1452 if (of_get_property(np, "st,sig-dir-cmd", NULL))
1453 host->pwr_reg_add |= MCI_ST_CMDDIREN;
1454 if (of_get_property(np, "st,sig-pin-fbclk", NULL))
1455 host->pwr_reg_add |= MCI_ST_FBCLKEN;
1456
Lee Jones000bc9d2012-04-16 10:18:43 +01001457 if (of_get_property(np, "mmc-cap-mmc-highspeed", NULL))
Ulf Hansson78f87df2014-03-17 15:53:07 +01001458 mmc->caps |= MMC_CAP_MMC_HIGHSPEED;
Lee Jones000bc9d2012-04-16 10:18:43 +01001459 if (of_get_property(np, "mmc-cap-sd-highspeed", NULL))
Ulf Hansson78f87df2014-03-17 15:53:07 +01001460 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
Lee Jones000bc9d2012-04-16 10:18:43 +01001461
Ulf Hansson78f87df2014-03-17 15:53:07 +01001462 return 0;
Lee Jones000bc9d2012-04-16 10:18:43 +01001463}
Lee Jones000bc9d2012-04-16 10:18:43 +01001464
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001465static int mmci_probe(struct amba_device *dev,
Russell Kingaa25afa2011-02-19 15:55:00 +00001466 const struct amba_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
Linus Walleij6ef297f2009-09-22 14:29:36 +01001468 struct mmci_platform_data *plat = dev->dev.platform_data;
Lee Jones000bc9d2012-04-16 10:18:43 +01001469 struct device_node *np = dev->dev.of_node;
Rabin Vincent4956e102010-07-21 12:54:40 +01001470 struct variant_data *variant = id->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 struct mmci_host *host;
1472 struct mmc_host *mmc;
1473 int ret;
1474
Lee Jones000bc9d2012-04-16 10:18:43 +01001475 /* Must have platform data or Device Tree. */
1476 if (!plat && !np) {
1477 dev_err(&dev->dev, "No plat data or DT found\n");
1478 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 }
1480
Lee Jonesb9b52912012-06-12 10:49:51 +01001481 if (!plat) {
1482 plat = devm_kzalloc(&dev->dev, sizeof(*plat), GFP_KERNEL);
1483 if (!plat)
1484 return -ENOMEM;
1485 }
1486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
Ulf Hanssonef289982014-03-17 13:56:32 +01001488 if (!mmc)
1489 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Ulf Hansson78f87df2014-03-17 15:53:07 +01001491 ret = mmci_of_parse(np, mmc);
1492 if (ret)
1493 goto host_free;
1494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 host = mmc_priv(mmc);
Rabin Vincent4ea580f2009-04-17 08:44:19 +05301496 host->mmc = mmc;
Russell King012b7d32009-07-09 15:13:56 +01001497
1498 host->hw_designer = amba_manf(dev);
1499 host->hw_revision = amba_rev(dev);
Linus Walleij64de0282010-02-19 01:09:10 +01001500 dev_dbg(mmc_dev(mmc), "designer ID = 0x%02x\n", host->hw_designer);
1501 dev_dbg(mmc_dev(mmc), "revision = 0x%01x\n", host->hw_revision);
Russell King012b7d32009-07-09 15:13:56 +01001502
Ulf Hansson665ba562013-05-13 15:39:17 +01001503 host->clk = devm_clk_get(&dev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 if (IS_ERR(host->clk)) {
1505 ret = PTR_ERR(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 goto host_free;
1507 }
1508
Julia Lawallac940932012-08-26 16:00:59 +00001509 ret = clk_prepare_enable(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 if (ret)
Ulf Hansson665ba562013-05-13 15:39:17 +01001511 goto host_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Srinivas Kandagatla9c34b732014-06-02 10:10:04 +01001513 if (variant->qcom_fifo)
1514 host->get_rx_fifocnt = mmci_qcom_get_rx_fifocnt;
1515 else
1516 host->get_rx_fifocnt = mmci_get_rx_fifocnt;
1517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 host->plat = plat;
Rabin Vincent4956e102010-07-21 12:54:40 +01001519 host->variant = variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 host->mclk = clk_get_rate(host->clk);
Linus Walleijc8df9a52008-04-29 09:34:07 +01001521 /*
1522 * According to the spec, mclk is max 100 MHz,
1523 * so we try to adjust the clock down to this,
1524 * (if possible).
1525 */
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +01001526 if (host->mclk > variant->f_max) {
1527 ret = clk_set_rate(host->clk, variant->f_max);
Linus Walleijc8df9a52008-04-29 09:34:07 +01001528 if (ret < 0)
1529 goto clk_disable;
1530 host->mclk = clk_get_rate(host->clk);
Linus Walleij64de0282010-02-19 01:09:10 +01001531 dev_dbg(mmc_dev(mmc), "eventual mclk rate: %u Hz\n",
1532 host->mclk);
Linus Walleijc8df9a52008-04-29 09:34:07 +01001533 }
Ulf Hanssonef289982014-03-17 13:56:32 +01001534
Russell Kingc8ebae32011-01-11 19:35:53 +00001535 host->phybase = dev->res.start;
Ulf Hanssonef289982014-03-17 13:56:32 +01001536 host->base = devm_ioremap_resource(&dev->dev, &dev->res);
1537 if (IS_ERR(host->base)) {
1538 ret = PTR_ERR(host->base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 goto clk_disable;
1540 }
1541
Linus Walleij7f294e42011-07-08 09:57:15 +01001542 /*
1543 * The ARM and ST versions of the block have slightly different
1544 * clock divider equations which means that the minimum divider
1545 * differs too.
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001546 * on Qualcomm like controllers get the nearest minimum clock to 100Khz
Linus Walleij7f294e42011-07-08 09:57:15 +01001547 */
1548 if (variant->st_clkdiv)
1549 mmc->f_min = DIV_ROUND_UP(host->mclk, 257);
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001550 else if (variant->explicit_mclk_control)
1551 mmc->f_min = clk_round_rate(host->clk, 100000);
Linus Walleij7f294e42011-07-08 09:57:15 +01001552 else
1553 mmc->f_min = DIV_ROUND_UP(host->mclk, 512);
Linus Walleij808d97c2010-04-08 07:39:38 +01001554 /*
Ulf Hansson78f87df2014-03-17 15:53:07 +01001555 * If no maximum operating frequency is supplied, fall back to use
1556 * the module parameter, which has a (low) default value in case it
1557 * is not specified. Either value must not exceed the clock rate into
Ulf Hansson5080a082014-03-21 10:46:39 +01001558 * the block, of course.
Linus Walleij808d97c2010-04-08 07:39:38 +01001559 */
Ulf Hansson78f87df2014-03-17 15:53:07 +01001560 if (mmc->f_max)
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001561 mmc->f_max = variant->explicit_mclk_control ?
1562 min(variant->f_max, mmc->f_max) :
1563 min(host->mclk, mmc->f_max);
Linus Walleij808d97c2010-04-08 07:39:38 +01001564 else
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001565 mmc->f_max = variant->explicit_mclk_control ?
1566 fmax : min(host->mclk, fmax);
1567
1568
Linus Walleij64de0282010-02-19 01:09:10 +01001569 dev_dbg(mmc_dev(mmc), "clocking block at %u Hz\n", mmc->f_max);
1570
Ulf Hansson599c1d52013-01-07 16:22:50 +01001571 /* Get regulators and the supported OCR mask */
1572 mmc_regulator_get_supply(mmc);
1573 if (!mmc->ocr_avail)
Linus Walleij34e84f32009-09-22 14:41:40 +01001574 mmc->ocr_avail = plat->ocr_mask;
Ulf Hansson599c1d52013-01-07 16:22:50 +01001575 else if (plat->ocr_mask)
1576 dev_warn(mmc_dev(mmc), "Platform OCR mask is ignored\n");
1577
Ulf Hansson78f87df2014-03-17 15:53:07 +01001578 /* DT takes precedence over platform data. */
Ulf Hansson78f87df2014-03-17 15:53:07 +01001579 if (!np) {
1580 if (!plat->cd_invert)
1581 mmc->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
1582 mmc->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
1583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Ulf Hansson9dd8a8b2014-03-19 13:54:18 +01001585 /* We support these capabilities. */
1586 mmc->caps |= MMC_CAP_CMD23;
1587
Ulf Hansson8d94b542014-01-13 16:49:31 +01001588 if (variant->busy_detect) {
1589 mmci_ops.card_busy = mmci_card_busy;
1590 mmci_write_datactrlreg(host, MCI_ST_DPSM_BUSYMODE);
1591 mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY;
1592 mmc->max_busy_timeout = 0;
1593 }
1594
1595 mmc->ops = &mmci_ops;
1596
Ulf Hansson70be2082013-01-07 15:35:06 +01001597 /* We support these PM capabilities. */
Ulf Hansson78f87df2014-03-17 15:53:07 +01001598 mmc->pm_caps |= MMC_PM_KEEP_POWER;
Ulf Hansson70be2082013-01-07 15:35:06 +01001599
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 /*
1601 * We can do SGIO
1602 */
Martin K. Petersena36274e2010-09-10 01:33:59 -04001603 mmc->max_segs = NR_SG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
1605 /*
Rabin Vincent08458ef2010-07-21 12:55:59 +01001606 * Since only a certain number of bits are valid in the data length
1607 * register, we must ensure that we don't exceed 2^num-1 bytes in a
1608 * single request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 */
Rabin Vincent08458ef2010-07-21 12:55:59 +01001610 mmc->max_req_size = (1 << variant->datalength_bits) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
1612 /*
1613 * Set the maximum segment size. Since we aren't doing DMA
1614 * (yet) we are only limited by the data length register.
1615 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001616 mmc->max_seg_size = mmc->max_req_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001618 /*
1619 * Block size can be up to 2048 bytes, but must be a power of two.
1620 */
Will Deacon8f7f6b7e2012-02-24 11:25:21 +00001621 mmc->max_blk_size = 1 << 11;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001622
Pierre Ossman55db8902006-11-21 17:55:45 +01001623 /*
Will Deacon8f7f6b7e2012-02-24 11:25:21 +00001624 * Limit the number of blocks transferred so that we don't overflow
1625 * the maximum request size.
Pierre Ossman55db8902006-11-21 17:55:45 +01001626 */
Will Deacon8f7f6b7e2012-02-24 11:25:21 +00001627 mmc->max_blk_count = mmc->max_req_size >> 11;
Pierre Ossman55db8902006-11-21 17:55:45 +01001628
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 spin_lock_init(&host->lock);
1630
1631 writel(0, host->base + MMCIMASK0);
1632 writel(0, host->base + MMCIMASK1);
1633 writel(0xfff, host->base + MMCICLEAR);
1634
Ulf Hansson78f87df2014-03-17 15:53:07 +01001635 /* If DT, cd/wp gpios must be supplied through it. */
1636 if (!np && gpio_is_valid(plat->gpio_cd)) {
Ulf Hanssond2762092014-03-17 13:56:19 +01001637 ret = mmc_gpio_request_cd(mmc, plat->gpio_cd, 0);
1638 if (ret)
Ulf Hanssonef289982014-03-17 13:56:32 +01001639 goto clk_disable;
Russell King89001442009-07-09 15:16:07 +01001640 }
Ulf Hansson78f87df2014-03-17 15:53:07 +01001641 if (!np && gpio_is_valid(plat->gpio_wp)) {
Ulf Hanssond2762092014-03-17 13:56:19 +01001642 ret = mmc_gpio_request_ro(mmc, plat->gpio_wp);
1643 if (ret)
Ulf Hanssonef289982014-03-17 13:56:32 +01001644 goto clk_disable;
Russell King89001442009-07-09 15:16:07 +01001645 }
1646
Ulf Hanssonef289982014-03-17 13:56:32 +01001647 ret = devm_request_irq(&dev->dev, dev->irq[0], mmci_irq, IRQF_SHARED,
1648 DRIVER_NAME " (cmd)", host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 if (ret)
Ulf Hanssonef289982014-03-17 13:56:32 +01001650 goto clk_disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Russell Kingdfb85182012-05-03 11:33:15 +01001652 if (!dev->irq[1])
Linus Walleij2686b4b2010-10-19 12:39:48 +01001653 host->singleirq = true;
1654 else {
Ulf Hanssonef289982014-03-17 13:56:32 +01001655 ret = devm_request_irq(&dev->dev, dev->irq[1], mmci_pio_irq,
1656 IRQF_SHARED, DRIVER_NAME " (pio)", host);
Linus Walleij2686b4b2010-10-19 12:39:48 +01001657 if (ret)
Ulf Hanssonef289982014-03-17 13:56:32 +01001658 goto clk_disable;
Linus Walleij2686b4b2010-10-19 12:39:48 +01001659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Linus Walleij8cb28152011-01-24 15:22:13 +01001661 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
1663 amba_set_drvdata(dev, mmc);
1664
Russell Kingc8ebae32011-01-11 19:35:53 +00001665 dev_info(&dev->dev, "%s: PL%03x manf %x rev%u at 0x%08llx irq %d,%d (pio)\n",
1666 mmc_hostname(mmc), amba_part(dev), amba_manf(dev),
1667 amba_rev(dev), (unsigned long long)dev->res.start,
1668 dev->irq[0], dev->irq[1]);
1669
1670 mmci_dma_setup(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
Ulf Hansson2cd976c2011-12-13 17:01:11 +01001672 pm_runtime_set_autosuspend_delay(&dev->dev, 50);
1673 pm_runtime_use_autosuspend(&dev->dev);
Russell King1c3be362011-08-14 09:17:05 +01001674 pm_runtime_put(&dev->dev);
1675
Russell King8c11a942010-12-28 19:40:40 +00001676 mmc_add_host(mmc);
1677
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 return 0;
1679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 clk_disable:
Julia Lawallac940932012-08-26 16:00:59 +00001681 clk_disable_unprepare(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 host_free:
1683 mmc_free_host(mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 return ret;
1685}
1686
Bill Pemberton6e0ee712012-11-19 13:26:03 -05001687static int mmci_remove(struct amba_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688{
1689 struct mmc_host *mmc = amba_get_drvdata(dev);
1690
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 if (mmc) {
1692 struct mmci_host *host = mmc_priv(mmc);
1693
Russell King1c3be362011-08-14 09:17:05 +01001694 /*
1695 * Undo pm_runtime_put() in probe. We use the _sync
1696 * version here so that we can access the primecell.
1697 */
1698 pm_runtime_get_sync(&dev->dev);
1699
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 mmc_remove_host(mmc);
1701
1702 writel(0, host->base + MMCIMASK0);
1703 writel(0, host->base + MMCIMASK1);
1704
1705 writel(0, host->base + MMCICOMMAND);
1706 writel(0, host->base + MMCIDATACTRL);
1707
Russell Kingc8ebae32011-01-11 19:35:53 +00001708 mmci_dma_release(host);
Julia Lawallac940932012-08-26 16:00:59 +00001709 clk_disable_unprepare(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 mmc_free_host(mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 }
1712
1713 return 0;
1714}
1715
Ulf Hansson571dce42014-01-23 00:38:00 +01001716#ifdef CONFIG_PM
Ulf Hansson1ff44432013-09-04 09:05:17 +01001717static void mmci_save(struct mmci_host *host)
1718{
1719 unsigned long flags;
1720
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001721 spin_lock_irqsave(&host->lock, flags);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001722
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001723 writel(0, host->base + MMCIMASK0);
1724 if (host->variant->pwrreg_nopower) {
Ulf Hansson1ff44432013-09-04 09:05:17 +01001725 writel(0, host->base + MMCIDATACTRL);
1726 writel(0, host->base + MMCIPOWER);
1727 writel(0, host->base + MMCICLOCK);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001728 }
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001729 mmci_reg_delay(host);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001730
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001731 spin_unlock_irqrestore(&host->lock, flags);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001732}
1733
1734static void mmci_restore(struct mmci_host *host)
1735{
1736 unsigned long flags;
1737
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001738 spin_lock_irqsave(&host->lock, flags);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001739
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001740 if (host->variant->pwrreg_nopower) {
Ulf Hansson1ff44432013-09-04 09:05:17 +01001741 writel(host->clk_reg, host->base + MMCICLOCK);
1742 writel(host->datactrl_reg, host->base + MMCIDATACTRL);
1743 writel(host->pwr_reg, host->base + MMCIPOWER);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001744 }
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001745 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
1746 mmci_reg_delay(host);
1747
1748 spin_unlock_irqrestore(&host->lock, flags);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001749}
1750
Ulf Hansson82592932013-01-09 11:15:26 +01001751static int mmci_runtime_suspend(struct device *dev)
1752{
1753 struct amba_device *adev = to_amba_device(dev);
1754 struct mmc_host *mmc = amba_get_drvdata(adev);
1755
1756 if (mmc) {
1757 struct mmci_host *host = mmc_priv(mmc);
Ulf Hanssone36bd9c62013-09-04 09:00:37 +01001758 pinctrl_pm_select_sleep_state(dev);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001759 mmci_save(host);
Ulf Hansson82592932013-01-09 11:15:26 +01001760 clk_disable_unprepare(host->clk);
1761 }
1762
1763 return 0;
1764}
1765
1766static int mmci_runtime_resume(struct device *dev)
1767{
1768 struct amba_device *adev = to_amba_device(dev);
1769 struct mmc_host *mmc = amba_get_drvdata(adev);
1770
1771 if (mmc) {
1772 struct mmci_host *host = mmc_priv(mmc);
1773 clk_prepare_enable(host->clk);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001774 mmci_restore(host);
Ulf Hanssone36bd9c62013-09-04 09:00:37 +01001775 pinctrl_pm_select_default_state(dev);
Ulf Hansson82592932013-01-09 11:15:26 +01001776 }
1777
1778 return 0;
1779}
1780#endif
1781
Ulf Hansson48fa7002011-12-13 16:59:34 +01001782static const struct dev_pm_ops mmci_dev_pm_ops = {
Ulf Hanssonf3737fa2014-01-23 01:11:33 +01001783 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1784 pm_runtime_force_resume)
Ulf Hansson571dce42014-01-23 00:38:00 +01001785 SET_PM_RUNTIME_PM_OPS(mmci_runtime_suspend, mmci_runtime_resume, NULL)
Ulf Hansson48fa7002011-12-13 16:59:34 +01001786};
1787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788static struct amba_id mmci_ids[] = {
1789 {
1790 .id = 0x00041180,
Pawel Moll768fbc12011-03-11 17:18:07 +00001791 .mask = 0xff0fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001792 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 },
1794 {
Pawel Moll768fbc12011-03-11 17:18:07 +00001795 .id = 0x01041180,
1796 .mask = 0xff0fffff,
1797 .data = &variant_arm_extended_fifo,
1798 },
1799 {
Pawel Moll3a372982013-01-24 14:12:45 +01001800 .id = 0x02041180,
1801 .mask = 0xff0fffff,
1802 .data = &variant_arm_extended_fifo_hwfc,
1803 },
1804 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 .id = 0x00041181,
1806 .mask = 0x000fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001807 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 },
Linus Walleijcc30d602009-01-04 15:18:54 +01001809 /* ST Micro variants */
1810 {
1811 .id = 0x00180180,
1812 .mask = 0x00ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001813 .data = &variant_u300,
Linus Walleijcc30d602009-01-04 15:18:54 +01001814 },
1815 {
Linus Walleij34fd4212012-04-10 17:43:59 +01001816 .id = 0x10180180,
1817 .mask = 0xf0ffffff,
1818 .data = &variant_nomadik,
1819 },
1820 {
Linus Walleijcc30d602009-01-04 15:18:54 +01001821 .id = 0x00280180,
1822 .mask = 0x00ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001823 .data = &variant_u300,
1824 },
1825 {
1826 .id = 0x00480180,
Philippe Langlais1784b152011-03-25 08:51:52 +01001827 .mask = 0xf0ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001828 .data = &variant_ux500,
Linus Walleijcc30d602009-01-04 15:18:54 +01001829 },
Philippe Langlais1784b152011-03-25 08:51:52 +01001830 {
1831 .id = 0x10480180,
1832 .mask = 0xf0ffffff,
1833 .data = &variant_ux500v2,
1834 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 { 0, 0 },
1836};
1837
Dave Martin9f998352011-10-05 15:15:21 +01001838MODULE_DEVICE_TABLE(amba, mmci_ids);
1839
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840static struct amba_driver mmci_driver = {
1841 .drv = {
1842 .name = DRIVER_NAME,
Ulf Hansson48fa7002011-12-13 16:59:34 +01001843 .pm = &mmci_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 },
1845 .probe = mmci_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05001846 .remove = mmci_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 .id_table = mmci_ids,
1848};
1849
viresh kumar9e5ed092012-03-15 10:40:38 +01001850module_amba_driver(mmci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852module_param(fmax, uint, 0444);
1853
1854MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
1855MODULE_LICENSE("GPL");