blob: 356833a606d58979021aa1ec14a7fbe7c884e139 [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>
Ludovic Barrec8073e52018-12-06 16:13:31 +010024#include <linux/mmc/mmc.h>
Ulf Hansson70be2082013-01-07 15:35:06 +010025#include <linux/mmc/pm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/mmc/host.h>
Linus Walleij34177802010-10-19 12:43:58 +010027#include <linux/mmc/card.h>
Ulf Hanssond2762092014-03-17 13:56:19 +010028#include <linux/mmc/slot-gpio.h>
Russell Kinga62c80e2006-01-07 13:52:45 +000029#include <linux/amba/bus.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000030#include <linux/clk.h>
Jens Axboebd6dee62007-10-24 09:01:09 +020031#include <linux/scatterlist.h>
Linus Walleij9ef986a2018-09-20 16:01:10 -070032#include <linux/of.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>
Ludovic Barre15878e52018-10-08 14:08:51 +020040#include <linux/reset.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Russell King7b09cda2005-07-01 12:02:59 +010042#include <asm/div64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#include "mmci.h"
46
47#define DRIVER_NAME "mmci-pl18x"
48
Ulf Hansson71953e02019-03-06 15:04:56 +010049static void mmci_variant_init(struct mmci_host *host);
Ludovic Barreb3fb9d62019-03-27 10:05:29 +010050static void ux500v2_variant_init(struct mmci_host *host);
Ludovic Barrec3647fd2018-10-08 14:08:33 +020051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052static unsigned int fmax = 515633;
53
Rabin Vincent4956e102010-07-21 12:54:40 +010054static struct variant_data variant_arm = {
Rabin Vincent8301bb62010-08-09 12:57:30 +010055 .fifosize = 16 * 4,
56 .fifohalfsize = 8 * 4,
Ludovic Barre0f244802018-10-08 14:08:45 +020057 .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
58 .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
59 .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
60 .cmdreg_srsp = MCI_CPSM_RESPONSE,
Rabin Vincent08458ef2010-07-21 12:55:59 +010061 .datalength_bits = 16,
Ludovic Barrec931d492018-10-08 14:08:43 +020062 .datactrl_blocksz = 11,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +010063 .pwrreg_powerup = MCI_PWR_UP,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +010064 .f_max = 100000000,
Ulf Hansson78782892014-06-13 13:21:38 +020065 .reversed_irq_handling = true,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +010066 .mmcimask1 = true,
Ludovic Barre59db5e22018-10-08 14:08:47 +020067 .irq_pio_mask = MCI_IRQ_PIO_MASK,
Patrice Chotard7f7b5502018-01-18 15:34:18 +010068 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +010069 .opendrain = MCI_ROD,
Ludovic Barrec3647fd2018-10-08 14:08:33 +020070 .init = mmci_variant_init,
Rabin Vincent4956e102010-07-21 12:54:40 +010071};
72
Pawel Moll768fbc12011-03-11 17:18:07 +000073static struct variant_data variant_arm_extended_fifo = {
74 .fifosize = 128 * 4,
75 .fifohalfsize = 64 * 4,
Ludovic Barre0f244802018-10-08 14:08:45 +020076 .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
77 .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
78 .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
79 .cmdreg_srsp = MCI_CPSM_RESPONSE,
Pawel Moll768fbc12011-03-11 17:18:07 +000080 .datalength_bits = 16,
Ludovic Barrec931d492018-10-08 14:08:43 +020081 .datactrl_blocksz = 11,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +010082 .pwrreg_powerup = MCI_PWR_UP,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +010083 .f_max = 100000000,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +010084 .mmcimask1 = true,
Ludovic Barre59db5e22018-10-08 14:08:47 +020085 .irq_pio_mask = MCI_IRQ_PIO_MASK,
Patrice Chotard7f7b5502018-01-18 15:34:18 +010086 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +010087 .opendrain = MCI_ROD,
Ludovic Barrec3647fd2018-10-08 14:08:33 +020088 .init = mmci_variant_init,
Pawel Moll768fbc12011-03-11 17:18:07 +000089};
90
Pawel Moll3a372982013-01-24 14:12:45 +010091static struct variant_data variant_arm_extended_fifo_hwfc = {
92 .fifosize = 128 * 4,
93 .fifohalfsize = 64 * 4,
94 .clkreg_enable = MCI_ARM_HWFCEN,
Ludovic Barre0f244802018-10-08 14:08:45 +020095 .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
96 .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
97 .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
98 .cmdreg_srsp = MCI_CPSM_RESPONSE,
Pawel Moll3a372982013-01-24 14:12:45 +010099 .datalength_bits = 16,
Ludovic Barrec931d492018-10-08 14:08:43 +0200100 .datactrl_blocksz = 11,
Pawel Moll3a372982013-01-24 14:12:45 +0100101 .pwrreg_powerup = MCI_PWR_UP,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +0100102 .f_max = 100000000,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100103 .mmcimask1 = true,
Ludovic Barre59db5e22018-10-08 14:08:47 +0200104 .irq_pio_mask = MCI_IRQ_PIO_MASK,
Patrice Chotard7f7b5502018-01-18 15:34:18 +0100105 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +0100106 .opendrain = MCI_ROD,
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200107 .init = mmci_variant_init,
Pawel Moll3a372982013-01-24 14:12:45 +0100108};
109
Rabin Vincent4956e102010-07-21 12:54:40 +0100110static struct variant_data variant_u300 = {
Rabin Vincent8301bb62010-08-09 12:57:30 +0100111 .fifosize = 16 * 4,
112 .fifohalfsize = 8 * 4,
Linus Walleij49ac2152011-03-04 14:54:16 +0100113 .clkreg_enable = MCI_ST_U300_HWFCEN,
Srinivas Kandagatlae1412d82014-06-02 10:09:23 +0100114 .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
Ludovic Barre0f244802018-10-08 14:08:45 +0200115 .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
116 .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
117 .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
118 .cmdreg_srsp = MCI_CPSM_RESPONSE,
Rabin Vincent08458ef2010-07-21 12:55:59 +0100119 .datalength_bits = 16,
Ludovic Barrec931d492018-10-08 14:08:43 +0200120 .datactrl_blocksz = 11,
Linus Walleij5db3eee2016-10-25 11:06:05 +0200121 .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
Srinivas Kandagatlac7354132014-08-22 05:55:16 +0100122 .st_sdio = true,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +0100123 .pwrreg_powerup = MCI_PWR_ON,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +0100124 .f_max = 100000000,
Ulf Hansson4d1a3a02011-12-13 16:57:07 +0100125 .signal_direction = true,
Ulf Hanssonf4670da2013-01-09 17:19:54 +0100126 .pwrreg_clkgate = true,
Ulf Hansson1ff44432013-09-04 09:05:17 +0100127 .pwrreg_nopower = true,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100128 .mmcimask1 = true,
Ludovic Barre59db5e22018-10-08 14:08:47 +0200129 .irq_pio_mask = MCI_IRQ_PIO_MASK,
Patrice Chotard7f7b5502018-01-18 15:34:18 +0100130 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +0100131 .opendrain = MCI_OD,
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200132 .init = mmci_variant_init,
Rabin Vincent4956e102010-07-21 12:54:40 +0100133};
134
Linus Walleij34fd4212012-04-10 17:43:59 +0100135static struct variant_data variant_nomadik = {
136 .fifosize = 16 * 4,
137 .fifohalfsize = 8 * 4,
138 .clkreg = MCI_CLK_ENABLE,
Linus Walleijf5abc762016-01-04 02:22:08 +0100139 .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
Ludovic Barre0f244802018-10-08 14:08:45 +0200140 .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
141 .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
142 .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
143 .cmdreg_srsp = MCI_CPSM_RESPONSE,
Linus Walleij34fd4212012-04-10 17:43:59 +0100144 .datalength_bits = 24,
Ludovic Barrec931d492018-10-08 14:08:43 +0200145 .datactrl_blocksz = 11,
Linus Walleij5db3eee2016-10-25 11:06:05 +0200146 .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
Srinivas Kandagatlac7354132014-08-22 05:55:16 +0100147 .st_sdio = true,
Linus Walleij34fd4212012-04-10 17:43:59 +0100148 .st_clkdiv = true,
149 .pwrreg_powerup = MCI_PWR_ON,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +0100150 .f_max = 100000000,
Linus Walleij34fd4212012-04-10 17:43:59 +0100151 .signal_direction = true,
Ulf Hanssonf4670da2013-01-09 17:19:54 +0100152 .pwrreg_clkgate = true,
Ulf Hansson1ff44432013-09-04 09:05:17 +0100153 .pwrreg_nopower = true,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100154 .mmcimask1 = true,
Ludovic Barre59db5e22018-10-08 14:08:47 +0200155 .irq_pio_mask = MCI_IRQ_PIO_MASK,
Patrice Chotard7f7b5502018-01-18 15:34:18 +0100156 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +0100157 .opendrain = MCI_OD,
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200158 .init = mmci_variant_init,
Linus Walleij34fd4212012-04-10 17:43:59 +0100159};
160
Rabin Vincent4956e102010-07-21 12:54:40 +0100161static struct variant_data variant_ux500 = {
Rabin Vincent8301bb62010-08-09 12:57:30 +0100162 .fifosize = 30 * 4,
163 .fifohalfsize = 8 * 4,
Rabin Vincent4956e102010-07-21 12:54:40 +0100164 .clkreg = MCI_CLK_ENABLE,
Linus Walleij49ac2152011-03-04 14:54:16 +0100165 .clkreg_enable = MCI_ST_UX500_HWFCEN,
Srinivas Kandagatlae1412d82014-06-02 10:09:23 +0100166 .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
Srinivas Kandagatlae8740642014-06-02 10:09:30 +0100167 .clkreg_neg_edge_enable = MCI_ST_UX500_NEG_EDGE,
Ludovic Barre0f244802018-10-08 14:08:45 +0200168 .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
169 .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
170 .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
171 .cmdreg_srsp = MCI_CPSM_RESPONSE,
Rabin Vincent08458ef2010-07-21 12:55:59 +0100172 .datalength_bits = 24,
Ludovic Barrec931d492018-10-08 14:08:43 +0200173 .datactrl_blocksz = 11,
Linus Walleij5db3eee2016-10-25 11:06:05 +0200174 .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
Srinivas Kandagatlac7354132014-08-22 05:55:16 +0100175 .st_sdio = true,
Linus Walleijb70a67f2010-12-06 09:24:14 +0100176 .st_clkdiv = true,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +0100177 .pwrreg_powerup = MCI_PWR_ON,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +0100178 .f_max = 100000000,
Ulf Hansson4d1a3a02011-12-13 16:57:07 +0100179 .signal_direction = true,
Ulf Hanssonf4670da2013-01-09 17:19:54 +0100180 .pwrreg_clkgate = true,
Ulf Hansson01259622013-05-15 20:53:22 +0100181 .busy_detect = true,
Linus Walleij49adc0c2016-10-25 11:06:06 +0200182 .busy_dpsm_flag = MCI_DPSM_ST_BUSYMODE,
183 .busy_detect_flag = MCI_ST_CARDBUSY,
184 .busy_detect_mask = MCI_ST_BUSYENDMASK,
Ulf Hansson1ff44432013-09-04 09:05:17 +0100185 .pwrreg_nopower = true,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100186 .mmcimask1 = true,
Ludovic Barre59db5e22018-10-08 14:08:47 +0200187 .irq_pio_mask = MCI_IRQ_PIO_MASK,
Patrice Chotard7f7b5502018-01-18 15:34:18 +0100188 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +0100189 .opendrain = MCI_OD,
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200190 .init = mmci_variant_init,
Rabin Vincent4956e102010-07-21 12:54:40 +0100191};
Linus Walleijb70a67f2010-12-06 09:24:14 +0100192
Philippe Langlais1784b152011-03-25 08:51:52 +0100193static struct variant_data variant_ux500v2 = {
194 .fifosize = 30 * 4,
195 .fifohalfsize = 8 * 4,
196 .clkreg = MCI_CLK_ENABLE,
197 .clkreg_enable = MCI_ST_UX500_HWFCEN,
Srinivas Kandagatlae1412d82014-06-02 10:09:23 +0100198 .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
Srinivas Kandagatlae8740642014-06-02 10:09:30 +0100199 .clkreg_neg_edge_enable = MCI_ST_UX500_NEG_EDGE,
Ludovic Barre0f244802018-10-08 14:08:45 +0200200 .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
201 .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
202 .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
203 .cmdreg_srsp = MCI_CPSM_RESPONSE,
Linus Walleij5db3eee2016-10-25 11:06:05 +0200204 .datactrl_mask_ddrmode = MCI_DPSM_ST_DDRMODE,
Philippe Langlais1784b152011-03-25 08:51:52 +0100205 .datalength_bits = 24,
Ludovic Barrec931d492018-10-08 14:08:43 +0200206 .datactrl_blocksz = 11,
Linus Walleij5db3eee2016-10-25 11:06:05 +0200207 .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
Srinivas Kandagatlac7354132014-08-22 05:55:16 +0100208 .st_sdio = true,
Philippe Langlais1784b152011-03-25 08:51:52 +0100209 .st_clkdiv = true,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +0100210 .pwrreg_powerup = MCI_PWR_ON,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +0100211 .f_max = 100000000,
Ulf Hansson4d1a3a02011-12-13 16:57:07 +0100212 .signal_direction = true,
Ulf Hanssonf4670da2013-01-09 17:19:54 +0100213 .pwrreg_clkgate = true,
Ulf Hansson01259622013-05-15 20:53:22 +0100214 .busy_detect = true,
Linus Walleij49adc0c2016-10-25 11:06:06 +0200215 .busy_dpsm_flag = MCI_DPSM_ST_BUSYMODE,
216 .busy_detect_flag = MCI_ST_CARDBUSY,
217 .busy_detect_mask = MCI_ST_BUSYENDMASK,
Ulf Hansson1ff44432013-09-04 09:05:17 +0100218 .pwrreg_nopower = true,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100219 .mmcimask1 = true,
Ludovic Barre59db5e22018-10-08 14:08:47 +0200220 .irq_pio_mask = MCI_IRQ_PIO_MASK,
Patrice Chotard7f7b5502018-01-18 15:34:18 +0100221 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +0100222 .opendrain = MCI_OD,
Ludovic Barreb3fb9d62019-03-27 10:05:29 +0100223 .init = ux500v2_variant_init,
Philippe Langlais1784b152011-03-25 08:51:52 +0100224};
225
Patrice Chotard2a9d6c82018-01-18 15:34:21 +0100226static struct variant_data variant_stm32 = {
227 .fifosize = 32 * 4,
228 .fifohalfsize = 8 * 4,
229 .clkreg = MCI_CLK_ENABLE,
230 .clkreg_enable = MCI_ST_UX500_HWFCEN,
231 .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
232 .clkreg_neg_edge_enable = MCI_ST_UX500_NEG_EDGE,
Ludovic Barre0f244802018-10-08 14:08:45 +0200233 .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
234 .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
235 .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
236 .cmdreg_srsp = MCI_CPSM_RESPONSE,
Ludovic Barre59db5e22018-10-08 14:08:47 +0200237 .irq_pio_mask = MCI_IRQ_PIO_MASK,
Patrice Chotard2a9d6c82018-01-18 15:34:21 +0100238 .datalength_bits = 24,
Ludovic Barrec931d492018-10-08 14:08:43 +0200239 .datactrl_blocksz = 11,
Patrice Chotard2a9d6c82018-01-18 15:34:21 +0100240 .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
241 .st_sdio = true,
242 .st_clkdiv = true,
243 .pwrreg_powerup = MCI_PWR_ON,
244 .f_max = 48000000,
245 .pwrreg_clkgate = true,
246 .pwrreg_nopower = true,
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200247 .init = mmci_variant_init,
Patrice Chotard2a9d6c82018-01-18 15:34:21 +0100248};
249
Ludovic Barre46b723d2018-10-08 14:08:55 +0200250static struct variant_data variant_stm32_sdmmc = {
251 .fifosize = 16 * 4,
252 .fifohalfsize = 8 * 4,
253 .f_max = 208000000,
254 .stm32_clkdiv = true,
255 .cmdreg_cpsm_enable = MCI_CPSM_STM32_ENABLE,
256 .cmdreg_lrsp_crc = MCI_CPSM_STM32_LRSP_CRC,
257 .cmdreg_srsp_crc = MCI_CPSM_STM32_SRSP_CRC,
258 .cmdreg_srsp = MCI_CPSM_STM32_SRSP,
Ludovic Barrec8073e52018-12-06 16:13:31 +0100259 .cmdreg_stop = MCI_CPSM_STM32_CMDSTOP,
Ludovic Barre46b723d2018-10-08 14:08:55 +0200260 .data_cmd_enable = MCI_CPSM_STM32_CMDTRANS,
261 .irq_pio_mask = MCI_IRQ_PIO_STM32_MASK,
262 .datactrl_first = true,
263 .datacnt_useless = true,
264 .datalength_bits = 25,
265 .datactrl_blocksz = 14,
266 .stm32_idmabsize_mask = GENMASK(12, 5),
267 .init = sdmmc_variant_init,
268};
269
Srinivas Kandagatla55b604a2014-06-02 10:10:13 +0100270static struct variant_data variant_qcom = {
271 .fifosize = 16 * 4,
272 .fifohalfsize = 8 * 4,
273 .clkreg = MCI_CLK_ENABLE,
274 .clkreg_enable = MCI_QCOM_CLK_FLOWENA |
275 MCI_QCOM_CLK_SELECT_IN_FBCLK,
276 .clkreg_8bit_bus_enable = MCI_QCOM_CLK_WIDEBUS_8,
277 .datactrl_mask_ddrmode = MCI_QCOM_CLK_SELECT_IN_DDR_MODE,
Ludovic Barre0f244802018-10-08 14:08:45 +0200278 .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
279 .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
280 .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
281 .cmdreg_srsp = MCI_CPSM_RESPONSE,
Linus Walleij5db3eee2016-10-25 11:06:05 +0200282 .data_cmd_enable = MCI_CPSM_QCOM_DATCMD,
Srinivas Kandagatla55b604a2014-06-02 10:10:13 +0100283 .datalength_bits = 24,
Ludovic Barrec931d492018-10-08 14:08:43 +0200284 .datactrl_blocksz = 11,
Srinivas Kandagatla55b604a2014-06-02 10:10:13 +0100285 .pwrreg_powerup = MCI_PWR_UP,
286 .f_max = 208000000,
287 .explicit_mclk_control = true,
288 .qcom_fifo = true,
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100289 .qcom_dml = true,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100290 .mmcimask1 = true,
Ludovic Barre59db5e22018-10-08 14:08:47 +0200291 .irq_pio_mask = MCI_IRQ_PIO_MASK,
Patrice Chotard7f7b5502018-01-18 15:34:18 +0100292 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +0100293 .opendrain = MCI_ROD,
Ulf Hansson29aba072018-07-16 13:08:18 +0200294 .init = qcom_variant_init,
Srinivas Kandagatla55b604a2014-06-02 10:10:13 +0100295};
296
Linus Walleij49adc0c2016-10-25 11:06:06 +0200297/* Busy detection for the ST Micro variant */
Ulf Hansson01259622013-05-15 20:53:22 +0100298static int mmci_card_busy(struct mmc_host *mmc)
299{
300 struct mmci_host *host = mmc_priv(mmc);
301 unsigned long flags;
302 int busy = 0;
303
Ulf Hansson01259622013-05-15 20:53:22 +0100304 spin_lock_irqsave(&host->lock, flags);
Linus Walleij49adc0c2016-10-25 11:06:06 +0200305 if (readl(host->base + MMCISTATUS) & host->variant->busy_detect_flag)
Ulf Hansson01259622013-05-15 20:53:22 +0100306 busy = 1;
307 spin_unlock_irqrestore(&host->lock, flags);
308
Ulf Hansson01259622013-05-15 20:53:22 +0100309 return busy;
310}
311
Ulf Hanssonf829c042013-09-04 09:01:15 +0100312static void mmci_reg_delay(struct mmci_host *host)
313{
314 /*
315 * According to the spec, at least three feedback clock cycles
316 * of max 52 MHz must pass between two writes to the MMCICLOCK reg.
317 * Three MCLK clock cycles must pass between two MMCIPOWER reg writes.
318 * Worst delay time during card init is at 100 kHz => 30 us.
319 * Worst delay time when up and running is at 25 MHz => 120 ns.
320 */
321 if (host->cclk < 25000000)
322 udelay(30);
323 else
324 ndelay(120);
325}
326
Ulf Hansson653a7612013-01-21 21:29:34 +0100327/*
Linus Walleija6a64642009-09-14 12:56:14 +0100328 * This must be called with host->lock held
329 */
Ludovic Barrecd3ee8c2018-10-08 14:08:42 +0200330void mmci_write_clkreg(struct mmci_host *host, u32 clk)
Ulf Hansson7437cfa2012-01-18 09:17:27 +0100331{
332 if (host->clk_reg != clk) {
333 host->clk_reg = clk;
334 writel(clk, host->base + MMCICLOCK);
335 }
336}
337
338/*
339 * This must be called with host->lock held
340 */
Ludovic Barrecd3ee8c2018-10-08 14:08:42 +0200341void mmci_write_pwrreg(struct mmci_host *host, u32 pwr)
Ulf Hansson7437cfa2012-01-18 09:17:27 +0100342{
343 if (host->pwr_reg != pwr) {
344 host->pwr_reg = pwr;
345 writel(pwr, host->base + MMCIPOWER);
346 }
347}
348
349/*
350 * This must be called with host->lock held
351 */
Ulf Hansson9cc639a2013-05-15 20:48:23 +0100352static void mmci_write_datactrlreg(struct mmci_host *host, u32 datactrl)
353{
Linus Walleij49adc0c2016-10-25 11:06:06 +0200354 /* Keep busy mode in DPSM if enabled */
355 datactrl |= host->datactrl_reg & host->variant->busy_dpsm_flag;
Ulf Hansson01259622013-05-15 20:53:22 +0100356
Ulf Hansson9cc639a2013-05-15 20:48:23 +0100357 if (host->datactrl_reg != datactrl) {
358 host->datactrl_reg = datactrl;
359 writel(datactrl, host->base + MMCIDATACTRL);
360 }
361}
362
363/*
364 * This must be called with host->lock held
365 */
Linus Walleija6a64642009-09-14 12:56:14 +0100366static void mmci_set_clkreg(struct mmci_host *host, unsigned int desired)
367{
Rabin Vincent4956e102010-07-21 12:54:40 +0100368 struct variant_data *variant = host->variant;
369 u32 clk = variant->clkreg;
Linus Walleija6a64642009-09-14 12:56:14 +0100370
Ulf Hanssonc58a8502013-05-13 15:40:03 +0100371 /* Make sure cclk reflects the current calculated clock */
372 host->cclk = 0;
373
Linus Walleija6a64642009-09-14 12:56:14 +0100374 if (desired) {
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +0100375 if (variant->explicit_mclk_control) {
376 host->cclk = host->mclk;
377 } else if (desired >= host->mclk) {
Linus Walleij991a86e2010-12-10 09:35:53 +0100378 clk = MCI_CLK_BYPASS;
Linus Walleij399bc482011-04-01 07:59:17 +0100379 if (variant->st_clkdiv)
380 clk |= MCI_ST_UX500_NEG_EDGE;
Linus Walleija6a64642009-09-14 12:56:14 +0100381 host->cclk = host->mclk;
Linus Walleijb70a67f2010-12-06 09:24:14 +0100382 } else if (variant->st_clkdiv) {
383 /*
384 * DB8500 TRM says f = mclk / (clkdiv + 2)
385 * => clkdiv = (mclk / f) - 2
386 * Round the divider up so we don't exceed the max
387 * frequency
388 */
389 clk = DIV_ROUND_UP(host->mclk, desired) - 2;
390 if (clk >= 256)
391 clk = 255;
392 host->cclk = host->mclk / (clk + 2);
Linus Walleija6a64642009-09-14 12:56:14 +0100393 } else {
Linus Walleijb70a67f2010-12-06 09:24:14 +0100394 /*
395 * PL180 TRM says f = mclk / (2 * (clkdiv + 1))
396 * => clkdiv = mclk / (2 * f) - 1
397 */
Linus Walleija6a64642009-09-14 12:56:14 +0100398 clk = host->mclk / (2 * desired) - 1;
399 if (clk >= 256)
400 clk = 255;
401 host->cclk = host->mclk / (2 * (clk + 1));
402 }
Rabin Vincent4380c142010-07-21 12:55:18 +0100403
404 clk |= variant->clkreg_enable;
Linus Walleija6a64642009-09-14 12:56:14 +0100405 clk |= MCI_CLK_ENABLE;
406 /* This hasn't proven to be worthwhile */
407 /* clk |= MCI_CLK_PWRSAVE; */
408 }
409
Ulf Hanssonc58a8502013-05-13 15:40:03 +0100410 /* Set actual clock for debug */
411 host->mmc->actual_clock = host->cclk;
412
Linus Walleij9e6c82c2009-09-14 12:57:11 +0100413 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4)
Linus Walleij771dc152010-04-08 07:38:52 +0100414 clk |= MCI_4BIT_BUS;
415 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_8)
Srinivas Kandagatlae1412d82014-06-02 10:09:23 +0100416 clk |= variant->clkreg_8bit_bus_enable;
Linus Walleij9e6c82c2009-09-14 12:57:11 +0100417
Seungwon Jeon6dad6c92014-03-14 21:12:13 +0900418 if (host->mmc->ios.timing == MMC_TIMING_UHS_DDR50 ||
419 host->mmc->ios.timing == MMC_TIMING_MMC_DDR52)
Srinivas Kandagatlae8740642014-06-02 10:09:30 +0100420 clk |= variant->clkreg_neg_edge_enable;
Ulf Hansson6dbb6ee2013-01-07 15:30:44 +0100421
Ulf Hansson7437cfa2012-01-18 09:17:27 +0100422 mmci_write_clkreg(host, clk);
Linus Walleija6a64642009-09-14 12:56:14 +0100423}
424
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200425void mmci_dma_release(struct mmci_host *host)
426{
427 if (host->ops && host->ops->dma_release)
428 host->ops->dma_release(host);
429
430 host->use_dma = false;
431}
432
433void mmci_dma_setup(struct mmci_host *host)
434{
435 if (!host->ops || !host->ops->dma_setup)
436 return;
437
438 if (host->ops->dma_setup(host))
439 return;
440
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200441 /* initialize pre request cookie */
442 host->next_cookie = 1;
443
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200444 host->use_dma = true;
445}
446
Ludovic Barree0da1722018-10-08 14:08:41 +0200447/*
448 * Validate mmc prerequisites
449 */
450static int mmci_validate_data(struct mmci_host *host,
451 struct mmc_data *data)
452{
453 if (!data)
454 return 0;
455
456 if (!is_power_of_2(data->blksz)) {
457 dev_err(mmc_dev(host->mmc),
458 "unsupported block size (%d bytes)\n", data->blksz);
459 return -EINVAL;
460 }
461
462 if (host->ops && host->ops->validate_data)
463 return host->ops->validate_data(host, data);
464
465 return 0;
466}
467
Ludovic Barre47983512018-10-08 14:08:36 +0200468int mmci_prep_data(struct mmci_host *host, struct mmc_data *data, bool next)
469{
470 int err;
471
472 if (!host->ops || !host->ops->prep_data)
473 return 0;
474
475 err = host->ops->prep_data(host, data, next);
476
477 if (next && !err)
478 data->host_cookie = ++host->next_cookie < 0 ?
479 1 : host->next_cookie;
480
481 return err;
482}
483
484void mmci_unprep_data(struct mmci_host *host, struct mmc_data *data,
485 int err)
486{
487 if (host->ops && host->ops->unprep_data)
488 host->ops->unprep_data(host, data, err);
489
490 data->host_cookie = 0;
491}
492
Ludovic Barre02769962018-10-08 14:08:37 +0200493void mmci_get_next_data(struct mmci_host *host, struct mmc_data *data)
494{
495 WARN_ON(data->host_cookie && data->host_cookie != host->next_cookie);
496
497 if (host->ops && host->ops->get_next_data)
498 host->ops->get_next_data(host, data);
499}
500
Ludovic Barre135ea302018-10-08 14:08:38 +0200501int mmci_dma_start(struct mmci_host *host, unsigned int datactrl)
502{
503 struct mmc_data *data = host->data;
504 int ret;
505
506 if (!host->use_dma)
507 return -EINVAL;
508
509 ret = mmci_prep_data(host, data, false);
510 if (ret)
511 return ret;
512
513 if (!host->ops || !host->ops->dma_start)
514 return -EINVAL;
515
516 /* Okay, go for it. */
517 dev_vdbg(mmc_dev(host->mmc),
518 "Submit MMCI DMA job, sglen %d blksz %04x blks %04x flags %08x\n",
519 data->sg_len, data->blksz, data->blocks, data->flags);
520
521 host->ops->dma_start(host, &datactrl);
522
523 /* Trigger the DMA transfer */
524 mmci_write_datactrlreg(host, datactrl);
525
526 /*
527 * Let the MMCI say when the data is ended and it's time
528 * to fire next DMA request. When that happens, MMCI will
529 * call mmci_data_end()
530 */
531 writel(readl(host->base + MMCIMASK0) | MCI_DATAENDMASK,
532 host->base + MMCIMASK0);
533 return 0;
534}
535
Ludovic Barre5a9f10c2018-10-08 14:08:39 +0200536void mmci_dma_finalize(struct mmci_host *host, struct mmc_data *data)
537{
538 if (!host->use_dma)
539 return;
540
541 if (host->ops && host->ops->dma_finalize)
542 host->ops->dma_finalize(host, data);
543}
544
Ludovic Barrecfccc6a2018-10-08 14:08:40 +0200545void mmci_dma_error(struct mmci_host *host)
546{
547 if (!host->use_dma)
548 return;
549
550 if (host->ops && host->ops->dma_error)
551 host->ops->dma_error(host);
552}
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554static void
555mmci_request_end(struct mmci_host *host, struct mmc_request *mrq)
556{
557 writel(0, host->base + MMCICOMMAND);
558
Russell Kinge47c2222007-01-08 16:42:51 +0000559 BUG_ON(host->data);
560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 host->mrq = NULL;
562 host->cmd = NULL;
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 mmc_request_done(host->mmc, mrq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
Linus Walleij2686b4b2010-10-19 12:39:48 +0100567static void mmci_set_mask1(struct mmci_host *host, unsigned int mask)
568{
569 void __iomem *base = host->base;
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100570 struct variant_data *variant = host->variant;
Linus Walleij2686b4b2010-10-19 12:39:48 +0100571
572 if (host->singleirq) {
573 unsigned int mask0 = readl(base + MMCIMASK0);
574
Ludovic Barre59db5e22018-10-08 14:08:47 +0200575 mask0 &= ~variant->irq_pio_mask;
Linus Walleij2686b4b2010-10-19 12:39:48 +0100576 mask0 |= mask;
577
578 writel(mask0, base + MMCIMASK0);
579 }
580
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100581 if (variant->mmcimask1)
582 writel(mask, base + MMCIMASK1);
583
584 host->mask1_reg = mask;
Linus Walleij2686b4b2010-10-19 12:39:48 +0100585}
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587static void mmci_stop_data(struct mmci_host *host)
588{
Ulf Hansson9cc639a2013-05-15 20:48:23 +0100589 mmci_write_datactrlreg(host, 0);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100590 mmci_set_mask1(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 host->data = NULL;
592}
593
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100594static void mmci_init_sg(struct mmci_host *host, struct mmc_data *data)
595{
596 unsigned int flags = SG_MITER_ATOMIC;
597
598 if (data->flags & MMC_DATA_READ)
599 flags |= SG_MITER_TO_SG;
600 else
601 flags |= SG_MITER_FROM_SG;
602
603 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
604}
605
Ludovic Barreb3fb9d62019-03-27 10:05:29 +0100606static u32 mmci_get_dctrl_cfg(struct mmci_host *host)
607{
608 return MCI_DPSM_ENABLE | mmci_dctrl_blksz(host);
609}
610
611static u32 ux500v2_get_dctrl_cfg(struct mmci_host *host)
612{
613 return MCI_DPSM_ENABLE | (host->data->blksz << 16);
614}
615
Russell Kingc8ebae32011-01-11 19:35:53 +0000616/*
617 * All the DMA operation mode stuff goes inside this ifdef.
618 * This assumes that you have a generic DMA device interface,
619 * no custom DMA interfaces are supported.
620 */
621#ifdef CONFIG_DMA_ENGINE
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200622struct mmci_dmae_next {
623 struct dma_async_tx_descriptor *desc;
624 struct dma_chan *chan;
625};
626
627struct mmci_dmae_priv {
628 struct dma_chan *cur;
629 struct dma_chan *rx_channel;
630 struct dma_chan *tx_channel;
631 struct dma_async_tx_descriptor *desc_current;
632 struct mmci_dmae_next next_data;
633};
634
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200635int mmci_dmae_setup(struct mmci_host *host)
Russell Kingc8ebae32011-01-11 19:35:53 +0000636{
Russell Kingc8ebae32011-01-11 19:35:53 +0000637 const char *rxname, *txname;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200638 struct mmci_dmae_priv *dmae;
Russell Kingc8ebae32011-01-11 19:35:53 +0000639
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200640 dmae = devm_kzalloc(mmc_dev(host->mmc), sizeof(*dmae), GFP_KERNEL);
641 if (!dmae)
642 return -ENOMEM;
Russell Kingc8ebae32011-01-11 19:35:53 +0000643
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200644 host->dma_priv = dmae;
645
646 dmae->rx_channel = dma_request_slave_channel(mmc_dev(host->mmc),
647 "rx");
648 dmae->tx_channel = dma_request_slave_channel(mmc_dev(host->mmc),
649 "tx");
Per Forlin58c7ccb2011-07-01 18:55:24 +0200650
Russell Kingc8ebae32011-01-11 19:35:53 +0000651 /*
652 * If only an RX channel is specified, the driver will
653 * attempt to use it bidirectionally, however if it is
654 * is specified but cannot be located, DMA will be disabled.
655 */
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200656 if (dmae->rx_channel && !dmae->tx_channel)
657 dmae->tx_channel = dmae->rx_channel;
Russell Kingc8ebae32011-01-11 19:35:53 +0000658
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200659 if (dmae->rx_channel)
660 rxname = dma_chan_name(dmae->rx_channel);
Russell Kingc8ebae32011-01-11 19:35:53 +0000661 else
662 rxname = "none";
663
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200664 if (dmae->tx_channel)
665 txname = dma_chan_name(dmae->tx_channel);
Russell Kingc8ebae32011-01-11 19:35:53 +0000666 else
667 txname = "none";
668
669 dev_info(mmc_dev(host->mmc), "DMA channels RX %s, TX %s\n",
670 rxname, txname);
671
672 /*
673 * Limit the maximum segment size in any SG entry according to
674 * the parameters of the DMA engine device.
675 */
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200676 if (dmae->tx_channel) {
677 struct device *dev = dmae->tx_channel->device->dev;
Russell Kingc8ebae32011-01-11 19:35:53 +0000678 unsigned int max_seg_size = dma_get_max_seg_size(dev);
679
680 if (max_seg_size < host->mmc->max_seg_size)
681 host->mmc->max_seg_size = max_seg_size;
682 }
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200683 if (dmae->rx_channel) {
684 struct device *dev = dmae->rx_channel->device->dev;
Russell Kingc8ebae32011-01-11 19:35:53 +0000685 unsigned int max_seg_size = dma_get_max_seg_size(dev);
686
687 if (max_seg_size < host->mmc->max_seg_size)
688 host->mmc->max_seg_size = max_seg_size;
689 }
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100690
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200691 if (!dmae->tx_channel || !dmae->rx_channel) {
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200692 mmci_dmae_release(host);
693 return -EINVAL;
694 }
695
696 return 0;
Russell Kingc8ebae32011-01-11 19:35:53 +0000697}
698
699/*
Bill Pemberton6e0ee712012-11-19 13:26:03 -0500700 * This is used in or so inline it
Russell Kingc8ebae32011-01-11 19:35:53 +0000701 * so it can be discarded.
702 */
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200703void mmci_dmae_release(struct mmci_host *host)
Russell Kingc8ebae32011-01-11 19:35:53 +0000704{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200705 struct mmci_dmae_priv *dmae = host->dma_priv;
706
707 if (dmae->rx_channel)
708 dma_release_channel(dmae->rx_channel);
709 if (dmae->tx_channel)
710 dma_release_channel(dmae->tx_channel);
711 dmae->rx_channel = dmae->tx_channel = NULL;
Russell Kingc8ebae32011-01-11 19:35:53 +0000712}
713
714static void mmci_dma_unmap(struct mmci_host *host, struct mmc_data *data)
715{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200716 struct mmci_dmae_priv *dmae = host->dma_priv;
Ulf Hansson653a7612013-01-21 21:29:34 +0100717 struct dma_chan *chan;
Ulf Hansson653a7612013-01-21 21:29:34 +0100718
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200719 if (data->flags & MMC_DATA_READ)
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200720 chan = dmae->rx_channel;
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200721 else
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200722 chan = dmae->tx_channel;
Ulf Hansson653a7612013-01-21 21:29:34 +0100723
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200724 dma_unmap_sg(chan->device->dev, data->sg, data->sg_len,
725 mmc_get_dma_dir(data));
Ulf Hansson653a7612013-01-21 21:29:34 +0100726}
727
Ludovic Barrecfccc6a2018-10-08 14:08:40 +0200728void mmci_dmae_error(struct mmci_host *host)
Ludovic Barre7b2a6d52018-09-21 11:45:55 +0200729{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200730 struct mmci_dmae_priv *dmae = host->dma_priv;
731
Ludovic Barrecfccc6a2018-10-08 14:08:40 +0200732 if (!dma_inprogress(host))
Ludovic Barrecdea1942018-09-21 11:45:56 +0200733 return;
734
Ludovic Barre7b2a6d52018-09-21 11:45:55 +0200735 dev_err(mmc_dev(host->mmc), "error during DMA transfer!\n");
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200736 dmaengine_terminate_all(dmae->cur);
Ludovic Barre7b2a6d52018-09-21 11:45:55 +0200737 host->dma_in_progress = false;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200738 dmae->cur = NULL;
739 dmae->desc_current = NULL;
Ludovic Barre7b2a6d52018-09-21 11:45:55 +0200740 host->data->host_cookie = 0;
741
742 mmci_dma_unmap(host, host->data);
743}
744
Ludovic Barre5a9f10c2018-10-08 14:08:39 +0200745void mmci_dmae_finalize(struct mmci_host *host, struct mmc_data *data)
Ulf Hansson653a7612013-01-21 21:29:34 +0100746{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200747 struct mmci_dmae_priv *dmae = host->dma_priv;
Russell Kingc8ebae32011-01-11 19:35:53 +0000748 u32 status;
749 int i;
750
Ludovic Barre5a9f10c2018-10-08 14:08:39 +0200751 if (!dma_inprogress(host))
Ludovic Barrecdea1942018-09-21 11:45:56 +0200752 return;
753
Russell Kingc8ebae32011-01-11 19:35:53 +0000754 /* Wait up to 1ms for the DMA to complete */
755 for (i = 0; ; i++) {
756 status = readl(host->base + MMCISTATUS);
757 if (!(status & MCI_RXDATAAVLBLMASK) || i >= 100)
758 break;
759 udelay(10);
760 }
761
762 /*
763 * Check to see whether we still have some data left in the FIFO -
764 * this catches DMA controllers which are unable to monitor the
765 * DMALBREQ and DMALSREQ signals while allowing us to DMA to non-
766 * contiguous buffers. On TX, we'll get a FIFO underrun error.
767 */
768 if (status & MCI_RXDATAAVLBLMASK) {
Ludovic Barrecfccc6a2018-10-08 14:08:40 +0200769 mmci_dma_error(host);
Russell Kingc8ebae32011-01-11 19:35:53 +0000770 if (!data->error)
771 data->error = -EIO;
Ludovic Barre7b2a6d52018-09-21 11:45:55 +0200772 } else if (!data->host_cookie) {
Ulf Hansson653a7612013-01-21 21:29:34 +0100773 mmci_dma_unmap(host, data);
Ludovic Barre7b2a6d52018-09-21 11:45:55 +0200774 }
Russell Kingc8ebae32011-01-11 19:35:53 +0000775
776 /*
777 * Use of DMA with scatter-gather is impossible.
778 * Give up with DMA and switch back to PIO mode.
779 */
780 if (status & MCI_RXDATAAVLBLMASK) {
781 dev_err(mmc_dev(host->mmc), "buggy DMA detected. Taking evasive action.\n");
782 mmci_dma_release(host);
783 }
Ulf Hansson653a7612013-01-21 21:29:34 +0100784
Linus Walleije13934b2017-01-27 15:04:54 +0100785 host->dma_in_progress = false;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200786 dmae->cur = NULL;
787 dmae->desc_current = NULL;
Russell Kingc8ebae32011-01-11 19:35:53 +0000788}
789
Ulf Hansson653a7612013-01-21 21:29:34 +0100790/* prepares DMA channel and DMA descriptor, returns non-zero on failure */
Ludovic Barre47983512018-10-08 14:08:36 +0200791static int _mmci_dmae_prep_data(struct mmci_host *host, struct mmc_data *data,
Ulf Hansson653a7612013-01-21 21:29:34 +0100792 struct dma_chan **dma_chan,
793 struct dma_async_tx_descriptor **dma_desc)
Russell Kingc8ebae32011-01-11 19:35:53 +0000794{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200795 struct mmci_dmae_priv *dmae = host->dma_priv;
Russell Kingc8ebae32011-01-11 19:35:53 +0000796 struct variant_data *variant = host->variant;
797 struct dma_slave_config conf = {
798 .src_addr = host->phybase + MMCIFIFO,
799 .dst_addr = host->phybase + MMCIFIFO,
800 .src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
801 .dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
802 .src_maxburst = variant->fifohalfsize >> 2, /* # of words */
803 .dst_maxburst = variant->fifohalfsize >> 2, /* # of words */
Viresh Kumar258aea72012-02-01 16:12:19 +0530804 .device_fc = false,
Russell Kingc8ebae32011-01-11 19:35:53 +0000805 };
Russell Kingc8ebae32011-01-11 19:35:53 +0000806 struct dma_chan *chan;
807 struct dma_device *device;
808 struct dma_async_tx_descriptor *desc;
809 int nr_sg;
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100810 unsigned long flags = DMA_CTRL_ACK;
Russell Kingc8ebae32011-01-11 19:35:53 +0000811
Russell Kingc8ebae32011-01-11 19:35:53 +0000812 if (data->flags & MMC_DATA_READ) {
Vinod Koul05f57992011-10-14 10:45:11 +0530813 conf.direction = DMA_DEV_TO_MEM;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200814 chan = dmae->rx_channel;
Russell Kingc8ebae32011-01-11 19:35:53 +0000815 } else {
Vinod Koul05f57992011-10-14 10:45:11 +0530816 conf.direction = DMA_MEM_TO_DEV;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200817 chan = dmae->tx_channel;
Russell Kingc8ebae32011-01-11 19:35:53 +0000818 }
819
820 /* If there's no DMA channel, fall back to PIO */
821 if (!chan)
822 return -EINVAL;
823
824 /* If less than or equal to the fifo size, don't bother with DMA */
Per Forlin58c7ccb2011-07-01 18:55:24 +0200825 if (data->blksz * data->blocks <= variant->fifosize)
Russell Kingc8ebae32011-01-11 19:35:53 +0000826 return -EINVAL;
827
828 device = chan->device;
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200829 nr_sg = dma_map_sg(device->dev, data->sg, data->sg_len,
830 mmc_get_dma_dir(data));
Russell Kingc8ebae32011-01-11 19:35:53 +0000831 if (nr_sg == 0)
832 return -EINVAL;
833
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100834 if (host->variant->qcom_dml)
835 flags |= DMA_PREP_INTERRUPT;
836
Russell Kingc8ebae32011-01-11 19:35:53 +0000837 dmaengine_slave_config(chan, &conf);
Alexandre Bounine16052822012-03-08 16:11:18 -0500838 desc = dmaengine_prep_slave_sg(chan, data->sg, nr_sg,
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100839 conf.direction, flags);
Russell Kingc8ebae32011-01-11 19:35:53 +0000840 if (!desc)
841 goto unmap_exit;
842
Ulf Hansson653a7612013-01-21 21:29:34 +0100843 *dma_chan = chan;
844 *dma_desc = desc;
Russell Kingc8ebae32011-01-11 19:35:53 +0000845
Per Forlin58c7ccb2011-07-01 18:55:24 +0200846 return 0;
847
848 unmap_exit:
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200849 dma_unmap_sg(device->dev, data->sg, data->sg_len,
850 mmc_get_dma_dir(data));
Per Forlin58c7ccb2011-07-01 18:55:24 +0200851 return -ENOMEM;
852}
853
Ludovic Barre47983512018-10-08 14:08:36 +0200854int mmci_dmae_prep_data(struct mmci_host *host,
855 struct mmc_data *data,
856 bool next)
Ulf Hansson653a7612013-01-21 21:29:34 +0100857{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200858 struct mmci_dmae_priv *dmae = host->dma_priv;
Ludovic Barread7b8912018-10-08 14:08:35 +0200859 struct mmci_dmae_next *nd = &dmae->next_data;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200860
Ludovic Barre47983512018-10-08 14:08:36 +0200861 if (!host->use_dma)
862 return -EINVAL;
863
Ludovic Barread7b8912018-10-08 14:08:35 +0200864 if (next)
Ludovic Barre47983512018-10-08 14:08:36 +0200865 return _mmci_dmae_prep_data(host, data, &nd->chan, &nd->desc);
Ulf Hansson653a7612013-01-21 21:29:34 +0100866 /* Check if next job is already prepared. */
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200867 if (dmae->cur && dmae->desc_current)
Ulf Hansson653a7612013-01-21 21:29:34 +0100868 return 0;
869
870 /* No job were prepared thus do it now. */
Ludovic Barre47983512018-10-08 14:08:36 +0200871 return _mmci_dmae_prep_data(host, data, &dmae->cur,
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200872 &dmae->desc_current);
Ulf Hansson653a7612013-01-21 21:29:34 +0100873}
874
Ludovic Barre135ea302018-10-08 14:08:38 +0200875int mmci_dmae_start(struct mmci_host *host, unsigned int *datactrl)
Per Forlin58c7ccb2011-07-01 18:55:24 +0200876{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200877 struct mmci_dmae_priv *dmae = host->dma_priv;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200878
Linus Walleije13934b2017-01-27 15:04:54 +0100879 host->dma_in_progress = true;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200880 dmaengine_submit(dmae->desc_current);
881 dma_async_issue_pending(dmae->cur);
Russell Kingc8ebae32011-01-11 19:35:53 +0000882
Ludovic Barre135ea302018-10-08 14:08:38 +0200883 *datactrl |= MCI_DPSM_DMAENABLE;
Russell Kingc8ebae32011-01-11 19:35:53 +0000884
Russell Kingc8ebae32011-01-11 19:35:53 +0000885 return 0;
Russell Kingc8ebae32011-01-11 19:35:53 +0000886}
Per Forlin58c7ccb2011-07-01 18:55:24 +0200887
Ludovic Barre02769962018-10-08 14:08:37 +0200888void mmci_dmae_get_next_data(struct mmci_host *host, struct mmc_data *data)
Per Forlin58c7ccb2011-07-01 18:55:24 +0200889{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200890 struct mmci_dmae_priv *dmae = host->dma_priv;
891 struct mmci_dmae_next *next = &dmae->next_data;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200892
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200893 if (!host->use_dma)
894 return;
895
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200896 WARN_ON(!data->host_cookie && (next->desc || next->chan));
Per Forlin58c7ccb2011-07-01 18:55:24 +0200897
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200898 dmae->desc_current = next->desc;
899 dmae->cur = next->chan;
900 next->desc = NULL;
901 next->chan = NULL;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200902}
903
Ludovic Barre47983512018-10-08 14:08:36 +0200904void mmci_dmae_unprep_data(struct mmci_host *host,
905 struct mmc_data *data, int err)
906
Per Forlin58c7ccb2011-07-01 18:55:24 +0200907{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200908 struct mmci_dmae_priv *dmae = host->dma_priv;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200909
Ludovic Barre47983512018-10-08 14:08:36 +0200910 if (!host->use_dma)
Per Forlin58c7ccb2011-07-01 18:55:24 +0200911 return;
912
Ulf Hansson653a7612013-01-21 21:29:34 +0100913 mmci_dma_unmap(host, data);
Per Forlin58c7ccb2011-07-01 18:55:24 +0200914
Ulf Hansson653a7612013-01-21 21:29:34 +0100915 if (err) {
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200916 struct mmci_dmae_next *next = &dmae->next_data;
Ulf Hansson653a7612013-01-21 21:29:34 +0100917 struct dma_chan *chan;
918 if (data->flags & MMC_DATA_READ)
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200919 chan = dmae->rx_channel;
Ulf Hansson653a7612013-01-21 21:29:34 +0100920 else
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200921 chan = dmae->tx_channel;
Ulf Hansson653a7612013-01-21 21:29:34 +0100922 dmaengine_terminate_all(chan);
Per Forlin58c7ccb2011-07-01 18:55:24 +0200923
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200924 if (dmae->desc_current == next->desc)
925 dmae->desc_current = NULL;
Srinivas Kandagatlab5c16a62014-10-08 12:25:17 +0100926
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200927 if (dmae->cur == next->chan) {
Linus Walleije13934b2017-01-27 15:04:54 +0100928 host->dma_in_progress = false;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200929 dmae->cur = NULL;
Linus Walleije13934b2017-01-27 15:04:54 +0100930 }
Srinivas Kandagatlab5c16a62014-10-08 12:25:17 +0100931
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200932 next->desc = NULL;
933 next->chan = NULL;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200934 }
935}
936
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200937static struct mmci_host_ops mmci_variant_ops = {
Ludovic Barre47983512018-10-08 14:08:36 +0200938 .prep_data = mmci_dmae_prep_data,
939 .unprep_data = mmci_dmae_unprep_data,
Ludovic Barreb3fb9d62019-03-27 10:05:29 +0100940 .get_datactrl_cfg = mmci_get_dctrl_cfg,
Ludovic Barre02769962018-10-08 14:08:37 +0200941 .get_next_data = mmci_dmae_get_next_data,
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200942 .dma_setup = mmci_dmae_setup,
943 .dma_release = mmci_dmae_release,
Ludovic Barre135ea302018-10-08 14:08:38 +0200944 .dma_start = mmci_dmae_start,
Ludovic Barre5a9f10c2018-10-08 14:08:39 +0200945 .dma_finalize = mmci_dmae_finalize,
Ludovic Barrecfccc6a2018-10-08 14:08:40 +0200946 .dma_error = mmci_dmae_error,
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200947};
Ludovic Barreb3fb9d62019-03-27 10:05:29 +0100948#else
949static struct mmci_host_ops mmci_variant_ops = {
950 .get_datactrl_cfg = mmci_get_dctrl_cfg,
951};
952#endif
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200953
954void mmci_variant_init(struct mmci_host *host)
955{
956 host->ops = &mmci_variant_ops;
957}
Ludovic Barreb3fb9d62019-03-27 10:05:29 +0100958
959void ux500v2_variant_init(struct mmci_host *host)
960{
961 host->ops = &mmci_variant_ops;
962 host->ops->get_datactrl_cfg = ux500v2_get_dctrl_cfg;
963}
Russell Kingc8ebae32011-01-11 19:35:53 +0000964
Ludovic Barre47983512018-10-08 14:08:36 +0200965static void mmci_pre_request(struct mmc_host *mmc, struct mmc_request *mrq)
966{
967 struct mmci_host *host = mmc_priv(mmc);
968 struct mmc_data *data = mrq->data;
969
970 if (!data)
971 return;
972
973 WARN_ON(data->host_cookie);
974
975 if (mmci_validate_data(host, data))
976 return;
977
978 mmci_prep_data(host, data, true);
979}
980
981static void mmci_post_request(struct mmc_host *mmc, struct mmc_request *mrq,
982 int err)
983{
984 struct mmci_host *host = mmc_priv(mmc);
985 struct mmc_data *data = mrq->data;
986
987 if (!data || !data->host_cookie)
988 return;
989
990 mmci_unprep_data(host, data, err);
991}
992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993static void mmci_start_data(struct mmci_host *host, struct mmc_data *data)
994{
Rabin Vincent8301bb62010-08-09 12:57:30 +0100995 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 unsigned int datactrl, timeout, irqmask;
Russell King7b09cda2005-07-01 12:02:59 +0100997 unsigned long long clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 void __iomem *base;
999
Linus Walleij64de0282010-02-19 01:09:10 +01001000 dev_dbg(mmc_dev(host->mmc), "blksz %04x blks %04x flags %08x\n",
1001 data->blksz, data->blocks, data->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003 host->data = data;
Rabin Vincent528320d2010-07-21 12:49:49 +01001004 host->size = data->blksz * data->blocks;
Russell King51d43752011-01-27 10:56:52 +00001005 data->bytes_xfered = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Russell King7b09cda2005-07-01 12:02:59 +01001007 clks = (unsigned long long)data->timeout_ns * host->cclk;
Srinivas Kandagatlac4a35762014-06-02 10:08:39 +01001008 do_div(clks, NSEC_PER_SEC);
Russell King7b09cda2005-07-01 12:02:59 +01001009
1010 timeout = data->timeout_clks + (unsigned int)clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
1012 base = host->base;
1013 writel(timeout, base + MMCIDATATIMER);
1014 writel(host->size, base + MMCIDATALENGTH);
1015
Ludovic Barre41ed65e2019-03-27 10:05:32 +01001016 datactrl = host->ops->get_datactrl_cfg(host);
1017 datactrl |= host->data->flags & MMC_DATA_READ ? MCI_DPSM_DIRECTION : 0;
Russell Kingc8ebae32011-01-11 19:35:53 +00001018
Srinivas Kandagatlac7354132014-08-22 05:55:16 +01001019 if (host->mmc->card && mmc_card_sdio(host->mmc->card)) {
1020 u32 clk;
Ulf Hansson7258db72011-12-13 17:05:28 +01001021
Srinivas Kandagatlac7354132014-08-22 05:55:16 +01001022 datactrl |= variant->datactrl_mask_sdio;
Ulf Hansson06c1a122012-10-12 14:01:50 +01001023
Srinivas Kandagatlac7354132014-08-22 05:55:16 +01001024 /*
1025 * The ST Micro variant for SDIO small write transfers
1026 * needs to have clock H/W flow control disabled,
1027 * otherwise the transfer will not start. The threshold
1028 * depends on the rate of MCLK.
1029 */
1030 if (variant->st_sdio && data->flags & MMC_DATA_WRITE &&
1031 (host->size < 8 ||
1032 (host->size <= 8 && host->mclk > 50000000)))
1033 clk = host->clk_reg & ~variant->clkreg_enable;
1034 else
1035 clk = host->clk_reg | variant->clkreg_enable;
1036
1037 mmci_write_clkreg(host, clk);
1038 }
Ulf Hansson06c1a122012-10-12 14:01:50 +01001039
Seungwon Jeon6dad6c92014-03-14 21:12:13 +09001040 if (host->mmc->ios.timing == MMC_TIMING_UHS_DDR50 ||
1041 host->mmc->ios.timing == MMC_TIMING_MMC_DDR52)
Srinivas Kandagatlae17dca22014-06-02 10:09:15 +01001042 datactrl |= variant->datactrl_mask_ddrmode;
Ulf Hansson6dbb6ee2013-01-07 15:30:44 +01001043
Russell Kingc8ebae32011-01-11 19:35:53 +00001044 /*
1045 * Attempt to use DMA operation mode, if this
1046 * should fail, fall back to PIO mode
1047 */
Ludovic Barre135ea302018-10-08 14:08:38 +02001048 if (!mmci_dma_start(host, datactrl))
Russell Kingc8ebae32011-01-11 19:35:53 +00001049 return;
1050
1051 /* IRQ mode, map the SG list for CPU reading/writing */
1052 mmci_init_sg(host, data);
1053
1054 if (data->flags & MMC_DATA_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 irqmask = MCI_RXFIFOHALFFULLMASK;
Russell King0425a142006-02-16 16:48:31 +00001056
1057 /*
Russell Kingc4d877c2011-01-27 09:50:13 +00001058 * If we have less than the fifo 'half-full' threshold to
1059 * transfer, trigger a PIO interrupt as soon as any data
1060 * is available.
Russell King0425a142006-02-16 16:48:31 +00001061 */
Russell Kingc4d877c2011-01-27 09:50:13 +00001062 if (host->size < variant->fifohalfsize)
Russell King0425a142006-02-16 16:48:31 +00001063 irqmask |= MCI_RXDATAAVLBLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 } else {
1065 /*
1066 * We don't actually need to include "FIFO empty" here
1067 * since its implicit in "FIFO half empty".
1068 */
1069 irqmask = MCI_TXFIFOHALFEMPTYMASK;
1070 }
1071
Ulf Hansson9cc639a2013-05-15 20:48:23 +01001072 mmci_write_datactrlreg(host, datactrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 writel(readl(base + MMCIMASK0) & ~MCI_DATAENDMASK, base + MMCIMASK0);
Linus Walleij2686b4b2010-10-19 12:39:48 +01001074 mmci_set_mask1(host, irqmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075}
1076
1077static void
1078mmci_start_command(struct mmci_host *host, struct mmc_command *cmd, u32 c)
1079{
1080 void __iomem *base = host->base;
1081
Linus Walleij64de0282010-02-19 01:09:10 +01001082 dev_dbg(mmc_dev(host->mmc), "op %02x arg %08x flags %08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 cmd->opcode, cmd->arg, cmd->flags);
1084
Ludovic Barre0f244802018-10-08 14:08:45 +02001085 if (readl(base + MMCICOMMAND) & host->variant->cmdreg_cpsm_enable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 writel(0, base + MMCICOMMAND);
Srinivas Kandagatla6adb2a82014-06-02 10:08:57 +01001087 mmci_reg_delay(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 }
1089
Ludovic Barrec8073e52018-12-06 16:13:31 +01001090 if (host->variant->cmdreg_stop &&
1091 cmd->opcode == MMC_STOP_TRANSMISSION)
1092 c |= host->variant->cmdreg_stop;
1093
Ludovic Barre0f244802018-10-08 14:08:45 +02001094 c |= cmd->opcode | host->variant->cmdreg_cpsm_enable;
Russell Kinge9225172006-02-02 12:23:12 +00001095 if (cmd->flags & MMC_RSP_PRESENT) {
1096 if (cmd->flags & MMC_RSP_136)
Ludovic Barre0f244802018-10-08 14:08:45 +02001097 c |= host->variant->cmdreg_lrsp_crc;
1098 else if (cmd->flags & MMC_RSP_CRC)
1099 c |= host->variant->cmdreg_srsp_crc;
1100 else
1101 c |= host->variant->cmdreg_srsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 }
1103 if (/*interrupt*/0)
1104 c |= MCI_CPSM_INTERRUPT;
1105
Srinivas Kandagatlaae7b0062014-06-02 10:09:39 +01001106 if (mmc_cmd_type(cmd) == MMC_CMD_ADTC)
1107 c |= host->variant->data_cmd_enable;
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 host->cmd = cmd;
1110
1111 writel(cmd->arg, base + MMCIARGUMENT);
1112 writel(c, base + MMCICOMMAND);
1113}
1114
Ulf Hanssone9968c62019-01-29 15:35:56 +01001115static void mmci_stop_command(struct mmci_host *host)
1116{
1117 host->stop_abort.error = 0;
1118 mmci_start_command(host, &host->stop_abort, 0);
1119}
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121static void
1122mmci_data_irq(struct mmci_host *host, struct mmc_data *data,
1123 unsigned int status)
1124{
Ludovic Barredaf97132018-10-08 14:08:44 +02001125 unsigned int status_err;
1126
Ulf Hansson1cb9da52014-06-12 14:42:23 +02001127 /* Make sure we have data to handle */
1128 if (!data)
1129 return;
1130
Linus Walleijf20f8f212010-10-19 13:41:24 +01001131 /* First check for errors */
Ludovic Barredaf97132018-10-08 14:08:44 +02001132 status_err = status & (host->variant->start_err |
1133 MCI_DATACRCFAIL | MCI_DATATIMEOUT |
1134 MCI_TXUNDERRUN | MCI_RXOVERRUN);
1135
1136 if (status_err) {
Linus Walleij8cb28152011-01-24 15:22:13 +01001137 u32 remain, success;
Linus Walleijf20f8f212010-10-19 13:41:24 +01001138
Russell Kingc8ebae32011-01-11 19:35:53 +00001139 /* Terminate the DMA transfer */
Ludovic Barrecfccc6a2018-10-08 14:08:40 +02001140 mmci_dma_error(host);
Russell Kingc8ebae32011-01-11 19:35:53 +00001141
Russell Kingc8afc9d2011-02-04 09:19:46 +00001142 /*
1143 * Calculate how far we are into the transfer. Note that
1144 * the data counter gives the number of bytes transferred
1145 * on the MMC bus, not on the host side. On reads, this
1146 * can be as much as a FIFO-worth of data ahead. This
1147 * matters for FIFO overruns only.
1148 */
Ludovic Barreb79220b2018-10-08 14:08:49 +02001149 if (!host->variant->datacnt_useless) {
1150 remain = readl(host->base + MMCIDATACNT);
1151 success = data->blksz * data->blocks - remain;
1152 } else {
1153 success = 0;
1154 }
Linus Walleij8cb28152011-01-24 15:22:13 +01001155
Russell Kingc8afc9d2011-02-04 09:19:46 +00001156 dev_dbg(mmc_dev(host->mmc), "MCI ERROR IRQ, status 0x%08x at 0x%08x\n",
Ludovic Barredaf97132018-10-08 14:08:44 +02001157 status_err, success);
1158 if (status_err & MCI_DATACRCFAIL) {
Linus Walleij8cb28152011-01-24 15:22:13 +01001159 /* Last block was not successful */
Russell Kingc8afc9d2011-02-04 09:19:46 +00001160 success -= 1;
Pierre Ossman17b04292007-07-22 22:18:46 +02001161 data->error = -EILSEQ;
Ludovic Barredaf97132018-10-08 14:08:44 +02001162 } else if (status_err & MCI_DATATIMEOUT) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001163 data->error = -ETIMEDOUT;
Ludovic Barredaf97132018-10-08 14:08:44 +02001164 } else if (status_err & MCI_STARTBITERR) {
Linus Walleij757df742011-06-30 15:10:21 +01001165 data->error = -ECOMM;
Ludovic Barredaf97132018-10-08 14:08:44 +02001166 } else if (status_err & MCI_TXUNDERRUN) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001167 data->error = -EIO;
Ludovic Barredaf97132018-10-08 14:08:44 +02001168 } else if (status_err & MCI_RXOVERRUN) {
Russell Kingc8afc9d2011-02-04 09:19:46 +00001169 if (success > host->variant->fifosize)
1170 success -= host->variant->fifosize;
1171 else
1172 success = 0;
Linus Walleij8cb28152011-01-24 15:22:13 +01001173 data->error = -EIO;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001174 }
Russell King51d43752011-01-27 10:56:52 +00001175 data->bytes_xfered = round_down(success, data->blksz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 }
Linus Walleijf20f8f212010-10-19 13:41:24 +01001177
Linus Walleij8cb28152011-01-24 15:22:13 +01001178 if (status & MCI_DATABLOCKEND)
1179 dev_err(mmc_dev(host->mmc), "stray MCI_DATABLOCKEND interrupt\n");
Linus Walleijf20f8f212010-10-19 13:41:24 +01001180
Russell Kingccff9b52011-01-30 21:03:50 +00001181 if (status & MCI_DATAEND || data->error) {
Ludovic Barrecdea1942018-09-21 11:45:56 +02001182 mmci_dma_finalize(host, data);
1183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 mmci_stop_data(host);
1185
Linus Walleij8cb28152011-01-24 15:22:13 +01001186 if (!data->error)
1187 /* The error clause is handled above, success! */
Russell King51d43752011-01-27 10:56:52 +00001188 data->bytes_xfered = data->blksz * data->blocks;
Linus Walleijf20f8f212010-10-19 13:41:24 +01001189
Ulf Hanssone9968c62019-01-29 15:35:56 +01001190 if (!data->stop) {
1191 if (host->variant->cmdreg_stop && data->error)
1192 mmci_stop_command(host);
1193 else
1194 mmci_request_end(host, data->mrq);
1195 } else if (host->mrq->sbc && !data->error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 mmci_request_end(host, data->mrq);
Ulf Hanssone9968c62019-01-29 15:35:56 +01001197 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 mmci_start_command(host, data->stop, 0);
Ulf Hanssone9968c62019-01-29 15:35:56 +01001199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 }
1201}
1202
1203static void
1204mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
1205 unsigned int status)
1206{
1207 void __iomem *base = host->base;
Ludovic Barre812513c2019-04-26 09:46:34 +02001208 bool sbc, busy_resp;
Ulf Hanssonad82bfe2014-06-12 15:01:57 +02001209
1210 if (!cmd)
1211 return;
1212
1213 sbc = (cmd == host->mrq->sbc);
Ludovic Barre812513c2019-04-26 09:46:34 +02001214 busy_resp = !!(cmd->flags & MMC_RSP_BUSY);
Ulf Hanssonad82bfe2014-06-12 15:01:57 +02001215
Linus Walleij49adc0c2016-10-25 11:06:06 +02001216 /*
1217 * We need to be one of these interrupts to be considered worth
1218 * handling. Note that we tag on any latent IRQs postponed
1219 * due to waiting for busy status.
1220 */
1221 if (!((status|host->busy_status) &
1222 (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT|MCI_CMDSENT|MCI_CMDRESPEND)))
Ulf Hanssonad82bfe2014-06-12 15:01:57 +02001223 return;
Ulf Hansson8d94b542014-01-13 16:49:31 +01001224
Linus Walleij49adc0c2016-10-25 11:06:06 +02001225 /*
1226 * ST Micro variant: handle busy detection.
1227 */
Ludovic Barre812513c2019-04-26 09:46:34 +02001228 if (busy_resp && host->variant->busy_detect) {
Ulf Hansson8d94b542014-01-13 16:49:31 +01001229
Linus Walleij49adc0c2016-10-25 11:06:06 +02001230 /* We are busy with a command, return */
1231 if (host->busy_status &&
1232 (status & host->variant->busy_detect_flag))
1233 return;
Ulf Hansson8d94b542014-01-13 16:49:31 +01001234
Linus Walleij49adc0c2016-10-25 11:06:06 +02001235 /*
1236 * We were not busy, but we now got a busy response on
1237 * something that was not an error, and we double-check
1238 * that the special busy status bit is still set before
1239 * proceeding.
1240 */
Ludovic Barre812513c2019-04-26 09:46:34 +02001241 if (!host->busy_status &&
Linus Walleij49adc0c2016-10-25 11:06:06 +02001242 !(status & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT)) &&
1243 (readl(base + MMCISTATUS) & host->variant->busy_detect_flag)) {
Jean-Nicolas Graux5cad24d2017-02-07 12:12:41 +01001244
1245 /* Clear the busy start IRQ */
1246 writel(host->variant->busy_detect_mask,
1247 host->base + MMCICLEAR);
1248
1249 /* Unmask the busy end IRQ */
Linus Walleij49adc0c2016-10-25 11:06:06 +02001250 writel(readl(base + MMCIMASK0) |
1251 host->variant->busy_detect_mask,
1252 base + MMCIMASK0);
1253 /*
1254 * Now cache the last response status code (until
1255 * the busy bit goes low), and return.
1256 */
1257 host->busy_status =
1258 status & (MCI_CMDSENT|MCI_CMDRESPEND);
1259 return;
1260 }
1261
1262 /*
1263 * At this point we are not busy with a command, we have
Jean-Nicolas Graux5cad24d2017-02-07 12:12:41 +01001264 * not received a new busy request, clear and mask the busy
1265 * end IRQ and fall through to process the IRQ.
Linus Walleij49adc0c2016-10-25 11:06:06 +02001266 */
1267 if (host->busy_status) {
Jean-Nicolas Graux5cad24d2017-02-07 12:12:41 +01001268
1269 writel(host->variant->busy_detect_mask,
1270 host->base + MMCICLEAR);
1271
Linus Walleij49adc0c2016-10-25 11:06:06 +02001272 writel(readl(base + MMCIMASK0) &
1273 ~host->variant->busy_detect_mask,
1274 base + MMCIMASK0);
1275 host->busy_status = 0;
1276 }
Ulf Hansson8d94b542014-01-13 16:49:31 +01001277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
1279 host->cmd = NULL;
1280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 if (status & MCI_CMDTIMEOUT) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001282 cmd->error = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001284 cmd->error = -EILSEQ;
Russell King - ARM Linux9047b432011-01-11 16:35:56 +00001285 } else {
1286 cmd->resp[0] = readl(base + MMCIRESPONSE0);
1287 cmd->resp[1] = readl(base + MMCIRESPONSE1);
1288 cmd->resp[2] = readl(base + MMCIRESPONSE2);
1289 cmd->resp[3] = readl(base + MMCIRESPONSE3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 }
1291
Ulf Hansson024629c2013-05-13 15:40:56 +01001292 if ((!sbc && !cmd->data) || cmd->error) {
Ulf Hansson3b6e3c72011-12-13 16:58:43 +01001293 if (host->data) {
1294 /* Terminate the DMA transfer */
Ludovic Barrecfccc6a2018-10-08 14:08:40 +02001295 mmci_dma_error(host);
Ludovic Barre7b2a6d52018-09-21 11:45:55 +02001296
Russell Kinge47c2222007-01-08 16:42:51 +00001297 mmci_stop_data(host);
Ulf Hanssone9968c62019-01-29 15:35:56 +01001298 if (host->variant->cmdreg_stop && cmd->error) {
1299 mmci_stop_command(host);
1300 return;
1301 }
Ulf Hansson3b6e3c72011-12-13 16:58:43 +01001302 }
Ulf Hansson024629c2013-05-13 15:40:56 +01001303 mmci_request_end(host, host->mrq);
1304 } else if (sbc) {
1305 mmci_start_command(host, host->mrq->cmd, 0);
Ludovic Barred2141542018-10-08 14:08:48 +02001306 } else if (!host->variant->datactrl_first &&
1307 !(cmd->data->flags & MMC_DATA_READ)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 mmci_start_data(host, cmd->data);
1309 }
1310}
1311
Srinivas Kandagatla9c34b732014-06-02 10:10:04 +01001312static int mmci_get_rx_fifocnt(struct mmci_host *host, u32 status, int remain)
1313{
1314 return remain - (readl(host->base + MMCIFIFOCNT) << 2);
1315}
1316
1317static int mmci_qcom_get_rx_fifocnt(struct mmci_host *host, u32 status, int r)
1318{
1319 /*
1320 * on qcom SDCC4 only 8 words are used in each burst so only 8 addresses
1321 * from the fifo range should be used
1322 */
1323 if (status & MCI_RXFIFOHALFFULL)
1324 return host->variant->fifohalfsize;
1325 else if (status & MCI_RXDATAAVLBL)
1326 return 4;
1327
1328 return 0;
1329}
1330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331static int mmci_pio_read(struct mmci_host *host, char *buffer, unsigned int remain)
1332{
1333 void __iomem *base = host->base;
1334 char *ptr = buffer;
Srinivas Kandagatla9c34b732014-06-02 10:10:04 +01001335 u32 status = readl(host->base + MMCISTATUS);
Linus Walleij26eed9a2008-04-26 23:39:44 +01001336 int host_remain = host->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338 do {
Srinivas Kandagatla9c34b732014-06-02 10:10:04 +01001339 int count = host->get_rx_fifocnt(host, status, host_remain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341 if (count > remain)
1342 count = remain;
1343
1344 if (count <= 0)
1345 break;
1346
Ulf Hansson393e5e22011-12-13 17:08:04 +01001347 /*
1348 * SDIO especially may want to send something that is
1349 * not divisible by 4 (as opposed to card sectors
1350 * etc). Therefore make sure to always read the last bytes
1351 * while only doing full 32-bit reads towards the FIFO.
1352 */
1353 if (unlikely(count & 0x3)) {
1354 if (count < 4) {
1355 unsigned char buf[4];
Davide Ciminaghi4b85da02012-12-10 14:47:21 +01001356 ioread32_rep(base + MMCIFIFO, buf, 1);
Ulf Hansson393e5e22011-12-13 17:08:04 +01001357 memcpy(ptr, buf, count);
1358 } else {
Davide Ciminaghi4b85da02012-12-10 14:47:21 +01001359 ioread32_rep(base + MMCIFIFO, ptr, count >> 2);
Ulf Hansson393e5e22011-12-13 17:08:04 +01001360 count &= ~0x3;
1361 }
1362 } else {
Davide Ciminaghi4b85da02012-12-10 14:47:21 +01001363 ioread32_rep(base + MMCIFIFO, ptr, count >> 2);
Ulf Hansson393e5e22011-12-13 17:08:04 +01001364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366 ptr += count;
1367 remain -= count;
Linus Walleij26eed9a2008-04-26 23:39:44 +01001368 host_remain -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370 if (remain == 0)
1371 break;
1372
1373 status = readl(base + MMCISTATUS);
1374 } while (status & MCI_RXDATAAVLBL);
1375
1376 return ptr - buffer;
1377}
1378
1379static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status)
1380{
Rabin Vincent8301bb62010-08-09 12:57:30 +01001381 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 void __iomem *base = host->base;
1383 char *ptr = buffer;
1384
1385 do {
1386 unsigned int count, maxcnt;
1387
Rabin Vincent8301bb62010-08-09 12:57:30 +01001388 maxcnt = status & MCI_TXFIFOEMPTY ?
1389 variant->fifosize : variant->fifohalfsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 count = min(remain, maxcnt);
1391
Linus Walleij34177802010-10-19 12:43:58 +01001392 /*
Linus Walleij34177802010-10-19 12:43:58 +01001393 * SDIO especially may want to send something that is
1394 * not divisible by 4 (as opposed to card sectors
1395 * etc), and the FIFO only accept full 32-bit writes.
1396 * So compensate by adding +3 on the count, a single
1397 * byte become a 32bit write, 7 bytes will be two
1398 * 32bit writes etc.
1399 */
Davide Ciminaghi4b85da02012-12-10 14:47:21 +01001400 iowrite32_rep(base + MMCIFIFO, ptr, (count + 3) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402 ptr += count;
1403 remain -= count;
1404
1405 if (remain == 0)
1406 break;
1407
1408 status = readl(base + MMCISTATUS);
1409 } while (status & MCI_TXFIFOHALFEMPTY);
1410
1411 return ptr - buffer;
1412}
1413
1414/*
1415 * PIO data transfer IRQ handler.
1416 */
David Howells7d12e782006-10-05 14:55:46 +01001417static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
1419 struct mmci_host *host = dev_id;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001420 struct sg_mapping_iter *sg_miter = &host->sg_miter;
Rabin Vincent8301bb62010-08-09 12:57:30 +01001421 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 void __iomem *base = host->base;
1423 u32 status;
1424
1425 status = readl(base + MMCISTATUS);
1426
Linus Walleij64de0282010-02-19 01:09:10 +01001427 dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
1429 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 unsigned int remain, len;
1431 char *buffer;
1432
1433 /*
1434 * For write, we only need to test the half-empty flag
1435 * here - if the FIFO is completely empty, then by
1436 * definition it is more than half empty.
1437 *
1438 * For read, check for data available.
1439 */
1440 if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL)))
1441 break;
1442
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001443 if (!sg_miter_next(sg_miter))
1444 break;
1445
1446 buffer = sg_miter->addr;
1447 remain = sg_miter->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449 len = 0;
1450 if (status & MCI_RXACTIVE)
1451 len = mmci_pio_read(host, buffer, remain);
1452 if (status & MCI_TXACTIVE)
1453 len = mmci_pio_write(host, buffer, remain, status);
1454
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001455 sg_miter->consumed = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 host->size -= len;
1458 remain -= len;
1459
1460 if (remain)
1461 break;
1462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 status = readl(base + MMCISTATUS);
1464 } while (1);
1465
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001466 sg_miter_stop(sg_miter);
1467
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 /*
Russell Kingc4d877c2011-01-27 09:50:13 +00001469 * If we have less than the fifo 'half-full' threshold to transfer,
1470 * trigger a PIO interrupt as soon as any data is available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 */
Russell Kingc4d877c2011-01-27 09:50:13 +00001472 if (status & MCI_RXACTIVE && host->size < variant->fifohalfsize)
Linus Walleij2686b4b2010-10-19 12:39:48 +01001473 mmci_set_mask1(host, MCI_RXDATAAVLBLMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
1475 /*
1476 * If we run out of data, disable the data IRQs; this
1477 * prevents a race where the FIFO becomes empty before
1478 * the chip itself has disabled the data path, and
1479 * stops us racing with our data end IRQ.
1480 */
1481 if (host->size == 0) {
Linus Walleij2686b4b2010-10-19 12:39:48 +01001482 mmci_set_mask1(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0);
1484 }
1485
1486 return IRQ_HANDLED;
1487}
1488
1489/*
1490 * Handle completion of command and data transfers.
1491 */
David Howells7d12e782006-10-05 14:55:46 +01001492static irqreturn_t mmci_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
1494 struct mmci_host *host = dev_id;
1495 u32 status;
1496 int ret = 0;
1497
1498 spin_lock(&host->lock);
1499
1500 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 status = readl(host->base + MMCISTATUS);
Linus Walleij2686b4b2010-10-19 12:39:48 +01001502
1503 if (host->singleirq) {
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +01001504 if (status & host->mask1_reg)
Linus Walleij2686b4b2010-10-19 12:39:48 +01001505 mmci_pio_irq(irq, dev_id);
1506
Ludovic Barre59db5e22018-10-08 14:08:47 +02001507 status &= ~host->variant->irq_pio_mask;
Linus Walleij2686b4b2010-10-19 12:39:48 +01001508 }
1509
Ulf Hansson8d94b542014-01-13 16:49:31 +01001510 /*
Jean-Nicolas Graux5cad24d2017-02-07 12:12:41 +01001511 * We intentionally clear the MCI_ST_CARDBUSY IRQ (if it's
1512 * enabled) in mmci_cmd_irq() function where ST Micro busy
1513 * detection variant is handled. Considering the HW seems to be
1514 * triggering the IRQ on both edges while monitoring DAT0 for
1515 * busy completion and that same status bit is used to monitor
1516 * start and end of busy detection, special care must be taken
1517 * to make sure that both start and end interrupts are always
1518 * cleared one after the other.
Ulf Hansson8d94b542014-01-13 16:49:31 +01001519 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 status &= readl(host->base + MMCIMASK0);
Jean-Nicolas Graux5cad24d2017-02-07 12:12:41 +01001521 if (host->variant->busy_detect)
1522 writel(status & ~host->variant->busy_detect_mask,
1523 host->base + MMCICLEAR);
1524 else
1525 writel(status, host->base + MMCICLEAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Linus Walleij64de0282010-02-19 01:09:10 +01001527 dev_dbg(mmc_dev(host->mmc), "irq0 (data+cmd) %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Ulf Hansson78782892014-06-13 13:21:38 +02001529 if (host->variant->reversed_irq_handling) {
1530 mmci_data_irq(host, host->data, status);
1531 mmci_cmd_irq(host, host->cmd, status);
1532 } else {
1533 mmci_cmd_irq(host, host->cmd, status);
1534 mmci_data_irq(host, host->data, status);
1535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Linus Walleij49adc0c2016-10-25 11:06:06 +02001537 /*
Ludovic Barre8520ce12019-04-26 09:46:35 +02001538 * Busy detection has been handled by mmci_cmd_irq() above.
1539 * Clear the status bit to prevent polling in IRQ context.
Linus Walleij49adc0c2016-10-25 11:06:06 +02001540 */
Ludovic Barre8520ce12019-04-26 09:46:35 +02001541 if (host->variant->busy_detect_flag)
Linus Walleij49adc0c2016-10-25 11:06:06 +02001542 status &= ~host->variant->busy_detect_flag;
Ulf Hansson8d94b542014-01-13 16:49:31 +01001543
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 ret = 1;
1545 } while (status);
1546
1547 spin_unlock(&host->lock);
1548
1549 return IRQ_RETVAL(ret);
1550}
1551
1552static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1553{
1554 struct mmci_host *host = mmc_priv(mmc);
Linus Walleij9e943022008-10-24 21:17:50 +01001555 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
1557 WARN_ON(host->mrq != NULL);
1558
Ulf Hansson653a7612013-01-21 21:29:34 +01001559 mrq->cmd->error = mmci_validate_data(host, mrq->data);
1560 if (mrq->cmd->error) {
Pierre Ossman255d01a2007-07-24 20:38:53 +02001561 mmc_request_done(mmc, mrq);
1562 return;
1563 }
1564
Linus Walleij9e943022008-10-24 21:17:50 +01001565 spin_lock_irqsave(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
1567 host->mrq = mrq;
1568
Per Forlin58c7ccb2011-07-01 18:55:24 +02001569 if (mrq->data)
1570 mmci_get_next_data(host, mrq->data);
1571
Ludovic Barred2141542018-10-08 14:08:48 +02001572 if (mrq->data &&
1573 (host->variant->datactrl_first || mrq->data->flags & MMC_DATA_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 mmci_start_data(host, mrq->data);
1575
Ulf Hansson024629c2013-05-13 15:40:56 +01001576 if (mrq->sbc)
1577 mmci_start_command(host, mrq->sbc, 0);
1578 else
1579 mmci_start_command(host, mrq->cmd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
Linus Walleij9e943022008-10-24 21:17:50 +01001581 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582}
1583
1584static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1585{
1586 struct mmci_host *host = mmc_priv(mmc);
Ulf Hansson7d72a1d2011-12-13 16:54:55 +01001587 struct variant_data *variant = host->variant;
Linus Walleija6a64642009-09-14 12:56:14 +01001588 u32 pwr = 0;
1589 unsigned long flags;
Lee Jonesdb90f912013-05-03 12:52:12 +01001590 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Ulf Hanssonbc521812011-12-13 16:57:55 +01001592 if (host->plat->ios_handler &&
1593 host->plat->ios_handler(mmc_dev(mmc), ios))
1594 dev_err(mmc_dev(mmc), "platform ios_handler failed\n");
1595
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 switch (ios->power_mode) {
1597 case MMC_POWER_OFF:
Ulf Hansson599c1d52013-01-07 16:22:50 +01001598 if (!IS_ERR(mmc->supply.vmmc))
1599 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Lee Jones237fb5e2013-01-31 11:27:52 +00001600
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001601 if (!IS_ERR(mmc->supply.vqmmc) && host->vqmmc_enabled) {
Lee Jones237fb5e2013-01-31 11:27:52 +00001602 regulator_disable(mmc->supply.vqmmc);
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001603 host->vqmmc_enabled = false;
1604 }
Lee Jones237fb5e2013-01-31 11:27:52 +00001605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 break;
1607 case MMC_POWER_UP:
Ulf Hansson599c1d52013-01-07 16:22:50 +01001608 if (!IS_ERR(mmc->supply.vmmc))
1609 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd);
1610
Ulf Hansson7d72a1d2011-12-13 16:54:55 +01001611 /*
1612 * The ST Micro variant doesn't have the PL180s MCI_PWR_UP
1613 * and instead uses MCI_PWR_ON so apply whatever value is
1614 * configured in the variant data.
1615 */
1616 pwr |= variant->pwrreg_powerup;
1617
1618 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 case MMC_POWER_ON:
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001620 if (!IS_ERR(mmc->supply.vqmmc) && !host->vqmmc_enabled) {
Lee Jonesdb90f912013-05-03 12:52:12 +01001621 ret = regulator_enable(mmc->supply.vqmmc);
1622 if (ret < 0)
1623 dev_err(mmc_dev(mmc),
1624 "failed to enable vqmmc regulator\n");
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001625 else
1626 host->vqmmc_enabled = true;
Lee Jonesdb90f912013-05-03 12:52:12 +01001627 }
Lee Jones237fb5e2013-01-31 11:27:52 +00001628
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 pwr |= MCI_PWR_ON;
1630 break;
1631 }
1632
Ulf Hansson4d1a3a02011-12-13 16:57:07 +01001633 if (variant->signal_direction && ios->power_mode != MMC_POWER_OFF) {
1634 /*
1635 * The ST Micro variant has some additional bits
1636 * indicating signal direction for the signals in
1637 * the SD/MMC bus and feedback-clock usage.
1638 */
Ulf Hansson4593df22014-03-21 10:13:05 +01001639 pwr |= host->pwr_reg_add;
Ulf Hansson4d1a3a02011-12-13 16:57:07 +01001640
1641 if (ios->bus_width == MMC_BUS_WIDTH_4)
1642 pwr &= ~MCI_ST_DATA74DIREN;
1643 else if (ios->bus_width == MMC_BUS_WIDTH_1)
1644 pwr &= (~MCI_ST_DATA74DIREN &
1645 ~MCI_ST_DATA31DIREN &
1646 ~MCI_ST_DATA2DIREN);
1647 }
1648
Patrice Chotardf9bb3042018-01-18 15:34:20 +01001649 if (variant->opendrain) {
1650 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
1651 pwr |= variant->opendrain;
1652 } else {
1653 /*
1654 * If the variant cannot configure the pads by its own, then we
1655 * expect the pinctrl to be able to do that for us
1656 */
1657 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
1658 pinctrl_select_state(host->pinctrl, host->pins_opendrain);
1659 else
1660 pinctrl_select_state(host->pinctrl, host->pins_default);
1661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Ulf Hanssonf4670da2013-01-09 17:19:54 +01001663 /*
1664 * If clock = 0 and the variant requires the MMCIPOWER to be used for
1665 * gating the clock, the MCI_PWR_ON bit is cleared.
1666 */
1667 if (!ios->clock && variant->pwrreg_clkgate)
1668 pwr &= ~MCI_PWR_ON;
1669
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001670 if (host->variant->explicit_mclk_control &&
1671 ios->clock != host->clock_cache) {
1672 ret = clk_set_rate(host->clk, ios->clock);
1673 if (ret < 0)
1674 dev_err(mmc_dev(host->mmc),
1675 "Error setting clock rate (%d)\n", ret);
1676 else
1677 host->mclk = clk_get_rate(host->clk);
1678 }
1679 host->clock_cache = ios->clock;
1680
Linus Walleija6a64642009-09-14 12:56:14 +01001681 spin_lock_irqsave(&host->lock, flags);
1682
Ludovic Barrecd3ee8c2018-10-08 14:08:42 +02001683 if (host->ops && host->ops->set_clkreg)
1684 host->ops->set_clkreg(host, ios->clock);
1685 else
1686 mmci_set_clkreg(host, ios->clock);
1687
1688 if (host->ops && host->ops->set_pwrreg)
1689 host->ops->set_pwrreg(host, pwr);
1690 else
1691 mmci_write_pwrreg(host, pwr);
1692
Ulf Hanssonf829c042013-09-04 09:01:15 +01001693 mmci_reg_delay(host);
Linus Walleija6a64642009-09-14 12:56:14 +01001694
1695 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696}
1697
Russell King89001442009-07-09 15:16:07 +01001698static int mmci_get_cd(struct mmc_host *mmc)
1699{
1700 struct mmci_host *host = mmc_priv(mmc);
Rabin Vincent29719442010-08-09 12:54:43 +01001701 struct mmci_platform_data *plat = host->plat;
Ulf Hanssond2762092014-03-17 13:56:19 +01001702 unsigned int status = mmc_gpio_get_cd(mmc);
Russell King89001442009-07-09 15:16:07 +01001703
Ulf Hanssond2762092014-03-17 13:56:19 +01001704 if (status == -ENOSYS) {
Rabin Vincent4b8caec2010-08-09 12:56:40 +01001705 if (!plat->status)
1706 return 1; /* Assume always present */
1707
Rabin Vincent29719442010-08-09 12:54:43 +01001708 status = plat->status(mmc_dev(host->mmc));
Ulf Hanssond2762092014-03-17 13:56:19 +01001709 }
Russell King74bc8092010-07-29 15:58:59 +01001710 return status;
Russell King89001442009-07-09 15:16:07 +01001711}
1712
Ulf Hansson0f3ed7f2013-05-15 20:47:33 +01001713static int mmci_sig_volt_switch(struct mmc_host *mmc, struct mmc_ios *ios)
1714{
1715 int ret = 0;
1716
1717 if (!IS_ERR(mmc->supply.vqmmc)) {
1718
Ulf Hansson0f3ed7f2013-05-15 20:47:33 +01001719 switch (ios->signal_voltage) {
1720 case MMC_SIGNAL_VOLTAGE_330:
1721 ret = regulator_set_voltage(mmc->supply.vqmmc,
1722 2700000, 3600000);
1723 break;
1724 case MMC_SIGNAL_VOLTAGE_180:
1725 ret = regulator_set_voltage(mmc->supply.vqmmc,
1726 1700000, 1950000);
1727 break;
1728 case MMC_SIGNAL_VOLTAGE_120:
1729 ret = regulator_set_voltage(mmc->supply.vqmmc,
1730 1100000, 1300000);
1731 break;
1732 }
1733
1734 if (ret)
1735 dev_warn(mmc_dev(mmc), "Voltage switch failed\n");
Ulf Hansson0f3ed7f2013-05-15 20:47:33 +01001736 }
1737
1738 return ret;
1739}
1740
Ulf Hansson01259622013-05-15 20:53:22 +01001741static struct mmc_host_ops mmci_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 .request = mmci_request,
Per Forlin58c7ccb2011-07-01 18:55:24 +02001743 .pre_req = mmci_pre_request,
1744 .post_req = mmci_post_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 .set_ios = mmci_set_ios,
Ulf Hanssond2762092014-03-17 13:56:19 +01001746 .get_ro = mmc_gpio_get_ro,
Russell King89001442009-07-09 15:16:07 +01001747 .get_cd = mmci_get_cd,
Ulf Hansson0f3ed7f2013-05-15 20:47:33 +01001748 .start_signal_voltage_switch = mmci_sig_volt_switch,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749};
1750
Ulf Hansson78f87df2014-03-17 15:53:07 +01001751static int mmci_of_parse(struct device_node *np, struct mmc_host *mmc)
1752{
Ulf Hansson4593df22014-03-21 10:13:05 +01001753 struct mmci_host *host = mmc_priv(mmc);
Ulf Hansson78f87df2014-03-17 15:53:07 +01001754 int ret = mmc_of_parse(mmc);
Lee Jones000bc9d2012-04-16 10:18:43 +01001755
Ulf Hansson78f87df2014-03-17 15:53:07 +01001756 if (ret)
1757 return ret;
Lee Jones000bc9d2012-04-16 10:18:43 +01001758
Ulf Hansson4593df22014-03-21 10:13:05 +01001759 if (of_get_property(np, "st,sig-dir-dat0", NULL))
1760 host->pwr_reg_add |= MCI_ST_DATA0DIREN;
1761 if (of_get_property(np, "st,sig-dir-dat2", NULL))
1762 host->pwr_reg_add |= MCI_ST_DATA2DIREN;
1763 if (of_get_property(np, "st,sig-dir-dat31", NULL))
1764 host->pwr_reg_add |= MCI_ST_DATA31DIREN;
1765 if (of_get_property(np, "st,sig-dir-dat74", NULL))
1766 host->pwr_reg_add |= MCI_ST_DATA74DIREN;
1767 if (of_get_property(np, "st,sig-dir-cmd", NULL))
1768 host->pwr_reg_add |= MCI_ST_CMDDIREN;
1769 if (of_get_property(np, "st,sig-pin-fbclk", NULL))
1770 host->pwr_reg_add |= MCI_ST_FBCLKEN;
Ludovic Barre46b723d2018-10-08 14:08:55 +02001771 if (of_get_property(np, "st,sig-dir", NULL))
1772 host->pwr_reg_add |= MCI_STM32_DIRPOL;
1773 if (of_get_property(np, "st,neg-edge", NULL))
1774 host->clk_reg_add |= MCI_STM32_CLK_NEGEDGE;
1775 if (of_get_property(np, "st,use-ckin", NULL))
1776 host->clk_reg_add |= MCI_STM32_CLK_SELCKIN;
Ulf Hansson4593df22014-03-21 10:13:05 +01001777
Lee Jones000bc9d2012-04-16 10:18:43 +01001778 if (of_get_property(np, "mmc-cap-mmc-highspeed", NULL))
Ulf Hansson78f87df2014-03-17 15:53:07 +01001779 mmc->caps |= MMC_CAP_MMC_HIGHSPEED;
Lee Jones000bc9d2012-04-16 10:18:43 +01001780 if (of_get_property(np, "mmc-cap-sd-highspeed", NULL))
Ulf Hansson78f87df2014-03-17 15:53:07 +01001781 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
Lee Jones000bc9d2012-04-16 10:18:43 +01001782
Ulf Hansson78f87df2014-03-17 15:53:07 +01001783 return 0;
Lee Jones000bc9d2012-04-16 10:18:43 +01001784}
Lee Jones000bc9d2012-04-16 10:18:43 +01001785
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001786static int mmci_probe(struct amba_device *dev,
Russell Kingaa25afa2011-02-19 15:55:00 +00001787 const struct amba_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788{
Linus Walleij6ef297f2009-09-22 14:29:36 +01001789 struct mmci_platform_data *plat = dev->dev.platform_data;
Lee Jones000bc9d2012-04-16 10:18:43 +01001790 struct device_node *np = dev->dev.of_node;
Rabin Vincent4956e102010-07-21 12:54:40 +01001791 struct variant_data *variant = id->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 struct mmci_host *host;
1793 struct mmc_host *mmc;
1794 int ret;
1795
Lee Jones000bc9d2012-04-16 10:18:43 +01001796 /* Must have platform data or Device Tree. */
1797 if (!plat && !np) {
1798 dev_err(&dev->dev, "No plat data or DT found\n");
1799 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 }
1801
Lee Jonesb9b52912012-06-12 10:49:51 +01001802 if (!plat) {
1803 plat = devm_kzalloc(&dev->dev, sizeof(*plat), GFP_KERNEL);
1804 if (!plat)
1805 return -ENOMEM;
1806 }
1807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
Ulf Hanssonef289982014-03-17 13:56:32 +01001809 if (!mmc)
1810 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
Ulf Hansson78f87df2014-03-17 15:53:07 +01001812 ret = mmci_of_parse(np, mmc);
1813 if (ret)
1814 goto host_free;
1815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 host = mmc_priv(mmc);
Rabin Vincent4ea580f2009-04-17 08:44:19 +05301817 host->mmc = mmc;
Russell King012b7d32009-07-09 15:13:56 +01001818
Patrice Chotardf9bb3042018-01-18 15:34:20 +01001819 /*
1820 * Some variant (STM32) doesn't have opendrain bit, nevertheless
1821 * pins can be set accordingly using pinctrl
1822 */
1823 if (!variant->opendrain) {
1824 host->pinctrl = devm_pinctrl_get(&dev->dev);
1825 if (IS_ERR(host->pinctrl)) {
1826 dev_err(&dev->dev, "failed to get pinctrl");
Wei Yongjun310eb252018-01-23 02:09:13 +00001827 ret = PTR_ERR(host->pinctrl);
Patrice Chotardf9bb3042018-01-18 15:34:20 +01001828 goto host_free;
1829 }
1830
1831 host->pins_default = pinctrl_lookup_state(host->pinctrl,
1832 PINCTRL_STATE_DEFAULT);
1833 if (IS_ERR(host->pins_default)) {
1834 dev_err(mmc_dev(mmc), "Can't select default pins\n");
Wei Yongjun310eb252018-01-23 02:09:13 +00001835 ret = PTR_ERR(host->pins_default);
Patrice Chotardf9bb3042018-01-18 15:34:20 +01001836 goto host_free;
1837 }
1838
1839 host->pins_opendrain = pinctrl_lookup_state(host->pinctrl,
1840 MMCI_PINCTRL_STATE_OPENDRAIN);
1841 if (IS_ERR(host->pins_opendrain)) {
1842 dev_err(mmc_dev(mmc), "Can't select opendrain pins\n");
Wei Yongjun310eb252018-01-23 02:09:13 +00001843 ret = PTR_ERR(host->pins_opendrain);
Patrice Chotardf9bb3042018-01-18 15:34:20 +01001844 goto host_free;
1845 }
1846 }
1847
Russell King012b7d32009-07-09 15:13:56 +01001848 host->hw_designer = amba_manf(dev);
1849 host->hw_revision = amba_rev(dev);
Linus Walleij64de0282010-02-19 01:09:10 +01001850 dev_dbg(mmc_dev(mmc), "designer ID = 0x%02x\n", host->hw_designer);
1851 dev_dbg(mmc_dev(mmc), "revision = 0x%01x\n", host->hw_revision);
Russell King012b7d32009-07-09 15:13:56 +01001852
Ulf Hansson665ba562013-05-13 15:39:17 +01001853 host->clk = devm_clk_get(&dev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 if (IS_ERR(host->clk)) {
1855 ret = PTR_ERR(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 goto host_free;
1857 }
1858
Julia Lawallac940932012-08-26 16:00:59 +00001859 ret = clk_prepare_enable(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 if (ret)
Ulf Hansson665ba562013-05-13 15:39:17 +01001861 goto host_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
Srinivas Kandagatla9c34b732014-06-02 10:10:04 +01001863 if (variant->qcom_fifo)
1864 host->get_rx_fifocnt = mmci_qcom_get_rx_fifocnt;
1865 else
1866 host->get_rx_fifocnt = mmci_get_rx_fifocnt;
1867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 host->plat = plat;
Rabin Vincent4956e102010-07-21 12:54:40 +01001869 host->variant = variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 host->mclk = clk_get_rate(host->clk);
Linus Walleijc8df9a52008-04-29 09:34:07 +01001871 /*
1872 * According to the spec, mclk is max 100 MHz,
1873 * so we try to adjust the clock down to this,
1874 * (if possible).
1875 */
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +01001876 if (host->mclk > variant->f_max) {
1877 ret = clk_set_rate(host->clk, variant->f_max);
Linus Walleijc8df9a52008-04-29 09:34:07 +01001878 if (ret < 0)
1879 goto clk_disable;
1880 host->mclk = clk_get_rate(host->clk);
Linus Walleij64de0282010-02-19 01:09:10 +01001881 dev_dbg(mmc_dev(mmc), "eventual mclk rate: %u Hz\n",
1882 host->mclk);
Linus Walleijc8df9a52008-04-29 09:34:07 +01001883 }
Ulf Hanssonef289982014-03-17 13:56:32 +01001884
Russell Kingc8ebae32011-01-11 19:35:53 +00001885 host->phybase = dev->res.start;
Ulf Hanssonef289982014-03-17 13:56:32 +01001886 host->base = devm_ioremap_resource(&dev->dev, &dev->res);
1887 if (IS_ERR(host->base)) {
1888 ret = PTR_ERR(host->base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 goto clk_disable;
1890 }
1891
Ulf Hanssoned9067f2018-07-13 13:15:23 +02001892 if (variant->init)
1893 variant->init(host);
1894
Linus Walleij7f294e42011-07-08 09:57:15 +01001895 /*
1896 * The ARM and ST versions of the block have slightly different
1897 * clock divider equations which means that the minimum divider
1898 * differs too.
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001899 * on Qualcomm like controllers get the nearest minimum clock to 100Khz
Linus Walleij7f294e42011-07-08 09:57:15 +01001900 */
1901 if (variant->st_clkdiv)
1902 mmc->f_min = DIV_ROUND_UP(host->mclk, 257);
Ludovic Barre00e930d2018-10-08 14:08:52 +02001903 else if (variant->stm32_clkdiv)
1904 mmc->f_min = DIV_ROUND_UP(host->mclk, 2046);
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001905 else if (variant->explicit_mclk_control)
1906 mmc->f_min = clk_round_rate(host->clk, 100000);
Linus Walleij7f294e42011-07-08 09:57:15 +01001907 else
1908 mmc->f_min = DIV_ROUND_UP(host->mclk, 512);
Linus Walleij808d97c2010-04-08 07:39:38 +01001909 /*
Ulf Hansson78f87df2014-03-17 15:53:07 +01001910 * If no maximum operating frequency is supplied, fall back to use
1911 * the module parameter, which has a (low) default value in case it
1912 * is not specified. Either value must not exceed the clock rate into
Ulf Hansson5080a082014-03-21 10:46:39 +01001913 * the block, of course.
Linus Walleij808d97c2010-04-08 07:39:38 +01001914 */
Ulf Hansson78f87df2014-03-17 15:53:07 +01001915 if (mmc->f_max)
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001916 mmc->f_max = variant->explicit_mclk_control ?
1917 min(variant->f_max, mmc->f_max) :
1918 min(host->mclk, mmc->f_max);
Linus Walleij808d97c2010-04-08 07:39:38 +01001919 else
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001920 mmc->f_max = variant->explicit_mclk_control ?
1921 fmax : min(host->mclk, fmax);
1922
1923
Linus Walleij64de0282010-02-19 01:09:10 +01001924 dev_dbg(mmc_dev(mmc), "clocking block at %u Hz\n", mmc->f_max);
1925
Ludovic Barre15878e52018-10-08 14:08:51 +02001926 host->rst = devm_reset_control_get_optional_exclusive(&dev->dev, NULL);
1927 if (IS_ERR(host->rst)) {
1928 ret = PTR_ERR(host->rst);
1929 goto clk_disable;
1930 }
1931
Ulf Hansson599c1d52013-01-07 16:22:50 +01001932 /* Get regulators and the supported OCR mask */
Bjorn Andersson9369c972015-03-24 18:39:49 -07001933 ret = mmc_regulator_get_supply(mmc);
Wolfram Sang51006952017-10-14 21:17:14 +02001934 if (ret)
Bjorn Andersson9369c972015-03-24 18:39:49 -07001935 goto clk_disable;
1936
Ulf Hansson599c1d52013-01-07 16:22:50 +01001937 if (!mmc->ocr_avail)
Linus Walleij34e84f32009-09-22 14:41:40 +01001938 mmc->ocr_avail = plat->ocr_mask;
Ulf Hansson599c1d52013-01-07 16:22:50 +01001939 else if (plat->ocr_mask)
1940 dev_warn(mmc_dev(mmc), "Platform OCR mask is ignored\n");
1941
Ulf Hansson9dd8a8b2014-03-19 13:54:18 +01001942 /* We support these capabilities. */
1943 mmc->caps |= MMC_CAP_CMD23;
1944
Linus Walleij49adc0c2016-10-25 11:06:06 +02001945 /*
1946 * Enable busy detection.
1947 */
Ulf Hansson8d94b542014-01-13 16:49:31 +01001948 if (variant->busy_detect) {
1949 mmci_ops.card_busy = mmci_card_busy;
Linus Walleij49adc0c2016-10-25 11:06:06 +02001950 /*
1951 * Not all variants have a flag to enable busy detection
1952 * in the DPSM, but if they do, set it here.
1953 */
1954 if (variant->busy_dpsm_flag)
1955 mmci_write_datactrlreg(host,
1956 host->variant->busy_dpsm_flag);
Ulf Hansson8d94b542014-01-13 16:49:31 +01001957 mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY;
1958 mmc->max_busy_timeout = 0;
1959 }
1960
Ulf Hanssone9968c62019-01-29 15:35:56 +01001961 /* Prepare a CMD12 - needed to clear the DPSM on some variants. */
1962 host->stop_abort.opcode = MMC_STOP_TRANSMISSION;
1963 host->stop_abort.arg = 0;
1964 host->stop_abort.flags = MMC_RSP_R1B | MMC_CMD_AC;
1965
Ulf Hansson8d94b542014-01-13 16:49:31 +01001966 mmc->ops = &mmci_ops;
1967
Ulf Hansson70be2082013-01-07 15:35:06 +01001968 /* We support these PM capabilities. */
Ulf Hansson78f87df2014-03-17 15:53:07 +01001969 mmc->pm_caps |= MMC_PM_KEEP_POWER;
Ulf Hansson70be2082013-01-07 15:35:06 +01001970
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 /*
1972 * We can do SGIO
1973 */
Martin K. Petersena36274e2010-09-10 01:33:59 -04001974 mmc->max_segs = NR_SG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
1976 /*
Rabin Vincent08458ef2010-07-21 12:55:59 +01001977 * Since only a certain number of bits are valid in the data length
1978 * register, we must ensure that we don't exceed 2^num-1 bytes in a
1979 * single request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 */
Rabin Vincent08458ef2010-07-21 12:55:59 +01001981 mmc->max_req_size = (1 << variant->datalength_bits) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
1983 /*
1984 * Set the maximum segment size. Since we aren't doing DMA
1985 * (yet) we are only limited by the data length register.
1986 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001987 mmc->max_seg_size = mmc->max_req_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001989 /*
1990 * Block size can be up to 2048 bytes, but must be a power of two.
1991 */
Ludovic Barrec931d492018-10-08 14:08:43 +02001992 mmc->max_blk_size = 1 << variant->datactrl_blocksz;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001993
Pierre Ossman55db8902006-11-21 17:55:45 +01001994 /*
Will Deacon8f7f6b7e2012-02-24 11:25:21 +00001995 * Limit the number of blocks transferred so that we don't overflow
1996 * the maximum request size.
Pierre Ossman55db8902006-11-21 17:55:45 +01001997 */
Ludovic Barrec931d492018-10-08 14:08:43 +02001998 mmc->max_blk_count = mmc->max_req_size >> variant->datactrl_blocksz;
Pierre Ossman55db8902006-11-21 17:55:45 +01001999
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 spin_lock_init(&host->lock);
2001
2002 writel(0, host->base + MMCIMASK0);
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +01002003
2004 if (variant->mmcimask1)
2005 writel(0, host->base + MMCIMASK1);
2006
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 writel(0xfff, host->base + MMCICLEAR);
2008
Linus Walleijce437aa2014-08-27 15:13:54 +02002009 /*
2010 * If:
2011 * - not using DT but using a descriptor table, or
2012 * - using a table of descriptors ALONGSIDE DT, or
2013 * look up these descriptors named "cd" and "wp" right here, fail
Linus Walleij9ef986a2018-09-20 16:01:10 -07002014 * silently of these do not exist
Linus Walleijce437aa2014-08-27 15:13:54 +02002015 */
2016 if (!np) {
Linus Walleij89168b42014-10-02 09:08:46 +02002017 ret = mmc_gpiod_request_cd(mmc, "cd", 0, false, 0, NULL);
Linus Walleij9ef986a2018-09-20 16:01:10 -07002018 if (ret == -EPROBE_DEFER)
2019 goto clk_disable;
Linus Walleijce437aa2014-08-27 15:13:54 +02002020
Linus Walleija2b760a2019-02-05 10:30:22 +01002021 ret = mmc_gpiod_request_ro(mmc, "wp", 0, 0, NULL);
Linus Walleij9ef986a2018-09-20 16:01:10 -07002022 if (ret == -EPROBE_DEFER)
2023 goto clk_disable;
Russell King89001442009-07-09 15:16:07 +01002024 }
2025
Ulf Hanssonef289982014-03-17 13:56:32 +01002026 ret = devm_request_irq(&dev->dev, dev->irq[0], mmci_irq, IRQF_SHARED,
2027 DRIVER_NAME " (cmd)", host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 if (ret)
Ulf Hanssonef289982014-03-17 13:56:32 +01002029 goto clk_disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
Russell Kingdfb85182012-05-03 11:33:15 +01002031 if (!dev->irq[1])
Linus Walleij2686b4b2010-10-19 12:39:48 +01002032 host->singleirq = true;
2033 else {
Ulf Hanssonef289982014-03-17 13:56:32 +01002034 ret = devm_request_irq(&dev->dev, dev->irq[1], mmci_pio_irq,
2035 IRQF_SHARED, DRIVER_NAME " (pio)", host);
Linus Walleij2686b4b2010-10-19 12:39:48 +01002036 if (ret)
Ulf Hanssonef289982014-03-17 13:56:32 +01002037 goto clk_disable;
Linus Walleij2686b4b2010-10-19 12:39:48 +01002038 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
Ludovic Barredaf97132018-10-08 14:08:44 +02002040 writel(MCI_IRQENABLE | variant->start_err, host->base + MMCIMASK0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
2042 amba_set_drvdata(dev, mmc);
2043
Russell Kingc8ebae32011-01-11 19:35:53 +00002044 dev_info(&dev->dev, "%s: PL%03x manf %x rev%u at 0x%08llx irq %d,%d (pio)\n",
2045 mmc_hostname(mmc), amba_part(dev), amba_manf(dev),
2046 amba_rev(dev), (unsigned long long)dev->res.start,
2047 dev->irq[0], dev->irq[1]);
2048
2049 mmci_dma_setup(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Ulf Hansson2cd976c2011-12-13 17:01:11 +01002051 pm_runtime_set_autosuspend_delay(&dev->dev, 50);
2052 pm_runtime_use_autosuspend(&dev->dev);
Russell King1c3be362011-08-14 09:17:05 +01002053
Russell King8c11a942010-12-28 19:40:40 +00002054 mmc_add_host(mmc);
2055
Ulf Hansson6f2d3c82014-12-11 14:35:55 +01002056 pm_runtime_put(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 return 0;
2058
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 clk_disable:
Julia Lawallac940932012-08-26 16:00:59 +00002060 clk_disable_unprepare(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 host_free:
2062 mmc_free_host(mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 return ret;
2064}
2065
Bill Pemberton6e0ee712012-11-19 13:26:03 -05002066static int mmci_remove(struct amba_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067{
2068 struct mmc_host *mmc = amba_get_drvdata(dev);
2069
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 if (mmc) {
2071 struct mmci_host *host = mmc_priv(mmc);
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +01002072 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
Russell King1c3be362011-08-14 09:17:05 +01002074 /*
2075 * Undo pm_runtime_put() in probe. We use the _sync
2076 * version here so that we can access the primecell.
2077 */
2078 pm_runtime_get_sync(&dev->dev);
2079
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 mmc_remove_host(mmc);
2081
2082 writel(0, host->base + MMCIMASK0);
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +01002083
2084 if (variant->mmcimask1)
2085 writel(0, host->base + MMCIMASK1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
2087 writel(0, host->base + MMCICOMMAND);
2088 writel(0, host->base + MMCIDATACTRL);
2089
Russell Kingc8ebae32011-01-11 19:35:53 +00002090 mmci_dma_release(host);
Julia Lawallac940932012-08-26 16:00:59 +00002091 clk_disable_unprepare(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 mmc_free_host(mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 }
2094
2095 return 0;
2096}
2097
Ulf Hansson571dce42014-01-23 00:38:00 +01002098#ifdef CONFIG_PM
Ulf Hansson1ff44432013-09-04 09:05:17 +01002099static void mmci_save(struct mmci_host *host)
2100{
2101 unsigned long flags;
2102
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01002103 spin_lock_irqsave(&host->lock, flags);
Ulf Hansson1ff44432013-09-04 09:05:17 +01002104
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01002105 writel(0, host->base + MMCIMASK0);
2106 if (host->variant->pwrreg_nopower) {
Ulf Hansson1ff44432013-09-04 09:05:17 +01002107 writel(0, host->base + MMCIDATACTRL);
2108 writel(0, host->base + MMCIPOWER);
2109 writel(0, host->base + MMCICLOCK);
Ulf Hansson1ff44432013-09-04 09:05:17 +01002110 }
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01002111 mmci_reg_delay(host);
Ulf Hansson1ff44432013-09-04 09:05:17 +01002112
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01002113 spin_unlock_irqrestore(&host->lock, flags);
Ulf Hansson1ff44432013-09-04 09:05:17 +01002114}
2115
2116static void mmci_restore(struct mmci_host *host)
2117{
2118 unsigned long flags;
2119
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01002120 spin_lock_irqsave(&host->lock, flags);
Ulf Hansson1ff44432013-09-04 09:05:17 +01002121
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01002122 if (host->variant->pwrreg_nopower) {
Ulf Hansson1ff44432013-09-04 09:05:17 +01002123 writel(host->clk_reg, host->base + MMCICLOCK);
2124 writel(host->datactrl_reg, host->base + MMCIDATACTRL);
2125 writel(host->pwr_reg, host->base + MMCIPOWER);
Ulf Hansson1ff44432013-09-04 09:05:17 +01002126 }
Ludovic Barredaf97132018-10-08 14:08:44 +02002127 writel(MCI_IRQENABLE | host->variant->start_err,
2128 host->base + MMCIMASK0);
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01002129 mmci_reg_delay(host);
2130
2131 spin_unlock_irqrestore(&host->lock, flags);
Ulf Hansson1ff44432013-09-04 09:05:17 +01002132}
2133
Ulf Hansson82592932013-01-09 11:15:26 +01002134static int mmci_runtime_suspend(struct device *dev)
2135{
2136 struct amba_device *adev = to_amba_device(dev);
2137 struct mmc_host *mmc = amba_get_drvdata(adev);
2138
2139 if (mmc) {
2140 struct mmci_host *host = mmc_priv(mmc);
Ulf Hanssone36bd9c62013-09-04 09:00:37 +01002141 pinctrl_pm_select_sleep_state(dev);
Ulf Hansson1ff44432013-09-04 09:05:17 +01002142 mmci_save(host);
Ulf Hansson82592932013-01-09 11:15:26 +01002143 clk_disable_unprepare(host->clk);
2144 }
2145
2146 return 0;
2147}
2148
2149static int mmci_runtime_resume(struct device *dev)
2150{
2151 struct amba_device *adev = to_amba_device(dev);
2152 struct mmc_host *mmc = amba_get_drvdata(adev);
2153
2154 if (mmc) {
2155 struct mmci_host *host = mmc_priv(mmc);
2156 clk_prepare_enable(host->clk);
Ulf Hansson1ff44432013-09-04 09:05:17 +01002157 mmci_restore(host);
Ulf Hanssone36bd9c62013-09-04 09:00:37 +01002158 pinctrl_pm_select_default_state(dev);
Ulf Hansson82592932013-01-09 11:15:26 +01002159 }
2160
2161 return 0;
2162}
2163#endif
2164
Ulf Hansson48fa7002011-12-13 16:59:34 +01002165static const struct dev_pm_ops mmci_dev_pm_ops = {
Ulf Hanssonf3737fa2014-01-23 01:11:33 +01002166 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
2167 pm_runtime_force_resume)
Rafael J. Wysocki6ed23b82014-12-04 00:34:11 +01002168 SET_RUNTIME_PM_OPS(mmci_runtime_suspend, mmci_runtime_resume, NULL)
Ulf Hansson48fa7002011-12-13 16:59:34 +01002169};
2170
Arvind Yadav88411de2017-08-23 22:00:49 +05302171static const struct amba_id mmci_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 {
2173 .id = 0x00041180,
Pawel Moll768fbc12011-03-11 17:18:07 +00002174 .mask = 0xff0fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01002175 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 },
2177 {
Pawel Moll768fbc12011-03-11 17:18:07 +00002178 .id = 0x01041180,
2179 .mask = 0xff0fffff,
2180 .data = &variant_arm_extended_fifo,
2181 },
2182 {
Pawel Moll3a372982013-01-24 14:12:45 +01002183 .id = 0x02041180,
2184 .mask = 0xff0fffff,
2185 .data = &variant_arm_extended_fifo_hwfc,
2186 },
2187 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 .id = 0x00041181,
2189 .mask = 0x000fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01002190 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 },
Linus Walleijcc30d602009-01-04 15:18:54 +01002192 /* ST Micro variants */
2193 {
2194 .id = 0x00180180,
2195 .mask = 0x00ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01002196 .data = &variant_u300,
Linus Walleijcc30d602009-01-04 15:18:54 +01002197 },
2198 {
Linus Walleij34fd4212012-04-10 17:43:59 +01002199 .id = 0x10180180,
2200 .mask = 0xf0ffffff,
2201 .data = &variant_nomadik,
2202 },
2203 {
Linus Walleijcc30d602009-01-04 15:18:54 +01002204 .id = 0x00280180,
2205 .mask = 0x00ffffff,
Linus Walleij0bcb7ef2016-01-04 02:21:55 +01002206 .data = &variant_nomadik,
Rabin Vincent4956e102010-07-21 12:54:40 +01002207 },
2208 {
2209 .id = 0x00480180,
Philippe Langlais1784b152011-03-25 08:51:52 +01002210 .mask = 0xf0ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01002211 .data = &variant_ux500,
Linus Walleijcc30d602009-01-04 15:18:54 +01002212 },
Philippe Langlais1784b152011-03-25 08:51:52 +01002213 {
2214 .id = 0x10480180,
2215 .mask = 0xf0ffffff,
2216 .data = &variant_ux500v2,
2217 },
Patrice Chotard2a9d6c82018-01-18 15:34:21 +01002218 {
2219 .id = 0x00880180,
2220 .mask = 0x00ffffff,
2221 .data = &variant_stm32,
2222 },
Ludovic Barre46b723d2018-10-08 14:08:55 +02002223 {
2224 .id = 0x10153180,
2225 .mask = 0xf0ffffff,
2226 .data = &variant_stm32_sdmmc,
2227 },
Srinivas Kandagatla55b604a2014-06-02 10:10:13 +01002228 /* Qualcomm variants */
2229 {
2230 .id = 0x00051180,
2231 .mask = 0x000fffff,
2232 .data = &variant_qcom,
2233 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 { 0, 0 },
2235};
2236
Dave Martin9f998352011-10-05 15:15:21 +01002237MODULE_DEVICE_TABLE(amba, mmci_ids);
2238
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239static struct amba_driver mmci_driver = {
2240 .drv = {
2241 .name = DRIVER_NAME,
Ulf Hansson48fa7002011-12-13 16:59:34 +01002242 .pm = &mmci_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 },
2244 .probe = mmci_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05002245 .remove = mmci_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 .id_table = mmci_ids,
2247};
2248
viresh kumar9e5ed092012-03-15 10:40:38 +01002249module_amba_driver(mmci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251module_param(fmax, uint, 0444);
2252
2253MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
2254MODULE_LICENSE("GPL");