Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Freescale eSDHC controller driver. |
| 4 | * |
Jerry Huang | f060bc9 | 2012-02-14 14:05:37 +0800 | [diff] [blame] | 5 | * Copyright (c) 2007, 2010, 2012 Freescale Semiconductor, Inc. |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 6 | * Copyright (c) 2009 MontaVista Software, Inc. |
Yangbo Lu | 011fde4 | 2020-10-20 16:11:16 +0800 | [diff] [blame] | 7 | * Copyright 2020 NXP |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 8 | * |
| 9 | * Authors: Xiaobo Xie <X.Xie@freescale.com> |
| 10 | * Anton Vorontsov <avorontsov@ru.mvista.com> |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 11 | */ |
| 12 | |
Oded Gabbay | 66b50a0 | 2013-06-27 12:00:05 -0400 | [diff] [blame] | 13 | #include <linux/err.h> |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 14 | #include <linux/io.h> |
Jerry Huang | f060bc9 | 2012-02-14 14:05:37 +0800 | [diff] [blame] | 15 | #include <linux/of.h> |
yangbo lu | ea35645 | 2017-04-20 16:14:41 +0800 | [diff] [blame] | 16 | #include <linux/of_address.h> |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 17 | #include <linux/delay.h> |
Paul Gortmaker | 88b4767 | 2011-07-03 15:15:51 -0400 | [diff] [blame] | 18 | #include <linux/module.h> |
yangbo lu | 151ede4 | 2016-11-09 11:14:12 +0800 | [diff] [blame] | 19 | #include <linux/sys_soc.h> |
yangbo lu | 19c3a0e | 2017-04-20 16:14:40 +0800 | [diff] [blame] | 20 | #include <linux/clk.h> |
| 21 | #include <linux/ktime.h> |
Laurentiu Tudor | 5552d7a | 2018-07-04 14:34:20 +0300 | [diff] [blame] | 22 | #include <linux/dma-mapping.h> |
Yangbo Lu | 011fde4 | 2020-10-20 16:11:16 +0800 | [diff] [blame] | 23 | #include <linux/iopoll.h> |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 24 | #include <linux/mmc/host.h> |
Yinbo Zhu | b214fe5 | 2019-03-11 02:16:47 +0000 | [diff] [blame] | 25 | #include <linux/mmc/mmc.h> |
Shawn Guo | 38576af | 2011-05-27 23:48:14 +0800 | [diff] [blame] | 26 | #include "sdhci-pltfm.h" |
Wolfram Sang | 80872e2 | 2010-10-15 12:21:03 +0200 | [diff] [blame] | 27 | #include "sdhci-esdhc.h" |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 28 | |
Jerry Huang | 137ccd4 | 2012-03-08 11:25:02 +0800 | [diff] [blame] | 29 | #define VENDOR_V_22 0x12 |
Haijun Zhang | a4071fb | 2012-12-04 10:41:28 +0800 | [diff] [blame] | 30 | #define VENDOR_V_23 0x13 |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 31 | |
yinbo.zhu | 67fdfbd | 2018-06-25 16:46:24 +0800 | [diff] [blame] | 32 | #define MMC_TIMING_NUM (MMC_TIMING_MMC_HS400 + 1) |
| 33 | |
| 34 | struct esdhc_clk_fixup { |
| 35 | const unsigned int sd_dflt_max_clk; |
| 36 | const unsigned int max_clk[MMC_TIMING_NUM]; |
| 37 | }; |
| 38 | |
| 39 | static const struct esdhc_clk_fixup ls1021a_esdhc_clk = { |
| 40 | .sd_dflt_max_clk = 25000000, |
| 41 | .max_clk[MMC_TIMING_MMC_HS] = 46500000, |
| 42 | .max_clk[MMC_TIMING_SD_HS] = 46500000, |
| 43 | }; |
| 44 | |
| 45 | static const struct esdhc_clk_fixup ls1046a_esdhc_clk = { |
| 46 | .sd_dflt_max_clk = 25000000, |
| 47 | .max_clk[MMC_TIMING_UHS_SDR104] = 167000000, |
| 48 | .max_clk[MMC_TIMING_MMC_HS200] = 167000000, |
| 49 | }; |
| 50 | |
| 51 | static const struct esdhc_clk_fixup ls1012a_esdhc_clk = { |
| 52 | .sd_dflt_max_clk = 25000000, |
| 53 | .max_clk[MMC_TIMING_UHS_SDR104] = 125000000, |
| 54 | .max_clk[MMC_TIMING_MMC_HS200] = 125000000, |
| 55 | }; |
| 56 | |
| 57 | static const struct esdhc_clk_fixup p1010_esdhc_clk = { |
| 58 | .sd_dflt_max_clk = 20000000, |
| 59 | .max_clk[MMC_TIMING_LEGACY] = 20000000, |
| 60 | .max_clk[MMC_TIMING_MMC_HS] = 42000000, |
| 61 | .max_clk[MMC_TIMING_SD_HS] = 40000000, |
| 62 | }; |
| 63 | |
| 64 | static const struct of_device_id sdhci_esdhc_of_match[] = { |
| 65 | { .compatible = "fsl,ls1021a-esdhc", .data = &ls1021a_esdhc_clk}, |
| 66 | { .compatible = "fsl,ls1046a-esdhc", .data = &ls1046a_esdhc_clk}, |
| 67 | { .compatible = "fsl,ls1012a-esdhc", .data = &ls1012a_esdhc_clk}, |
| 68 | { .compatible = "fsl,p1010-esdhc", .data = &p1010_esdhc_clk}, |
| 69 | { .compatible = "fsl,mpc8379-esdhc" }, |
| 70 | { .compatible = "fsl,mpc8536-esdhc" }, |
| 71 | { .compatible = "fsl,esdhc" }, |
| 72 | { } |
| 73 | }; |
| 74 | MODULE_DEVICE_TABLE(of, sdhci_esdhc_of_match); |
| 75 | |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 76 | struct sdhci_esdhc { |
| 77 | u8 vendor_ver; |
| 78 | u8 spec_ver; |
yangbo lu | 151ede4 | 2016-11-09 11:14:12 +0800 | [diff] [blame] | 79 | bool quirk_incorrect_hostver; |
Yangbo Lu | 6079e63 | 2018-11-23 11:15:35 +0800 | [diff] [blame] | 80 | bool quirk_limited_clk_division; |
Yangbo Lu | 48e304c | 2018-11-23 11:15:37 +0800 | [diff] [blame] | 81 | bool quirk_unreliable_pulse_detection; |
Yangbo Lu | 22dc132 | 2019-10-09 15:41:40 +0800 | [diff] [blame] | 82 | bool quirk_tuning_erratum_type1; |
| 83 | bool quirk_tuning_erratum_type2; |
Yangbo Lu | 1f1929f | 2019-03-11 02:16:51 +0000 | [diff] [blame] | 84 | bool quirk_ignore_data_inhibit; |
Yangbo Lu | f667216 | 2019-12-19 11:23:35 +0800 | [diff] [blame] | 85 | bool quirk_delay_before_data_reset; |
Chris Packham | 060522d | 2020-09-03 13:20:29 +1200 | [diff] [blame] | 86 | bool quirk_trans_complete_erratum; |
Yangbo Lu | 22dc132 | 2019-10-09 15:41:40 +0800 | [diff] [blame] | 87 | bool in_sw_tuning; |
yangbo lu | 19c3a0e | 2017-04-20 16:14:40 +0800 | [diff] [blame] | 88 | unsigned int peripheral_clock; |
yinbo.zhu | 67fdfbd | 2018-06-25 16:46:24 +0800 | [diff] [blame] | 89 | const struct esdhc_clk_fixup *clk_fixup; |
Yinbo Zhu | b1f378a | 2018-08-23 16:48:32 +0800 | [diff] [blame] | 90 | u32 div_ratio; |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | /** |
| 94 | * esdhc_read*_fixup - Fixup the value read from incompatible eSDHC register |
| 95 | * to make it compatible with SD spec. |
| 96 | * |
| 97 | * @host: pointer to sdhci_host |
| 98 | * @spec_reg: SD spec register address |
| 99 | * @value: 32bit eSDHC register value on spec_reg address |
| 100 | * |
| 101 | * In SD spec, there are 8/16/32/64 bits registers, while all of eSDHC |
| 102 | * registers are 32 bits. There are differences in register size, register |
| 103 | * address, register function, bit position and function between eSDHC spec |
| 104 | * and SD spec. |
| 105 | * |
| 106 | * Return a fixed up register value |
| 107 | */ |
| 108 | static u32 esdhc_readl_fixup(struct sdhci_host *host, |
| 109 | int spec_reg, u32 value) |
Jerry Huang | 137ccd4 | 2012-03-08 11:25:02 +0800 | [diff] [blame] | 110 | { |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 111 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
Jisheng Zhang | 8605e7a | 2016-02-16 21:08:26 +0800 | [diff] [blame] | 112 | struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); |
Jerry Huang | 137ccd4 | 2012-03-08 11:25:02 +0800 | [diff] [blame] | 113 | u32 ret; |
| 114 | |
Jerry Huang | 137ccd4 | 2012-03-08 11:25:02 +0800 | [diff] [blame] | 115 | /* |
| 116 | * The bit of ADMA flag in eSDHC is not compatible with standard |
| 117 | * SDHC register, so set fake flag SDHCI_CAN_DO_ADMA2 when ADMA is |
| 118 | * supported by eSDHC. |
| 119 | * And for many FSL eSDHC controller, the reset value of field |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 120 | * SDHCI_CAN_DO_ADMA1 is 1, but some of them can't support ADMA, |
Jerry Huang | 137ccd4 | 2012-03-08 11:25:02 +0800 | [diff] [blame] | 121 | * only these vendor version is greater than 2.2/0x12 support ADMA. |
Jerry Huang | 137ccd4 | 2012-03-08 11:25:02 +0800 | [diff] [blame] | 122 | */ |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 123 | if ((spec_reg == SDHCI_CAPABILITIES) && (value & SDHCI_CAN_DO_ADMA1)) { |
| 124 | if (esdhc->vendor_ver > VENDOR_V_22) { |
| 125 | ret = value | SDHCI_CAN_DO_ADMA2; |
| 126 | return ret; |
| 127 | } |
Jerry Huang | 137ccd4 | 2012-03-08 11:25:02 +0800 | [diff] [blame] | 128 | } |
Michael Walle | b0921d5 | 2016-11-15 11:13:16 +0100 | [diff] [blame] | 129 | /* |
| 130 | * The DAT[3:0] line signal levels and the CMD line signal level are |
| 131 | * not compatible with standard SDHC register. The line signal levels |
| 132 | * DAT[7:0] are at bits 31:24 and the command line signal level is at |
| 133 | * bit 23. All other bits are the same as in the standard SDHC |
| 134 | * register. |
| 135 | */ |
| 136 | if (spec_reg == SDHCI_PRESENT_STATE) { |
| 137 | ret = value & 0x000fffff; |
| 138 | ret |= (value >> 4) & SDHCI_DATA_LVL_MASK; |
| 139 | ret |= (value << 1) & SDHCI_CMD_LVL; |
| 140 | return ret; |
| 141 | } |
| 142 | |
yangbo lu | 2f3110c | 2017-08-15 10:17:03 +0800 | [diff] [blame] | 143 | /* |
| 144 | * DTS properties of mmc host are used to enable each speed mode |
| 145 | * according to soc and board capability. So clean up |
| 146 | * SDR50/SDR104/DDR50 support bits here. |
| 147 | */ |
| 148 | if (spec_reg == SDHCI_CAPABILITIES_1) { |
| 149 | ret = value & ~(SDHCI_SUPPORT_SDR50 | SDHCI_SUPPORT_SDR104 | |
| 150 | SDHCI_SUPPORT_DDR50); |
| 151 | return ret; |
| 152 | } |
| 153 | |
Yangbo Lu | 1f1929f | 2019-03-11 02:16:51 +0000 | [diff] [blame] | 154 | /* |
| 155 | * Some controllers have unreliable Data Line Active |
| 156 | * bit for commands with busy signal. This affects |
| 157 | * Command Inhibit (data) bit. Just ignore it since |
| 158 | * MMC core driver has already polled card status |
| 159 | * with CMD13 after any command with busy siganl. |
| 160 | */ |
| 161 | if ((spec_reg == SDHCI_PRESENT_STATE) && |
| 162 | (esdhc->quirk_ignore_data_inhibit == true)) { |
| 163 | ret = value & ~SDHCI_DATA_INHIBIT; |
| 164 | return ret; |
| 165 | } |
| 166 | |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 167 | ret = value; |
Jerry Huang | 137ccd4 | 2012-03-08 11:25:02 +0800 | [diff] [blame] | 168 | return ret; |
| 169 | } |
| 170 | |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 171 | static u16 esdhc_readw_fixup(struct sdhci_host *host, |
| 172 | int spec_reg, u32 value) |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 173 | { |
yangbo lu | 151ede4 | 2016-11-09 11:14:12 +0800 | [diff] [blame] | 174 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 175 | struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 176 | u16 ret; |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 177 | int shift = (spec_reg & 0x2) * 8; |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 178 | |
Yangbo Lu | 429d939 | 2020-01-17 14:38:58 +0800 | [diff] [blame] | 179 | if (spec_reg == SDHCI_TRANSFER_MODE) |
| 180 | return pltfm_host->xfer_mode_shadow; |
| 181 | |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 182 | if (spec_reg == SDHCI_HOST_VERSION) |
| 183 | ret = value & 0xffff; |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 184 | else |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 185 | ret = (value >> shift) & 0xffff; |
yangbo lu | 151ede4 | 2016-11-09 11:14:12 +0800 | [diff] [blame] | 186 | /* Workaround for T4240-R1.0-R2.0 eSDHC which has incorrect |
| 187 | * vendor version and spec version information. |
| 188 | */ |
| 189 | if ((spec_reg == SDHCI_HOST_VERSION) && |
| 190 | (esdhc->quirk_incorrect_hostver)) |
| 191 | ret = (VENDOR_V_23 << SDHCI_VENDOR_VER_SHIFT) | SDHCI_SPEC_200; |
Xu lei | e51cbc9 | 2011-09-09 20:05:46 +0800 | [diff] [blame] | 192 | return ret; |
| 193 | } |
| 194 | |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 195 | static u8 esdhc_readb_fixup(struct sdhci_host *host, |
| 196 | int spec_reg, u32 value) |
Xu lei | e51cbc9 | 2011-09-09 20:05:46 +0800 | [diff] [blame] | 197 | { |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 198 | u8 ret; |
| 199 | u8 dma_bits; |
| 200 | int shift = (spec_reg & 0x3) * 8; |
| 201 | |
| 202 | ret = (value >> shift) & 0xff; |
Roy Zang | ba8c4dc | 2012-01-13 15:02:01 +0800 | [diff] [blame] | 203 | |
| 204 | /* |
| 205 | * "DMA select" locates at offset 0x28 in SD specification, but on |
| 206 | * P5020 or P3041, it locates at 0x29. |
| 207 | */ |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 208 | if (spec_reg == SDHCI_HOST_CONTROL) { |
Roy Zang | ba8c4dc | 2012-01-13 15:02:01 +0800 | [diff] [blame] | 209 | /* DMA select is 22,23 bits in Protocol Control Register */ |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 210 | dma_bits = (value >> 5) & SDHCI_CTRL_DMA_MASK; |
Roy Zang | ba8c4dc | 2012-01-13 15:02:01 +0800 | [diff] [blame] | 211 | /* fixup the result */ |
| 212 | ret &= ~SDHCI_CTRL_DMA_MASK; |
| 213 | ret |= dma_bits; |
| 214 | } |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 215 | return ret; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * esdhc_write*_fixup - Fixup the SD spec register value so that it could be |
| 220 | * written into eSDHC register. |
| 221 | * |
| 222 | * @host: pointer to sdhci_host |
| 223 | * @spec_reg: SD spec register address |
| 224 | * @value: 8/16/32bit SD spec register value that would be written |
| 225 | * @old_value: 32bit eSDHC register value on spec_reg address |
| 226 | * |
| 227 | * In SD spec, there are 8/16/32/64 bits registers, while all of eSDHC |
| 228 | * registers are 32 bits. There are differences in register size, register |
| 229 | * address, register function, bit position and function between eSDHC spec |
| 230 | * and SD spec. |
| 231 | * |
| 232 | * Return a fixed up register value |
| 233 | */ |
| 234 | static u32 esdhc_writel_fixup(struct sdhci_host *host, |
| 235 | int spec_reg, u32 value, u32 old_value) |
| 236 | { |
| 237 | u32 ret; |
| 238 | |
| 239 | /* |
| 240 | * Enabling IRQSTATEN[BGESEN] is just to set IRQSTAT[BGE] |
| 241 | * when SYSCTL[RSTD] is set for some special operations. |
| 242 | * No any impact on other operation. |
| 243 | */ |
| 244 | if (spec_reg == SDHCI_INT_ENABLE) |
| 245 | ret = value | SDHCI_INT_BLK_GAP; |
| 246 | else |
| 247 | ret = value; |
Roy Zang | ba8c4dc | 2012-01-13 15:02:01 +0800 | [diff] [blame] | 248 | |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 249 | return ret; |
| 250 | } |
| 251 | |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 252 | static u32 esdhc_writew_fixup(struct sdhci_host *host, |
| 253 | int spec_reg, u16 value, u32 old_value) |
Haijun Zhang | a4071fb | 2012-12-04 10:41:28 +0800 | [diff] [blame] | 254 | { |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 255 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 256 | int shift = (spec_reg & 0x2) * 8; |
| 257 | u32 ret; |
Haijun Zhang | a4071fb | 2012-12-04 10:41:28 +0800 | [diff] [blame] | 258 | |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 259 | switch (spec_reg) { |
| 260 | case SDHCI_TRANSFER_MODE: |
| 261 | /* |
| 262 | * Postpone this write, we must do it together with a |
| 263 | * command write that is down below. Return old value. |
| 264 | */ |
| 265 | pltfm_host->xfer_mode_shadow = value; |
| 266 | return old_value; |
| 267 | case SDHCI_COMMAND: |
| 268 | ret = (value << 16) | pltfm_host->xfer_mode_shadow; |
| 269 | return ret; |
| 270 | } |
| 271 | |
| 272 | ret = old_value & (~(0xffff << shift)); |
| 273 | ret |= (value << shift); |
| 274 | |
| 275 | if (spec_reg == SDHCI_BLOCK_SIZE) { |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 276 | /* |
| 277 | * Two last DMA bits are reserved, and first one is used for |
| 278 | * non-standard blksz of 4096 bytes that we don't support |
| 279 | * yet. So clear the DMA boundary bits. |
| 280 | */ |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 281 | ret &= (~SDHCI_MAKE_BLKSZ(0x7, 0)); |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 282 | } |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 283 | return ret; |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 284 | } |
| 285 | |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 286 | static u32 esdhc_writeb_fixup(struct sdhci_host *host, |
| 287 | int spec_reg, u8 value, u32 old_value) |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 288 | { |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 289 | u32 ret; |
| 290 | u32 dma_bits; |
| 291 | u8 tmp; |
| 292 | int shift = (spec_reg & 0x3) * 8; |
| 293 | |
Roy Zang | ba8c4dc | 2012-01-13 15:02:01 +0800 | [diff] [blame] | 294 | /* |
yangbo lu | 9e4703d | 2015-10-16 15:44:03 +0800 | [diff] [blame] | 295 | * eSDHC doesn't have a standard power control register, so we do |
| 296 | * nothing here to avoid incorrect operation. |
| 297 | */ |
| 298 | if (spec_reg == SDHCI_POWER_CONTROL) |
| 299 | return old_value; |
| 300 | /* |
Roy Zang | ba8c4dc | 2012-01-13 15:02:01 +0800 | [diff] [blame] | 301 | * "DMA select" location is offset 0x28 in SD specification, but on |
| 302 | * P5020 or P3041, it's located at 0x29. |
| 303 | */ |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 304 | if (spec_reg == SDHCI_HOST_CONTROL) { |
Oded Gabbay | dcaff04 | 2013-07-05 12:48:35 -0400 | [diff] [blame] | 305 | /* |
| 306 | * If host control register is not standard, exit |
| 307 | * this function |
| 308 | */ |
| 309 | if (host->quirks2 & SDHCI_QUIRK2_BROKEN_HOST_CONTROL) |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 310 | return old_value; |
Oded Gabbay | dcaff04 | 2013-07-05 12:48:35 -0400 | [diff] [blame] | 311 | |
Roy Zang | ba8c4dc | 2012-01-13 15:02:01 +0800 | [diff] [blame] | 312 | /* DMA select is 22,23 bits in Protocol Control Register */ |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 313 | dma_bits = (value & SDHCI_CTRL_DMA_MASK) << 5; |
| 314 | ret = (old_value & (~(SDHCI_CTRL_DMA_MASK << 5))) | dma_bits; |
| 315 | tmp = (value & (~SDHCI_CTRL_DMA_MASK)) | |
| 316 | (old_value & SDHCI_CTRL_DMA_MASK); |
| 317 | ret = (ret & (~0xff)) | tmp; |
| 318 | |
| 319 | /* Prevent SDHCI core from writing reserved bits (e.g. HISPD) */ |
| 320 | ret &= ~ESDHC_HOST_CONTROL_RES; |
| 321 | return ret; |
Roy Zang | ba8c4dc | 2012-01-13 15:02:01 +0800 | [diff] [blame] | 322 | } |
| 323 | |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 324 | ret = (old_value & (~(0xff << shift))) | (value << shift); |
| 325 | return ret; |
| 326 | } |
| 327 | |
| 328 | static u32 esdhc_be_readl(struct sdhci_host *host, int reg) |
| 329 | { |
| 330 | u32 ret; |
| 331 | u32 value; |
| 332 | |
yangbo lu | 2f3110c | 2017-08-15 10:17:03 +0800 | [diff] [blame] | 333 | if (reg == SDHCI_CAPABILITIES_1) |
| 334 | value = ioread32be(host->ioaddr + ESDHC_CAPABILITIES_1); |
| 335 | else |
| 336 | value = ioread32be(host->ioaddr + reg); |
| 337 | |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 338 | ret = esdhc_readl_fixup(host, reg, value); |
| 339 | |
| 340 | return ret; |
| 341 | } |
| 342 | |
| 343 | static u32 esdhc_le_readl(struct sdhci_host *host, int reg) |
| 344 | { |
| 345 | u32 ret; |
| 346 | u32 value; |
| 347 | |
yangbo lu | 2f3110c | 2017-08-15 10:17:03 +0800 | [diff] [blame] | 348 | if (reg == SDHCI_CAPABILITIES_1) |
| 349 | value = ioread32(host->ioaddr + ESDHC_CAPABILITIES_1); |
| 350 | else |
| 351 | value = ioread32(host->ioaddr + reg); |
| 352 | |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 353 | ret = esdhc_readl_fixup(host, reg, value); |
| 354 | |
| 355 | return ret; |
| 356 | } |
| 357 | |
| 358 | static u16 esdhc_be_readw(struct sdhci_host *host, int reg) |
| 359 | { |
| 360 | u16 ret; |
| 361 | u32 value; |
| 362 | int base = reg & ~0x3; |
| 363 | |
| 364 | value = ioread32be(host->ioaddr + base); |
| 365 | ret = esdhc_readw_fixup(host, reg, value); |
| 366 | return ret; |
| 367 | } |
| 368 | |
| 369 | static u16 esdhc_le_readw(struct sdhci_host *host, int reg) |
| 370 | { |
| 371 | u16 ret; |
| 372 | u32 value; |
| 373 | int base = reg & ~0x3; |
| 374 | |
| 375 | value = ioread32(host->ioaddr + base); |
| 376 | ret = esdhc_readw_fixup(host, reg, value); |
| 377 | return ret; |
| 378 | } |
| 379 | |
| 380 | static u8 esdhc_be_readb(struct sdhci_host *host, int reg) |
| 381 | { |
| 382 | u8 ret; |
| 383 | u32 value; |
| 384 | int base = reg & ~0x3; |
| 385 | |
| 386 | value = ioread32be(host->ioaddr + base); |
| 387 | ret = esdhc_readb_fixup(host, reg, value); |
| 388 | return ret; |
| 389 | } |
| 390 | |
| 391 | static u8 esdhc_le_readb(struct sdhci_host *host, int reg) |
| 392 | { |
| 393 | u8 ret; |
| 394 | u32 value; |
| 395 | int base = reg & ~0x3; |
| 396 | |
| 397 | value = ioread32(host->ioaddr + base); |
| 398 | ret = esdhc_readb_fixup(host, reg, value); |
| 399 | return ret; |
| 400 | } |
| 401 | |
| 402 | static void esdhc_be_writel(struct sdhci_host *host, u32 val, int reg) |
| 403 | { |
| 404 | u32 value; |
| 405 | |
| 406 | value = esdhc_writel_fixup(host, reg, val, 0); |
| 407 | iowrite32be(value, host->ioaddr + reg); |
| 408 | } |
| 409 | |
| 410 | static void esdhc_le_writel(struct sdhci_host *host, u32 val, int reg) |
| 411 | { |
| 412 | u32 value; |
| 413 | |
| 414 | value = esdhc_writel_fixup(host, reg, val, 0); |
| 415 | iowrite32(value, host->ioaddr + reg); |
| 416 | } |
| 417 | |
| 418 | static void esdhc_be_writew(struct sdhci_host *host, u16 val, int reg) |
| 419 | { |
Yangbo Lu | 22dc132 | 2019-10-09 15:41:40 +0800 | [diff] [blame] | 420 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 421 | struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 422 | int base = reg & ~0x3; |
| 423 | u32 value; |
| 424 | u32 ret; |
| 425 | |
| 426 | value = ioread32be(host->ioaddr + base); |
| 427 | ret = esdhc_writew_fixup(host, reg, val, value); |
| 428 | if (reg != SDHCI_TRANSFER_MODE) |
| 429 | iowrite32be(ret, host->ioaddr + base); |
Yangbo Lu | 22dc132 | 2019-10-09 15:41:40 +0800 | [diff] [blame] | 430 | |
| 431 | /* Starting SW tuning requires ESDHC_SMPCLKSEL to be set |
| 432 | * 1us later after ESDHC_EXTN is set. |
| 433 | */ |
| 434 | if (base == ESDHC_SYSTEM_CONTROL_2) { |
| 435 | if (!(value & ESDHC_EXTN) && (ret & ESDHC_EXTN) && |
| 436 | esdhc->in_sw_tuning) { |
| 437 | udelay(1); |
| 438 | ret |= ESDHC_SMPCLKSEL; |
| 439 | iowrite32be(ret, host->ioaddr + base); |
| 440 | } |
| 441 | } |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | static void esdhc_le_writew(struct sdhci_host *host, u16 val, int reg) |
| 445 | { |
Yangbo Lu | 22dc132 | 2019-10-09 15:41:40 +0800 | [diff] [blame] | 446 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 447 | struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 448 | int base = reg & ~0x3; |
| 449 | u32 value; |
| 450 | u32 ret; |
| 451 | |
| 452 | value = ioread32(host->ioaddr + base); |
| 453 | ret = esdhc_writew_fixup(host, reg, val, value); |
| 454 | if (reg != SDHCI_TRANSFER_MODE) |
| 455 | iowrite32(ret, host->ioaddr + base); |
Yangbo Lu | 22dc132 | 2019-10-09 15:41:40 +0800 | [diff] [blame] | 456 | |
| 457 | /* Starting SW tuning requires ESDHC_SMPCLKSEL to be set |
| 458 | * 1us later after ESDHC_EXTN is set. |
| 459 | */ |
| 460 | if (base == ESDHC_SYSTEM_CONTROL_2) { |
| 461 | if (!(value & ESDHC_EXTN) && (ret & ESDHC_EXTN) && |
| 462 | esdhc->in_sw_tuning) { |
| 463 | udelay(1); |
| 464 | ret |= ESDHC_SMPCLKSEL; |
| 465 | iowrite32(ret, host->ioaddr + base); |
| 466 | } |
| 467 | } |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | static void esdhc_be_writeb(struct sdhci_host *host, u8 val, int reg) |
| 471 | { |
| 472 | int base = reg & ~0x3; |
| 473 | u32 value; |
| 474 | u32 ret; |
| 475 | |
| 476 | value = ioread32be(host->ioaddr + base); |
| 477 | ret = esdhc_writeb_fixup(host, reg, val, value); |
| 478 | iowrite32be(ret, host->ioaddr + base); |
| 479 | } |
| 480 | |
| 481 | static void esdhc_le_writeb(struct sdhci_host *host, u8 val, int reg) |
| 482 | { |
| 483 | int base = reg & ~0x3; |
| 484 | u32 value; |
| 485 | u32 ret; |
| 486 | |
| 487 | value = ioread32(host->ioaddr + base); |
| 488 | ret = esdhc_writeb_fixup(host, reg, val, value); |
| 489 | iowrite32(ret, host->ioaddr + base); |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 490 | } |
| 491 | |
Haijun Zhang | a4071fb | 2012-12-04 10:41:28 +0800 | [diff] [blame] | 492 | /* |
| 493 | * For Abort or Suspend after Stop at Block Gap, ignore the ADMA |
| 494 | * error(IRQSTAT[ADMAE]) if both Transfer Complete(IRQSTAT[TC]) |
| 495 | * and Block Gap Event(IRQSTAT[BGE]) are also set. |
| 496 | * For Continue, apply soft reset for data(SYSCTL[RSTD]); |
| 497 | * and re-issue the entire read transaction from beginning. |
| 498 | */ |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 499 | static void esdhc_of_adma_workaround(struct sdhci_host *host, u32 intmask) |
Haijun Zhang | a4071fb | 2012-12-04 10:41:28 +0800 | [diff] [blame] | 500 | { |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 501 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
Jisheng Zhang | 8605e7a | 2016-02-16 21:08:26 +0800 | [diff] [blame] | 502 | struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); |
Haijun Zhang | a4071fb | 2012-12-04 10:41:28 +0800 | [diff] [blame] | 503 | bool applicable; |
| 504 | dma_addr_t dmastart; |
| 505 | dma_addr_t dmanow; |
| 506 | |
Haijun Zhang | a4071fb | 2012-12-04 10:41:28 +0800 | [diff] [blame] | 507 | applicable = (intmask & SDHCI_INT_DATA_END) && |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 508 | (intmask & SDHCI_INT_BLK_GAP) && |
| 509 | (esdhc->vendor_ver == VENDOR_V_23); |
Haijun Zhang | a4071fb | 2012-12-04 10:41:28 +0800 | [diff] [blame] | 510 | if (!applicable) |
| 511 | return; |
| 512 | |
| 513 | host->data->error = 0; |
| 514 | dmastart = sg_dma_address(host->data->sg); |
| 515 | dmanow = dmastart + host->data->bytes_xfered; |
| 516 | /* |
| 517 | * Force update to the next DMA block boundary. |
| 518 | */ |
| 519 | dmanow = (dmanow & ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) + |
| 520 | SDHCI_DEFAULT_BOUNDARY_SIZE; |
| 521 | host->data->bytes_xfered = dmanow - dmastart; |
| 522 | sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS); |
| 523 | } |
| 524 | |
Wolfram Sang | 80872e2 | 2010-10-15 12:21:03 +0200 | [diff] [blame] | 525 | static int esdhc_of_enable_dma(struct sdhci_host *host) |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 526 | { |
Jiasheng Jiang | 40c67c2 | 2022-01-12 16:31:56 +0800 | [diff] [blame] | 527 | int ret; |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 528 | u32 value; |
Laurentiu Tudor | 5552d7a | 2018-07-04 14:34:20 +0300 | [diff] [blame] | 529 | struct device *dev = mmc_dev(host->mmc); |
| 530 | |
| 531 | if (of_device_is_compatible(dev->of_node, "fsl,ls1043a-esdhc") || |
Jiasheng Jiang | 40c67c2 | 2022-01-12 16:31:56 +0800 | [diff] [blame] | 532 | of_device_is_compatible(dev->of_node, "fsl,ls1046a-esdhc")) { |
| 533 | ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(40)); |
| 534 | if (ret) |
| 535 | return ret; |
| 536 | } |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 537 | |
| 538 | value = sdhci_readl(host, ESDHC_DMA_SYSCTL); |
Russell King | 121bd08 | 2019-09-22 11:26:58 +0100 | [diff] [blame] | 539 | |
| 540 | if (of_dma_is_coherent(dev->of_node)) |
| 541 | value |= ESDHC_DMA_SNOOP; |
| 542 | else |
| 543 | value &= ~ESDHC_DMA_SNOOP; |
| 544 | |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 545 | sdhci_writel(host, value, ESDHC_DMA_SYSCTL); |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 546 | return 0; |
| 547 | } |
| 548 | |
Wolfram Sang | 80872e2 | 2010-10-15 12:21:03 +0200 | [diff] [blame] | 549 | static unsigned int esdhc_of_get_max_clock(struct sdhci_host *host) |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 550 | { |
Shawn Guo | e307148 | 2011-07-20 17:13:36 -0400 | [diff] [blame] | 551 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
yangbo lu | 19c3a0e | 2017-04-20 16:14:40 +0800 | [diff] [blame] | 552 | struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 553 | |
yangbo lu | 19c3a0e | 2017-04-20 16:14:40 +0800 | [diff] [blame] | 554 | if (esdhc->peripheral_clock) |
| 555 | return esdhc->peripheral_clock; |
| 556 | else |
| 557 | return pltfm_host->clock; |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 558 | } |
| 559 | |
Wolfram Sang | 80872e2 | 2010-10-15 12:21:03 +0200 | [diff] [blame] | 560 | static unsigned int esdhc_of_get_min_clock(struct sdhci_host *host) |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 561 | { |
Shawn Guo | e307148 | 2011-07-20 17:13:36 -0400 | [diff] [blame] | 562 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
yangbo lu | 19c3a0e | 2017-04-20 16:14:40 +0800 | [diff] [blame] | 563 | struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); |
| 564 | unsigned int clock; |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 565 | |
yangbo lu | 19c3a0e | 2017-04-20 16:14:40 +0800 | [diff] [blame] | 566 | if (esdhc->peripheral_clock) |
| 567 | clock = esdhc->peripheral_clock; |
| 568 | else |
| 569 | clock = pltfm_host->clock; |
| 570 | return clock / 256 / 16; |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 571 | } |
| 572 | |
yangbo lu | dd3f698 | 2017-09-21 16:43:31 +0800 | [diff] [blame] | 573 | static void esdhc_clock_enable(struct sdhci_host *host, bool enable) |
| 574 | { |
Yangbo Lu | 1b21a70 | 2020-01-08 12:07:13 +0800 | [diff] [blame] | 575 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 576 | struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); |
yangbo lu | dd3f698 | 2017-09-21 16:43:31 +0800 | [diff] [blame] | 577 | ktime_t timeout; |
Yangbo Lu | 1b21a70 | 2020-01-08 12:07:13 +0800 | [diff] [blame] | 578 | u32 val, clk_en; |
| 579 | |
| 580 | clk_en = ESDHC_CLOCK_SDCLKEN; |
| 581 | |
| 582 | /* |
| 583 | * IPGEN/HCKEN/PEREN bits exist on eSDHC whose vendor version |
| 584 | * is 2.2 or lower. |
| 585 | */ |
| 586 | if (esdhc->vendor_ver <= VENDOR_V_22) |
| 587 | clk_en |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | |
| 588 | ESDHC_CLOCK_PEREN); |
yangbo lu | dd3f698 | 2017-09-21 16:43:31 +0800 | [diff] [blame] | 589 | |
| 590 | val = sdhci_readl(host, ESDHC_SYSTEM_CONTROL); |
| 591 | |
| 592 | if (enable) |
Yangbo Lu | 1b21a70 | 2020-01-08 12:07:13 +0800 | [diff] [blame] | 593 | val |= clk_en; |
yangbo lu | dd3f698 | 2017-09-21 16:43:31 +0800 | [diff] [blame] | 594 | else |
Yangbo Lu | 1b21a70 | 2020-01-08 12:07:13 +0800 | [diff] [blame] | 595 | val &= ~clk_en; |
yangbo lu | dd3f698 | 2017-09-21 16:43:31 +0800 | [diff] [blame] | 596 | |
| 597 | sdhci_writel(host, val, ESDHC_SYSTEM_CONTROL); |
| 598 | |
Yangbo Lu | 1b21a70 | 2020-01-08 12:07:13 +0800 | [diff] [blame] | 599 | /* |
| 600 | * Wait max 20 ms. If vendor version is 2.2 or lower, do not |
| 601 | * wait clock stable bit which does not exist. |
| 602 | */ |
yangbo lu | dd3f698 | 2017-09-21 16:43:31 +0800 | [diff] [blame] | 603 | timeout = ktime_add_ms(ktime_get(), 20); |
Yangbo Lu | 1b21a70 | 2020-01-08 12:07:13 +0800 | [diff] [blame] | 604 | while (esdhc->vendor_ver > VENDOR_V_22) { |
Adrian Hunter | ea6d027 | 2018-12-10 10:56:24 +0200 | [diff] [blame] | 605 | bool timedout = ktime_after(ktime_get(), timeout); |
| 606 | |
Yangbo Lu | 1b21a70 | 2020-01-08 12:07:13 +0800 | [diff] [blame] | 607 | if (sdhci_readl(host, ESDHC_PRSSTAT) & ESDHC_CLOCK_STABLE) |
Adrian Hunter | ea6d027 | 2018-12-10 10:56:24 +0200 | [diff] [blame] | 608 | break; |
| 609 | if (timedout) { |
yangbo lu | dd3f698 | 2017-09-21 16:43:31 +0800 | [diff] [blame] | 610 | pr_err("%s: Internal clock never stabilised.\n", |
| 611 | mmc_hostname(host->mmc)); |
| 612 | break; |
| 613 | } |
Yangbo Lu | 1b21a70 | 2020-01-08 12:07:13 +0800 | [diff] [blame] | 614 | usleep_range(10, 20); |
yangbo lu | dd3f698 | 2017-09-21 16:43:31 +0800 | [diff] [blame] | 615 | } |
| 616 | } |
| 617 | |
Yangbo Lu | 6e32f65 | 2019-10-09 15:41:39 +0800 | [diff] [blame] | 618 | static void esdhc_flush_async_fifo(struct sdhci_host *host) |
| 619 | { |
| 620 | ktime_t timeout; |
| 621 | u32 val; |
| 622 | |
| 623 | val = sdhci_readl(host, ESDHC_DMA_SYSCTL); |
| 624 | val |= ESDHC_FLUSH_ASYNC_FIFO; |
| 625 | sdhci_writel(host, val, ESDHC_DMA_SYSCTL); |
| 626 | |
| 627 | /* Wait max 20 ms */ |
| 628 | timeout = ktime_add_ms(ktime_get(), 20); |
| 629 | while (1) { |
| 630 | bool timedout = ktime_after(ktime_get(), timeout); |
| 631 | |
| 632 | if (!(sdhci_readl(host, ESDHC_DMA_SYSCTL) & |
| 633 | ESDHC_FLUSH_ASYNC_FIFO)) |
| 634 | break; |
| 635 | if (timedout) { |
| 636 | pr_err("%s: flushing asynchronous FIFO timeout.\n", |
| 637 | mmc_hostname(host->mmc)); |
| 638 | break; |
| 639 | } |
| 640 | usleep_range(10, 20); |
| 641 | } |
| 642 | } |
| 643 | |
Jerry Huang | f060bc9 | 2012-02-14 14:05:37 +0800 | [diff] [blame] | 644 | static void esdhc_of_set_clock(struct sdhci_host *host, unsigned int clock) |
| 645 | { |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 646 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
Jisheng Zhang | 8605e7a | 2016-02-16 21:08:26 +0800 | [diff] [blame] | 647 | struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); |
Yangbo Lu | 1b21a70 | 2020-01-08 12:07:13 +0800 | [diff] [blame] | 648 | unsigned int pre_div = 1, div = 1; |
| 649 | unsigned int clock_fixup = 0; |
yangbo lu | e145ac4 | 2017-04-26 10:45:49 +0800 | [diff] [blame] | 650 | ktime_t timeout; |
Dong Aisheng | d31fc00 | 2013-09-13 19:11:32 +0800 | [diff] [blame] | 651 | u32 temp; |
| 652 | |
yangbo lu | dd3f698 | 2017-09-21 16:43:31 +0800 | [diff] [blame] | 653 | if (clock == 0) { |
Yangbo Lu | 1b21a70 | 2020-01-08 12:07:13 +0800 | [diff] [blame] | 654 | host->mmc->actual_clock = 0; |
yangbo lu | dd3f698 | 2017-09-21 16:43:31 +0800 | [diff] [blame] | 655 | esdhc_clock_enable(host, false); |
Russell King | 373073e | 2014-04-25 12:58:45 +0100 | [diff] [blame] | 656 | return; |
yangbo lu | dd3f698 | 2017-09-21 16:43:31 +0800 | [diff] [blame] | 657 | } |
Dong Aisheng | d31fc00 | 2013-09-13 19:11:32 +0800 | [diff] [blame] | 658 | |
Yangbo Lu | 1b21a70 | 2020-01-08 12:07:13 +0800 | [diff] [blame] | 659 | /* Start pre_div at 2 for vendor version < 2.3. */ |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 660 | if (esdhc->vendor_ver < VENDOR_V_23) |
Yangbo Lu | 77bd2f6 | 2015-08-11 10:53:34 +0800 | [diff] [blame] | 661 | pre_div = 2; |
| 662 | |
Yangbo Lu | 1b21a70 | 2020-01-08 12:07:13 +0800 | [diff] [blame] | 663 | /* Fix clock value. */ |
yinbo.zhu | 67fdfbd | 2018-06-25 16:46:24 +0800 | [diff] [blame] | 664 | if (host->mmc->card && mmc_card_sd(host->mmc->card) && |
Yangbo Lu | 1b21a70 | 2020-01-08 12:07:13 +0800 | [diff] [blame] | 665 | esdhc->clk_fixup && host->mmc->ios.timing == MMC_TIMING_LEGACY) |
| 666 | clock_fixup = esdhc->clk_fixup->sd_dflt_max_clk; |
yinbo.zhu | 67fdfbd | 2018-06-25 16:46:24 +0800 | [diff] [blame] | 667 | else if (esdhc->clk_fixup) |
Yangbo Lu | 1b21a70 | 2020-01-08 12:07:13 +0800 | [diff] [blame] | 668 | clock_fixup = esdhc->clk_fixup->max_clk[host->mmc->ios.timing]; |
yangbo lu | a627f02 | 2017-04-20 14:58:29 +0800 | [diff] [blame] | 669 | |
Yangbo Lu | 1b21a70 | 2020-01-08 12:07:13 +0800 | [diff] [blame] | 670 | if (clock_fixup == 0 || clock < clock_fixup) |
| 671 | clock_fixup = clock; |
Jerry Huang | f060bc9 | 2012-02-14 14:05:37 +0800 | [diff] [blame] | 672 | |
Yangbo Lu | 1b21a70 | 2020-01-08 12:07:13 +0800 | [diff] [blame] | 673 | /* Calculate pre_div and div. */ |
| 674 | while (host->max_clk / pre_div / 16 > clock_fixup && pre_div < 256) |
Dong Aisheng | d31fc00 | 2013-09-13 19:11:32 +0800 | [diff] [blame] | 675 | pre_div *= 2; |
| 676 | |
Yangbo Lu | 1b21a70 | 2020-01-08 12:07:13 +0800 | [diff] [blame] | 677 | while (host->max_clk / pre_div / div > clock_fixup && div < 16) |
Dong Aisheng | d31fc00 | 2013-09-13 19:11:32 +0800 | [diff] [blame] | 678 | div++; |
| 679 | |
Yangbo Lu | 1b21a70 | 2020-01-08 12:07:13 +0800 | [diff] [blame] | 680 | esdhc->div_ratio = pre_div * div; |
| 681 | |
| 682 | /* Limit clock division for HS400 200MHz clock for quirk. */ |
Yangbo Lu | 6079e63 | 2018-11-23 11:15:35 +0800 | [diff] [blame] | 683 | if (esdhc->quirk_limited_clk_division && |
| 684 | clock == MMC_HS200_MAX_DTR && |
| 685 | (host->mmc->ios.timing == MMC_TIMING_MMC_HS400 || |
| 686 | host->flags & SDHCI_HS400_TUNING)) { |
Yangbo Lu | 1b21a70 | 2020-01-08 12:07:13 +0800 | [diff] [blame] | 687 | if (esdhc->div_ratio <= 4) { |
Yangbo Lu | 6079e63 | 2018-11-23 11:15:35 +0800 | [diff] [blame] | 688 | pre_div = 4; |
| 689 | div = 1; |
Yangbo Lu | 1b21a70 | 2020-01-08 12:07:13 +0800 | [diff] [blame] | 690 | } else if (esdhc->div_ratio <= 8) { |
Yangbo Lu | 6079e63 | 2018-11-23 11:15:35 +0800 | [diff] [blame] | 691 | pre_div = 4; |
| 692 | div = 2; |
Yangbo Lu | 1b21a70 | 2020-01-08 12:07:13 +0800 | [diff] [blame] | 693 | } else if (esdhc->div_ratio <= 12) { |
Yangbo Lu | 6079e63 | 2018-11-23 11:15:35 +0800 | [diff] [blame] | 694 | pre_div = 4; |
| 695 | div = 3; |
| 696 | } else { |
Colin Ian King | b11c36d | 2018-12-06 09:24:11 +0000 | [diff] [blame] | 697 | pr_warn("%s: using unsupported clock division.\n", |
Yangbo Lu | 6079e63 | 2018-11-23 11:15:35 +0800 | [diff] [blame] | 698 | mmc_hostname(host->mmc)); |
| 699 | } |
Yangbo Lu | 1b21a70 | 2020-01-08 12:07:13 +0800 | [diff] [blame] | 700 | esdhc->div_ratio = pre_div * div; |
Yangbo Lu | 6079e63 | 2018-11-23 11:15:35 +0800 | [diff] [blame] | 701 | } |
| 702 | |
Yangbo Lu | 1b21a70 | 2020-01-08 12:07:13 +0800 | [diff] [blame] | 703 | host->mmc->actual_clock = host->max_clk / esdhc->div_ratio; |
| 704 | |
Dong Aisheng | d31fc00 | 2013-09-13 19:11:32 +0800 | [diff] [blame] | 705 | dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n", |
Yangbo Lu | 1b21a70 | 2020-01-08 12:07:13 +0800 | [diff] [blame] | 706 | clock, host->mmc->actual_clock); |
| 707 | |
| 708 | /* Set clock division into register. */ |
Dong Aisheng | d31fc00 | 2013-09-13 19:11:32 +0800 | [diff] [blame] | 709 | pre_div >>= 1; |
| 710 | div--; |
| 711 | |
Yangbo Lu | 1b21a70 | 2020-01-08 12:07:13 +0800 | [diff] [blame] | 712 | esdhc_clock_enable(host, false); |
| 713 | |
Dong Aisheng | d31fc00 | 2013-09-13 19:11:32 +0800 | [diff] [blame] | 714 | temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL); |
Yangbo Lu | 1b21a70 | 2020-01-08 12:07:13 +0800 | [diff] [blame] | 715 | temp &= ~ESDHC_CLOCK_MASK; |
| 716 | temp |= ((div << ESDHC_DIVIDER_SHIFT) | |
| 717 | (pre_div << ESDHC_PREDIV_SHIFT)); |
Dong Aisheng | d31fc00 | 2013-09-13 19:11:32 +0800 | [diff] [blame] | 718 | sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL); |
yangbo lu | e87d2db | 2016-12-26 17:46:30 +0800 | [diff] [blame] | 719 | |
Yangbo Lu | 1b21a70 | 2020-01-08 12:07:13 +0800 | [diff] [blame] | 720 | /* |
| 721 | * Wait max 20 ms. If vendor version is 2.2 or lower, do not |
| 722 | * wait clock stable bit which does not exist. |
| 723 | */ |
| 724 | timeout = ktime_add_ms(ktime_get(), 20); |
| 725 | while (esdhc->vendor_ver > VENDOR_V_22) { |
| 726 | bool timedout = ktime_after(ktime_get(), timeout); |
| 727 | |
| 728 | if (sdhci_readl(host, ESDHC_PRSSTAT) & ESDHC_CLOCK_STABLE) |
| 729 | break; |
| 730 | if (timedout) { |
| 731 | pr_err("%s: Internal clock never stabilised.\n", |
| 732 | mmc_hostname(host->mmc)); |
| 733 | break; |
| 734 | } |
| 735 | usleep_range(10, 20); |
| 736 | } |
| 737 | |
| 738 | /* Additional setting for HS400. */ |
Yangbo Lu | 54e08d9 | 2018-11-23 11:15:34 +0800 | [diff] [blame] | 739 | if (host->mmc->ios.timing == MMC_TIMING_MMC_HS400 && |
| 740 | clock == MMC_HS200_MAX_DTR) { |
| 741 | temp = sdhci_readl(host, ESDHC_TBCTL); |
| 742 | sdhci_writel(host, temp | ESDHC_HS400_MODE, ESDHC_TBCTL); |
| 743 | temp = sdhci_readl(host, ESDHC_SDCLKCTL); |
| 744 | sdhci_writel(host, temp | ESDHC_CMD_CLK_CTL, ESDHC_SDCLKCTL); |
| 745 | esdhc_clock_enable(host, true); |
| 746 | |
| 747 | temp = sdhci_readl(host, ESDHC_DLLCFG0); |
Yangbo Lu | 58d0bf8 | 2018-11-23 11:15:36 +0800 | [diff] [blame] | 748 | temp |= ESDHC_DLL_ENABLE; |
| 749 | if (host->mmc->actual_clock == MMC_HS200_MAX_DTR) |
| 750 | temp |= ESDHC_DLL_FREQ_SEL; |
Yangbo Lu | 54e08d9 | 2018-11-23 11:15:34 +0800 | [diff] [blame] | 751 | sdhci_writel(host, temp, ESDHC_DLLCFG0); |
Yangbo Lu | 011fde4 | 2020-10-20 16:11:16 +0800 | [diff] [blame] | 752 | |
| 753 | temp |= ESDHC_DLL_RESET; |
| 754 | sdhci_writel(host, temp, ESDHC_DLLCFG0); |
| 755 | udelay(1); |
| 756 | temp &= ~ESDHC_DLL_RESET; |
| 757 | sdhci_writel(host, temp, ESDHC_DLLCFG0); |
| 758 | |
| 759 | /* Wait max 20 ms */ |
| 760 | if (read_poll_timeout(sdhci_readl, temp, |
| 761 | temp & ESDHC_DLL_STS_SLV_LOCK, |
| 762 | 10, 20000, false, |
| 763 | host, ESDHC_DLLSTAT0)) |
| 764 | pr_err("%s: timeout for delay chain lock.\n", |
| 765 | mmc_hostname(host->mmc)); |
| 766 | |
Yangbo Lu | 54e08d9 | 2018-11-23 11:15:34 +0800 | [diff] [blame] | 767 | temp = sdhci_readl(host, ESDHC_TBCTL); |
| 768 | sdhci_writel(host, temp | ESDHC_HS400_WNDW_ADJUST, ESDHC_TBCTL); |
| 769 | |
| 770 | esdhc_clock_enable(host, false); |
Yangbo Lu | 6e32f65 | 2019-10-09 15:41:39 +0800 | [diff] [blame] | 771 | esdhc_flush_async_fifo(host); |
Yangbo Lu | 54e08d9 | 2018-11-23 11:15:34 +0800 | [diff] [blame] | 772 | } |
Yangbo Lu | 80c7482 | 2020-01-20 17:48:35 +0800 | [diff] [blame] | 773 | esdhc_clock_enable(host, true); |
Jerry Huang | f060bc9 | 2012-02-14 14:05:37 +0800 | [diff] [blame] | 774 | } |
| 775 | |
Russell King | 2317f56 | 2014-04-25 12:57:07 +0100 | [diff] [blame] | 776 | static void esdhc_pltfm_set_bus_width(struct sdhci_host *host, int width) |
Oded Gabbay | 66b50a0 | 2013-06-27 12:00:05 -0400 | [diff] [blame] | 777 | { |
| 778 | u32 ctrl; |
| 779 | |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 780 | ctrl = sdhci_readl(host, ESDHC_PROCTL); |
| 781 | ctrl &= (~ESDHC_CTRL_BUSWIDTH_MASK); |
Oded Gabbay | 66b50a0 | 2013-06-27 12:00:05 -0400 | [diff] [blame] | 782 | switch (width) { |
| 783 | case MMC_BUS_WIDTH_8: |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 784 | ctrl |= ESDHC_CTRL_8BITBUS; |
Oded Gabbay | 66b50a0 | 2013-06-27 12:00:05 -0400 | [diff] [blame] | 785 | break; |
| 786 | |
| 787 | case MMC_BUS_WIDTH_4: |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 788 | ctrl |= ESDHC_CTRL_4BITBUS; |
Oded Gabbay | 66b50a0 | 2013-06-27 12:00:05 -0400 | [diff] [blame] | 789 | break; |
| 790 | |
| 791 | default: |
Oded Gabbay | 66b50a0 | 2013-06-27 12:00:05 -0400 | [diff] [blame] | 792 | break; |
| 793 | } |
| 794 | |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 795 | sdhci_writel(host, ctrl, ESDHC_PROCTL); |
Oded Gabbay | 66b50a0 | 2013-06-27 12:00:05 -0400 | [diff] [blame] | 796 | } |
| 797 | |
Alessio Igor Bogani | 304f0a9 | 2014-12-09 09:40:38 +0100 | [diff] [blame] | 798 | static void esdhc_reset(struct sdhci_host *host, u8 mask) |
| 799 | { |
Yangbo Lu | 48e304c | 2018-11-23 11:15:37 +0800 | [diff] [blame] | 800 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 801 | struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); |
Yangbo Lu | 2aa3d82 | 2020-01-08 12:07:12 +0800 | [diff] [blame] | 802 | u32 val, bus_width = 0; |
yinbo.zhu | f2bc600 | 2017-12-01 15:09:34 +0800 | [diff] [blame] | 803 | |
Yangbo Lu | 2aa3d82 | 2020-01-08 12:07:12 +0800 | [diff] [blame] | 804 | /* |
| 805 | * Add delay to make sure all the DMA transfers are finished |
| 806 | * for quirk. |
| 807 | */ |
Yangbo Lu | f667216 | 2019-12-19 11:23:35 +0800 | [diff] [blame] | 808 | if (esdhc->quirk_delay_before_data_reset && |
| 809 | (mask & SDHCI_RESET_DATA) && |
| 810 | (host->flags & SDHCI_REQ_USE_DMA)) |
| 811 | mdelay(5); |
| 812 | |
Yangbo Lu | 2aa3d82 | 2020-01-08 12:07:12 +0800 | [diff] [blame] | 813 | /* |
| 814 | * Save bus-width for eSDHC whose vendor version is 2.2 |
| 815 | * or lower for data reset. |
| 816 | */ |
| 817 | if ((mask & SDHCI_RESET_DATA) && |
| 818 | (esdhc->vendor_ver <= VENDOR_V_22)) { |
| 819 | val = sdhci_readl(host, ESDHC_PROCTL); |
| 820 | bus_width = val & ESDHC_CTRL_BUSWIDTH_MASK; |
| 821 | } |
| 822 | |
Alessio Igor Bogani | 304f0a9 | 2014-12-09 09:40:38 +0100 | [diff] [blame] | 823 | sdhci_reset(host, mask); |
| 824 | |
Yangbo Lu | 2aa3d82 | 2020-01-08 12:07:12 +0800 | [diff] [blame] | 825 | /* |
| 826 | * Restore bus-width setting and interrupt registers for eSDHC |
| 827 | * whose vendor version is 2.2 or lower for data reset. |
| 828 | */ |
| 829 | if ((mask & SDHCI_RESET_DATA) && |
| 830 | (esdhc->vendor_ver <= VENDOR_V_22)) { |
| 831 | val = sdhci_readl(host, ESDHC_PROCTL); |
| 832 | val &= ~ESDHC_CTRL_BUSWIDTH_MASK; |
| 833 | val |= bus_width; |
| 834 | sdhci_writel(host, val, ESDHC_PROCTL); |
yinbo.zhu | f2bc600 | 2017-12-01 15:09:34 +0800 | [diff] [blame] | 835 | |
Yangbo Lu | 2aa3d82 | 2020-01-08 12:07:12 +0800 | [diff] [blame] | 836 | sdhci_writel(host, host->ier, SDHCI_INT_ENABLE); |
| 837 | sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE); |
| 838 | } |
| 839 | |
| 840 | /* |
| 841 | * Some bits have to be cleaned manually for eSDHC whose spec |
| 842 | * version is higher than 3.0 for all reset. |
| 843 | */ |
| 844 | if ((mask & SDHCI_RESET_ALL) && |
| 845 | (esdhc->spec_ver >= SDHCI_SPEC_300)) { |
yinbo.zhu | f2bc600 | 2017-12-01 15:09:34 +0800 | [diff] [blame] | 846 | val = sdhci_readl(host, ESDHC_TBCTL); |
| 847 | val &= ~ESDHC_TB_EN; |
| 848 | sdhci_writel(host, val, ESDHC_TBCTL); |
Yangbo Lu | 48e304c | 2018-11-23 11:15:37 +0800 | [diff] [blame] | 849 | |
Yangbo Lu | 2aa3d82 | 2020-01-08 12:07:12 +0800 | [diff] [blame] | 850 | /* |
| 851 | * Initialize eSDHC_DLLCFG1[DLL_PD_PULSE_STRETCH_SEL] to |
| 852 | * 0 for quirk. |
| 853 | */ |
Yangbo Lu | 48e304c | 2018-11-23 11:15:37 +0800 | [diff] [blame] | 854 | if (esdhc->quirk_unreliable_pulse_detection) { |
| 855 | val = sdhci_readl(host, ESDHC_DLLCFG1); |
| 856 | val &= ~ESDHC_DLL_PD_PULSE_STRETCH_SEL; |
| 857 | sdhci_writel(host, val, ESDHC_DLLCFG1); |
| 858 | } |
yinbo.zhu | f2bc600 | 2017-12-01 15:09:34 +0800 | [diff] [blame] | 859 | } |
Alessio Igor Bogani | 304f0a9 | 2014-12-09 09:40:38 +0100 | [diff] [blame] | 860 | } |
| 861 | |
yangbo lu | ea35645 | 2017-04-20 16:14:41 +0800 | [diff] [blame] | 862 | /* The SCFG, Supplemental Configuration Unit, provides SoC specific |
| 863 | * configuration and status registers for the device. There is a |
| 864 | * SDHC IO VSEL control register on SCFG for some platforms. It's |
| 865 | * used to support SDHC IO voltage switching. |
| 866 | */ |
| 867 | static const struct of_device_id scfg_device_ids[] = { |
| 868 | { .compatible = "fsl,t1040-scfg", }, |
| 869 | { .compatible = "fsl,ls1012a-scfg", }, |
| 870 | { .compatible = "fsl,ls1046a-scfg", }, |
| 871 | {} |
| 872 | }; |
| 873 | |
| 874 | /* SDHC IO VSEL control register definition */ |
| 875 | #define SCFG_SDHCIOVSELCR 0x408 |
| 876 | #define SDHCIOVSELCR_TGLEN 0x80000000 |
| 877 | #define SDHCIOVSELCR_VSELVAL 0x60000000 |
| 878 | #define SDHCIOVSELCR_SDHC_VS 0x00000001 |
| 879 | |
| 880 | static int esdhc_signal_voltage_switch(struct mmc_host *mmc, |
| 881 | struct mmc_ios *ios) |
| 882 | { |
| 883 | struct sdhci_host *host = mmc_priv(mmc); |
| 884 | struct device_node *scfg_node; |
| 885 | void __iomem *scfg_base = NULL; |
| 886 | u32 sdhciovselcr; |
| 887 | u32 val; |
| 888 | |
| 889 | /* |
| 890 | * Signal Voltage Switching is only applicable for Host Controllers |
| 891 | * v3.00 and above. |
| 892 | */ |
| 893 | if (host->version < SDHCI_SPEC_300) |
| 894 | return 0; |
| 895 | |
| 896 | val = sdhci_readl(host, ESDHC_PROCTL); |
| 897 | |
| 898 | switch (ios->signal_voltage) { |
| 899 | case MMC_SIGNAL_VOLTAGE_330: |
| 900 | val &= ~ESDHC_VOLT_SEL; |
| 901 | sdhci_writel(host, val, ESDHC_PROCTL); |
| 902 | return 0; |
| 903 | case MMC_SIGNAL_VOLTAGE_180: |
| 904 | scfg_node = of_find_matching_node(NULL, scfg_device_ids); |
| 905 | if (scfg_node) |
| 906 | scfg_base = of_iomap(scfg_node, 0); |
| 907 | if (scfg_base) { |
| 908 | sdhciovselcr = SDHCIOVSELCR_TGLEN | |
| 909 | SDHCIOVSELCR_VSELVAL; |
| 910 | iowrite32be(sdhciovselcr, |
| 911 | scfg_base + SCFG_SDHCIOVSELCR); |
| 912 | |
| 913 | val |= ESDHC_VOLT_SEL; |
| 914 | sdhci_writel(host, val, ESDHC_PROCTL); |
| 915 | mdelay(5); |
| 916 | |
| 917 | sdhciovselcr = SDHCIOVSELCR_TGLEN | |
| 918 | SDHCIOVSELCR_SDHC_VS; |
| 919 | iowrite32be(sdhciovselcr, |
| 920 | scfg_base + SCFG_SDHCIOVSELCR); |
| 921 | iounmap(scfg_base); |
| 922 | } else { |
| 923 | val |= ESDHC_VOLT_SEL; |
| 924 | sdhci_writel(host, val, ESDHC_PROCTL); |
| 925 | } |
| 926 | return 0; |
| 927 | default: |
| 928 | return 0; |
| 929 | } |
| 930 | } |
| 931 | |
Yangbo Lu | 22dc132 | 2019-10-09 15:41:40 +0800 | [diff] [blame] | 932 | static struct soc_device_attribute soc_tuning_erratum_type1[] = { |
Yangbo Lu | 5b74223 | 2019-12-12 15:52:19 +0800 | [diff] [blame] | 933 | { .family = "QorIQ T1023", }, |
| 934 | { .family = "QorIQ T1040", }, |
| 935 | { .family = "QorIQ T2080", }, |
| 936 | { .family = "QorIQ LS1021A", }, |
Yangbo Lu | 22dc132 | 2019-10-09 15:41:40 +0800 | [diff] [blame] | 937 | { }, |
| 938 | }; |
| 939 | |
| 940 | static struct soc_device_attribute soc_tuning_erratum_type2[] = { |
Yangbo Lu | 5b74223 | 2019-12-12 15:52:19 +0800 | [diff] [blame] | 941 | { .family = "QorIQ LS1012A", }, |
| 942 | { .family = "QorIQ LS1043A", }, |
| 943 | { .family = "QorIQ LS1046A", }, |
| 944 | { .family = "QorIQ LS1080A", }, |
| 945 | { .family = "QorIQ LS2080A", }, |
| 946 | { .family = "QorIQ LA1575A", }, |
Yinbo Zhu | b1f378a | 2018-08-23 16:48:32 +0800 | [diff] [blame] | 947 | { }, |
| 948 | }; |
| 949 | |
Yangbo Lu | 54e08d9 | 2018-11-23 11:15:34 +0800 | [diff] [blame] | 950 | static void esdhc_tuning_block_enable(struct sdhci_host *host, bool enable) |
yangbo lu | ba49cbd | 2017-04-20 16:14:42 +0800 | [diff] [blame] | 951 | { |
yangbo lu | ba49cbd | 2017-04-20 16:14:42 +0800 | [diff] [blame] | 952 | u32 val; |
| 953 | |
yangbo lu | ba49cbd | 2017-04-20 16:14:42 +0800 | [diff] [blame] | 954 | esdhc_clock_enable(host, false); |
Yangbo Lu | 6e32f65 | 2019-10-09 15:41:39 +0800 | [diff] [blame] | 955 | esdhc_flush_async_fifo(host); |
yangbo lu | ba49cbd | 2017-04-20 16:14:42 +0800 | [diff] [blame] | 956 | |
| 957 | val = sdhci_readl(host, ESDHC_TBCTL); |
Yangbo Lu | 54e08d9 | 2018-11-23 11:15:34 +0800 | [diff] [blame] | 958 | if (enable) |
| 959 | val |= ESDHC_TB_EN; |
| 960 | else |
| 961 | val &= ~ESDHC_TB_EN; |
yangbo lu | ba49cbd | 2017-04-20 16:14:42 +0800 | [diff] [blame] | 962 | sdhci_writel(host, val, ESDHC_TBCTL); |
yangbo lu | ba49cbd | 2017-04-20 16:14:42 +0800 | [diff] [blame] | 963 | |
Yangbo Lu | 54e08d9 | 2018-11-23 11:15:34 +0800 | [diff] [blame] | 964 | esdhc_clock_enable(host, true); |
| 965 | } |
| 966 | |
Yangbo Lu | f3c2082 | 2019-12-12 15:52:18 +0800 | [diff] [blame] | 967 | static void esdhc_tuning_window_ptr(struct sdhci_host *host, u8 *window_start, |
Yangbo Lu | 22dc132 | 2019-10-09 15:41:40 +0800 | [diff] [blame] | 968 | u8 *window_end) |
| 969 | { |
Yangbo Lu | 22dc132 | 2019-10-09 15:41:40 +0800 | [diff] [blame] | 970 | u32 val; |
| 971 | |
Yangbo Lu | 22dc132 | 2019-10-09 15:41:40 +0800 | [diff] [blame] | 972 | /* Write TBCTL[11:8]=4'h8 */ |
| 973 | val = sdhci_readl(host, ESDHC_TBCTL); |
| 974 | val &= ~(0xf << 8); |
| 975 | val |= 8 << 8; |
| 976 | sdhci_writel(host, val, ESDHC_TBCTL); |
| 977 | |
| 978 | mdelay(1); |
| 979 | |
| 980 | /* Read TBCTL[31:0] register and rewrite again */ |
| 981 | val = sdhci_readl(host, ESDHC_TBCTL); |
| 982 | sdhci_writel(host, val, ESDHC_TBCTL); |
| 983 | |
| 984 | mdelay(1); |
| 985 | |
| 986 | /* Read the TBSTAT[31:0] register twice */ |
| 987 | val = sdhci_readl(host, ESDHC_TBSTAT); |
| 988 | val = sdhci_readl(host, ESDHC_TBSTAT); |
| 989 | |
Yangbo Lu | f3c2082 | 2019-12-12 15:52:18 +0800 | [diff] [blame] | 990 | *window_end = val & 0xff; |
| 991 | *window_start = (val >> 8) & 0xff; |
| 992 | } |
| 993 | |
| 994 | static void esdhc_prepare_sw_tuning(struct sdhci_host *host, u8 *window_start, |
| 995 | u8 *window_end) |
| 996 | { |
| 997 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 998 | struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); |
| 999 | u8 start_ptr, end_ptr; |
| 1000 | |
| 1001 | if (esdhc->quirk_tuning_erratum_type1) { |
| 1002 | *window_start = 5 * esdhc->div_ratio; |
| 1003 | *window_end = 3 * esdhc->div_ratio; |
| 1004 | return; |
| 1005 | } |
| 1006 | |
| 1007 | esdhc_tuning_window_ptr(host, &start_ptr, &end_ptr); |
| 1008 | |
Yangbo Lu | 22dc132 | 2019-10-09 15:41:40 +0800 | [diff] [blame] | 1009 | /* Reset data lines by setting ESDHCCTL[RSTD] */ |
| 1010 | sdhci_reset(host, SDHCI_RESET_DATA); |
| 1011 | /* Write 32'hFFFF_FFFF to IRQSTAT register */ |
| 1012 | sdhci_writel(host, 0xFFFFFFFF, SDHCI_INT_STATUS); |
| 1013 | |
Yangbo Lu | 5b74223 | 2019-12-12 15:52:19 +0800 | [diff] [blame] | 1014 | /* If TBSTAT[15:8]-TBSTAT[7:0] > (4 * div_ratio) + 2 |
| 1015 | * or TBSTAT[7:0]-TBSTAT[15:8] > (4 * div_ratio) + 2, |
Yangbo Lu | 22dc132 | 2019-10-09 15:41:40 +0800 | [diff] [blame] | 1016 | * then program TBPTR[TB_WNDW_END_PTR] = 4 * div_ratio |
| 1017 | * and program TBPTR[TB_WNDW_START_PTR] = 8 * div_ratio. |
| 1018 | */ |
Yangbo Lu | 22dc132 | 2019-10-09 15:41:40 +0800 | [diff] [blame] | 1019 | |
Yangbo Lu | 5b74223 | 2019-12-12 15:52:19 +0800 | [diff] [blame] | 1020 | if (abs(start_ptr - end_ptr) > (4 * esdhc->div_ratio + 2)) { |
Yangbo Lu | 22dc132 | 2019-10-09 15:41:40 +0800 | [diff] [blame] | 1021 | *window_start = 8 * esdhc->div_ratio; |
| 1022 | *window_end = 4 * esdhc->div_ratio; |
| 1023 | } else { |
| 1024 | *window_start = 5 * esdhc->div_ratio; |
| 1025 | *window_end = 3 * esdhc->div_ratio; |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | static int esdhc_execute_sw_tuning(struct mmc_host *mmc, u32 opcode, |
| 1030 | u8 window_start, u8 window_end) |
| 1031 | { |
| 1032 | struct sdhci_host *host = mmc_priv(mmc); |
| 1033 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 1034 | struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); |
| 1035 | u32 val; |
| 1036 | int ret; |
| 1037 | |
| 1038 | /* Program TBPTR[TB_WNDW_END_PTR] and TBPTR[TB_WNDW_START_PTR] */ |
| 1039 | val = ((u32)window_start << ESDHC_WNDW_STRT_PTR_SHIFT) & |
| 1040 | ESDHC_WNDW_STRT_PTR_MASK; |
| 1041 | val |= window_end & ESDHC_WNDW_END_PTR_MASK; |
| 1042 | sdhci_writel(host, val, ESDHC_TBPTR); |
| 1043 | |
| 1044 | /* Program the software tuning mode by setting TBCTL[TB_MODE]=2'h3 */ |
| 1045 | val = sdhci_readl(host, ESDHC_TBCTL); |
| 1046 | val &= ~ESDHC_TB_MODE_MASK; |
| 1047 | val |= ESDHC_TB_MODE_SW; |
| 1048 | sdhci_writel(host, val, ESDHC_TBCTL); |
| 1049 | |
| 1050 | esdhc->in_sw_tuning = true; |
| 1051 | ret = sdhci_execute_tuning(mmc, opcode); |
| 1052 | esdhc->in_sw_tuning = false; |
| 1053 | return ret; |
| 1054 | } |
| 1055 | |
Yangbo Lu | 54e08d9 | 2018-11-23 11:15:34 +0800 | [diff] [blame] | 1056 | static int esdhc_execute_tuning(struct mmc_host *mmc, u32 opcode) |
| 1057 | { |
| 1058 | struct sdhci_host *host = mmc_priv(mmc); |
| 1059 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 1060 | struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); |
Yangbo Lu | 22dc132 | 2019-10-09 15:41:40 +0800 | [diff] [blame] | 1061 | u8 window_start, window_end; |
| 1062 | int ret, retries = 1; |
Yangbo Lu | 54e08d9 | 2018-11-23 11:15:34 +0800 | [diff] [blame] | 1063 | bool hs400_tuning; |
Yangbo Lu | 04509d7 | 2019-06-14 16:29:53 +0800 | [diff] [blame] | 1064 | unsigned int clk; |
Yangbo Lu | 54e08d9 | 2018-11-23 11:15:34 +0800 | [diff] [blame] | 1065 | u32 val; |
Yangbo Lu | 54e08d9 | 2018-11-23 11:15:34 +0800 | [diff] [blame] | 1066 | |
Yangbo Lu | 04509d7 | 2019-06-14 16:29:53 +0800 | [diff] [blame] | 1067 | /* For tuning mode, the sd clock divisor value |
| 1068 | * must be larger than 3 according to reference manual. |
| 1069 | */ |
| 1070 | clk = esdhc->peripheral_clock / 3; |
| 1071 | if (host->clock > clk) |
| 1072 | esdhc_of_set_clock(host, clk); |
| 1073 | |
Yangbo Lu | 54e08d9 | 2018-11-23 11:15:34 +0800 | [diff] [blame] | 1074 | esdhc_tuning_block_enable(host, true); |
| 1075 | |
Michael Walle | 0add6e9 | 2020-10-23 00:23:37 +0200 | [diff] [blame] | 1076 | /* |
| 1077 | * The eSDHC controller takes the data timeout value into account |
| 1078 | * during tuning. If the SD card is too slow sending the response, the |
| 1079 | * timer will expire and a "Buffer Read Ready" interrupt without data |
| 1080 | * is triggered. This leads to tuning errors. |
| 1081 | * |
| 1082 | * Just set the timeout to the maximum value because the core will |
| 1083 | * already take care of it in sdhci_send_tuning(). |
| 1084 | */ |
| 1085 | sdhci_writeb(host, 0xe, SDHCI_TIMEOUT_CONTROL); |
| 1086 | |
Yangbo Lu | 54e08d9 | 2018-11-23 11:15:34 +0800 | [diff] [blame] | 1087 | hs400_tuning = host->flags & SDHCI_HS400_TUNING; |
Yangbo Lu | 54e08d9 | 2018-11-23 11:15:34 +0800 | [diff] [blame] | 1088 | |
Yangbo Lu | 22dc132 | 2019-10-09 15:41:40 +0800 | [diff] [blame] | 1089 | do { |
| 1090 | if (esdhc->quirk_limited_clk_division && |
| 1091 | hs400_tuning) |
| 1092 | esdhc_of_set_clock(host, host->clock); |
| 1093 | |
| 1094 | /* Do HW tuning */ |
| 1095 | val = sdhci_readl(host, ESDHC_TBCTL); |
| 1096 | val &= ~ESDHC_TB_MODE_MASK; |
| 1097 | val |= ESDHC_TB_MODE_3; |
| 1098 | sdhci_writel(host, val, ESDHC_TBCTL); |
| 1099 | |
| 1100 | ret = sdhci_execute_tuning(mmc, opcode); |
| 1101 | if (ret) |
| 1102 | break; |
| 1103 | |
Yangbo Lu | 5b74223 | 2019-12-12 15:52:19 +0800 | [diff] [blame] | 1104 | /* For type2 affected platforms of the tuning erratum, |
| 1105 | * tuning may succeed although eSDHC might not have |
| 1106 | * tuned properly. Need to check tuning window. |
| 1107 | */ |
| 1108 | if (esdhc->quirk_tuning_erratum_type2 && |
| 1109 | !host->tuning_err) { |
| 1110 | esdhc_tuning_window_ptr(host, &window_start, |
| 1111 | &window_end); |
| 1112 | if (abs(window_start - window_end) > |
| 1113 | (4 * esdhc->div_ratio + 2)) |
| 1114 | host->tuning_err = -EAGAIN; |
| 1115 | } |
| 1116 | |
Yangbo Lu | 22dc132 | 2019-10-09 15:41:40 +0800 | [diff] [blame] | 1117 | /* If HW tuning fails and triggers erratum, |
| 1118 | * try workaround. |
| 1119 | */ |
| 1120 | ret = host->tuning_err; |
| 1121 | if (ret == -EAGAIN && |
| 1122 | (esdhc->quirk_tuning_erratum_type1 || |
| 1123 | esdhc->quirk_tuning_erratum_type2)) { |
| 1124 | /* Recover HS400 tuning flag */ |
| 1125 | if (hs400_tuning) |
| 1126 | host->flags |= SDHCI_HS400_TUNING; |
| 1127 | pr_info("%s: Hold on to use fixed sampling clock. Try SW tuning!\n", |
| 1128 | mmc_hostname(mmc)); |
| 1129 | /* Do SW tuning */ |
| 1130 | esdhc_prepare_sw_tuning(host, &window_start, |
| 1131 | &window_end); |
| 1132 | ret = esdhc_execute_sw_tuning(mmc, opcode, |
| 1133 | window_start, |
| 1134 | window_end); |
| 1135 | if (ret) |
| 1136 | break; |
| 1137 | |
| 1138 | /* Retry both HW/SW tuning with reduced clock. */ |
| 1139 | ret = host->tuning_err; |
| 1140 | if (ret == -EAGAIN && retries) { |
| 1141 | /* Recover HS400 tuning flag */ |
| 1142 | if (hs400_tuning) |
| 1143 | host->flags |= SDHCI_HS400_TUNING; |
| 1144 | |
| 1145 | clk = host->max_clk / (esdhc->div_ratio + 1); |
| 1146 | esdhc_of_set_clock(host, clk); |
| 1147 | pr_info("%s: Hold on to use fixed sampling clock. Try tuning with reduced clock!\n", |
| 1148 | mmc_hostname(mmc)); |
| 1149 | } else { |
| 1150 | break; |
| 1151 | } |
| 1152 | } else { |
| 1153 | break; |
| 1154 | } |
| 1155 | } while (retries--); |
| 1156 | |
| 1157 | if (ret) { |
| 1158 | esdhc_tuning_block_enable(host, false); |
| 1159 | } else if (hs400_tuning) { |
Yangbo Lu | 54e08d9 | 2018-11-23 11:15:34 +0800 | [diff] [blame] | 1160 | val = sdhci_readl(host, ESDHC_SDTIMNGCTL); |
| 1161 | val |= ESDHC_FLW_CTL_BG; |
| 1162 | sdhci_writel(host, val, ESDHC_SDTIMNGCTL); |
| 1163 | } |
| 1164 | |
Yangbo Lu | 54e08d9 | 2018-11-23 11:15:34 +0800 | [diff] [blame] | 1165 | return ret; |
| 1166 | } |
| 1167 | |
| 1168 | static void esdhc_set_uhs_signaling(struct sdhci_host *host, |
| 1169 | unsigned int timing) |
| 1170 | { |
Yangbo Lu | 47e9e10 | 2020-05-22 11:12:56 +0800 | [diff] [blame] | 1171 | u32 val; |
| 1172 | |
| 1173 | /* |
| 1174 | * There are specific registers setting for HS400 mode. |
| 1175 | * Clean all of them if controller is in HS400 mode to |
| 1176 | * exit HS400 mode before re-setting any speed mode. |
| 1177 | */ |
| 1178 | val = sdhci_readl(host, ESDHC_TBCTL); |
| 1179 | if (val & ESDHC_HS400_MODE) { |
| 1180 | val = sdhci_readl(host, ESDHC_SDTIMNGCTL); |
| 1181 | val &= ~ESDHC_FLW_CTL_BG; |
| 1182 | sdhci_writel(host, val, ESDHC_SDTIMNGCTL); |
| 1183 | |
| 1184 | val = sdhci_readl(host, ESDHC_SDCLKCTL); |
| 1185 | val &= ~ESDHC_CMD_CLK_CTL; |
| 1186 | sdhci_writel(host, val, ESDHC_SDCLKCTL); |
| 1187 | |
| 1188 | esdhc_clock_enable(host, false); |
| 1189 | val = sdhci_readl(host, ESDHC_TBCTL); |
| 1190 | val &= ~ESDHC_HS400_MODE; |
| 1191 | sdhci_writel(host, val, ESDHC_TBCTL); |
| 1192 | esdhc_clock_enable(host, true); |
| 1193 | |
| 1194 | val = sdhci_readl(host, ESDHC_DLLCFG0); |
| 1195 | val &= ~(ESDHC_DLL_ENABLE | ESDHC_DLL_FREQ_SEL); |
| 1196 | sdhci_writel(host, val, ESDHC_DLLCFG0); |
| 1197 | |
| 1198 | val = sdhci_readl(host, ESDHC_TBCTL); |
| 1199 | val &= ~ESDHC_HS400_WNDW_ADJUST; |
| 1200 | sdhci_writel(host, val, ESDHC_TBCTL); |
| 1201 | |
| 1202 | esdhc_tuning_block_enable(host, false); |
| 1203 | } |
| 1204 | |
Yangbo Lu | 54e08d9 | 2018-11-23 11:15:34 +0800 | [diff] [blame] | 1205 | if (timing == MMC_TIMING_MMC_HS400) |
| 1206 | esdhc_tuning_block_enable(host, true); |
| 1207 | else |
| 1208 | sdhci_set_uhs_signaling(host, timing); |
yangbo lu | ba49cbd | 2017-04-20 16:14:42 +0800 | [diff] [blame] | 1209 | } |
| 1210 | |
Yinbo Zhu | b214fe5 | 2019-03-11 02:16:47 +0000 | [diff] [blame] | 1211 | static u32 esdhc_irq(struct sdhci_host *host, u32 intmask) |
| 1212 | { |
Chris Packham | 060522d | 2020-09-03 13:20:29 +1200 | [diff] [blame] | 1213 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 1214 | struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); |
Yinbo Zhu | b214fe5 | 2019-03-11 02:16:47 +0000 | [diff] [blame] | 1215 | u32 command; |
| 1216 | |
Chris Packham | 060522d | 2020-09-03 13:20:29 +1200 | [diff] [blame] | 1217 | if (esdhc->quirk_trans_complete_erratum) { |
Yinbo Zhu | b214fe5 | 2019-03-11 02:16:47 +0000 | [diff] [blame] | 1218 | command = SDHCI_GET_CMD(sdhci_readw(host, |
| 1219 | SDHCI_COMMAND)); |
| 1220 | if (command == MMC_WRITE_MULTIPLE_BLOCK && |
| 1221 | sdhci_readw(host, SDHCI_BLOCK_COUNT) && |
| 1222 | intmask & SDHCI_INT_DATA_END) { |
| 1223 | intmask &= ~SDHCI_INT_DATA_END; |
| 1224 | sdhci_writel(host, SDHCI_INT_DATA_END, |
| 1225 | SDHCI_INT_STATUS); |
| 1226 | } |
| 1227 | } |
| 1228 | return intmask; |
| 1229 | } |
| 1230 | |
Ulf Hansson | 9e48b33 | 2016-07-27 11:01:48 +0200 | [diff] [blame] | 1231 | #ifdef CONFIG_PM_SLEEP |
Russell King | 723f792 | 2014-04-25 12:59:46 +0100 | [diff] [blame] | 1232 | static u32 esdhc_proctl; |
| 1233 | static int esdhc_of_suspend(struct device *dev) |
| 1234 | { |
| 1235 | struct sdhci_host *host = dev_get_drvdata(dev); |
| 1236 | |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 1237 | esdhc_proctl = sdhci_readl(host, SDHCI_HOST_CONTROL); |
Russell King | 723f792 | 2014-04-25 12:59:46 +0100 | [diff] [blame] | 1238 | |
Adrian Hunter | d38dcad | 2017-03-20 19:50:32 +0200 | [diff] [blame] | 1239 | if (host->tuning_mode != SDHCI_TUNING_MODE_3) |
| 1240 | mmc_retune_needed(host->mmc); |
| 1241 | |
Russell King | 723f792 | 2014-04-25 12:59:46 +0100 | [diff] [blame] | 1242 | return sdhci_suspend_host(host); |
| 1243 | } |
| 1244 | |
Ulf Hansson | 06732b8 | 2014-05-23 10:36:44 +0200 | [diff] [blame] | 1245 | static int esdhc_of_resume(struct device *dev) |
Russell King | 723f792 | 2014-04-25 12:59:46 +0100 | [diff] [blame] | 1246 | { |
| 1247 | struct sdhci_host *host = dev_get_drvdata(dev); |
| 1248 | int ret = sdhci_resume_host(host); |
| 1249 | |
| 1250 | if (ret == 0) { |
| 1251 | /* Isn't this already done by sdhci_resume_host() ? --rmk */ |
| 1252 | esdhc_of_enable_dma(host); |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 1253 | sdhci_writel(host, esdhc_proctl, SDHCI_HOST_CONTROL); |
Russell King | 723f792 | 2014-04-25 12:59:46 +0100 | [diff] [blame] | 1254 | } |
Russell King | 723f792 | 2014-04-25 12:59:46 +0100 | [diff] [blame] | 1255 | return ret; |
| 1256 | } |
Russell King | 723f792 | 2014-04-25 12:59:46 +0100 | [diff] [blame] | 1257 | #endif |
| 1258 | |
Ulf Hansson | 9e48b33 | 2016-07-27 11:01:48 +0200 | [diff] [blame] | 1259 | static SIMPLE_DEV_PM_OPS(esdhc_of_dev_pm_ops, |
| 1260 | esdhc_of_suspend, |
| 1261 | esdhc_of_resume); |
| 1262 | |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 1263 | static const struct sdhci_ops sdhci_esdhc_be_ops = { |
| 1264 | .read_l = esdhc_be_readl, |
| 1265 | .read_w = esdhc_be_readw, |
| 1266 | .read_b = esdhc_be_readb, |
| 1267 | .write_l = esdhc_be_writel, |
| 1268 | .write_w = esdhc_be_writew, |
| 1269 | .write_b = esdhc_be_writeb, |
| 1270 | .set_clock = esdhc_of_set_clock, |
| 1271 | .enable_dma = esdhc_of_enable_dma, |
| 1272 | .get_max_clock = esdhc_of_get_max_clock, |
| 1273 | .get_min_clock = esdhc_of_get_min_clock, |
| 1274 | .adma_workaround = esdhc_of_adma_workaround, |
| 1275 | .set_bus_width = esdhc_pltfm_set_bus_width, |
| 1276 | .reset = esdhc_reset, |
Yangbo Lu | 54e08d9 | 2018-11-23 11:15:34 +0800 | [diff] [blame] | 1277 | .set_uhs_signaling = esdhc_set_uhs_signaling, |
Yinbo Zhu | b214fe5 | 2019-03-11 02:16:47 +0000 | [diff] [blame] | 1278 | .irq = esdhc_irq, |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 1279 | }; |
| 1280 | |
| 1281 | static const struct sdhci_ops sdhci_esdhc_le_ops = { |
| 1282 | .read_l = esdhc_le_readl, |
| 1283 | .read_w = esdhc_le_readw, |
| 1284 | .read_b = esdhc_le_readb, |
| 1285 | .write_l = esdhc_le_writel, |
| 1286 | .write_w = esdhc_le_writew, |
| 1287 | .write_b = esdhc_le_writeb, |
| 1288 | .set_clock = esdhc_of_set_clock, |
| 1289 | .enable_dma = esdhc_of_enable_dma, |
| 1290 | .get_max_clock = esdhc_of_get_max_clock, |
| 1291 | .get_min_clock = esdhc_of_get_min_clock, |
| 1292 | .adma_workaround = esdhc_of_adma_workaround, |
| 1293 | .set_bus_width = esdhc_pltfm_set_bus_width, |
| 1294 | .reset = esdhc_reset, |
Yangbo Lu | 54e08d9 | 2018-11-23 11:15:34 +0800 | [diff] [blame] | 1295 | .set_uhs_signaling = esdhc_set_uhs_signaling, |
Yinbo Zhu | b214fe5 | 2019-03-11 02:16:47 +0000 | [diff] [blame] | 1296 | .irq = esdhc_irq, |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 1297 | }; |
| 1298 | |
| 1299 | static const struct sdhci_pltfm_data sdhci_esdhc_be_pdata = { |
yangbo lu | e9acc77 | 2016-12-26 17:40:44 +0800 | [diff] [blame] | 1300 | .quirks = ESDHC_DEFAULT_QUIRKS | |
| 1301 | #ifdef CONFIG_PPC |
| 1302 | SDHCI_QUIRK_BROKEN_CARD_DETECTION | |
| 1303 | #endif |
| 1304 | SDHCI_QUIRK_NO_CARD_NO_RESET | |
| 1305 | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC, |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 1306 | .ops = &sdhci_esdhc_be_ops, |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 1307 | }; |
Shawn Guo | 38576af | 2011-05-27 23:48:14 +0800 | [diff] [blame] | 1308 | |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 1309 | static const struct sdhci_pltfm_data sdhci_esdhc_le_pdata = { |
yangbo lu | e9acc77 | 2016-12-26 17:40:44 +0800 | [diff] [blame] | 1310 | .quirks = ESDHC_DEFAULT_QUIRKS | |
| 1311 | SDHCI_QUIRK_NO_CARD_NO_RESET | |
| 1312 | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC, |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 1313 | .ops = &sdhci_esdhc_le_ops, |
| 1314 | }; |
| 1315 | |
yangbo lu | 151ede4 | 2016-11-09 11:14:12 +0800 | [diff] [blame] | 1316 | static struct soc_device_attribute soc_incorrect_hostver[] = { |
| 1317 | { .family = "QorIQ T4240", .revision = "1.0", }, |
| 1318 | { .family = "QorIQ T4240", .revision = "2.0", }, |
| 1319 | { }, |
| 1320 | }; |
| 1321 | |
Yangbo Lu | 6079e63 | 2018-11-23 11:15:35 +0800 | [diff] [blame] | 1322 | static struct soc_device_attribute soc_fixup_sdhc_clkdivs[] = { |
| 1323 | { .family = "QorIQ LX2160A", .revision = "1.0", }, |
Yinbo Zhu | 8e9a691 | 2019-03-07 02:32:44 +0000 | [diff] [blame] | 1324 | { .family = "QorIQ LX2160A", .revision = "2.0", }, |
Yinbo Zhu | 5f3ad19 | 2019-08-14 15:26:49 +0800 | [diff] [blame] | 1325 | { .family = "QorIQ LS1028A", .revision = "1.0", }, |
Yangbo Lu | 6079e63 | 2018-11-23 11:15:35 +0800 | [diff] [blame] | 1326 | { }, |
| 1327 | }; |
| 1328 | |
Yangbo Lu | 48e304c | 2018-11-23 11:15:37 +0800 | [diff] [blame] | 1329 | static struct soc_device_attribute soc_unreliable_pulse_detection[] = { |
| 1330 | { .family = "QorIQ LX2160A", .revision = "1.0", }, |
Yangbo Lu | 71b0532 | 2020-11-10 15:13:14 +0800 | [diff] [blame] | 1331 | { .family = "QorIQ LX2160A", .revision = "2.0", }, |
| 1332 | { .family = "QorIQ LS1028A", .revision = "1.0", }, |
Yangbo Lu | 48e304c | 2018-11-23 11:15:37 +0800 | [diff] [blame] | 1333 | { }, |
| 1334 | }; |
| 1335 | |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 1336 | static void esdhc_init(struct platform_device *pdev, struct sdhci_host *host) |
| 1337 | { |
yinbo.zhu | 67fdfbd | 2018-06-25 16:46:24 +0800 | [diff] [blame] | 1338 | const struct of_device_id *match; |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 1339 | struct sdhci_pltfm_host *pltfm_host; |
| 1340 | struct sdhci_esdhc *esdhc; |
yangbo lu | 19c3a0e | 2017-04-20 16:14:40 +0800 | [diff] [blame] | 1341 | struct device_node *np; |
| 1342 | struct clk *clk; |
| 1343 | u32 val; |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 1344 | u16 host_ver; |
| 1345 | |
| 1346 | pltfm_host = sdhci_priv(host); |
Jisheng Zhang | 8605e7a | 2016-02-16 21:08:26 +0800 | [diff] [blame] | 1347 | esdhc = sdhci_pltfm_priv(pltfm_host); |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 1348 | |
| 1349 | host_ver = sdhci_readw(host, SDHCI_HOST_VERSION); |
| 1350 | esdhc->vendor_ver = (host_ver & SDHCI_VENDOR_VER_MASK) >> |
| 1351 | SDHCI_VENDOR_VER_SHIFT; |
| 1352 | esdhc->spec_ver = host_ver & SDHCI_SPEC_VER_MASK; |
yangbo lu | 151ede4 | 2016-11-09 11:14:12 +0800 | [diff] [blame] | 1353 | if (soc_device_match(soc_incorrect_hostver)) |
| 1354 | esdhc->quirk_incorrect_hostver = true; |
| 1355 | else |
| 1356 | esdhc->quirk_incorrect_hostver = false; |
yangbo lu | 19c3a0e | 2017-04-20 16:14:40 +0800 | [diff] [blame] | 1357 | |
Yangbo Lu | 6079e63 | 2018-11-23 11:15:35 +0800 | [diff] [blame] | 1358 | if (soc_device_match(soc_fixup_sdhc_clkdivs)) |
| 1359 | esdhc->quirk_limited_clk_division = true; |
| 1360 | else |
| 1361 | esdhc->quirk_limited_clk_division = false; |
| 1362 | |
Yangbo Lu | 48e304c | 2018-11-23 11:15:37 +0800 | [diff] [blame] | 1363 | if (soc_device_match(soc_unreliable_pulse_detection)) |
| 1364 | esdhc->quirk_unreliable_pulse_detection = true; |
| 1365 | else |
| 1366 | esdhc->quirk_unreliable_pulse_detection = false; |
| 1367 | |
yinbo.zhu | 67fdfbd | 2018-06-25 16:46:24 +0800 | [diff] [blame] | 1368 | match = of_match_node(sdhci_esdhc_of_match, pdev->dev.of_node); |
| 1369 | if (match) |
| 1370 | esdhc->clk_fixup = match->data; |
yangbo lu | 19c3a0e | 2017-04-20 16:14:40 +0800 | [diff] [blame] | 1371 | np = pdev->dev.of_node; |
Yangbo Lu | f667216 | 2019-12-19 11:23:35 +0800 | [diff] [blame] | 1372 | |
Chris Packham | 060522d | 2020-09-03 13:20:29 +1200 | [diff] [blame] | 1373 | if (of_device_is_compatible(np, "fsl,p2020-esdhc")) { |
Yangbo Lu | f667216 | 2019-12-19 11:23:35 +0800 | [diff] [blame] | 1374 | esdhc->quirk_delay_before_data_reset = true; |
Chris Packham | 060522d | 2020-09-03 13:20:29 +1200 | [diff] [blame] | 1375 | esdhc->quirk_trans_complete_erratum = true; |
| 1376 | } |
Yangbo Lu | f667216 | 2019-12-19 11:23:35 +0800 | [diff] [blame] | 1377 | |
yangbo lu | 19c3a0e | 2017-04-20 16:14:40 +0800 | [diff] [blame] | 1378 | clk = of_clk_get(np, 0); |
| 1379 | if (!IS_ERR(clk)) { |
| 1380 | /* |
| 1381 | * esdhc->peripheral_clock would be assigned with a value |
| 1382 | * which is eSDHC base clock when use periperal clock. |
Yangbo Lu | 791463b | 2019-06-14 16:29:54 +0800 | [diff] [blame] | 1383 | * For some platforms, the clock value got by common clk |
| 1384 | * API is peripheral clock while the eSDHC base clock is |
| 1385 | * 1/2 peripheral clock. |
yangbo lu | 19c3a0e | 2017-04-20 16:14:40 +0800 | [diff] [blame] | 1386 | */ |
Yangbo Lu | 791463b | 2019-06-14 16:29:54 +0800 | [diff] [blame] | 1387 | if (of_device_is_compatible(np, "fsl,ls1046a-esdhc") || |
Yangbo Lu | 66a83fe | 2019-12-16 17:19:11 +0800 | [diff] [blame] | 1388 | of_device_is_compatible(np, "fsl,ls1028a-esdhc") || |
| 1389 | of_device_is_compatible(np, "fsl,ls1088a-esdhc")) |
yangbo lu | 19c3a0e | 2017-04-20 16:14:40 +0800 | [diff] [blame] | 1390 | esdhc->peripheral_clock = clk_get_rate(clk) / 2; |
| 1391 | else |
| 1392 | esdhc->peripheral_clock = clk_get_rate(clk); |
| 1393 | |
| 1394 | clk_put(clk); |
| 1395 | } |
| 1396 | |
Yangbo Lu | 975520f | 2020-09-27 16:23:04 +0800 | [diff] [blame] | 1397 | esdhc_clock_enable(host, false); |
| 1398 | val = sdhci_readl(host, ESDHC_DMA_SYSCTL); |
| 1399 | /* |
| 1400 | * This bit is not able to be reset by SDHCI_RESET_ALL. Need to |
| 1401 | * initialize it as 1 or 0 once, to override the different value |
| 1402 | * which may be configured in bootloader. |
| 1403 | */ |
| 1404 | if (esdhc->peripheral_clock) |
yangbo lu | 19c3a0e | 2017-04-20 16:14:40 +0800 | [diff] [blame] | 1405 | val |= ESDHC_PERIPHERAL_CLK_SEL; |
Yangbo Lu | 975520f | 2020-09-27 16:23:04 +0800 | [diff] [blame] | 1406 | else |
| 1407 | val &= ~ESDHC_PERIPHERAL_CLK_SEL; |
| 1408 | sdhci_writel(host, val, ESDHC_DMA_SYSCTL); |
| 1409 | esdhc_clock_enable(host, true); |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 1410 | } |
| 1411 | |
Yangbo Lu | 54e08d9 | 2018-11-23 11:15:34 +0800 | [diff] [blame] | 1412 | static int esdhc_hs400_prepare_ddr(struct mmc_host *mmc) |
| 1413 | { |
| 1414 | esdhc_tuning_block_enable(mmc_priv(mmc), false); |
| 1415 | return 0; |
| 1416 | } |
| 1417 | |
Bill Pemberton | c3be1ef | 2012-11-19 13:23:06 -0500 | [diff] [blame] | 1418 | static int sdhci_esdhc_probe(struct platform_device *pdev) |
Shawn Guo | 38576af | 2011-05-27 23:48:14 +0800 | [diff] [blame] | 1419 | { |
Oded Gabbay | 66b50a0 | 2013-06-27 12:00:05 -0400 | [diff] [blame] | 1420 | struct sdhci_host *host; |
Oded Gabbay | dcaff04 | 2013-07-05 12:48:35 -0400 | [diff] [blame] | 1421 | struct device_node *np; |
yangbo lu | 1ef5e49 | 2015-11-25 10:05:37 +0800 | [diff] [blame] | 1422 | struct sdhci_pltfm_host *pltfm_host; |
| 1423 | struct sdhci_esdhc *esdhc; |
Oded Gabbay | 66b50a0 | 2013-06-27 12:00:05 -0400 | [diff] [blame] | 1424 | int ret; |
| 1425 | |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 1426 | np = pdev->dev.of_node; |
| 1427 | |
Julia Lawall | 150d424 | 2016-08-05 10:56:46 +0200 | [diff] [blame] | 1428 | if (of_property_read_bool(np, "little-endian")) |
Jisheng Zhang | 8605e7a | 2016-02-16 21:08:26 +0800 | [diff] [blame] | 1429 | host = sdhci_pltfm_init(pdev, &sdhci_esdhc_le_pdata, |
| 1430 | sizeof(struct sdhci_esdhc)); |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 1431 | else |
Jisheng Zhang | 8605e7a | 2016-02-16 21:08:26 +0800 | [diff] [blame] | 1432 | host = sdhci_pltfm_init(pdev, &sdhci_esdhc_be_pdata, |
| 1433 | sizeof(struct sdhci_esdhc)); |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 1434 | |
Oded Gabbay | 66b50a0 | 2013-06-27 12:00:05 -0400 | [diff] [blame] | 1435 | if (IS_ERR(host)) |
| 1436 | return PTR_ERR(host); |
| 1437 | |
yangbo lu | ea35645 | 2017-04-20 16:14:41 +0800 | [diff] [blame] | 1438 | host->mmc_host_ops.start_signal_voltage_switch = |
| 1439 | esdhc_signal_voltage_switch; |
yangbo lu | ba49cbd | 2017-04-20 16:14:42 +0800 | [diff] [blame] | 1440 | host->mmc_host_ops.execute_tuning = esdhc_execute_tuning; |
Yangbo Lu | 54e08d9 | 2018-11-23 11:15:34 +0800 | [diff] [blame] | 1441 | host->mmc_host_ops.hs400_prepare_ddr = esdhc_hs400_prepare_ddr; |
yangbo lu | 6b236f3 | 2017-04-20 16:14:44 +0800 | [diff] [blame] | 1442 | host->tuning_delay = 1; |
yangbo lu | ea35645 | 2017-04-20 16:14:41 +0800 | [diff] [blame] | 1443 | |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 1444 | esdhc_init(pdev, host); |
| 1445 | |
Oded Gabbay | 66b50a0 | 2013-06-27 12:00:05 -0400 | [diff] [blame] | 1446 | sdhci_get_of_property(pdev); |
| 1447 | |
yangbo lu | 1ef5e49 | 2015-11-25 10:05:37 +0800 | [diff] [blame] | 1448 | pltfm_host = sdhci_priv(host); |
Jisheng Zhang | 8605e7a | 2016-02-16 21:08:26 +0800 | [diff] [blame] | 1449 | esdhc = sdhci_pltfm_priv(pltfm_host); |
Yangbo Lu | 22dc132 | 2019-10-09 15:41:40 +0800 | [diff] [blame] | 1450 | if (soc_device_match(soc_tuning_erratum_type1)) |
| 1451 | esdhc->quirk_tuning_erratum_type1 = true; |
Yinbo Zhu | b1f378a | 2018-08-23 16:48:32 +0800 | [diff] [blame] | 1452 | else |
Yangbo Lu | 22dc132 | 2019-10-09 15:41:40 +0800 | [diff] [blame] | 1453 | esdhc->quirk_tuning_erratum_type1 = false; |
| 1454 | |
| 1455 | if (soc_device_match(soc_tuning_erratum_type2)) |
| 1456 | esdhc->quirk_tuning_erratum_type2 = true; |
| 1457 | else |
| 1458 | esdhc->quirk_tuning_erratum_type2 = false; |
Yinbo Zhu | b1f378a | 2018-08-23 16:48:32 +0800 | [diff] [blame] | 1459 | |
yangbo lu | 1ef5e49 | 2015-11-25 10:05:37 +0800 | [diff] [blame] | 1460 | if (esdhc->vendor_ver == VENDOR_V_22) |
| 1461 | host->quirks2 |= SDHCI_QUIRK2_HOST_NO_CMD23; |
| 1462 | |
| 1463 | if (esdhc->vendor_ver > VENDOR_V_22) |
| 1464 | host->quirks &= ~SDHCI_QUIRK_NO_BUSY_IRQ; |
| 1465 | |
Yinbo Zhu | 05cb6b2 | 2019-03-11 02:16:40 +0000 | [diff] [blame] | 1466 | if (of_find_compatible_node(NULL, NULL, "fsl,p2020-esdhc")) { |
Yangbo Lu | fe0acab | 2019-12-16 11:18:42 +0800 | [diff] [blame] | 1467 | host->quirks |= SDHCI_QUIRK_RESET_AFTER_REQUEST; |
| 1468 | host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL; |
Yinbo Zhu | 05cb6b2 | 2019-03-11 02:16:40 +0000 | [diff] [blame] | 1469 | } |
Yinbo Zhu | a46e427 | 2019-03-11 02:16:36 +0000 | [diff] [blame] | 1470 | |
Yangbo Lu | 74fd5e3 | 2015-06-01 13:47:12 +0800 | [diff] [blame] | 1471 | if (of_device_is_compatible(np, "fsl,p5040-esdhc") || |
| 1472 | of_device_is_compatible(np, "fsl,p5020-esdhc") || |
| 1473 | of_device_is_compatible(np, "fsl,p4080-esdhc") || |
| 1474 | of_device_is_compatible(np, "fsl,p1020-esdhc") || |
yangbo lu | e9acc77 | 2016-12-26 17:40:44 +0800 | [diff] [blame] | 1475 | of_device_is_compatible(np, "fsl,t1040-esdhc")) |
Yangbo Lu | 74fd5e3 | 2015-06-01 13:47:12 +0800 | [diff] [blame] | 1476 | host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION; |
| 1477 | |
yangbo lu | a22950c | 2015-10-08 18:36:57 +0800 | [diff] [blame] | 1478 | if (of_device_is_compatible(np, "fsl,ls1021a-esdhc")) |
| 1479 | host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL; |
| 1480 | |
Yangbo Lu | 1f1929f | 2019-03-11 02:16:51 +0000 | [diff] [blame] | 1481 | esdhc->quirk_ignore_data_inhibit = false; |
Oded Gabbay | dcaff04 | 2013-07-05 12:48:35 -0400 | [diff] [blame] | 1482 | if (of_device_is_compatible(np, "fsl,p2020-esdhc")) { |
| 1483 | /* |
| 1484 | * Freescale messed up with P2020 as it has a non-standard |
| 1485 | * host control register |
| 1486 | */ |
| 1487 | host->quirks2 |= SDHCI_QUIRK2_BROKEN_HOST_CONTROL; |
Yangbo Lu | 1f1929f | 2019-03-11 02:16:51 +0000 | [diff] [blame] | 1488 | esdhc->quirk_ignore_data_inhibit = true; |
Oded Gabbay | dcaff04 | 2013-07-05 12:48:35 -0400 | [diff] [blame] | 1489 | } |
| 1490 | |
Oded Gabbay | 66b50a0 | 2013-06-27 12:00:05 -0400 | [diff] [blame] | 1491 | /* call to generic mmc_of_parse to support additional capabilities */ |
Ulf Hansson | f099140 | 2014-12-18 10:41:41 +0100 | [diff] [blame] | 1492 | ret = mmc_of_parse(host->mmc); |
| 1493 | if (ret) |
| 1494 | goto err; |
| 1495 | |
Andy Shevchenko | 6dab809b | 2021-04-19 14:24:55 +0300 | [diff] [blame] | 1496 | mmc_of_parse_voltage(host->mmc, &host->ocr_mask); |
Oded Gabbay | 66b50a0 | 2013-06-27 12:00:05 -0400 | [diff] [blame] | 1497 | |
| 1498 | ret = sdhci_add_host(host); |
| 1499 | if (ret) |
Ulf Hansson | f099140 | 2014-12-18 10:41:41 +0100 | [diff] [blame] | 1500 | goto err; |
Oded Gabbay | 66b50a0 | 2013-06-27 12:00:05 -0400 | [diff] [blame] | 1501 | |
Ulf Hansson | f099140 | 2014-12-18 10:41:41 +0100 | [diff] [blame] | 1502 | return 0; |
| 1503 | err: |
| 1504 | sdhci_pltfm_free(pdev); |
Oded Gabbay | 66b50a0 | 2013-06-27 12:00:05 -0400 | [diff] [blame] | 1505 | return ret; |
Shawn Guo | 38576af | 2011-05-27 23:48:14 +0800 | [diff] [blame] | 1506 | } |
| 1507 | |
Shawn Guo | 38576af | 2011-05-27 23:48:14 +0800 | [diff] [blame] | 1508 | static struct platform_driver sdhci_esdhc_driver = { |
| 1509 | .driver = { |
| 1510 | .name = "sdhci-esdhc", |
Douglas Anderson | 21b2cec | 2020-09-03 16:24:36 -0700 | [diff] [blame] | 1511 | .probe_type = PROBE_PREFER_ASYNCHRONOUS, |
Shawn Guo | 38576af | 2011-05-27 23:48:14 +0800 | [diff] [blame] | 1512 | .of_match_table = sdhci_esdhc_of_match, |
Ulf Hansson | 9e48b33 | 2016-07-27 11:01:48 +0200 | [diff] [blame] | 1513 | .pm = &esdhc_of_dev_pm_ops, |
Shawn Guo | 38576af | 2011-05-27 23:48:14 +0800 | [diff] [blame] | 1514 | }, |
| 1515 | .probe = sdhci_esdhc_probe, |
Kevin Hao | caebcae | 2015-02-27 15:47:31 +0800 | [diff] [blame] | 1516 | .remove = sdhci_pltfm_unregister, |
Shawn Guo | 38576af | 2011-05-27 23:48:14 +0800 | [diff] [blame] | 1517 | }; |
| 1518 | |
Axel Lin | d1f81a6 | 2011-11-26 12:55:43 +0800 | [diff] [blame] | 1519 | module_platform_driver(sdhci_esdhc_driver); |
Shawn Guo | 38576af | 2011-05-27 23:48:14 +0800 | [diff] [blame] | 1520 | |
| 1521 | MODULE_DESCRIPTION("SDHCI OF driver for Freescale MPC eSDHC"); |
| 1522 | MODULE_AUTHOR("Xiaobo Xie <X.Xie@freescale.com>, " |
| 1523 | "Anton Vorontsov <avorontsov@ru.mvista.com>"); |
| 1524 | MODULE_LICENSE("GPL v2"); |