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