blob: 9f0cef0f0669e76839e08f70afe4d2925424436b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Pierre Ossman70f10482007-07-11 20:04:50 +02002 * linux/drivers/mmc/host/mmci.c - ARM PrimeCell MMCI PL180/1 driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 2003 Deep Blue Solutions, Ltd, All Rights Reserved.
Russell Kingc8ebae32011-01-11 19:35:53 +00005 * Copyright (C) 2010 ST-Ericsson SA
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
12#include <linux/moduleparam.h>
13#include <linux/init.h>
14#include <linux/ioport.h>
15#include <linux/device.h>
Ulf Hanssonef289982014-03-17 13:56:32 +010016#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/interrupt.h>
Russell King613b1522011-01-30 21:06:53 +000018#include <linux/kernel.h>
Lee Jones000bc9d2012-04-16 10:18:43 +010019#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/delay.h>
21#include <linux/err.h>
22#include <linux/highmem.h>
Nicolas Pitre019a5f52007-10-11 01:06:03 -040023#include <linux/log2.h>
Ulf Hansson70be2082013-01-07 15:35:06 +010024#include <linux/mmc/pm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/mmc/host.h>
Linus Walleij34177802010-10-19 12:43:58 +010026#include <linux/mmc/card.h>
Ulf Hanssond2762092014-03-17 13:56:19 +010027#include <linux/mmc/slot-gpio.h>
Russell Kinga62c80e2006-01-07 13:52:45 +000028#include <linux/amba/bus.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000029#include <linux/clk.h>
Jens Axboebd6dee62007-10-24 09:01:09 +020030#include <linux/scatterlist.h>
Linus Walleij9ef986a2018-09-20 16:01:10 -070031#include <linux/of.h>
Linus Walleij34e84f32009-09-22 14:41:40 +010032#include <linux/regulator/consumer.h>
Russell Kingc8ebae32011-01-11 19:35:53 +000033#include <linux/dmaengine.h>
34#include <linux/dma-mapping.h>
35#include <linux/amba/mmci.h>
Russell King1c3be362011-08-14 09:17:05 +010036#include <linux/pm_runtime.h>
Viresh Kumar258aea72012-02-01 16:12:19 +053037#include <linux/types.h>
Linus Walleija9a83782012-10-29 14:39:30 +010038#include <linux/pinctrl/consumer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Russell King7b09cda2005-07-01 12:02:59 +010040#include <asm/div64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include "mmci.h"
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +010044#include "mmci_qcom_dml.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#define DRIVER_NAME "mmci-pl18x"
47
Ludovic Barrec3647fd2018-10-08 14:08:33 +020048#ifdef CONFIG_DMA_ENGINE
49void mmci_variant_init(struct mmci_host *host);
50#else
51static inline void mmci_variant_init(struct mmci_host *host) {}
52#endif
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static unsigned int fmax = 515633;
55
Rabin Vincent4956e102010-07-21 12:54:40 +010056static struct variant_data variant_arm = {
Rabin Vincent8301bb62010-08-09 12:57:30 +010057 .fifosize = 16 * 4,
58 .fifohalfsize = 8 * 4,
Ludovic Barre0f244802018-10-08 14:08:45 +020059 .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
60 .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
61 .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
62 .cmdreg_srsp = MCI_CPSM_RESPONSE,
Rabin Vincent08458ef2010-07-21 12:55:59 +010063 .datalength_bits = 16,
Ludovic Barrec931d492018-10-08 14:08:43 +020064 .datactrl_blocksz = 11,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +010065 .pwrreg_powerup = MCI_PWR_UP,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +010066 .f_max = 100000000,
Ulf Hansson78782892014-06-13 13:21:38 +020067 .reversed_irq_handling = true,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +010068 .mmcimask1 = true,
Patrice Chotard7f7b5502018-01-18 15:34:18 +010069 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +010070 .opendrain = MCI_ROD,
Ludovic Barrec3647fd2018-10-08 14:08:33 +020071 .init = mmci_variant_init,
Rabin Vincent4956e102010-07-21 12:54:40 +010072};
73
Pawel Moll768fbc12011-03-11 17:18:07 +000074static struct variant_data variant_arm_extended_fifo = {
75 .fifosize = 128 * 4,
76 .fifohalfsize = 64 * 4,
Ludovic Barre0f244802018-10-08 14:08:45 +020077 .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
78 .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
79 .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
80 .cmdreg_srsp = MCI_CPSM_RESPONSE,
Pawel Moll768fbc12011-03-11 17:18:07 +000081 .datalength_bits = 16,
Ludovic Barrec931d492018-10-08 14:08:43 +020082 .datactrl_blocksz = 11,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +010083 .pwrreg_powerup = MCI_PWR_UP,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +010084 .f_max = 100000000,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +010085 .mmcimask1 = true,
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,
Patrice Chotard7f7b5502018-01-18 15:34:18 +0100104 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +0100105 .opendrain = MCI_ROD,
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200106 .init = mmci_variant_init,
Pawel Moll3a372982013-01-24 14:12:45 +0100107};
108
Rabin Vincent4956e102010-07-21 12:54:40 +0100109static struct variant_data variant_u300 = {
Rabin Vincent8301bb62010-08-09 12:57:30 +0100110 .fifosize = 16 * 4,
111 .fifohalfsize = 8 * 4,
Linus Walleij49ac2152011-03-04 14:54:16 +0100112 .clkreg_enable = MCI_ST_U300_HWFCEN,
Srinivas Kandagatlae1412d82014-06-02 10:09:23 +0100113 .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
Ludovic Barre0f244802018-10-08 14:08:45 +0200114 .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
115 .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
116 .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
117 .cmdreg_srsp = MCI_CPSM_RESPONSE,
Rabin Vincent08458ef2010-07-21 12:55:59 +0100118 .datalength_bits = 16,
Ludovic Barrec931d492018-10-08 14:08:43 +0200119 .datactrl_blocksz = 11,
Linus Walleij5db3eee2016-10-25 11:06:05 +0200120 .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
Srinivas Kandagatlac7354132014-08-22 05:55:16 +0100121 .st_sdio = true,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +0100122 .pwrreg_powerup = MCI_PWR_ON,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +0100123 .f_max = 100000000,
Ulf Hansson4d1a3a02011-12-13 16:57:07 +0100124 .signal_direction = true,
Ulf Hanssonf4670da2013-01-09 17:19:54 +0100125 .pwrreg_clkgate = true,
Ulf Hansson1ff44432013-09-04 09:05:17 +0100126 .pwrreg_nopower = true,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100127 .mmcimask1 = true,
Patrice Chotard7f7b5502018-01-18 15:34:18 +0100128 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +0100129 .opendrain = MCI_OD,
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200130 .init = mmci_variant_init,
Rabin Vincent4956e102010-07-21 12:54:40 +0100131};
132
Linus Walleij34fd4212012-04-10 17:43:59 +0100133static struct variant_data variant_nomadik = {
134 .fifosize = 16 * 4,
135 .fifohalfsize = 8 * 4,
136 .clkreg = MCI_CLK_ENABLE,
Linus Walleijf5abc762016-01-04 02:22:08 +0100137 .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
Ludovic Barre0f244802018-10-08 14:08:45 +0200138 .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
139 .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
140 .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
141 .cmdreg_srsp = MCI_CPSM_RESPONSE,
Linus Walleij34fd4212012-04-10 17:43:59 +0100142 .datalength_bits = 24,
Ludovic Barrec931d492018-10-08 14:08:43 +0200143 .datactrl_blocksz = 11,
Linus Walleij5db3eee2016-10-25 11:06:05 +0200144 .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
Srinivas Kandagatlac7354132014-08-22 05:55:16 +0100145 .st_sdio = true,
Linus Walleij34fd4212012-04-10 17:43:59 +0100146 .st_clkdiv = true,
147 .pwrreg_powerup = MCI_PWR_ON,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +0100148 .f_max = 100000000,
Linus Walleij34fd4212012-04-10 17:43:59 +0100149 .signal_direction = true,
Ulf Hanssonf4670da2013-01-09 17:19:54 +0100150 .pwrreg_clkgate = true,
Ulf Hansson1ff44432013-09-04 09:05:17 +0100151 .pwrreg_nopower = true,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100152 .mmcimask1 = true,
Patrice Chotard7f7b5502018-01-18 15:34:18 +0100153 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +0100154 .opendrain = MCI_OD,
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200155 .init = mmci_variant_init,
Linus Walleij34fd4212012-04-10 17:43:59 +0100156};
157
Rabin Vincent4956e102010-07-21 12:54:40 +0100158static struct variant_data variant_ux500 = {
Rabin Vincent8301bb62010-08-09 12:57:30 +0100159 .fifosize = 30 * 4,
160 .fifohalfsize = 8 * 4,
Rabin Vincent4956e102010-07-21 12:54:40 +0100161 .clkreg = MCI_CLK_ENABLE,
Linus Walleij49ac2152011-03-04 14:54:16 +0100162 .clkreg_enable = MCI_ST_UX500_HWFCEN,
Srinivas Kandagatlae1412d82014-06-02 10:09:23 +0100163 .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
Srinivas Kandagatlae8740642014-06-02 10:09:30 +0100164 .clkreg_neg_edge_enable = MCI_ST_UX500_NEG_EDGE,
Ludovic Barre0f244802018-10-08 14:08:45 +0200165 .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
166 .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
167 .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
168 .cmdreg_srsp = MCI_CPSM_RESPONSE,
Rabin Vincent08458ef2010-07-21 12:55:59 +0100169 .datalength_bits = 24,
Ludovic Barrec931d492018-10-08 14:08:43 +0200170 .datactrl_blocksz = 11,
Linus Walleij5db3eee2016-10-25 11:06:05 +0200171 .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
Srinivas Kandagatlac7354132014-08-22 05:55:16 +0100172 .st_sdio = true,
Linus Walleijb70a67f2010-12-06 09:24:14 +0100173 .st_clkdiv = true,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +0100174 .pwrreg_powerup = MCI_PWR_ON,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +0100175 .f_max = 100000000,
Ulf Hansson4d1a3a02011-12-13 16:57:07 +0100176 .signal_direction = true,
Ulf Hanssonf4670da2013-01-09 17:19:54 +0100177 .pwrreg_clkgate = true,
Ulf Hansson01259622013-05-15 20:53:22 +0100178 .busy_detect = true,
Linus Walleij49adc0c2016-10-25 11:06:06 +0200179 .busy_dpsm_flag = MCI_DPSM_ST_BUSYMODE,
180 .busy_detect_flag = MCI_ST_CARDBUSY,
181 .busy_detect_mask = MCI_ST_BUSYENDMASK,
Ulf Hansson1ff44432013-09-04 09:05:17 +0100182 .pwrreg_nopower = true,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100183 .mmcimask1 = true,
Patrice Chotard7f7b5502018-01-18 15:34:18 +0100184 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +0100185 .opendrain = MCI_OD,
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200186 .init = mmci_variant_init,
Rabin Vincent4956e102010-07-21 12:54:40 +0100187};
Linus Walleijb70a67f2010-12-06 09:24:14 +0100188
Philippe Langlais1784b152011-03-25 08:51:52 +0100189static struct variant_data variant_ux500v2 = {
190 .fifosize = 30 * 4,
191 .fifohalfsize = 8 * 4,
192 .clkreg = MCI_CLK_ENABLE,
193 .clkreg_enable = MCI_ST_UX500_HWFCEN,
Srinivas Kandagatlae1412d82014-06-02 10:09:23 +0100194 .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
Srinivas Kandagatlae8740642014-06-02 10:09:30 +0100195 .clkreg_neg_edge_enable = MCI_ST_UX500_NEG_EDGE,
Ludovic Barre0f244802018-10-08 14:08:45 +0200196 .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
197 .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
198 .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
199 .cmdreg_srsp = MCI_CPSM_RESPONSE,
Linus Walleij5db3eee2016-10-25 11:06:05 +0200200 .datactrl_mask_ddrmode = MCI_DPSM_ST_DDRMODE,
Philippe Langlais1784b152011-03-25 08:51:52 +0100201 .datalength_bits = 24,
Ludovic Barrec931d492018-10-08 14:08:43 +0200202 .datactrl_blocksz = 11,
Linus Walleij5db3eee2016-10-25 11:06:05 +0200203 .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
Srinivas Kandagatlac7354132014-08-22 05:55:16 +0100204 .st_sdio = true,
Philippe Langlais1784b152011-03-25 08:51:52 +0100205 .st_clkdiv = true,
206 .blksz_datactrl16 = true,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +0100207 .pwrreg_powerup = MCI_PWR_ON,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +0100208 .f_max = 100000000,
Ulf Hansson4d1a3a02011-12-13 16:57:07 +0100209 .signal_direction = true,
Ulf Hanssonf4670da2013-01-09 17:19:54 +0100210 .pwrreg_clkgate = true,
Ulf Hansson01259622013-05-15 20:53:22 +0100211 .busy_detect = true,
Linus Walleij49adc0c2016-10-25 11:06:06 +0200212 .busy_dpsm_flag = MCI_DPSM_ST_BUSYMODE,
213 .busy_detect_flag = MCI_ST_CARDBUSY,
214 .busy_detect_mask = MCI_ST_BUSYENDMASK,
Ulf Hansson1ff44432013-09-04 09:05:17 +0100215 .pwrreg_nopower = true,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100216 .mmcimask1 = true,
Patrice Chotard7f7b5502018-01-18 15:34:18 +0100217 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +0100218 .opendrain = MCI_OD,
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200219 .init = mmci_variant_init,
Philippe Langlais1784b152011-03-25 08:51:52 +0100220};
221
Patrice Chotard2a9d6c82018-01-18 15:34:21 +0100222static struct variant_data variant_stm32 = {
223 .fifosize = 32 * 4,
224 .fifohalfsize = 8 * 4,
225 .clkreg = MCI_CLK_ENABLE,
226 .clkreg_enable = MCI_ST_UX500_HWFCEN,
227 .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
228 .clkreg_neg_edge_enable = MCI_ST_UX500_NEG_EDGE,
Ludovic Barre0f244802018-10-08 14:08:45 +0200229 .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
230 .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
231 .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
232 .cmdreg_srsp = MCI_CPSM_RESPONSE,
Patrice Chotard2a9d6c82018-01-18 15:34:21 +0100233 .datalength_bits = 24,
Ludovic Barrec931d492018-10-08 14:08:43 +0200234 .datactrl_blocksz = 11,
Patrice Chotard2a9d6c82018-01-18 15:34:21 +0100235 .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
236 .st_sdio = true,
237 .st_clkdiv = true,
238 .pwrreg_powerup = MCI_PWR_ON,
239 .f_max = 48000000,
240 .pwrreg_clkgate = true,
241 .pwrreg_nopower = true,
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200242 .init = mmci_variant_init,
Patrice Chotard2a9d6c82018-01-18 15:34:21 +0100243};
244
Srinivas Kandagatla55b604a2014-06-02 10:10:13 +0100245static struct variant_data variant_qcom = {
246 .fifosize = 16 * 4,
247 .fifohalfsize = 8 * 4,
248 .clkreg = MCI_CLK_ENABLE,
249 .clkreg_enable = MCI_QCOM_CLK_FLOWENA |
250 MCI_QCOM_CLK_SELECT_IN_FBCLK,
251 .clkreg_8bit_bus_enable = MCI_QCOM_CLK_WIDEBUS_8,
252 .datactrl_mask_ddrmode = MCI_QCOM_CLK_SELECT_IN_DDR_MODE,
Ludovic Barre0f244802018-10-08 14:08:45 +0200253 .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
254 .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
255 .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
256 .cmdreg_srsp = MCI_CPSM_RESPONSE,
Linus Walleij5db3eee2016-10-25 11:06:05 +0200257 .data_cmd_enable = MCI_CPSM_QCOM_DATCMD,
Srinivas Kandagatla55b604a2014-06-02 10:10:13 +0100258 .blksz_datactrl4 = true,
259 .datalength_bits = 24,
Ludovic Barrec931d492018-10-08 14:08:43 +0200260 .datactrl_blocksz = 11,
Srinivas Kandagatla55b604a2014-06-02 10:10:13 +0100261 .pwrreg_powerup = MCI_PWR_UP,
262 .f_max = 208000000,
263 .explicit_mclk_control = true,
264 .qcom_fifo = true,
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100265 .qcom_dml = true,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100266 .mmcimask1 = true,
Patrice Chotard7f7b5502018-01-18 15:34:18 +0100267 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +0100268 .opendrain = MCI_ROD,
Ulf Hansson29aba072018-07-16 13:08:18 +0200269 .init = qcom_variant_init,
Srinivas Kandagatla55b604a2014-06-02 10:10:13 +0100270};
271
Linus Walleij49adc0c2016-10-25 11:06:06 +0200272/* Busy detection for the ST Micro variant */
Ulf Hansson01259622013-05-15 20:53:22 +0100273static int mmci_card_busy(struct mmc_host *mmc)
274{
275 struct mmci_host *host = mmc_priv(mmc);
276 unsigned long flags;
277 int busy = 0;
278
Ulf Hansson01259622013-05-15 20:53:22 +0100279 spin_lock_irqsave(&host->lock, flags);
Linus Walleij49adc0c2016-10-25 11:06:06 +0200280 if (readl(host->base + MMCISTATUS) & host->variant->busy_detect_flag)
Ulf Hansson01259622013-05-15 20:53:22 +0100281 busy = 1;
282 spin_unlock_irqrestore(&host->lock, flags);
283
Ulf Hansson01259622013-05-15 20:53:22 +0100284 return busy;
285}
286
Ulf Hanssonf829c042013-09-04 09:01:15 +0100287static void mmci_reg_delay(struct mmci_host *host)
288{
289 /*
290 * According to the spec, at least three feedback clock cycles
291 * of max 52 MHz must pass between two writes to the MMCICLOCK reg.
292 * Three MCLK clock cycles must pass between two MMCIPOWER reg writes.
293 * Worst delay time during card init is at 100 kHz => 30 us.
294 * Worst delay time when up and running is at 25 MHz => 120 ns.
295 */
296 if (host->cclk < 25000000)
297 udelay(30);
298 else
299 ndelay(120);
300}
301
Ulf Hansson653a7612013-01-21 21:29:34 +0100302/*
Linus Walleija6a64642009-09-14 12:56:14 +0100303 * This must be called with host->lock held
304 */
Ludovic Barrecd3ee8c2018-10-08 14:08:42 +0200305void mmci_write_clkreg(struct mmci_host *host, u32 clk)
Ulf Hansson7437cfa2012-01-18 09:17:27 +0100306{
307 if (host->clk_reg != clk) {
308 host->clk_reg = clk;
309 writel(clk, host->base + MMCICLOCK);
310 }
311}
312
313/*
314 * This must be called with host->lock held
315 */
Ludovic Barrecd3ee8c2018-10-08 14:08:42 +0200316void mmci_write_pwrreg(struct mmci_host *host, u32 pwr)
Ulf Hansson7437cfa2012-01-18 09:17:27 +0100317{
318 if (host->pwr_reg != pwr) {
319 host->pwr_reg = pwr;
320 writel(pwr, host->base + MMCIPOWER);
321 }
322}
323
324/*
325 * This must be called with host->lock held
326 */
Ulf Hansson9cc639a2013-05-15 20:48:23 +0100327static void mmci_write_datactrlreg(struct mmci_host *host, u32 datactrl)
328{
Linus Walleij49adc0c2016-10-25 11:06:06 +0200329 /* Keep busy mode in DPSM if enabled */
330 datactrl |= host->datactrl_reg & host->variant->busy_dpsm_flag;
Ulf Hansson01259622013-05-15 20:53:22 +0100331
Ulf Hansson9cc639a2013-05-15 20:48:23 +0100332 if (host->datactrl_reg != datactrl) {
333 host->datactrl_reg = datactrl;
334 writel(datactrl, host->base + MMCIDATACTRL);
335 }
336}
337
338/*
339 * This must be called with host->lock held
340 */
Linus Walleija6a64642009-09-14 12:56:14 +0100341static void mmci_set_clkreg(struct mmci_host *host, unsigned int desired)
342{
Rabin Vincent4956e102010-07-21 12:54:40 +0100343 struct variant_data *variant = host->variant;
344 u32 clk = variant->clkreg;
Linus Walleija6a64642009-09-14 12:56:14 +0100345
Ulf Hanssonc58a8502013-05-13 15:40:03 +0100346 /* Make sure cclk reflects the current calculated clock */
347 host->cclk = 0;
348
Linus Walleija6a64642009-09-14 12:56:14 +0100349 if (desired) {
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +0100350 if (variant->explicit_mclk_control) {
351 host->cclk = host->mclk;
352 } else if (desired >= host->mclk) {
Linus Walleij991a86e2010-12-10 09:35:53 +0100353 clk = MCI_CLK_BYPASS;
Linus Walleij399bc482011-04-01 07:59:17 +0100354 if (variant->st_clkdiv)
355 clk |= MCI_ST_UX500_NEG_EDGE;
Linus Walleija6a64642009-09-14 12:56:14 +0100356 host->cclk = host->mclk;
Linus Walleijb70a67f2010-12-06 09:24:14 +0100357 } else if (variant->st_clkdiv) {
358 /*
359 * DB8500 TRM says f = mclk / (clkdiv + 2)
360 * => clkdiv = (mclk / f) - 2
361 * Round the divider up so we don't exceed the max
362 * frequency
363 */
364 clk = DIV_ROUND_UP(host->mclk, desired) - 2;
365 if (clk >= 256)
366 clk = 255;
367 host->cclk = host->mclk / (clk + 2);
Linus Walleija6a64642009-09-14 12:56:14 +0100368 } else {
Linus Walleijb70a67f2010-12-06 09:24:14 +0100369 /*
370 * PL180 TRM says f = mclk / (2 * (clkdiv + 1))
371 * => clkdiv = mclk / (2 * f) - 1
372 */
Linus Walleija6a64642009-09-14 12:56:14 +0100373 clk = host->mclk / (2 * desired) - 1;
374 if (clk >= 256)
375 clk = 255;
376 host->cclk = host->mclk / (2 * (clk + 1));
377 }
Rabin Vincent4380c142010-07-21 12:55:18 +0100378
379 clk |= variant->clkreg_enable;
Linus Walleija6a64642009-09-14 12:56:14 +0100380 clk |= MCI_CLK_ENABLE;
381 /* This hasn't proven to be worthwhile */
382 /* clk |= MCI_CLK_PWRSAVE; */
383 }
384
Ulf Hanssonc58a8502013-05-13 15:40:03 +0100385 /* Set actual clock for debug */
386 host->mmc->actual_clock = host->cclk;
387
Linus Walleij9e6c82c2009-09-14 12:57:11 +0100388 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4)
Linus Walleij771dc152010-04-08 07:38:52 +0100389 clk |= MCI_4BIT_BUS;
390 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_8)
Srinivas Kandagatlae1412d82014-06-02 10:09:23 +0100391 clk |= variant->clkreg_8bit_bus_enable;
Linus Walleij9e6c82c2009-09-14 12:57:11 +0100392
Seungwon Jeon6dad6c92014-03-14 21:12:13 +0900393 if (host->mmc->ios.timing == MMC_TIMING_UHS_DDR50 ||
394 host->mmc->ios.timing == MMC_TIMING_MMC_DDR52)
Srinivas Kandagatlae8740642014-06-02 10:09:30 +0100395 clk |= variant->clkreg_neg_edge_enable;
Ulf Hansson6dbb6ee2013-01-07 15:30:44 +0100396
Ulf Hansson7437cfa2012-01-18 09:17:27 +0100397 mmci_write_clkreg(host, clk);
Linus Walleija6a64642009-09-14 12:56:14 +0100398}
399
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200400void mmci_dma_release(struct mmci_host *host)
401{
402 if (host->ops && host->ops->dma_release)
403 host->ops->dma_release(host);
404
405 host->use_dma = false;
406}
407
408void mmci_dma_setup(struct mmci_host *host)
409{
410 if (!host->ops || !host->ops->dma_setup)
411 return;
412
413 if (host->ops->dma_setup(host))
414 return;
415
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200416 /* initialize pre request cookie */
417 host->next_cookie = 1;
418
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200419 host->use_dma = true;
420}
421
Ludovic Barree0da1722018-10-08 14:08:41 +0200422/*
423 * Validate mmc prerequisites
424 */
425static int mmci_validate_data(struct mmci_host *host,
426 struct mmc_data *data)
427{
428 if (!data)
429 return 0;
430
431 if (!is_power_of_2(data->blksz)) {
432 dev_err(mmc_dev(host->mmc),
433 "unsupported block size (%d bytes)\n", data->blksz);
434 return -EINVAL;
435 }
436
437 if (host->ops && host->ops->validate_data)
438 return host->ops->validate_data(host, data);
439
440 return 0;
441}
442
Ludovic Barre47983512018-10-08 14:08:36 +0200443int mmci_prep_data(struct mmci_host *host, struct mmc_data *data, bool next)
444{
445 int err;
446
447 if (!host->ops || !host->ops->prep_data)
448 return 0;
449
450 err = host->ops->prep_data(host, data, next);
451
452 if (next && !err)
453 data->host_cookie = ++host->next_cookie < 0 ?
454 1 : host->next_cookie;
455
456 return err;
457}
458
459void mmci_unprep_data(struct mmci_host *host, struct mmc_data *data,
460 int err)
461{
462 if (host->ops && host->ops->unprep_data)
463 host->ops->unprep_data(host, data, err);
464
465 data->host_cookie = 0;
466}
467
Ludovic Barre02769962018-10-08 14:08:37 +0200468void mmci_get_next_data(struct mmci_host *host, struct mmc_data *data)
469{
470 WARN_ON(data->host_cookie && data->host_cookie != host->next_cookie);
471
472 if (host->ops && host->ops->get_next_data)
473 host->ops->get_next_data(host, data);
474}
475
Ludovic Barre135ea302018-10-08 14:08:38 +0200476int mmci_dma_start(struct mmci_host *host, unsigned int datactrl)
477{
478 struct mmc_data *data = host->data;
479 int ret;
480
481 if (!host->use_dma)
482 return -EINVAL;
483
484 ret = mmci_prep_data(host, data, false);
485 if (ret)
486 return ret;
487
488 if (!host->ops || !host->ops->dma_start)
489 return -EINVAL;
490
491 /* Okay, go for it. */
492 dev_vdbg(mmc_dev(host->mmc),
493 "Submit MMCI DMA job, sglen %d blksz %04x blks %04x flags %08x\n",
494 data->sg_len, data->blksz, data->blocks, data->flags);
495
496 host->ops->dma_start(host, &datactrl);
497
498 /* Trigger the DMA transfer */
499 mmci_write_datactrlreg(host, datactrl);
500
501 /*
502 * Let the MMCI say when the data is ended and it's time
503 * to fire next DMA request. When that happens, MMCI will
504 * call mmci_data_end()
505 */
506 writel(readl(host->base + MMCIMASK0) | MCI_DATAENDMASK,
507 host->base + MMCIMASK0);
508 return 0;
509}
510
Ludovic Barre5a9f10c2018-10-08 14:08:39 +0200511void mmci_dma_finalize(struct mmci_host *host, struct mmc_data *data)
512{
513 if (!host->use_dma)
514 return;
515
516 if (host->ops && host->ops->dma_finalize)
517 host->ops->dma_finalize(host, data);
518}
519
Ludovic Barrecfccc6a2018-10-08 14:08:40 +0200520void mmci_dma_error(struct mmci_host *host)
521{
522 if (!host->use_dma)
523 return;
524
525 if (host->ops && host->ops->dma_error)
526 host->ops->dma_error(host);
527}
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529static void
530mmci_request_end(struct mmci_host *host, struct mmc_request *mrq)
531{
532 writel(0, host->base + MMCICOMMAND);
533
Russell Kinge47c2222007-01-08 16:42:51 +0000534 BUG_ON(host->data);
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 host->mrq = NULL;
537 host->cmd = NULL;
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 mmc_request_done(host->mmc, mrq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
541
Linus Walleij2686b4b2010-10-19 12:39:48 +0100542static void mmci_set_mask1(struct mmci_host *host, unsigned int mask)
543{
544 void __iomem *base = host->base;
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100545 struct variant_data *variant = host->variant;
Linus Walleij2686b4b2010-10-19 12:39:48 +0100546
547 if (host->singleirq) {
548 unsigned int mask0 = readl(base + MMCIMASK0);
549
550 mask0 &= ~MCI_IRQ1MASK;
551 mask0 |= mask;
552
553 writel(mask0, base + MMCIMASK0);
554 }
555
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100556 if (variant->mmcimask1)
557 writel(mask, base + MMCIMASK1);
558
559 host->mask1_reg = mask;
Linus Walleij2686b4b2010-10-19 12:39:48 +0100560}
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562static void mmci_stop_data(struct mmci_host *host)
563{
Ulf Hansson9cc639a2013-05-15 20:48:23 +0100564 mmci_write_datactrlreg(host, 0);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100565 mmci_set_mask1(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 host->data = NULL;
567}
568
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100569static void mmci_init_sg(struct mmci_host *host, struct mmc_data *data)
570{
571 unsigned int flags = SG_MITER_ATOMIC;
572
573 if (data->flags & MMC_DATA_READ)
574 flags |= SG_MITER_TO_SG;
575 else
576 flags |= SG_MITER_FROM_SG;
577
578 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
579}
580
Russell Kingc8ebae32011-01-11 19:35:53 +0000581/*
582 * All the DMA operation mode stuff goes inside this ifdef.
583 * This assumes that you have a generic DMA device interface,
584 * no custom DMA interfaces are supported.
585 */
586#ifdef CONFIG_DMA_ENGINE
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200587struct mmci_dmae_next {
588 struct dma_async_tx_descriptor *desc;
589 struct dma_chan *chan;
590};
591
592struct mmci_dmae_priv {
593 struct dma_chan *cur;
594 struct dma_chan *rx_channel;
595 struct dma_chan *tx_channel;
596 struct dma_async_tx_descriptor *desc_current;
597 struct mmci_dmae_next next_data;
598};
599
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200600int mmci_dmae_setup(struct mmci_host *host)
Russell Kingc8ebae32011-01-11 19:35:53 +0000601{
Russell Kingc8ebae32011-01-11 19:35:53 +0000602 const char *rxname, *txname;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200603 struct mmci_dmae_priv *dmae;
Russell Kingc8ebae32011-01-11 19:35:53 +0000604
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200605 dmae = devm_kzalloc(mmc_dev(host->mmc), sizeof(*dmae), GFP_KERNEL);
606 if (!dmae)
607 return -ENOMEM;
Russell Kingc8ebae32011-01-11 19:35:53 +0000608
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200609 host->dma_priv = dmae;
610
611 dmae->rx_channel = dma_request_slave_channel(mmc_dev(host->mmc),
612 "rx");
613 dmae->tx_channel = dma_request_slave_channel(mmc_dev(host->mmc),
614 "tx");
Per Forlin58c7ccb2011-07-01 18:55:24 +0200615
Russell Kingc8ebae32011-01-11 19:35:53 +0000616 /*
617 * If only an RX channel is specified, the driver will
618 * attempt to use it bidirectionally, however if it is
619 * is specified but cannot be located, DMA will be disabled.
620 */
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200621 if (dmae->rx_channel && !dmae->tx_channel)
622 dmae->tx_channel = dmae->rx_channel;
Russell Kingc8ebae32011-01-11 19:35:53 +0000623
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200624 if (dmae->rx_channel)
625 rxname = dma_chan_name(dmae->rx_channel);
Russell Kingc8ebae32011-01-11 19:35:53 +0000626 else
627 rxname = "none";
628
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200629 if (dmae->tx_channel)
630 txname = dma_chan_name(dmae->tx_channel);
Russell Kingc8ebae32011-01-11 19:35:53 +0000631 else
632 txname = "none";
633
634 dev_info(mmc_dev(host->mmc), "DMA channels RX %s, TX %s\n",
635 rxname, txname);
636
637 /*
638 * Limit the maximum segment size in any SG entry according to
639 * the parameters of the DMA engine device.
640 */
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200641 if (dmae->tx_channel) {
642 struct device *dev = dmae->tx_channel->device->dev;
Russell Kingc8ebae32011-01-11 19:35:53 +0000643 unsigned int max_seg_size = dma_get_max_seg_size(dev);
644
645 if (max_seg_size < host->mmc->max_seg_size)
646 host->mmc->max_seg_size = max_seg_size;
647 }
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200648 if (dmae->rx_channel) {
649 struct device *dev = dmae->rx_channel->device->dev;
Russell Kingc8ebae32011-01-11 19:35:53 +0000650 unsigned int max_seg_size = dma_get_max_seg_size(dev);
651
652 if (max_seg_size < host->mmc->max_seg_size)
653 host->mmc->max_seg_size = max_seg_size;
654 }
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100655
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200656 if (!dmae->tx_channel || !dmae->rx_channel) {
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200657 mmci_dmae_release(host);
658 return -EINVAL;
659 }
660
661 return 0;
Russell Kingc8ebae32011-01-11 19:35:53 +0000662}
663
664/*
Bill Pemberton6e0ee712012-11-19 13:26:03 -0500665 * This is used in or so inline it
Russell Kingc8ebae32011-01-11 19:35:53 +0000666 * so it can be discarded.
667 */
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200668void mmci_dmae_release(struct mmci_host *host)
Russell Kingc8ebae32011-01-11 19:35:53 +0000669{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200670 struct mmci_dmae_priv *dmae = host->dma_priv;
671
672 if (dmae->rx_channel)
673 dma_release_channel(dmae->rx_channel);
674 if (dmae->tx_channel)
675 dma_release_channel(dmae->tx_channel);
676 dmae->rx_channel = dmae->tx_channel = NULL;
Russell Kingc8ebae32011-01-11 19:35:53 +0000677}
678
679static void mmci_dma_unmap(struct mmci_host *host, struct mmc_data *data)
680{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200681 struct mmci_dmae_priv *dmae = host->dma_priv;
Ulf Hansson653a7612013-01-21 21:29:34 +0100682 struct dma_chan *chan;
Ulf Hansson653a7612013-01-21 21:29:34 +0100683
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200684 if (data->flags & MMC_DATA_READ)
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200685 chan = dmae->rx_channel;
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200686 else
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200687 chan = dmae->tx_channel;
Ulf Hansson653a7612013-01-21 21:29:34 +0100688
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200689 dma_unmap_sg(chan->device->dev, data->sg, data->sg_len,
690 mmc_get_dma_dir(data));
Ulf Hansson653a7612013-01-21 21:29:34 +0100691}
692
Ludovic Barrecfccc6a2018-10-08 14:08:40 +0200693void mmci_dmae_error(struct mmci_host *host)
Ludovic Barre7b2a6d52018-09-21 11:45:55 +0200694{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200695 struct mmci_dmae_priv *dmae = host->dma_priv;
696
Ludovic Barrecfccc6a2018-10-08 14:08:40 +0200697 if (!dma_inprogress(host))
Ludovic Barrecdea1942018-09-21 11:45:56 +0200698 return;
699
Ludovic Barre7b2a6d52018-09-21 11:45:55 +0200700 dev_err(mmc_dev(host->mmc), "error during DMA transfer!\n");
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200701 dmaengine_terminate_all(dmae->cur);
Ludovic Barre7b2a6d52018-09-21 11:45:55 +0200702 host->dma_in_progress = false;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200703 dmae->cur = NULL;
704 dmae->desc_current = NULL;
Ludovic Barre7b2a6d52018-09-21 11:45:55 +0200705 host->data->host_cookie = 0;
706
707 mmci_dma_unmap(host, host->data);
708}
709
Ludovic Barre5a9f10c2018-10-08 14:08:39 +0200710void mmci_dmae_finalize(struct mmci_host *host, struct mmc_data *data)
Ulf Hansson653a7612013-01-21 21:29:34 +0100711{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200712 struct mmci_dmae_priv *dmae = host->dma_priv;
Russell Kingc8ebae32011-01-11 19:35:53 +0000713 u32 status;
714 int i;
715
Ludovic Barre5a9f10c2018-10-08 14:08:39 +0200716 if (!dma_inprogress(host))
Ludovic Barrecdea1942018-09-21 11:45:56 +0200717 return;
718
Russell Kingc8ebae32011-01-11 19:35:53 +0000719 /* Wait up to 1ms for the DMA to complete */
720 for (i = 0; ; i++) {
721 status = readl(host->base + MMCISTATUS);
722 if (!(status & MCI_RXDATAAVLBLMASK) || i >= 100)
723 break;
724 udelay(10);
725 }
726
727 /*
728 * Check to see whether we still have some data left in the FIFO -
729 * this catches DMA controllers which are unable to monitor the
730 * DMALBREQ and DMALSREQ signals while allowing us to DMA to non-
731 * contiguous buffers. On TX, we'll get a FIFO underrun error.
732 */
733 if (status & MCI_RXDATAAVLBLMASK) {
Ludovic Barrecfccc6a2018-10-08 14:08:40 +0200734 mmci_dma_error(host);
Russell Kingc8ebae32011-01-11 19:35:53 +0000735 if (!data->error)
736 data->error = -EIO;
Ludovic Barre7b2a6d52018-09-21 11:45:55 +0200737 } else if (!data->host_cookie) {
Ulf Hansson653a7612013-01-21 21:29:34 +0100738 mmci_dma_unmap(host, data);
Ludovic Barre7b2a6d52018-09-21 11:45:55 +0200739 }
Russell Kingc8ebae32011-01-11 19:35:53 +0000740
741 /*
742 * Use of DMA with scatter-gather is impossible.
743 * Give up with DMA and switch back to PIO mode.
744 */
745 if (status & MCI_RXDATAAVLBLMASK) {
746 dev_err(mmc_dev(host->mmc), "buggy DMA detected. Taking evasive action.\n");
747 mmci_dma_release(host);
748 }
Ulf Hansson653a7612013-01-21 21:29:34 +0100749
Linus Walleije13934b2017-01-27 15:04:54 +0100750 host->dma_in_progress = false;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200751 dmae->cur = NULL;
752 dmae->desc_current = NULL;
Russell Kingc8ebae32011-01-11 19:35:53 +0000753}
754
Ulf Hansson653a7612013-01-21 21:29:34 +0100755/* prepares DMA channel and DMA descriptor, returns non-zero on failure */
Ludovic Barre47983512018-10-08 14:08:36 +0200756static int _mmci_dmae_prep_data(struct mmci_host *host, struct mmc_data *data,
Ulf Hansson653a7612013-01-21 21:29:34 +0100757 struct dma_chan **dma_chan,
758 struct dma_async_tx_descriptor **dma_desc)
Russell Kingc8ebae32011-01-11 19:35:53 +0000759{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200760 struct mmci_dmae_priv *dmae = host->dma_priv;
Russell Kingc8ebae32011-01-11 19:35:53 +0000761 struct variant_data *variant = host->variant;
762 struct dma_slave_config conf = {
763 .src_addr = host->phybase + MMCIFIFO,
764 .dst_addr = host->phybase + MMCIFIFO,
765 .src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
766 .dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
767 .src_maxburst = variant->fifohalfsize >> 2, /* # of words */
768 .dst_maxburst = variant->fifohalfsize >> 2, /* # of words */
Viresh Kumar258aea72012-02-01 16:12:19 +0530769 .device_fc = false,
Russell Kingc8ebae32011-01-11 19:35:53 +0000770 };
Russell Kingc8ebae32011-01-11 19:35:53 +0000771 struct dma_chan *chan;
772 struct dma_device *device;
773 struct dma_async_tx_descriptor *desc;
774 int nr_sg;
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100775 unsigned long flags = DMA_CTRL_ACK;
Russell Kingc8ebae32011-01-11 19:35:53 +0000776
Russell Kingc8ebae32011-01-11 19:35:53 +0000777 if (data->flags & MMC_DATA_READ) {
Vinod Koul05f57992011-10-14 10:45:11 +0530778 conf.direction = DMA_DEV_TO_MEM;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200779 chan = dmae->rx_channel;
Russell Kingc8ebae32011-01-11 19:35:53 +0000780 } else {
Vinod Koul05f57992011-10-14 10:45:11 +0530781 conf.direction = DMA_MEM_TO_DEV;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200782 chan = dmae->tx_channel;
Russell Kingc8ebae32011-01-11 19:35:53 +0000783 }
784
785 /* If there's no DMA channel, fall back to PIO */
786 if (!chan)
787 return -EINVAL;
788
789 /* If less than or equal to the fifo size, don't bother with DMA */
Per Forlin58c7ccb2011-07-01 18:55:24 +0200790 if (data->blksz * data->blocks <= variant->fifosize)
Russell Kingc8ebae32011-01-11 19:35:53 +0000791 return -EINVAL;
792
793 device = chan->device;
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200794 nr_sg = dma_map_sg(device->dev, data->sg, data->sg_len,
795 mmc_get_dma_dir(data));
Russell Kingc8ebae32011-01-11 19:35:53 +0000796 if (nr_sg == 0)
797 return -EINVAL;
798
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100799 if (host->variant->qcom_dml)
800 flags |= DMA_PREP_INTERRUPT;
801
Russell Kingc8ebae32011-01-11 19:35:53 +0000802 dmaengine_slave_config(chan, &conf);
Alexandre Bounine16052822012-03-08 16:11:18 -0500803 desc = dmaengine_prep_slave_sg(chan, data->sg, nr_sg,
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100804 conf.direction, flags);
Russell Kingc8ebae32011-01-11 19:35:53 +0000805 if (!desc)
806 goto unmap_exit;
807
Ulf Hansson653a7612013-01-21 21:29:34 +0100808 *dma_chan = chan;
809 *dma_desc = desc;
Russell Kingc8ebae32011-01-11 19:35:53 +0000810
Per Forlin58c7ccb2011-07-01 18:55:24 +0200811 return 0;
812
813 unmap_exit:
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200814 dma_unmap_sg(device->dev, data->sg, data->sg_len,
815 mmc_get_dma_dir(data));
Per Forlin58c7ccb2011-07-01 18:55:24 +0200816 return -ENOMEM;
817}
818
Ludovic Barre47983512018-10-08 14:08:36 +0200819int mmci_dmae_prep_data(struct mmci_host *host,
820 struct mmc_data *data,
821 bool next)
Ulf Hansson653a7612013-01-21 21:29:34 +0100822{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200823 struct mmci_dmae_priv *dmae = host->dma_priv;
Ludovic Barread7b8912018-10-08 14:08:35 +0200824 struct mmci_dmae_next *nd = &dmae->next_data;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200825
Ludovic Barre47983512018-10-08 14:08:36 +0200826 if (!host->use_dma)
827 return -EINVAL;
828
Ludovic Barread7b8912018-10-08 14:08:35 +0200829 if (next)
Ludovic Barre47983512018-10-08 14:08:36 +0200830 return _mmci_dmae_prep_data(host, data, &nd->chan, &nd->desc);
Ulf Hansson653a7612013-01-21 21:29:34 +0100831 /* Check if next job is already prepared. */
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200832 if (dmae->cur && dmae->desc_current)
Ulf Hansson653a7612013-01-21 21:29:34 +0100833 return 0;
834
835 /* No job were prepared thus do it now. */
Ludovic Barre47983512018-10-08 14:08:36 +0200836 return _mmci_dmae_prep_data(host, data, &dmae->cur,
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200837 &dmae->desc_current);
Ulf Hansson653a7612013-01-21 21:29:34 +0100838}
839
Ludovic Barre135ea302018-10-08 14:08:38 +0200840int mmci_dmae_start(struct mmci_host *host, unsigned int *datactrl)
Per Forlin58c7ccb2011-07-01 18:55:24 +0200841{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200842 struct mmci_dmae_priv *dmae = host->dma_priv;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200843 struct mmc_data *data = host->data;
844
Linus Walleije13934b2017-01-27 15:04:54 +0100845 host->dma_in_progress = true;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200846 dmaengine_submit(dmae->desc_current);
847 dma_async_issue_pending(dmae->cur);
Russell Kingc8ebae32011-01-11 19:35:53 +0000848
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100849 if (host->variant->qcom_dml)
850 dml_start_xfer(host, data);
851
Ludovic Barre135ea302018-10-08 14:08:38 +0200852 *datactrl |= MCI_DPSM_DMAENABLE;
Russell Kingc8ebae32011-01-11 19:35:53 +0000853
Russell Kingc8ebae32011-01-11 19:35:53 +0000854 return 0;
Russell Kingc8ebae32011-01-11 19:35:53 +0000855}
Per Forlin58c7ccb2011-07-01 18:55:24 +0200856
Ludovic Barre02769962018-10-08 14:08:37 +0200857void mmci_dmae_get_next_data(struct mmci_host *host, struct mmc_data *data)
Per Forlin58c7ccb2011-07-01 18:55:24 +0200858{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200859 struct mmci_dmae_priv *dmae = host->dma_priv;
860 struct mmci_dmae_next *next = &dmae->next_data;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200861
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200862 if (!host->use_dma)
863 return;
864
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200865 WARN_ON(!data->host_cookie && (next->desc || next->chan));
Per Forlin58c7ccb2011-07-01 18:55:24 +0200866
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200867 dmae->desc_current = next->desc;
868 dmae->cur = next->chan;
869 next->desc = NULL;
870 next->chan = NULL;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200871}
872
Ludovic Barre47983512018-10-08 14:08:36 +0200873void mmci_dmae_unprep_data(struct mmci_host *host,
874 struct mmc_data *data, int err)
875
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
Ludovic Barre47983512018-10-08 14:08:36 +0200879 if (!host->use_dma)
Per Forlin58c7ccb2011-07-01 18:55:24 +0200880 return;
881
Ulf Hansson653a7612013-01-21 21:29:34 +0100882 mmci_dma_unmap(host, data);
Per Forlin58c7ccb2011-07-01 18:55:24 +0200883
Ulf Hansson653a7612013-01-21 21:29:34 +0100884 if (err) {
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200885 struct mmci_dmae_next *next = &dmae->next_data;
Ulf Hansson653a7612013-01-21 21:29:34 +0100886 struct dma_chan *chan;
887 if (data->flags & MMC_DATA_READ)
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200888 chan = dmae->rx_channel;
Ulf Hansson653a7612013-01-21 21:29:34 +0100889 else
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200890 chan = dmae->tx_channel;
Ulf Hansson653a7612013-01-21 21:29:34 +0100891 dmaengine_terminate_all(chan);
Per Forlin58c7ccb2011-07-01 18:55:24 +0200892
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200893 if (dmae->desc_current == next->desc)
894 dmae->desc_current = NULL;
Srinivas Kandagatlab5c16a62014-10-08 12:25:17 +0100895
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200896 if (dmae->cur == next->chan) {
Linus Walleije13934b2017-01-27 15:04:54 +0100897 host->dma_in_progress = false;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200898 dmae->cur = NULL;
Linus Walleije13934b2017-01-27 15:04:54 +0100899 }
Srinivas Kandagatlab5c16a62014-10-08 12:25:17 +0100900
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200901 next->desc = NULL;
902 next->chan = NULL;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200903 }
904}
905
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200906static struct mmci_host_ops mmci_variant_ops = {
Ludovic Barre47983512018-10-08 14:08:36 +0200907 .prep_data = mmci_dmae_prep_data,
908 .unprep_data = mmci_dmae_unprep_data,
Ludovic Barre02769962018-10-08 14:08:37 +0200909 .get_next_data = mmci_dmae_get_next_data,
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200910 .dma_setup = mmci_dmae_setup,
911 .dma_release = mmci_dmae_release,
Ludovic Barre135ea302018-10-08 14:08:38 +0200912 .dma_start = mmci_dmae_start,
Ludovic Barre5a9f10c2018-10-08 14:08:39 +0200913 .dma_finalize = mmci_dmae_finalize,
Ludovic Barrecfccc6a2018-10-08 14:08:40 +0200914 .dma_error = mmci_dmae_error,
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200915};
916
917void mmci_variant_init(struct mmci_host *host)
918{
919 host->ops = &mmci_variant_ops;
920}
Russell Kingc8ebae32011-01-11 19:35:53 +0000921#endif
922
Ludovic Barre47983512018-10-08 14:08:36 +0200923static void mmci_pre_request(struct mmc_host *mmc, struct mmc_request *mrq)
924{
925 struct mmci_host *host = mmc_priv(mmc);
926 struct mmc_data *data = mrq->data;
927
928 if (!data)
929 return;
930
931 WARN_ON(data->host_cookie);
932
933 if (mmci_validate_data(host, data))
934 return;
935
936 mmci_prep_data(host, data, true);
937}
938
939static void mmci_post_request(struct mmc_host *mmc, struct mmc_request *mrq,
940 int err)
941{
942 struct mmci_host *host = mmc_priv(mmc);
943 struct mmc_data *data = mrq->data;
944
945 if (!data || !data->host_cookie)
946 return;
947
948 mmci_unprep_data(host, data, err);
949}
950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951static void mmci_start_data(struct mmci_host *host, struct mmc_data *data)
952{
Rabin Vincent8301bb62010-08-09 12:57:30 +0100953 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 unsigned int datactrl, timeout, irqmask;
Russell King7b09cda2005-07-01 12:02:59 +0100955 unsigned long long clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 void __iomem *base;
Russell King3bc87f22006-08-27 13:51:28 +0100957 int blksz_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Linus Walleij64de0282010-02-19 01:09:10 +0100959 dev_dbg(mmc_dev(host->mmc), "blksz %04x blks %04x flags %08x\n",
960 data->blksz, data->blocks, data->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962 host->data = data;
Rabin Vincent528320d2010-07-21 12:49:49 +0100963 host->size = data->blksz * data->blocks;
Russell King51d43752011-01-27 10:56:52 +0000964 data->bytes_xfered = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Russell King7b09cda2005-07-01 12:02:59 +0100966 clks = (unsigned long long)data->timeout_ns * host->cclk;
Srinivas Kandagatlac4a35762014-06-02 10:08:39 +0100967 do_div(clks, NSEC_PER_SEC);
Russell King7b09cda2005-07-01 12:02:59 +0100968
969 timeout = data->timeout_clks + (unsigned int)clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971 base = host->base;
972 writel(timeout, base + MMCIDATATIMER);
973 writel(host->size, base + MMCIDATALENGTH);
974
Russell King3bc87f22006-08-27 13:51:28 +0100975 blksz_bits = ffs(data->blksz) - 1;
976 BUG_ON(1 << blksz_bits != data->blksz);
977
Philippe Langlais1784b152011-03-25 08:51:52 +0100978 if (variant->blksz_datactrl16)
979 datactrl = MCI_DPSM_ENABLE | (data->blksz << 16);
Srinivas Kandagatlaff783232014-06-02 10:09:06 +0100980 else if (variant->blksz_datactrl4)
981 datactrl = MCI_DPSM_ENABLE | (data->blksz << 4);
Philippe Langlais1784b152011-03-25 08:51:52 +0100982 else
983 datactrl = MCI_DPSM_ENABLE | blksz_bits << 4;
Russell Kingc8ebae32011-01-11 19:35:53 +0000984
985 if (data->flags & MMC_DATA_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 datactrl |= MCI_DPSM_DIRECTION;
Russell Kingc8ebae32011-01-11 19:35:53 +0000987
Srinivas Kandagatlac7354132014-08-22 05:55:16 +0100988 if (host->mmc->card && mmc_card_sdio(host->mmc->card)) {
989 u32 clk;
Ulf Hansson7258db72011-12-13 17:05:28 +0100990
Srinivas Kandagatlac7354132014-08-22 05:55:16 +0100991 datactrl |= variant->datactrl_mask_sdio;
Ulf Hansson06c1a122012-10-12 14:01:50 +0100992
Srinivas Kandagatlac7354132014-08-22 05:55:16 +0100993 /*
994 * The ST Micro variant for SDIO small write transfers
995 * needs to have clock H/W flow control disabled,
996 * otherwise the transfer will not start. The threshold
997 * depends on the rate of MCLK.
998 */
999 if (variant->st_sdio && data->flags & MMC_DATA_WRITE &&
1000 (host->size < 8 ||
1001 (host->size <= 8 && host->mclk > 50000000)))
1002 clk = host->clk_reg & ~variant->clkreg_enable;
1003 else
1004 clk = host->clk_reg | variant->clkreg_enable;
1005
1006 mmci_write_clkreg(host, clk);
1007 }
Ulf Hansson06c1a122012-10-12 14:01:50 +01001008
Seungwon Jeon6dad6c92014-03-14 21:12:13 +09001009 if (host->mmc->ios.timing == MMC_TIMING_UHS_DDR50 ||
1010 host->mmc->ios.timing == MMC_TIMING_MMC_DDR52)
Srinivas Kandagatlae17dca22014-06-02 10:09:15 +01001011 datactrl |= variant->datactrl_mask_ddrmode;
Ulf Hansson6dbb6ee2013-01-07 15:30:44 +01001012
Russell Kingc8ebae32011-01-11 19:35:53 +00001013 /*
1014 * Attempt to use DMA operation mode, if this
1015 * should fail, fall back to PIO mode
1016 */
Ludovic Barre135ea302018-10-08 14:08:38 +02001017 if (!mmci_dma_start(host, datactrl))
Russell Kingc8ebae32011-01-11 19:35:53 +00001018 return;
1019
1020 /* IRQ mode, map the SG list for CPU reading/writing */
1021 mmci_init_sg(host, data);
1022
1023 if (data->flags & MMC_DATA_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 irqmask = MCI_RXFIFOHALFFULLMASK;
Russell King0425a142006-02-16 16:48:31 +00001025
1026 /*
Russell Kingc4d877c2011-01-27 09:50:13 +00001027 * If we have less than the fifo 'half-full' threshold to
1028 * transfer, trigger a PIO interrupt as soon as any data
1029 * is available.
Russell King0425a142006-02-16 16:48:31 +00001030 */
Russell Kingc4d877c2011-01-27 09:50:13 +00001031 if (host->size < variant->fifohalfsize)
Russell King0425a142006-02-16 16:48:31 +00001032 irqmask |= MCI_RXDATAAVLBLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 } else {
1034 /*
1035 * We don't actually need to include "FIFO empty" here
1036 * since its implicit in "FIFO half empty".
1037 */
1038 irqmask = MCI_TXFIFOHALFEMPTYMASK;
1039 }
1040
Ulf Hansson9cc639a2013-05-15 20:48:23 +01001041 mmci_write_datactrlreg(host, datactrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 writel(readl(base + MMCIMASK0) & ~MCI_DATAENDMASK, base + MMCIMASK0);
Linus Walleij2686b4b2010-10-19 12:39:48 +01001043 mmci_set_mask1(host, irqmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044}
1045
1046static void
1047mmci_start_command(struct mmci_host *host, struct mmc_command *cmd, u32 c)
1048{
1049 void __iomem *base = host->base;
1050
Linus Walleij64de0282010-02-19 01:09:10 +01001051 dev_dbg(mmc_dev(host->mmc), "op %02x arg %08x flags %08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 cmd->opcode, cmd->arg, cmd->flags);
1053
Ludovic Barre0f244802018-10-08 14:08:45 +02001054 if (readl(base + MMCICOMMAND) & host->variant->cmdreg_cpsm_enable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 writel(0, base + MMCICOMMAND);
Srinivas Kandagatla6adb2a82014-06-02 10:08:57 +01001056 mmci_reg_delay(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 }
1058
Ludovic Barre0f244802018-10-08 14:08:45 +02001059 c |= cmd->opcode | host->variant->cmdreg_cpsm_enable;
Russell Kinge9225172006-02-02 12:23:12 +00001060 if (cmd->flags & MMC_RSP_PRESENT) {
1061 if (cmd->flags & MMC_RSP_136)
Ludovic Barre0f244802018-10-08 14:08:45 +02001062 c |= host->variant->cmdreg_lrsp_crc;
1063 else if (cmd->flags & MMC_RSP_CRC)
1064 c |= host->variant->cmdreg_srsp_crc;
1065 else
1066 c |= host->variant->cmdreg_srsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
1068 if (/*interrupt*/0)
1069 c |= MCI_CPSM_INTERRUPT;
1070
Srinivas Kandagatlaae7b0062014-06-02 10:09:39 +01001071 if (mmc_cmd_type(cmd) == MMC_CMD_ADTC)
1072 c |= host->variant->data_cmd_enable;
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 host->cmd = cmd;
1075
1076 writel(cmd->arg, base + MMCIARGUMENT);
1077 writel(c, base + MMCICOMMAND);
1078}
1079
1080static void
1081mmci_data_irq(struct mmci_host *host, struct mmc_data *data,
1082 unsigned int status)
1083{
Ludovic Barredaf97132018-10-08 14:08:44 +02001084 unsigned int status_err;
1085
Ulf Hansson1cb9da52014-06-12 14:42:23 +02001086 /* Make sure we have data to handle */
1087 if (!data)
1088 return;
1089
Linus Walleijf20f8f212010-10-19 13:41:24 +01001090 /* First check for errors */
Ludovic Barredaf97132018-10-08 14:08:44 +02001091 status_err = status & (host->variant->start_err |
1092 MCI_DATACRCFAIL | MCI_DATATIMEOUT |
1093 MCI_TXUNDERRUN | MCI_RXOVERRUN);
1094
1095 if (status_err) {
Linus Walleij8cb28152011-01-24 15:22:13 +01001096 u32 remain, success;
Linus Walleijf20f8f212010-10-19 13:41:24 +01001097
Russell Kingc8ebae32011-01-11 19:35:53 +00001098 /* Terminate the DMA transfer */
Ludovic Barrecfccc6a2018-10-08 14:08:40 +02001099 mmci_dma_error(host);
Russell Kingc8ebae32011-01-11 19:35:53 +00001100
Russell Kingc8afc9d2011-02-04 09:19:46 +00001101 /*
1102 * Calculate how far we are into the transfer. Note that
1103 * the data counter gives the number of bytes transferred
1104 * on the MMC bus, not on the host side. On reads, this
1105 * can be as much as a FIFO-worth of data ahead. This
1106 * matters for FIFO overruns only.
1107 */
Linus Walleijf5a106d2011-01-27 17:44:34 +01001108 remain = readl(host->base + MMCIDATACNT);
Linus Walleij8cb28152011-01-24 15:22:13 +01001109 success = data->blksz * data->blocks - remain;
1110
Russell Kingc8afc9d2011-02-04 09:19:46 +00001111 dev_dbg(mmc_dev(host->mmc), "MCI ERROR IRQ, status 0x%08x at 0x%08x\n",
Ludovic Barredaf97132018-10-08 14:08:44 +02001112 status_err, success);
1113 if (status_err & MCI_DATACRCFAIL) {
Linus Walleij8cb28152011-01-24 15:22:13 +01001114 /* Last block was not successful */
Russell Kingc8afc9d2011-02-04 09:19:46 +00001115 success -= 1;
Pierre Ossman17b04292007-07-22 22:18:46 +02001116 data->error = -EILSEQ;
Ludovic Barredaf97132018-10-08 14:08:44 +02001117 } else if (status_err & MCI_DATATIMEOUT) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001118 data->error = -ETIMEDOUT;
Ludovic Barredaf97132018-10-08 14:08:44 +02001119 } else if (status_err & MCI_STARTBITERR) {
Linus Walleij757df742011-06-30 15:10:21 +01001120 data->error = -ECOMM;
Ludovic Barredaf97132018-10-08 14:08:44 +02001121 } else if (status_err & MCI_TXUNDERRUN) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001122 data->error = -EIO;
Ludovic Barredaf97132018-10-08 14:08:44 +02001123 } else if (status_err & MCI_RXOVERRUN) {
Russell Kingc8afc9d2011-02-04 09:19:46 +00001124 if (success > host->variant->fifosize)
1125 success -= host->variant->fifosize;
1126 else
1127 success = 0;
Linus Walleij8cb28152011-01-24 15:22:13 +01001128 data->error = -EIO;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001129 }
Russell King51d43752011-01-27 10:56:52 +00001130 data->bytes_xfered = round_down(success, data->blksz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 }
Linus Walleijf20f8f212010-10-19 13:41:24 +01001132
Linus Walleij8cb28152011-01-24 15:22:13 +01001133 if (status & MCI_DATABLOCKEND)
1134 dev_err(mmc_dev(host->mmc), "stray MCI_DATABLOCKEND interrupt\n");
Linus Walleijf20f8f212010-10-19 13:41:24 +01001135
Russell Kingccff9b52011-01-30 21:03:50 +00001136 if (status & MCI_DATAEND || data->error) {
Ludovic Barrecdea1942018-09-21 11:45:56 +02001137 mmci_dma_finalize(host, data);
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 mmci_stop_data(host);
1140
Linus Walleij8cb28152011-01-24 15:22:13 +01001141 if (!data->error)
1142 /* The error clause is handled above, success! */
Russell King51d43752011-01-27 10:56:52 +00001143 data->bytes_xfered = data->blksz * data->blocks;
Linus Walleijf20f8f212010-10-19 13:41:24 +01001144
Ulf Hansson024629c2013-05-13 15:40:56 +01001145 if (!data->stop || host->mrq->sbc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 mmci_request_end(host, data->mrq);
1147 } else {
1148 mmci_start_command(host, data->stop, 0);
1149 }
1150 }
1151}
1152
1153static void
1154mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
1155 unsigned int status)
1156{
1157 void __iomem *base = host->base;
Linus Walleij49adc0c2016-10-25 11:06:06 +02001158 bool sbc;
Ulf Hanssonad82bfe2014-06-12 15:01:57 +02001159
1160 if (!cmd)
1161 return;
1162
1163 sbc = (cmd == host->mrq->sbc);
Ulf Hanssonad82bfe2014-06-12 15:01:57 +02001164
Linus Walleij49adc0c2016-10-25 11:06:06 +02001165 /*
1166 * We need to be one of these interrupts to be considered worth
1167 * handling. Note that we tag on any latent IRQs postponed
1168 * due to waiting for busy status.
1169 */
1170 if (!((status|host->busy_status) &
1171 (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT|MCI_CMDSENT|MCI_CMDRESPEND)))
Ulf Hanssonad82bfe2014-06-12 15:01:57 +02001172 return;
Ulf Hansson8d94b542014-01-13 16:49:31 +01001173
Linus Walleij49adc0c2016-10-25 11:06:06 +02001174 /*
1175 * ST Micro variant: handle busy detection.
1176 */
1177 if (host->variant->busy_detect) {
1178 bool busy_resp = !!(cmd->flags & MMC_RSP_BUSY);
Ulf Hansson8d94b542014-01-13 16:49:31 +01001179
Linus Walleij49adc0c2016-10-25 11:06:06 +02001180 /* We are busy with a command, return */
1181 if (host->busy_status &&
1182 (status & host->variant->busy_detect_flag))
1183 return;
Ulf Hansson8d94b542014-01-13 16:49:31 +01001184
Linus Walleij49adc0c2016-10-25 11:06:06 +02001185 /*
1186 * We were not busy, but we now got a busy response on
1187 * something that was not an error, and we double-check
1188 * that the special busy status bit is still set before
1189 * proceeding.
1190 */
1191 if (!host->busy_status && busy_resp &&
1192 !(status & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT)) &&
1193 (readl(base + MMCISTATUS) & host->variant->busy_detect_flag)) {
Jean-Nicolas Graux5cad24d2017-02-07 12:12:41 +01001194
1195 /* Clear the busy start IRQ */
1196 writel(host->variant->busy_detect_mask,
1197 host->base + MMCICLEAR);
1198
1199 /* Unmask the busy end IRQ */
Linus Walleij49adc0c2016-10-25 11:06:06 +02001200 writel(readl(base + MMCIMASK0) |
1201 host->variant->busy_detect_mask,
1202 base + MMCIMASK0);
1203 /*
1204 * Now cache the last response status code (until
1205 * the busy bit goes low), and return.
1206 */
1207 host->busy_status =
1208 status & (MCI_CMDSENT|MCI_CMDRESPEND);
1209 return;
1210 }
1211
1212 /*
1213 * At this point we are not busy with a command, we have
Jean-Nicolas Graux5cad24d2017-02-07 12:12:41 +01001214 * not received a new busy request, clear and mask the busy
1215 * end IRQ and fall through to process the IRQ.
Linus Walleij49adc0c2016-10-25 11:06:06 +02001216 */
1217 if (host->busy_status) {
Jean-Nicolas Graux5cad24d2017-02-07 12:12:41 +01001218
1219 writel(host->variant->busy_detect_mask,
1220 host->base + MMCICLEAR);
1221
Linus Walleij49adc0c2016-10-25 11:06:06 +02001222 writel(readl(base + MMCIMASK0) &
1223 ~host->variant->busy_detect_mask,
1224 base + MMCIMASK0);
1225 host->busy_status = 0;
1226 }
Ulf Hansson8d94b542014-01-13 16:49:31 +01001227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229 host->cmd = NULL;
1230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 if (status & MCI_CMDTIMEOUT) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001232 cmd->error = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001234 cmd->error = -EILSEQ;
Russell King - ARM Linux9047b432011-01-11 16:35:56 +00001235 } else {
1236 cmd->resp[0] = readl(base + MMCIRESPONSE0);
1237 cmd->resp[1] = readl(base + MMCIRESPONSE1);
1238 cmd->resp[2] = readl(base + MMCIRESPONSE2);
1239 cmd->resp[3] = readl(base + MMCIRESPONSE3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 }
1241
Ulf Hansson024629c2013-05-13 15:40:56 +01001242 if ((!sbc && !cmd->data) || cmd->error) {
Ulf Hansson3b6e3c72011-12-13 16:58:43 +01001243 if (host->data) {
1244 /* Terminate the DMA transfer */
Ludovic Barrecfccc6a2018-10-08 14:08:40 +02001245 mmci_dma_error(host);
Ludovic Barre7b2a6d52018-09-21 11:45:55 +02001246
Russell Kinge47c2222007-01-08 16:42:51 +00001247 mmci_stop_data(host);
Ulf Hansson3b6e3c72011-12-13 16:58:43 +01001248 }
Ulf Hansson024629c2013-05-13 15:40:56 +01001249 mmci_request_end(host, host->mrq);
1250 } else if (sbc) {
1251 mmci_start_command(host, host->mrq->cmd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 } else if (!(cmd->data->flags & MMC_DATA_READ)) {
1253 mmci_start_data(host, cmd->data);
1254 }
1255}
1256
Srinivas Kandagatla9c34b732014-06-02 10:10:04 +01001257static int mmci_get_rx_fifocnt(struct mmci_host *host, u32 status, int remain)
1258{
1259 return remain - (readl(host->base + MMCIFIFOCNT) << 2);
1260}
1261
1262static int mmci_qcom_get_rx_fifocnt(struct mmci_host *host, u32 status, int r)
1263{
1264 /*
1265 * on qcom SDCC4 only 8 words are used in each burst so only 8 addresses
1266 * from the fifo range should be used
1267 */
1268 if (status & MCI_RXFIFOHALFFULL)
1269 return host->variant->fifohalfsize;
1270 else if (status & MCI_RXDATAAVLBL)
1271 return 4;
1272
1273 return 0;
1274}
1275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276static int mmci_pio_read(struct mmci_host *host, char *buffer, unsigned int remain)
1277{
1278 void __iomem *base = host->base;
1279 char *ptr = buffer;
Srinivas Kandagatla9c34b732014-06-02 10:10:04 +01001280 u32 status = readl(host->base + MMCISTATUS);
Linus Walleij26eed9a2008-04-26 23:39:44 +01001281 int host_remain = host->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
1283 do {
Srinivas Kandagatla9c34b732014-06-02 10:10:04 +01001284 int count = host->get_rx_fifocnt(host, status, host_remain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 if (count > remain)
1287 count = remain;
1288
1289 if (count <= 0)
1290 break;
1291
Ulf Hansson393e5e22011-12-13 17:08:04 +01001292 /*
1293 * SDIO especially may want to send something that is
1294 * not divisible by 4 (as opposed to card sectors
1295 * etc). Therefore make sure to always read the last bytes
1296 * while only doing full 32-bit reads towards the FIFO.
1297 */
1298 if (unlikely(count & 0x3)) {
1299 if (count < 4) {
1300 unsigned char buf[4];
Davide Ciminaghi4b85da02012-12-10 14:47:21 +01001301 ioread32_rep(base + MMCIFIFO, buf, 1);
Ulf Hansson393e5e22011-12-13 17:08:04 +01001302 memcpy(ptr, buf, count);
1303 } else {
Davide Ciminaghi4b85da02012-12-10 14:47:21 +01001304 ioread32_rep(base + MMCIFIFO, ptr, count >> 2);
Ulf Hansson393e5e22011-12-13 17:08:04 +01001305 count &= ~0x3;
1306 }
1307 } else {
Davide Ciminaghi4b85da02012-12-10 14:47:21 +01001308 ioread32_rep(base + MMCIFIFO, ptr, count >> 2);
Ulf Hansson393e5e22011-12-13 17:08:04 +01001309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 ptr += count;
1312 remain -= count;
Linus Walleij26eed9a2008-04-26 23:39:44 +01001313 host_remain -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 if (remain == 0)
1316 break;
1317
1318 status = readl(base + MMCISTATUS);
1319 } while (status & MCI_RXDATAAVLBL);
1320
1321 return ptr - buffer;
1322}
1323
1324static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status)
1325{
Rabin Vincent8301bb62010-08-09 12:57:30 +01001326 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 void __iomem *base = host->base;
1328 char *ptr = buffer;
1329
1330 do {
1331 unsigned int count, maxcnt;
1332
Rabin Vincent8301bb62010-08-09 12:57:30 +01001333 maxcnt = status & MCI_TXFIFOEMPTY ?
1334 variant->fifosize : variant->fifohalfsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 count = min(remain, maxcnt);
1336
Linus Walleij34177802010-10-19 12:43:58 +01001337 /*
Linus Walleij34177802010-10-19 12:43:58 +01001338 * SDIO especially may want to send something that is
1339 * not divisible by 4 (as opposed to card sectors
1340 * etc), and the FIFO only accept full 32-bit writes.
1341 * So compensate by adding +3 on the count, a single
1342 * byte become a 32bit write, 7 bytes will be two
1343 * 32bit writes etc.
1344 */
Davide Ciminaghi4b85da02012-12-10 14:47:21 +01001345 iowrite32_rep(base + MMCIFIFO, ptr, (count + 3) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347 ptr += count;
1348 remain -= count;
1349
1350 if (remain == 0)
1351 break;
1352
1353 status = readl(base + MMCISTATUS);
1354 } while (status & MCI_TXFIFOHALFEMPTY);
1355
1356 return ptr - buffer;
1357}
1358
1359/*
1360 * PIO data transfer IRQ handler.
1361 */
David Howells7d12e782006-10-05 14:55:46 +01001362static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
1364 struct mmci_host *host = dev_id;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001365 struct sg_mapping_iter *sg_miter = &host->sg_miter;
Rabin Vincent8301bb62010-08-09 12:57:30 +01001366 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 void __iomem *base = host->base;
1368 u32 status;
1369
1370 status = readl(base + MMCISTATUS);
1371
Linus Walleij64de0282010-02-19 01:09:10 +01001372 dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 unsigned int remain, len;
1376 char *buffer;
1377
1378 /*
1379 * For write, we only need to test the half-empty flag
1380 * here - if the FIFO is completely empty, then by
1381 * definition it is more than half empty.
1382 *
1383 * For read, check for data available.
1384 */
1385 if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL)))
1386 break;
1387
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001388 if (!sg_miter_next(sg_miter))
1389 break;
1390
1391 buffer = sg_miter->addr;
1392 remain = sg_miter->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394 len = 0;
1395 if (status & MCI_RXACTIVE)
1396 len = mmci_pio_read(host, buffer, remain);
1397 if (status & MCI_TXACTIVE)
1398 len = mmci_pio_write(host, buffer, remain, status);
1399
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001400 sg_miter->consumed = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 host->size -= len;
1403 remain -= len;
1404
1405 if (remain)
1406 break;
1407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 status = readl(base + MMCISTATUS);
1409 } while (1);
1410
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001411 sg_miter_stop(sg_miter);
1412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 /*
Russell Kingc4d877c2011-01-27 09:50:13 +00001414 * If we have less than the fifo 'half-full' threshold to transfer,
1415 * trigger a PIO interrupt as soon as any data is available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 */
Russell Kingc4d877c2011-01-27 09:50:13 +00001417 if (status & MCI_RXACTIVE && host->size < variant->fifohalfsize)
Linus Walleij2686b4b2010-10-19 12:39:48 +01001418 mmci_set_mask1(host, MCI_RXDATAAVLBLMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
1420 /*
1421 * If we run out of data, disable the data IRQs; this
1422 * prevents a race where the FIFO becomes empty before
1423 * the chip itself has disabled the data path, and
1424 * stops us racing with our data end IRQ.
1425 */
1426 if (host->size == 0) {
Linus Walleij2686b4b2010-10-19 12:39:48 +01001427 mmci_set_mask1(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0);
1429 }
1430
1431 return IRQ_HANDLED;
1432}
1433
1434/*
1435 * Handle completion of command and data transfers.
1436 */
David Howells7d12e782006-10-05 14:55:46 +01001437static irqreturn_t mmci_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
1439 struct mmci_host *host = dev_id;
1440 u32 status;
1441 int ret = 0;
1442
1443 spin_lock(&host->lock);
1444
1445 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 status = readl(host->base + MMCISTATUS);
Linus Walleij2686b4b2010-10-19 12:39:48 +01001447
1448 if (host->singleirq) {
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +01001449 if (status & host->mask1_reg)
Linus Walleij2686b4b2010-10-19 12:39:48 +01001450 mmci_pio_irq(irq, dev_id);
1451
1452 status &= ~MCI_IRQ1MASK;
1453 }
1454
Ulf Hansson8d94b542014-01-13 16:49:31 +01001455 /*
Jean-Nicolas Graux5cad24d2017-02-07 12:12:41 +01001456 * We intentionally clear the MCI_ST_CARDBUSY IRQ (if it's
1457 * enabled) in mmci_cmd_irq() function where ST Micro busy
1458 * detection variant is handled. Considering the HW seems to be
1459 * triggering the IRQ on both edges while monitoring DAT0 for
1460 * busy completion and that same status bit is used to monitor
1461 * start and end of busy detection, special care must be taken
1462 * to make sure that both start and end interrupts are always
1463 * cleared one after the other.
Ulf Hansson8d94b542014-01-13 16:49:31 +01001464 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 status &= readl(host->base + MMCIMASK0);
Jean-Nicolas Graux5cad24d2017-02-07 12:12:41 +01001466 if (host->variant->busy_detect)
1467 writel(status & ~host->variant->busy_detect_mask,
1468 host->base + MMCICLEAR);
1469 else
1470 writel(status, host->base + MMCICLEAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Linus Walleij64de0282010-02-19 01:09:10 +01001472 dev_dbg(mmc_dev(host->mmc), "irq0 (data+cmd) %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Ulf Hansson78782892014-06-13 13:21:38 +02001474 if (host->variant->reversed_irq_handling) {
1475 mmci_data_irq(host, host->data, status);
1476 mmci_cmd_irq(host, host->cmd, status);
1477 } else {
1478 mmci_cmd_irq(host, host->cmd, status);
1479 mmci_data_irq(host, host->data, status);
1480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Linus Walleij49adc0c2016-10-25 11:06:06 +02001482 /*
1483 * Don't poll for busy completion in irq context.
1484 */
1485 if (host->variant->busy_detect && host->busy_status)
1486 status &= ~host->variant->busy_detect_flag;
Ulf Hansson8d94b542014-01-13 16:49:31 +01001487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 ret = 1;
1489 } while (status);
1490
1491 spin_unlock(&host->lock);
1492
1493 return IRQ_RETVAL(ret);
1494}
1495
1496static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1497{
1498 struct mmci_host *host = mmc_priv(mmc);
Linus Walleij9e943022008-10-24 21:17:50 +01001499 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
1501 WARN_ON(host->mrq != NULL);
1502
Ulf Hansson653a7612013-01-21 21:29:34 +01001503 mrq->cmd->error = mmci_validate_data(host, mrq->data);
1504 if (mrq->cmd->error) {
Pierre Ossman255d01a2007-07-24 20:38:53 +02001505 mmc_request_done(mmc, mrq);
1506 return;
1507 }
1508
Linus Walleij9e943022008-10-24 21:17:50 +01001509 spin_lock_irqsave(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
1511 host->mrq = mrq;
1512
Per Forlin58c7ccb2011-07-01 18:55:24 +02001513 if (mrq->data)
1514 mmci_get_next_data(host, mrq->data);
1515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 if (mrq->data && mrq->data->flags & MMC_DATA_READ)
1517 mmci_start_data(host, mrq->data);
1518
Ulf Hansson024629c2013-05-13 15:40:56 +01001519 if (mrq->sbc)
1520 mmci_start_command(host, mrq->sbc, 0);
1521 else
1522 mmci_start_command(host, mrq->cmd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Linus Walleij9e943022008-10-24 21:17:50 +01001524 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525}
1526
1527static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1528{
1529 struct mmci_host *host = mmc_priv(mmc);
Ulf Hansson7d72a1d2011-12-13 16:54:55 +01001530 struct variant_data *variant = host->variant;
Linus Walleija6a64642009-09-14 12:56:14 +01001531 u32 pwr = 0;
1532 unsigned long flags;
Lee Jonesdb90f912013-05-03 12:52:12 +01001533 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Ulf Hanssonbc521812011-12-13 16:57:55 +01001535 if (host->plat->ios_handler &&
1536 host->plat->ios_handler(mmc_dev(mmc), ios))
1537 dev_err(mmc_dev(mmc), "platform ios_handler failed\n");
1538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 switch (ios->power_mode) {
1540 case MMC_POWER_OFF:
Ulf Hansson599c1d52013-01-07 16:22:50 +01001541 if (!IS_ERR(mmc->supply.vmmc))
1542 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Lee Jones237fb5e2013-01-31 11:27:52 +00001543
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001544 if (!IS_ERR(mmc->supply.vqmmc) && host->vqmmc_enabled) {
Lee Jones237fb5e2013-01-31 11:27:52 +00001545 regulator_disable(mmc->supply.vqmmc);
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001546 host->vqmmc_enabled = false;
1547 }
Lee Jones237fb5e2013-01-31 11:27:52 +00001548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 break;
1550 case MMC_POWER_UP:
Ulf Hansson599c1d52013-01-07 16:22:50 +01001551 if (!IS_ERR(mmc->supply.vmmc))
1552 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd);
1553
Ulf Hansson7d72a1d2011-12-13 16:54:55 +01001554 /*
1555 * The ST Micro variant doesn't have the PL180s MCI_PWR_UP
1556 * and instead uses MCI_PWR_ON so apply whatever value is
1557 * configured in the variant data.
1558 */
1559 pwr |= variant->pwrreg_powerup;
1560
1561 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 case MMC_POWER_ON:
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001563 if (!IS_ERR(mmc->supply.vqmmc) && !host->vqmmc_enabled) {
Lee Jonesdb90f912013-05-03 12:52:12 +01001564 ret = regulator_enable(mmc->supply.vqmmc);
1565 if (ret < 0)
1566 dev_err(mmc_dev(mmc),
1567 "failed to enable vqmmc regulator\n");
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001568 else
1569 host->vqmmc_enabled = true;
Lee Jonesdb90f912013-05-03 12:52:12 +01001570 }
Lee Jones237fb5e2013-01-31 11:27:52 +00001571
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 pwr |= MCI_PWR_ON;
1573 break;
1574 }
1575
Ulf Hansson4d1a3a02011-12-13 16:57:07 +01001576 if (variant->signal_direction && ios->power_mode != MMC_POWER_OFF) {
1577 /*
1578 * The ST Micro variant has some additional bits
1579 * indicating signal direction for the signals in
1580 * the SD/MMC bus and feedback-clock usage.
1581 */
Ulf Hansson4593df22014-03-21 10:13:05 +01001582 pwr |= host->pwr_reg_add;
Ulf Hansson4d1a3a02011-12-13 16:57:07 +01001583
1584 if (ios->bus_width == MMC_BUS_WIDTH_4)
1585 pwr &= ~MCI_ST_DATA74DIREN;
1586 else if (ios->bus_width == MMC_BUS_WIDTH_1)
1587 pwr &= (~MCI_ST_DATA74DIREN &
1588 ~MCI_ST_DATA31DIREN &
1589 ~MCI_ST_DATA2DIREN);
1590 }
1591
Patrice Chotardf9bb3042018-01-18 15:34:20 +01001592 if (variant->opendrain) {
1593 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
1594 pwr |= variant->opendrain;
1595 } else {
1596 /*
1597 * If the variant cannot configure the pads by its own, then we
1598 * expect the pinctrl to be able to do that for us
1599 */
1600 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
1601 pinctrl_select_state(host->pinctrl, host->pins_opendrain);
1602 else
1603 pinctrl_select_state(host->pinctrl, host->pins_default);
1604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Ulf Hanssonf4670da2013-01-09 17:19:54 +01001606 /*
1607 * If clock = 0 and the variant requires the MMCIPOWER to be used for
1608 * gating the clock, the MCI_PWR_ON bit is cleared.
1609 */
1610 if (!ios->clock && variant->pwrreg_clkgate)
1611 pwr &= ~MCI_PWR_ON;
1612
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001613 if (host->variant->explicit_mclk_control &&
1614 ios->clock != host->clock_cache) {
1615 ret = clk_set_rate(host->clk, ios->clock);
1616 if (ret < 0)
1617 dev_err(mmc_dev(host->mmc),
1618 "Error setting clock rate (%d)\n", ret);
1619 else
1620 host->mclk = clk_get_rate(host->clk);
1621 }
1622 host->clock_cache = ios->clock;
1623
Linus Walleija6a64642009-09-14 12:56:14 +01001624 spin_lock_irqsave(&host->lock, flags);
1625
Ludovic Barrecd3ee8c2018-10-08 14:08:42 +02001626 if (host->ops && host->ops->set_clkreg)
1627 host->ops->set_clkreg(host, ios->clock);
1628 else
1629 mmci_set_clkreg(host, ios->clock);
1630
1631 if (host->ops && host->ops->set_pwrreg)
1632 host->ops->set_pwrreg(host, pwr);
1633 else
1634 mmci_write_pwrreg(host, pwr);
1635
Ulf Hanssonf829c042013-09-04 09:01:15 +01001636 mmci_reg_delay(host);
Linus Walleija6a64642009-09-14 12:56:14 +01001637
1638 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639}
1640
Russell King89001442009-07-09 15:16:07 +01001641static int mmci_get_cd(struct mmc_host *mmc)
1642{
1643 struct mmci_host *host = mmc_priv(mmc);
Rabin Vincent29719442010-08-09 12:54:43 +01001644 struct mmci_platform_data *plat = host->plat;
Ulf Hanssond2762092014-03-17 13:56:19 +01001645 unsigned int status = mmc_gpio_get_cd(mmc);
Russell King89001442009-07-09 15:16:07 +01001646
Ulf Hanssond2762092014-03-17 13:56:19 +01001647 if (status == -ENOSYS) {
Rabin Vincent4b8caec2010-08-09 12:56:40 +01001648 if (!plat->status)
1649 return 1; /* Assume always present */
1650
Rabin Vincent29719442010-08-09 12:54:43 +01001651 status = plat->status(mmc_dev(host->mmc));
Ulf Hanssond2762092014-03-17 13:56:19 +01001652 }
Russell King74bc8092010-07-29 15:58:59 +01001653 return status;
Russell King89001442009-07-09 15:16:07 +01001654}
1655
Ulf Hansson0f3ed7f2013-05-15 20:47:33 +01001656static int mmci_sig_volt_switch(struct mmc_host *mmc, struct mmc_ios *ios)
1657{
1658 int ret = 0;
1659
1660 if (!IS_ERR(mmc->supply.vqmmc)) {
1661
Ulf Hansson0f3ed7f2013-05-15 20:47:33 +01001662 switch (ios->signal_voltage) {
1663 case MMC_SIGNAL_VOLTAGE_330:
1664 ret = regulator_set_voltage(mmc->supply.vqmmc,
1665 2700000, 3600000);
1666 break;
1667 case MMC_SIGNAL_VOLTAGE_180:
1668 ret = regulator_set_voltage(mmc->supply.vqmmc,
1669 1700000, 1950000);
1670 break;
1671 case MMC_SIGNAL_VOLTAGE_120:
1672 ret = regulator_set_voltage(mmc->supply.vqmmc,
1673 1100000, 1300000);
1674 break;
1675 }
1676
1677 if (ret)
1678 dev_warn(mmc_dev(mmc), "Voltage switch failed\n");
Ulf Hansson0f3ed7f2013-05-15 20:47:33 +01001679 }
1680
1681 return ret;
1682}
1683
Ulf Hansson01259622013-05-15 20:53:22 +01001684static struct mmc_host_ops mmci_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 .request = mmci_request,
Per Forlin58c7ccb2011-07-01 18:55:24 +02001686 .pre_req = mmci_pre_request,
1687 .post_req = mmci_post_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 .set_ios = mmci_set_ios,
Ulf Hanssond2762092014-03-17 13:56:19 +01001689 .get_ro = mmc_gpio_get_ro,
Russell King89001442009-07-09 15:16:07 +01001690 .get_cd = mmci_get_cd,
Ulf Hansson0f3ed7f2013-05-15 20:47:33 +01001691 .start_signal_voltage_switch = mmci_sig_volt_switch,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692};
1693
Ulf Hansson78f87df2014-03-17 15:53:07 +01001694static int mmci_of_parse(struct device_node *np, struct mmc_host *mmc)
1695{
Ulf Hansson4593df22014-03-21 10:13:05 +01001696 struct mmci_host *host = mmc_priv(mmc);
Ulf Hansson78f87df2014-03-17 15:53:07 +01001697 int ret = mmc_of_parse(mmc);
Lee Jones000bc9d2012-04-16 10:18:43 +01001698
Ulf Hansson78f87df2014-03-17 15:53:07 +01001699 if (ret)
1700 return ret;
Lee Jones000bc9d2012-04-16 10:18:43 +01001701
Ulf Hansson4593df22014-03-21 10:13:05 +01001702 if (of_get_property(np, "st,sig-dir-dat0", NULL))
1703 host->pwr_reg_add |= MCI_ST_DATA0DIREN;
1704 if (of_get_property(np, "st,sig-dir-dat2", NULL))
1705 host->pwr_reg_add |= MCI_ST_DATA2DIREN;
1706 if (of_get_property(np, "st,sig-dir-dat31", NULL))
1707 host->pwr_reg_add |= MCI_ST_DATA31DIREN;
1708 if (of_get_property(np, "st,sig-dir-dat74", NULL))
1709 host->pwr_reg_add |= MCI_ST_DATA74DIREN;
1710 if (of_get_property(np, "st,sig-dir-cmd", NULL))
1711 host->pwr_reg_add |= MCI_ST_CMDDIREN;
1712 if (of_get_property(np, "st,sig-pin-fbclk", NULL))
1713 host->pwr_reg_add |= MCI_ST_FBCLKEN;
1714
Lee Jones000bc9d2012-04-16 10:18:43 +01001715 if (of_get_property(np, "mmc-cap-mmc-highspeed", NULL))
Ulf Hansson78f87df2014-03-17 15:53:07 +01001716 mmc->caps |= MMC_CAP_MMC_HIGHSPEED;
Lee Jones000bc9d2012-04-16 10:18:43 +01001717 if (of_get_property(np, "mmc-cap-sd-highspeed", NULL))
Ulf Hansson78f87df2014-03-17 15:53:07 +01001718 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
Lee Jones000bc9d2012-04-16 10:18:43 +01001719
Ulf Hansson78f87df2014-03-17 15:53:07 +01001720 return 0;
Lee Jones000bc9d2012-04-16 10:18:43 +01001721}
Lee Jones000bc9d2012-04-16 10:18:43 +01001722
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001723static int mmci_probe(struct amba_device *dev,
Russell Kingaa25afa2011-02-19 15:55:00 +00001724 const struct amba_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725{
Linus Walleij6ef297f2009-09-22 14:29:36 +01001726 struct mmci_platform_data *plat = dev->dev.platform_data;
Lee Jones000bc9d2012-04-16 10:18:43 +01001727 struct device_node *np = dev->dev.of_node;
Rabin Vincent4956e102010-07-21 12:54:40 +01001728 struct variant_data *variant = id->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 struct mmci_host *host;
1730 struct mmc_host *mmc;
1731 int ret;
1732
Lee Jones000bc9d2012-04-16 10:18:43 +01001733 /* Must have platform data or Device Tree. */
1734 if (!plat && !np) {
1735 dev_err(&dev->dev, "No plat data or DT found\n");
1736 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 }
1738
Lee Jonesb9b52912012-06-12 10:49:51 +01001739 if (!plat) {
1740 plat = devm_kzalloc(&dev->dev, sizeof(*plat), GFP_KERNEL);
1741 if (!plat)
1742 return -ENOMEM;
1743 }
1744
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
Ulf Hanssonef289982014-03-17 13:56:32 +01001746 if (!mmc)
1747 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Ulf Hansson78f87df2014-03-17 15:53:07 +01001749 ret = mmci_of_parse(np, mmc);
1750 if (ret)
1751 goto host_free;
1752
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 host = mmc_priv(mmc);
Rabin Vincent4ea580f2009-04-17 08:44:19 +05301754 host->mmc = mmc;
Russell King012b7d32009-07-09 15:13:56 +01001755
Patrice Chotardf9bb3042018-01-18 15:34:20 +01001756 /*
1757 * Some variant (STM32) doesn't have opendrain bit, nevertheless
1758 * pins can be set accordingly using pinctrl
1759 */
1760 if (!variant->opendrain) {
1761 host->pinctrl = devm_pinctrl_get(&dev->dev);
1762 if (IS_ERR(host->pinctrl)) {
1763 dev_err(&dev->dev, "failed to get pinctrl");
Wei Yongjun310eb252018-01-23 02:09:13 +00001764 ret = PTR_ERR(host->pinctrl);
Patrice Chotardf9bb3042018-01-18 15:34:20 +01001765 goto host_free;
1766 }
1767
1768 host->pins_default = pinctrl_lookup_state(host->pinctrl,
1769 PINCTRL_STATE_DEFAULT);
1770 if (IS_ERR(host->pins_default)) {
1771 dev_err(mmc_dev(mmc), "Can't select default pins\n");
Wei Yongjun310eb252018-01-23 02:09:13 +00001772 ret = PTR_ERR(host->pins_default);
Patrice Chotardf9bb3042018-01-18 15:34:20 +01001773 goto host_free;
1774 }
1775
1776 host->pins_opendrain = pinctrl_lookup_state(host->pinctrl,
1777 MMCI_PINCTRL_STATE_OPENDRAIN);
1778 if (IS_ERR(host->pins_opendrain)) {
1779 dev_err(mmc_dev(mmc), "Can't select opendrain pins\n");
Wei Yongjun310eb252018-01-23 02:09:13 +00001780 ret = PTR_ERR(host->pins_opendrain);
Patrice Chotardf9bb3042018-01-18 15:34:20 +01001781 goto host_free;
1782 }
1783 }
1784
Russell King012b7d32009-07-09 15:13:56 +01001785 host->hw_designer = amba_manf(dev);
1786 host->hw_revision = amba_rev(dev);
Linus Walleij64de0282010-02-19 01:09:10 +01001787 dev_dbg(mmc_dev(mmc), "designer ID = 0x%02x\n", host->hw_designer);
1788 dev_dbg(mmc_dev(mmc), "revision = 0x%01x\n", host->hw_revision);
Russell King012b7d32009-07-09 15:13:56 +01001789
Ulf Hansson665ba562013-05-13 15:39:17 +01001790 host->clk = devm_clk_get(&dev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 if (IS_ERR(host->clk)) {
1792 ret = PTR_ERR(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 goto host_free;
1794 }
1795
Julia Lawallac940932012-08-26 16:00:59 +00001796 ret = clk_prepare_enable(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 if (ret)
Ulf Hansson665ba562013-05-13 15:39:17 +01001798 goto host_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
Srinivas Kandagatla9c34b732014-06-02 10:10:04 +01001800 if (variant->qcom_fifo)
1801 host->get_rx_fifocnt = mmci_qcom_get_rx_fifocnt;
1802 else
1803 host->get_rx_fifocnt = mmci_get_rx_fifocnt;
1804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 host->plat = plat;
Rabin Vincent4956e102010-07-21 12:54:40 +01001806 host->variant = variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 host->mclk = clk_get_rate(host->clk);
Linus Walleijc8df9a52008-04-29 09:34:07 +01001808 /*
1809 * According to the spec, mclk is max 100 MHz,
1810 * so we try to adjust the clock down to this,
1811 * (if possible).
1812 */
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +01001813 if (host->mclk > variant->f_max) {
1814 ret = clk_set_rate(host->clk, variant->f_max);
Linus Walleijc8df9a52008-04-29 09:34:07 +01001815 if (ret < 0)
1816 goto clk_disable;
1817 host->mclk = clk_get_rate(host->clk);
Linus Walleij64de0282010-02-19 01:09:10 +01001818 dev_dbg(mmc_dev(mmc), "eventual mclk rate: %u Hz\n",
1819 host->mclk);
Linus Walleijc8df9a52008-04-29 09:34:07 +01001820 }
Ulf Hanssonef289982014-03-17 13:56:32 +01001821
Russell Kingc8ebae32011-01-11 19:35:53 +00001822 host->phybase = dev->res.start;
Ulf Hanssonef289982014-03-17 13:56:32 +01001823 host->base = devm_ioremap_resource(&dev->dev, &dev->res);
1824 if (IS_ERR(host->base)) {
1825 ret = PTR_ERR(host->base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 goto clk_disable;
1827 }
1828
Ulf Hanssoned9067f2018-07-13 13:15:23 +02001829 if (variant->init)
1830 variant->init(host);
1831
Linus Walleij7f294e42011-07-08 09:57:15 +01001832 /*
1833 * The ARM and ST versions of the block have slightly different
1834 * clock divider equations which means that the minimum divider
1835 * differs too.
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001836 * on Qualcomm like controllers get the nearest minimum clock to 100Khz
Linus Walleij7f294e42011-07-08 09:57:15 +01001837 */
1838 if (variant->st_clkdiv)
1839 mmc->f_min = DIV_ROUND_UP(host->mclk, 257);
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001840 else if (variant->explicit_mclk_control)
1841 mmc->f_min = clk_round_rate(host->clk, 100000);
Linus Walleij7f294e42011-07-08 09:57:15 +01001842 else
1843 mmc->f_min = DIV_ROUND_UP(host->mclk, 512);
Linus Walleij808d97c2010-04-08 07:39:38 +01001844 /*
Ulf Hansson78f87df2014-03-17 15:53:07 +01001845 * If no maximum operating frequency is supplied, fall back to use
1846 * the module parameter, which has a (low) default value in case it
1847 * is not specified. Either value must not exceed the clock rate into
Ulf Hansson5080a082014-03-21 10:46:39 +01001848 * the block, of course.
Linus Walleij808d97c2010-04-08 07:39:38 +01001849 */
Ulf Hansson78f87df2014-03-17 15:53:07 +01001850 if (mmc->f_max)
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001851 mmc->f_max = variant->explicit_mclk_control ?
1852 min(variant->f_max, mmc->f_max) :
1853 min(host->mclk, mmc->f_max);
Linus Walleij808d97c2010-04-08 07:39:38 +01001854 else
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001855 mmc->f_max = variant->explicit_mclk_control ?
1856 fmax : min(host->mclk, fmax);
1857
1858
Linus Walleij64de0282010-02-19 01:09:10 +01001859 dev_dbg(mmc_dev(mmc), "clocking block at %u Hz\n", mmc->f_max);
1860
Ulf Hansson599c1d52013-01-07 16:22:50 +01001861 /* Get regulators and the supported OCR mask */
Bjorn Andersson9369c972015-03-24 18:39:49 -07001862 ret = mmc_regulator_get_supply(mmc);
Wolfram Sang51006952017-10-14 21:17:14 +02001863 if (ret)
Bjorn Andersson9369c972015-03-24 18:39:49 -07001864 goto clk_disable;
1865
Ulf Hansson599c1d52013-01-07 16:22:50 +01001866 if (!mmc->ocr_avail)
Linus Walleij34e84f32009-09-22 14:41:40 +01001867 mmc->ocr_avail = plat->ocr_mask;
Ulf Hansson599c1d52013-01-07 16:22:50 +01001868 else if (plat->ocr_mask)
1869 dev_warn(mmc_dev(mmc), "Platform OCR mask is ignored\n");
1870
Ulf Hansson9dd8a8b2014-03-19 13:54:18 +01001871 /* We support these capabilities. */
1872 mmc->caps |= MMC_CAP_CMD23;
1873
Linus Walleij49adc0c2016-10-25 11:06:06 +02001874 /*
1875 * Enable busy detection.
1876 */
Ulf Hansson8d94b542014-01-13 16:49:31 +01001877 if (variant->busy_detect) {
1878 mmci_ops.card_busy = mmci_card_busy;
Linus Walleij49adc0c2016-10-25 11:06:06 +02001879 /*
1880 * Not all variants have a flag to enable busy detection
1881 * in the DPSM, but if they do, set it here.
1882 */
1883 if (variant->busy_dpsm_flag)
1884 mmci_write_datactrlreg(host,
1885 host->variant->busy_dpsm_flag);
Ulf Hansson8d94b542014-01-13 16:49:31 +01001886 mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY;
1887 mmc->max_busy_timeout = 0;
1888 }
1889
1890 mmc->ops = &mmci_ops;
1891
Ulf Hansson70be2082013-01-07 15:35:06 +01001892 /* We support these PM capabilities. */
Ulf Hansson78f87df2014-03-17 15:53:07 +01001893 mmc->pm_caps |= MMC_PM_KEEP_POWER;
Ulf Hansson70be2082013-01-07 15:35:06 +01001894
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 /*
1896 * We can do SGIO
1897 */
Martin K. Petersena36274e2010-09-10 01:33:59 -04001898 mmc->max_segs = NR_SG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
1900 /*
Rabin Vincent08458ef2010-07-21 12:55:59 +01001901 * Since only a certain number of bits are valid in the data length
1902 * register, we must ensure that we don't exceed 2^num-1 bytes in a
1903 * single request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 */
Rabin Vincent08458ef2010-07-21 12:55:59 +01001905 mmc->max_req_size = (1 << variant->datalength_bits) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
1907 /*
1908 * Set the maximum segment size. Since we aren't doing DMA
1909 * (yet) we are only limited by the data length register.
1910 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001911 mmc->max_seg_size = mmc->max_req_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001913 /*
1914 * Block size can be up to 2048 bytes, but must be a power of two.
1915 */
Ludovic Barrec931d492018-10-08 14:08:43 +02001916 mmc->max_blk_size = 1 << variant->datactrl_blocksz;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001917
Pierre Ossman55db8902006-11-21 17:55:45 +01001918 /*
Will Deacon8f7f6b7e2012-02-24 11:25:21 +00001919 * Limit the number of blocks transferred so that we don't overflow
1920 * the maximum request size.
Pierre Ossman55db8902006-11-21 17:55:45 +01001921 */
Ludovic Barrec931d492018-10-08 14:08:43 +02001922 mmc->max_blk_count = mmc->max_req_size >> variant->datactrl_blocksz;
Pierre Ossman55db8902006-11-21 17:55:45 +01001923
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 spin_lock_init(&host->lock);
1925
1926 writel(0, host->base + MMCIMASK0);
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +01001927
1928 if (variant->mmcimask1)
1929 writel(0, host->base + MMCIMASK1);
1930
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 writel(0xfff, host->base + MMCICLEAR);
1932
Linus Walleijce437aa2014-08-27 15:13:54 +02001933 /*
1934 * If:
1935 * - not using DT but using a descriptor table, or
1936 * - using a table of descriptors ALONGSIDE DT, or
1937 * look up these descriptors named "cd" and "wp" right here, fail
Linus Walleij9ef986a2018-09-20 16:01:10 -07001938 * silently of these do not exist
Linus Walleijce437aa2014-08-27 15:13:54 +02001939 */
1940 if (!np) {
Linus Walleij89168b42014-10-02 09:08:46 +02001941 ret = mmc_gpiod_request_cd(mmc, "cd", 0, false, 0, NULL);
Linus Walleij9ef986a2018-09-20 16:01:10 -07001942 if (ret == -EPROBE_DEFER)
1943 goto clk_disable;
Linus Walleijce437aa2014-08-27 15:13:54 +02001944
Linus Walleij89168b42014-10-02 09:08:46 +02001945 ret = mmc_gpiod_request_ro(mmc, "wp", 0, false, 0, NULL);
Linus Walleij9ef986a2018-09-20 16:01:10 -07001946 if (ret == -EPROBE_DEFER)
1947 goto clk_disable;
Russell King89001442009-07-09 15:16:07 +01001948 }
1949
Ulf Hanssonef289982014-03-17 13:56:32 +01001950 ret = devm_request_irq(&dev->dev, dev->irq[0], mmci_irq, IRQF_SHARED,
1951 DRIVER_NAME " (cmd)", host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 if (ret)
Ulf Hanssonef289982014-03-17 13:56:32 +01001953 goto clk_disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
Russell Kingdfb85182012-05-03 11:33:15 +01001955 if (!dev->irq[1])
Linus Walleij2686b4b2010-10-19 12:39:48 +01001956 host->singleirq = true;
1957 else {
Ulf Hanssonef289982014-03-17 13:56:32 +01001958 ret = devm_request_irq(&dev->dev, dev->irq[1], mmci_pio_irq,
1959 IRQF_SHARED, DRIVER_NAME " (pio)", host);
Linus Walleij2686b4b2010-10-19 12:39:48 +01001960 if (ret)
Ulf Hanssonef289982014-03-17 13:56:32 +01001961 goto clk_disable;
Linus Walleij2686b4b2010-10-19 12:39:48 +01001962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Ludovic Barredaf97132018-10-08 14:08:44 +02001964 writel(MCI_IRQENABLE | variant->start_err, host->base + MMCIMASK0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
1966 amba_set_drvdata(dev, mmc);
1967
Russell Kingc8ebae32011-01-11 19:35:53 +00001968 dev_info(&dev->dev, "%s: PL%03x manf %x rev%u at 0x%08llx irq %d,%d (pio)\n",
1969 mmc_hostname(mmc), amba_part(dev), amba_manf(dev),
1970 amba_rev(dev), (unsigned long long)dev->res.start,
1971 dev->irq[0], dev->irq[1]);
1972
1973 mmci_dma_setup(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
Ulf Hansson2cd976c2011-12-13 17:01:11 +01001975 pm_runtime_set_autosuspend_delay(&dev->dev, 50);
1976 pm_runtime_use_autosuspend(&dev->dev);
Russell King1c3be362011-08-14 09:17:05 +01001977
Russell King8c11a942010-12-28 19:40:40 +00001978 mmc_add_host(mmc);
1979
Ulf Hansson6f2d3c82014-12-11 14:35:55 +01001980 pm_runtime_put(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 return 0;
1982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 clk_disable:
Julia Lawallac940932012-08-26 16:00:59 +00001984 clk_disable_unprepare(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 host_free:
1986 mmc_free_host(mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 return ret;
1988}
1989
Bill Pemberton6e0ee712012-11-19 13:26:03 -05001990static int mmci_remove(struct amba_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991{
1992 struct mmc_host *mmc = amba_get_drvdata(dev);
1993
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 if (mmc) {
1995 struct mmci_host *host = mmc_priv(mmc);
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +01001996 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
Russell King1c3be362011-08-14 09:17:05 +01001998 /*
1999 * Undo pm_runtime_put() in probe. We use the _sync
2000 * version here so that we can access the primecell.
2001 */
2002 pm_runtime_get_sync(&dev->dev);
2003
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 mmc_remove_host(mmc);
2005
2006 writel(0, host->base + MMCIMASK0);
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +01002007
2008 if (variant->mmcimask1)
2009 writel(0, host->base + MMCIMASK1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
2011 writel(0, host->base + MMCICOMMAND);
2012 writel(0, host->base + MMCIDATACTRL);
2013
Russell Kingc8ebae32011-01-11 19:35:53 +00002014 mmci_dma_release(host);
Julia Lawallac940932012-08-26 16:00:59 +00002015 clk_disable_unprepare(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 mmc_free_host(mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 }
2018
2019 return 0;
2020}
2021
Ulf Hansson571dce42014-01-23 00:38:00 +01002022#ifdef CONFIG_PM
Ulf Hansson1ff44432013-09-04 09:05:17 +01002023static void mmci_save(struct mmci_host *host)
2024{
2025 unsigned long flags;
2026
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01002027 spin_lock_irqsave(&host->lock, flags);
Ulf Hansson1ff44432013-09-04 09:05:17 +01002028
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01002029 writel(0, host->base + MMCIMASK0);
2030 if (host->variant->pwrreg_nopower) {
Ulf Hansson1ff44432013-09-04 09:05:17 +01002031 writel(0, host->base + MMCIDATACTRL);
2032 writel(0, host->base + MMCIPOWER);
2033 writel(0, host->base + MMCICLOCK);
Ulf Hansson1ff44432013-09-04 09:05:17 +01002034 }
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01002035 mmci_reg_delay(host);
Ulf Hansson1ff44432013-09-04 09:05:17 +01002036
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01002037 spin_unlock_irqrestore(&host->lock, flags);
Ulf Hansson1ff44432013-09-04 09:05:17 +01002038}
2039
2040static void mmci_restore(struct mmci_host *host)
2041{
2042 unsigned long flags;
2043
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01002044 spin_lock_irqsave(&host->lock, flags);
Ulf Hansson1ff44432013-09-04 09:05:17 +01002045
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01002046 if (host->variant->pwrreg_nopower) {
Ulf Hansson1ff44432013-09-04 09:05:17 +01002047 writel(host->clk_reg, host->base + MMCICLOCK);
2048 writel(host->datactrl_reg, host->base + MMCIDATACTRL);
2049 writel(host->pwr_reg, host->base + MMCIPOWER);
Ulf Hansson1ff44432013-09-04 09:05:17 +01002050 }
Ludovic Barredaf97132018-10-08 14:08:44 +02002051 writel(MCI_IRQENABLE | host->variant->start_err,
2052 host->base + MMCIMASK0);
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01002053 mmci_reg_delay(host);
2054
2055 spin_unlock_irqrestore(&host->lock, flags);
Ulf Hansson1ff44432013-09-04 09:05:17 +01002056}
2057
Ulf Hansson82592932013-01-09 11:15:26 +01002058static int mmci_runtime_suspend(struct device *dev)
2059{
2060 struct amba_device *adev = to_amba_device(dev);
2061 struct mmc_host *mmc = amba_get_drvdata(adev);
2062
2063 if (mmc) {
2064 struct mmci_host *host = mmc_priv(mmc);
Ulf Hanssone36bd9c62013-09-04 09:00:37 +01002065 pinctrl_pm_select_sleep_state(dev);
Ulf Hansson1ff44432013-09-04 09:05:17 +01002066 mmci_save(host);
Ulf Hansson82592932013-01-09 11:15:26 +01002067 clk_disable_unprepare(host->clk);
2068 }
2069
2070 return 0;
2071}
2072
2073static int mmci_runtime_resume(struct device *dev)
2074{
2075 struct amba_device *adev = to_amba_device(dev);
2076 struct mmc_host *mmc = amba_get_drvdata(adev);
2077
2078 if (mmc) {
2079 struct mmci_host *host = mmc_priv(mmc);
2080 clk_prepare_enable(host->clk);
Ulf Hansson1ff44432013-09-04 09:05:17 +01002081 mmci_restore(host);
Ulf Hanssone36bd9c62013-09-04 09:00:37 +01002082 pinctrl_pm_select_default_state(dev);
Ulf Hansson82592932013-01-09 11:15:26 +01002083 }
2084
2085 return 0;
2086}
2087#endif
2088
Ulf Hansson48fa7002011-12-13 16:59:34 +01002089static const struct dev_pm_ops mmci_dev_pm_ops = {
Ulf Hanssonf3737fa2014-01-23 01:11:33 +01002090 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
2091 pm_runtime_force_resume)
Rafael J. Wysocki6ed23b82014-12-04 00:34:11 +01002092 SET_RUNTIME_PM_OPS(mmci_runtime_suspend, mmci_runtime_resume, NULL)
Ulf Hansson48fa7002011-12-13 16:59:34 +01002093};
2094
Arvind Yadav88411de2017-08-23 22:00:49 +05302095static const struct amba_id mmci_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 {
2097 .id = 0x00041180,
Pawel Moll768fbc12011-03-11 17:18:07 +00002098 .mask = 0xff0fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01002099 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 },
2101 {
Pawel Moll768fbc12011-03-11 17:18:07 +00002102 .id = 0x01041180,
2103 .mask = 0xff0fffff,
2104 .data = &variant_arm_extended_fifo,
2105 },
2106 {
Pawel Moll3a372982013-01-24 14:12:45 +01002107 .id = 0x02041180,
2108 .mask = 0xff0fffff,
2109 .data = &variant_arm_extended_fifo_hwfc,
2110 },
2111 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 .id = 0x00041181,
2113 .mask = 0x000fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01002114 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 },
Linus Walleijcc30d602009-01-04 15:18:54 +01002116 /* ST Micro variants */
2117 {
2118 .id = 0x00180180,
2119 .mask = 0x00ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01002120 .data = &variant_u300,
Linus Walleijcc30d602009-01-04 15:18:54 +01002121 },
2122 {
Linus Walleij34fd4212012-04-10 17:43:59 +01002123 .id = 0x10180180,
2124 .mask = 0xf0ffffff,
2125 .data = &variant_nomadik,
2126 },
2127 {
Linus Walleijcc30d602009-01-04 15:18:54 +01002128 .id = 0x00280180,
2129 .mask = 0x00ffffff,
Linus Walleij0bcb7ef2016-01-04 02:21:55 +01002130 .data = &variant_nomadik,
Rabin Vincent4956e102010-07-21 12:54:40 +01002131 },
2132 {
2133 .id = 0x00480180,
Philippe Langlais1784b152011-03-25 08:51:52 +01002134 .mask = 0xf0ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01002135 .data = &variant_ux500,
Linus Walleijcc30d602009-01-04 15:18:54 +01002136 },
Philippe Langlais1784b152011-03-25 08:51:52 +01002137 {
2138 .id = 0x10480180,
2139 .mask = 0xf0ffffff,
2140 .data = &variant_ux500v2,
2141 },
Patrice Chotard2a9d6c82018-01-18 15:34:21 +01002142 {
2143 .id = 0x00880180,
2144 .mask = 0x00ffffff,
2145 .data = &variant_stm32,
2146 },
Srinivas Kandagatla55b604a2014-06-02 10:10:13 +01002147 /* Qualcomm variants */
2148 {
2149 .id = 0x00051180,
2150 .mask = 0x000fffff,
2151 .data = &variant_qcom,
2152 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 { 0, 0 },
2154};
2155
Dave Martin9f998352011-10-05 15:15:21 +01002156MODULE_DEVICE_TABLE(amba, mmci_ids);
2157
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158static struct amba_driver mmci_driver = {
2159 .drv = {
2160 .name = DRIVER_NAME,
Ulf Hansson48fa7002011-12-13 16:59:34 +01002161 .pm = &mmci_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 },
2163 .probe = mmci_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05002164 .remove = mmci_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 .id_table = mmci_ids,
2166};
2167
viresh kumar9e5ed092012-03-15 10:40:38 +01002168module_amba_driver(mmci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170module_param(fmax, uint, 0444);
2171
2172MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
2173MODULE_LICENSE("GPL");