Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Kever Yang | 65922e0 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2016 Fuzhou Rockchip Electronics Co., Ltd |
| 4 | * |
| 5 | * Rockchip SD Host Controller Interface |
Kever Yang | 65922e0 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Yifeng Zhao | 5c2a5ab | 2021-06-29 16:24:41 +0800 | [diff] [blame] | 9 | #include <clk.h> |
Kever Yang | 65922e0 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 10 | #include <dm.h> |
Yifeng Zhao | 5c2a5ab | 2021-06-29 16:24:41 +0800 | [diff] [blame] | 11 | #include <dm/ofnode.h> |
Kever Yang | dd99a02 | 2017-02-13 17:38:57 +0800 | [diff] [blame] | 12 | #include <dt-structs.h> |
Yifeng Zhao | 5c2a5ab | 2021-06-29 16:24:41 +0800 | [diff] [blame] | 13 | #include <linux/delay.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 14 | #include <linux/err.h> |
Masahiro Yamada | 75f82d0 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 15 | #include <linux/libfdt.h> |
Yifeng Zhao | 5c2a5ab | 2021-06-29 16:24:41 +0800 | [diff] [blame] | 16 | #include <linux/iopoll.h> |
Kever Yang | 65922e0 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 17 | #include <malloc.h> |
Kever Yang | dd99a02 | 2017-02-13 17:38:57 +0800 | [diff] [blame] | 18 | #include <mapmem.h> |
Yifeng Zhao | 5c2a5ab | 2021-06-29 16:24:41 +0800 | [diff] [blame] | 19 | #include "mmc_private.h" |
Kever Yang | 65922e0 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 20 | #include <sdhci.h> |
Yifeng Zhao | 5c2a5ab | 2021-06-29 16:24:41 +0800 | [diff] [blame] | 21 | #include <syscon.h> |
| 22 | #include <asm/arch-rockchip/clock.h> |
| 23 | #include <asm/arch-rockchip/hardware.h> |
Kever Yang | 65922e0 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 24 | |
Alper Nebi Yasak | 6f19869 | 2022-03-15 20:46:28 +0300 | [diff] [blame] | 25 | /* DWCMSHC specific Mode Select value */ |
| 26 | #define DWCMSHC_CTRL_HS400 0x7 |
Kever Yang | 65922e0 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 27 | /* 400KHz is max freq for card ID etc. Use that as min */ |
| 28 | #define EMMC_MIN_FREQ 400000 |
Yifeng Zhao | 5c2a5ab | 2021-06-29 16:24:41 +0800 | [diff] [blame] | 29 | #define KHz (1000) |
| 30 | #define MHz (1000 * KHz) |
| 31 | #define SDHCI_TUNING_LOOP_COUNT 40 |
| 32 | |
| 33 | #define PHYCTRL_CALDONE_MASK 0x1 |
| 34 | #define PHYCTRL_CALDONE_SHIFT 0x6 |
| 35 | #define PHYCTRL_CALDONE_DONE 0x1 |
| 36 | #define PHYCTRL_DLLRDY_MASK 0x1 |
| 37 | #define PHYCTRL_DLLRDY_SHIFT 0x5 |
| 38 | #define PHYCTRL_DLLRDY_DONE 0x1 |
| 39 | #define PHYCTRL_FREQSEL_200M 0x0 |
| 40 | #define PHYCTRL_FREQSEL_50M 0x1 |
| 41 | #define PHYCTRL_FREQSEL_100M 0x2 |
| 42 | #define PHYCTRL_FREQSEL_150M 0x3 |
| 43 | #define PHYCTRL_DLL_LOCK_WO_TMOUT(x) \ |
| 44 | ((((x) >> PHYCTRL_DLLRDY_SHIFT) & PHYCTRL_DLLRDY_MASK) ==\ |
| 45 | PHYCTRL_DLLRDY_DONE) |
Kever Yang | 65922e0 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 46 | |
Alper Nebi Yasak | 9099d03 | 2022-03-15 20:46:27 +0300 | [diff] [blame] | 47 | #define ARASAN_VENDOR_REGISTER 0x78 |
| 48 | #define ARASAN_VENDOR_ENHANCED_STROBE BIT(0) |
| 49 | |
Alper Nebi Yasak | 6f19869 | 2022-03-15 20:46:28 +0300 | [diff] [blame] | 50 | /* DWC IP vendor area 1 pointer */ |
| 51 | #define DWCMSHC_P_VENDOR_AREA1 0xe8 |
| 52 | #define DWCMSHC_AREA1_MASK GENMASK(11, 0) |
| 53 | /* Offset inside the vendor area 1 */ |
| 54 | #define DWCMSHC_EMMC_CONTROL 0x2c |
| 55 | #define DWCMSHC_CARD_IS_EMMC BIT(0) |
| 56 | #define DWCMSHC_ENHANCED_STROBE BIT(8) |
| 57 | |
Yifeng Zhao | e5dddfa | 2021-06-29 16:24:42 +0800 | [diff] [blame] | 58 | /* Rockchip specific Registers */ |
| 59 | #define DWCMSHC_EMMC_DLL_CTRL 0x800 |
| 60 | #define DWCMSHC_EMMC_DLL_CTRL_RESET BIT(1) |
| 61 | #define DWCMSHC_EMMC_DLL_RXCLK 0x804 |
| 62 | #define DWCMSHC_EMMC_DLL_TXCLK 0x808 |
| 63 | #define DWCMSHC_EMMC_DLL_STRBIN 0x80c |
| 64 | #define DWCMSHC_EMMC_DLL_STATUS0 0x840 |
| 65 | #define DWCMSHC_EMMC_DLL_STATUS1 0x844 |
| 66 | #define DWCMSHC_EMMC_DLL_START BIT(0) |
| 67 | #define DWCMSHC_EMMC_DLL_RXCLK_SRCSEL 29 |
| 68 | #define DWCMSHC_EMMC_DLL_START_POINT 16 |
| 69 | #define DWCMSHC_EMMC_DLL_START_DEFAULT 5 |
| 70 | #define DWCMSHC_EMMC_DLL_INC_VALUE 2 |
| 71 | #define DWCMSHC_EMMC_DLL_INC 8 |
| 72 | #define DWCMSHC_EMMC_DLL_DLYENA BIT(27) |
Alper Nebi Yasak | 6f19869 | 2022-03-15 20:46:28 +0300 | [diff] [blame] | 73 | #define DLL_TXCLK_TAPNUM_DEFAULT 0xA |
| 74 | |
| 75 | #define DLL_STRBIN_TAPNUM_DEFAULT 0x8 |
| 76 | #define DLL_STRBIN_TAPNUM_FROM_SW BIT(24) |
| 77 | #define DLL_STRBIN_DELAY_NUM_SEL BIT(26) |
| 78 | #define DLL_STRBIN_DELAY_NUM_OFFSET 16 |
| 79 | #define DLL_STRBIN_DELAY_NUM_DEFAULT 0x16 |
| 80 | |
Yifeng Zhao | e5dddfa | 2021-06-29 16:24:42 +0800 | [diff] [blame] | 81 | #define DLL_TXCLK_TAPNUM_FROM_SW BIT(24) |
| 82 | #define DWCMSHC_EMMC_DLL_LOCKED BIT(8) |
| 83 | #define DWCMSHC_EMMC_DLL_TIMEOUT BIT(9) |
| 84 | #define DLL_RXCLK_NO_INVERTER 1 |
| 85 | #define DLL_RXCLK_INVERTER 0 |
| 86 | #define DWCMSHC_ENHANCED_STROBE BIT(8) |
| 87 | #define DLL_LOCK_WO_TMOUT(x) \ |
| 88 | ((((x) & DWCMSHC_EMMC_DLL_LOCKED) == DWCMSHC_EMMC_DLL_LOCKED) && \ |
| 89 | (((x) & DWCMSHC_EMMC_DLL_TIMEOUT) == 0)) |
| 90 | #define ROCKCHIP_MAX_CLKS 3 |
| 91 | |
Kever Yang | 65922e0 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 92 | struct rockchip_sdhc_plat { |
| 93 | struct mmc_config cfg; |
| 94 | struct mmc mmc; |
| 95 | }; |
| 96 | |
Yifeng Zhao | 5c2a5ab | 2021-06-29 16:24:41 +0800 | [diff] [blame] | 97 | struct rockchip_emmc_phy { |
| 98 | u32 emmcphy_con[7]; |
| 99 | u32 reserved; |
| 100 | u32 emmcphy_status; |
| 101 | }; |
| 102 | |
Kever Yang | 65922e0 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 103 | struct rockchip_sdhc { |
| 104 | struct sdhci_host host; |
Yifeng Zhao | 5c2a5ab | 2021-06-29 16:24:41 +0800 | [diff] [blame] | 105 | struct udevice *dev; |
Kever Yang | 65922e0 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 106 | void *base; |
Yifeng Zhao | 5c2a5ab | 2021-06-29 16:24:41 +0800 | [diff] [blame] | 107 | struct rockchip_emmc_phy *phy; |
| 108 | struct clk emmc_clk; |
| 109 | }; |
| 110 | |
| 111 | struct sdhci_data { |
Yifeng Zhao | 5c2a5ab | 2021-06-29 16:24:41 +0800 | [diff] [blame] | 112 | int (*emmc_phy_init)(struct udevice *dev); |
| 113 | int (*get_phy)(struct udevice *dev); |
Alper Nebi Yasak | 676a5a5 | 2022-01-29 01:42:37 +0300 | [diff] [blame] | 114 | |
| 115 | /** |
| 116 | * set_control_reg() - Set SDHCI control registers |
| 117 | * |
| 118 | * This is the set_control_reg() SDHCI operation that should be |
| 119 | * used for the hardware this driver data is associated with. |
| 120 | * Normally, this is used to set up control registers for |
| 121 | * voltage level and UHS speed mode. |
| 122 | * |
| 123 | * @host: SDHCI host structure |
| 124 | */ |
| 125 | void (*set_control_reg)(struct sdhci_host *host); |
| 126 | |
| 127 | /** |
| 128 | * set_ios_post() - Host specific hook after set_ios() calls |
| 129 | * |
| 130 | * This is the set_ios_post() SDHCI operation that should be |
| 131 | * used for the hardware this driver data is associated with. |
| 132 | * Normally, this is a hook that is called after sdhci_set_ios() |
| 133 | * that does any necessary host-specific configuration. |
| 134 | * |
| 135 | * @host: SDHCI host structure |
| 136 | * Return: 0 if successful, -ve on error |
| 137 | */ |
| 138 | int (*set_ios_post)(struct sdhci_host *host); |
Alper Nebi Yasak | 9099d03 | 2022-03-15 20:46:27 +0300 | [diff] [blame] | 139 | |
| 140 | /** |
| 141 | * set_enhanced_strobe() - Set HS400 Enhanced Strobe config |
| 142 | * |
| 143 | * This is the set_enhanced_strobe() SDHCI operation that should |
| 144 | * be used for the hardware this driver data is associated with. |
| 145 | * Normally, this is used to set any host-specific configuration |
| 146 | * necessary for HS400 ES. |
| 147 | * |
| 148 | * @host: SDHCI host structure |
| 149 | * Return: 0 if successful, -ve on error |
| 150 | */ |
| 151 | int (*set_enhanced_strobe)(struct sdhci_host *host); |
Yifeng Zhao | 5c2a5ab | 2021-06-29 16:24:41 +0800 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | static int rk3399_emmc_phy_init(struct udevice *dev) |
| 155 | { |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | static void rk3399_emmc_phy_power_on(struct rockchip_emmc_phy *phy, u32 clock) |
| 160 | { |
| 161 | u32 caldone, dllrdy, freqsel; |
| 162 | |
| 163 | writel(RK_CLRSETBITS(7 << 4, 0), &phy->emmcphy_con[6]); |
| 164 | writel(RK_CLRSETBITS(1 << 11, 1 << 11), &phy->emmcphy_con[0]); |
| 165 | writel(RK_CLRSETBITS(0xf << 7, 6 << 7), &phy->emmcphy_con[0]); |
| 166 | |
| 167 | /* |
| 168 | * According to the user manual, calpad calibration |
| 169 | * cycle takes more than 2us without the minimal recommended |
| 170 | * value, so we may need a little margin here |
| 171 | */ |
| 172 | udelay(3); |
| 173 | writel(RK_CLRSETBITS(1, 1), &phy->emmcphy_con[6]); |
| 174 | |
| 175 | /* |
| 176 | * According to the user manual, it asks driver to |
| 177 | * wait 5us for calpad busy trimming. But it seems that |
| 178 | * 5us of caldone isn't enough for all cases. |
| 179 | */ |
| 180 | udelay(500); |
| 181 | caldone = readl(&phy->emmcphy_status); |
| 182 | caldone = (caldone >> PHYCTRL_CALDONE_SHIFT) & PHYCTRL_CALDONE_MASK; |
| 183 | if (caldone != PHYCTRL_CALDONE_DONE) { |
| 184 | printf("%s: caldone timeout.\n", __func__); |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | /* Set the frequency of the DLL operation */ |
| 189 | if (clock < 75 * MHz) |
| 190 | freqsel = PHYCTRL_FREQSEL_50M; |
| 191 | else if (clock < 125 * MHz) |
| 192 | freqsel = PHYCTRL_FREQSEL_100M; |
| 193 | else if (clock < 175 * MHz) |
| 194 | freqsel = PHYCTRL_FREQSEL_150M; |
| 195 | else |
| 196 | freqsel = PHYCTRL_FREQSEL_200M; |
| 197 | |
| 198 | /* Set the frequency of the DLL operation */ |
| 199 | writel(RK_CLRSETBITS(3 << 12, freqsel << 12), &phy->emmcphy_con[0]); |
| 200 | writel(RK_CLRSETBITS(1 << 1, 1 << 1), &phy->emmcphy_con[6]); |
| 201 | |
Yifeng Zhao | 58ec23b | 2021-10-15 16:41:27 +0800 | [diff] [blame] | 202 | /* REN Enable on STRB Line for HS400 */ |
| 203 | writel(RK_CLRSETBITS(0, 1 << 9), &phy->emmcphy_con[2]); |
| 204 | |
Yifeng Zhao | 5c2a5ab | 2021-06-29 16:24:41 +0800 | [diff] [blame] | 205 | read_poll_timeout(readl, &phy->emmcphy_status, dllrdy, |
| 206 | PHYCTRL_DLL_LOCK_WO_TMOUT(dllrdy), 1, 5000); |
| 207 | } |
| 208 | |
| 209 | static void rk3399_emmc_phy_power_off(struct rockchip_emmc_phy *phy) |
| 210 | { |
| 211 | writel(RK_CLRSETBITS(1, 0), &phy->emmcphy_con[6]); |
| 212 | writel(RK_CLRSETBITS(1 << 1, 0), &phy->emmcphy_con[6]); |
| 213 | } |
| 214 | |
| 215 | static int rk3399_emmc_get_phy(struct udevice *dev) |
| 216 | { |
| 217 | struct rockchip_sdhc *priv = dev_get_priv(dev); |
| 218 | ofnode phy_node; |
| 219 | void *grf_base; |
| 220 | u32 grf_phy_offset, phandle; |
| 221 | |
| 222 | phandle = dev_read_u32_default(dev, "phys", 0); |
| 223 | phy_node = ofnode_get_by_phandle(phandle); |
| 224 | if (!ofnode_valid(phy_node)) { |
| 225 | debug("Not found emmc phy device\n"); |
| 226 | return -ENODEV; |
| 227 | } |
| 228 | |
| 229 | grf_base = syscon_get_first_range(ROCKCHIP_SYSCON_GRF); |
| 230 | if (grf_base < 0) { |
| 231 | printf("%s Get syscon grf failed", __func__); |
| 232 | return -ENODEV; |
| 233 | } |
| 234 | grf_phy_offset = ofnode_read_u32_default(phy_node, "reg", 0); |
| 235 | |
| 236 | priv->phy = (struct rockchip_emmc_phy *)(grf_base + grf_phy_offset); |
| 237 | |
| 238 | return 0; |
| 239 | } |
| 240 | |
Alper Nebi Yasak | 9099d03 | 2022-03-15 20:46:27 +0300 | [diff] [blame] | 241 | static int rk3399_sdhci_set_enhanced_strobe(struct sdhci_host *host) |
| 242 | { |
| 243 | struct mmc *mmc = host->mmc; |
| 244 | u32 vendor; |
| 245 | |
| 246 | vendor = sdhci_readl(host, ARASAN_VENDOR_REGISTER); |
| 247 | if (mmc->selected_mode == MMC_HS_400_ES) |
| 248 | vendor |= ARASAN_VENDOR_ENHANCED_STROBE; |
| 249 | else |
| 250 | vendor &= ~ARASAN_VENDOR_ENHANCED_STROBE; |
| 251 | sdhci_writel(host, vendor, ARASAN_VENDOR_REGISTER); |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
Alper Nebi Yasak | 676a5a5 | 2022-01-29 01:42:37 +0300 | [diff] [blame] | 256 | static void rk3399_sdhci_set_control_reg(struct sdhci_host *host) |
Yifeng Zhao | 5c2a5ab | 2021-06-29 16:24:41 +0800 | [diff] [blame] | 257 | { |
| 258 | struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host); |
Alper Nebi Yasak | 676a5a5 | 2022-01-29 01:42:37 +0300 | [diff] [blame] | 259 | struct mmc *mmc = host->mmc; |
| 260 | uint clock = mmc->tran_speed; |
Yifeng Zhao | 5c2a5ab | 2021-06-29 16:24:41 +0800 | [diff] [blame] | 261 | int cycle_phy = host->clock != clock && clock > EMMC_MIN_FREQ; |
| 262 | |
| 263 | if (cycle_phy) |
| 264 | rk3399_emmc_phy_power_off(priv->phy); |
| 265 | |
Alper Nebi Yasak | 676a5a5 | 2022-01-29 01:42:37 +0300 | [diff] [blame] | 266 | sdhci_set_control_reg(host); |
Alper Nebi Yasak | 9099d03 | 2022-03-15 20:46:27 +0300 | [diff] [blame] | 267 | |
| 268 | /* |
| 269 | * Reinitializing the device tries to set it to lower-speed modes |
| 270 | * first, which fails if the Enhanced Strobe bit is set, making |
| 271 | * the device impossible to use. Set the correct value here to |
| 272 | * let reinitialization attempts succeed. |
| 273 | */ |
| 274 | if (CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)) |
| 275 | rk3399_sdhci_set_enhanced_strobe(host); |
Alper Nebi Yasak | 676a5a5 | 2022-01-29 01:42:37 +0300 | [diff] [blame] | 276 | }; |
| 277 | |
| 278 | static int rk3399_sdhci_set_ios_post(struct sdhci_host *host) |
| 279 | { |
| 280 | struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host); |
| 281 | struct mmc *mmc = host->mmc; |
| 282 | uint clock = mmc->tran_speed; |
| 283 | int cycle_phy = host->clock != clock && clock > EMMC_MIN_FREQ; |
| 284 | |
| 285 | if (!clock) |
| 286 | clock = mmc->clock; |
Yifeng Zhao | 5c2a5ab | 2021-06-29 16:24:41 +0800 | [diff] [blame] | 287 | |
| 288 | if (cycle_phy) |
| 289 | rk3399_emmc_phy_power_on(priv->phy, clock); |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
Yifeng Zhao | e5dddfa | 2021-06-29 16:24:42 +0800 | [diff] [blame] | 294 | static int rk3568_emmc_phy_init(struct udevice *dev) |
| 295 | { |
| 296 | struct rockchip_sdhc *prv = dev_get_priv(dev); |
| 297 | struct sdhci_host *host = &prv->host; |
| 298 | u32 extra; |
| 299 | |
| 300 | extra = DLL_RXCLK_NO_INVERTER << DWCMSHC_EMMC_DLL_RXCLK_SRCSEL; |
| 301 | sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_RXCLK); |
| 302 | |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | static int rk3568_sdhci_emmc_set_clock(struct sdhci_host *host, unsigned int clock) |
| 307 | { |
| 308 | struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host); |
| 309 | int val, ret; |
| 310 | u32 extra; |
| 311 | |
| 312 | if (clock > host->max_clk) |
| 313 | clock = host->max_clk; |
| 314 | if (clock) |
| 315 | clk_set_rate(&priv->emmc_clk, clock); |
| 316 | |
| 317 | sdhci_set_clock(host->mmc, clock); |
| 318 | |
| 319 | if (clock >= 100 * MHz) { |
| 320 | /* reset DLL */ |
| 321 | sdhci_writel(host, DWCMSHC_EMMC_DLL_CTRL_RESET, DWCMSHC_EMMC_DLL_CTRL); |
| 322 | udelay(1); |
| 323 | sdhci_writel(host, 0, DWCMSHC_EMMC_DLL_CTRL); |
| 324 | |
| 325 | /* Init DLL settings */ |
| 326 | extra = DWCMSHC_EMMC_DLL_START_DEFAULT << DWCMSHC_EMMC_DLL_START_POINT | |
| 327 | DWCMSHC_EMMC_DLL_INC_VALUE << DWCMSHC_EMMC_DLL_INC | |
| 328 | DWCMSHC_EMMC_DLL_START; |
| 329 | sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_CTRL); |
| 330 | |
| 331 | ret = read_poll_timeout(readl, host->ioaddr + DWCMSHC_EMMC_DLL_STATUS0, |
| 332 | val, DLL_LOCK_WO_TMOUT(val), 1, 500); |
| 333 | if (ret) |
| 334 | return ret; |
| 335 | |
| 336 | extra = DWCMSHC_EMMC_DLL_DLYENA | |
| 337 | DLL_RXCLK_NO_INVERTER << DWCMSHC_EMMC_DLL_RXCLK_SRCSEL; |
| 338 | sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_RXCLK); |
| 339 | |
| 340 | extra = DWCMSHC_EMMC_DLL_DLYENA | |
| 341 | DLL_TXCLK_TAPNUM_DEFAULT | |
| 342 | DLL_TXCLK_TAPNUM_FROM_SW; |
| 343 | sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_TXCLK); |
| 344 | |
| 345 | extra = DWCMSHC_EMMC_DLL_DLYENA | |
Alper Nebi Yasak | 6f19869 | 2022-03-15 20:46:28 +0300 | [diff] [blame] | 346 | DLL_STRBIN_TAPNUM_DEFAULT | |
| 347 | DLL_STRBIN_TAPNUM_FROM_SW; |
Yifeng Zhao | e5dddfa | 2021-06-29 16:24:42 +0800 | [diff] [blame] | 348 | sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_STRBIN); |
| 349 | } else { |
| 350 | /* reset the clock phase when the frequency is lower than 100MHz */ |
| 351 | sdhci_writel(host, 0, DWCMSHC_EMMC_DLL_CTRL); |
| 352 | extra = DLL_RXCLK_NO_INVERTER << DWCMSHC_EMMC_DLL_RXCLK_SRCSEL; |
| 353 | sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_RXCLK); |
| 354 | sdhci_writel(host, 0, DWCMSHC_EMMC_DLL_TXCLK); |
Alper Nebi Yasak | 6f19869 | 2022-03-15 20:46:28 +0300 | [diff] [blame] | 355 | /* |
| 356 | * Before switching to hs400es mode, the driver will enable |
| 357 | * enhanced strobe first. PHY needs to configure the parameters |
| 358 | * of enhanced strobe first. |
| 359 | */ |
| 360 | extra = DWCMSHC_EMMC_DLL_DLYENA | |
| 361 | DLL_STRBIN_DELAY_NUM_SEL | |
| 362 | DLL_STRBIN_DELAY_NUM_DEFAULT << DLL_STRBIN_DELAY_NUM_OFFSET; |
| 363 | sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_STRBIN); |
Yifeng Zhao | e5dddfa | 2021-06-29 16:24:42 +0800 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | static int rk3568_emmc_get_phy(struct udevice *dev) |
| 370 | { |
Alper Nebi Yasak | 6f19869 | 2022-03-15 20:46:28 +0300 | [diff] [blame] | 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | static int rk3568_sdhci_set_enhanced_strobe(struct sdhci_host *host) |
| 375 | { |
| 376 | struct mmc *mmc = host->mmc; |
| 377 | u32 vendor; |
| 378 | int reg; |
| 379 | |
| 380 | reg = (sdhci_readl(host, DWCMSHC_P_VENDOR_AREA1) & DWCMSHC_AREA1_MASK) |
| 381 | + DWCMSHC_EMMC_CONTROL; |
| 382 | |
| 383 | vendor = sdhci_readl(host, reg); |
| 384 | if (mmc->selected_mode == MMC_HS_400_ES) |
| 385 | vendor |= DWCMSHC_ENHANCED_STROBE; |
| 386 | else |
| 387 | vendor &= ~DWCMSHC_ENHANCED_STROBE; |
| 388 | sdhci_writel(host, vendor, reg); |
| 389 | |
Yifeng Zhao | e5dddfa | 2021-06-29 16:24:42 +0800 | [diff] [blame] | 390 | return 0; |
| 391 | } |
| 392 | |
Alper Nebi Yasak | 676a5a5 | 2022-01-29 01:42:37 +0300 | [diff] [blame] | 393 | static int rk3568_sdhci_set_ios_post(struct sdhci_host *host) |
Yifeng Zhao | 5c2a5ab | 2021-06-29 16:24:41 +0800 | [diff] [blame] | 394 | { |
Yifeng Zhao | 5c2a5ab | 2021-06-29 16:24:41 +0800 | [diff] [blame] | 395 | struct mmc *mmc = host->mmc; |
| 396 | uint clock = mmc->tran_speed; |
Alper Nebi Yasak | 6f19869 | 2022-03-15 20:46:28 +0300 | [diff] [blame] | 397 | u32 reg, vendor_reg; |
Yifeng Zhao | 5c2a5ab | 2021-06-29 16:24:41 +0800 | [diff] [blame] | 398 | |
| 399 | if (!clock) |
| 400 | clock = mmc->clock; |
| 401 | |
Alper Nebi Yasak | 676a5a5 | 2022-01-29 01:42:37 +0300 | [diff] [blame] | 402 | rk3568_sdhci_emmc_set_clock(host, clock); |
Yifeng Zhao | 5c2a5ab | 2021-06-29 16:24:41 +0800 | [diff] [blame] | 403 | |
| 404 | if (mmc->selected_mode == MMC_HS_400 || mmc->selected_mode == MMC_HS_400_ES) { |
| 405 | reg = sdhci_readw(host, SDHCI_HOST_CONTROL2); |
| 406 | reg &= ~SDHCI_CTRL_UHS_MASK; |
Alper Nebi Yasak | 6f19869 | 2022-03-15 20:46:28 +0300 | [diff] [blame] | 407 | reg |= DWCMSHC_CTRL_HS400; |
Yifeng Zhao | 5c2a5ab | 2021-06-29 16:24:41 +0800 | [diff] [blame] | 408 | sdhci_writew(host, reg, SDHCI_HOST_CONTROL2); |
Alper Nebi Yasak | 6f19869 | 2022-03-15 20:46:28 +0300 | [diff] [blame] | 409 | |
| 410 | vendor_reg = (sdhci_readl(host, DWCMSHC_P_VENDOR_AREA1) & DWCMSHC_AREA1_MASK) |
| 411 | + DWCMSHC_EMMC_CONTROL; |
| 412 | /* set CARD_IS_EMMC bit to enable Data Strobe for HS400 */ |
| 413 | reg = sdhci_readw(host, vendor_reg); |
| 414 | reg |= DWCMSHC_CARD_IS_EMMC; |
| 415 | sdhci_writew(host, reg, vendor_reg); |
Yifeng Zhao | 5c2a5ab | 2021-06-29 16:24:41 +0800 | [diff] [blame] | 416 | } else { |
| 417 | sdhci_set_uhs_timing(host); |
| 418 | } |
| 419 | |
| 420 | return 0; |
| 421 | } |
| 422 | |
Alper Nebi Yasak | 676a5a5 | 2022-01-29 01:42:37 +0300 | [diff] [blame] | 423 | static void rockchip_sdhci_set_control_reg(struct sdhci_host *host) |
| 424 | { |
| 425 | struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host); |
| 426 | struct sdhci_data *data = (struct sdhci_data *)dev_get_driver_data(priv->dev); |
| 427 | |
| 428 | if (data->set_control_reg) |
| 429 | data->set_control_reg(host); |
| 430 | } |
| 431 | |
| 432 | static int rockchip_sdhci_set_ios_post(struct sdhci_host *host) |
| 433 | { |
| 434 | struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host); |
| 435 | struct sdhci_data *data = (struct sdhci_data *)dev_get_driver_data(priv->dev); |
| 436 | |
| 437 | if (data->set_ios_post) |
| 438 | return data->set_ios_post(host); |
| 439 | |
| 440 | return 0; |
| 441 | } |
| 442 | |
Yifeng Zhao | 5c2a5ab | 2021-06-29 16:24:41 +0800 | [diff] [blame] | 443 | static int rockchip_sdhci_execute_tuning(struct mmc *mmc, u8 opcode) |
| 444 | { |
| 445 | struct sdhci_host *host = dev_get_priv(mmc->dev); |
| 446 | char tuning_loop_counter = SDHCI_TUNING_LOOP_COUNT; |
| 447 | struct mmc_cmd cmd; |
| 448 | u32 ctrl, blk_size; |
| 449 | int ret = 0; |
| 450 | |
| 451 | ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2); |
| 452 | ctrl |= SDHCI_CTRL_EXEC_TUNING; |
| 453 | sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2); |
| 454 | |
| 455 | sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE); |
| 456 | sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE); |
| 457 | |
| 458 | blk_size = SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG, 64); |
| 459 | if (opcode == MMC_CMD_SEND_TUNING_BLOCK_HS200 && host->mmc->bus_width == 8) |
| 460 | blk_size = SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG, 128); |
| 461 | sdhci_writew(host, blk_size, SDHCI_BLOCK_SIZE); |
| 462 | sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE); |
| 463 | |
| 464 | cmd.cmdidx = opcode; |
| 465 | cmd.resp_type = MMC_RSP_R1; |
| 466 | cmd.cmdarg = 0; |
| 467 | |
| 468 | do { |
| 469 | if (tuning_loop_counter-- == 0) |
| 470 | break; |
| 471 | |
| 472 | mmc_send_cmd(mmc, &cmd, NULL); |
| 473 | |
| 474 | if (opcode == MMC_CMD_SEND_TUNING_BLOCK) |
| 475 | /* |
| 476 | * For tuning command, do not do busy loop. As tuning |
| 477 | * is happening (CLK-DATA latching for setup/hold time |
| 478 | * requirements), give time to complete |
| 479 | */ |
| 480 | udelay(1); |
| 481 | |
| 482 | ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2); |
| 483 | } while (ctrl & SDHCI_CTRL_EXEC_TUNING); |
| 484 | |
| 485 | if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) { |
| 486 | printf("%s:Tuning failed\n", __func__); |
| 487 | ret = -EIO; |
| 488 | } |
| 489 | |
| 490 | if (tuning_loop_counter < 0) { |
| 491 | ctrl &= ~SDHCI_CTRL_TUNED_CLK; |
| 492 | sdhci_writel(host, ctrl, SDHCI_HOST_CONTROL2); |
| 493 | } |
| 494 | |
| 495 | /* Enable only interrupts served by the SD controller */ |
| 496 | sdhci_writel(host, SDHCI_INT_DATA_MASK | SDHCI_INT_CMD_MASK, SDHCI_INT_ENABLE); |
| 497 | /* Mask all sdhci interrupt sources */ |
| 498 | sdhci_writel(host, 0x0, SDHCI_SIGNAL_ENABLE); |
| 499 | |
| 500 | return ret; |
| 501 | } |
| 502 | |
Alper Nebi Yasak | 9099d03 | 2022-03-15 20:46:27 +0300 | [diff] [blame] | 503 | static int rockchip_sdhci_set_enhanced_strobe(struct sdhci_host *host) |
| 504 | { |
| 505 | struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host); |
| 506 | struct sdhci_data *data = (struct sdhci_data *)dev_get_driver_data(priv->dev); |
| 507 | |
| 508 | if (data->set_enhanced_strobe) |
| 509 | return data->set_enhanced_strobe(host); |
| 510 | |
| 511 | return -ENOTSUPP; |
| 512 | } |
| 513 | |
Yifeng Zhao | 5c2a5ab | 2021-06-29 16:24:41 +0800 | [diff] [blame] | 514 | static struct sdhci_ops rockchip_sdhci_ops = { |
| 515 | .set_ios_post = rockchip_sdhci_set_ios_post, |
| 516 | .platform_execute_tuning = &rockchip_sdhci_execute_tuning, |
Alper Nebi Yasak | 676a5a5 | 2022-01-29 01:42:37 +0300 | [diff] [blame] | 517 | .set_control_reg = rockchip_sdhci_set_control_reg, |
Alper Nebi Yasak | 9099d03 | 2022-03-15 20:46:27 +0300 | [diff] [blame] | 518 | .set_enhanced_strobe = rockchip_sdhci_set_enhanced_strobe, |
Kever Yang | 65922e0 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 519 | }; |
| 520 | |
Yifeng Zhao | 5c2a5ab | 2021-06-29 16:24:41 +0800 | [diff] [blame] | 521 | static int rockchip_sdhci_probe(struct udevice *dev) |
Kever Yang | 65922e0 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 522 | { |
Yifeng Zhao | 5c2a5ab | 2021-06-29 16:24:41 +0800 | [diff] [blame] | 523 | struct sdhci_data *data = (struct sdhci_data *)dev_get_driver_data(dev); |
Kever Yang | 65922e0 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 524 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 525 | struct rockchip_sdhc_plat *plat = dev_get_plat(dev); |
Kever Yang | 65922e0 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 526 | struct rockchip_sdhc *prv = dev_get_priv(dev); |
Yifeng Zhao | 5c2a5ab | 2021-06-29 16:24:41 +0800 | [diff] [blame] | 527 | struct mmc_config *cfg = &plat->cfg; |
Kever Yang | 65922e0 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 528 | struct sdhci_host *host = &prv->host; |
Kever Yang | 9ea1fdf | 2016-12-28 11:32:35 +0800 | [diff] [blame] | 529 | struct clk clk; |
Yifeng Zhao | 5c2a5ab | 2021-06-29 16:24:41 +0800 | [diff] [blame] | 530 | int ret; |
Kever Yang | 9ea1fdf | 2016-12-28 11:32:35 +0800 | [diff] [blame] | 531 | |
Yifeng Zhao | 5c2a5ab | 2021-06-29 16:24:41 +0800 | [diff] [blame] | 532 | host->max_clk = cfg->f_max; |
Kever Yang | 9ea1fdf | 2016-12-28 11:32:35 +0800 | [diff] [blame] | 533 | ret = clk_get_by_index(dev, 0, &clk); |
| 534 | if (!ret) { |
Yifeng Zhao | 5c2a5ab | 2021-06-29 16:24:41 +0800 | [diff] [blame] | 535 | ret = clk_set_rate(&clk, host->max_clk); |
Kever Yang | 9ea1fdf | 2016-12-28 11:32:35 +0800 | [diff] [blame] | 536 | if (IS_ERR_VALUE(ret)) |
| 537 | printf("%s clk set rate fail!\n", __func__); |
| 538 | } else { |
| 539 | printf("%s fail to get clk\n", __func__); |
| 540 | } |
Kever Yang | 65922e0 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 541 | |
Yifeng Zhao | 5c2a5ab | 2021-06-29 16:24:41 +0800 | [diff] [blame] | 542 | prv->emmc_clk = clk; |
| 543 | prv->dev = dev; |
| 544 | |
| 545 | if (data->get_phy) { |
| 546 | ret = data->get_phy(dev); |
| 547 | if (ret) |
| 548 | return ret; |
| 549 | } |
| 550 | |
| 551 | if (data->emmc_phy_init) { |
| 552 | ret = data->emmc_phy_init(dev); |
| 553 | if (ret) |
| 554 | return ret; |
| 555 | } |
| 556 | |
| 557 | host->ops = &rockchip_sdhci_ops; |
Kever Yang | 65922e0 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 558 | host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD; |
| 559 | |
Kever Yang | 65922e0 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 560 | host->mmc = &plat->mmc; |
Kever Yang | 65922e0 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 561 | host->mmc->priv = &prv->host; |
| 562 | host->mmc->dev = dev; |
| 563 | upriv->mmc = host->mmc; |
| 564 | |
Yifeng Zhao | 5c2a5ab | 2021-06-29 16:24:41 +0800 | [diff] [blame] | 565 | ret = sdhci_setup_cfg(cfg, host, cfg->f_max, EMMC_MIN_FREQ); |
Kever Yang | 36d9bf8 | 2019-07-19 18:01:11 +0800 | [diff] [blame] | 566 | if (ret) |
| 567 | return ret; |
| 568 | |
Kever Yang | 65922e0 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 569 | return sdhci_probe(dev); |
| 570 | } |
| 571 | |
Yifeng Zhao | 5c2a5ab | 2021-06-29 16:24:41 +0800 | [diff] [blame] | 572 | static int rockchip_sdhci_of_to_plat(struct udevice *dev) |
Kever Yang | 65922e0 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 573 | { |
Yifeng Zhao | 5c2a5ab | 2021-06-29 16:24:41 +0800 | [diff] [blame] | 574 | struct rockchip_sdhc_plat *plat = dev_get_plat(dev); |
Kever Yang | 65922e0 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 575 | struct sdhci_host *host = dev_get_priv(dev); |
Yifeng Zhao | 5c2a5ab | 2021-06-29 16:24:41 +0800 | [diff] [blame] | 576 | struct mmc_config *cfg = &plat->cfg; |
| 577 | int ret; |
Kever Yang | 65922e0 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 578 | |
| 579 | host->name = dev->name; |
Philipp Tomsich | dbb2828 | 2017-09-11 22:04:21 +0200 | [diff] [blame] | 580 | host->ioaddr = dev_read_addr_ptr(dev); |
Yifeng Zhao | 5c2a5ab | 2021-06-29 16:24:41 +0800 | [diff] [blame] | 581 | |
| 582 | ret = mmc_of_parse(dev, cfg); |
| 583 | if (ret) |
| 584 | return ret; |
Kever Yang | 65922e0 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 585 | |
| 586 | return 0; |
| 587 | } |
| 588 | |
| 589 | static int rockchip_sdhci_bind(struct udevice *dev) |
| 590 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 591 | struct rockchip_sdhc_plat *plat = dev_get_plat(dev); |
Kever Yang | 65922e0 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 592 | |
Masahiro Yamada | cdb67f3 | 2016-09-06 22:17:32 +0900 | [diff] [blame] | 593 | return sdhci_bind(dev, &plat->mmc, &plat->cfg); |
Kever Yang | 65922e0 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 594 | } |
| 595 | |
Yifeng Zhao | 5c2a5ab | 2021-06-29 16:24:41 +0800 | [diff] [blame] | 596 | static const struct sdhci_data rk3399_data = { |
Yifeng Zhao | 5c2a5ab | 2021-06-29 16:24:41 +0800 | [diff] [blame] | 597 | .get_phy = rk3399_emmc_get_phy, |
| 598 | .emmc_phy_init = rk3399_emmc_phy_init, |
Alper Nebi Yasak | 676a5a5 | 2022-01-29 01:42:37 +0300 | [diff] [blame] | 599 | .set_control_reg = rk3399_sdhci_set_control_reg, |
| 600 | .set_ios_post = rk3399_sdhci_set_ios_post, |
Alper Nebi Yasak | 9099d03 | 2022-03-15 20:46:27 +0300 | [diff] [blame] | 601 | .set_enhanced_strobe = rk3399_sdhci_set_enhanced_strobe, |
Yifeng Zhao | 5c2a5ab | 2021-06-29 16:24:41 +0800 | [diff] [blame] | 602 | }; |
| 603 | |
Yifeng Zhao | e5dddfa | 2021-06-29 16:24:42 +0800 | [diff] [blame] | 604 | static const struct sdhci_data rk3568_data = { |
Yifeng Zhao | e5dddfa | 2021-06-29 16:24:42 +0800 | [diff] [blame] | 605 | .get_phy = rk3568_emmc_get_phy, |
| 606 | .emmc_phy_init = rk3568_emmc_phy_init, |
Alper Nebi Yasak | 676a5a5 | 2022-01-29 01:42:37 +0300 | [diff] [blame] | 607 | .set_ios_post = rk3568_sdhci_set_ios_post, |
Alper Nebi Yasak | 6f19869 | 2022-03-15 20:46:28 +0300 | [diff] [blame] | 608 | .set_enhanced_strobe = rk3568_sdhci_set_enhanced_strobe, |
Yifeng Zhao | e5dddfa | 2021-06-29 16:24:42 +0800 | [diff] [blame] | 609 | }; |
| 610 | |
Yifeng Zhao | 5c2a5ab | 2021-06-29 16:24:41 +0800 | [diff] [blame] | 611 | static const struct udevice_id sdhci_ids[] = { |
| 612 | { |
| 613 | .compatible = "arasan,sdhci-5.1", |
| 614 | .data = (ulong)&rk3399_data, |
| 615 | }, |
Yifeng Zhao | e5dddfa | 2021-06-29 16:24:42 +0800 | [diff] [blame] | 616 | { |
| 617 | .compatible = "rockchip,rk3568-dwcmshc", |
| 618 | .data = (ulong)&rk3568_data, |
| 619 | }, |
Kever Yang | 65922e0 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 620 | { } |
| 621 | }; |
| 622 | |
| 623 | U_BOOT_DRIVER(arasan_sdhci_drv) = { |
Yifeng Zhao | 5c2a5ab | 2021-06-29 16:24:41 +0800 | [diff] [blame] | 624 | .name = "rockchip_sdhci_5_1", |
Kever Yang | 65922e0 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 625 | .id = UCLASS_MMC, |
Yifeng Zhao | 5c2a5ab | 2021-06-29 16:24:41 +0800 | [diff] [blame] | 626 | .of_match = sdhci_ids, |
| 627 | .of_to_plat = rockchip_sdhci_of_to_plat, |
Kever Yang | 65922e0 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 628 | .ops = &sdhci_ops, |
| 629 | .bind = rockchip_sdhci_bind, |
Yifeng Zhao | 5c2a5ab | 2021-06-29 16:24:41 +0800 | [diff] [blame] | 630 | .probe = rockchip_sdhci_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 631 | .priv_auto = sizeof(struct rockchip_sdhc), |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 632 | .plat_auto = sizeof(struct rockchip_sdhc_plat), |
Kever Yang | 65922e0 | 2016-07-18 17:00:58 +0800 | [diff] [blame] | 633 | }; |