blob: 6483c5cc67355ef0fed4bd2ca79ba2d7e15fa64a [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
Srinivas Kandagatla55b604a2014-06-02 10:10:13 +0100193static struct variant_data variant_qcom = {
194 .fifosize = 16 * 4,
195 .fifohalfsize = 8 * 4,
196 .clkreg = MCI_CLK_ENABLE,
197 .clkreg_enable = MCI_QCOM_CLK_FLOWENA |
198 MCI_QCOM_CLK_SELECT_IN_FBCLK,
199 .clkreg_8bit_bus_enable = MCI_QCOM_CLK_WIDEBUS_8,
200 .datactrl_mask_ddrmode = MCI_QCOM_CLK_SELECT_IN_DDR_MODE,
201 .data_cmd_enable = MCI_QCOM_CSPM_DATCMD,
202 .blksz_datactrl4 = true,
203 .datalength_bits = 24,
204 .pwrreg_powerup = MCI_PWR_UP,
205 .f_max = 208000000,
206 .explicit_mclk_control = true,
207 .qcom_fifo = true,
208};
209
Ulf Hansson01259622013-05-15 20:53:22 +0100210static int mmci_card_busy(struct mmc_host *mmc)
211{
212 struct mmci_host *host = mmc_priv(mmc);
213 unsigned long flags;
214 int busy = 0;
215
216 pm_runtime_get_sync(mmc_dev(mmc));
217
218 spin_lock_irqsave(&host->lock, flags);
219 if (readl(host->base + MMCISTATUS) & MCI_ST_CARDBUSY)
220 busy = 1;
221 spin_unlock_irqrestore(&host->lock, flags);
222
223 pm_runtime_mark_last_busy(mmc_dev(mmc));
224 pm_runtime_put_autosuspend(mmc_dev(mmc));
225
226 return busy;
227}
228
Linus Walleija6a64642009-09-14 12:56:14 +0100229/*
Ulf Hansson653a7612013-01-21 21:29:34 +0100230 * Validate mmc prerequisites
231 */
232static int mmci_validate_data(struct mmci_host *host,
233 struct mmc_data *data)
234{
235 if (!data)
236 return 0;
237
238 if (!is_power_of_2(data->blksz)) {
239 dev_err(mmc_dev(host->mmc),
240 "unsupported block size (%d bytes)\n", data->blksz);
241 return -EINVAL;
242 }
243
244 return 0;
245}
246
Ulf Hanssonf829c042013-09-04 09:01:15 +0100247static void mmci_reg_delay(struct mmci_host *host)
248{
249 /*
250 * According to the spec, at least three feedback clock cycles
251 * of max 52 MHz must pass between two writes to the MMCICLOCK reg.
252 * Three MCLK clock cycles must pass between two MMCIPOWER reg writes.
253 * Worst delay time during card init is at 100 kHz => 30 us.
254 * Worst delay time when up and running is at 25 MHz => 120 ns.
255 */
256 if (host->cclk < 25000000)
257 udelay(30);
258 else
259 ndelay(120);
260}
261
Ulf Hansson653a7612013-01-21 21:29:34 +0100262/*
Linus Walleija6a64642009-09-14 12:56:14 +0100263 * This must be called with host->lock held
264 */
Ulf Hansson7437cfa2012-01-18 09:17:27 +0100265static void mmci_write_clkreg(struct mmci_host *host, u32 clk)
266{
267 if (host->clk_reg != clk) {
268 host->clk_reg = clk;
269 writel(clk, host->base + MMCICLOCK);
270 }
271}
272
273/*
274 * This must be called with host->lock held
275 */
276static void mmci_write_pwrreg(struct mmci_host *host, u32 pwr)
277{
278 if (host->pwr_reg != pwr) {
279 host->pwr_reg = pwr;
280 writel(pwr, host->base + MMCIPOWER);
281 }
282}
283
284/*
285 * This must be called with host->lock held
286 */
Ulf Hansson9cc639a2013-05-15 20:48:23 +0100287static void mmci_write_datactrlreg(struct mmci_host *host, u32 datactrl)
288{
Ulf Hansson01259622013-05-15 20:53:22 +0100289 /* Keep ST Micro busy mode if enabled */
290 datactrl |= host->datactrl_reg & MCI_ST_DPSM_BUSYMODE;
291
Ulf Hansson9cc639a2013-05-15 20:48:23 +0100292 if (host->datactrl_reg != datactrl) {
293 host->datactrl_reg = datactrl;
294 writel(datactrl, host->base + MMCIDATACTRL);
295 }
296}
297
298/*
299 * This must be called with host->lock held
300 */
Linus Walleija6a64642009-09-14 12:56:14 +0100301static void mmci_set_clkreg(struct mmci_host *host, unsigned int desired)
302{
Rabin Vincent4956e102010-07-21 12:54:40 +0100303 struct variant_data *variant = host->variant;
304 u32 clk = variant->clkreg;
Linus Walleija6a64642009-09-14 12:56:14 +0100305
Ulf Hanssonc58a8502013-05-13 15:40:03 +0100306 /* Make sure cclk reflects the current calculated clock */
307 host->cclk = 0;
308
Linus Walleija6a64642009-09-14 12:56:14 +0100309 if (desired) {
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +0100310 if (variant->explicit_mclk_control) {
311 host->cclk = host->mclk;
312 } else if (desired >= host->mclk) {
Linus Walleij991a86e2010-12-10 09:35:53 +0100313 clk = MCI_CLK_BYPASS;
Linus Walleij399bc482011-04-01 07:59:17 +0100314 if (variant->st_clkdiv)
315 clk |= MCI_ST_UX500_NEG_EDGE;
Linus Walleija6a64642009-09-14 12:56:14 +0100316 host->cclk = host->mclk;
Linus Walleijb70a67f2010-12-06 09:24:14 +0100317 } else if (variant->st_clkdiv) {
318 /*
319 * DB8500 TRM says f = mclk / (clkdiv + 2)
320 * => clkdiv = (mclk / f) - 2
321 * Round the divider up so we don't exceed the max
322 * frequency
323 */
324 clk = DIV_ROUND_UP(host->mclk, desired) - 2;
325 if (clk >= 256)
326 clk = 255;
327 host->cclk = host->mclk / (clk + 2);
Linus Walleija6a64642009-09-14 12:56:14 +0100328 } else {
Linus Walleijb70a67f2010-12-06 09:24:14 +0100329 /*
330 * PL180 TRM says f = mclk / (2 * (clkdiv + 1))
331 * => clkdiv = mclk / (2 * f) - 1
332 */
Linus Walleija6a64642009-09-14 12:56:14 +0100333 clk = host->mclk / (2 * desired) - 1;
334 if (clk >= 256)
335 clk = 255;
336 host->cclk = host->mclk / (2 * (clk + 1));
337 }
Rabin Vincent4380c142010-07-21 12:55:18 +0100338
339 clk |= variant->clkreg_enable;
Linus Walleija6a64642009-09-14 12:56:14 +0100340 clk |= MCI_CLK_ENABLE;
341 /* This hasn't proven to be worthwhile */
342 /* clk |= MCI_CLK_PWRSAVE; */
343 }
344
Ulf Hanssonc58a8502013-05-13 15:40:03 +0100345 /* Set actual clock for debug */
346 host->mmc->actual_clock = host->cclk;
347
Linus Walleij9e6c82c2009-09-14 12:57:11 +0100348 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4)
Linus Walleij771dc152010-04-08 07:38:52 +0100349 clk |= MCI_4BIT_BUS;
350 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_8)
Srinivas Kandagatlae1412d82014-06-02 10:09:23 +0100351 clk |= variant->clkreg_8bit_bus_enable;
Linus Walleij9e6c82c2009-09-14 12:57:11 +0100352
Seungwon Jeon6dad6c92014-03-14 21:12:13 +0900353 if (host->mmc->ios.timing == MMC_TIMING_UHS_DDR50 ||
354 host->mmc->ios.timing == MMC_TIMING_MMC_DDR52)
Srinivas Kandagatlae8740642014-06-02 10:09:30 +0100355 clk |= variant->clkreg_neg_edge_enable;
Ulf Hansson6dbb6ee2013-01-07 15:30:44 +0100356
Ulf Hansson7437cfa2012-01-18 09:17:27 +0100357 mmci_write_clkreg(host, clk);
Linus Walleija6a64642009-09-14 12:56:14 +0100358}
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360static void
361mmci_request_end(struct mmci_host *host, struct mmc_request *mrq)
362{
363 writel(0, host->base + MMCICOMMAND);
364
Russell Kinge47c2222007-01-08 16:42:51 +0000365 BUG_ON(host->data);
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 host->mrq = NULL;
368 host->cmd = NULL;
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 mmc_request_done(host->mmc, mrq);
Ulf Hansson2cd976c2011-12-13 17:01:11 +0100371
372 pm_runtime_mark_last_busy(mmc_dev(host->mmc));
373 pm_runtime_put_autosuspend(mmc_dev(host->mmc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
Linus Walleij2686b4b2010-10-19 12:39:48 +0100376static void mmci_set_mask1(struct mmci_host *host, unsigned int mask)
377{
378 void __iomem *base = host->base;
379
380 if (host->singleirq) {
381 unsigned int mask0 = readl(base + MMCIMASK0);
382
383 mask0 &= ~MCI_IRQ1MASK;
384 mask0 |= mask;
385
386 writel(mask0, base + MMCIMASK0);
387 }
388
389 writel(mask, base + MMCIMASK1);
390}
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392static void mmci_stop_data(struct mmci_host *host)
393{
Ulf Hansson9cc639a2013-05-15 20:48:23 +0100394 mmci_write_datactrlreg(host, 0);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100395 mmci_set_mask1(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 host->data = NULL;
397}
398
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100399static void mmci_init_sg(struct mmci_host *host, struct mmc_data *data)
400{
401 unsigned int flags = SG_MITER_ATOMIC;
402
403 if (data->flags & MMC_DATA_READ)
404 flags |= SG_MITER_TO_SG;
405 else
406 flags |= SG_MITER_FROM_SG;
407
408 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
409}
410
Russell Kingc8ebae32011-01-11 19:35:53 +0000411/*
412 * All the DMA operation mode stuff goes inside this ifdef.
413 * This assumes that you have a generic DMA device interface,
414 * no custom DMA interfaces are supported.
415 */
416#ifdef CONFIG_DMA_ENGINE
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500417static void mmci_dma_setup(struct mmci_host *host)
Russell Kingc8ebae32011-01-11 19:35:53 +0000418{
Russell Kingc8ebae32011-01-11 19:35:53 +0000419 const char *rxname, *txname;
420 dma_cap_mask_t mask;
421
Lee Jones1fd83f02013-05-03 12:51:17 +0100422 host->dma_rx_channel = dma_request_slave_channel(mmc_dev(host->mmc), "rx");
423 host->dma_tx_channel = dma_request_slave_channel(mmc_dev(host->mmc), "tx");
Russell Kingc8ebae32011-01-11 19:35:53 +0000424
Per Forlin58c7ccb2011-07-01 18:55:24 +0200425 /* initialize pre request cookie */
426 host->next_data.cookie = 1;
427
Russell Kingc8ebae32011-01-11 19:35:53 +0000428 /* Try to acquire a generic DMA engine slave channel */
429 dma_cap_zero(mask);
430 dma_cap_set(DMA_SLAVE, mask);
431
432 /*
433 * If only an RX channel is specified, the driver will
434 * attempt to use it bidirectionally, however if it is
435 * is specified but cannot be located, DMA will be disabled.
436 */
Lee Jones1fd83f02013-05-03 12:51:17 +0100437 if (host->dma_rx_channel && !host->dma_tx_channel)
Russell Kingc8ebae32011-01-11 19:35:53 +0000438 host->dma_tx_channel = host->dma_rx_channel;
Russell Kingc8ebae32011-01-11 19:35:53 +0000439
440 if (host->dma_rx_channel)
441 rxname = dma_chan_name(host->dma_rx_channel);
442 else
443 rxname = "none";
444
445 if (host->dma_tx_channel)
446 txname = dma_chan_name(host->dma_tx_channel);
447 else
448 txname = "none";
449
450 dev_info(mmc_dev(host->mmc), "DMA channels RX %s, TX %s\n",
451 rxname, txname);
452
453 /*
454 * Limit the maximum segment size in any SG entry according to
455 * the parameters of the DMA engine device.
456 */
457 if (host->dma_tx_channel) {
458 struct device *dev = host->dma_tx_channel->device->dev;
459 unsigned int max_seg_size = dma_get_max_seg_size(dev);
460
461 if (max_seg_size < host->mmc->max_seg_size)
462 host->mmc->max_seg_size = max_seg_size;
463 }
464 if (host->dma_rx_channel) {
465 struct device *dev = host->dma_rx_channel->device->dev;
466 unsigned int max_seg_size = dma_get_max_seg_size(dev);
467
468 if (max_seg_size < host->mmc->max_seg_size)
469 host->mmc->max_seg_size = max_seg_size;
470 }
471}
472
473/*
Bill Pemberton6e0ee712012-11-19 13:26:03 -0500474 * This is used in or so inline it
Russell Kingc8ebae32011-01-11 19:35:53 +0000475 * so it can be discarded.
476 */
477static inline void mmci_dma_release(struct mmci_host *host)
478{
Russell Kingc8ebae32011-01-11 19:35:53 +0000479 if (host->dma_rx_channel)
480 dma_release_channel(host->dma_rx_channel);
Ulf Hansson8c3a05b2014-05-20 06:45:54 +0200481 if (host->dma_tx_channel)
Russell Kingc8ebae32011-01-11 19:35:53 +0000482 dma_release_channel(host->dma_tx_channel);
483 host->dma_rx_channel = host->dma_tx_channel = NULL;
484}
485
Ulf Hansson653a7612013-01-21 21:29:34 +0100486static void mmci_dma_data_error(struct mmci_host *host)
487{
488 dev_err(mmc_dev(host->mmc), "error during DMA transfer!\n");
489 dmaengine_terminate_all(host->dma_current);
490 host->dma_current = NULL;
491 host->dma_desc_current = NULL;
492 host->data->host_cookie = 0;
493}
494
Russell Kingc8ebae32011-01-11 19:35:53 +0000495static void mmci_dma_unmap(struct mmci_host *host, struct mmc_data *data)
496{
Ulf Hansson653a7612013-01-21 21:29:34 +0100497 struct dma_chan *chan;
Russell Kingc8ebae32011-01-11 19:35:53 +0000498 enum dma_data_direction dir;
Ulf Hansson653a7612013-01-21 21:29:34 +0100499
500 if (data->flags & MMC_DATA_READ) {
501 dir = DMA_FROM_DEVICE;
502 chan = host->dma_rx_channel;
503 } else {
504 dir = DMA_TO_DEVICE;
505 chan = host->dma_tx_channel;
506 }
507
508 dma_unmap_sg(chan->device->dev, data->sg, data->sg_len, dir);
509}
510
511static void mmci_dma_finalize(struct mmci_host *host, struct mmc_data *data)
512{
Russell Kingc8ebae32011-01-11 19:35:53 +0000513 u32 status;
514 int i;
515
516 /* Wait up to 1ms for the DMA to complete */
517 for (i = 0; ; i++) {
518 status = readl(host->base + MMCISTATUS);
519 if (!(status & MCI_RXDATAAVLBLMASK) || i >= 100)
520 break;
521 udelay(10);
522 }
523
524 /*
525 * Check to see whether we still have some data left in the FIFO -
526 * this catches DMA controllers which are unable to monitor the
527 * DMALBREQ and DMALSREQ signals while allowing us to DMA to non-
528 * contiguous buffers. On TX, we'll get a FIFO underrun error.
529 */
530 if (status & MCI_RXDATAAVLBLMASK) {
Ulf Hansson653a7612013-01-21 21:29:34 +0100531 mmci_dma_data_error(host);
Russell Kingc8ebae32011-01-11 19:35:53 +0000532 if (!data->error)
533 data->error = -EIO;
534 }
535
Per Forlin58c7ccb2011-07-01 18:55:24 +0200536 if (!data->host_cookie)
Ulf Hansson653a7612013-01-21 21:29:34 +0100537 mmci_dma_unmap(host, data);
Russell Kingc8ebae32011-01-11 19:35:53 +0000538
539 /*
540 * Use of DMA with scatter-gather is impossible.
541 * Give up with DMA and switch back to PIO mode.
542 */
543 if (status & MCI_RXDATAAVLBLMASK) {
544 dev_err(mmc_dev(host->mmc), "buggy DMA detected. Taking evasive action.\n");
545 mmci_dma_release(host);
546 }
Ulf Hansson653a7612013-01-21 21:29:34 +0100547
548 host->dma_current = NULL;
549 host->dma_desc_current = NULL;
Russell Kingc8ebae32011-01-11 19:35:53 +0000550}
551
Ulf Hansson653a7612013-01-21 21:29:34 +0100552/* prepares DMA channel and DMA descriptor, returns non-zero on failure */
553static int __mmci_dma_prep_data(struct mmci_host *host, struct mmc_data *data,
554 struct dma_chan **dma_chan,
555 struct dma_async_tx_descriptor **dma_desc)
Russell Kingc8ebae32011-01-11 19:35:53 +0000556{
557 struct variant_data *variant = host->variant;
558 struct dma_slave_config conf = {
559 .src_addr = host->phybase + MMCIFIFO,
560 .dst_addr = host->phybase + MMCIFIFO,
561 .src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
562 .dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
563 .src_maxburst = variant->fifohalfsize >> 2, /* # of words */
564 .dst_maxburst = variant->fifohalfsize >> 2, /* # of words */
Viresh Kumar258aea72012-02-01 16:12:19 +0530565 .device_fc = false,
Russell Kingc8ebae32011-01-11 19:35:53 +0000566 };
Russell Kingc8ebae32011-01-11 19:35:53 +0000567 struct dma_chan *chan;
568 struct dma_device *device;
569 struct dma_async_tx_descriptor *desc;
Vinod Koul05f57992011-10-14 10:45:11 +0530570 enum dma_data_direction buffer_dirn;
Russell Kingc8ebae32011-01-11 19:35:53 +0000571 int nr_sg;
572
Russell Kingc8ebae32011-01-11 19:35:53 +0000573 if (data->flags & MMC_DATA_READ) {
Vinod Koul05f57992011-10-14 10:45:11 +0530574 conf.direction = DMA_DEV_TO_MEM;
575 buffer_dirn = DMA_FROM_DEVICE;
Russell Kingc8ebae32011-01-11 19:35:53 +0000576 chan = host->dma_rx_channel;
577 } else {
Vinod Koul05f57992011-10-14 10:45:11 +0530578 conf.direction = DMA_MEM_TO_DEV;
579 buffer_dirn = DMA_TO_DEVICE;
Russell Kingc8ebae32011-01-11 19:35:53 +0000580 chan = host->dma_tx_channel;
581 }
582
583 /* If there's no DMA channel, fall back to PIO */
584 if (!chan)
585 return -EINVAL;
586
587 /* If less than or equal to the fifo size, don't bother with DMA */
Per Forlin58c7ccb2011-07-01 18:55:24 +0200588 if (data->blksz * data->blocks <= variant->fifosize)
Russell Kingc8ebae32011-01-11 19:35:53 +0000589 return -EINVAL;
590
591 device = chan->device;
Vinod Koul05f57992011-10-14 10:45:11 +0530592 nr_sg = dma_map_sg(device->dev, data->sg, data->sg_len, buffer_dirn);
Russell Kingc8ebae32011-01-11 19:35:53 +0000593 if (nr_sg == 0)
594 return -EINVAL;
595
596 dmaengine_slave_config(chan, &conf);
Alexandre Bounine16052822012-03-08 16:11:18 -0500597 desc = dmaengine_prep_slave_sg(chan, data->sg, nr_sg,
Russell Kingc8ebae32011-01-11 19:35:53 +0000598 conf.direction, DMA_CTRL_ACK);
599 if (!desc)
600 goto unmap_exit;
601
Ulf Hansson653a7612013-01-21 21:29:34 +0100602 *dma_chan = chan;
603 *dma_desc = desc;
Russell Kingc8ebae32011-01-11 19:35:53 +0000604
Per Forlin58c7ccb2011-07-01 18:55:24 +0200605 return 0;
606
607 unmap_exit:
Vinod Koul05f57992011-10-14 10:45:11 +0530608 dma_unmap_sg(device->dev, data->sg, data->sg_len, buffer_dirn);
Per Forlin58c7ccb2011-07-01 18:55:24 +0200609 return -ENOMEM;
610}
611
Ulf Hansson653a7612013-01-21 21:29:34 +0100612static inline int mmci_dma_prep_data(struct mmci_host *host,
613 struct mmc_data *data)
614{
615 /* Check if next job is already prepared. */
616 if (host->dma_current && host->dma_desc_current)
617 return 0;
618
619 /* No job were prepared thus do it now. */
620 return __mmci_dma_prep_data(host, data, &host->dma_current,
621 &host->dma_desc_current);
622}
623
624static inline int mmci_dma_prep_next(struct mmci_host *host,
625 struct mmc_data *data)
626{
627 struct mmci_host_next *nd = &host->next_data;
628 return __mmci_dma_prep_data(host, data, &nd->dma_chan, &nd->dma_desc);
629}
630
Per Forlin58c7ccb2011-07-01 18:55:24 +0200631static int mmci_dma_start_data(struct mmci_host *host, unsigned int datactrl)
632{
633 int ret;
634 struct mmc_data *data = host->data;
635
Ulf Hansson653a7612013-01-21 21:29:34 +0100636 ret = mmci_dma_prep_data(host, host->data);
Per Forlin58c7ccb2011-07-01 18:55:24 +0200637 if (ret)
638 return ret;
639
640 /* Okay, go for it. */
Russell Kingc8ebae32011-01-11 19:35:53 +0000641 dev_vdbg(mmc_dev(host->mmc),
642 "Submit MMCI DMA job, sglen %d blksz %04x blks %04x flags %08x\n",
643 data->sg_len, data->blksz, data->blocks, data->flags);
Per Forlin58c7ccb2011-07-01 18:55:24 +0200644 dmaengine_submit(host->dma_desc_current);
645 dma_async_issue_pending(host->dma_current);
Russell Kingc8ebae32011-01-11 19:35:53 +0000646
647 datactrl |= MCI_DPSM_DMAENABLE;
648
649 /* Trigger the DMA transfer */
Ulf Hansson9cc639a2013-05-15 20:48:23 +0100650 mmci_write_datactrlreg(host, datactrl);
Russell Kingc8ebae32011-01-11 19:35:53 +0000651
652 /*
653 * Let the MMCI say when the data is ended and it's time
654 * to fire next DMA request. When that happens, MMCI will
655 * call mmci_data_end()
656 */
657 writel(readl(host->base + MMCIMASK0) | MCI_DATAENDMASK,
658 host->base + MMCIMASK0);
659 return 0;
Russell Kingc8ebae32011-01-11 19:35:53 +0000660}
Per Forlin58c7ccb2011-07-01 18:55:24 +0200661
662static void mmci_get_next_data(struct mmci_host *host, struct mmc_data *data)
663{
664 struct mmci_host_next *next = &host->next_data;
665
Ulf Hansson653a7612013-01-21 21:29:34 +0100666 WARN_ON(data->host_cookie && data->host_cookie != next->cookie);
667 WARN_ON(!data->host_cookie && (next->dma_desc || next->dma_chan));
Per Forlin58c7ccb2011-07-01 18:55:24 +0200668
669 host->dma_desc_current = next->dma_desc;
670 host->dma_current = next->dma_chan;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200671 next->dma_desc = NULL;
672 next->dma_chan = NULL;
673}
674
675static void mmci_pre_request(struct mmc_host *mmc, struct mmc_request *mrq,
676 bool is_first_req)
677{
678 struct mmci_host *host = mmc_priv(mmc);
679 struct mmc_data *data = mrq->data;
680 struct mmci_host_next *nd = &host->next_data;
681
682 if (!data)
683 return;
684
Ulf Hansson653a7612013-01-21 21:29:34 +0100685 BUG_ON(data->host_cookie);
Per Forlin58c7ccb2011-07-01 18:55:24 +0200686
Ulf Hansson653a7612013-01-21 21:29:34 +0100687 if (mmci_validate_data(host, data))
688 return;
689
690 if (!mmci_dma_prep_next(host, data))
691 data->host_cookie = ++nd->cookie < 0 ? 1 : nd->cookie;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200692}
693
694static void mmci_post_request(struct mmc_host *mmc, struct mmc_request *mrq,
695 int err)
696{
697 struct mmci_host *host = mmc_priv(mmc);
698 struct mmc_data *data = mrq->data;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200699
Ulf Hansson653a7612013-01-21 21:29:34 +0100700 if (!data || !data->host_cookie)
Per Forlin58c7ccb2011-07-01 18:55:24 +0200701 return;
702
Ulf Hansson653a7612013-01-21 21:29:34 +0100703 mmci_dma_unmap(host, data);
Per Forlin58c7ccb2011-07-01 18:55:24 +0200704
Ulf Hansson653a7612013-01-21 21:29:34 +0100705 if (err) {
706 struct mmci_host_next *next = &host->next_data;
707 struct dma_chan *chan;
708 if (data->flags & MMC_DATA_READ)
709 chan = host->dma_rx_channel;
710 else
711 chan = host->dma_tx_channel;
712 dmaengine_terminate_all(chan);
Per Forlin58c7ccb2011-07-01 18:55:24 +0200713
Ulf Hansson653a7612013-01-21 21:29:34 +0100714 next->dma_desc = NULL;
715 next->dma_chan = NULL;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200716 }
717}
718
Russell Kingc8ebae32011-01-11 19:35:53 +0000719#else
720/* Blank functions if the DMA engine is not available */
Per Forlin58c7ccb2011-07-01 18:55:24 +0200721static void mmci_get_next_data(struct mmci_host *host, struct mmc_data *data)
722{
723}
Russell Kingc8ebae32011-01-11 19:35:53 +0000724static inline void mmci_dma_setup(struct mmci_host *host)
725{
726}
727
728static inline void mmci_dma_release(struct mmci_host *host)
729{
730}
731
732static inline void mmci_dma_unmap(struct mmci_host *host, struct mmc_data *data)
733{
734}
735
Ulf Hansson653a7612013-01-21 21:29:34 +0100736static inline void mmci_dma_finalize(struct mmci_host *host,
737 struct mmc_data *data)
738{
739}
740
Russell Kingc8ebae32011-01-11 19:35:53 +0000741static inline void mmci_dma_data_error(struct mmci_host *host)
742{
743}
744
745static inline int mmci_dma_start_data(struct mmci_host *host, unsigned int datactrl)
746{
747 return -ENOSYS;
748}
Per Forlin58c7ccb2011-07-01 18:55:24 +0200749
750#define mmci_pre_request NULL
751#define mmci_post_request NULL
752
Russell Kingc8ebae32011-01-11 19:35:53 +0000753#endif
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755static void mmci_start_data(struct mmci_host *host, struct mmc_data *data)
756{
Rabin Vincent8301bb62010-08-09 12:57:30 +0100757 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 unsigned int datactrl, timeout, irqmask;
Russell King7b09cda2005-07-01 12:02:59 +0100759 unsigned long long clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 void __iomem *base;
Russell King3bc87f22006-08-27 13:51:28 +0100761 int blksz_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Linus Walleij64de0282010-02-19 01:09:10 +0100763 dev_dbg(mmc_dev(host->mmc), "blksz %04x blks %04x flags %08x\n",
764 data->blksz, data->blocks, data->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 host->data = data;
Rabin Vincent528320d2010-07-21 12:49:49 +0100767 host->size = data->blksz * data->blocks;
Russell King51d43752011-01-27 10:56:52 +0000768 data->bytes_xfered = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Russell King7b09cda2005-07-01 12:02:59 +0100770 clks = (unsigned long long)data->timeout_ns * host->cclk;
Srinivas Kandagatlac4a35762014-06-02 10:08:39 +0100771 do_div(clks, NSEC_PER_SEC);
Russell King7b09cda2005-07-01 12:02:59 +0100772
773 timeout = data->timeout_clks + (unsigned int)clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 base = host->base;
776 writel(timeout, base + MMCIDATATIMER);
777 writel(host->size, base + MMCIDATALENGTH);
778
Russell King3bc87f22006-08-27 13:51:28 +0100779 blksz_bits = ffs(data->blksz) - 1;
780 BUG_ON(1 << blksz_bits != data->blksz);
781
Philippe Langlais1784b152011-03-25 08:51:52 +0100782 if (variant->blksz_datactrl16)
783 datactrl = MCI_DPSM_ENABLE | (data->blksz << 16);
Srinivas Kandagatlaff783232014-06-02 10:09:06 +0100784 else if (variant->blksz_datactrl4)
785 datactrl = MCI_DPSM_ENABLE | (data->blksz << 4);
Philippe Langlais1784b152011-03-25 08:51:52 +0100786 else
787 datactrl = MCI_DPSM_ENABLE | blksz_bits << 4;
Russell Kingc8ebae32011-01-11 19:35:53 +0000788
789 if (data->flags & MMC_DATA_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 datactrl |= MCI_DPSM_DIRECTION;
Russell Kingc8ebae32011-01-11 19:35:53 +0000791
Ulf Hansson7258db72011-12-13 17:05:28 +0100792 /* The ST Micro variants has a special bit to enable SDIO */
793 if (variant->sdio && host->mmc->card)
Ulf Hansson06c1a122012-10-12 14:01:50 +0100794 if (mmc_card_sdio(host->mmc->card)) {
795 /*
796 * The ST Micro variants has a special bit
797 * to enable SDIO.
798 */
799 u32 clk;
800
Ulf Hansson7258db72011-12-13 17:05:28 +0100801 datactrl |= MCI_ST_DPSM_SDIOEN;
802
Ulf Hansson06c1a122012-10-12 14:01:50 +0100803 /*
Ulf Hansson70ac0932012-10-12 14:07:36 +0100804 * The ST Micro variant for SDIO small write transfers
805 * needs to have clock H/W flow control disabled,
806 * otherwise the transfer will not start. The threshold
807 * depends on the rate of MCLK.
Ulf Hansson06c1a122012-10-12 14:01:50 +0100808 */
Ulf Hansson70ac0932012-10-12 14:07:36 +0100809 if (data->flags & MMC_DATA_WRITE &&
810 (host->size < 8 ||
811 (host->size <= 8 && host->mclk > 50000000)))
Ulf Hansson06c1a122012-10-12 14:01:50 +0100812 clk = host->clk_reg & ~variant->clkreg_enable;
813 else
814 clk = host->clk_reg | variant->clkreg_enable;
815
816 mmci_write_clkreg(host, clk);
817 }
818
Seungwon Jeon6dad6c92014-03-14 21:12:13 +0900819 if (host->mmc->ios.timing == MMC_TIMING_UHS_DDR50 ||
820 host->mmc->ios.timing == MMC_TIMING_MMC_DDR52)
Srinivas Kandagatlae17dca22014-06-02 10:09:15 +0100821 datactrl |= variant->datactrl_mask_ddrmode;
Ulf Hansson6dbb6ee2013-01-07 15:30:44 +0100822
Russell Kingc8ebae32011-01-11 19:35:53 +0000823 /*
824 * Attempt to use DMA operation mode, if this
825 * should fail, fall back to PIO mode
826 */
827 if (!mmci_dma_start_data(host, datactrl))
828 return;
829
830 /* IRQ mode, map the SG list for CPU reading/writing */
831 mmci_init_sg(host, data);
832
833 if (data->flags & MMC_DATA_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 irqmask = MCI_RXFIFOHALFFULLMASK;
Russell King0425a142006-02-16 16:48:31 +0000835
836 /*
Russell Kingc4d877c2011-01-27 09:50:13 +0000837 * If we have less than the fifo 'half-full' threshold to
838 * transfer, trigger a PIO interrupt as soon as any data
839 * is available.
Russell King0425a142006-02-16 16:48:31 +0000840 */
Russell Kingc4d877c2011-01-27 09:50:13 +0000841 if (host->size < variant->fifohalfsize)
Russell King0425a142006-02-16 16:48:31 +0000842 irqmask |= MCI_RXDATAAVLBLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 } else {
844 /*
845 * We don't actually need to include "FIFO empty" here
846 * since its implicit in "FIFO half empty".
847 */
848 irqmask = MCI_TXFIFOHALFEMPTYMASK;
849 }
850
Ulf Hansson9cc639a2013-05-15 20:48:23 +0100851 mmci_write_datactrlreg(host, datactrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 writel(readl(base + MMCIMASK0) & ~MCI_DATAENDMASK, base + MMCIMASK0);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100853 mmci_set_mask1(host, irqmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854}
855
856static void
857mmci_start_command(struct mmci_host *host, struct mmc_command *cmd, u32 c)
858{
859 void __iomem *base = host->base;
860
Linus Walleij64de0282010-02-19 01:09:10 +0100861 dev_dbg(mmc_dev(host->mmc), "op %02x arg %08x flags %08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 cmd->opcode, cmd->arg, cmd->flags);
863
864 if (readl(base + MMCICOMMAND) & MCI_CPSM_ENABLE) {
865 writel(0, base + MMCICOMMAND);
Srinivas Kandagatla6adb2a82014-06-02 10:08:57 +0100866 mmci_reg_delay(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 }
868
869 c |= cmd->opcode | MCI_CPSM_ENABLE;
Russell Kinge9225172006-02-02 12:23:12 +0000870 if (cmd->flags & MMC_RSP_PRESENT) {
871 if (cmd->flags & MMC_RSP_136)
872 c |= MCI_CPSM_LONGRSP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 c |= MCI_CPSM_RESPONSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
875 if (/*interrupt*/0)
876 c |= MCI_CPSM_INTERRUPT;
877
Srinivas Kandagatlaae7b0062014-06-02 10:09:39 +0100878 if (mmc_cmd_type(cmd) == MMC_CMD_ADTC)
879 c |= host->variant->data_cmd_enable;
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 host->cmd = cmd;
882
883 writel(cmd->arg, base + MMCIARGUMENT);
884 writel(c, base + MMCICOMMAND);
885}
886
887static void
888mmci_data_irq(struct mmci_host *host, struct mmc_data *data,
889 unsigned int status)
890{
Linus Walleijf20f8f212010-10-19 13:41:24 +0100891 /* First check for errors */
Ulf Hanssonb63038d2011-12-13 16:51:04 +0100892 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_STARTBITERR|
893 MCI_TXUNDERRUN|MCI_RXOVERRUN)) {
Linus Walleij8cb28152011-01-24 15:22:13 +0100894 u32 remain, success;
Linus Walleijf20f8f212010-10-19 13:41:24 +0100895
Russell Kingc8ebae32011-01-11 19:35:53 +0000896 /* Terminate the DMA transfer */
Ulf Hansson653a7612013-01-21 21:29:34 +0100897 if (dma_inprogress(host)) {
Russell Kingc8ebae32011-01-11 19:35:53 +0000898 mmci_dma_data_error(host);
Ulf Hansson653a7612013-01-21 21:29:34 +0100899 mmci_dma_unmap(host, data);
900 }
Russell Kingc8ebae32011-01-11 19:35:53 +0000901
Russell Kingc8afc9d2011-02-04 09:19:46 +0000902 /*
903 * Calculate how far we are into the transfer. Note that
904 * the data counter gives the number of bytes transferred
905 * on the MMC bus, not on the host side. On reads, this
906 * can be as much as a FIFO-worth of data ahead. This
907 * matters for FIFO overruns only.
908 */
Linus Walleijf5a106d2011-01-27 17:44:34 +0100909 remain = readl(host->base + MMCIDATACNT);
Linus Walleij8cb28152011-01-24 15:22:13 +0100910 success = data->blksz * data->blocks - remain;
911
Russell Kingc8afc9d2011-02-04 09:19:46 +0000912 dev_dbg(mmc_dev(host->mmc), "MCI ERROR IRQ, status 0x%08x at 0x%08x\n",
913 status, success);
Linus Walleij8cb28152011-01-24 15:22:13 +0100914 if (status & MCI_DATACRCFAIL) {
915 /* Last block was not successful */
Russell Kingc8afc9d2011-02-04 09:19:46 +0000916 success -= 1;
Pierre Ossman17b04292007-07-22 22:18:46 +0200917 data->error = -EILSEQ;
Linus Walleij8cb28152011-01-24 15:22:13 +0100918 } else if (status & MCI_DATATIMEOUT) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200919 data->error = -ETIMEDOUT;
Linus Walleij757df742011-06-30 15:10:21 +0100920 } else if (status & MCI_STARTBITERR) {
921 data->error = -ECOMM;
Russell Kingc8afc9d2011-02-04 09:19:46 +0000922 } else if (status & MCI_TXUNDERRUN) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200923 data->error = -EIO;
Russell Kingc8afc9d2011-02-04 09:19:46 +0000924 } else if (status & MCI_RXOVERRUN) {
925 if (success > host->variant->fifosize)
926 success -= host->variant->fifosize;
927 else
928 success = 0;
Linus Walleij8cb28152011-01-24 15:22:13 +0100929 data->error = -EIO;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100930 }
Russell King51d43752011-01-27 10:56:52 +0000931 data->bytes_xfered = round_down(success, data->blksz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
Linus Walleijf20f8f212010-10-19 13:41:24 +0100933
Linus Walleij8cb28152011-01-24 15:22:13 +0100934 if (status & MCI_DATABLOCKEND)
935 dev_err(mmc_dev(host->mmc), "stray MCI_DATABLOCKEND interrupt\n");
Linus Walleijf20f8f212010-10-19 13:41:24 +0100936
Russell Kingccff9b52011-01-30 21:03:50 +0000937 if (status & MCI_DATAEND || data->error) {
Russell Kingc8ebae32011-01-11 19:35:53 +0000938 if (dma_inprogress(host))
Ulf Hansson653a7612013-01-21 21:29:34 +0100939 mmci_dma_finalize(host, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 mmci_stop_data(host);
941
Linus Walleij8cb28152011-01-24 15:22:13 +0100942 if (!data->error)
943 /* The error clause is handled above, success! */
Russell King51d43752011-01-27 10:56:52 +0000944 data->bytes_xfered = data->blksz * data->blocks;
Linus Walleijf20f8f212010-10-19 13:41:24 +0100945
Ulf Hansson024629c2013-05-13 15:40:56 +0100946 if (!data->stop || host->mrq->sbc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 mmci_request_end(host, data->mrq);
948 } else {
949 mmci_start_command(host, data->stop, 0);
950 }
951 }
952}
953
954static void
955mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
956 unsigned int status)
957{
958 void __iomem *base = host->base;
Ulf Hansson024629c2013-05-13 15:40:56 +0100959 bool sbc = (cmd == host->mrq->sbc);
Ulf Hansson8d94b542014-01-13 16:49:31 +0100960 bool busy_resp = host->variant->busy_detect &&
961 (cmd->flags & MMC_RSP_BUSY);
962
963 /* Check if we need to wait for busy completion. */
964 if (host->busy_status && (status & MCI_ST_CARDBUSY))
965 return;
966
967 /* Enable busy completion if needed and supported. */
968 if (!host->busy_status && busy_resp &&
969 !(status & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT)) &&
970 (readl(base + MMCISTATUS) & MCI_ST_CARDBUSY)) {
971 writel(readl(base + MMCIMASK0) | MCI_ST_BUSYEND,
972 base + MMCIMASK0);
973 host->busy_status = status & (MCI_CMDSENT|MCI_CMDRESPEND);
974 return;
975 }
976
977 /* At busy completion, mask the IRQ and complete the request. */
978 if (host->busy_status) {
979 writel(readl(base + MMCIMASK0) & ~MCI_ST_BUSYEND,
980 base + MMCIMASK0);
981 host->busy_status = 0;
982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984 host->cmd = NULL;
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 if (status & MCI_CMDTIMEOUT) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200987 cmd->error = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200989 cmd->error = -EILSEQ;
Russell King - ARM Linux9047b432011-01-11 16:35:56 +0000990 } else {
991 cmd->resp[0] = readl(base + MMCIRESPONSE0);
992 cmd->resp[1] = readl(base + MMCIRESPONSE1);
993 cmd->resp[2] = readl(base + MMCIRESPONSE2);
994 cmd->resp[3] = readl(base + MMCIRESPONSE3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 }
996
Ulf Hansson024629c2013-05-13 15:40:56 +0100997 if ((!sbc && !cmd->data) || cmd->error) {
Ulf Hansson3b6e3c72011-12-13 16:58:43 +0100998 if (host->data) {
999 /* Terminate the DMA transfer */
Ulf Hansson653a7612013-01-21 21:29:34 +01001000 if (dma_inprogress(host)) {
Ulf Hansson3b6e3c72011-12-13 16:58:43 +01001001 mmci_dma_data_error(host);
Ulf Hansson653a7612013-01-21 21:29:34 +01001002 mmci_dma_unmap(host, host->data);
1003 }
Russell Kinge47c2222007-01-08 16:42:51 +00001004 mmci_stop_data(host);
Ulf Hansson3b6e3c72011-12-13 16:58:43 +01001005 }
Ulf Hansson024629c2013-05-13 15:40:56 +01001006 mmci_request_end(host, host->mrq);
1007 } else if (sbc) {
1008 mmci_start_command(host, host->mrq->cmd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 } else if (!(cmd->data->flags & MMC_DATA_READ)) {
1010 mmci_start_data(host, cmd->data);
1011 }
1012}
1013
Srinivas Kandagatla9c34b732014-06-02 10:10:04 +01001014static int mmci_get_rx_fifocnt(struct mmci_host *host, u32 status, int remain)
1015{
1016 return remain - (readl(host->base + MMCIFIFOCNT) << 2);
1017}
1018
1019static int mmci_qcom_get_rx_fifocnt(struct mmci_host *host, u32 status, int r)
1020{
1021 /*
1022 * on qcom SDCC4 only 8 words are used in each burst so only 8 addresses
1023 * from the fifo range should be used
1024 */
1025 if (status & MCI_RXFIFOHALFFULL)
1026 return host->variant->fifohalfsize;
1027 else if (status & MCI_RXDATAAVLBL)
1028 return 4;
1029
1030 return 0;
1031}
1032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033static int mmci_pio_read(struct mmci_host *host, char *buffer, unsigned int remain)
1034{
1035 void __iomem *base = host->base;
1036 char *ptr = buffer;
Srinivas Kandagatla9c34b732014-06-02 10:10:04 +01001037 u32 status = readl(host->base + MMCISTATUS);
Linus Walleij26eed9a2008-04-26 23:39:44 +01001038 int host_remain = host->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 do {
Srinivas Kandagatla9c34b732014-06-02 10:10:04 +01001041 int count = host->get_rx_fifocnt(host, status, host_remain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 if (count > remain)
1044 count = remain;
1045
1046 if (count <= 0)
1047 break;
1048
Ulf Hansson393e5e22011-12-13 17:08:04 +01001049 /*
1050 * SDIO especially may want to send something that is
1051 * not divisible by 4 (as opposed to card sectors
1052 * etc). Therefore make sure to always read the last bytes
1053 * while only doing full 32-bit reads towards the FIFO.
1054 */
1055 if (unlikely(count & 0x3)) {
1056 if (count < 4) {
1057 unsigned char buf[4];
Davide Ciminaghi4b85da02012-12-10 14:47:21 +01001058 ioread32_rep(base + MMCIFIFO, buf, 1);
Ulf Hansson393e5e22011-12-13 17:08:04 +01001059 memcpy(ptr, buf, count);
1060 } else {
Davide Ciminaghi4b85da02012-12-10 14:47:21 +01001061 ioread32_rep(base + MMCIFIFO, ptr, count >> 2);
Ulf Hansson393e5e22011-12-13 17:08:04 +01001062 count &= ~0x3;
1063 }
1064 } else {
Davide Ciminaghi4b85da02012-12-10 14:47:21 +01001065 ioread32_rep(base + MMCIFIFO, ptr, count >> 2);
Ulf Hansson393e5e22011-12-13 17:08:04 +01001066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068 ptr += count;
1069 remain -= count;
Linus Walleij26eed9a2008-04-26 23:39:44 +01001070 host_remain -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 if (remain == 0)
1073 break;
1074
1075 status = readl(base + MMCISTATUS);
1076 } while (status & MCI_RXDATAAVLBL);
1077
1078 return ptr - buffer;
1079}
1080
1081static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status)
1082{
Rabin Vincent8301bb62010-08-09 12:57:30 +01001083 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 void __iomem *base = host->base;
1085 char *ptr = buffer;
1086
1087 do {
1088 unsigned int count, maxcnt;
1089
Rabin Vincent8301bb62010-08-09 12:57:30 +01001090 maxcnt = status & MCI_TXFIFOEMPTY ?
1091 variant->fifosize : variant->fifohalfsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 count = min(remain, maxcnt);
1093
Linus Walleij34177802010-10-19 12:43:58 +01001094 /*
Linus Walleij34177802010-10-19 12:43:58 +01001095 * SDIO especially may want to send something that is
1096 * not divisible by 4 (as opposed to card sectors
1097 * etc), and the FIFO only accept full 32-bit writes.
1098 * So compensate by adding +3 on the count, a single
1099 * byte become a 32bit write, 7 bytes will be two
1100 * 32bit writes etc.
1101 */
Davide Ciminaghi4b85da02012-12-10 14:47:21 +01001102 iowrite32_rep(base + MMCIFIFO, ptr, (count + 3) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104 ptr += count;
1105 remain -= count;
1106
1107 if (remain == 0)
1108 break;
1109
1110 status = readl(base + MMCISTATUS);
1111 } while (status & MCI_TXFIFOHALFEMPTY);
1112
1113 return ptr - buffer;
1114}
1115
1116/*
1117 * PIO data transfer IRQ handler.
1118 */
David Howells7d12e782006-10-05 14:55:46 +01001119static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
1121 struct mmci_host *host = dev_id;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001122 struct sg_mapping_iter *sg_miter = &host->sg_miter;
Rabin Vincent8301bb62010-08-09 12:57:30 +01001123 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 void __iomem *base = host->base;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001125 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 u32 status;
1127
1128 status = readl(base + MMCISTATUS);
1129
Linus Walleij64de0282010-02-19 01:09:10 +01001130 dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001132 local_irq_save(flags);
1133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 unsigned int remain, len;
1136 char *buffer;
1137
1138 /*
1139 * For write, we only need to test the half-empty flag
1140 * here - if the FIFO is completely empty, then by
1141 * definition it is more than half empty.
1142 *
1143 * For read, check for data available.
1144 */
1145 if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL)))
1146 break;
1147
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001148 if (!sg_miter_next(sg_miter))
1149 break;
1150
1151 buffer = sg_miter->addr;
1152 remain = sg_miter->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
1154 len = 0;
1155 if (status & MCI_RXACTIVE)
1156 len = mmci_pio_read(host, buffer, remain);
1157 if (status & MCI_TXACTIVE)
1158 len = mmci_pio_write(host, buffer, remain, status);
1159
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001160 sg_miter->consumed = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 host->size -= len;
1163 remain -= len;
1164
1165 if (remain)
1166 break;
1167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 status = readl(base + MMCISTATUS);
1169 } while (1);
1170
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001171 sg_miter_stop(sg_miter);
1172
1173 local_irq_restore(flags);
1174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 /*
Russell Kingc4d877c2011-01-27 09:50:13 +00001176 * If we have less than the fifo 'half-full' threshold to transfer,
1177 * trigger a PIO interrupt as soon as any data is available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 */
Russell Kingc4d877c2011-01-27 09:50:13 +00001179 if (status & MCI_RXACTIVE && host->size < variant->fifohalfsize)
Linus Walleij2686b4b2010-10-19 12:39:48 +01001180 mmci_set_mask1(host, MCI_RXDATAAVLBLMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 /*
1183 * If we run out of data, disable the data IRQs; this
1184 * prevents a race where the FIFO becomes empty before
1185 * the chip itself has disabled the data path, and
1186 * stops us racing with our data end IRQ.
1187 */
1188 if (host->size == 0) {
Linus Walleij2686b4b2010-10-19 12:39:48 +01001189 mmci_set_mask1(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0);
1191 }
1192
1193 return IRQ_HANDLED;
1194}
1195
1196/*
1197 * Handle completion of command and data transfers.
1198 */
David Howells7d12e782006-10-05 14:55:46 +01001199static irqreturn_t mmci_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
1201 struct mmci_host *host = dev_id;
1202 u32 status;
1203 int ret = 0;
1204
1205 spin_lock(&host->lock);
1206
1207 do {
1208 struct mmc_command *cmd;
1209 struct mmc_data *data;
1210
1211 status = readl(host->base + MMCISTATUS);
Linus Walleij2686b4b2010-10-19 12:39:48 +01001212
1213 if (host->singleirq) {
1214 if (status & readl(host->base + MMCIMASK1))
1215 mmci_pio_irq(irq, dev_id);
1216
1217 status &= ~MCI_IRQ1MASK;
1218 }
1219
Ulf Hansson8d94b542014-01-13 16:49:31 +01001220 /*
1221 * We intentionally clear the MCI_ST_CARDBUSY IRQ here (if it's
1222 * enabled) since the HW seems to be triggering the IRQ on both
1223 * edges while monitoring DAT0 for busy completion.
1224 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 status &= readl(host->base + MMCIMASK0);
1226 writel(status, host->base + MMCICLEAR);
1227
Linus Walleij64de0282010-02-19 01:09:10 +01001228 dev_dbg(mmc_dev(host->mmc), "irq0 (data+cmd) %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Ulf Hanssone7f3d222014-01-10 14:51:42 +01001230 cmd = host->cmd;
Ulf Hansson8d94b542014-01-13 16:49:31 +01001231 if ((status|host->busy_status) & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT|
1232 MCI_CMDSENT|MCI_CMDRESPEND) && cmd)
Ulf Hanssone7f3d222014-01-10 14:51:42 +01001233 mmci_cmd_irq(host, cmd, status);
1234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 data = host->data;
Ulf Hanssonb63038d2011-12-13 16:51:04 +01001236 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_STARTBITERR|
1237 MCI_TXUNDERRUN|MCI_RXOVERRUN|MCI_DATAEND|
1238 MCI_DATABLOCKEND) && data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 mmci_data_irq(host, data, status);
1240
Ulf Hansson8d94b542014-01-13 16:49:31 +01001241 /* Don't poll for busy completion in irq context. */
1242 if (host->busy_status)
1243 status &= ~MCI_ST_CARDBUSY;
1244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 ret = 1;
1246 } while (status);
1247
1248 spin_unlock(&host->lock);
1249
1250 return IRQ_RETVAL(ret);
1251}
1252
1253static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1254{
1255 struct mmci_host *host = mmc_priv(mmc);
Linus Walleij9e943022008-10-24 21:17:50 +01001256 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258 WARN_ON(host->mrq != NULL);
1259
Ulf Hansson653a7612013-01-21 21:29:34 +01001260 mrq->cmd->error = mmci_validate_data(host, mrq->data);
1261 if (mrq->cmd->error) {
Pierre Ossman255d01a2007-07-24 20:38:53 +02001262 mmc_request_done(mmc, mrq);
1263 return;
1264 }
1265
Russell King1c3be362011-08-14 09:17:05 +01001266 pm_runtime_get_sync(mmc_dev(mmc));
1267
Linus Walleij9e943022008-10-24 21:17:50 +01001268 spin_lock_irqsave(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270 host->mrq = mrq;
1271
Per Forlin58c7ccb2011-07-01 18:55:24 +02001272 if (mrq->data)
1273 mmci_get_next_data(host, mrq->data);
1274
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 if (mrq->data && mrq->data->flags & MMC_DATA_READ)
1276 mmci_start_data(host, mrq->data);
1277
Ulf Hansson024629c2013-05-13 15:40:56 +01001278 if (mrq->sbc)
1279 mmci_start_command(host, mrq->sbc, 0);
1280 else
1281 mmci_start_command(host, mrq->cmd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Linus Walleij9e943022008-10-24 21:17:50 +01001283 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284}
1285
1286static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1287{
1288 struct mmci_host *host = mmc_priv(mmc);
Ulf Hansson7d72a1d2011-12-13 16:54:55 +01001289 struct variant_data *variant = host->variant;
Linus Walleija6a64642009-09-14 12:56:14 +01001290 u32 pwr = 0;
1291 unsigned long flags;
Lee Jonesdb90f912013-05-03 12:52:12 +01001292 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Ulf Hansson2cd976c2011-12-13 17:01:11 +01001294 pm_runtime_get_sync(mmc_dev(mmc));
1295
Ulf Hanssonbc521812011-12-13 16:57:55 +01001296 if (host->plat->ios_handler &&
1297 host->plat->ios_handler(mmc_dev(mmc), ios))
1298 dev_err(mmc_dev(mmc), "platform ios_handler failed\n");
1299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 switch (ios->power_mode) {
1301 case MMC_POWER_OFF:
Ulf Hansson599c1d52013-01-07 16:22:50 +01001302 if (!IS_ERR(mmc->supply.vmmc))
1303 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Lee Jones237fb5e2013-01-31 11:27:52 +00001304
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001305 if (!IS_ERR(mmc->supply.vqmmc) && host->vqmmc_enabled) {
Lee Jones237fb5e2013-01-31 11:27:52 +00001306 regulator_disable(mmc->supply.vqmmc);
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001307 host->vqmmc_enabled = false;
1308 }
Lee Jones237fb5e2013-01-31 11:27:52 +00001309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 break;
1311 case MMC_POWER_UP:
Ulf Hansson599c1d52013-01-07 16:22:50 +01001312 if (!IS_ERR(mmc->supply.vmmc))
1313 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd);
1314
Ulf Hansson7d72a1d2011-12-13 16:54:55 +01001315 /*
1316 * The ST Micro variant doesn't have the PL180s MCI_PWR_UP
1317 * and instead uses MCI_PWR_ON so apply whatever value is
1318 * configured in the variant data.
1319 */
1320 pwr |= variant->pwrreg_powerup;
1321
1322 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 case MMC_POWER_ON:
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001324 if (!IS_ERR(mmc->supply.vqmmc) && !host->vqmmc_enabled) {
Lee Jonesdb90f912013-05-03 12:52:12 +01001325 ret = regulator_enable(mmc->supply.vqmmc);
1326 if (ret < 0)
1327 dev_err(mmc_dev(mmc),
1328 "failed to enable vqmmc regulator\n");
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001329 else
1330 host->vqmmc_enabled = true;
Lee Jonesdb90f912013-05-03 12:52:12 +01001331 }
Lee Jones237fb5e2013-01-31 11:27:52 +00001332
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 pwr |= MCI_PWR_ON;
1334 break;
1335 }
1336
Ulf Hansson4d1a3a02011-12-13 16:57:07 +01001337 if (variant->signal_direction && ios->power_mode != MMC_POWER_OFF) {
1338 /*
1339 * The ST Micro variant has some additional bits
1340 * indicating signal direction for the signals in
1341 * the SD/MMC bus and feedback-clock usage.
1342 */
Ulf Hansson4593df22014-03-21 10:13:05 +01001343 pwr |= host->pwr_reg_add;
Ulf Hansson4d1a3a02011-12-13 16:57:07 +01001344
1345 if (ios->bus_width == MMC_BUS_WIDTH_4)
1346 pwr &= ~MCI_ST_DATA74DIREN;
1347 else if (ios->bus_width == MMC_BUS_WIDTH_1)
1348 pwr &= (~MCI_ST_DATA74DIREN &
1349 ~MCI_ST_DATA31DIREN &
1350 ~MCI_ST_DATA2DIREN);
1351 }
1352
Linus Walleijcc30d602009-01-04 15:18:54 +01001353 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN) {
Linus Walleijf17a1f02009-08-04 01:01:02 +01001354 if (host->hw_designer != AMBA_VENDOR_ST)
Linus Walleijcc30d602009-01-04 15:18:54 +01001355 pwr |= MCI_ROD;
1356 else {
1357 /*
1358 * The ST Micro variant use the ROD bit for something
1359 * else and only has OD (Open Drain).
1360 */
1361 pwr |= MCI_OD;
1362 }
1363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Ulf Hanssonf4670da2013-01-09 17:19:54 +01001365 /*
1366 * If clock = 0 and the variant requires the MMCIPOWER to be used for
1367 * gating the clock, the MCI_PWR_ON bit is cleared.
1368 */
1369 if (!ios->clock && variant->pwrreg_clkgate)
1370 pwr &= ~MCI_PWR_ON;
1371
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001372 if (host->variant->explicit_mclk_control &&
1373 ios->clock != host->clock_cache) {
1374 ret = clk_set_rate(host->clk, ios->clock);
1375 if (ret < 0)
1376 dev_err(mmc_dev(host->mmc),
1377 "Error setting clock rate (%d)\n", ret);
1378 else
1379 host->mclk = clk_get_rate(host->clk);
1380 }
1381 host->clock_cache = ios->clock;
1382
Linus Walleija6a64642009-09-14 12:56:14 +01001383 spin_lock_irqsave(&host->lock, flags);
1384
1385 mmci_set_clkreg(host, ios->clock);
Ulf Hansson7437cfa2012-01-18 09:17:27 +01001386 mmci_write_pwrreg(host, pwr);
Ulf Hanssonf829c042013-09-04 09:01:15 +01001387 mmci_reg_delay(host);
Linus Walleija6a64642009-09-14 12:56:14 +01001388
1389 spin_unlock_irqrestore(&host->lock, flags);
Ulf Hansson2cd976c2011-12-13 17:01:11 +01001390
Ulf Hansson2cd976c2011-12-13 17:01:11 +01001391 pm_runtime_mark_last_busy(mmc_dev(mmc));
1392 pm_runtime_put_autosuspend(mmc_dev(mmc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393}
1394
Russell King89001442009-07-09 15:16:07 +01001395static int mmci_get_cd(struct mmc_host *mmc)
1396{
1397 struct mmci_host *host = mmc_priv(mmc);
Rabin Vincent29719442010-08-09 12:54:43 +01001398 struct mmci_platform_data *plat = host->plat;
Ulf Hanssond2762092014-03-17 13:56:19 +01001399 unsigned int status = mmc_gpio_get_cd(mmc);
Russell King89001442009-07-09 15:16:07 +01001400
Ulf Hanssond2762092014-03-17 13:56:19 +01001401 if (status == -ENOSYS) {
Rabin Vincent4b8caec2010-08-09 12:56:40 +01001402 if (!plat->status)
1403 return 1; /* Assume always present */
1404
Rabin Vincent29719442010-08-09 12:54:43 +01001405 status = plat->status(mmc_dev(host->mmc));
Ulf Hanssond2762092014-03-17 13:56:19 +01001406 }
Russell King74bc8092010-07-29 15:58:59 +01001407 return status;
Russell King89001442009-07-09 15:16:07 +01001408}
1409
Ulf Hansson0f3ed7f2013-05-15 20:47:33 +01001410static int mmci_sig_volt_switch(struct mmc_host *mmc, struct mmc_ios *ios)
1411{
1412 int ret = 0;
1413
1414 if (!IS_ERR(mmc->supply.vqmmc)) {
1415
1416 pm_runtime_get_sync(mmc_dev(mmc));
1417
1418 switch (ios->signal_voltage) {
1419 case MMC_SIGNAL_VOLTAGE_330:
1420 ret = regulator_set_voltage(mmc->supply.vqmmc,
1421 2700000, 3600000);
1422 break;
1423 case MMC_SIGNAL_VOLTAGE_180:
1424 ret = regulator_set_voltage(mmc->supply.vqmmc,
1425 1700000, 1950000);
1426 break;
1427 case MMC_SIGNAL_VOLTAGE_120:
1428 ret = regulator_set_voltage(mmc->supply.vqmmc,
1429 1100000, 1300000);
1430 break;
1431 }
1432
1433 if (ret)
1434 dev_warn(mmc_dev(mmc), "Voltage switch failed\n");
1435
1436 pm_runtime_mark_last_busy(mmc_dev(mmc));
1437 pm_runtime_put_autosuspend(mmc_dev(mmc));
1438 }
1439
1440 return ret;
1441}
1442
Ulf Hansson01259622013-05-15 20:53:22 +01001443static struct mmc_host_ops mmci_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 .request = mmci_request,
Per Forlin58c7ccb2011-07-01 18:55:24 +02001445 .pre_req = mmci_pre_request,
1446 .post_req = mmci_post_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 .set_ios = mmci_set_ios,
Ulf Hanssond2762092014-03-17 13:56:19 +01001448 .get_ro = mmc_gpio_get_ro,
Russell King89001442009-07-09 15:16:07 +01001449 .get_cd = mmci_get_cd,
Ulf Hansson0f3ed7f2013-05-15 20:47:33 +01001450 .start_signal_voltage_switch = mmci_sig_volt_switch,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451};
1452
Ulf Hansson78f87df2014-03-17 15:53:07 +01001453static int mmci_of_parse(struct device_node *np, struct mmc_host *mmc)
1454{
Ulf Hansson4593df22014-03-21 10:13:05 +01001455 struct mmci_host *host = mmc_priv(mmc);
Ulf Hansson78f87df2014-03-17 15:53:07 +01001456 int ret = mmc_of_parse(mmc);
Lee Jones000bc9d2012-04-16 10:18:43 +01001457
Ulf Hansson78f87df2014-03-17 15:53:07 +01001458 if (ret)
1459 return ret;
Lee Jones000bc9d2012-04-16 10:18:43 +01001460
Ulf Hansson4593df22014-03-21 10:13:05 +01001461 if (of_get_property(np, "st,sig-dir-dat0", NULL))
1462 host->pwr_reg_add |= MCI_ST_DATA0DIREN;
1463 if (of_get_property(np, "st,sig-dir-dat2", NULL))
1464 host->pwr_reg_add |= MCI_ST_DATA2DIREN;
1465 if (of_get_property(np, "st,sig-dir-dat31", NULL))
1466 host->pwr_reg_add |= MCI_ST_DATA31DIREN;
1467 if (of_get_property(np, "st,sig-dir-dat74", NULL))
1468 host->pwr_reg_add |= MCI_ST_DATA74DIREN;
1469 if (of_get_property(np, "st,sig-dir-cmd", NULL))
1470 host->pwr_reg_add |= MCI_ST_CMDDIREN;
1471 if (of_get_property(np, "st,sig-pin-fbclk", NULL))
1472 host->pwr_reg_add |= MCI_ST_FBCLKEN;
1473
Lee Jones000bc9d2012-04-16 10:18:43 +01001474 if (of_get_property(np, "mmc-cap-mmc-highspeed", NULL))
Ulf Hansson78f87df2014-03-17 15:53:07 +01001475 mmc->caps |= MMC_CAP_MMC_HIGHSPEED;
Lee Jones000bc9d2012-04-16 10:18:43 +01001476 if (of_get_property(np, "mmc-cap-sd-highspeed", NULL))
Ulf Hansson78f87df2014-03-17 15:53:07 +01001477 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
Lee Jones000bc9d2012-04-16 10:18:43 +01001478
Ulf Hansson78f87df2014-03-17 15:53:07 +01001479 return 0;
Lee Jones000bc9d2012-04-16 10:18:43 +01001480}
Lee Jones000bc9d2012-04-16 10:18:43 +01001481
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001482static int mmci_probe(struct amba_device *dev,
Russell Kingaa25afa2011-02-19 15:55:00 +00001483 const struct amba_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
Linus Walleij6ef297f2009-09-22 14:29:36 +01001485 struct mmci_platform_data *plat = dev->dev.platform_data;
Lee Jones000bc9d2012-04-16 10:18:43 +01001486 struct device_node *np = dev->dev.of_node;
Rabin Vincent4956e102010-07-21 12:54:40 +01001487 struct variant_data *variant = id->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 struct mmci_host *host;
1489 struct mmc_host *mmc;
1490 int ret;
1491
Lee Jones000bc9d2012-04-16 10:18:43 +01001492 /* Must have platform data or Device Tree. */
1493 if (!plat && !np) {
1494 dev_err(&dev->dev, "No plat data or DT found\n");
1495 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 }
1497
Lee Jonesb9b52912012-06-12 10:49:51 +01001498 if (!plat) {
1499 plat = devm_kzalloc(&dev->dev, sizeof(*plat), GFP_KERNEL);
1500 if (!plat)
1501 return -ENOMEM;
1502 }
1503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
Ulf Hanssonef289982014-03-17 13:56:32 +01001505 if (!mmc)
1506 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Ulf Hansson78f87df2014-03-17 15:53:07 +01001508 ret = mmci_of_parse(np, mmc);
1509 if (ret)
1510 goto host_free;
1511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 host = mmc_priv(mmc);
Rabin Vincent4ea580f2009-04-17 08:44:19 +05301513 host->mmc = mmc;
Russell King012b7d32009-07-09 15:13:56 +01001514
1515 host->hw_designer = amba_manf(dev);
1516 host->hw_revision = amba_rev(dev);
Linus Walleij64de0282010-02-19 01:09:10 +01001517 dev_dbg(mmc_dev(mmc), "designer ID = 0x%02x\n", host->hw_designer);
1518 dev_dbg(mmc_dev(mmc), "revision = 0x%01x\n", host->hw_revision);
Russell King012b7d32009-07-09 15:13:56 +01001519
Ulf Hansson665ba562013-05-13 15:39:17 +01001520 host->clk = devm_clk_get(&dev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 if (IS_ERR(host->clk)) {
1522 ret = PTR_ERR(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 goto host_free;
1524 }
1525
Julia Lawallac940932012-08-26 16:00:59 +00001526 ret = clk_prepare_enable(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 if (ret)
Ulf Hansson665ba562013-05-13 15:39:17 +01001528 goto host_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Srinivas Kandagatla9c34b732014-06-02 10:10:04 +01001530 if (variant->qcom_fifo)
1531 host->get_rx_fifocnt = mmci_qcom_get_rx_fifocnt;
1532 else
1533 host->get_rx_fifocnt = mmci_get_rx_fifocnt;
1534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 host->plat = plat;
Rabin Vincent4956e102010-07-21 12:54:40 +01001536 host->variant = variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 host->mclk = clk_get_rate(host->clk);
Linus Walleijc8df9a52008-04-29 09:34:07 +01001538 /*
1539 * According to the spec, mclk is max 100 MHz,
1540 * so we try to adjust the clock down to this,
1541 * (if possible).
1542 */
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +01001543 if (host->mclk > variant->f_max) {
1544 ret = clk_set_rate(host->clk, variant->f_max);
Linus Walleijc8df9a52008-04-29 09:34:07 +01001545 if (ret < 0)
1546 goto clk_disable;
1547 host->mclk = clk_get_rate(host->clk);
Linus Walleij64de0282010-02-19 01:09:10 +01001548 dev_dbg(mmc_dev(mmc), "eventual mclk rate: %u Hz\n",
1549 host->mclk);
Linus Walleijc8df9a52008-04-29 09:34:07 +01001550 }
Ulf Hanssonef289982014-03-17 13:56:32 +01001551
Russell Kingc8ebae32011-01-11 19:35:53 +00001552 host->phybase = dev->res.start;
Ulf Hanssonef289982014-03-17 13:56:32 +01001553 host->base = devm_ioremap_resource(&dev->dev, &dev->res);
1554 if (IS_ERR(host->base)) {
1555 ret = PTR_ERR(host->base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 goto clk_disable;
1557 }
1558
Linus Walleij7f294e42011-07-08 09:57:15 +01001559 /*
1560 * The ARM and ST versions of the block have slightly different
1561 * clock divider equations which means that the minimum divider
1562 * differs too.
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001563 * on Qualcomm like controllers get the nearest minimum clock to 100Khz
Linus Walleij7f294e42011-07-08 09:57:15 +01001564 */
1565 if (variant->st_clkdiv)
1566 mmc->f_min = DIV_ROUND_UP(host->mclk, 257);
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001567 else if (variant->explicit_mclk_control)
1568 mmc->f_min = clk_round_rate(host->clk, 100000);
Linus Walleij7f294e42011-07-08 09:57:15 +01001569 else
1570 mmc->f_min = DIV_ROUND_UP(host->mclk, 512);
Linus Walleij808d97c2010-04-08 07:39:38 +01001571 /*
Ulf Hansson78f87df2014-03-17 15:53:07 +01001572 * If no maximum operating frequency is supplied, fall back to use
1573 * the module parameter, which has a (low) default value in case it
1574 * is not specified. Either value must not exceed the clock rate into
Ulf Hansson5080a082014-03-21 10:46:39 +01001575 * the block, of course.
Linus Walleij808d97c2010-04-08 07:39:38 +01001576 */
Ulf Hansson78f87df2014-03-17 15:53:07 +01001577 if (mmc->f_max)
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001578 mmc->f_max = variant->explicit_mclk_control ?
1579 min(variant->f_max, mmc->f_max) :
1580 min(host->mclk, mmc->f_max);
Linus Walleij808d97c2010-04-08 07:39:38 +01001581 else
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001582 mmc->f_max = variant->explicit_mclk_control ?
1583 fmax : min(host->mclk, fmax);
1584
1585
Linus Walleij64de0282010-02-19 01:09:10 +01001586 dev_dbg(mmc_dev(mmc), "clocking block at %u Hz\n", mmc->f_max);
1587
Ulf Hansson599c1d52013-01-07 16:22:50 +01001588 /* Get regulators and the supported OCR mask */
1589 mmc_regulator_get_supply(mmc);
1590 if (!mmc->ocr_avail)
Linus Walleij34e84f32009-09-22 14:41:40 +01001591 mmc->ocr_avail = plat->ocr_mask;
Ulf Hansson599c1d52013-01-07 16:22:50 +01001592 else if (plat->ocr_mask)
1593 dev_warn(mmc_dev(mmc), "Platform OCR mask is ignored\n");
1594
Ulf Hansson78f87df2014-03-17 15:53:07 +01001595 /* DT takes precedence over platform data. */
Ulf Hansson78f87df2014-03-17 15:53:07 +01001596 if (!np) {
1597 if (!plat->cd_invert)
1598 mmc->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
1599 mmc->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
1600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Ulf Hansson9dd8a8b2014-03-19 13:54:18 +01001602 /* We support these capabilities. */
1603 mmc->caps |= MMC_CAP_CMD23;
1604
Ulf Hansson8d94b542014-01-13 16:49:31 +01001605 if (variant->busy_detect) {
1606 mmci_ops.card_busy = mmci_card_busy;
1607 mmci_write_datactrlreg(host, MCI_ST_DPSM_BUSYMODE);
1608 mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY;
1609 mmc->max_busy_timeout = 0;
1610 }
1611
1612 mmc->ops = &mmci_ops;
1613
Ulf Hansson70be2082013-01-07 15:35:06 +01001614 /* We support these PM capabilities. */
Ulf Hansson78f87df2014-03-17 15:53:07 +01001615 mmc->pm_caps |= MMC_PM_KEEP_POWER;
Ulf Hansson70be2082013-01-07 15:35:06 +01001616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 /*
1618 * We can do SGIO
1619 */
Martin K. Petersena36274e2010-09-10 01:33:59 -04001620 mmc->max_segs = NR_SG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
1622 /*
Rabin Vincent08458ef2010-07-21 12:55:59 +01001623 * Since only a certain number of bits are valid in the data length
1624 * register, we must ensure that we don't exceed 2^num-1 bytes in a
1625 * single request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 */
Rabin Vincent08458ef2010-07-21 12:55:59 +01001627 mmc->max_req_size = (1 << variant->datalength_bits) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
1629 /*
1630 * Set the maximum segment size. Since we aren't doing DMA
1631 * (yet) we are only limited by the data length register.
1632 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001633 mmc->max_seg_size = mmc->max_req_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001635 /*
1636 * Block size can be up to 2048 bytes, but must be a power of two.
1637 */
Will Deacon8f7f6b7e2012-02-24 11:25:21 +00001638 mmc->max_blk_size = 1 << 11;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001639
Pierre Ossman55db8902006-11-21 17:55:45 +01001640 /*
Will Deacon8f7f6b7e2012-02-24 11:25:21 +00001641 * Limit the number of blocks transferred so that we don't overflow
1642 * the maximum request size.
Pierre Ossman55db8902006-11-21 17:55:45 +01001643 */
Will Deacon8f7f6b7e2012-02-24 11:25:21 +00001644 mmc->max_blk_count = mmc->max_req_size >> 11;
Pierre Ossman55db8902006-11-21 17:55:45 +01001645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 spin_lock_init(&host->lock);
1647
1648 writel(0, host->base + MMCIMASK0);
1649 writel(0, host->base + MMCIMASK1);
1650 writel(0xfff, host->base + MMCICLEAR);
1651
Ulf Hansson78f87df2014-03-17 15:53:07 +01001652 /* If DT, cd/wp gpios must be supplied through it. */
1653 if (!np && gpio_is_valid(plat->gpio_cd)) {
Ulf Hanssond2762092014-03-17 13:56:19 +01001654 ret = mmc_gpio_request_cd(mmc, plat->gpio_cd, 0);
1655 if (ret)
Ulf Hanssonef289982014-03-17 13:56:32 +01001656 goto clk_disable;
Russell King89001442009-07-09 15:16:07 +01001657 }
Ulf Hansson78f87df2014-03-17 15:53:07 +01001658 if (!np && gpio_is_valid(plat->gpio_wp)) {
Ulf Hanssond2762092014-03-17 13:56:19 +01001659 ret = mmc_gpio_request_ro(mmc, plat->gpio_wp);
1660 if (ret)
Ulf Hanssonef289982014-03-17 13:56:32 +01001661 goto clk_disable;
Russell King89001442009-07-09 15:16:07 +01001662 }
1663
Ulf Hanssonef289982014-03-17 13:56:32 +01001664 ret = devm_request_irq(&dev->dev, dev->irq[0], mmci_irq, IRQF_SHARED,
1665 DRIVER_NAME " (cmd)", host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 if (ret)
Ulf Hanssonef289982014-03-17 13:56:32 +01001667 goto clk_disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
Russell Kingdfb85182012-05-03 11:33:15 +01001669 if (!dev->irq[1])
Linus Walleij2686b4b2010-10-19 12:39:48 +01001670 host->singleirq = true;
1671 else {
Ulf Hanssonef289982014-03-17 13:56:32 +01001672 ret = devm_request_irq(&dev->dev, dev->irq[1], mmci_pio_irq,
1673 IRQF_SHARED, DRIVER_NAME " (pio)", host);
Linus Walleij2686b4b2010-10-19 12:39:48 +01001674 if (ret)
Ulf Hanssonef289982014-03-17 13:56:32 +01001675 goto clk_disable;
Linus Walleij2686b4b2010-10-19 12:39:48 +01001676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Linus Walleij8cb28152011-01-24 15:22:13 +01001678 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
1680 amba_set_drvdata(dev, mmc);
1681
Russell Kingc8ebae32011-01-11 19:35:53 +00001682 dev_info(&dev->dev, "%s: PL%03x manf %x rev%u at 0x%08llx irq %d,%d (pio)\n",
1683 mmc_hostname(mmc), amba_part(dev), amba_manf(dev),
1684 amba_rev(dev), (unsigned long long)dev->res.start,
1685 dev->irq[0], dev->irq[1]);
1686
1687 mmci_dma_setup(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Ulf Hansson2cd976c2011-12-13 17:01:11 +01001689 pm_runtime_set_autosuspend_delay(&dev->dev, 50);
1690 pm_runtime_use_autosuspend(&dev->dev);
Russell King1c3be362011-08-14 09:17:05 +01001691 pm_runtime_put(&dev->dev);
1692
Russell King8c11a942010-12-28 19:40:40 +00001693 mmc_add_host(mmc);
1694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 return 0;
1696
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 clk_disable:
Julia Lawallac940932012-08-26 16:00:59 +00001698 clk_disable_unprepare(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 host_free:
1700 mmc_free_host(mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 return ret;
1702}
1703
Bill Pemberton6e0ee712012-11-19 13:26:03 -05001704static int mmci_remove(struct amba_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705{
1706 struct mmc_host *mmc = amba_get_drvdata(dev);
1707
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 if (mmc) {
1709 struct mmci_host *host = mmc_priv(mmc);
1710
Russell King1c3be362011-08-14 09:17:05 +01001711 /*
1712 * Undo pm_runtime_put() in probe. We use the _sync
1713 * version here so that we can access the primecell.
1714 */
1715 pm_runtime_get_sync(&dev->dev);
1716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 mmc_remove_host(mmc);
1718
1719 writel(0, host->base + MMCIMASK0);
1720 writel(0, host->base + MMCIMASK1);
1721
1722 writel(0, host->base + MMCICOMMAND);
1723 writel(0, host->base + MMCIDATACTRL);
1724
Russell Kingc8ebae32011-01-11 19:35:53 +00001725 mmci_dma_release(host);
Julia Lawallac940932012-08-26 16:00:59 +00001726 clk_disable_unprepare(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 mmc_free_host(mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 }
1729
1730 return 0;
1731}
1732
Ulf Hansson571dce42014-01-23 00:38:00 +01001733#ifdef CONFIG_PM
Ulf Hansson1ff44432013-09-04 09:05:17 +01001734static void mmci_save(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 writel(0, host->base + MMCIMASK0);
1741 if (host->variant->pwrreg_nopower) {
Ulf Hansson1ff44432013-09-04 09:05:17 +01001742 writel(0, host->base + MMCIDATACTRL);
1743 writel(0, host->base + MMCIPOWER);
1744 writel(0, host->base + MMCICLOCK);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001745 }
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001746 mmci_reg_delay(host);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001747
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001748 spin_unlock_irqrestore(&host->lock, flags);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001749}
1750
1751static void mmci_restore(struct mmci_host *host)
1752{
1753 unsigned long flags;
1754
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001755 spin_lock_irqsave(&host->lock, flags);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001756
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001757 if (host->variant->pwrreg_nopower) {
Ulf Hansson1ff44432013-09-04 09:05:17 +01001758 writel(host->clk_reg, host->base + MMCICLOCK);
1759 writel(host->datactrl_reg, host->base + MMCIDATACTRL);
1760 writel(host->pwr_reg, host->base + MMCIPOWER);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001761 }
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001762 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
1763 mmci_reg_delay(host);
1764
1765 spin_unlock_irqrestore(&host->lock, flags);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001766}
1767
Ulf Hansson82592932013-01-09 11:15:26 +01001768static int mmci_runtime_suspend(struct device *dev)
1769{
1770 struct amba_device *adev = to_amba_device(dev);
1771 struct mmc_host *mmc = amba_get_drvdata(adev);
1772
1773 if (mmc) {
1774 struct mmci_host *host = mmc_priv(mmc);
Ulf Hanssone36bd9c62013-09-04 09:00:37 +01001775 pinctrl_pm_select_sleep_state(dev);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001776 mmci_save(host);
Ulf Hansson82592932013-01-09 11:15:26 +01001777 clk_disable_unprepare(host->clk);
1778 }
1779
1780 return 0;
1781}
1782
1783static int mmci_runtime_resume(struct device *dev)
1784{
1785 struct amba_device *adev = to_amba_device(dev);
1786 struct mmc_host *mmc = amba_get_drvdata(adev);
1787
1788 if (mmc) {
1789 struct mmci_host *host = mmc_priv(mmc);
1790 clk_prepare_enable(host->clk);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001791 mmci_restore(host);
Ulf Hanssone36bd9c62013-09-04 09:00:37 +01001792 pinctrl_pm_select_default_state(dev);
Ulf Hansson82592932013-01-09 11:15:26 +01001793 }
1794
1795 return 0;
1796}
1797#endif
1798
Ulf Hansson48fa7002011-12-13 16:59:34 +01001799static const struct dev_pm_ops mmci_dev_pm_ops = {
Ulf Hanssonf3737fa2014-01-23 01:11:33 +01001800 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1801 pm_runtime_force_resume)
Ulf Hansson571dce42014-01-23 00:38:00 +01001802 SET_PM_RUNTIME_PM_OPS(mmci_runtime_suspend, mmci_runtime_resume, NULL)
Ulf Hansson48fa7002011-12-13 16:59:34 +01001803};
1804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805static struct amba_id mmci_ids[] = {
1806 {
1807 .id = 0x00041180,
Pawel Moll768fbc12011-03-11 17:18:07 +00001808 .mask = 0xff0fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001809 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 },
1811 {
Pawel Moll768fbc12011-03-11 17:18:07 +00001812 .id = 0x01041180,
1813 .mask = 0xff0fffff,
1814 .data = &variant_arm_extended_fifo,
1815 },
1816 {
Pawel Moll3a372982013-01-24 14:12:45 +01001817 .id = 0x02041180,
1818 .mask = 0xff0fffff,
1819 .data = &variant_arm_extended_fifo_hwfc,
1820 },
1821 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 .id = 0x00041181,
1823 .mask = 0x000fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001824 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 },
Linus Walleijcc30d602009-01-04 15:18:54 +01001826 /* ST Micro variants */
1827 {
1828 .id = 0x00180180,
1829 .mask = 0x00ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001830 .data = &variant_u300,
Linus Walleijcc30d602009-01-04 15:18:54 +01001831 },
1832 {
Linus Walleij34fd4212012-04-10 17:43:59 +01001833 .id = 0x10180180,
1834 .mask = 0xf0ffffff,
1835 .data = &variant_nomadik,
1836 },
1837 {
Linus Walleijcc30d602009-01-04 15:18:54 +01001838 .id = 0x00280180,
1839 .mask = 0x00ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001840 .data = &variant_u300,
1841 },
1842 {
1843 .id = 0x00480180,
Philippe Langlais1784b152011-03-25 08:51:52 +01001844 .mask = 0xf0ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001845 .data = &variant_ux500,
Linus Walleijcc30d602009-01-04 15:18:54 +01001846 },
Philippe Langlais1784b152011-03-25 08:51:52 +01001847 {
1848 .id = 0x10480180,
1849 .mask = 0xf0ffffff,
1850 .data = &variant_ux500v2,
1851 },
Srinivas Kandagatla55b604a2014-06-02 10:10:13 +01001852 /* Qualcomm variants */
1853 {
1854 .id = 0x00051180,
1855 .mask = 0x000fffff,
1856 .data = &variant_qcom,
1857 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 { 0, 0 },
1859};
1860
Dave Martin9f998352011-10-05 15:15:21 +01001861MODULE_DEVICE_TABLE(amba, mmci_ids);
1862
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863static struct amba_driver mmci_driver = {
1864 .drv = {
1865 .name = DRIVER_NAME,
Ulf Hansson48fa7002011-12-13 16:59:34 +01001866 .pm = &mmci_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 },
1868 .probe = mmci_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05001869 .remove = mmci_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 .id_table = mmci_ids,
1871};
1872
viresh kumar9e5ed092012-03-15 10:40:38 +01001873module_amba_driver(mmci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875module_param(fmax, uint, 0444);
1876
1877MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
1878MODULE_LICENSE("GPL");