blob: 8d483ddb10b532dd919c1c830525c5b3547c7033 [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>
Linus Walleij9ef986a2018-09-20 16:01:10 -070031#include <linux/of.h>
Linus Walleij34e84f32009-09-22 14:41:40 +010032#include <linux/regulator/consumer.h>
Russell Kingc8ebae32011-01-11 19:35:53 +000033#include <linux/dmaengine.h>
34#include <linux/dma-mapping.h>
35#include <linux/amba/mmci.h>
Russell King1c3be362011-08-14 09:17:05 +010036#include <linux/pm_runtime.h>
Viresh Kumar258aea72012-02-01 16:12:19 +053037#include <linux/types.h>
Linus Walleija9a83782012-10-29 14:39:30 +010038#include <linux/pinctrl/consumer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Russell King7b09cda2005-07-01 12:02:59 +010040#include <asm/div64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include "mmci.h"
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +010044#include "mmci_qcom_dml.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#define DRIVER_NAME "mmci-pl18x"
47
Ludovic Barrec3647fd2018-10-08 14:08:33 +020048#ifdef CONFIG_DMA_ENGINE
49void mmci_variant_init(struct mmci_host *host);
50#else
51static inline void mmci_variant_init(struct mmci_host *host) {}
52#endif
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static unsigned int fmax = 515633;
55
Rabin Vincent4956e102010-07-21 12:54:40 +010056static struct variant_data variant_arm = {
Rabin Vincent8301bb62010-08-09 12:57:30 +010057 .fifosize = 16 * 4,
58 .fifohalfsize = 8 * 4,
Rabin Vincent08458ef2010-07-21 12:55:59 +010059 .datalength_bits = 16,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +010060 .pwrreg_powerup = MCI_PWR_UP,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +010061 .f_max = 100000000,
Ulf Hansson78782892014-06-13 13:21:38 +020062 .reversed_irq_handling = true,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +010063 .mmcimask1 = true,
Patrice Chotard7f7b5502018-01-18 15:34:18 +010064 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +010065 .opendrain = MCI_ROD,
Ludovic Barrec3647fd2018-10-08 14:08:33 +020066 .init = mmci_variant_init,
Rabin Vincent4956e102010-07-21 12:54:40 +010067};
68
Pawel Moll768fbc12011-03-11 17:18:07 +000069static struct variant_data variant_arm_extended_fifo = {
70 .fifosize = 128 * 4,
71 .fifohalfsize = 64 * 4,
72 .datalength_bits = 16,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +010073 .pwrreg_powerup = MCI_PWR_UP,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +010074 .f_max = 100000000,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +010075 .mmcimask1 = true,
Patrice Chotard7f7b5502018-01-18 15:34:18 +010076 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +010077 .opendrain = MCI_ROD,
Ludovic Barrec3647fd2018-10-08 14:08:33 +020078 .init = mmci_variant_init,
Pawel Moll768fbc12011-03-11 17:18:07 +000079};
80
Pawel Moll3a372982013-01-24 14:12:45 +010081static struct variant_data variant_arm_extended_fifo_hwfc = {
82 .fifosize = 128 * 4,
83 .fifohalfsize = 64 * 4,
84 .clkreg_enable = MCI_ARM_HWFCEN,
85 .datalength_bits = 16,
86 .pwrreg_powerup = MCI_PWR_UP,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +010087 .f_max = 100000000,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +010088 .mmcimask1 = true,
Patrice Chotard7f7b5502018-01-18 15:34:18 +010089 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +010090 .opendrain = MCI_ROD,
Ludovic Barrec3647fd2018-10-08 14:08:33 +020091 .init = mmci_variant_init,
Pawel Moll3a372982013-01-24 14:12:45 +010092};
93
Rabin Vincent4956e102010-07-21 12:54:40 +010094static struct variant_data variant_u300 = {
Rabin Vincent8301bb62010-08-09 12:57:30 +010095 .fifosize = 16 * 4,
96 .fifohalfsize = 8 * 4,
Linus Walleij49ac2152011-03-04 14:54:16 +010097 .clkreg_enable = MCI_ST_U300_HWFCEN,
Srinivas Kandagatlae1412d82014-06-02 10:09:23 +010098 .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
Rabin Vincent08458ef2010-07-21 12:55:59 +010099 .datalength_bits = 16,
Linus Walleij5db3eee2016-10-25 11:06:05 +0200100 .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
Srinivas Kandagatlac7354132014-08-22 05:55:16 +0100101 .st_sdio = true,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +0100102 .pwrreg_powerup = MCI_PWR_ON,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +0100103 .f_max = 100000000,
Ulf Hansson4d1a3a02011-12-13 16:57:07 +0100104 .signal_direction = true,
Ulf Hanssonf4670da2013-01-09 17:19:54 +0100105 .pwrreg_clkgate = true,
Ulf Hansson1ff44432013-09-04 09:05:17 +0100106 .pwrreg_nopower = true,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100107 .mmcimask1 = true,
Patrice Chotard7f7b5502018-01-18 15:34:18 +0100108 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +0100109 .opendrain = MCI_OD,
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200110 .init = mmci_variant_init,
Rabin Vincent4956e102010-07-21 12:54:40 +0100111};
112
Linus Walleij34fd4212012-04-10 17:43:59 +0100113static struct variant_data variant_nomadik = {
114 .fifosize = 16 * 4,
115 .fifohalfsize = 8 * 4,
116 .clkreg = MCI_CLK_ENABLE,
Linus Walleijf5abc762016-01-04 02:22:08 +0100117 .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
Linus Walleij34fd4212012-04-10 17:43:59 +0100118 .datalength_bits = 24,
Linus Walleij5db3eee2016-10-25 11:06:05 +0200119 .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
Srinivas Kandagatlac7354132014-08-22 05:55:16 +0100120 .st_sdio = true,
Linus Walleij34fd4212012-04-10 17:43:59 +0100121 .st_clkdiv = true,
122 .pwrreg_powerup = MCI_PWR_ON,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +0100123 .f_max = 100000000,
Linus Walleij34fd4212012-04-10 17:43:59 +0100124 .signal_direction = true,
Ulf Hanssonf4670da2013-01-09 17:19:54 +0100125 .pwrreg_clkgate = true,
Ulf Hansson1ff44432013-09-04 09:05:17 +0100126 .pwrreg_nopower = true,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100127 .mmcimask1 = true,
Patrice Chotard7f7b5502018-01-18 15:34:18 +0100128 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +0100129 .opendrain = MCI_OD,
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200130 .init = mmci_variant_init,
Linus Walleij34fd4212012-04-10 17:43:59 +0100131};
132
Rabin Vincent4956e102010-07-21 12:54:40 +0100133static struct variant_data variant_ux500 = {
Rabin Vincent8301bb62010-08-09 12:57:30 +0100134 .fifosize = 30 * 4,
135 .fifohalfsize = 8 * 4,
Rabin Vincent4956e102010-07-21 12:54:40 +0100136 .clkreg = MCI_CLK_ENABLE,
Linus Walleij49ac2152011-03-04 14:54:16 +0100137 .clkreg_enable = MCI_ST_UX500_HWFCEN,
Srinivas Kandagatlae1412d82014-06-02 10:09:23 +0100138 .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
Srinivas Kandagatlae8740642014-06-02 10:09:30 +0100139 .clkreg_neg_edge_enable = MCI_ST_UX500_NEG_EDGE,
Rabin Vincent08458ef2010-07-21 12:55:59 +0100140 .datalength_bits = 24,
Linus Walleij5db3eee2016-10-25 11:06:05 +0200141 .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
Srinivas Kandagatlac7354132014-08-22 05:55:16 +0100142 .st_sdio = true,
Linus Walleijb70a67f2010-12-06 09:24:14 +0100143 .st_clkdiv = true,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +0100144 .pwrreg_powerup = MCI_PWR_ON,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +0100145 .f_max = 100000000,
Ulf Hansson4d1a3a02011-12-13 16:57:07 +0100146 .signal_direction = true,
Ulf Hanssonf4670da2013-01-09 17:19:54 +0100147 .pwrreg_clkgate = true,
Ulf Hansson01259622013-05-15 20:53:22 +0100148 .busy_detect = true,
Linus Walleij49adc0c2016-10-25 11:06:06 +0200149 .busy_dpsm_flag = MCI_DPSM_ST_BUSYMODE,
150 .busy_detect_flag = MCI_ST_CARDBUSY,
151 .busy_detect_mask = MCI_ST_BUSYENDMASK,
Ulf Hansson1ff44432013-09-04 09:05:17 +0100152 .pwrreg_nopower = true,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100153 .mmcimask1 = true,
Patrice Chotard7f7b5502018-01-18 15:34:18 +0100154 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +0100155 .opendrain = MCI_OD,
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200156 .init = mmci_variant_init,
Rabin Vincent4956e102010-07-21 12:54:40 +0100157};
Linus Walleijb70a67f2010-12-06 09:24:14 +0100158
Philippe Langlais1784b152011-03-25 08:51:52 +0100159static struct variant_data variant_ux500v2 = {
160 .fifosize = 30 * 4,
161 .fifohalfsize = 8 * 4,
162 .clkreg = MCI_CLK_ENABLE,
163 .clkreg_enable = MCI_ST_UX500_HWFCEN,
Srinivas Kandagatlae1412d82014-06-02 10:09:23 +0100164 .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
Srinivas Kandagatlae8740642014-06-02 10:09:30 +0100165 .clkreg_neg_edge_enable = MCI_ST_UX500_NEG_EDGE,
Linus Walleij5db3eee2016-10-25 11:06:05 +0200166 .datactrl_mask_ddrmode = MCI_DPSM_ST_DDRMODE,
Philippe Langlais1784b152011-03-25 08:51:52 +0100167 .datalength_bits = 24,
Linus Walleij5db3eee2016-10-25 11:06:05 +0200168 .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
Srinivas Kandagatlac7354132014-08-22 05:55:16 +0100169 .st_sdio = true,
Philippe Langlais1784b152011-03-25 08:51:52 +0100170 .st_clkdiv = true,
171 .blksz_datactrl16 = true,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +0100172 .pwrreg_powerup = MCI_PWR_ON,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +0100173 .f_max = 100000000,
Ulf Hansson4d1a3a02011-12-13 16:57:07 +0100174 .signal_direction = true,
Ulf Hanssonf4670da2013-01-09 17:19:54 +0100175 .pwrreg_clkgate = true,
Ulf Hansson01259622013-05-15 20:53:22 +0100176 .busy_detect = true,
Linus Walleij49adc0c2016-10-25 11:06:06 +0200177 .busy_dpsm_flag = MCI_DPSM_ST_BUSYMODE,
178 .busy_detect_flag = MCI_ST_CARDBUSY,
179 .busy_detect_mask = MCI_ST_BUSYENDMASK,
Ulf Hansson1ff44432013-09-04 09:05:17 +0100180 .pwrreg_nopower = true,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100181 .mmcimask1 = true,
Patrice Chotard7f7b5502018-01-18 15:34:18 +0100182 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +0100183 .opendrain = MCI_OD,
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200184 .init = mmci_variant_init,
Philippe Langlais1784b152011-03-25 08:51:52 +0100185};
186
Patrice Chotard2a9d6c82018-01-18 15:34:21 +0100187static struct variant_data variant_stm32 = {
188 .fifosize = 32 * 4,
189 .fifohalfsize = 8 * 4,
190 .clkreg = MCI_CLK_ENABLE,
191 .clkreg_enable = MCI_ST_UX500_HWFCEN,
192 .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
193 .clkreg_neg_edge_enable = MCI_ST_UX500_NEG_EDGE,
194 .datalength_bits = 24,
195 .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
196 .st_sdio = true,
197 .st_clkdiv = true,
198 .pwrreg_powerup = MCI_PWR_ON,
199 .f_max = 48000000,
200 .pwrreg_clkgate = true,
201 .pwrreg_nopower = true,
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200202 .init = mmci_variant_init,
Patrice Chotard2a9d6c82018-01-18 15:34:21 +0100203};
204
Srinivas Kandagatla55b604a2014-06-02 10:10:13 +0100205static struct variant_data variant_qcom = {
206 .fifosize = 16 * 4,
207 .fifohalfsize = 8 * 4,
208 .clkreg = MCI_CLK_ENABLE,
209 .clkreg_enable = MCI_QCOM_CLK_FLOWENA |
210 MCI_QCOM_CLK_SELECT_IN_FBCLK,
211 .clkreg_8bit_bus_enable = MCI_QCOM_CLK_WIDEBUS_8,
212 .datactrl_mask_ddrmode = MCI_QCOM_CLK_SELECT_IN_DDR_MODE,
Linus Walleij5db3eee2016-10-25 11:06:05 +0200213 .data_cmd_enable = MCI_CPSM_QCOM_DATCMD,
Srinivas Kandagatla55b604a2014-06-02 10:10:13 +0100214 .blksz_datactrl4 = true,
215 .datalength_bits = 24,
216 .pwrreg_powerup = MCI_PWR_UP,
217 .f_max = 208000000,
218 .explicit_mclk_control = true,
219 .qcom_fifo = true,
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100220 .qcom_dml = true,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100221 .mmcimask1 = true,
Patrice Chotard7f7b5502018-01-18 15:34:18 +0100222 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +0100223 .opendrain = MCI_ROD,
Ulf Hansson29aba072018-07-16 13:08:18 +0200224 .init = qcom_variant_init,
Srinivas Kandagatla55b604a2014-06-02 10:10:13 +0100225};
226
Linus Walleij49adc0c2016-10-25 11:06:06 +0200227/* Busy detection for the ST Micro variant */
Ulf Hansson01259622013-05-15 20:53:22 +0100228static int mmci_card_busy(struct mmc_host *mmc)
229{
230 struct mmci_host *host = mmc_priv(mmc);
231 unsigned long flags;
232 int busy = 0;
233
Ulf Hansson01259622013-05-15 20:53:22 +0100234 spin_lock_irqsave(&host->lock, flags);
Linus Walleij49adc0c2016-10-25 11:06:06 +0200235 if (readl(host->base + MMCISTATUS) & host->variant->busy_detect_flag)
Ulf Hansson01259622013-05-15 20:53:22 +0100236 busy = 1;
237 spin_unlock_irqrestore(&host->lock, flags);
238
Ulf Hansson01259622013-05-15 20:53:22 +0100239 return busy;
240}
241
Linus Walleija6a64642009-09-14 12:56:14 +0100242/*
Ulf Hansson653a7612013-01-21 21:29:34 +0100243 * Validate mmc prerequisites
244 */
245static int mmci_validate_data(struct mmci_host *host,
246 struct mmc_data *data)
247{
248 if (!data)
249 return 0;
250
251 if (!is_power_of_2(data->blksz)) {
252 dev_err(mmc_dev(host->mmc),
253 "unsupported block size (%d bytes)\n", data->blksz);
254 return -EINVAL;
255 }
256
257 return 0;
258}
259
Ulf Hanssonf829c042013-09-04 09:01:15 +0100260static void mmci_reg_delay(struct mmci_host *host)
261{
262 /*
263 * According to the spec, at least three feedback clock cycles
264 * of max 52 MHz must pass between two writes to the MMCICLOCK reg.
265 * Three MCLK clock cycles must pass between two MMCIPOWER reg writes.
266 * Worst delay time during card init is at 100 kHz => 30 us.
267 * Worst delay time when up and running is at 25 MHz => 120 ns.
268 */
269 if (host->cclk < 25000000)
270 udelay(30);
271 else
272 ndelay(120);
273}
274
Ulf Hansson653a7612013-01-21 21:29:34 +0100275/*
Linus Walleija6a64642009-09-14 12:56:14 +0100276 * This must be called with host->lock held
277 */
Ulf Hansson7437cfa2012-01-18 09:17:27 +0100278static void mmci_write_clkreg(struct mmci_host *host, u32 clk)
279{
280 if (host->clk_reg != clk) {
281 host->clk_reg = clk;
282 writel(clk, host->base + MMCICLOCK);
283 }
284}
285
286/*
287 * This must be called with host->lock held
288 */
289static void mmci_write_pwrreg(struct mmci_host *host, u32 pwr)
290{
291 if (host->pwr_reg != pwr) {
292 host->pwr_reg = pwr;
293 writel(pwr, host->base + MMCIPOWER);
294 }
295}
296
297/*
298 * This must be called with host->lock held
299 */
Ulf Hansson9cc639a2013-05-15 20:48:23 +0100300static void mmci_write_datactrlreg(struct mmci_host *host, u32 datactrl)
301{
Linus Walleij49adc0c2016-10-25 11:06:06 +0200302 /* Keep busy mode in DPSM if enabled */
303 datactrl |= host->datactrl_reg & host->variant->busy_dpsm_flag;
Ulf Hansson01259622013-05-15 20:53:22 +0100304
Ulf Hansson9cc639a2013-05-15 20:48:23 +0100305 if (host->datactrl_reg != datactrl) {
306 host->datactrl_reg = datactrl;
307 writel(datactrl, host->base + MMCIDATACTRL);
308 }
309}
310
311/*
312 * This must be called with host->lock held
313 */
Linus Walleija6a64642009-09-14 12:56:14 +0100314static void mmci_set_clkreg(struct mmci_host *host, unsigned int desired)
315{
Rabin Vincent4956e102010-07-21 12:54:40 +0100316 struct variant_data *variant = host->variant;
317 u32 clk = variant->clkreg;
Linus Walleija6a64642009-09-14 12:56:14 +0100318
Ulf Hanssonc58a8502013-05-13 15:40:03 +0100319 /* Make sure cclk reflects the current calculated clock */
320 host->cclk = 0;
321
Linus Walleija6a64642009-09-14 12:56:14 +0100322 if (desired) {
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +0100323 if (variant->explicit_mclk_control) {
324 host->cclk = host->mclk;
325 } else if (desired >= host->mclk) {
Linus Walleij991a86e2010-12-10 09:35:53 +0100326 clk = MCI_CLK_BYPASS;
Linus Walleij399bc482011-04-01 07:59:17 +0100327 if (variant->st_clkdiv)
328 clk |= MCI_ST_UX500_NEG_EDGE;
Linus Walleija6a64642009-09-14 12:56:14 +0100329 host->cclk = host->mclk;
Linus Walleijb70a67f2010-12-06 09:24:14 +0100330 } else if (variant->st_clkdiv) {
331 /*
332 * DB8500 TRM says f = mclk / (clkdiv + 2)
333 * => clkdiv = (mclk / f) - 2
334 * Round the divider up so we don't exceed the max
335 * frequency
336 */
337 clk = DIV_ROUND_UP(host->mclk, desired) - 2;
338 if (clk >= 256)
339 clk = 255;
340 host->cclk = host->mclk / (clk + 2);
Linus Walleija6a64642009-09-14 12:56:14 +0100341 } else {
Linus Walleijb70a67f2010-12-06 09:24:14 +0100342 /*
343 * PL180 TRM says f = mclk / (2 * (clkdiv + 1))
344 * => clkdiv = mclk / (2 * f) - 1
345 */
Linus Walleija6a64642009-09-14 12:56:14 +0100346 clk = host->mclk / (2 * desired) - 1;
347 if (clk >= 256)
348 clk = 255;
349 host->cclk = host->mclk / (2 * (clk + 1));
350 }
Rabin Vincent4380c142010-07-21 12:55:18 +0100351
352 clk |= variant->clkreg_enable;
Linus Walleija6a64642009-09-14 12:56:14 +0100353 clk |= MCI_CLK_ENABLE;
354 /* This hasn't proven to be worthwhile */
355 /* clk |= MCI_CLK_PWRSAVE; */
356 }
357
Ulf Hanssonc58a8502013-05-13 15:40:03 +0100358 /* Set actual clock for debug */
359 host->mmc->actual_clock = host->cclk;
360
Linus Walleij9e6c82c2009-09-14 12:57:11 +0100361 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4)
Linus Walleij771dc152010-04-08 07:38:52 +0100362 clk |= MCI_4BIT_BUS;
363 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_8)
Srinivas Kandagatlae1412d82014-06-02 10:09:23 +0100364 clk |= variant->clkreg_8bit_bus_enable;
Linus Walleij9e6c82c2009-09-14 12:57:11 +0100365
Seungwon Jeon6dad6c92014-03-14 21:12:13 +0900366 if (host->mmc->ios.timing == MMC_TIMING_UHS_DDR50 ||
367 host->mmc->ios.timing == MMC_TIMING_MMC_DDR52)
Srinivas Kandagatlae8740642014-06-02 10:09:30 +0100368 clk |= variant->clkreg_neg_edge_enable;
Ulf Hansson6dbb6ee2013-01-07 15:30:44 +0100369
Ulf Hansson7437cfa2012-01-18 09:17:27 +0100370 mmci_write_clkreg(host, clk);
Linus Walleija6a64642009-09-14 12:56:14 +0100371}
372
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200373void mmci_dma_release(struct mmci_host *host)
374{
375 if (host->ops && host->ops->dma_release)
376 host->ops->dma_release(host);
377
378 host->use_dma = false;
379}
380
381void mmci_dma_setup(struct mmci_host *host)
382{
383 if (!host->ops || !host->ops->dma_setup)
384 return;
385
386 if (host->ops->dma_setup(host))
387 return;
388
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200389 /* initialize pre request cookie */
390 host->next_cookie = 1;
391
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200392 host->use_dma = true;
393}
394
Ludovic Barre47983512018-10-08 14:08:36 +0200395int mmci_prep_data(struct mmci_host *host, struct mmc_data *data, bool next)
396{
397 int err;
398
399 if (!host->ops || !host->ops->prep_data)
400 return 0;
401
402 err = host->ops->prep_data(host, data, next);
403
404 if (next && !err)
405 data->host_cookie = ++host->next_cookie < 0 ?
406 1 : host->next_cookie;
407
408 return err;
409}
410
411void mmci_unprep_data(struct mmci_host *host, struct mmc_data *data,
412 int err)
413{
414 if (host->ops && host->ops->unprep_data)
415 host->ops->unprep_data(host, data, err);
416
417 data->host_cookie = 0;
418}
419
Ludovic Barre02769962018-10-08 14:08:37 +0200420void mmci_get_next_data(struct mmci_host *host, struct mmc_data *data)
421{
422 WARN_ON(data->host_cookie && data->host_cookie != host->next_cookie);
423
424 if (host->ops && host->ops->get_next_data)
425 host->ops->get_next_data(host, data);
426}
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428static void
429mmci_request_end(struct mmci_host *host, struct mmc_request *mrq)
430{
431 writel(0, host->base + MMCICOMMAND);
432
Russell Kinge47c2222007-01-08 16:42:51 +0000433 BUG_ON(host->data);
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 host->mrq = NULL;
436 host->cmd = NULL;
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 mmc_request_done(host->mmc, mrq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440
Linus Walleij2686b4b2010-10-19 12:39:48 +0100441static void mmci_set_mask1(struct mmci_host *host, unsigned int mask)
442{
443 void __iomem *base = host->base;
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100444 struct variant_data *variant = host->variant;
Linus Walleij2686b4b2010-10-19 12:39:48 +0100445
446 if (host->singleirq) {
447 unsigned int mask0 = readl(base + MMCIMASK0);
448
449 mask0 &= ~MCI_IRQ1MASK;
450 mask0 |= mask;
451
452 writel(mask0, base + MMCIMASK0);
453 }
454
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100455 if (variant->mmcimask1)
456 writel(mask, base + MMCIMASK1);
457
458 host->mask1_reg = mask;
Linus Walleij2686b4b2010-10-19 12:39:48 +0100459}
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461static void mmci_stop_data(struct mmci_host *host)
462{
Ulf Hansson9cc639a2013-05-15 20:48:23 +0100463 mmci_write_datactrlreg(host, 0);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100464 mmci_set_mask1(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 host->data = NULL;
466}
467
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100468static void mmci_init_sg(struct mmci_host *host, struct mmc_data *data)
469{
470 unsigned int flags = SG_MITER_ATOMIC;
471
472 if (data->flags & MMC_DATA_READ)
473 flags |= SG_MITER_TO_SG;
474 else
475 flags |= SG_MITER_FROM_SG;
476
477 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
478}
479
Russell Kingc8ebae32011-01-11 19:35:53 +0000480/*
481 * All the DMA operation mode stuff goes inside this ifdef.
482 * This assumes that you have a generic DMA device interface,
483 * no custom DMA interfaces are supported.
484 */
485#ifdef CONFIG_DMA_ENGINE
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200486struct mmci_dmae_next {
487 struct dma_async_tx_descriptor *desc;
488 struct dma_chan *chan;
489};
490
491struct mmci_dmae_priv {
492 struct dma_chan *cur;
493 struct dma_chan *rx_channel;
494 struct dma_chan *tx_channel;
495 struct dma_async_tx_descriptor *desc_current;
496 struct mmci_dmae_next next_data;
497};
498
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200499int mmci_dmae_setup(struct mmci_host *host)
Russell Kingc8ebae32011-01-11 19:35:53 +0000500{
Russell Kingc8ebae32011-01-11 19:35:53 +0000501 const char *rxname, *txname;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200502 struct mmci_dmae_priv *dmae;
Russell Kingc8ebae32011-01-11 19:35:53 +0000503
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200504 dmae = devm_kzalloc(mmc_dev(host->mmc), sizeof(*dmae), GFP_KERNEL);
505 if (!dmae)
506 return -ENOMEM;
Russell Kingc8ebae32011-01-11 19:35:53 +0000507
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200508 host->dma_priv = dmae;
509
510 dmae->rx_channel = dma_request_slave_channel(mmc_dev(host->mmc),
511 "rx");
512 dmae->tx_channel = dma_request_slave_channel(mmc_dev(host->mmc),
513 "tx");
Per Forlin58c7ccb2011-07-01 18:55:24 +0200514
Russell Kingc8ebae32011-01-11 19:35:53 +0000515 /*
516 * If only an RX channel is specified, the driver will
517 * attempt to use it bidirectionally, however if it is
518 * is specified but cannot be located, DMA will be disabled.
519 */
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200520 if (dmae->rx_channel && !dmae->tx_channel)
521 dmae->tx_channel = dmae->rx_channel;
Russell Kingc8ebae32011-01-11 19:35:53 +0000522
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200523 if (dmae->rx_channel)
524 rxname = dma_chan_name(dmae->rx_channel);
Russell Kingc8ebae32011-01-11 19:35:53 +0000525 else
526 rxname = "none";
527
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200528 if (dmae->tx_channel)
529 txname = dma_chan_name(dmae->tx_channel);
Russell Kingc8ebae32011-01-11 19:35:53 +0000530 else
531 txname = "none";
532
533 dev_info(mmc_dev(host->mmc), "DMA channels RX %s, TX %s\n",
534 rxname, txname);
535
536 /*
537 * Limit the maximum segment size in any SG entry according to
538 * the parameters of the DMA engine device.
539 */
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200540 if (dmae->tx_channel) {
541 struct device *dev = dmae->tx_channel->device->dev;
Russell Kingc8ebae32011-01-11 19:35:53 +0000542 unsigned int max_seg_size = dma_get_max_seg_size(dev);
543
544 if (max_seg_size < host->mmc->max_seg_size)
545 host->mmc->max_seg_size = max_seg_size;
546 }
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200547 if (dmae->rx_channel) {
548 struct device *dev = dmae->rx_channel->device->dev;
Russell Kingc8ebae32011-01-11 19:35:53 +0000549 unsigned int max_seg_size = dma_get_max_seg_size(dev);
550
551 if (max_seg_size < host->mmc->max_seg_size)
552 host->mmc->max_seg_size = max_seg_size;
553 }
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100554
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200555 if (!dmae->tx_channel || !dmae->rx_channel) {
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200556 mmci_dmae_release(host);
557 return -EINVAL;
558 }
559
560 return 0;
Russell Kingc8ebae32011-01-11 19:35:53 +0000561}
562
563/*
Bill Pemberton6e0ee712012-11-19 13:26:03 -0500564 * This is used in or so inline it
Russell Kingc8ebae32011-01-11 19:35:53 +0000565 * so it can be discarded.
566 */
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200567void mmci_dmae_release(struct mmci_host *host)
Russell Kingc8ebae32011-01-11 19:35:53 +0000568{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200569 struct mmci_dmae_priv *dmae = host->dma_priv;
570
571 if (dmae->rx_channel)
572 dma_release_channel(dmae->rx_channel);
573 if (dmae->tx_channel)
574 dma_release_channel(dmae->tx_channel);
575 dmae->rx_channel = dmae->tx_channel = NULL;
Russell Kingc8ebae32011-01-11 19:35:53 +0000576}
577
578static void mmci_dma_unmap(struct mmci_host *host, struct mmc_data *data)
579{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200580 struct mmci_dmae_priv *dmae = host->dma_priv;
Ulf Hansson653a7612013-01-21 21:29:34 +0100581 struct dma_chan *chan;
Ulf Hansson653a7612013-01-21 21:29:34 +0100582
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200583 if (data->flags & MMC_DATA_READ)
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200584 chan = dmae->rx_channel;
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200585 else
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200586 chan = dmae->tx_channel;
Ulf Hansson653a7612013-01-21 21:29:34 +0100587
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200588 dma_unmap_sg(chan->device->dev, data->sg, data->sg_len,
589 mmc_get_dma_dir(data));
Ulf Hansson653a7612013-01-21 21:29:34 +0100590}
591
Ludovic Barre7b2a6d52018-09-21 11:45:55 +0200592static void mmci_dma_data_error(struct mmci_host *host)
593{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200594 struct mmci_dmae_priv *dmae = host->dma_priv;
595
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200596 if (!host->use_dma || !dma_inprogress(host))
Ludovic Barrecdea1942018-09-21 11:45:56 +0200597 return;
598
Ludovic Barre7b2a6d52018-09-21 11:45:55 +0200599 dev_err(mmc_dev(host->mmc), "error during DMA transfer!\n");
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200600 dmaengine_terminate_all(dmae->cur);
Ludovic Barre7b2a6d52018-09-21 11:45:55 +0200601 host->dma_in_progress = false;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200602 dmae->cur = NULL;
603 dmae->desc_current = NULL;
Ludovic Barre7b2a6d52018-09-21 11:45:55 +0200604 host->data->host_cookie = 0;
605
606 mmci_dma_unmap(host, host->data);
607}
608
Ulf Hansson653a7612013-01-21 21:29:34 +0100609static void mmci_dma_finalize(struct mmci_host *host, struct mmc_data *data)
610{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200611 struct mmci_dmae_priv *dmae = host->dma_priv;
Russell Kingc8ebae32011-01-11 19:35:53 +0000612 u32 status;
613 int i;
614
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200615 if (!host->use_dma || !dma_inprogress(host))
Ludovic Barrecdea1942018-09-21 11:45:56 +0200616 return;
617
Russell Kingc8ebae32011-01-11 19:35:53 +0000618 /* Wait up to 1ms for the DMA to complete */
619 for (i = 0; ; i++) {
620 status = readl(host->base + MMCISTATUS);
621 if (!(status & MCI_RXDATAAVLBLMASK) || i >= 100)
622 break;
623 udelay(10);
624 }
625
626 /*
627 * Check to see whether we still have some data left in the FIFO -
628 * this catches DMA controllers which are unable to monitor the
629 * DMALBREQ and DMALSREQ signals while allowing us to DMA to non-
630 * contiguous buffers. On TX, we'll get a FIFO underrun error.
631 */
632 if (status & MCI_RXDATAAVLBLMASK) {
Ulf Hansson653a7612013-01-21 21:29:34 +0100633 mmci_dma_data_error(host);
Russell Kingc8ebae32011-01-11 19:35:53 +0000634 if (!data->error)
635 data->error = -EIO;
Ludovic Barre7b2a6d52018-09-21 11:45:55 +0200636 } else if (!data->host_cookie) {
Ulf Hansson653a7612013-01-21 21:29:34 +0100637 mmci_dma_unmap(host, data);
Ludovic Barre7b2a6d52018-09-21 11:45:55 +0200638 }
Russell Kingc8ebae32011-01-11 19:35:53 +0000639
640 /*
641 * Use of DMA with scatter-gather is impossible.
642 * Give up with DMA and switch back to PIO mode.
643 */
644 if (status & MCI_RXDATAAVLBLMASK) {
645 dev_err(mmc_dev(host->mmc), "buggy DMA detected. Taking evasive action.\n");
646 mmci_dma_release(host);
647 }
Ulf Hansson653a7612013-01-21 21:29:34 +0100648
Linus Walleije13934b2017-01-27 15:04:54 +0100649 host->dma_in_progress = false;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200650 dmae->cur = NULL;
651 dmae->desc_current = NULL;
Russell Kingc8ebae32011-01-11 19:35:53 +0000652}
653
Ulf Hansson653a7612013-01-21 21:29:34 +0100654/* prepares DMA channel and DMA descriptor, returns non-zero on failure */
Ludovic Barre47983512018-10-08 14:08:36 +0200655static int _mmci_dmae_prep_data(struct mmci_host *host, struct mmc_data *data,
Ulf Hansson653a7612013-01-21 21:29:34 +0100656 struct dma_chan **dma_chan,
657 struct dma_async_tx_descriptor **dma_desc)
Russell Kingc8ebae32011-01-11 19:35:53 +0000658{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200659 struct mmci_dmae_priv *dmae = host->dma_priv;
Russell Kingc8ebae32011-01-11 19:35:53 +0000660 struct variant_data *variant = host->variant;
661 struct dma_slave_config conf = {
662 .src_addr = host->phybase + MMCIFIFO,
663 .dst_addr = host->phybase + MMCIFIFO,
664 .src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
665 .dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
666 .src_maxburst = variant->fifohalfsize >> 2, /* # of words */
667 .dst_maxburst = variant->fifohalfsize >> 2, /* # of words */
Viresh Kumar258aea72012-02-01 16:12:19 +0530668 .device_fc = false,
Russell Kingc8ebae32011-01-11 19:35:53 +0000669 };
Russell Kingc8ebae32011-01-11 19:35:53 +0000670 struct dma_chan *chan;
671 struct dma_device *device;
672 struct dma_async_tx_descriptor *desc;
673 int nr_sg;
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100674 unsigned long flags = DMA_CTRL_ACK;
Russell Kingc8ebae32011-01-11 19:35:53 +0000675
Russell Kingc8ebae32011-01-11 19:35:53 +0000676 if (data->flags & MMC_DATA_READ) {
Vinod Koul05f57992011-10-14 10:45:11 +0530677 conf.direction = DMA_DEV_TO_MEM;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200678 chan = dmae->rx_channel;
Russell Kingc8ebae32011-01-11 19:35:53 +0000679 } else {
Vinod Koul05f57992011-10-14 10:45:11 +0530680 conf.direction = DMA_MEM_TO_DEV;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200681 chan = dmae->tx_channel;
Russell Kingc8ebae32011-01-11 19:35:53 +0000682 }
683
684 /* If there's no DMA channel, fall back to PIO */
685 if (!chan)
686 return -EINVAL;
687
688 /* If less than or equal to the fifo size, don't bother with DMA */
Per Forlin58c7ccb2011-07-01 18:55:24 +0200689 if (data->blksz * data->blocks <= variant->fifosize)
Russell Kingc8ebae32011-01-11 19:35:53 +0000690 return -EINVAL;
691
692 device = chan->device;
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200693 nr_sg = dma_map_sg(device->dev, data->sg, data->sg_len,
694 mmc_get_dma_dir(data));
Russell Kingc8ebae32011-01-11 19:35:53 +0000695 if (nr_sg == 0)
696 return -EINVAL;
697
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100698 if (host->variant->qcom_dml)
699 flags |= DMA_PREP_INTERRUPT;
700
Russell Kingc8ebae32011-01-11 19:35:53 +0000701 dmaengine_slave_config(chan, &conf);
Alexandre Bounine16052822012-03-08 16:11:18 -0500702 desc = dmaengine_prep_slave_sg(chan, data->sg, nr_sg,
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100703 conf.direction, flags);
Russell Kingc8ebae32011-01-11 19:35:53 +0000704 if (!desc)
705 goto unmap_exit;
706
Ulf Hansson653a7612013-01-21 21:29:34 +0100707 *dma_chan = chan;
708 *dma_desc = desc;
Russell Kingc8ebae32011-01-11 19:35:53 +0000709
Per Forlin58c7ccb2011-07-01 18:55:24 +0200710 return 0;
711
712 unmap_exit:
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200713 dma_unmap_sg(device->dev, data->sg, data->sg_len,
714 mmc_get_dma_dir(data));
Per Forlin58c7ccb2011-07-01 18:55:24 +0200715 return -ENOMEM;
716}
717
Ludovic Barre47983512018-10-08 14:08:36 +0200718int mmci_dmae_prep_data(struct mmci_host *host,
719 struct mmc_data *data,
720 bool next)
Ulf Hansson653a7612013-01-21 21:29:34 +0100721{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200722 struct mmci_dmae_priv *dmae = host->dma_priv;
Ludovic Barread7b8912018-10-08 14:08:35 +0200723 struct mmci_dmae_next *nd = &dmae->next_data;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200724
Ludovic Barre47983512018-10-08 14:08:36 +0200725 if (!host->use_dma)
726 return -EINVAL;
727
Ludovic Barread7b8912018-10-08 14:08:35 +0200728 if (next)
Ludovic Barre47983512018-10-08 14:08:36 +0200729 return _mmci_dmae_prep_data(host, data, &nd->chan, &nd->desc);
Ulf Hansson653a7612013-01-21 21:29:34 +0100730 /* Check if next job is already prepared. */
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200731 if (dmae->cur && dmae->desc_current)
Ulf Hansson653a7612013-01-21 21:29:34 +0100732 return 0;
733
734 /* No job were prepared thus do it now. */
Ludovic Barre47983512018-10-08 14:08:36 +0200735 return _mmci_dmae_prep_data(host, data, &dmae->cur,
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200736 &dmae->desc_current);
Ulf Hansson653a7612013-01-21 21:29:34 +0100737}
738
Per Forlin58c7ccb2011-07-01 18:55:24 +0200739static int mmci_dma_start_data(struct mmci_host *host, unsigned int datactrl)
740{
741 int ret;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200742 struct mmci_dmae_priv *dmae = host->dma_priv;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200743 struct mmc_data *data = host->data;
744
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200745 if (!host->use_dma)
746 return -EINVAL;
747
Ludovic Barre47983512018-10-08 14:08:36 +0200748 ret = mmci_dmae_prep_data(host, host->data, false);
Per Forlin58c7ccb2011-07-01 18:55:24 +0200749 if (ret)
750 return ret;
751
752 /* Okay, go for it. */
Russell Kingc8ebae32011-01-11 19:35:53 +0000753 dev_vdbg(mmc_dev(host->mmc),
754 "Submit MMCI DMA job, sglen %d blksz %04x blks %04x flags %08x\n",
755 data->sg_len, data->blksz, data->blocks, data->flags);
Linus Walleije13934b2017-01-27 15:04:54 +0100756 host->dma_in_progress = true;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200757 dmaengine_submit(dmae->desc_current);
758 dma_async_issue_pending(dmae->cur);
Russell Kingc8ebae32011-01-11 19:35:53 +0000759
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100760 if (host->variant->qcom_dml)
761 dml_start_xfer(host, data);
762
Russell Kingc8ebae32011-01-11 19:35:53 +0000763 datactrl |= MCI_DPSM_DMAENABLE;
764
765 /* Trigger the DMA transfer */
Ulf Hansson9cc639a2013-05-15 20:48:23 +0100766 mmci_write_datactrlreg(host, datactrl);
Russell Kingc8ebae32011-01-11 19:35:53 +0000767
768 /*
769 * Let the MMCI say when the data is ended and it's time
770 * to fire next DMA request. When that happens, MMCI will
771 * call mmci_data_end()
772 */
773 writel(readl(host->base + MMCIMASK0) | MCI_DATAENDMASK,
774 host->base + MMCIMASK0);
775 return 0;
Russell Kingc8ebae32011-01-11 19:35:53 +0000776}
Per Forlin58c7ccb2011-07-01 18:55:24 +0200777
Ludovic Barre02769962018-10-08 14:08:37 +0200778void mmci_dmae_get_next_data(struct mmci_host *host, struct mmc_data *data)
Per Forlin58c7ccb2011-07-01 18:55:24 +0200779{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200780 struct mmci_dmae_priv *dmae = host->dma_priv;
781 struct mmci_dmae_next *next = &dmae->next_data;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200782
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200783 if (!host->use_dma)
784 return;
785
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200786 WARN_ON(!data->host_cookie && (next->desc || next->chan));
Per Forlin58c7ccb2011-07-01 18:55:24 +0200787
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200788 dmae->desc_current = next->desc;
789 dmae->cur = next->chan;
790 next->desc = NULL;
791 next->chan = NULL;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200792}
793
Ludovic Barre47983512018-10-08 14:08:36 +0200794void mmci_dmae_unprep_data(struct mmci_host *host,
795 struct mmc_data *data, int err)
796
Per Forlin58c7ccb2011-07-01 18:55:24 +0200797{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200798 struct mmci_dmae_priv *dmae = host->dma_priv;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200799
Ludovic Barre47983512018-10-08 14:08:36 +0200800 if (!host->use_dma)
Per Forlin58c7ccb2011-07-01 18:55:24 +0200801 return;
802
Ulf Hansson653a7612013-01-21 21:29:34 +0100803 mmci_dma_unmap(host, data);
Per Forlin58c7ccb2011-07-01 18:55:24 +0200804
Ulf Hansson653a7612013-01-21 21:29:34 +0100805 if (err) {
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200806 struct mmci_dmae_next *next = &dmae->next_data;
Ulf Hansson653a7612013-01-21 21:29:34 +0100807 struct dma_chan *chan;
808 if (data->flags & MMC_DATA_READ)
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200809 chan = dmae->rx_channel;
Ulf Hansson653a7612013-01-21 21:29:34 +0100810 else
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200811 chan = dmae->tx_channel;
Ulf Hansson653a7612013-01-21 21:29:34 +0100812 dmaengine_terminate_all(chan);
Per Forlin58c7ccb2011-07-01 18:55:24 +0200813
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200814 if (dmae->desc_current == next->desc)
815 dmae->desc_current = NULL;
Srinivas Kandagatlab5c16a62014-10-08 12:25:17 +0100816
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200817 if (dmae->cur == next->chan) {
Linus Walleije13934b2017-01-27 15:04:54 +0100818 host->dma_in_progress = false;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200819 dmae->cur = NULL;
Linus Walleije13934b2017-01-27 15:04:54 +0100820 }
Srinivas Kandagatlab5c16a62014-10-08 12:25:17 +0100821
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200822 next->desc = NULL;
823 next->chan = NULL;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200824 }
825}
826
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200827static struct mmci_host_ops mmci_variant_ops = {
Ludovic Barre47983512018-10-08 14:08:36 +0200828 .prep_data = mmci_dmae_prep_data,
829 .unprep_data = mmci_dmae_unprep_data,
Ludovic Barre02769962018-10-08 14:08:37 +0200830 .get_next_data = mmci_dmae_get_next_data,
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200831 .dma_setup = mmci_dmae_setup,
832 .dma_release = mmci_dmae_release,
833};
834
835void mmci_variant_init(struct mmci_host *host)
836{
837 host->ops = &mmci_variant_ops;
838}
Russell Kingc8ebae32011-01-11 19:35:53 +0000839#else
840/* Blank functions if the DMA engine is not available */
Ulf Hansson653a7612013-01-21 21:29:34 +0100841static inline void mmci_dma_finalize(struct mmci_host *host,
842 struct mmc_data *data)
843{
844}
845
Russell Kingc8ebae32011-01-11 19:35:53 +0000846static inline void mmci_dma_data_error(struct mmci_host *host)
847{
848}
849
850static inline int mmci_dma_start_data(struct mmci_host *host, unsigned int datactrl)
851{
852 return -ENOSYS;
853}
Per Forlin58c7ccb2011-07-01 18:55:24 +0200854
Russell Kingc8ebae32011-01-11 19:35:53 +0000855#endif
856
Ludovic Barre47983512018-10-08 14:08:36 +0200857static void mmci_pre_request(struct mmc_host *mmc, struct mmc_request *mrq)
858{
859 struct mmci_host *host = mmc_priv(mmc);
860 struct mmc_data *data = mrq->data;
861
862 if (!data)
863 return;
864
865 WARN_ON(data->host_cookie);
866
867 if (mmci_validate_data(host, data))
868 return;
869
870 mmci_prep_data(host, data, true);
871}
872
873static void mmci_post_request(struct mmc_host *mmc, struct mmc_request *mrq,
874 int err)
875{
876 struct mmci_host *host = mmc_priv(mmc);
877 struct mmc_data *data = mrq->data;
878
879 if (!data || !data->host_cookie)
880 return;
881
882 mmci_unprep_data(host, data, err);
883}
884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885static void mmci_start_data(struct mmci_host *host, struct mmc_data *data)
886{
Rabin Vincent8301bb62010-08-09 12:57:30 +0100887 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 unsigned int datactrl, timeout, irqmask;
Russell King7b09cda2005-07-01 12:02:59 +0100889 unsigned long long clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 void __iomem *base;
Russell King3bc87f22006-08-27 13:51:28 +0100891 int blksz_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Linus Walleij64de0282010-02-19 01:09:10 +0100893 dev_dbg(mmc_dev(host->mmc), "blksz %04x blks %04x flags %08x\n",
894 data->blksz, data->blocks, data->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 host->data = data;
Rabin Vincent528320d2010-07-21 12:49:49 +0100897 host->size = data->blksz * data->blocks;
Russell King51d43752011-01-27 10:56:52 +0000898 data->bytes_xfered = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Russell King7b09cda2005-07-01 12:02:59 +0100900 clks = (unsigned long long)data->timeout_ns * host->cclk;
Srinivas Kandagatlac4a35762014-06-02 10:08:39 +0100901 do_div(clks, NSEC_PER_SEC);
Russell King7b09cda2005-07-01 12:02:59 +0100902
903 timeout = data->timeout_clks + (unsigned int)clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 base = host->base;
906 writel(timeout, base + MMCIDATATIMER);
907 writel(host->size, base + MMCIDATALENGTH);
908
Russell King3bc87f22006-08-27 13:51:28 +0100909 blksz_bits = ffs(data->blksz) - 1;
910 BUG_ON(1 << blksz_bits != data->blksz);
911
Philippe Langlais1784b152011-03-25 08:51:52 +0100912 if (variant->blksz_datactrl16)
913 datactrl = MCI_DPSM_ENABLE | (data->blksz << 16);
Srinivas Kandagatlaff783232014-06-02 10:09:06 +0100914 else if (variant->blksz_datactrl4)
915 datactrl = MCI_DPSM_ENABLE | (data->blksz << 4);
Philippe Langlais1784b152011-03-25 08:51:52 +0100916 else
917 datactrl = MCI_DPSM_ENABLE | blksz_bits << 4;
Russell Kingc8ebae32011-01-11 19:35:53 +0000918
919 if (data->flags & MMC_DATA_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 datactrl |= MCI_DPSM_DIRECTION;
Russell Kingc8ebae32011-01-11 19:35:53 +0000921
Srinivas Kandagatlac7354132014-08-22 05:55:16 +0100922 if (host->mmc->card && mmc_card_sdio(host->mmc->card)) {
923 u32 clk;
Ulf Hansson7258db72011-12-13 17:05:28 +0100924
Srinivas Kandagatlac7354132014-08-22 05:55:16 +0100925 datactrl |= variant->datactrl_mask_sdio;
Ulf Hansson06c1a122012-10-12 14:01:50 +0100926
Srinivas Kandagatlac7354132014-08-22 05:55:16 +0100927 /*
928 * The ST Micro variant for SDIO small write transfers
929 * needs to have clock H/W flow control disabled,
930 * otherwise the transfer will not start. The threshold
931 * depends on the rate of MCLK.
932 */
933 if (variant->st_sdio && data->flags & MMC_DATA_WRITE &&
934 (host->size < 8 ||
935 (host->size <= 8 && host->mclk > 50000000)))
936 clk = host->clk_reg & ~variant->clkreg_enable;
937 else
938 clk = host->clk_reg | variant->clkreg_enable;
939
940 mmci_write_clkreg(host, clk);
941 }
Ulf Hansson06c1a122012-10-12 14:01:50 +0100942
Seungwon Jeon6dad6c92014-03-14 21:12:13 +0900943 if (host->mmc->ios.timing == MMC_TIMING_UHS_DDR50 ||
944 host->mmc->ios.timing == MMC_TIMING_MMC_DDR52)
Srinivas Kandagatlae17dca22014-06-02 10:09:15 +0100945 datactrl |= variant->datactrl_mask_ddrmode;
Ulf Hansson6dbb6ee2013-01-07 15:30:44 +0100946
Russell Kingc8ebae32011-01-11 19:35:53 +0000947 /*
948 * Attempt to use DMA operation mode, if this
949 * should fail, fall back to PIO mode
950 */
951 if (!mmci_dma_start_data(host, datactrl))
952 return;
953
954 /* IRQ mode, map the SG list for CPU reading/writing */
955 mmci_init_sg(host, data);
956
957 if (data->flags & MMC_DATA_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 irqmask = MCI_RXFIFOHALFFULLMASK;
Russell King0425a142006-02-16 16:48:31 +0000959
960 /*
Russell Kingc4d877c2011-01-27 09:50:13 +0000961 * If we have less than the fifo 'half-full' threshold to
962 * transfer, trigger a PIO interrupt as soon as any data
963 * is available.
Russell King0425a142006-02-16 16:48:31 +0000964 */
Russell Kingc4d877c2011-01-27 09:50:13 +0000965 if (host->size < variant->fifohalfsize)
Russell King0425a142006-02-16 16:48:31 +0000966 irqmask |= MCI_RXDATAAVLBLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 } else {
968 /*
969 * We don't actually need to include "FIFO empty" here
970 * since its implicit in "FIFO half empty".
971 */
972 irqmask = MCI_TXFIFOHALFEMPTYMASK;
973 }
974
Ulf Hansson9cc639a2013-05-15 20:48:23 +0100975 mmci_write_datactrlreg(host, datactrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 writel(readl(base + MMCIMASK0) & ~MCI_DATAENDMASK, base + MMCIMASK0);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100977 mmci_set_mask1(host, irqmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978}
979
980static void
981mmci_start_command(struct mmci_host *host, struct mmc_command *cmd, u32 c)
982{
983 void __iomem *base = host->base;
984
Linus Walleij64de0282010-02-19 01:09:10 +0100985 dev_dbg(mmc_dev(host->mmc), "op %02x arg %08x flags %08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 cmd->opcode, cmd->arg, cmd->flags);
987
988 if (readl(base + MMCICOMMAND) & MCI_CPSM_ENABLE) {
989 writel(0, base + MMCICOMMAND);
Srinivas Kandagatla6adb2a82014-06-02 10:08:57 +0100990 mmci_reg_delay(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 }
992
993 c |= cmd->opcode | MCI_CPSM_ENABLE;
Russell Kinge9225172006-02-02 12:23:12 +0000994 if (cmd->flags & MMC_RSP_PRESENT) {
995 if (cmd->flags & MMC_RSP_136)
996 c |= MCI_CPSM_LONGRSP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 c |= MCI_CPSM_RESPONSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 }
999 if (/*interrupt*/0)
1000 c |= MCI_CPSM_INTERRUPT;
1001
Srinivas Kandagatlaae7b0062014-06-02 10:09:39 +01001002 if (mmc_cmd_type(cmd) == MMC_CMD_ADTC)
1003 c |= host->variant->data_cmd_enable;
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 host->cmd = cmd;
1006
1007 writel(cmd->arg, base + MMCIARGUMENT);
1008 writel(c, base + MMCICOMMAND);
1009}
1010
1011static void
1012mmci_data_irq(struct mmci_host *host, struct mmc_data *data,
1013 unsigned int status)
1014{
Ulf Hansson1cb9da52014-06-12 14:42:23 +02001015 /* Make sure we have data to handle */
1016 if (!data)
1017 return;
1018
Linus Walleijf20f8f212010-10-19 13:41:24 +01001019 /* First check for errors */
Patrice Chotard7f7b5502018-01-18 15:34:18 +01001020 if (status & (MCI_DATACRCFAIL | MCI_DATATIMEOUT |
1021 host->variant->start_err |
1022 MCI_TXUNDERRUN | MCI_RXOVERRUN)) {
Linus Walleij8cb28152011-01-24 15:22:13 +01001023 u32 remain, success;
Linus Walleijf20f8f212010-10-19 13:41:24 +01001024
Russell Kingc8ebae32011-01-11 19:35:53 +00001025 /* Terminate the DMA transfer */
Ludovic Barrecdea1942018-09-21 11:45:56 +02001026 mmci_dma_data_error(host);
Russell Kingc8ebae32011-01-11 19:35:53 +00001027
Russell Kingc8afc9d2011-02-04 09:19:46 +00001028 /*
1029 * Calculate how far we are into the transfer. Note that
1030 * the data counter gives the number of bytes transferred
1031 * on the MMC bus, not on the host side. On reads, this
1032 * can be as much as a FIFO-worth of data ahead. This
1033 * matters for FIFO overruns only.
1034 */
Linus Walleijf5a106d2011-01-27 17:44:34 +01001035 remain = readl(host->base + MMCIDATACNT);
Linus Walleij8cb28152011-01-24 15:22:13 +01001036 success = data->blksz * data->blocks - remain;
1037
Russell Kingc8afc9d2011-02-04 09:19:46 +00001038 dev_dbg(mmc_dev(host->mmc), "MCI ERROR IRQ, status 0x%08x at 0x%08x\n",
1039 status, success);
Linus Walleij8cb28152011-01-24 15:22:13 +01001040 if (status & MCI_DATACRCFAIL) {
1041 /* Last block was not successful */
Russell Kingc8afc9d2011-02-04 09:19:46 +00001042 success -= 1;
Pierre Ossman17b04292007-07-22 22:18:46 +02001043 data->error = -EILSEQ;
Linus Walleij8cb28152011-01-24 15:22:13 +01001044 } else if (status & MCI_DATATIMEOUT) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001045 data->error = -ETIMEDOUT;
Linus Walleij757df742011-06-30 15:10:21 +01001046 } else if (status & MCI_STARTBITERR) {
1047 data->error = -ECOMM;
Russell Kingc8afc9d2011-02-04 09:19:46 +00001048 } else if (status & MCI_TXUNDERRUN) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001049 data->error = -EIO;
Russell Kingc8afc9d2011-02-04 09:19:46 +00001050 } else if (status & MCI_RXOVERRUN) {
1051 if (success > host->variant->fifosize)
1052 success -= host->variant->fifosize;
1053 else
1054 success = 0;
Linus Walleij8cb28152011-01-24 15:22:13 +01001055 data->error = -EIO;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001056 }
Russell King51d43752011-01-27 10:56:52 +00001057 data->bytes_xfered = round_down(success, data->blksz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 }
Linus Walleijf20f8f212010-10-19 13:41:24 +01001059
Linus Walleij8cb28152011-01-24 15:22:13 +01001060 if (status & MCI_DATABLOCKEND)
1061 dev_err(mmc_dev(host->mmc), "stray MCI_DATABLOCKEND interrupt\n");
Linus Walleijf20f8f212010-10-19 13:41:24 +01001062
Russell Kingccff9b52011-01-30 21:03:50 +00001063 if (status & MCI_DATAEND || data->error) {
Ludovic Barrecdea1942018-09-21 11:45:56 +02001064 mmci_dma_finalize(host, data);
1065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 mmci_stop_data(host);
1067
Linus Walleij8cb28152011-01-24 15:22:13 +01001068 if (!data->error)
1069 /* The error clause is handled above, success! */
Russell King51d43752011-01-27 10:56:52 +00001070 data->bytes_xfered = data->blksz * data->blocks;
Linus Walleijf20f8f212010-10-19 13:41:24 +01001071
Ulf Hansson024629c2013-05-13 15:40:56 +01001072 if (!data->stop || host->mrq->sbc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 mmci_request_end(host, data->mrq);
1074 } else {
1075 mmci_start_command(host, data->stop, 0);
1076 }
1077 }
1078}
1079
1080static void
1081mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
1082 unsigned int status)
1083{
1084 void __iomem *base = host->base;
Linus Walleij49adc0c2016-10-25 11:06:06 +02001085 bool sbc;
Ulf Hanssonad82bfe2014-06-12 15:01:57 +02001086
1087 if (!cmd)
1088 return;
1089
1090 sbc = (cmd == host->mrq->sbc);
Ulf Hanssonad82bfe2014-06-12 15:01:57 +02001091
Linus Walleij49adc0c2016-10-25 11:06:06 +02001092 /*
1093 * We need to be one of these interrupts to be considered worth
1094 * handling. Note that we tag on any latent IRQs postponed
1095 * due to waiting for busy status.
1096 */
1097 if (!((status|host->busy_status) &
1098 (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT|MCI_CMDSENT|MCI_CMDRESPEND)))
Ulf Hanssonad82bfe2014-06-12 15:01:57 +02001099 return;
Ulf Hansson8d94b542014-01-13 16:49:31 +01001100
Linus Walleij49adc0c2016-10-25 11:06:06 +02001101 /*
1102 * ST Micro variant: handle busy detection.
1103 */
1104 if (host->variant->busy_detect) {
1105 bool busy_resp = !!(cmd->flags & MMC_RSP_BUSY);
Ulf Hansson8d94b542014-01-13 16:49:31 +01001106
Linus Walleij49adc0c2016-10-25 11:06:06 +02001107 /* We are busy with a command, return */
1108 if (host->busy_status &&
1109 (status & host->variant->busy_detect_flag))
1110 return;
Ulf Hansson8d94b542014-01-13 16:49:31 +01001111
Linus Walleij49adc0c2016-10-25 11:06:06 +02001112 /*
1113 * We were not busy, but we now got a busy response on
1114 * something that was not an error, and we double-check
1115 * that the special busy status bit is still set before
1116 * proceeding.
1117 */
1118 if (!host->busy_status && busy_resp &&
1119 !(status & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT)) &&
1120 (readl(base + MMCISTATUS) & host->variant->busy_detect_flag)) {
Jean-Nicolas Graux5cad24d2017-02-07 12:12:41 +01001121
1122 /* Clear the busy start IRQ */
1123 writel(host->variant->busy_detect_mask,
1124 host->base + MMCICLEAR);
1125
1126 /* Unmask the busy end IRQ */
Linus Walleij49adc0c2016-10-25 11:06:06 +02001127 writel(readl(base + MMCIMASK0) |
1128 host->variant->busy_detect_mask,
1129 base + MMCIMASK0);
1130 /*
1131 * Now cache the last response status code (until
1132 * the busy bit goes low), and return.
1133 */
1134 host->busy_status =
1135 status & (MCI_CMDSENT|MCI_CMDRESPEND);
1136 return;
1137 }
1138
1139 /*
1140 * At this point we are not busy with a command, we have
Jean-Nicolas Graux5cad24d2017-02-07 12:12:41 +01001141 * not received a new busy request, clear and mask the busy
1142 * end IRQ and fall through to process the IRQ.
Linus Walleij49adc0c2016-10-25 11:06:06 +02001143 */
1144 if (host->busy_status) {
Jean-Nicolas Graux5cad24d2017-02-07 12:12:41 +01001145
1146 writel(host->variant->busy_detect_mask,
1147 host->base + MMCICLEAR);
1148
Linus Walleij49adc0c2016-10-25 11:06:06 +02001149 writel(readl(base + MMCIMASK0) &
1150 ~host->variant->busy_detect_mask,
1151 base + MMCIMASK0);
1152 host->busy_status = 0;
1153 }
Ulf Hansson8d94b542014-01-13 16:49:31 +01001154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 host->cmd = NULL;
1157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 if (status & MCI_CMDTIMEOUT) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001159 cmd->error = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001161 cmd->error = -EILSEQ;
Russell King - ARM Linux9047b432011-01-11 16:35:56 +00001162 } else {
1163 cmd->resp[0] = readl(base + MMCIRESPONSE0);
1164 cmd->resp[1] = readl(base + MMCIRESPONSE1);
1165 cmd->resp[2] = readl(base + MMCIRESPONSE2);
1166 cmd->resp[3] = readl(base + MMCIRESPONSE3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 }
1168
Ulf Hansson024629c2013-05-13 15:40:56 +01001169 if ((!sbc && !cmd->data) || cmd->error) {
Ulf Hansson3b6e3c72011-12-13 16:58:43 +01001170 if (host->data) {
1171 /* Terminate the DMA transfer */
Ludovic Barrecdea1942018-09-21 11:45:56 +02001172 mmci_dma_data_error(host);
Ludovic Barre7b2a6d52018-09-21 11:45:55 +02001173
Russell Kinge47c2222007-01-08 16:42:51 +00001174 mmci_stop_data(host);
Ulf Hansson3b6e3c72011-12-13 16:58:43 +01001175 }
Ulf Hansson024629c2013-05-13 15:40:56 +01001176 mmci_request_end(host, host->mrq);
1177 } else if (sbc) {
1178 mmci_start_command(host, host->mrq->cmd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 } else if (!(cmd->data->flags & MMC_DATA_READ)) {
1180 mmci_start_data(host, cmd->data);
1181 }
1182}
1183
Srinivas Kandagatla9c34b732014-06-02 10:10:04 +01001184static int mmci_get_rx_fifocnt(struct mmci_host *host, u32 status, int remain)
1185{
1186 return remain - (readl(host->base + MMCIFIFOCNT) << 2);
1187}
1188
1189static int mmci_qcom_get_rx_fifocnt(struct mmci_host *host, u32 status, int r)
1190{
1191 /*
1192 * on qcom SDCC4 only 8 words are used in each burst so only 8 addresses
1193 * from the fifo range should be used
1194 */
1195 if (status & MCI_RXFIFOHALFFULL)
1196 return host->variant->fifohalfsize;
1197 else if (status & MCI_RXDATAAVLBL)
1198 return 4;
1199
1200 return 0;
1201}
1202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203static int mmci_pio_read(struct mmci_host *host, char *buffer, unsigned int remain)
1204{
1205 void __iomem *base = host->base;
1206 char *ptr = buffer;
Srinivas Kandagatla9c34b732014-06-02 10:10:04 +01001207 u32 status = readl(host->base + MMCISTATUS);
Linus Walleij26eed9a2008-04-26 23:39:44 +01001208 int host_remain = host->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
1210 do {
Srinivas Kandagatla9c34b732014-06-02 10:10:04 +01001211 int count = host->get_rx_fifocnt(host, status, host_remain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213 if (count > remain)
1214 count = remain;
1215
1216 if (count <= 0)
1217 break;
1218
Ulf Hansson393e5e22011-12-13 17:08:04 +01001219 /*
1220 * SDIO especially may want to send something that is
1221 * not divisible by 4 (as opposed to card sectors
1222 * etc). Therefore make sure to always read the last bytes
1223 * while only doing full 32-bit reads towards the FIFO.
1224 */
1225 if (unlikely(count & 0x3)) {
1226 if (count < 4) {
1227 unsigned char buf[4];
Davide Ciminaghi4b85da02012-12-10 14:47:21 +01001228 ioread32_rep(base + MMCIFIFO, buf, 1);
Ulf Hansson393e5e22011-12-13 17:08:04 +01001229 memcpy(ptr, buf, count);
1230 } else {
Davide Ciminaghi4b85da02012-12-10 14:47:21 +01001231 ioread32_rep(base + MMCIFIFO, ptr, count >> 2);
Ulf Hansson393e5e22011-12-13 17:08:04 +01001232 count &= ~0x3;
1233 }
1234 } else {
Davide Ciminaghi4b85da02012-12-10 14:47:21 +01001235 ioread32_rep(base + MMCIFIFO, ptr, count >> 2);
Ulf Hansson393e5e22011-12-13 17:08:04 +01001236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 ptr += count;
1239 remain -= count;
Linus Walleij26eed9a2008-04-26 23:39:44 +01001240 host_remain -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242 if (remain == 0)
1243 break;
1244
1245 status = readl(base + MMCISTATUS);
1246 } while (status & MCI_RXDATAAVLBL);
1247
1248 return ptr - buffer;
1249}
1250
1251static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status)
1252{
Rabin Vincent8301bb62010-08-09 12:57:30 +01001253 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 void __iomem *base = host->base;
1255 char *ptr = buffer;
1256
1257 do {
1258 unsigned int count, maxcnt;
1259
Rabin Vincent8301bb62010-08-09 12:57:30 +01001260 maxcnt = status & MCI_TXFIFOEMPTY ?
1261 variant->fifosize : variant->fifohalfsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 count = min(remain, maxcnt);
1263
Linus Walleij34177802010-10-19 12:43:58 +01001264 /*
Linus Walleij34177802010-10-19 12:43:58 +01001265 * SDIO especially may want to send something that is
1266 * not divisible by 4 (as opposed to card sectors
1267 * etc), and the FIFO only accept full 32-bit writes.
1268 * So compensate by adding +3 on the count, a single
1269 * byte become a 32bit write, 7 bytes will be two
1270 * 32bit writes etc.
1271 */
Davide Ciminaghi4b85da02012-12-10 14:47:21 +01001272 iowrite32_rep(base + MMCIFIFO, ptr, (count + 3) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 ptr += count;
1275 remain -= count;
1276
1277 if (remain == 0)
1278 break;
1279
1280 status = readl(base + MMCISTATUS);
1281 } while (status & MCI_TXFIFOHALFEMPTY);
1282
1283 return ptr - buffer;
1284}
1285
1286/*
1287 * PIO data transfer IRQ handler.
1288 */
David Howells7d12e782006-10-05 14:55:46 +01001289static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
1291 struct mmci_host *host = dev_id;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001292 struct sg_mapping_iter *sg_miter = &host->sg_miter;
Rabin Vincent8301bb62010-08-09 12:57:30 +01001293 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 void __iomem *base = host->base;
1295 u32 status;
1296
1297 status = readl(base + MMCISTATUS);
1298
Linus Walleij64de0282010-02-19 01:09:10 +01001299 dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 unsigned int remain, len;
1303 char *buffer;
1304
1305 /*
1306 * For write, we only need to test the half-empty flag
1307 * here - if the FIFO is completely empty, then by
1308 * definition it is more than half empty.
1309 *
1310 * For read, check for data available.
1311 */
1312 if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL)))
1313 break;
1314
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001315 if (!sg_miter_next(sg_miter))
1316 break;
1317
1318 buffer = sg_miter->addr;
1319 remain = sg_miter->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321 len = 0;
1322 if (status & MCI_RXACTIVE)
1323 len = mmci_pio_read(host, buffer, remain);
1324 if (status & MCI_TXACTIVE)
1325 len = mmci_pio_write(host, buffer, remain, status);
1326
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001327 sg_miter->consumed = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 host->size -= len;
1330 remain -= len;
1331
1332 if (remain)
1333 break;
1334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 status = readl(base + MMCISTATUS);
1336 } while (1);
1337
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001338 sg_miter_stop(sg_miter);
1339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 /*
Russell Kingc4d877c2011-01-27 09:50:13 +00001341 * If we have less than the fifo 'half-full' threshold to transfer,
1342 * trigger a PIO interrupt as soon as any data is available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 */
Russell Kingc4d877c2011-01-27 09:50:13 +00001344 if (status & MCI_RXACTIVE && host->size < variant->fifohalfsize)
Linus Walleij2686b4b2010-10-19 12:39:48 +01001345 mmci_set_mask1(host, MCI_RXDATAAVLBLMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347 /*
1348 * If we run out of data, disable the data IRQs; this
1349 * prevents a race where the FIFO becomes empty before
1350 * the chip itself has disabled the data path, and
1351 * stops us racing with our data end IRQ.
1352 */
1353 if (host->size == 0) {
Linus Walleij2686b4b2010-10-19 12:39:48 +01001354 mmci_set_mask1(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0);
1356 }
1357
1358 return IRQ_HANDLED;
1359}
1360
1361/*
1362 * Handle completion of command and data transfers.
1363 */
David Howells7d12e782006-10-05 14:55:46 +01001364static irqreturn_t mmci_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
1366 struct mmci_host *host = dev_id;
1367 u32 status;
1368 int ret = 0;
1369
1370 spin_lock(&host->lock);
1371
1372 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 status = readl(host->base + MMCISTATUS);
Linus Walleij2686b4b2010-10-19 12:39:48 +01001374
1375 if (host->singleirq) {
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +01001376 if (status & host->mask1_reg)
Linus Walleij2686b4b2010-10-19 12:39:48 +01001377 mmci_pio_irq(irq, dev_id);
1378
1379 status &= ~MCI_IRQ1MASK;
1380 }
1381
Ulf Hansson8d94b542014-01-13 16:49:31 +01001382 /*
Jean-Nicolas Graux5cad24d2017-02-07 12:12:41 +01001383 * We intentionally clear the MCI_ST_CARDBUSY IRQ (if it's
1384 * enabled) in mmci_cmd_irq() function where ST Micro busy
1385 * detection variant is handled. Considering the HW seems to be
1386 * triggering the IRQ on both edges while monitoring DAT0 for
1387 * busy completion and that same status bit is used to monitor
1388 * start and end of busy detection, special care must be taken
1389 * to make sure that both start and end interrupts are always
1390 * cleared one after the other.
Ulf Hansson8d94b542014-01-13 16:49:31 +01001391 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 status &= readl(host->base + MMCIMASK0);
Jean-Nicolas Graux5cad24d2017-02-07 12:12:41 +01001393 if (host->variant->busy_detect)
1394 writel(status & ~host->variant->busy_detect_mask,
1395 host->base + MMCICLEAR);
1396 else
1397 writel(status, host->base + MMCICLEAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Linus Walleij64de0282010-02-19 01:09:10 +01001399 dev_dbg(mmc_dev(host->mmc), "irq0 (data+cmd) %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Ulf Hansson78782892014-06-13 13:21:38 +02001401 if (host->variant->reversed_irq_handling) {
1402 mmci_data_irq(host, host->data, status);
1403 mmci_cmd_irq(host, host->cmd, status);
1404 } else {
1405 mmci_cmd_irq(host, host->cmd, status);
1406 mmci_data_irq(host, host->data, status);
1407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Linus Walleij49adc0c2016-10-25 11:06:06 +02001409 /*
1410 * Don't poll for busy completion in irq context.
1411 */
1412 if (host->variant->busy_detect && host->busy_status)
1413 status &= ~host->variant->busy_detect_flag;
Ulf Hansson8d94b542014-01-13 16:49:31 +01001414
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 ret = 1;
1416 } while (status);
1417
1418 spin_unlock(&host->lock);
1419
1420 return IRQ_RETVAL(ret);
1421}
1422
1423static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1424{
1425 struct mmci_host *host = mmc_priv(mmc);
Linus Walleij9e943022008-10-24 21:17:50 +01001426 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
1428 WARN_ON(host->mrq != NULL);
1429
Ulf Hansson653a7612013-01-21 21:29:34 +01001430 mrq->cmd->error = mmci_validate_data(host, mrq->data);
1431 if (mrq->cmd->error) {
Pierre Ossman255d01a2007-07-24 20:38:53 +02001432 mmc_request_done(mmc, mrq);
1433 return;
1434 }
1435
Linus Walleij9e943022008-10-24 21:17:50 +01001436 spin_lock_irqsave(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
1438 host->mrq = mrq;
1439
Per Forlin58c7ccb2011-07-01 18:55:24 +02001440 if (mrq->data)
1441 mmci_get_next_data(host, mrq->data);
1442
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 if (mrq->data && mrq->data->flags & MMC_DATA_READ)
1444 mmci_start_data(host, mrq->data);
1445
Ulf Hansson024629c2013-05-13 15:40:56 +01001446 if (mrq->sbc)
1447 mmci_start_command(host, mrq->sbc, 0);
1448 else
1449 mmci_start_command(host, mrq->cmd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Linus Walleij9e943022008-10-24 21:17:50 +01001451 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452}
1453
1454static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1455{
1456 struct mmci_host *host = mmc_priv(mmc);
Ulf Hansson7d72a1d2011-12-13 16:54:55 +01001457 struct variant_data *variant = host->variant;
Linus Walleija6a64642009-09-14 12:56:14 +01001458 u32 pwr = 0;
1459 unsigned long flags;
Lee Jonesdb90f912013-05-03 12:52:12 +01001460 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Ulf Hanssonbc521812011-12-13 16:57:55 +01001462 if (host->plat->ios_handler &&
1463 host->plat->ios_handler(mmc_dev(mmc), ios))
1464 dev_err(mmc_dev(mmc), "platform ios_handler failed\n");
1465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 switch (ios->power_mode) {
1467 case MMC_POWER_OFF:
Ulf Hansson599c1d52013-01-07 16:22:50 +01001468 if (!IS_ERR(mmc->supply.vmmc))
1469 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Lee Jones237fb5e2013-01-31 11:27:52 +00001470
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001471 if (!IS_ERR(mmc->supply.vqmmc) && host->vqmmc_enabled) {
Lee Jones237fb5e2013-01-31 11:27:52 +00001472 regulator_disable(mmc->supply.vqmmc);
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001473 host->vqmmc_enabled = false;
1474 }
Lee Jones237fb5e2013-01-31 11:27:52 +00001475
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 break;
1477 case MMC_POWER_UP:
Ulf Hansson599c1d52013-01-07 16:22:50 +01001478 if (!IS_ERR(mmc->supply.vmmc))
1479 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd);
1480
Ulf Hansson7d72a1d2011-12-13 16:54:55 +01001481 /*
1482 * The ST Micro variant doesn't have the PL180s MCI_PWR_UP
1483 * and instead uses MCI_PWR_ON so apply whatever value is
1484 * configured in the variant data.
1485 */
1486 pwr |= variant->pwrreg_powerup;
1487
1488 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 case MMC_POWER_ON:
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001490 if (!IS_ERR(mmc->supply.vqmmc) && !host->vqmmc_enabled) {
Lee Jonesdb90f912013-05-03 12:52:12 +01001491 ret = regulator_enable(mmc->supply.vqmmc);
1492 if (ret < 0)
1493 dev_err(mmc_dev(mmc),
1494 "failed to enable vqmmc regulator\n");
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001495 else
1496 host->vqmmc_enabled = true;
Lee Jonesdb90f912013-05-03 12:52:12 +01001497 }
Lee Jones237fb5e2013-01-31 11:27:52 +00001498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 pwr |= MCI_PWR_ON;
1500 break;
1501 }
1502
Ulf Hansson4d1a3a02011-12-13 16:57:07 +01001503 if (variant->signal_direction && ios->power_mode != MMC_POWER_OFF) {
1504 /*
1505 * The ST Micro variant has some additional bits
1506 * indicating signal direction for the signals in
1507 * the SD/MMC bus and feedback-clock usage.
1508 */
Ulf Hansson4593df22014-03-21 10:13:05 +01001509 pwr |= host->pwr_reg_add;
Ulf Hansson4d1a3a02011-12-13 16:57:07 +01001510
1511 if (ios->bus_width == MMC_BUS_WIDTH_4)
1512 pwr &= ~MCI_ST_DATA74DIREN;
1513 else if (ios->bus_width == MMC_BUS_WIDTH_1)
1514 pwr &= (~MCI_ST_DATA74DIREN &
1515 ~MCI_ST_DATA31DIREN &
1516 ~MCI_ST_DATA2DIREN);
1517 }
1518
Patrice Chotardf9bb3042018-01-18 15:34:20 +01001519 if (variant->opendrain) {
1520 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
1521 pwr |= variant->opendrain;
1522 } else {
1523 /*
1524 * If the variant cannot configure the pads by its own, then we
1525 * expect the pinctrl to be able to do that for us
1526 */
1527 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
1528 pinctrl_select_state(host->pinctrl, host->pins_opendrain);
1529 else
1530 pinctrl_select_state(host->pinctrl, host->pins_default);
1531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Ulf Hanssonf4670da2013-01-09 17:19:54 +01001533 /*
1534 * If clock = 0 and the variant requires the MMCIPOWER to be used for
1535 * gating the clock, the MCI_PWR_ON bit is cleared.
1536 */
1537 if (!ios->clock && variant->pwrreg_clkgate)
1538 pwr &= ~MCI_PWR_ON;
1539
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001540 if (host->variant->explicit_mclk_control &&
1541 ios->clock != host->clock_cache) {
1542 ret = clk_set_rate(host->clk, ios->clock);
1543 if (ret < 0)
1544 dev_err(mmc_dev(host->mmc),
1545 "Error setting clock rate (%d)\n", ret);
1546 else
1547 host->mclk = clk_get_rate(host->clk);
1548 }
1549 host->clock_cache = ios->clock;
1550
Linus Walleija6a64642009-09-14 12:56:14 +01001551 spin_lock_irqsave(&host->lock, flags);
1552
1553 mmci_set_clkreg(host, ios->clock);
Ulf Hansson7437cfa2012-01-18 09:17:27 +01001554 mmci_write_pwrreg(host, pwr);
Ulf Hanssonf829c042013-09-04 09:01:15 +01001555 mmci_reg_delay(host);
Linus Walleija6a64642009-09-14 12:56:14 +01001556
1557 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558}
1559
Russell King89001442009-07-09 15:16:07 +01001560static int mmci_get_cd(struct mmc_host *mmc)
1561{
1562 struct mmci_host *host = mmc_priv(mmc);
Rabin Vincent29719442010-08-09 12:54:43 +01001563 struct mmci_platform_data *plat = host->plat;
Ulf Hanssond2762092014-03-17 13:56:19 +01001564 unsigned int status = mmc_gpio_get_cd(mmc);
Russell King89001442009-07-09 15:16:07 +01001565
Ulf Hanssond2762092014-03-17 13:56:19 +01001566 if (status == -ENOSYS) {
Rabin Vincent4b8caec2010-08-09 12:56:40 +01001567 if (!plat->status)
1568 return 1; /* Assume always present */
1569
Rabin Vincent29719442010-08-09 12:54:43 +01001570 status = plat->status(mmc_dev(host->mmc));
Ulf Hanssond2762092014-03-17 13:56:19 +01001571 }
Russell King74bc8092010-07-29 15:58:59 +01001572 return status;
Russell King89001442009-07-09 15:16:07 +01001573}
1574
Ulf Hansson0f3ed7f2013-05-15 20:47:33 +01001575static int mmci_sig_volt_switch(struct mmc_host *mmc, struct mmc_ios *ios)
1576{
1577 int ret = 0;
1578
1579 if (!IS_ERR(mmc->supply.vqmmc)) {
1580
Ulf Hansson0f3ed7f2013-05-15 20:47:33 +01001581 switch (ios->signal_voltage) {
1582 case MMC_SIGNAL_VOLTAGE_330:
1583 ret = regulator_set_voltage(mmc->supply.vqmmc,
1584 2700000, 3600000);
1585 break;
1586 case MMC_SIGNAL_VOLTAGE_180:
1587 ret = regulator_set_voltage(mmc->supply.vqmmc,
1588 1700000, 1950000);
1589 break;
1590 case MMC_SIGNAL_VOLTAGE_120:
1591 ret = regulator_set_voltage(mmc->supply.vqmmc,
1592 1100000, 1300000);
1593 break;
1594 }
1595
1596 if (ret)
1597 dev_warn(mmc_dev(mmc), "Voltage switch failed\n");
Ulf Hansson0f3ed7f2013-05-15 20:47:33 +01001598 }
1599
1600 return ret;
1601}
1602
Ulf Hansson01259622013-05-15 20:53:22 +01001603static struct mmc_host_ops mmci_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 .request = mmci_request,
Per Forlin58c7ccb2011-07-01 18:55:24 +02001605 .pre_req = mmci_pre_request,
1606 .post_req = mmci_post_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 .set_ios = mmci_set_ios,
Ulf Hanssond2762092014-03-17 13:56:19 +01001608 .get_ro = mmc_gpio_get_ro,
Russell King89001442009-07-09 15:16:07 +01001609 .get_cd = mmci_get_cd,
Ulf Hansson0f3ed7f2013-05-15 20:47:33 +01001610 .start_signal_voltage_switch = mmci_sig_volt_switch,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611};
1612
Ulf Hansson78f87df2014-03-17 15:53:07 +01001613static int mmci_of_parse(struct device_node *np, struct mmc_host *mmc)
1614{
Ulf Hansson4593df22014-03-21 10:13:05 +01001615 struct mmci_host *host = mmc_priv(mmc);
Ulf Hansson78f87df2014-03-17 15:53:07 +01001616 int ret = mmc_of_parse(mmc);
Lee Jones000bc9d2012-04-16 10:18:43 +01001617
Ulf Hansson78f87df2014-03-17 15:53:07 +01001618 if (ret)
1619 return ret;
Lee Jones000bc9d2012-04-16 10:18:43 +01001620
Ulf Hansson4593df22014-03-21 10:13:05 +01001621 if (of_get_property(np, "st,sig-dir-dat0", NULL))
1622 host->pwr_reg_add |= MCI_ST_DATA0DIREN;
1623 if (of_get_property(np, "st,sig-dir-dat2", NULL))
1624 host->pwr_reg_add |= MCI_ST_DATA2DIREN;
1625 if (of_get_property(np, "st,sig-dir-dat31", NULL))
1626 host->pwr_reg_add |= MCI_ST_DATA31DIREN;
1627 if (of_get_property(np, "st,sig-dir-dat74", NULL))
1628 host->pwr_reg_add |= MCI_ST_DATA74DIREN;
1629 if (of_get_property(np, "st,sig-dir-cmd", NULL))
1630 host->pwr_reg_add |= MCI_ST_CMDDIREN;
1631 if (of_get_property(np, "st,sig-pin-fbclk", NULL))
1632 host->pwr_reg_add |= MCI_ST_FBCLKEN;
1633
Lee Jones000bc9d2012-04-16 10:18:43 +01001634 if (of_get_property(np, "mmc-cap-mmc-highspeed", NULL))
Ulf Hansson78f87df2014-03-17 15:53:07 +01001635 mmc->caps |= MMC_CAP_MMC_HIGHSPEED;
Lee Jones000bc9d2012-04-16 10:18:43 +01001636 if (of_get_property(np, "mmc-cap-sd-highspeed", NULL))
Ulf Hansson78f87df2014-03-17 15:53:07 +01001637 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
Lee Jones000bc9d2012-04-16 10:18:43 +01001638
Ulf Hansson78f87df2014-03-17 15:53:07 +01001639 return 0;
Lee Jones000bc9d2012-04-16 10:18:43 +01001640}
Lee Jones000bc9d2012-04-16 10:18:43 +01001641
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001642static int mmci_probe(struct amba_device *dev,
Russell Kingaa25afa2011-02-19 15:55:00 +00001643 const struct amba_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644{
Linus Walleij6ef297f2009-09-22 14:29:36 +01001645 struct mmci_platform_data *plat = dev->dev.platform_data;
Lee Jones000bc9d2012-04-16 10:18:43 +01001646 struct device_node *np = dev->dev.of_node;
Rabin Vincent4956e102010-07-21 12:54:40 +01001647 struct variant_data *variant = id->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 struct mmci_host *host;
1649 struct mmc_host *mmc;
1650 int ret;
1651
Lee Jones000bc9d2012-04-16 10:18:43 +01001652 /* Must have platform data or Device Tree. */
1653 if (!plat && !np) {
1654 dev_err(&dev->dev, "No plat data or DT found\n");
1655 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 }
1657
Lee Jonesb9b52912012-06-12 10:49:51 +01001658 if (!plat) {
1659 plat = devm_kzalloc(&dev->dev, sizeof(*plat), GFP_KERNEL);
1660 if (!plat)
1661 return -ENOMEM;
1662 }
1663
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
Ulf Hanssonef289982014-03-17 13:56:32 +01001665 if (!mmc)
1666 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Ulf Hansson78f87df2014-03-17 15:53:07 +01001668 ret = mmci_of_parse(np, mmc);
1669 if (ret)
1670 goto host_free;
1671
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 host = mmc_priv(mmc);
Rabin Vincent4ea580f2009-04-17 08:44:19 +05301673 host->mmc = mmc;
Russell King012b7d32009-07-09 15:13:56 +01001674
Patrice Chotardf9bb3042018-01-18 15:34:20 +01001675 /*
1676 * Some variant (STM32) doesn't have opendrain bit, nevertheless
1677 * pins can be set accordingly using pinctrl
1678 */
1679 if (!variant->opendrain) {
1680 host->pinctrl = devm_pinctrl_get(&dev->dev);
1681 if (IS_ERR(host->pinctrl)) {
1682 dev_err(&dev->dev, "failed to get pinctrl");
Wei Yongjun310eb252018-01-23 02:09:13 +00001683 ret = PTR_ERR(host->pinctrl);
Patrice Chotardf9bb3042018-01-18 15:34:20 +01001684 goto host_free;
1685 }
1686
1687 host->pins_default = pinctrl_lookup_state(host->pinctrl,
1688 PINCTRL_STATE_DEFAULT);
1689 if (IS_ERR(host->pins_default)) {
1690 dev_err(mmc_dev(mmc), "Can't select default pins\n");
Wei Yongjun310eb252018-01-23 02:09:13 +00001691 ret = PTR_ERR(host->pins_default);
Patrice Chotardf9bb3042018-01-18 15:34:20 +01001692 goto host_free;
1693 }
1694
1695 host->pins_opendrain = pinctrl_lookup_state(host->pinctrl,
1696 MMCI_PINCTRL_STATE_OPENDRAIN);
1697 if (IS_ERR(host->pins_opendrain)) {
1698 dev_err(mmc_dev(mmc), "Can't select opendrain pins\n");
Wei Yongjun310eb252018-01-23 02:09:13 +00001699 ret = PTR_ERR(host->pins_opendrain);
Patrice Chotardf9bb3042018-01-18 15:34:20 +01001700 goto host_free;
1701 }
1702 }
1703
Russell King012b7d32009-07-09 15:13:56 +01001704 host->hw_designer = amba_manf(dev);
1705 host->hw_revision = amba_rev(dev);
Linus Walleij64de0282010-02-19 01:09:10 +01001706 dev_dbg(mmc_dev(mmc), "designer ID = 0x%02x\n", host->hw_designer);
1707 dev_dbg(mmc_dev(mmc), "revision = 0x%01x\n", host->hw_revision);
Russell King012b7d32009-07-09 15:13:56 +01001708
Ulf Hansson665ba562013-05-13 15:39:17 +01001709 host->clk = devm_clk_get(&dev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 if (IS_ERR(host->clk)) {
1711 ret = PTR_ERR(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 goto host_free;
1713 }
1714
Julia Lawallac940932012-08-26 16:00:59 +00001715 ret = clk_prepare_enable(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 if (ret)
Ulf Hansson665ba562013-05-13 15:39:17 +01001717 goto host_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Srinivas Kandagatla9c34b732014-06-02 10:10:04 +01001719 if (variant->qcom_fifo)
1720 host->get_rx_fifocnt = mmci_qcom_get_rx_fifocnt;
1721 else
1722 host->get_rx_fifocnt = mmci_get_rx_fifocnt;
1723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 host->plat = plat;
Rabin Vincent4956e102010-07-21 12:54:40 +01001725 host->variant = variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 host->mclk = clk_get_rate(host->clk);
Linus Walleijc8df9a52008-04-29 09:34:07 +01001727 /*
1728 * According to the spec, mclk is max 100 MHz,
1729 * so we try to adjust the clock down to this,
1730 * (if possible).
1731 */
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +01001732 if (host->mclk > variant->f_max) {
1733 ret = clk_set_rate(host->clk, variant->f_max);
Linus Walleijc8df9a52008-04-29 09:34:07 +01001734 if (ret < 0)
1735 goto clk_disable;
1736 host->mclk = clk_get_rate(host->clk);
Linus Walleij64de0282010-02-19 01:09:10 +01001737 dev_dbg(mmc_dev(mmc), "eventual mclk rate: %u Hz\n",
1738 host->mclk);
Linus Walleijc8df9a52008-04-29 09:34:07 +01001739 }
Ulf Hanssonef289982014-03-17 13:56:32 +01001740
Russell Kingc8ebae32011-01-11 19:35:53 +00001741 host->phybase = dev->res.start;
Ulf Hanssonef289982014-03-17 13:56:32 +01001742 host->base = devm_ioremap_resource(&dev->dev, &dev->res);
1743 if (IS_ERR(host->base)) {
1744 ret = PTR_ERR(host->base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 goto clk_disable;
1746 }
1747
Ulf Hanssoned9067f2018-07-13 13:15:23 +02001748 if (variant->init)
1749 variant->init(host);
1750
Linus Walleij7f294e42011-07-08 09:57:15 +01001751 /*
1752 * The ARM and ST versions of the block have slightly different
1753 * clock divider equations which means that the minimum divider
1754 * differs too.
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001755 * on Qualcomm like controllers get the nearest minimum clock to 100Khz
Linus Walleij7f294e42011-07-08 09:57:15 +01001756 */
1757 if (variant->st_clkdiv)
1758 mmc->f_min = DIV_ROUND_UP(host->mclk, 257);
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001759 else if (variant->explicit_mclk_control)
1760 mmc->f_min = clk_round_rate(host->clk, 100000);
Linus Walleij7f294e42011-07-08 09:57:15 +01001761 else
1762 mmc->f_min = DIV_ROUND_UP(host->mclk, 512);
Linus Walleij808d97c2010-04-08 07:39:38 +01001763 /*
Ulf Hansson78f87df2014-03-17 15:53:07 +01001764 * If no maximum operating frequency is supplied, fall back to use
1765 * the module parameter, which has a (low) default value in case it
1766 * is not specified. Either value must not exceed the clock rate into
Ulf Hansson5080a082014-03-21 10:46:39 +01001767 * the block, of course.
Linus Walleij808d97c2010-04-08 07:39:38 +01001768 */
Ulf Hansson78f87df2014-03-17 15:53:07 +01001769 if (mmc->f_max)
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001770 mmc->f_max = variant->explicit_mclk_control ?
1771 min(variant->f_max, mmc->f_max) :
1772 min(host->mclk, mmc->f_max);
Linus Walleij808d97c2010-04-08 07:39:38 +01001773 else
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001774 mmc->f_max = variant->explicit_mclk_control ?
1775 fmax : min(host->mclk, fmax);
1776
1777
Linus Walleij64de0282010-02-19 01:09:10 +01001778 dev_dbg(mmc_dev(mmc), "clocking block at %u Hz\n", mmc->f_max);
1779
Ulf Hansson599c1d52013-01-07 16:22:50 +01001780 /* Get regulators and the supported OCR mask */
Bjorn Andersson9369c972015-03-24 18:39:49 -07001781 ret = mmc_regulator_get_supply(mmc);
Wolfram Sang51006952017-10-14 21:17:14 +02001782 if (ret)
Bjorn Andersson9369c972015-03-24 18:39:49 -07001783 goto clk_disable;
1784
Ulf Hansson599c1d52013-01-07 16:22:50 +01001785 if (!mmc->ocr_avail)
Linus Walleij34e84f32009-09-22 14:41:40 +01001786 mmc->ocr_avail = plat->ocr_mask;
Ulf Hansson599c1d52013-01-07 16:22:50 +01001787 else if (plat->ocr_mask)
1788 dev_warn(mmc_dev(mmc), "Platform OCR mask is ignored\n");
1789
Ulf Hansson9dd8a8b2014-03-19 13:54:18 +01001790 /* We support these capabilities. */
1791 mmc->caps |= MMC_CAP_CMD23;
1792
Linus Walleij49adc0c2016-10-25 11:06:06 +02001793 /*
1794 * Enable busy detection.
1795 */
Ulf Hansson8d94b542014-01-13 16:49:31 +01001796 if (variant->busy_detect) {
1797 mmci_ops.card_busy = mmci_card_busy;
Linus Walleij49adc0c2016-10-25 11:06:06 +02001798 /*
1799 * Not all variants have a flag to enable busy detection
1800 * in the DPSM, but if they do, set it here.
1801 */
1802 if (variant->busy_dpsm_flag)
1803 mmci_write_datactrlreg(host,
1804 host->variant->busy_dpsm_flag);
Ulf Hansson8d94b542014-01-13 16:49:31 +01001805 mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY;
1806 mmc->max_busy_timeout = 0;
1807 }
1808
1809 mmc->ops = &mmci_ops;
1810
Ulf Hansson70be2082013-01-07 15:35:06 +01001811 /* We support these PM capabilities. */
Ulf Hansson78f87df2014-03-17 15:53:07 +01001812 mmc->pm_caps |= MMC_PM_KEEP_POWER;
Ulf Hansson70be2082013-01-07 15:35:06 +01001813
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 /*
1815 * We can do SGIO
1816 */
Martin K. Petersena36274e2010-09-10 01:33:59 -04001817 mmc->max_segs = NR_SG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
1819 /*
Rabin Vincent08458ef2010-07-21 12:55:59 +01001820 * Since only a certain number of bits are valid in the data length
1821 * register, we must ensure that we don't exceed 2^num-1 bytes in a
1822 * single request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 */
Rabin Vincent08458ef2010-07-21 12:55:59 +01001824 mmc->max_req_size = (1 << variant->datalength_bits) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
1826 /*
1827 * Set the maximum segment size. Since we aren't doing DMA
1828 * (yet) we are only limited by the data length register.
1829 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001830 mmc->max_seg_size = mmc->max_req_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001832 /*
1833 * Block size can be up to 2048 bytes, but must be a power of two.
1834 */
Will Deacon8f7f6b7e2012-02-24 11:25:21 +00001835 mmc->max_blk_size = 1 << 11;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001836
Pierre Ossman55db8902006-11-21 17:55:45 +01001837 /*
Will Deacon8f7f6b7e2012-02-24 11:25:21 +00001838 * Limit the number of blocks transferred so that we don't overflow
1839 * the maximum request size.
Pierre Ossman55db8902006-11-21 17:55:45 +01001840 */
Will Deacon8f7f6b7e2012-02-24 11:25:21 +00001841 mmc->max_blk_count = mmc->max_req_size >> 11;
Pierre Ossman55db8902006-11-21 17:55:45 +01001842
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 spin_lock_init(&host->lock);
1844
1845 writel(0, host->base + MMCIMASK0);
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +01001846
1847 if (variant->mmcimask1)
1848 writel(0, host->base + MMCIMASK1);
1849
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 writel(0xfff, host->base + MMCICLEAR);
1851
Linus Walleijce437aa2014-08-27 15:13:54 +02001852 /*
1853 * If:
1854 * - not using DT but using a descriptor table, or
1855 * - using a table of descriptors ALONGSIDE DT, or
1856 * look up these descriptors named "cd" and "wp" right here, fail
Linus Walleij9ef986a2018-09-20 16:01:10 -07001857 * silently of these do not exist
Linus Walleijce437aa2014-08-27 15:13:54 +02001858 */
1859 if (!np) {
Linus Walleij89168b42014-10-02 09:08:46 +02001860 ret = mmc_gpiod_request_cd(mmc, "cd", 0, false, 0, NULL);
Linus Walleij9ef986a2018-09-20 16:01:10 -07001861 if (ret == -EPROBE_DEFER)
1862 goto clk_disable;
Linus Walleijce437aa2014-08-27 15:13:54 +02001863
Linus Walleij89168b42014-10-02 09:08:46 +02001864 ret = mmc_gpiod_request_ro(mmc, "wp", 0, false, 0, NULL);
Linus Walleij9ef986a2018-09-20 16:01:10 -07001865 if (ret == -EPROBE_DEFER)
1866 goto clk_disable;
Russell King89001442009-07-09 15:16:07 +01001867 }
1868
Ulf Hanssonef289982014-03-17 13:56:32 +01001869 ret = devm_request_irq(&dev->dev, dev->irq[0], mmci_irq, IRQF_SHARED,
1870 DRIVER_NAME " (cmd)", host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 if (ret)
Ulf Hanssonef289982014-03-17 13:56:32 +01001872 goto clk_disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
Russell Kingdfb85182012-05-03 11:33:15 +01001874 if (!dev->irq[1])
Linus Walleij2686b4b2010-10-19 12:39:48 +01001875 host->singleirq = true;
1876 else {
Ulf Hanssonef289982014-03-17 13:56:32 +01001877 ret = devm_request_irq(&dev->dev, dev->irq[1], mmci_pio_irq,
1878 IRQF_SHARED, DRIVER_NAME " (pio)", host);
Linus Walleij2686b4b2010-10-19 12:39:48 +01001879 if (ret)
Ulf Hanssonef289982014-03-17 13:56:32 +01001880 goto clk_disable;
Linus Walleij2686b4b2010-10-19 12:39:48 +01001881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Linus Walleij8cb28152011-01-24 15:22:13 +01001883 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
1885 amba_set_drvdata(dev, mmc);
1886
Russell Kingc8ebae32011-01-11 19:35:53 +00001887 dev_info(&dev->dev, "%s: PL%03x manf %x rev%u at 0x%08llx irq %d,%d (pio)\n",
1888 mmc_hostname(mmc), amba_part(dev), amba_manf(dev),
1889 amba_rev(dev), (unsigned long long)dev->res.start,
1890 dev->irq[0], dev->irq[1]);
1891
1892 mmci_dma_setup(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
Ulf Hansson2cd976c2011-12-13 17:01:11 +01001894 pm_runtime_set_autosuspend_delay(&dev->dev, 50);
1895 pm_runtime_use_autosuspend(&dev->dev);
Russell King1c3be362011-08-14 09:17:05 +01001896
Russell King8c11a942010-12-28 19:40:40 +00001897 mmc_add_host(mmc);
1898
Ulf Hansson6f2d3c82014-12-11 14:35:55 +01001899 pm_runtime_put(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 return 0;
1901
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 clk_disable:
Julia Lawallac940932012-08-26 16:00:59 +00001903 clk_disable_unprepare(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 host_free:
1905 mmc_free_host(mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 return ret;
1907}
1908
Bill Pemberton6e0ee712012-11-19 13:26:03 -05001909static int mmci_remove(struct amba_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910{
1911 struct mmc_host *mmc = amba_get_drvdata(dev);
1912
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 if (mmc) {
1914 struct mmci_host *host = mmc_priv(mmc);
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +01001915 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
Russell King1c3be362011-08-14 09:17:05 +01001917 /*
1918 * Undo pm_runtime_put() in probe. We use the _sync
1919 * version here so that we can access the primecell.
1920 */
1921 pm_runtime_get_sync(&dev->dev);
1922
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 mmc_remove_host(mmc);
1924
1925 writel(0, host->base + MMCIMASK0);
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +01001926
1927 if (variant->mmcimask1)
1928 writel(0, host->base + MMCIMASK1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
1930 writel(0, host->base + MMCICOMMAND);
1931 writel(0, host->base + MMCIDATACTRL);
1932
Russell Kingc8ebae32011-01-11 19:35:53 +00001933 mmci_dma_release(host);
Julia Lawallac940932012-08-26 16:00:59 +00001934 clk_disable_unprepare(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 mmc_free_host(mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 }
1937
1938 return 0;
1939}
1940
Ulf Hansson571dce42014-01-23 00:38:00 +01001941#ifdef CONFIG_PM
Ulf Hansson1ff44432013-09-04 09:05:17 +01001942static void mmci_save(struct mmci_host *host)
1943{
1944 unsigned long flags;
1945
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001946 spin_lock_irqsave(&host->lock, flags);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001947
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001948 writel(0, host->base + MMCIMASK0);
1949 if (host->variant->pwrreg_nopower) {
Ulf Hansson1ff44432013-09-04 09:05:17 +01001950 writel(0, host->base + MMCIDATACTRL);
1951 writel(0, host->base + MMCIPOWER);
1952 writel(0, host->base + MMCICLOCK);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001953 }
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001954 mmci_reg_delay(host);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001955
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001956 spin_unlock_irqrestore(&host->lock, flags);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001957}
1958
1959static void mmci_restore(struct mmci_host *host)
1960{
1961 unsigned long flags;
1962
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001963 spin_lock_irqsave(&host->lock, flags);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001964
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001965 if (host->variant->pwrreg_nopower) {
Ulf Hansson1ff44432013-09-04 09:05:17 +01001966 writel(host->clk_reg, host->base + MMCICLOCK);
1967 writel(host->datactrl_reg, host->base + MMCIDATACTRL);
1968 writel(host->pwr_reg, host->base + MMCIPOWER);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001969 }
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001970 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
1971 mmci_reg_delay(host);
1972
1973 spin_unlock_irqrestore(&host->lock, flags);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001974}
1975
Ulf Hansson82592932013-01-09 11:15:26 +01001976static int mmci_runtime_suspend(struct device *dev)
1977{
1978 struct amba_device *adev = to_amba_device(dev);
1979 struct mmc_host *mmc = amba_get_drvdata(adev);
1980
1981 if (mmc) {
1982 struct mmci_host *host = mmc_priv(mmc);
Ulf Hanssone36bd9c62013-09-04 09:00:37 +01001983 pinctrl_pm_select_sleep_state(dev);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001984 mmci_save(host);
Ulf Hansson82592932013-01-09 11:15:26 +01001985 clk_disable_unprepare(host->clk);
1986 }
1987
1988 return 0;
1989}
1990
1991static int mmci_runtime_resume(struct device *dev)
1992{
1993 struct amba_device *adev = to_amba_device(dev);
1994 struct mmc_host *mmc = amba_get_drvdata(adev);
1995
1996 if (mmc) {
1997 struct mmci_host *host = mmc_priv(mmc);
1998 clk_prepare_enable(host->clk);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001999 mmci_restore(host);
Ulf Hanssone36bd9c62013-09-04 09:00:37 +01002000 pinctrl_pm_select_default_state(dev);
Ulf Hansson82592932013-01-09 11:15:26 +01002001 }
2002
2003 return 0;
2004}
2005#endif
2006
Ulf Hansson48fa7002011-12-13 16:59:34 +01002007static const struct dev_pm_ops mmci_dev_pm_ops = {
Ulf Hanssonf3737fa2014-01-23 01:11:33 +01002008 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
2009 pm_runtime_force_resume)
Rafael J. Wysocki6ed23b82014-12-04 00:34:11 +01002010 SET_RUNTIME_PM_OPS(mmci_runtime_suspend, mmci_runtime_resume, NULL)
Ulf Hansson48fa7002011-12-13 16:59:34 +01002011};
2012
Arvind Yadav88411de2017-08-23 22:00:49 +05302013static const struct amba_id mmci_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 {
2015 .id = 0x00041180,
Pawel Moll768fbc12011-03-11 17:18:07 +00002016 .mask = 0xff0fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01002017 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 },
2019 {
Pawel Moll768fbc12011-03-11 17:18:07 +00002020 .id = 0x01041180,
2021 .mask = 0xff0fffff,
2022 .data = &variant_arm_extended_fifo,
2023 },
2024 {
Pawel Moll3a372982013-01-24 14:12:45 +01002025 .id = 0x02041180,
2026 .mask = 0xff0fffff,
2027 .data = &variant_arm_extended_fifo_hwfc,
2028 },
2029 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 .id = 0x00041181,
2031 .mask = 0x000fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01002032 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 },
Linus Walleijcc30d602009-01-04 15:18:54 +01002034 /* ST Micro variants */
2035 {
2036 .id = 0x00180180,
2037 .mask = 0x00ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01002038 .data = &variant_u300,
Linus Walleijcc30d602009-01-04 15:18:54 +01002039 },
2040 {
Linus Walleij34fd4212012-04-10 17:43:59 +01002041 .id = 0x10180180,
2042 .mask = 0xf0ffffff,
2043 .data = &variant_nomadik,
2044 },
2045 {
Linus Walleijcc30d602009-01-04 15:18:54 +01002046 .id = 0x00280180,
2047 .mask = 0x00ffffff,
Linus Walleij0bcb7ef2016-01-04 02:21:55 +01002048 .data = &variant_nomadik,
Rabin Vincent4956e102010-07-21 12:54:40 +01002049 },
2050 {
2051 .id = 0x00480180,
Philippe Langlais1784b152011-03-25 08:51:52 +01002052 .mask = 0xf0ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01002053 .data = &variant_ux500,
Linus Walleijcc30d602009-01-04 15:18:54 +01002054 },
Philippe Langlais1784b152011-03-25 08:51:52 +01002055 {
2056 .id = 0x10480180,
2057 .mask = 0xf0ffffff,
2058 .data = &variant_ux500v2,
2059 },
Patrice Chotard2a9d6c82018-01-18 15:34:21 +01002060 {
2061 .id = 0x00880180,
2062 .mask = 0x00ffffff,
2063 .data = &variant_stm32,
2064 },
Srinivas Kandagatla55b604a2014-06-02 10:10:13 +01002065 /* Qualcomm variants */
2066 {
2067 .id = 0x00051180,
2068 .mask = 0x000fffff,
2069 .data = &variant_qcom,
2070 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 { 0, 0 },
2072};
2073
Dave Martin9f998352011-10-05 15:15:21 +01002074MODULE_DEVICE_TABLE(amba, mmci_ids);
2075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076static struct amba_driver mmci_driver = {
2077 .drv = {
2078 .name = DRIVER_NAME,
Ulf Hansson48fa7002011-12-13 16:59:34 +01002079 .pm = &mmci_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 },
2081 .probe = mmci_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05002082 .remove = mmci_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 .id_table = mmci_ids,
2084};
2085
viresh kumar9e5ed092012-03-15 10:40:38 +01002086module_amba_driver(mmci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088module_param(fmax, uint, 0444);
2089
2090MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
2091MODULE_LICENSE("GPL");