blob: 4570042319e9c2cd3b9029cc0aaeb256fd098ff1 [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
Ludovic Barre135ea302018-10-08 14:08:38 +0200428int mmci_dma_start(struct mmci_host *host, unsigned int datactrl)
429{
430 struct mmc_data *data = host->data;
431 int ret;
432
433 if (!host->use_dma)
434 return -EINVAL;
435
436 ret = mmci_prep_data(host, data, false);
437 if (ret)
438 return ret;
439
440 if (!host->ops || !host->ops->dma_start)
441 return -EINVAL;
442
443 /* Okay, go for it. */
444 dev_vdbg(mmc_dev(host->mmc),
445 "Submit MMCI DMA job, sglen %d blksz %04x blks %04x flags %08x\n",
446 data->sg_len, data->blksz, data->blocks, data->flags);
447
448 host->ops->dma_start(host, &datactrl);
449
450 /* Trigger the DMA transfer */
451 mmci_write_datactrlreg(host, datactrl);
452
453 /*
454 * Let the MMCI say when the data is ended and it's time
455 * to fire next DMA request. When that happens, MMCI will
456 * call mmci_data_end()
457 */
458 writel(readl(host->base + MMCIMASK0) | MCI_DATAENDMASK,
459 host->base + MMCIMASK0);
460 return 0;
461}
462
Ludovic Barre5a9f10c2018-10-08 14:08:39 +0200463void mmci_dma_finalize(struct mmci_host *host, struct mmc_data *data)
464{
465 if (!host->use_dma)
466 return;
467
468 if (host->ops && host->ops->dma_finalize)
469 host->ops->dma_finalize(host, data);
470}
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472static void
473mmci_request_end(struct mmci_host *host, struct mmc_request *mrq)
474{
475 writel(0, host->base + MMCICOMMAND);
476
Russell Kinge47c2222007-01-08 16:42:51 +0000477 BUG_ON(host->data);
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 host->mrq = NULL;
480 host->cmd = NULL;
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 mmc_request_done(host->mmc, mrq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
Linus Walleij2686b4b2010-10-19 12:39:48 +0100485static void mmci_set_mask1(struct mmci_host *host, unsigned int mask)
486{
487 void __iomem *base = host->base;
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100488 struct variant_data *variant = host->variant;
Linus Walleij2686b4b2010-10-19 12:39:48 +0100489
490 if (host->singleirq) {
491 unsigned int mask0 = readl(base + MMCIMASK0);
492
493 mask0 &= ~MCI_IRQ1MASK;
494 mask0 |= mask;
495
496 writel(mask0, base + MMCIMASK0);
497 }
498
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100499 if (variant->mmcimask1)
500 writel(mask, base + MMCIMASK1);
501
502 host->mask1_reg = mask;
Linus Walleij2686b4b2010-10-19 12:39:48 +0100503}
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505static void mmci_stop_data(struct mmci_host *host)
506{
Ulf Hansson9cc639a2013-05-15 20:48:23 +0100507 mmci_write_datactrlreg(host, 0);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100508 mmci_set_mask1(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 host->data = NULL;
510}
511
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100512static void mmci_init_sg(struct mmci_host *host, struct mmc_data *data)
513{
514 unsigned int flags = SG_MITER_ATOMIC;
515
516 if (data->flags & MMC_DATA_READ)
517 flags |= SG_MITER_TO_SG;
518 else
519 flags |= SG_MITER_FROM_SG;
520
521 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
522}
523
Russell Kingc8ebae32011-01-11 19:35:53 +0000524/*
525 * All the DMA operation mode stuff goes inside this ifdef.
526 * This assumes that you have a generic DMA device interface,
527 * no custom DMA interfaces are supported.
528 */
529#ifdef CONFIG_DMA_ENGINE
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200530struct mmci_dmae_next {
531 struct dma_async_tx_descriptor *desc;
532 struct dma_chan *chan;
533};
534
535struct mmci_dmae_priv {
536 struct dma_chan *cur;
537 struct dma_chan *rx_channel;
538 struct dma_chan *tx_channel;
539 struct dma_async_tx_descriptor *desc_current;
540 struct mmci_dmae_next next_data;
541};
542
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200543int mmci_dmae_setup(struct mmci_host *host)
Russell Kingc8ebae32011-01-11 19:35:53 +0000544{
Russell Kingc8ebae32011-01-11 19:35:53 +0000545 const char *rxname, *txname;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200546 struct mmci_dmae_priv *dmae;
Russell Kingc8ebae32011-01-11 19:35:53 +0000547
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200548 dmae = devm_kzalloc(mmc_dev(host->mmc), sizeof(*dmae), GFP_KERNEL);
549 if (!dmae)
550 return -ENOMEM;
Russell Kingc8ebae32011-01-11 19:35:53 +0000551
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200552 host->dma_priv = dmae;
553
554 dmae->rx_channel = dma_request_slave_channel(mmc_dev(host->mmc),
555 "rx");
556 dmae->tx_channel = dma_request_slave_channel(mmc_dev(host->mmc),
557 "tx");
Per Forlin58c7ccb2011-07-01 18:55:24 +0200558
Russell Kingc8ebae32011-01-11 19:35:53 +0000559 /*
560 * If only an RX channel is specified, the driver will
561 * attempt to use it bidirectionally, however if it is
562 * is specified but cannot be located, DMA will be disabled.
563 */
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200564 if (dmae->rx_channel && !dmae->tx_channel)
565 dmae->tx_channel = dmae->rx_channel;
Russell Kingc8ebae32011-01-11 19:35:53 +0000566
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200567 if (dmae->rx_channel)
568 rxname = dma_chan_name(dmae->rx_channel);
Russell Kingc8ebae32011-01-11 19:35:53 +0000569 else
570 rxname = "none";
571
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200572 if (dmae->tx_channel)
573 txname = dma_chan_name(dmae->tx_channel);
Russell Kingc8ebae32011-01-11 19:35:53 +0000574 else
575 txname = "none";
576
577 dev_info(mmc_dev(host->mmc), "DMA channels RX %s, TX %s\n",
578 rxname, txname);
579
580 /*
581 * Limit the maximum segment size in any SG entry according to
582 * the parameters of the DMA engine device.
583 */
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200584 if (dmae->tx_channel) {
585 struct device *dev = dmae->tx_channel->device->dev;
Russell Kingc8ebae32011-01-11 19:35:53 +0000586 unsigned int max_seg_size = dma_get_max_seg_size(dev);
587
588 if (max_seg_size < host->mmc->max_seg_size)
589 host->mmc->max_seg_size = max_seg_size;
590 }
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200591 if (dmae->rx_channel) {
592 struct device *dev = dmae->rx_channel->device->dev;
Russell Kingc8ebae32011-01-11 19:35:53 +0000593 unsigned int max_seg_size = dma_get_max_seg_size(dev);
594
595 if (max_seg_size < host->mmc->max_seg_size)
596 host->mmc->max_seg_size = max_seg_size;
597 }
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100598
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200599 if (!dmae->tx_channel || !dmae->rx_channel) {
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200600 mmci_dmae_release(host);
601 return -EINVAL;
602 }
603
604 return 0;
Russell Kingc8ebae32011-01-11 19:35:53 +0000605}
606
607/*
Bill Pemberton6e0ee712012-11-19 13:26:03 -0500608 * This is used in or so inline it
Russell Kingc8ebae32011-01-11 19:35:53 +0000609 * so it can be discarded.
610 */
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200611void mmci_dmae_release(struct mmci_host *host)
Russell Kingc8ebae32011-01-11 19:35:53 +0000612{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200613 struct mmci_dmae_priv *dmae = host->dma_priv;
614
615 if (dmae->rx_channel)
616 dma_release_channel(dmae->rx_channel);
617 if (dmae->tx_channel)
618 dma_release_channel(dmae->tx_channel);
619 dmae->rx_channel = dmae->tx_channel = NULL;
Russell Kingc8ebae32011-01-11 19:35:53 +0000620}
621
622static void mmci_dma_unmap(struct mmci_host *host, struct mmc_data *data)
623{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200624 struct mmci_dmae_priv *dmae = host->dma_priv;
Ulf Hansson653a7612013-01-21 21:29:34 +0100625 struct dma_chan *chan;
Ulf Hansson653a7612013-01-21 21:29:34 +0100626
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200627 if (data->flags & MMC_DATA_READ)
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200628 chan = dmae->rx_channel;
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200629 else
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200630 chan = dmae->tx_channel;
Ulf Hansson653a7612013-01-21 21:29:34 +0100631
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200632 dma_unmap_sg(chan->device->dev, data->sg, data->sg_len,
633 mmc_get_dma_dir(data));
Ulf Hansson653a7612013-01-21 21:29:34 +0100634}
635
Ludovic Barre7b2a6d52018-09-21 11:45:55 +0200636static void mmci_dma_data_error(struct mmci_host *host)
637{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200638 struct mmci_dmae_priv *dmae = host->dma_priv;
639
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200640 if (!host->use_dma || !dma_inprogress(host))
Ludovic Barrecdea1942018-09-21 11:45:56 +0200641 return;
642
Ludovic Barre7b2a6d52018-09-21 11:45:55 +0200643 dev_err(mmc_dev(host->mmc), "error during DMA transfer!\n");
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200644 dmaengine_terminate_all(dmae->cur);
Ludovic Barre7b2a6d52018-09-21 11:45:55 +0200645 host->dma_in_progress = false;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200646 dmae->cur = NULL;
647 dmae->desc_current = NULL;
Ludovic Barre7b2a6d52018-09-21 11:45:55 +0200648 host->data->host_cookie = 0;
649
650 mmci_dma_unmap(host, host->data);
651}
652
Ludovic Barre5a9f10c2018-10-08 14:08:39 +0200653void mmci_dmae_finalize(struct mmci_host *host, struct mmc_data *data)
Ulf Hansson653a7612013-01-21 21:29:34 +0100654{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200655 struct mmci_dmae_priv *dmae = host->dma_priv;
Russell Kingc8ebae32011-01-11 19:35:53 +0000656 u32 status;
657 int i;
658
Ludovic Barre5a9f10c2018-10-08 14:08:39 +0200659 if (!dma_inprogress(host))
Ludovic Barrecdea1942018-09-21 11:45:56 +0200660 return;
661
Russell Kingc8ebae32011-01-11 19:35:53 +0000662 /* Wait up to 1ms for the DMA to complete */
663 for (i = 0; ; i++) {
664 status = readl(host->base + MMCISTATUS);
665 if (!(status & MCI_RXDATAAVLBLMASK) || i >= 100)
666 break;
667 udelay(10);
668 }
669
670 /*
671 * Check to see whether we still have some data left in the FIFO -
672 * this catches DMA controllers which are unable to monitor the
673 * DMALBREQ and DMALSREQ signals while allowing us to DMA to non-
674 * contiguous buffers. On TX, we'll get a FIFO underrun error.
675 */
676 if (status & MCI_RXDATAAVLBLMASK) {
Ulf Hansson653a7612013-01-21 21:29:34 +0100677 mmci_dma_data_error(host);
Russell Kingc8ebae32011-01-11 19:35:53 +0000678 if (!data->error)
679 data->error = -EIO;
Ludovic Barre7b2a6d52018-09-21 11:45:55 +0200680 } else if (!data->host_cookie) {
Ulf Hansson653a7612013-01-21 21:29:34 +0100681 mmci_dma_unmap(host, data);
Ludovic Barre7b2a6d52018-09-21 11:45:55 +0200682 }
Russell Kingc8ebae32011-01-11 19:35:53 +0000683
684 /*
685 * Use of DMA with scatter-gather is impossible.
686 * Give up with DMA and switch back to PIO mode.
687 */
688 if (status & MCI_RXDATAAVLBLMASK) {
689 dev_err(mmc_dev(host->mmc), "buggy DMA detected. Taking evasive action.\n");
690 mmci_dma_release(host);
691 }
Ulf Hansson653a7612013-01-21 21:29:34 +0100692
Linus Walleije13934b2017-01-27 15:04:54 +0100693 host->dma_in_progress = false;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200694 dmae->cur = NULL;
695 dmae->desc_current = NULL;
Russell Kingc8ebae32011-01-11 19:35:53 +0000696}
697
Ulf Hansson653a7612013-01-21 21:29:34 +0100698/* prepares DMA channel and DMA descriptor, returns non-zero on failure */
Ludovic Barre47983512018-10-08 14:08:36 +0200699static int _mmci_dmae_prep_data(struct mmci_host *host, struct mmc_data *data,
Ulf Hansson653a7612013-01-21 21:29:34 +0100700 struct dma_chan **dma_chan,
701 struct dma_async_tx_descriptor **dma_desc)
Russell Kingc8ebae32011-01-11 19:35:53 +0000702{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200703 struct mmci_dmae_priv *dmae = host->dma_priv;
Russell Kingc8ebae32011-01-11 19:35:53 +0000704 struct variant_data *variant = host->variant;
705 struct dma_slave_config conf = {
706 .src_addr = host->phybase + MMCIFIFO,
707 .dst_addr = host->phybase + MMCIFIFO,
708 .src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
709 .dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
710 .src_maxburst = variant->fifohalfsize >> 2, /* # of words */
711 .dst_maxburst = variant->fifohalfsize >> 2, /* # of words */
Viresh Kumar258aea72012-02-01 16:12:19 +0530712 .device_fc = false,
Russell Kingc8ebae32011-01-11 19:35:53 +0000713 };
Russell Kingc8ebae32011-01-11 19:35:53 +0000714 struct dma_chan *chan;
715 struct dma_device *device;
716 struct dma_async_tx_descriptor *desc;
717 int nr_sg;
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100718 unsigned long flags = DMA_CTRL_ACK;
Russell Kingc8ebae32011-01-11 19:35:53 +0000719
Russell Kingc8ebae32011-01-11 19:35:53 +0000720 if (data->flags & MMC_DATA_READ) {
Vinod Koul05f57992011-10-14 10:45:11 +0530721 conf.direction = DMA_DEV_TO_MEM;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200722 chan = dmae->rx_channel;
Russell Kingc8ebae32011-01-11 19:35:53 +0000723 } else {
Vinod Koul05f57992011-10-14 10:45:11 +0530724 conf.direction = DMA_MEM_TO_DEV;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200725 chan = dmae->tx_channel;
Russell Kingc8ebae32011-01-11 19:35:53 +0000726 }
727
728 /* If there's no DMA channel, fall back to PIO */
729 if (!chan)
730 return -EINVAL;
731
732 /* If less than or equal to the fifo size, don't bother with DMA */
Per Forlin58c7ccb2011-07-01 18:55:24 +0200733 if (data->blksz * data->blocks <= variant->fifosize)
Russell Kingc8ebae32011-01-11 19:35:53 +0000734 return -EINVAL;
735
736 device = chan->device;
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200737 nr_sg = dma_map_sg(device->dev, data->sg, data->sg_len,
738 mmc_get_dma_dir(data));
Russell Kingc8ebae32011-01-11 19:35:53 +0000739 if (nr_sg == 0)
740 return -EINVAL;
741
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100742 if (host->variant->qcom_dml)
743 flags |= DMA_PREP_INTERRUPT;
744
Russell Kingc8ebae32011-01-11 19:35:53 +0000745 dmaengine_slave_config(chan, &conf);
Alexandre Bounine16052822012-03-08 16:11:18 -0500746 desc = dmaengine_prep_slave_sg(chan, data->sg, nr_sg,
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100747 conf.direction, flags);
Russell Kingc8ebae32011-01-11 19:35:53 +0000748 if (!desc)
749 goto unmap_exit;
750
Ulf Hansson653a7612013-01-21 21:29:34 +0100751 *dma_chan = chan;
752 *dma_desc = desc;
Russell Kingc8ebae32011-01-11 19:35:53 +0000753
Per Forlin58c7ccb2011-07-01 18:55:24 +0200754 return 0;
755
756 unmap_exit:
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200757 dma_unmap_sg(device->dev, data->sg, data->sg_len,
758 mmc_get_dma_dir(data));
Per Forlin58c7ccb2011-07-01 18:55:24 +0200759 return -ENOMEM;
760}
761
Ludovic Barre47983512018-10-08 14:08:36 +0200762int mmci_dmae_prep_data(struct mmci_host *host,
763 struct mmc_data *data,
764 bool next)
Ulf Hansson653a7612013-01-21 21:29:34 +0100765{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200766 struct mmci_dmae_priv *dmae = host->dma_priv;
Ludovic Barread7b8912018-10-08 14:08:35 +0200767 struct mmci_dmae_next *nd = &dmae->next_data;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200768
Ludovic Barre47983512018-10-08 14:08:36 +0200769 if (!host->use_dma)
770 return -EINVAL;
771
Ludovic Barread7b8912018-10-08 14:08:35 +0200772 if (next)
Ludovic Barre47983512018-10-08 14:08:36 +0200773 return _mmci_dmae_prep_data(host, data, &nd->chan, &nd->desc);
Ulf Hansson653a7612013-01-21 21:29:34 +0100774 /* Check if next job is already prepared. */
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200775 if (dmae->cur && dmae->desc_current)
Ulf Hansson653a7612013-01-21 21:29:34 +0100776 return 0;
777
778 /* No job were prepared thus do it now. */
Ludovic Barre47983512018-10-08 14:08:36 +0200779 return _mmci_dmae_prep_data(host, data, &dmae->cur,
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200780 &dmae->desc_current);
Ulf Hansson653a7612013-01-21 21:29:34 +0100781}
782
Ludovic Barre135ea302018-10-08 14:08:38 +0200783int mmci_dmae_start(struct mmci_host *host, unsigned int *datactrl)
Per Forlin58c7ccb2011-07-01 18:55:24 +0200784{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200785 struct mmci_dmae_priv *dmae = host->dma_priv;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200786 struct mmc_data *data = host->data;
787
Linus Walleije13934b2017-01-27 15:04:54 +0100788 host->dma_in_progress = true;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200789 dmaengine_submit(dmae->desc_current);
790 dma_async_issue_pending(dmae->cur);
Russell Kingc8ebae32011-01-11 19:35:53 +0000791
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100792 if (host->variant->qcom_dml)
793 dml_start_xfer(host, data);
794
Ludovic Barre135ea302018-10-08 14:08:38 +0200795 *datactrl |= MCI_DPSM_DMAENABLE;
Russell Kingc8ebae32011-01-11 19:35:53 +0000796
Russell Kingc8ebae32011-01-11 19:35:53 +0000797 return 0;
Russell Kingc8ebae32011-01-11 19:35:53 +0000798}
Per Forlin58c7ccb2011-07-01 18:55:24 +0200799
Ludovic Barre02769962018-10-08 14:08:37 +0200800void mmci_dmae_get_next_data(struct mmci_host *host, struct mmc_data *data)
Per Forlin58c7ccb2011-07-01 18:55:24 +0200801{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200802 struct mmci_dmae_priv *dmae = host->dma_priv;
803 struct mmci_dmae_next *next = &dmae->next_data;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200804
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200805 if (!host->use_dma)
806 return;
807
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200808 WARN_ON(!data->host_cookie && (next->desc || next->chan));
Per Forlin58c7ccb2011-07-01 18:55:24 +0200809
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200810 dmae->desc_current = next->desc;
811 dmae->cur = next->chan;
812 next->desc = NULL;
813 next->chan = NULL;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200814}
815
Ludovic Barre47983512018-10-08 14:08:36 +0200816void mmci_dmae_unprep_data(struct mmci_host *host,
817 struct mmc_data *data, int err)
818
Per Forlin58c7ccb2011-07-01 18:55:24 +0200819{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200820 struct mmci_dmae_priv *dmae = host->dma_priv;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200821
Ludovic Barre47983512018-10-08 14:08:36 +0200822 if (!host->use_dma)
Per Forlin58c7ccb2011-07-01 18:55:24 +0200823 return;
824
Ulf Hansson653a7612013-01-21 21:29:34 +0100825 mmci_dma_unmap(host, data);
Per Forlin58c7ccb2011-07-01 18:55:24 +0200826
Ulf Hansson653a7612013-01-21 21:29:34 +0100827 if (err) {
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200828 struct mmci_dmae_next *next = &dmae->next_data;
Ulf Hansson653a7612013-01-21 21:29:34 +0100829 struct dma_chan *chan;
830 if (data->flags & MMC_DATA_READ)
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200831 chan = dmae->rx_channel;
Ulf Hansson653a7612013-01-21 21:29:34 +0100832 else
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200833 chan = dmae->tx_channel;
Ulf Hansson653a7612013-01-21 21:29:34 +0100834 dmaengine_terminate_all(chan);
Per Forlin58c7ccb2011-07-01 18:55:24 +0200835
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200836 if (dmae->desc_current == next->desc)
837 dmae->desc_current = NULL;
Srinivas Kandagatlab5c16a62014-10-08 12:25:17 +0100838
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200839 if (dmae->cur == next->chan) {
Linus Walleije13934b2017-01-27 15:04:54 +0100840 host->dma_in_progress = false;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200841 dmae->cur = NULL;
Linus Walleije13934b2017-01-27 15:04:54 +0100842 }
Srinivas Kandagatlab5c16a62014-10-08 12:25:17 +0100843
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200844 next->desc = NULL;
845 next->chan = NULL;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200846 }
847}
848
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200849static struct mmci_host_ops mmci_variant_ops = {
Ludovic Barre47983512018-10-08 14:08:36 +0200850 .prep_data = mmci_dmae_prep_data,
851 .unprep_data = mmci_dmae_unprep_data,
Ludovic Barre02769962018-10-08 14:08:37 +0200852 .get_next_data = mmci_dmae_get_next_data,
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200853 .dma_setup = mmci_dmae_setup,
854 .dma_release = mmci_dmae_release,
Ludovic Barre135ea302018-10-08 14:08:38 +0200855 .dma_start = mmci_dmae_start,
Ludovic Barre5a9f10c2018-10-08 14:08:39 +0200856 .dma_finalize = mmci_dmae_finalize,
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200857};
858
859void mmci_variant_init(struct mmci_host *host)
860{
861 host->ops = &mmci_variant_ops;
862}
Russell Kingc8ebae32011-01-11 19:35:53 +0000863#else
864/* Blank functions if the DMA engine is not available */
Russell Kingc8ebae32011-01-11 19:35:53 +0000865static inline void mmci_dma_data_error(struct mmci_host *host)
866{
867}
868
Russell Kingc8ebae32011-01-11 19:35:53 +0000869#endif
870
Ludovic Barre47983512018-10-08 14:08:36 +0200871static void mmci_pre_request(struct mmc_host *mmc, struct mmc_request *mrq)
872{
873 struct mmci_host *host = mmc_priv(mmc);
874 struct mmc_data *data = mrq->data;
875
876 if (!data)
877 return;
878
879 WARN_ON(data->host_cookie);
880
881 if (mmci_validate_data(host, data))
882 return;
883
884 mmci_prep_data(host, data, true);
885}
886
887static void mmci_post_request(struct mmc_host *mmc, struct mmc_request *mrq,
888 int err)
889{
890 struct mmci_host *host = mmc_priv(mmc);
891 struct mmc_data *data = mrq->data;
892
893 if (!data || !data->host_cookie)
894 return;
895
896 mmci_unprep_data(host, data, err);
897}
898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899static void mmci_start_data(struct mmci_host *host, struct mmc_data *data)
900{
Rabin Vincent8301bb62010-08-09 12:57:30 +0100901 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 unsigned int datactrl, timeout, irqmask;
Russell King7b09cda2005-07-01 12:02:59 +0100903 unsigned long long clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 void __iomem *base;
Russell King3bc87f22006-08-27 13:51:28 +0100905 int blksz_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Linus Walleij64de0282010-02-19 01:09:10 +0100907 dev_dbg(mmc_dev(host->mmc), "blksz %04x blks %04x flags %08x\n",
908 data->blksz, data->blocks, data->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 host->data = data;
Rabin Vincent528320d2010-07-21 12:49:49 +0100911 host->size = data->blksz * data->blocks;
Russell King51d43752011-01-27 10:56:52 +0000912 data->bytes_xfered = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Russell King7b09cda2005-07-01 12:02:59 +0100914 clks = (unsigned long long)data->timeout_ns * host->cclk;
Srinivas Kandagatlac4a35762014-06-02 10:08:39 +0100915 do_div(clks, NSEC_PER_SEC);
Russell King7b09cda2005-07-01 12:02:59 +0100916
917 timeout = data->timeout_clks + (unsigned int)clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 base = host->base;
920 writel(timeout, base + MMCIDATATIMER);
921 writel(host->size, base + MMCIDATALENGTH);
922
Russell King3bc87f22006-08-27 13:51:28 +0100923 blksz_bits = ffs(data->blksz) - 1;
924 BUG_ON(1 << blksz_bits != data->blksz);
925
Philippe Langlais1784b152011-03-25 08:51:52 +0100926 if (variant->blksz_datactrl16)
927 datactrl = MCI_DPSM_ENABLE | (data->blksz << 16);
Srinivas Kandagatlaff783232014-06-02 10:09:06 +0100928 else if (variant->blksz_datactrl4)
929 datactrl = MCI_DPSM_ENABLE | (data->blksz << 4);
Philippe Langlais1784b152011-03-25 08:51:52 +0100930 else
931 datactrl = MCI_DPSM_ENABLE | blksz_bits << 4;
Russell Kingc8ebae32011-01-11 19:35:53 +0000932
933 if (data->flags & MMC_DATA_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 datactrl |= MCI_DPSM_DIRECTION;
Russell Kingc8ebae32011-01-11 19:35:53 +0000935
Srinivas Kandagatlac7354132014-08-22 05:55:16 +0100936 if (host->mmc->card && mmc_card_sdio(host->mmc->card)) {
937 u32 clk;
Ulf Hansson7258db72011-12-13 17:05:28 +0100938
Srinivas Kandagatlac7354132014-08-22 05:55:16 +0100939 datactrl |= variant->datactrl_mask_sdio;
Ulf Hansson06c1a122012-10-12 14:01:50 +0100940
Srinivas Kandagatlac7354132014-08-22 05:55:16 +0100941 /*
942 * The ST Micro variant for SDIO small write transfers
943 * needs to have clock H/W flow control disabled,
944 * otherwise the transfer will not start. The threshold
945 * depends on the rate of MCLK.
946 */
947 if (variant->st_sdio && data->flags & MMC_DATA_WRITE &&
948 (host->size < 8 ||
949 (host->size <= 8 && host->mclk > 50000000)))
950 clk = host->clk_reg & ~variant->clkreg_enable;
951 else
952 clk = host->clk_reg | variant->clkreg_enable;
953
954 mmci_write_clkreg(host, clk);
955 }
Ulf Hansson06c1a122012-10-12 14:01:50 +0100956
Seungwon Jeon6dad6c92014-03-14 21:12:13 +0900957 if (host->mmc->ios.timing == MMC_TIMING_UHS_DDR50 ||
958 host->mmc->ios.timing == MMC_TIMING_MMC_DDR52)
Srinivas Kandagatlae17dca22014-06-02 10:09:15 +0100959 datactrl |= variant->datactrl_mask_ddrmode;
Ulf Hansson6dbb6ee2013-01-07 15:30:44 +0100960
Russell Kingc8ebae32011-01-11 19:35:53 +0000961 /*
962 * Attempt to use DMA operation mode, if this
963 * should fail, fall back to PIO mode
964 */
Ludovic Barre135ea302018-10-08 14:08:38 +0200965 if (!mmci_dma_start(host, datactrl))
Russell Kingc8ebae32011-01-11 19:35:53 +0000966 return;
967
968 /* IRQ mode, map the SG list for CPU reading/writing */
969 mmci_init_sg(host, data);
970
971 if (data->flags & MMC_DATA_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 irqmask = MCI_RXFIFOHALFFULLMASK;
Russell King0425a142006-02-16 16:48:31 +0000973
974 /*
Russell Kingc4d877c2011-01-27 09:50:13 +0000975 * If we have less than the fifo 'half-full' threshold to
976 * transfer, trigger a PIO interrupt as soon as any data
977 * is available.
Russell King0425a142006-02-16 16:48:31 +0000978 */
Russell Kingc4d877c2011-01-27 09:50:13 +0000979 if (host->size < variant->fifohalfsize)
Russell King0425a142006-02-16 16:48:31 +0000980 irqmask |= MCI_RXDATAAVLBLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 } else {
982 /*
983 * We don't actually need to include "FIFO empty" here
984 * since its implicit in "FIFO half empty".
985 */
986 irqmask = MCI_TXFIFOHALFEMPTYMASK;
987 }
988
Ulf Hansson9cc639a2013-05-15 20:48:23 +0100989 mmci_write_datactrlreg(host, datactrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 writel(readl(base + MMCIMASK0) & ~MCI_DATAENDMASK, base + MMCIMASK0);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100991 mmci_set_mask1(host, irqmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992}
993
994static void
995mmci_start_command(struct mmci_host *host, struct mmc_command *cmd, u32 c)
996{
997 void __iomem *base = host->base;
998
Linus Walleij64de0282010-02-19 01:09:10 +0100999 dev_dbg(mmc_dev(host->mmc), "op %02x arg %08x flags %08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 cmd->opcode, cmd->arg, cmd->flags);
1001
1002 if (readl(base + MMCICOMMAND) & MCI_CPSM_ENABLE) {
1003 writel(0, base + MMCICOMMAND);
Srinivas Kandagatla6adb2a82014-06-02 10:08:57 +01001004 mmci_reg_delay(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 }
1006
1007 c |= cmd->opcode | MCI_CPSM_ENABLE;
Russell Kinge9225172006-02-02 12:23:12 +00001008 if (cmd->flags & MMC_RSP_PRESENT) {
1009 if (cmd->flags & MMC_RSP_136)
1010 c |= MCI_CPSM_LONGRSP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 c |= MCI_CPSM_RESPONSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 }
1013 if (/*interrupt*/0)
1014 c |= MCI_CPSM_INTERRUPT;
1015
Srinivas Kandagatlaae7b0062014-06-02 10:09:39 +01001016 if (mmc_cmd_type(cmd) == MMC_CMD_ADTC)
1017 c |= host->variant->data_cmd_enable;
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 host->cmd = cmd;
1020
1021 writel(cmd->arg, base + MMCIARGUMENT);
1022 writel(c, base + MMCICOMMAND);
1023}
1024
1025static void
1026mmci_data_irq(struct mmci_host *host, struct mmc_data *data,
1027 unsigned int status)
1028{
Ulf Hansson1cb9da52014-06-12 14:42:23 +02001029 /* Make sure we have data to handle */
1030 if (!data)
1031 return;
1032
Linus Walleijf20f8f212010-10-19 13:41:24 +01001033 /* First check for errors */
Patrice Chotard7f7b5502018-01-18 15:34:18 +01001034 if (status & (MCI_DATACRCFAIL | MCI_DATATIMEOUT |
1035 host->variant->start_err |
1036 MCI_TXUNDERRUN | MCI_RXOVERRUN)) {
Linus Walleij8cb28152011-01-24 15:22:13 +01001037 u32 remain, success;
Linus Walleijf20f8f212010-10-19 13:41:24 +01001038
Russell Kingc8ebae32011-01-11 19:35:53 +00001039 /* Terminate the DMA transfer */
Ludovic Barrecdea1942018-09-21 11:45:56 +02001040 mmci_dma_data_error(host);
Russell Kingc8ebae32011-01-11 19:35:53 +00001041
Russell Kingc8afc9d2011-02-04 09:19:46 +00001042 /*
1043 * Calculate how far we are into the transfer. Note that
1044 * the data counter gives the number of bytes transferred
1045 * on the MMC bus, not on the host side. On reads, this
1046 * can be as much as a FIFO-worth of data ahead. This
1047 * matters for FIFO overruns only.
1048 */
Linus Walleijf5a106d2011-01-27 17:44:34 +01001049 remain = readl(host->base + MMCIDATACNT);
Linus Walleij8cb28152011-01-24 15:22:13 +01001050 success = data->blksz * data->blocks - remain;
1051
Russell Kingc8afc9d2011-02-04 09:19:46 +00001052 dev_dbg(mmc_dev(host->mmc), "MCI ERROR IRQ, status 0x%08x at 0x%08x\n",
1053 status, success);
Linus Walleij8cb28152011-01-24 15:22:13 +01001054 if (status & MCI_DATACRCFAIL) {
1055 /* Last block was not successful */
Russell Kingc8afc9d2011-02-04 09:19:46 +00001056 success -= 1;
Pierre Ossman17b04292007-07-22 22:18:46 +02001057 data->error = -EILSEQ;
Linus Walleij8cb28152011-01-24 15:22:13 +01001058 } else if (status & MCI_DATATIMEOUT) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001059 data->error = -ETIMEDOUT;
Linus Walleij757df742011-06-30 15:10:21 +01001060 } else if (status & MCI_STARTBITERR) {
1061 data->error = -ECOMM;
Russell Kingc8afc9d2011-02-04 09:19:46 +00001062 } else if (status & MCI_TXUNDERRUN) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001063 data->error = -EIO;
Russell Kingc8afc9d2011-02-04 09:19:46 +00001064 } else if (status & MCI_RXOVERRUN) {
1065 if (success > host->variant->fifosize)
1066 success -= host->variant->fifosize;
1067 else
1068 success = 0;
Linus Walleij8cb28152011-01-24 15:22:13 +01001069 data->error = -EIO;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001070 }
Russell King51d43752011-01-27 10:56:52 +00001071 data->bytes_xfered = round_down(success, data->blksz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 }
Linus Walleijf20f8f212010-10-19 13:41:24 +01001073
Linus Walleij8cb28152011-01-24 15:22:13 +01001074 if (status & MCI_DATABLOCKEND)
1075 dev_err(mmc_dev(host->mmc), "stray MCI_DATABLOCKEND interrupt\n");
Linus Walleijf20f8f212010-10-19 13:41:24 +01001076
Russell Kingccff9b52011-01-30 21:03:50 +00001077 if (status & MCI_DATAEND || data->error) {
Ludovic Barrecdea1942018-09-21 11:45:56 +02001078 mmci_dma_finalize(host, data);
1079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 mmci_stop_data(host);
1081
Linus Walleij8cb28152011-01-24 15:22:13 +01001082 if (!data->error)
1083 /* The error clause is handled above, success! */
Russell King51d43752011-01-27 10:56:52 +00001084 data->bytes_xfered = data->blksz * data->blocks;
Linus Walleijf20f8f212010-10-19 13:41:24 +01001085
Ulf Hansson024629c2013-05-13 15:40:56 +01001086 if (!data->stop || host->mrq->sbc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 mmci_request_end(host, data->mrq);
1088 } else {
1089 mmci_start_command(host, data->stop, 0);
1090 }
1091 }
1092}
1093
1094static void
1095mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
1096 unsigned int status)
1097{
1098 void __iomem *base = host->base;
Linus Walleij49adc0c2016-10-25 11:06:06 +02001099 bool sbc;
Ulf Hanssonad82bfe2014-06-12 15:01:57 +02001100
1101 if (!cmd)
1102 return;
1103
1104 sbc = (cmd == host->mrq->sbc);
Ulf Hanssonad82bfe2014-06-12 15:01:57 +02001105
Linus Walleij49adc0c2016-10-25 11:06:06 +02001106 /*
1107 * We need to be one of these interrupts to be considered worth
1108 * handling. Note that we tag on any latent IRQs postponed
1109 * due to waiting for busy status.
1110 */
1111 if (!((status|host->busy_status) &
1112 (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT|MCI_CMDSENT|MCI_CMDRESPEND)))
Ulf Hanssonad82bfe2014-06-12 15:01:57 +02001113 return;
Ulf Hansson8d94b542014-01-13 16:49:31 +01001114
Linus Walleij49adc0c2016-10-25 11:06:06 +02001115 /*
1116 * ST Micro variant: handle busy detection.
1117 */
1118 if (host->variant->busy_detect) {
1119 bool busy_resp = !!(cmd->flags & MMC_RSP_BUSY);
Ulf Hansson8d94b542014-01-13 16:49:31 +01001120
Linus Walleij49adc0c2016-10-25 11:06:06 +02001121 /* We are busy with a command, return */
1122 if (host->busy_status &&
1123 (status & host->variant->busy_detect_flag))
1124 return;
Ulf Hansson8d94b542014-01-13 16:49:31 +01001125
Linus Walleij49adc0c2016-10-25 11:06:06 +02001126 /*
1127 * We were not busy, but we now got a busy response on
1128 * something that was not an error, and we double-check
1129 * that the special busy status bit is still set before
1130 * proceeding.
1131 */
1132 if (!host->busy_status && busy_resp &&
1133 !(status & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT)) &&
1134 (readl(base + MMCISTATUS) & host->variant->busy_detect_flag)) {
Jean-Nicolas Graux5cad24d2017-02-07 12:12:41 +01001135
1136 /* Clear the busy start IRQ */
1137 writel(host->variant->busy_detect_mask,
1138 host->base + MMCICLEAR);
1139
1140 /* Unmask the busy end IRQ */
Linus Walleij49adc0c2016-10-25 11:06:06 +02001141 writel(readl(base + MMCIMASK0) |
1142 host->variant->busy_detect_mask,
1143 base + MMCIMASK0);
1144 /*
1145 * Now cache the last response status code (until
1146 * the busy bit goes low), and return.
1147 */
1148 host->busy_status =
1149 status & (MCI_CMDSENT|MCI_CMDRESPEND);
1150 return;
1151 }
1152
1153 /*
1154 * At this point we are not busy with a command, we have
Jean-Nicolas Graux5cad24d2017-02-07 12:12:41 +01001155 * not received a new busy request, clear and mask the busy
1156 * end IRQ and fall through to process the IRQ.
Linus Walleij49adc0c2016-10-25 11:06:06 +02001157 */
1158 if (host->busy_status) {
Jean-Nicolas Graux5cad24d2017-02-07 12:12:41 +01001159
1160 writel(host->variant->busy_detect_mask,
1161 host->base + MMCICLEAR);
1162
Linus Walleij49adc0c2016-10-25 11:06:06 +02001163 writel(readl(base + MMCIMASK0) &
1164 ~host->variant->busy_detect_mask,
1165 base + MMCIMASK0);
1166 host->busy_status = 0;
1167 }
Ulf Hansson8d94b542014-01-13 16:49:31 +01001168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170 host->cmd = NULL;
1171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 if (status & MCI_CMDTIMEOUT) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001173 cmd->error = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001175 cmd->error = -EILSEQ;
Russell King - ARM Linux9047b432011-01-11 16:35:56 +00001176 } else {
1177 cmd->resp[0] = readl(base + MMCIRESPONSE0);
1178 cmd->resp[1] = readl(base + MMCIRESPONSE1);
1179 cmd->resp[2] = readl(base + MMCIRESPONSE2);
1180 cmd->resp[3] = readl(base + MMCIRESPONSE3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 }
1182
Ulf Hansson024629c2013-05-13 15:40:56 +01001183 if ((!sbc && !cmd->data) || cmd->error) {
Ulf Hansson3b6e3c72011-12-13 16:58:43 +01001184 if (host->data) {
1185 /* Terminate the DMA transfer */
Ludovic Barrecdea1942018-09-21 11:45:56 +02001186 mmci_dma_data_error(host);
Ludovic Barre7b2a6d52018-09-21 11:45:55 +02001187
Russell Kinge47c2222007-01-08 16:42:51 +00001188 mmci_stop_data(host);
Ulf Hansson3b6e3c72011-12-13 16:58:43 +01001189 }
Ulf Hansson024629c2013-05-13 15:40:56 +01001190 mmci_request_end(host, host->mrq);
1191 } else if (sbc) {
1192 mmci_start_command(host, host->mrq->cmd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 } else if (!(cmd->data->flags & MMC_DATA_READ)) {
1194 mmci_start_data(host, cmd->data);
1195 }
1196}
1197
Srinivas Kandagatla9c34b732014-06-02 10:10:04 +01001198static int mmci_get_rx_fifocnt(struct mmci_host *host, u32 status, int remain)
1199{
1200 return remain - (readl(host->base + MMCIFIFOCNT) << 2);
1201}
1202
1203static int mmci_qcom_get_rx_fifocnt(struct mmci_host *host, u32 status, int r)
1204{
1205 /*
1206 * on qcom SDCC4 only 8 words are used in each burst so only 8 addresses
1207 * from the fifo range should be used
1208 */
1209 if (status & MCI_RXFIFOHALFFULL)
1210 return host->variant->fifohalfsize;
1211 else if (status & MCI_RXDATAAVLBL)
1212 return 4;
1213
1214 return 0;
1215}
1216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217static int mmci_pio_read(struct mmci_host *host, char *buffer, unsigned int remain)
1218{
1219 void __iomem *base = host->base;
1220 char *ptr = buffer;
Srinivas Kandagatla9c34b732014-06-02 10:10:04 +01001221 u32 status = readl(host->base + MMCISTATUS);
Linus Walleij26eed9a2008-04-26 23:39:44 +01001222 int host_remain = host->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224 do {
Srinivas Kandagatla9c34b732014-06-02 10:10:04 +01001225 int count = host->get_rx_fifocnt(host, status, host_remain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227 if (count > remain)
1228 count = remain;
1229
1230 if (count <= 0)
1231 break;
1232
Ulf Hansson393e5e22011-12-13 17:08:04 +01001233 /*
1234 * SDIO especially may want to send something that is
1235 * not divisible by 4 (as opposed to card sectors
1236 * etc). Therefore make sure to always read the last bytes
1237 * while only doing full 32-bit reads towards the FIFO.
1238 */
1239 if (unlikely(count & 0x3)) {
1240 if (count < 4) {
1241 unsigned char buf[4];
Davide Ciminaghi4b85da02012-12-10 14:47:21 +01001242 ioread32_rep(base + MMCIFIFO, buf, 1);
Ulf Hansson393e5e22011-12-13 17:08:04 +01001243 memcpy(ptr, buf, count);
1244 } else {
Davide Ciminaghi4b85da02012-12-10 14:47:21 +01001245 ioread32_rep(base + MMCIFIFO, ptr, count >> 2);
Ulf Hansson393e5e22011-12-13 17:08:04 +01001246 count &= ~0x3;
1247 }
1248 } else {
Davide Ciminaghi4b85da02012-12-10 14:47:21 +01001249 ioread32_rep(base + MMCIFIFO, ptr, count >> 2);
Ulf Hansson393e5e22011-12-13 17:08:04 +01001250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 ptr += count;
1253 remain -= count;
Linus Walleij26eed9a2008-04-26 23:39:44 +01001254 host_remain -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
1256 if (remain == 0)
1257 break;
1258
1259 status = readl(base + MMCISTATUS);
1260 } while (status & MCI_RXDATAAVLBL);
1261
1262 return ptr - buffer;
1263}
1264
1265static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status)
1266{
Rabin Vincent8301bb62010-08-09 12:57:30 +01001267 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 void __iomem *base = host->base;
1269 char *ptr = buffer;
1270
1271 do {
1272 unsigned int count, maxcnt;
1273
Rabin Vincent8301bb62010-08-09 12:57:30 +01001274 maxcnt = status & MCI_TXFIFOEMPTY ?
1275 variant->fifosize : variant->fifohalfsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 count = min(remain, maxcnt);
1277
Linus Walleij34177802010-10-19 12:43:58 +01001278 /*
Linus Walleij34177802010-10-19 12:43:58 +01001279 * SDIO especially may want to send something that is
1280 * not divisible by 4 (as opposed to card sectors
1281 * etc), and the FIFO only accept full 32-bit writes.
1282 * So compensate by adding +3 on the count, a single
1283 * byte become a 32bit write, 7 bytes will be two
1284 * 32bit writes etc.
1285 */
Davide Ciminaghi4b85da02012-12-10 14:47:21 +01001286 iowrite32_rep(base + MMCIFIFO, ptr, (count + 3) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288 ptr += count;
1289 remain -= count;
1290
1291 if (remain == 0)
1292 break;
1293
1294 status = readl(base + MMCISTATUS);
1295 } while (status & MCI_TXFIFOHALFEMPTY);
1296
1297 return ptr - buffer;
1298}
1299
1300/*
1301 * PIO data transfer IRQ handler.
1302 */
David Howells7d12e782006-10-05 14:55:46 +01001303static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
1305 struct mmci_host *host = dev_id;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001306 struct sg_mapping_iter *sg_miter = &host->sg_miter;
Rabin Vincent8301bb62010-08-09 12:57:30 +01001307 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 void __iomem *base = host->base;
1309 u32 status;
1310
1311 status = readl(base + MMCISTATUS);
1312
Linus Walleij64de0282010-02-19 01:09:10 +01001313 dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 unsigned int remain, len;
1317 char *buffer;
1318
1319 /*
1320 * For write, we only need to test the half-empty flag
1321 * here - if the FIFO is completely empty, then by
1322 * definition it is more than half empty.
1323 *
1324 * For read, check for data available.
1325 */
1326 if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL)))
1327 break;
1328
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001329 if (!sg_miter_next(sg_miter))
1330 break;
1331
1332 buffer = sg_miter->addr;
1333 remain = sg_miter->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335 len = 0;
1336 if (status & MCI_RXACTIVE)
1337 len = mmci_pio_read(host, buffer, remain);
1338 if (status & MCI_TXACTIVE)
1339 len = mmci_pio_write(host, buffer, remain, status);
1340
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001341 sg_miter->consumed = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 host->size -= len;
1344 remain -= len;
1345
1346 if (remain)
1347 break;
1348
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 status = readl(base + MMCISTATUS);
1350 } while (1);
1351
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001352 sg_miter_stop(sg_miter);
1353
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 /*
Russell Kingc4d877c2011-01-27 09:50:13 +00001355 * If we have less than the fifo 'half-full' threshold to transfer,
1356 * trigger a PIO interrupt as soon as any data is available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 */
Russell Kingc4d877c2011-01-27 09:50:13 +00001358 if (status & MCI_RXACTIVE && host->size < variant->fifohalfsize)
Linus Walleij2686b4b2010-10-19 12:39:48 +01001359 mmci_set_mask1(host, MCI_RXDATAAVLBLMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361 /*
1362 * If we run out of data, disable the data IRQs; this
1363 * prevents a race where the FIFO becomes empty before
1364 * the chip itself has disabled the data path, and
1365 * stops us racing with our data end IRQ.
1366 */
1367 if (host->size == 0) {
Linus Walleij2686b4b2010-10-19 12:39:48 +01001368 mmci_set_mask1(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0);
1370 }
1371
1372 return IRQ_HANDLED;
1373}
1374
1375/*
1376 * Handle completion of command and data transfers.
1377 */
David Howells7d12e782006-10-05 14:55:46 +01001378static irqreturn_t mmci_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{
1380 struct mmci_host *host = dev_id;
1381 u32 status;
1382 int ret = 0;
1383
1384 spin_lock(&host->lock);
1385
1386 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 status = readl(host->base + MMCISTATUS);
Linus Walleij2686b4b2010-10-19 12:39:48 +01001388
1389 if (host->singleirq) {
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +01001390 if (status & host->mask1_reg)
Linus Walleij2686b4b2010-10-19 12:39:48 +01001391 mmci_pio_irq(irq, dev_id);
1392
1393 status &= ~MCI_IRQ1MASK;
1394 }
1395
Ulf Hansson8d94b542014-01-13 16:49:31 +01001396 /*
Jean-Nicolas Graux5cad24d2017-02-07 12:12:41 +01001397 * We intentionally clear the MCI_ST_CARDBUSY IRQ (if it's
1398 * enabled) in mmci_cmd_irq() function where ST Micro busy
1399 * detection variant is handled. Considering the HW seems to be
1400 * triggering the IRQ on both edges while monitoring DAT0 for
1401 * busy completion and that same status bit is used to monitor
1402 * start and end of busy detection, special care must be taken
1403 * to make sure that both start and end interrupts are always
1404 * cleared one after the other.
Ulf Hansson8d94b542014-01-13 16:49:31 +01001405 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 status &= readl(host->base + MMCIMASK0);
Jean-Nicolas Graux5cad24d2017-02-07 12:12:41 +01001407 if (host->variant->busy_detect)
1408 writel(status & ~host->variant->busy_detect_mask,
1409 host->base + MMCICLEAR);
1410 else
1411 writel(status, host->base + MMCICLEAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Linus Walleij64de0282010-02-19 01:09:10 +01001413 dev_dbg(mmc_dev(host->mmc), "irq0 (data+cmd) %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Ulf Hansson78782892014-06-13 13:21:38 +02001415 if (host->variant->reversed_irq_handling) {
1416 mmci_data_irq(host, host->data, status);
1417 mmci_cmd_irq(host, host->cmd, status);
1418 } else {
1419 mmci_cmd_irq(host, host->cmd, status);
1420 mmci_data_irq(host, host->data, status);
1421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Linus Walleij49adc0c2016-10-25 11:06:06 +02001423 /*
1424 * Don't poll for busy completion in irq context.
1425 */
1426 if (host->variant->busy_detect && host->busy_status)
1427 status &= ~host->variant->busy_detect_flag;
Ulf Hansson8d94b542014-01-13 16:49:31 +01001428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 ret = 1;
1430 } while (status);
1431
1432 spin_unlock(&host->lock);
1433
1434 return IRQ_RETVAL(ret);
1435}
1436
1437static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1438{
1439 struct mmci_host *host = mmc_priv(mmc);
Linus Walleij9e943022008-10-24 21:17:50 +01001440 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
1442 WARN_ON(host->mrq != NULL);
1443
Ulf Hansson653a7612013-01-21 21:29:34 +01001444 mrq->cmd->error = mmci_validate_data(host, mrq->data);
1445 if (mrq->cmd->error) {
Pierre Ossman255d01a2007-07-24 20:38:53 +02001446 mmc_request_done(mmc, mrq);
1447 return;
1448 }
1449
Linus Walleij9e943022008-10-24 21:17:50 +01001450 spin_lock_irqsave(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
1452 host->mrq = mrq;
1453
Per Forlin58c7ccb2011-07-01 18:55:24 +02001454 if (mrq->data)
1455 mmci_get_next_data(host, mrq->data);
1456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 if (mrq->data && mrq->data->flags & MMC_DATA_READ)
1458 mmci_start_data(host, mrq->data);
1459
Ulf Hansson024629c2013-05-13 15:40:56 +01001460 if (mrq->sbc)
1461 mmci_start_command(host, mrq->sbc, 0);
1462 else
1463 mmci_start_command(host, mrq->cmd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Linus Walleij9e943022008-10-24 21:17:50 +01001465 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466}
1467
1468static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1469{
1470 struct mmci_host *host = mmc_priv(mmc);
Ulf Hansson7d72a1d2011-12-13 16:54:55 +01001471 struct variant_data *variant = host->variant;
Linus Walleija6a64642009-09-14 12:56:14 +01001472 u32 pwr = 0;
1473 unsigned long flags;
Lee Jonesdb90f912013-05-03 12:52:12 +01001474 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Ulf Hanssonbc521812011-12-13 16:57:55 +01001476 if (host->plat->ios_handler &&
1477 host->plat->ios_handler(mmc_dev(mmc), ios))
1478 dev_err(mmc_dev(mmc), "platform ios_handler failed\n");
1479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 switch (ios->power_mode) {
1481 case MMC_POWER_OFF:
Ulf Hansson599c1d52013-01-07 16:22:50 +01001482 if (!IS_ERR(mmc->supply.vmmc))
1483 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Lee Jones237fb5e2013-01-31 11:27:52 +00001484
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001485 if (!IS_ERR(mmc->supply.vqmmc) && host->vqmmc_enabled) {
Lee Jones237fb5e2013-01-31 11:27:52 +00001486 regulator_disable(mmc->supply.vqmmc);
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001487 host->vqmmc_enabled = false;
1488 }
Lee Jones237fb5e2013-01-31 11:27:52 +00001489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 break;
1491 case MMC_POWER_UP:
Ulf Hansson599c1d52013-01-07 16:22:50 +01001492 if (!IS_ERR(mmc->supply.vmmc))
1493 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd);
1494
Ulf Hansson7d72a1d2011-12-13 16:54:55 +01001495 /*
1496 * The ST Micro variant doesn't have the PL180s MCI_PWR_UP
1497 * and instead uses MCI_PWR_ON so apply whatever value is
1498 * configured in the variant data.
1499 */
1500 pwr |= variant->pwrreg_powerup;
1501
1502 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 case MMC_POWER_ON:
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001504 if (!IS_ERR(mmc->supply.vqmmc) && !host->vqmmc_enabled) {
Lee Jonesdb90f912013-05-03 12:52:12 +01001505 ret = regulator_enable(mmc->supply.vqmmc);
1506 if (ret < 0)
1507 dev_err(mmc_dev(mmc),
1508 "failed to enable vqmmc regulator\n");
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001509 else
1510 host->vqmmc_enabled = true;
Lee Jonesdb90f912013-05-03 12:52:12 +01001511 }
Lee Jones237fb5e2013-01-31 11:27:52 +00001512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 pwr |= MCI_PWR_ON;
1514 break;
1515 }
1516
Ulf Hansson4d1a3a02011-12-13 16:57:07 +01001517 if (variant->signal_direction && ios->power_mode != MMC_POWER_OFF) {
1518 /*
1519 * The ST Micro variant has some additional bits
1520 * indicating signal direction for the signals in
1521 * the SD/MMC bus and feedback-clock usage.
1522 */
Ulf Hansson4593df22014-03-21 10:13:05 +01001523 pwr |= host->pwr_reg_add;
Ulf Hansson4d1a3a02011-12-13 16:57:07 +01001524
1525 if (ios->bus_width == MMC_BUS_WIDTH_4)
1526 pwr &= ~MCI_ST_DATA74DIREN;
1527 else if (ios->bus_width == MMC_BUS_WIDTH_1)
1528 pwr &= (~MCI_ST_DATA74DIREN &
1529 ~MCI_ST_DATA31DIREN &
1530 ~MCI_ST_DATA2DIREN);
1531 }
1532
Patrice Chotardf9bb3042018-01-18 15:34:20 +01001533 if (variant->opendrain) {
1534 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
1535 pwr |= variant->opendrain;
1536 } else {
1537 /*
1538 * If the variant cannot configure the pads by its own, then we
1539 * expect the pinctrl to be able to do that for us
1540 */
1541 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
1542 pinctrl_select_state(host->pinctrl, host->pins_opendrain);
1543 else
1544 pinctrl_select_state(host->pinctrl, host->pins_default);
1545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
Ulf Hanssonf4670da2013-01-09 17:19:54 +01001547 /*
1548 * If clock = 0 and the variant requires the MMCIPOWER to be used for
1549 * gating the clock, the MCI_PWR_ON bit is cleared.
1550 */
1551 if (!ios->clock && variant->pwrreg_clkgate)
1552 pwr &= ~MCI_PWR_ON;
1553
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001554 if (host->variant->explicit_mclk_control &&
1555 ios->clock != host->clock_cache) {
1556 ret = clk_set_rate(host->clk, ios->clock);
1557 if (ret < 0)
1558 dev_err(mmc_dev(host->mmc),
1559 "Error setting clock rate (%d)\n", ret);
1560 else
1561 host->mclk = clk_get_rate(host->clk);
1562 }
1563 host->clock_cache = ios->clock;
1564
Linus Walleija6a64642009-09-14 12:56:14 +01001565 spin_lock_irqsave(&host->lock, flags);
1566
1567 mmci_set_clkreg(host, ios->clock);
Ulf Hansson7437cfa2012-01-18 09:17:27 +01001568 mmci_write_pwrreg(host, pwr);
Ulf Hanssonf829c042013-09-04 09:01:15 +01001569 mmci_reg_delay(host);
Linus Walleija6a64642009-09-14 12:56:14 +01001570
1571 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572}
1573
Russell King89001442009-07-09 15:16:07 +01001574static int mmci_get_cd(struct mmc_host *mmc)
1575{
1576 struct mmci_host *host = mmc_priv(mmc);
Rabin Vincent29719442010-08-09 12:54:43 +01001577 struct mmci_platform_data *plat = host->plat;
Ulf Hanssond2762092014-03-17 13:56:19 +01001578 unsigned int status = mmc_gpio_get_cd(mmc);
Russell King89001442009-07-09 15:16:07 +01001579
Ulf Hanssond2762092014-03-17 13:56:19 +01001580 if (status == -ENOSYS) {
Rabin Vincent4b8caec2010-08-09 12:56:40 +01001581 if (!plat->status)
1582 return 1; /* Assume always present */
1583
Rabin Vincent29719442010-08-09 12:54:43 +01001584 status = plat->status(mmc_dev(host->mmc));
Ulf Hanssond2762092014-03-17 13:56:19 +01001585 }
Russell King74bc8092010-07-29 15:58:59 +01001586 return status;
Russell King89001442009-07-09 15:16:07 +01001587}
1588
Ulf Hansson0f3ed7f2013-05-15 20:47:33 +01001589static int mmci_sig_volt_switch(struct mmc_host *mmc, struct mmc_ios *ios)
1590{
1591 int ret = 0;
1592
1593 if (!IS_ERR(mmc->supply.vqmmc)) {
1594
Ulf Hansson0f3ed7f2013-05-15 20:47:33 +01001595 switch (ios->signal_voltage) {
1596 case MMC_SIGNAL_VOLTAGE_330:
1597 ret = regulator_set_voltage(mmc->supply.vqmmc,
1598 2700000, 3600000);
1599 break;
1600 case MMC_SIGNAL_VOLTAGE_180:
1601 ret = regulator_set_voltage(mmc->supply.vqmmc,
1602 1700000, 1950000);
1603 break;
1604 case MMC_SIGNAL_VOLTAGE_120:
1605 ret = regulator_set_voltage(mmc->supply.vqmmc,
1606 1100000, 1300000);
1607 break;
1608 }
1609
1610 if (ret)
1611 dev_warn(mmc_dev(mmc), "Voltage switch failed\n");
Ulf Hansson0f3ed7f2013-05-15 20:47:33 +01001612 }
1613
1614 return ret;
1615}
1616
Ulf Hansson01259622013-05-15 20:53:22 +01001617static struct mmc_host_ops mmci_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 .request = mmci_request,
Per Forlin58c7ccb2011-07-01 18:55:24 +02001619 .pre_req = mmci_pre_request,
1620 .post_req = mmci_post_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 .set_ios = mmci_set_ios,
Ulf Hanssond2762092014-03-17 13:56:19 +01001622 .get_ro = mmc_gpio_get_ro,
Russell King89001442009-07-09 15:16:07 +01001623 .get_cd = mmci_get_cd,
Ulf Hansson0f3ed7f2013-05-15 20:47:33 +01001624 .start_signal_voltage_switch = mmci_sig_volt_switch,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625};
1626
Ulf Hansson78f87df2014-03-17 15:53:07 +01001627static int mmci_of_parse(struct device_node *np, struct mmc_host *mmc)
1628{
Ulf Hansson4593df22014-03-21 10:13:05 +01001629 struct mmci_host *host = mmc_priv(mmc);
Ulf Hansson78f87df2014-03-17 15:53:07 +01001630 int ret = mmc_of_parse(mmc);
Lee Jones000bc9d2012-04-16 10:18:43 +01001631
Ulf Hansson78f87df2014-03-17 15:53:07 +01001632 if (ret)
1633 return ret;
Lee Jones000bc9d2012-04-16 10:18:43 +01001634
Ulf Hansson4593df22014-03-21 10:13:05 +01001635 if (of_get_property(np, "st,sig-dir-dat0", NULL))
1636 host->pwr_reg_add |= MCI_ST_DATA0DIREN;
1637 if (of_get_property(np, "st,sig-dir-dat2", NULL))
1638 host->pwr_reg_add |= MCI_ST_DATA2DIREN;
1639 if (of_get_property(np, "st,sig-dir-dat31", NULL))
1640 host->pwr_reg_add |= MCI_ST_DATA31DIREN;
1641 if (of_get_property(np, "st,sig-dir-dat74", NULL))
1642 host->pwr_reg_add |= MCI_ST_DATA74DIREN;
1643 if (of_get_property(np, "st,sig-dir-cmd", NULL))
1644 host->pwr_reg_add |= MCI_ST_CMDDIREN;
1645 if (of_get_property(np, "st,sig-pin-fbclk", NULL))
1646 host->pwr_reg_add |= MCI_ST_FBCLKEN;
1647
Lee Jones000bc9d2012-04-16 10:18:43 +01001648 if (of_get_property(np, "mmc-cap-mmc-highspeed", NULL))
Ulf Hansson78f87df2014-03-17 15:53:07 +01001649 mmc->caps |= MMC_CAP_MMC_HIGHSPEED;
Lee Jones000bc9d2012-04-16 10:18:43 +01001650 if (of_get_property(np, "mmc-cap-sd-highspeed", NULL))
Ulf Hansson78f87df2014-03-17 15:53:07 +01001651 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
Lee Jones000bc9d2012-04-16 10:18:43 +01001652
Ulf Hansson78f87df2014-03-17 15:53:07 +01001653 return 0;
Lee Jones000bc9d2012-04-16 10:18:43 +01001654}
Lee Jones000bc9d2012-04-16 10:18:43 +01001655
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001656static int mmci_probe(struct amba_device *dev,
Russell Kingaa25afa2011-02-19 15:55:00 +00001657 const struct amba_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658{
Linus Walleij6ef297f2009-09-22 14:29:36 +01001659 struct mmci_platform_data *plat = dev->dev.platform_data;
Lee Jones000bc9d2012-04-16 10:18:43 +01001660 struct device_node *np = dev->dev.of_node;
Rabin Vincent4956e102010-07-21 12:54:40 +01001661 struct variant_data *variant = id->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 struct mmci_host *host;
1663 struct mmc_host *mmc;
1664 int ret;
1665
Lee Jones000bc9d2012-04-16 10:18:43 +01001666 /* Must have platform data or Device Tree. */
1667 if (!plat && !np) {
1668 dev_err(&dev->dev, "No plat data or DT found\n");
1669 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 }
1671
Lee Jonesb9b52912012-06-12 10:49:51 +01001672 if (!plat) {
1673 plat = devm_kzalloc(&dev->dev, sizeof(*plat), GFP_KERNEL);
1674 if (!plat)
1675 return -ENOMEM;
1676 }
1677
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
Ulf Hanssonef289982014-03-17 13:56:32 +01001679 if (!mmc)
1680 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Ulf Hansson78f87df2014-03-17 15:53:07 +01001682 ret = mmci_of_parse(np, mmc);
1683 if (ret)
1684 goto host_free;
1685
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 host = mmc_priv(mmc);
Rabin Vincent4ea580f2009-04-17 08:44:19 +05301687 host->mmc = mmc;
Russell King012b7d32009-07-09 15:13:56 +01001688
Patrice Chotardf9bb3042018-01-18 15:34:20 +01001689 /*
1690 * Some variant (STM32) doesn't have opendrain bit, nevertheless
1691 * pins can be set accordingly using pinctrl
1692 */
1693 if (!variant->opendrain) {
1694 host->pinctrl = devm_pinctrl_get(&dev->dev);
1695 if (IS_ERR(host->pinctrl)) {
1696 dev_err(&dev->dev, "failed to get pinctrl");
Wei Yongjun310eb252018-01-23 02:09:13 +00001697 ret = PTR_ERR(host->pinctrl);
Patrice Chotardf9bb3042018-01-18 15:34:20 +01001698 goto host_free;
1699 }
1700
1701 host->pins_default = pinctrl_lookup_state(host->pinctrl,
1702 PINCTRL_STATE_DEFAULT);
1703 if (IS_ERR(host->pins_default)) {
1704 dev_err(mmc_dev(mmc), "Can't select default pins\n");
Wei Yongjun310eb252018-01-23 02:09:13 +00001705 ret = PTR_ERR(host->pins_default);
Patrice Chotardf9bb3042018-01-18 15:34:20 +01001706 goto host_free;
1707 }
1708
1709 host->pins_opendrain = pinctrl_lookup_state(host->pinctrl,
1710 MMCI_PINCTRL_STATE_OPENDRAIN);
1711 if (IS_ERR(host->pins_opendrain)) {
1712 dev_err(mmc_dev(mmc), "Can't select opendrain pins\n");
Wei Yongjun310eb252018-01-23 02:09:13 +00001713 ret = PTR_ERR(host->pins_opendrain);
Patrice Chotardf9bb3042018-01-18 15:34:20 +01001714 goto host_free;
1715 }
1716 }
1717
Russell King012b7d32009-07-09 15:13:56 +01001718 host->hw_designer = amba_manf(dev);
1719 host->hw_revision = amba_rev(dev);
Linus Walleij64de0282010-02-19 01:09:10 +01001720 dev_dbg(mmc_dev(mmc), "designer ID = 0x%02x\n", host->hw_designer);
1721 dev_dbg(mmc_dev(mmc), "revision = 0x%01x\n", host->hw_revision);
Russell King012b7d32009-07-09 15:13:56 +01001722
Ulf Hansson665ba562013-05-13 15:39:17 +01001723 host->clk = devm_clk_get(&dev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 if (IS_ERR(host->clk)) {
1725 ret = PTR_ERR(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 goto host_free;
1727 }
1728
Julia Lawallac940932012-08-26 16:00:59 +00001729 ret = clk_prepare_enable(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 if (ret)
Ulf Hansson665ba562013-05-13 15:39:17 +01001731 goto host_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
Srinivas Kandagatla9c34b732014-06-02 10:10:04 +01001733 if (variant->qcom_fifo)
1734 host->get_rx_fifocnt = mmci_qcom_get_rx_fifocnt;
1735 else
1736 host->get_rx_fifocnt = mmci_get_rx_fifocnt;
1737
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 host->plat = plat;
Rabin Vincent4956e102010-07-21 12:54:40 +01001739 host->variant = variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 host->mclk = clk_get_rate(host->clk);
Linus Walleijc8df9a52008-04-29 09:34:07 +01001741 /*
1742 * According to the spec, mclk is max 100 MHz,
1743 * so we try to adjust the clock down to this,
1744 * (if possible).
1745 */
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +01001746 if (host->mclk > variant->f_max) {
1747 ret = clk_set_rate(host->clk, variant->f_max);
Linus Walleijc8df9a52008-04-29 09:34:07 +01001748 if (ret < 0)
1749 goto clk_disable;
1750 host->mclk = clk_get_rate(host->clk);
Linus Walleij64de0282010-02-19 01:09:10 +01001751 dev_dbg(mmc_dev(mmc), "eventual mclk rate: %u Hz\n",
1752 host->mclk);
Linus Walleijc8df9a52008-04-29 09:34:07 +01001753 }
Ulf Hanssonef289982014-03-17 13:56:32 +01001754
Russell Kingc8ebae32011-01-11 19:35:53 +00001755 host->phybase = dev->res.start;
Ulf Hanssonef289982014-03-17 13:56:32 +01001756 host->base = devm_ioremap_resource(&dev->dev, &dev->res);
1757 if (IS_ERR(host->base)) {
1758 ret = PTR_ERR(host->base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 goto clk_disable;
1760 }
1761
Ulf Hanssoned9067f2018-07-13 13:15:23 +02001762 if (variant->init)
1763 variant->init(host);
1764
Linus Walleij7f294e42011-07-08 09:57:15 +01001765 /*
1766 * The ARM and ST versions of the block have slightly different
1767 * clock divider equations which means that the minimum divider
1768 * differs too.
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001769 * on Qualcomm like controllers get the nearest minimum clock to 100Khz
Linus Walleij7f294e42011-07-08 09:57:15 +01001770 */
1771 if (variant->st_clkdiv)
1772 mmc->f_min = DIV_ROUND_UP(host->mclk, 257);
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001773 else if (variant->explicit_mclk_control)
1774 mmc->f_min = clk_round_rate(host->clk, 100000);
Linus Walleij7f294e42011-07-08 09:57:15 +01001775 else
1776 mmc->f_min = DIV_ROUND_UP(host->mclk, 512);
Linus Walleij808d97c2010-04-08 07:39:38 +01001777 /*
Ulf Hansson78f87df2014-03-17 15:53:07 +01001778 * If no maximum operating frequency is supplied, fall back to use
1779 * the module parameter, which has a (low) default value in case it
1780 * is not specified. Either value must not exceed the clock rate into
Ulf Hansson5080a082014-03-21 10:46:39 +01001781 * the block, of course.
Linus Walleij808d97c2010-04-08 07:39:38 +01001782 */
Ulf Hansson78f87df2014-03-17 15:53:07 +01001783 if (mmc->f_max)
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001784 mmc->f_max = variant->explicit_mclk_control ?
1785 min(variant->f_max, mmc->f_max) :
1786 min(host->mclk, mmc->f_max);
Linus Walleij808d97c2010-04-08 07:39:38 +01001787 else
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001788 mmc->f_max = variant->explicit_mclk_control ?
1789 fmax : min(host->mclk, fmax);
1790
1791
Linus Walleij64de0282010-02-19 01:09:10 +01001792 dev_dbg(mmc_dev(mmc), "clocking block at %u Hz\n", mmc->f_max);
1793
Ulf Hansson599c1d52013-01-07 16:22:50 +01001794 /* Get regulators and the supported OCR mask */
Bjorn Andersson9369c972015-03-24 18:39:49 -07001795 ret = mmc_regulator_get_supply(mmc);
Wolfram Sang510069522017-10-14 21:17:14 +02001796 if (ret)
Bjorn Andersson9369c972015-03-24 18:39:49 -07001797 goto clk_disable;
1798
Ulf Hansson599c1d52013-01-07 16:22:50 +01001799 if (!mmc->ocr_avail)
Linus Walleij34e84f32009-09-22 14:41:40 +01001800 mmc->ocr_avail = plat->ocr_mask;
Ulf Hansson599c1d52013-01-07 16:22:50 +01001801 else if (plat->ocr_mask)
1802 dev_warn(mmc_dev(mmc), "Platform OCR mask is ignored\n");
1803
Ulf Hansson9dd8a8b2014-03-19 13:54:18 +01001804 /* We support these capabilities. */
1805 mmc->caps |= MMC_CAP_CMD23;
1806
Linus Walleij49adc0c2016-10-25 11:06:06 +02001807 /*
1808 * Enable busy detection.
1809 */
Ulf Hansson8d94b542014-01-13 16:49:31 +01001810 if (variant->busy_detect) {
1811 mmci_ops.card_busy = mmci_card_busy;
Linus Walleij49adc0c2016-10-25 11:06:06 +02001812 /*
1813 * Not all variants have a flag to enable busy detection
1814 * in the DPSM, but if they do, set it here.
1815 */
1816 if (variant->busy_dpsm_flag)
1817 mmci_write_datactrlreg(host,
1818 host->variant->busy_dpsm_flag);
Ulf Hansson8d94b542014-01-13 16:49:31 +01001819 mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY;
1820 mmc->max_busy_timeout = 0;
1821 }
1822
1823 mmc->ops = &mmci_ops;
1824
Ulf Hansson70be2082013-01-07 15:35:06 +01001825 /* We support these PM capabilities. */
Ulf Hansson78f87df2014-03-17 15:53:07 +01001826 mmc->pm_caps |= MMC_PM_KEEP_POWER;
Ulf Hansson70be2082013-01-07 15:35:06 +01001827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 /*
1829 * We can do SGIO
1830 */
Martin K. Petersena36274e2010-09-10 01:33:59 -04001831 mmc->max_segs = NR_SG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
1833 /*
Rabin Vincent08458ef2010-07-21 12:55:59 +01001834 * Since only a certain number of bits are valid in the data length
1835 * register, we must ensure that we don't exceed 2^num-1 bytes in a
1836 * single request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 */
Rabin Vincent08458ef2010-07-21 12:55:59 +01001838 mmc->max_req_size = (1 << variant->datalength_bits) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
1840 /*
1841 * Set the maximum segment size. Since we aren't doing DMA
1842 * (yet) we are only limited by the data length register.
1843 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001844 mmc->max_seg_size = mmc->max_req_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001846 /*
1847 * Block size can be up to 2048 bytes, but must be a power of two.
1848 */
Will Deacon8f7f6b7e2012-02-24 11:25:21 +00001849 mmc->max_blk_size = 1 << 11;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001850
Pierre Ossman55db8902006-11-21 17:55:45 +01001851 /*
Will Deacon8f7f6b7e2012-02-24 11:25:21 +00001852 * Limit the number of blocks transferred so that we don't overflow
1853 * the maximum request size.
Pierre Ossman55db8902006-11-21 17:55:45 +01001854 */
Will Deacon8f7f6b7e2012-02-24 11:25:21 +00001855 mmc->max_blk_count = mmc->max_req_size >> 11;
Pierre Ossman55db8902006-11-21 17:55:45 +01001856
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 spin_lock_init(&host->lock);
1858
1859 writel(0, host->base + MMCIMASK0);
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +01001860
1861 if (variant->mmcimask1)
1862 writel(0, host->base + MMCIMASK1);
1863
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 writel(0xfff, host->base + MMCICLEAR);
1865
Linus Walleijce437aa2014-08-27 15:13:54 +02001866 /*
1867 * If:
1868 * - not using DT but using a descriptor table, or
1869 * - using a table of descriptors ALONGSIDE DT, or
1870 * look up these descriptors named "cd" and "wp" right here, fail
Linus Walleij9ef986a2018-09-20 16:01:10 -07001871 * silently of these do not exist
Linus Walleijce437aa2014-08-27 15:13:54 +02001872 */
1873 if (!np) {
Linus Walleij89168b42014-10-02 09:08:46 +02001874 ret = mmc_gpiod_request_cd(mmc, "cd", 0, false, 0, NULL);
Linus Walleij9ef986a2018-09-20 16:01:10 -07001875 if (ret == -EPROBE_DEFER)
1876 goto clk_disable;
Linus Walleijce437aa2014-08-27 15:13:54 +02001877
Linus Walleij89168b42014-10-02 09:08:46 +02001878 ret = mmc_gpiod_request_ro(mmc, "wp", 0, false, 0, NULL);
Linus Walleij9ef986a2018-09-20 16:01:10 -07001879 if (ret == -EPROBE_DEFER)
1880 goto clk_disable;
Russell King89001442009-07-09 15:16:07 +01001881 }
1882
Ulf Hanssonef289982014-03-17 13:56:32 +01001883 ret = devm_request_irq(&dev->dev, dev->irq[0], mmci_irq, IRQF_SHARED,
1884 DRIVER_NAME " (cmd)", host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 if (ret)
Ulf Hanssonef289982014-03-17 13:56:32 +01001886 goto clk_disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
Russell Kingdfb85182012-05-03 11:33:15 +01001888 if (!dev->irq[1])
Linus Walleij2686b4b2010-10-19 12:39:48 +01001889 host->singleirq = true;
1890 else {
Ulf Hanssonef289982014-03-17 13:56:32 +01001891 ret = devm_request_irq(&dev->dev, dev->irq[1], mmci_pio_irq,
1892 IRQF_SHARED, DRIVER_NAME " (pio)", host);
Linus Walleij2686b4b2010-10-19 12:39:48 +01001893 if (ret)
Ulf Hanssonef289982014-03-17 13:56:32 +01001894 goto clk_disable;
Linus Walleij2686b4b2010-10-19 12:39:48 +01001895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
Linus Walleij8cb28152011-01-24 15:22:13 +01001897 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
1899 amba_set_drvdata(dev, mmc);
1900
Russell Kingc8ebae32011-01-11 19:35:53 +00001901 dev_info(&dev->dev, "%s: PL%03x manf %x rev%u at 0x%08llx irq %d,%d (pio)\n",
1902 mmc_hostname(mmc), amba_part(dev), amba_manf(dev),
1903 amba_rev(dev), (unsigned long long)dev->res.start,
1904 dev->irq[0], dev->irq[1]);
1905
1906 mmci_dma_setup(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
Ulf Hansson2cd976c2011-12-13 17:01:11 +01001908 pm_runtime_set_autosuspend_delay(&dev->dev, 50);
1909 pm_runtime_use_autosuspend(&dev->dev);
Russell King1c3be362011-08-14 09:17:05 +01001910
Russell King8c11a942010-12-28 19:40:40 +00001911 mmc_add_host(mmc);
1912
Ulf Hansson6f2d3c82014-12-11 14:35:55 +01001913 pm_runtime_put(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 return 0;
1915
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 clk_disable:
Julia Lawallac940932012-08-26 16:00:59 +00001917 clk_disable_unprepare(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 host_free:
1919 mmc_free_host(mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 return ret;
1921}
1922
Bill Pemberton6e0ee712012-11-19 13:26:03 -05001923static int mmci_remove(struct amba_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
1925 struct mmc_host *mmc = amba_get_drvdata(dev);
1926
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 if (mmc) {
1928 struct mmci_host *host = mmc_priv(mmc);
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +01001929 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
Russell King1c3be362011-08-14 09:17:05 +01001931 /*
1932 * Undo pm_runtime_put() in probe. We use the _sync
1933 * version here so that we can access the primecell.
1934 */
1935 pm_runtime_get_sync(&dev->dev);
1936
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 mmc_remove_host(mmc);
1938
1939 writel(0, host->base + MMCIMASK0);
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +01001940
1941 if (variant->mmcimask1)
1942 writel(0, host->base + MMCIMASK1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
1944 writel(0, host->base + MMCICOMMAND);
1945 writel(0, host->base + MMCIDATACTRL);
1946
Russell Kingc8ebae32011-01-11 19:35:53 +00001947 mmci_dma_release(host);
Julia Lawallac940932012-08-26 16:00:59 +00001948 clk_disable_unprepare(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 mmc_free_host(mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 }
1951
1952 return 0;
1953}
1954
Ulf Hansson571dce42014-01-23 00:38:00 +01001955#ifdef CONFIG_PM
Ulf Hansson1ff44432013-09-04 09:05:17 +01001956static void mmci_save(struct mmci_host *host)
1957{
1958 unsigned long flags;
1959
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001960 spin_lock_irqsave(&host->lock, flags);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001961
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001962 writel(0, host->base + MMCIMASK0);
1963 if (host->variant->pwrreg_nopower) {
Ulf Hansson1ff44432013-09-04 09:05:17 +01001964 writel(0, host->base + MMCIDATACTRL);
1965 writel(0, host->base + MMCIPOWER);
1966 writel(0, host->base + MMCICLOCK);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001967 }
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001968 mmci_reg_delay(host);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001969
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001970 spin_unlock_irqrestore(&host->lock, flags);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001971}
1972
1973static void mmci_restore(struct mmci_host *host)
1974{
1975 unsigned long flags;
1976
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001977 spin_lock_irqsave(&host->lock, flags);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001978
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001979 if (host->variant->pwrreg_nopower) {
Ulf Hansson1ff44432013-09-04 09:05:17 +01001980 writel(host->clk_reg, host->base + MMCICLOCK);
1981 writel(host->datactrl_reg, host->base + MMCIDATACTRL);
1982 writel(host->pwr_reg, host->base + MMCIPOWER);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001983 }
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001984 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
1985 mmci_reg_delay(host);
1986
1987 spin_unlock_irqrestore(&host->lock, flags);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001988}
1989
Ulf Hansson82592932013-01-09 11:15:26 +01001990static int mmci_runtime_suspend(struct device *dev)
1991{
1992 struct amba_device *adev = to_amba_device(dev);
1993 struct mmc_host *mmc = amba_get_drvdata(adev);
1994
1995 if (mmc) {
1996 struct mmci_host *host = mmc_priv(mmc);
Ulf Hanssone36bd9c62013-09-04 09:00:37 +01001997 pinctrl_pm_select_sleep_state(dev);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001998 mmci_save(host);
Ulf Hansson82592932013-01-09 11:15:26 +01001999 clk_disable_unprepare(host->clk);
2000 }
2001
2002 return 0;
2003}
2004
2005static int mmci_runtime_resume(struct device *dev)
2006{
2007 struct amba_device *adev = to_amba_device(dev);
2008 struct mmc_host *mmc = amba_get_drvdata(adev);
2009
2010 if (mmc) {
2011 struct mmci_host *host = mmc_priv(mmc);
2012 clk_prepare_enable(host->clk);
Ulf Hansson1ff44432013-09-04 09:05:17 +01002013 mmci_restore(host);
Ulf Hanssone36bd9c62013-09-04 09:00:37 +01002014 pinctrl_pm_select_default_state(dev);
Ulf Hansson82592932013-01-09 11:15:26 +01002015 }
2016
2017 return 0;
2018}
2019#endif
2020
Ulf Hansson48fa7002011-12-13 16:59:34 +01002021static const struct dev_pm_ops mmci_dev_pm_ops = {
Ulf Hanssonf3737fa2014-01-23 01:11:33 +01002022 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
2023 pm_runtime_force_resume)
Rafael J. Wysocki6ed23b82014-12-04 00:34:11 +01002024 SET_RUNTIME_PM_OPS(mmci_runtime_suspend, mmci_runtime_resume, NULL)
Ulf Hansson48fa7002011-12-13 16:59:34 +01002025};
2026
Arvind Yadav88411de2017-08-23 22:00:49 +05302027static const struct amba_id mmci_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 {
2029 .id = 0x00041180,
Pawel Moll768fbc12011-03-11 17:18:07 +00002030 .mask = 0xff0fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01002031 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 },
2033 {
Pawel Moll768fbc12011-03-11 17:18:07 +00002034 .id = 0x01041180,
2035 .mask = 0xff0fffff,
2036 .data = &variant_arm_extended_fifo,
2037 },
2038 {
Pawel Moll3a372982013-01-24 14:12:45 +01002039 .id = 0x02041180,
2040 .mask = 0xff0fffff,
2041 .data = &variant_arm_extended_fifo_hwfc,
2042 },
2043 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 .id = 0x00041181,
2045 .mask = 0x000fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01002046 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 },
Linus Walleijcc30d602009-01-04 15:18:54 +01002048 /* ST Micro variants */
2049 {
2050 .id = 0x00180180,
2051 .mask = 0x00ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01002052 .data = &variant_u300,
Linus Walleijcc30d602009-01-04 15:18:54 +01002053 },
2054 {
Linus Walleij34fd4212012-04-10 17:43:59 +01002055 .id = 0x10180180,
2056 .mask = 0xf0ffffff,
2057 .data = &variant_nomadik,
2058 },
2059 {
Linus Walleijcc30d602009-01-04 15:18:54 +01002060 .id = 0x00280180,
2061 .mask = 0x00ffffff,
Linus Walleij0bcb7ef2016-01-04 02:21:55 +01002062 .data = &variant_nomadik,
Rabin Vincent4956e102010-07-21 12:54:40 +01002063 },
2064 {
2065 .id = 0x00480180,
Philippe Langlais1784b152011-03-25 08:51:52 +01002066 .mask = 0xf0ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01002067 .data = &variant_ux500,
Linus Walleijcc30d602009-01-04 15:18:54 +01002068 },
Philippe Langlais1784b152011-03-25 08:51:52 +01002069 {
2070 .id = 0x10480180,
2071 .mask = 0xf0ffffff,
2072 .data = &variant_ux500v2,
2073 },
Patrice Chotard2a9d6c82018-01-18 15:34:21 +01002074 {
2075 .id = 0x00880180,
2076 .mask = 0x00ffffff,
2077 .data = &variant_stm32,
2078 },
Srinivas Kandagatla55b604a2014-06-02 10:10:13 +01002079 /* Qualcomm variants */
2080 {
2081 .id = 0x00051180,
2082 .mask = 0x000fffff,
2083 .data = &variant_qcom,
2084 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 { 0, 0 },
2086};
2087
Dave Martin9f998352011-10-05 15:15:21 +01002088MODULE_DEVICE_TABLE(amba, mmci_ids);
2089
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090static struct amba_driver mmci_driver = {
2091 .drv = {
2092 .name = DRIVER_NAME,
Ulf Hansson48fa7002011-12-13 16:59:34 +01002093 .pm = &mmci_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 },
2095 .probe = mmci_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05002096 .remove = mmci_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 .id_table = mmci_ids,
2098};
2099
viresh kumar9e5ed092012-03-15 10:40:38 +01002100module_amba_driver(mmci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102module_param(fmax, uint, 0444);
2103
2104MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
2105MODULE_LICENSE("GPL");