blob: ad2f62fc0af810920e1ced8cf804e015663bfda0 [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>
Ludovic Barre15878e52018-10-08 14:08:51 +020039#include <linux/reset.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Russell King7b09cda2005-07-01 12:02:59 +010041#include <asm/div64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#include "mmci.h"
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +010045#include "mmci_qcom_dml.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#define DRIVER_NAME "mmci-pl18x"
48
Ludovic Barrec3647fd2018-10-08 14:08:33 +020049#ifdef CONFIG_DMA_ENGINE
50void mmci_variant_init(struct mmci_host *host);
51#else
52static inline void mmci_variant_init(struct mmci_host *host) {}
53#endif
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055static unsigned int fmax = 515633;
56
Rabin Vincent4956e102010-07-21 12:54:40 +010057static struct variant_data variant_arm = {
Rabin Vincent8301bb62010-08-09 12:57:30 +010058 .fifosize = 16 * 4,
59 .fifohalfsize = 8 * 4,
Ludovic Barre0f244802018-10-08 14:08:45 +020060 .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
61 .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
62 .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
63 .cmdreg_srsp = MCI_CPSM_RESPONSE,
Rabin Vincent08458ef2010-07-21 12:55:59 +010064 .datalength_bits = 16,
Ludovic Barrec931d492018-10-08 14:08:43 +020065 .datactrl_blocksz = 11,
Ludovic Barre9b279942018-10-08 14:08:46 +020066 .datactrl_dpsm_enable = MCI_DPSM_ENABLE,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +010067 .pwrreg_powerup = MCI_PWR_UP,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +010068 .f_max = 100000000,
Ulf Hansson78782892014-06-13 13:21:38 +020069 .reversed_irq_handling = true,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +010070 .mmcimask1 = true,
Ludovic Barre59db5e22018-10-08 14:08:47 +020071 .irq_pio_mask = MCI_IRQ_PIO_MASK,
Patrice Chotard7f7b5502018-01-18 15:34:18 +010072 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +010073 .opendrain = MCI_ROD,
Ludovic Barrec3647fd2018-10-08 14:08:33 +020074 .init = mmci_variant_init,
Rabin Vincent4956e102010-07-21 12:54:40 +010075};
76
Pawel Moll768fbc12011-03-11 17:18:07 +000077static struct variant_data variant_arm_extended_fifo = {
78 .fifosize = 128 * 4,
79 .fifohalfsize = 64 * 4,
Ludovic Barre0f244802018-10-08 14:08:45 +020080 .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
81 .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
82 .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
83 .cmdreg_srsp = MCI_CPSM_RESPONSE,
Pawel Moll768fbc12011-03-11 17:18:07 +000084 .datalength_bits = 16,
Ludovic Barrec931d492018-10-08 14:08:43 +020085 .datactrl_blocksz = 11,
Ludovic Barre9b279942018-10-08 14:08:46 +020086 .datactrl_dpsm_enable = MCI_DPSM_ENABLE,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +010087 .pwrreg_powerup = MCI_PWR_UP,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +010088 .f_max = 100000000,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +010089 .mmcimask1 = true,
Ludovic Barre59db5e22018-10-08 14:08:47 +020090 .irq_pio_mask = MCI_IRQ_PIO_MASK,
Patrice Chotard7f7b5502018-01-18 15:34:18 +010091 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +010092 .opendrain = MCI_ROD,
Ludovic Barrec3647fd2018-10-08 14:08:33 +020093 .init = mmci_variant_init,
Pawel Moll768fbc12011-03-11 17:18:07 +000094};
95
Pawel Moll3a372982013-01-24 14:12:45 +010096static struct variant_data variant_arm_extended_fifo_hwfc = {
97 .fifosize = 128 * 4,
98 .fifohalfsize = 64 * 4,
99 .clkreg_enable = MCI_ARM_HWFCEN,
Ludovic Barre0f244802018-10-08 14:08:45 +0200100 .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
101 .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
102 .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
103 .cmdreg_srsp = MCI_CPSM_RESPONSE,
Pawel Moll3a372982013-01-24 14:12:45 +0100104 .datalength_bits = 16,
Ludovic Barrec931d492018-10-08 14:08:43 +0200105 .datactrl_blocksz = 11,
Ludovic Barre9b279942018-10-08 14:08:46 +0200106 .datactrl_dpsm_enable = MCI_DPSM_ENABLE,
Pawel Moll3a372982013-01-24 14:12:45 +0100107 .pwrreg_powerup = MCI_PWR_UP,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +0100108 .f_max = 100000000,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100109 .mmcimask1 = true,
Ludovic Barre59db5e22018-10-08 14:08:47 +0200110 .irq_pio_mask = MCI_IRQ_PIO_MASK,
Patrice Chotard7f7b5502018-01-18 15:34:18 +0100111 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +0100112 .opendrain = MCI_ROD,
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200113 .init = mmci_variant_init,
Pawel Moll3a372982013-01-24 14:12:45 +0100114};
115
Rabin Vincent4956e102010-07-21 12:54:40 +0100116static struct variant_data variant_u300 = {
Rabin Vincent8301bb62010-08-09 12:57:30 +0100117 .fifosize = 16 * 4,
118 .fifohalfsize = 8 * 4,
Linus Walleij49ac2152011-03-04 14:54:16 +0100119 .clkreg_enable = MCI_ST_U300_HWFCEN,
Srinivas Kandagatlae1412d82014-06-02 10:09:23 +0100120 .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
Ludovic Barre0f244802018-10-08 14:08:45 +0200121 .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
122 .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
123 .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
124 .cmdreg_srsp = MCI_CPSM_RESPONSE,
Rabin Vincent08458ef2010-07-21 12:55:59 +0100125 .datalength_bits = 16,
Ludovic Barrec931d492018-10-08 14:08:43 +0200126 .datactrl_blocksz = 11,
Ludovic Barre9b279942018-10-08 14:08:46 +0200127 .datactrl_dpsm_enable = MCI_DPSM_ENABLE,
Linus Walleij5db3eee2016-10-25 11:06:05 +0200128 .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
Srinivas Kandagatlac7354132014-08-22 05:55:16 +0100129 .st_sdio = true,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +0100130 .pwrreg_powerup = MCI_PWR_ON,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +0100131 .f_max = 100000000,
Ulf Hansson4d1a3a02011-12-13 16:57:07 +0100132 .signal_direction = true,
Ulf Hanssonf4670da2013-01-09 17:19:54 +0100133 .pwrreg_clkgate = true,
Ulf Hansson1ff44432013-09-04 09:05:17 +0100134 .pwrreg_nopower = true,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100135 .mmcimask1 = true,
Ludovic Barre59db5e22018-10-08 14:08:47 +0200136 .irq_pio_mask = MCI_IRQ_PIO_MASK,
Patrice Chotard7f7b5502018-01-18 15:34:18 +0100137 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +0100138 .opendrain = MCI_OD,
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200139 .init = mmci_variant_init,
Rabin Vincent4956e102010-07-21 12:54:40 +0100140};
141
Linus Walleij34fd4212012-04-10 17:43:59 +0100142static struct variant_data variant_nomadik = {
143 .fifosize = 16 * 4,
144 .fifohalfsize = 8 * 4,
145 .clkreg = MCI_CLK_ENABLE,
Linus Walleijf5abc762016-01-04 02:22:08 +0100146 .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
Ludovic Barre0f244802018-10-08 14:08:45 +0200147 .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
148 .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
149 .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
150 .cmdreg_srsp = MCI_CPSM_RESPONSE,
Linus Walleij34fd4212012-04-10 17:43:59 +0100151 .datalength_bits = 24,
Ludovic Barrec931d492018-10-08 14:08:43 +0200152 .datactrl_blocksz = 11,
Ludovic Barre9b279942018-10-08 14:08:46 +0200153 .datactrl_dpsm_enable = MCI_DPSM_ENABLE,
Linus Walleij5db3eee2016-10-25 11:06:05 +0200154 .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
Srinivas Kandagatlac7354132014-08-22 05:55:16 +0100155 .st_sdio = true,
Linus Walleij34fd4212012-04-10 17:43:59 +0100156 .st_clkdiv = true,
157 .pwrreg_powerup = MCI_PWR_ON,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +0100158 .f_max = 100000000,
Linus Walleij34fd4212012-04-10 17:43:59 +0100159 .signal_direction = true,
Ulf Hanssonf4670da2013-01-09 17:19:54 +0100160 .pwrreg_clkgate = true,
Ulf Hansson1ff44432013-09-04 09:05:17 +0100161 .pwrreg_nopower = true,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100162 .mmcimask1 = true,
Ludovic Barre59db5e22018-10-08 14:08:47 +0200163 .irq_pio_mask = MCI_IRQ_PIO_MASK,
Patrice Chotard7f7b5502018-01-18 15:34:18 +0100164 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +0100165 .opendrain = MCI_OD,
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200166 .init = mmci_variant_init,
Linus Walleij34fd4212012-04-10 17:43:59 +0100167};
168
Rabin Vincent4956e102010-07-21 12:54:40 +0100169static struct variant_data variant_ux500 = {
Rabin Vincent8301bb62010-08-09 12:57:30 +0100170 .fifosize = 30 * 4,
171 .fifohalfsize = 8 * 4,
Rabin Vincent4956e102010-07-21 12:54:40 +0100172 .clkreg = MCI_CLK_ENABLE,
Linus Walleij49ac2152011-03-04 14:54:16 +0100173 .clkreg_enable = MCI_ST_UX500_HWFCEN,
Srinivas Kandagatlae1412d82014-06-02 10:09:23 +0100174 .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
Srinivas Kandagatlae8740642014-06-02 10:09:30 +0100175 .clkreg_neg_edge_enable = MCI_ST_UX500_NEG_EDGE,
Ludovic Barre0f244802018-10-08 14:08:45 +0200176 .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
177 .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
178 .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
179 .cmdreg_srsp = MCI_CPSM_RESPONSE,
Rabin Vincent08458ef2010-07-21 12:55:59 +0100180 .datalength_bits = 24,
Ludovic Barrec931d492018-10-08 14:08:43 +0200181 .datactrl_blocksz = 11,
Ludovic Barre9b279942018-10-08 14:08:46 +0200182 .datactrl_dpsm_enable = MCI_DPSM_ENABLE,
Linus Walleij5db3eee2016-10-25 11:06:05 +0200183 .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
Srinivas Kandagatlac7354132014-08-22 05:55:16 +0100184 .st_sdio = true,
Linus Walleijb70a67f2010-12-06 09:24:14 +0100185 .st_clkdiv = true,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +0100186 .pwrreg_powerup = MCI_PWR_ON,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +0100187 .f_max = 100000000,
Ulf Hansson4d1a3a02011-12-13 16:57:07 +0100188 .signal_direction = true,
Ulf Hanssonf4670da2013-01-09 17:19:54 +0100189 .pwrreg_clkgate = true,
Ulf Hansson01259622013-05-15 20:53:22 +0100190 .busy_detect = true,
Linus Walleij49adc0c2016-10-25 11:06:06 +0200191 .busy_dpsm_flag = MCI_DPSM_ST_BUSYMODE,
192 .busy_detect_flag = MCI_ST_CARDBUSY,
193 .busy_detect_mask = MCI_ST_BUSYENDMASK,
Ulf Hansson1ff44432013-09-04 09:05:17 +0100194 .pwrreg_nopower = true,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100195 .mmcimask1 = true,
Ludovic Barre59db5e22018-10-08 14:08:47 +0200196 .irq_pio_mask = MCI_IRQ_PIO_MASK,
Patrice Chotard7f7b5502018-01-18 15:34:18 +0100197 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +0100198 .opendrain = MCI_OD,
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200199 .init = mmci_variant_init,
Rabin Vincent4956e102010-07-21 12:54:40 +0100200};
Linus Walleijb70a67f2010-12-06 09:24:14 +0100201
Philippe Langlais1784b152011-03-25 08:51:52 +0100202static struct variant_data variant_ux500v2 = {
203 .fifosize = 30 * 4,
204 .fifohalfsize = 8 * 4,
205 .clkreg = MCI_CLK_ENABLE,
206 .clkreg_enable = MCI_ST_UX500_HWFCEN,
Srinivas Kandagatlae1412d82014-06-02 10:09:23 +0100207 .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
Srinivas Kandagatlae8740642014-06-02 10:09:30 +0100208 .clkreg_neg_edge_enable = MCI_ST_UX500_NEG_EDGE,
Ludovic Barre0f244802018-10-08 14:08:45 +0200209 .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
210 .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
211 .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
212 .cmdreg_srsp = MCI_CPSM_RESPONSE,
Linus Walleij5db3eee2016-10-25 11:06:05 +0200213 .datactrl_mask_ddrmode = MCI_DPSM_ST_DDRMODE,
Philippe Langlais1784b152011-03-25 08:51:52 +0100214 .datalength_bits = 24,
Ludovic Barrec931d492018-10-08 14:08:43 +0200215 .datactrl_blocksz = 11,
Ludovic Barre9b279942018-10-08 14:08:46 +0200216 .datactrl_dpsm_enable = MCI_DPSM_ENABLE,
Linus Walleij5db3eee2016-10-25 11:06:05 +0200217 .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
Srinivas Kandagatlac7354132014-08-22 05:55:16 +0100218 .st_sdio = true,
Philippe Langlais1784b152011-03-25 08:51:52 +0100219 .st_clkdiv = true,
220 .blksz_datactrl16 = true,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +0100221 .pwrreg_powerup = MCI_PWR_ON,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +0100222 .f_max = 100000000,
Ulf Hansson4d1a3a02011-12-13 16:57:07 +0100223 .signal_direction = true,
Ulf Hanssonf4670da2013-01-09 17:19:54 +0100224 .pwrreg_clkgate = true,
Ulf Hansson01259622013-05-15 20:53:22 +0100225 .busy_detect = true,
Linus Walleij49adc0c2016-10-25 11:06:06 +0200226 .busy_dpsm_flag = MCI_DPSM_ST_BUSYMODE,
227 .busy_detect_flag = MCI_ST_CARDBUSY,
228 .busy_detect_mask = MCI_ST_BUSYENDMASK,
Ulf Hansson1ff44432013-09-04 09:05:17 +0100229 .pwrreg_nopower = true,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100230 .mmcimask1 = true,
Ludovic Barre59db5e22018-10-08 14:08:47 +0200231 .irq_pio_mask = MCI_IRQ_PIO_MASK,
Patrice Chotard7f7b5502018-01-18 15:34:18 +0100232 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +0100233 .opendrain = MCI_OD,
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200234 .init = mmci_variant_init,
Philippe Langlais1784b152011-03-25 08:51:52 +0100235};
236
Patrice Chotard2a9d6c82018-01-18 15:34:21 +0100237static struct variant_data variant_stm32 = {
238 .fifosize = 32 * 4,
239 .fifohalfsize = 8 * 4,
240 .clkreg = MCI_CLK_ENABLE,
241 .clkreg_enable = MCI_ST_UX500_HWFCEN,
242 .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
243 .clkreg_neg_edge_enable = MCI_ST_UX500_NEG_EDGE,
Ludovic Barre0f244802018-10-08 14:08:45 +0200244 .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
245 .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
246 .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
247 .cmdreg_srsp = MCI_CPSM_RESPONSE,
Ludovic Barre59db5e22018-10-08 14:08:47 +0200248 .irq_pio_mask = MCI_IRQ_PIO_MASK,
Patrice Chotard2a9d6c82018-01-18 15:34:21 +0100249 .datalength_bits = 24,
Ludovic Barrec931d492018-10-08 14:08:43 +0200250 .datactrl_blocksz = 11,
Ludovic Barre9b279942018-10-08 14:08:46 +0200251 .datactrl_dpsm_enable = MCI_DPSM_ENABLE,
Patrice Chotard2a9d6c82018-01-18 15:34:21 +0100252 .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
253 .st_sdio = true,
254 .st_clkdiv = true,
255 .pwrreg_powerup = MCI_PWR_ON,
256 .f_max = 48000000,
257 .pwrreg_clkgate = true,
258 .pwrreg_nopower = true,
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200259 .init = mmci_variant_init,
Patrice Chotard2a9d6c82018-01-18 15:34:21 +0100260};
261
Srinivas Kandagatla55b604a2014-06-02 10:10:13 +0100262static struct variant_data variant_qcom = {
263 .fifosize = 16 * 4,
264 .fifohalfsize = 8 * 4,
265 .clkreg = MCI_CLK_ENABLE,
266 .clkreg_enable = MCI_QCOM_CLK_FLOWENA |
267 MCI_QCOM_CLK_SELECT_IN_FBCLK,
268 .clkreg_8bit_bus_enable = MCI_QCOM_CLK_WIDEBUS_8,
269 .datactrl_mask_ddrmode = MCI_QCOM_CLK_SELECT_IN_DDR_MODE,
Ludovic Barre0f244802018-10-08 14:08:45 +0200270 .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
271 .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
272 .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
273 .cmdreg_srsp = MCI_CPSM_RESPONSE,
Linus Walleij5db3eee2016-10-25 11:06:05 +0200274 .data_cmd_enable = MCI_CPSM_QCOM_DATCMD,
Srinivas Kandagatla55b604a2014-06-02 10:10:13 +0100275 .blksz_datactrl4 = true,
276 .datalength_bits = 24,
Ludovic Barrec931d492018-10-08 14:08:43 +0200277 .datactrl_blocksz = 11,
Ludovic Barre9b279942018-10-08 14:08:46 +0200278 .datactrl_dpsm_enable = MCI_DPSM_ENABLE,
Srinivas Kandagatla55b604a2014-06-02 10:10:13 +0100279 .pwrreg_powerup = MCI_PWR_UP,
280 .f_max = 208000000,
281 .explicit_mclk_control = true,
282 .qcom_fifo = true,
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100283 .qcom_dml = true,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100284 .mmcimask1 = true,
Ludovic Barre59db5e22018-10-08 14:08:47 +0200285 .irq_pio_mask = MCI_IRQ_PIO_MASK,
Patrice Chotard7f7b5502018-01-18 15:34:18 +0100286 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +0100287 .opendrain = MCI_ROD,
Ulf Hansson29aba072018-07-16 13:08:18 +0200288 .init = qcom_variant_init,
Srinivas Kandagatla55b604a2014-06-02 10:10:13 +0100289};
290
Linus Walleij49adc0c2016-10-25 11:06:06 +0200291/* Busy detection for the ST Micro variant */
Ulf Hansson01259622013-05-15 20:53:22 +0100292static int mmci_card_busy(struct mmc_host *mmc)
293{
294 struct mmci_host *host = mmc_priv(mmc);
295 unsigned long flags;
296 int busy = 0;
297
Ulf Hansson01259622013-05-15 20:53:22 +0100298 spin_lock_irqsave(&host->lock, flags);
Linus Walleij49adc0c2016-10-25 11:06:06 +0200299 if (readl(host->base + MMCISTATUS) & host->variant->busy_detect_flag)
Ulf Hansson01259622013-05-15 20:53:22 +0100300 busy = 1;
301 spin_unlock_irqrestore(&host->lock, flags);
302
Ulf Hansson01259622013-05-15 20:53:22 +0100303 return busy;
304}
305
Ulf Hanssonf829c042013-09-04 09:01:15 +0100306static void mmci_reg_delay(struct mmci_host *host)
307{
308 /*
309 * According to the spec, at least three feedback clock cycles
310 * of max 52 MHz must pass between two writes to the MMCICLOCK reg.
311 * Three MCLK clock cycles must pass between two MMCIPOWER reg writes.
312 * Worst delay time during card init is at 100 kHz => 30 us.
313 * Worst delay time when up and running is at 25 MHz => 120 ns.
314 */
315 if (host->cclk < 25000000)
316 udelay(30);
317 else
318 ndelay(120);
319}
320
Ulf Hansson653a7612013-01-21 21:29:34 +0100321/*
Linus Walleija6a64642009-09-14 12:56:14 +0100322 * This must be called with host->lock held
323 */
Ludovic Barrecd3ee8c2018-10-08 14:08:42 +0200324void mmci_write_clkreg(struct mmci_host *host, u32 clk)
Ulf Hansson7437cfa2012-01-18 09:17:27 +0100325{
326 if (host->clk_reg != clk) {
327 host->clk_reg = clk;
328 writel(clk, host->base + MMCICLOCK);
329 }
330}
331
332/*
333 * This must be called with host->lock held
334 */
Ludovic Barrecd3ee8c2018-10-08 14:08:42 +0200335void mmci_write_pwrreg(struct mmci_host *host, u32 pwr)
Ulf Hansson7437cfa2012-01-18 09:17:27 +0100336{
337 if (host->pwr_reg != pwr) {
338 host->pwr_reg = pwr;
339 writel(pwr, host->base + MMCIPOWER);
340 }
341}
342
343/*
344 * This must be called with host->lock held
345 */
Ulf Hansson9cc639a2013-05-15 20:48:23 +0100346static void mmci_write_datactrlreg(struct mmci_host *host, u32 datactrl)
347{
Linus Walleij49adc0c2016-10-25 11:06:06 +0200348 /* Keep busy mode in DPSM if enabled */
349 datactrl |= host->datactrl_reg & host->variant->busy_dpsm_flag;
Ulf Hansson01259622013-05-15 20:53:22 +0100350
Ulf Hansson9cc639a2013-05-15 20:48:23 +0100351 if (host->datactrl_reg != datactrl) {
352 host->datactrl_reg = datactrl;
353 writel(datactrl, host->base + MMCIDATACTRL);
354 }
355}
356
357/*
358 * This must be called with host->lock held
359 */
Linus Walleija6a64642009-09-14 12:56:14 +0100360static void mmci_set_clkreg(struct mmci_host *host, unsigned int desired)
361{
Rabin Vincent4956e102010-07-21 12:54:40 +0100362 struct variant_data *variant = host->variant;
363 u32 clk = variant->clkreg;
Linus Walleija6a64642009-09-14 12:56:14 +0100364
Ulf Hanssonc58a8502013-05-13 15:40:03 +0100365 /* Make sure cclk reflects the current calculated clock */
366 host->cclk = 0;
367
Linus Walleija6a64642009-09-14 12:56:14 +0100368 if (desired) {
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +0100369 if (variant->explicit_mclk_control) {
370 host->cclk = host->mclk;
371 } else if (desired >= host->mclk) {
Linus Walleij991a86e2010-12-10 09:35:53 +0100372 clk = MCI_CLK_BYPASS;
Linus Walleij399bc482011-04-01 07:59:17 +0100373 if (variant->st_clkdiv)
374 clk |= MCI_ST_UX500_NEG_EDGE;
Linus Walleija6a64642009-09-14 12:56:14 +0100375 host->cclk = host->mclk;
Linus Walleijb70a67f2010-12-06 09:24:14 +0100376 } else if (variant->st_clkdiv) {
377 /*
378 * DB8500 TRM says f = mclk / (clkdiv + 2)
379 * => clkdiv = (mclk / f) - 2
380 * Round the divider up so we don't exceed the max
381 * frequency
382 */
383 clk = DIV_ROUND_UP(host->mclk, desired) - 2;
384 if (clk >= 256)
385 clk = 255;
386 host->cclk = host->mclk / (clk + 2);
Linus Walleija6a64642009-09-14 12:56:14 +0100387 } else {
Linus Walleijb70a67f2010-12-06 09:24:14 +0100388 /*
389 * PL180 TRM says f = mclk / (2 * (clkdiv + 1))
390 * => clkdiv = mclk / (2 * f) - 1
391 */
Linus Walleija6a64642009-09-14 12:56:14 +0100392 clk = host->mclk / (2 * desired) - 1;
393 if (clk >= 256)
394 clk = 255;
395 host->cclk = host->mclk / (2 * (clk + 1));
396 }
Rabin Vincent4380c142010-07-21 12:55:18 +0100397
398 clk |= variant->clkreg_enable;
Linus Walleija6a64642009-09-14 12:56:14 +0100399 clk |= MCI_CLK_ENABLE;
400 /* This hasn't proven to be worthwhile */
401 /* clk |= MCI_CLK_PWRSAVE; */
402 }
403
Ulf Hanssonc58a8502013-05-13 15:40:03 +0100404 /* Set actual clock for debug */
405 host->mmc->actual_clock = host->cclk;
406
Linus Walleij9e6c82c2009-09-14 12:57:11 +0100407 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4)
Linus Walleij771dc152010-04-08 07:38:52 +0100408 clk |= MCI_4BIT_BUS;
409 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_8)
Srinivas Kandagatlae1412d82014-06-02 10:09:23 +0100410 clk |= variant->clkreg_8bit_bus_enable;
Linus Walleij9e6c82c2009-09-14 12:57:11 +0100411
Seungwon Jeon6dad6c92014-03-14 21:12:13 +0900412 if (host->mmc->ios.timing == MMC_TIMING_UHS_DDR50 ||
413 host->mmc->ios.timing == MMC_TIMING_MMC_DDR52)
Srinivas Kandagatlae8740642014-06-02 10:09:30 +0100414 clk |= variant->clkreg_neg_edge_enable;
Ulf Hansson6dbb6ee2013-01-07 15:30:44 +0100415
Ulf Hansson7437cfa2012-01-18 09:17:27 +0100416 mmci_write_clkreg(host, clk);
Linus Walleija6a64642009-09-14 12:56:14 +0100417}
418
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200419void mmci_dma_release(struct mmci_host *host)
420{
421 if (host->ops && host->ops->dma_release)
422 host->ops->dma_release(host);
423
424 host->use_dma = false;
425}
426
427void mmci_dma_setup(struct mmci_host *host)
428{
429 if (!host->ops || !host->ops->dma_setup)
430 return;
431
432 if (host->ops->dma_setup(host))
433 return;
434
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200435 /* initialize pre request cookie */
436 host->next_cookie = 1;
437
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200438 host->use_dma = true;
439}
440
Ludovic Barree0da1722018-10-08 14:08:41 +0200441/*
442 * Validate mmc prerequisites
443 */
444static int mmci_validate_data(struct mmci_host *host,
445 struct mmc_data *data)
446{
447 if (!data)
448 return 0;
449
450 if (!is_power_of_2(data->blksz)) {
451 dev_err(mmc_dev(host->mmc),
452 "unsupported block size (%d bytes)\n", data->blksz);
453 return -EINVAL;
454 }
455
456 if (host->ops && host->ops->validate_data)
457 return host->ops->validate_data(host, data);
458
459 return 0;
460}
461
Ludovic Barre47983512018-10-08 14:08:36 +0200462int mmci_prep_data(struct mmci_host *host, struct mmc_data *data, bool next)
463{
464 int err;
465
466 if (!host->ops || !host->ops->prep_data)
467 return 0;
468
469 err = host->ops->prep_data(host, data, next);
470
471 if (next && !err)
472 data->host_cookie = ++host->next_cookie < 0 ?
473 1 : host->next_cookie;
474
475 return err;
476}
477
478void mmci_unprep_data(struct mmci_host *host, struct mmc_data *data,
479 int err)
480{
481 if (host->ops && host->ops->unprep_data)
482 host->ops->unprep_data(host, data, err);
483
484 data->host_cookie = 0;
485}
486
Ludovic Barre02769962018-10-08 14:08:37 +0200487void mmci_get_next_data(struct mmci_host *host, struct mmc_data *data)
488{
489 WARN_ON(data->host_cookie && data->host_cookie != host->next_cookie);
490
491 if (host->ops && host->ops->get_next_data)
492 host->ops->get_next_data(host, data);
493}
494
Ludovic Barre135ea302018-10-08 14:08:38 +0200495int mmci_dma_start(struct mmci_host *host, unsigned int datactrl)
496{
497 struct mmc_data *data = host->data;
498 int ret;
499
500 if (!host->use_dma)
501 return -EINVAL;
502
503 ret = mmci_prep_data(host, data, false);
504 if (ret)
505 return ret;
506
507 if (!host->ops || !host->ops->dma_start)
508 return -EINVAL;
509
510 /* Okay, go for it. */
511 dev_vdbg(mmc_dev(host->mmc),
512 "Submit MMCI DMA job, sglen %d blksz %04x blks %04x flags %08x\n",
513 data->sg_len, data->blksz, data->blocks, data->flags);
514
515 host->ops->dma_start(host, &datactrl);
516
517 /* Trigger the DMA transfer */
518 mmci_write_datactrlreg(host, datactrl);
519
520 /*
521 * Let the MMCI say when the data is ended and it's time
522 * to fire next DMA request. When that happens, MMCI will
523 * call mmci_data_end()
524 */
525 writel(readl(host->base + MMCIMASK0) | MCI_DATAENDMASK,
526 host->base + MMCIMASK0);
527 return 0;
528}
529
Ludovic Barre5a9f10c2018-10-08 14:08:39 +0200530void mmci_dma_finalize(struct mmci_host *host, struct mmc_data *data)
531{
532 if (!host->use_dma)
533 return;
534
535 if (host->ops && host->ops->dma_finalize)
536 host->ops->dma_finalize(host, data);
537}
538
Ludovic Barrecfccc6a2018-10-08 14:08:40 +0200539void mmci_dma_error(struct mmci_host *host)
540{
541 if (!host->use_dma)
542 return;
543
544 if (host->ops && host->ops->dma_error)
545 host->ops->dma_error(host);
546}
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548static void
549mmci_request_end(struct mmci_host *host, struct mmc_request *mrq)
550{
551 writel(0, host->base + MMCICOMMAND);
552
Russell Kinge47c2222007-01-08 16:42:51 +0000553 BUG_ON(host->data);
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 host->mrq = NULL;
556 host->cmd = NULL;
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 mmc_request_done(host->mmc, mrq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
Linus Walleij2686b4b2010-10-19 12:39:48 +0100561static void mmci_set_mask1(struct mmci_host *host, unsigned int mask)
562{
563 void __iomem *base = host->base;
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100564 struct variant_data *variant = host->variant;
Linus Walleij2686b4b2010-10-19 12:39:48 +0100565
566 if (host->singleirq) {
567 unsigned int mask0 = readl(base + MMCIMASK0);
568
Ludovic Barre59db5e22018-10-08 14:08:47 +0200569 mask0 &= ~variant->irq_pio_mask;
Linus Walleij2686b4b2010-10-19 12:39:48 +0100570 mask0 |= mask;
571
572 writel(mask0, base + MMCIMASK0);
573 }
574
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100575 if (variant->mmcimask1)
576 writel(mask, base + MMCIMASK1);
577
578 host->mask1_reg = mask;
Linus Walleij2686b4b2010-10-19 12:39:48 +0100579}
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581static void mmci_stop_data(struct mmci_host *host)
582{
Ulf Hansson9cc639a2013-05-15 20:48:23 +0100583 mmci_write_datactrlreg(host, 0);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100584 mmci_set_mask1(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 host->data = NULL;
586}
587
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100588static void mmci_init_sg(struct mmci_host *host, struct mmc_data *data)
589{
590 unsigned int flags = SG_MITER_ATOMIC;
591
592 if (data->flags & MMC_DATA_READ)
593 flags |= SG_MITER_TO_SG;
594 else
595 flags |= SG_MITER_FROM_SG;
596
597 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
598}
599
Russell Kingc8ebae32011-01-11 19:35:53 +0000600/*
601 * All the DMA operation mode stuff goes inside this ifdef.
602 * This assumes that you have a generic DMA device interface,
603 * no custom DMA interfaces are supported.
604 */
605#ifdef CONFIG_DMA_ENGINE
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200606struct mmci_dmae_next {
607 struct dma_async_tx_descriptor *desc;
608 struct dma_chan *chan;
609};
610
611struct mmci_dmae_priv {
612 struct dma_chan *cur;
613 struct dma_chan *rx_channel;
614 struct dma_chan *tx_channel;
615 struct dma_async_tx_descriptor *desc_current;
616 struct mmci_dmae_next next_data;
617};
618
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200619int mmci_dmae_setup(struct mmci_host *host)
Russell Kingc8ebae32011-01-11 19:35:53 +0000620{
Russell Kingc8ebae32011-01-11 19:35:53 +0000621 const char *rxname, *txname;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200622 struct mmci_dmae_priv *dmae;
Russell Kingc8ebae32011-01-11 19:35:53 +0000623
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200624 dmae = devm_kzalloc(mmc_dev(host->mmc), sizeof(*dmae), GFP_KERNEL);
625 if (!dmae)
626 return -ENOMEM;
Russell Kingc8ebae32011-01-11 19:35:53 +0000627
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200628 host->dma_priv = dmae;
629
630 dmae->rx_channel = dma_request_slave_channel(mmc_dev(host->mmc),
631 "rx");
632 dmae->tx_channel = dma_request_slave_channel(mmc_dev(host->mmc),
633 "tx");
Per Forlin58c7ccb2011-07-01 18:55:24 +0200634
Russell Kingc8ebae32011-01-11 19:35:53 +0000635 /*
636 * If only an RX channel is specified, the driver will
637 * attempt to use it bidirectionally, however if it is
638 * is specified but cannot be located, DMA will be disabled.
639 */
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200640 if (dmae->rx_channel && !dmae->tx_channel)
641 dmae->tx_channel = dmae->rx_channel;
Russell Kingc8ebae32011-01-11 19:35:53 +0000642
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200643 if (dmae->rx_channel)
644 rxname = dma_chan_name(dmae->rx_channel);
Russell Kingc8ebae32011-01-11 19:35:53 +0000645 else
646 rxname = "none";
647
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200648 if (dmae->tx_channel)
649 txname = dma_chan_name(dmae->tx_channel);
Russell Kingc8ebae32011-01-11 19:35:53 +0000650 else
651 txname = "none";
652
653 dev_info(mmc_dev(host->mmc), "DMA channels RX %s, TX %s\n",
654 rxname, txname);
655
656 /*
657 * Limit the maximum segment size in any SG entry according to
658 * the parameters of the DMA engine device.
659 */
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200660 if (dmae->tx_channel) {
661 struct device *dev = dmae->tx_channel->device->dev;
Russell Kingc8ebae32011-01-11 19:35:53 +0000662 unsigned int max_seg_size = dma_get_max_seg_size(dev);
663
664 if (max_seg_size < host->mmc->max_seg_size)
665 host->mmc->max_seg_size = max_seg_size;
666 }
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200667 if (dmae->rx_channel) {
668 struct device *dev = dmae->rx_channel->device->dev;
Russell Kingc8ebae32011-01-11 19:35:53 +0000669 unsigned int max_seg_size = dma_get_max_seg_size(dev);
670
671 if (max_seg_size < host->mmc->max_seg_size)
672 host->mmc->max_seg_size = max_seg_size;
673 }
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100674
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200675 if (!dmae->tx_channel || !dmae->rx_channel) {
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200676 mmci_dmae_release(host);
677 return -EINVAL;
678 }
679
680 return 0;
Russell Kingc8ebae32011-01-11 19:35:53 +0000681}
682
683/*
Bill Pemberton6e0ee712012-11-19 13:26:03 -0500684 * This is used in or so inline it
Russell Kingc8ebae32011-01-11 19:35:53 +0000685 * so it can be discarded.
686 */
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200687void mmci_dmae_release(struct mmci_host *host)
Russell Kingc8ebae32011-01-11 19:35:53 +0000688{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200689 struct mmci_dmae_priv *dmae = host->dma_priv;
690
691 if (dmae->rx_channel)
692 dma_release_channel(dmae->rx_channel);
693 if (dmae->tx_channel)
694 dma_release_channel(dmae->tx_channel);
695 dmae->rx_channel = dmae->tx_channel = NULL;
Russell Kingc8ebae32011-01-11 19:35:53 +0000696}
697
698static void mmci_dma_unmap(struct mmci_host *host, struct mmc_data *data)
699{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200700 struct mmci_dmae_priv *dmae = host->dma_priv;
Ulf Hansson653a7612013-01-21 21:29:34 +0100701 struct dma_chan *chan;
Ulf Hansson653a7612013-01-21 21:29:34 +0100702
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200703 if (data->flags & MMC_DATA_READ)
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200704 chan = dmae->rx_channel;
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200705 else
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200706 chan = dmae->tx_channel;
Ulf Hansson653a7612013-01-21 21:29:34 +0100707
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200708 dma_unmap_sg(chan->device->dev, data->sg, data->sg_len,
709 mmc_get_dma_dir(data));
Ulf Hansson653a7612013-01-21 21:29:34 +0100710}
711
Ludovic Barrecfccc6a2018-10-08 14:08:40 +0200712void mmci_dmae_error(struct mmci_host *host)
Ludovic Barre7b2a6d52018-09-21 11:45:55 +0200713{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200714 struct mmci_dmae_priv *dmae = host->dma_priv;
715
Ludovic Barrecfccc6a2018-10-08 14:08:40 +0200716 if (!dma_inprogress(host))
Ludovic Barrecdea1942018-09-21 11:45:56 +0200717 return;
718
Ludovic Barre7b2a6d52018-09-21 11:45:55 +0200719 dev_err(mmc_dev(host->mmc), "error during DMA transfer!\n");
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200720 dmaengine_terminate_all(dmae->cur);
Ludovic Barre7b2a6d52018-09-21 11:45:55 +0200721 host->dma_in_progress = false;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200722 dmae->cur = NULL;
723 dmae->desc_current = NULL;
Ludovic Barre7b2a6d52018-09-21 11:45:55 +0200724 host->data->host_cookie = 0;
725
726 mmci_dma_unmap(host, host->data);
727}
728
Ludovic Barre5a9f10c2018-10-08 14:08:39 +0200729void mmci_dmae_finalize(struct mmci_host *host, struct mmc_data *data)
Ulf Hansson653a7612013-01-21 21:29:34 +0100730{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200731 struct mmci_dmae_priv *dmae = host->dma_priv;
Russell Kingc8ebae32011-01-11 19:35:53 +0000732 u32 status;
733 int i;
734
Ludovic Barre5a9f10c2018-10-08 14:08:39 +0200735 if (!dma_inprogress(host))
Ludovic Barrecdea1942018-09-21 11:45:56 +0200736 return;
737
Russell Kingc8ebae32011-01-11 19:35:53 +0000738 /* Wait up to 1ms for the DMA to complete */
739 for (i = 0; ; i++) {
740 status = readl(host->base + MMCISTATUS);
741 if (!(status & MCI_RXDATAAVLBLMASK) || i >= 100)
742 break;
743 udelay(10);
744 }
745
746 /*
747 * Check to see whether we still have some data left in the FIFO -
748 * this catches DMA controllers which are unable to monitor the
749 * DMALBREQ and DMALSREQ signals while allowing us to DMA to non-
750 * contiguous buffers. On TX, we'll get a FIFO underrun error.
751 */
752 if (status & MCI_RXDATAAVLBLMASK) {
Ludovic Barrecfccc6a2018-10-08 14:08:40 +0200753 mmci_dma_error(host);
Russell Kingc8ebae32011-01-11 19:35:53 +0000754 if (!data->error)
755 data->error = -EIO;
Ludovic Barre7b2a6d52018-09-21 11:45:55 +0200756 } else if (!data->host_cookie) {
Ulf Hansson653a7612013-01-21 21:29:34 +0100757 mmci_dma_unmap(host, data);
Ludovic Barre7b2a6d52018-09-21 11:45:55 +0200758 }
Russell Kingc8ebae32011-01-11 19:35:53 +0000759
760 /*
761 * Use of DMA with scatter-gather is impossible.
762 * Give up with DMA and switch back to PIO mode.
763 */
764 if (status & MCI_RXDATAAVLBLMASK) {
765 dev_err(mmc_dev(host->mmc), "buggy DMA detected. Taking evasive action.\n");
766 mmci_dma_release(host);
767 }
Ulf Hansson653a7612013-01-21 21:29:34 +0100768
Linus Walleije13934b2017-01-27 15:04:54 +0100769 host->dma_in_progress = false;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200770 dmae->cur = NULL;
771 dmae->desc_current = NULL;
Russell Kingc8ebae32011-01-11 19:35:53 +0000772}
773
Ulf Hansson653a7612013-01-21 21:29:34 +0100774/* prepares DMA channel and DMA descriptor, returns non-zero on failure */
Ludovic Barre47983512018-10-08 14:08:36 +0200775static int _mmci_dmae_prep_data(struct mmci_host *host, struct mmc_data *data,
Ulf Hansson653a7612013-01-21 21:29:34 +0100776 struct dma_chan **dma_chan,
777 struct dma_async_tx_descriptor **dma_desc)
Russell Kingc8ebae32011-01-11 19:35:53 +0000778{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200779 struct mmci_dmae_priv *dmae = host->dma_priv;
Russell Kingc8ebae32011-01-11 19:35:53 +0000780 struct variant_data *variant = host->variant;
781 struct dma_slave_config conf = {
782 .src_addr = host->phybase + MMCIFIFO,
783 .dst_addr = host->phybase + MMCIFIFO,
784 .src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
785 .dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
786 .src_maxburst = variant->fifohalfsize >> 2, /* # of words */
787 .dst_maxburst = variant->fifohalfsize >> 2, /* # of words */
Viresh Kumar258aea72012-02-01 16:12:19 +0530788 .device_fc = false,
Russell Kingc8ebae32011-01-11 19:35:53 +0000789 };
Russell Kingc8ebae32011-01-11 19:35:53 +0000790 struct dma_chan *chan;
791 struct dma_device *device;
792 struct dma_async_tx_descriptor *desc;
793 int nr_sg;
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100794 unsigned long flags = DMA_CTRL_ACK;
Russell Kingc8ebae32011-01-11 19:35:53 +0000795
Russell Kingc8ebae32011-01-11 19:35:53 +0000796 if (data->flags & MMC_DATA_READ) {
Vinod Koul05f57992011-10-14 10:45:11 +0530797 conf.direction = DMA_DEV_TO_MEM;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200798 chan = dmae->rx_channel;
Russell Kingc8ebae32011-01-11 19:35:53 +0000799 } else {
Vinod Koul05f57992011-10-14 10:45:11 +0530800 conf.direction = DMA_MEM_TO_DEV;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200801 chan = dmae->tx_channel;
Russell Kingc8ebae32011-01-11 19:35:53 +0000802 }
803
804 /* If there's no DMA channel, fall back to PIO */
805 if (!chan)
806 return -EINVAL;
807
808 /* If less than or equal to the fifo size, don't bother with DMA */
Per Forlin58c7ccb2011-07-01 18:55:24 +0200809 if (data->blksz * data->blocks <= variant->fifosize)
Russell Kingc8ebae32011-01-11 19:35:53 +0000810 return -EINVAL;
811
812 device = chan->device;
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200813 nr_sg = dma_map_sg(device->dev, data->sg, data->sg_len,
814 mmc_get_dma_dir(data));
Russell Kingc8ebae32011-01-11 19:35:53 +0000815 if (nr_sg == 0)
816 return -EINVAL;
817
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100818 if (host->variant->qcom_dml)
819 flags |= DMA_PREP_INTERRUPT;
820
Russell Kingc8ebae32011-01-11 19:35:53 +0000821 dmaengine_slave_config(chan, &conf);
Alexandre Bounine16052822012-03-08 16:11:18 -0500822 desc = dmaengine_prep_slave_sg(chan, data->sg, nr_sg,
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100823 conf.direction, flags);
Russell Kingc8ebae32011-01-11 19:35:53 +0000824 if (!desc)
825 goto unmap_exit;
826
Ulf Hansson653a7612013-01-21 21:29:34 +0100827 *dma_chan = chan;
828 *dma_desc = desc;
Russell Kingc8ebae32011-01-11 19:35:53 +0000829
Per Forlin58c7ccb2011-07-01 18:55:24 +0200830 return 0;
831
832 unmap_exit:
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200833 dma_unmap_sg(device->dev, data->sg, data->sg_len,
834 mmc_get_dma_dir(data));
Per Forlin58c7ccb2011-07-01 18:55:24 +0200835 return -ENOMEM;
836}
837
Ludovic Barre47983512018-10-08 14:08:36 +0200838int mmci_dmae_prep_data(struct mmci_host *host,
839 struct mmc_data *data,
840 bool next)
Ulf Hansson653a7612013-01-21 21:29:34 +0100841{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200842 struct mmci_dmae_priv *dmae = host->dma_priv;
Ludovic Barread7b8912018-10-08 14:08:35 +0200843 struct mmci_dmae_next *nd = &dmae->next_data;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200844
Ludovic Barre47983512018-10-08 14:08:36 +0200845 if (!host->use_dma)
846 return -EINVAL;
847
Ludovic Barread7b8912018-10-08 14:08:35 +0200848 if (next)
Ludovic Barre47983512018-10-08 14:08:36 +0200849 return _mmci_dmae_prep_data(host, data, &nd->chan, &nd->desc);
Ulf Hansson653a7612013-01-21 21:29:34 +0100850 /* Check if next job is already prepared. */
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200851 if (dmae->cur && dmae->desc_current)
Ulf Hansson653a7612013-01-21 21:29:34 +0100852 return 0;
853
854 /* No job were prepared thus do it now. */
Ludovic Barre47983512018-10-08 14:08:36 +0200855 return _mmci_dmae_prep_data(host, data, &dmae->cur,
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200856 &dmae->desc_current);
Ulf Hansson653a7612013-01-21 21:29:34 +0100857}
858
Ludovic Barre135ea302018-10-08 14:08:38 +0200859int mmci_dmae_start(struct mmci_host *host, unsigned int *datactrl)
Per Forlin58c7ccb2011-07-01 18:55:24 +0200860{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200861 struct mmci_dmae_priv *dmae = host->dma_priv;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200862 struct mmc_data *data = host->data;
863
Linus Walleije13934b2017-01-27 15:04:54 +0100864 host->dma_in_progress = true;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200865 dmaengine_submit(dmae->desc_current);
866 dma_async_issue_pending(dmae->cur);
Russell Kingc8ebae32011-01-11 19:35:53 +0000867
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100868 if (host->variant->qcom_dml)
869 dml_start_xfer(host, data);
870
Ludovic Barre135ea302018-10-08 14:08:38 +0200871 *datactrl |= MCI_DPSM_DMAENABLE;
Russell Kingc8ebae32011-01-11 19:35:53 +0000872
Russell Kingc8ebae32011-01-11 19:35:53 +0000873 return 0;
Russell Kingc8ebae32011-01-11 19:35:53 +0000874}
Per Forlin58c7ccb2011-07-01 18:55:24 +0200875
Ludovic Barre02769962018-10-08 14:08:37 +0200876void mmci_dmae_get_next_data(struct mmci_host *host, struct mmc_data *data)
Per Forlin58c7ccb2011-07-01 18:55:24 +0200877{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200878 struct mmci_dmae_priv *dmae = host->dma_priv;
879 struct mmci_dmae_next *next = &dmae->next_data;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200880
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200881 if (!host->use_dma)
882 return;
883
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200884 WARN_ON(!data->host_cookie && (next->desc || next->chan));
Per Forlin58c7ccb2011-07-01 18:55:24 +0200885
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200886 dmae->desc_current = next->desc;
887 dmae->cur = next->chan;
888 next->desc = NULL;
889 next->chan = NULL;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200890}
891
Ludovic Barre47983512018-10-08 14:08:36 +0200892void mmci_dmae_unprep_data(struct mmci_host *host,
893 struct mmc_data *data, int err)
894
Per Forlin58c7ccb2011-07-01 18:55:24 +0200895{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200896 struct mmci_dmae_priv *dmae = host->dma_priv;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200897
Ludovic Barre47983512018-10-08 14:08:36 +0200898 if (!host->use_dma)
Per Forlin58c7ccb2011-07-01 18:55:24 +0200899 return;
900
Ulf Hansson653a7612013-01-21 21:29:34 +0100901 mmci_dma_unmap(host, data);
Per Forlin58c7ccb2011-07-01 18:55:24 +0200902
Ulf Hansson653a7612013-01-21 21:29:34 +0100903 if (err) {
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200904 struct mmci_dmae_next *next = &dmae->next_data;
Ulf Hansson653a7612013-01-21 21:29:34 +0100905 struct dma_chan *chan;
906 if (data->flags & MMC_DATA_READ)
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200907 chan = dmae->rx_channel;
Ulf Hansson653a7612013-01-21 21:29:34 +0100908 else
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200909 chan = dmae->tx_channel;
Ulf Hansson653a7612013-01-21 21:29:34 +0100910 dmaengine_terminate_all(chan);
Per Forlin58c7ccb2011-07-01 18:55:24 +0200911
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200912 if (dmae->desc_current == next->desc)
913 dmae->desc_current = NULL;
Srinivas Kandagatlab5c16a62014-10-08 12:25:17 +0100914
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200915 if (dmae->cur == next->chan) {
Linus Walleije13934b2017-01-27 15:04:54 +0100916 host->dma_in_progress = false;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200917 dmae->cur = NULL;
Linus Walleije13934b2017-01-27 15:04:54 +0100918 }
Srinivas Kandagatlab5c16a62014-10-08 12:25:17 +0100919
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200920 next->desc = NULL;
921 next->chan = NULL;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200922 }
923}
924
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200925static struct mmci_host_ops mmci_variant_ops = {
Ludovic Barre47983512018-10-08 14:08:36 +0200926 .prep_data = mmci_dmae_prep_data,
927 .unprep_data = mmci_dmae_unprep_data,
Ludovic Barre02769962018-10-08 14:08:37 +0200928 .get_next_data = mmci_dmae_get_next_data,
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200929 .dma_setup = mmci_dmae_setup,
930 .dma_release = mmci_dmae_release,
Ludovic Barre135ea302018-10-08 14:08:38 +0200931 .dma_start = mmci_dmae_start,
Ludovic Barre5a9f10c2018-10-08 14:08:39 +0200932 .dma_finalize = mmci_dmae_finalize,
Ludovic Barrecfccc6a2018-10-08 14:08:40 +0200933 .dma_error = mmci_dmae_error,
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200934};
935
936void mmci_variant_init(struct mmci_host *host)
937{
938 host->ops = &mmci_variant_ops;
939}
Russell Kingc8ebae32011-01-11 19:35:53 +0000940#endif
941
Ludovic Barre47983512018-10-08 14:08:36 +0200942static void mmci_pre_request(struct mmc_host *mmc, struct mmc_request *mrq)
943{
944 struct mmci_host *host = mmc_priv(mmc);
945 struct mmc_data *data = mrq->data;
946
947 if (!data)
948 return;
949
950 WARN_ON(data->host_cookie);
951
952 if (mmci_validate_data(host, data))
953 return;
954
955 mmci_prep_data(host, data, true);
956}
957
958static void mmci_post_request(struct mmc_host *mmc, struct mmc_request *mrq,
959 int err)
960{
961 struct mmci_host *host = mmc_priv(mmc);
962 struct mmc_data *data = mrq->data;
963
964 if (!data || !data->host_cookie)
965 return;
966
967 mmci_unprep_data(host, data, err);
968}
969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970static void mmci_start_data(struct mmci_host *host, struct mmc_data *data)
971{
Rabin Vincent8301bb62010-08-09 12:57:30 +0100972 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 unsigned int datactrl, timeout, irqmask;
Russell King7b09cda2005-07-01 12:02:59 +0100974 unsigned long long clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 void __iomem *base;
Russell King3bc87f22006-08-27 13:51:28 +0100976 int blksz_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Linus Walleij64de0282010-02-19 01:09:10 +0100978 dev_dbg(mmc_dev(host->mmc), "blksz %04x blks %04x flags %08x\n",
979 data->blksz, data->blocks, data->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981 host->data = data;
Rabin Vincent528320d2010-07-21 12:49:49 +0100982 host->size = data->blksz * data->blocks;
Russell King51d43752011-01-27 10:56:52 +0000983 data->bytes_xfered = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Russell King7b09cda2005-07-01 12:02:59 +0100985 clks = (unsigned long long)data->timeout_ns * host->cclk;
Srinivas Kandagatlac4a35762014-06-02 10:08:39 +0100986 do_div(clks, NSEC_PER_SEC);
Russell King7b09cda2005-07-01 12:02:59 +0100987
988 timeout = data->timeout_clks + (unsigned int)clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 base = host->base;
991 writel(timeout, base + MMCIDATATIMER);
992 writel(host->size, base + MMCIDATALENGTH);
993
Russell King3bc87f22006-08-27 13:51:28 +0100994 blksz_bits = ffs(data->blksz) - 1;
995 BUG_ON(1 << blksz_bits != data->blksz);
996
Philippe Langlais1784b152011-03-25 08:51:52 +0100997 if (variant->blksz_datactrl16)
Ludovic Barre9b279942018-10-08 14:08:46 +0200998 datactrl = variant->datactrl_dpsm_enable | (data->blksz << 16);
Srinivas Kandagatlaff783232014-06-02 10:09:06 +0100999 else if (variant->blksz_datactrl4)
Ludovic Barre9b279942018-10-08 14:08:46 +02001000 datactrl = variant->datactrl_dpsm_enable | (data->blksz << 4);
Philippe Langlais1784b152011-03-25 08:51:52 +01001001 else
Ludovic Barre9b279942018-10-08 14:08:46 +02001002 datactrl = variant->datactrl_dpsm_enable | blksz_bits << 4;
Russell Kingc8ebae32011-01-11 19:35:53 +00001003
1004 if (data->flags & MMC_DATA_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 datactrl |= MCI_DPSM_DIRECTION;
Russell Kingc8ebae32011-01-11 19:35:53 +00001006
Srinivas Kandagatlac7354132014-08-22 05:55:16 +01001007 if (host->mmc->card && mmc_card_sdio(host->mmc->card)) {
1008 u32 clk;
Ulf Hansson7258db72011-12-13 17:05:28 +01001009
Srinivas Kandagatlac7354132014-08-22 05:55:16 +01001010 datactrl |= variant->datactrl_mask_sdio;
Ulf Hansson06c1a122012-10-12 14:01:50 +01001011
Srinivas Kandagatlac7354132014-08-22 05:55:16 +01001012 /*
1013 * The ST Micro variant for SDIO small write transfers
1014 * needs to have clock H/W flow control disabled,
1015 * otherwise the transfer will not start. The threshold
1016 * depends on the rate of MCLK.
1017 */
1018 if (variant->st_sdio && data->flags & MMC_DATA_WRITE &&
1019 (host->size < 8 ||
1020 (host->size <= 8 && host->mclk > 50000000)))
1021 clk = host->clk_reg & ~variant->clkreg_enable;
1022 else
1023 clk = host->clk_reg | variant->clkreg_enable;
1024
1025 mmci_write_clkreg(host, clk);
1026 }
Ulf Hansson06c1a122012-10-12 14:01:50 +01001027
Seungwon Jeon6dad6c92014-03-14 21:12:13 +09001028 if (host->mmc->ios.timing == MMC_TIMING_UHS_DDR50 ||
1029 host->mmc->ios.timing == MMC_TIMING_MMC_DDR52)
Srinivas Kandagatlae17dca22014-06-02 10:09:15 +01001030 datactrl |= variant->datactrl_mask_ddrmode;
Ulf Hansson6dbb6ee2013-01-07 15:30:44 +01001031
Russell Kingc8ebae32011-01-11 19:35:53 +00001032 /*
1033 * Attempt to use DMA operation mode, if this
1034 * should fail, fall back to PIO mode
1035 */
Ludovic Barre135ea302018-10-08 14:08:38 +02001036 if (!mmci_dma_start(host, datactrl))
Russell Kingc8ebae32011-01-11 19:35:53 +00001037 return;
1038
1039 /* IRQ mode, map the SG list for CPU reading/writing */
1040 mmci_init_sg(host, data);
1041
1042 if (data->flags & MMC_DATA_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 irqmask = MCI_RXFIFOHALFFULLMASK;
Russell King0425a142006-02-16 16:48:31 +00001044
1045 /*
Russell Kingc4d877c2011-01-27 09:50:13 +00001046 * If we have less than the fifo 'half-full' threshold to
1047 * transfer, trigger a PIO interrupt as soon as any data
1048 * is available.
Russell King0425a142006-02-16 16:48:31 +00001049 */
Russell Kingc4d877c2011-01-27 09:50:13 +00001050 if (host->size < variant->fifohalfsize)
Russell King0425a142006-02-16 16:48:31 +00001051 irqmask |= MCI_RXDATAAVLBLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 } else {
1053 /*
1054 * We don't actually need to include "FIFO empty" here
1055 * since its implicit in "FIFO half empty".
1056 */
1057 irqmask = MCI_TXFIFOHALFEMPTYMASK;
1058 }
1059
Ulf Hansson9cc639a2013-05-15 20:48:23 +01001060 mmci_write_datactrlreg(host, datactrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 writel(readl(base + MMCIMASK0) & ~MCI_DATAENDMASK, base + MMCIMASK0);
Linus Walleij2686b4b2010-10-19 12:39:48 +01001062 mmci_set_mask1(host, irqmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
1064
1065static void
1066mmci_start_command(struct mmci_host *host, struct mmc_command *cmd, u32 c)
1067{
1068 void __iomem *base = host->base;
1069
Linus Walleij64de0282010-02-19 01:09:10 +01001070 dev_dbg(mmc_dev(host->mmc), "op %02x arg %08x flags %08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 cmd->opcode, cmd->arg, cmd->flags);
1072
Ludovic Barre0f244802018-10-08 14:08:45 +02001073 if (readl(base + MMCICOMMAND) & host->variant->cmdreg_cpsm_enable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 writel(0, base + MMCICOMMAND);
Srinivas Kandagatla6adb2a82014-06-02 10:08:57 +01001075 mmci_reg_delay(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 }
1077
Ludovic Barre0f244802018-10-08 14:08:45 +02001078 c |= cmd->opcode | host->variant->cmdreg_cpsm_enable;
Russell Kinge9225172006-02-02 12:23:12 +00001079 if (cmd->flags & MMC_RSP_PRESENT) {
1080 if (cmd->flags & MMC_RSP_136)
Ludovic Barre0f244802018-10-08 14:08:45 +02001081 c |= host->variant->cmdreg_lrsp_crc;
1082 else if (cmd->flags & MMC_RSP_CRC)
1083 c |= host->variant->cmdreg_srsp_crc;
1084 else
1085 c |= host->variant->cmdreg_srsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 }
1087 if (/*interrupt*/0)
1088 c |= MCI_CPSM_INTERRUPT;
1089
Srinivas Kandagatlaae7b0062014-06-02 10:09:39 +01001090 if (mmc_cmd_type(cmd) == MMC_CMD_ADTC)
1091 c |= host->variant->data_cmd_enable;
1092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 host->cmd = cmd;
1094
1095 writel(cmd->arg, base + MMCIARGUMENT);
1096 writel(c, base + MMCICOMMAND);
1097}
1098
1099static void
1100mmci_data_irq(struct mmci_host *host, struct mmc_data *data,
1101 unsigned int status)
1102{
Ludovic Barredaf97132018-10-08 14:08:44 +02001103 unsigned int status_err;
1104
Ulf Hansson1cb9da52014-06-12 14:42:23 +02001105 /* Make sure we have data to handle */
1106 if (!data)
1107 return;
1108
Linus Walleijf20f8f212010-10-19 13:41:24 +01001109 /* First check for errors */
Ludovic Barredaf97132018-10-08 14:08:44 +02001110 status_err = status & (host->variant->start_err |
1111 MCI_DATACRCFAIL | MCI_DATATIMEOUT |
1112 MCI_TXUNDERRUN | MCI_RXOVERRUN);
1113
1114 if (status_err) {
Linus Walleij8cb28152011-01-24 15:22:13 +01001115 u32 remain, success;
Linus Walleijf20f8f212010-10-19 13:41:24 +01001116
Russell Kingc8ebae32011-01-11 19:35:53 +00001117 /* Terminate the DMA transfer */
Ludovic Barrecfccc6a2018-10-08 14:08:40 +02001118 mmci_dma_error(host);
Russell Kingc8ebae32011-01-11 19:35:53 +00001119
Russell Kingc8afc9d2011-02-04 09:19:46 +00001120 /*
1121 * Calculate how far we are into the transfer. Note that
1122 * the data counter gives the number of bytes transferred
1123 * on the MMC bus, not on the host side. On reads, this
1124 * can be as much as a FIFO-worth of data ahead. This
1125 * matters for FIFO overruns only.
1126 */
Ludovic Barreb79220b2018-10-08 14:08:49 +02001127 if (!host->variant->datacnt_useless) {
1128 remain = readl(host->base + MMCIDATACNT);
1129 success = data->blksz * data->blocks - remain;
1130 } else {
1131 success = 0;
1132 }
Linus Walleij8cb28152011-01-24 15:22:13 +01001133
Russell Kingc8afc9d2011-02-04 09:19:46 +00001134 dev_dbg(mmc_dev(host->mmc), "MCI ERROR IRQ, status 0x%08x at 0x%08x\n",
Ludovic Barredaf97132018-10-08 14:08:44 +02001135 status_err, success);
1136 if (status_err & MCI_DATACRCFAIL) {
Linus Walleij8cb28152011-01-24 15:22:13 +01001137 /* Last block was not successful */
Russell Kingc8afc9d2011-02-04 09:19:46 +00001138 success -= 1;
Pierre Ossman17b04292007-07-22 22:18:46 +02001139 data->error = -EILSEQ;
Ludovic Barredaf97132018-10-08 14:08:44 +02001140 } else if (status_err & MCI_DATATIMEOUT) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001141 data->error = -ETIMEDOUT;
Ludovic Barredaf97132018-10-08 14:08:44 +02001142 } else if (status_err & MCI_STARTBITERR) {
Linus Walleij757df742011-06-30 15:10:21 +01001143 data->error = -ECOMM;
Ludovic Barredaf97132018-10-08 14:08:44 +02001144 } else if (status_err & MCI_TXUNDERRUN) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001145 data->error = -EIO;
Ludovic Barredaf97132018-10-08 14:08:44 +02001146 } else if (status_err & MCI_RXOVERRUN) {
Russell Kingc8afc9d2011-02-04 09:19:46 +00001147 if (success > host->variant->fifosize)
1148 success -= host->variant->fifosize;
1149 else
1150 success = 0;
Linus Walleij8cb28152011-01-24 15:22:13 +01001151 data->error = -EIO;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001152 }
Russell King51d43752011-01-27 10:56:52 +00001153 data->bytes_xfered = round_down(success, data->blksz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 }
Linus Walleijf20f8f212010-10-19 13:41:24 +01001155
Linus Walleij8cb28152011-01-24 15:22:13 +01001156 if (status & MCI_DATABLOCKEND)
1157 dev_err(mmc_dev(host->mmc), "stray MCI_DATABLOCKEND interrupt\n");
Linus Walleijf20f8f212010-10-19 13:41:24 +01001158
Russell Kingccff9b52011-01-30 21:03:50 +00001159 if (status & MCI_DATAEND || data->error) {
Ludovic Barrecdea1942018-09-21 11:45:56 +02001160 mmci_dma_finalize(host, data);
1161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 mmci_stop_data(host);
1163
Linus Walleij8cb28152011-01-24 15:22:13 +01001164 if (!data->error)
1165 /* The error clause is handled above, success! */
Russell King51d43752011-01-27 10:56:52 +00001166 data->bytes_xfered = data->blksz * data->blocks;
Linus Walleijf20f8f212010-10-19 13:41:24 +01001167
Ulf Hansson024629c2013-05-13 15:40:56 +01001168 if (!data->stop || host->mrq->sbc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 mmci_request_end(host, data->mrq);
1170 } else {
1171 mmci_start_command(host, data->stop, 0);
1172 }
1173 }
1174}
1175
1176static void
1177mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
1178 unsigned int status)
1179{
1180 void __iomem *base = host->base;
Linus Walleij49adc0c2016-10-25 11:06:06 +02001181 bool sbc;
Ulf Hanssonad82bfe2014-06-12 15:01:57 +02001182
1183 if (!cmd)
1184 return;
1185
1186 sbc = (cmd == host->mrq->sbc);
Ulf Hanssonad82bfe2014-06-12 15:01:57 +02001187
Linus Walleij49adc0c2016-10-25 11:06:06 +02001188 /*
1189 * We need to be one of these interrupts to be considered worth
1190 * handling. Note that we tag on any latent IRQs postponed
1191 * due to waiting for busy status.
1192 */
1193 if (!((status|host->busy_status) &
1194 (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT|MCI_CMDSENT|MCI_CMDRESPEND)))
Ulf Hanssonad82bfe2014-06-12 15:01:57 +02001195 return;
Ulf Hansson8d94b542014-01-13 16:49:31 +01001196
Linus Walleij49adc0c2016-10-25 11:06:06 +02001197 /*
1198 * ST Micro variant: handle busy detection.
1199 */
1200 if (host->variant->busy_detect) {
1201 bool busy_resp = !!(cmd->flags & MMC_RSP_BUSY);
Ulf Hansson8d94b542014-01-13 16:49:31 +01001202
Linus Walleij49adc0c2016-10-25 11:06:06 +02001203 /* We are busy with a command, return */
1204 if (host->busy_status &&
1205 (status & host->variant->busy_detect_flag))
1206 return;
Ulf Hansson8d94b542014-01-13 16:49:31 +01001207
Linus Walleij49adc0c2016-10-25 11:06:06 +02001208 /*
1209 * We were not busy, but we now got a busy response on
1210 * something that was not an error, and we double-check
1211 * that the special busy status bit is still set before
1212 * proceeding.
1213 */
1214 if (!host->busy_status && busy_resp &&
1215 !(status & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT)) &&
1216 (readl(base + MMCISTATUS) & host->variant->busy_detect_flag)) {
Jean-Nicolas Graux5cad24d2017-02-07 12:12:41 +01001217
1218 /* Clear the busy start IRQ */
1219 writel(host->variant->busy_detect_mask,
1220 host->base + MMCICLEAR);
1221
1222 /* Unmask the busy end IRQ */
Linus Walleij49adc0c2016-10-25 11:06:06 +02001223 writel(readl(base + MMCIMASK0) |
1224 host->variant->busy_detect_mask,
1225 base + MMCIMASK0);
1226 /*
1227 * Now cache the last response status code (until
1228 * the busy bit goes low), and return.
1229 */
1230 host->busy_status =
1231 status & (MCI_CMDSENT|MCI_CMDRESPEND);
1232 return;
1233 }
1234
1235 /*
1236 * At this point we are not busy with a command, we have
Jean-Nicolas Graux5cad24d2017-02-07 12:12:41 +01001237 * not received a new busy request, clear and mask the busy
1238 * end IRQ and fall through to process the IRQ.
Linus Walleij49adc0c2016-10-25 11:06:06 +02001239 */
1240 if (host->busy_status) {
Jean-Nicolas Graux5cad24d2017-02-07 12:12:41 +01001241
1242 writel(host->variant->busy_detect_mask,
1243 host->base + MMCICLEAR);
1244
Linus Walleij49adc0c2016-10-25 11:06:06 +02001245 writel(readl(base + MMCIMASK0) &
1246 ~host->variant->busy_detect_mask,
1247 base + MMCIMASK0);
1248 host->busy_status = 0;
1249 }
Ulf Hansson8d94b542014-01-13 16:49:31 +01001250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 host->cmd = NULL;
1253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 if (status & MCI_CMDTIMEOUT) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001255 cmd->error = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001257 cmd->error = -EILSEQ;
Russell King - ARM Linux9047b432011-01-11 16:35:56 +00001258 } else {
1259 cmd->resp[0] = readl(base + MMCIRESPONSE0);
1260 cmd->resp[1] = readl(base + MMCIRESPONSE1);
1261 cmd->resp[2] = readl(base + MMCIRESPONSE2);
1262 cmd->resp[3] = readl(base + MMCIRESPONSE3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 }
1264
Ulf Hansson024629c2013-05-13 15:40:56 +01001265 if ((!sbc && !cmd->data) || cmd->error) {
Ulf Hansson3b6e3c72011-12-13 16:58:43 +01001266 if (host->data) {
1267 /* Terminate the DMA transfer */
Ludovic Barrecfccc6a2018-10-08 14:08:40 +02001268 mmci_dma_error(host);
Ludovic Barre7b2a6d52018-09-21 11:45:55 +02001269
Russell Kinge47c2222007-01-08 16:42:51 +00001270 mmci_stop_data(host);
Ulf Hansson3b6e3c72011-12-13 16:58:43 +01001271 }
Ulf Hansson024629c2013-05-13 15:40:56 +01001272 mmci_request_end(host, host->mrq);
1273 } else if (sbc) {
1274 mmci_start_command(host, host->mrq->cmd, 0);
Ludovic Barred2141542018-10-08 14:08:48 +02001275 } else if (!host->variant->datactrl_first &&
1276 !(cmd->data->flags & MMC_DATA_READ)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 mmci_start_data(host, cmd->data);
1278 }
1279}
1280
Srinivas Kandagatla9c34b732014-06-02 10:10:04 +01001281static int mmci_get_rx_fifocnt(struct mmci_host *host, u32 status, int remain)
1282{
1283 return remain - (readl(host->base + MMCIFIFOCNT) << 2);
1284}
1285
1286static int mmci_qcom_get_rx_fifocnt(struct mmci_host *host, u32 status, int r)
1287{
1288 /*
1289 * on qcom SDCC4 only 8 words are used in each burst so only 8 addresses
1290 * from the fifo range should be used
1291 */
1292 if (status & MCI_RXFIFOHALFFULL)
1293 return host->variant->fifohalfsize;
1294 else if (status & MCI_RXDATAAVLBL)
1295 return 4;
1296
1297 return 0;
1298}
1299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300static int mmci_pio_read(struct mmci_host *host, char *buffer, unsigned int remain)
1301{
1302 void __iomem *base = host->base;
1303 char *ptr = buffer;
Srinivas Kandagatla9c34b732014-06-02 10:10:04 +01001304 u32 status = readl(host->base + MMCISTATUS);
Linus Walleij26eed9a2008-04-26 23:39:44 +01001305 int host_remain = host->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 do {
Srinivas Kandagatla9c34b732014-06-02 10:10:04 +01001308 int count = host->get_rx_fifocnt(host, status, host_remain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 if (count > remain)
1311 count = remain;
1312
1313 if (count <= 0)
1314 break;
1315
Ulf Hansson393e5e22011-12-13 17:08:04 +01001316 /*
1317 * SDIO especially may want to send something that is
1318 * not divisible by 4 (as opposed to card sectors
1319 * etc). Therefore make sure to always read the last bytes
1320 * while only doing full 32-bit reads towards the FIFO.
1321 */
1322 if (unlikely(count & 0x3)) {
1323 if (count < 4) {
1324 unsigned char buf[4];
Davide Ciminaghi4b85da02012-12-10 14:47:21 +01001325 ioread32_rep(base + MMCIFIFO, buf, 1);
Ulf Hansson393e5e22011-12-13 17:08:04 +01001326 memcpy(ptr, buf, count);
1327 } else {
Davide Ciminaghi4b85da02012-12-10 14:47:21 +01001328 ioread32_rep(base + MMCIFIFO, ptr, count >> 2);
Ulf Hansson393e5e22011-12-13 17:08:04 +01001329 count &= ~0x3;
1330 }
1331 } else {
Davide Ciminaghi4b85da02012-12-10 14:47:21 +01001332 ioread32_rep(base + MMCIFIFO, ptr, count >> 2);
Ulf Hansson393e5e22011-12-13 17:08:04 +01001333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335 ptr += count;
1336 remain -= count;
Linus Walleij26eed9a2008-04-26 23:39:44 +01001337 host_remain -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339 if (remain == 0)
1340 break;
1341
1342 status = readl(base + MMCISTATUS);
1343 } while (status & MCI_RXDATAAVLBL);
1344
1345 return ptr - buffer;
1346}
1347
1348static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status)
1349{
Rabin Vincent8301bb62010-08-09 12:57:30 +01001350 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 void __iomem *base = host->base;
1352 char *ptr = buffer;
1353
1354 do {
1355 unsigned int count, maxcnt;
1356
Rabin Vincent8301bb62010-08-09 12:57:30 +01001357 maxcnt = status & MCI_TXFIFOEMPTY ?
1358 variant->fifosize : variant->fifohalfsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 count = min(remain, maxcnt);
1360
Linus Walleij34177802010-10-19 12:43:58 +01001361 /*
Linus Walleij34177802010-10-19 12:43:58 +01001362 * SDIO especially may want to send something that is
1363 * not divisible by 4 (as opposed to card sectors
1364 * etc), and the FIFO only accept full 32-bit writes.
1365 * So compensate by adding +3 on the count, a single
1366 * byte become a 32bit write, 7 bytes will be two
1367 * 32bit writes etc.
1368 */
Davide Ciminaghi4b85da02012-12-10 14:47:21 +01001369 iowrite32_rep(base + MMCIFIFO, ptr, (count + 3) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 ptr += count;
1372 remain -= count;
1373
1374 if (remain == 0)
1375 break;
1376
1377 status = readl(base + MMCISTATUS);
1378 } while (status & MCI_TXFIFOHALFEMPTY);
1379
1380 return ptr - buffer;
1381}
1382
1383/*
1384 * PIO data transfer IRQ handler.
1385 */
David Howells7d12e782006-10-05 14:55:46 +01001386static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
1388 struct mmci_host *host = dev_id;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001389 struct sg_mapping_iter *sg_miter = &host->sg_miter;
Rabin Vincent8301bb62010-08-09 12:57:30 +01001390 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 void __iomem *base = host->base;
1392 u32 status;
1393
1394 status = readl(base + MMCISTATUS);
1395
Linus Walleij64de0282010-02-19 01:09:10 +01001396 dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 unsigned int remain, len;
1400 char *buffer;
1401
1402 /*
1403 * For write, we only need to test the half-empty flag
1404 * here - if the FIFO is completely empty, then by
1405 * definition it is more than half empty.
1406 *
1407 * For read, check for data available.
1408 */
1409 if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL)))
1410 break;
1411
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001412 if (!sg_miter_next(sg_miter))
1413 break;
1414
1415 buffer = sg_miter->addr;
1416 remain = sg_miter->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
1418 len = 0;
1419 if (status & MCI_RXACTIVE)
1420 len = mmci_pio_read(host, buffer, remain);
1421 if (status & MCI_TXACTIVE)
1422 len = mmci_pio_write(host, buffer, remain, status);
1423
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001424 sg_miter->consumed = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 host->size -= len;
1427 remain -= len;
1428
1429 if (remain)
1430 break;
1431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 status = readl(base + MMCISTATUS);
1433 } while (1);
1434
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001435 sg_miter_stop(sg_miter);
1436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 /*
Russell Kingc4d877c2011-01-27 09:50:13 +00001438 * If we have less than the fifo 'half-full' threshold to transfer,
1439 * trigger a PIO interrupt as soon as any data is available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 */
Russell Kingc4d877c2011-01-27 09:50:13 +00001441 if (status & MCI_RXACTIVE && host->size < variant->fifohalfsize)
Linus Walleij2686b4b2010-10-19 12:39:48 +01001442 mmci_set_mask1(host, MCI_RXDATAAVLBLMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
1444 /*
1445 * If we run out of data, disable the data IRQs; this
1446 * prevents a race where the FIFO becomes empty before
1447 * the chip itself has disabled the data path, and
1448 * stops us racing with our data end IRQ.
1449 */
1450 if (host->size == 0) {
Linus Walleij2686b4b2010-10-19 12:39:48 +01001451 mmci_set_mask1(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0);
1453 }
1454
1455 return IRQ_HANDLED;
1456}
1457
1458/*
1459 * Handle completion of command and data transfers.
1460 */
David Howells7d12e782006-10-05 14:55:46 +01001461static irqreturn_t mmci_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462{
1463 struct mmci_host *host = dev_id;
1464 u32 status;
1465 int ret = 0;
1466
1467 spin_lock(&host->lock);
1468
1469 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 status = readl(host->base + MMCISTATUS);
Linus Walleij2686b4b2010-10-19 12:39:48 +01001471
1472 if (host->singleirq) {
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +01001473 if (status & host->mask1_reg)
Linus Walleij2686b4b2010-10-19 12:39:48 +01001474 mmci_pio_irq(irq, dev_id);
1475
Ludovic Barre59db5e22018-10-08 14:08:47 +02001476 status &= ~host->variant->irq_pio_mask;
Linus Walleij2686b4b2010-10-19 12:39:48 +01001477 }
1478
Ulf Hansson8d94b542014-01-13 16:49:31 +01001479 /*
Jean-Nicolas Graux5cad24d2017-02-07 12:12:41 +01001480 * We intentionally clear the MCI_ST_CARDBUSY IRQ (if it's
1481 * enabled) in mmci_cmd_irq() function where ST Micro busy
1482 * detection variant is handled. Considering the HW seems to be
1483 * triggering the IRQ on both edges while monitoring DAT0 for
1484 * busy completion and that same status bit is used to monitor
1485 * start and end of busy detection, special care must be taken
1486 * to make sure that both start and end interrupts are always
1487 * cleared one after the other.
Ulf Hansson8d94b542014-01-13 16:49:31 +01001488 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 status &= readl(host->base + MMCIMASK0);
Jean-Nicolas Graux5cad24d2017-02-07 12:12:41 +01001490 if (host->variant->busy_detect)
1491 writel(status & ~host->variant->busy_detect_mask,
1492 host->base + MMCICLEAR);
1493 else
1494 writel(status, host->base + MMCICLEAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Linus Walleij64de0282010-02-19 01:09:10 +01001496 dev_dbg(mmc_dev(host->mmc), "irq0 (data+cmd) %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
Ulf Hansson78782892014-06-13 13:21:38 +02001498 if (host->variant->reversed_irq_handling) {
1499 mmci_data_irq(host, host->data, status);
1500 mmci_cmd_irq(host, host->cmd, status);
1501 } else {
1502 mmci_cmd_irq(host, host->cmd, status);
1503 mmci_data_irq(host, host->data, status);
1504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Linus Walleij49adc0c2016-10-25 11:06:06 +02001506 /*
1507 * Don't poll for busy completion in irq context.
1508 */
1509 if (host->variant->busy_detect && host->busy_status)
1510 status &= ~host->variant->busy_detect_flag;
Ulf Hansson8d94b542014-01-13 16:49:31 +01001511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 ret = 1;
1513 } while (status);
1514
1515 spin_unlock(&host->lock);
1516
1517 return IRQ_RETVAL(ret);
1518}
1519
1520static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1521{
1522 struct mmci_host *host = mmc_priv(mmc);
Linus Walleij9e943022008-10-24 21:17:50 +01001523 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
1525 WARN_ON(host->mrq != NULL);
1526
Ulf Hansson653a7612013-01-21 21:29:34 +01001527 mrq->cmd->error = mmci_validate_data(host, mrq->data);
1528 if (mrq->cmd->error) {
Pierre Ossman255d01a2007-07-24 20:38:53 +02001529 mmc_request_done(mmc, mrq);
1530 return;
1531 }
1532
Linus Walleij9e943022008-10-24 21:17:50 +01001533 spin_lock_irqsave(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
1535 host->mrq = mrq;
1536
Per Forlin58c7ccb2011-07-01 18:55:24 +02001537 if (mrq->data)
1538 mmci_get_next_data(host, mrq->data);
1539
Ludovic Barred2141542018-10-08 14:08:48 +02001540 if (mrq->data &&
1541 (host->variant->datactrl_first || mrq->data->flags & MMC_DATA_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 mmci_start_data(host, mrq->data);
1543
Ulf Hansson024629c2013-05-13 15:40:56 +01001544 if (mrq->sbc)
1545 mmci_start_command(host, mrq->sbc, 0);
1546 else
1547 mmci_start_command(host, mrq->cmd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Linus Walleij9e943022008-10-24 21:17:50 +01001549 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550}
1551
1552static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1553{
1554 struct mmci_host *host = mmc_priv(mmc);
Ulf Hansson7d72a1d2011-12-13 16:54:55 +01001555 struct variant_data *variant = host->variant;
Linus Walleija6a64642009-09-14 12:56:14 +01001556 u32 pwr = 0;
1557 unsigned long flags;
Lee Jonesdb90f912013-05-03 12:52:12 +01001558 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Ulf Hanssonbc521812011-12-13 16:57:55 +01001560 if (host->plat->ios_handler &&
1561 host->plat->ios_handler(mmc_dev(mmc), ios))
1562 dev_err(mmc_dev(mmc), "platform ios_handler failed\n");
1563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 switch (ios->power_mode) {
1565 case MMC_POWER_OFF:
Ulf Hansson599c1d52013-01-07 16:22:50 +01001566 if (!IS_ERR(mmc->supply.vmmc))
1567 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Lee Jones237fb5e2013-01-31 11:27:52 +00001568
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001569 if (!IS_ERR(mmc->supply.vqmmc) && host->vqmmc_enabled) {
Lee Jones237fb5e2013-01-31 11:27:52 +00001570 regulator_disable(mmc->supply.vqmmc);
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001571 host->vqmmc_enabled = false;
1572 }
Lee Jones237fb5e2013-01-31 11:27:52 +00001573
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 break;
1575 case MMC_POWER_UP:
Ulf Hansson599c1d52013-01-07 16:22:50 +01001576 if (!IS_ERR(mmc->supply.vmmc))
1577 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd);
1578
Ulf Hansson7d72a1d2011-12-13 16:54:55 +01001579 /*
1580 * The ST Micro variant doesn't have the PL180s MCI_PWR_UP
1581 * and instead uses MCI_PWR_ON so apply whatever value is
1582 * configured in the variant data.
1583 */
1584 pwr |= variant->pwrreg_powerup;
1585
1586 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 case MMC_POWER_ON:
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001588 if (!IS_ERR(mmc->supply.vqmmc) && !host->vqmmc_enabled) {
Lee Jonesdb90f912013-05-03 12:52:12 +01001589 ret = regulator_enable(mmc->supply.vqmmc);
1590 if (ret < 0)
1591 dev_err(mmc_dev(mmc),
1592 "failed to enable vqmmc regulator\n");
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001593 else
1594 host->vqmmc_enabled = true;
Lee Jonesdb90f912013-05-03 12:52:12 +01001595 }
Lee Jones237fb5e2013-01-31 11:27:52 +00001596
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 pwr |= MCI_PWR_ON;
1598 break;
1599 }
1600
Ulf Hansson4d1a3a02011-12-13 16:57:07 +01001601 if (variant->signal_direction && ios->power_mode != MMC_POWER_OFF) {
1602 /*
1603 * The ST Micro variant has some additional bits
1604 * indicating signal direction for the signals in
1605 * the SD/MMC bus and feedback-clock usage.
1606 */
Ulf Hansson4593df22014-03-21 10:13:05 +01001607 pwr |= host->pwr_reg_add;
Ulf Hansson4d1a3a02011-12-13 16:57:07 +01001608
1609 if (ios->bus_width == MMC_BUS_WIDTH_4)
1610 pwr &= ~MCI_ST_DATA74DIREN;
1611 else if (ios->bus_width == MMC_BUS_WIDTH_1)
1612 pwr &= (~MCI_ST_DATA74DIREN &
1613 ~MCI_ST_DATA31DIREN &
1614 ~MCI_ST_DATA2DIREN);
1615 }
1616
Patrice Chotardf9bb3042018-01-18 15:34:20 +01001617 if (variant->opendrain) {
1618 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
1619 pwr |= variant->opendrain;
1620 } else {
1621 /*
1622 * If the variant cannot configure the pads by its own, then we
1623 * expect the pinctrl to be able to do that for us
1624 */
1625 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
1626 pinctrl_select_state(host->pinctrl, host->pins_opendrain);
1627 else
1628 pinctrl_select_state(host->pinctrl, host->pins_default);
1629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Ulf Hanssonf4670da2013-01-09 17:19:54 +01001631 /*
1632 * If clock = 0 and the variant requires the MMCIPOWER to be used for
1633 * gating the clock, the MCI_PWR_ON bit is cleared.
1634 */
1635 if (!ios->clock && variant->pwrreg_clkgate)
1636 pwr &= ~MCI_PWR_ON;
1637
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001638 if (host->variant->explicit_mclk_control &&
1639 ios->clock != host->clock_cache) {
1640 ret = clk_set_rate(host->clk, ios->clock);
1641 if (ret < 0)
1642 dev_err(mmc_dev(host->mmc),
1643 "Error setting clock rate (%d)\n", ret);
1644 else
1645 host->mclk = clk_get_rate(host->clk);
1646 }
1647 host->clock_cache = ios->clock;
1648
Linus Walleija6a64642009-09-14 12:56:14 +01001649 spin_lock_irqsave(&host->lock, flags);
1650
Ludovic Barrecd3ee8c2018-10-08 14:08:42 +02001651 if (host->ops && host->ops->set_clkreg)
1652 host->ops->set_clkreg(host, ios->clock);
1653 else
1654 mmci_set_clkreg(host, ios->clock);
1655
1656 if (host->ops && host->ops->set_pwrreg)
1657 host->ops->set_pwrreg(host, pwr);
1658 else
1659 mmci_write_pwrreg(host, pwr);
1660
Ulf Hanssonf829c042013-09-04 09:01:15 +01001661 mmci_reg_delay(host);
Linus Walleija6a64642009-09-14 12:56:14 +01001662
1663 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664}
1665
Russell King89001442009-07-09 15:16:07 +01001666static int mmci_get_cd(struct mmc_host *mmc)
1667{
1668 struct mmci_host *host = mmc_priv(mmc);
Rabin Vincent29719442010-08-09 12:54:43 +01001669 struct mmci_platform_data *plat = host->plat;
Ulf Hanssond2762092014-03-17 13:56:19 +01001670 unsigned int status = mmc_gpio_get_cd(mmc);
Russell King89001442009-07-09 15:16:07 +01001671
Ulf Hanssond2762092014-03-17 13:56:19 +01001672 if (status == -ENOSYS) {
Rabin Vincent4b8caec2010-08-09 12:56:40 +01001673 if (!plat->status)
1674 return 1; /* Assume always present */
1675
Rabin Vincent29719442010-08-09 12:54:43 +01001676 status = plat->status(mmc_dev(host->mmc));
Ulf Hanssond2762092014-03-17 13:56:19 +01001677 }
Russell King74bc8092010-07-29 15:58:59 +01001678 return status;
Russell King89001442009-07-09 15:16:07 +01001679}
1680
Ulf Hansson0f3ed7f2013-05-15 20:47:33 +01001681static int mmci_sig_volt_switch(struct mmc_host *mmc, struct mmc_ios *ios)
1682{
1683 int ret = 0;
1684
1685 if (!IS_ERR(mmc->supply.vqmmc)) {
1686
Ulf Hansson0f3ed7f2013-05-15 20:47:33 +01001687 switch (ios->signal_voltage) {
1688 case MMC_SIGNAL_VOLTAGE_330:
1689 ret = regulator_set_voltage(mmc->supply.vqmmc,
1690 2700000, 3600000);
1691 break;
1692 case MMC_SIGNAL_VOLTAGE_180:
1693 ret = regulator_set_voltage(mmc->supply.vqmmc,
1694 1700000, 1950000);
1695 break;
1696 case MMC_SIGNAL_VOLTAGE_120:
1697 ret = regulator_set_voltage(mmc->supply.vqmmc,
1698 1100000, 1300000);
1699 break;
1700 }
1701
1702 if (ret)
1703 dev_warn(mmc_dev(mmc), "Voltage switch failed\n");
Ulf Hansson0f3ed7f2013-05-15 20:47:33 +01001704 }
1705
1706 return ret;
1707}
1708
Ulf Hansson01259622013-05-15 20:53:22 +01001709static struct mmc_host_ops mmci_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 .request = mmci_request,
Per Forlin58c7ccb2011-07-01 18:55:24 +02001711 .pre_req = mmci_pre_request,
1712 .post_req = mmci_post_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 .set_ios = mmci_set_ios,
Ulf Hanssond2762092014-03-17 13:56:19 +01001714 .get_ro = mmc_gpio_get_ro,
Russell King89001442009-07-09 15:16:07 +01001715 .get_cd = mmci_get_cd,
Ulf Hansson0f3ed7f2013-05-15 20:47:33 +01001716 .start_signal_voltage_switch = mmci_sig_volt_switch,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717};
1718
Ulf Hansson78f87df2014-03-17 15:53:07 +01001719static int mmci_of_parse(struct device_node *np, struct mmc_host *mmc)
1720{
Ulf Hansson4593df22014-03-21 10:13:05 +01001721 struct mmci_host *host = mmc_priv(mmc);
Ulf Hansson78f87df2014-03-17 15:53:07 +01001722 int ret = mmc_of_parse(mmc);
Lee Jones000bc9d2012-04-16 10:18:43 +01001723
Ulf Hansson78f87df2014-03-17 15:53:07 +01001724 if (ret)
1725 return ret;
Lee Jones000bc9d2012-04-16 10:18:43 +01001726
Ulf Hansson4593df22014-03-21 10:13:05 +01001727 if (of_get_property(np, "st,sig-dir-dat0", NULL))
1728 host->pwr_reg_add |= MCI_ST_DATA0DIREN;
1729 if (of_get_property(np, "st,sig-dir-dat2", NULL))
1730 host->pwr_reg_add |= MCI_ST_DATA2DIREN;
1731 if (of_get_property(np, "st,sig-dir-dat31", NULL))
1732 host->pwr_reg_add |= MCI_ST_DATA31DIREN;
1733 if (of_get_property(np, "st,sig-dir-dat74", NULL))
1734 host->pwr_reg_add |= MCI_ST_DATA74DIREN;
1735 if (of_get_property(np, "st,sig-dir-cmd", NULL))
1736 host->pwr_reg_add |= MCI_ST_CMDDIREN;
1737 if (of_get_property(np, "st,sig-pin-fbclk", NULL))
1738 host->pwr_reg_add |= MCI_ST_FBCLKEN;
1739
Lee Jones000bc9d2012-04-16 10:18:43 +01001740 if (of_get_property(np, "mmc-cap-mmc-highspeed", NULL))
Ulf Hansson78f87df2014-03-17 15:53:07 +01001741 mmc->caps |= MMC_CAP_MMC_HIGHSPEED;
Lee Jones000bc9d2012-04-16 10:18:43 +01001742 if (of_get_property(np, "mmc-cap-sd-highspeed", NULL))
Ulf Hansson78f87df2014-03-17 15:53:07 +01001743 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
Lee Jones000bc9d2012-04-16 10:18:43 +01001744
Ulf Hansson78f87df2014-03-17 15:53:07 +01001745 return 0;
Lee Jones000bc9d2012-04-16 10:18:43 +01001746}
Lee Jones000bc9d2012-04-16 10:18:43 +01001747
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001748static int mmci_probe(struct amba_device *dev,
Russell Kingaa25afa2011-02-19 15:55:00 +00001749 const struct amba_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750{
Linus Walleij6ef297f2009-09-22 14:29:36 +01001751 struct mmci_platform_data *plat = dev->dev.platform_data;
Lee Jones000bc9d2012-04-16 10:18:43 +01001752 struct device_node *np = dev->dev.of_node;
Rabin Vincent4956e102010-07-21 12:54:40 +01001753 struct variant_data *variant = id->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 struct mmci_host *host;
1755 struct mmc_host *mmc;
1756 int ret;
1757
Lee Jones000bc9d2012-04-16 10:18:43 +01001758 /* Must have platform data or Device Tree. */
1759 if (!plat && !np) {
1760 dev_err(&dev->dev, "No plat data or DT found\n");
1761 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 }
1763
Lee Jonesb9b52912012-06-12 10:49:51 +01001764 if (!plat) {
1765 plat = devm_kzalloc(&dev->dev, sizeof(*plat), GFP_KERNEL);
1766 if (!plat)
1767 return -ENOMEM;
1768 }
1769
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
Ulf Hanssonef289982014-03-17 13:56:32 +01001771 if (!mmc)
1772 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
Ulf Hansson78f87df2014-03-17 15:53:07 +01001774 ret = mmci_of_parse(np, mmc);
1775 if (ret)
1776 goto host_free;
1777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 host = mmc_priv(mmc);
Rabin Vincent4ea580f2009-04-17 08:44:19 +05301779 host->mmc = mmc;
Russell King012b7d32009-07-09 15:13:56 +01001780
Patrice Chotardf9bb3042018-01-18 15:34:20 +01001781 /*
1782 * Some variant (STM32) doesn't have opendrain bit, nevertheless
1783 * pins can be set accordingly using pinctrl
1784 */
1785 if (!variant->opendrain) {
1786 host->pinctrl = devm_pinctrl_get(&dev->dev);
1787 if (IS_ERR(host->pinctrl)) {
1788 dev_err(&dev->dev, "failed to get pinctrl");
Wei Yongjun310eb252018-01-23 02:09:13 +00001789 ret = PTR_ERR(host->pinctrl);
Patrice Chotardf9bb3042018-01-18 15:34:20 +01001790 goto host_free;
1791 }
1792
1793 host->pins_default = pinctrl_lookup_state(host->pinctrl,
1794 PINCTRL_STATE_DEFAULT);
1795 if (IS_ERR(host->pins_default)) {
1796 dev_err(mmc_dev(mmc), "Can't select default pins\n");
Wei Yongjun310eb252018-01-23 02:09:13 +00001797 ret = PTR_ERR(host->pins_default);
Patrice Chotardf9bb3042018-01-18 15:34:20 +01001798 goto host_free;
1799 }
1800
1801 host->pins_opendrain = pinctrl_lookup_state(host->pinctrl,
1802 MMCI_PINCTRL_STATE_OPENDRAIN);
1803 if (IS_ERR(host->pins_opendrain)) {
1804 dev_err(mmc_dev(mmc), "Can't select opendrain pins\n");
Wei Yongjun310eb252018-01-23 02:09:13 +00001805 ret = PTR_ERR(host->pins_opendrain);
Patrice Chotardf9bb3042018-01-18 15:34:20 +01001806 goto host_free;
1807 }
1808 }
1809
Russell King012b7d32009-07-09 15:13:56 +01001810 host->hw_designer = amba_manf(dev);
1811 host->hw_revision = amba_rev(dev);
Linus Walleij64de0282010-02-19 01:09:10 +01001812 dev_dbg(mmc_dev(mmc), "designer ID = 0x%02x\n", host->hw_designer);
1813 dev_dbg(mmc_dev(mmc), "revision = 0x%01x\n", host->hw_revision);
Russell King012b7d32009-07-09 15:13:56 +01001814
Ulf Hansson665ba562013-05-13 15:39:17 +01001815 host->clk = devm_clk_get(&dev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 if (IS_ERR(host->clk)) {
1817 ret = PTR_ERR(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 goto host_free;
1819 }
1820
Julia Lawallac940932012-08-26 16:00:59 +00001821 ret = clk_prepare_enable(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 if (ret)
Ulf Hansson665ba562013-05-13 15:39:17 +01001823 goto host_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
Srinivas Kandagatla9c34b732014-06-02 10:10:04 +01001825 if (variant->qcom_fifo)
1826 host->get_rx_fifocnt = mmci_qcom_get_rx_fifocnt;
1827 else
1828 host->get_rx_fifocnt = mmci_get_rx_fifocnt;
1829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 host->plat = plat;
Rabin Vincent4956e102010-07-21 12:54:40 +01001831 host->variant = variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 host->mclk = clk_get_rate(host->clk);
Linus Walleijc8df9a52008-04-29 09:34:07 +01001833 /*
1834 * According to the spec, mclk is max 100 MHz,
1835 * so we try to adjust the clock down to this,
1836 * (if possible).
1837 */
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +01001838 if (host->mclk > variant->f_max) {
1839 ret = clk_set_rate(host->clk, variant->f_max);
Linus Walleijc8df9a52008-04-29 09:34:07 +01001840 if (ret < 0)
1841 goto clk_disable;
1842 host->mclk = clk_get_rate(host->clk);
Linus Walleij64de0282010-02-19 01:09:10 +01001843 dev_dbg(mmc_dev(mmc), "eventual mclk rate: %u Hz\n",
1844 host->mclk);
Linus Walleijc8df9a52008-04-29 09:34:07 +01001845 }
Ulf Hanssonef289982014-03-17 13:56:32 +01001846
Russell Kingc8ebae32011-01-11 19:35:53 +00001847 host->phybase = dev->res.start;
Ulf Hanssonef289982014-03-17 13:56:32 +01001848 host->base = devm_ioremap_resource(&dev->dev, &dev->res);
1849 if (IS_ERR(host->base)) {
1850 ret = PTR_ERR(host->base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 goto clk_disable;
1852 }
1853
Ulf Hanssoned9067f2018-07-13 13:15:23 +02001854 if (variant->init)
1855 variant->init(host);
1856
Linus Walleij7f294e42011-07-08 09:57:15 +01001857 /*
1858 * The ARM and ST versions of the block have slightly different
1859 * clock divider equations which means that the minimum divider
1860 * differs too.
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001861 * on Qualcomm like controllers get the nearest minimum clock to 100Khz
Linus Walleij7f294e42011-07-08 09:57:15 +01001862 */
1863 if (variant->st_clkdiv)
1864 mmc->f_min = DIV_ROUND_UP(host->mclk, 257);
Ludovic Barre00e930d2018-10-08 14:08:52 +02001865 else if (variant->stm32_clkdiv)
1866 mmc->f_min = DIV_ROUND_UP(host->mclk, 2046);
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001867 else if (variant->explicit_mclk_control)
1868 mmc->f_min = clk_round_rate(host->clk, 100000);
Linus Walleij7f294e42011-07-08 09:57:15 +01001869 else
1870 mmc->f_min = DIV_ROUND_UP(host->mclk, 512);
Linus Walleij808d97c2010-04-08 07:39:38 +01001871 /*
Ulf Hansson78f87df2014-03-17 15:53:07 +01001872 * If no maximum operating frequency is supplied, fall back to use
1873 * the module parameter, which has a (low) default value in case it
1874 * is not specified. Either value must not exceed the clock rate into
Ulf Hansson5080a082014-03-21 10:46:39 +01001875 * the block, of course.
Linus Walleij808d97c2010-04-08 07:39:38 +01001876 */
Ulf Hansson78f87df2014-03-17 15:53:07 +01001877 if (mmc->f_max)
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001878 mmc->f_max = variant->explicit_mclk_control ?
1879 min(variant->f_max, mmc->f_max) :
1880 min(host->mclk, mmc->f_max);
Linus Walleij808d97c2010-04-08 07:39:38 +01001881 else
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001882 mmc->f_max = variant->explicit_mclk_control ?
1883 fmax : min(host->mclk, fmax);
1884
1885
Linus Walleij64de0282010-02-19 01:09:10 +01001886 dev_dbg(mmc_dev(mmc), "clocking block at %u Hz\n", mmc->f_max);
1887
Ludovic Barre15878e52018-10-08 14:08:51 +02001888 host->rst = devm_reset_control_get_optional_exclusive(&dev->dev, NULL);
1889 if (IS_ERR(host->rst)) {
1890 ret = PTR_ERR(host->rst);
1891 goto clk_disable;
1892 }
1893
Ulf Hansson599c1d52013-01-07 16:22:50 +01001894 /* Get regulators and the supported OCR mask */
Bjorn Andersson9369c972015-03-24 18:39:49 -07001895 ret = mmc_regulator_get_supply(mmc);
Wolfram Sang51006952017-10-14 21:17:14 +02001896 if (ret)
Bjorn Andersson9369c972015-03-24 18:39:49 -07001897 goto clk_disable;
1898
Ulf Hansson599c1d52013-01-07 16:22:50 +01001899 if (!mmc->ocr_avail)
Linus Walleij34e84f32009-09-22 14:41:40 +01001900 mmc->ocr_avail = plat->ocr_mask;
Ulf Hansson599c1d52013-01-07 16:22:50 +01001901 else if (plat->ocr_mask)
1902 dev_warn(mmc_dev(mmc), "Platform OCR mask is ignored\n");
1903
Ulf Hansson9dd8a8b2014-03-19 13:54:18 +01001904 /* We support these capabilities. */
1905 mmc->caps |= MMC_CAP_CMD23;
1906
Linus Walleij49adc0c2016-10-25 11:06:06 +02001907 /*
1908 * Enable busy detection.
1909 */
Ulf Hansson8d94b542014-01-13 16:49:31 +01001910 if (variant->busy_detect) {
1911 mmci_ops.card_busy = mmci_card_busy;
Linus Walleij49adc0c2016-10-25 11:06:06 +02001912 /*
1913 * Not all variants have a flag to enable busy detection
1914 * in the DPSM, but if they do, set it here.
1915 */
1916 if (variant->busy_dpsm_flag)
1917 mmci_write_datactrlreg(host,
1918 host->variant->busy_dpsm_flag);
Ulf Hansson8d94b542014-01-13 16:49:31 +01001919 mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY;
1920 mmc->max_busy_timeout = 0;
1921 }
1922
1923 mmc->ops = &mmci_ops;
1924
Ulf Hansson70be2082013-01-07 15:35:06 +01001925 /* We support these PM capabilities. */
Ulf Hansson78f87df2014-03-17 15:53:07 +01001926 mmc->pm_caps |= MMC_PM_KEEP_POWER;
Ulf Hansson70be2082013-01-07 15:35:06 +01001927
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 /*
1929 * We can do SGIO
1930 */
Martin K. Petersena36274e2010-09-10 01:33:59 -04001931 mmc->max_segs = NR_SG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
1933 /*
Rabin Vincent08458ef2010-07-21 12:55:59 +01001934 * Since only a certain number of bits are valid in the data length
1935 * register, we must ensure that we don't exceed 2^num-1 bytes in a
1936 * single request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 */
Rabin Vincent08458ef2010-07-21 12:55:59 +01001938 mmc->max_req_size = (1 << variant->datalength_bits) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
1940 /*
1941 * Set the maximum segment size. Since we aren't doing DMA
1942 * (yet) we are only limited by the data length register.
1943 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001944 mmc->max_seg_size = mmc->max_req_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001946 /*
1947 * Block size can be up to 2048 bytes, but must be a power of two.
1948 */
Ludovic Barrec931d492018-10-08 14:08:43 +02001949 mmc->max_blk_size = 1 << variant->datactrl_blocksz;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001950
Pierre Ossman55db8902006-11-21 17:55:45 +01001951 /*
Will Deacon8f7f6b7e2012-02-24 11:25:21 +00001952 * Limit the number of blocks transferred so that we don't overflow
1953 * the maximum request size.
Pierre Ossman55db8902006-11-21 17:55:45 +01001954 */
Ludovic Barrec931d492018-10-08 14:08:43 +02001955 mmc->max_blk_count = mmc->max_req_size >> variant->datactrl_blocksz;
Pierre Ossman55db8902006-11-21 17:55:45 +01001956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 spin_lock_init(&host->lock);
1958
1959 writel(0, host->base + MMCIMASK0);
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +01001960
1961 if (variant->mmcimask1)
1962 writel(0, host->base + MMCIMASK1);
1963
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 writel(0xfff, host->base + MMCICLEAR);
1965
Linus Walleijce437aa2014-08-27 15:13:54 +02001966 /*
1967 * If:
1968 * - not using DT but using a descriptor table, or
1969 * - using a table of descriptors ALONGSIDE DT, or
1970 * look up these descriptors named "cd" and "wp" right here, fail
Linus Walleij9ef986a2018-09-20 16:01:10 -07001971 * silently of these do not exist
Linus Walleijce437aa2014-08-27 15:13:54 +02001972 */
1973 if (!np) {
Linus Walleij89168b42014-10-02 09:08:46 +02001974 ret = mmc_gpiod_request_cd(mmc, "cd", 0, false, 0, NULL);
Linus Walleij9ef986a2018-09-20 16:01:10 -07001975 if (ret == -EPROBE_DEFER)
1976 goto clk_disable;
Linus Walleijce437aa2014-08-27 15:13:54 +02001977
Linus Walleij89168b42014-10-02 09:08:46 +02001978 ret = mmc_gpiod_request_ro(mmc, "wp", 0, false, 0, NULL);
Linus Walleij9ef986a2018-09-20 16:01:10 -07001979 if (ret == -EPROBE_DEFER)
1980 goto clk_disable;
Russell King89001442009-07-09 15:16:07 +01001981 }
1982
Ulf Hanssonef289982014-03-17 13:56:32 +01001983 ret = devm_request_irq(&dev->dev, dev->irq[0], mmci_irq, IRQF_SHARED,
1984 DRIVER_NAME " (cmd)", host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 if (ret)
Ulf Hanssonef289982014-03-17 13:56:32 +01001986 goto clk_disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
Russell Kingdfb85182012-05-03 11:33:15 +01001988 if (!dev->irq[1])
Linus Walleij2686b4b2010-10-19 12:39:48 +01001989 host->singleirq = true;
1990 else {
Ulf Hanssonef289982014-03-17 13:56:32 +01001991 ret = devm_request_irq(&dev->dev, dev->irq[1], mmci_pio_irq,
1992 IRQF_SHARED, DRIVER_NAME " (pio)", host);
Linus Walleij2686b4b2010-10-19 12:39:48 +01001993 if (ret)
Ulf Hanssonef289982014-03-17 13:56:32 +01001994 goto clk_disable;
Linus Walleij2686b4b2010-10-19 12:39:48 +01001995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
Ludovic Barredaf97132018-10-08 14:08:44 +02001997 writel(MCI_IRQENABLE | variant->start_err, host->base + MMCIMASK0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
1999 amba_set_drvdata(dev, mmc);
2000
Russell Kingc8ebae32011-01-11 19:35:53 +00002001 dev_info(&dev->dev, "%s: PL%03x manf %x rev%u at 0x%08llx irq %d,%d (pio)\n",
2002 mmc_hostname(mmc), amba_part(dev), amba_manf(dev),
2003 amba_rev(dev), (unsigned long long)dev->res.start,
2004 dev->irq[0], dev->irq[1]);
2005
2006 mmci_dma_setup(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Ulf Hansson2cd976c2011-12-13 17:01:11 +01002008 pm_runtime_set_autosuspend_delay(&dev->dev, 50);
2009 pm_runtime_use_autosuspend(&dev->dev);
Russell King1c3be362011-08-14 09:17:05 +01002010
Russell King8c11a942010-12-28 19:40:40 +00002011 mmc_add_host(mmc);
2012
Ulf Hansson6f2d3c82014-12-11 14:35:55 +01002013 pm_runtime_put(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 return 0;
2015
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 clk_disable:
Julia Lawallac940932012-08-26 16:00:59 +00002017 clk_disable_unprepare(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 host_free:
2019 mmc_free_host(mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 return ret;
2021}
2022
Bill Pemberton6e0ee712012-11-19 13:26:03 -05002023static int mmci_remove(struct amba_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024{
2025 struct mmc_host *mmc = amba_get_drvdata(dev);
2026
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 if (mmc) {
2028 struct mmci_host *host = mmc_priv(mmc);
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +01002029 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
Russell King1c3be362011-08-14 09:17:05 +01002031 /*
2032 * Undo pm_runtime_put() in probe. We use the _sync
2033 * version here so that we can access the primecell.
2034 */
2035 pm_runtime_get_sync(&dev->dev);
2036
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 mmc_remove_host(mmc);
2038
2039 writel(0, host->base + MMCIMASK0);
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +01002040
2041 if (variant->mmcimask1)
2042 writel(0, host->base + MMCIMASK1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
2044 writel(0, host->base + MMCICOMMAND);
2045 writel(0, host->base + MMCIDATACTRL);
2046
Russell Kingc8ebae32011-01-11 19:35:53 +00002047 mmci_dma_release(host);
Julia Lawallac940932012-08-26 16:00:59 +00002048 clk_disable_unprepare(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 mmc_free_host(mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 }
2051
2052 return 0;
2053}
2054
Ulf Hansson571dce42014-01-23 00:38:00 +01002055#ifdef CONFIG_PM
Ulf Hansson1ff44432013-09-04 09:05:17 +01002056static void mmci_save(struct mmci_host *host)
2057{
2058 unsigned long flags;
2059
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01002060 spin_lock_irqsave(&host->lock, flags);
Ulf Hansson1ff44432013-09-04 09:05:17 +01002061
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01002062 writel(0, host->base + MMCIMASK0);
2063 if (host->variant->pwrreg_nopower) {
Ulf Hansson1ff44432013-09-04 09:05:17 +01002064 writel(0, host->base + MMCIDATACTRL);
2065 writel(0, host->base + MMCIPOWER);
2066 writel(0, host->base + MMCICLOCK);
Ulf Hansson1ff44432013-09-04 09:05:17 +01002067 }
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01002068 mmci_reg_delay(host);
Ulf Hansson1ff44432013-09-04 09:05:17 +01002069
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01002070 spin_unlock_irqrestore(&host->lock, flags);
Ulf Hansson1ff44432013-09-04 09:05:17 +01002071}
2072
2073static void mmci_restore(struct mmci_host *host)
2074{
2075 unsigned long flags;
2076
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01002077 spin_lock_irqsave(&host->lock, flags);
Ulf Hansson1ff44432013-09-04 09:05:17 +01002078
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01002079 if (host->variant->pwrreg_nopower) {
Ulf Hansson1ff44432013-09-04 09:05:17 +01002080 writel(host->clk_reg, host->base + MMCICLOCK);
2081 writel(host->datactrl_reg, host->base + MMCIDATACTRL);
2082 writel(host->pwr_reg, host->base + MMCIPOWER);
Ulf Hansson1ff44432013-09-04 09:05:17 +01002083 }
Ludovic Barredaf97132018-10-08 14:08:44 +02002084 writel(MCI_IRQENABLE | host->variant->start_err,
2085 host->base + MMCIMASK0);
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01002086 mmci_reg_delay(host);
2087
2088 spin_unlock_irqrestore(&host->lock, flags);
Ulf Hansson1ff44432013-09-04 09:05:17 +01002089}
2090
Ulf Hansson82592932013-01-09 11:15:26 +01002091static int mmci_runtime_suspend(struct device *dev)
2092{
2093 struct amba_device *adev = to_amba_device(dev);
2094 struct mmc_host *mmc = amba_get_drvdata(adev);
2095
2096 if (mmc) {
2097 struct mmci_host *host = mmc_priv(mmc);
Ulf Hanssone36bd9c62013-09-04 09:00:37 +01002098 pinctrl_pm_select_sleep_state(dev);
Ulf Hansson1ff44432013-09-04 09:05:17 +01002099 mmci_save(host);
Ulf Hansson82592932013-01-09 11:15:26 +01002100 clk_disable_unprepare(host->clk);
2101 }
2102
2103 return 0;
2104}
2105
2106static int mmci_runtime_resume(struct device *dev)
2107{
2108 struct amba_device *adev = to_amba_device(dev);
2109 struct mmc_host *mmc = amba_get_drvdata(adev);
2110
2111 if (mmc) {
2112 struct mmci_host *host = mmc_priv(mmc);
2113 clk_prepare_enable(host->clk);
Ulf Hansson1ff44432013-09-04 09:05:17 +01002114 mmci_restore(host);
Ulf Hanssone36bd9c62013-09-04 09:00:37 +01002115 pinctrl_pm_select_default_state(dev);
Ulf Hansson82592932013-01-09 11:15:26 +01002116 }
2117
2118 return 0;
2119}
2120#endif
2121
Ulf Hansson48fa7002011-12-13 16:59:34 +01002122static const struct dev_pm_ops mmci_dev_pm_ops = {
Ulf Hanssonf3737fa2014-01-23 01:11:33 +01002123 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
2124 pm_runtime_force_resume)
Rafael J. Wysocki6ed23b82014-12-04 00:34:11 +01002125 SET_RUNTIME_PM_OPS(mmci_runtime_suspend, mmci_runtime_resume, NULL)
Ulf Hansson48fa7002011-12-13 16:59:34 +01002126};
2127
Arvind Yadav88411de2017-08-23 22:00:49 +05302128static const struct amba_id mmci_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 {
2130 .id = 0x00041180,
Pawel Moll768fbc12011-03-11 17:18:07 +00002131 .mask = 0xff0fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01002132 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 },
2134 {
Pawel Moll768fbc12011-03-11 17:18:07 +00002135 .id = 0x01041180,
2136 .mask = 0xff0fffff,
2137 .data = &variant_arm_extended_fifo,
2138 },
2139 {
Pawel Moll3a372982013-01-24 14:12:45 +01002140 .id = 0x02041180,
2141 .mask = 0xff0fffff,
2142 .data = &variant_arm_extended_fifo_hwfc,
2143 },
2144 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 .id = 0x00041181,
2146 .mask = 0x000fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01002147 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 },
Linus Walleijcc30d602009-01-04 15:18:54 +01002149 /* ST Micro variants */
2150 {
2151 .id = 0x00180180,
2152 .mask = 0x00ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01002153 .data = &variant_u300,
Linus Walleijcc30d602009-01-04 15:18:54 +01002154 },
2155 {
Linus Walleij34fd4212012-04-10 17:43:59 +01002156 .id = 0x10180180,
2157 .mask = 0xf0ffffff,
2158 .data = &variant_nomadik,
2159 },
2160 {
Linus Walleijcc30d602009-01-04 15:18:54 +01002161 .id = 0x00280180,
2162 .mask = 0x00ffffff,
Linus Walleij0bcb7ef2016-01-04 02:21:55 +01002163 .data = &variant_nomadik,
Rabin Vincent4956e102010-07-21 12:54:40 +01002164 },
2165 {
2166 .id = 0x00480180,
Philippe Langlais1784b152011-03-25 08:51:52 +01002167 .mask = 0xf0ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01002168 .data = &variant_ux500,
Linus Walleijcc30d602009-01-04 15:18:54 +01002169 },
Philippe Langlais1784b152011-03-25 08:51:52 +01002170 {
2171 .id = 0x10480180,
2172 .mask = 0xf0ffffff,
2173 .data = &variant_ux500v2,
2174 },
Patrice Chotard2a9d6c82018-01-18 15:34:21 +01002175 {
2176 .id = 0x00880180,
2177 .mask = 0x00ffffff,
2178 .data = &variant_stm32,
2179 },
Srinivas Kandagatla55b604a2014-06-02 10:10:13 +01002180 /* Qualcomm variants */
2181 {
2182 .id = 0x00051180,
2183 .mask = 0x000fffff,
2184 .data = &variant_qcom,
2185 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 { 0, 0 },
2187};
2188
Dave Martin9f998352011-10-05 15:15:21 +01002189MODULE_DEVICE_TABLE(amba, mmci_ids);
2190
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191static struct amba_driver mmci_driver = {
2192 .drv = {
2193 .name = DRIVER_NAME,
Ulf Hansson48fa7002011-12-13 16:59:34 +01002194 .pm = &mmci_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 },
2196 .probe = mmci_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05002197 .remove = mmci_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 .id_table = mmci_ids,
2199};
2200
viresh kumar9e5ed092012-03-15 10:40:38 +01002201module_amba_driver(mmci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203module_param(fmax, uint, 0444);
2204
2205MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
2206MODULE_LICENSE("GPL");