Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Freescale eSDHC controller driver. |
| 3 | * |
Jerry Huang | f060bc9 | 2012-02-14 14:05:37 +0800 | [diff] [blame] | 4 | * Copyright (c) 2007, 2010, 2012 Freescale Semiconductor, Inc. |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 5 | * Copyright (c) 2009 MontaVista Software, Inc. |
| 6 | * |
| 7 | * Authors: Xiaobo Xie <X.Xie@freescale.com> |
| 8 | * Anton Vorontsov <avorontsov@ru.mvista.com> |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or (at |
| 13 | * your option) any later version. |
| 14 | */ |
| 15 | |
Oded Gabbay | 66b50a0 | 2013-06-27 12:00:05 -0400 | [diff] [blame] | 16 | #include <linux/err.h> |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 17 | #include <linux/io.h> |
Jerry Huang | f060bc9 | 2012-02-14 14:05:37 +0800 | [diff] [blame] | 18 | #include <linux/of.h> |
yangbo lu | ea35645 | 2017-04-20 16:14:41 +0800 | [diff] [blame] | 19 | #include <linux/of_address.h> |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 20 | #include <linux/delay.h> |
Paul Gortmaker | 88b4767 | 2011-07-03 15:15:51 -0400 | [diff] [blame] | 21 | #include <linux/module.h> |
yangbo lu | 151ede4 | 2016-11-09 11:14:12 +0800 | [diff] [blame] | 22 | #include <linux/sys_soc.h> |
yangbo lu | 19c3a0e | 2017-04-20 16:14:40 +0800 | [diff] [blame] | 23 | #include <linux/clk.h> |
| 24 | #include <linux/ktime.h> |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 25 | #include <linux/mmc/host.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 | |
| 32 | struct sdhci_esdhc { |
| 33 | u8 vendor_ver; |
| 34 | u8 spec_ver; |
yangbo lu | 151ede4 | 2016-11-09 11:14:12 +0800 | [diff] [blame] | 35 | bool quirk_incorrect_hostver; |
yangbo lu | 19c3a0e | 2017-04-20 16:14:40 +0800 | [diff] [blame] | 36 | unsigned int peripheral_clock; |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | /** |
| 40 | * esdhc_read*_fixup - Fixup the value read from incompatible eSDHC register |
| 41 | * to make it compatible with SD spec. |
| 42 | * |
| 43 | * @host: pointer to sdhci_host |
| 44 | * @spec_reg: SD spec register address |
| 45 | * @value: 32bit eSDHC register value on spec_reg address |
| 46 | * |
| 47 | * In SD spec, there are 8/16/32/64 bits registers, while all of eSDHC |
| 48 | * registers are 32 bits. There are differences in register size, register |
| 49 | * address, register function, bit position and function between eSDHC spec |
| 50 | * and SD spec. |
| 51 | * |
| 52 | * Return a fixed up register value |
| 53 | */ |
| 54 | static u32 esdhc_readl_fixup(struct sdhci_host *host, |
| 55 | int spec_reg, u32 value) |
Jerry Huang | 137ccd4 | 2012-03-08 11:25:02 +0800 | [diff] [blame] | 56 | { |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 57 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
Jisheng Zhang | 8605e7a | 2016-02-16 21:08:26 +0800 | [diff] [blame] | 58 | struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); |
Jerry Huang | 137ccd4 | 2012-03-08 11:25:02 +0800 | [diff] [blame] | 59 | u32 ret; |
| 60 | |
Jerry Huang | 137ccd4 | 2012-03-08 11:25:02 +0800 | [diff] [blame] | 61 | /* |
| 62 | * The bit of ADMA flag in eSDHC is not compatible with standard |
| 63 | * SDHC register, so set fake flag SDHCI_CAN_DO_ADMA2 when ADMA is |
| 64 | * supported by eSDHC. |
| 65 | * And for many FSL eSDHC controller, the reset value of field |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 66 | * 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] | 67 | * only these vendor version is greater than 2.2/0x12 support ADMA. |
Jerry Huang | 137ccd4 | 2012-03-08 11:25:02 +0800 | [diff] [blame] | 68 | */ |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 69 | if ((spec_reg == SDHCI_CAPABILITIES) && (value & SDHCI_CAN_DO_ADMA1)) { |
| 70 | if (esdhc->vendor_ver > VENDOR_V_22) { |
| 71 | ret = value | SDHCI_CAN_DO_ADMA2; |
| 72 | return ret; |
| 73 | } |
Jerry Huang | 137ccd4 | 2012-03-08 11:25:02 +0800 | [diff] [blame] | 74 | } |
Michael Walle | b0921d5 | 2016-11-15 11:13:16 +0100 | [diff] [blame] | 75 | /* |
| 76 | * The DAT[3:0] line signal levels and the CMD line signal level are |
| 77 | * not compatible with standard SDHC register. The line signal levels |
| 78 | * DAT[7:0] are at bits 31:24 and the command line signal level is at |
| 79 | * bit 23. All other bits are the same as in the standard SDHC |
| 80 | * register. |
| 81 | */ |
| 82 | if (spec_reg == SDHCI_PRESENT_STATE) { |
| 83 | ret = value & 0x000fffff; |
| 84 | ret |= (value >> 4) & SDHCI_DATA_LVL_MASK; |
| 85 | ret |= (value << 1) & SDHCI_CMD_LVL; |
| 86 | return ret; |
| 87 | } |
| 88 | |
yangbo lu | 2f3110c | 2017-08-15 10:17:03 +0800 | [diff] [blame] | 89 | /* |
| 90 | * DTS properties of mmc host are used to enable each speed mode |
| 91 | * according to soc and board capability. So clean up |
| 92 | * SDR50/SDR104/DDR50 support bits here. |
| 93 | */ |
| 94 | if (spec_reg == SDHCI_CAPABILITIES_1) { |
| 95 | ret = value & ~(SDHCI_SUPPORT_SDR50 | SDHCI_SUPPORT_SDR104 | |
| 96 | SDHCI_SUPPORT_DDR50); |
| 97 | return ret; |
| 98 | } |
| 99 | |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 100 | ret = value; |
Jerry Huang | 137ccd4 | 2012-03-08 11:25:02 +0800 | [diff] [blame] | 101 | return ret; |
| 102 | } |
| 103 | |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 104 | static u16 esdhc_readw_fixup(struct sdhci_host *host, |
| 105 | int spec_reg, u32 value) |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 106 | { |
yangbo lu | 151ede4 | 2016-11-09 11:14:12 +0800 | [diff] [blame] | 107 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 108 | struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 109 | u16 ret; |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 110 | int shift = (spec_reg & 0x2) * 8; |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 111 | |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 112 | if (spec_reg == SDHCI_HOST_VERSION) |
| 113 | ret = value & 0xffff; |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 114 | else |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 115 | ret = (value >> shift) & 0xffff; |
yangbo lu | 151ede4 | 2016-11-09 11:14:12 +0800 | [diff] [blame] | 116 | /* Workaround for T4240-R1.0-R2.0 eSDHC which has incorrect |
| 117 | * vendor version and spec version information. |
| 118 | */ |
| 119 | if ((spec_reg == SDHCI_HOST_VERSION) && |
| 120 | (esdhc->quirk_incorrect_hostver)) |
| 121 | ret = (VENDOR_V_23 << SDHCI_VENDOR_VER_SHIFT) | SDHCI_SPEC_200; |
Xu lei | e51cbc9 | 2011-09-09 20:05:46 +0800 | [diff] [blame] | 122 | return ret; |
| 123 | } |
| 124 | |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 125 | static u8 esdhc_readb_fixup(struct sdhci_host *host, |
| 126 | int spec_reg, u32 value) |
Xu lei | e51cbc9 | 2011-09-09 20:05:46 +0800 | [diff] [blame] | 127 | { |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 128 | u8 ret; |
| 129 | u8 dma_bits; |
| 130 | int shift = (spec_reg & 0x3) * 8; |
| 131 | |
| 132 | ret = (value >> shift) & 0xff; |
Roy Zang | ba8c4dc | 2012-01-13 15:02:01 +0800 | [diff] [blame] | 133 | |
| 134 | /* |
| 135 | * "DMA select" locates at offset 0x28 in SD specification, but on |
| 136 | * P5020 or P3041, it locates at 0x29. |
| 137 | */ |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 138 | if (spec_reg == SDHCI_HOST_CONTROL) { |
Roy Zang | ba8c4dc | 2012-01-13 15:02:01 +0800 | [diff] [blame] | 139 | /* DMA select is 22,23 bits in Protocol Control Register */ |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 140 | dma_bits = (value >> 5) & SDHCI_CTRL_DMA_MASK; |
Roy Zang | ba8c4dc | 2012-01-13 15:02:01 +0800 | [diff] [blame] | 141 | /* fixup the result */ |
| 142 | ret &= ~SDHCI_CTRL_DMA_MASK; |
| 143 | ret |= dma_bits; |
| 144 | } |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 145 | return ret; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * esdhc_write*_fixup - Fixup the SD spec register value so that it could be |
| 150 | * written into eSDHC register. |
| 151 | * |
| 152 | * @host: pointer to sdhci_host |
| 153 | * @spec_reg: SD spec register address |
| 154 | * @value: 8/16/32bit SD spec register value that would be written |
| 155 | * @old_value: 32bit eSDHC register value on spec_reg address |
| 156 | * |
| 157 | * In SD spec, there are 8/16/32/64 bits registers, while all of eSDHC |
| 158 | * registers are 32 bits. There are differences in register size, register |
| 159 | * address, register function, bit position and function between eSDHC spec |
| 160 | * and SD spec. |
| 161 | * |
| 162 | * Return a fixed up register value |
| 163 | */ |
| 164 | static u32 esdhc_writel_fixup(struct sdhci_host *host, |
| 165 | int spec_reg, u32 value, u32 old_value) |
| 166 | { |
| 167 | u32 ret; |
| 168 | |
| 169 | /* |
| 170 | * Enabling IRQSTATEN[BGESEN] is just to set IRQSTAT[BGE] |
| 171 | * when SYSCTL[RSTD] is set for some special operations. |
| 172 | * No any impact on other operation. |
| 173 | */ |
| 174 | if (spec_reg == SDHCI_INT_ENABLE) |
| 175 | ret = value | SDHCI_INT_BLK_GAP; |
| 176 | else |
| 177 | ret = value; |
Roy Zang | ba8c4dc | 2012-01-13 15:02:01 +0800 | [diff] [blame] | 178 | |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 179 | return ret; |
| 180 | } |
| 181 | |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 182 | static u32 esdhc_writew_fixup(struct sdhci_host *host, |
| 183 | int spec_reg, u16 value, u32 old_value) |
Haijun Zhang | a4071fb | 2012-12-04 10:41:28 +0800 | [diff] [blame] | 184 | { |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 185 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 186 | int shift = (spec_reg & 0x2) * 8; |
| 187 | u32 ret; |
Haijun Zhang | a4071fb | 2012-12-04 10:41:28 +0800 | [diff] [blame] | 188 | |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 189 | switch (spec_reg) { |
| 190 | case SDHCI_TRANSFER_MODE: |
| 191 | /* |
| 192 | * Postpone this write, we must do it together with a |
| 193 | * command write that is down below. Return old value. |
| 194 | */ |
| 195 | pltfm_host->xfer_mode_shadow = value; |
| 196 | return old_value; |
| 197 | case SDHCI_COMMAND: |
| 198 | ret = (value << 16) | pltfm_host->xfer_mode_shadow; |
| 199 | return ret; |
| 200 | } |
| 201 | |
| 202 | ret = old_value & (~(0xffff << shift)); |
| 203 | ret |= (value << shift); |
| 204 | |
| 205 | if (spec_reg == SDHCI_BLOCK_SIZE) { |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 206 | /* |
| 207 | * Two last DMA bits are reserved, and first one is used for |
| 208 | * non-standard blksz of 4096 bytes that we don't support |
| 209 | * yet. So clear the DMA boundary bits. |
| 210 | */ |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 211 | ret &= (~SDHCI_MAKE_BLKSZ(0x7, 0)); |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 212 | } |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 213 | return ret; |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 214 | } |
| 215 | |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 216 | static u32 esdhc_writeb_fixup(struct sdhci_host *host, |
| 217 | int spec_reg, u8 value, u32 old_value) |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 218 | { |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 219 | u32 ret; |
| 220 | u32 dma_bits; |
| 221 | u8 tmp; |
| 222 | int shift = (spec_reg & 0x3) * 8; |
| 223 | |
Roy Zang | ba8c4dc | 2012-01-13 15:02:01 +0800 | [diff] [blame] | 224 | /* |
yangbo lu | 9e4703d | 2015-10-16 15:44:03 +0800 | [diff] [blame] | 225 | * eSDHC doesn't have a standard power control register, so we do |
| 226 | * nothing here to avoid incorrect operation. |
| 227 | */ |
| 228 | if (spec_reg == SDHCI_POWER_CONTROL) |
| 229 | return old_value; |
| 230 | /* |
Roy Zang | ba8c4dc | 2012-01-13 15:02:01 +0800 | [diff] [blame] | 231 | * "DMA select" location is offset 0x28 in SD specification, but on |
| 232 | * P5020 or P3041, it's located at 0x29. |
| 233 | */ |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 234 | if (spec_reg == SDHCI_HOST_CONTROL) { |
Oded Gabbay | dcaff04 | 2013-07-05 12:48:35 -0400 | [diff] [blame] | 235 | /* |
| 236 | * If host control register is not standard, exit |
| 237 | * this function |
| 238 | */ |
| 239 | if (host->quirks2 & SDHCI_QUIRK2_BROKEN_HOST_CONTROL) |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 240 | return old_value; |
Oded Gabbay | dcaff04 | 2013-07-05 12:48:35 -0400 | [diff] [blame] | 241 | |
Roy Zang | ba8c4dc | 2012-01-13 15:02:01 +0800 | [diff] [blame] | 242 | /* DMA select is 22,23 bits in Protocol Control Register */ |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 243 | dma_bits = (value & SDHCI_CTRL_DMA_MASK) << 5; |
| 244 | ret = (old_value & (~(SDHCI_CTRL_DMA_MASK << 5))) | dma_bits; |
| 245 | tmp = (value & (~SDHCI_CTRL_DMA_MASK)) | |
| 246 | (old_value & SDHCI_CTRL_DMA_MASK); |
| 247 | ret = (ret & (~0xff)) | tmp; |
| 248 | |
| 249 | /* Prevent SDHCI core from writing reserved bits (e.g. HISPD) */ |
| 250 | ret &= ~ESDHC_HOST_CONTROL_RES; |
| 251 | return ret; |
Roy Zang | ba8c4dc | 2012-01-13 15:02:01 +0800 | [diff] [blame] | 252 | } |
| 253 | |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 254 | ret = (old_value & (~(0xff << shift))) | (value << shift); |
| 255 | return ret; |
| 256 | } |
| 257 | |
| 258 | static u32 esdhc_be_readl(struct sdhci_host *host, int reg) |
| 259 | { |
| 260 | u32 ret; |
| 261 | u32 value; |
| 262 | |
yangbo lu | 2f3110c | 2017-08-15 10:17:03 +0800 | [diff] [blame] | 263 | if (reg == SDHCI_CAPABILITIES_1) |
| 264 | value = ioread32be(host->ioaddr + ESDHC_CAPABILITIES_1); |
| 265 | else |
| 266 | value = ioread32be(host->ioaddr + reg); |
| 267 | |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 268 | ret = esdhc_readl_fixup(host, reg, value); |
| 269 | |
| 270 | return ret; |
| 271 | } |
| 272 | |
| 273 | static u32 esdhc_le_readl(struct sdhci_host *host, int reg) |
| 274 | { |
| 275 | u32 ret; |
| 276 | u32 value; |
| 277 | |
yangbo lu | 2f3110c | 2017-08-15 10:17:03 +0800 | [diff] [blame] | 278 | if (reg == SDHCI_CAPABILITIES_1) |
| 279 | value = ioread32(host->ioaddr + ESDHC_CAPABILITIES_1); |
| 280 | else |
| 281 | value = ioread32(host->ioaddr + reg); |
| 282 | |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 283 | ret = esdhc_readl_fixup(host, reg, value); |
| 284 | |
| 285 | return ret; |
| 286 | } |
| 287 | |
| 288 | static u16 esdhc_be_readw(struct sdhci_host *host, int reg) |
| 289 | { |
| 290 | u16 ret; |
| 291 | u32 value; |
| 292 | int base = reg & ~0x3; |
| 293 | |
| 294 | value = ioread32be(host->ioaddr + base); |
| 295 | ret = esdhc_readw_fixup(host, reg, value); |
| 296 | return ret; |
| 297 | } |
| 298 | |
| 299 | static u16 esdhc_le_readw(struct sdhci_host *host, int reg) |
| 300 | { |
| 301 | u16 ret; |
| 302 | u32 value; |
| 303 | int base = reg & ~0x3; |
| 304 | |
| 305 | value = ioread32(host->ioaddr + base); |
| 306 | ret = esdhc_readw_fixup(host, reg, value); |
| 307 | return ret; |
| 308 | } |
| 309 | |
| 310 | static u8 esdhc_be_readb(struct sdhci_host *host, int reg) |
| 311 | { |
| 312 | u8 ret; |
| 313 | u32 value; |
| 314 | int base = reg & ~0x3; |
| 315 | |
| 316 | value = ioread32be(host->ioaddr + base); |
| 317 | ret = esdhc_readb_fixup(host, reg, value); |
| 318 | return ret; |
| 319 | } |
| 320 | |
| 321 | static u8 esdhc_le_readb(struct sdhci_host *host, int reg) |
| 322 | { |
| 323 | u8 ret; |
| 324 | u32 value; |
| 325 | int base = reg & ~0x3; |
| 326 | |
| 327 | value = ioread32(host->ioaddr + base); |
| 328 | ret = esdhc_readb_fixup(host, reg, value); |
| 329 | return ret; |
| 330 | } |
| 331 | |
| 332 | static void esdhc_be_writel(struct sdhci_host *host, u32 val, int reg) |
| 333 | { |
| 334 | u32 value; |
| 335 | |
| 336 | value = esdhc_writel_fixup(host, reg, val, 0); |
| 337 | iowrite32be(value, host->ioaddr + reg); |
| 338 | } |
| 339 | |
| 340 | static void esdhc_le_writel(struct sdhci_host *host, u32 val, int reg) |
| 341 | { |
| 342 | u32 value; |
| 343 | |
| 344 | value = esdhc_writel_fixup(host, reg, val, 0); |
| 345 | iowrite32(value, host->ioaddr + reg); |
| 346 | } |
| 347 | |
| 348 | static void esdhc_be_writew(struct sdhci_host *host, u16 val, int reg) |
| 349 | { |
| 350 | int base = reg & ~0x3; |
| 351 | u32 value; |
| 352 | u32 ret; |
| 353 | |
| 354 | value = ioread32be(host->ioaddr + base); |
| 355 | ret = esdhc_writew_fixup(host, reg, val, value); |
| 356 | if (reg != SDHCI_TRANSFER_MODE) |
| 357 | iowrite32be(ret, host->ioaddr + base); |
| 358 | } |
| 359 | |
| 360 | static void esdhc_le_writew(struct sdhci_host *host, u16 val, int reg) |
| 361 | { |
| 362 | int base = reg & ~0x3; |
| 363 | u32 value; |
| 364 | u32 ret; |
| 365 | |
| 366 | value = ioread32(host->ioaddr + base); |
| 367 | ret = esdhc_writew_fixup(host, reg, val, value); |
| 368 | if (reg != SDHCI_TRANSFER_MODE) |
| 369 | iowrite32(ret, host->ioaddr + base); |
| 370 | } |
| 371 | |
| 372 | static void esdhc_be_writeb(struct sdhci_host *host, u8 val, int reg) |
| 373 | { |
| 374 | int base = reg & ~0x3; |
| 375 | u32 value; |
| 376 | u32 ret; |
| 377 | |
| 378 | value = ioread32be(host->ioaddr + base); |
| 379 | ret = esdhc_writeb_fixup(host, reg, val, value); |
| 380 | iowrite32be(ret, host->ioaddr + base); |
| 381 | } |
| 382 | |
| 383 | static void esdhc_le_writeb(struct sdhci_host *host, u8 val, int reg) |
| 384 | { |
| 385 | int base = reg & ~0x3; |
| 386 | u32 value; |
| 387 | u32 ret; |
| 388 | |
| 389 | value = ioread32(host->ioaddr + base); |
| 390 | ret = esdhc_writeb_fixup(host, reg, val, value); |
| 391 | iowrite32(ret, host->ioaddr + base); |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 392 | } |
| 393 | |
Haijun Zhang | a4071fb | 2012-12-04 10:41:28 +0800 | [diff] [blame] | 394 | /* |
| 395 | * For Abort or Suspend after Stop at Block Gap, ignore the ADMA |
| 396 | * error(IRQSTAT[ADMAE]) if both Transfer Complete(IRQSTAT[TC]) |
| 397 | * and Block Gap Event(IRQSTAT[BGE]) are also set. |
| 398 | * For Continue, apply soft reset for data(SYSCTL[RSTD]); |
| 399 | * and re-issue the entire read transaction from beginning. |
| 400 | */ |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 401 | static void esdhc_of_adma_workaround(struct sdhci_host *host, u32 intmask) |
Haijun Zhang | a4071fb | 2012-12-04 10:41:28 +0800 | [diff] [blame] | 402 | { |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 403 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
Jisheng Zhang | 8605e7a | 2016-02-16 21:08:26 +0800 | [diff] [blame] | 404 | struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); |
Haijun Zhang | a4071fb | 2012-12-04 10:41:28 +0800 | [diff] [blame] | 405 | bool applicable; |
| 406 | dma_addr_t dmastart; |
| 407 | dma_addr_t dmanow; |
| 408 | |
Haijun Zhang | a4071fb | 2012-12-04 10:41:28 +0800 | [diff] [blame] | 409 | applicable = (intmask & SDHCI_INT_DATA_END) && |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 410 | (intmask & SDHCI_INT_BLK_GAP) && |
| 411 | (esdhc->vendor_ver == VENDOR_V_23); |
Haijun Zhang | a4071fb | 2012-12-04 10:41:28 +0800 | [diff] [blame] | 412 | if (!applicable) |
| 413 | return; |
| 414 | |
| 415 | host->data->error = 0; |
| 416 | dmastart = sg_dma_address(host->data->sg); |
| 417 | dmanow = dmastart + host->data->bytes_xfered; |
| 418 | /* |
| 419 | * Force update to the next DMA block boundary. |
| 420 | */ |
| 421 | dmanow = (dmanow & ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) + |
| 422 | SDHCI_DEFAULT_BOUNDARY_SIZE; |
| 423 | host->data->bytes_xfered = dmanow - dmastart; |
| 424 | sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS); |
| 425 | } |
| 426 | |
Wolfram Sang | 80872e2 | 2010-10-15 12:21:03 +0200 | [diff] [blame] | 427 | static int esdhc_of_enable_dma(struct sdhci_host *host) |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 428 | { |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 429 | u32 value; |
| 430 | |
| 431 | value = sdhci_readl(host, ESDHC_DMA_SYSCTL); |
| 432 | value |= ESDHC_DMA_SNOOP; |
| 433 | sdhci_writel(host, value, ESDHC_DMA_SYSCTL); |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 434 | return 0; |
| 435 | } |
| 436 | |
Wolfram Sang | 80872e2 | 2010-10-15 12:21:03 +0200 | [diff] [blame] | 437 | static unsigned int esdhc_of_get_max_clock(struct sdhci_host *host) |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 438 | { |
Shawn Guo | e307148 | 2011-07-20 17:13:36 -0400 | [diff] [blame] | 439 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
yangbo lu | 19c3a0e | 2017-04-20 16:14:40 +0800 | [diff] [blame] | 440 | struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 441 | |
yangbo lu | 19c3a0e | 2017-04-20 16:14:40 +0800 | [diff] [blame] | 442 | if (esdhc->peripheral_clock) |
| 443 | return esdhc->peripheral_clock; |
| 444 | else |
| 445 | return pltfm_host->clock; |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 446 | } |
| 447 | |
Wolfram Sang | 80872e2 | 2010-10-15 12:21:03 +0200 | [diff] [blame] | 448 | static unsigned int esdhc_of_get_min_clock(struct sdhci_host *host) |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 449 | { |
Shawn Guo | e307148 | 2011-07-20 17:13:36 -0400 | [diff] [blame] | 450 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
yangbo lu | 19c3a0e | 2017-04-20 16:14:40 +0800 | [diff] [blame] | 451 | struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); |
| 452 | unsigned int clock; |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 453 | |
yangbo lu | 19c3a0e | 2017-04-20 16:14:40 +0800 | [diff] [blame] | 454 | if (esdhc->peripheral_clock) |
| 455 | clock = esdhc->peripheral_clock; |
| 456 | else |
| 457 | clock = pltfm_host->clock; |
| 458 | return clock / 256 / 16; |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 459 | } |
| 460 | |
yangbo lu | dd3f698 | 2017-09-21 16:43:31 +0800 | [diff] [blame] | 461 | static void esdhc_clock_enable(struct sdhci_host *host, bool enable) |
| 462 | { |
| 463 | u32 val; |
| 464 | ktime_t timeout; |
| 465 | |
| 466 | val = sdhci_readl(host, ESDHC_SYSTEM_CONTROL); |
| 467 | |
| 468 | if (enable) |
| 469 | val |= ESDHC_CLOCK_SDCLKEN; |
| 470 | else |
| 471 | val &= ~ESDHC_CLOCK_SDCLKEN; |
| 472 | |
| 473 | sdhci_writel(host, val, ESDHC_SYSTEM_CONTROL); |
| 474 | |
| 475 | /* Wait max 20 ms */ |
| 476 | timeout = ktime_add_ms(ktime_get(), 20); |
| 477 | val = ESDHC_CLOCK_STABLE; |
| 478 | while (!(sdhci_readl(host, ESDHC_PRSSTAT) & val)) { |
| 479 | if (ktime_after(ktime_get(), timeout)) { |
| 480 | pr_err("%s: Internal clock never stabilised.\n", |
| 481 | mmc_hostname(host->mmc)); |
| 482 | break; |
| 483 | } |
| 484 | udelay(10); |
| 485 | } |
| 486 | } |
| 487 | |
Jerry Huang | f060bc9 | 2012-02-14 14:05:37 +0800 | [diff] [blame] | 488 | static void esdhc_of_set_clock(struct sdhci_host *host, unsigned int clock) |
| 489 | { |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 490 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
Jisheng Zhang | 8605e7a | 2016-02-16 21:08:26 +0800 | [diff] [blame] | 491 | struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); |
Joakim Tjernlund | bd45502 | 2015-04-20 23:12:13 +0200 | [diff] [blame] | 492 | int pre_div = 1; |
Dong Aisheng | d31fc00 | 2013-09-13 19:11:32 +0800 | [diff] [blame] | 493 | int div = 1; |
yangbo lu | e145ac4 | 2017-04-26 10:45:49 +0800 | [diff] [blame] | 494 | ktime_t timeout; |
Dong Aisheng | d31fc00 | 2013-09-13 19:11:32 +0800 | [diff] [blame] | 495 | u32 temp; |
| 496 | |
Russell King | 1650d0c | 2014-04-25 12:58:50 +0100 | [diff] [blame] | 497 | host->mmc->actual_clock = 0; |
| 498 | |
yangbo lu | dd3f698 | 2017-09-21 16:43:31 +0800 | [diff] [blame] | 499 | if (clock == 0) { |
| 500 | esdhc_clock_enable(host, false); |
Russell King | 373073e | 2014-04-25 12:58:45 +0100 | [diff] [blame] | 501 | return; |
yangbo lu | dd3f698 | 2017-09-21 16:43:31 +0800 | [diff] [blame] | 502 | } |
Dong Aisheng | d31fc00 | 2013-09-13 19:11:32 +0800 | [diff] [blame] | 503 | |
Yangbo Lu | 77bd2f6 | 2015-08-11 10:53:34 +0800 | [diff] [blame] | 504 | /* Workaround to start pre_div at 2 for VNN < VENDOR_V_23 */ |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 505 | if (esdhc->vendor_ver < VENDOR_V_23) |
Yangbo Lu | 77bd2f6 | 2015-08-11 10:53:34 +0800 | [diff] [blame] | 506 | pre_div = 2; |
| 507 | |
yangbo lu | a627f02 | 2017-04-20 14:58:29 +0800 | [diff] [blame] | 508 | /* |
| 509 | * Limit SD clock to 167MHz for ls1046a according to its datasheet |
| 510 | */ |
| 511 | if (clock > 167000000 && |
| 512 | of_find_compatible_node(NULL, NULL, "fsl,ls1046a-esdhc")) |
| 513 | clock = 167000000; |
| 514 | |
| 515 | /* |
| 516 | * Limit SD clock to 125MHz for ls1012a according to its datasheet |
| 517 | */ |
| 518 | if (clock > 125000000 && |
| 519 | of_find_compatible_node(NULL, NULL, "fsl,ls1012a-esdhc")) |
| 520 | clock = 125000000; |
| 521 | |
Jerry Huang | f060bc9 | 2012-02-14 14:05:37 +0800 | [diff] [blame] | 522 | /* Workaround to reduce the clock frequency for p1010 esdhc */ |
| 523 | if (of_find_compatible_node(NULL, NULL, "fsl,p1010-esdhc")) { |
| 524 | if (clock > 20000000) |
| 525 | clock -= 5000000; |
| 526 | if (clock > 40000000) |
| 527 | clock -= 5000000; |
| 528 | } |
| 529 | |
Dong Aisheng | d31fc00 | 2013-09-13 19:11:32 +0800 | [diff] [blame] | 530 | temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL); |
yangbo lu | e87d2db | 2016-12-26 17:46:30 +0800 | [diff] [blame] | 531 | temp &= ~(ESDHC_CLOCK_SDCLKEN | ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | |
| 532 | ESDHC_CLOCK_PEREN | ESDHC_CLOCK_MASK); |
Dong Aisheng | d31fc00 | 2013-09-13 19:11:32 +0800 | [diff] [blame] | 533 | sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL); |
| 534 | |
| 535 | while (host->max_clk / pre_div / 16 > clock && pre_div < 256) |
| 536 | pre_div *= 2; |
| 537 | |
| 538 | while (host->max_clk / pre_div / div > clock && div < 16) |
| 539 | div++; |
| 540 | |
| 541 | dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n", |
Dong Aisheng | e76b855 | 2013-09-13 19:11:37 +0800 | [diff] [blame] | 542 | clock, host->max_clk / pre_div / div); |
Joakim Tjernlund | bd45502 | 2015-04-20 23:12:13 +0200 | [diff] [blame] | 543 | host->mmc->actual_clock = host->max_clk / pre_div / div; |
Dong Aisheng | d31fc00 | 2013-09-13 19:11:32 +0800 | [diff] [blame] | 544 | pre_div >>= 1; |
| 545 | div--; |
| 546 | |
| 547 | temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL); |
| 548 | temp |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN |
| 549 | | (div << ESDHC_DIVIDER_SHIFT) |
| 550 | | (pre_div << ESDHC_PREDIV_SHIFT)); |
| 551 | sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL); |
yangbo lu | e87d2db | 2016-12-26 17:46:30 +0800 | [diff] [blame] | 552 | |
| 553 | /* Wait max 20 ms */ |
yangbo lu | e145ac4 | 2017-04-26 10:45:49 +0800 | [diff] [blame] | 554 | timeout = ktime_add_ms(ktime_get(), 20); |
yangbo lu | e87d2db | 2016-12-26 17:46:30 +0800 | [diff] [blame] | 555 | while (!(sdhci_readl(host, ESDHC_PRSSTAT) & ESDHC_CLOCK_STABLE)) { |
yangbo lu | e145ac4 | 2017-04-26 10:45:49 +0800 | [diff] [blame] | 556 | if (ktime_after(ktime_get(), timeout)) { |
yangbo lu | e87d2db | 2016-12-26 17:46:30 +0800 | [diff] [blame] | 557 | pr_err("%s: Internal clock never stabilised.\n", |
| 558 | mmc_hostname(host->mmc)); |
| 559 | return; |
| 560 | } |
yangbo lu | e145ac4 | 2017-04-26 10:45:49 +0800 | [diff] [blame] | 561 | udelay(10); |
yangbo lu | e87d2db | 2016-12-26 17:46:30 +0800 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | temp |= ESDHC_CLOCK_SDCLKEN; |
| 565 | sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL); |
Jerry Huang | f060bc9 | 2012-02-14 14:05:37 +0800 | [diff] [blame] | 566 | } |
| 567 | |
Russell King | 2317f56 | 2014-04-25 12:57:07 +0100 | [diff] [blame] | 568 | 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] | 569 | { |
| 570 | u32 ctrl; |
| 571 | |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 572 | ctrl = sdhci_readl(host, ESDHC_PROCTL); |
| 573 | ctrl &= (~ESDHC_CTRL_BUSWIDTH_MASK); |
Oded Gabbay | 66b50a0 | 2013-06-27 12:00:05 -0400 | [diff] [blame] | 574 | switch (width) { |
| 575 | case MMC_BUS_WIDTH_8: |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 576 | ctrl |= ESDHC_CTRL_8BITBUS; |
Oded Gabbay | 66b50a0 | 2013-06-27 12:00:05 -0400 | [diff] [blame] | 577 | break; |
| 578 | |
| 579 | case MMC_BUS_WIDTH_4: |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 580 | ctrl |= ESDHC_CTRL_4BITBUS; |
Oded Gabbay | 66b50a0 | 2013-06-27 12:00:05 -0400 | [diff] [blame] | 581 | break; |
| 582 | |
| 583 | default: |
Oded Gabbay | 66b50a0 | 2013-06-27 12:00:05 -0400 | [diff] [blame] | 584 | break; |
| 585 | } |
| 586 | |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 587 | sdhci_writel(host, ctrl, ESDHC_PROCTL); |
Oded Gabbay | 66b50a0 | 2013-06-27 12:00:05 -0400 | [diff] [blame] | 588 | } |
| 589 | |
Alessio Igor Bogani | 304f0a9 | 2014-12-09 09:40:38 +0100 | [diff] [blame] | 590 | static void esdhc_reset(struct sdhci_host *host, u8 mask) |
| 591 | { |
yinbo.zhu | f2bc600 | 2017-12-01 15:09:34 +0800 | [diff] [blame] | 592 | u32 val; |
| 593 | |
Alessio Igor Bogani | 304f0a9 | 2014-12-09 09:40:38 +0100 | [diff] [blame] | 594 | sdhci_reset(host, mask); |
| 595 | |
| 596 | sdhci_writel(host, host->ier, SDHCI_INT_ENABLE); |
| 597 | sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE); |
yinbo.zhu | f2bc600 | 2017-12-01 15:09:34 +0800 | [diff] [blame] | 598 | |
| 599 | if (mask & SDHCI_RESET_ALL) { |
| 600 | val = sdhci_readl(host, ESDHC_TBCTL); |
| 601 | val &= ~ESDHC_TB_EN; |
| 602 | sdhci_writel(host, val, ESDHC_TBCTL); |
| 603 | } |
Alessio Igor Bogani | 304f0a9 | 2014-12-09 09:40:38 +0100 | [diff] [blame] | 604 | } |
| 605 | |
yangbo lu | ea35645 | 2017-04-20 16:14:41 +0800 | [diff] [blame] | 606 | /* The SCFG, Supplemental Configuration Unit, provides SoC specific |
| 607 | * configuration and status registers for the device. There is a |
| 608 | * SDHC IO VSEL control register on SCFG for some platforms. It's |
| 609 | * used to support SDHC IO voltage switching. |
| 610 | */ |
| 611 | static const struct of_device_id scfg_device_ids[] = { |
| 612 | { .compatible = "fsl,t1040-scfg", }, |
| 613 | { .compatible = "fsl,ls1012a-scfg", }, |
| 614 | { .compatible = "fsl,ls1046a-scfg", }, |
| 615 | {} |
| 616 | }; |
| 617 | |
| 618 | /* SDHC IO VSEL control register definition */ |
| 619 | #define SCFG_SDHCIOVSELCR 0x408 |
| 620 | #define SDHCIOVSELCR_TGLEN 0x80000000 |
| 621 | #define SDHCIOVSELCR_VSELVAL 0x60000000 |
| 622 | #define SDHCIOVSELCR_SDHC_VS 0x00000001 |
| 623 | |
| 624 | static int esdhc_signal_voltage_switch(struct mmc_host *mmc, |
| 625 | struct mmc_ios *ios) |
| 626 | { |
| 627 | struct sdhci_host *host = mmc_priv(mmc); |
| 628 | struct device_node *scfg_node; |
| 629 | void __iomem *scfg_base = NULL; |
| 630 | u32 sdhciovselcr; |
| 631 | u32 val; |
| 632 | |
| 633 | /* |
| 634 | * Signal Voltage Switching is only applicable for Host Controllers |
| 635 | * v3.00 and above. |
| 636 | */ |
| 637 | if (host->version < SDHCI_SPEC_300) |
| 638 | return 0; |
| 639 | |
| 640 | val = sdhci_readl(host, ESDHC_PROCTL); |
| 641 | |
| 642 | switch (ios->signal_voltage) { |
| 643 | case MMC_SIGNAL_VOLTAGE_330: |
| 644 | val &= ~ESDHC_VOLT_SEL; |
| 645 | sdhci_writel(host, val, ESDHC_PROCTL); |
| 646 | return 0; |
| 647 | case MMC_SIGNAL_VOLTAGE_180: |
| 648 | scfg_node = of_find_matching_node(NULL, scfg_device_ids); |
| 649 | if (scfg_node) |
| 650 | scfg_base = of_iomap(scfg_node, 0); |
| 651 | if (scfg_base) { |
| 652 | sdhciovselcr = SDHCIOVSELCR_TGLEN | |
| 653 | SDHCIOVSELCR_VSELVAL; |
| 654 | iowrite32be(sdhciovselcr, |
| 655 | scfg_base + SCFG_SDHCIOVSELCR); |
| 656 | |
| 657 | val |= ESDHC_VOLT_SEL; |
| 658 | sdhci_writel(host, val, ESDHC_PROCTL); |
| 659 | mdelay(5); |
| 660 | |
| 661 | sdhciovselcr = SDHCIOVSELCR_TGLEN | |
| 662 | SDHCIOVSELCR_SDHC_VS; |
| 663 | iowrite32be(sdhciovselcr, |
| 664 | scfg_base + SCFG_SDHCIOVSELCR); |
| 665 | iounmap(scfg_base); |
| 666 | } else { |
| 667 | val |= ESDHC_VOLT_SEL; |
| 668 | sdhci_writel(host, val, ESDHC_PROCTL); |
| 669 | } |
| 670 | return 0; |
| 671 | default: |
| 672 | return 0; |
| 673 | } |
| 674 | } |
| 675 | |
yangbo lu | ba49cbd | 2017-04-20 16:14:42 +0800 | [diff] [blame] | 676 | static int esdhc_execute_tuning(struct mmc_host *mmc, u32 opcode) |
| 677 | { |
| 678 | struct sdhci_host *host = mmc_priv(mmc); |
| 679 | u32 val; |
| 680 | |
| 681 | /* Use tuning block for tuning procedure */ |
| 682 | esdhc_clock_enable(host, false); |
| 683 | val = sdhci_readl(host, ESDHC_DMA_SYSCTL); |
| 684 | val |= ESDHC_FLUSH_ASYNC_FIFO; |
| 685 | sdhci_writel(host, val, ESDHC_DMA_SYSCTL); |
| 686 | |
| 687 | val = sdhci_readl(host, ESDHC_TBCTL); |
| 688 | val |= ESDHC_TB_EN; |
| 689 | sdhci_writel(host, val, ESDHC_TBCTL); |
| 690 | esdhc_clock_enable(host, true); |
| 691 | |
| 692 | return sdhci_execute_tuning(mmc, opcode); |
| 693 | } |
| 694 | |
Ulf Hansson | 9e48b33 | 2016-07-27 11:01:48 +0200 | [diff] [blame] | 695 | #ifdef CONFIG_PM_SLEEP |
Russell King | 723f792 | 2014-04-25 12:59:46 +0100 | [diff] [blame] | 696 | static u32 esdhc_proctl; |
| 697 | static int esdhc_of_suspend(struct device *dev) |
| 698 | { |
| 699 | struct sdhci_host *host = dev_get_drvdata(dev); |
| 700 | |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 701 | esdhc_proctl = sdhci_readl(host, SDHCI_HOST_CONTROL); |
Russell King | 723f792 | 2014-04-25 12:59:46 +0100 | [diff] [blame] | 702 | |
Adrian Hunter | d38dcad | 2017-03-20 19:50:32 +0200 | [diff] [blame] | 703 | if (host->tuning_mode != SDHCI_TUNING_MODE_3) |
| 704 | mmc_retune_needed(host->mmc); |
| 705 | |
Russell King | 723f792 | 2014-04-25 12:59:46 +0100 | [diff] [blame] | 706 | return sdhci_suspend_host(host); |
| 707 | } |
| 708 | |
Ulf Hansson | 06732b8 | 2014-05-23 10:36:44 +0200 | [diff] [blame] | 709 | static int esdhc_of_resume(struct device *dev) |
Russell King | 723f792 | 2014-04-25 12:59:46 +0100 | [diff] [blame] | 710 | { |
| 711 | struct sdhci_host *host = dev_get_drvdata(dev); |
| 712 | int ret = sdhci_resume_host(host); |
| 713 | |
| 714 | if (ret == 0) { |
| 715 | /* Isn't this already done by sdhci_resume_host() ? --rmk */ |
| 716 | esdhc_of_enable_dma(host); |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 717 | sdhci_writel(host, esdhc_proctl, SDHCI_HOST_CONTROL); |
Russell King | 723f792 | 2014-04-25 12:59:46 +0100 | [diff] [blame] | 718 | } |
Russell King | 723f792 | 2014-04-25 12:59:46 +0100 | [diff] [blame] | 719 | return ret; |
| 720 | } |
Russell King | 723f792 | 2014-04-25 12:59:46 +0100 | [diff] [blame] | 721 | #endif |
| 722 | |
Ulf Hansson | 9e48b33 | 2016-07-27 11:01:48 +0200 | [diff] [blame] | 723 | static SIMPLE_DEV_PM_OPS(esdhc_of_dev_pm_ops, |
| 724 | esdhc_of_suspend, |
| 725 | esdhc_of_resume); |
| 726 | |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 727 | static const struct sdhci_ops sdhci_esdhc_be_ops = { |
| 728 | .read_l = esdhc_be_readl, |
| 729 | .read_w = esdhc_be_readw, |
| 730 | .read_b = esdhc_be_readb, |
| 731 | .write_l = esdhc_be_writel, |
| 732 | .write_w = esdhc_be_writew, |
| 733 | .write_b = esdhc_be_writeb, |
| 734 | .set_clock = esdhc_of_set_clock, |
| 735 | .enable_dma = esdhc_of_enable_dma, |
| 736 | .get_max_clock = esdhc_of_get_max_clock, |
| 737 | .get_min_clock = esdhc_of_get_min_clock, |
| 738 | .adma_workaround = esdhc_of_adma_workaround, |
| 739 | .set_bus_width = esdhc_pltfm_set_bus_width, |
| 740 | .reset = esdhc_reset, |
| 741 | .set_uhs_signaling = sdhci_set_uhs_signaling, |
| 742 | }; |
| 743 | |
| 744 | static const struct sdhci_ops sdhci_esdhc_le_ops = { |
| 745 | .read_l = esdhc_le_readl, |
| 746 | .read_w = esdhc_le_readw, |
| 747 | .read_b = esdhc_le_readb, |
| 748 | .write_l = esdhc_le_writel, |
| 749 | .write_w = esdhc_le_writew, |
| 750 | .write_b = esdhc_le_writeb, |
| 751 | .set_clock = esdhc_of_set_clock, |
| 752 | .enable_dma = esdhc_of_enable_dma, |
| 753 | .get_max_clock = esdhc_of_get_max_clock, |
| 754 | .get_min_clock = esdhc_of_get_min_clock, |
| 755 | .adma_workaround = esdhc_of_adma_workaround, |
| 756 | .set_bus_width = esdhc_pltfm_set_bus_width, |
| 757 | .reset = esdhc_reset, |
| 758 | .set_uhs_signaling = sdhci_set_uhs_signaling, |
| 759 | }; |
| 760 | |
| 761 | static const struct sdhci_pltfm_data sdhci_esdhc_be_pdata = { |
yangbo lu | e9acc77 | 2016-12-26 17:40:44 +0800 | [diff] [blame] | 762 | .quirks = ESDHC_DEFAULT_QUIRKS | |
| 763 | #ifdef CONFIG_PPC |
| 764 | SDHCI_QUIRK_BROKEN_CARD_DETECTION | |
| 765 | #endif |
| 766 | SDHCI_QUIRK_NO_CARD_NO_RESET | |
| 767 | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC, |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 768 | .ops = &sdhci_esdhc_be_ops, |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 769 | }; |
Shawn Guo | 38576af | 2011-05-27 23:48:14 +0800 | [diff] [blame] | 770 | |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 771 | static const struct sdhci_pltfm_data sdhci_esdhc_le_pdata = { |
yangbo lu | e9acc77 | 2016-12-26 17:40:44 +0800 | [diff] [blame] | 772 | .quirks = ESDHC_DEFAULT_QUIRKS | |
| 773 | SDHCI_QUIRK_NO_CARD_NO_RESET | |
| 774 | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC, |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 775 | .ops = &sdhci_esdhc_le_ops, |
| 776 | }; |
| 777 | |
yangbo lu | 151ede4 | 2016-11-09 11:14:12 +0800 | [diff] [blame] | 778 | static struct soc_device_attribute soc_incorrect_hostver[] = { |
| 779 | { .family = "QorIQ T4240", .revision = "1.0", }, |
| 780 | { .family = "QorIQ T4240", .revision = "2.0", }, |
| 781 | { }, |
| 782 | }; |
| 783 | |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 784 | static void esdhc_init(struct platform_device *pdev, struct sdhci_host *host) |
| 785 | { |
| 786 | struct sdhci_pltfm_host *pltfm_host; |
| 787 | struct sdhci_esdhc *esdhc; |
yangbo lu | 19c3a0e | 2017-04-20 16:14:40 +0800 | [diff] [blame] | 788 | struct device_node *np; |
| 789 | struct clk *clk; |
| 790 | u32 val; |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 791 | u16 host_ver; |
| 792 | |
| 793 | pltfm_host = sdhci_priv(host); |
Jisheng Zhang | 8605e7a | 2016-02-16 21:08:26 +0800 | [diff] [blame] | 794 | esdhc = sdhci_pltfm_priv(pltfm_host); |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 795 | |
| 796 | host_ver = sdhci_readw(host, SDHCI_HOST_VERSION); |
| 797 | esdhc->vendor_ver = (host_ver & SDHCI_VENDOR_VER_MASK) >> |
| 798 | SDHCI_VENDOR_VER_SHIFT; |
| 799 | esdhc->spec_ver = host_ver & SDHCI_SPEC_VER_MASK; |
yangbo lu | 151ede4 | 2016-11-09 11:14:12 +0800 | [diff] [blame] | 800 | if (soc_device_match(soc_incorrect_hostver)) |
| 801 | esdhc->quirk_incorrect_hostver = true; |
| 802 | else |
| 803 | esdhc->quirk_incorrect_hostver = false; |
yangbo lu | 19c3a0e | 2017-04-20 16:14:40 +0800 | [diff] [blame] | 804 | |
| 805 | np = pdev->dev.of_node; |
| 806 | clk = of_clk_get(np, 0); |
| 807 | if (!IS_ERR(clk)) { |
| 808 | /* |
| 809 | * esdhc->peripheral_clock would be assigned with a value |
| 810 | * which is eSDHC base clock when use periperal clock. |
| 811 | * For ls1046a, the clock value got by common clk API is |
| 812 | * peripheral clock while the eSDHC base clock is 1/2 |
| 813 | * peripheral clock. |
| 814 | */ |
| 815 | if (of_device_is_compatible(np, "fsl,ls1046a-esdhc")) |
| 816 | esdhc->peripheral_clock = clk_get_rate(clk) / 2; |
| 817 | else |
| 818 | esdhc->peripheral_clock = clk_get_rate(clk); |
| 819 | |
| 820 | clk_put(clk); |
| 821 | } |
| 822 | |
| 823 | if (esdhc->peripheral_clock) { |
| 824 | esdhc_clock_enable(host, false); |
| 825 | val = sdhci_readl(host, ESDHC_DMA_SYSCTL); |
| 826 | val |= ESDHC_PERIPHERAL_CLK_SEL; |
| 827 | sdhci_writel(host, val, ESDHC_DMA_SYSCTL); |
| 828 | esdhc_clock_enable(host, true); |
| 829 | } |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 830 | } |
| 831 | |
Bill Pemberton | c3be1ef | 2012-11-19 13:23:06 -0500 | [diff] [blame] | 832 | static int sdhci_esdhc_probe(struct platform_device *pdev) |
Shawn Guo | 38576af | 2011-05-27 23:48:14 +0800 | [diff] [blame] | 833 | { |
Oded Gabbay | 66b50a0 | 2013-06-27 12:00:05 -0400 | [diff] [blame] | 834 | struct sdhci_host *host; |
Oded Gabbay | dcaff04 | 2013-07-05 12:48:35 -0400 | [diff] [blame] | 835 | struct device_node *np; |
yangbo lu | 1ef5e49 | 2015-11-25 10:05:37 +0800 | [diff] [blame] | 836 | struct sdhci_pltfm_host *pltfm_host; |
| 837 | struct sdhci_esdhc *esdhc; |
Oded Gabbay | 66b50a0 | 2013-06-27 12:00:05 -0400 | [diff] [blame] | 838 | int ret; |
| 839 | |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 840 | np = pdev->dev.of_node; |
| 841 | |
Julia Lawall | 150d424 | 2016-08-05 10:56:46 +0200 | [diff] [blame] | 842 | if (of_property_read_bool(np, "little-endian")) |
Jisheng Zhang | 8605e7a | 2016-02-16 21:08:26 +0800 | [diff] [blame] | 843 | host = sdhci_pltfm_init(pdev, &sdhci_esdhc_le_pdata, |
| 844 | sizeof(struct sdhci_esdhc)); |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 845 | else |
Jisheng Zhang | 8605e7a | 2016-02-16 21:08:26 +0800 | [diff] [blame] | 846 | host = sdhci_pltfm_init(pdev, &sdhci_esdhc_be_pdata, |
| 847 | sizeof(struct sdhci_esdhc)); |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 848 | |
Oded Gabbay | 66b50a0 | 2013-06-27 12:00:05 -0400 | [diff] [blame] | 849 | if (IS_ERR(host)) |
| 850 | return PTR_ERR(host); |
| 851 | |
yangbo lu | ea35645 | 2017-04-20 16:14:41 +0800 | [diff] [blame] | 852 | host->mmc_host_ops.start_signal_voltage_switch = |
| 853 | esdhc_signal_voltage_switch; |
yangbo lu | ba49cbd | 2017-04-20 16:14:42 +0800 | [diff] [blame] | 854 | host->mmc_host_ops.execute_tuning = esdhc_execute_tuning; |
yangbo lu | 6b236f3 | 2017-04-20 16:14:44 +0800 | [diff] [blame] | 855 | host->tuning_delay = 1; |
yangbo lu | ea35645 | 2017-04-20 16:14:41 +0800 | [diff] [blame] | 856 | |
yangbo lu | f4932cf | 2015-10-08 18:36:36 +0800 | [diff] [blame] | 857 | esdhc_init(pdev, host); |
| 858 | |
Oded Gabbay | 66b50a0 | 2013-06-27 12:00:05 -0400 | [diff] [blame] | 859 | sdhci_get_of_property(pdev); |
| 860 | |
yangbo lu | 1ef5e49 | 2015-11-25 10:05:37 +0800 | [diff] [blame] | 861 | pltfm_host = sdhci_priv(host); |
Jisheng Zhang | 8605e7a | 2016-02-16 21:08:26 +0800 | [diff] [blame] | 862 | esdhc = sdhci_pltfm_priv(pltfm_host); |
yangbo lu | 1ef5e49 | 2015-11-25 10:05:37 +0800 | [diff] [blame] | 863 | if (esdhc->vendor_ver == VENDOR_V_22) |
| 864 | host->quirks2 |= SDHCI_QUIRK2_HOST_NO_CMD23; |
| 865 | |
| 866 | if (esdhc->vendor_ver > VENDOR_V_22) |
| 867 | host->quirks &= ~SDHCI_QUIRK_NO_BUSY_IRQ; |
| 868 | |
Yangbo Lu | 74fd5e3 | 2015-06-01 13:47:12 +0800 | [diff] [blame] | 869 | if (of_device_is_compatible(np, "fsl,p5040-esdhc") || |
| 870 | of_device_is_compatible(np, "fsl,p5020-esdhc") || |
| 871 | of_device_is_compatible(np, "fsl,p4080-esdhc") || |
| 872 | of_device_is_compatible(np, "fsl,p1020-esdhc") || |
yangbo lu | e9acc77 | 2016-12-26 17:40:44 +0800 | [diff] [blame] | 873 | of_device_is_compatible(np, "fsl,t1040-esdhc")) |
Yangbo Lu | 74fd5e3 | 2015-06-01 13:47:12 +0800 | [diff] [blame] | 874 | host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION; |
| 875 | |
yangbo lu | a22950c | 2015-10-08 18:36:57 +0800 | [diff] [blame] | 876 | if (of_device_is_compatible(np, "fsl,ls1021a-esdhc")) |
| 877 | host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL; |
| 878 | |
Oded Gabbay | dcaff04 | 2013-07-05 12:48:35 -0400 | [diff] [blame] | 879 | if (of_device_is_compatible(np, "fsl,p2020-esdhc")) { |
| 880 | /* |
| 881 | * Freescale messed up with P2020 as it has a non-standard |
| 882 | * host control register |
| 883 | */ |
| 884 | host->quirks2 |= SDHCI_QUIRK2_BROKEN_HOST_CONTROL; |
| 885 | } |
| 886 | |
Oded Gabbay | 66b50a0 | 2013-06-27 12:00:05 -0400 | [diff] [blame] | 887 | /* call to generic mmc_of_parse to support additional capabilities */ |
Ulf Hansson | f099140 | 2014-12-18 10:41:41 +0100 | [diff] [blame] | 888 | ret = mmc_of_parse(host->mmc); |
| 889 | if (ret) |
| 890 | goto err; |
| 891 | |
Haijun Zhang | 490104a | 2013-08-26 09:19:24 +0800 | [diff] [blame] | 892 | mmc_of_parse_voltage(np, &host->ocr_mask); |
Oded Gabbay | 66b50a0 | 2013-06-27 12:00:05 -0400 | [diff] [blame] | 893 | |
| 894 | ret = sdhci_add_host(host); |
| 895 | if (ret) |
Ulf Hansson | f099140 | 2014-12-18 10:41:41 +0100 | [diff] [blame] | 896 | goto err; |
Oded Gabbay | 66b50a0 | 2013-06-27 12:00:05 -0400 | [diff] [blame] | 897 | |
Ulf Hansson | f099140 | 2014-12-18 10:41:41 +0100 | [diff] [blame] | 898 | return 0; |
| 899 | err: |
| 900 | sdhci_pltfm_free(pdev); |
Oded Gabbay | 66b50a0 | 2013-06-27 12:00:05 -0400 | [diff] [blame] | 901 | return ret; |
Shawn Guo | 38576af | 2011-05-27 23:48:14 +0800 | [diff] [blame] | 902 | } |
| 903 | |
Shawn Guo | 38576af | 2011-05-27 23:48:14 +0800 | [diff] [blame] | 904 | static const struct of_device_id sdhci_esdhc_of_match[] = { |
| 905 | { .compatible = "fsl,mpc8379-esdhc" }, |
| 906 | { .compatible = "fsl,mpc8536-esdhc" }, |
| 907 | { .compatible = "fsl,esdhc" }, |
| 908 | { } |
| 909 | }; |
| 910 | MODULE_DEVICE_TABLE(of, sdhci_esdhc_of_match); |
| 911 | |
| 912 | static struct platform_driver sdhci_esdhc_driver = { |
| 913 | .driver = { |
| 914 | .name = "sdhci-esdhc", |
Shawn Guo | 38576af | 2011-05-27 23:48:14 +0800 | [diff] [blame] | 915 | .of_match_table = sdhci_esdhc_of_match, |
Ulf Hansson | 9e48b33 | 2016-07-27 11:01:48 +0200 | [diff] [blame] | 916 | .pm = &esdhc_of_dev_pm_ops, |
Shawn Guo | 38576af | 2011-05-27 23:48:14 +0800 | [diff] [blame] | 917 | }, |
| 918 | .probe = sdhci_esdhc_probe, |
Kevin Hao | caebcae | 2015-02-27 15:47:31 +0800 | [diff] [blame] | 919 | .remove = sdhci_pltfm_unregister, |
Shawn Guo | 38576af | 2011-05-27 23:48:14 +0800 | [diff] [blame] | 920 | }; |
| 921 | |
Axel Lin | d1f81a6 | 2011-11-26 12:55:43 +0800 | [diff] [blame] | 922 | module_platform_driver(sdhci_esdhc_driver); |
Shawn Guo | 38576af | 2011-05-27 23:48:14 +0800 | [diff] [blame] | 923 | |
| 924 | MODULE_DESCRIPTION("SDHCI OF driver for Freescale MPC eSDHC"); |
| 925 | MODULE_AUTHOR("Xiaobo Xie <X.Xie@freescale.com>, " |
| 926 | "Anton Vorontsov <avorontsov@ru.mvista.com>"); |
| 927 | MODULE_LICENSE("GPL v2"); |