blob: 1b99ae3bbd0facec067d6290746fb3fdb8209c18 [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
Ulf Hanssonf829c042013-09-04 09:01:15 +0100242static void mmci_reg_delay(struct mmci_host *host)
243{
244 /*
245 * According to the spec, at least three feedback clock cycles
246 * of max 52 MHz must pass between two writes to the MMCICLOCK reg.
247 * Three MCLK clock cycles must pass between two MMCIPOWER reg writes.
248 * Worst delay time during card init is at 100 kHz => 30 us.
249 * Worst delay time when up and running is at 25 MHz => 120 ns.
250 */
251 if (host->cclk < 25000000)
252 udelay(30);
253 else
254 ndelay(120);
255}
256
Ulf Hansson653a7612013-01-21 21:29:34 +0100257/*
Linus Walleija6a64642009-09-14 12:56:14 +0100258 * This must be called with host->lock held
259 */
Ludovic Barrecd3ee8c2018-10-08 14:08:42 +0200260void mmci_write_clkreg(struct mmci_host *host, u32 clk)
Ulf Hansson7437cfa2012-01-18 09:17:27 +0100261{
262 if (host->clk_reg != clk) {
263 host->clk_reg = clk;
264 writel(clk, host->base + MMCICLOCK);
265 }
266}
267
268/*
269 * This must be called with host->lock held
270 */
Ludovic Barrecd3ee8c2018-10-08 14:08:42 +0200271void mmci_write_pwrreg(struct mmci_host *host, u32 pwr)
Ulf Hansson7437cfa2012-01-18 09:17:27 +0100272{
273 if (host->pwr_reg != pwr) {
274 host->pwr_reg = pwr;
275 writel(pwr, host->base + MMCIPOWER);
276 }
277}
278
279/*
280 * This must be called with host->lock held
281 */
Ulf Hansson9cc639a2013-05-15 20:48:23 +0100282static void mmci_write_datactrlreg(struct mmci_host *host, u32 datactrl)
283{
Linus Walleij49adc0c2016-10-25 11:06:06 +0200284 /* Keep busy mode in DPSM if enabled */
285 datactrl |= host->datactrl_reg & host->variant->busy_dpsm_flag;
Ulf Hansson01259622013-05-15 20:53:22 +0100286
Ulf Hansson9cc639a2013-05-15 20:48:23 +0100287 if (host->datactrl_reg != datactrl) {
288 host->datactrl_reg = datactrl;
289 writel(datactrl, host->base + MMCIDATACTRL);
290 }
291}
292
293/*
294 * This must be called with host->lock held
295 */
Linus Walleija6a64642009-09-14 12:56:14 +0100296static void mmci_set_clkreg(struct mmci_host *host, unsigned int desired)
297{
Rabin Vincent4956e102010-07-21 12:54:40 +0100298 struct variant_data *variant = host->variant;
299 u32 clk = variant->clkreg;
Linus Walleija6a64642009-09-14 12:56:14 +0100300
Ulf Hanssonc58a8502013-05-13 15:40:03 +0100301 /* Make sure cclk reflects the current calculated clock */
302 host->cclk = 0;
303
Linus Walleija6a64642009-09-14 12:56:14 +0100304 if (desired) {
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +0100305 if (variant->explicit_mclk_control) {
306 host->cclk = host->mclk;
307 } else if (desired >= host->mclk) {
Linus Walleij991a86e2010-12-10 09:35:53 +0100308 clk = MCI_CLK_BYPASS;
Linus Walleij399bc482011-04-01 07:59:17 +0100309 if (variant->st_clkdiv)
310 clk |= MCI_ST_UX500_NEG_EDGE;
Linus Walleija6a64642009-09-14 12:56:14 +0100311 host->cclk = host->mclk;
Linus Walleijb70a67f2010-12-06 09:24:14 +0100312 } else if (variant->st_clkdiv) {
313 /*
314 * DB8500 TRM says f = mclk / (clkdiv + 2)
315 * => clkdiv = (mclk / f) - 2
316 * Round the divider up so we don't exceed the max
317 * frequency
318 */
319 clk = DIV_ROUND_UP(host->mclk, desired) - 2;
320 if (clk >= 256)
321 clk = 255;
322 host->cclk = host->mclk / (clk + 2);
Linus Walleija6a64642009-09-14 12:56:14 +0100323 } else {
Linus Walleijb70a67f2010-12-06 09:24:14 +0100324 /*
325 * PL180 TRM says f = mclk / (2 * (clkdiv + 1))
326 * => clkdiv = mclk / (2 * f) - 1
327 */
Linus Walleija6a64642009-09-14 12:56:14 +0100328 clk = host->mclk / (2 * desired) - 1;
329 if (clk >= 256)
330 clk = 255;
331 host->cclk = host->mclk / (2 * (clk + 1));
332 }
Rabin Vincent4380c142010-07-21 12:55:18 +0100333
334 clk |= variant->clkreg_enable;
Linus Walleija6a64642009-09-14 12:56:14 +0100335 clk |= MCI_CLK_ENABLE;
336 /* This hasn't proven to be worthwhile */
337 /* clk |= MCI_CLK_PWRSAVE; */
338 }
339
Ulf Hanssonc58a8502013-05-13 15:40:03 +0100340 /* Set actual clock for debug */
341 host->mmc->actual_clock = host->cclk;
342
Linus Walleij9e6c82c2009-09-14 12:57:11 +0100343 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4)
Linus Walleij771dc152010-04-08 07:38:52 +0100344 clk |= MCI_4BIT_BUS;
345 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_8)
Srinivas Kandagatlae1412d82014-06-02 10:09:23 +0100346 clk |= variant->clkreg_8bit_bus_enable;
Linus Walleij9e6c82c2009-09-14 12:57:11 +0100347
Seungwon Jeon6dad6c92014-03-14 21:12:13 +0900348 if (host->mmc->ios.timing == MMC_TIMING_UHS_DDR50 ||
349 host->mmc->ios.timing == MMC_TIMING_MMC_DDR52)
Srinivas Kandagatlae8740642014-06-02 10:09:30 +0100350 clk |= variant->clkreg_neg_edge_enable;
Ulf Hansson6dbb6ee2013-01-07 15:30:44 +0100351
Ulf Hansson7437cfa2012-01-18 09:17:27 +0100352 mmci_write_clkreg(host, clk);
Linus Walleija6a64642009-09-14 12:56:14 +0100353}
354
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200355void mmci_dma_release(struct mmci_host *host)
356{
357 if (host->ops && host->ops->dma_release)
358 host->ops->dma_release(host);
359
360 host->use_dma = false;
361}
362
363void mmci_dma_setup(struct mmci_host *host)
364{
365 if (!host->ops || !host->ops->dma_setup)
366 return;
367
368 if (host->ops->dma_setup(host))
369 return;
370
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200371 /* initialize pre request cookie */
372 host->next_cookie = 1;
373
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200374 host->use_dma = true;
375}
376
Ludovic Barree0da1722018-10-08 14:08:41 +0200377/*
378 * Validate mmc prerequisites
379 */
380static int mmci_validate_data(struct mmci_host *host,
381 struct mmc_data *data)
382{
383 if (!data)
384 return 0;
385
386 if (!is_power_of_2(data->blksz)) {
387 dev_err(mmc_dev(host->mmc),
388 "unsupported block size (%d bytes)\n", data->blksz);
389 return -EINVAL;
390 }
391
392 if (host->ops && host->ops->validate_data)
393 return host->ops->validate_data(host, data);
394
395 return 0;
396}
397
Ludovic Barre47983512018-10-08 14:08:36 +0200398int mmci_prep_data(struct mmci_host *host, struct mmc_data *data, bool next)
399{
400 int err;
401
402 if (!host->ops || !host->ops->prep_data)
403 return 0;
404
405 err = host->ops->prep_data(host, data, next);
406
407 if (next && !err)
408 data->host_cookie = ++host->next_cookie < 0 ?
409 1 : host->next_cookie;
410
411 return err;
412}
413
414void mmci_unprep_data(struct mmci_host *host, struct mmc_data *data,
415 int err)
416{
417 if (host->ops && host->ops->unprep_data)
418 host->ops->unprep_data(host, data, err);
419
420 data->host_cookie = 0;
421}
422
Ludovic Barre02769962018-10-08 14:08:37 +0200423void mmci_get_next_data(struct mmci_host *host, struct mmc_data *data)
424{
425 WARN_ON(data->host_cookie && data->host_cookie != host->next_cookie);
426
427 if (host->ops && host->ops->get_next_data)
428 host->ops->get_next_data(host, data);
429}
430
Ludovic Barre135ea302018-10-08 14:08:38 +0200431int mmci_dma_start(struct mmci_host *host, unsigned int datactrl)
432{
433 struct mmc_data *data = host->data;
434 int ret;
435
436 if (!host->use_dma)
437 return -EINVAL;
438
439 ret = mmci_prep_data(host, data, false);
440 if (ret)
441 return ret;
442
443 if (!host->ops || !host->ops->dma_start)
444 return -EINVAL;
445
446 /* Okay, go for it. */
447 dev_vdbg(mmc_dev(host->mmc),
448 "Submit MMCI DMA job, sglen %d blksz %04x blks %04x flags %08x\n",
449 data->sg_len, data->blksz, data->blocks, data->flags);
450
451 host->ops->dma_start(host, &datactrl);
452
453 /* Trigger the DMA transfer */
454 mmci_write_datactrlreg(host, datactrl);
455
456 /*
457 * Let the MMCI say when the data is ended and it's time
458 * to fire next DMA request. When that happens, MMCI will
459 * call mmci_data_end()
460 */
461 writel(readl(host->base + MMCIMASK0) | MCI_DATAENDMASK,
462 host->base + MMCIMASK0);
463 return 0;
464}
465
Ludovic Barre5a9f10c2018-10-08 14:08:39 +0200466void mmci_dma_finalize(struct mmci_host *host, struct mmc_data *data)
467{
468 if (!host->use_dma)
469 return;
470
471 if (host->ops && host->ops->dma_finalize)
472 host->ops->dma_finalize(host, data);
473}
474
Ludovic Barrecfccc6a2018-10-08 14:08:40 +0200475void mmci_dma_error(struct mmci_host *host)
476{
477 if (!host->use_dma)
478 return;
479
480 if (host->ops && host->ops->dma_error)
481 host->ops->dma_error(host);
482}
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484static void
485mmci_request_end(struct mmci_host *host, struct mmc_request *mrq)
486{
487 writel(0, host->base + MMCICOMMAND);
488
Russell Kinge47c2222007-01-08 16:42:51 +0000489 BUG_ON(host->data);
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 host->mrq = NULL;
492 host->cmd = NULL;
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 mmc_request_done(host->mmc, mrq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
Linus Walleij2686b4b2010-10-19 12:39:48 +0100497static void mmci_set_mask1(struct mmci_host *host, unsigned int mask)
498{
499 void __iomem *base = host->base;
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100500 struct variant_data *variant = host->variant;
Linus Walleij2686b4b2010-10-19 12:39:48 +0100501
502 if (host->singleirq) {
503 unsigned int mask0 = readl(base + MMCIMASK0);
504
505 mask0 &= ~MCI_IRQ1MASK;
506 mask0 |= mask;
507
508 writel(mask0, base + MMCIMASK0);
509 }
510
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100511 if (variant->mmcimask1)
512 writel(mask, base + MMCIMASK1);
513
514 host->mask1_reg = mask;
Linus Walleij2686b4b2010-10-19 12:39:48 +0100515}
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517static void mmci_stop_data(struct mmci_host *host)
518{
Ulf Hansson9cc639a2013-05-15 20:48:23 +0100519 mmci_write_datactrlreg(host, 0);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100520 mmci_set_mask1(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 host->data = NULL;
522}
523
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100524static void mmci_init_sg(struct mmci_host *host, struct mmc_data *data)
525{
526 unsigned int flags = SG_MITER_ATOMIC;
527
528 if (data->flags & MMC_DATA_READ)
529 flags |= SG_MITER_TO_SG;
530 else
531 flags |= SG_MITER_FROM_SG;
532
533 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
534}
535
Russell Kingc8ebae32011-01-11 19:35:53 +0000536/*
537 * All the DMA operation mode stuff goes inside this ifdef.
538 * This assumes that you have a generic DMA device interface,
539 * no custom DMA interfaces are supported.
540 */
541#ifdef CONFIG_DMA_ENGINE
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200542struct mmci_dmae_next {
543 struct dma_async_tx_descriptor *desc;
544 struct dma_chan *chan;
545};
546
547struct mmci_dmae_priv {
548 struct dma_chan *cur;
549 struct dma_chan *rx_channel;
550 struct dma_chan *tx_channel;
551 struct dma_async_tx_descriptor *desc_current;
552 struct mmci_dmae_next next_data;
553};
554
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200555int mmci_dmae_setup(struct mmci_host *host)
Russell Kingc8ebae32011-01-11 19:35:53 +0000556{
Russell Kingc8ebae32011-01-11 19:35:53 +0000557 const char *rxname, *txname;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200558 struct mmci_dmae_priv *dmae;
Russell Kingc8ebae32011-01-11 19:35:53 +0000559
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200560 dmae = devm_kzalloc(mmc_dev(host->mmc), sizeof(*dmae), GFP_KERNEL);
561 if (!dmae)
562 return -ENOMEM;
Russell Kingc8ebae32011-01-11 19:35:53 +0000563
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200564 host->dma_priv = dmae;
565
566 dmae->rx_channel = dma_request_slave_channel(mmc_dev(host->mmc),
567 "rx");
568 dmae->tx_channel = dma_request_slave_channel(mmc_dev(host->mmc),
569 "tx");
Per Forlin58c7ccb2011-07-01 18:55:24 +0200570
Russell Kingc8ebae32011-01-11 19:35:53 +0000571 /*
572 * If only an RX channel is specified, the driver will
573 * attempt to use it bidirectionally, however if it is
574 * is specified but cannot be located, DMA will be disabled.
575 */
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200576 if (dmae->rx_channel && !dmae->tx_channel)
577 dmae->tx_channel = dmae->rx_channel;
Russell Kingc8ebae32011-01-11 19:35:53 +0000578
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200579 if (dmae->rx_channel)
580 rxname = dma_chan_name(dmae->rx_channel);
Russell Kingc8ebae32011-01-11 19:35:53 +0000581 else
582 rxname = "none";
583
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200584 if (dmae->tx_channel)
585 txname = dma_chan_name(dmae->tx_channel);
Russell Kingc8ebae32011-01-11 19:35:53 +0000586 else
587 txname = "none";
588
589 dev_info(mmc_dev(host->mmc), "DMA channels RX %s, TX %s\n",
590 rxname, txname);
591
592 /*
593 * Limit the maximum segment size in any SG entry according to
594 * the parameters of the DMA engine device.
595 */
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200596 if (dmae->tx_channel) {
597 struct device *dev = dmae->tx_channel->device->dev;
Russell Kingc8ebae32011-01-11 19:35:53 +0000598 unsigned int max_seg_size = dma_get_max_seg_size(dev);
599
600 if (max_seg_size < host->mmc->max_seg_size)
601 host->mmc->max_seg_size = max_seg_size;
602 }
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200603 if (dmae->rx_channel) {
604 struct device *dev = dmae->rx_channel->device->dev;
Russell Kingc8ebae32011-01-11 19:35:53 +0000605 unsigned int max_seg_size = dma_get_max_seg_size(dev);
606
607 if (max_seg_size < host->mmc->max_seg_size)
608 host->mmc->max_seg_size = max_seg_size;
609 }
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100610
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200611 if (!dmae->tx_channel || !dmae->rx_channel) {
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200612 mmci_dmae_release(host);
613 return -EINVAL;
614 }
615
616 return 0;
Russell Kingc8ebae32011-01-11 19:35:53 +0000617}
618
619/*
Bill Pemberton6e0ee712012-11-19 13:26:03 -0500620 * This is used in or so inline it
Russell Kingc8ebae32011-01-11 19:35:53 +0000621 * so it can be discarded.
622 */
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200623void mmci_dmae_release(struct mmci_host *host)
Russell Kingc8ebae32011-01-11 19:35:53 +0000624{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200625 struct mmci_dmae_priv *dmae = host->dma_priv;
626
627 if (dmae->rx_channel)
628 dma_release_channel(dmae->rx_channel);
629 if (dmae->tx_channel)
630 dma_release_channel(dmae->tx_channel);
631 dmae->rx_channel = dmae->tx_channel = NULL;
Russell Kingc8ebae32011-01-11 19:35:53 +0000632}
633
634static void mmci_dma_unmap(struct mmci_host *host, struct mmc_data *data)
635{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200636 struct mmci_dmae_priv *dmae = host->dma_priv;
Ulf Hansson653a7612013-01-21 21:29:34 +0100637 struct dma_chan *chan;
Ulf Hansson653a7612013-01-21 21:29:34 +0100638
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200639 if (data->flags & MMC_DATA_READ)
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200640 chan = dmae->rx_channel;
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200641 else
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200642 chan = dmae->tx_channel;
Ulf Hansson653a7612013-01-21 21:29:34 +0100643
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200644 dma_unmap_sg(chan->device->dev, data->sg, data->sg_len,
645 mmc_get_dma_dir(data));
Ulf Hansson653a7612013-01-21 21:29:34 +0100646}
647
Ludovic Barrecfccc6a2018-10-08 14:08:40 +0200648void mmci_dmae_error(struct mmci_host *host)
Ludovic Barre7b2a6d52018-09-21 11:45:55 +0200649{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200650 struct mmci_dmae_priv *dmae = host->dma_priv;
651
Ludovic Barrecfccc6a2018-10-08 14:08:40 +0200652 if (!dma_inprogress(host))
Ludovic Barrecdea1942018-09-21 11:45:56 +0200653 return;
654
Ludovic Barre7b2a6d52018-09-21 11:45:55 +0200655 dev_err(mmc_dev(host->mmc), "error during DMA transfer!\n");
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200656 dmaengine_terminate_all(dmae->cur);
Ludovic Barre7b2a6d52018-09-21 11:45:55 +0200657 host->dma_in_progress = false;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200658 dmae->cur = NULL;
659 dmae->desc_current = NULL;
Ludovic Barre7b2a6d52018-09-21 11:45:55 +0200660 host->data->host_cookie = 0;
661
662 mmci_dma_unmap(host, host->data);
663}
664
Ludovic Barre5a9f10c2018-10-08 14:08:39 +0200665void mmci_dmae_finalize(struct mmci_host *host, struct mmc_data *data)
Ulf Hansson653a7612013-01-21 21:29:34 +0100666{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200667 struct mmci_dmae_priv *dmae = host->dma_priv;
Russell Kingc8ebae32011-01-11 19:35:53 +0000668 u32 status;
669 int i;
670
Ludovic Barre5a9f10c2018-10-08 14:08:39 +0200671 if (!dma_inprogress(host))
Ludovic Barrecdea1942018-09-21 11:45:56 +0200672 return;
673
Russell Kingc8ebae32011-01-11 19:35:53 +0000674 /* Wait up to 1ms for the DMA to complete */
675 for (i = 0; ; i++) {
676 status = readl(host->base + MMCISTATUS);
677 if (!(status & MCI_RXDATAAVLBLMASK) || i >= 100)
678 break;
679 udelay(10);
680 }
681
682 /*
683 * Check to see whether we still have some data left in the FIFO -
684 * this catches DMA controllers which are unable to monitor the
685 * DMALBREQ and DMALSREQ signals while allowing us to DMA to non-
686 * contiguous buffers. On TX, we'll get a FIFO underrun error.
687 */
688 if (status & MCI_RXDATAAVLBLMASK) {
Ludovic Barrecfccc6a2018-10-08 14:08:40 +0200689 mmci_dma_error(host);
Russell Kingc8ebae32011-01-11 19:35:53 +0000690 if (!data->error)
691 data->error = -EIO;
Ludovic Barre7b2a6d52018-09-21 11:45:55 +0200692 } else if (!data->host_cookie) {
Ulf Hansson653a7612013-01-21 21:29:34 +0100693 mmci_dma_unmap(host, data);
Ludovic Barre7b2a6d52018-09-21 11:45:55 +0200694 }
Russell Kingc8ebae32011-01-11 19:35:53 +0000695
696 /*
697 * Use of DMA with scatter-gather is impossible.
698 * Give up with DMA and switch back to PIO mode.
699 */
700 if (status & MCI_RXDATAAVLBLMASK) {
701 dev_err(mmc_dev(host->mmc), "buggy DMA detected. Taking evasive action.\n");
702 mmci_dma_release(host);
703 }
Ulf Hansson653a7612013-01-21 21:29:34 +0100704
Linus Walleije13934b2017-01-27 15:04:54 +0100705 host->dma_in_progress = false;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200706 dmae->cur = NULL;
707 dmae->desc_current = NULL;
Russell Kingc8ebae32011-01-11 19:35:53 +0000708}
709
Ulf Hansson653a7612013-01-21 21:29:34 +0100710/* prepares DMA channel and DMA descriptor, returns non-zero on failure */
Ludovic Barre47983512018-10-08 14:08:36 +0200711static int _mmci_dmae_prep_data(struct mmci_host *host, struct mmc_data *data,
Ulf Hansson653a7612013-01-21 21:29:34 +0100712 struct dma_chan **dma_chan,
713 struct dma_async_tx_descriptor **dma_desc)
Russell Kingc8ebae32011-01-11 19:35:53 +0000714{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200715 struct mmci_dmae_priv *dmae = host->dma_priv;
Russell Kingc8ebae32011-01-11 19:35:53 +0000716 struct variant_data *variant = host->variant;
717 struct dma_slave_config conf = {
718 .src_addr = host->phybase + MMCIFIFO,
719 .dst_addr = host->phybase + MMCIFIFO,
720 .src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
721 .dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
722 .src_maxburst = variant->fifohalfsize >> 2, /* # of words */
723 .dst_maxburst = variant->fifohalfsize >> 2, /* # of words */
Viresh Kumar258aea72012-02-01 16:12:19 +0530724 .device_fc = false,
Russell Kingc8ebae32011-01-11 19:35:53 +0000725 };
Russell Kingc8ebae32011-01-11 19:35:53 +0000726 struct dma_chan *chan;
727 struct dma_device *device;
728 struct dma_async_tx_descriptor *desc;
729 int nr_sg;
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100730 unsigned long flags = DMA_CTRL_ACK;
Russell Kingc8ebae32011-01-11 19:35:53 +0000731
Russell Kingc8ebae32011-01-11 19:35:53 +0000732 if (data->flags & MMC_DATA_READ) {
Vinod Koul05f57992011-10-14 10:45:11 +0530733 conf.direction = DMA_DEV_TO_MEM;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200734 chan = dmae->rx_channel;
Russell Kingc8ebae32011-01-11 19:35:53 +0000735 } else {
Vinod Koul05f57992011-10-14 10:45:11 +0530736 conf.direction = DMA_MEM_TO_DEV;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200737 chan = dmae->tx_channel;
Russell Kingc8ebae32011-01-11 19:35:53 +0000738 }
739
740 /* If there's no DMA channel, fall back to PIO */
741 if (!chan)
742 return -EINVAL;
743
744 /* If less than or equal to the fifo size, don't bother with DMA */
Per Forlin58c7ccb2011-07-01 18:55:24 +0200745 if (data->blksz * data->blocks <= variant->fifosize)
Russell Kingc8ebae32011-01-11 19:35:53 +0000746 return -EINVAL;
747
748 device = chan->device;
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200749 nr_sg = dma_map_sg(device->dev, data->sg, data->sg_len,
750 mmc_get_dma_dir(data));
Russell Kingc8ebae32011-01-11 19:35:53 +0000751 if (nr_sg == 0)
752 return -EINVAL;
753
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100754 if (host->variant->qcom_dml)
755 flags |= DMA_PREP_INTERRUPT;
756
Russell Kingc8ebae32011-01-11 19:35:53 +0000757 dmaengine_slave_config(chan, &conf);
Alexandre Bounine16052822012-03-08 16:11:18 -0500758 desc = dmaengine_prep_slave_sg(chan, data->sg, nr_sg,
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100759 conf.direction, flags);
Russell Kingc8ebae32011-01-11 19:35:53 +0000760 if (!desc)
761 goto unmap_exit;
762
Ulf Hansson653a7612013-01-21 21:29:34 +0100763 *dma_chan = chan;
764 *dma_desc = desc;
Russell Kingc8ebae32011-01-11 19:35:53 +0000765
Per Forlin58c7ccb2011-07-01 18:55:24 +0200766 return 0;
767
768 unmap_exit:
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200769 dma_unmap_sg(device->dev, data->sg, data->sg_len,
770 mmc_get_dma_dir(data));
Per Forlin58c7ccb2011-07-01 18:55:24 +0200771 return -ENOMEM;
772}
773
Ludovic Barre47983512018-10-08 14:08:36 +0200774int mmci_dmae_prep_data(struct mmci_host *host,
775 struct mmc_data *data,
776 bool next)
Ulf Hansson653a7612013-01-21 21:29:34 +0100777{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200778 struct mmci_dmae_priv *dmae = host->dma_priv;
Ludovic Barread7b8912018-10-08 14:08:35 +0200779 struct mmci_dmae_next *nd = &dmae->next_data;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200780
Ludovic Barre47983512018-10-08 14:08:36 +0200781 if (!host->use_dma)
782 return -EINVAL;
783
Ludovic Barread7b8912018-10-08 14:08:35 +0200784 if (next)
Ludovic Barre47983512018-10-08 14:08:36 +0200785 return _mmci_dmae_prep_data(host, data, &nd->chan, &nd->desc);
Ulf Hansson653a7612013-01-21 21:29:34 +0100786 /* Check if next job is already prepared. */
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200787 if (dmae->cur && dmae->desc_current)
Ulf Hansson653a7612013-01-21 21:29:34 +0100788 return 0;
789
790 /* No job were prepared thus do it now. */
Ludovic Barre47983512018-10-08 14:08:36 +0200791 return _mmci_dmae_prep_data(host, data, &dmae->cur,
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200792 &dmae->desc_current);
Ulf Hansson653a7612013-01-21 21:29:34 +0100793}
794
Ludovic Barre135ea302018-10-08 14:08:38 +0200795int mmci_dmae_start(struct mmci_host *host, unsigned int *datactrl)
Per Forlin58c7ccb2011-07-01 18:55:24 +0200796{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200797 struct mmci_dmae_priv *dmae = host->dma_priv;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200798 struct mmc_data *data = host->data;
799
Linus Walleije13934b2017-01-27 15:04:54 +0100800 host->dma_in_progress = true;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200801 dmaengine_submit(dmae->desc_current);
802 dma_async_issue_pending(dmae->cur);
Russell Kingc8ebae32011-01-11 19:35:53 +0000803
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100804 if (host->variant->qcom_dml)
805 dml_start_xfer(host, data);
806
Ludovic Barre135ea302018-10-08 14:08:38 +0200807 *datactrl |= MCI_DPSM_DMAENABLE;
Russell Kingc8ebae32011-01-11 19:35:53 +0000808
Russell Kingc8ebae32011-01-11 19:35:53 +0000809 return 0;
Russell Kingc8ebae32011-01-11 19:35:53 +0000810}
Per Forlin58c7ccb2011-07-01 18:55:24 +0200811
Ludovic Barre02769962018-10-08 14:08:37 +0200812void mmci_dmae_get_next_data(struct mmci_host *host, struct mmc_data *data)
Per Forlin58c7ccb2011-07-01 18:55:24 +0200813{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200814 struct mmci_dmae_priv *dmae = host->dma_priv;
815 struct mmci_dmae_next *next = &dmae->next_data;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200816
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200817 if (!host->use_dma)
818 return;
819
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200820 WARN_ON(!data->host_cookie && (next->desc || next->chan));
Per Forlin58c7ccb2011-07-01 18:55:24 +0200821
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200822 dmae->desc_current = next->desc;
823 dmae->cur = next->chan;
824 next->desc = NULL;
825 next->chan = NULL;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200826}
827
Ludovic Barre47983512018-10-08 14:08:36 +0200828void mmci_dmae_unprep_data(struct mmci_host *host,
829 struct mmc_data *data, int err)
830
Per Forlin58c7ccb2011-07-01 18:55:24 +0200831{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200832 struct mmci_dmae_priv *dmae = host->dma_priv;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200833
Ludovic Barre47983512018-10-08 14:08:36 +0200834 if (!host->use_dma)
Per Forlin58c7ccb2011-07-01 18:55:24 +0200835 return;
836
Ulf Hansson653a7612013-01-21 21:29:34 +0100837 mmci_dma_unmap(host, data);
Per Forlin58c7ccb2011-07-01 18:55:24 +0200838
Ulf Hansson653a7612013-01-21 21:29:34 +0100839 if (err) {
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200840 struct mmci_dmae_next *next = &dmae->next_data;
Ulf Hansson653a7612013-01-21 21:29:34 +0100841 struct dma_chan *chan;
842 if (data->flags & MMC_DATA_READ)
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200843 chan = dmae->rx_channel;
Ulf Hansson653a7612013-01-21 21:29:34 +0100844 else
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200845 chan = dmae->tx_channel;
Ulf Hansson653a7612013-01-21 21:29:34 +0100846 dmaengine_terminate_all(chan);
Per Forlin58c7ccb2011-07-01 18:55:24 +0200847
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200848 if (dmae->desc_current == next->desc)
849 dmae->desc_current = NULL;
Srinivas Kandagatlab5c16a62014-10-08 12:25:17 +0100850
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200851 if (dmae->cur == next->chan) {
Linus Walleije13934b2017-01-27 15:04:54 +0100852 host->dma_in_progress = false;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200853 dmae->cur = NULL;
Linus Walleije13934b2017-01-27 15:04:54 +0100854 }
Srinivas Kandagatlab5c16a62014-10-08 12:25:17 +0100855
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200856 next->desc = NULL;
857 next->chan = NULL;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200858 }
859}
860
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200861static struct mmci_host_ops mmci_variant_ops = {
Ludovic Barre47983512018-10-08 14:08:36 +0200862 .prep_data = mmci_dmae_prep_data,
863 .unprep_data = mmci_dmae_unprep_data,
Ludovic Barre02769962018-10-08 14:08:37 +0200864 .get_next_data = mmci_dmae_get_next_data,
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200865 .dma_setup = mmci_dmae_setup,
866 .dma_release = mmci_dmae_release,
Ludovic Barre135ea302018-10-08 14:08:38 +0200867 .dma_start = mmci_dmae_start,
Ludovic Barre5a9f10c2018-10-08 14:08:39 +0200868 .dma_finalize = mmci_dmae_finalize,
Ludovic Barrecfccc6a2018-10-08 14:08:40 +0200869 .dma_error = mmci_dmae_error,
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200870};
871
872void mmci_variant_init(struct mmci_host *host)
873{
874 host->ops = &mmci_variant_ops;
875}
Russell Kingc8ebae32011-01-11 19:35:53 +0000876#endif
877
Ludovic Barre47983512018-10-08 14:08:36 +0200878static void mmci_pre_request(struct mmc_host *mmc, struct mmc_request *mrq)
879{
880 struct mmci_host *host = mmc_priv(mmc);
881 struct mmc_data *data = mrq->data;
882
883 if (!data)
884 return;
885
886 WARN_ON(data->host_cookie);
887
888 if (mmci_validate_data(host, data))
889 return;
890
891 mmci_prep_data(host, data, true);
892}
893
894static void mmci_post_request(struct mmc_host *mmc, struct mmc_request *mrq,
895 int err)
896{
897 struct mmci_host *host = mmc_priv(mmc);
898 struct mmc_data *data = mrq->data;
899
900 if (!data || !data->host_cookie)
901 return;
902
903 mmci_unprep_data(host, data, err);
904}
905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906static void mmci_start_data(struct mmci_host *host, struct mmc_data *data)
907{
Rabin Vincent8301bb62010-08-09 12:57:30 +0100908 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 unsigned int datactrl, timeout, irqmask;
Russell King7b09cda2005-07-01 12:02:59 +0100910 unsigned long long clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 void __iomem *base;
Russell King3bc87f22006-08-27 13:51:28 +0100912 int blksz_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Linus Walleij64de0282010-02-19 01:09:10 +0100914 dev_dbg(mmc_dev(host->mmc), "blksz %04x blks %04x flags %08x\n",
915 data->blksz, data->blocks, data->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917 host->data = data;
Rabin Vincent528320d2010-07-21 12:49:49 +0100918 host->size = data->blksz * data->blocks;
Russell King51d43752011-01-27 10:56:52 +0000919 data->bytes_xfered = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Russell King7b09cda2005-07-01 12:02:59 +0100921 clks = (unsigned long long)data->timeout_ns * host->cclk;
Srinivas Kandagatlac4a35762014-06-02 10:08:39 +0100922 do_div(clks, NSEC_PER_SEC);
Russell King7b09cda2005-07-01 12:02:59 +0100923
924 timeout = data->timeout_clks + (unsigned int)clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 base = host->base;
927 writel(timeout, base + MMCIDATATIMER);
928 writel(host->size, base + MMCIDATALENGTH);
929
Russell King3bc87f22006-08-27 13:51:28 +0100930 blksz_bits = ffs(data->blksz) - 1;
931 BUG_ON(1 << blksz_bits != data->blksz);
932
Philippe Langlais1784b152011-03-25 08:51:52 +0100933 if (variant->blksz_datactrl16)
934 datactrl = MCI_DPSM_ENABLE | (data->blksz << 16);
Srinivas Kandagatlaff783232014-06-02 10:09:06 +0100935 else if (variant->blksz_datactrl4)
936 datactrl = MCI_DPSM_ENABLE | (data->blksz << 4);
Philippe Langlais1784b152011-03-25 08:51:52 +0100937 else
938 datactrl = MCI_DPSM_ENABLE | blksz_bits << 4;
Russell Kingc8ebae32011-01-11 19:35:53 +0000939
940 if (data->flags & MMC_DATA_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 datactrl |= MCI_DPSM_DIRECTION;
Russell Kingc8ebae32011-01-11 19:35:53 +0000942
Srinivas Kandagatlac7354132014-08-22 05:55:16 +0100943 if (host->mmc->card && mmc_card_sdio(host->mmc->card)) {
944 u32 clk;
Ulf Hansson7258db72011-12-13 17:05:28 +0100945
Srinivas Kandagatlac7354132014-08-22 05:55:16 +0100946 datactrl |= variant->datactrl_mask_sdio;
Ulf Hansson06c1a122012-10-12 14:01:50 +0100947
Srinivas Kandagatlac7354132014-08-22 05:55:16 +0100948 /*
949 * The ST Micro variant for SDIO small write transfers
950 * needs to have clock H/W flow control disabled,
951 * otherwise the transfer will not start. The threshold
952 * depends on the rate of MCLK.
953 */
954 if (variant->st_sdio && data->flags & MMC_DATA_WRITE &&
955 (host->size < 8 ||
956 (host->size <= 8 && host->mclk > 50000000)))
957 clk = host->clk_reg & ~variant->clkreg_enable;
958 else
959 clk = host->clk_reg | variant->clkreg_enable;
960
961 mmci_write_clkreg(host, clk);
962 }
Ulf Hansson06c1a122012-10-12 14:01:50 +0100963
Seungwon Jeon6dad6c92014-03-14 21:12:13 +0900964 if (host->mmc->ios.timing == MMC_TIMING_UHS_DDR50 ||
965 host->mmc->ios.timing == MMC_TIMING_MMC_DDR52)
Srinivas Kandagatlae17dca22014-06-02 10:09:15 +0100966 datactrl |= variant->datactrl_mask_ddrmode;
Ulf Hansson6dbb6ee2013-01-07 15:30:44 +0100967
Russell Kingc8ebae32011-01-11 19:35:53 +0000968 /*
969 * Attempt to use DMA operation mode, if this
970 * should fail, fall back to PIO mode
971 */
Ludovic Barre135ea302018-10-08 14:08:38 +0200972 if (!mmci_dma_start(host, datactrl))
Russell Kingc8ebae32011-01-11 19:35:53 +0000973 return;
974
975 /* IRQ mode, map the SG list for CPU reading/writing */
976 mmci_init_sg(host, data);
977
978 if (data->flags & MMC_DATA_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 irqmask = MCI_RXFIFOHALFFULLMASK;
Russell King0425a142006-02-16 16:48:31 +0000980
981 /*
Russell Kingc4d877c2011-01-27 09:50:13 +0000982 * If we have less than the fifo 'half-full' threshold to
983 * transfer, trigger a PIO interrupt as soon as any data
984 * is available.
Russell King0425a142006-02-16 16:48:31 +0000985 */
Russell Kingc4d877c2011-01-27 09:50:13 +0000986 if (host->size < variant->fifohalfsize)
Russell King0425a142006-02-16 16:48:31 +0000987 irqmask |= MCI_RXDATAAVLBLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 } else {
989 /*
990 * We don't actually need to include "FIFO empty" here
991 * since its implicit in "FIFO half empty".
992 */
993 irqmask = MCI_TXFIFOHALFEMPTYMASK;
994 }
995
Ulf Hansson9cc639a2013-05-15 20:48:23 +0100996 mmci_write_datactrlreg(host, datactrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 writel(readl(base + MMCIMASK0) & ~MCI_DATAENDMASK, base + MMCIMASK0);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100998 mmci_set_mask1(host, irqmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
1000
1001static void
1002mmci_start_command(struct mmci_host *host, struct mmc_command *cmd, u32 c)
1003{
1004 void __iomem *base = host->base;
1005
Linus Walleij64de0282010-02-19 01:09:10 +01001006 dev_dbg(mmc_dev(host->mmc), "op %02x arg %08x flags %08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 cmd->opcode, cmd->arg, cmd->flags);
1008
1009 if (readl(base + MMCICOMMAND) & MCI_CPSM_ENABLE) {
1010 writel(0, base + MMCICOMMAND);
Srinivas Kandagatla6adb2a82014-06-02 10:08:57 +01001011 mmci_reg_delay(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 }
1013
1014 c |= cmd->opcode | MCI_CPSM_ENABLE;
Russell Kinge9225172006-02-02 12:23:12 +00001015 if (cmd->flags & MMC_RSP_PRESENT) {
1016 if (cmd->flags & MMC_RSP_136)
1017 c |= MCI_CPSM_LONGRSP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 c |= MCI_CPSM_RESPONSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 }
1020 if (/*interrupt*/0)
1021 c |= MCI_CPSM_INTERRUPT;
1022
Srinivas Kandagatlaae7b0062014-06-02 10:09:39 +01001023 if (mmc_cmd_type(cmd) == MMC_CMD_ADTC)
1024 c |= host->variant->data_cmd_enable;
1025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 host->cmd = cmd;
1027
1028 writel(cmd->arg, base + MMCIARGUMENT);
1029 writel(c, base + MMCICOMMAND);
1030}
1031
1032static void
1033mmci_data_irq(struct mmci_host *host, struct mmc_data *data,
1034 unsigned int status)
1035{
Ulf Hansson1cb9da52014-06-12 14:42:23 +02001036 /* Make sure we have data to handle */
1037 if (!data)
1038 return;
1039
Linus Walleijf20f8f212010-10-19 13:41:24 +01001040 /* First check for errors */
Patrice Chotard7f7b5502018-01-18 15:34:18 +01001041 if (status & (MCI_DATACRCFAIL | MCI_DATATIMEOUT |
1042 host->variant->start_err |
1043 MCI_TXUNDERRUN | MCI_RXOVERRUN)) {
Linus Walleij8cb28152011-01-24 15:22:13 +01001044 u32 remain, success;
Linus Walleijf20f8f212010-10-19 13:41:24 +01001045
Russell Kingc8ebae32011-01-11 19:35:53 +00001046 /* Terminate the DMA transfer */
Ludovic Barrecfccc6a2018-10-08 14:08:40 +02001047 mmci_dma_error(host);
Russell Kingc8ebae32011-01-11 19:35:53 +00001048
Russell Kingc8afc9d2011-02-04 09:19:46 +00001049 /*
1050 * Calculate how far we are into the transfer. Note that
1051 * the data counter gives the number of bytes transferred
1052 * on the MMC bus, not on the host side. On reads, this
1053 * can be as much as a FIFO-worth of data ahead. This
1054 * matters for FIFO overruns only.
1055 */
Linus Walleijf5a106d2011-01-27 17:44:34 +01001056 remain = readl(host->base + MMCIDATACNT);
Linus Walleij8cb28152011-01-24 15:22:13 +01001057 success = data->blksz * data->blocks - remain;
1058
Russell Kingc8afc9d2011-02-04 09:19:46 +00001059 dev_dbg(mmc_dev(host->mmc), "MCI ERROR IRQ, status 0x%08x at 0x%08x\n",
1060 status, success);
Linus Walleij8cb28152011-01-24 15:22:13 +01001061 if (status & MCI_DATACRCFAIL) {
1062 /* Last block was not successful */
Russell Kingc8afc9d2011-02-04 09:19:46 +00001063 success -= 1;
Pierre Ossman17b04292007-07-22 22:18:46 +02001064 data->error = -EILSEQ;
Linus Walleij8cb28152011-01-24 15:22:13 +01001065 } else if (status & MCI_DATATIMEOUT) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001066 data->error = -ETIMEDOUT;
Linus Walleij757df742011-06-30 15:10:21 +01001067 } else if (status & MCI_STARTBITERR) {
1068 data->error = -ECOMM;
Russell Kingc8afc9d2011-02-04 09:19:46 +00001069 } else if (status & MCI_TXUNDERRUN) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001070 data->error = -EIO;
Russell Kingc8afc9d2011-02-04 09:19:46 +00001071 } else if (status & MCI_RXOVERRUN) {
1072 if (success > host->variant->fifosize)
1073 success -= host->variant->fifosize;
1074 else
1075 success = 0;
Linus Walleij8cb28152011-01-24 15:22:13 +01001076 data->error = -EIO;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001077 }
Russell King51d43752011-01-27 10:56:52 +00001078 data->bytes_xfered = round_down(success, data->blksz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 }
Linus Walleijf20f8f212010-10-19 13:41:24 +01001080
Linus Walleij8cb28152011-01-24 15:22:13 +01001081 if (status & MCI_DATABLOCKEND)
1082 dev_err(mmc_dev(host->mmc), "stray MCI_DATABLOCKEND interrupt\n");
Linus Walleijf20f8f212010-10-19 13:41:24 +01001083
Russell Kingccff9b52011-01-30 21:03:50 +00001084 if (status & MCI_DATAEND || data->error) {
Ludovic Barrecdea1942018-09-21 11:45:56 +02001085 mmci_dma_finalize(host, data);
1086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 mmci_stop_data(host);
1088
Linus Walleij8cb28152011-01-24 15:22:13 +01001089 if (!data->error)
1090 /* The error clause is handled above, success! */
Russell King51d43752011-01-27 10:56:52 +00001091 data->bytes_xfered = data->blksz * data->blocks;
Linus Walleijf20f8f212010-10-19 13:41:24 +01001092
Ulf Hansson024629c2013-05-13 15:40:56 +01001093 if (!data->stop || host->mrq->sbc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 mmci_request_end(host, data->mrq);
1095 } else {
1096 mmci_start_command(host, data->stop, 0);
1097 }
1098 }
1099}
1100
1101static void
1102mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
1103 unsigned int status)
1104{
1105 void __iomem *base = host->base;
Linus Walleij49adc0c2016-10-25 11:06:06 +02001106 bool sbc;
Ulf Hanssonad82bfe2014-06-12 15:01:57 +02001107
1108 if (!cmd)
1109 return;
1110
1111 sbc = (cmd == host->mrq->sbc);
Ulf Hanssonad82bfe2014-06-12 15:01:57 +02001112
Linus Walleij49adc0c2016-10-25 11:06:06 +02001113 /*
1114 * We need to be one of these interrupts to be considered worth
1115 * handling. Note that we tag on any latent IRQs postponed
1116 * due to waiting for busy status.
1117 */
1118 if (!((status|host->busy_status) &
1119 (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT|MCI_CMDSENT|MCI_CMDRESPEND)))
Ulf Hanssonad82bfe2014-06-12 15:01:57 +02001120 return;
Ulf Hansson8d94b542014-01-13 16:49:31 +01001121
Linus Walleij49adc0c2016-10-25 11:06:06 +02001122 /*
1123 * ST Micro variant: handle busy detection.
1124 */
1125 if (host->variant->busy_detect) {
1126 bool busy_resp = !!(cmd->flags & MMC_RSP_BUSY);
Ulf Hansson8d94b542014-01-13 16:49:31 +01001127
Linus Walleij49adc0c2016-10-25 11:06:06 +02001128 /* We are busy with a command, return */
1129 if (host->busy_status &&
1130 (status & host->variant->busy_detect_flag))
1131 return;
Ulf Hansson8d94b542014-01-13 16:49:31 +01001132
Linus Walleij49adc0c2016-10-25 11:06:06 +02001133 /*
1134 * We were not busy, but we now got a busy response on
1135 * something that was not an error, and we double-check
1136 * that the special busy status bit is still set before
1137 * proceeding.
1138 */
1139 if (!host->busy_status && busy_resp &&
1140 !(status & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT)) &&
1141 (readl(base + MMCISTATUS) & host->variant->busy_detect_flag)) {
Jean-Nicolas Graux5cad24d2017-02-07 12:12:41 +01001142
1143 /* Clear the busy start IRQ */
1144 writel(host->variant->busy_detect_mask,
1145 host->base + MMCICLEAR);
1146
1147 /* Unmask the busy end IRQ */
Linus Walleij49adc0c2016-10-25 11:06:06 +02001148 writel(readl(base + MMCIMASK0) |
1149 host->variant->busy_detect_mask,
1150 base + MMCIMASK0);
1151 /*
1152 * Now cache the last response status code (until
1153 * the busy bit goes low), and return.
1154 */
1155 host->busy_status =
1156 status & (MCI_CMDSENT|MCI_CMDRESPEND);
1157 return;
1158 }
1159
1160 /*
1161 * At this point we are not busy with a command, we have
Jean-Nicolas Graux5cad24d2017-02-07 12:12:41 +01001162 * not received a new busy request, clear and mask the busy
1163 * end IRQ and fall through to process the IRQ.
Linus Walleij49adc0c2016-10-25 11:06:06 +02001164 */
1165 if (host->busy_status) {
Jean-Nicolas Graux5cad24d2017-02-07 12:12:41 +01001166
1167 writel(host->variant->busy_detect_mask,
1168 host->base + MMCICLEAR);
1169
Linus Walleij49adc0c2016-10-25 11:06:06 +02001170 writel(readl(base + MMCIMASK0) &
1171 ~host->variant->busy_detect_mask,
1172 base + MMCIMASK0);
1173 host->busy_status = 0;
1174 }
Ulf Hansson8d94b542014-01-13 16:49:31 +01001175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177 host->cmd = NULL;
1178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 if (status & MCI_CMDTIMEOUT) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001180 cmd->error = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001182 cmd->error = -EILSEQ;
Russell King - ARM Linux9047b432011-01-11 16:35:56 +00001183 } else {
1184 cmd->resp[0] = readl(base + MMCIRESPONSE0);
1185 cmd->resp[1] = readl(base + MMCIRESPONSE1);
1186 cmd->resp[2] = readl(base + MMCIRESPONSE2);
1187 cmd->resp[3] = readl(base + MMCIRESPONSE3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 }
1189
Ulf Hansson024629c2013-05-13 15:40:56 +01001190 if ((!sbc && !cmd->data) || cmd->error) {
Ulf Hansson3b6e3c72011-12-13 16:58:43 +01001191 if (host->data) {
1192 /* Terminate the DMA transfer */
Ludovic Barrecfccc6a2018-10-08 14:08:40 +02001193 mmci_dma_error(host);
Ludovic Barre7b2a6d52018-09-21 11:45:55 +02001194
Russell Kinge47c2222007-01-08 16:42:51 +00001195 mmci_stop_data(host);
Ulf Hansson3b6e3c72011-12-13 16:58:43 +01001196 }
Ulf Hansson024629c2013-05-13 15:40:56 +01001197 mmci_request_end(host, host->mrq);
1198 } else if (sbc) {
1199 mmci_start_command(host, host->mrq->cmd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 } else if (!(cmd->data->flags & MMC_DATA_READ)) {
1201 mmci_start_data(host, cmd->data);
1202 }
1203}
1204
Srinivas Kandagatla9c34b732014-06-02 10:10:04 +01001205static int mmci_get_rx_fifocnt(struct mmci_host *host, u32 status, int remain)
1206{
1207 return remain - (readl(host->base + MMCIFIFOCNT) << 2);
1208}
1209
1210static int mmci_qcom_get_rx_fifocnt(struct mmci_host *host, u32 status, int r)
1211{
1212 /*
1213 * on qcom SDCC4 only 8 words are used in each burst so only 8 addresses
1214 * from the fifo range should be used
1215 */
1216 if (status & MCI_RXFIFOHALFFULL)
1217 return host->variant->fifohalfsize;
1218 else if (status & MCI_RXDATAAVLBL)
1219 return 4;
1220
1221 return 0;
1222}
1223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224static int mmci_pio_read(struct mmci_host *host, char *buffer, unsigned int remain)
1225{
1226 void __iomem *base = host->base;
1227 char *ptr = buffer;
Srinivas Kandagatla9c34b732014-06-02 10:10:04 +01001228 u32 status = readl(host->base + MMCISTATUS);
Linus Walleij26eed9a2008-04-26 23:39:44 +01001229 int host_remain = host->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231 do {
Srinivas Kandagatla9c34b732014-06-02 10:10:04 +01001232 int count = host->get_rx_fifocnt(host, status, host_remain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 if (count > remain)
1235 count = remain;
1236
1237 if (count <= 0)
1238 break;
1239
Ulf Hansson393e5e22011-12-13 17:08:04 +01001240 /*
1241 * SDIO especially may want to send something that is
1242 * not divisible by 4 (as opposed to card sectors
1243 * etc). Therefore make sure to always read the last bytes
1244 * while only doing full 32-bit reads towards the FIFO.
1245 */
1246 if (unlikely(count & 0x3)) {
1247 if (count < 4) {
1248 unsigned char buf[4];
Davide Ciminaghi4b85da02012-12-10 14:47:21 +01001249 ioread32_rep(base + MMCIFIFO, buf, 1);
Ulf Hansson393e5e22011-12-13 17:08:04 +01001250 memcpy(ptr, buf, count);
1251 } else {
Davide Ciminaghi4b85da02012-12-10 14:47:21 +01001252 ioread32_rep(base + MMCIFIFO, ptr, count >> 2);
Ulf Hansson393e5e22011-12-13 17:08:04 +01001253 count &= ~0x3;
1254 }
1255 } else {
Davide Ciminaghi4b85da02012-12-10 14:47:21 +01001256 ioread32_rep(base + MMCIFIFO, ptr, count >> 2);
Ulf Hansson393e5e22011-12-13 17:08:04 +01001257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259 ptr += count;
1260 remain -= count;
Linus Walleij26eed9a2008-04-26 23:39:44 +01001261 host_remain -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 if (remain == 0)
1264 break;
1265
1266 status = readl(base + MMCISTATUS);
1267 } while (status & MCI_RXDATAAVLBL);
1268
1269 return ptr - buffer;
1270}
1271
1272static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status)
1273{
Rabin Vincent8301bb62010-08-09 12:57:30 +01001274 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 void __iomem *base = host->base;
1276 char *ptr = buffer;
1277
1278 do {
1279 unsigned int count, maxcnt;
1280
Rabin Vincent8301bb62010-08-09 12:57:30 +01001281 maxcnt = status & MCI_TXFIFOEMPTY ?
1282 variant->fifosize : variant->fifohalfsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 count = min(remain, maxcnt);
1284
Linus Walleij34177802010-10-19 12:43:58 +01001285 /*
Linus Walleij34177802010-10-19 12:43:58 +01001286 * SDIO especially may want to send something that is
1287 * not divisible by 4 (as opposed to card sectors
1288 * etc), and the FIFO only accept full 32-bit writes.
1289 * So compensate by adding +3 on the count, a single
1290 * byte become a 32bit write, 7 bytes will be two
1291 * 32bit writes etc.
1292 */
Davide Ciminaghi4b85da02012-12-10 14:47:21 +01001293 iowrite32_rep(base + MMCIFIFO, ptr, (count + 3) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 ptr += count;
1296 remain -= count;
1297
1298 if (remain == 0)
1299 break;
1300
1301 status = readl(base + MMCISTATUS);
1302 } while (status & MCI_TXFIFOHALFEMPTY);
1303
1304 return ptr - buffer;
1305}
1306
1307/*
1308 * PIO data transfer IRQ handler.
1309 */
David Howells7d12e782006-10-05 14:55:46 +01001310static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
1312 struct mmci_host *host = dev_id;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001313 struct sg_mapping_iter *sg_miter = &host->sg_miter;
Rabin Vincent8301bb62010-08-09 12:57:30 +01001314 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 void __iomem *base = host->base;
1316 u32 status;
1317
1318 status = readl(base + MMCISTATUS);
1319
Linus Walleij64de0282010-02-19 01:09:10 +01001320 dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 unsigned int remain, len;
1324 char *buffer;
1325
1326 /*
1327 * For write, we only need to test the half-empty flag
1328 * here - if the FIFO is completely empty, then by
1329 * definition it is more than half empty.
1330 *
1331 * For read, check for data available.
1332 */
1333 if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL)))
1334 break;
1335
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001336 if (!sg_miter_next(sg_miter))
1337 break;
1338
1339 buffer = sg_miter->addr;
1340 remain = sg_miter->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 len = 0;
1343 if (status & MCI_RXACTIVE)
1344 len = mmci_pio_read(host, buffer, remain);
1345 if (status & MCI_TXACTIVE)
1346 len = mmci_pio_write(host, buffer, remain, status);
1347
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001348 sg_miter->consumed = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 host->size -= len;
1351 remain -= len;
1352
1353 if (remain)
1354 break;
1355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 status = readl(base + MMCISTATUS);
1357 } while (1);
1358
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001359 sg_miter_stop(sg_miter);
1360
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 /*
Russell Kingc4d877c2011-01-27 09:50:13 +00001362 * If we have less than the fifo 'half-full' threshold to transfer,
1363 * trigger a PIO interrupt as soon as any data is available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 */
Russell Kingc4d877c2011-01-27 09:50:13 +00001365 if (status & MCI_RXACTIVE && host->size < variant->fifohalfsize)
Linus Walleij2686b4b2010-10-19 12:39:48 +01001366 mmci_set_mask1(host, MCI_RXDATAAVLBLMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
1368 /*
1369 * If we run out of data, disable the data IRQs; this
1370 * prevents a race where the FIFO becomes empty before
1371 * the chip itself has disabled the data path, and
1372 * stops us racing with our data end IRQ.
1373 */
1374 if (host->size == 0) {
Linus Walleij2686b4b2010-10-19 12:39:48 +01001375 mmci_set_mask1(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0);
1377 }
1378
1379 return IRQ_HANDLED;
1380}
1381
1382/*
1383 * Handle completion of command and data transfers.
1384 */
David Howells7d12e782006-10-05 14:55:46 +01001385static irqreturn_t mmci_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386{
1387 struct mmci_host *host = dev_id;
1388 u32 status;
1389 int ret = 0;
1390
1391 spin_lock(&host->lock);
1392
1393 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 status = readl(host->base + MMCISTATUS);
Linus Walleij2686b4b2010-10-19 12:39:48 +01001395
1396 if (host->singleirq) {
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +01001397 if (status & host->mask1_reg)
Linus Walleij2686b4b2010-10-19 12:39:48 +01001398 mmci_pio_irq(irq, dev_id);
1399
1400 status &= ~MCI_IRQ1MASK;
1401 }
1402
Ulf Hansson8d94b542014-01-13 16:49:31 +01001403 /*
Jean-Nicolas Graux5cad24d2017-02-07 12:12:41 +01001404 * We intentionally clear the MCI_ST_CARDBUSY IRQ (if it's
1405 * enabled) in mmci_cmd_irq() function where ST Micro busy
1406 * detection variant is handled. Considering the HW seems to be
1407 * triggering the IRQ on both edges while monitoring DAT0 for
1408 * busy completion and that same status bit is used to monitor
1409 * start and end of busy detection, special care must be taken
1410 * to make sure that both start and end interrupts are always
1411 * cleared one after the other.
Ulf Hansson8d94b542014-01-13 16:49:31 +01001412 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 status &= readl(host->base + MMCIMASK0);
Jean-Nicolas Graux5cad24d2017-02-07 12:12:41 +01001414 if (host->variant->busy_detect)
1415 writel(status & ~host->variant->busy_detect_mask,
1416 host->base + MMCICLEAR);
1417 else
1418 writel(status, host->base + MMCICLEAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Linus Walleij64de0282010-02-19 01:09:10 +01001420 dev_dbg(mmc_dev(host->mmc), "irq0 (data+cmd) %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Ulf Hansson78782892014-06-13 13:21:38 +02001422 if (host->variant->reversed_irq_handling) {
1423 mmci_data_irq(host, host->data, status);
1424 mmci_cmd_irq(host, host->cmd, status);
1425 } else {
1426 mmci_cmd_irq(host, host->cmd, status);
1427 mmci_data_irq(host, host->data, status);
1428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Linus Walleij49adc0c2016-10-25 11:06:06 +02001430 /*
1431 * Don't poll for busy completion in irq context.
1432 */
1433 if (host->variant->busy_detect && host->busy_status)
1434 status &= ~host->variant->busy_detect_flag;
Ulf Hansson8d94b542014-01-13 16:49:31 +01001435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 ret = 1;
1437 } while (status);
1438
1439 spin_unlock(&host->lock);
1440
1441 return IRQ_RETVAL(ret);
1442}
1443
1444static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1445{
1446 struct mmci_host *host = mmc_priv(mmc);
Linus Walleij9e943022008-10-24 21:17:50 +01001447 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449 WARN_ON(host->mrq != NULL);
1450
Ulf Hansson653a7612013-01-21 21:29:34 +01001451 mrq->cmd->error = mmci_validate_data(host, mrq->data);
1452 if (mrq->cmd->error) {
Pierre Ossman255d01a2007-07-24 20:38:53 +02001453 mmc_request_done(mmc, mrq);
1454 return;
1455 }
1456
Linus Walleij9e943022008-10-24 21:17:50 +01001457 spin_lock_irqsave(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
1459 host->mrq = mrq;
1460
Per Forlin58c7ccb2011-07-01 18:55:24 +02001461 if (mrq->data)
1462 mmci_get_next_data(host, mrq->data);
1463
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 if (mrq->data && mrq->data->flags & MMC_DATA_READ)
1465 mmci_start_data(host, mrq->data);
1466
Ulf Hansson024629c2013-05-13 15:40:56 +01001467 if (mrq->sbc)
1468 mmci_start_command(host, mrq->sbc, 0);
1469 else
1470 mmci_start_command(host, mrq->cmd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Linus Walleij9e943022008-10-24 21:17:50 +01001472 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473}
1474
1475static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1476{
1477 struct mmci_host *host = mmc_priv(mmc);
Ulf Hansson7d72a1d2011-12-13 16:54:55 +01001478 struct variant_data *variant = host->variant;
Linus Walleija6a64642009-09-14 12:56:14 +01001479 u32 pwr = 0;
1480 unsigned long flags;
Lee Jonesdb90f912013-05-03 12:52:12 +01001481 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Ulf Hanssonbc521812011-12-13 16:57:55 +01001483 if (host->plat->ios_handler &&
1484 host->plat->ios_handler(mmc_dev(mmc), ios))
1485 dev_err(mmc_dev(mmc), "platform ios_handler failed\n");
1486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 switch (ios->power_mode) {
1488 case MMC_POWER_OFF:
Ulf Hansson599c1d52013-01-07 16:22:50 +01001489 if (!IS_ERR(mmc->supply.vmmc))
1490 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Lee Jones237fb5e2013-01-31 11:27:52 +00001491
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001492 if (!IS_ERR(mmc->supply.vqmmc) && host->vqmmc_enabled) {
Lee Jones237fb5e2013-01-31 11:27:52 +00001493 regulator_disable(mmc->supply.vqmmc);
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001494 host->vqmmc_enabled = false;
1495 }
Lee Jones237fb5e2013-01-31 11:27:52 +00001496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 break;
1498 case MMC_POWER_UP:
Ulf Hansson599c1d52013-01-07 16:22:50 +01001499 if (!IS_ERR(mmc->supply.vmmc))
1500 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd);
1501
Ulf Hansson7d72a1d2011-12-13 16:54:55 +01001502 /*
1503 * The ST Micro variant doesn't have the PL180s MCI_PWR_UP
1504 * and instead uses MCI_PWR_ON so apply whatever value is
1505 * configured in the variant data.
1506 */
1507 pwr |= variant->pwrreg_powerup;
1508
1509 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 case MMC_POWER_ON:
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001511 if (!IS_ERR(mmc->supply.vqmmc) && !host->vqmmc_enabled) {
Lee Jonesdb90f912013-05-03 12:52:12 +01001512 ret = regulator_enable(mmc->supply.vqmmc);
1513 if (ret < 0)
1514 dev_err(mmc_dev(mmc),
1515 "failed to enable vqmmc regulator\n");
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001516 else
1517 host->vqmmc_enabled = true;
Lee Jonesdb90f912013-05-03 12:52:12 +01001518 }
Lee Jones237fb5e2013-01-31 11:27:52 +00001519
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 pwr |= MCI_PWR_ON;
1521 break;
1522 }
1523
Ulf Hansson4d1a3a02011-12-13 16:57:07 +01001524 if (variant->signal_direction && ios->power_mode != MMC_POWER_OFF) {
1525 /*
1526 * The ST Micro variant has some additional bits
1527 * indicating signal direction for the signals in
1528 * the SD/MMC bus and feedback-clock usage.
1529 */
Ulf Hansson4593df22014-03-21 10:13:05 +01001530 pwr |= host->pwr_reg_add;
Ulf Hansson4d1a3a02011-12-13 16:57:07 +01001531
1532 if (ios->bus_width == MMC_BUS_WIDTH_4)
1533 pwr &= ~MCI_ST_DATA74DIREN;
1534 else if (ios->bus_width == MMC_BUS_WIDTH_1)
1535 pwr &= (~MCI_ST_DATA74DIREN &
1536 ~MCI_ST_DATA31DIREN &
1537 ~MCI_ST_DATA2DIREN);
1538 }
1539
Patrice Chotardf9bb3042018-01-18 15:34:20 +01001540 if (variant->opendrain) {
1541 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
1542 pwr |= variant->opendrain;
1543 } else {
1544 /*
1545 * If the variant cannot configure the pads by its own, then we
1546 * expect the pinctrl to be able to do that for us
1547 */
1548 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
1549 pinctrl_select_state(host->pinctrl, host->pins_opendrain);
1550 else
1551 pinctrl_select_state(host->pinctrl, host->pins_default);
1552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Ulf Hanssonf4670da2013-01-09 17:19:54 +01001554 /*
1555 * If clock = 0 and the variant requires the MMCIPOWER to be used for
1556 * gating the clock, the MCI_PWR_ON bit is cleared.
1557 */
1558 if (!ios->clock && variant->pwrreg_clkgate)
1559 pwr &= ~MCI_PWR_ON;
1560
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001561 if (host->variant->explicit_mclk_control &&
1562 ios->clock != host->clock_cache) {
1563 ret = clk_set_rate(host->clk, ios->clock);
1564 if (ret < 0)
1565 dev_err(mmc_dev(host->mmc),
1566 "Error setting clock rate (%d)\n", ret);
1567 else
1568 host->mclk = clk_get_rate(host->clk);
1569 }
1570 host->clock_cache = ios->clock;
1571
Linus Walleija6a64642009-09-14 12:56:14 +01001572 spin_lock_irqsave(&host->lock, flags);
1573
Ludovic Barrecd3ee8c2018-10-08 14:08:42 +02001574 if (host->ops && host->ops->set_clkreg)
1575 host->ops->set_clkreg(host, ios->clock);
1576 else
1577 mmci_set_clkreg(host, ios->clock);
1578
1579 if (host->ops && host->ops->set_pwrreg)
1580 host->ops->set_pwrreg(host, pwr);
1581 else
1582 mmci_write_pwrreg(host, pwr);
1583
Ulf Hanssonf829c042013-09-04 09:01:15 +01001584 mmci_reg_delay(host);
Linus Walleija6a64642009-09-14 12:56:14 +01001585
1586 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587}
1588
Russell King89001442009-07-09 15:16:07 +01001589static int mmci_get_cd(struct mmc_host *mmc)
1590{
1591 struct mmci_host *host = mmc_priv(mmc);
Rabin Vincent29719442010-08-09 12:54:43 +01001592 struct mmci_platform_data *plat = host->plat;
Ulf Hanssond2762092014-03-17 13:56:19 +01001593 unsigned int status = mmc_gpio_get_cd(mmc);
Russell King89001442009-07-09 15:16:07 +01001594
Ulf Hanssond2762092014-03-17 13:56:19 +01001595 if (status == -ENOSYS) {
Rabin Vincent4b8caec2010-08-09 12:56:40 +01001596 if (!plat->status)
1597 return 1; /* Assume always present */
1598
Rabin Vincent29719442010-08-09 12:54:43 +01001599 status = plat->status(mmc_dev(host->mmc));
Ulf Hanssond2762092014-03-17 13:56:19 +01001600 }
Russell King74bc8092010-07-29 15:58:59 +01001601 return status;
Russell King89001442009-07-09 15:16:07 +01001602}
1603
Ulf Hansson0f3ed7f2013-05-15 20:47:33 +01001604static int mmci_sig_volt_switch(struct mmc_host *mmc, struct mmc_ios *ios)
1605{
1606 int ret = 0;
1607
1608 if (!IS_ERR(mmc->supply.vqmmc)) {
1609
Ulf Hansson0f3ed7f2013-05-15 20:47:33 +01001610 switch (ios->signal_voltage) {
1611 case MMC_SIGNAL_VOLTAGE_330:
1612 ret = regulator_set_voltage(mmc->supply.vqmmc,
1613 2700000, 3600000);
1614 break;
1615 case MMC_SIGNAL_VOLTAGE_180:
1616 ret = regulator_set_voltage(mmc->supply.vqmmc,
1617 1700000, 1950000);
1618 break;
1619 case MMC_SIGNAL_VOLTAGE_120:
1620 ret = regulator_set_voltage(mmc->supply.vqmmc,
1621 1100000, 1300000);
1622 break;
1623 }
1624
1625 if (ret)
1626 dev_warn(mmc_dev(mmc), "Voltage switch failed\n");
Ulf Hansson0f3ed7f2013-05-15 20:47:33 +01001627 }
1628
1629 return ret;
1630}
1631
Ulf Hansson01259622013-05-15 20:53:22 +01001632static struct mmc_host_ops mmci_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 .request = mmci_request,
Per Forlin58c7ccb2011-07-01 18:55:24 +02001634 .pre_req = mmci_pre_request,
1635 .post_req = mmci_post_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 .set_ios = mmci_set_ios,
Ulf Hanssond2762092014-03-17 13:56:19 +01001637 .get_ro = mmc_gpio_get_ro,
Russell King89001442009-07-09 15:16:07 +01001638 .get_cd = mmci_get_cd,
Ulf Hansson0f3ed7f2013-05-15 20:47:33 +01001639 .start_signal_voltage_switch = mmci_sig_volt_switch,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640};
1641
Ulf Hansson78f87df2014-03-17 15:53:07 +01001642static int mmci_of_parse(struct device_node *np, struct mmc_host *mmc)
1643{
Ulf Hansson4593df22014-03-21 10:13:05 +01001644 struct mmci_host *host = mmc_priv(mmc);
Ulf Hansson78f87df2014-03-17 15:53:07 +01001645 int ret = mmc_of_parse(mmc);
Lee Jones000bc9d2012-04-16 10:18:43 +01001646
Ulf Hansson78f87df2014-03-17 15:53:07 +01001647 if (ret)
1648 return ret;
Lee Jones000bc9d2012-04-16 10:18:43 +01001649
Ulf Hansson4593df22014-03-21 10:13:05 +01001650 if (of_get_property(np, "st,sig-dir-dat0", NULL))
1651 host->pwr_reg_add |= MCI_ST_DATA0DIREN;
1652 if (of_get_property(np, "st,sig-dir-dat2", NULL))
1653 host->pwr_reg_add |= MCI_ST_DATA2DIREN;
1654 if (of_get_property(np, "st,sig-dir-dat31", NULL))
1655 host->pwr_reg_add |= MCI_ST_DATA31DIREN;
1656 if (of_get_property(np, "st,sig-dir-dat74", NULL))
1657 host->pwr_reg_add |= MCI_ST_DATA74DIREN;
1658 if (of_get_property(np, "st,sig-dir-cmd", NULL))
1659 host->pwr_reg_add |= MCI_ST_CMDDIREN;
1660 if (of_get_property(np, "st,sig-pin-fbclk", NULL))
1661 host->pwr_reg_add |= MCI_ST_FBCLKEN;
1662
Lee Jones000bc9d2012-04-16 10:18:43 +01001663 if (of_get_property(np, "mmc-cap-mmc-highspeed", NULL))
Ulf Hansson78f87df2014-03-17 15:53:07 +01001664 mmc->caps |= MMC_CAP_MMC_HIGHSPEED;
Lee Jones000bc9d2012-04-16 10:18:43 +01001665 if (of_get_property(np, "mmc-cap-sd-highspeed", NULL))
Ulf Hansson78f87df2014-03-17 15:53:07 +01001666 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
Lee Jones000bc9d2012-04-16 10:18:43 +01001667
Ulf Hansson78f87df2014-03-17 15:53:07 +01001668 return 0;
Lee Jones000bc9d2012-04-16 10:18:43 +01001669}
Lee Jones000bc9d2012-04-16 10:18:43 +01001670
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001671static int mmci_probe(struct amba_device *dev,
Russell Kingaa25afa2011-02-19 15:55:00 +00001672 const struct amba_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673{
Linus Walleij6ef297f2009-09-22 14:29:36 +01001674 struct mmci_platform_data *plat = dev->dev.platform_data;
Lee Jones000bc9d2012-04-16 10:18:43 +01001675 struct device_node *np = dev->dev.of_node;
Rabin Vincent4956e102010-07-21 12:54:40 +01001676 struct variant_data *variant = id->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 struct mmci_host *host;
1678 struct mmc_host *mmc;
1679 int ret;
1680
Lee Jones000bc9d2012-04-16 10:18:43 +01001681 /* Must have platform data or Device Tree. */
1682 if (!plat && !np) {
1683 dev_err(&dev->dev, "No plat data or DT found\n");
1684 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 }
1686
Lee Jonesb9b52912012-06-12 10:49:51 +01001687 if (!plat) {
1688 plat = devm_kzalloc(&dev->dev, sizeof(*plat), GFP_KERNEL);
1689 if (!plat)
1690 return -ENOMEM;
1691 }
1692
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
Ulf Hanssonef289982014-03-17 13:56:32 +01001694 if (!mmc)
1695 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Ulf Hansson78f87df2014-03-17 15:53:07 +01001697 ret = mmci_of_parse(np, mmc);
1698 if (ret)
1699 goto host_free;
1700
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 host = mmc_priv(mmc);
Rabin Vincent4ea580f2009-04-17 08:44:19 +05301702 host->mmc = mmc;
Russell King012b7d32009-07-09 15:13:56 +01001703
Patrice Chotardf9bb3042018-01-18 15:34:20 +01001704 /*
1705 * Some variant (STM32) doesn't have opendrain bit, nevertheless
1706 * pins can be set accordingly using pinctrl
1707 */
1708 if (!variant->opendrain) {
1709 host->pinctrl = devm_pinctrl_get(&dev->dev);
1710 if (IS_ERR(host->pinctrl)) {
1711 dev_err(&dev->dev, "failed to get pinctrl");
Wei Yongjun310eb252018-01-23 02:09:13 +00001712 ret = PTR_ERR(host->pinctrl);
Patrice Chotardf9bb3042018-01-18 15:34:20 +01001713 goto host_free;
1714 }
1715
1716 host->pins_default = pinctrl_lookup_state(host->pinctrl,
1717 PINCTRL_STATE_DEFAULT);
1718 if (IS_ERR(host->pins_default)) {
1719 dev_err(mmc_dev(mmc), "Can't select default pins\n");
Wei Yongjun310eb252018-01-23 02:09:13 +00001720 ret = PTR_ERR(host->pins_default);
Patrice Chotardf9bb3042018-01-18 15:34:20 +01001721 goto host_free;
1722 }
1723
1724 host->pins_opendrain = pinctrl_lookup_state(host->pinctrl,
1725 MMCI_PINCTRL_STATE_OPENDRAIN);
1726 if (IS_ERR(host->pins_opendrain)) {
1727 dev_err(mmc_dev(mmc), "Can't select opendrain pins\n");
Wei Yongjun310eb252018-01-23 02:09:13 +00001728 ret = PTR_ERR(host->pins_opendrain);
Patrice Chotardf9bb3042018-01-18 15:34:20 +01001729 goto host_free;
1730 }
1731 }
1732
Russell King012b7d32009-07-09 15:13:56 +01001733 host->hw_designer = amba_manf(dev);
1734 host->hw_revision = amba_rev(dev);
Linus Walleij64de0282010-02-19 01:09:10 +01001735 dev_dbg(mmc_dev(mmc), "designer ID = 0x%02x\n", host->hw_designer);
1736 dev_dbg(mmc_dev(mmc), "revision = 0x%01x\n", host->hw_revision);
Russell King012b7d32009-07-09 15:13:56 +01001737
Ulf Hansson665ba562013-05-13 15:39:17 +01001738 host->clk = devm_clk_get(&dev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 if (IS_ERR(host->clk)) {
1740 ret = PTR_ERR(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 goto host_free;
1742 }
1743
Julia Lawallac940932012-08-26 16:00:59 +00001744 ret = clk_prepare_enable(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 if (ret)
Ulf Hansson665ba562013-05-13 15:39:17 +01001746 goto host_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
Srinivas Kandagatla9c34b732014-06-02 10:10:04 +01001748 if (variant->qcom_fifo)
1749 host->get_rx_fifocnt = mmci_qcom_get_rx_fifocnt;
1750 else
1751 host->get_rx_fifocnt = mmci_get_rx_fifocnt;
1752
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 host->plat = plat;
Rabin Vincent4956e102010-07-21 12:54:40 +01001754 host->variant = variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 host->mclk = clk_get_rate(host->clk);
Linus Walleijc8df9a52008-04-29 09:34:07 +01001756 /*
1757 * According to the spec, mclk is max 100 MHz,
1758 * so we try to adjust the clock down to this,
1759 * (if possible).
1760 */
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +01001761 if (host->mclk > variant->f_max) {
1762 ret = clk_set_rate(host->clk, variant->f_max);
Linus Walleijc8df9a52008-04-29 09:34:07 +01001763 if (ret < 0)
1764 goto clk_disable;
1765 host->mclk = clk_get_rate(host->clk);
Linus Walleij64de0282010-02-19 01:09:10 +01001766 dev_dbg(mmc_dev(mmc), "eventual mclk rate: %u Hz\n",
1767 host->mclk);
Linus Walleijc8df9a52008-04-29 09:34:07 +01001768 }
Ulf Hanssonef289982014-03-17 13:56:32 +01001769
Russell Kingc8ebae32011-01-11 19:35:53 +00001770 host->phybase = dev->res.start;
Ulf Hanssonef289982014-03-17 13:56:32 +01001771 host->base = devm_ioremap_resource(&dev->dev, &dev->res);
1772 if (IS_ERR(host->base)) {
1773 ret = PTR_ERR(host->base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 goto clk_disable;
1775 }
1776
Ulf Hanssoned9067f2018-07-13 13:15:23 +02001777 if (variant->init)
1778 variant->init(host);
1779
Linus Walleij7f294e42011-07-08 09:57:15 +01001780 /*
1781 * The ARM and ST versions of the block have slightly different
1782 * clock divider equations which means that the minimum divider
1783 * differs too.
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001784 * on Qualcomm like controllers get the nearest minimum clock to 100Khz
Linus Walleij7f294e42011-07-08 09:57:15 +01001785 */
1786 if (variant->st_clkdiv)
1787 mmc->f_min = DIV_ROUND_UP(host->mclk, 257);
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001788 else if (variant->explicit_mclk_control)
1789 mmc->f_min = clk_round_rate(host->clk, 100000);
Linus Walleij7f294e42011-07-08 09:57:15 +01001790 else
1791 mmc->f_min = DIV_ROUND_UP(host->mclk, 512);
Linus Walleij808d97c2010-04-08 07:39:38 +01001792 /*
Ulf Hansson78f87df2014-03-17 15:53:07 +01001793 * If no maximum operating frequency is supplied, fall back to use
1794 * the module parameter, which has a (low) default value in case it
1795 * is not specified. Either value must not exceed the clock rate into
Ulf Hansson5080a082014-03-21 10:46:39 +01001796 * the block, of course.
Linus Walleij808d97c2010-04-08 07:39:38 +01001797 */
Ulf Hansson78f87df2014-03-17 15:53:07 +01001798 if (mmc->f_max)
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001799 mmc->f_max = variant->explicit_mclk_control ?
1800 min(variant->f_max, mmc->f_max) :
1801 min(host->mclk, mmc->f_max);
Linus Walleij808d97c2010-04-08 07:39:38 +01001802 else
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001803 mmc->f_max = variant->explicit_mclk_control ?
1804 fmax : min(host->mclk, fmax);
1805
1806
Linus Walleij64de0282010-02-19 01:09:10 +01001807 dev_dbg(mmc_dev(mmc), "clocking block at %u Hz\n", mmc->f_max);
1808
Ulf Hansson599c1d52013-01-07 16:22:50 +01001809 /* Get regulators and the supported OCR mask */
Bjorn Andersson9369c972015-03-24 18:39:49 -07001810 ret = mmc_regulator_get_supply(mmc);
Wolfram Sang51006952017-10-14 21:17:14 +02001811 if (ret)
Bjorn Andersson9369c972015-03-24 18:39:49 -07001812 goto clk_disable;
1813
Ulf Hansson599c1d52013-01-07 16:22:50 +01001814 if (!mmc->ocr_avail)
Linus Walleij34e84f32009-09-22 14:41:40 +01001815 mmc->ocr_avail = plat->ocr_mask;
Ulf Hansson599c1d52013-01-07 16:22:50 +01001816 else if (plat->ocr_mask)
1817 dev_warn(mmc_dev(mmc), "Platform OCR mask is ignored\n");
1818
Ulf Hansson9dd8a8b2014-03-19 13:54:18 +01001819 /* We support these capabilities. */
1820 mmc->caps |= MMC_CAP_CMD23;
1821
Linus Walleij49adc0c2016-10-25 11:06:06 +02001822 /*
1823 * Enable busy detection.
1824 */
Ulf Hansson8d94b542014-01-13 16:49:31 +01001825 if (variant->busy_detect) {
1826 mmci_ops.card_busy = mmci_card_busy;
Linus Walleij49adc0c2016-10-25 11:06:06 +02001827 /*
1828 * Not all variants have a flag to enable busy detection
1829 * in the DPSM, but if they do, set it here.
1830 */
1831 if (variant->busy_dpsm_flag)
1832 mmci_write_datactrlreg(host,
1833 host->variant->busy_dpsm_flag);
Ulf Hansson8d94b542014-01-13 16:49:31 +01001834 mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY;
1835 mmc->max_busy_timeout = 0;
1836 }
1837
1838 mmc->ops = &mmci_ops;
1839
Ulf Hansson70be2082013-01-07 15:35:06 +01001840 /* We support these PM capabilities. */
Ulf Hansson78f87df2014-03-17 15:53:07 +01001841 mmc->pm_caps |= MMC_PM_KEEP_POWER;
Ulf Hansson70be2082013-01-07 15:35:06 +01001842
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 /*
1844 * We can do SGIO
1845 */
Martin K. Petersena36274e2010-09-10 01:33:59 -04001846 mmc->max_segs = NR_SG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
1848 /*
Rabin Vincent08458ef2010-07-21 12:55:59 +01001849 * Since only a certain number of bits are valid in the data length
1850 * register, we must ensure that we don't exceed 2^num-1 bytes in a
1851 * single request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 */
Rabin Vincent08458ef2010-07-21 12:55:59 +01001853 mmc->max_req_size = (1 << variant->datalength_bits) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
1855 /*
1856 * Set the maximum segment size. Since we aren't doing DMA
1857 * (yet) we are only limited by the data length register.
1858 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001859 mmc->max_seg_size = mmc->max_req_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001861 /*
1862 * Block size can be up to 2048 bytes, but must be a power of two.
1863 */
Will Deacon8f7f6b7e2012-02-24 11:25:21 +00001864 mmc->max_blk_size = 1 << 11;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001865
Pierre Ossman55db8902006-11-21 17:55:45 +01001866 /*
Will Deacon8f7f6b7e2012-02-24 11:25:21 +00001867 * Limit the number of blocks transferred so that we don't overflow
1868 * the maximum request size.
Pierre Ossman55db8902006-11-21 17:55:45 +01001869 */
Will Deacon8f7f6b7e2012-02-24 11:25:21 +00001870 mmc->max_blk_count = mmc->max_req_size >> 11;
Pierre Ossman55db8902006-11-21 17:55:45 +01001871
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 spin_lock_init(&host->lock);
1873
1874 writel(0, host->base + MMCIMASK0);
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +01001875
1876 if (variant->mmcimask1)
1877 writel(0, host->base + MMCIMASK1);
1878
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 writel(0xfff, host->base + MMCICLEAR);
1880
Linus Walleijce437aa2014-08-27 15:13:54 +02001881 /*
1882 * If:
1883 * - not using DT but using a descriptor table, or
1884 * - using a table of descriptors ALONGSIDE DT, or
1885 * look up these descriptors named "cd" and "wp" right here, fail
Linus Walleij9ef986a2018-09-20 16:01:10 -07001886 * silently of these do not exist
Linus Walleijce437aa2014-08-27 15:13:54 +02001887 */
1888 if (!np) {
Linus Walleij89168b42014-10-02 09:08:46 +02001889 ret = mmc_gpiod_request_cd(mmc, "cd", 0, false, 0, NULL);
Linus Walleij9ef986a2018-09-20 16:01:10 -07001890 if (ret == -EPROBE_DEFER)
1891 goto clk_disable;
Linus Walleijce437aa2014-08-27 15:13:54 +02001892
Linus Walleij89168b42014-10-02 09:08:46 +02001893 ret = mmc_gpiod_request_ro(mmc, "wp", 0, false, 0, NULL);
Linus Walleij9ef986a2018-09-20 16:01:10 -07001894 if (ret == -EPROBE_DEFER)
1895 goto clk_disable;
Russell King89001442009-07-09 15:16:07 +01001896 }
1897
Ulf Hanssonef289982014-03-17 13:56:32 +01001898 ret = devm_request_irq(&dev->dev, dev->irq[0], mmci_irq, IRQF_SHARED,
1899 DRIVER_NAME " (cmd)", host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 if (ret)
Ulf Hanssonef289982014-03-17 13:56:32 +01001901 goto clk_disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Russell Kingdfb85182012-05-03 11:33:15 +01001903 if (!dev->irq[1])
Linus Walleij2686b4b2010-10-19 12:39:48 +01001904 host->singleirq = true;
1905 else {
Ulf Hanssonef289982014-03-17 13:56:32 +01001906 ret = devm_request_irq(&dev->dev, dev->irq[1], mmci_pio_irq,
1907 IRQF_SHARED, DRIVER_NAME " (pio)", host);
Linus Walleij2686b4b2010-10-19 12:39:48 +01001908 if (ret)
Ulf Hanssonef289982014-03-17 13:56:32 +01001909 goto clk_disable;
Linus Walleij2686b4b2010-10-19 12:39:48 +01001910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
Linus Walleij8cb28152011-01-24 15:22:13 +01001912 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
1914 amba_set_drvdata(dev, mmc);
1915
Russell Kingc8ebae32011-01-11 19:35:53 +00001916 dev_info(&dev->dev, "%s: PL%03x manf %x rev%u at 0x%08llx irq %d,%d (pio)\n",
1917 mmc_hostname(mmc), amba_part(dev), amba_manf(dev),
1918 amba_rev(dev), (unsigned long long)dev->res.start,
1919 dev->irq[0], dev->irq[1]);
1920
1921 mmci_dma_setup(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Ulf Hansson2cd976c2011-12-13 17:01:11 +01001923 pm_runtime_set_autosuspend_delay(&dev->dev, 50);
1924 pm_runtime_use_autosuspend(&dev->dev);
Russell King1c3be362011-08-14 09:17:05 +01001925
Russell King8c11a942010-12-28 19:40:40 +00001926 mmc_add_host(mmc);
1927
Ulf Hansson6f2d3c82014-12-11 14:35:55 +01001928 pm_runtime_put(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 return 0;
1930
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 clk_disable:
Julia Lawallac940932012-08-26 16:00:59 +00001932 clk_disable_unprepare(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 host_free:
1934 mmc_free_host(mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 return ret;
1936}
1937
Bill Pemberton6e0ee712012-11-19 13:26:03 -05001938static int mmci_remove(struct amba_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939{
1940 struct mmc_host *mmc = amba_get_drvdata(dev);
1941
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 if (mmc) {
1943 struct mmci_host *host = mmc_priv(mmc);
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +01001944 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Russell King1c3be362011-08-14 09:17:05 +01001946 /*
1947 * Undo pm_runtime_put() in probe. We use the _sync
1948 * version here so that we can access the primecell.
1949 */
1950 pm_runtime_get_sync(&dev->dev);
1951
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 mmc_remove_host(mmc);
1953
1954 writel(0, host->base + MMCIMASK0);
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +01001955
1956 if (variant->mmcimask1)
1957 writel(0, host->base + MMCIMASK1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
1959 writel(0, host->base + MMCICOMMAND);
1960 writel(0, host->base + MMCIDATACTRL);
1961
Russell Kingc8ebae32011-01-11 19:35:53 +00001962 mmci_dma_release(host);
Julia Lawallac940932012-08-26 16:00:59 +00001963 clk_disable_unprepare(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 mmc_free_host(mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 }
1966
1967 return 0;
1968}
1969
Ulf Hansson571dce42014-01-23 00:38:00 +01001970#ifdef CONFIG_PM
Ulf Hansson1ff44432013-09-04 09:05:17 +01001971static void mmci_save(struct mmci_host *host)
1972{
1973 unsigned long flags;
1974
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001975 spin_lock_irqsave(&host->lock, flags);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001976
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001977 writel(0, host->base + MMCIMASK0);
1978 if (host->variant->pwrreg_nopower) {
Ulf Hansson1ff44432013-09-04 09:05:17 +01001979 writel(0, host->base + MMCIDATACTRL);
1980 writel(0, host->base + MMCIPOWER);
1981 writel(0, host->base + MMCICLOCK);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001982 }
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001983 mmci_reg_delay(host);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001984
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001985 spin_unlock_irqrestore(&host->lock, flags);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001986}
1987
1988static void mmci_restore(struct mmci_host *host)
1989{
1990 unsigned long flags;
1991
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001992 spin_lock_irqsave(&host->lock, flags);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001993
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001994 if (host->variant->pwrreg_nopower) {
Ulf Hansson1ff44432013-09-04 09:05:17 +01001995 writel(host->clk_reg, host->base + MMCICLOCK);
1996 writel(host->datactrl_reg, host->base + MMCIDATACTRL);
1997 writel(host->pwr_reg, host->base + MMCIPOWER);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001998 }
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001999 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
2000 mmci_reg_delay(host);
2001
2002 spin_unlock_irqrestore(&host->lock, flags);
Ulf Hansson1ff44432013-09-04 09:05:17 +01002003}
2004
Ulf Hansson82592932013-01-09 11:15:26 +01002005static int mmci_runtime_suspend(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);
Ulf Hanssone36bd9c62013-09-04 09:00:37 +01002012 pinctrl_pm_select_sleep_state(dev);
Ulf Hansson1ff44432013-09-04 09:05:17 +01002013 mmci_save(host);
Ulf Hansson82592932013-01-09 11:15:26 +01002014 clk_disable_unprepare(host->clk);
2015 }
2016
2017 return 0;
2018}
2019
2020static int mmci_runtime_resume(struct device *dev)
2021{
2022 struct amba_device *adev = to_amba_device(dev);
2023 struct mmc_host *mmc = amba_get_drvdata(adev);
2024
2025 if (mmc) {
2026 struct mmci_host *host = mmc_priv(mmc);
2027 clk_prepare_enable(host->clk);
Ulf Hansson1ff44432013-09-04 09:05:17 +01002028 mmci_restore(host);
Ulf Hanssone36bd9c62013-09-04 09:00:37 +01002029 pinctrl_pm_select_default_state(dev);
Ulf Hansson82592932013-01-09 11:15:26 +01002030 }
2031
2032 return 0;
2033}
2034#endif
2035
Ulf Hansson48fa7002011-12-13 16:59:34 +01002036static const struct dev_pm_ops mmci_dev_pm_ops = {
Ulf Hanssonf3737fa2014-01-23 01:11:33 +01002037 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
2038 pm_runtime_force_resume)
Rafael J. Wysocki6ed23b82014-12-04 00:34:11 +01002039 SET_RUNTIME_PM_OPS(mmci_runtime_suspend, mmci_runtime_resume, NULL)
Ulf Hansson48fa7002011-12-13 16:59:34 +01002040};
2041
Arvind Yadav88411de2017-08-23 22:00:49 +05302042static const struct amba_id mmci_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 {
2044 .id = 0x00041180,
Pawel Moll768fbc12011-03-11 17:18:07 +00002045 .mask = 0xff0fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01002046 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 },
2048 {
Pawel Moll768fbc12011-03-11 17:18:07 +00002049 .id = 0x01041180,
2050 .mask = 0xff0fffff,
2051 .data = &variant_arm_extended_fifo,
2052 },
2053 {
Pawel Moll3a372982013-01-24 14:12:45 +01002054 .id = 0x02041180,
2055 .mask = 0xff0fffff,
2056 .data = &variant_arm_extended_fifo_hwfc,
2057 },
2058 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 .id = 0x00041181,
2060 .mask = 0x000fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01002061 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 },
Linus Walleijcc30d602009-01-04 15:18:54 +01002063 /* ST Micro variants */
2064 {
2065 .id = 0x00180180,
2066 .mask = 0x00ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01002067 .data = &variant_u300,
Linus Walleijcc30d602009-01-04 15:18:54 +01002068 },
2069 {
Linus Walleij34fd4212012-04-10 17:43:59 +01002070 .id = 0x10180180,
2071 .mask = 0xf0ffffff,
2072 .data = &variant_nomadik,
2073 },
2074 {
Linus Walleijcc30d602009-01-04 15:18:54 +01002075 .id = 0x00280180,
2076 .mask = 0x00ffffff,
Linus Walleij0bcb7ef2016-01-04 02:21:55 +01002077 .data = &variant_nomadik,
Rabin Vincent4956e102010-07-21 12:54:40 +01002078 },
2079 {
2080 .id = 0x00480180,
Philippe Langlais1784b152011-03-25 08:51:52 +01002081 .mask = 0xf0ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01002082 .data = &variant_ux500,
Linus Walleijcc30d602009-01-04 15:18:54 +01002083 },
Philippe Langlais1784b152011-03-25 08:51:52 +01002084 {
2085 .id = 0x10480180,
2086 .mask = 0xf0ffffff,
2087 .data = &variant_ux500v2,
2088 },
Patrice Chotard2a9d6c82018-01-18 15:34:21 +01002089 {
2090 .id = 0x00880180,
2091 .mask = 0x00ffffff,
2092 .data = &variant_stm32,
2093 },
Srinivas Kandagatla55b604a2014-06-02 10:10:13 +01002094 /* Qualcomm variants */
2095 {
2096 .id = 0x00051180,
2097 .mask = 0x000fffff,
2098 .data = &variant_qcom,
2099 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 { 0, 0 },
2101};
2102
Dave Martin9f998352011-10-05 15:15:21 +01002103MODULE_DEVICE_TABLE(amba, mmci_ids);
2104
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105static struct amba_driver mmci_driver = {
2106 .drv = {
2107 .name = DRIVER_NAME,
Ulf Hansson48fa7002011-12-13 16:59:34 +01002108 .pm = &mmci_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 },
2110 .probe = mmci_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05002111 .remove = mmci_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 .id_table = mmci_ids,
2113};
2114
viresh kumar9e5ed092012-03-15 10:40:38 +01002115module_amba_driver(mmci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117module_param(fmax, uint, 0444);
2118
2119MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
2120MODULE_LICENSE("GPL");