Hu Ziji | 06c8b66 | 2017-03-30 17:23:00 +0200 | [diff] [blame] | 1 | /* |
| 2 | * PHY support for Xenon SDHC |
| 3 | * |
| 4 | * Copyright (C) 2016 Marvell, All Rights Reserved. |
| 5 | * |
| 6 | * Author: Hu Ziji <huziji@marvell.com> |
| 7 | * Date: 2016-8-24 |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License as |
| 11 | * published by the Free Software Foundation version 2. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/delay.h> |
| 16 | #include <linux/ktime.h> |
| 17 | #include <linux/of_address.h> |
| 18 | |
| 19 | #include "sdhci-pltfm.h" |
| 20 | #include "sdhci-xenon.h" |
| 21 | |
| 22 | /* Register base for eMMC PHY 5.0 Version */ |
| 23 | #define XENON_EMMC_5_0_PHY_REG_BASE 0x0160 |
| 24 | /* Register base for eMMC PHY 5.1 Version */ |
| 25 | #define XENON_EMMC_PHY_REG_BASE 0x0170 |
| 26 | |
| 27 | #define XENON_EMMC_PHY_TIMING_ADJUST XENON_EMMC_PHY_REG_BASE |
| 28 | #define XENON_EMMC_5_0_PHY_TIMING_ADJUST XENON_EMMC_5_0_PHY_REG_BASE |
| 29 | #define XENON_TIMING_ADJUST_SLOW_MODE BIT(29) |
| 30 | #define XENON_TIMING_ADJUST_SDIO_MODE BIT(28) |
| 31 | #define XENON_SAMPL_INV_QSP_PHASE_SELECT BIT(18) |
| 32 | #define XENON_SAMPL_INV_QSP_PHASE_SELECT_SHIFT 18 |
| 33 | #define XENON_PHY_INITIALIZAION BIT(31) |
| 34 | #define XENON_WAIT_CYCLE_BEFORE_USING_MASK 0xF |
| 35 | #define XENON_WAIT_CYCLE_BEFORE_USING_SHIFT 12 |
| 36 | #define XENON_FC_SYNC_EN_DURATION_MASK 0xF |
| 37 | #define XENON_FC_SYNC_EN_DURATION_SHIFT 8 |
| 38 | #define XENON_FC_SYNC_RST_EN_DURATION_MASK 0xF |
| 39 | #define XENON_FC_SYNC_RST_EN_DURATION_SHIFT 4 |
| 40 | #define XENON_FC_SYNC_RST_DURATION_MASK 0xF |
| 41 | #define XENON_FC_SYNC_RST_DURATION_SHIFT 0 |
| 42 | |
| 43 | #define XENON_EMMC_PHY_FUNC_CONTROL (XENON_EMMC_PHY_REG_BASE + 0x4) |
| 44 | #define XENON_EMMC_5_0_PHY_FUNC_CONTROL \ |
| 45 | (XENON_EMMC_5_0_PHY_REG_BASE + 0x4) |
| 46 | #define XENON_ASYNC_DDRMODE_MASK BIT(23) |
| 47 | #define XENON_ASYNC_DDRMODE_SHIFT 23 |
| 48 | #define XENON_CMD_DDR_MODE BIT(16) |
| 49 | #define XENON_DQ_DDR_MODE_SHIFT 8 |
| 50 | #define XENON_DQ_DDR_MODE_MASK 0xFF |
| 51 | #define XENON_DQ_ASYNC_MODE BIT(4) |
| 52 | |
| 53 | #define XENON_EMMC_PHY_PAD_CONTROL (XENON_EMMC_PHY_REG_BASE + 0x8) |
| 54 | #define XENON_EMMC_5_0_PHY_PAD_CONTROL \ |
| 55 | (XENON_EMMC_5_0_PHY_REG_BASE + 0x8) |
| 56 | #define XENON_REC_EN_SHIFT 24 |
| 57 | #define XENON_REC_EN_MASK 0xF |
| 58 | #define XENON_FC_DQ_RECEN BIT(24) |
| 59 | #define XENON_FC_CMD_RECEN BIT(25) |
| 60 | #define XENON_FC_QSP_RECEN BIT(26) |
| 61 | #define XENON_FC_QSN_RECEN BIT(27) |
| 62 | #define XENON_OEN_QSN BIT(28) |
| 63 | #define XENON_AUTO_RECEN_CTRL BIT(30) |
| 64 | #define XENON_FC_ALL_CMOS_RECEIVER 0xF000 |
| 65 | |
| 66 | #define XENON_EMMC5_FC_QSP_PD BIT(18) |
| 67 | #define XENON_EMMC5_FC_QSP_PU BIT(22) |
| 68 | #define XENON_EMMC5_FC_CMD_PD BIT(17) |
| 69 | #define XENON_EMMC5_FC_CMD_PU BIT(21) |
| 70 | #define XENON_EMMC5_FC_DQ_PD BIT(16) |
| 71 | #define XENON_EMMC5_FC_DQ_PU BIT(20) |
| 72 | |
| 73 | #define XENON_EMMC_PHY_PAD_CONTROL1 (XENON_EMMC_PHY_REG_BASE + 0xC) |
| 74 | #define XENON_EMMC5_1_FC_QSP_PD BIT(9) |
| 75 | #define XENON_EMMC5_1_FC_QSP_PU BIT(25) |
| 76 | #define XENON_EMMC5_1_FC_CMD_PD BIT(8) |
| 77 | #define XENON_EMMC5_1_FC_CMD_PU BIT(24) |
| 78 | #define XENON_EMMC5_1_FC_DQ_PD 0xFF |
| 79 | #define XENON_EMMC5_1_FC_DQ_PU (0xFF << 16) |
| 80 | |
| 81 | #define XENON_EMMC_PHY_PAD_CONTROL2 (XENON_EMMC_PHY_REG_BASE + 0x10) |
| 82 | #define XENON_EMMC_5_0_PHY_PAD_CONTROL2 \ |
| 83 | (XENON_EMMC_5_0_PHY_REG_BASE + 0xC) |
| 84 | #define XENON_ZNR_MASK 0x1F |
| 85 | #define XENON_ZNR_SHIFT 8 |
| 86 | #define XENON_ZPR_MASK 0x1F |
| 87 | /* Preferred ZNR and ZPR value vary between different boards. |
| 88 | * The specific ZNR and ZPR value should be defined here |
| 89 | * according to board actual timing. |
| 90 | */ |
| 91 | #define XENON_ZNR_DEF_VALUE 0xF |
| 92 | #define XENON_ZPR_DEF_VALUE 0xF |
| 93 | |
| 94 | #define XENON_EMMC_PHY_DLL_CONTROL (XENON_EMMC_PHY_REG_BASE + 0x14) |
| 95 | #define XENON_EMMC_5_0_PHY_DLL_CONTROL \ |
| 96 | (XENON_EMMC_5_0_PHY_REG_BASE + 0x10) |
| 97 | #define XENON_DLL_ENABLE BIT(31) |
| 98 | #define XENON_DLL_UPDATE_STROBE_5_0 BIT(30) |
| 99 | #define XENON_DLL_REFCLK_SEL BIT(30) |
| 100 | #define XENON_DLL_UPDATE BIT(23) |
| 101 | #define XENON_DLL_PHSEL1_SHIFT 24 |
| 102 | #define XENON_DLL_PHSEL0_SHIFT 16 |
| 103 | #define XENON_DLL_PHASE_MASK 0x3F |
| 104 | #define XENON_DLL_PHASE_90_DEGREE 0x1F |
| 105 | #define XENON_DLL_FAST_LOCK BIT(5) |
| 106 | #define XENON_DLL_GAIN2X BIT(3) |
| 107 | #define XENON_DLL_BYPASS_EN BIT(0) |
| 108 | |
| 109 | #define XENON_EMMC_5_0_PHY_LOGIC_TIMING_ADJUST \ |
| 110 | (XENON_EMMC_5_0_PHY_REG_BASE + 0x14) |
| 111 | #define XENON_EMMC_PHY_LOGIC_TIMING_ADJUST (XENON_EMMC_PHY_REG_BASE + 0x18) |
| 112 | #define XENON_LOGIC_TIMING_VALUE 0x00AA8977 |
| 113 | |
| 114 | /* |
| 115 | * List offset of PHY registers and some special register values |
| 116 | * in eMMC PHY 5.0 or eMMC PHY 5.1 |
| 117 | */ |
| 118 | struct xenon_emmc_phy_regs { |
| 119 | /* Offset of Timing Adjust register */ |
| 120 | u16 timing_adj; |
| 121 | /* Offset of Func Control register */ |
| 122 | u16 func_ctrl; |
| 123 | /* Offset of Pad Control register */ |
| 124 | u16 pad_ctrl; |
| 125 | /* Offset of Pad Control register 2 */ |
| 126 | u16 pad_ctrl2; |
| 127 | /* Offset of DLL Control register */ |
| 128 | u16 dll_ctrl; |
| 129 | /* Offset of Logic Timing Adjust register */ |
| 130 | u16 logic_timing_adj; |
| 131 | /* DLL Update Enable bit */ |
| 132 | u32 dll_update; |
| 133 | }; |
| 134 | |
| 135 | static const char * const phy_types[] = { |
| 136 | "emmc 5.0 phy", |
| 137 | "emmc 5.1 phy" |
| 138 | }; |
| 139 | |
| 140 | enum xenon_phy_type_enum { |
| 141 | EMMC_5_0_PHY, |
| 142 | EMMC_5_1_PHY, |
| 143 | NR_PHY_TYPES |
| 144 | }; |
| 145 | |
Hu Ziji | 298269c | 2017-03-30 17:23:01 +0200 | [diff] [blame] | 146 | enum soc_pad_ctrl_type { |
| 147 | SOC_PAD_SD, |
| 148 | SOC_PAD_FIXED_1_8V, |
| 149 | }; |
| 150 | |
| 151 | struct soc_pad_ctrl { |
| 152 | /* Register address of SoC PHY PAD ctrl */ |
| 153 | void __iomem *reg; |
| 154 | /* SoC PHY PAD ctrl type */ |
| 155 | enum soc_pad_ctrl_type pad_type; |
| 156 | /* SoC specific operation to set SoC PHY PAD */ |
| 157 | void (*set_soc_pad)(struct sdhci_host *host, |
| 158 | unsigned char signal_voltage); |
| 159 | }; |
| 160 | |
Hu Ziji | 06c8b66 | 2017-03-30 17:23:00 +0200 | [diff] [blame] | 161 | static struct xenon_emmc_phy_regs xenon_emmc_5_0_phy_regs = { |
| 162 | .timing_adj = XENON_EMMC_5_0_PHY_TIMING_ADJUST, |
| 163 | .func_ctrl = XENON_EMMC_5_0_PHY_FUNC_CONTROL, |
| 164 | .pad_ctrl = XENON_EMMC_5_0_PHY_PAD_CONTROL, |
| 165 | .pad_ctrl2 = XENON_EMMC_5_0_PHY_PAD_CONTROL2, |
| 166 | .dll_ctrl = XENON_EMMC_5_0_PHY_DLL_CONTROL, |
| 167 | .logic_timing_adj = XENON_EMMC_5_0_PHY_LOGIC_TIMING_ADJUST, |
| 168 | .dll_update = XENON_DLL_UPDATE_STROBE_5_0, |
| 169 | }; |
| 170 | |
| 171 | static struct xenon_emmc_phy_regs xenon_emmc_5_1_phy_regs = { |
| 172 | .timing_adj = XENON_EMMC_PHY_TIMING_ADJUST, |
| 173 | .func_ctrl = XENON_EMMC_PHY_FUNC_CONTROL, |
| 174 | .pad_ctrl = XENON_EMMC_PHY_PAD_CONTROL, |
| 175 | .pad_ctrl2 = XENON_EMMC_PHY_PAD_CONTROL2, |
| 176 | .dll_ctrl = XENON_EMMC_PHY_DLL_CONTROL, |
| 177 | .logic_timing_adj = XENON_EMMC_PHY_LOGIC_TIMING_ADJUST, |
| 178 | .dll_update = XENON_DLL_UPDATE, |
| 179 | }; |
| 180 | |
| 181 | /* |
| 182 | * eMMC PHY configuration and operations |
| 183 | */ |
| 184 | struct xenon_emmc_phy_params { |
| 185 | bool slow_mode; |
| 186 | |
| 187 | u8 znr; |
| 188 | u8 zpr; |
| 189 | |
| 190 | /* Nr of consecutive Sampling Points of a Valid Sampling Window */ |
| 191 | u8 nr_tun_times; |
| 192 | /* Divider for calculating Tuning Step */ |
| 193 | u8 tun_step_divider; |
Hu Ziji | 298269c | 2017-03-30 17:23:01 +0200 | [diff] [blame] | 194 | |
| 195 | struct soc_pad_ctrl pad_ctrl; |
Hu Ziji | 06c8b66 | 2017-03-30 17:23:00 +0200 | [diff] [blame] | 196 | }; |
| 197 | |
| 198 | static int xenon_alloc_emmc_phy(struct sdhci_host *host) |
| 199 | { |
| 200 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 201 | struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host); |
| 202 | struct xenon_emmc_phy_params *params; |
| 203 | |
| 204 | params = devm_kzalloc(mmc_dev(host->mmc), sizeof(*params), GFP_KERNEL); |
| 205 | if (!params) |
| 206 | return -ENOMEM; |
| 207 | |
| 208 | priv->phy_params = params; |
| 209 | if (priv->phy_type == EMMC_5_0_PHY) |
| 210 | priv->emmc_phy_regs = &xenon_emmc_5_0_phy_regs; |
| 211 | else |
| 212 | priv->emmc_phy_regs = &xenon_emmc_5_1_phy_regs; |
| 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * eMMC 5.0/5.1 PHY init/re-init. |
| 219 | * eMMC PHY init should be executed after: |
| 220 | * 1. SDCLK frequency changes. |
| 221 | * 2. SDCLK is stopped and re-enabled. |
| 222 | * 3. config in emmc_phy_regs->timing_adj and emmc_phy_regs->func_ctrl |
| 223 | * are changed |
| 224 | */ |
| 225 | static int xenon_emmc_phy_init(struct sdhci_host *host) |
| 226 | { |
| 227 | u32 reg; |
| 228 | u32 wait, clock; |
| 229 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 230 | struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host); |
| 231 | struct xenon_emmc_phy_regs *phy_regs = priv->emmc_phy_regs; |
| 232 | |
| 233 | reg = sdhci_readl(host, phy_regs->timing_adj); |
| 234 | reg |= XENON_PHY_INITIALIZAION; |
| 235 | sdhci_writel(host, reg, phy_regs->timing_adj); |
| 236 | |
| 237 | /* Add duration of FC_SYNC_RST */ |
| 238 | wait = ((reg >> XENON_FC_SYNC_RST_DURATION_SHIFT) & |
| 239 | XENON_FC_SYNC_RST_DURATION_MASK); |
| 240 | /* Add interval between FC_SYNC_EN and FC_SYNC_RST */ |
| 241 | wait += ((reg >> XENON_FC_SYNC_RST_EN_DURATION_SHIFT) & |
| 242 | XENON_FC_SYNC_RST_EN_DURATION_MASK); |
| 243 | /* Add duration of asserting FC_SYNC_EN */ |
| 244 | wait += ((reg >> XENON_FC_SYNC_EN_DURATION_SHIFT) & |
| 245 | XENON_FC_SYNC_EN_DURATION_MASK); |
| 246 | /* Add duration of waiting for PHY */ |
| 247 | wait += ((reg >> XENON_WAIT_CYCLE_BEFORE_USING_SHIFT) & |
| 248 | XENON_WAIT_CYCLE_BEFORE_USING_MASK); |
| 249 | /* 4 additional bus clock and 4 AXI bus clock are required */ |
| 250 | wait += 8; |
| 251 | wait <<= 20; |
| 252 | |
| 253 | clock = host->clock; |
| 254 | if (!clock) |
| 255 | /* Use the possibly slowest bus frequency value */ |
| 256 | clock = XENON_LOWEST_SDCLK_FREQ; |
| 257 | /* get the wait time */ |
| 258 | wait /= clock; |
| 259 | wait++; |
| 260 | /* wait for host eMMC PHY init completes */ |
| 261 | udelay(wait); |
| 262 | |
| 263 | reg = sdhci_readl(host, phy_regs->timing_adj); |
| 264 | reg &= XENON_PHY_INITIALIZAION; |
| 265 | if (reg) { |
| 266 | dev_err(mmc_dev(host->mmc), "eMMC PHY init cannot complete after %d us\n", |
| 267 | wait); |
| 268 | return -ETIMEDOUT; |
| 269 | } |
| 270 | |
| 271 | return 0; |
| 272 | } |
| 273 | |
Hu Ziji | 298269c | 2017-03-30 17:23:01 +0200 | [diff] [blame] | 274 | #define ARMADA_3700_SOC_PAD_1_8V 0x1 |
| 275 | #define ARMADA_3700_SOC_PAD_3_3V 0x0 |
| 276 | |
| 277 | static void armada_3700_soc_pad_voltage_set(struct sdhci_host *host, |
| 278 | unsigned char signal_voltage) |
| 279 | { |
| 280 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 281 | struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host); |
| 282 | struct xenon_emmc_phy_params *params = priv->phy_params; |
| 283 | |
| 284 | if (params->pad_ctrl.pad_type == SOC_PAD_FIXED_1_8V) { |
| 285 | writel(ARMADA_3700_SOC_PAD_1_8V, params->pad_ctrl.reg); |
| 286 | } else if (params->pad_ctrl.pad_type == SOC_PAD_SD) { |
| 287 | if (signal_voltage == MMC_SIGNAL_VOLTAGE_180) |
| 288 | writel(ARMADA_3700_SOC_PAD_1_8V, params->pad_ctrl.reg); |
| 289 | else if (signal_voltage == MMC_SIGNAL_VOLTAGE_330) |
| 290 | writel(ARMADA_3700_SOC_PAD_3_3V, params->pad_ctrl.reg); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | /* |
| 295 | * Set SoC PHY voltage PAD control register, |
| 296 | * according to the operation voltage on PAD. |
| 297 | * The detailed operation depends on SoC implementation. |
| 298 | */ |
| 299 | static void xenon_emmc_phy_set_soc_pad(struct sdhci_host *host, |
| 300 | unsigned char signal_voltage) |
| 301 | { |
| 302 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 303 | struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host); |
| 304 | struct xenon_emmc_phy_params *params = priv->phy_params; |
| 305 | |
| 306 | if (!params->pad_ctrl.reg) |
| 307 | return; |
| 308 | |
| 309 | if (params->pad_ctrl.set_soc_pad) |
| 310 | params->pad_ctrl.set_soc_pad(host, signal_voltage); |
| 311 | } |
| 312 | |
Hu Ziji | 06c8b66 | 2017-03-30 17:23:00 +0200 | [diff] [blame] | 313 | /* |
| 314 | * Enable eMMC PHY HW DLL |
| 315 | * DLL should be enabled and stable before HS200/SDR104 tuning, |
| 316 | * and before HS400 data strobe setting. |
| 317 | */ |
| 318 | static int xenon_emmc_phy_enable_dll(struct sdhci_host *host) |
| 319 | { |
| 320 | u32 reg; |
| 321 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 322 | struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host); |
| 323 | struct xenon_emmc_phy_regs *phy_regs = priv->emmc_phy_regs; |
| 324 | ktime_t timeout; |
| 325 | |
| 326 | if (WARN_ON(host->clock <= MMC_HIGH_52_MAX_DTR)) |
| 327 | return -EINVAL; |
| 328 | |
| 329 | reg = sdhci_readl(host, phy_regs->dll_ctrl); |
| 330 | if (reg & XENON_DLL_ENABLE) |
| 331 | return 0; |
| 332 | |
| 333 | /* Enable DLL */ |
| 334 | reg = sdhci_readl(host, phy_regs->dll_ctrl); |
| 335 | reg |= (XENON_DLL_ENABLE | XENON_DLL_FAST_LOCK); |
| 336 | |
| 337 | /* |
| 338 | * Set Phase as 90 degree, which is most common value. |
| 339 | * Might set another value if necessary. |
| 340 | * The granularity is 1 degree. |
| 341 | */ |
| 342 | reg &= ~((XENON_DLL_PHASE_MASK << XENON_DLL_PHSEL0_SHIFT) | |
| 343 | (XENON_DLL_PHASE_MASK << XENON_DLL_PHSEL1_SHIFT)); |
| 344 | reg |= ((XENON_DLL_PHASE_90_DEGREE << XENON_DLL_PHSEL0_SHIFT) | |
| 345 | (XENON_DLL_PHASE_90_DEGREE << XENON_DLL_PHSEL1_SHIFT)); |
| 346 | |
| 347 | reg &= ~XENON_DLL_BYPASS_EN; |
| 348 | reg |= phy_regs->dll_update; |
| 349 | if (priv->phy_type == EMMC_5_1_PHY) |
| 350 | reg &= ~XENON_DLL_REFCLK_SEL; |
| 351 | sdhci_writel(host, reg, phy_regs->dll_ctrl); |
| 352 | |
| 353 | /* Wait max 32 ms */ |
| 354 | timeout = ktime_add_ms(ktime_get(), 32); |
| 355 | while (!(sdhci_readw(host, XENON_SLOT_EXT_PRESENT_STATE) & |
| 356 | XENON_DLL_LOCK_STATE)) { |
| 357 | if (ktime_after(ktime_get(), timeout)) { |
| 358 | dev_err(mmc_dev(host->mmc), "Wait for DLL Lock time-out\n"); |
| 359 | return -ETIMEDOUT; |
| 360 | } |
| 361 | udelay(100); |
| 362 | } |
| 363 | return 0; |
| 364 | } |
| 365 | |
| 366 | /* |
| 367 | * Config to eMMC PHY to prepare for tuning. |
| 368 | * Enable HW DLL and set the TUNING_STEP |
| 369 | */ |
| 370 | static int xenon_emmc_phy_config_tuning(struct sdhci_host *host) |
| 371 | { |
| 372 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 373 | struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host); |
| 374 | struct xenon_emmc_phy_params *params = priv->phy_params; |
| 375 | u32 reg, tuning_step; |
| 376 | int ret; |
| 377 | |
| 378 | if (host->clock <= MMC_HIGH_52_MAX_DTR) |
| 379 | return -EINVAL; |
| 380 | |
| 381 | ret = xenon_emmc_phy_enable_dll(host); |
| 382 | if (ret) |
| 383 | return ret; |
| 384 | |
| 385 | /* Achieve TUNING_STEP with HW DLL help */ |
| 386 | reg = sdhci_readl(host, XENON_SLOT_DLL_CUR_DLY_VAL); |
| 387 | tuning_step = reg / params->tun_step_divider; |
| 388 | if (unlikely(tuning_step > XENON_TUNING_STEP_MASK)) { |
| 389 | dev_warn(mmc_dev(host->mmc), |
| 390 | "HS200 TUNING_STEP %d is larger than MAX value\n", |
| 391 | tuning_step); |
| 392 | tuning_step = XENON_TUNING_STEP_MASK; |
| 393 | } |
| 394 | |
| 395 | /* Set TUNING_STEP for later tuning */ |
| 396 | reg = sdhci_readl(host, XENON_SLOT_OP_STATUS_CTRL); |
| 397 | reg &= ~(XENON_TUN_CONSECUTIVE_TIMES_MASK << |
| 398 | XENON_TUN_CONSECUTIVE_TIMES_SHIFT); |
| 399 | reg |= (params->nr_tun_times << XENON_TUN_CONSECUTIVE_TIMES_SHIFT); |
| 400 | reg &= ~(XENON_TUNING_STEP_MASK << XENON_TUNING_STEP_SHIFT); |
| 401 | reg |= (tuning_step << XENON_TUNING_STEP_SHIFT); |
| 402 | sdhci_writel(host, reg, XENON_SLOT_OP_STATUS_CTRL); |
| 403 | |
| 404 | return 0; |
| 405 | } |
| 406 | |
| 407 | static void xenon_emmc_phy_disable_data_strobe(struct sdhci_host *host) |
| 408 | { |
| 409 | u32 reg; |
| 410 | |
| 411 | /* Disable SDHC Data Strobe */ |
| 412 | reg = sdhci_readl(host, XENON_SLOT_EMMC_CTRL); |
| 413 | reg &= ~XENON_ENABLE_DATA_STROBE; |
| 414 | sdhci_writel(host, reg, XENON_SLOT_EMMC_CTRL); |
| 415 | } |
| 416 | |
| 417 | /* Set HS400 Data Strobe */ |
| 418 | static void xenon_emmc_phy_strobe_delay_adj(struct sdhci_host *host) |
| 419 | { |
| 420 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 421 | struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host); |
| 422 | u32 reg; |
| 423 | |
| 424 | if (WARN_ON(host->timing != MMC_TIMING_MMC_HS400)) |
| 425 | return; |
| 426 | |
| 427 | if (host->clock <= MMC_HIGH_52_MAX_DTR) |
| 428 | return; |
| 429 | |
| 430 | dev_dbg(mmc_dev(host->mmc), "starts HS400 strobe delay adjustment\n"); |
| 431 | |
| 432 | xenon_emmc_phy_enable_dll(host); |
| 433 | |
| 434 | /* Enable SDHC Data Strobe */ |
| 435 | reg = sdhci_readl(host, XENON_SLOT_EMMC_CTRL); |
| 436 | reg |= XENON_ENABLE_DATA_STROBE; |
| 437 | sdhci_writel(host, reg, XENON_SLOT_EMMC_CTRL); |
| 438 | |
| 439 | /* Set Data Strobe Pull down */ |
| 440 | if (priv->phy_type == EMMC_5_0_PHY) { |
| 441 | reg = sdhci_readl(host, XENON_EMMC_5_0_PHY_PAD_CONTROL); |
| 442 | reg |= XENON_EMMC5_FC_QSP_PD; |
| 443 | reg &= ~XENON_EMMC5_FC_QSP_PU; |
| 444 | sdhci_writel(host, reg, XENON_EMMC_5_0_PHY_PAD_CONTROL); |
| 445 | } else { |
| 446 | reg = sdhci_readl(host, XENON_EMMC_PHY_PAD_CONTROL1); |
| 447 | reg |= XENON_EMMC5_1_FC_QSP_PD; |
| 448 | reg &= ~XENON_EMMC5_1_FC_QSP_PU; |
| 449 | sdhci_writel(host, reg, XENON_EMMC_PHY_PAD_CONTROL1); |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | /* |
| 454 | * If eMMC PHY Slow Mode is required in lower speed mode (SDCLK < 55MHz) |
| 455 | * in SDR mode, enable Slow Mode to bypass eMMC PHY. |
| 456 | * SDIO slower SDR mode also requires Slow Mode. |
| 457 | * |
| 458 | * If Slow Mode is enabled, return true. |
| 459 | * Otherwise, return false. |
| 460 | */ |
| 461 | static bool xenon_emmc_phy_slow_mode(struct sdhci_host *host, |
| 462 | unsigned char timing) |
| 463 | { |
| 464 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 465 | struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host); |
| 466 | struct xenon_emmc_phy_params *params = priv->phy_params; |
| 467 | struct xenon_emmc_phy_regs *phy_regs = priv->emmc_phy_regs; |
| 468 | u32 reg; |
| 469 | int ret; |
| 470 | |
| 471 | if (host->clock > MMC_HIGH_52_MAX_DTR) |
| 472 | return false; |
| 473 | |
| 474 | reg = sdhci_readl(host, phy_regs->timing_adj); |
| 475 | /* When in slower SDR mode, enable Slow Mode for SDIO |
| 476 | * or when Slow Mode flag is set |
| 477 | */ |
| 478 | switch (timing) { |
| 479 | case MMC_TIMING_LEGACY: |
| 480 | /* |
| 481 | * If Slow Mode is required, enable Slow Mode by default |
| 482 | * in early init phase to avoid any potential issue. |
| 483 | */ |
| 484 | if (params->slow_mode) { |
| 485 | reg |= XENON_TIMING_ADJUST_SLOW_MODE; |
| 486 | ret = true; |
| 487 | } else { |
| 488 | reg &= ~XENON_TIMING_ADJUST_SLOW_MODE; |
| 489 | ret = false; |
| 490 | } |
| 491 | break; |
| 492 | case MMC_TIMING_UHS_SDR25: |
| 493 | case MMC_TIMING_UHS_SDR12: |
| 494 | case MMC_TIMING_SD_HS: |
| 495 | case MMC_TIMING_MMC_HS: |
| 496 | if ((priv->init_card_type == MMC_TYPE_SDIO) || |
| 497 | params->slow_mode) { |
| 498 | reg |= XENON_TIMING_ADJUST_SLOW_MODE; |
| 499 | ret = true; |
| 500 | break; |
| 501 | } |
| 502 | default: |
| 503 | reg &= ~XENON_TIMING_ADJUST_SLOW_MODE; |
| 504 | ret = false; |
| 505 | } |
| 506 | |
| 507 | sdhci_writel(host, reg, phy_regs->timing_adj); |
| 508 | return ret; |
| 509 | } |
| 510 | |
| 511 | /* |
| 512 | * Set-up eMMC 5.0/5.1 PHY. |
| 513 | * Specific configuration depends on the current speed mode in use. |
| 514 | */ |
| 515 | static void xenon_emmc_phy_set(struct sdhci_host *host, |
| 516 | unsigned char timing) |
| 517 | { |
| 518 | u32 reg; |
| 519 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 520 | struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host); |
| 521 | struct xenon_emmc_phy_params *params = priv->phy_params; |
| 522 | struct xenon_emmc_phy_regs *phy_regs = priv->emmc_phy_regs; |
| 523 | |
| 524 | dev_dbg(mmc_dev(host->mmc), "eMMC PHY setting starts\n"); |
| 525 | |
| 526 | /* Setup pad, set bit[28] and bits[26:24] */ |
| 527 | reg = sdhci_readl(host, phy_regs->pad_ctrl); |
| 528 | reg |= (XENON_FC_DQ_RECEN | XENON_FC_CMD_RECEN | |
| 529 | XENON_FC_QSP_RECEN | XENON_OEN_QSN); |
| 530 | /* All FC_XX_RECEIVCE should be set as CMOS Type */ |
| 531 | reg |= XENON_FC_ALL_CMOS_RECEIVER; |
| 532 | sdhci_writel(host, reg, phy_regs->pad_ctrl); |
| 533 | |
| 534 | /* Set CMD and DQ Pull Up */ |
| 535 | if (priv->phy_type == EMMC_5_0_PHY) { |
| 536 | reg = sdhci_readl(host, XENON_EMMC_5_0_PHY_PAD_CONTROL); |
| 537 | reg |= (XENON_EMMC5_FC_CMD_PU | XENON_EMMC5_FC_DQ_PU); |
| 538 | reg &= ~(XENON_EMMC5_FC_CMD_PD | XENON_EMMC5_FC_DQ_PD); |
| 539 | sdhci_writel(host, reg, XENON_EMMC_5_0_PHY_PAD_CONTROL); |
| 540 | } else { |
| 541 | reg = sdhci_readl(host, XENON_EMMC_PHY_PAD_CONTROL1); |
| 542 | reg |= (XENON_EMMC5_1_FC_CMD_PU | XENON_EMMC5_1_FC_DQ_PU); |
| 543 | reg &= ~(XENON_EMMC5_1_FC_CMD_PD | XENON_EMMC5_1_FC_DQ_PD); |
| 544 | sdhci_writel(host, reg, XENON_EMMC_PHY_PAD_CONTROL1); |
| 545 | } |
| 546 | |
| 547 | if (timing == MMC_TIMING_LEGACY) { |
| 548 | xenon_emmc_phy_slow_mode(host, timing); |
| 549 | goto phy_init; |
| 550 | } |
| 551 | |
| 552 | /* |
| 553 | * If SDIO card, set SDIO Mode |
| 554 | * Otherwise, clear SDIO Mode |
| 555 | */ |
| 556 | reg = sdhci_readl(host, phy_regs->timing_adj); |
| 557 | if (priv->init_card_type == MMC_TYPE_SDIO) |
| 558 | reg |= XENON_TIMING_ADJUST_SDIO_MODE; |
| 559 | else |
| 560 | reg &= ~XENON_TIMING_ADJUST_SDIO_MODE; |
| 561 | sdhci_writel(host, reg, phy_regs->timing_adj); |
| 562 | |
| 563 | if (xenon_emmc_phy_slow_mode(host, timing)) |
| 564 | goto phy_init; |
| 565 | |
| 566 | /* |
| 567 | * Set preferred ZNR and ZPR value |
| 568 | * The ZNR and ZPR value vary between different boards. |
| 569 | * Define them both in sdhci-xenon-emmc-phy.h. |
| 570 | */ |
| 571 | reg = sdhci_readl(host, phy_regs->pad_ctrl2); |
| 572 | reg &= ~((XENON_ZNR_MASK << XENON_ZNR_SHIFT) | XENON_ZPR_MASK); |
| 573 | reg |= ((params->znr << XENON_ZNR_SHIFT) | params->zpr); |
| 574 | sdhci_writel(host, reg, phy_regs->pad_ctrl2); |
| 575 | |
| 576 | /* |
| 577 | * When setting EMMC_PHY_FUNC_CONTROL register, |
| 578 | * SD clock should be disabled |
| 579 | */ |
| 580 | reg = sdhci_readl(host, SDHCI_CLOCK_CONTROL); |
| 581 | reg &= ~SDHCI_CLOCK_CARD_EN; |
| 582 | sdhci_writew(host, reg, SDHCI_CLOCK_CONTROL); |
| 583 | |
| 584 | reg = sdhci_readl(host, phy_regs->func_ctrl); |
| 585 | switch (timing) { |
| 586 | case MMC_TIMING_MMC_HS400: |
| 587 | reg |= (XENON_DQ_DDR_MODE_MASK << XENON_DQ_DDR_MODE_SHIFT) | |
| 588 | XENON_CMD_DDR_MODE; |
| 589 | reg &= ~XENON_DQ_ASYNC_MODE; |
| 590 | break; |
| 591 | case MMC_TIMING_UHS_DDR50: |
| 592 | case MMC_TIMING_MMC_DDR52: |
| 593 | reg |= (XENON_DQ_DDR_MODE_MASK << XENON_DQ_DDR_MODE_SHIFT) | |
| 594 | XENON_CMD_DDR_MODE | XENON_DQ_ASYNC_MODE; |
| 595 | break; |
| 596 | default: |
| 597 | reg &= ~((XENON_DQ_DDR_MODE_MASK << XENON_DQ_DDR_MODE_SHIFT) | |
| 598 | XENON_CMD_DDR_MODE); |
| 599 | reg |= XENON_DQ_ASYNC_MODE; |
| 600 | } |
| 601 | sdhci_writel(host, reg, phy_regs->func_ctrl); |
| 602 | |
| 603 | /* Enable bus clock */ |
| 604 | reg = sdhci_readl(host, SDHCI_CLOCK_CONTROL); |
| 605 | reg |= SDHCI_CLOCK_CARD_EN; |
| 606 | sdhci_writew(host, reg, SDHCI_CLOCK_CONTROL); |
| 607 | |
| 608 | if (timing == MMC_TIMING_MMC_HS400) |
| 609 | /* Hardware team recommend a value for HS400 */ |
| 610 | sdhci_writel(host, XENON_LOGIC_TIMING_VALUE, |
| 611 | phy_regs->logic_timing_adj); |
| 612 | else |
| 613 | xenon_emmc_phy_disable_data_strobe(host); |
| 614 | |
| 615 | phy_init: |
| 616 | xenon_emmc_phy_init(host); |
| 617 | |
| 618 | dev_dbg(mmc_dev(host->mmc), "eMMC PHY setting completes\n"); |
| 619 | } |
| 620 | |
Hu Ziji | 298269c | 2017-03-30 17:23:01 +0200 | [diff] [blame] | 621 | static int get_dt_pad_ctrl_data(struct sdhci_host *host, |
| 622 | struct device_node *np, |
| 623 | struct xenon_emmc_phy_params *params) |
| 624 | { |
| 625 | int ret = 0; |
| 626 | const char *name; |
| 627 | struct resource iomem; |
| 628 | |
| 629 | if (of_device_is_compatible(np, "marvell,armada-3700-sdhci")) |
| 630 | params->pad_ctrl.set_soc_pad = armada_3700_soc_pad_voltage_set; |
| 631 | else |
| 632 | return 0; |
| 633 | |
| 634 | if (of_address_to_resource(np, 1, &iomem)) { |
| 635 | dev_err(mmc_dev(host->mmc), "Unable to find SoC PAD ctrl register address for %s\n", |
| 636 | np->name); |
| 637 | return -EINVAL; |
| 638 | } |
| 639 | |
| 640 | params->pad_ctrl.reg = devm_ioremap_resource(mmc_dev(host->mmc), |
| 641 | &iomem); |
Wei Yongjun | e6e267b | 2017-04-25 06:34:46 +0000 | [diff] [blame^] | 642 | if (IS_ERR(params->pad_ctrl.reg)) |
Hu Ziji | 298269c | 2017-03-30 17:23:01 +0200 | [diff] [blame] | 643 | return PTR_ERR(params->pad_ctrl.reg); |
Hu Ziji | 298269c | 2017-03-30 17:23:01 +0200 | [diff] [blame] | 644 | |
| 645 | ret = of_property_read_string(np, "marvell,pad-type", &name); |
| 646 | if (ret) { |
| 647 | dev_err(mmc_dev(host->mmc), "Unable to determine SoC PHY PAD ctrl type\n"); |
| 648 | return ret; |
| 649 | } |
| 650 | if (!strcmp(name, "sd")) { |
| 651 | params->pad_ctrl.pad_type = SOC_PAD_SD; |
| 652 | } else if (!strcmp(name, "fixed-1-8v")) { |
| 653 | params->pad_ctrl.pad_type = SOC_PAD_FIXED_1_8V; |
| 654 | } else { |
| 655 | dev_err(mmc_dev(host->mmc), "Unsupported SoC PHY PAD ctrl type %s\n", |
| 656 | name); |
| 657 | return -EINVAL; |
| 658 | } |
| 659 | |
| 660 | return ret; |
| 661 | } |
| 662 | |
Hu Ziji | 06c8b66 | 2017-03-30 17:23:00 +0200 | [diff] [blame] | 663 | static int xenon_emmc_phy_parse_param_dt(struct sdhci_host *host, |
| 664 | struct device_node *np, |
| 665 | struct xenon_emmc_phy_params *params) |
| 666 | { |
| 667 | u32 value; |
| 668 | |
| 669 | params->slow_mode = false; |
| 670 | if (of_property_read_bool(np, "marvell,xenon-phy-slow-mode")) |
| 671 | params->slow_mode = true; |
| 672 | |
| 673 | params->znr = XENON_ZNR_DEF_VALUE; |
| 674 | if (!of_property_read_u32(np, "marvell,xenon-phy-znr", &value)) |
| 675 | params->znr = value & XENON_ZNR_MASK; |
| 676 | |
| 677 | params->zpr = XENON_ZPR_DEF_VALUE; |
| 678 | if (!of_property_read_u32(np, "marvell,xenon-phy-zpr", &value)) |
| 679 | params->zpr = value & XENON_ZPR_MASK; |
| 680 | |
| 681 | params->nr_tun_times = XENON_TUN_CONSECUTIVE_TIMES; |
| 682 | if (!of_property_read_u32(np, "marvell,xenon-phy-nr-success-tun", |
| 683 | &value)) |
| 684 | params->nr_tun_times = value & XENON_TUN_CONSECUTIVE_TIMES_MASK; |
| 685 | |
| 686 | params->tun_step_divider = XENON_TUNING_STEP_DIVIDER; |
| 687 | if (!of_property_read_u32(np, "marvell,xenon-phy-tun-step-divider", |
| 688 | &value)) |
| 689 | params->tun_step_divider = value & 0xFF; |
| 690 | |
Hu Ziji | 298269c | 2017-03-30 17:23:01 +0200 | [diff] [blame] | 691 | return get_dt_pad_ctrl_data(host, np, params); |
| 692 | } |
| 693 | |
| 694 | /* Set SoC PHY Voltage PAD */ |
| 695 | void xenon_soc_pad_ctrl(struct sdhci_host *host, |
| 696 | unsigned char signal_voltage) |
| 697 | { |
| 698 | xenon_emmc_phy_set_soc_pad(host, signal_voltage); |
Hu Ziji | 06c8b66 | 2017-03-30 17:23:00 +0200 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | /* |
| 702 | * Setting PHY when card is working in High Speed Mode. |
| 703 | * HS400 set data strobe line. |
| 704 | * HS200/SDR104 set tuning config to prepare for tuning. |
| 705 | */ |
| 706 | static int xenon_hs_delay_adj(struct sdhci_host *host) |
| 707 | { |
| 708 | int ret = 0; |
| 709 | |
| 710 | if (WARN_ON(host->clock <= XENON_DEFAULT_SDCLK_FREQ)) |
| 711 | return -EINVAL; |
| 712 | |
| 713 | switch (host->timing) { |
| 714 | case MMC_TIMING_MMC_HS400: |
| 715 | xenon_emmc_phy_strobe_delay_adj(host); |
| 716 | return 0; |
| 717 | case MMC_TIMING_MMC_HS200: |
| 718 | case MMC_TIMING_UHS_SDR104: |
| 719 | return xenon_emmc_phy_config_tuning(host); |
| 720 | case MMC_TIMING_MMC_DDR52: |
| 721 | case MMC_TIMING_UHS_DDR50: |
| 722 | /* |
| 723 | * DDR Mode requires driver to scan Sampling Fixed Delay Line, |
| 724 | * to find out a perfect operation sampling point. |
| 725 | * It is hard to implement such a scan in host driver |
| 726 | * since initiating commands by host driver is not safe. |
| 727 | * Thus so far just keep PHY Sampling Fixed Delay in |
| 728 | * default value of DDR mode. |
| 729 | * |
| 730 | * If any timing issue occurs in DDR mode on Marvell products, |
| 731 | * please contact maintainer for internal support in Marvell. |
| 732 | */ |
| 733 | dev_warn_once(mmc_dev(host->mmc), "Timing issue might occur in DDR mode\n"); |
| 734 | return 0; |
| 735 | } |
| 736 | |
| 737 | return ret; |
| 738 | } |
| 739 | |
| 740 | /* |
| 741 | * Adjust PHY setting. |
| 742 | * PHY setting should be adjusted when SDCLK frequency, Bus Width |
| 743 | * or Speed Mode is changed. |
| 744 | * Additional config are required when card is working in High Speed mode, |
| 745 | * after leaving Legacy Mode. |
| 746 | */ |
| 747 | int xenon_phy_adj(struct sdhci_host *host, struct mmc_ios *ios) |
| 748 | { |
| 749 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 750 | struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host); |
| 751 | int ret = 0; |
| 752 | |
| 753 | if (!host->clock) { |
| 754 | priv->clock = 0; |
| 755 | return 0; |
| 756 | } |
| 757 | |
| 758 | /* |
| 759 | * The timing, frequency or bus width is changed, |
| 760 | * better to set eMMC PHY based on current setting |
| 761 | * and adjust Xenon SDHC delay. |
| 762 | */ |
| 763 | if ((host->clock == priv->clock) && |
| 764 | (ios->bus_width == priv->bus_width) && |
| 765 | (ios->timing == priv->timing)) |
| 766 | return 0; |
| 767 | |
| 768 | xenon_emmc_phy_set(host, ios->timing); |
| 769 | |
| 770 | /* Update the record */ |
| 771 | priv->bus_width = ios->bus_width; |
| 772 | |
| 773 | priv->timing = ios->timing; |
| 774 | priv->clock = host->clock; |
| 775 | |
| 776 | /* Legacy mode is a special case */ |
| 777 | if (ios->timing == MMC_TIMING_LEGACY) |
| 778 | return 0; |
| 779 | |
| 780 | if (host->clock > XENON_DEFAULT_SDCLK_FREQ) |
| 781 | ret = xenon_hs_delay_adj(host); |
| 782 | return ret; |
| 783 | } |
| 784 | |
| 785 | void xenon_clean_phy(struct sdhci_host *host) |
| 786 | { |
| 787 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 788 | struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host); |
| 789 | |
| 790 | kfree(priv->phy_params); |
| 791 | } |
| 792 | |
| 793 | static int xenon_add_phy(struct device_node *np, struct sdhci_host *host, |
| 794 | const char *phy_name) |
| 795 | { |
| 796 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 797 | struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host); |
| 798 | int i, ret; |
| 799 | |
| 800 | for (i = 0; i < NR_PHY_TYPES; i++) { |
| 801 | if (!strcmp(phy_name, phy_types[i])) { |
| 802 | priv->phy_type = i; |
| 803 | break; |
| 804 | } |
| 805 | } |
| 806 | if (i == NR_PHY_TYPES) { |
| 807 | dev_err(mmc_dev(host->mmc), |
| 808 | "Unable to determine PHY name %s. Use default eMMC 5.1 PHY\n", |
| 809 | phy_name); |
| 810 | priv->phy_type = EMMC_5_1_PHY; |
| 811 | } |
| 812 | |
| 813 | ret = xenon_alloc_emmc_phy(host); |
| 814 | if (ret) |
| 815 | return ret; |
| 816 | |
| 817 | ret = xenon_emmc_phy_parse_param_dt(host, np, priv->phy_params); |
| 818 | if (ret) |
| 819 | xenon_clean_phy(host); |
| 820 | |
| 821 | return ret; |
| 822 | } |
| 823 | |
| 824 | int xenon_phy_parse_dt(struct device_node *np, struct sdhci_host *host) |
| 825 | { |
| 826 | const char *phy_type = NULL; |
| 827 | |
| 828 | if (!of_property_read_string(np, "marvell,xenon-phy-type", &phy_type)) |
| 829 | return xenon_add_phy(np, host, phy_type); |
| 830 | |
| 831 | return xenon_add_phy(np, host, "emmc 5.1 phy"); |
| 832 | } |