blob: f1849775e47e2726ef2addb357016418a246469b [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;
1256 u32 status;
1257
1258 status = readl(base + MMCISTATUS);
1259
Linus Walleij64de0282010-02-19 01:09:10 +01001260 dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
1262 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 unsigned int remain, len;
1264 char *buffer;
1265
1266 /*
1267 * For write, we only need to test the half-empty flag
1268 * here - if the FIFO is completely empty, then by
1269 * definition it is more than half empty.
1270 *
1271 * For read, check for data available.
1272 */
1273 if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL)))
1274 break;
1275
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001276 if (!sg_miter_next(sg_miter))
1277 break;
1278
1279 buffer = sg_miter->addr;
1280 remain = sg_miter->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282 len = 0;
1283 if (status & MCI_RXACTIVE)
1284 len = mmci_pio_read(host, buffer, remain);
1285 if (status & MCI_TXACTIVE)
1286 len = mmci_pio_write(host, buffer, remain, status);
1287
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001288 sg_miter->consumed = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 host->size -= len;
1291 remain -= len;
1292
1293 if (remain)
1294 break;
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 status = readl(base + MMCISTATUS);
1297 } while (1);
1298
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001299 sg_miter_stop(sg_miter);
1300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 /*
Russell Kingc4d877c2011-01-27 09:50:13 +00001302 * If we have less than the fifo 'half-full' threshold to transfer,
1303 * trigger a PIO interrupt as soon as any data is available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 */
Russell Kingc4d877c2011-01-27 09:50:13 +00001305 if (status & MCI_RXACTIVE && host->size < variant->fifohalfsize)
Linus Walleij2686b4b2010-10-19 12:39:48 +01001306 mmci_set_mask1(host, MCI_RXDATAAVLBLMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
1308 /*
1309 * If we run out of data, disable the data IRQs; this
1310 * prevents a race where the FIFO becomes empty before
1311 * the chip itself has disabled the data path, and
1312 * stops us racing with our data end IRQ.
1313 */
1314 if (host->size == 0) {
Linus Walleij2686b4b2010-10-19 12:39:48 +01001315 mmci_set_mask1(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0);
1317 }
1318
1319 return IRQ_HANDLED;
1320}
1321
1322/*
1323 * Handle completion of command and data transfers.
1324 */
David Howells7d12e782006-10-05 14:55:46 +01001325static irqreturn_t mmci_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
1327 struct mmci_host *host = dev_id;
1328 u32 status;
1329 int ret = 0;
1330
1331 spin_lock(&host->lock);
1332
1333 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 status = readl(host->base + MMCISTATUS);
Linus Walleij2686b4b2010-10-19 12:39:48 +01001335
1336 if (host->singleirq) {
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +01001337 if (status & host->mask1_reg)
Linus Walleij2686b4b2010-10-19 12:39:48 +01001338 mmci_pio_irq(irq, dev_id);
1339
1340 status &= ~MCI_IRQ1MASK;
1341 }
1342
Ulf Hansson8d94b542014-01-13 16:49:31 +01001343 /*
Jean-Nicolas Graux5cad24d2017-02-07 12:12:41 +01001344 * We intentionally clear the MCI_ST_CARDBUSY IRQ (if it's
1345 * enabled) in mmci_cmd_irq() function where ST Micro busy
1346 * detection variant is handled. Considering the HW seems to be
1347 * triggering the IRQ on both edges while monitoring DAT0 for
1348 * busy completion and that same status bit is used to monitor
1349 * start and end of busy detection, special care must be taken
1350 * to make sure that both start and end interrupts are always
1351 * cleared one after the other.
Ulf Hansson8d94b542014-01-13 16:49:31 +01001352 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 status &= readl(host->base + MMCIMASK0);
Jean-Nicolas Graux5cad24d2017-02-07 12:12:41 +01001354 if (host->variant->busy_detect)
1355 writel(status & ~host->variant->busy_detect_mask,
1356 host->base + MMCICLEAR);
1357 else
1358 writel(status, host->base + MMCICLEAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Linus Walleij64de0282010-02-19 01:09:10 +01001360 dev_dbg(mmc_dev(host->mmc), "irq0 (data+cmd) %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Ulf Hansson78782892014-06-13 13:21:38 +02001362 if (host->variant->reversed_irq_handling) {
1363 mmci_data_irq(host, host->data, status);
1364 mmci_cmd_irq(host, host->cmd, status);
1365 } else {
1366 mmci_cmd_irq(host, host->cmd, status);
1367 mmci_data_irq(host, host->data, status);
1368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Linus Walleij49adc0c2016-10-25 11:06:06 +02001370 /*
1371 * Don't poll for busy completion in irq context.
1372 */
1373 if (host->variant->busy_detect && host->busy_status)
1374 status &= ~host->variant->busy_detect_flag;
Ulf Hansson8d94b542014-01-13 16:49:31 +01001375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 ret = 1;
1377 } while (status);
1378
1379 spin_unlock(&host->lock);
1380
1381 return IRQ_RETVAL(ret);
1382}
1383
1384static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1385{
1386 struct mmci_host *host = mmc_priv(mmc);
Linus Walleij9e943022008-10-24 21:17:50 +01001387 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
1389 WARN_ON(host->mrq != NULL);
1390
Ulf Hansson653a7612013-01-21 21:29:34 +01001391 mrq->cmd->error = mmci_validate_data(host, mrq->data);
1392 if (mrq->cmd->error) {
Pierre Ossman255d01a2007-07-24 20:38:53 +02001393 mmc_request_done(mmc, mrq);
1394 return;
1395 }
1396
Linus Walleij9e943022008-10-24 21:17:50 +01001397 spin_lock_irqsave(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399 host->mrq = mrq;
1400
Per Forlin58c7ccb2011-07-01 18:55:24 +02001401 if (mrq->data)
1402 mmci_get_next_data(host, mrq->data);
1403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 if (mrq->data && mrq->data->flags & MMC_DATA_READ)
1405 mmci_start_data(host, mrq->data);
1406
Ulf Hansson024629c2013-05-13 15:40:56 +01001407 if (mrq->sbc)
1408 mmci_start_command(host, mrq->sbc, 0);
1409 else
1410 mmci_start_command(host, mrq->cmd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Linus Walleij9e943022008-10-24 21:17:50 +01001412 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413}
1414
1415static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1416{
1417 struct mmci_host *host = mmc_priv(mmc);
Ulf Hansson7d72a1d2011-12-13 16:54:55 +01001418 struct variant_data *variant = host->variant;
Linus Walleija6a64642009-09-14 12:56:14 +01001419 u32 pwr = 0;
1420 unsigned long flags;
Lee Jonesdb90f912013-05-03 12:52:12 +01001421 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Ulf Hanssonbc521812011-12-13 16:57:55 +01001423 if (host->plat->ios_handler &&
1424 host->plat->ios_handler(mmc_dev(mmc), ios))
1425 dev_err(mmc_dev(mmc), "platform ios_handler failed\n");
1426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 switch (ios->power_mode) {
1428 case MMC_POWER_OFF:
Ulf Hansson599c1d52013-01-07 16:22:50 +01001429 if (!IS_ERR(mmc->supply.vmmc))
1430 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Lee Jones237fb5e2013-01-31 11:27:52 +00001431
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001432 if (!IS_ERR(mmc->supply.vqmmc) && host->vqmmc_enabled) {
Lee Jones237fb5e2013-01-31 11:27:52 +00001433 regulator_disable(mmc->supply.vqmmc);
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001434 host->vqmmc_enabled = false;
1435 }
Lee Jones237fb5e2013-01-31 11:27:52 +00001436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 break;
1438 case MMC_POWER_UP:
Ulf Hansson599c1d52013-01-07 16:22:50 +01001439 if (!IS_ERR(mmc->supply.vmmc))
1440 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd);
1441
Ulf Hansson7d72a1d2011-12-13 16:54:55 +01001442 /*
1443 * The ST Micro variant doesn't have the PL180s MCI_PWR_UP
1444 * and instead uses MCI_PWR_ON so apply whatever value is
1445 * configured in the variant data.
1446 */
1447 pwr |= variant->pwrreg_powerup;
1448
1449 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 case MMC_POWER_ON:
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001451 if (!IS_ERR(mmc->supply.vqmmc) && !host->vqmmc_enabled) {
Lee Jonesdb90f912013-05-03 12:52:12 +01001452 ret = regulator_enable(mmc->supply.vqmmc);
1453 if (ret < 0)
1454 dev_err(mmc_dev(mmc),
1455 "failed to enable vqmmc regulator\n");
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001456 else
1457 host->vqmmc_enabled = true;
Lee Jonesdb90f912013-05-03 12:52:12 +01001458 }
Lee Jones237fb5e2013-01-31 11:27:52 +00001459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 pwr |= MCI_PWR_ON;
1461 break;
1462 }
1463
Ulf Hansson4d1a3a02011-12-13 16:57:07 +01001464 if (variant->signal_direction && ios->power_mode != MMC_POWER_OFF) {
1465 /*
1466 * The ST Micro variant has some additional bits
1467 * indicating signal direction for the signals in
1468 * the SD/MMC bus and feedback-clock usage.
1469 */
Ulf Hansson4593df22014-03-21 10:13:05 +01001470 pwr |= host->pwr_reg_add;
Ulf Hansson4d1a3a02011-12-13 16:57:07 +01001471
1472 if (ios->bus_width == MMC_BUS_WIDTH_4)
1473 pwr &= ~MCI_ST_DATA74DIREN;
1474 else if (ios->bus_width == MMC_BUS_WIDTH_1)
1475 pwr &= (~MCI_ST_DATA74DIREN &
1476 ~MCI_ST_DATA31DIREN &
1477 ~MCI_ST_DATA2DIREN);
1478 }
1479
Patrice Chotardf9bb3042018-01-18 15:34:20 +01001480 if (variant->opendrain) {
1481 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
1482 pwr |= variant->opendrain;
1483 } else {
1484 /*
1485 * If the variant cannot configure the pads by its own, then we
1486 * expect the pinctrl to be able to do that for us
1487 */
1488 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
1489 pinctrl_select_state(host->pinctrl, host->pins_opendrain);
1490 else
1491 pinctrl_select_state(host->pinctrl, host->pins_default);
1492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Ulf Hanssonf4670da2013-01-09 17:19:54 +01001494 /*
1495 * If clock = 0 and the variant requires the MMCIPOWER to be used for
1496 * gating the clock, the MCI_PWR_ON bit is cleared.
1497 */
1498 if (!ios->clock && variant->pwrreg_clkgate)
1499 pwr &= ~MCI_PWR_ON;
1500
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001501 if (host->variant->explicit_mclk_control &&
1502 ios->clock != host->clock_cache) {
1503 ret = clk_set_rate(host->clk, ios->clock);
1504 if (ret < 0)
1505 dev_err(mmc_dev(host->mmc),
1506 "Error setting clock rate (%d)\n", ret);
1507 else
1508 host->mclk = clk_get_rate(host->clk);
1509 }
1510 host->clock_cache = ios->clock;
1511
Linus Walleija6a64642009-09-14 12:56:14 +01001512 spin_lock_irqsave(&host->lock, flags);
1513
1514 mmci_set_clkreg(host, ios->clock);
Ulf Hansson7437cfa2012-01-18 09:17:27 +01001515 mmci_write_pwrreg(host, pwr);
Ulf Hanssonf829c042013-09-04 09:01:15 +01001516 mmci_reg_delay(host);
Linus Walleija6a64642009-09-14 12:56:14 +01001517
1518 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519}
1520
Russell King89001442009-07-09 15:16:07 +01001521static int mmci_get_cd(struct mmc_host *mmc)
1522{
1523 struct mmci_host *host = mmc_priv(mmc);
Rabin Vincent29719442010-08-09 12:54:43 +01001524 struct mmci_platform_data *plat = host->plat;
Ulf Hanssond2762092014-03-17 13:56:19 +01001525 unsigned int status = mmc_gpio_get_cd(mmc);
Russell King89001442009-07-09 15:16:07 +01001526
Ulf Hanssond2762092014-03-17 13:56:19 +01001527 if (status == -ENOSYS) {
Rabin Vincent4b8caec2010-08-09 12:56:40 +01001528 if (!plat->status)
1529 return 1; /* Assume always present */
1530
Rabin Vincent29719442010-08-09 12:54:43 +01001531 status = plat->status(mmc_dev(host->mmc));
Ulf Hanssond2762092014-03-17 13:56:19 +01001532 }
Russell King74bc8092010-07-29 15:58:59 +01001533 return status;
Russell King89001442009-07-09 15:16:07 +01001534}
1535
Ulf Hansson0f3ed7f2013-05-15 20:47:33 +01001536static int mmci_sig_volt_switch(struct mmc_host *mmc, struct mmc_ios *ios)
1537{
1538 int ret = 0;
1539
1540 if (!IS_ERR(mmc->supply.vqmmc)) {
1541
Ulf Hansson0f3ed7f2013-05-15 20:47:33 +01001542 switch (ios->signal_voltage) {
1543 case MMC_SIGNAL_VOLTAGE_330:
1544 ret = regulator_set_voltage(mmc->supply.vqmmc,
1545 2700000, 3600000);
1546 break;
1547 case MMC_SIGNAL_VOLTAGE_180:
1548 ret = regulator_set_voltage(mmc->supply.vqmmc,
1549 1700000, 1950000);
1550 break;
1551 case MMC_SIGNAL_VOLTAGE_120:
1552 ret = regulator_set_voltage(mmc->supply.vqmmc,
1553 1100000, 1300000);
1554 break;
1555 }
1556
1557 if (ret)
1558 dev_warn(mmc_dev(mmc), "Voltage switch failed\n");
Ulf Hansson0f3ed7f2013-05-15 20:47:33 +01001559 }
1560
1561 return ret;
1562}
1563
Ulf Hansson01259622013-05-15 20:53:22 +01001564static struct mmc_host_ops mmci_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 .request = mmci_request,
Per Forlin58c7ccb2011-07-01 18:55:24 +02001566 .pre_req = mmci_pre_request,
1567 .post_req = mmci_post_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 .set_ios = mmci_set_ios,
Ulf Hanssond2762092014-03-17 13:56:19 +01001569 .get_ro = mmc_gpio_get_ro,
Russell King89001442009-07-09 15:16:07 +01001570 .get_cd = mmci_get_cd,
Ulf Hansson0f3ed7f2013-05-15 20:47:33 +01001571 .start_signal_voltage_switch = mmci_sig_volt_switch,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572};
1573
Ulf Hansson78f87df2014-03-17 15:53:07 +01001574static int mmci_of_parse(struct device_node *np, struct mmc_host *mmc)
1575{
Ulf Hansson4593df22014-03-21 10:13:05 +01001576 struct mmci_host *host = mmc_priv(mmc);
Ulf Hansson78f87df2014-03-17 15:53:07 +01001577 int ret = mmc_of_parse(mmc);
Lee Jones000bc9d2012-04-16 10:18:43 +01001578
Ulf Hansson78f87df2014-03-17 15:53:07 +01001579 if (ret)
1580 return ret;
Lee Jones000bc9d2012-04-16 10:18:43 +01001581
Ulf Hansson4593df22014-03-21 10:13:05 +01001582 if (of_get_property(np, "st,sig-dir-dat0", NULL))
1583 host->pwr_reg_add |= MCI_ST_DATA0DIREN;
1584 if (of_get_property(np, "st,sig-dir-dat2", NULL))
1585 host->pwr_reg_add |= MCI_ST_DATA2DIREN;
1586 if (of_get_property(np, "st,sig-dir-dat31", NULL))
1587 host->pwr_reg_add |= MCI_ST_DATA31DIREN;
1588 if (of_get_property(np, "st,sig-dir-dat74", NULL))
1589 host->pwr_reg_add |= MCI_ST_DATA74DIREN;
1590 if (of_get_property(np, "st,sig-dir-cmd", NULL))
1591 host->pwr_reg_add |= MCI_ST_CMDDIREN;
1592 if (of_get_property(np, "st,sig-pin-fbclk", NULL))
1593 host->pwr_reg_add |= MCI_ST_FBCLKEN;
1594
Lee Jones000bc9d2012-04-16 10:18:43 +01001595 if (of_get_property(np, "mmc-cap-mmc-highspeed", NULL))
Ulf Hansson78f87df2014-03-17 15:53:07 +01001596 mmc->caps |= MMC_CAP_MMC_HIGHSPEED;
Lee Jones000bc9d2012-04-16 10:18:43 +01001597 if (of_get_property(np, "mmc-cap-sd-highspeed", NULL))
Ulf Hansson78f87df2014-03-17 15:53:07 +01001598 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
Lee Jones000bc9d2012-04-16 10:18:43 +01001599
Ulf Hansson78f87df2014-03-17 15:53:07 +01001600 return 0;
Lee Jones000bc9d2012-04-16 10:18:43 +01001601}
Lee Jones000bc9d2012-04-16 10:18:43 +01001602
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001603static int mmci_probe(struct amba_device *dev,
Russell Kingaa25afa2011-02-19 15:55:00 +00001604 const struct amba_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605{
Linus Walleij6ef297f2009-09-22 14:29:36 +01001606 struct mmci_platform_data *plat = dev->dev.platform_data;
Lee Jones000bc9d2012-04-16 10:18:43 +01001607 struct device_node *np = dev->dev.of_node;
Rabin Vincent4956e102010-07-21 12:54:40 +01001608 struct variant_data *variant = id->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 struct mmci_host *host;
1610 struct mmc_host *mmc;
1611 int ret;
1612
Lee Jones000bc9d2012-04-16 10:18:43 +01001613 /* Must have platform data or Device Tree. */
1614 if (!plat && !np) {
1615 dev_err(&dev->dev, "No plat data or DT found\n");
1616 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 }
1618
Lee Jonesb9b52912012-06-12 10:49:51 +01001619 if (!plat) {
1620 plat = devm_kzalloc(&dev->dev, sizeof(*plat), GFP_KERNEL);
1621 if (!plat)
1622 return -ENOMEM;
1623 }
1624
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
Ulf Hanssonef289982014-03-17 13:56:32 +01001626 if (!mmc)
1627 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
Ulf Hansson78f87df2014-03-17 15:53:07 +01001629 ret = mmci_of_parse(np, mmc);
1630 if (ret)
1631 goto host_free;
1632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 host = mmc_priv(mmc);
Rabin Vincent4ea580f2009-04-17 08:44:19 +05301634 host->mmc = mmc;
Russell King012b7d32009-07-09 15:13:56 +01001635
Patrice Chotardf9bb3042018-01-18 15:34:20 +01001636 /*
1637 * Some variant (STM32) doesn't have opendrain bit, nevertheless
1638 * pins can be set accordingly using pinctrl
1639 */
1640 if (!variant->opendrain) {
1641 host->pinctrl = devm_pinctrl_get(&dev->dev);
1642 if (IS_ERR(host->pinctrl)) {
1643 dev_err(&dev->dev, "failed to get pinctrl");
Wei Yongjun310eb252018-01-23 02:09:13 +00001644 ret = PTR_ERR(host->pinctrl);
Patrice Chotardf9bb3042018-01-18 15:34:20 +01001645 goto host_free;
1646 }
1647
1648 host->pins_default = pinctrl_lookup_state(host->pinctrl,
1649 PINCTRL_STATE_DEFAULT);
1650 if (IS_ERR(host->pins_default)) {
1651 dev_err(mmc_dev(mmc), "Can't select default pins\n");
Wei Yongjun310eb252018-01-23 02:09:13 +00001652 ret = PTR_ERR(host->pins_default);
Patrice Chotardf9bb3042018-01-18 15:34:20 +01001653 goto host_free;
1654 }
1655
1656 host->pins_opendrain = pinctrl_lookup_state(host->pinctrl,
1657 MMCI_PINCTRL_STATE_OPENDRAIN);
1658 if (IS_ERR(host->pins_opendrain)) {
1659 dev_err(mmc_dev(mmc), "Can't select opendrain pins\n");
Wei Yongjun310eb252018-01-23 02:09:13 +00001660 ret = PTR_ERR(host->pins_opendrain);
Patrice Chotardf9bb3042018-01-18 15:34:20 +01001661 goto host_free;
1662 }
1663 }
1664
Russell King012b7d32009-07-09 15:13:56 +01001665 host->hw_designer = amba_manf(dev);
1666 host->hw_revision = amba_rev(dev);
Linus Walleij64de0282010-02-19 01:09:10 +01001667 dev_dbg(mmc_dev(mmc), "designer ID = 0x%02x\n", host->hw_designer);
1668 dev_dbg(mmc_dev(mmc), "revision = 0x%01x\n", host->hw_revision);
Russell King012b7d32009-07-09 15:13:56 +01001669
Ulf Hansson665ba562013-05-13 15:39:17 +01001670 host->clk = devm_clk_get(&dev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 if (IS_ERR(host->clk)) {
1672 ret = PTR_ERR(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 goto host_free;
1674 }
1675
Julia Lawallac940932012-08-26 16:00:59 +00001676 ret = clk_prepare_enable(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 if (ret)
Ulf Hansson665ba562013-05-13 15:39:17 +01001678 goto host_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Srinivas Kandagatla9c34b732014-06-02 10:10:04 +01001680 if (variant->qcom_fifo)
1681 host->get_rx_fifocnt = mmci_qcom_get_rx_fifocnt;
1682 else
1683 host->get_rx_fifocnt = mmci_get_rx_fifocnt;
1684
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 host->plat = plat;
Rabin Vincent4956e102010-07-21 12:54:40 +01001686 host->variant = variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 host->mclk = clk_get_rate(host->clk);
Linus Walleijc8df9a52008-04-29 09:34:07 +01001688 /*
1689 * According to the spec, mclk is max 100 MHz,
1690 * so we try to adjust the clock down to this,
1691 * (if possible).
1692 */
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +01001693 if (host->mclk > variant->f_max) {
1694 ret = clk_set_rate(host->clk, variant->f_max);
Linus Walleijc8df9a52008-04-29 09:34:07 +01001695 if (ret < 0)
1696 goto clk_disable;
1697 host->mclk = clk_get_rate(host->clk);
Linus Walleij64de0282010-02-19 01:09:10 +01001698 dev_dbg(mmc_dev(mmc), "eventual mclk rate: %u Hz\n",
1699 host->mclk);
Linus Walleijc8df9a52008-04-29 09:34:07 +01001700 }
Ulf Hanssonef289982014-03-17 13:56:32 +01001701
Russell Kingc8ebae32011-01-11 19:35:53 +00001702 host->phybase = dev->res.start;
Ulf Hanssonef289982014-03-17 13:56:32 +01001703 host->base = devm_ioremap_resource(&dev->dev, &dev->res);
1704 if (IS_ERR(host->base)) {
1705 ret = PTR_ERR(host->base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 goto clk_disable;
1707 }
1708
Linus Walleij7f294e42011-07-08 09:57:15 +01001709 /*
1710 * The ARM and ST versions of the block have slightly different
1711 * clock divider equations which means that the minimum divider
1712 * differs too.
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001713 * on Qualcomm like controllers get the nearest minimum clock to 100Khz
Linus Walleij7f294e42011-07-08 09:57:15 +01001714 */
1715 if (variant->st_clkdiv)
1716 mmc->f_min = DIV_ROUND_UP(host->mclk, 257);
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001717 else if (variant->explicit_mclk_control)
1718 mmc->f_min = clk_round_rate(host->clk, 100000);
Linus Walleij7f294e42011-07-08 09:57:15 +01001719 else
1720 mmc->f_min = DIV_ROUND_UP(host->mclk, 512);
Linus Walleij808d97c2010-04-08 07:39:38 +01001721 /*
Ulf Hansson78f87df2014-03-17 15:53:07 +01001722 * If no maximum operating frequency is supplied, fall back to use
1723 * the module parameter, which has a (low) default value in case it
1724 * is not specified. Either value must not exceed the clock rate into
Ulf Hansson5080a082014-03-21 10:46:39 +01001725 * the block, of course.
Linus Walleij808d97c2010-04-08 07:39:38 +01001726 */
Ulf Hansson78f87df2014-03-17 15:53:07 +01001727 if (mmc->f_max)
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001728 mmc->f_max = variant->explicit_mclk_control ?
1729 min(variant->f_max, mmc->f_max) :
1730 min(host->mclk, mmc->f_max);
Linus Walleij808d97c2010-04-08 07:39:38 +01001731 else
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001732 mmc->f_max = variant->explicit_mclk_control ?
1733 fmax : min(host->mclk, fmax);
1734
1735
Linus Walleij64de0282010-02-19 01:09:10 +01001736 dev_dbg(mmc_dev(mmc), "clocking block at %u Hz\n", mmc->f_max);
1737
Ulf Hansson599c1d52013-01-07 16:22:50 +01001738 /* Get regulators and the supported OCR mask */
Bjorn Andersson9369c972015-03-24 18:39:49 -07001739 ret = mmc_regulator_get_supply(mmc);
Wolfram Sang51006952017-10-14 21:17:14 +02001740 if (ret)
Bjorn Andersson9369c972015-03-24 18:39:49 -07001741 goto clk_disable;
1742
Ulf Hansson599c1d52013-01-07 16:22:50 +01001743 if (!mmc->ocr_avail)
Linus Walleij34e84f32009-09-22 14:41:40 +01001744 mmc->ocr_avail = plat->ocr_mask;
Ulf Hansson599c1d52013-01-07 16:22:50 +01001745 else if (plat->ocr_mask)
1746 dev_warn(mmc_dev(mmc), "Platform OCR mask is ignored\n");
1747
Ulf Hansson78f87df2014-03-17 15:53:07 +01001748 /* DT takes precedence over platform data. */
Ulf Hansson78f87df2014-03-17 15:53:07 +01001749 if (!np) {
1750 if (!plat->cd_invert)
1751 mmc->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
1752 mmc->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
1753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Ulf Hansson9dd8a8b2014-03-19 13:54:18 +01001755 /* We support these capabilities. */
1756 mmc->caps |= MMC_CAP_CMD23;
1757
Linus Walleij49adc0c2016-10-25 11:06:06 +02001758 /*
1759 * Enable busy detection.
1760 */
Ulf Hansson8d94b542014-01-13 16:49:31 +01001761 if (variant->busy_detect) {
1762 mmci_ops.card_busy = mmci_card_busy;
Linus Walleij49adc0c2016-10-25 11:06:06 +02001763 /*
1764 * Not all variants have a flag to enable busy detection
1765 * in the DPSM, but if they do, set it here.
1766 */
1767 if (variant->busy_dpsm_flag)
1768 mmci_write_datactrlreg(host,
1769 host->variant->busy_dpsm_flag);
Ulf Hansson8d94b542014-01-13 16:49:31 +01001770 mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY;
1771 mmc->max_busy_timeout = 0;
1772 }
1773
1774 mmc->ops = &mmci_ops;
1775
Ulf Hansson70be2082013-01-07 15:35:06 +01001776 /* We support these PM capabilities. */
Ulf Hansson78f87df2014-03-17 15:53:07 +01001777 mmc->pm_caps |= MMC_PM_KEEP_POWER;
Ulf Hansson70be2082013-01-07 15:35:06 +01001778
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 /*
1780 * We can do SGIO
1781 */
Martin K. Petersena36274e2010-09-10 01:33:59 -04001782 mmc->max_segs = NR_SG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
1784 /*
Rabin Vincent08458ef2010-07-21 12:55:59 +01001785 * Since only a certain number of bits are valid in the data length
1786 * register, we must ensure that we don't exceed 2^num-1 bytes in a
1787 * single request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 */
Rabin Vincent08458ef2010-07-21 12:55:59 +01001789 mmc->max_req_size = (1 << variant->datalength_bits) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
1791 /*
1792 * Set the maximum segment size. Since we aren't doing DMA
1793 * (yet) we are only limited by the data length register.
1794 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001795 mmc->max_seg_size = mmc->max_req_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001797 /*
1798 * Block size can be up to 2048 bytes, but must be a power of two.
1799 */
Will Deacon8f7f6b72012-02-24 11:25:21 +00001800 mmc->max_blk_size = 1 << 11;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001801
Pierre Ossman55db8902006-11-21 17:55:45 +01001802 /*
Will Deacon8f7f6b72012-02-24 11:25:21 +00001803 * Limit the number of blocks transferred so that we don't overflow
1804 * the maximum request size.
Pierre Ossman55db8902006-11-21 17:55:45 +01001805 */
Will Deacon8f7f6b72012-02-24 11:25:21 +00001806 mmc->max_blk_count = mmc->max_req_size >> 11;
Pierre Ossman55db8902006-11-21 17:55:45 +01001807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 spin_lock_init(&host->lock);
1809
1810 writel(0, host->base + MMCIMASK0);
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +01001811
1812 if (variant->mmcimask1)
1813 writel(0, host->base + MMCIMASK1);
1814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 writel(0xfff, host->base + MMCICLEAR);
1816
Linus Walleijce437aa2014-08-27 15:13:54 +02001817 /*
1818 * If:
1819 * - not using DT but using a descriptor table, or
1820 * - using a table of descriptors ALONGSIDE DT, or
1821 * look up these descriptors named "cd" and "wp" right here, fail
1822 * silently of these do not exist and proceed to try platform data
1823 */
1824 if (!np) {
Linus Walleij89168b42014-10-02 09:08:46 +02001825 ret = mmc_gpiod_request_cd(mmc, "cd", 0, false, 0, NULL);
Linus Walleijce437aa2014-08-27 15:13:54 +02001826 if (ret < 0) {
1827 if (ret == -EPROBE_DEFER)
1828 goto clk_disable;
1829 else if (gpio_is_valid(plat->gpio_cd)) {
1830 ret = mmc_gpio_request_cd(mmc, plat->gpio_cd, 0);
1831 if (ret)
1832 goto clk_disable;
1833 }
1834 }
1835
Linus Walleij89168b42014-10-02 09:08:46 +02001836 ret = mmc_gpiod_request_ro(mmc, "wp", 0, false, 0, NULL);
Linus Walleijce437aa2014-08-27 15:13:54 +02001837 if (ret < 0) {
1838 if (ret == -EPROBE_DEFER)
1839 goto clk_disable;
1840 else if (gpio_is_valid(plat->gpio_wp)) {
1841 ret = mmc_gpio_request_ro(mmc, plat->gpio_wp);
1842 if (ret)
1843 goto clk_disable;
1844 }
1845 }
Russell King89001442009-07-09 15:16:07 +01001846 }
1847
Ulf Hanssonef289982014-03-17 13:56:32 +01001848 ret = devm_request_irq(&dev->dev, dev->irq[0], mmci_irq, IRQF_SHARED,
1849 DRIVER_NAME " (cmd)", host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 if (ret)
Ulf Hanssonef289982014-03-17 13:56:32 +01001851 goto clk_disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
Russell Kingdfb851852012-05-03 11:33:15 +01001853 if (!dev->irq[1])
Linus Walleij2686b4b2010-10-19 12:39:48 +01001854 host->singleirq = true;
1855 else {
Ulf Hanssonef289982014-03-17 13:56:32 +01001856 ret = devm_request_irq(&dev->dev, dev->irq[1], mmci_pio_irq,
1857 IRQF_SHARED, DRIVER_NAME " (pio)", host);
Linus Walleij2686b4b2010-10-19 12:39:48 +01001858 if (ret)
Ulf Hanssonef289982014-03-17 13:56:32 +01001859 goto clk_disable;
Linus Walleij2686b4b2010-10-19 12:39:48 +01001860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
Linus Walleij8cb28152011-01-24 15:22:13 +01001862 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
1864 amba_set_drvdata(dev, mmc);
1865
Russell Kingc8ebae32011-01-11 19:35:53 +00001866 dev_info(&dev->dev, "%s: PL%03x manf %x rev%u at 0x%08llx irq %d,%d (pio)\n",
1867 mmc_hostname(mmc), amba_part(dev), amba_manf(dev),
1868 amba_rev(dev), (unsigned long long)dev->res.start,
1869 dev->irq[0], dev->irq[1]);
1870
1871 mmci_dma_setup(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
Ulf Hansson2cd976c2011-12-13 17:01:11 +01001873 pm_runtime_set_autosuspend_delay(&dev->dev, 50);
1874 pm_runtime_use_autosuspend(&dev->dev);
Russell King1c3be362011-08-14 09:17:05 +01001875
Russell King8c11a942010-12-28 19:40:40 +00001876 mmc_add_host(mmc);
1877
Ulf Hansson6f2d3c82014-12-11 14:35:55 +01001878 pm_runtime_put(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 return 0;
1880
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 clk_disable:
Julia Lawallac940932012-08-26 16:00:59 +00001882 clk_disable_unprepare(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 host_free:
1884 mmc_free_host(mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 return ret;
1886}
1887
Bill Pemberton6e0ee712012-11-19 13:26:03 -05001888static int mmci_remove(struct amba_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889{
1890 struct mmc_host *mmc = amba_get_drvdata(dev);
1891
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 if (mmc) {
1893 struct mmci_host *host = mmc_priv(mmc);
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +01001894 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Russell King1c3be362011-08-14 09:17:05 +01001896 /*
1897 * Undo pm_runtime_put() in probe. We use the _sync
1898 * version here so that we can access the primecell.
1899 */
1900 pm_runtime_get_sync(&dev->dev);
1901
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 mmc_remove_host(mmc);
1903
1904 writel(0, host->base + MMCIMASK0);
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +01001905
1906 if (variant->mmcimask1)
1907 writel(0, host->base + MMCIMASK1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
1909 writel(0, host->base + MMCICOMMAND);
1910 writel(0, host->base + MMCIDATACTRL);
1911
Russell Kingc8ebae32011-01-11 19:35:53 +00001912 mmci_dma_release(host);
Julia Lawallac940932012-08-26 16:00:59 +00001913 clk_disable_unprepare(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 mmc_free_host(mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 }
1916
1917 return 0;
1918}
1919
Ulf Hansson571dce42014-01-23 00:38:00 +01001920#ifdef CONFIG_PM
Ulf Hansson1ff44432013-09-04 09:05:17 +01001921static void mmci_save(struct mmci_host *host)
1922{
1923 unsigned long flags;
1924
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001925 spin_lock_irqsave(&host->lock, flags);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001926
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001927 writel(0, host->base + MMCIMASK0);
1928 if (host->variant->pwrreg_nopower) {
Ulf Hansson1ff44432013-09-04 09:05:17 +01001929 writel(0, host->base + MMCIDATACTRL);
1930 writel(0, host->base + MMCIPOWER);
1931 writel(0, host->base + MMCICLOCK);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001932 }
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001933 mmci_reg_delay(host);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001934
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001935 spin_unlock_irqrestore(&host->lock, flags);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001936}
1937
1938static void mmci_restore(struct mmci_host *host)
1939{
1940 unsigned long flags;
1941
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001942 spin_lock_irqsave(&host->lock, flags);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001943
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001944 if (host->variant->pwrreg_nopower) {
Ulf Hansson1ff44432013-09-04 09:05:17 +01001945 writel(host->clk_reg, host->base + MMCICLOCK);
1946 writel(host->datactrl_reg, host->base + MMCIDATACTRL);
1947 writel(host->pwr_reg, host->base + MMCIPOWER);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001948 }
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01001949 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
1950 mmci_reg_delay(host);
1951
1952 spin_unlock_irqrestore(&host->lock, flags);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001953}
1954
Ulf Hansson82592932013-01-09 11:15:26 +01001955static int mmci_runtime_suspend(struct device *dev)
1956{
1957 struct amba_device *adev = to_amba_device(dev);
1958 struct mmc_host *mmc = amba_get_drvdata(adev);
1959
1960 if (mmc) {
1961 struct mmci_host *host = mmc_priv(mmc);
Ulf Hanssone36bd9c62013-09-04 09:00:37 +01001962 pinctrl_pm_select_sleep_state(dev);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001963 mmci_save(host);
Ulf Hansson82592932013-01-09 11:15:26 +01001964 clk_disable_unprepare(host->clk);
1965 }
1966
1967 return 0;
1968}
1969
1970static int mmci_runtime_resume(struct device *dev)
1971{
1972 struct amba_device *adev = to_amba_device(dev);
1973 struct mmc_host *mmc = amba_get_drvdata(adev);
1974
1975 if (mmc) {
1976 struct mmci_host *host = mmc_priv(mmc);
1977 clk_prepare_enable(host->clk);
Ulf Hansson1ff44432013-09-04 09:05:17 +01001978 mmci_restore(host);
Ulf Hanssone36bd9c62013-09-04 09:00:37 +01001979 pinctrl_pm_select_default_state(dev);
Ulf Hansson82592932013-01-09 11:15:26 +01001980 }
1981
1982 return 0;
1983}
1984#endif
1985
Ulf Hansson48fa7002011-12-13 16:59:34 +01001986static const struct dev_pm_ops mmci_dev_pm_ops = {
Ulf Hanssonf3737fa2014-01-23 01:11:33 +01001987 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1988 pm_runtime_force_resume)
Rafael J. Wysocki6ed23b82014-12-04 00:34:11 +01001989 SET_RUNTIME_PM_OPS(mmci_runtime_suspend, mmci_runtime_resume, NULL)
Ulf Hansson48fa7002011-12-13 16:59:34 +01001990};
1991
Arvind Yadav88411de2017-08-23 22:00:49 +05301992static const struct amba_id mmci_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 {
1994 .id = 0x00041180,
Pawel Moll768fbc12011-03-11 17:18:07 +00001995 .mask = 0xff0fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01001996 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 },
1998 {
Pawel Moll768fbc12011-03-11 17:18:07 +00001999 .id = 0x01041180,
2000 .mask = 0xff0fffff,
2001 .data = &variant_arm_extended_fifo,
2002 },
2003 {
Pawel Moll3a372982013-01-24 14:12:45 +01002004 .id = 0x02041180,
2005 .mask = 0xff0fffff,
2006 .data = &variant_arm_extended_fifo_hwfc,
2007 },
2008 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 .id = 0x00041181,
2010 .mask = 0x000fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01002011 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 },
Linus Walleijcc30d602009-01-04 15:18:54 +01002013 /* ST Micro variants */
2014 {
2015 .id = 0x00180180,
2016 .mask = 0x00ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01002017 .data = &variant_u300,
Linus Walleijcc30d602009-01-04 15:18:54 +01002018 },
2019 {
Linus Walleij34fd4212012-04-10 17:43:59 +01002020 .id = 0x10180180,
2021 .mask = 0xf0ffffff,
2022 .data = &variant_nomadik,
2023 },
2024 {
Linus Walleijcc30d602009-01-04 15:18:54 +01002025 .id = 0x00280180,
2026 .mask = 0x00ffffff,
Linus Walleij0bcb7ef2016-01-04 02:21:55 +01002027 .data = &variant_nomadik,
Rabin Vincent4956e102010-07-21 12:54:40 +01002028 },
2029 {
2030 .id = 0x00480180,
Philippe Langlais1784b152011-03-25 08:51:52 +01002031 .mask = 0xf0ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01002032 .data = &variant_ux500,
Linus Walleijcc30d602009-01-04 15:18:54 +01002033 },
Philippe Langlais1784b152011-03-25 08:51:52 +01002034 {
2035 .id = 0x10480180,
2036 .mask = 0xf0ffffff,
2037 .data = &variant_ux500v2,
2038 },
Patrice Chotard2a9d6c82018-01-18 15:34:21 +01002039 {
2040 .id = 0x00880180,
2041 .mask = 0x00ffffff,
2042 .data = &variant_stm32,
2043 },
Srinivas Kandagatla55b604a2014-06-02 10:10:13 +01002044 /* Qualcomm variants */
2045 {
2046 .id = 0x00051180,
2047 .mask = 0x000fffff,
2048 .data = &variant_qcom,
2049 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 { 0, 0 },
2051};
2052
Dave Martin9f998352011-10-05 15:15:21 +01002053MODULE_DEVICE_TABLE(amba, mmci_ids);
2054
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055static struct amba_driver mmci_driver = {
2056 .drv = {
2057 .name = DRIVER_NAME,
Ulf Hansson48fa7002011-12-13 16:59:34 +01002058 .pm = &mmci_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 },
2060 .probe = mmci_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05002061 .remove = mmci_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 .id_table = mmci_ids,
2063};
2064
viresh kumar9e5ed092012-03-15 10:40:38 +01002065module_amba_driver(mmci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067module_param(fmax, uint, 0444);
2068
2069MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
2070MODULE_LICENSE("GPL");