blob: 387ff14587b873cd327e75286235dbbd86069c31 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Pierre Ossman70f10482007-07-11 20:04:50 +02002 * linux/drivers/mmc/host/mmci.c - ARM PrimeCell MMCI PL180/1 driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 2003 Deep Blue Solutions, Ltd, All Rights Reserved.
Russell Kingc8ebae32011-01-11 19:35:53 +00005 * Copyright (C) 2010 ST-Ericsson SA
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
12#include <linux/moduleparam.h>
13#include <linux/init.h>
14#include <linux/ioport.h>
15#include <linux/device.h>
Ulf Hanssonef289982014-03-17 13:56:32 +010016#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/interrupt.h>
Russell King613b1522011-01-30 21:06:53 +000018#include <linux/kernel.h>
Lee Jones000bc9d2012-04-16 10:18:43 +010019#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/delay.h>
21#include <linux/err.h>
22#include <linux/highmem.h>
Nicolas Pitre019a5f52007-10-11 01:06:03 -040023#include <linux/log2.h>
Ludovic Barrec8073e52018-12-06 16:13:31 +010024#include <linux/mmc/mmc.h>
Ulf Hansson70be2082013-01-07 15:35:06 +010025#include <linux/mmc/pm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/mmc/host.h>
Linus Walleij34177802010-10-19 12:43:58 +010027#include <linux/mmc/card.h>
Ulf Hanssond2762092014-03-17 13:56:19 +010028#include <linux/mmc/slot-gpio.h>
Russell Kinga62c80e2006-01-07 13:52:45 +000029#include <linux/amba/bus.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000030#include <linux/clk.h>
Jens Axboebd6dee62007-10-24 09:01:09 +020031#include <linux/scatterlist.h>
Linus Walleij9ef986a2018-09-20 16:01:10 -070032#include <linux/of.h>
Linus Walleij34e84f32009-09-22 14:41:40 +010033#include <linux/regulator/consumer.h>
Russell Kingc8ebae32011-01-11 19:35:53 +000034#include <linux/dmaengine.h>
35#include <linux/dma-mapping.h>
36#include <linux/amba/mmci.h>
Russell King1c3be362011-08-14 09:17:05 +010037#include <linux/pm_runtime.h>
Viresh Kumar258aea72012-02-01 16:12:19 +053038#include <linux/types.h>
Linus Walleija9a83782012-10-29 14:39:30 +010039#include <linux/pinctrl/consumer.h>
Ludovic Barre15878e52018-10-08 14:08:51 +020040#include <linux/reset.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Russell King7b09cda2005-07-01 12:02:59 +010042#include <asm/div64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#include "mmci.h"
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +010046#include "mmci_qcom_dml.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#define DRIVER_NAME "mmci-pl18x"
49
Ludovic Barrec3647fd2018-10-08 14:08:33 +020050#ifdef CONFIG_DMA_ENGINE
51void mmci_variant_init(struct mmci_host *host);
52#else
53static inline void mmci_variant_init(struct mmci_host *host) {}
54#endif
55
Ludovic Barre46b723d2018-10-08 14:08:55 +020056#ifdef CONFIG_MMC_STM32_SDMMC
57void sdmmc_variant_init(struct mmci_host *host);
58#else
59static inline void sdmmc_variant_init(struct mmci_host *host) {}
60#endif
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static unsigned int fmax = 515633;
63
Rabin Vincent4956e102010-07-21 12:54:40 +010064static struct variant_data variant_arm = {
Rabin Vincent8301bb62010-08-09 12:57:30 +010065 .fifosize = 16 * 4,
66 .fifohalfsize = 8 * 4,
Ludovic Barre0f244802018-10-08 14:08:45 +020067 .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
68 .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
69 .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
70 .cmdreg_srsp = MCI_CPSM_RESPONSE,
Rabin Vincent08458ef2010-07-21 12:55:59 +010071 .datalength_bits = 16,
Ludovic Barrec931d492018-10-08 14:08:43 +020072 .datactrl_blocksz = 11,
Ludovic Barre9b279942018-10-08 14:08:46 +020073 .datactrl_dpsm_enable = MCI_DPSM_ENABLE,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +010074 .pwrreg_powerup = MCI_PWR_UP,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +010075 .f_max = 100000000,
Ulf Hansson78782892014-06-13 13:21:38 +020076 .reversed_irq_handling = true,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +010077 .mmcimask1 = true,
Ludovic Barre59db5e22018-10-08 14:08:47 +020078 .irq_pio_mask = MCI_IRQ_PIO_MASK,
Patrice Chotard7f7b5502018-01-18 15:34:18 +010079 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +010080 .opendrain = MCI_ROD,
Ludovic Barrec3647fd2018-10-08 14:08:33 +020081 .init = mmci_variant_init,
Rabin Vincent4956e102010-07-21 12:54:40 +010082};
83
Pawel Moll768fbc12011-03-11 17:18:07 +000084static struct variant_data variant_arm_extended_fifo = {
85 .fifosize = 128 * 4,
86 .fifohalfsize = 64 * 4,
Ludovic Barre0f244802018-10-08 14:08:45 +020087 .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
88 .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
89 .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
90 .cmdreg_srsp = MCI_CPSM_RESPONSE,
Pawel Moll768fbc12011-03-11 17:18:07 +000091 .datalength_bits = 16,
Ludovic Barrec931d492018-10-08 14:08:43 +020092 .datactrl_blocksz = 11,
Ludovic Barre9b279942018-10-08 14:08:46 +020093 .datactrl_dpsm_enable = MCI_DPSM_ENABLE,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +010094 .pwrreg_powerup = MCI_PWR_UP,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +010095 .f_max = 100000000,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +010096 .mmcimask1 = true,
Ludovic Barre59db5e22018-10-08 14:08:47 +020097 .irq_pio_mask = MCI_IRQ_PIO_MASK,
Patrice Chotard7f7b5502018-01-18 15:34:18 +010098 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +010099 .opendrain = MCI_ROD,
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200100 .init = mmci_variant_init,
Pawel Moll768fbc12011-03-11 17:18:07 +0000101};
102
Pawel Moll3a372982013-01-24 14:12:45 +0100103static struct variant_data variant_arm_extended_fifo_hwfc = {
104 .fifosize = 128 * 4,
105 .fifohalfsize = 64 * 4,
106 .clkreg_enable = MCI_ARM_HWFCEN,
Ludovic Barre0f244802018-10-08 14:08:45 +0200107 .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
108 .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
109 .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
110 .cmdreg_srsp = MCI_CPSM_RESPONSE,
Pawel Moll3a372982013-01-24 14:12:45 +0100111 .datalength_bits = 16,
Ludovic Barrec931d492018-10-08 14:08:43 +0200112 .datactrl_blocksz = 11,
Ludovic Barre9b279942018-10-08 14:08:46 +0200113 .datactrl_dpsm_enable = MCI_DPSM_ENABLE,
Pawel Moll3a372982013-01-24 14:12:45 +0100114 .pwrreg_powerup = MCI_PWR_UP,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +0100115 .f_max = 100000000,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100116 .mmcimask1 = true,
Ludovic Barre59db5e22018-10-08 14:08:47 +0200117 .irq_pio_mask = MCI_IRQ_PIO_MASK,
Patrice Chotard7f7b5502018-01-18 15:34:18 +0100118 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +0100119 .opendrain = MCI_ROD,
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200120 .init = mmci_variant_init,
Pawel Moll3a372982013-01-24 14:12:45 +0100121};
122
Rabin Vincent4956e102010-07-21 12:54:40 +0100123static struct variant_data variant_u300 = {
Rabin Vincent8301bb62010-08-09 12:57:30 +0100124 .fifosize = 16 * 4,
125 .fifohalfsize = 8 * 4,
Linus Walleij49ac2152011-03-04 14:54:16 +0100126 .clkreg_enable = MCI_ST_U300_HWFCEN,
Srinivas Kandagatlae1412d82014-06-02 10:09:23 +0100127 .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
Ludovic Barre0f244802018-10-08 14:08:45 +0200128 .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
129 .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
130 .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
131 .cmdreg_srsp = MCI_CPSM_RESPONSE,
Rabin Vincent08458ef2010-07-21 12:55:59 +0100132 .datalength_bits = 16,
Ludovic Barrec931d492018-10-08 14:08:43 +0200133 .datactrl_blocksz = 11,
Ludovic Barre9b279942018-10-08 14:08:46 +0200134 .datactrl_dpsm_enable = MCI_DPSM_ENABLE,
Linus Walleij5db3eee2016-10-25 11:06:05 +0200135 .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
Srinivas Kandagatlac7354132014-08-22 05:55:16 +0100136 .st_sdio = true,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +0100137 .pwrreg_powerup = MCI_PWR_ON,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +0100138 .f_max = 100000000,
Ulf Hansson4d1a3a02011-12-13 16:57:07 +0100139 .signal_direction = true,
Ulf Hanssonf4670da2013-01-09 17:19:54 +0100140 .pwrreg_clkgate = true,
Ulf Hansson1ff44432013-09-04 09:05:17 +0100141 .pwrreg_nopower = true,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100142 .mmcimask1 = true,
Ludovic Barre59db5e22018-10-08 14:08:47 +0200143 .irq_pio_mask = MCI_IRQ_PIO_MASK,
Patrice Chotard7f7b5502018-01-18 15:34:18 +0100144 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +0100145 .opendrain = MCI_OD,
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200146 .init = mmci_variant_init,
Rabin Vincent4956e102010-07-21 12:54:40 +0100147};
148
Linus Walleij34fd4212012-04-10 17:43:59 +0100149static struct variant_data variant_nomadik = {
150 .fifosize = 16 * 4,
151 .fifohalfsize = 8 * 4,
152 .clkreg = MCI_CLK_ENABLE,
Linus Walleijf5abc762016-01-04 02:22:08 +0100153 .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
Ludovic Barre0f244802018-10-08 14:08:45 +0200154 .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
155 .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
156 .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
157 .cmdreg_srsp = MCI_CPSM_RESPONSE,
Linus Walleij34fd4212012-04-10 17:43:59 +0100158 .datalength_bits = 24,
Ludovic Barrec931d492018-10-08 14:08:43 +0200159 .datactrl_blocksz = 11,
Ludovic Barre9b279942018-10-08 14:08:46 +0200160 .datactrl_dpsm_enable = MCI_DPSM_ENABLE,
Linus Walleij5db3eee2016-10-25 11:06:05 +0200161 .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
Srinivas Kandagatlac7354132014-08-22 05:55:16 +0100162 .st_sdio = true,
Linus Walleij34fd4212012-04-10 17:43:59 +0100163 .st_clkdiv = true,
164 .pwrreg_powerup = MCI_PWR_ON,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +0100165 .f_max = 100000000,
Linus Walleij34fd4212012-04-10 17:43:59 +0100166 .signal_direction = true,
Ulf Hanssonf4670da2013-01-09 17:19:54 +0100167 .pwrreg_clkgate = true,
Ulf Hansson1ff44432013-09-04 09:05:17 +0100168 .pwrreg_nopower = true,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100169 .mmcimask1 = true,
Ludovic Barre59db5e22018-10-08 14:08:47 +0200170 .irq_pio_mask = MCI_IRQ_PIO_MASK,
Patrice Chotard7f7b5502018-01-18 15:34:18 +0100171 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +0100172 .opendrain = MCI_OD,
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200173 .init = mmci_variant_init,
Linus Walleij34fd4212012-04-10 17:43:59 +0100174};
175
Rabin Vincent4956e102010-07-21 12:54:40 +0100176static struct variant_data variant_ux500 = {
Rabin Vincent8301bb62010-08-09 12:57:30 +0100177 .fifosize = 30 * 4,
178 .fifohalfsize = 8 * 4,
Rabin Vincent4956e102010-07-21 12:54:40 +0100179 .clkreg = MCI_CLK_ENABLE,
Linus Walleij49ac2152011-03-04 14:54:16 +0100180 .clkreg_enable = MCI_ST_UX500_HWFCEN,
Srinivas Kandagatlae1412d82014-06-02 10:09:23 +0100181 .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
Srinivas Kandagatlae8740642014-06-02 10:09:30 +0100182 .clkreg_neg_edge_enable = MCI_ST_UX500_NEG_EDGE,
Ludovic Barre0f244802018-10-08 14:08:45 +0200183 .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
184 .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
185 .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
186 .cmdreg_srsp = MCI_CPSM_RESPONSE,
Rabin Vincent08458ef2010-07-21 12:55:59 +0100187 .datalength_bits = 24,
Ludovic Barrec931d492018-10-08 14:08:43 +0200188 .datactrl_blocksz = 11,
Ludovic Barre9b279942018-10-08 14:08:46 +0200189 .datactrl_dpsm_enable = MCI_DPSM_ENABLE,
Linus Walleij5db3eee2016-10-25 11:06:05 +0200190 .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
Srinivas Kandagatlac7354132014-08-22 05:55:16 +0100191 .st_sdio = true,
Linus Walleijb70a67f2010-12-06 09:24:14 +0100192 .st_clkdiv = true,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +0100193 .pwrreg_powerup = MCI_PWR_ON,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +0100194 .f_max = 100000000,
Ulf Hansson4d1a3a02011-12-13 16:57:07 +0100195 .signal_direction = true,
Ulf Hanssonf4670da2013-01-09 17:19:54 +0100196 .pwrreg_clkgate = true,
Ulf Hansson01259622013-05-15 20:53:22 +0100197 .busy_detect = true,
Linus Walleij49adc0c2016-10-25 11:06:06 +0200198 .busy_dpsm_flag = MCI_DPSM_ST_BUSYMODE,
199 .busy_detect_flag = MCI_ST_CARDBUSY,
200 .busy_detect_mask = MCI_ST_BUSYENDMASK,
Ulf Hansson1ff44432013-09-04 09:05:17 +0100201 .pwrreg_nopower = true,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100202 .mmcimask1 = true,
Ludovic Barre59db5e22018-10-08 14:08:47 +0200203 .irq_pio_mask = MCI_IRQ_PIO_MASK,
Patrice Chotard7f7b5502018-01-18 15:34:18 +0100204 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +0100205 .opendrain = MCI_OD,
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200206 .init = mmci_variant_init,
Rabin Vincent4956e102010-07-21 12:54:40 +0100207};
Linus Walleijb70a67f2010-12-06 09:24:14 +0100208
Philippe Langlais1784b152011-03-25 08:51:52 +0100209static struct variant_data variant_ux500v2 = {
210 .fifosize = 30 * 4,
211 .fifohalfsize = 8 * 4,
212 .clkreg = MCI_CLK_ENABLE,
213 .clkreg_enable = MCI_ST_UX500_HWFCEN,
Srinivas Kandagatlae1412d82014-06-02 10:09:23 +0100214 .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
Srinivas Kandagatlae8740642014-06-02 10:09:30 +0100215 .clkreg_neg_edge_enable = MCI_ST_UX500_NEG_EDGE,
Ludovic Barre0f244802018-10-08 14:08:45 +0200216 .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
217 .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
218 .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
219 .cmdreg_srsp = MCI_CPSM_RESPONSE,
Linus Walleij5db3eee2016-10-25 11:06:05 +0200220 .datactrl_mask_ddrmode = MCI_DPSM_ST_DDRMODE,
Philippe Langlais1784b152011-03-25 08:51:52 +0100221 .datalength_bits = 24,
Ludovic Barrec931d492018-10-08 14:08:43 +0200222 .datactrl_blocksz = 11,
Ludovic Barre9b279942018-10-08 14:08:46 +0200223 .datactrl_dpsm_enable = MCI_DPSM_ENABLE,
Linus Walleij5db3eee2016-10-25 11:06:05 +0200224 .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
Srinivas Kandagatlac7354132014-08-22 05:55:16 +0100225 .st_sdio = true,
Philippe Langlais1784b152011-03-25 08:51:52 +0100226 .st_clkdiv = true,
227 .blksz_datactrl16 = true,
Ulf Hansson7d72a1d2011-12-13 16:54:55 +0100228 .pwrreg_powerup = MCI_PWR_ON,
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +0100229 .f_max = 100000000,
Ulf Hansson4d1a3a02011-12-13 16:57:07 +0100230 .signal_direction = true,
Ulf Hanssonf4670da2013-01-09 17:19:54 +0100231 .pwrreg_clkgate = true,
Ulf Hansson01259622013-05-15 20:53:22 +0100232 .busy_detect = true,
Linus Walleij49adc0c2016-10-25 11:06:06 +0200233 .busy_dpsm_flag = MCI_DPSM_ST_BUSYMODE,
234 .busy_detect_flag = MCI_ST_CARDBUSY,
235 .busy_detect_mask = MCI_ST_BUSYENDMASK,
Ulf Hansson1ff44432013-09-04 09:05:17 +0100236 .pwrreg_nopower = true,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100237 .mmcimask1 = true,
Ludovic Barre59db5e22018-10-08 14:08:47 +0200238 .irq_pio_mask = MCI_IRQ_PIO_MASK,
Patrice Chotard7f7b5502018-01-18 15:34:18 +0100239 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +0100240 .opendrain = MCI_OD,
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200241 .init = mmci_variant_init,
Philippe Langlais1784b152011-03-25 08:51:52 +0100242};
243
Patrice Chotard2a9d6c82018-01-18 15:34:21 +0100244static struct variant_data variant_stm32 = {
245 .fifosize = 32 * 4,
246 .fifohalfsize = 8 * 4,
247 .clkreg = MCI_CLK_ENABLE,
248 .clkreg_enable = MCI_ST_UX500_HWFCEN,
249 .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
250 .clkreg_neg_edge_enable = MCI_ST_UX500_NEG_EDGE,
Ludovic Barre0f244802018-10-08 14:08:45 +0200251 .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
252 .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
253 .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
254 .cmdreg_srsp = MCI_CPSM_RESPONSE,
Ludovic Barre59db5e22018-10-08 14:08:47 +0200255 .irq_pio_mask = MCI_IRQ_PIO_MASK,
Patrice Chotard2a9d6c82018-01-18 15:34:21 +0100256 .datalength_bits = 24,
Ludovic Barrec931d492018-10-08 14:08:43 +0200257 .datactrl_blocksz = 11,
Ludovic Barre9b279942018-10-08 14:08:46 +0200258 .datactrl_dpsm_enable = MCI_DPSM_ENABLE,
Patrice Chotard2a9d6c82018-01-18 15:34:21 +0100259 .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
260 .st_sdio = true,
261 .st_clkdiv = true,
262 .pwrreg_powerup = MCI_PWR_ON,
263 .f_max = 48000000,
264 .pwrreg_clkgate = true,
265 .pwrreg_nopower = true,
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200266 .init = mmci_variant_init,
Patrice Chotard2a9d6c82018-01-18 15:34:21 +0100267};
268
Ludovic Barre46b723d2018-10-08 14:08:55 +0200269static struct variant_data variant_stm32_sdmmc = {
270 .fifosize = 16 * 4,
271 .fifohalfsize = 8 * 4,
272 .f_max = 208000000,
273 .stm32_clkdiv = true,
274 .cmdreg_cpsm_enable = MCI_CPSM_STM32_ENABLE,
275 .cmdreg_lrsp_crc = MCI_CPSM_STM32_LRSP_CRC,
276 .cmdreg_srsp_crc = MCI_CPSM_STM32_SRSP_CRC,
277 .cmdreg_srsp = MCI_CPSM_STM32_SRSP,
Ludovic Barrec8073e52018-12-06 16:13:31 +0100278 .cmdreg_stop = MCI_CPSM_STM32_CMDSTOP,
Ludovic Barre46b723d2018-10-08 14:08:55 +0200279 .data_cmd_enable = MCI_CPSM_STM32_CMDTRANS,
280 .irq_pio_mask = MCI_IRQ_PIO_STM32_MASK,
281 .datactrl_first = true,
282 .datacnt_useless = true,
283 .datalength_bits = 25,
284 .datactrl_blocksz = 14,
285 .stm32_idmabsize_mask = GENMASK(12, 5),
286 .init = sdmmc_variant_init,
287};
288
Srinivas Kandagatla55b604a2014-06-02 10:10:13 +0100289static struct variant_data variant_qcom = {
290 .fifosize = 16 * 4,
291 .fifohalfsize = 8 * 4,
292 .clkreg = MCI_CLK_ENABLE,
293 .clkreg_enable = MCI_QCOM_CLK_FLOWENA |
294 MCI_QCOM_CLK_SELECT_IN_FBCLK,
295 .clkreg_8bit_bus_enable = MCI_QCOM_CLK_WIDEBUS_8,
296 .datactrl_mask_ddrmode = MCI_QCOM_CLK_SELECT_IN_DDR_MODE,
Ludovic Barre0f244802018-10-08 14:08:45 +0200297 .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
298 .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
299 .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
300 .cmdreg_srsp = MCI_CPSM_RESPONSE,
Linus Walleij5db3eee2016-10-25 11:06:05 +0200301 .data_cmd_enable = MCI_CPSM_QCOM_DATCMD,
Srinivas Kandagatla55b604a2014-06-02 10:10:13 +0100302 .blksz_datactrl4 = true,
303 .datalength_bits = 24,
Ludovic Barrec931d492018-10-08 14:08:43 +0200304 .datactrl_blocksz = 11,
Ludovic Barre9b279942018-10-08 14:08:46 +0200305 .datactrl_dpsm_enable = MCI_DPSM_ENABLE,
Srinivas Kandagatla55b604a2014-06-02 10:10:13 +0100306 .pwrreg_powerup = MCI_PWR_UP,
307 .f_max = 208000000,
308 .explicit_mclk_control = true,
309 .qcom_fifo = true,
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100310 .qcom_dml = true,
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100311 .mmcimask1 = true,
Ludovic Barre59db5e22018-10-08 14:08:47 +0200312 .irq_pio_mask = MCI_IRQ_PIO_MASK,
Patrice Chotard7f7b5502018-01-18 15:34:18 +0100313 .start_err = MCI_STARTBITERR,
Patrice Chotard11dfb972018-01-18 15:34:19 +0100314 .opendrain = MCI_ROD,
Ulf Hansson29aba072018-07-16 13:08:18 +0200315 .init = qcom_variant_init,
Srinivas Kandagatla55b604a2014-06-02 10:10:13 +0100316};
317
Linus Walleij49adc0c2016-10-25 11:06:06 +0200318/* Busy detection for the ST Micro variant */
Ulf Hansson01259622013-05-15 20:53:22 +0100319static int mmci_card_busy(struct mmc_host *mmc)
320{
321 struct mmci_host *host = mmc_priv(mmc);
322 unsigned long flags;
323 int busy = 0;
324
Ulf Hansson01259622013-05-15 20:53:22 +0100325 spin_lock_irqsave(&host->lock, flags);
Linus Walleij49adc0c2016-10-25 11:06:06 +0200326 if (readl(host->base + MMCISTATUS) & host->variant->busy_detect_flag)
Ulf Hansson01259622013-05-15 20:53:22 +0100327 busy = 1;
328 spin_unlock_irqrestore(&host->lock, flags);
329
Ulf Hansson01259622013-05-15 20:53:22 +0100330 return busy;
331}
332
Ulf Hanssonf829c042013-09-04 09:01:15 +0100333static void mmci_reg_delay(struct mmci_host *host)
334{
335 /*
336 * According to the spec, at least three feedback clock cycles
337 * of max 52 MHz must pass between two writes to the MMCICLOCK reg.
338 * Three MCLK clock cycles must pass between two MMCIPOWER reg writes.
339 * Worst delay time during card init is at 100 kHz => 30 us.
340 * Worst delay time when up and running is at 25 MHz => 120 ns.
341 */
342 if (host->cclk < 25000000)
343 udelay(30);
344 else
345 ndelay(120);
346}
347
Ulf Hansson653a7612013-01-21 21:29:34 +0100348/*
Linus Walleija6a64642009-09-14 12:56:14 +0100349 * This must be called with host->lock held
350 */
Ludovic Barrecd3ee8c2018-10-08 14:08:42 +0200351void mmci_write_clkreg(struct mmci_host *host, u32 clk)
Ulf Hansson7437cfa2012-01-18 09:17:27 +0100352{
353 if (host->clk_reg != clk) {
354 host->clk_reg = clk;
355 writel(clk, host->base + MMCICLOCK);
356 }
357}
358
359/*
360 * This must be called with host->lock held
361 */
Ludovic Barrecd3ee8c2018-10-08 14:08:42 +0200362void mmci_write_pwrreg(struct mmci_host *host, u32 pwr)
Ulf Hansson7437cfa2012-01-18 09:17:27 +0100363{
364 if (host->pwr_reg != pwr) {
365 host->pwr_reg = pwr;
366 writel(pwr, host->base + MMCIPOWER);
367 }
368}
369
370/*
371 * This must be called with host->lock held
372 */
Ulf Hansson9cc639a2013-05-15 20:48:23 +0100373static void mmci_write_datactrlreg(struct mmci_host *host, u32 datactrl)
374{
Linus Walleij49adc0c2016-10-25 11:06:06 +0200375 /* Keep busy mode in DPSM if enabled */
376 datactrl |= host->datactrl_reg & host->variant->busy_dpsm_flag;
Ulf Hansson01259622013-05-15 20:53:22 +0100377
Ulf Hansson9cc639a2013-05-15 20:48:23 +0100378 if (host->datactrl_reg != datactrl) {
379 host->datactrl_reg = datactrl;
380 writel(datactrl, host->base + MMCIDATACTRL);
381 }
382}
383
384/*
385 * This must be called with host->lock held
386 */
Linus Walleija6a64642009-09-14 12:56:14 +0100387static void mmci_set_clkreg(struct mmci_host *host, unsigned int desired)
388{
Rabin Vincent4956e102010-07-21 12:54:40 +0100389 struct variant_data *variant = host->variant;
390 u32 clk = variant->clkreg;
Linus Walleija6a64642009-09-14 12:56:14 +0100391
Ulf Hanssonc58a8502013-05-13 15:40:03 +0100392 /* Make sure cclk reflects the current calculated clock */
393 host->cclk = 0;
394
Linus Walleija6a64642009-09-14 12:56:14 +0100395 if (desired) {
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +0100396 if (variant->explicit_mclk_control) {
397 host->cclk = host->mclk;
398 } else if (desired >= host->mclk) {
Linus Walleij991a86e2010-12-10 09:35:53 +0100399 clk = MCI_CLK_BYPASS;
Linus Walleij399bc482011-04-01 07:59:17 +0100400 if (variant->st_clkdiv)
401 clk |= MCI_ST_UX500_NEG_EDGE;
Linus Walleija6a64642009-09-14 12:56:14 +0100402 host->cclk = host->mclk;
Linus Walleijb70a67f2010-12-06 09:24:14 +0100403 } else if (variant->st_clkdiv) {
404 /*
405 * DB8500 TRM says f = mclk / (clkdiv + 2)
406 * => clkdiv = (mclk / f) - 2
407 * Round the divider up so we don't exceed the max
408 * frequency
409 */
410 clk = DIV_ROUND_UP(host->mclk, desired) - 2;
411 if (clk >= 256)
412 clk = 255;
413 host->cclk = host->mclk / (clk + 2);
Linus Walleija6a64642009-09-14 12:56:14 +0100414 } else {
Linus Walleijb70a67f2010-12-06 09:24:14 +0100415 /*
416 * PL180 TRM says f = mclk / (2 * (clkdiv + 1))
417 * => clkdiv = mclk / (2 * f) - 1
418 */
Linus Walleija6a64642009-09-14 12:56:14 +0100419 clk = host->mclk / (2 * desired) - 1;
420 if (clk >= 256)
421 clk = 255;
422 host->cclk = host->mclk / (2 * (clk + 1));
423 }
Rabin Vincent4380c142010-07-21 12:55:18 +0100424
425 clk |= variant->clkreg_enable;
Linus Walleija6a64642009-09-14 12:56:14 +0100426 clk |= MCI_CLK_ENABLE;
427 /* This hasn't proven to be worthwhile */
428 /* clk |= MCI_CLK_PWRSAVE; */
429 }
430
Ulf Hanssonc58a8502013-05-13 15:40:03 +0100431 /* Set actual clock for debug */
432 host->mmc->actual_clock = host->cclk;
433
Linus Walleij9e6c82c2009-09-14 12:57:11 +0100434 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4)
Linus Walleij771dc152010-04-08 07:38:52 +0100435 clk |= MCI_4BIT_BUS;
436 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_8)
Srinivas Kandagatlae1412d82014-06-02 10:09:23 +0100437 clk |= variant->clkreg_8bit_bus_enable;
Linus Walleij9e6c82c2009-09-14 12:57:11 +0100438
Seungwon Jeon6dad6c92014-03-14 21:12:13 +0900439 if (host->mmc->ios.timing == MMC_TIMING_UHS_DDR50 ||
440 host->mmc->ios.timing == MMC_TIMING_MMC_DDR52)
Srinivas Kandagatlae8740642014-06-02 10:09:30 +0100441 clk |= variant->clkreg_neg_edge_enable;
Ulf Hansson6dbb6ee2013-01-07 15:30:44 +0100442
Ulf Hansson7437cfa2012-01-18 09:17:27 +0100443 mmci_write_clkreg(host, clk);
Linus Walleija6a64642009-09-14 12:56:14 +0100444}
445
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200446void mmci_dma_release(struct mmci_host *host)
447{
448 if (host->ops && host->ops->dma_release)
449 host->ops->dma_release(host);
450
451 host->use_dma = false;
452}
453
454void mmci_dma_setup(struct mmci_host *host)
455{
456 if (!host->ops || !host->ops->dma_setup)
457 return;
458
459 if (host->ops->dma_setup(host))
460 return;
461
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200462 /* initialize pre request cookie */
463 host->next_cookie = 1;
464
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200465 host->use_dma = true;
466}
467
Ludovic Barree0da1722018-10-08 14:08:41 +0200468/*
469 * Validate mmc prerequisites
470 */
471static int mmci_validate_data(struct mmci_host *host,
472 struct mmc_data *data)
473{
474 if (!data)
475 return 0;
476
477 if (!is_power_of_2(data->blksz)) {
478 dev_err(mmc_dev(host->mmc),
479 "unsupported block size (%d bytes)\n", data->blksz);
480 return -EINVAL;
481 }
482
483 if (host->ops && host->ops->validate_data)
484 return host->ops->validate_data(host, data);
485
486 return 0;
487}
488
Ludovic Barre47983512018-10-08 14:08:36 +0200489int mmci_prep_data(struct mmci_host *host, struct mmc_data *data, bool next)
490{
491 int err;
492
493 if (!host->ops || !host->ops->prep_data)
494 return 0;
495
496 err = host->ops->prep_data(host, data, next);
497
498 if (next && !err)
499 data->host_cookie = ++host->next_cookie < 0 ?
500 1 : host->next_cookie;
501
502 return err;
503}
504
505void mmci_unprep_data(struct mmci_host *host, struct mmc_data *data,
506 int err)
507{
508 if (host->ops && host->ops->unprep_data)
509 host->ops->unprep_data(host, data, err);
510
511 data->host_cookie = 0;
512}
513
Ludovic Barre02769962018-10-08 14:08:37 +0200514void mmci_get_next_data(struct mmci_host *host, struct mmc_data *data)
515{
516 WARN_ON(data->host_cookie && data->host_cookie != host->next_cookie);
517
518 if (host->ops && host->ops->get_next_data)
519 host->ops->get_next_data(host, data);
520}
521
Ludovic Barre135ea302018-10-08 14:08:38 +0200522int mmci_dma_start(struct mmci_host *host, unsigned int datactrl)
523{
524 struct mmc_data *data = host->data;
525 int ret;
526
527 if (!host->use_dma)
528 return -EINVAL;
529
530 ret = mmci_prep_data(host, data, false);
531 if (ret)
532 return ret;
533
534 if (!host->ops || !host->ops->dma_start)
535 return -EINVAL;
536
537 /* Okay, go for it. */
538 dev_vdbg(mmc_dev(host->mmc),
539 "Submit MMCI DMA job, sglen %d blksz %04x blks %04x flags %08x\n",
540 data->sg_len, data->blksz, data->blocks, data->flags);
541
542 host->ops->dma_start(host, &datactrl);
543
544 /* Trigger the DMA transfer */
545 mmci_write_datactrlreg(host, datactrl);
546
547 /*
548 * Let the MMCI say when the data is ended and it's time
549 * to fire next DMA request. When that happens, MMCI will
550 * call mmci_data_end()
551 */
552 writel(readl(host->base + MMCIMASK0) | MCI_DATAENDMASK,
553 host->base + MMCIMASK0);
554 return 0;
555}
556
Ludovic Barre5a9f10c2018-10-08 14:08:39 +0200557void mmci_dma_finalize(struct mmci_host *host, struct mmc_data *data)
558{
559 if (!host->use_dma)
560 return;
561
562 if (host->ops && host->ops->dma_finalize)
563 host->ops->dma_finalize(host, data);
564}
565
Ludovic Barrecfccc6a2018-10-08 14:08:40 +0200566void mmci_dma_error(struct mmci_host *host)
567{
568 if (!host->use_dma)
569 return;
570
571 if (host->ops && host->ops->dma_error)
572 host->ops->dma_error(host);
573}
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575static void
576mmci_request_end(struct mmci_host *host, struct mmc_request *mrq)
577{
578 writel(0, host->base + MMCICOMMAND);
579
Russell Kinge47c2222007-01-08 16:42:51 +0000580 BUG_ON(host->data);
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 host->mrq = NULL;
583 host->cmd = NULL;
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 mmc_request_done(host->mmc, mrq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587
Linus Walleij2686b4b2010-10-19 12:39:48 +0100588static void mmci_set_mask1(struct mmci_host *host, unsigned int mask)
589{
590 void __iomem *base = host->base;
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100591 struct variant_data *variant = host->variant;
Linus Walleij2686b4b2010-10-19 12:39:48 +0100592
593 if (host->singleirq) {
594 unsigned int mask0 = readl(base + MMCIMASK0);
595
Ludovic Barre59db5e22018-10-08 14:08:47 +0200596 mask0 &= ~variant->irq_pio_mask;
Linus Walleij2686b4b2010-10-19 12:39:48 +0100597 mask0 |= mask;
598
599 writel(mask0, base + MMCIMASK0);
600 }
601
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +0100602 if (variant->mmcimask1)
603 writel(mask, base + MMCIMASK1);
604
605 host->mask1_reg = mask;
Linus Walleij2686b4b2010-10-19 12:39:48 +0100606}
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608static void mmci_stop_data(struct mmci_host *host)
609{
Ulf Hansson9cc639a2013-05-15 20:48:23 +0100610 mmci_write_datactrlreg(host, 0);
Linus Walleij2686b4b2010-10-19 12:39:48 +0100611 mmci_set_mask1(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 host->data = NULL;
613}
614
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +0100615static void mmci_init_sg(struct mmci_host *host, struct mmc_data *data)
616{
617 unsigned int flags = SG_MITER_ATOMIC;
618
619 if (data->flags & MMC_DATA_READ)
620 flags |= SG_MITER_TO_SG;
621 else
622 flags |= SG_MITER_FROM_SG;
623
624 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
625}
626
Russell Kingc8ebae32011-01-11 19:35:53 +0000627/*
628 * All the DMA operation mode stuff goes inside this ifdef.
629 * This assumes that you have a generic DMA device interface,
630 * no custom DMA interfaces are supported.
631 */
632#ifdef CONFIG_DMA_ENGINE
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200633struct mmci_dmae_next {
634 struct dma_async_tx_descriptor *desc;
635 struct dma_chan *chan;
636};
637
638struct mmci_dmae_priv {
639 struct dma_chan *cur;
640 struct dma_chan *rx_channel;
641 struct dma_chan *tx_channel;
642 struct dma_async_tx_descriptor *desc_current;
643 struct mmci_dmae_next next_data;
644};
645
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200646int mmci_dmae_setup(struct mmci_host *host)
Russell Kingc8ebae32011-01-11 19:35:53 +0000647{
Russell Kingc8ebae32011-01-11 19:35:53 +0000648 const char *rxname, *txname;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200649 struct mmci_dmae_priv *dmae;
Russell Kingc8ebae32011-01-11 19:35:53 +0000650
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200651 dmae = devm_kzalloc(mmc_dev(host->mmc), sizeof(*dmae), GFP_KERNEL);
652 if (!dmae)
653 return -ENOMEM;
Russell Kingc8ebae32011-01-11 19:35:53 +0000654
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200655 host->dma_priv = dmae;
656
657 dmae->rx_channel = dma_request_slave_channel(mmc_dev(host->mmc),
658 "rx");
659 dmae->tx_channel = dma_request_slave_channel(mmc_dev(host->mmc),
660 "tx");
Per Forlin58c7ccb2011-07-01 18:55:24 +0200661
Russell Kingc8ebae32011-01-11 19:35:53 +0000662 /*
663 * If only an RX channel is specified, the driver will
664 * attempt to use it bidirectionally, however if it is
665 * is specified but cannot be located, DMA will be disabled.
666 */
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200667 if (dmae->rx_channel && !dmae->tx_channel)
668 dmae->tx_channel = dmae->rx_channel;
Russell Kingc8ebae32011-01-11 19:35:53 +0000669
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200670 if (dmae->rx_channel)
671 rxname = dma_chan_name(dmae->rx_channel);
Russell Kingc8ebae32011-01-11 19:35:53 +0000672 else
673 rxname = "none";
674
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200675 if (dmae->tx_channel)
676 txname = dma_chan_name(dmae->tx_channel);
Russell Kingc8ebae32011-01-11 19:35:53 +0000677 else
678 txname = "none";
679
680 dev_info(mmc_dev(host->mmc), "DMA channels RX %s, TX %s\n",
681 rxname, txname);
682
683 /*
684 * Limit the maximum segment size in any SG entry according to
685 * the parameters of the DMA engine device.
686 */
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200687 if (dmae->tx_channel) {
688 struct device *dev = dmae->tx_channel->device->dev;
Russell Kingc8ebae32011-01-11 19:35:53 +0000689 unsigned int max_seg_size = dma_get_max_seg_size(dev);
690
691 if (max_seg_size < host->mmc->max_seg_size)
692 host->mmc->max_seg_size = max_seg_size;
693 }
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200694 if (dmae->rx_channel) {
695 struct device *dev = dmae->rx_channel->device->dev;
Russell Kingc8ebae32011-01-11 19:35:53 +0000696 unsigned int max_seg_size = dma_get_max_seg_size(dev);
697
698 if (max_seg_size < host->mmc->max_seg_size)
699 host->mmc->max_seg_size = max_seg_size;
700 }
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100701
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200702 if (!dmae->tx_channel || !dmae->rx_channel) {
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200703 mmci_dmae_release(host);
704 return -EINVAL;
705 }
706
707 return 0;
Russell Kingc8ebae32011-01-11 19:35:53 +0000708}
709
710/*
Bill Pemberton6e0ee712012-11-19 13:26:03 -0500711 * This is used in or so inline it
Russell Kingc8ebae32011-01-11 19:35:53 +0000712 * so it can be discarded.
713 */
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200714void mmci_dmae_release(struct mmci_host *host)
Russell Kingc8ebae32011-01-11 19:35:53 +0000715{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200716 struct mmci_dmae_priv *dmae = host->dma_priv;
717
718 if (dmae->rx_channel)
719 dma_release_channel(dmae->rx_channel);
720 if (dmae->tx_channel)
721 dma_release_channel(dmae->tx_channel);
722 dmae->rx_channel = dmae->tx_channel = NULL;
Russell Kingc8ebae32011-01-11 19:35:53 +0000723}
724
725static void mmci_dma_unmap(struct mmci_host *host, struct mmc_data *data)
726{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200727 struct mmci_dmae_priv *dmae = host->dma_priv;
Ulf Hansson653a7612013-01-21 21:29:34 +0100728 struct dma_chan *chan;
Ulf Hansson653a7612013-01-21 21:29:34 +0100729
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200730 if (data->flags & MMC_DATA_READ)
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200731 chan = dmae->rx_channel;
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200732 else
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200733 chan = dmae->tx_channel;
Ulf Hansson653a7612013-01-21 21:29:34 +0100734
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200735 dma_unmap_sg(chan->device->dev, data->sg, data->sg_len,
736 mmc_get_dma_dir(data));
Ulf Hansson653a7612013-01-21 21:29:34 +0100737}
738
Ludovic Barrecfccc6a2018-10-08 14:08:40 +0200739void mmci_dmae_error(struct mmci_host *host)
Ludovic Barre7b2a6d52018-09-21 11:45:55 +0200740{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200741 struct mmci_dmae_priv *dmae = host->dma_priv;
742
Ludovic Barrecfccc6a2018-10-08 14:08:40 +0200743 if (!dma_inprogress(host))
Ludovic Barrecdea1942018-09-21 11:45:56 +0200744 return;
745
Ludovic Barre7b2a6d52018-09-21 11:45:55 +0200746 dev_err(mmc_dev(host->mmc), "error during DMA transfer!\n");
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200747 dmaengine_terminate_all(dmae->cur);
Ludovic Barre7b2a6d52018-09-21 11:45:55 +0200748 host->dma_in_progress = false;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200749 dmae->cur = NULL;
750 dmae->desc_current = NULL;
Ludovic Barre7b2a6d52018-09-21 11:45:55 +0200751 host->data->host_cookie = 0;
752
753 mmci_dma_unmap(host, host->data);
754}
755
Ludovic Barre5a9f10c2018-10-08 14:08:39 +0200756void mmci_dmae_finalize(struct mmci_host *host, struct mmc_data *data)
Ulf Hansson653a7612013-01-21 21:29:34 +0100757{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200758 struct mmci_dmae_priv *dmae = host->dma_priv;
Russell Kingc8ebae32011-01-11 19:35:53 +0000759 u32 status;
760 int i;
761
Ludovic Barre5a9f10c2018-10-08 14:08:39 +0200762 if (!dma_inprogress(host))
Ludovic Barrecdea1942018-09-21 11:45:56 +0200763 return;
764
Russell Kingc8ebae32011-01-11 19:35:53 +0000765 /* Wait up to 1ms for the DMA to complete */
766 for (i = 0; ; i++) {
767 status = readl(host->base + MMCISTATUS);
768 if (!(status & MCI_RXDATAAVLBLMASK) || i >= 100)
769 break;
770 udelay(10);
771 }
772
773 /*
774 * Check to see whether we still have some data left in the FIFO -
775 * this catches DMA controllers which are unable to monitor the
776 * DMALBREQ and DMALSREQ signals while allowing us to DMA to non-
777 * contiguous buffers. On TX, we'll get a FIFO underrun error.
778 */
779 if (status & MCI_RXDATAAVLBLMASK) {
Ludovic Barrecfccc6a2018-10-08 14:08:40 +0200780 mmci_dma_error(host);
Russell Kingc8ebae32011-01-11 19:35:53 +0000781 if (!data->error)
782 data->error = -EIO;
Ludovic Barre7b2a6d52018-09-21 11:45:55 +0200783 } else if (!data->host_cookie) {
Ulf Hansson653a7612013-01-21 21:29:34 +0100784 mmci_dma_unmap(host, data);
Ludovic Barre7b2a6d52018-09-21 11:45:55 +0200785 }
Russell Kingc8ebae32011-01-11 19:35:53 +0000786
787 /*
788 * Use of DMA with scatter-gather is impossible.
789 * Give up with DMA and switch back to PIO mode.
790 */
791 if (status & MCI_RXDATAAVLBLMASK) {
792 dev_err(mmc_dev(host->mmc), "buggy DMA detected. Taking evasive action.\n");
793 mmci_dma_release(host);
794 }
Ulf Hansson653a7612013-01-21 21:29:34 +0100795
Linus Walleije13934b2017-01-27 15:04:54 +0100796 host->dma_in_progress = false;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200797 dmae->cur = NULL;
798 dmae->desc_current = NULL;
Russell Kingc8ebae32011-01-11 19:35:53 +0000799}
800
Ulf Hansson653a7612013-01-21 21:29:34 +0100801/* prepares DMA channel and DMA descriptor, returns non-zero on failure */
Ludovic Barre47983512018-10-08 14:08:36 +0200802static int _mmci_dmae_prep_data(struct mmci_host *host, struct mmc_data *data,
Ulf Hansson653a7612013-01-21 21:29:34 +0100803 struct dma_chan **dma_chan,
804 struct dma_async_tx_descriptor **dma_desc)
Russell Kingc8ebae32011-01-11 19:35:53 +0000805{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200806 struct mmci_dmae_priv *dmae = host->dma_priv;
Russell Kingc8ebae32011-01-11 19:35:53 +0000807 struct variant_data *variant = host->variant;
808 struct dma_slave_config conf = {
809 .src_addr = host->phybase + MMCIFIFO,
810 .dst_addr = host->phybase + MMCIFIFO,
811 .src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
812 .dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
813 .src_maxburst = variant->fifohalfsize >> 2, /* # of words */
814 .dst_maxburst = variant->fifohalfsize >> 2, /* # of words */
Viresh Kumar258aea72012-02-01 16:12:19 +0530815 .device_fc = false,
Russell Kingc8ebae32011-01-11 19:35:53 +0000816 };
Russell Kingc8ebae32011-01-11 19:35:53 +0000817 struct dma_chan *chan;
818 struct dma_device *device;
819 struct dma_async_tx_descriptor *desc;
820 int nr_sg;
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100821 unsigned long flags = DMA_CTRL_ACK;
Russell Kingc8ebae32011-01-11 19:35:53 +0000822
Russell Kingc8ebae32011-01-11 19:35:53 +0000823 if (data->flags & MMC_DATA_READ) {
Vinod Koul05f57992011-10-14 10:45:11 +0530824 conf.direction = DMA_DEV_TO_MEM;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200825 chan = dmae->rx_channel;
Russell Kingc8ebae32011-01-11 19:35:53 +0000826 } else {
Vinod Koul05f57992011-10-14 10:45:11 +0530827 conf.direction = DMA_MEM_TO_DEV;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200828 chan = dmae->tx_channel;
Russell Kingc8ebae32011-01-11 19:35:53 +0000829 }
830
831 /* If there's no DMA channel, fall back to PIO */
832 if (!chan)
833 return -EINVAL;
834
835 /* If less than or equal to the fifo size, don't bother with DMA */
Per Forlin58c7ccb2011-07-01 18:55:24 +0200836 if (data->blksz * data->blocks <= variant->fifosize)
Russell Kingc8ebae32011-01-11 19:35:53 +0000837 return -EINVAL;
838
839 device = chan->device;
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200840 nr_sg = dma_map_sg(device->dev, data->sg, data->sg_len,
841 mmc_get_dma_dir(data));
Russell Kingc8ebae32011-01-11 19:35:53 +0000842 if (nr_sg == 0)
843 return -EINVAL;
844
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100845 if (host->variant->qcom_dml)
846 flags |= DMA_PREP_INTERRUPT;
847
Russell Kingc8ebae32011-01-11 19:35:53 +0000848 dmaengine_slave_config(chan, &conf);
Alexandre Bounine16052822012-03-08 16:11:18 -0500849 desc = dmaengine_prep_slave_sg(chan, data->sg, nr_sg,
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100850 conf.direction, flags);
Russell Kingc8ebae32011-01-11 19:35:53 +0000851 if (!desc)
852 goto unmap_exit;
853
Ulf Hansson653a7612013-01-21 21:29:34 +0100854 *dma_chan = chan;
855 *dma_desc = desc;
Russell Kingc8ebae32011-01-11 19:35:53 +0000856
Per Forlin58c7ccb2011-07-01 18:55:24 +0200857 return 0;
858
859 unmap_exit:
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200860 dma_unmap_sg(device->dev, data->sg, data->sg_len,
861 mmc_get_dma_dir(data));
Per Forlin58c7ccb2011-07-01 18:55:24 +0200862 return -ENOMEM;
863}
864
Ludovic Barre47983512018-10-08 14:08:36 +0200865int mmci_dmae_prep_data(struct mmci_host *host,
866 struct mmc_data *data,
867 bool next)
Ulf Hansson653a7612013-01-21 21:29:34 +0100868{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200869 struct mmci_dmae_priv *dmae = host->dma_priv;
Ludovic Barread7b8912018-10-08 14:08:35 +0200870 struct mmci_dmae_next *nd = &dmae->next_data;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200871
Ludovic Barre47983512018-10-08 14:08:36 +0200872 if (!host->use_dma)
873 return -EINVAL;
874
Ludovic Barread7b8912018-10-08 14:08:35 +0200875 if (next)
Ludovic Barre47983512018-10-08 14:08:36 +0200876 return _mmci_dmae_prep_data(host, data, &nd->chan, &nd->desc);
Ulf Hansson653a7612013-01-21 21:29:34 +0100877 /* Check if next job is already prepared. */
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200878 if (dmae->cur && dmae->desc_current)
Ulf Hansson653a7612013-01-21 21:29:34 +0100879 return 0;
880
881 /* No job were prepared thus do it now. */
Ludovic Barre47983512018-10-08 14:08:36 +0200882 return _mmci_dmae_prep_data(host, data, &dmae->cur,
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200883 &dmae->desc_current);
Ulf Hansson653a7612013-01-21 21:29:34 +0100884}
885
Ludovic Barre135ea302018-10-08 14:08:38 +0200886int mmci_dmae_start(struct mmci_host *host, unsigned int *datactrl)
Per Forlin58c7ccb2011-07-01 18:55:24 +0200887{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200888 struct mmci_dmae_priv *dmae = host->dma_priv;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200889 struct mmc_data *data = host->data;
890
Linus Walleije13934b2017-01-27 15:04:54 +0100891 host->dma_in_progress = true;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200892 dmaengine_submit(dmae->desc_current);
893 dma_async_issue_pending(dmae->cur);
Russell Kingc8ebae32011-01-11 19:35:53 +0000894
Srinivas Kandagatla9cb15142014-07-29 03:50:30 +0100895 if (host->variant->qcom_dml)
896 dml_start_xfer(host, data);
897
Ludovic Barre135ea302018-10-08 14:08:38 +0200898 *datactrl |= MCI_DPSM_DMAENABLE;
Russell Kingc8ebae32011-01-11 19:35:53 +0000899
Russell Kingc8ebae32011-01-11 19:35:53 +0000900 return 0;
Russell Kingc8ebae32011-01-11 19:35:53 +0000901}
Per Forlin58c7ccb2011-07-01 18:55:24 +0200902
Ludovic Barre02769962018-10-08 14:08:37 +0200903void mmci_dmae_get_next_data(struct mmci_host *host, struct mmc_data *data)
Per Forlin58c7ccb2011-07-01 18:55:24 +0200904{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200905 struct mmci_dmae_priv *dmae = host->dma_priv;
906 struct mmci_dmae_next *next = &dmae->next_data;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200907
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200908 if (!host->use_dma)
909 return;
910
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200911 WARN_ON(!data->host_cookie && (next->desc || next->chan));
Per Forlin58c7ccb2011-07-01 18:55:24 +0200912
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200913 dmae->desc_current = next->desc;
914 dmae->cur = next->chan;
915 next->desc = NULL;
916 next->chan = NULL;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200917}
918
Ludovic Barre47983512018-10-08 14:08:36 +0200919void mmci_dmae_unprep_data(struct mmci_host *host,
920 struct mmc_data *data, int err)
921
Per Forlin58c7ccb2011-07-01 18:55:24 +0200922{
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200923 struct mmci_dmae_priv *dmae = host->dma_priv;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200924
Ludovic Barre47983512018-10-08 14:08:36 +0200925 if (!host->use_dma)
Per Forlin58c7ccb2011-07-01 18:55:24 +0200926 return;
927
Ulf Hansson653a7612013-01-21 21:29:34 +0100928 mmci_dma_unmap(host, data);
Per Forlin58c7ccb2011-07-01 18:55:24 +0200929
Ulf Hansson653a7612013-01-21 21:29:34 +0100930 if (err) {
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200931 struct mmci_dmae_next *next = &dmae->next_data;
Ulf Hansson653a7612013-01-21 21:29:34 +0100932 struct dma_chan *chan;
933 if (data->flags & MMC_DATA_READ)
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200934 chan = dmae->rx_channel;
Ulf Hansson653a7612013-01-21 21:29:34 +0100935 else
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200936 chan = dmae->tx_channel;
Ulf Hansson653a7612013-01-21 21:29:34 +0100937 dmaengine_terminate_all(chan);
Per Forlin58c7ccb2011-07-01 18:55:24 +0200938
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200939 if (dmae->desc_current == next->desc)
940 dmae->desc_current = NULL;
Srinivas Kandagatlab5c16a62014-10-08 12:25:17 +0100941
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200942 if (dmae->cur == next->chan) {
Linus Walleije13934b2017-01-27 15:04:54 +0100943 host->dma_in_progress = false;
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200944 dmae->cur = NULL;
Linus Walleije13934b2017-01-27 15:04:54 +0100945 }
Srinivas Kandagatlab5c16a62014-10-08 12:25:17 +0100946
Ludovic Barrea813f2a2018-10-08 14:08:34 +0200947 next->desc = NULL;
948 next->chan = NULL;
Per Forlin58c7ccb2011-07-01 18:55:24 +0200949 }
950}
951
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200952static struct mmci_host_ops mmci_variant_ops = {
Ludovic Barre47983512018-10-08 14:08:36 +0200953 .prep_data = mmci_dmae_prep_data,
954 .unprep_data = mmci_dmae_unprep_data,
Ludovic Barre02769962018-10-08 14:08:37 +0200955 .get_next_data = mmci_dmae_get_next_data,
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200956 .dma_setup = mmci_dmae_setup,
957 .dma_release = mmci_dmae_release,
Ludovic Barre135ea302018-10-08 14:08:38 +0200958 .dma_start = mmci_dmae_start,
Ludovic Barre5a9f10c2018-10-08 14:08:39 +0200959 .dma_finalize = mmci_dmae_finalize,
Ludovic Barrecfccc6a2018-10-08 14:08:40 +0200960 .dma_error = mmci_dmae_error,
Ludovic Barrec3647fd2018-10-08 14:08:33 +0200961};
962
963void mmci_variant_init(struct mmci_host *host)
964{
965 host->ops = &mmci_variant_ops;
966}
Russell Kingc8ebae32011-01-11 19:35:53 +0000967#endif
968
Ludovic Barre47983512018-10-08 14:08:36 +0200969static void mmci_pre_request(struct mmc_host *mmc, struct mmc_request *mrq)
970{
971 struct mmci_host *host = mmc_priv(mmc);
972 struct mmc_data *data = mrq->data;
973
974 if (!data)
975 return;
976
977 WARN_ON(data->host_cookie);
978
979 if (mmci_validate_data(host, data))
980 return;
981
982 mmci_prep_data(host, data, true);
983}
984
985static void mmci_post_request(struct mmc_host *mmc, struct mmc_request *mrq,
986 int err)
987{
988 struct mmci_host *host = mmc_priv(mmc);
989 struct mmc_data *data = mrq->data;
990
991 if (!data || !data->host_cookie)
992 return;
993
994 mmci_unprep_data(host, data, err);
995}
996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997static void mmci_start_data(struct mmci_host *host, struct mmc_data *data)
998{
Rabin Vincent8301bb62010-08-09 12:57:30 +0100999 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 unsigned int datactrl, timeout, irqmask;
Russell King7b09cda2005-07-01 12:02:59 +01001001 unsigned long long clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 void __iomem *base;
Russell King3bc87f22006-08-27 13:51:28 +01001003 int blksz_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Linus Walleij64de0282010-02-19 01:09:10 +01001005 dev_dbg(mmc_dev(host->mmc), "blksz %04x blks %04x flags %08x\n",
1006 data->blksz, data->blocks, data->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
1008 host->data = data;
Rabin Vincent528320d2010-07-21 12:49:49 +01001009 host->size = data->blksz * data->blocks;
Russell King51d43752011-01-27 10:56:52 +00001010 data->bytes_xfered = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Russell King7b09cda2005-07-01 12:02:59 +01001012 clks = (unsigned long long)data->timeout_ns * host->cclk;
Srinivas Kandagatlac4a35762014-06-02 10:08:39 +01001013 do_div(clks, NSEC_PER_SEC);
Russell King7b09cda2005-07-01 12:02:59 +01001014
1015 timeout = data->timeout_clks + (unsigned int)clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 base = host->base;
1018 writel(timeout, base + MMCIDATATIMER);
1019 writel(host->size, base + MMCIDATALENGTH);
1020
Russell King3bc87f22006-08-27 13:51:28 +01001021 blksz_bits = ffs(data->blksz) - 1;
1022 BUG_ON(1 << blksz_bits != data->blksz);
1023
Philippe Langlais1784b152011-03-25 08:51:52 +01001024 if (variant->blksz_datactrl16)
Ludovic Barre9b279942018-10-08 14:08:46 +02001025 datactrl = variant->datactrl_dpsm_enable | (data->blksz << 16);
Srinivas Kandagatlaff783232014-06-02 10:09:06 +01001026 else if (variant->blksz_datactrl4)
Ludovic Barre9b279942018-10-08 14:08:46 +02001027 datactrl = variant->datactrl_dpsm_enable | (data->blksz << 4);
Philippe Langlais1784b152011-03-25 08:51:52 +01001028 else
Ludovic Barre9b279942018-10-08 14:08:46 +02001029 datactrl = variant->datactrl_dpsm_enable | blksz_bits << 4;
Russell Kingc8ebae32011-01-11 19:35:53 +00001030
1031 if (data->flags & MMC_DATA_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 datactrl |= MCI_DPSM_DIRECTION;
Russell Kingc8ebae32011-01-11 19:35:53 +00001033
Srinivas Kandagatlac7354132014-08-22 05:55:16 +01001034 if (host->mmc->card && mmc_card_sdio(host->mmc->card)) {
1035 u32 clk;
Ulf Hansson7258db72011-12-13 17:05:28 +01001036
Srinivas Kandagatlac7354132014-08-22 05:55:16 +01001037 datactrl |= variant->datactrl_mask_sdio;
Ulf Hansson06c1a122012-10-12 14:01:50 +01001038
Srinivas Kandagatlac7354132014-08-22 05:55:16 +01001039 /*
1040 * The ST Micro variant for SDIO small write transfers
1041 * needs to have clock H/W flow control disabled,
1042 * otherwise the transfer will not start. The threshold
1043 * depends on the rate of MCLK.
1044 */
1045 if (variant->st_sdio && data->flags & MMC_DATA_WRITE &&
1046 (host->size < 8 ||
1047 (host->size <= 8 && host->mclk > 50000000)))
1048 clk = host->clk_reg & ~variant->clkreg_enable;
1049 else
1050 clk = host->clk_reg | variant->clkreg_enable;
1051
1052 mmci_write_clkreg(host, clk);
1053 }
Ulf Hansson06c1a122012-10-12 14:01:50 +01001054
Seungwon Jeon6dad6c92014-03-14 21:12:13 +09001055 if (host->mmc->ios.timing == MMC_TIMING_UHS_DDR50 ||
1056 host->mmc->ios.timing == MMC_TIMING_MMC_DDR52)
Srinivas Kandagatlae17dca22014-06-02 10:09:15 +01001057 datactrl |= variant->datactrl_mask_ddrmode;
Ulf Hansson6dbb6ee2013-01-07 15:30:44 +01001058
Russell Kingc8ebae32011-01-11 19:35:53 +00001059 /*
1060 * Attempt to use DMA operation mode, if this
1061 * should fail, fall back to PIO mode
1062 */
Ludovic Barre135ea302018-10-08 14:08:38 +02001063 if (!mmci_dma_start(host, datactrl))
Russell Kingc8ebae32011-01-11 19:35:53 +00001064 return;
1065
1066 /* IRQ mode, map the SG list for CPU reading/writing */
1067 mmci_init_sg(host, data);
1068
1069 if (data->flags & MMC_DATA_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 irqmask = MCI_RXFIFOHALFFULLMASK;
Russell King0425a142006-02-16 16:48:31 +00001071
1072 /*
Russell Kingc4d877c2011-01-27 09:50:13 +00001073 * If we have less than the fifo 'half-full' threshold to
1074 * transfer, trigger a PIO interrupt as soon as any data
1075 * is available.
Russell King0425a142006-02-16 16:48:31 +00001076 */
Russell Kingc4d877c2011-01-27 09:50:13 +00001077 if (host->size < variant->fifohalfsize)
Russell King0425a142006-02-16 16:48:31 +00001078 irqmask |= MCI_RXDATAAVLBLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 } else {
1080 /*
1081 * We don't actually need to include "FIFO empty" here
1082 * since its implicit in "FIFO half empty".
1083 */
1084 irqmask = MCI_TXFIFOHALFEMPTYMASK;
1085 }
1086
Ulf Hansson9cc639a2013-05-15 20:48:23 +01001087 mmci_write_datactrlreg(host, datactrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 writel(readl(base + MMCIMASK0) & ~MCI_DATAENDMASK, base + MMCIMASK0);
Linus Walleij2686b4b2010-10-19 12:39:48 +01001089 mmci_set_mask1(host, irqmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090}
1091
1092static void
1093mmci_start_command(struct mmci_host *host, struct mmc_command *cmd, u32 c)
1094{
1095 void __iomem *base = host->base;
1096
Linus Walleij64de0282010-02-19 01:09:10 +01001097 dev_dbg(mmc_dev(host->mmc), "op %02x arg %08x flags %08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 cmd->opcode, cmd->arg, cmd->flags);
1099
Ludovic Barre0f244802018-10-08 14:08:45 +02001100 if (readl(base + MMCICOMMAND) & host->variant->cmdreg_cpsm_enable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 writel(0, base + MMCICOMMAND);
Srinivas Kandagatla6adb2a82014-06-02 10:08:57 +01001102 mmci_reg_delay(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 }
1104
Ludovic Barrec8073e52018-12-06 16:13:31 +01001105 if (host->variant->cmdreg_stop &&
1106 cmd->opcode == MMC_STOP_TRANSMISSION)
1107 c |= host->variant->cmdreg_stop;
1108
Ludovic Barre0f244802018-10-08 14:08:45 +02001109 c |= cmd->opcode | host->variant->cmdreg_cpsm_enable;
Russell Kinge9225172006-02-02 12:23:12 +00001110 if (cmd->flags & MMC_RSP_PRESENT) {
1111 if (cmd->flags & MMC_RSP_136)
Ludovic Barre0f244802018-10-08 14:08:45 +02001112 c |= host->variant->cmdreg_lrsp_crc;
1113 else if (cmd->flags & MMC_RSP_CRC)
1114 c |= host->variant->cmdreg_srsp_crc;
1115 else
1116 c |= host->variant->cmdreg_srsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 }
1118 if (/*interrupt*/0)
1119 c |= MCI_CPSM_INTERRUPT;
1120
Srinivas Kandagatlaae7b0062014-06-02 10:09:39 +01001121 if (mmc_cmd_type(cmd) == MMC_CMD_ADTC)
1122 c |= host->variant->data_cmd_enable;
1123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 host->cmd = cmd;
1125
1126 writel(cmd->arg, base + MMCIARGUMENT);
1127 writel(c, base + MMCICOMMAND);
1128}
1129
Ulf Hanssone9968c62019-01-29 15:35:56 +01001130static void mmci_stop_command(struct mmci_host *host)
1131{
1132 host->stop_abort.error = 0;
1133 mmci_start_command(host, &host->stop_abort, 0);
1134}
1135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136static void
1137mmci_data_irq(struct mmci_host *host, struct mmc_data *data,
1138 unsigned int status)
1139{
Ludovic Barredaf97132018-10-08 14:08:44 +02001140 unsigned int status_err;
1141
Ulf Hansson1cb9da52014-06-12 14:42:23 +02001142 /* Make sure we have data to handle */
1143 if (!data)
1144 return;
1145
Linus Walleijf20f8f212010-10-19 13:41:24 +01001146 /* First check for errors */
Ludovic Barredaf97132018-10-08 14:08:44 +02001147 status_err = status & (host->variant->start_err |
1148 MCI_DATACRCFAIL | MCI_DATATIMEOUT |
1149 MCI_TXUNDERRUN | MCI_RXOVERRUN);
1150
1151 if (status_err) {
Linus Walleij8cb28152011-01-24 15:22:13 +01001152 u32 remain, success;
Linus Walleijf20f8f212010-10-19 13:41:24 +01001153
Russell Kingc8ebae32011-01-11 19:35:53 +00001154 /* Terminate the DMA transfer */
Ludovic Barrecfccc6a2018-10-08 14:08:40 +02001155 mmci_dma_error(host);
Russell Kingc8ebae32011-01-11 19:35:53 +00001156
Russell Kingc8afc9d2011-02-04 09:19:46 +00001157 /*
1158 * Calculate how far we are into the transfer. Note that
1159 * the data counter gives the number of bytes transferred
1160 * on the MMC bus, not on the host side. On reads, this
1161 * can be as much as a FIFO-worth of data ahead. This
1162 * matters for FIFO overruns only.
1163 */
Ludovic Barreb79220b2018-10-08 14:08:49 +02001164 if (!host->variant->datacnt_useless) {
1165 remain = readl(host->base + MMCIDATACNT);
1166 success = data->blksz * data->blocks - remain;
1167 } else {
1168 success = 0;
1169 }
Linus Walleij8cb28152011-01-24 15:22:13 +01001170
Russell Kingc8afc9d2011-02-04 09:19:46 +00001171 dev_dbg(mmc_dev(host->mmc), "MCI ERROR IRQ, status 0x%08x at 0x%08x\n",
Ludovic Barredaf97132018-10-08 14:08:44 +02001172 status_err, success);
1173 if (status_err & MCI_DATACRCFAIL) {
Linus Walleij8cb28152011-01-24 15:22:13 +01001174 /* Last block was not successful */
Russell Kingc8afc9d2011-02-04 09:19:46 +00001175 success -= 1;
Pierre Ossman17b04292007-07-22 22:18:46 +02001176 data->error = -EILSEQ;
Ludovic Barredaf97132018-10-08 14:08:44 +02001177 } else if (status_err & MCI_DATATIMEOUT) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001178 data->error = -ETIMEDOUT;
Ludovic Barredaf97132018-10-08 14:08:44 +02001179 } else if (status_err & MCI_STARTBITERR) {
Linus Walleij757df742011-06-30 15:10:21 +01001180 data->error = -ECOMM;
Ludovic Barredaf97132018-10-08 14:08:44 +02001181 } else if (status_err & MCI_TXUNDERRUN) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001182 data->error = -EIO;
Ludovic Barredaf97132018-10-08 14:08:44 +02001183 } else if (status_err & MCI_RXOVERRUN) {
Russell Kingc8afc9d2011-02-04 09:19:46 +00001184 if (success > host->variant->fifosize)
1185 success -= host->variant->fifosize;
1186 else
1187 success = 0;
Linus Walleij8cb28152011-01-24 15:22:13 +01001188 data->error = -EIO;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001189 }
Russell King51d43752011-01-27 10:56:52 +00001190 data->bytes_xfered = round_down(success, data->blksz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 }
Linus Walleijf20f8f212010-10-19 13:41:24 +01001192
Linus Walleij8cb28152011-01-24 15:22:13 +01001193 if (status & MCI_DATABLOCKEND)
1194 dev_err(mmc_dev(host->mmc), "stray MCI_DATABLOCKEND interrupt\n");
Linus Walleijf20f8f212010-10-19 13:41:24 +01001195
Russell Kingccff9b52011-01-30 21:03:50 +00001196 if (status & MCI_DATAEND || data->error) {
Ludovic Barrecdea1942018-09-21 11:45:56 +02001197 mmci_dma_finalize(host, data);
1198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 mmci_stop_data(host);
1200
Linus Walleij8cb28152011-01-24 15:22:13 +01001201 if (!data->error)
1202 /* The error clause is handled above, success! */
Russell King51d43752011-01-27 10:56:52 +00001203 data->bytes_xfered = data->blksz * data->blocks;
Linus Walleijf20f8f212010-10-19 13:41:24 +01001204
Ulf Hanssone9968c62019-01-29 15:35:56 +01001205 if (!data->stop) {
1206 if (host->variant->cmdreg_stop && data->error)
1207 mmci_stop_command(host);
1208 else
1209 mmci_request_end(host, data->mrq);
1210 } else if (host->mrq->sbc && !data->error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 mmci_request_end(host, data->mrq);
Ulf Hanssone9968c62019-01-29 15:35:56 +01001212 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 mmci_start_command(host, data->stop, 0);
Ulf Hanssone9968c62019-01-29 15:35:56 +01001214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 }
1216}
1217
1218static void
1219mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
1220 unsigned int status)
1221{
1222 void __iomem *base = host->base;
Linus Walleij49adc0c2016-10-25 11:06:06 +02001223 bool sbc;
Ulf Hanssonad82bfe2014-06-12 15:01:57 +02001224
1225 if (!cmd)
1226 return;
1227
1228 sbc = (cmd == host->mrq->sbc);
Ulf Hanssonad82bfe2014-06-12 15:01:57 +02001229
Linus Walleij49adc0c2016-10-25 11:06:06 +02001230 /*
1231 * We need to be one of these interrupts to be considered worth
1232 * handling. Note that we tag on any latent IRQs postponed
1233 * due to waiting for busy status.
1234 */
1235 if (!((status|host->busy_status) &
1236 (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT|MCI_CMDSENT|MCI_CMDRESPEND)))
Ulf Hanssonad82bfe2014-06-12 15:01:57 +02001237 return;
Ulf Hansson8d94b542014-01-13 16:49:31 +01001238
Linus Walleij49adc0c2016-10-25 11:06:06 +02001239 /*
1240 * ST Micro variant: handle busy detection.
1241 */
1242 if (host->variant->busy_detect) {
1243 bool busy_resp = !!(cmd->flags & MMC_RSP_BUSY);
Ulf Hansson8d94b542014-01-13 16:49:31 +01001244
Linus Walleij49adc0c2016-10-25 11:06:06 +02001245 /* We are busy with a command, return */
1246 if (host->busy_status &&
1247 (status & host->variant->busy_detect_flag))
1248 return;
Ulf Hansson8d94b542014-01-13 16:49:31 +01001249
Linus Walleij49adc0c2016-10-25 11:06:06 +02001250 /*
1251 * We were not busy, but we now got a busy response on
1252 * something that was not an error, and we double-check
1253 * that the special busy status bit is still set before
1254 * proceeding.
1255 */
1256 if (!host->busy_status && busy_resp &&
1257 !(status & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT)) &&
1258 (readl(base + MMCISTATUS) & host->variant->busy_detect_flag)) {
Jean-Nicolas Graux5cad24d2017-02-07 12:12:41 +01001259
1260 /* Clear the busy start IRQ */
1261 writel(host->variant->busy_detect_mask,
1262 host->base + MMCICLEAR);
1263
1264 /* Unmask the busy end IRQ */
Linus Walleij49adc0c2016-10-25 11:06:06 +02001265 writel(readl(base + MMCIMASK0) |
1266 host->variant->busy_detect_mask,
1267 base + MMCIMASK0);
1268 /*
1269 * Now cache the last response status code (until
1270 * the busy bit goes low), and return.
1271 */
1272 host->busy_status =
1273 status & (MCI_CMDSENT|MCI_CMDRESPEND);
1274 return;
1275 }
1276
1277 /*
1278 * At this point we are not busy with a command, we have
Jean-Nicolas Graux5cad24d2017-02-07 12:12:41 +01001279 * not received a new busy request, clear and mask the busy
1280 * end IRQ and fall through to process the IRQ.
Linus Walleij49adc0c2016-10-25 11:06:06 +02001281 */
1282 if (host->busy_status) {
Jean-Nicolas Graux5cad24d2017-02-07 12:12:41 +01001283
1284 writel(host->variant->busy_detect_mask,
1285 host->base + MMCICLEAR);
1286
Linus Walleij49adc0c2016-10-25 11:06:06 +02001287 writel(readl(base + MMCIMASK0) &
1288 ~host->variant->busy_detect_mask,
1289 base + MMCIMASK0);
1290 host->busy_status = 0;
1291 }
Ulf Hansson8d94b542014-01-13 16:49:31 +01001292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 host->cmd = NULL;
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 if (status & MCI_CMDTIMEOUT) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001297 cmd->error = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001299 cmd->error = -EILSEQ;
Russell King - ARM Linux9047b432011-01-11 16:35:56 +00001300 } else {
1301 cmd->resp[0] = readl(base + MMCIRESPONSE0);
1302 cmd->resp[1] = readl(base + MMCIRESPONSE1);
1303 cmd->resp[2] = readl(base + MMCIRESPONSE2);
1304 cmd->resp[3] = readl(base + MMCIRESPONSE3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 }
1306
Ulf Hansson024629c2013-05-13 15:40:56 +01001307 if ((!sbc && !cmd->data) || cmd->error) {
Ulf Hansson3b6e3c72011-12-13 16:58:43 +01001308 if (host->data) {
1309 /* Terminate the DMA transfer */
Ludovic Barrecfccc6a2018-10-08 14:08:40 +02001310 mmci_dma_error(host);
Ludovic Barre7b2a6d52018-09-21 11:45:55 +02001311
Russell Kinge47c2222007-01-08 16:42:51 +00001312 mmci_stop_data(host);
Ulf Hanssone9968c62019-01-29 15:35:56 +01001313 if (host->variant->cmdreg_stop && cmd->error) {
1314 mmci_stop_command(host);
1315 return;
1316 }
Ulf Hansson3b6e3c72011-12-13 16:58:43 +01001317 }
Ulf Hansson024629c2013-05-13 15:40:56 +01001318 mmci_request_end(host, host->mrq);
1319 } else if (sbc) {
1320 mmci_start_command(host, host->mrq->cmd, 0);
Ludovic Barred2141542018-10-08 14:08:48 +02001321 } else if (!host->variant->datactrl_first &&
1322 !(cmd->data->flags & MMC_DATA_READ)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 mmci_start_data(host, cmd->data);
1324 }
1325}
1326
Srinivas Kandagatla9c34b732014-06-02 10:10:04 +01001327static int mmci_get_rx_fifocnt(struct mmci_host *host, u32 status, int remain)
1328{
1329 return remain - (readl(host->base + MMCIFIFOCNT) << 2);
1330}
1331
1332static int mmci_qcom_get_rx_fifocnt(struct mmci_host *host, u32 status, int r)
1333{
1334 /*
1335 * on qcom SDCC4 only 8 words are used in each burst so only 8 addresses
1336 * from the fifo range should be used
1337 */
1338 if (status & MCI_RXFIFOHALFFULL)
1339 return host->variant->fifohalfsize;
1340 else if (status & MCI_RXDATAAVLBL)
1341 return 4;
1342
1343 return 0;
1344}
1345
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346static int mmci_pio_read(struct mmci_host *host, char *buffer, unsigned int remain)
1347{
1348 void __iomem *base = host->base;
1349 char *ptr = buffer;
Srinivas Kandagatla9c34b732014-06-02 10:10:04 +01001350 u32 status = readl(host->base + MMCISTATUS);
Linus Walleij26eed9a2008-04-26 23:39:44 +01001351 int host_remain = host->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 do {
Srinivas Kandagatla9c34b732014-06-02 10:10:04 +01001354 int count = host->get_rx_fifocnt(host, status, host_remain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
1356 if (count > remain)
1357 count = remain;
1358
1359 if (count <= 0)
1360 break;
1361
Ulf Hansson393e5e22011-12-13 17:08:04 +01001362 /*
1363 * SDIO especially may want to send something that is
1364 * not divisible by 4 (as opposed to card sectors
1365 * etc). Therefore make sure to always read the last bytes
1366 * while only doing full 32-bit reads towards the FIFO.
1367 */
1368 if (unlikely(count & 0x3)) {
1369 if (count < 4) {
1370 unsigned char buf[4];
Davide Ciminaghi4b85da02012-12-10 14:47:21 +01001371 ioread32_rep(base + MMCIFIFO, buf, 1);
Ulf Hansson393e5e22011-12-13 17:08:04 +01001372 memcpy(ptr, buf, count);
1373 } else {
Davide Ciminaghi4b85da02012-12-10 14:47:21 +01001374 ioread32_rep(base + MMCIFIFO, ptr, count >> 2);
Ulf Hansson393e5e22011-12-13 17:08:04 +01001375 count &= ~0x3;
1376 }
1377 } else {
Davide Ciminaghi4b85da02012-12-10 14:47:21 +01001378 ioread32_rep(base + MMCIFIFO, ptr, count >> 2);
Ulf Hansson393e5e22011-12-13 17:08:04 +01001379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
1381 ptr += count;
1382 remain -= count;
Linus Walleij26eed9a2008-04-26 23:39:44 +01001383 host_remain -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 if (remain == 0)
1386 break;
1387
1388 status = readl(base + MMCISTATUS);
1389 } while (status & MCI_RXDATAAVLBL);
1390
1391 return ptr - buffer;
1392}
1393
1394static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status)
1395{
Rabin Vincent8301bb62010-08-09 12:57:30 +01001396 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 void __iomem *base = host->base;
1398 char *ptr = buffer;
1399
1400 do {
1401 unsigned int count, maxcnt;
1402
Rabin Vincent8301bb62010-08-09 12:57:30 +01001403 maxcnt = status & MCI_TXFIFOEMPTY ?
1404 variant->fifosize : variant->fifohalfsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 count = min(remain, maxcnt);
1406
Linus Walleij34177802010-10-19 12:43:58 +01001407 /*
Linus Walleij34177802010-10-19 12:43:58 +01001408 * SDIO especially may want to send something that is
1409 * not divisible by 4 (as opposed to card sectors
1410 * etc), and the FIFO only accept full 32-bit writes.
1411 * So compensate by adding +3 on the count, a single
1412 * byte become a 32bit write, 7 bytes will be two
1413 * 32bit writes etc.
1414 */
Davide Ciminaghi4b85da02012-12-10 14:47:21 +01001415 iowrite32_rep(base + MMCIFIFO, ptr, (count + 3) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417 ptr += count;
1418 remain -= count;
1419
1420 if (remain == 0)
1421 break;
1422
1423 status = readl(base + MMCISTATUS);
1424 } while (status & MCI_TXFIFOHALFEMPTY);
1425
1426 return ptr - buffer;
1427}
1428
1429/*
1430 * PIO data transfer IRQ handler.
1431 */
David Howells7d12e782006-10-05 14:55:46 +01001432static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
1434 struct mmci_host *host = dev_id;
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001435 struct sg_mapping_iter *sg_miter = &host->sg_miter;
Rabin Vincent8301bb62010-08-09 12:57:30 +01001436 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 void __iomem *base = host->base;
1438 u32 status;
1439
1440 status = readl(base + MMCISTATUS);
1441
Linus Walleij64de0282010-02-19 01:09:10 +01001442 dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
1444 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 unsigned int remain, len;
1446 char *buffer;
1447
1448 /*
1449 * For write, we only need to test the half-empty flag
1450 * here - if the FIFO is completely empty, then by
1451 * definition it is more than half empty.
1452 *
1453 * For read, check for data available.
1454 */
1455 if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL)))
1456 break;
1457
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001458 if (!sg_miter_next(sg_miter))
1459 break;
1460
1461 buffer = sg_miter->addr;
1462 remain = sg_miter->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
1464 len = 0;
1465 if (status & MCI_RXACTIVE)
1466 len = mmci_pio_read(host, buffer, remain);
1467 if (status & MCI_TXACTIVE)
1468 len = mmci_pio_write(host, buffer, remain, status);
1469
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001470 sg_miter->consumed = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 host->size -= len;
1473 remain -= len;
1474
1475 if (remain)
1476 break;
1477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 status = readl(base + MMCISTATUS);
1479 } while (1);
1480
Rabin Vincent4ce1d6c2010-07-21 12:44:58 +01001481 sg_miter_stop(sg_miter);
1482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 /*
Russell Kingc4d877c2011-01-27 09:50:13 +00001484 * If we have less than the fifo 'half-full' threshold to transfer,
1485 * trigger a PIO interrupt as soon as any data is available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 */
Russell Kingc4d877c2011-01-27 09:50:13 +00001487 if (status & MCI_RXACTIVE && host->size < variant->fifohalfsize)
Linus Walleij2686b4b2010-10-19 12:39:48 +01001488 mmci_set_mask1(host, MCI_RXDATAAVLBLMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
1490 /*
1491 * If we run out of data, disable the data IRQs; this
1492 * prevents a race where the FIFO becomes empty before
1493 * the chip itself has disabled the data path, and
1494 * stops us racing with our data end IRQ.
1495 */
1496 if (host->size == 0) {
Linus Walleij2686b4b2010-10-19 12:39:48 +01001497 mmci_set_mask1(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0);
1499 }
1500
1501 return IRQ_HANDLED;
1502}
1503
1504/*
1505 * Handle completion of command and data transfers.
1506 */
David Howells7d12e782006-10-05 14:55:46 +01001507static irqreturn_t mmci_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
1509 struct mmci_host *host = dev_id;
1510 u32 status;
1511 int ret = 0;
1512
1513 spin_lock(&host->lock);
1514
1515 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 status = readl(host->base + MMCISTATUS);
Linus Walleij2686b4b2010-10-19 12:39:48 +01001517
1518 if (host->singleirq) {
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +01001519 if (status & host->mask1_reg)
Linus Walleij2686b4b2010-10-19 12:39:48 +01001520 mmci_pio_irq(irq, dev_id);
1521
Ludovic Barre59db5e22018-10-08 14:08:47 +02001522 status &= ~host->variant->irq_pio_mask;
Linus Walleij2686b4b2010-10-19 12:39:48 +01001523 }
1524
Ulf Hansson8d94b542014-01-13 16:49:31 +01001525 /*
Jean-Nicolas Graux5cad24d2017-02-07 12:12:41 +01001526 * We intentionally clear the MCI_ST_CARDBUSY IRQ (if it's
1527 * enabled) in mmci_cmd_irq() function where ST Micro busy
1528 * detection variant is handled. Considering the HW seems to be
1529 * triggering the IRQ on both edges while monitoring DAT0 for
1530 * busy completion and that same status bit is used to monitor
1531 * start and end of busy detection, special care must be taken
1532 * to make sure that both start and end interrupts are always
1533 * cleared one after the other.
Ulf Hansson8d94b542014-01-13 16:49:31 +01001534 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 status &= readl(host->base + MMCIMASK0);
Jean-Nicolas Graux5cad24d2017-02-07 12:12:41 +01001536 if (host->variant->busy_detect)
1537 writel(status & ~host->variant->busy_detect_mask,
1538 host->base + MMCICLEAR);
1539 else
1540 writel(status, host->base + MMCICLEAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Linus Walleij64de0282010-02-19 01:09:10 +01001542 dev_dbg(mmc_dev(host->mmc), "irq0 (data+cmd) %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Ulf Hansson78782892014-06-13 13:21:38 +02001544 if (host->variant->reversed_irq_handling) {
1545 mmci_data_irq(host, host->data, status);
1546 mmci_cmd_irq(host, host->cmd, status);
1547 } else {
1548 mmci_cmd_irq(host, host->cmd, status);
1549 mmci_data_irq(host, host->data, status);
1550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Linus Walleij49adc0c2016-10-25 11:06:06 +02001552 /*
1553 * Don't poll for busy completion in irq context.
1554 */
1555 if (host->variant->busy_detect && host->busy_status)
1556 status &= ~host->variant->busy_detect_flag;
Ulf Hansson8d94b542014-01-13 16:49:31 +01001557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 ret = 1;
1559 } while (status);
1560
1561 spin_unlock(&host->lock);
1562
1563 return IRQ_RETVAL(ret);
1564}
1565
1566static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1567{
1568 struct mmci_host *host = mmc_priv(mmc);
Linus Walleij9e943022008-10-24 21:17:50 +01001569 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
1571 WARN_ON(host->mrq != NULL);
1572
Ulf Hansson653a7612013-01-21 21:29:34 +01001573 mrq->cmd->error = mmci_validate_data(host, mrq->data);
1574 if (mrq->cmd->error) {
Pierre Ossman255d01a2007-07-24 20:38:53 +02001575 mmc_request_done(mmc, mrq);
1576 return;
1577 }
1578
Linus Walleij9e943022008-10-24 21:17:50 +01001579 spin_lock_irqsave(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
1581 host->mrq = mrq;
1582
Per Forlin58c7ccb2011-07-01 18:55:24 +02001583 if (mrq->data)
1584 mmci_get_next_data(host, mrq->data);
1585
Ludovic Barred2141542018-10-08 14:08:48 +02001586 if (mrq->data &&
1587 (host->variant->datactrl_first || mrq->data->flags & MMC_DATA_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 mmci_start_data(host, mrq->data);
1589
Ulf Hansson024629c2013-05-13 15:40:56 +01001590 if (mrq->sbc)
1591 mmci_start_command(host, mrq->sbc, 0);
1592 else
1593 mmci_start_command(host, mrq->cmd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Linus Walleij9e943022008-10-24 21:17:50 +01001595 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596}
1597
1598static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1599{
1600 struct mmci_host *host = mmc_priv(mmc);
Ulf Hansson7d72a1d2011-12-13 16:54:55 +01001601 struct variant_data *variant = host->variant;
Linus Walleija6a64642009-09-14 12:56:14 +01001602 u32 pwr = 0;
1603 unsigned long flags;
Lee Jonesdb90f912013-05-03 12:52:12 +01001604 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Ulf Hanssonbc521812011-12-13 16:57:55 +01001606 if (host->plat->ios_handler &&
1607 host->plat->ios_handler(mmc_dev(mmc), ios))
1608 dev_err(mmc_dev(mmc), "platform ios_handler failed\n");
1609
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 switch (ios->power_mode) {
1611 case MMC_POWER_OFF:
Ulf Hansson599c1d52013-01-07 16:22:50 +01001612 if (!IS_ERR(mmc->supply.vmmc))
1613 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Lee Jones237fb5e2013-01-31 11:27:52 +00001614
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001615 if (!IS_ERR(mmc->supply.vqmmc) && host->vqmmc_enabled) {
Lee Jones237fb5e2013-01-31 11:27:52 +00001616 regulator_disable(mmc->supply.vqmmc);
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001617 host->vqmmc_enabled = false;
1618 }
Lee Jones237fb5e2013-01-31 11:27:52 +00001619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 break;
1621 case MMC_POWER_UP:
Ulf Hansson599c1d52013-01-07 16:22:50 +01001622 if (!IS_ERR(mmc->supply.vmmc))
1623 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd);
1624
Ulf Hansson7d72a1d2011-12-13 16:54:55 +01001625 /*
1626 * The ST Micro variant doesn't have the PL180s MCI_PWR_UP
1627 * and instead uses MCI_PWR_ON so apply whatever value is
1628 * configured in the variant data.
1629 */
1630 pwr |= variant->pwrreg_powerup;
1631
1632 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 case MMC_POWER_ON:
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001634 if (!IS_ERR(mmc->supply.vqmmc) && !host->vqmmc_enabled) {
Lee Jonesdb90f912013-05-03 12:52:12 +01001635 ret = regulator_enable(mmc->supply.vqmmc);
1636 if (ret < 0)
1637 dev_err(mmc_dev(mmc),
1638 "failed to enable vqmmc regulator\n");
Ulf Hansson7c0136e2013-05-14 13:53:10 +01001639 else
1640 host->vqmmc_enabled = true;
Lee Jonesdb90f912013-05-03 12:52:12 +01001641 }
Lee Jones237fb5e2013-01-31 11:27:52 +00001642
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 pwr |= MCI_PWR_ON;
1644 break;
1645 }
1646
Ulf Hansson4d1a3a02011-12-13 16:57:07 +01001647 if (variant->signal_direction && ios->power_mode != MMC_POWER_OFF) {
1648 /*
1649 * The ST Micro variant has some additional bits
1650 * indicating signal direction for the signals in
1651 * the SD/MMC bus and feedback-clock usage.
1652 */
Ulf Hansson4593df22014-03-21 10:13:05 +01001653 pwr |= host->pwr_reg_add;
Ulf Hansson4d1a3a02011-12-13 16:57:07 +01001654
1655 if (ios->bus_width == MMC_BUS_WIDTH_4)
1656 pwr &= ~MCI_ST_DATA74DIREN;
1657 else if (ios->bus_width == MMC_BUS_WIDTH_1)
1658 pwr &= (~MCI_ST_DATA74DIREN &
1659 ~MCI_ST_DATA31DIREN &
1660 ~MCI_ST_DATA2DIREN);
1661 }
1662
Patrice Chotardf9bb3042018-01-18 15:34:20 +01001663 if (variant->opendrain) {
1664 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
1665 pwr |= variant->opendrain;
1666 } else {
1667 /*
1668 * If the variant cannot configure the pads by its own, then we
1669 * expect the pinctrl to be able to do that for us
1670 */
1671 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
1672 pinctrl_select_state(host->pinctrl, host->pins_opendrain);
1673 else
1674 pinctrl_select_state(host->pinctrl, host->pins_default);
1675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Ulf Hanssonf4670da2013-01-09 17:19:54 +01001677 /*
1678 * If clock = 0 and the variant requires the MMCIPOWER to be used for
1679 * gating the clock, the MCI_PWR_ON bit is cleared.
1680 */
1681 if (!ios->clock && variant->pwrreg_clkgate)
1682 pwr &= ~MCI_PWR_ON;
1683
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001684 if (host->variant->explicit_mclk_control &&
1685 ios->clock != host->clock_cache) {
1686 ret = clk_set_rate(host->clk, ios->clock);
1687 if (ret < 0)
1688 dev_err(mmc_dev(host->mmc),
1689 "Error setting clock rate (%d)\n", ret);
1690 else
1691 host->mclk = clk_get_rate(host->clk);
1692 }
1693 host->clock_cache = ios->clock;
1694
Linus Walleija6a64642009-09-14 12:56:14 +01001695 spin_lock_irqsave(&host->lock, flags);
1696
Ludovic Barrecd3ee8c2018-10-08 14:08:42 +02001697 if (host->ops && host->ops->set_clkreg)
1698 host->ops->set_clkreg(host, ios->clock);
1699 else
1700 mmci_set_clkreg(host, ios->clock);
1701
1702 if (host->ops && host->ops->set_pwrreg)
1703 host->ops->set_pwrreg(host, pwr);
1704 else
1705 mmci_write_pwrreg(host, pwr);
1706
Ulf Hanssonf829c042013-09-04 09:01:15 +01001707 mmci_reg_delay(host);
Linus Walleija6a64642009-09-14 12:56:14 +01001708
1709 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710}
1711
Russell King89001442009-07-09 15:16:07 +01001712static int mmci_get_cd(struct mmc_host *mmc)
1713{
1714 struct mmci_host *host = mmc_priv(mmc);
Rabin Vincent29719442010-08-09 12:54:43 +01001715 struct mmci_platform_data *plat = host->plat;
Ulf Hanssond2762092014-03-17 13:56:19 +01001716 unsigned int status = mmc_gpio_get_cd(mmc);
Russell King89001442009-07-09 15:16:07 +01001717
Ulf Hanssond2762092014-03-17 13:56:19 +01001718 if (status == -ENOSYS) {
Rabin Vincent4b8caec2010-08-09 12:56:40 +01001719 if (!plat->status)
1720 return 1; /* Assume always present */
1721
Rabin Vincent29719442010-08-09 12:54:43 +01001722 status = plat->status(mmc_dev(host->mmc));
Ulf Hanssond2762092014-03-17 13:56:19 +01001723 }
Russell King74bc8092010-07-29 15:58:59 +01001724 return status;
Russell King89001442009-07-09 15:16:07 +01001725}
1726
Ulf Hansson0f3ed7f2013-05-15 20:47:33 +01001727static int mmci_sig_volt_switch(struct mmc_host *mmc, struct mmc_ios *ios)
1728{
1729 int ret = 0;
1730
1731 if (!IS_ERR(mmc->supply.vqmmc)) {
1732
Ulf Hansson0f3ed7f2013-05-15 20:47:33 +01001733 switch (ios->signal_voltage) {
1734 case MMC_SIGNAL_VOLTAGE_330:
1735 ret = regulator_set_voltage(mmc->supply.vqmmc,
1736 2700000, 3600000);
1737 break;
1738 case MMC_SIGNAL_VOLTAGE_180:
1739 ret = regulator_set_voltage(mmc->supply.vqmmc,
1740 1700000, 1950000);
1741 break;
1742 case MMC_SIGNAL_VOLTAGE_120:
1743 ret = regulator_set_voltage(mmc->supply.vqmmc,
1744 1100000, 1300000);
1745 break;
1746 }
1747
1748 if (ret)
1749 dev_warn(mmc_dev(mmc), "Voltage switch failed\n");
Ulf Hansson0f3ed7f2013-05-15 20:47:33 +01001750 }
1751
1752 return ret;
1753}
1754
Ulf Hansson01259622013-05-15 20:53:22 +01001755static struct mmc_host_ops mmci_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 .request = mmci_request,
Per Forlin58c7ccb2011-07-01 18:55:24 +02001757 .pre_req = mmci_pre_request,
1758 .post_req = mmci_post_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 .set_ios = mmci_set_ios,
Ulf Hanssond2762092014-03-17 13:56:19 +01001760 .get_ro = mmc_gpio_get_ro,
Russell King89001442009-07-09 15:16:07 +01001761 .get_cd = mmci_get_cd,
Ulf Hansson0f3ed7f2013-05-15 20:47:33 +01001762 .start_signal_voltage_switch = mmci_sig_volt_switch,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763};
1764
Ulf Hansson78f87df2014-03-17 15:53:07 +01001765static int mmci_of_parse(struct device_node *np, struct mmc_host *mmc)
1766{
Ulf Hansson4593df22014-03-21 10:13:05 +01001767 struct mmci_host *host = mmc_priv(mmc);
Ulf Hansson78f87df2014-03-17 15:53:07 +01001768 int ret = mmc_of_parse(mmc);
Lee Jones000bc9d2012-04-16 10:18:43 +01001769
Ulf Hansson78f87df2014-03-17 15:53:07 +01001770 if (ret)
1771 return ret;
Lee Jones000bc9d2012-04-16 10:18:43 +01001772
Ulf Hansson4593df22014-03-21 10:13:05 +01001773 if (of_get_property(np, "st,sig-dir-dat0", NULL))
1774 host->pwr_reg_add |= MCI_ST_DATA0DIREN;
1775 if (of_get_property(np, "st,sig-dir-dat2", NULL))
1776 host->pwr_reg_add |= MCI_ST_DATA2DIREN;
1777 if (of_get_property(np, "st,sig-dir-dat31", NULL))
1778 host->pwr_reg_add |= MCI_ST_DATA31DIREN;
1779 if (of_get_property(np, "st,sig-dir-dat74", NULL))
1780 host->pwr_reg_add |= MCI_ST_DATA74DIREN;
1781 if (of_get_property(np, "st,sig-dir-cmd", NULL))
1782 host->pwr_reg_add |= MCI_ST_CMDDIREN;
1783 if (of_get_property(np, "st,sig-pin-fbclk", NULL))
1784 host->pwr_reg_add |= MCI_ST_FBCLKEN;
Ludovic Barre46b723d2018-10-08 14:08:55 +02001785 if (of_get_property(np, "st,sig-dir", NULL))
1786 host->pwr_reg_add |= MCI_STM32_DIRPOL;
1787 if (of_get_property(np, "st,neg-edge", NULL))
1788 host->clk_reg_add |= MCI_STM32_CLK_NEGEDGE;
1789 if (of_get_property(np, "st,use-ckin", NULL))
1790 host->clk_reg_add |= MCI_STM32_CLK_SELCKIN;
Ulf Hansson4593df22014-03-21 10:13:05 +01001791
Lee Jones000bc9d2012-04-16 10:18:43 +01001792 if (of_get_property(np, "mmc-cap-mmc-highspeed", NULL))
Ulf Hansson78f87df2014-03-17 15:53:07 +01001793 mmc->caps |= MMC_CAP_MMC_HIGHSPEED;
Lee Jones000bc9d2012-04-16 10:18:43 +01001794 if (of_get_property(np, "mmc-cap-sd-highspeed", NULL))
Ulf Hansson78f87df2014-03-17 15:53:07 +01001795 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
Lee Jones000bc9d2012-04-16 10:18:43 +01001796
Ulf Hansson78f87df2014-03-17 15:53:07 +01001797 return 0;
Lee Jones000bc9d2012-04-16 10:18:43 +01001798}
Lee Jones000bc9d2012-04-16 10:18:43 +01001799
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001800static int mmci_probe(struct amba_device *dev,
Russell Kingaa25afa2011-02-19 15:55:00 +00001801 const struct amba_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802{
Linus Walleij6ef297f2009-09-22 14:29:36 +01001803 struct mmci_platform_data *plat = dev->dev.platform_data;
Lee Jones000bc9d2012-04-16 10:18:43 +01001804 struct device_node *np = dev->dev.of_node;
Rabin Vincent4956e102010-07-21 12:54:40 +01001805 struct variant_data *variant = id->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 struct mmci_host *host;
1807 struct mmc_host *mmc;
1808 int ret;
1809
Lee Jones000bc9d2012-04-16 10:18:43 +01001810 /* Must have platform data or Device Tree. */
1811 if (!plat && !np) {
1812 dev_err(&dev->dev, "No plat data or DT found\n");
1813 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 }
1815
Lee Jonesb9b52912012-06-12 10:49:51 +01001816 if (!plat) {
1817 plat = devm_kzalloc(&dev->dev, sizeof(*plat), GFP_KERNEL);
1818 if (!plat)
1819 return -ENOMEM;
1820 }
1821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
Ulf Hanssonef289982014-03-17 13:56:32 +01001823 if (!mmc)
1824 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Ulf Hansson78f87df2014-03-17 15:53:07 +01001826 ret = mmci_of_parse(np, mmc);
1827 if (ret)
1828 goto host_free;
1829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 host = mmc_priv(mmc);
Rabin Vincent4ea580f2009-04-17 08:44:19 +05301831 host->mmc = mmc;
Russell King012b7d32009-07-09 15:13:56 +01001832
Patrice Chotardf9bb3042018-01-18 15:34:20 +01001833 /*
1834 * Some variant (STM32) doesn't have opendrain bit, nevertheless
1835 * pins can be set accordingly using pinctrl
1836 */
1837 if (!variant->opendrain) {
1838 host->pinctrl = devm_pinctrl_get(&dev->dev);
1839 if (IS_ERR(host->pinctrl)) {
1840 dev_err(&dev->dev, "failed to get pinctrl");
Wei Yongjun310eb252018-01-23 02:09:13 +00001841 ret = PTR_ERR(host->pinctrl);
Patrice Chotardf9bb3042018-01-18 15:34:20 +01001842 goto host_free;
1843 }
1844
1845 host->pins_default = pinctrl_lookup_state(host->pinctrl,
1846 PINCTRL_STATE_DEFAULT);
1847 if (IS_ERR(host->pins_default)) {
1848 dev_err(mmc_dev(mmc), "Can't select default pins\n");
Wei Yongjun310eb252018-01-23 02:09:13 +00001849 ret = PTR_ERR(host->pins_default);
Patrice Chotardf9bb3042018-01-18 15:34:20 +01001850 goto host_free;
1851 }
1852
1853 host->pins_opendrain = pinctrl_lookup_state(host->pinctrl,
1854 MMCI_PINCTRL_STATE_OPENDRAIN);
1855 if (IS_ERR(host->pins_opendrain)) {
1856 dev_err(mmc_dev(mmc), "Can't select opendrain pins\n");
Wei Yongjun310eb252018-01-23 02:09:13 +00001857 ret = PTR_ERR(host->pins_opendrain);
Patrice Chotardf9bb3042018-01-18 15:34:20 +01001858 goto host_free;
1859 }
1860 }
1861
Russell King012b7d32009-07-09 15:13:56 +01001862 host->hw_designer = amba_manf(dev);
1863 host->hw_revision = amba_rev(dev);
Linus Walleij64de0282010-02-19 01:09:10 +01001864 dev_dbg(mmc_dev(mmc), "designer ID = 0x%02x\n", host->hw_designer);
1865 dev_dbg(mmc_dev(mmc), "revision = 0x%01x\n", host->hw_revision);
Russell King012b7d32009-07-09 15:13:56 +01001866
Ulf Hansson665ba562013-05-13 15:39:17 +01001867 host->clk = devm_clk_get(&dev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 if (IS_ERR(host->clk)) {
1869 ret = PTR_ERR(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 goto host_free;
1871 }
1872
Julia Lawallac940932012-08-26 16:00:59 +00001873 ret = clk_prepare_enable(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 if (ret)
Ulf Hansson665ba562013-05-13 15:39:17 +01001875 goto host_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
Srinivas Kandagatla9c34b732014-06-02 10:10:04 +01001877 if (variant->qcom_fifo)
1878 host->get_rx_fifocnt = mmci_qcom_get_rx_fifocnt;
1879 else
1880 host->get_rx_fifocnt = mmci_get_rx_fifocnt;
1881
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 host->plat = plat;
Rabin Vincent4956e102010-07-21 12:54:40 +01001883 host->variant = variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 host->mclk = clk_get_rate(host->clk);
Linus Walleijc8df9a52008-04-29 09:34:07 +01001885 /*
1886 * According to the spec, mclk is max 100 MHz,
1887 * so we try to adjust the clock down to this,
1888 * (if possible).
1889 */
Srinivas Kandagatladc6500b2014-06-02 10:09:47 +01001890 if (host->mclk > variant->f_max) {
1891 ret = clk_set_rate(host->clk, variant->f_max);
Linus Walleijc8df9a52008-04-29 09:34:07 +01001892 if (ret < 0)
1893 goto clk_disable;
1894 host->mclk = clk_get_rate(host->clk);
Linus Walleij64de0282010-02-19 01:09:10 +01001895 dev_dbg(mmc_dev(mmc), "eventual mclk rate: %u Hz\n",
1896 host->mclk);
Linus Walleijc8df9a52008-04-29 09:34:07 +01001897 }
Ulf Hanssonef289982014-03-17 13:56:32 +01001898
Russell Kingc8ebae32011-01-11 19:35:53 +00001899 host->phybase = dev->res.start;
Ulf Hanssonef289982014-03-17 13:56:32 +01001900 host->base = devm_ioremap_resource(&dev->dev, &dev->res);
1901 if (IS_ERR(host->base)) {
1902 ret = PTR_ERR(host->base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 goto clk_disable;
1904 }
1905
Ulf Hanssoned9067f2018-07-13 13:15:23 +02001906 if (variant->init)
1907 variant->init(host);
1908
Linus Walleij7f294e42011-07-08 09:57:15 +01001909 /*
1910 * The ARM and ST versions of the block have slightly different
1911 * clock divider equations which means that the minimum divider
1912 * differs too.
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001913 * on Qualcomm like controllers get the nearest minimum clock to 100Khz
Linus Walleij7f294e42011-07-08 09:57:15 +01001914 */
1915 if (variant->st_clkdiv)
1916 mmc->f_min = DIV_ROUND_UP(host->mclk, 257);
Ludovic Barre00e930d2018-10-08 14:08:52 +02001917 else if (variant->stm32_clkdiv)
1918 mmc->f_min = DIV_ROUND_UP(host->mclk, 2046);
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001919 else if (variant->explicit_mclk_control)
1920 mmc->f_min = clk_round_rate(host->clk, 100000);
Linus Walleij7f294e42011-07-08 09:57:15 +01001921 else
1922 mmc->f_min = DIV_ROUND_UP(host->mclk, 512);
Linus Walleij808d97c2010-04-08 07:39:38 +01001923 /*
Ulf Hansson78f87df2014-03-17 15:53:07 +01001924 * If no maximum operating frequency is supplied, fall back to use
1925 * the module parameter, which has a (low) default value in case it
1926 * is not specified. Either value must not exceed the clock rate into
Ulf Hansson5080a082014-03-21 10:46:39 +01001927 * the block, of course.
Linus Walleij808d97c2010-04-08 07:39:38 +01001928 */
Ulf Hansson78f87df2014-03-17 15:53:07 +01001929 if (mmc->f_max)
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001930 mmc->f_max = variant->explicit_mclk_control ?
1931 min(variant->f_max, mmc->f_max) :
1932 min(host->mclk, mmc->f_max);
Linus Walleij808d97c2010-04-08 07:39:38 +01001933 else
Srinivas Kandagatla3f4e6f72014-06-02 10:09:55 +01001934 mmc->f_max = variant->explicit_mclk_control ?
1935 fmax : min(host->mclk, fmax);
1936
1937
Linus Walleij64de0282010-02-19 01:09:10 +01001938 dev_dbg(mmc_dev(mmc), "clocking block at %u Hz\n", mmc->f_max);
1939
Ludovic Barre15878e52018-10-08 14:08:51 +02001940 host->rst = devm_reset_control_get_optional_exclusive(&dev->dev, NULL);
1941 if (IS_ERR(host->rst)) {
1942 ret = PTR_ERR(host->rst);
1943 goto clk_disable;
1944 }
1945
Ulf Hansson599c1d52013-01-07 16:22:50 +01001946 /* Get regulators and the supported OCR mask */
Bjorn Andersson9369c972015-03-24 18:39:49 -07001947 ret = mmc_regulator_get_supply(mmc);
Wolfram Sang510069522017-10-14 21:17:14 +02001948 if (ret)
Bjorn Andersson9369c972015-03-24 18:39:49 -07001949 goto clk_disable;
1950
Ulf Hansson599c1d52013-01-07 16:22:50 +01001951 if (!mmc->ocr_avail)
Linus Walleij34e84f32009-09-22 14:41:40 +01001952 mmc->ocr_avail = plat->ocr_mask;
Ulf Hansson599c1d52013-01-07 16:22:50 +01001953 else if (plat->ocr_mask)
1954 dev_warn(mmc_dev(mmc), "Platform OCR mask is ignored\n");
1955
Ulf Hansson9dd8a8b2014-03-19 13:54:18 +01001956 /* We support these capabilities. */
1957 mmc->caps |= MMC_CAP_CMD23;
1958
Linus Walleij49adc0c2016-10-25 11:06:06 +02001959 /*
1960 * Enable busy detection.
1961 */
Ulf Hansson8d94b542014-01-13 16:49:31 +01001962 if (variant->busy_detect) {
1963 mmci_ops.card_busy = mmci_card_busy;
Linus Walleij49adc0c2016-10-25 11:06:06 +02001964 /*
1965 * Not all variants have a flag to enable busy detection
1966 * in the DPSM, but if they do, set it here.
1967 */
1968 if (variant->busy_dpsm_flag)
1969 mmci_write_datactrlreg(host,
1970 host->variant->busy_dpsm_flag);
Ulf Hansson8d94b542014-01-13 16:49:31 +01001971 mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY;
1972 mmc->max_busy_timeout = 0;
1973 }
1974
Ulf Hanssone9968c62019-01-29 15:35:56 +01001975 /* Prepare a CMD12 - needed to clear the DPSM on some variants. */
1976 host->stop_abort.opcode = MMC_STOP_TRANSMISSION;
1977 host->stop_abort.arg = 0;
1978 host->stop_abort.flags = MMC_RSP_R1B | MMC_CMD_AC;
1979
Ulf Hansson8d94b542014-01-13 16:49:31 +01001980 mmc->ops = &mmci_ops;
1981
Ulf Hansson70be2082013-01-07 15:35:06 +01001982 /* We support these PM capabilities. */
Ulf Hansson78f87df2014-03-17 15:53:07 +01001983 mmc->pm_caps |= MMC_PM_KEEP_POWER;
Ulf Hansson70be2082013-01-07 15:35:06 +01001984
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 /*
1986 * We can do SGIO
1987 */
Martin K. Petersena36274e2010-09-10 01:33:59 -04001988 mmc->max_segs = NR_SG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
1990 /*
Rabin Vincent08458ef2010-07-21 12:55:59 +01001991 * Since only a certain number of bits are valid in the data length
1992 * register, we must ensure that we don't exceed 2^num-1 bytes in a
1993 * single request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 */
Rabin Vincent08458ef2010-07-21 12:55:59 +01001995 mmc->max_req_size = (1 << variant->datalength_bits) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
1997 /*
1998 * Set the maximum segment size. Since we aren't doing DMA
1999 * (yet) we are only limited by the data length register.
2000 */
Pierre Ossman55db8902006-11-21 17:55:45 +01002001 mmc->max_seg_size = mmc->max_req_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01002003 /*
2004 * Block size can be up to 2048 bytes, but must be a power of two.
2005 */
Ludovic Barrec931d492018-10-08 14:08:43 +02002006 mmc->max_blk_size = 1 << variant->datactrl_blocksz;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01002007
Pierre Ossman55db8902006-11-21 17:55:45 +01002008 /*
Will Deacon8f7f6b7e2012-02-24 11:25:21 +00002009 * Limit the number of blocks transferred so that we don't overflow
2010 * the maximum request size.
Pierre Ossman55db8902006-11-21 17:55:45 +01002011 */
Ludovic Barrec931d492018-10-08 14:08:43 +02002012 mmc->max_blk_count = mmc->max_req_size >> variant->datactrl_blocksz;
Pierre Ossman55db8902006-11-21 17:55:45 +01002013
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 spin_lock_init(&host->lock);
2015
2016 writel(0, host->base + MMCIMASK0);
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +01002017
2018 if (variant->mmcimask1)
2019 writel(0, host->base + MMCIMASK1);
2020
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 writel(0xfff, host->base + MMCICLEAR);
2022
Linus Walleijce437aa2014-08-27 15:13:54 +02002023 /*
2024 * If:
2025 * - not using DT but using a descriptor table, or
2026 * - using a table of descriptors ALONGSIDE DT, or
2027 * look up these descriptors named "cd" and "wp" right here, fail
Linus Walleij9ef986a2018-09-20 16:01:10 -07002028 * silently of these do not exist
Linus Walleijce437aa2014-08-27 15:13:54 +02002029 */
2030 if (!np) {
Linus Walleij89168b42014-10-02 09:08:46 +02002031 ret = mmc_gpiod_request_cd(mmc, "cd", 0, false, 0, NULL);
Linus Walleij9ef986a2018-09-20 16:01:10 -07002032 if (ret == -EPROBE_DEFER)
2033 goto clk_disable;
Linus Walleijce437aa2014-08-27 15:13:54 +02002034
Linus Walleija2b760a2019-02-05 10:30:22 +01002035 ret = mmc_gpiod_request_ro(mmc, "wp", 0, 0, NULL);
Linus Walleij9ef986a2018-09-20 16:01:10 -07002036 if (ret == -EPROBE_DEFER)
2037 goto clk_disable;
Russell King89001442009-07-09 15:16:07 +01002038 }
2039
Ulf Hanssonef289982014-03-17 13:56:32 +01002040 ret = devm_request_irq(&dev->dev, dev->irq[0], mmci_irq, IRQF_SHARED,
2041 DRIVER_NAME " (cmd)", host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 if (ret)
Ulf Hanssonef289982014-03-17 13:56:32 +01002043 goto clk_disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
Russell Kingdfb85182012-05-03 11:33:15 +01002045 if (!dev->irq[1])
Linus Walleij2686b4b2010-10-19 12:39:48 +01002046 host->singleirq = true;
2047 else {
Ulf Hanssonef289982014-03-17 13:56:32 +01002048 ret = devm_request_irq(&dev->dev, dev->irq[1], mmci_pio_irq,
2049 IRQF_SHARED, DRIVER_NAME " (pio)", host);
Linus Walleij2686b4b2010-10-19 12:39:48 +01002050 if (ret)
Ulf Hanssonef289982014-03-17 13:56:32 +01002051 goto clk_disable;
Linus Walleij2686b4b2010-10-19 12:39:48 +01002052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
Ludovic Barredaf97132018-10-08 14:08:44 +02002054 writel(MCI_IRQENABLE | variant->start_err, host->base + MMCIMASK0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
2056 amba_set_drvdata(dev, mmc);
2057
Russell Kingc8ebae32011-01-11 19:35:53 +00002058 dev_info(&dev->dev, "%s: PL%03x manf %x rev%u at 0x%08llx irq %d,%d (pio)\n",
2059 mmc_hostname(mmc), amba_part(dev), amba_manf(dev),
2060 amba_rev(dev), (unsigned long long)dev->res.start,
2061 dev->irq[0], dev->irq[1]);
2062
2063 mmci_dma_setup(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Ulf Hansson2cd976c2011-12-13 17:01:11 +01002065 pm_runtime_set_autosuspend_delay(&dev->dev, 50);
2066 pm_runtime_use_autosuspend(&dev->dev);
Russell King1c3be362011-08-14 09:17:05 +01002067
Russell King8c11a942010-12-28 19:40:40 +00002068 mmc_add_host(mmc);
2069
Ulf Hansson6f2d3c82014-12-11 14:35:55 +01002070 pm_runtime_put(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 return 0;
2072
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 clk_disable:
Julia Lawallac940932012-08-26 16:00:59 +00002074 clk_disable_unprepare(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 host_free:
2076 mmc_free_host(mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 return ret;
2078}
2079
Bill Pemberton6e0ee712012-11-19 13:26:03 -05002080static int mmci_remove(struct amba_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081{
2082 struct mmc_host *mmc = amba_get_drvdata(dev);
2083
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 if (mmc) {
2085 struct mmci_host *host = mmc_priv(mmc);
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +01002086 struct variant_data *variant = host->variant;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Russell King1c3be362011-08-14 09:17:05 +01002088 /*
2089 * Undo pm_runtime_put() in probe. We use the _sync
2090 * version here so that we can access the primecell.
2091 */
2092 pm_runtime_get_sync(&dev->dev);
2093
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 mmc_remove_host(mmc);
2095
2096 writel(0, host->base + MMCIMASK0);
Patrice Chotard6ea9cdf2018-01-18 15:34:17 +01002097
2098 if (variant->mmcimask1)
2099 writel(0, host->base + MMCIMASK1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
2101 writel(0, host->base + MMCICOMMAND);
2102 writel(0, host->base + MMCIDATACTRL);
2103
Russell Kingc8ebae32011-01-11 19:35:53 +00002104 mmci_dma_release(host);
Julia Lawallac940932012-08-26 16:00:59 +00002105 clk_disable_unprepare(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 mmc_free_host(mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 }
2108
2109 return 0;
2110}
2111
Ulf Hansson571dce42014-01-23 00:38:00 +01002112#ifdef CONFIG_PM
Ulf Hansson1ff44432013-09-04 09:05:17 +01002113static void mmci_save(struct mmci_host *host)
2114{
2115 unsigned long flags;
2116
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01002117 spin_lock_irqsave(&host->lock, flags);
Ulf Hansson1ff44432013-09-04 09:05:17 +01002118
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01002119 writel(0, host->base + MMCIMASK0);
2120 if (host->variant->pwrreg_nopower) {
Ulf Hansson1ff44432013-09-04 09:05:17 +01002121 writel(0, host->base + MMCIDATACTRL);
2122 writel(0, host->base + MMCIPOWER);
2123 writel(0, host->base + MMCICLOCK);
Ulf Hansson1ff44432013-09-04 09:05:17 +01002124 }
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01002125 mmci_reg_delay(host);
Ulf Hansson1ff44432013-09-04 09:05:17 +01002126
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01002127 spin_unlock_irqrestore(&host->lock, flags);
Ulf Hansson1ff44432013-09-04 09:05:17 +01002128}
2129
2130static void mmci_restore(struct mmci_host *host)
2131{
2132 unsigned long flags;
2133
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01002134 spin_lock_irqsave(&host->lock, flags);
Ulf Hansson1ff44432013-09-04 09:05:17 +01002135
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01002136 if (host->variant->pwrreg_nopower) {
Ulf Hansson1ff44432013-09-04 09:05:17 +01002137 writel(host->clk_reg, host->base + MMCICLOCK);
2138 writel(host->datactrl_reg, host->base + MMCIDATACTRL);
2139 writel(host->pwr_reg, host->base + MMCIPOWER);
Ulf Hansson1ff44432013-09-04 09:05:17 +01002140 }
Ludovic Barredaf97132018-10-08 14:08:44 +02002141 writel(MCI_IRQENABLE | host->variant->start_err,
2142 host->base + MMCIMASK0);
Ulf Hansson42dcc89a2014-01-23 00:19:38 +01002143 mmci_reg_delay(host);
2144
2145 spin_unlock_irqrestore(&host->lock, flags);
Ulf Hansson1ff44432013-09-04 09:05:17 +01002146}
2147
Ulf Hansson82592932013-01-09 11:15:26 +01002148static int mmci_runtime_suspend(struct device *dev)
2149{
2150 struct amba_device *adev = to_amba_device(dev);
2151 struct mmc_host *mmc = amba_get_drvdata(adev);
2152
2153 if (mmc) {
2154 struct mmci_host *host = mmc_priv(mmc);
Ulf Hanssone36bd9c62013-09-04 09:00:37 +01002155 pinctrl_pm_select_sleep_state(dev);
Ulf Hansson1ff44432013-09-04 09:05:17 +01002156 mmci_save(host);
Ulf Hansson82592932013-01-09 11:15:26 +01002157 clk_disable_unprepare(host->clk);
2158 }
2159
2160 return 0;
2161}
2162
2163static int mmci_runtime_resume(struct device *dev)
2164{
2165 struct amba_device *adev = to_amba_device(dev);
2166 struct mmc_host *mmc = amba_get_drvdata(adev);
2167
2168 if (mmc) {
2169 struct mmci_host *host = mmc_priv(mmc);
2170 clk_prepare_enable(host->clk);
Ulf Hansson1ff44432013-09-04 09:05:17 +01002171 mmci_restore(host);
Ulf Hanssone36bd9c62013-09-04 09:00:37 +01002172 pinctrl_pm_select_default_state(dev);
Ulf Hansson82592932013-01-09 11:15:26 +01002173 }
2174
2175 return 0;
2176}
2177#endif
2178
Ulf Hansson48fa7002011-12-13 16:59:34 +01002179static const struct dev_pm_ops mmci_dev_pm_ops = {
Ulf Hanssonf3737fa2014-01-23 01:11:33 +01002180 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
2181 pm_runtime_force_resume)
Rafael J. Wysocki6ed23b82014-12-04 00:34:11 +01002182 SET_RUNTIME_PM_OPS(mmci_runtime_suspend, mmci_runtime_resume, NULL)
Ulf Hansson48fa7002011-12-13 16:59:34 +01002183};
2184
Arvind Yadav88411de2017-08-23 22:00:49 +05302185static const struct amba_id mmci_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 {
2187 .id = 0x00041180,
Pawel Moll768fbc12011-03-11 17:18:07 +00002188 .mask = 0xff0fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01002189 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 },
2191 {
Pawel Moll768fbc12011-03-11 17:18:07 +00002192 .id = 0x01041180,
2193 .mask = 0xff0fffff,
2194 .data = &variant_arm_extended_fifo,
2195 },
2196 {
Pawel Moll3a372982013-01-24 14:12:45 +01002197 .id = 0x02041180,
2198 .mask = 0xff0fffff,
2199 .data = &variant_arm_extended_fifo_hwfc,
2200 },
2201 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 .id = 0x00041181,
2203 .mask = 0x000fffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01002204 .data = &variant_arm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 },
Linus Walleijcc30d602009-01-04 15:18:54 +01002206 /* ST Micro variants */
2207 {
2208 .id = 0x00180180,
2209 .mask = 0x00ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01002210 .data = &variant_u300,
Linus Walleijcc30d602009-01-04 15:18:54 +01002211 },
2212 {
Linus Walleij34fd4212012-04-10 17:43:59 +01002213 .id = 0x10180180,
2214 .mask = 0xf0ffffff,
2215 .data = &variant_nomadik,
2216 },
2217 {
Linus Walleijcc30d602009-01-04 15:18:54 +01002218 .id = 0x00280180,
2219 .mask = 0x00ffffff,
Linus Walleij0bcb7ef2016-01-04 02:21:55 +01002220 .data = &variant_nomadik,
Rabin Vincent4956e102010-07-21 12:54:40 +01002221 },
2222 {
2223 .id = 0x00480180,
Philippe Langlais1784b152011-03-25 08:51:52 +01002224 .mask = 0xf0ffffff,
Rabin Vincent4956e102010-07-21 12:54:40 +01002225 .data = &variant_ux500,
Linus Walleijcc30d602009-01-04 15:18:54 +01002226 },
Philippe Langlais1784b152011-03-25 08:51:52 +01002227 {
2228 .id = 0x10480180,
2229 .mask = 0xf0ffffff,
2230 .data = &variant_ux500v2,
2231 },
Patrice Chotard2a9d6c82018-01-18 15:34:21 +01002232 {
2233 .id = 0x00880180,
2234 .mask = 0x00ffffff,
2235 .data = &variant_stm32,
2236 },
Ludovic Barre46b723d2018-10-08 14:08:55 +02002237 {
2238 .id = 0x10153180,
2239 .mask = 0xf0ffffff,
2240 .data = &variant_stm32_sdmmc,
2241 },
Srinivas Kandagatla55b604a2014-06-02 10:10:13 +01002242 /* Qualcomm variants */
2243 {
2244 .id = 0x00051180,
2245 .mask = 0x000fffff,
2246 .data = &variant_qcom,
2247 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 { 0, 0 },
2249};
2250
Dave Martin9f998352011-10-05 15:15:21 +01002251MODULE_DEVICE_TABLE(amba, mmci_ids);
2252
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253static struct amba_driver mmci_driver = {
2254 .drv = {
2255 .name = DRIVER_NAME,
Ulf Hansson48fa7002011-12-13 16:59:34 +01002256 .pm = &mmci_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 },
2258 .probe = mmci_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05002259 .remove = mmci_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 .id_table = mmci_ids,
2261};
2262
viresh kumar9e5ed092012-03-15 10:40:38 +01002263module_amba_driver(mmci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265module_param(fmax, uint, 0444);
2266
2267MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
2268MODULE_LICENSE("GPL");