Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Marek Vasut | e94cad9 | 2018-04-08 15:22:58 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2018 Marek Vasut <marek.vasut@gmail.com> |
Marek Vasut | e94cad9 | 2018-04-08 15:22:58 +0200 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <clk.h> |
| 8 | #include <fdtdec.h> |
| 9 | #include <mmc.h> |
| 10 | #include <dm.h> |
| 11 | #include <linux/compat.h> |
| 12 | #include <linux/dma-direction.h> |
| 13 | #include <linux/io.h> |
| 14 | #include <linux/sizes.h> |
| 15 | #include <power/regulator.h> |
| 16 | #include <asm/unaligned.h> |
| 17 | |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 18 | #include "tmio-common.h" |
Marek Vasut | e94cad9 | 2018-04-08 15:22:58 +0200 | [diff] [blame] | 19 | |
Marek Vasut | 50aa1d9 | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 20 | #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \ |
| 21 | CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \ |
| 22 | CONFIG_IS_ENABLED(MMC_HS400_SUPPORT) |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 23 | |
| 24 | /* SCC registers */ |
| 25 | #define RENESAS_SDHI_SCC_DTCNTL 0x800 |
Marek Vasut | 1bac2b6 | 2019-05-19 02:33:06 +0200 | [diff] [blame] | 26 | #define RENESAS_SDHI_SCC_DTCNTL_TAPEN BIT(0) |
| 27 | #define RENESAS_SDHI_SCC_DTCNTL_TAPNUM_SHIFT 16 |
| 28 | #define RENESAS_SDHI_SCC_DTCNTL_TAPNUM_MASK 0xff |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 29 | #define RENESAS_SDHI_SCC_TAPSET 0x804 |
| 30 | #define RENESAS_SDHI_SCC_DT2FF 0x808 |
| 31 | #define RENESAS_SDHI_SCC_CKSEL 0x80c |
Marek Vasut | 1bac2b6 | 2019-05-19 02:33:06 +0200 | [diff] [blame] | 32 | #define RENESAS_SDHI_SCC_CKSEL_DTSEL BIT(0) |
| 33 | #define RENESAS_SDHI_SCC_RVSCNTL 0x810 |
| 34 | #define RENESAS_SDHI_SCC_RVSCNTL_RVSEN BIT(0) |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 35 | #define RENESAS_SDHI_SCC_RVSREQ 0x814 |
Marek Vasut | 1bac2b6 | 2019-05-19 02:33:06 +0200 | [diff] [blame] | 36 | #define RENESAS_SDHI_SCC_RVSREQ_RVSERR BIT(2) |
Marek Vasut | 6900066 | 2019-11-23 13:36:23 +0100 | [diff] [blame^] | 37 | #define RENESAS_SDHI_SCC_RVSREQ_REQTAPUP BIT(1) |
| 38 | #define RENESAS_SDHI_SCC_RVSREQ_REQTAPDOWN BIT(0) |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 39 | #define RENESAS_SDHI_SCC_SMPCMP 0x818 |
Marek Vasut | 6900066 | 2019-11-23 13:36:23 +0100 | [diff] [blame^] | 40 | #define RENESAS_SDHI_SCC_SMPCMP_CMD_ERR (BIT(24) | BIT(8)) |
| 41 | #define RENESAS_SDHI_SCC_SMPCMP_CMD_REQUP BIT(24) |
| 42 | #define RENESAS_SDHI_SCC_SMPCMP_CMD_REQDOWN BIT(8) |
Marek Vasut | 1bac2b6 | 2019-05-19 02:33:06 +0200 | [diff] [blame] | 43 | #define RENESAS_SDHI_SCC_TMPPORT2 0x81c |
| 44 | #define RENESAS_SDHI_SCC_TMPPORT2_HS400EN BIT(31) |
| 45 | #define RENESAS_SDHI_SCC_TMPPORT2_HS400OSEL BIT(4) |
Marek Vasut | b5900a5 | 2019-05-19 03:47:07 +0200 | [diff] [blame] | 46 | #define RENESAS_SDHI_SCC_TMPPORT3 0x828 |
| 47 | #define RENESAS_SDHI_SCC_TMPPORT3_OFFSET_0 3 |
| 48 | #define RENESAS_SDHI_SCC_TMPPORT3_OFFSET_1 2 |
| 49 | #define RENESAS_SDHI_SCC_TMPPORT3_OFFSET_2 1 |
| 50 | #define RENESAS_SDHI_SCC_TMPPORT3_OFFSET_3 0 |
| 51 | #define RENESAS_SDHI_SCC_TMPPORT3_OFFSET_MASK 0x3 |
| 52 | #define RENESAS_SDHI_SCC_TMPPORT4 0x82c |
| 53 | #define RENESAS_SDHI_SCC_TMPPORT4_DLL_ACC_START BIT(0) |
| 54 | #define RENESAS_SDHI_SCC_TMPPORT5 0x830 |
| 55 | #define RENESAS_SDHI_SCC_TMPPORT5_DLL_RW_SEL_R BIT(8) |
| 56 | #define RENESAS_SDHI_SCC_TMPPORT5_DLL_RW_SEL_W (0 << 8) |
| 57 | #define RENESAS_SDHI_SCC_TMPPORT5_DLL_ADR_MASK 0x3F |
| 58 | #define RENESAS_SDHI_SCC_TMPPORT6 0x834 |
| 59 | #define RENESAS_SDHI_SCC_TMPPORT7 0x838 |
| 60 | #define RENESAS_SDHI_SCC_TMPPORT_DISABLE_WP_CODE 0xa5000000 |
| 61 | #define RENESAS_SDHI_SCC_TMPPORT_CALIB_CODE_MASK 0x1f |
| 62 | #define RENESAS_SDHI_SCC_TMPPORT_MANUAL_MODE BIT(7) |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 63 | |
| 64 | #define RENESAS_SDHI_MAX_TAP 3 |
| 65 | |
Marek Vasut | b5900a5 | 2019-05-19 03:47:07 +0200 | [diff] [blame] | 66 | static u32 sd_scc_tmpport_read32(struct tmio_sd_priv *priv, u32 addr) |
| 67 | { |
| 68 | /* read mode */ |
| 69 | tmio_sd_writel(priv, RENESAS_SDHI_SCC_TMPPORT5_DLL_RW_SEL_R | |
| 70 | (RENESAS_SDHI_SCC_TMPPORT5_DLL_ADR_MASK & addr), |
| 71 | RENESAS_SDHI_SCC_TMPPORT5); |
| 72 | |
| 73 | /* access start and stop */ |
| 74 | tmio_sd_writel(priv, RENESAS_SDHI_SCC_TMPPORT4_DLL_ACC_START, |
| 75 | RENESAS_SDHI_SCC_TMPPORT4); |
| 76 | tmio_sd_writel(priv, 0, RENESAS_SDHI_SCC_TMPPORT4); |
| 77 | |
| 78 | return tmio_sd_readl(priv, RENESAS_SDHI_SCC_TMPPORT7); |
| 79 | } |
| 80 | |
| 81 | static void sd_scc_tmpport_write32(struct tmio_sd_priv *priv, u32 addr, u32 val) |
| 82 | { |
| 83 | /* write mode */ |
| 84 | tmio_sd_writel(priv, RENESAS_SDHI_SCC_TMPPORT5_DLL_RW_SEL_W | |
| 85 | (RENESAS_SDHI_SCC_TMPPORT5_DLL_ADR_MASK & addr), |
| 86 | RENESAS_SDHI_SCC_TMPPORT5); |
| 87 | tmio_sd_writel(priv, val, RENESAS_SDHI_SCC_TMPPORT6); |
| 88 | |
| 89 | /* access start and stop */ |
| 90 | tmio_sd_writel(priv, RENESAS_SDHI_SCC_TMPPORT4_DLL_ACC_START, |
| 91 | RENESAS_SDHI_SCC_TMPPORT4); |
| 92 | tmio_sd_writel(priv, 0, RENESAS_SDHI_SCC_TMPPORT4); |
| 93 | } |
| 94 | |
Marek Vasut | 6900066 | 2019-11-23 13:36:23 +0100 | [diff] [blame^] | 95 | static bool renesas_sdhi_check_scc_error(struct udevice *dev) |
| 96 | { |
| 97 | struct tmio_sd_priv *priv = dev_get_priv(dev); |
| 98 | struct mmc *mmc = mmc_get_mmc_dev(dev); |
| 99 | unsigned long new_tap = priv->tap_set; |
| 100 | u32 reg, smpcmp; |
| 101 | |
| 102 | if ((priv->caps & TMIO_SD_CAP_RCAR_UHS) && |
| 103 | (mmc->selected_mode != UHS_SDR104) && |
| 104 | (mmc->selected_mode != MMC_HS_200) && |
| 105 | (mmc->selected_mode != MMC_HS_400) && |
| 106 | (priv->nrtaps != 4)) |
| 107 | return false; |
| 108 | |
| 109 | reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSCNTL); |
| 110 | /* Handle automatic tuning correction */ |
| 111 | if (reg & RENESAS_SDHI_SCC_RVSCNTL_RVSEN) { |
| 112 | reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSREQ); |
| 113 | if (reg & RENESAS_SDHI_SCC_RVSREQ_RVSERR) { |
| 114 | tmio_sd_writel(priv, 0, RENESAS_SDHI_SCC_RVSREQ); |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | /* Handle manual tuning correction */ |
| 122 | reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSREQ); |
| 123 | if (!reg) /* No error */ |
| 124 | return false; |
| 125 | |
| 126 | tmio_sd_writel(priv, 0, RENESAS_SDHI_SCC_RVSREQ); |
| 127 | |
| 128 | if (mmc->selected_mode == MMC_HS_400) { |
| 129 | /* |
| 130 | * Correction Error Status contains CMD and DAT signal status. |
| 131 | * In HS400, DAT signal based on DS signal, not CLK. |
| 132 | * Therefore, use only CMD status. |
| 133 | */ |
| 134 | smpcmp = tmio_sd_readl(priv, RENESAS_SDHI_SCC_SMPCMP) & |
| 135 | RENESAS_SDHI_SCC_SMPCMP_CMD_ERR; |
| 136 | |
| 137 | switch (smpcmp) { |
| 138 | case 0: |
| 139 | return false; /* No error in CMD signal */ |
| 140 | case RENESAS_SDHI_SCC_SMPCMP_CMD_REQUP: |
| 141 | new_tap = (priv->tap_set + |
| 142 | priv->tap_num + 1) % priv->tap_num; |
| 143 | break; |
| 144 | case RENESAS_SDHI_SCC_SMPCMP_CMD_REQDOWN: |
| 145 | new_tap = (priv->tap_set + |
| 146 | priv->tap_num - 1) % priv->tap_num; |
| 147 | break; |
| 148 | default: |
| 149 | return true; /* Need re-tune */ |
| 150 | } |
| 151 | |
| 152 | priv->tap_set = new_tap; |
| 153 | } else { |
| 154 | if (reg & RENESAS_SDHI_SCC_RVSREQ_RVSERR) |
| 155 | return true; /* Need re-tune */ |
| 156 | else if (reg & RENESAS_SDHI_SCC_RVSREQ_REQTAPUP) |
| 157 | priv->tap_set = (priv->tap_set + |
| 158 | priv->tap_num + 1) % priv->tap_num; |
| 159 | else if (reg & RENESAS_SDHI_SCC_RVSREQ_REQTAPDOWN) |
| 160 | priv->tap_set = (priv->tap_set + |
| 161 | priv->tap_num - 1) % priv->tap_num; |
| 162 | else |
| 163 | return false; |
| 164 | } |
| 165 | |
| 166 | /* Set TAP position */ |
| 167 | tmio_sd_writel(priv, priv->tap_set >> ((priv->nrtaps == 4) ? 1 : 0), |
| 168 | RENESAS_SDHI_SCC_TAPSET); |
| 169 | |
| 170 | return false; |
| 171 | } |
| 172 | |
Marek Vasut | b5900a5 | 2019-05-19 03:47:07 +0200 | [diff] [blame] | 173 | static void renesas_sdhi_adjust_hs400_mode_enable(struct tmio_sd_priv *priv) |
| 174 | { |
| 175 | u32 calib_code; |
| 176 | |
| 177 | if (!priv->adjust_hs400_enable) |
| 178 | return; |
| 179 | |
| 180 | if (!priv->needs_adjust_hs400) |
| 181 | return; |
| 182 | |
| 183 | /* |
| 184 | * Enabled Manual adjust HS400 mode |
| 185 | * |
| 186 | * 1) Disabled Write Protect |
| 187 | * W(addr=0x00, WP_DISABLE_CODE) |
| 188 | * 2) Read Calibration code and adjust |
| 189 | * R(addr=0x26) - adjust value |
| 190 | * 3) Enabled Manual Calibration |
| 191 | * W(addr=0x22, manual mode | Calibration code) |
| 192 | * 4) Set Offset value to TMPPORT3 Reg |
| 193 | */ |
| 194 | sd_scc_tmpport_write32(priv, 0x00, |
| 195 | RENESAS_SDHI_SCC_TMPPORT_DISABLE_WP_CODE); |
| 196 | calib_code = sd_scc_tmpport_read32(priv, 0x26); |
| 197 | calib_code &= RENESAS_SDHI_SCC_TMPPORT_CALIB_CODE_MASK; |
| 198 | if (calib_code > priv->adjust_hs400_calibrate) |
| 199 | calib_code -= priv->adjust_hs400_calibrate; |
| 200 | else |
| 201 | calib_code = 0; |
| 202 | sd_scc_tmpport_write32(priv, 0x22, |
| 203 | RENESAS_SDHI_SCC_TMPPORT_MANUAL_MODE | |
| 204 | calib_code); |
| 205 | tmio_sd_writel(priv, priv->adjust_hs400_offset, |
| 206 | RENESAS_SDHI_SCC_TMPPORT3); |
| 207 | |
| 208 | /* Clear flag */ |
| 209 | priv->needs_adjust_hs400 = false; |
| 210 | } |
| 211 | |
| 212 | static void renesas_sdhi_adjust_hs400_mode_disable(struct tmio_sd_priv *priv) |
| 213 | { |
| 214 | |
| 215 | /* Disabled Manual adjust HS400 mode |
| 216 | * |
| 217 | * 1) Disabled Write Protect |
| 218 | * W(addr=0x00, WP_DISABLE_CODE) |
| 219 | * 2) Disabled Manual Calibration |
| 220 | * W(addr=0x22, 0) |
| 221 | * 3) Clear offset value to TMPPORT3 Reg |
| 222 | */ |
| 223 | sd_scc_tmpport_write32(priv, 0x00, |
| 224 | RENESAS_SDHI_SCC_TMPPORT_DISABLE_WP_CODE); |
| 225 | sd_scc_tmpport_write32(priv, 0x22, 0); |
| 226 | tmio_sd_writel(priv, 0, RENESAS_SDHI_SCC_TMPPORT3); |
| 227 | } |
| 228 | |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 229 | static unsigned int renesas_sdhi_init_tuning(struct tmio_sd_priv *priv) |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 230 | { |
| 231 | u32 reg; |
| 232 | |
| 233 | /* Initialize SCC */ |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 234 | tmio_sd_writel(priv, 0, TMIO_SD_INFO1); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 235 | |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 236 | reg = tmio_sd_readl(priv, TMIO_SD_CLKCTL); |
| 237 | reg &= ~TMIO_SD_CLKCTL_SCLKEN; |
| 238 | tmio_sd_writel(priv, reg, TMIO_SD_CLKCTL); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 239 | |
| 240 | /* Set sampling clock selection range */ |
Marek Vasut | a376dde | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 241 | tmio_sd_writel(priv, (0x8 << RENESAS_SDHI_SCC_DTCNTL_TAPNUM_SHIFT) | |
| 242 | RENESAS_SDHI_SCC_DTCNTL_TAPEN, |
| 243 | RENESAS_SDHI_SCC_DTCNTL); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 244 | |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 245 | reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_CKSEL); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 246 | reg |= RENESAS_SDHI_SCC_CKSEL_DTSEL; |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 247 | tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_CKSEL); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 248 | |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 249 | reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSCNTL); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 250 | reg &= ~RENESAS_SDHI_SCC_RVSCNTL_RVSEN; |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 251 | tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_RVSCNTL); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 252 | |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 253 | tmio_sd_writel(priv, 0x300 /* scc_tappos */, |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 254 | RENESAS_SDHI_SCC_DT2FF); |
| 255 | |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 256 | reg = tmio_sd_readl(priv, TMIO_SD_CLKCTL); |
| 257 | reg |= TMIO_SD_CLKCTL_SCLKEN; |
| 258 | tmio_sd_writel(priv, reg, TMIO_SD_CLKCTL); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 259 | |
| 260 | /* Read TAPNUM */ |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 261 | return (tmio_sd_readl(priv, RENESAS_SDHI_SCC_DTCNTL) >> |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 262 | RENESAS_SDHI_SCC_DTCNTL_TAPNUM_SHIFT) & |
| 263 | RENESAS_SDHI_SCC_DTCNTL_TAPNUM_MASK; |
| 264 | } |
| 265 | |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 266 | static void renesas_sdhi_reset_tuning(struct tmio_sd_priv *priv) |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 267 | { |
| 268 | u32 reg; |
| 269 | |
| 270 | /* Reset SCC */ |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 271 | reg = tmio_sd_readl(priv, TMIO_SD_CLKCTL); |
| 272 | reg &= ~TMIO_SD_CLKCTL_SCLKEN; |
| 273 | tmio_sd_writel(priv, reg, TMIO_SD_CLKCTL); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 274 | |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 275 | reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_CKSEL); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 276 | reg &= ~RENESAS_SDHI_SCC_CKSEL_DTSEL; |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 277 | tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_CKSEL); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 278 | |
Marek Vasut | dc1488f | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 279 | reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_TMPPORT2); |
| 280 | reg &= ~(RENESAS_SDHI_SCC_TMPPORT2_HS400EN | |
| 281 | RENESAS_SDHI_SCC_TMPPORT2_HS400OSEL); |
| 282 | tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_TMPPORT2); |
| 283 | |
Marek Vasut | b5900a5 | 2019-05-19 03:47:07 +0200 | [diff] [blame] | 284 | /* Disable HS400 mode adjustment */ |
| 285 | renesas_sdhi_adjust_hs400_mode_disable(priv); |
| 286 | |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 287 | reg = tmio_sd_readl(priv, TMIO_SD_CLKCTL); |
| 288 | reg |= TMIO_SD_CLKCTL_SCLKEN; |
| 289 | tmio_sd_writel(priv, reg, TMIO_SD_CLKCTL); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 290 | |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 291 | reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSCNTL); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 292 | reg &= ~RENESAS_SDHI_SCC_RVSCNTL_RVSEN; |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 293 | tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_RVSCNTL); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 294 | |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 295 | reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSCNTL); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 296 | reg &= ~RENESAS_SDHI_SCC_RVSCNTL_RVSEN; |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 297 | tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_RVSCNTL); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 298 | } |
| 299 | |
Marek Vasut | 50aa1d9 | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 300 | static int renesas_sdhi_hs400(struct udevice *dev) |
| 301 | { |
| 302 | struct tmio_sd_priv *priv = dev_get_priv(dev); |
| 303 | struct mmc *mmc = mmc_get_mmc_dev(dev); |
| 304 | bool hs400 = (mmc->selected_mode == MMC_HS_400); |
| 305 | int ret, taps = hs400 ? priv->nrtaps : 8; |
| 306 | u32 reg; |
| 307 | |
| 308 | if (taps == 4) /* HS400 on 4tap SoC needs different clock */ |
| 309 | ret = clk_set_rate(&priv->clk, 400000000); |
| 310 | else |
| 311 | ret = clk_set_rate(&priv->clk, 200000000); |
| 312 | if (ret < 0) |
| 313 | return ret; |
| 314 | |
Marek Vasut | 8f39b03 | 2019-11-23 13:36:22 +0100 | [diff] [blame] | 315 | reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSCNTL); |
| 316 | reg &= ~RENESAS_SDHI_SCC_RVSCNTL_RVSEN; |
| 317 | tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_RVSCNTL); |
Marek Vasut | 50aa1d9 | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 318 | |
| 319 | reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_TMPPORT2); |
| 320 | if (hs400) { |
| 321 | reg |= RENESAS_SDHI_SCC_TMPPORT2_HS400EN | |
| 322 | RENESAS_SDHI_SCC_TMPPORT2_HS400OSEL; |
| 323 | } else { |
| 324 | reg &= ~(RENESAS_SDHI_SCC_TMPPORT2_HS400EN | |
| 325 | RENESAS_SDHI_SCC_TMPPORT2_HS400OSEL); |
| 326 | } |
| 327 | |
| 328 | tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_TMPPORT2); |
| 329 | |
Marek Vasut | b5900a5 | 2019-05-19 03:47:07 +0200 | [diff] [blame] | 330 | /* Disable HS400 mode adjustment */ |
| 331 | if (!hs400) |
| 332 | renesas_sdhi_adjust_hs400_mode_disable(priv); |
| 333 | |
Marek Vasut | ba41c45 | 2019-02-19 19:32:28 +0100 | [diff] [blame] | 334 | tmio_sd_writel(priv, (0x8 << RENESAS_SDHI_SCC_DTCNTL_TAPNUM_SHIFT) | |
Marek Vasut | 50aa1d9 | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 335 | RENESAS_SDHI_SCC_DTCNTL_TAPEN, |
| 336 | RENESAS_SDHI_SCC_DTCNTL); |
| 337 | |
| 338 | if (taps == 4) { |
| 339 | tmio_sd_writel(priv, priv->tap_set >> 1, |
| 340 | RENESAS_SDHI_SCC_TAPSET); |
Marek Vasut | dc419fc | 2019-11-23 13:36:20 +0100 | [diff] [blame] | 341 | tmio_sd_writel(priv, hs400 ? 0x100 : 0x300, |
| 342 | RENESAS_SDHI_SCC_DT2FF); |
Marek Vasut | 50aa1d9 | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 343 | } else { |
| 344 | tmio_sd_writel(priv, priv->tap_set, RENESAS_SDHI_SCC_TAPSET); |
Marek Vasut | dc419fc | 2019-11-23 13:36:20 +0100 | [diff] [blame] | 345 | tmio_sd_writel(priv, 0x300, RENESAS_SDHI_SCC_DT2FF); |
Marek Vasut | 50aa1d9 | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_CKSEL); |
| 349 | reg |= RENESAS_SDHI_SCC_CKSEL_DTSEL; |
| 350 | tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_CKSEL); |
| 351 | |
Marek Vasut | b5900a5 | 2019-05-19 03:47:07 +0200 | [diff] [blame] | 352 | /* Execute adjust hs400 offset after setting to HS400 mode */ |
| 353 | if (hs400) |
| 354 | priv->needs_adjust_hs400 = true; |
| 355 | |
Marek Vasut | 50aa1d9 | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 356 | return 0; |
| 357 | } |
| 358 | |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 359 | static void renesas_sdhi_prepare_tuning(struct tmio_sd_priv *priv, |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 360 | unsigned long tap) |
| 361 | { |
| 362 | /* Set sampling clock position */ |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 363 | tmio_sd_writel(priv, tap, RENESAS_SDHI_SCC_TAPSET); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 364 | } |
| 365 | |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 366 | static unsigned int renesas_sdhi_compare_scc_data(struct tmio_sd_priv *priv) |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 367 | { |
| 368 | /* Get comparison of sampling data */ |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 369 | return tmio_sd_readl(priv, RENESAS_SDHI_SCC_SMPCMP); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 370 | } |
| 371 | |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 372 | static int renesas_sdhi_select_tuning(struct tmio_sd_priv *priv, |
Marek Vasut | 37c3990 | 2019-11-23 13:36:18 +0100 | [diff] [blame] | 373 | unsigned int taps) |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 374 | { |
| 375 | unsigned long tap_cnt; /* counter of tuning success */ |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 376 | unsigned long tap_start;/* start position of tuning success */ |
| 377 | unsigned long tap_end; /* end position of tuning success */ |
| 378 | unsigned long ntap; /* temporary counter of tuning success */ |
| 379 | unsigned long match_cnt;/* counter of matching data */ |
| 380 | unsigned long i; |
| 381 | bool select = false; |
| 382 | u32 reg; |
| 383 | |
Marek Vasut | b5900a5 | 2019-05-19 03:47:07 +0200 | [diff] [blame] | 384 | priv->needs_adjust_hs400 = false; |
| 385 | |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 386 | /* Clear SCC_RVSREQ */ |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 387 | tmio_sd_writel(priv, 0, RENESAS_SDHI_SCC_RVSREQ); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 388 | |
| 389 | /* Merge the results */ |
Marek Vasut | 0196a58 | 2019-11-23 13:36:17 +0100 | [diff] [blame] | 390 | for (i = 0; i < priv->tap_num * 2; i++) { |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 391 | if (!(taps & BIT(i))) { |
Marek Vasut | 0196a58 | 2019-11-23 13:36:17 +0100 | [diff] [blame] | 392 | taps &= ~BIT(i % priv->tap_num); |
| 393 | taps &= ~BIT((i % priv->tap_num) + priv->tap_num); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 394 | } |
Marek Vasut | 37c3990 | 2019-11-23 13:36:18 +0100 | [diff] [blame] | 395 | if (!(priv->smpcmp & BIT(i))) { |
| 396 | priv->smpcmp &= ~BIT(i % priv->tap_num); |
| 397 | priv->smpcmp &= ~BIT((i % priv->tap_num) + priv->tap_num); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | |
| 401 | /* |
| 402 | * Find the longest consecutive run of successful probes. If that |
| 403 | * is more than RENESAS_SDHI_MAX_TAP probes long then use the |
| 404 | * center index as the tap. |
| 405 | */ |
| 406 | tap_cnt = 0; |
| 407 | ntap = 0; |
| 408 | tap_start = 0; |
| 409 | tap_end = 0; |
Marek Vasut | 0196a58 | 2019-11-23 13:36:17 +0100 | [diff] [blame] | 410 | for (i = 0; i < priv->tap_num * 2; i++) { |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 411 | if (taps & BIT(i)) |
| 412 | ntap++; |
| 413 | else { |
| 414 | if (ntap > tap_cnt) { |
| 415 | tap_start = i - ntap; |
| 416 | tap_end = i - 1; |
| 417 | tap_cnt = ntap; |
| 418 | } |
| 419 | ntap = 0; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | if (ntap > tap_cnt) { |
| 424 | tap_start = i - ntap; |
| 425 | tap_end = i - 1; |
| 426 | tap_cnt = ntap; |
| 427 | } |
| 428 | |
| 429 | /* |
| 430 | * If all of the TAP is OK, the sampling clock position is selected by |
| 431 | * identifying the change point of data. |
| 432 | */ |
Marek Vasut | 0196a58 | 2019-11-23 13:36:17 +0100 | [diff] [blame] | 433 | if (tap_cnt == priv->tap_num * 2) { |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 434 | match_cnt = 0; |
| 435 | ntap = 0; |
| 436 | tap_start = 0; |
| 437 | tap_end = 0; |
Marek Vasut | 0196a58 | 2019-11-23 13:36:17 +0100 | [diff] [blame] | 438 | for (i = 0; i < priv->tap_num * 2; i++) { |
Marek Vasut | 37c3990 | 2019-11-23 13:36:18 +0100 | [diff] [blame] | 439 | if (priv->smpcmp & BIT(i)) |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 440 | ntap++; |
| 441 | else { |
| 442 | if (ntap > match_cnt) { |
| 443 | tap_start = i - ntap; |
| 444 | tap_end = i - 1; |
| 445 | match_cnt = ntap; |
| 446 | } |
| 447 | ntap = 0; |
| 448 | } |
| 449 | } |
| 450 | if (ntap > match_cnt) { |
| 451 | tap_start = i - ntap; |
| 452 | tap_end = i - 1; |
| 453 | match_cnt = ntap; |
| 454 | } |
| 455 | if (match_cnt) |
| 456 | select = true; |
| 457 | } else if (tap_cnt >= RENESAS_SDHI_MAX_TAP) |
| 458 | select = true; |
| 459 | |
| 460 | if (select) |
Marek Vasut | 0196a58 | 2019-11-23 13:36:17 +0100 | [diff] [blame] | 461 | priv->tap_set = ((tap_start + tap_end) / 2) % priv->tap_num; |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 462 | else |
| 463 | return -EIO; |
| 464 | |
| 465 | /* Set SCC */ |
Marek Vasut | 95ead3d | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 466 | tmio_sd_writel(priv, priv->tap_set, RENESAS_SDHI_SCC_TAPSET); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 467 | |
| 468 | /* Enable auto re-tuning */ |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 469 | reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSCNTL); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 470 | reg |= RENESAS_SDHI_SCC_RVSCNTL_RVSEN; |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 471 | tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_RVSCNTL); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 472 | |
| 473 | return 0; |
| 474 | } |
| 475 | |
| 476 | int renesas_sdhi_execute_tuning(struct udevice *dev, uint opcode) |
| 477 | { |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 478 | struct tmio_sd_priv *priv = dev_get_priv(dev); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 479 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
| 480 | struct mmc *mmc = upriv->mmc; |
| 481 | unsigned int tap_num; |
Marek Vasut | 37c3990 | 2019-11-23 13:36:18 +0100 | [diff] [blame] | 482 | unsigned int taps = 0; |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 483 | int i, ret = 0; |
| 484 | u32 caps; |
| 485 | |
| 486 | /* Only supported on Renesas RCar */ |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 487 | if (!(priv->caps & TMIO_SD_CAP_RCAR_UHS)) |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 488 | return -EINVAL; |
| 489 | |
| 490 | /* clock tuning is not needed for upto 52MHz */ |
| 491 | if (!((mmc->selected_mode == MMC_HS_200) || |
Marek Vasut | 50aa1d9 | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 492 | (mmc->selected_mode == MMC_HS_400) || |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 493 | (mmc->selected_mode == UHS_SDR104) || |
| 494 | (mmc->selected_mode == UHS_SDR50))) |
| 495 | return 0; |
| 496 | |
| 497 | tap_num = renesas_sdhi_init_tuning(priv); |
| 498 | if (!tap_num) |
| 499 | /* Tuning is not supported */ |
| 500 | goto out; |
| 501 | |
Marek Vasut | 0196a58 | 2019-11-23 13:36:17 +0100 | [diff] [blame] | 502 | priv->tap_num = tap_num; |
| 503 | |
| 504 | if (priv->tap_num * 2 >= sizeof(taps) * 8) { |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 505 | dev_err(dev, |
| 506 | "Too many taps, skipping tuning. Please consider updating size of taps field of tmio_mmc_host\n"); |
| 507 | goto out; |
| 508 | } |
| 509 | |
Marek Vasut | 37c3990 | 2019-11-23 13:36:18 +0100 | [diff] [blame] | 510 | priv->smpcmp = 0; |
| 511 | |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 512 | /* Issue CMD19 twice for each tap */ |
Marek Vasut | 0196a58 | 2019-11-23 13:36:17 +0100 | [diff] [blame] | 513 | for (i = 0; i < 2 * priv->tap_num; i++) { |
| 514 | renesas_sdhi_prepare_tuning(priv, i % priv->tap_num); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 515 | |
| 516 | /* Force PIO for the tuning */ |
| 517 | caps = priv->caps; |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 518 | priv->caps &= ~TMIO_SD_CAP_DMA_INTERNAL; |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 519 | |
| 520 | ret = mmc_send_tuning(mmc, opcode, NULL); |
| 521 | |
| 522 | priv->caps = caps; |
| 523 | |
| 524 | if (ret == 0) |
| 525 | taps |= BIT(i); |
| 526 | |
| 527 | ret = renesas_sdhi_compare_scc_data(priv); |
| 528 | if (ret == 0) |
Marek Vasut | 37c3990 | 2019-11-23 13:36:18 +0100 | [diff] [blame] | 529 | priv->smpcmp |= BIT(i); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 530 | |
| 531 | mdelay(1); |
| 532 | } |
| 533 | |
Marek Vasut | 37c3990 | 2019-11-23 13:36:18 +0100 | [diff] [blame] | 534 | ret = renesas_sdhi_select_tuning(priv, taps); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 535 | |
| 536 | out: |
| 537 | if (ret < 0) { |
| 538 | dev_warn(dev, "Tuning procedure failed\n"); |
| 539 | renesas_sdhi_reset_tuning(priv); |
| 540 | } |
| 541 | |
| 542 | return ret; |
| 543 | } |
Marek Vasut | 50aa1d9 | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 544 | #else |
| 545 | static int renesas_sdhi_hs400(struct udevice *dev) |
| 546 | { |
| 547 | return 0; |
| 548 | } |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 549 | #endif |
| 550 | |
| 551 | static int renesas_sdhi_set_ios(struct udevice *dev) |
| 552 | { |
Marek Vasut | 50aa1d9 | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 553 | struct tmio_sd_priv *priv = dev_get_priv(dev); |
| 554 | u32 tmp; |
| 555 | int ret; |
| 556 | |
| 557 | /* Stop the clock before changing its rate to avoid a glitch signal */ |
| 558 | tmp = tmio_sd_readl(priv, TMIO_SD_CLKCTL); |
| 559 | tmp &= ~TMIO_SD_CLKCTL_SCLKEN; |
| 560 | tmio_sd_writel(priv, tmp, TMIO_SD_CLKCTL); |
| 561 | |
| 562 | ret = renesas_sdhi_hs400(dev); |
| 563 | if (ret) |
| 564 | return ret; |
| 565 | |
| 566 | ret = tmio_sd_set_ios(dev); |
Marek Vasut | cf39f3f | 2018-04-09 20:47:31 +0200 | [diff] [blame] | 567 | |
| 568 | mdelay(10); |
| 569 | |
Marek Vasut | 50aa1d9 | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 570 | #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \ |
| 571 | CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \ |
| 572 | CONFIG_IS_ENABLED(MMC_HS400_SUPPORT) |
| 573 | struct mmc *mmc = mmc_get_mmc_dev(dev); |
| 574 | if ((priv->caps & TMIO_SD_CAP_RCAR_UHS) && |
| 575 | (mmc->selected_mode != UHS_SDR104) && |
| 576 | (mmc->selected_mode != MMC_HS_200) && |
| 577 | (mmc->selected_mode != MMC_HS_400)) { |
Marek Vasut | 52e1796 | 2018-10-28 15:30:06 +0100 | [diff] [blame] | 578 | renesas_sdhi_reset_tuning(priv); |
Marek Vasut | 50aa1d9 | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 579 | } |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 580 | #endif |
| 581 | |
| 582 | return ret; |
| 583 | } |
| 584 | |
Marek Vasut | 2fc1075 | 2018-10-28 19:28:56 +0100 | [diff] [blame] | 585 | #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) |
Sam Protsenko | 6cf8a90 | 2019-08-14 22:52:51 +0300 | [diff] [blame] | 586 | static int renesas_sdhi_wait_dat0(struct udevice *dev, int state, |
| 587 | int timeout_us) |
Marek Vasut | 2fc1075 | 2018-10-28 19:28:56 +0100 | [diff] [blame] | 588 | { |
| 589 | int ret = -ETIMEDOUT; |
| 590 | bool dat0_high; |
| 591 | bool target_dat0_high = !!state; |
| 592 | struct tmio_sd_priv *priv = dev_get_priv(dev); |
| 593 | |
Sam Protsenko | 6cf8a90 | 2019-08-14 22:52:51 +0300 | [diff] [blame] | 594 | timeout_us = DIV_ROUND_UP(timeout_us, 10); /* check every 10 us. */ |
| 595 | while (timeout_us--) { |
Marek Vasut | 2fc1075 | 2018-10-28 19:28:56 +0100 | [diff] [blame] | 596 | dat0_high = !!(tmio_sd_readl(priv, TMIO_SD_INFO2) & TMIO_SD_INFO2_DAT0); |
| 597 | if (dat0_high == target_dat0_high) { |
| 598 | ret = 0; |
| 599 | break; |
| 600 | } |
| 601 | udelay(10); |
| 602 | } |
| 603 | |
| 604 | return ret; |
| 605 | } |
| 606 | #endif |
| 607 | |
Marek Vasut | b5900a5 | 2019-05-19 03:47:07 +0200 | [diff] [blame] | 608 | static int renesas_sdhi_send_cmd(struct udevice *dev, struct mmc_cmd *cmd, |
| 609 | struct mmc_data *data) |
| 610 | { |
| 611 | int ret; |
| 612 | |
| 613 | ret = tmio_sd_send_cmd(dev, cmd, data); |
| 614 | if (ret) |
| 615 | return ret; |
| 616 | |
| 617 | #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \ |
| 618 | CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \ |
| 619 | CONFIG_IS_ENABLED(MMC_HS400_SUPPORT) |
| 620 | struct tmio_sd_priv *priv = dev_get_priv(dev); |
| 621 | |
Marek Vasut | 6900066 | 2019-11-23 13:36:23 +0100 | [diff] [blame^] | 622 | renesas_sdhi_check_scc_error(dev); |
| 623 | |
Marek Vasut | b5900a5 | 2019-05-19 03:47:07 +0200 | [diff] [blame] | 624 | if (cmd->cmdidx == MMC_CMD_SEND_STATUS) |
| 625 | renesas_sdhi_adjust_hs400_mode_enable(priv); |
| 626 | #endif |
| 627 | |
| 628 | return 0; |
| 629 | } |
| 630 | |
Marek Vasut | e94cad9 | 2018-04-08 15:22:58 +0200 | [diff] [blame] | 631 | static const struct dm_mmc_ops renesas_sdhi_ops = { |
Marek Vasut | b5900a5 | 2019-05-19 03:47:07 +0200 | [diff] [blame] | 632 | .send_cmd = renesas_sdhi_send_cmd, |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 633 | .set_ios = renesas_sdhi_set_ios, |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 634 | .get_cd = tmio_sd_get_cd, |
Marek Vasut | 50aa1d9 | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 635 | #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \ |
| 636 | CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \ |
| 637 | CONFIG_IS_ENABLED(MMC_HS400_SUPPORT) |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 638 | .execute_tuning = renesas_sdhi_execute_tuning, |
| 639 | #endif |
Marek Vasut | 2fc1075 | 2018-10-28 19:28:56 +0100 | [diff] [blame] | 640 | #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) |
| 641 | .wait_dat0 = renesas_sdhi_wait_dat0, |
| 642 | #endif |
Marek Vasut | e94cad9 | 2018-04-08 15:22:58 +0200 | [diff] [blame] | 643 | }; |
| 644 | |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 645 | #define RENESAS_GEN2_QUIRKS TMIO_SD_CAP_RCAR_GEN2 |
Marek Vasut | f98833d | 2018-04-08 18:49:52 +0200 | [diff] [blame] | 646 | #define RENESAS_GEN3_QUIRKS \ |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 647 | TMIO_SD_CAP_64BIT | TMIO_SD_CAP_RCAR_GEN3 | TMIO_SD_CAP_RCAR_UHS |
Marek Vasut | f98833d | 2018-04-08 18:49:52 +0200 | [diff] [blame] | 648 | |
Marek Vasut | e94cad9 | 2018-04-08 15:22:58 +0200 | [diff] [blame] | 649 | static const struct udevice_id renesas_sdhi_match[] = { |
Marek Vasut | f98833d | 2018-04-08 18:49:52 +0200 | [diff] [blame] | 650 | { .compatible = "renesas,sdhi-r8a7790", .data = RENESAS_GEN2_QUIRKS }, |
| 651 | { .compatible = "renesas,sdhi-r8a7791", .data = RENESAS_GEN2_QUIRKS }, |
| 652 | { .compatible = "renesas,sdhi-r8a7792", .data = RENESAS_GEN2_QUIRKS }, |
| 653 | { .compatible = "renesas,sdhi-r8a7793", .data = RENESAS_GEN2_QUIRKS }, |
| 654 | { .compatible = "renesas,sdhi-r8a7794", .data = RENESAS_GEN2_QUIRKS }, |
| 655 | { .compatible = "renesas,sdhi-r8a7795", .data = RENESAS_GEN3_QUIRKS }, |
| 656 | { .compatible = "renesas,sdhi-r8a7796", .data = RENESAS_GEN3_QUIRKS }, |
| 657 | { .compatible = "renesas,sdhi-r8a77965", .data = RENESAS_GEN3_QUIRKS }, |
| 658 | { .compatible = "renesas,sdhi-r8a77970", .data = RENESAS_GEN3_QUIRKS }, |
Marek Vasut | d629152 | 2018-04-26 13:19:29 +0200 | [diff] [blame] | 659 | { .compatible = "renesas,sdhi-r8a77990", .data = RENESAS_GEN3_QUIRKS }, |
Marek Vasut | f98833d | 2018-04-08 18:49:52 +0200 | [diff] [blame] | 660 | { .compatible = "renesas,sdhi-r8a77995", .data = RENESAS_GEN3_QUIRKS }, |
Marek Vasut | e94cad9 | 2018-04-08 15:22:58 +0200 | [diff] [blame] | 661 | { /* sentinel */ } |
| 662 | }; |
| 663 | |
Marek Vasut | 8ec6a04 | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 664 | static ulong renesas_sdhi_clk_get_rate(struct tmio_sd_priv *priv) |
| 665 | { |
| 666 | return clk_get_rate(&priv->clk); |
| 667 | } |
| 668 | |
Marek Vasut | d34bd2d | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 669 | static void renesas_sdhi_filter_caps(struct udevice *dev) |
| 670 | { |
| 671 | struct tmio_sd_plat *plat = dev_get_platdata(dev); |
| 672 | struct tmio_sd_priv *priv = dev_get_priv(dev); |
| 673 | |
| 674 | if (!(priv->caps & TMIO_SD_CAP_RCAR_GEN3)) |
| 675 | return; |
| 676 | |
Marek Vasut | b5900a5 | 2019-05-19 03:47:07 +0200 | [diff] [blame] | 677 | /* HS400 is not supported on H3 ES1.x and M3W ES1.0,ES1.1,ES1.2 */ |
Marek Vasut | d34bd2d | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 678 | if (((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7795) && |
| 679 | (rmobile_get_cpu_rev_integer() <= 1)) || |
| 680 | ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7796) && |
| 681 | (rmobile_get_cpu_rev_integer() == 1) && |
Marek Vasut | b5900a5 | 2019-05-19 03:47:07 +0200 | [diff] [blame] | 682 | (rmobile_get_cpu_rev_fraction() <= 2))) |
Marek Vasut | d34bd2d | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 683 | plat->cfg.host_caps &= ~MMC_MODE_HS400; |
Marek Vasut | 50aa1d9 | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 684 | |
Marek Vasut | b5900a5 | 2019-05-19 03:47:07 +0200 | [diff] [blame] | 685 | /* M3W ES1.x for x>2 can use HS400 with manual adjustment */ |
| 686 | if ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7796) && |
| 687 | (rmobile_get_cpu_rev_integer() == 1) && |
| 688 | (rmobile_get_cpu_rev_fraction() > 2)) { |
| 689 | priv->adjust_hs400_enable = true; |
Marek Vasut | e5d3f3d | 2019-11-23 13:36:21 +0100 | [diff] [blame] | 690 | priv->adjust_hs400_offset = 3; |
Marek Vasut | b5900a5 | 2019-05-19 03:47:07 +0200 | [diff] [blame] | 691 | priv->adjust_hs400_calibrate = 0x9; |
| 692 | } |
| 693 | |
| 694 | /* M3N can use HS400 with manual adjustment */ |
| 695 | if (rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A77965) { |
| 696 | priv->adjust_hs400_enable = true; |
Marek Vasut | e5d3f3d | 2019-11-23 13:36:21 +0100 | [diff] [blame] | 697 | priv->adjust_hs400_offset = 3; |
Marek Vasut | b5900a5 | 2019-05-19 03:47:07 +0200 | [diff] [blame] | 698 | priv->adjust_hs400_calibrate = 0x0; |
| 699 | } |
| 700 | |
| 701 | /* E3 can use HS400 with manual adjustment */ |
| 702 | if (rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A77990) { |
| 703 | priv->adjust_hs400_enable = true; |
Marek Vasut | e5d3f3d | 2019-11-23 13:36:21 +0100 | [diff] [blame] | 704 | priv->adjust_hs400_offset = 3; |
| 705 | priv->adjust_hs400_calibrate = 0x4; |
Marek Vasut | b5900a5 | 2019-05-19 03:47:07 +0200 | [diff] [blame] | 706 | } |
| 707 | |
Marek Vasut | 8109988 | 2019-11-23 13:36:19 +0100 | [diff] [blame] | 708 | /* H3 ES1.x, ES2.0 and M3W ES1.0, ES1.1, ES1.2 uses 4 tuning taps */ |
| 709 | if (((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7795) && |
| 710 | (rmobile_get_cpu_rev_integer() <= 2)) || |
| 711 | ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7796) && |
| 712 | (rmobile_get_cpu_rev_integer() == 1) && |
| 713 | (rmobile_get_cpu_rev_fraction() <= 2))) |
Marek Vasut | 50aa1d9 | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 714 | priv->nrtaps = 4; |
| 715 | else |
| 716 | priv->nrtaps = 8; |
Marek Vasut | 992bcf4 | 2019-01-11 23:45:54 +0100 | [diff] [blame] | 717 | |
| 718 | /* H3 ES1.x and M3W ES1.0 uses bit 17 for DTRAEND */ |
| 719 | if (((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7795) && |
| 720 | (rmobile_get_cpu_rev_integer() <= 1)) || |
| 721 | ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7796) && |
| 722 | (rmobile_get_cpu_rev_integer() == 1) && |
| 723 | (rmobile_get_cpu_rev_fraction() == 0))) |
| 724 | priv->read_poll_flag = TMIO_SD_DMA_INFO1_END_RD; |
| 725 | else |
| 726 | priv->read_poll_flag = TMIO_SD_DMA_INFO1_END_RD2; |
Marek Vasut | d34bd2d | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 727 | } |
| 728 | |
Marek Vasut | c769e60 | 2018-04-08 17:45:23 +0200 | [diff] [blame] | 729 | static int renesas_sdhi_probe(struct udevice *dev) |
| 730 | { |
Masahiro Yamada | 30b5d9a | 2018-04-20 18:14:24 +0900 | [diff] [blame] | 731 | struct tmio_sd_priv *priv = dev_get_priv(dev); |
Marek Vasut | c769e60 | 2018-04-08 17:45:23 +0200 | [diff] [blame] | 732 | u32 quirks = dev_get_driver_data(dev); |
Marek Vasut | 7cf7ef8 | 2018-04-08 18:14:22 +0200 | [diff] [blame] | 733 | struct fdt_resource reg_res; |
| 734 | DECLARE_GLOBAL_DATA_PTR; |
| 735 | int ret; |
| 736 | |
Marek Vasut | 8ec6a04 | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 737 | priv->clk_get_rate = renesas_sdhi_clk_get_rate; |
| 738 | |
Marek Vasut | f98833d | 2018-04-08 18:49:52 +0200 | [diff] [blame] | 739 | if (quirks == RENESAS_GEN2_QUIRKS) { |
| 740 | ret = fdt_get_resource(gd->fdt_blob, dev_of_offset(dev), |
| 741 | "reg", 0, ®_res); |
| 742 | if (ret < 0) { |
| 743 | dev_err(dev, "\"reg\" resource not found, ret=%i\n", |
| 744 | ret); |
| 745 | return ret; |
| 746 | } |
Marek Vasut | 7cf7ef8 | 2018-04-08 18:14:22 +0200 | [diff] [blame] | 747 | |
Marek Vasut | f98833d | 2018-04-08 18:49:52 +0200 | [diff] [blame] | 748 | if (fdt_resource_size(®_res) == 0x100) |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 749 | quirks |= TMIO_SD_CAP_16BIT; |
Marek Vasut | f98833d | 2018-04-08 18:49:52 +0200 | [diff] [blame] | 750 | } |
Marek Vasut | c769e60 | 2018-04-08 17:45:23 +0200 | [diff] [blame] | 751 | |
Marek Vasut | 8ec6a04 | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 752 | ret = clk_get_by_index(dev, 0, &priv->clk); |
Masahiro Yamada | 30b5d9a | 2018-04-20 18:14:24 +0900 | [diff] [blame] | 753 | if (ret < 0) { |
| 754 | dev_err(dev, "failed to get host clock\n"); |
| 755 | return ret; |
| 756 | } |
| 757 | |
| 758 | /* set to max rate */ |
Marek Vasut | 8ec6a04 | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 759 | ret = clk_set_rate(&priv->clk, 200000000); |
| 760 | if (ret < 0) { |
Masahiro Yamada | 30b5d9a | 2018-04-20 18:14:24 +0900 | [diff] [blame] | 761 | dev_err(dev, "failed to set rate for host clock\n"); |
Marek Vasut | 8ec6a04 | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 762 | clk_free(&priv->clk); |
| 763 | return ret; |
Masahiro Yamada | 30b5d9a | 2018-04-20 18:14:24 +0900 | [diff] [blame] | 764 | } |
| 765 | |
Marek Vasut | 8ec6a04 | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 766 | ret = clk_enable(&priv->clk); |
Masahiro Yamada | 30b5d9a | 2018-04-20 18:14:24 +0900 | [diff] [blame] | 767 | if (ret) { |
| 768 | dev_err(dev, "failed to enable host clock\n"); |
| 769 | return ret; |
| 770 | } |
| 771 | |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 772 | ret = tmio_sd_probe(dev, quirks); |
Marek Vasut | d34bd2d | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 773 | |
| 774 | renesas_sdhi_filter_caps(dev); |
| 775 | |
Marek Vasut | 50aa1d9 | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 776 | #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \ |
| 777 | CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \ |
| 778 | CONFIG_IS_ENABLED(MMC_HS400_SUPPORT) |
Marek Vasut | 52e1796 | 2018-10-28 15:30:06 +0100 | [diff] [blame] | 779 | if (!ret && (priv->caps & TMIO_SD_CAP_RCAR_UHS)) |
Marek Vasut | 6518697 | 2018-08-30 15:27:26 +0200 | [diff] [blame] | 780 | renesas_sdhi_reset_tuning(priv); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 781 | #endif |
| 782 | return ret; |
Marek Vasut | c769e60 | 2018-04-08 17:45:23 +0200 | [diff] [blame] | 783 | } |
| 784 | |
Marek Vasut | e94cad9 | 2018-04-08 15:22:58 +0200 | [diff] [blame] | 785 | U_BOOT_DRIVER(renesas_sdhi) = { |
| 786 | .name = "renesas-sdhi", |
| 787 | .id = UCLASS_MMC, |
| 788 | .of_match = renesas_sdhi_match, |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 789 | .bind = tmio_sd_bind, |
Marek Vasut | c769e60 | 2018-04-08 17:45:23 +0200 | [diff] [blame] | 790 | .probe = renesas_sdhi_probe, |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 791 | .priv_auto_alloc_size = sizeof(struct tmio_sd_priv), |
| 792 | .platdata_auto_alloc_size = sizeof(struct tmio_sd_plat), |
Marek Vasut | e94cad9 | 2018-04-08 15:22:58 +0200 | [diff] [blame] | 793 | .ops = &renesas_sdhi_ops, |
| 794 | }; |