blob: 70b0df8b9c7865a5dbb4d466d9c856215bbc6984 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Pierre Ossman70f10482007-07-11 20:04:50 +02002 * linux/drivers/mmc/host/mmci.c - ARM PrimeCell MMCI PL180/1 driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 2003 Deep Blue Solutions, Ltd, All Rights Reserved.
Russell Kingc8ebae32011-01-11 19:35:53 +00005 * Copyright (C) 2010 ST-Ericsson SA
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
12#include <linux/moduleparam.h>
13#include <linux/init.h>
14#include <linux/ioport.h>
15#include <linux/device.h>
Ulf Hanssonef289982014-03-17 13:56:32 +010016#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/interrupt.h>
Russell King613b1522011-01-30 21:06:53 +000018#include <linux/kernel.h>
Lee Jones000bc9d2012-04-16 10:18:43 +010019#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/delay.h>
21#include <linux/err.h>
22#include <linux/highmem.h>
Nicolas Pitre019a5f52007-10-11 01:06:03 -040023#include <linux/log2.h>
Ulf Hansson70be2082013-01-07 15:35:06 +010024#include <linux/mmc/pm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/mmc/host.h>
Linus Walleij34177802010-10-19 12:43:58 +010026#include <linux/mmc/card.h>
Ulf Hanssond2762092014-03-17 13:56:19 +010027#include <linux/mmc/slot-gpio.h>
Russell Kinga62c80e2006-01-07 13:52:45 +000028#include <linux/amba/bus.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000029#include <linux/clk.h>
Jens Axboebd6dee62007-10-24 09:01:09 +020030#include <linux/scatterlist.h>
Russell King89001442009-07-09 15:16:07 +010031#include <linux/gpio.h>
Lee Jones9a597012012-04-12 16:51:13 +010032#include <linux/of_gpio.h>
Linus Walleij34e84f32009-09-22 14:41:40 +010033#include <linux/regulator/consumer.h>
Russell Kingc8ebae32011-01-11 19:35:53 +000034#include <linux/dmaengine.h>
35#include <linux/dma-mapping.h>
36#include <linux/amba/mmci.h>
Russell King1c3be362011-08-14 09:17:05 +010037#include <linux/pm_runtime.h>
Viresh Kumar258aea72012-02-01 16:12:19 +053038#include <linux/types.h>
Linus Walleija9a83782012-10-29 14:39:30 +010039#include <linux/pinctrl/consumer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Russell King7b09cda2005-07-01 12:02:59 +010041#include <asm/div64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#include "mmci.h"
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +010045#include "mmci_qcom_dml.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#define DRIVER_NAME "mmci-pl18x"
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049static unsigned int fmax = 515633;
50
Rabin Vincent4956e102010-07-21 12:54:40 +010051/**
52 * struct variant_data - MMCI variant-specific quirks
53 * @clkreg: default value for MCICLOCK register
Rabin Vincent4380c142010-07-21 12:55:18 +010054 * @clkreg_enable: enable value for MMCICLOCK register
Srinivas Kandagatlae1412d82014-06-02 10:09:23 +010055 * @clkreg_8bit_bus_enable: enable value for 8 bit bus
Srinivas Kandagatlae8740642014-06-02 10:09:30 +010056 * @clkreg_neg_edge_enable: enable value for inverted data/cmd output
Rabin Vincent08458ef2010-07-21 12:55:59 +010057 * @datalength_bits: number of bits in the MMCIDATALENGTH register
Rabin Vincent8301bb62010-08-09 12:57:30 +010058 * @fifosize: number of bytes that can be written when MMCI_TXFIFOEMPTY
59 * is asserted (likewise for RX)
60 * @fifohalfsize: number of bytes that can be written when MCI_TXFIFOHALFEMPTY
61 * is asserted (likewise for RX)
Srinivas Kandagatlaae7b0062014-06-02 10:09:39 +010062 * @data_cmd_enable: enable value for data commands.
Srinivas Kandagatlac7354132014-08-22 05:55:16 +010063 * @st_sdio: enable ST specific SDIO logic
Linus Walleijb70a67f2010-12-06 09:24:14 +010064 * @st_clkdiv: true if using a ST-specific clock divider algorithm
Srinivas Kandagatlae17dca22014-06-02 10:09:15 +010065 * @datactrl_mask_ddrmode: ddr mode mask in datactrl register.
Philippe Langlais1784b152011-03-25 08:51:52 +010066 * @blksz_datactrl16: true if Block size is at b16..b30 position in datactrl register
Srinivas Kandagatlaff783232014-06-02 10:09:06 +010067 * @blksz_datactrl4: true if Block size is at b4..b16 position in datactrl
68 * register
Srinivas Kandagatla5df014d2014-08-22 05:54:55 +010069 * @datactrl_mask_sdio: SDIO enable mask in datactrl register
Ulf Hansson7d72a1d2011-12-13 16:54:55 +010070 * @pwrreg_powerup: power up value for MMCIPOWER register
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +010071 * @f_max: maximum clk frequency supported by the controller.
Ulf Hansson4d1a3a02011-12-13 16:57:07 +010072 * @signal_direction: input/out direction of bus signals can be indicated
Ulf Hanssonf4670da2013-01-09 17:19:54 +010073 * @pwrreg_clkgate: MMCIPOWER register must be used to gate the clock
Linus Walleij49adc0c2016-10-25 11:06:06 +020074 * @busy_detect: true if the variant supports busy detection on DAT0.
75 * @busy_dpsm_flag: bitmask enabling busy detection in the DPSM
76 * @busy_detect_flag: bitmask identifying the bit in the MMCISTATUS register
77 * indicating that the card is busy
78 * @busy_detect_mask: bitmask identifying the bit in the MMCIMASK0 to mask for
79 * getting busy end detection interrupts
Ulf Hansson1ff44432013-09-04 09:05:17 +010080 * @pwrreg_nopower: bits in MMCIPOWER don't controls ext. power supply
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +010081 * @explicit_mclk_control: enable explicit mclk control in driver.
Srinivas Kandagatla9c34b732014-06-02 10:10:04 +010082 * @qcom_fifo: enables qcom specific fifo pio read logic.
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +010083 * @qcom_dml: enables qcom specific dma glue for dma transfers.
Ulf Hansson78782892014-06-13 13:21:38 +020084 * @reversed_irq_handling: handle data irq before cmd irq.
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +010085 * @mmcimask1: true if variant have a MMCIMASK1 register.
Patrice Chotard7f7b5502018-01-18 15:34:18 +010086 * @start_err: bitmask identifying the STARTBITERR bit inside MMCISTATUS
87 * register.
Patrice Chotard11dfb972018-01-18 15:34:19 +010088 * @opendrain: bitmask identifying the OPENDRAIN bit inside MMCIPOWER register
Rabin Vincent4956e102010-07-21 12:54:40 +010089 */
90struct variant_data {
91 unsigned int clkreg;
Rabin Vincent4380c142010-07-21 12:55:18 +010092 unsigned int clkreg_enable;
Srinivas Kandagatlae1412d82014-06-02 10:09:23 +010093 unsigned int clkreg_8bit_bus_enable;
Srinivas Kandagatlae8740642014-06-02 10:09:30 +010094 unsigned int clkreg_neg_edge_enable;
Rabin Vincent08458ef2010-07-21 12:55:59 +010095 unsigned int datalength_bits;
Rabin Vincent8301bb62010-08-09 12:57:30 +010096 unsigned int fifosize;
97 unsigned int fifohalfsize;
Srinivas Kandagatlaae7b0062014-06-02 10:09:39 +010098 unsigned int data_cmd_enable;
Srinivas Kandagatlae17dca22014-06-02 10:09:15 +010099 unsigned int datactrl_mask_ddrmode;
Srinivas Kandagatla5df014d2014-08-22 05:54:55 +0100100 unsigned int datactrl_mask_sdio;
Srinivas Kandagatlac7354132014-08-22 05:55:16 +0100101 bool st_sdio;
Linus Walleijb70a67f2010-12-06 09:24:14 +0100102 bool st_clkdiv;
Philippe Langlais1784b152011-03-25 08:51:52 +0100103 bool blksz_datactrl16;
Srinivas Kandagatlaff783232014-06-02 10:09:06 +0100104 bool blksz_datactrl4;
Ulf Hansson7d72a1d2011-12-13 16:54:55 +0100105 u32 pwrreg_powerup;
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +0100106 u32 f_max;
Ulf Hansson4d1a3a02011-12-13 16:57:07 +0100107 bool signal_direction;
Ulf Hanssonf4670da2013-01-09 17:19:54 +0100108 bool pwrreg_clkgate;
Ulf Hansson01259622013-05-15 20:53:22 +0100109 bool busy_detect;
Linus Walleij49adc0c2016-10-25 11:06:06 +0200110 u32 busy_dpsm_flag;
111 u32 busy_detect_flag;
112 u32 busy_detect_mask;
Ulf Hansson1ff44432013-09-04 09:05:17 +0100113 bool pwrreg_nopower;
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +0100114 bool explicit_mclk_control;
Srinivas Kandagatla9c34b732014-06-02 10:10:04 +0100115 bool qcom_fifo;
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100116 bool qcom_dml;
Ulf Hansson78782892014-06-13 13:21:38 +0200117 bool reversed_irq_handling;
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100118 bool mmcimask1;
Patrice Chotard7f7b5502018-01-18 15:34:18 +0100119 u32 start_err;
Patrice Chotard11dfb972018-01-18 15:34:19 +0100120 u32 opendrain;
Rabin Vincent4956e102010-07-21 12:54:40 +0100121};
122
123static struct variant_data variant_arm = {
Rabin Vincent8301bb62010-08-09 12:57:30 +0100124 .fifosize = 16 * 4,
125 .fifohalfsize = 8 * 4,
Rabin Vincent08458ef2010-07-21 12:55:59 +0100126 .datalength_bits = 16,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +0100127 .pwrreg_powerup = MCI_PWR_UP,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +0100128 .f_max = 100000000,
Ulf Hansson78782892014-06-13 13:21:38 +0200129 .reversed_irq_handling = true,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100130 .mmcimask1 = true,
Patrice Chotard7f7b5502018-01-18 15:34:18 +0100131 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +0100132 .opendrain = MCI_ROD,
Rabin Vincent4956e102010-07-21 12:54:40 +0100133};
134
Pawel Moll768fbc12011-03-11 17:18:07 +0000135static struct variant_data variant_arm_extended_fifo = {
136 .fifosize = 128 * 4,
137 .fifohalfsize = 64 * 4,
138 .datalength_bits = 16,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +0100139 .pwrreg_powerup = MCI_PWR_UP,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +0100140 .f_max = 100000000,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100141 .mmcimask1 = true,
Patrice Chotard7f7b5502018-01-18 15:34:18 +0100142 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +0100143 .opendrain = MCI_ROD,
Pawel Moll768fbc12011-03-11 17:18:07 +0000144};
145
Pawel Moll3a372982013-01-24 14:12:45 +0100146static struct variant_data variant_arm_extended_fifo_hwfc = {
147 .fifosize = 128 * 4,
148 .fifohalfsize = 64 * 4,
149 .clkreg_enable = MCI_ARM_HWFCEN,
150 .datalength_bits = 16,
151 .pwrreg_powerup = MCI_PWR_UP,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +0100152 .f_max = 100000000,
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_ROD,
Pawel Moll3a372982013-01-24 14:12:45 +0100156};
157
Rabin Vincent4956e102010-07-21 12:54:40 +0100158static struct variant_data variant_u300 = {
Rabin Vincent8301bb62010-08-09 12:57:30 +0100159 .fifosize = 16 * 4,
160 .fifohalfsize = 8 * 4,
Linus Walleij49ac2152011-03-04 14:54:16 +0100161 .clkreg_enable = MCI_ST_U300_HWFCEN,
Srinivas Kandagatlae1412d82014-06-02 10:09:23 +0100162 .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
Rabin Vincent08458ef2010-07-21 12:55:59 +0100163 .datalength_bits = 16,
Linus Walleij5db3eee2016-10-25 11:06:05 +0200164 .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
Srinivas Kandagatlac7354132014-08-22 05:55:16 +0100165 .st_sdio = true,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +0100166 .pwrreg_powerup = MCI_PWR_ON,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +0100167 .f_max = 100000000,
Ulf Hansson4d1a3a02011-12-13 16:57:07 +0100168 .signal_direction = true,
Ulf Hanssonf4670da2013-01-09 17:19:54 +0100169 .pwrreg_clkgate = true,
Ulf Hansson1ff44432013-09-04 09:05:17 +0100170 .pwrreg_nopower = true,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100171 .mmcimask1 = true,
Patrice Chotard7f7b5502018-01-18 15:34:18 +0100172 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +0100173 .opendrain = MCI_OD,
Rabin Vincent4956e102010-07-21 12:54:40 +0100174};
175
Linus Walleij34fd4212012-04-10 17:43:59 +0100176static struct variant_data variant_nomadik = {
177 .fifosize = 16 * 4,
178 .fifohalfsize = 8 * 4,
179 .clkreg = MCI_CLK_ENABLE,
Linus Walleijf5abc762016-01-04 02:22:08 +0100180 .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
Linus Walleij34fd4212012-04-10 17:43:59 +0100181 .datalength_bits = 24,
Linus Walleij5db3eee2016-10-25 11:06:05 +0200182 .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
Srinivas Kandagatlac7354132014-08-22 05:55:16 +0100183 .st_sdio = true,
Linus Walleij34fd4212012-04-10 17:43:59 +0100184 .st_clkdiv = true,
185 .pwrreg_powerup = MCI_PWR_ON,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +0100186 .f_max = 100000000,
Linus Walleij34fd4212012-04-10 17:43:59 +0100187 .signal_direction = true,
Ulf Hanssonf4670da2013-01-09 17:19:54 +0100188 .pwrreg_clkgate = true,
Ulf Hansson1ff44432013-09-04 09:05:17 +0100189 .pwrreg_nopower = true,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100190 .mmcimask1 = true,
Patrice Chotard7f7b5502018-01-18 15:34:18 +0100191 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +0100192 .opendrain = MCI_OD,
Linus Walleij34fd4212012-04-10 17:43:59 +0100193};
194
Rabin Vincent4956e102010-07-21 12:54:40 +0100195static struct variant_data variant_ux500 = {
Rabin Vincent8301bb62010-08-09 12:57:30 +0100196 .fifosize = 30 * 4,
197 .fifohalfsize = 8 * 4,
Rabin Vincent4956e102010-07-21 12:54:40 +0100198 .clkreg = MCI_CLK_ENABLE,
Linus Walleij49ac2152011-03-04 14:54:16 +0100199 .clkreg_enable = MCI_ST_UX500_HWFCEN,
Srinivas Kandagatlae1412d82014-06-02 10:09:23 +0100200 .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
Srinivas Kandagatlae8740642014-06-02 10:09:30 +0100201 .clkreg_neg_edge_enable = MCI_ST_UX500_NEG_EDGE,
Rabin Vincent08458ef2010-07-21 12:55:59 +0100202 .datalength_bits = 24,
Linus Walleij5db3eee2016-10-25 11:06:05 +0200203 .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
Srinivas Kandagatlac7354132014-08-22 05:55:16 +0100204 .st_sdio = true,
Linus Walleijb70a67f2010-12-06 09:24:14 +0100205 .st_clkdiv = true,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +0100206 .pwrreg_powerup = MCI_PWR_ON,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +0100207 .f_max = 100000000,
Ulf Hansson4d1a3a02011-12-13 16:57:07 +0100208 .signal_direction = true,
Ulf Hanssonf4670da2013-01-09 17:19:54 +0100209 .pwrreg_clkgate = true,
Ulf Hansson01259622013-05-15 20:53:22 +0100210 .busy_detect = true,
Linus Walleij49adc0c2016-10-25 11:06:06 +0200211 .busy_dpsm_flag = MCI_DPSM_ST_BUSYMODE,
212 .busy_detect_flag = MCI_ST_CARDBUSY,
213 .busy_detect_mask = MCI_ST_BUSYENDMASK,
Ulf Hansson1ff44432013-09-04 09:05:17 +0100214 .pwrreg_nopower = true,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100215 .mmcimask1 = true,
Patrice Chotard7f7b5502018-01-18 15:34:18 +0100216 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +0100217 .opendrain = MCI_OD,
Rabin Vincent4956e102010-07-21 12:54:40 +0100218};
Linus Walleijb70a67f2010-12-06 09:24:14 +0100219
Philippe Langlais1784b152011-03-25 08:51:52 +0100220static struct variant_data variant_ux500v2 = {
221 .fifosize = 30 * 4,
222 .fifohalfsize = 8 * 4,
223 .clkreg = MCI_CLK_ENABLE,
224 .clkreg_enable = MCI_ST_UX500_HWFCEN,
Srinivas Kandagatlae1412d82014-06-02 10:09:23 +0100225 .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
Srinivas Kandagatlae8740642014-06-02 10:09:30 +0100226 .clkreg_neg_edge_enable = MCI_ST_UX500_NEG_EDGE,
Linus Walleij5db3eee2016-10-25 11:06:05 +0200227 .datactrl_mask_ddrmode = MCI_DPSM_ST_DDRMODE,
Philippe Langlais1784b152011-03-25 08:51:52 +0100228 .datalength_bits = 24,
Linus Walleij5db3eee2016-10-25 11:06:05 +0200229 .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
Srinivas Kandagatlac7354132014-08-22 05:55:16 +0100230 .st_sdio = true,
Philippe Langlais1784b152011-03-25 08:51:52 +0100231 .st_clkdiv = true,
232 .blksz_datactrl16 = true,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +0100233 .pwrreg_powerup = MCI_PWR_ON,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +0100234 .f_max = 100000000,
Ulf Hansson4d1a3a02011-12-13 16:57:07 +0100235 .signal_direction = true,
Ulf Hanssonf4670da2013-01-09 17:19:54 +0100236 .pwrreg_clkgate = true,
Ulf Hansson01259622013-05-15 20:53:22 +0100237 .busy_detect = true,
Linus Walleij49adc0c2016-10-25 11:06:06 +0200238 .busy_dpsm_flag = MCI_DPSM_ST_BUSYMODE,
239 .busy_detect_flag = MCI_ST_CARDBUSY,
240 .busy_detect_mask = MCI_ST_BUSYENDMASK,
Ulf Hansson1ff44432013-09-04 09:05:17 +0100241 .pwrreg_nopower = true,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100242 .mmcimask1 = true,
Patrice Chotard7f7b5502018-01-18 15:34:18 +0100243 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +0100244 .opendrain = MCI_OD,
Philippe Langlais1784b152011-03-25 08:51:52 +0100245};
246
Patrice Chotard2a9d6c82018-01-18 15:34:21 +0100247static struct variant_data variant_stm32 = {
248 .fifosize = 32 * 4,
249 .fifohalfsize = 8 * 4,
250 .clkreg = MCI_CLK_ENABLE,
251 .clkreg_enable = MCI_ST_UX500_HWFCEN,
252 .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
253 .clkreg_neg_edge_enable = MCI_ST_UX500_NEG_EDGE,
254 .datalength_bits = 24,
255 .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
256 .st_sdio = true,
257 .st_clkdiv = true,
258 .pwrreg_powerup = MCI_PWR_ON,
259 .f_max = 48000000,
260 .pwrreg_clkgate = true,
261 .pwrreg_nopower = true,
262};
263
Srinivas Kandagatla55b604a2014-06-02 10:10:13 +0100264static struct variant_data variant_qcom = {
265 .fifosize = 16 * 4,
266 .fifohalfsize = 8 * 4,
267 .clkreg = MCI_CLK_ENABLE,
268 .clkreg_enable = MCI_QCOM_CLK_FLOWENA |
269 MCI_QCOM_CLK_SELECT_IN_FBCLK,
270 .clkreg_8bit_bus_enable = MCI_QCOM_CLK_WIDEBUS_8,
271 .datactrl_mask_ddrmode = MCI_QCOM_CLK_SELECT_IN_DDR_MODE,
Linus Walleij5db3eee2016-10-25 11:06:05 +0200272 .data_cmd_enable = MCI_CPSM_QCOM_DATCMD,
Srinivas Kandagatla55b604a2014-06-02 10:10:13 +0100273 .blksz_datactrl4 = true,
274 .datalength_bits = 24,
275 .pwrreg_powerup = MCI_PWR_UP,
276 .f_max = 208000000,
277 .explicit_mclk_control = true,
278 .qcom_fifo = true,
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100279 .qcom_dml = true,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100280 .mmcimask1 = true,
Patrice Chotard7f7b5502018-01-18 15:34:18 +0100281 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +0100282 .opendrain = MCI_ROD,
Srinivas Kandagatla55b604a2014-06-02 10:10:13 +0100283};
284
Linus Walleij49adc0c2016-10-25 11:06:06 +0200285/* Busy detection for the ST Micro variant */
Ulf Hansson01259622013-05-15 20:53:22 +0100286static int mmci_card_busy(struct mmc_host *mmc)
287{
288 struct mmci_host *host = mmc_priv(mmc);
289 unsigned long flags;
290 int busy = 0;
291
Ulf Hansson01259622013-05-15 20:53:22 +0100292 spin_lock_irqsave(&host->lock, flags);
Linus Walleij49adc0c2016-10-25 11:06:06 +0200293 if (readl(host->base + MMCISTATUS) & host->variant->busy_detect_flag)
Ulf Hansson01259622013-05-15 20:53:22 +0100294 busy = 1;
295 spin_unlock_irqrestore(&host->lock, flags);
296
Ulf Hansson01259622013-05-15 20:53:22 +0100297 return busy;
298}
299
Linus Walleija6a64642009-09-14 12:56:14 +0100300/*
Ulf Hansson653a7612013-01-21 21:29:34 +0100301 * Validate mmc prerequisites
302 */
303static int mmci_validate_data(struct mmci_host *host,
304 struct mmc_data *data)
305{
306 if (!data)
307 return 0;
308
309 if (!is_power_of_2(data->blksz)) {
310 dev_err(mmc_dev(host->mmc),
311 "unsupported block size (%d bytes)\n", data->blksz);
312 return -EINVAL;
313 }
314
315 return 0;
316}
317
Ulf Hanssonf829c042013-09-04 09:01:15 +0100318static void mmci_reg_delay(struct mmci_host *host)
319{
320 /*
321 * According to the spec, at least three feedback clock cycles
322 * of max 52 MHz must pass between two writes to the MMCICLOCK reg.
323 * Three MCLK clock cycles must pass between two MMCIPOWER reg writes.
324 * Worst delay time during card init is at 100 kHz => 30 us.
325 * Worst delay time when up and running is at 25 MHz => 120 ns.
326 */
327 if (host->cclk < 25000000)
328 udelay(30);
329 else
330 ndelay(120);
331}
332
Ulf Hansson653a7612013-01-21 21:29:34 +0100333/*
Linus Walleija6a64642009-09-14 12:56:14 +0100334 * This must be called with host->lock held
335 */
Ulf Hansson7437cfa2012-01-18 09:17:27 +0100336static void mmci_write_clkreg(struct mmci_host *host, u32 clk)
337{
338 if (host->clk_reg != clk) {
339 host->clk_reg = clk;
340 writel(clk, host->base + MMCICLOCK);
341 }
342}
343
344/*
345 * This must be called with host->lock held
346 */
347static void mmci_write_pwrreg(struct mmci_host *host, u32 pwr)
348{
349 if (host->pwr_reg != pwr) {
350 host->pwr_reg = pwr;
351 writel(pwr, host->base + MMCIPOWER);
352 }
353}
354
355/*
356 * This must be called with host->lock held
357 */
Ulf Hansson9cc639a2013-05-15 20:48:23 +0100358static void mmci_write_datactrlreg(struct mmci_host *host, u32 datactrl)
359{
Linus Walleij49adc0c2016-10-25 11:06:06 +0200360 /* Keep busy mode in DPSM if enabled */
361 datactrl |= host->datactrl_reg & host->variant->busy_dpsm_flag;
Ulf Hansson01259622013-05-15 20:53:22 +0100362
Ulf Hansson9cc639a2013-05-15 20:48:23 +0100363 if (host->datactrl_reg != datactrl) {
364 host->datactrl_reg = datactrl;
365 writel(datactrl, host->base + MMCIDATACTRL);
366 }
367}
368
369/*
370 * This must be called with host->lock held
371 */
Linus Walleija6a64642009-09-14 12:56:14 +0100372static void mmci_set_clkreg(struct mmci_host *host, unsigned int desired)
373{
Rabin Vincent4956e102010-07-21 12:54:40 +0100374 struct variant_data *variant = host->variant;
375 u32 clk = variant->clkreg;
Linus Walleija6a64642009-09-14 12:56:14 +0100376
Ulf Hanssonc58a8502013-05-13 15:40:03 +0100377 /* Make sure cclk reflects the current calculated clock */
378 host->cclk = 0;
379
Linus Walleija6a64642009-09-14 12:56:14 +0100380 if (desired) {
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +0100381 if (variant->explicit_mclk_control) {
382 host->cclk = host->mclk;
383 } else if (desired >= host->mclk) {
Linus Walleij991a86e2010-12-10 09:35:53 +0100384 clk = MCI_CLK_BYPASS;
Linus Walleij399bc482011-04-01 07:59:17 +0100385 if (variant->st_clkdiv)
386 clk |= MCI_ST_UX500_NEG_EDGE;
Linus Walleija6a64642009-09-14 12:56:14 +0100387 host->cclk = host->mclk;
Linus Walleijb70a67f2010-12-06 09:24:14 +0100388 } else if (variant->st_clkdiv) {
389 /*
390 * DB8500 TRM says f = mclk / (clkdiv + 2)
391 * => clkdiv = (mclk / f) - 2
392 * Round the divider up so we don't exceed the max
393 * frequency
394 */
395 clk = DIV_ROUND_UP(host->mclk, desired) - 2;
396 if (clk >= 256)
397 clk = 255;
398 host->cclk = host->mclk / (clk + 2);
Linus Walleija6a64642009-09-14 12:56:14 +0100399 } else {
Linus Walleijb70a67f2010-12-06 09:24:14 +0100400 /*
401 * PL180 TRM says f = mclk / (2 * (clkdiv + 1))
402 * => clkdiv = mclk / (2 * f) - 1
403 */
Linus Walleija6a64642009-09-14 12:56:14 +0100404 clk = host->mclk / (2 * desired) - 1;
405 if (clk >= 256)
406 clk = 255;
407 host->cclk = host->mclk / (2 * (clk + 1));
408 }
Rabin Vincent4380c142010-07-21 12:55:18 +0100409
410 clk |= variant->clkreg_enable;
Linus Walleija6a64642009-09-14 12:56:14 +0100411 clk |= MCI_CLK_ENABLE;
412 /* This hasn't proven to be worthwhile */
413 /* clk |= MCI_CLK_PWRSAVE; */
414 }
415
Ulf Hanssonc58a8502013-05-13 15:40:03 +0100416 /* Set actual clock for debug */
417 host->mmc->actual_clock = host->cclk;
418
Linus Walleij9e6c82c2009-09-14 12:57:11 +0100419 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4)
Linus Walleij771dc152010-04-08 07:38:52 +0100420 clk |= MCI_4BIT_BUS;
421 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_8)
Srinivas Kandagatlae1412d82014-06-02 10:09:23 +0100422 clk |= variant->clkreg_8bit_bus_enable;
Linus Walleij9e6c82c2009-09-14 12:57:11 +0100423
Seungwon Jeon6dad6c92014-03-14 21:12:13 +0900424 if (host->mmc->ios.timing == MMC_TIMING_UHS_DDR50 ||
425 host->mmc->ios.timing == MMC_TIMING_MMC_DDR52)
Srinivas Kandagatlae8740642014-06-02 10:09:30 +0100426 clk |= variant->clkreg_neg_edge_enable;
Ulf Hansson6dbb6ee2013-01-07 15:30:44 +0100427
Ulf Hansson7437cfa2012-01-18 09:17:27 +0100428 mmci_write_clkreg(host, clk);
Linus Walleija6a64642009-09-14 12:56:14 +0100429}
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431static void
432mmci_request_end(struct mmci_host *host, struct mmc_request *mrq)
433{
434 writel(0, host->base + MMCICOMMAND);
435
Russell Kinge47c2222007-01-08 16:42:51 +0000436 BUG_ON(host->data);
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 host->mrq = NULL;
439 host->cmd = NULL;
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 mmc_request_done(host->mmc, mrq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
Linus Walleij2686b4b2010-10-19 12:39:48 +0100444static void mmci_set_mask1(struct mmci_host *host, unsigned int mask)
445{
446 void __iomem *base = host->base;
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100447 struct variant_data *variant = host->variant;
Linus Walleij2686b4b2010-10-19 12:39:48 +0100448
449 if (host->singleirq) {
450 unsigned int mask0 = readl(base + MMCIMASK0);
451
452 mask0 &= ~MCI_IRQ1MASK;
453 mask0 |= mask;
454
455 writel(mask0, base + MMCIMASK0);
456 }
457
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100458 if (variant->mmcimask1)
459 writel(mask, base + MMCIMASK1);
460
461 host->mask1_reg = mask;
Linus Walleij2686b4b2010-10-19 12:39:48 +0100462}
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464static void mmci_stop_data(struct mmci_host *host)
465{
Ulf Hansson9cc639a2013-05-15 20:48:23 +0100466 mmci_write_datactrlreg(host, 0);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100467 mmci_set_mask1(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 host->data = NULL;
469}
470
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100471static void mmci_init_sg(struct mmci_host *host, struct mmc_data *data)
472{
473 unsigned int flags = SG_MITER_ATOMIC;
474
475 if (data->flags & MMC_DATA_READ)
476 flags |= SG_MITER_TO_SG;
477 else
478 flags |= SG_MITER_FROM_SG;
479
480 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
481}
482
Russell Kingc8ebae32011-01-11 19:35:53 +0000483/*
484 * All the DMA operation mode stuff goes inside this ifdef.
485 * This assumes that you have a generic DMA device interface,
486 * no custom DMA interfaces are supported.
487 */
488#ifdef CONFIG_DMA_ENGINE
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500489static void mmci_dma_setup(struct mmci_host *host)
Russell Kingc8ebae32011-01-11 19:35:53 +0000490{
Russell Kingc8ebae32011-01-11 19:35:53 +0000491 const char *rxname, *txname;
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100492 struct variant_data *variant = host->variant;
Russell Kingc8ebae32011-01-11 19:35:53 +0000493
Lee Jones1fd83f02013-05-03 12:51:17 +0100494 host->dma_rx_channel = dma_request_slave_channel(mmc_dev(host->mmc), "rx");
495 host->dma_tx_channel = dma_request_slave_channel(mmc_dev(host->mmc), "tx");
Russell Kingc8ebae32011-01-11 19:35:53 +0000496
Per Forlin58c7ccb2011-07-01 18:55:24 +0200497 /* initialize pre request cookie */
498 host->next_data.cookie = 1;
499
Russell Kingc8ebae32011-01-11 19:35:53 +0000500 /*
501 * If only an RX channel is specified, the driver will
502 * attempt to use it bidirectionally, however if it is
503 * is specified but cannot be located, DMA will be disabled.
504 */
Lee Jones1fd83f02013-05-03 12:51:17 +0100505 if (host->dma_rx_channel && !host->dma_tx_channel)
Russell Kingc8ebae32011-01-11 19:35:53 +0000506 host->dma_tx_channel = host->dma_rx_channel;
Russell Kingc8ebae32011-01-11 19:35:53 +0000507
508 if (host->dma_rx_channel)
509 rxname = dma_chan_name(host->dma_rx_channel);
510 else
511 rxname = "none";
512
513 if (host->dma_tx_channel)
514 txname = dma_chan_name(host->dma_tx_channel);
515 else
516 txname = "none";
517
518 dev_info(mmc_dev(host->mmc), "DMA channels RX %s, TX %s\n",
519 rxname, txname);
520
521 /*
522 * Limit the maximum segment size in any SG entry according to
523 * the parameters of the DMA engine device.
524 */
525 if (host->dma_tx_channel) {
526 struct device *dev = host->dma_tx_channel->device->dev;
527 unsigned int max_seg_size = dma_get_max_seg_size(dev);
528
529 if (max_seg_size < host->mmc->max_seg_size)
530 host->mmc->max_seg_size = max_seg_size;
531 }
532 if (host->dma_rx_channel) {
533 struct device *dev = host->dma_rx_channel->device->dev;
534 unsigned int max_seg_size = dma_get_max_seg_size(dev);
535
536 if (max_seg_size < host->mmc->max_seg_size)
537 host->mmc->max_seg_size = max_seg_size;
538 }
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100539
540 if (variant->qcom_dml && host->dma_rx_channel && host->dma_tx_channel)
541 if (dml_hw_init(host, host->mmc->parent->of_node))
542 variant->qcom_dml = false;
Russell Kingc8ebae32011-01-11 19:35:53 +0000543}
544
545/*
Bill Pemberton6e0ee712012-11-19 13:26:03 -0500546 * This is used in or so inline it
Russell Kingc8ebae32011-01-11 19:35:53 +0000547 * so it can be discarded.
548 */
549static inline void mmci_dma_release(struct mmci_host *host)
550{
Russell Kingc8ebae32011-01-11 19:35:53 +0000551 if (host->dma_rx_channel)
552 dma_release_channel(host->dma_rx_channel);
Ulf Hansson8c3a05b2014-05-20 06:45:54 +0200553 if (host->dma_tx_channel)
Russell Kingc8ebae32011-01-11 19:35:53 +0000554 dma_release_channel(host->dma_tx_channel);
555 host->dma_rx_channel = host->dma_tx_channel = NULL;
556}
557
Ulf Hansson653a7612013-01-21 21:29:34 +0100558static void mmci_dma_data_error(struct mmci_host *host)
559{
560 dev_err(mmc_dev(host->mmc), "error during DMA transfer!\n");
561 dmaengine_terminate_all(host->dma_current);
Linus Walleije13934b2017-01-27 15:04:54 +0100562 host->dma_in_progress = false;
Ulf Hansson653a7612013-01-21 21:29:34 +0100563 host->dma_current = NULL;
564 host->dma_desc_current = NULL;
565 host->data->host_cookie = 0;
566}
567
Russell Kingc8ebae32011-01-11 19:35:53 +0000568static void mmci_dma_unmap(struct mmci_host *host, struct mmc_data *data)
569{
Ulf Hansson653a7612013-01-21 21:29:34 +0100570 struct dma_chan *chan;
Ulf Hansson653a7612013-01-21 21:29:34 +0100571
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200572 if (data->flags & MMC_DATA_READ)
Ulf Hansson653a7612013-01-21 21:29:34 +0100573 chan = host->dma_rx_channel;
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200574 else
Ulf Hansson653a7612013-01-21 21:29:34 +0100575 chan = host->dma_tx_channel;
Ulf Hansson653a7612013-01-21 21:29:34 +0100576
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200577 dma_unmap_sg(chan->device->dev, data->sg, data->sg_len,
578 mmc_get_dma_dir(data));
Ulf Hansson653a7612013-01-21 21:29:34 +0100579}
580
581static void mmci_dma_finalize(struct mmci_host *host, struct mmc_data *data)
582{
Russell Kingc8ebae32011-01-11 19:35:53 +0000583 u32 status;
584 int i;
585
586 /* Wait up to 1ms for the DMA to complete */
587 for (i = 0; ; i++) {
588 status = readl(host->base + MMCISTATUS);
589 if (!(status & MCI_RXDATAAVLBLMASK) || i >= 100)
590 break;
591 udelay(10);
592 }
593
594 /*
595 * Check to see whether we still have some data left in the FIFO -
596 * this catches DMA controllers which are unable to monitor the
597 * DMALBREQ and DMALSREQ signals while allowing us to DMA to non-
598 * contiguous buffers. On TX, we'll get a FIFO underrun error.
599 */
600 if (status & MCI_RXDATAAVLBLMASK) {
Ulf Hansson653a7612013-01-21 21:29:34 +0100601 mmci_dma_data_error(host);
Russell Kingc8ebae32011-01-11 19:35:53 +0000602 if (!data->error)
603 data->error = -EIO;
604 }
605
Per Forlin58c7ccb2011-07-01 18:55:24 +0200606 if (!data->host_cookie)
Ulf Hansson653a7612013-01-21 21:29:34 +0100607 mmci_dma_unmap(host, data);
Russell Kingc8ebae32011-01-11 19:35:53 +0000608
609 /*
610 * Use of DMA with scatter-gather is impossible.
611 * Give up with DMA and switch back to PIO mode.
612 */
613 if (status & MCI_RXDATAAVLBLMASK) {
614 dev_err(mmc_dev(host->mmc), "buggy DMA detected. Taking evasive action.\n");
615 mmci_dma_release(host);
616 }
Ulf Hansson653a7612013-01-21 21:29:34 +0100617
Linus Walleije13934b2017-01-27 15:04:54 +0100618 host->dma_in_progress = false;
Ulf Hansson653a7612013-01-21 21:29:34 +0100619 host->dma_current = NULL;
620 host->dma_desc_current = NULL;
Russell Kingc8ebae32011-01-11 19:35:53 +0000621}
622
Ulf Hansson653a7612013-01-21 21:29:34 +0100623/* prepares DMA channel and DMA descriptor, returns non-zero on failure */
624static int __mmci_dma_prep_data(struct mmci_host *host, struct mmc_data *data,
625 struct dma_chan **dma_chan,
626 struct dma_async_tx_descriptor **dma_desc)
Russell Kingc8ebae32011-01-11 19:35:53 +0000627{
628 struct variant_data *variant = host->variant;
629 struct dma_slave_config conf = {
630 .src_addr = host->phybase + MMCIFIFO,
631 .dst_addr = host->phybase + MMCIFIFO,
632 .src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
633 .dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
634 .src_maxburst = variant->fifohalfsize >> 2, /* # of words */
635 .dst_maxburst = variant->fifohalfsize >> 2, /* # of words */
Viresh Kumar258aea72012-02-01 16:12:19 +0530636 .device_fc = false,
Russell Kingc8ebae32011-01-11 19:35:53 +0000637 };
Russell Kingc8ebae32011-01-11 19:35:53 +0000638 struct dma_chan *chan;
639 struct dma_device *device;
640 struct dma_async_tx_descriptor *desc;
641 int nr_sg;
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100642 unsigned long flags = DMA_CTRL_ACK;
Russell Kingc8ebae32011-01-11 19:35:53 +0000643
Russell Kingc8ebae32011-01-11 19:35:53 +0000644 if (data->flags & MMC_DATA_READ) {
Vinod Koul05f57992011-10-14 10:45:11 +0530645 conf.direction = DMA_DEV_TO_MEM;
Russell Kingc8ebae32011-01-11 19:35:53 +0000646 chan = host->dma_rx_channel;
647 } else {
Vinod Koul05f57992011-10-14 10:45:11 +0530648 conf.direction = DMA_MEM_TO_DEV;
Russell Kingc8ebae32011-01-11 19:35:53 +0000649 chan = host->dma_tx_channel;
650 }
651
652 /* If there's no DMA channel, fall back to PIO */
653 if (!chan)
654 return -EINVAL;
655
656 /* If less than or equal to the fifo size, don't bother with DMA */
Per Forlin58c7ccb2011-07-01 18:55:24 +0200657 if (data->blksz * data->blocks <= variant->fifosize)
Russell Kingc8ebae32011-01-11 19:35:53 +0000658 return -EINVAL;
659
660 device = chan->device;
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200661 nr_sg = dma_map_sg(device->dev, data->sg, data->sg_len,
662 mmc_get_dma_dir(data));
Russell Kingc8ebae32011-01-11 19:35:53 +0000663 if (nr_sg == 0)
664 return -EINVAL;
665
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100666 if (host->variant->qcom_dml)
667 flags |= DMA_PREP_INTERRUPT;
668
Russell Kingc8ebae32011-01-11 19:35:53 +0000669 dmaengine_slave_config(chan, &conf);
Alexandre Bounine16052822012-03-08 16:11:18 -0500670 desc = dmaengine_prep_slave_sg(chan, data->sg, nr_sg,
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100671 conf.direction, flags);
Russell Kingc8ebae32011-01-11 19:35:53 +0000672 if (!desc)
673 goto unmap_exit;
674
Ulf Hansson653a7612013-01-21 21:29:34 +0100675 *dma_chan = chan;
676 *dma_desc = desc;
Russell Kingc8ebae32011-01-11 19:35:53 +0000677
Per Forlin58c7ccb2011-07-01 18:55:24 +0200678 return 0;
679
680 unmap_exit:
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200681 dma_unmap_sg(device->dev, data->sg, data->sg_len,
682 mmc_get_dma_dir(data));
Per Forlin58c7ccb2011-07-01 18:55:24 +0200683 return -ENOMEM;
684}
685
Ulf Hansson653a7612013-01-21 21:29:34 +0100686static inline int mmci_dma_prep_data(struct mmci_host *host,
687 struct mmc_data *data)
688{
689 /* Check if next job is already prepared. */
690 if (host->dma_current && host->dma_desc_current)
691 return 0;
692
693 /* No job were prepared thus do it now. */
694 return __mmci_dma_prep_data(host, data, &host->dma_current,
695 &host->dma_desc_current);
696}
697
698static inline int mmci_dma_prep_next(struct mmci_host *host,
699 struct mmc_data *data)
700{
701 struct mmci_host_next *nd = &host->next_data;
702 return __mmci_dma_prep_data(host, data, &nd->dma_chan, &nd->dma_desc);
703}
704
Per Forlin58c7ccb2011-07-01 18:55:24 +0200705static int mmci_dma_start_data(struct mmci_host *host, unsigned int datactrl)
706{
707 int ret;
708 struct mmc_data *data = host->data;
709
Ulf Hansson653a7612013-01-21 21:29:34 +0100710 ret = mmci_dma_prep_data(host, host->data);
Per Forlin58c7ccb2011-07-01 18:55:24 +0200711 if (ret)
712 return ret;
713
714 /* Okay, go for it. */
Russell Kingc8ebae32011-01-11 19:35:53 +0000715 dev_vdbg(mmc_dev(host->mmc),
716 "Submit MMCI DMA job, sglen %d blksz %04x blks %04x flags %08x\n",
717 data->sg_len, data->blksz, data->blocks, data->flags);
Linus Walleije13934b2017-01-27 15:04:54 +0100718 host->dma_in_progress = true;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200719 dmaengine_submit(host->dma_desc_current);
720 dma_async_issue_pending(host->dma_current);
Russell Kingc8ebae32011-01-11 19:35:53 +0000721
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100722 if (host->variant->qcom_dml)
723 dml_start_xfer(host, data);
724
Russell Kingc8ebae32011-01-11 19:35:53 +0000725 datactrl |= MCI_DPSM_DMAENABLE;
726
727 /* Trigger the DMA transfer */
Ulf Hansson9cc639a2013-05-15 20:48:23 +0100728 mmci_write_datactrlreg(host, datactrl);
Russell Kingc8ebae32011-01-11 19:35:53 +0000729
730 /*
731 * Let the MMCI say when the data is ended and it's time
732 * to fire next DMA request. When that happens, MMCI will
733 * call mmci_data_end()
734 */
735 writel(readl(host->base + MMCIMASK0) | MCI_DATAENDMASK,
736 host->base + MMCIMASK0);
737 return 0;
Russell Kingc8ebae32011-01-11 19:35:53 +0000738}
Per Forlin58c7ccb2011-07-01 18:55:24 +0200739
740static void mmci_get_next_data(struct mmci_host *host, struct mmc_data *data)
741{
742 struct mmci_host_next *next = &host->next_data;
743
Ulf Hansson653a7612013-01-21 21:29:34 +0100744 WARN_ON(data->host_cookie && data->host_cookie != next->cookie);
745 WARN_ON(!data->host_cookie && (next->dma_desc || next->dma_chan));
Per Forlin58c7ccb2011-07-01 18:55:24 +0200746
747 host->dma_desc_current = next->dma_desc;
748 host->dma_current = next->dma_chan;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200749 next->dma_desc = NULL;
750 next->dma_chan = NULL;
751}
752
Linus Walleijd3c6aac2016-11-23 11:02:24 +0100753static void mmci_pre_request(struct mmc_host *mmc, struct mmc_request *mrq)
Per Forlin58c7ccb2011-07-01 18:55:24 +0200754{
755 struct mmci_host *host = mmc_priv(mmc);
756 struct mmc_data *data = mrq->data;
757 struct mmci_host_next *nd = &host->next_data;
758
759 if (!data)
760 return;
761
Ulf Hansson653a7612013-01-21 21:29:34 +0100762 BUG_ON(data->host_cookie);
Per Forlin58c7ccb2011-07-01 18:55:24 +0200763
Ulf Hansson653a7612013-01-21 21:29:34 +0100764 if (mmci_validate_data(host, data))
765 return;
766
767 if (!mmci_dma_prep_next(host, data))
768 data->host_cookie = ++nd->cookie < 0 ? 1 : nd->cookie;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200769}
770
771static void mmci_post_request(struct mmc_host *mmc, struct mmc_request *mrq,
772 int err)
773{
774 struct mmci_host *host = mmc_priv(mmc);
775 struct mmc_data *data = mrq->data;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200776
Ulf Hansson653a7612013-01-21 21:29:34 +0100777 if (!data || !data->host_cookie)
Per Forlin58c7ccb2011-07-01 18:55:24 +0200778 return;
779
Ulf Hansson653a7612013-01-21 21:29:34 +0100780 mmci_dma_unmap(host, data);
Per Forlin58c7ccb2011-07-01 18:55:24 +0200781
Ulf Hansson653a7612013-01-21 21:29:34 +0100782 if (err) {
783 struct mmci_host_next *next = &host->next_data;
784 struct dma_chan *chan;
785 if (data->flags & MMC_DATA_READ)
786 chan = host->dma_rx_channel;
787 else
788 chan = host->dma_tx_channel;
789 dmaengine_terminate_all(chan);
Per Forlin58c7ccb2011-07-01 18:55:24 +0200790
Srinivas Kandagatlab5c16a62014-10-08 12:25:17 +0100791 if (host->dma_desc_current == next->dma_desc)
792 host->dma_desc_current = NULL;
793
Linus Walleije13934b2017-01-27 15:04:54 +0100794 if (host->dma_current == next->dma_chan) {
795 host->dma_in_progress = false;
Srinivas Kandagatlab5c16a62014-10-08 12:25:17 +0100796 host->dma_current = NULL;
Linus Walleije13934b2017-01-27 15:04:54 +0100797 }
Srinivas Kandagatlab5c16a62014-10-08 12:25:17 +0100798
Ulf Hansson653a7612013-01-21 21:29:34 +0100799 next->dma_desc = NULL;
800 next->dma_chan = NULL;
Srinivas Kandagatlab5c16a62014-10-08 12:25:17 +0100801 data->host_cookie = 0;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200802 }
803}
804
Russell Kingc8ebae32011-01-11 19:35:53 +0000805#else
806/* Blank functions if the DMA engine is not available */
Per Forlin58c7ccb2011-07-01 18:55:24 +0200807static void mmci_get_next_data(struct mmci_host *host, struct mmc_data *data)
808{
809}
Russell Kingc8ebae32011-01-11 19:35:53 +0000810static inline void mmci_dma_setup(struct mmci_host *host)
811{
812}
813
814static inline void mmci_dma_release(struct mmci_host *host)
815{
816}
817
818static inline void mmci_dma_unmap(struct mmci_host *host, struct mmc_data *data)
819{
820}
821
Ulf Hansson653a7612013-01-21 21:29:34 +0100822static inline void mmci_dma_finalize(struct mmci_host *host,
823 struct mmc_data *data)
824{
825}
826
Russell Kingc8ebae32011-01-11 19:35:53 +0000827static inline void mmci_dma_data_error(struct mmci_host *host)
828{
829}
830
831static inline int mmci_dma_start_data(struct mmci_host *host, unsigned int datactrl)
832{
833 return -ENOSYS;
834}
Per Forlin58c7ccb2011-07-01 18:55:24 +0200835
836#define mmci_pre_request NULL
837#define mmci_post_request NULL
838
Russell Kingc8ebae32011-01-11 19:35:53 +0000839#endif
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841static void mmci_start_data(struct mmci_host *host, struct mmc_data *data)
842{
Rabin Vincent8301bb62010-08-09 12:57:30 +0100843 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 unsigned int datactrl, timeout, irqmask;
Russell King7b09cda2005-07-01 12:02:59 +0100845 unsigned long long clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 void __iomem *base;
Russell King3bc87f22006-08-27 13:51:28 +0100847 int blksz_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Linus Walleij64de0282010-02-19 01:09:10 +0100849 dev_dbg(mmc_dev(host->mmc), "blksz %04x blks %04x flags %08x\n",
850 data->blksz, data->blocks, data->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 host->data = data;
Rabin Vincent528320d2010-07-21 12:49:49 +0100853 host->size = data->blksz * data->blocks;
Russell King51d43752011-01-27 10:56:52 +0000854 data->bytes_xfered = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Russell King7b09cda2005-07-01 12:02:59 +0100856 clks = (unsigned long long)data->timeout_ns * host->cclk;
Srinivas Kandagatlac4a35762014-06-02 10:08:39 +0100857 do_div(clks, NSEC_PER_SEC);
Russell King7b09cda2005-07-01 12:02:59 +0100858
859 timeout = data->timeout_clks + (unsigned int)clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 base = host->base;
862 writel(timeout, base + MMCIDATATIMER);
863 writel(host->size, base + MMCIDATALENGTH);
864
Russell King3bc87f22006-08-27 13:51:28 +0100865 blksz_bits = ffs(data->blksz) - 1;
866 BUG_ON(1 << blksz_bits != data->blksz);
867
Philippe Langlais1784b152011-03-25 08:51:52 +0100868 if (variant->blksz_datactrl16)
869 datactrl = MCI_DPSM_ENABLE | (data->blksz << 16);
Srinivas Kandagatlaff783232014-06-02 10:09:06 +0100870 else if (variant->blksz_datactrl4)
871 datactrl = MCI_DPSM_ENABLE | (data->blksz << 4);
Philippe Langlais1784b152011-03-25 08:51:52 +0100872 else
873 datactrl = MCI_DPSM_ENABLE | blksz_bits << 4;
Russell Kingc8ebae32011-01-11 19:35:53 +0000874
875 if (data->flags & MMC_DATA_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 datactrl |= MCI_DPSM_DIRECTION;
Russell Kingc8ebae32011-01-11 19:35:53 +0000877
Srinivas Kandagatlac7354132014-08-22 05:55:16 +0100878 if (host->mmc->card && mmc_card_sdio(host->mmc->card)) {
879 u32 clk;
Ulf Hansson7258db72011-12-13 17:05:28 +0100880
Srinivas Kandagatlac7354132014-08-22 05:55:16 +0100881 datactrl |= variant->datactrl_mask_sdio;
Ulf Hansson06c1a122012-10-12 14:01:50 +0100882
Srinivas Kandagatlac7354132014-08-22 05:55:16 +0100883 /*
884 * The ST Micro variant for SDIO small write transfers
885 * needs to have clock H/W flow control disabled,
886 * otherwise the transfer will not start. The threshold
887 * depends on the rate of MCLK.
888 */
889 if (variant->st_sdio && data->flags & MMC_DATA_WRITE &&
890 (host->size < 8 ||
891 (host->size <= 8 && host->mclk > 50000000)))
892 clk = host->clk_reg & ~variant->clkreg_enable;
893 else
894 clk = host->clk_reg | variant->clkreg_enable;
895
896 mmci_write_clkreg(host, clk);
897 }
Ulf Hansson06c1a122012-10-12 14:01:50 +0100898
Seungwon Jeon6dad6c92014-03-14 21:12:13 +0900899 if (host->mmc->ios.timing == MMC_TIMING_UHS_DDR50 ||
900 host->mmc->ios.timing == MMC_TIMING_MMC_DDR52)
Srinivas Kandagatlae17dca22014-06-02 10:09:15 +0100901 datactrl |= variant->datactrl_mask_ddrmode;
Ulf Hansson6dbb6ee2013-01-07 15:30:44 +0100902
Russell Kingc8ebae32011-01-11 19:35:53 +0000903 /*
904 * Attempt to use DMA operation mode, if this
905 * should fail, fall back to PIO mode
906 */
907 if (!mmci_dma_start_data(host, datactrl))
908 return;
909
910 /* IRQ mode, map the SG list for CPU reading/writing */
911 mmci_init_sg(host, data);
912
913 if (data->flags & MMC_DATA_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 irqmask = MCI_RXFIFOHALFFULLMASK;
Russell King0425a142006-02-16 16:48:31 +0000915
916 /*
Russell Kingc4d877c2011-01-27 09:50:13 +0000917 * If we have less than the fifo 'half-full' threshold to
918 * transfer, trigger a PIO interrupt as soon as any data
919 * is available.
Russell King0425a142006-02-16 16:48:31 +0000920 */
Russell Kingc4d877c2011-01-27 09:50:13 +0000921 if (host->size < variant->fifohalfsize)
Russell King0425a142006-02-16 16:48:31 +0000922 irqmask |= MCI_RXDATAAVLBLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 } else {
924 /*
925 * We don't actually need to include "FIFO empty" here
926 * since its implicit in "FIFO half empty".
927 */
928 irqmask = MCI_TXFIFOHALFEMPTYMASK;
929 }
930
Ulf Hansson9cc639a2013-05-15 20:48:23 +0100931 mmci_write_datactrlreg(host, datactrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 writel(readl(base + MMCIMASK0) & ~MCI_DATAENDMASK, base + MMCIMASK0);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100933 mmci_set_mask1(host, irqmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934}
935
936static void
937mmci_start_command(struct mmci_host *host, struct mmc_command *cmd, u32 c)
938{
939 void __iomem *base = host->base;
940
Linus Walleij64de0282010-02-19 01:09:10 +0100941 dev_dbg(mmc_dev(host->mmc), "op %02x arg %08x flags %08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 cmd->opcode, cmd->arg, cmd->flags);
943
944 if (readl(base + MMCICOMMAND) & MCI_CPSM_ENABLE) {
945 writel(0, base + MMCICOMMAND);
Srinivas Kandagatla6adb2a82014-06-02 10:08:57 +0100946 mmci_reg_delay(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 }
948
949 c |= cmd->opcode | MCI_CPSM_ENABLE;
Russell Kinge9225172006-02-02 12:23:12 +0000950 if (cmd->flags & MMC_RSP_PRESENT) {
951 if (cmd->flags & MMC_RSP_136)
952 c |= MCI_CPSM_LONGRSP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 c |= MCI_CPSM_RESPONSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 }
955 if (/*interrupt*/0)
956 c |= MCI_CPSM_INTERRUPT;
957
Srinivas Kandagatlaae7b0062014-06-02 10:09:39 +0100958 if (mmc_cmd_type(cmd) == MMC_CMD_ADTC)
959 c |= host->variant->data_cmd_enable;
960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 host->cmd = cmd;
962
963 writel(cmd->arg, base + MMCIARGUMENT);
964 writel(c, base + MMCICOMMAND);
965}
966
967static void
968mmci_data_irq(struct mmci_host *host, struct mmc_data *data,
969 unsigned int status)
970{
Ulf Hansson1cb9da52014-06-12 14:42:23 +0200971 /* Make sure we have data to handle */
972 if (!data)
973 return;
974
Linus Walleijf20f8f22010-10-19 13:41:24 +0100975 /* First check for errors */
Patrice Chotard7f7b5502018-01-18 15:34:18 +0100976 if (status & (MCI_DATACRCFAIL | MCI_DATATIMEOUT |
977 host->variant->start_err |
978 MCI_TXUNDERRUN | MCI_RXOVERRUN)) {
Linus Walleij8cb28152011-01-24 15:22:13 +0100979 u32 remain, success;
Linus Walleijf20f8f22010-10-19 13:41:24 +0100980
Russell Kingc8ebae32011-01-11 19:35:53 +0000981 /* Terminate the DMA transfer */
Ulf Hansson653a7612013-01-21 21:29:34 +0100982 if (dma_inprogress(host)) {
Russell Kingc8ebae32011-01-11 19:35:53 +0000983 mmci_dma_data_error(host);
Ulf Hansson653a7612013-01-21 21:29:34 +0100984 mmci_dma_unmap(host, data);
985 }
Russell Kingc8ebae32011-01-11 19:35:53 +0000986
Russell Kingc8afc9d2011-02-04 09:19:46 +0000987 /*
988 * Calculate how far we are into the transfer. Note that
989 * the data counter gives the number of bytes transferred
990 * on the MMC bus, not on the host side. On reads, this
991 * can be as much as a FIFO-worth of data ahead. This
992 * matters for FIFO overruns only.
993 */
Linus Walleijf5a106d2011-01-27 17:44:34 +0100994 remain = readl(host->base + MMCIDATACNT);
Linus Walleij8cb28152011-01-24 15:22:13 +0100995 success = data->blksz * data->blocks - remain;
996
Russell Kingc8afc9d2011-02-04 09:19:46 +0000997 dev_dbg(mmc_dev(host->mmc), "MCI ERROR IRQ, status 0x%08x at 0x%08x\n",
998 status, success);
Linus Walleij8cb28152011-01-24 15:22:13 +0100999 if (status & MCI_DATACRCFAIL) {
1000 /* Last block was not successful */
Russell Kingc8afc9d2011-02-04 09:19:46 +00001001 success -= 1;
Pierre Ossman17b04292007-07-22 22:18:46 +02001002 data->error = -EILSEQ;
Linus Walleij8cb28152011-01-24 15:22:13 +01001003 } else if (status & MCI_DATATIMEOUT) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001004 data->error = -ETIMEDOUT;
Linus Walleij757df742011-06-30 15:10:21 +01001005 } else if (status & MCI_STARTBITERR) {
1006 data->error = -ECOMM;
Russell Kingc8afc9d2011-02-04 09:19:46 +00001007 } else if (status & MCI_TXUNDERRUN) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001008 data->error = -EIO;
Russell Kingc8afc9d2011-02-04 09:19:46 +00001009 } else if (status & MCI_RXOVERRUN) {
1010 if (success > host->variant->fifosize)
1011 success -= host->variant->fifosize;
1012 else
1013 success = 0;
Linus Walleij8cb28152011-01-24 15:22:13 +01001014 data->error = -EIO;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001015 }
Russell King51d43752011-01-27 10:56:52 +00001016 data->bytes_xfered = round_down(success, data->blksz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 }
Linus Walleijf20f8f22010-10-19 13:41:24 +01001018
Linus Walleij8cb28152011-01-24 15:22:13 +01001019 if (status & MCI_DATABLOCKEND)
1020 dev_err(mmc_dev(host->mmc), "stray MCI_DATABLOCKEND interrupt\n");
Linus Walleijf20f8f22010-10-19 13:41:24 +01001021
Russell Kingccff9b52011-01-30 21:03:50 +00001022 if (status & MCI_DATAEND || data->error) {
Russell Kingc8ebae32011-01-11 19:35:53 +00001023 if (dma_inprogress(host))
Ulf Hansson653a7612013-01-21 21:29:34 +01001024 mmci_dma_finalize(host, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 mmci_stop_data(host);
1026
Linus Walleij8cb28152011-01-24 15:22:13 +01001027 if (!data->error)
1028 /* The error clause is handled above, success! */
Russell King51d43752011-01-27 10:56:52 +00001029 data->bytes_xfered = data->blksz * data->blocks;
Linus Walleijf20f8f22010-10-19 13:41:24 +01001030
Ulf Hansson024629c2013-05-13 15:40:56 +01001031 if (!data->stop || host->mrq->sbc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 mmci_request_end(host, data->mrq);
1033 } else {
1034 mmci_start_command(host, data->stop, 0);
1035 }
1036 }
1037}
1038
1039static void
1040mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
1041 unsigned int status)
1042{
1043 void __iomem *base = host->base;
Linus Walleij49adc0c2016-10-25 11:06:06 +02001044 bool sbc;
Ulf Hanssonad82bfe2014-06-12 15:01:57 +02001045
1046 if (!cmd)
1047 return;
1048
1049 sbc = (cmd == host->mrq->sbc);
Ulf Hanssonad82bfe2014-06-12 15:01:57 +02001050
Linus Walleij49adc0c2016-10-25 11:06:06 +02001051 /*
1052 * We need to be one of these interrupts to be considered worth
1053 * handling. Note that we tag on any latent IRQs postponed
1054 * due to waiting for busy status.
1055 */
1056 if (!((status|host->busy_status) &
1057 (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT|MCI_CMDSENT|MCI_CMDRESPEND)))
Ulf Hanssonad82bfe2014-06-12 15:01:57 +02001058 return;
Ulf Hansson8d94b542014-01-13 16:49:31 +01001059
Linus Walleij49adc0c2016-10-25 11:06:06 +02001060 /*
1061 * ST Micro variant: handle busy detection.
1062 */
1063 if (host->variant->busy_detect) {
1064 bool busy_resp = !!(cmd->flags & MMC_RSP_BUSY);
Ulf Hansson8d94b542014-01-13 16:49:31 +01001065
Linus Walleij49adc0c2016-10-25 11:06:06 +02001066 /* We are busy with a command, return */
1067 if (host->busy_status &&
1068 (status & host->variant->busy_detect_flag))
1069 return;
Ulf Hansson8d94b542014-01-13 16:49:31 +01001070
Linus Walleij49adc0c2016-10-25 11:06:06 +02001071 /*
1072 * We were not busy, but we now got a busy response on
1073 * something that was not an error, and we double-check
1074 * that the special busy status bit is still set before
1075 * proceeding.
1076 */
1077 if (!host->busy_status && busy_resp &&
1078 !(status & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT)) &&
1079 (readl(base + MMCISTATUS) & host->variant->busy_detect_flag)) {
Jean-Nicolas Graux5cad24d2017-02-07 12:12:41 +01001080
1081 /* Clear the busy start IRQ */
1082 writel(host->variant->busy_detect_mask,
1083 host->base + MMCICLEAR);
1084
1085 /* Unmask the busy end IRQ */
Linus Walleij49adc0c2016-10-25 11:06:06 +02001086 writel(readl(base + MMCIMASK0) |
1087 host->variant->busy_detect_mask,
1088 base + MMCIMASK0);
1089 /*
1090 * Now cache the last response status code (until
1091 * the busy bit goes low), and return.
1092 */
1093 host->busy_status =
1094 status & (MCI_CMDSENT|MCI_CMDRESPEND);
1095 return;
1096 }
1097
1098 /*
1099 * At this point we are not busy with a command, we have
Jean-Nicolas Graux5cad24d2017-02-07 12:12:41 +01001100 * not received a new busy request, clear and mask the busy
1101 * end IRQ and fall through to process the IRQ.
Linus Walleij49adc0c2016-10-25 11:06:06 +02001102 */
1103 if (host->busy_status) {
Jean-Nicolas Graux5cad24d2017-02-07 12:12:41 +01001104
1105 writel(host->variant->busy_detect_mask,
1106 host->base + MMCICLEAR);
1107
Linus Walleij49adc0c2016-10-25 11:06:06 +02001108 writel(readl(base + MMCIMASK0) &
1109 ~host->variant->busy_detect_mask,
1110 base + MMCIMASK0);
1111 host->busy_status = 0;
1112 }
Ulf Hansson8d94b542014-01-13 16:49:31 +01001113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115 host->cmd = NULL;
1116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 if (status & MCI_CMDTIMEOUT) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001118 cmd->error = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001120 cmd->error = -EILSEQ;
Russell King - ARM Linux9047b432011-01-11 16:35:56 +00001121 } else {
1122 cmd->resp[0] = readl(base + MMCIRESPONSE0);
1123 cmd->resp[1] = readl(base + MMCIRESPONSE1);
1124 cmd->resp[2] = readl(base + MMCIRESPONSE2);
1125 cmd->resp[3] = readl(base + MMCIRESPONSE3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 }
1127
Ulf Hansson024629c2013-05-13 15:40:56 +01001128 if ((!sbc && !cmd->data) || cmd->error) {
Ulf Hansson3b6e3c72011-12-13 16:58:43 +01001129 if (host->data) {
1130 /* Terminate the DMA transfer */
Ulf Hansson653a7612013-01-21 21:29:34 +01001131 if (dma_inprogress(host)) {
Ulf Hansson3b6e3c72011-12-13 16:58:43 +01001132 mmci_dma_data_error(host);
Ulf Hansson653a7612013-01-21 21:29:34 +01001133 mmci_dma_unmap(host, host->data);
1134 }
Russell Kinge47c2222007-01-08 16:42:51 +00001135 mmci_stop_data(host);
Ulf Hansson3b6e3c72011-12-13 16:58:43 +01001136 }
Ulf Hansson024629c2013-05-13 15:40:56 +01001137 mmci_request_end(host, host->mrq);
1138 } else if (sbc) {
1139 mmci_start_command(host, host->mrq->cmd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 } else if (!(cmd->data->flags & MMC_DATA_READ)) {
1141 mmci_start_data(host, cmd->data);
1142 }
1143}
1144
Srinivas Kandagatla9c34b732014-06-02 10:10:04 +01001145static int mmci_get_rx_fifocnt(struct mmci_host *host, u32 status, int remain)
1146{
1147 return remain - (readl(host->base + MMCIFIFOCNT) << 2);
1148}
1149
1150static int mmci_qcom_get_rx_fifocnt(struct mmci_host *host, u32 status, int r)
1151{
1152 /*
1153 * on qcom SDCC4 only 8 words are used in each burst so only 8 addresses
1154 * from the fifo range should be used
1155 */
1156 if (status & MCI_RXFIFOHALFFULL)
1157 return host->variant->fifohalfsize;
1158 else if (status & MCI_RXDATAAVLBL)
1159 return 4;
1160
1161 return 0;
1162}
1163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164static int mmci_pio_read(struct mmci_host *host, char *buffer, unsigned int remain)
1165{
1166 void __iomem *base = host->base;
1167 char *ptr = buffer;
Srinivas Kandagatla9c34b732014-06-02 10:10:04 +01001168 u32 status = readl(host->base + MMCISTATUS);
Linus Walleij26eed9a2008-04-26 23:39:44 +01001169 int host_remain = host->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171 do {
Srinivas Kandagatla9c34b732014-06-02 10:10:04 +01001172 int count = host->get_rx_fifocnt(host, status, host_remain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
1174 if (count > remain)
1175 count = remain;
1176
1177 if (count <= 0)
1178 break;
1179
Ulf Hansson393e5e22011-12-13 17:08:04 +01001180 /*
1181 * SDIO especially may want to send something that is
1182 * not divisible by 4 (as opposed to card sectors
1183 * etc). Therefore make sure to always read the last bytes
1184 * while only doing full 32-bit reads towards the FIFO.
1185 */
1186 if (unlikely(count & 0x3)) {
1187 if (count < 4) {
1188 unsigned char buf[4];
Davide Ciminaghi4b85da02012-12-10 14:47:21 +01001189 ioread32_rep(base + MMCIFIFO, buf, 1);
Ulf Hansson393e5e22011-12-13 17:08:04 +01001190 memcpy(ptr, buf, count);
1191 } else {
Davide Ciminaghi4b85da02012-12-10 14:47:21 +01001192 ioread32_rep(base + MMCIFIFO, ptr, count >> 2);
Ulf Hansson393e5e22011-12-13 17:08:04 +01001193 count &= ~0x3;
1194 }
1195 } else {
Davide Ciminaghi4b85da02012-12-10 14:47:21 +01001196 ioread32_rep(base + MMCIFIFO, ptr, count >> 2);
Ulf Hansson393e5e22011-12-13 17:08:04 +01001197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199 ptr += count;
1200 remain -= count;
Linus Walleij26eed9a2008-04-26 23:39:44 +01001201 host_remain -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
1203 if (remain == 0)
1204 break;
1205
1206 status = readl(base + MMCISTATUS);
1207 } while (status & MCI_RXDATAAVLBL);
1208
1209 return ptr - buffer;
1210}
1211
1212static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status)
1213{
Rabin Vincent8301bb62010-08-09 12:57:30 +01001214 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 void __iomem *base = host->base;
1216 char *ptr = buffer;
1217
1218 do {
1219 unsigned int count, maxcnt;
1220
Rabin Vincent8301bb62010-08-09 12:57:30 +01001221 maxcnt = status & MCI_TXFIFOEMPTY ?
1222 variant->fifosize : variant->fifohalfsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 count = min(remain, maxcnt);
1224
Linus Walleij34177802010-10-19 12:43:58 +01001225 /*
Linus Walleij34177802010-10-19 12:43:58 +01001226 * SDIO especially may want to send something that is
1227 * not divisible by 4 (as opposed to card sectors
1228 * etc), and the FIFO only accept full 32-bit writes.
1229 * So compensate by adding +3 on the count, a single
1230 * byte become a 32bit write, 7 bytes will be two
1231 * 32bit writes etc.
1232 */
Davide Ciminaghi4b85da02012-12-10 14:47:21 +01001233 iowrite32_rep(base + MMCIFIFO, ptr, (count + 3) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 ptr += count;
1236 remain -= count;
1237
1238 if (remain == 0)
1239 break;
1240
1241 status = readl(base + MMCISTATUS);
1242 } while (status & MCI_TXFIFOHALFEMPTY);
1243
1244 return ptr - buffer;
1245}
1246
1247/*
1248 * PIO data transfer IRQ handler.
1249 */
David Howells7d12e782006-10-05 14:55:46 +01001250static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
1252 struct mmci_host *host = dev_id;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001253 struct sg_mapping_iter *sg_miter = &host->sg_miter;
Rabin Vincent8301bb62010-08-09 12:57:30 +01001254 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 void __iomem *base = host->base;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001256 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 u32 status;
1258
1259 status = readl(base + MMCISTATUS);
1260
Linus Walleij64de0282010-02-19 01:09:10 +01001261 dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001263 local_irq_save(flags);
1264
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 unsigned int remain, len;
1267 char *buffer;
1268
1269 /*
1270 * For write, we only need to test the half-empty flag
1271 * here - if the FIFO is completely empty, then by
1272 * definition it is more than half empty.
1273 *
1274 * For read, check for data available.
1275 */
1276 if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL)))
1277 break;
1278
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001279 if (!sg_miter_next(sg_miter))
1280 break;
1281
1282 buffer = sg_miter->addr;
1283 remain = sg_miter->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 len = 0;
1286 if (status & MCI_RXACTIVE)
1287 len = mmci_pio_read(host, buffer, remain);
1288 if (status & MCI_TXACTIVE)
1289 len = mmci_pio_write(host, buffer, remain, status);
1290
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001291 sg_miter->consumed = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 host->size -= len;
1294 remain -= len;
1295
1296 if (remain)
1297 break;
1298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 status = readl(base + MMCISTATUS);
1300 } while (1);
1301
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001302 sg_miter_stop(sg_miter);
1303
1304 local_irq_restore(flags);
1305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 /*
Russell Kingc4d877c2011-01-27 09:50:13 +00001307 * If we have less than the fifo 'half-full' threshold to transfer,
1308 * trigger a PIO interrupt as soon as any data is available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 */
Russell Kingc4d877c2011-01-27 09:50:13 +00001310 if (status & MCI_RXACTIVE && host->size < variant->fifohalfsize)
Linus Walleij2686b4b2010-10-19 12:39:48 +01001311 mmci_set_mask1(host, MCI_RXDATAAVLBLMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313 /*
1314 * If we run out of data, disable the data IRQs; this
1315 * prevents a race where the FIFO becomes empty before
1316 * the chip itself has disabled the data path, and
1317 * stops us racing with our data end IRQ.
1318 */
1319 if (host->size == 0) {
Linus Walleij2686b4b2010-10-19 12:39:48 +01001320 mmci_set_mask1(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0);
1322 }
1323
1324 return IRQ_HANDLED;
1325}
1326
1327/*
1328 * Handle completion of command and data transfers.
1329 */
David Howells7d12e782006-10-05 14:55:46 +01001330static irqreturn_t mmci_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
1332 struct mmci_host *host = dev_id;
1333 u32 status;
1334 int ret = 0;
1335
1336 spin_lock(&host->lock);
1337
1338 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 status = readl(host->base + MMCISTATUS);
Linus Walleij2686b4b2010-10-19 12:39:48 +01001340
1341 if (host->singleirq) {
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +01001342 if (status & host->mask1_reg)
Linus Walleij2686b4b2010-10-19 12:39:48 +01001343 mmci_pio_irq(irq, dev_id);
1344
1345 status &= ~MCI_IRQ1MASK;
1346 }
1347
Ulf Hansson8d94b542014-01-13 16:49:31 +01001348 /*
Jean-Nicolas Graux5cad24d2017-02-07 12:12:41 +01001349 * We intentionally clear the MCI_ST_CARDBUSY IRQ (if it's
1350 * enabled) in mmci_cmd_irq() function where ST Micro busy
1351 * detection variant is handled. Considering the HW seems to be
1352 * triggering the IRQ on both edges while monitoring DAT0 for
1353 * busy completion and that same status bit is used to monitor
1354 * start and end of busy detection, special care must be taken
1355 * to make sure that both start and end interrupts are always
1356 * cleared one after the other.
Ulf Hansson8d94b542014-01-13 16:49:31 +01001357 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 status &= readl(host->base + MMCIMASK0);
Jean-Nicolas Graux5cad24d2017-02-07 12:12:41 +01001359 if (host->variant->busy_detect)
1360 writel(status & ~host->variant->busy_detect_mask,
1361 host->base + MMCICLEAR);
1362 else
1363 writel(status, host->base + MMCICLEAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Linus Walleij64de0282010-02-19 01:09:10 +01001365 dev_dbg(mmc_dev(host->mmc), "irq0 (data+cmd) %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Ulf Hansson78782892014-06-13 13:21:38 +02001367 if (host->variant->reversed_irq_handling) {
1368 mmci_data_irq(host, host->data, status);
1369 mmci_cmd_irq(host, host->cmd, status);
1370 } else {
1371 mmci_cmd_irq(host, host->cmd, status);
1372 mmci_data_irq(host, host->data, status);
1373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Linus Walleij49adc0c2016-10-25 11:06:06 +02001375 /*
1376 * Don't poll for busy completion in irq context.
1377 */
1378 if (host->variant->busy_detect && host->busy_status)
1379 status &= ~host->variant->busy_detect_flag;
Ulf Hansson8d94b542014-01-13 16:49:31 +01001380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 ret = 1;
1382 } while (status);
1383
1384 spin_unlock(&host->lock);
1385
1386 return IRQ_RETVAL(ret);
1387}
1388
1389static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1390{
1391 struct mmci_host *host = mmc_priv(mmc);
Linus Walleij9e943022008-10-24 21:17:50 +01001392 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394 WARN_ON(host->mrq != NULL);
1395
Ulf Hansson653a7612013-01-21 21:29:34 +01001396 mrq->cmd->error = mmci_validate_data(host, mrq->data);
1397 if (mrq->cmd->error) {
Pierre Ossman255d01a2007-07-24 20:38:53 +02001398 mmc_request_done(mmc, mrq);
1399 return;
1400 }
1401
Linus Walleij9e943022008-10-24 21:17:50 +01001402 spin_lock_irqsave(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 host->mrq = mrq;
1405
Per Forlin58c7ccb2011-07-01 18:55:24 +02001406 if (mrq->data)
1407 mmci_get_next_data(host, mrq->data);
1408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 if (mrq->data && mrq->data->flags & MMC_DATA_READ)
1410 mmci_start_data(host, mrq->data);
1411
Ulf Hansson024629c2013-05-13 15:40:56 +01001412 if (mrq->sbc)
1413 mmci_start_command(host, mrq->sbc, 0);
1414 else
1415 mmci_start_command(host, mrq->cmd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Linus Walleij9e943022008-10-24 21:17:50 +01001417 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418}
1419
1420static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1421{
1422 struct mmci_host *host = mmc_priv(mmc);
Ulf Hansson7d72a1d2011-12-13 16:54:55 +01001423 struct variant_data *variant = host->variant;
Linus Walleija6a64642009-09-14 12:56:14 +01001424 u32 pwr = 0;
1425 unsigned long flags;
Lee Jonesdb90f912013-05-03 12:52:12 +01001426 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Ulf Hanssonbc521812011-12-13 16:57:55 +01001428 if (host->plat->ios_handler &&
1429 host->plat->ios_handler(mmc_dev(mmc), ios))
1430 dev_err(mmc_dev(mmc), "platform ios_handler failed\n");
1431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 switch (ios->power_mode) {
1433 case MMC_POWER_OFF:
Ulf Hansson599c1d52013-01-07 16:22:50 +01001434 if (!IS_ERR(mmc->supply.vmmc))
1435 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Lee Jones237fb5e2013-01-31 11:27:52 +00001436
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001437 if (!IS_ERR(mmc->supply.vqmmc) && host->vqmmc_enabled) {
Lee Jones237fb5e2013-01-31 11:27:52 +00001438 regulator_disable(mmc->supply.vqmmc);
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001439 host->vqmmc_enabled = false;
1440 }
Lee Jones237fb5e2013-01-31 11:27:52 +00001441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 break;
1443 case MMC_POWER_UP:
Ulf Hansson599c1d52013-01-07 16:22:50 +01001444 if (!IS_ERR(mmc->supply.vmmc))
1445 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd);
1446
Ulf Hansson7d72a1d2011-12-13 16:54:55 +01001447 /*
1448 * The ST Micro variant doesn't have the PL180s MCI_PWR_UP
1449 * and instead uses MCI_PWR_ON so apply whatever value is
1450 * configured in the variant data.
1451 */
1452 pwr |= variant->pwrreg_powerup;
1453
1454 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 case MMC_POWER_ON:
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001456 if (!IS_ERR(mmc->supply.vqmmc) && !host->vqmmc_enabled) {
Lee Jonesdb90f912013-05-03 12:52:12 +01001457 ret = regulator_enable(mmc->supply.vqmmc);
1458 if (ret < 0)
1459 dev_err(mmc_dev(mmc),
1460 "failed to enable vqmmc regulator\n");
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001461 else
1462 host->vqmmc_enabled = true;
Lee Jonesdb90f912013-05-03 12:52:12 +01001463 }
Lee Jones237fb5e2013-01-31 11:27:52 +00001464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 pwr |= MCI_PWR_ON;
1466 break;
1467 }
1468
Ulf Hansson4d1a3a02011-12-13 16:57:07 +01001469 if (variant->signal_direction && ios->power_mode != MMC_POWER_OFF) {
1470 /*
1471 * The ST Micro variant has some additional bits
1472 * indicating signal direction for the signals in
1473 * the SD/MMC bus and feedback-clock usage.
1474 */
Ulf Hansson4593df22014-03-21 10:13:05 +01001475 pwr |= host->pwr_reg_add;
Ulf Hansson4d1a3a02011-12-13 16:57:07 +01001476
1477 if (ios->bus_width == MMC_BUS_WIDTH_4)
1478 pwr &= ~MCI_ST_DATA74DIREN;
1479 else if (ios->bus_width == MMC_BUS_WIDTH_1)
1480 pwr &= (~MCI_ST_DATA74DIREN &
1481 ~MCI_ST_DATA31DIREN &
1482 ~MCI_ST_DATA2DIREN);
1483 }
1484
Patrice Chotardf9bb3042018-01-18 15:34:20 +01001485 if (variant->opendrain) {
1486 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
1487 pwr |= variant->opendrain;
1488 } else {
1489 /*
1490 * If the variant cannot configure the pads by its own, then we
1491 * expect the pinctrl to be able to do that for us
1492 */
1493 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
1494 pinctrl_select_state(host->pinctrl, host->pins_opendrain);
1495 else
1496 pinctrl_select_state(host->pinctrl, host->pins_default);
1497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Ulf Hanssonf4670da2013-01-09 17:19:54 +01001499 /*
1500 * If clock = 0 and the variant requires the MMCIPOWER to be used for
1501 * gating the clock, the MCI_PWR_ON bit is cleared.
1502 */
1503 if (!ios->clock && variant->pwrreg_clkgate)
1504 pwr &= ~MCI_PWR_ON;
1505
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001506 if (host->variant->explicit_mclk_control &&
1507 ios->clock != host->clock_cache) {
1508 ret = clk_set_rate(host->clk, ios->clock);
1509 if (ret < 0)
1510 dev_err(mmc_dev(host->mmc),
1511 "Error setting clock rate (%d)\n", ret);
1512 else
1513 host->mclk = clk_get_rate(host->clk);
1514 }
1515 host->clock_cache = ios->clock;
1516
Linus Walleija6a64642009-09-14 12:56:14 +01001517 spin_lock_irqsave(&host->lock, flags);
1518
1519 mmci_set_clkreg(host, ios->clock);
Ulf Hansson7437cfa2012-01-18 09:17:27 +01001520 mmci_write_pwrreg(host, pwr);
Ulf Hanssonf829c042013-09-04 09:01:15 +01001521 mmci_reg_delay(host);
Linus Walleija6a64642009-09-14 12:56:14 +01001522
1523 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524}
1525
Russell King89001442009-07-09 15:16:07 +01001526static int mmci_get_cd(struct mmc_host *mmc)
1527{
1528 struct mmci_host *host = mmc_priv(mmc);
Rabin Vincent29719442010-08-09 12:54:43 +01001529 struct mmci_platform_data *plat = host->plat;
Ulf Hanssond2762092014-03-17 13:56:19 +01001530 unsigned int status = mmc_gpio_get_cd(mmc);
Russell King89001442009-07-09 15:16:07 +01001531
Ulf Hanssond2762092014-03-17 13:56:19 +01001532 if (status == -ENOSYS) {
Rabin Vincent4b8caec2010-08-09 12:56:40 +01001533 if (!plat->status)
1534 return 1; /* Assume always present */
1535
Rabin Vincent29719442010-08-09 12:54:43 +01001536 status = plat->status(mmc_dev(host->mmc));
Ulf Hanssond2762092014-03-17 13:56:19 +01001537 }
Russell King74bc8092010-07-29 15:58:59 +01001538 return status;
Russell King89001442009-07-09 15:16:07 +01001539}
1540
Ulf Hansson0f3ed7f2013-05-15 20:47:33 +01001541static int mmci_sig_volt_switch(struct mmc_host *mmc, struct mmc_ios *ios)
1542{
1543 int ret = 0;
1544
1545 if (!IS_ERR(mmc->supply.vqmmc)) {
1546
Ulf Hansson0f3ed7f2013-05-15 20:47:33 +01001547 switch (ios->signal_voltage) {
1548 case MMC_SIGNAL_VOLTAGE_330:
1549 ret = regulator_set_voltage(mmc->supply.vqmmc,
1550 2700000, 3600000);
1551 break;
1552 case MMC_SIGNAL_VOLTAGE_180:
1553 ret = regulator_set_voltage(mmc->supply.vqmmc,
1554 1700000, 1950000);
1555 break;
1556 case MMC_SIGNAL_VOLTAGE_120:
1557 ret = regulator_set_voltage(mmc->supply.vqmmc,
1558 1100000, 1300000);
1559 break;
1560 }
1561
1562 if (ret)
1563 dev_warn(mmc_dev(mmc), "Voltage switch failed\n");
Ulf Hansson0f3ed7f2013-05-15 20:47:33 +01001564 }
1565
1566 return ret;
1567}
1568
Ulf Hansson01259622013-05-15 20:53:22 +01001569static struct mmc_host_ops mmci_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 .request = mmci_request,
Per Forlin58c7ccb2011-07-01 18:55:24 +02001571 .pre_req = mmci_pre_request,
1572 .post_req = mmci_post_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 .set_ios = mmci_set_ios,
Ulf Hanssond2762092014-03-17 13:56:19 +01001574 .get_ro = mmc_gpio_get_ro,
Russell King89001442009-07-09 15:16:07 +01001575 .get_cd = mmci_get_cd,
Ulf Hansson0f3ed7f2013-05-15 20:47:33 +01001576 .start_signal_voltage_switch = mmci_sig_volt_switch,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577};
1578
Ulf Hansson78f87df2014-03-17 15:53:07 +01001579static int mmci_of_parse(struct device_node *np, struct mmc_host *mmc)
1580{
Ulf Hansson4593df22014-03-21 10:13:05 +01001581 struct mmci_host *host = mmc_priv(mmc);
Ulf Hansson78f87df2014-03-17 15:53:07 +01001582 int ret = mmc_of_parse(mmc);
Lee Jones000bc9d2012-04-16 10:18:43 +01001583
Ulf Hansson78f87df2014-03-17 15:53:07 +01001584 if (ret)
1585 return ret;
Lee Jones000bc9d2012-04-16 10:18:43 +01001586
Ulf Hansson4593df22014-03-21 10:13:05 +01001587 if (of_get_property(np, "st,sig-dir-dat0", NULL))
1588 host->pwr_reg_add |= MCI_ST_DATA0DIREN;
1589 if (of_get_property(np, "st,sig-dir-dat2", NULL))
1590 host->pwr_reg_add |= MCI_ST_DATA2DIREN;
1591 if (of_get_property(np, "st,sig-dir-dat31", NULL))
1592 host->pwr_reg_add |= MCI_ST_DATA31DIREN;
1593 if (of_get_property(np, "st,sig-dir-dat74", NULL))
1594 host->pwr_reg_add |= MCI_ST_DATA74DIREN;
1595 if (of_get_property(np, "st,sig-dir-cmd", NULL))
1596 host->pwr_reg_add |= MCI_ST_CMDDIREN;
1597 if (of_get_property(np, "st,sig-pin-fbclk", NULL))
1598 host->pwr_reg_add |= MCI_ST_FBCLKEN;
1599
Lee Jones000bc9d2012-04-16 10:18:43 +01001600 if (of_get_property(np, "mmc-cap-mmc-highspeed", NULL))
Ulf Hansson78f87df2014-03-17 15:53:07 +01001601 mmc->caps |= MMC_CAP_MMC_HIGHSPEED;
Lee Jones000bc9d2012-04-16 10:18:43 +01001602 if (of_get_property(np, "mmc-cap-sd-highspeed", NULL))
Ulf Hansson78f87df2014-03-17 15:53:07 +01001603 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
Lee Jones000bc9d2012-04-16 10:18:43 +01001604
Ulf Hansson78f87df2014-03-17 15:53:07 +01001605 return 0;
Lee Jones000bc9d2012-04-16 10:18:43 +01001606}
Lee Jones000bc9d2012-04-16 10:18:43 +01001607
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001608static int mmci_probe(struct amba_device *dev,
Russell Kingaa25afa2011-02-19 15:55:00 +00001609 const struct amba_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610{
Linus Walleij6ef297f2009-09-22 14:29:36 +01001611 struct mmci_platform_data *plat = dev->dev.platform_data;
Lee Jones000bc9d2012-04-16 10:18:43 +01001612 struct device_node *np = dev->dev.of_node;
Rabin Vincent4956e102010-07-21 12:54:40 +01001613 struct variant_data *variant = id->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 struct mmci_host *host;
1615 struct mmc_host *mmc;
1616 int ret;
1617
Lee Jones000bc9d2012-04-16 10:18:43 +01001618 /* Must have platform data or Device Tree. */
1619 if (!plat && !np) {
1620 dev_err(&dev->dev, "No plat data or DT found\n");
1621 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 }
1623
Lee Jonesb9b52912012-06-12 10:49:51 +01001624 if (!plat) {
1625 plat = devm_kzalloc(&dev->dev, sizeof(*plat), GFP_KERNEL);
1626 if (!plat)
1627 return -ENOMEM;
1628 }
1629
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
Ulf Hanssonef289982014-03-17 13:56:32 +01001631 if (!mmc)
1632 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
Ulf Hansson78f87df2014-03-17 15:53:07 +01001634 ret = mmci_of_parse(np, mmc);
1635 if (ret)
1636 goto host_free;
1637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 host = mmc_priv(mmc);
Rabin Vincent4ea580f2009-04-17 08:44:19 +05301639 host->mmc = mmc;
Russell King012b7d32009-07-09 15:13:56 +01001640
Patrice Chotardf9bb3042018-01-18 15:34:20 +01001641 /*
1642 * Some variant (STM32) doesn't have opendrain bit, nevertheless
1643 * pins can be set accordingly using pinctrl
1644 */
1645 if (!variant->opendrain) {
1646 host->pinctrl = devm_pinctrl_get(&dev->dev);
1647 if (IS_ERR(host->pinctrl)) {
1648 dev_err(&dev->dev, "failed to get pinctrl");
Wei Yongjun310eb252018-01-23 02:09:13 +00001649 ret = PTR_ERR(host->pinctrl);
Patrice Chotardf9bb3042018-01-18 15:34:20 +01001650 goto host_free;
1651 }
1652
1653 host->pins_default = pinctrl_lookup_state(host->pinctrl,
1654 PINCTRL_STATE_DEFAULT);
1655 if (IS_ERR(host->pins_default)) {
1656 dev_err(mmc_dev(mmc), "Can't select default pins\n");
Wei Yongjun310eb252018-01-23 02:09:13 +00001657 ret = PTR_ERR(host->pins_default);
Patrice Chotardf9bb3042018-01-18 15:34:20 +01001658 goto host_free;
1659 }
1660
1661 host->pins_opendrain = pinctrl_lookup_state(host->pinctrl,
1662 MMCI_PINCTRL_STATE_OPENDRAIN);
1663 if (IS_ERR(host->pins_opendrain)) {
1664 dev_err(mmc_dev(mmc), "Can't select opendrain pins\n");
Wei Yongjun310eb252018-01-23 02:09:13 +00001665 ret = PTR_ERR(host->pins_opendrain);
Patrice Chotardf9bb3042018-01-18 15:34:20 +01001666 goto host_free;
1667 }
1668 }
1669
Russell King012b7d32009-07-09 15:13:56 +01001670 host->hw_designer = amba_manf(dev);
1671 host->hw_revision = amba_rev(dev);
Linus Walleij64de0282010-02-19 01:09:10 +01001672 dev_dbg(mmc_dev(mmc), "designer ID = 0x%02x\n", host->hw_designer);
1673 dev_dbg(mmc_dev(mmc), "revision = 0x%01x\n", host->hw_revision);
Russell King012b7d32009-07-09 15:13:56 +01001674
Ulf Hansson665ba562013-05-13 15:39:17 +01001675 host->clk = devm_clk_get(&dev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 if (IS_ERR(host->clk)) {
1677 ret = PTR_ERR(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 goto host_free;
1679 }
1680
Julia Lawallac940932012-08-26 16:00:59 +00001681 ret = clk_prepare_enable(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 if (ret)
Ulf Hansson665ba562013-05-13 15:39:17 +01001683 goto host_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
Srinivas Kandagatla9c34b732014-06-02 10:10:04 +01001685 if (variant->qcom_fifo)
1686 host->get_rx_fifocnt = mmci_qcom_get_rx_fifocnt;
1687 else
1688 host->get_rx_fifocnt = mmci_get_rx_fifocnt;
1689
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 host->plat = plat;
Rabin Vincent4956e102010-07-21 12:54:40 +01001691 host->variant = variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 host->mclk = clk_get_rate(host->clk);
Linus Walleijc8df9a52008-04-29 09:34:07 +01001693 /*
1694 * According to the spec, mclk is max 100 MHz,
1695 * so we try to adjust the clock down to this,
1696 * (if possible).
1697 */
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +01001698 if (host->mclk > variant->f_max) {
1699 ret = clk_set_rate(host->clk, variant->f_max);
Linus Walleijc8df9a52008-04-29 09:34:07 +01001700 if (ret < 0)
1701 goto clk_disable;
1702 host->mclk = clk_get_rate(host->clk);
Linus Walleij64de0282010-02-19 01:09:10 +01001703 dev_dbg(mmc_dev(mmc), "eventual mclk rate: %u Hz\n",
1704 host->mclk);
Linus Walleijc8df9a52008-04-29 09:34:07 +01001705 }
Ulf Hanssonef289982014-03-17 13:56:32 +01001706
Russell Kingc8ebae32011-01-11 19:35:53 +00001707 host->phybase = dev->res.start;
Ulf Hanssonef289982014-03-17 13:56:32 +01001708 host->base = devm_ioremap_resource(&dev->dev, &dev->res);
1709 if (IS_ERR(host->base)) {
1710 ret = PTR_ERR(host->base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 goto clk_disable;
1712 }
1713
Linus Walleij7f294e42011-07-08 09:57:15 +01001714 /*
1715 * The ARM and ST versions of the block have slightly different
1716 * clock divider equations which means that the minimum divider
1717 * differs too.
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001718 * on Qualcomm like controllers get the nearest minimum clock to 100Khz
Linus Walleij7f294e42011-07-08 09:57:15 +01001719 */
1720 if (variant->st_clkdiv)
1721 mmc->f_min = DIV_ROUND_UP(host->mclk, 257);
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001722 else if (variant->explicit_mclk_control)
1723 mmc->f_min = clk_round_rate(host->clk, 100000);
Linus Walleij7f294e42011-07-08 09:57:15 +01001724 else
1725 mmc->f_min = DIV_ROUND_UP(host->mclk, 512);
Linus Walleij808d97c2010-04-08 07:39:38 +01001726 /*
Ulf Hansson78f87df2014-03-17 15:53:07 +01001727 * If no maximum operating frequency is supplied, fall back to use
1728 * the module parameter, which has a (low) default value in case it
1729 * is not specified. Either value must not exceed the clock rate into
Ulf Hansson5080a082014-03-21 10:46:39 +01001730 * the block, of course.
Linus Walleij808d97c2010-04-08 07:39:38 +01001731 */
Ulf Hansson78f87df2014-03-17 15:53:07 +01001732 if (mmc->f_max)
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001733 mmc->f_max = variant->explicit_mclk_control ?
1734 min(variant->f_max, mmc->f_max) :
1735 min(host->mclk, mmc->f_max);
Linus Walleij808d97c2010-04-08 07:39:38 +01001736 else
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001737 mmc->f_max = variant->explicit_mclk_control ?
1738 fmax : min(host->mclk, fmax);
1739
1740
Linus Walleij64de0282010-02-19 01:09:10 +01001741 dev_dbg(mmc_dev(mmc), "clocking block at %u Hz\n", mmc->f_max);
1742
Ulf Hansson599c1d52013-01-07 16:22:50 +01001743 /* Get regulators and the supported OCR mask */
Bjorn Andersson9369c972015-03-24 18:39:49 -07001744 ret = mmc_regulator_get_supply(mmc);
Wolfram Sang51006952017-10-14 21:17:14 +02001745 if (ret)
Bjorn Andersson9369c972015-03-24 18:39:49 -07001746 goto clk_disable;
1747
Ulf Hansson599c1d52013-01-07 16:22:50 +01001748 if (!mmc->ocr_avail)
Linus Walleij34e84f32009-09-22 14:41:40 +01001749 mmc->ocr_avail = plat->ocr_mask;
Ulf Hansson599c1d52013-01-07 16:22:50 +01001750 else if (plat->ocr_mask)
1751 dev_warn(mmc_dev(mmc), "Platform OCR mask is ignored\n");
1752
Ulf Hansson78f87df2014-03-17 15:53:07 +01001753 /* DT takes precedence over platform data. */
Ulf Hansson78f87df2014-03-17 15:53:07 +01001754 if (!np) {
1755 if (!plat->cd_invert)
1756 mmc->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
1757 mmc->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
1758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
Ulf Hansson9dd8a8b2014-03-19 13:54:18 +01001760 /* We support these capabilities. */
1761 mmc->caps |= MMC_CAP_CMD23;
1762
Linus Walleij49adc0c2016-10-25 11:06:06 +02001763 /*
1764 * Enable busy detection.
1765 */
Ulf Hansson8d94b542014-01-13 16:49:31 +01001766 if (variant->busy_detect) {
1767 mmci_ops.card_busy = mmci_card_busy;
Linus Walleij49adc0c2016-10-25 11:06:06 +02001768 /*
1769 * Not all variants have a flag to enable busy detection
1770 * in the DPSM, but if they do, set it here.
1771 */
1772 if (variant->busy_dpsm_flag)
1773 mmci_write_datactrlreg(host,
1774 host->variant->busy_dpsm_flag);
Ulf Hansson8d94b542014-01-13 16:49:31 +01001775 mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY;
1776 mmc->max_busy_timeout = 0;
1777 }
1778
1779 mmc->ops = &mmci_ops;
1780
Ulf Hansson70be2082013-01-07 15:35:06 +01001781 /* We support these PM capabilities. */
Ulf Hansson78f87df2014-03-17 15:53:07 +01001782 mmc->pm_caps |= MMC_PM_KEEP_POWER;
Ulf Hansson70be2082013-01-07 15:35:06 +01001783
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 /*
1785 * We can do SGIO
1786 */
Martin K. Petersena36274e2010-09-10 01:33:59 -04001787 mmc->max_segs = NR_SG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
1789 /*
Rabin Vincent08458ef2010-07-21 12:55:59 +01001790 * Since only a certain number of bits are valid in the data length
1791 * register, we must ensure that we don't exceed 2^num-1 bytes in a
1792 * single request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 */
Rabin Vincent08458ef2010-07-21 12:55:59 +01001794 mmc->max_req_size = (1 << variant->datalength_bits) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
1796 /*
1797 * Set the maximum segment size. Since we aren't doing DMA
1798 * (yet) we are only limited by the data length register.
1799 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001800 mmc->max_seg_size = mmc->max_req_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001802 /*
1803 * Block size can be up to 2048 bytes, but must be a power of two.
1804 */
Will Deacon8f7f6b72012-02-24 11:25:21 +00001805 mmc->max_blk_size = 1 << 11;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001806
Pierre Ossman55db8902006-11-21 17:55:45 +01001807 /*
Will Deacon8f7f6b72012-02-24 11:25:21 +00001808 * Limit the number of blocks transferred so that we don't overflow
1809 * the maximum request size.
Pierre Ossman55db8902006-11-21 17:55:45 +01001810 */
Will Deacon8f7f6b72012-02-24 11:25:21 +00001811 mmc->max_blk_count = mmc->max_req_size >> 11;
Pierre Ossman55db8902006-11-21 17:55:45 +01001812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 spin_lock_init(&host->lock);
1814
1815 writel(0, host->base + MMCIMASK0);
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +01001816
1817 if (variant->mmcimask1)
1818 writel(0, host->base + MMCIMASK1);
1819
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 writel(0xfff, host->base + MMCICLEAR);
1821
Linus Walleijce437aa2014-08-27 15:13:54 +02001822 /*
1823 * If:
1824 * - not using DT but using a descriptor table, or
1825 * - using a table of descriptors ALONGSIDE DT, or
1826 * look up these descriptors named "cd" and "wp" right here, fail
1827 * silently of these do not exist and proceed to try platform data
1828 */
1829 if (!np) {
Linus Walleij89168b42014-10-02 09:08:46 +02001830 ret = mmc_gpiod_request_cd(mmc, "cd", 0, false, 0, NULL);
Linus Walleijce437aa2014-08-27 15:13:54 +02001831 if (ret < 0) {
1832 if (ret == -EPROBE_DEFER)
1833 goto clk_disable;
1834 else if (gpio_is_valid(plat->gpio_cd)) {
1835 ret = mmc_gpio_request_cd(mmc, plat->gpio_cd, 0);
1836 if (ret)
1837 goto clk_disable;
1838 }
1839 }
1840
Linus Walleij89168b42014-10-02 09:08:46 +02001841 ret = mmc_gpiod_request_ro(mmc, "wp", 0, false, 0, NULL);
Linus Walleijce437aa2014-08-27 15:13:54 +02001842 if (ret < 0) {
1843 if (ret == -EPROBE_DEFER)
1844 goto clk_disable;
1845 else if (gpio_is_valid(plat->gpio_wp)) {
1846 ret = mmc_gpio_request_ro(mmc, plat->gpio_wp);
1847 if (ret)
1848 goto clk_disable;
1849 }
1850 }
Russell King89001442009-07-09 15:16:07 +01001851 }
1852
Ulf Hanssonef289982014-03-17 13:56:32 +01001853 ret = devm_request_irq(&dev->dev, dev->irq[0], mmci_irq, IRQF_SHARED,
1854 DRIVER_NAME " (cmd)", host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 if (ret)
Ulf Hanssonef289982014-03-17 13:56:32 +01001856 goto clk_disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Russell Kingdfb851852012-05-03 11:33:15 +01001858 if (!dev->irq[1])
Linus Walleij2686b4b2010-10-19 12:39:48 +01001859 host->singleirq = true;
1860 else {
Ulf Hanssonef289982014-03-17 13:56:32 +01001861 ret = devm_request_irq(&dev->dev, dev->irq[1], mmci_pio_irq,
1862 IRQF_SHARED, DRIVER_NAME " (pio)", host);
Linus Walleij2686b4b2010-10-19 12:39:48 +01001863 if (ret)
Ulf Hanssonef289982014-03-17 13:56:32 +01001864 goto clk_disable;
Linus Walleij2686b4b2010-10-19 12:39:48 +01001865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Linus Walleij8cb28152011-01-24 15:22:13 +01001867 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
1869 amba_set_drvdata(dev, mmc);
1870
Russell Kingc8ebae32011-01-11 19:35:53 +00001871 dev_info(&dev->dev, "%s: PL%03x manf %x rev%u at 0x%08llx irq %d,%d (pio)\n",
1872 mmc_hostname(mmc), amba_part(dev), amba_manf(dev),
1873 amba_rev(dev), (unsigned long long)dev->res.start,
1874 dev->irq[0], dev->irq[1]);
1875
1876 mmci_dma_setup(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
Ulf Hansson2cd976c2011-12-13 17:01:11 +01001878 pm_runtime_set_autosuspend_delay(&dev->dev, 50);
1879 pm_runtime_use_autosuspend(&dev->dev);
Russell King1c3be362011-08-14 09:17:05 +01001880
Russell King8c11a942010-12-28 19:40:40 +00001881 mmc_add_host(mmc);
1882
Ulf Hansson6f2d3c82014-12-11 14:35:55 +01001883 pm_runtime_put(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 return 0;
1885
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 clk_disable:
Julia Lawallac940932012-08-26 16:00:59 +00001887 clk_disable_unprepare(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 host_free:
1889 mmc_free_host(mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 return ret;
1891}
1892
Bill Pemberton6e0ee712012-11-19 13:26:03 -05001893static int mmci_remove(struct amba_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894{
1895 struct mmc_host *mmc = amba_get_drvdata(dev);
1896
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 if (mmc) {
1898 struct mmci_host *host = mmc_priv(mmc);
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +01001899 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
Russell King1c3be362011-08-14 09:17:05 +01001901 /*
1902 * Undo pm_runtime_put() in probe. We use the _sync
1903 * version here so that we can access the primecell.
1904 */
1905 pm_runtime_get_sync(&dev->dev);
1906
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 mmc_remove_host(mmc);
1908
1909 writel(0, host->base + MMCIMASK0);
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +01001910
1911 if (variant->mmcimask1)
1912 writel(0, host->base + MMCIMASK1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
1914 writel(0, host->base + MMCICOMMAND);
1915 writel(0, host->base + MMCIDATACTRL);
1916
Russell Kingc8ebae32011-01-11 19:35:53 +00001917 mmci_dma_release(host);
Julia Lawallac940932012-08-26 16:00:59 +00001918 clk_disable_unprepare(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 mmc_free_host(mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 }
1921
1922 return 0;
1923}
1924
Ulf Hansson571dce42014-01-23 00:38:00 +01001925#ifdef CONFIG_PM
Ulf Hansson1ff44432013-09-04 09:05:17 +01001926static void mmci_save(struct mmci_host *host)
1927{
1928 unsigned long flags;
1929
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001930 spin_lock_irqsave(&host->lock, flags);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001931
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001932 writel(0, host->base + MMCIMASK0);
1933 if (host->variant->pwrreg_nopower) {
Ulf Hansson1ff44432013-09-04 09:05:17 +01001934 writel(0, host->base + MMCIDATACTRL);
1935 writel(0, host->base + MMCIPOWER);
1936 writel(0, host->base + MMCICLOCK);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001937 }
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001938 mmci_reg_delay(host);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001939
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001940 spin_unlock_irqrestore(&host->lock, flags);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001941}
1942
1943static void mmci_restore(struct mmci_host *host)
1944{
1945 unsigned long flags;
1946
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001947 spin_lock_irqsave(&host->lock, flags);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001948
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001949 if (host->variant->pwrreg_nopower) {
Ulf Hansson1ff44432013-09-04 09:05:17 +01001950 writel(host->clk_reg, host->base + MMCICLOCK);
1951 writel(host->datactrl_reg, host->base + MMCIDATACTRL);
1952 writel(host->pwr_reg, host->base + MMCIPOWER);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001953 }
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001954 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
1955 mmci_reg_delay(host);
1956
1957 spin_unlock_irqrestore(&host->lock, flags);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001958}
1959
Ulf Hansson82592932013-01-09 11:15:26 +01001960static int mmci_runtime_suspend(struct device *dev)
1961{
1962 struct amba_device *adev = to_amba_device(dev);
1963 struct mmc_host *mmc = amba_get_drvdata(adev);
1964
1965 if (mmc) {
1966 struct mmci_host *host = mmc_priv(mmc);
Ulf Hanssone36bd9c62013-09-04 09:00:37 +01001967 pinctrl_pm_select_sleep_state(dev);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001968 mmci_save(host);
Ulf Hansson82592932013-01-09 11:15:26 +01001969 clk_disable_unprepare(host->clk);
1970 }
1971
1972 return 0;
1973}
1974
1975static int mmci_runtime_resume(struct device *dev)
1976{
1977 struct amba_device *adev = to_amba_device(dev);
1978 struct mmc_host *mmc = amba_get_drvdata(adev);
1979
1980 if (mmc) {
1981 struct mmci_host *host = mmc_priv(mmc);
1982 clk_prepare_enable(host->clk);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001983 mmci_restore(host);
Ulf Hanssone36bd9c62013-09-04 09:00:37 +01001984 pinctrl_pm_select_default_state(dev);
Ulf Hansson82592932013-01-09 11:15:26 +01001985 }
1986
1987 return 0;
1988}
1989#endif
1990
Ulf Hansson48fa7002011-12-13 16:59:34 +01001991static const struct dev_pm_ops mmci_dev_pm_ops = {
Ulf Hanssonf3737fa2014-01-23 01:11:33 +01001992 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1993 pm_runtime_force_resume)
Rafael J. Wysocki6ed23b82014-12-04 00:34:11 +01001994 SET_RUNTIME_PM_OPS(mmci_runtime_suspend, mmci_runtime_resume, NULL)
Ulf Hansson48fa7002011-12-13 16:59:34 +01001995};
1996
Arvind Yadav88411de2017-08-23 22:00:49 +05301997static const struct amba_id mmci_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 {
1999 .id = 0x00041180,
Pawel Moll768fbc12011-03-11 17:18:07 +00002000 .mask = 0xff0fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01002001 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 },
2003 {
Pawel Moll768fbc12011-03-11 17:18:07 +00002004 .id = 0x01041180,
2005 .mask = 0xff0fffff,
2006 .data = &variant_arm_extended_fifo,
2007 },
2008 {
Pawel Moll3a372982013-01-24 14:12:45 +01002009 .id = 0x02041180,
2010 .mask = 0xff0fffff,
2011 .data = &variant_arm_extended_fifo_hwfc,
2012 },
2013 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 .id = 0x00041181,
2015 .mask = 0x000fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01002016 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 },
Linus Walleijcc30d602009-01-04 15:18:54 +01002018 /* ST Micro variants */
2019 {
2020 .id = 0x00180180,
2021 .mask = 0x00ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01002022 .data = &variant_u300,
Linus Walleijcc30d602009-01-04 15:18:54 +01002023 },
2024 {
Linus Walleij34fd4212012-04-10 17:43:59 +01002025 .id = 0x10180180,
2026 .mask = 0xf0ffffff,
2027 .data = &variant_nomadik,
2028 },
2029 {
Linus Walleijcc30d602009-01-04 15:18:54 +01002030 .id = 0x00280180,
2031 .mask = 0x00ffffff,
Linus Walleij0bcb7ef2016-01-04 02:21:55 +01002032 .data = &variant_nomadik,
Rabin Vincent4956e102010-07-21 12:54:40 +01002033 },
2034 {
2035 .id = 0x00480180,
Philippe Langlais1784b152011-03-25 08:51:52 +01002036 .mask = 0xf0ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01002037 .data = &variant_ux500,
Linus Walleijcc30d602009-01-04 15:18:54 +01002038 },
Philippe Langlais1784b152011-03-25 08:51:52 +01002039 {
2040 .id = 0x10480180,
2041 .mask = 0xf0ffffff,
2042 .data = &variant_ux500v2,
2043 },
Patrice Chotard2a9d6c82018-01-18 15:34:21 +01002044 {
2045 .id = 0x00880180,
2046 .mask = 0x00ffffff,
2047 .data = &variant_stm32,
2048 },
Srinivas Kandagatla55b604a2014-06-02 10:10:13 +01002049 /* Qualcomm variants */
2050 {
2051 .id = 0x00051180,
2052 .mask = 0x000fffff,
2053 .data = &variant_qcom,
2054 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 { 0, 0 },
2056};
2057
Dave Martin9f998352011-10-05 15:15:21 +01002058MODULE_DEVICE_TABLE(amba, mmci_ids);
2059
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060static struct amba_driver mmci_driver = {
2061 .drv = {
2062 .name = DRIVER_NAME,
Ulf Hansson48fa7002011-12-13 16:59:34 +01002063 .pm = &mmci_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 },
2065 .probe = mmci_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05002066 .remove = mmci_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 .id_table = mmci_ids,
2068};
2069
viresh kumar9e5ed092012-03-15 10:40:38 +01002070module_amba_driver(mmci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072module_param(fmax, uint, 0444);
2073
2074MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
2075MODULE_LICENSE("GPL");