Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Marek Vasut | 06485cf | 2018-04-08 15:22:58 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2018 Marek Vasut <marek.vasut@gmail.com> |
Marek Vasut | 06485cf | 2018-04-08 15:22:58 +0200 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Marek Vasut | 9f7baeb | 2020-04-04 12:45:04 +0200 | [diff] [blame^] | 7 | #include <bouncebuf.h> |
Marek Vasut | 06485cf | 2018-04-08 15:22:58 +0200 | [diff] [blame] | 8 | #include <clk.h> |
| 9 | #include <fdtdec.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 10 | #include <malloc.h> |
Marek Vasut | 06485cf | 2018-04-08 15:22:58 +0200 | [diff] [blame] | 11 | #include <mmc.h> |
| 12 | #include <dm.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 13 | #include <dm/device_compat.h> |
Marek Vasut | 06485cf | 2018-04-08 15:22:58 +0200 | [diff] [blame] | 14 | #include <linux/compat.h> |
| 15 | #include <linux/dma-direction.h> |
| 16 | #include <linux/io.h> |
| 17 | #include <linux/sizes.h> |
| 18 | #include <power/regulator.h> |
| 19 | #include <asm/unaligned.h> |
| 20 | |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 21 | #include "tmio-common.h" |
Marek Vasut | 06485cf | 2018-04-08 15:22:58 +0200 | [diff] [blame] | 22 | |
Marek Vasut | 10d77ed | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 23 | #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \ |
| 24 | CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \ |
| 25 | CONFIG_IS_ENABLED(MMC_HS400_SUPPORT) |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 26 | |
| 27 | /* SCC registers */ |
| 28 | #define RENESAS_SDHI_SCC_DTCNTL 0x800 |
Marek Vasut | b1d442b | 2019-05-19 02:33:06 +0200 | [diff] [blame] | 29 | #define RENESAS_SDHI_SCC_DTCNTL_TAPEN BIT(0) |
| 30 | #define RENESAS_SDHI_SCC_DTCNTL_TAPNUM_SHIFT 16 |
| 31 | #define RENESAS_SDHI_SCC_DTCNTL_TAPNUM_MASK 0xff |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 32 | #define RENESAS_SDHI_SCC_TAPSET 0x804 |
| 33 | #define RENESAS_SDHI_SCC_DT2FF 0x808 |
| 34 | #define RENESAS_SDHI_SCC_CKSEL 0x80c |
Marek Vasut | b1d442b | 2019-05-19 02:33:06 +0200 | [diff] [blame] | 35 | #define RENESAS_SDHI_SCC_CKSEL_DTSEL BIT(0) |
| 36 | #define RENESAS_SDHI_SCC_RVSCNTL 0x810 |
| 37 | #define RENESAS_SDHI_SCC_RVSCNTL_RVSEN BIT(0) |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 38 | #define RENESAS_SDHI_SCC_RVSREQ 0x814 |
Marek Vasut | b1d442b | 2019-05-19 02:33:06 +0200 | [diff] [blame] | 39 | #define RENESAS_SDHI_SCC_RVSREQ_RVSERR BIT(2) |
Marek Vasut | 52647a0 | 2019-11-23 13:36:23 +0100 | [diff] [blame] | 40 | #define RENESAS_SDHI_SCC_RVSREQ_REQTAPUP BIT(1) |
| 41 | #define RENESAS_SDHI_SCC_RVSREQ_REQTAPDOWN BIT(0) |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 42 | #define RENESAS_SDHI_SCC_SMPCMP 0x818 |
Marek Vasut | 52647a0 | 2019-11-23 13:36:23 +0100 | [diff] [blame] | 43 | #define RENESAS_SDHI_SCC_SMPCMP_CMD_ERR (BIT(24) | BIT(8)) |
| 44 | #define RENESAS_SDHI_SCC_SMPCMP_CMD_REQUP BIT(24) |
| 45 | #define RENESAS_SDHI_SCC_SMPCMP_CMD_REQDOWN BIT(8) |
Marek Vasut | b1d442b | 2019-05-19 02:33:06 +0200 | [diff] [blame] | 46 | #define RENESAS_SDHI_SCC_TMPPORT2 0x81c |
| 47 | #define RENESAS_SDHI_SCC_TMPPORT2_HS400EN BIT(31) |
| 48 | #define RENESAS_SDHI_SCC_TMPPORT2_HS400OSEL BIT(4) |
Marek Vasut | fee0c68 | 2019-05-19 03:47:07 +0200 | [diff] [blame] | 49 | #define RENESAS_SDHI_SCC_TMPPORT3 0x828 |
| 50 | #define RENESAS_SDHI_SCC_TMPPORT3_OFFSET_0 3 |
| 51 | #define RENESAS_SDHI_SCC_TMPPORT3_OFFSET_1 2 |
| 52 | #define RENESAS_SDHI_SCC_TMPPORT3_OFFSET_2 1 |
| 53 | #define RENESAS_SDHI_SCC_TMPPORT3_OFFSET_3 0 |
| 54 | #define RENESAS_SDHI_SCC_TMPPORT3_OFFSET_MASK 0x3 |
| 55 | #define RENESAS_SDHI_SCC_TMPPORT4 0x82c |
| 56 | #define RENESAS_SDHI_SCC_TMPPORT4_DLL_ACC_START BIT(0) |
| 57 | #define RENESAS_SDHI_SCC_TMPPORT5 0x830 |
| 58 | #define RENESAS_SDHI_SCC_TMPPORT5_DLL_RW_SEL_R BIT(8) |
| 59 | #define RENESAS_SDHI_SCC_TMPPORT5_DLL_RW_SEL_W (0 << 8) |
| 60 | #define RENESAS_SDHI_SCC_TMPPORT5_DLL_ADR_MASK 0x3F |
| 61 | #define RENESAS_SDHI_SCC_TMPPORT6 0x834 |
| 62 | #define RENESAS_SDHI_SCC_TMPPORT7 0x838 |
| 63 | #define RENESAS_SDHI_SCC_TMPPORT_DISABLE_WP_CODE 0xa5000000 |
| 64 | #define RENESAS_SDHI_SCC_TMPPORT_CALIB_CODE_MASK 0x1f |
| 65 | #define RENESAS_SDHI_SCC_TMPPORT_MANUAL_MODE BIT(7) |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 66 | |
| 67 | #define RENESAS_SDHI_MAX_TAP 3 |
| 68 | |
Marek Vasut | 7b46176 | 2019-11-23 13:36:25 +0100 | [diff] [blame] | 69 | #define CALIB_TABLE_MAX (RENESAS_SDHI_SCC_TMPPORT_CALIB_CODE_MASK + 1) |
| 70 | |
| 71 | static const u8 r8a7795_calib_table[2][CALIB_TABLE_MAX] = { |
| 72 | { 0, 0, 0, 0, 0, 1, 1, 2, 3, 4, 5, 5, 6, 6, 7, 11, |
| 73 | 15, 16, 16, 17, 17, 17, 17, 17, 18, 18, 18, 18, 19, 20, 21, 21 }, |
| 74 | { 3, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 9, 10, 11, 12, 15, |
| 75 | 16, 16, 17, 17, 17, 17, 17, 18, 18, 18, 18, 19, 20, 21, 22, 22 } |
| 76 | }; |
| 77 | |
| 78 | static const u8 r8a7796_rev1_calib_table[2][CALIB_TABLE_MAX] = { |
| 79 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 3, 4, 9, |
| 80 | 15, 15, 15, 16, 16, 16, 16, 16, 17, 18, 19, 20, 21, 21, 22, 22 }, |
| 81 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, |
| 82 | 2, 9, 16, 17, 17, 17, 18, 18, 18, 18, 19, 20, 21, 22, 23, 24} |
| 83 | }; |
| 84 | |
| 85 | static const u8 r8a7796_rev3_calib_table[2][CALIB_TABLE_MAX] = { |
| 86 | { 0, 0, 0, 0, 2, 3, 4, 4, 5, 6, 7, 7, 8, 9, 9, 10, |
| 87 | 11, 12, 13, 15, 16, 17, 17, 18, 19, 19, 20, 21, 21, 22, 23, 23 }, |
| 88 | { 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 9, 9, 10, 11, 12, |
| 89 | 13, 14, 15, 16, 17, 17, 18, 19, 20, 20, 21, 22, 22, 23, 24, 24 } |
| 90 | }; |
| 91 | |
| 92 | static const u8 r8a77965_calib_table[2][CALIB_TABLE_MAX] = { |
| 93 | { 0, 1, 2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, |
| 94 | 16, 17, 18, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 29 }, |
| 95 | { 0, 1, 2, 2, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 13, 15, |
| 96 | 16, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 31 } |
| 97 | }; |
| 98 | |
| 99 | static const u8 r8a77990_calib_table[2][CALIB_TABLE_MAX] = { |
| 100 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 101 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 102 | { 0, 0, 1, 2, 3, 4, 4, 4, 4, 5, 5, 6, 7, 8, 10, 11, |
| 103 | 12, 13, 14, 16, 17, 18, 18, 18, 19, 19, 20, 24, 26, 26, 26, 26 } |
| 104 | }; |
| 105 | |
| 106 | static int rmobile_is_gen3_mmc0(struct tmio_sd_priv *priv) |
| 107 | { |
| 108 | /* On R-Car Gen3, MMC0 is at 0xee140000 */ |
| 109 | return (uintptr_t)(priv->regbase) == 0xee140000; |
| 110 | } |
| 111 | |
Marek Vasut | fee0c68 | 2019-05-19 03:47:07 +0200 | [diff] [blame] | 112 | static u32 sd_scc_tmpport_read32(struct tmio_sd_priv *priv, u32 addr) |
| 113 | { |
| 114 | /* read mode */ |
| 115 | tmio_sd_writel(priv, RENESAS_SDHI_SCC_TMPPORT5_DLL_RW_SEL_R | |
| 116 | (RENESAS_SDHI_SCC_TMPPORT5_DLL_ADR_MASK & addr), |
| 117 | RENESAS_SDHI_SCC_TMPPORT5); |
| 118 | |
| 119 | /* access start and stop */ |
| 120 | tmio_sd_writel(priv, RENESAS_SDHI_SCC_TMPPORT4_DLL_ACC_START, |
| 121 | RENESAS_SDHI_SCC_TMPPORT4); |
| 122 | tmio_sd_writel(priv, 0, RENESAS_SDHI_SCC_TMPPORT4); |
| 123 | |
| 124 | return tmio_sd_readl(priv, RENESAS_SDHI_SCC_TMPPORT7); |
| 125 | } |
| 126 | |
| 127 | static void sd_scc_tmpport_write32(struct tmio_sd_priv *priv, u32 addr, u32 val) |
| 128 | { |
| 129 | /* write mode */ |
| 130 | tmio_sd_writel(priv, RENESAS_SDHI_SCC_TMPPORT5_DLL_RW_SEL_W | |
| 131 | (RENESAS_SDHI_SCC_TMPPORT5_DLL_ADR_MASK & addr), |
| 132 | RENESAS_SDHI_SCC_TMPPORT5); |
| 133 | tmio_sd_writel(priv, val, RENESAS_SDHI_SCC_TMPPORT6); |
| 134 | |
| 135 | /* access start and stop */ |
| 136 | tmio_sd_writel(priv, RENESAS_SDHI_SCC_TMPPORT4_DLL_ACC_START, |
| 137 | RENESAS_SDHI_SCC_TMPPORT4); |
| 138 | tmio_sd_writel(priv, 0, RENESAS_SDHI_SCC_TMPPORT4); |
| 139 | } |
| 140 | |
Marek Vasut | 52647a0 | 2019-11-23 13:36:23 +0100 | [diff] [blame] | 141 | static bool renesas_sdhi_check_scc_error(struct udevice *dev) |
| 142 | { |
| 143 | struct tmio_sd_priv *priv = dev_get_priv(dev); |
| 144 | struct mmc *mmc = mmc_get_mmc_dev(dev); |
| 145 | unsigned long new_tap = priv->tap_set; |
Marek Vasut | 531fc99 | 2019-11-23 13:36:24 +0100 | [diff] [blame] | 146 | unsigned long error_tap = priv->tap_set; |
Marek Vasut | 52647a0 | 2019-11-23 13:36:23 +0100 | [diff] [blame] | 147 | u32 reg, smpcmp; |
| 148 | |
| 149 | if ((priv->caps & TMIO_SD_CAP_RCAR_UHS) && |
| 150 | (mmc->selected_mode != UHS_SDR104) && |
| 151 | (mmc->selected_mode != MMC_HS_200) && |
| 152 | (mmc->selected_mode != MMC_HS_400) && |
| 153 | (priv->nrtaps != 4)) |
| 154 | return false; |
| 155 | |
| 156 | reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSCNTL); |
| 157 | /* Handle automatic tuning correction */ |
| 158 | if (reg & RENESAS_SDHI_SCC_RVSCNTL_RVSEN) { |
| 159 | reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSREQ); |
| 160 | if (reg & RENESAS_SDHI_SCC_RVSREQ_RVSERR) { |
| 161 | tmio_sd_writel(priv, 0, RENESAS_SDHI_SCC_RVSREQ); |
| 162 | return true; |
| 163 | } |
| 164 | |
| 165 | return false; |
| 166 | } |
| 167 | |
| 168 | /* Handle manual tuning correction */ |
| 169 | reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSREQ); |
| 170 | if (!reg) /* No error */ |
| 171 | return false; |
| 172 | |
| 173 | tmio_sd_writel(priv, 0, RENESAS_SDHI_SCC_RVSREQ); |
| 174 | |
| 175 | if (mmc->selected_mode == MMC_HS_400) { |
| 176 | /* |
| 177 | * Correction Error Status contains CMD and DAT signal status. |
| 178 | * In HS400, DAT signal based on DS signal, not CLK. |
| 179 | * Therefore, use only CMD status. |
| 180 | */ |
| 181 | smpcmp = tmio_sd_readl(priv, RENESAS_SDHI_SCC_SMPCMP) & |
| 182 | RENESAS_SDHI_SCC_SMPCMP_CMD_ERR; |
| 183 | |
| 184 | switch (smpcmp) { |
| 185 | case 0: |
| 186 | return false; /* No error in CMD signal */ |
| 187 | case RENESAS_SDHI_SCC_SMPCMP_CMD_REQUP: |
| 188 | new_tap = (priv->tap_set + |
| 189 | priv->tap_num + 1) % priv->tap_num; |
Marek Vasut | 531fc99 | 2019-11-23 13:36:24 +0100 | [diff] [blame] | 190 | error_tap = (priv->tap_set + |
| 191 | priv->tap_num - 1) % priv->tap_num; |
Marek Vasut | 52647a0 | 2019-11-23 13:36:23 +0100 | [diff] [blame] | 192 | break; |
| 193 | case RENESAS_SDHI_SCC_SMPCMP_CMD_REQDOWN: |
| 194 | new_tap = (priv->tap_set + |
| 195 | priv->tap_num - 1) % priv->tap_num; |
Marek Vasut | 531fc99 | 2019-11-23 13:36:24 +0100 | [diff] [blame] | 196 | error_tap = (priv->tap_set + |
| 197 | priv->tap_num + 1) % priv->tap_num; |
Marek Vasut | 52647a0 | 2019-11-23 13:36:23 +0100 | [diff] [blame] | 198 | break; |
| 199 | default: |
| 200 | return true; /* Need re-tune */ |
| 201 | } |
| 202 | |
Marek Vasut | 531fc99 | 2019-11-23 13:36:24 +0100 | [diff] [blame] | 203 | if (priv->hs400_bad_tap & BIT(new_tap)) { |
| 204 | /* |
| 205 | * New tap is bad tap (cannot change). |
| 206 | * Compare with HS200 tuning result. |
| 207 | * In HS200 tuning, when smpcmp[error_tap] |
| 208 | * is OK, retune is executed. |
| 209 | */ |
| 210 | if (priv->smpcmp & BIT(error_tap)) |
| 211 | return true; /* Need retune */ |
| 212 | |
| 213 | return false; /* cannot change */ |
| 214 | } |
| 215 | |
Marek Vasut | 52647a0 | 2019-11-23 13:36:23 +0100 | [diff] [blame] | 216 | priv->tap_set = new_tap; |
| 217 | } else { |
| 218 | if (reg & RENESAS_SDHI_SCC_RVSREQ_RVSERR) |
| 219 | return true; /* Need re-tune */ |
| 220 | else if (reg & RENESAS_SDHI_SCC_RVSREQ_REQTAPUP) |
| 221 | priv->tap_set = (priv->tap_set + |
| 222 | priv->tap_num + 1) % priv->tap_num; |
| 223 | else if (reg & RENESAS_SDHI_SCC_RVSREQ_REQTAPDOWN) |
| 224 | priv->tap_set = (priv->tap_set + |
| 225 | priv->tap_num - 1) % priv->tap_num; |
| 226 | else |
| 227 | return false; |
| 228 | } |
| 229 | |
| 230 | /* Set TAP position */ |
| 231 | tmio_sd_writel(priv, priv->tap_set >> ((priv->nrtaps == 4) ? 1 : 0), |
| 232 | RENESAS_SDHI_SCC_TAPSET); |
| 233 | |
| 234 | return false; |
| 235 | } |
| 236 | |
Marek Vasut | fee0c68 | 2019-05-19 03:47:07 +0200 | [diff] [blame] | 237 | static void renesas_sdhi_adjust_hs400_mode_enable(struct tmio_sd_priv *priv) |
| 238 | { |
| 239 | u32 calib_code; |
| 240 | |
| 241 | if (!priv->adjust_hs400_enable) |
| 242 | return; |
| 243 | |
| 244 | if (!priv->needs_adjust_hs400) |
| 245 | return; |
| 246 | |
Marek Vasut | 7b46176 | 2019-11-23 13:36:25 +0100 | [diff] [blame] | 247 | if (!priv->adjust_hs400_calib_table) |
| 248 | return; |
| 249 | |
Marek Vasut | fee0c68 | 2019-05-19 03:47:07 +0200 | [diff] [blame] | 250 | /* |
| 251 | * Enabled Manual adjust HS400 mode |
| 252 | * |
| 253 | * 1) Disabled Write Protect |
| 254 | * W(addr=0x00, WP_DISABLE_CODE) |
Marek Vasut | 7b46176 | 2019-11-23 13:36:25 +0100 | [diff] [blame] | 255 | * |
| 256 | * 2) Read Calibration code |
| 257 | * read_value = R(addr=0x26) |
| 258 | * 3) Refer to calibration table |
| 259 | * Calibration code = table[read_value] |
| 260 | * 4) Enabled Manual Calibration |
Marek Vasut | fee0c68 | 2019-05-19 03:47:07 +0200 | [diff] [blame] | 261 | * W(addr=0x22, manual mode | Calibration code) |
Marek Vasut | 7b46176 | 2019-11-23 13:36:25 +0100 | [diff] [blame] | 262 | * 5) Set Offset value to TMPPORT3 Reg |
Marek Vasut | fee0c68 | 2019-05-19 03:47:07 +0200 | [diff] [blame] | 263 | */ |
| 264 | sd_scc_tmpport_write32(priv, 0x00, |
| 265 | RENESAS_SDHI_SCC_TMPPORT_DISABLE_WP_CODE); |
| 266 | calib_code = sd_scc_tmpport_read32(priv, 0x26); |
| 267 | calib_code &= RENESAS_SDHI_SCC_TMPPORT_CALIB_CODE_MASK; |
Marek Vasut | fee0c68 | 2019-05-19 03:47:07 +0200 | [diff] [blame] | 268 | sd_scc_tmpport_write32(priv, 0x22, |
| 269 | RENESAS_SDHI_SCC_TMPPORT_MANUAL_MODE | |
Marek Vasut | 7b46176 | 2019-11-23 13:36:25 +0100 | [diff] [blame] | 270 | priv->adjust_hs400_calib_table[calib_code]); |
Marek Vasut | fee0c68 | 2019-05-19 03:47:07 +0200 | [diff] [blame] | 271 | tmio_sd_writel(priv, priv->adjust_hs400_offset, |
| 272 | RENESAS_SDHI_SCC_TMPPORT3); |
| 273 | |
| 274 | /* Clear flag */ |
| 275 | priv->needs_adjust_hs400 = false; |
| 276 | } |
| 277 | |
| 278 | static void renesas_sdhi_adjust_hs400_mode_disable(struct tmio_sd_priv *priv) |
| 279 | { |
| 280 | |
| 281 | /* Disabled Manual adjust HS400 mode |
| 282 | * |
| 283 | * 1) Disabled Write Protect |
| 284 | * W(addr=0x00, WP_DISABLE_CODE) |
| 285 | * 2) Disabled Manual Calibration |
| 286 | * W(addr=0x22, 0) |
| 287 | * 3) Clear offset value to TMPPORT3 Reg |
| 288 | */ |
| 289 | sd_scc_tmpport_write32(priv, 0x00, |
| 290 | RENESAS_SDHI_SCC_TMPPORT_DISABLE_WP_CODE); |
| 291 | sd_scc_tmpport_write32(priv, 0x22, 0); |
| 292 | tmio_sd_writel(priv, 0, RENESAS_SDHI_SCC_TMPPORT3); |
| 293 | } |
| 294 | |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 295 | static unsigned int renesas_sdhi_init_tuning(struct tmio_sd_priv *priv) |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 296 | { |
| 297 | u32 reg; |
| 298 | |
| 299 | /* Initialize SCC */ |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 300 | tmio_sd_writel(priv, 0, TMIO_SD_INFO1); |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 301 | |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 302 | reg = tmio_sd_readl(priv, TMIO_SD_CLKCTL); |
| 303 | reg &= ~TMIO_SD_CLKCTL_SCLKEN; |
| 304 | tmio_sd_writel(priv, reg, TMIO_SD_CLKCTL); |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 305 | |
| 306 | /* Set sampling clock selection range */ |
Marek Vasut | da4873d | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 307 | tmio_sd_writel(priv, (0x8 << RENESAS_SDHI_SCC_DTCNTL_TAPNUM_SHIFT) | |
| 308 | RENESAS_SDHI_SCC_DTCNTL_TAPEN, |
| 309 | RENESAS_SDHI_SCC_DTCNTL); |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 310 | |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 311 | reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_CKSEL); |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 312 | reg |= RENESAS_SDHI_SCC_CKSEL_DTSEL; |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 313 | tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_CKSEL); |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 314 | |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 315 | reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSCNTL); |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 316 | reg &= ~RENESAS_SDHI_SCC_RVSCNTL_RVSEN; |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 317 | tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_RVSCNTL); |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 318 | |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 319 | tmio_sd_writel(priv, 0x300 /* scc_tappos */, |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 320 | RENESAS_SDHI_SCC_DT2FF); |
| 321 | |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 322 | reg = tmio_sd_readl(priv, TMIO_SD_CLKCTL); |
| 323 | reg |= TMIO_SD_CLKCTL_SCLKEN; |
| 324 | tmio_sd_writel(priv, reg, TMIO_SD_CLKCTL); |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 325 | |
| 326 | /* Read TAPNUM */ |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 327 | return (tmio_sd_readl(priv, RENESAS_SDHI_SCC_DTCNTL) >> |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 328 | RENESAS_SDHI_SCC_DTCNTL_TAPNUM_SHIFT) & |
| 329 | RENESAS_SDHI_SCC_DTCNTL_TAPNUM_MASK; |
| 330 | } |
| 331 | |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 332 | static void renesas_sdhi_reset_tuning(struct tmio_sd_priv *priv) |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 333 | { |
| 334 | u32 reg; |
| 335 | |
| 336 | /* Reset SCC */ |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 337 | reg = tmio_sd_readl(priv, TMIO_SD_CLKCTL); |
| 338 | reg &= ~TMIO_SD_CLKCTL_SCLKEN; |
| 339 | tmio_sd_writel(priv, reg, TMIO_SD_CLKCTL); |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 340 | |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 341 | reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_CKSEL); |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 342 | reg &= ~RENESAS_SDHI_SCC_CKSEL_DTSEL; |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 343 | tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_CKSEL); |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 344 | |
Marek Vasut | efea7a8 | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 345 | reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_TMPPORT2); |
| 346 | reg &= ~(RENESAS_SDHI_SCC_TMPPORT2_HS400EN | |
| 347 | RENESAS_SDHI_SCC_TMPPORT2_HS400OSEL); |
| 348 | tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_TMPPORT2); |
| 349 | |
Marek Vasut | fee0c68 | 2019-05-19 03:47:07 +0200 | [diff] [blame] | 350 | /* Disable HS400 mode adjustment */ |
| 351 | renesas_sdhi_adjust_hs400_mode_disable(priv); |
| 352 | |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 353 | reg = tmio_sd_readl(priv, TMIO_SD_CLKCTL); |
| 354 | reg |= TMIO_SD_CLKCTL_SCLKEN; |
| 355 | tmio_sd_writel(priv, reg, TMIO_SD_CLKCTL); |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 356 | |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 357 | reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSCNTL); |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 358 | reg &= ~RENESAS_SDHI_SCC_RVSCNTL_RVSEN; |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 359 | tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_RVSCNTL); |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 360 | |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 361 | reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSCNTL); |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 362 | reg &= ~RENESAS_SDHI_SCC_RVSCNTL_RVSEN; |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 363 | tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_RVSCNTL); |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 364 | } |
| 365 | |
Marek Vasut | 10d77ed | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 366 | static int renesas_sdhi_hs400(struct udevice *dev) |
| 367 | { |
| 368 | struct tmio_sd_priv *priv = dev_get_priv(dev); |
| 369 | struct mmc *mmc = mmc_get_mmc_dev(dev); |
| 370 | bool hs400 = (mmc->selected_mode == MMC_HS_400); |
| 371 | int ret, taps = hs400 ? priv->nrtaps : 8; |
Marek Vasut | 531fc99 | 2019-11-23 13:36:24 +0100 | [diff] [blame] | 372 | unsigned long new_tap; |
Marek Vasut | 10d77ed | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 373 | u32 reg; |
| 374 | |
| 375 | if (taps == 4) /* HS400 on 4tap SoC needs different clock */ |
| 376 | ret = clk_set_rate(&priv->clk, 400000000); |
| 377 | else |
| 378 | ret = clk_set_rate(&priv->clk, 200000000); |
| 379 | if (ret < 0) |
| 380 | return ret; |
| 381 | |
Marek Vasut | 242c63d | 2019-11-23 13:36:22 +0100 | [diff] [blame] | 382 | reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSCNTL); |
| 383 | reg &= ~RENESAS_SDHI_SCC_RVSCNTL_RVSEN; |
| 384 | tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_RVSCNTL); |
Marek Vasut | 10d77ed | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 385 | |
| 386 | reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_TMPPORT2); |
| 387 | if (hs400) { |
| 388 | reg |= RENESAS_SDHI_SCC_TMPPORT2_HS400EN | |
| 389 | RENESAS_SDHI_SCC_TMPPORT2_HS400OSEL; |
| 390 | } else { |
| 391 | reg &= ~(RENESAS_SDHI_SCC_TMPPORT2_HS400EN | |
| 392 | RENESAS_SDHI_SCC_TMPPORT2_HS400OSEL); |
| 393 | } |
| 394 | |
| 395 | tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_TMPPORT2); |
| 396 | |
Marek Vasut | fee0c68 | 2019-05-19 03:47:07 +0200 | [diff] [blame] | 397 | /* Disable HS400 mode adjustment */ |
| 398 | if (!hs400) |
| 399 | renesas_sdhi_adjust_hs400_mode_disable(priv); |
| 400 | |
Marek Vasut | 3d42a07 | 2019-02-19 19:32:28 +0100 | [diff] [blame] | 401 | tmio_sd_writel(priv, (0x8 << RENESAS_SDHI_SCC_DTCNTL_TAPNUM_SHIFT) | |
Marek Vasut | 10d77ed | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 402 | RENESAS_SDHI_SCC_DTCNTL_TAPEN, |
| 403 | RENESAS_SDHI_SCC_DTCNTL); |
| 404 | |
Marek Vasut | 531fc99 | 2019-11-23 13:36:24 +0100 | [diff] [blame] | 405 | /* Avoid bad TAP */ |
| 406 | if (priv->hs400_bad_tap & BIT(priv->tap_set)) { |
| 407 | new_tap = (priv->tap_set + |
| 408 | priv->tap_num + 1) % priv->tap_num; |
| 409 | |
| 410 | if (priv->hs400_bad_tap & BIT(new_tap)) |
| 411 | new_tap = (priv->tap_set + |
| 412 | priv->tap_num - 1) % priv->tap_num; |
| 413 | |
| 414 | if (priv->hs400_bad_tap & BIT(new_tap)) { |
| 415 | new_tap = priv->tap_set; |
| 416 | debug("Three consecutive bad tap is prohibited\n"); |
| 417 | } |
| 418 | |
| 419 | priv->tap_set = new_tap; |
| 420 | tmio_sd_writel(priv, priv->tap_set, RENESAS_SDHI_SCC_TAPSET); |
| 421 | } |
| 422 | |
Marek Vasut | 10d77ed | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 423 | if (taps == 4) { |
| 424 | tmio_sd_writel(priv, priv->tap_set >> 1, |
| 425 | RENESAS_SDHI_SCC_TAPSET); |
Marek Vasut | 1a95303 | 2019-11-23 13:36:20 +0100 | [diff] [blame] | 426 | tmio_sd_writel(priv, hs400 ? 0x100 : 0x300, |
| 427 | RENESAS_SDHI_SCC_DT2FF); |
Marek Vasut | 10d77ed | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 428 | } else { |
| 429 | tmio_sd_writel(priv, priv->tap_set, RENESAS_SDHI_SCC_TAPSET); |
Marek Vasut | 1a95303 | 2019-11-23 13:36:20 +0100 | [diff] [blame] | 430 | tmio_sd_writel(priv, 0x300, RENESAS_SDHI_SCC_DT2FF); |
Marek Vasut | 10d77ed | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_CKSEL); |
| 434 | reg |= RENESAS_SDHI_SCC_CKSEL_DTSEL; |
| 435 | tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_CKSEL); |
| 436 | |
Marek Vasut | fee0c68 | 2019-05-19 03:47:07 +0200 | [diff] [blame] | 437 | /* Execute adjust hs400 offset after setting to HS400 mode */ |
| 438 | if (hs400) |
| 439 | priv->needs_adjust_hs400 = true; |
| 440 | |
Marek Vasut | 10d77ed | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 441 | return 0; |
| 442 | } |
| 443 | |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 444 | static void renesas_sdhi_prepare_tuning(struct tmio_sd_priv *priv, |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 445 | unsigned long tap) |
| 446 | { |
| 447 | /* Set sampling clock position */ |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 448 | tmio_sd_writel(priv, tap, RENESAS_SDHI_SCC_TAPSET); |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 449 | } |
| 450 | |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 451 | static unsigned int renesas_sdhi_compare_scc_data(struct tmio_sd_priv *priv) |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 452 | { |
| 453 | /* Get comparison of sampling data */ |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 454 | return tmio_sd_readl(priv, RENESAS_SDHI_SCC_SMPCMP); |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 455 | } |
| 456 | |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 457 | static int renesas_sdhi_select_tuning(struct tmio_sd_priv *priv, |
Marek Vasut | 0555dc6 | 2019-11-23 13:36:18 +0100 | [diff] [blame] | 458 | unsigned int taps) |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 459 | { |
| 460 | unsigned long tap_cnt; /* counter of tuning success */ |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 461 | unsigned long tap_start;/* start position of tuning success */ |
| 462 | unsigned long tap_end; /* end position of tuning success */ |
| 463 | unsigned long ntap; /* temporary counter of tuning success */ |
| 464 | unsigned long match_cnt;/* counter of matching data */ |
| 465 | unsigned long i; |
| 466 | bool select = false; |
| 467 | u32 reg; |
| 468 | |
Marek Vasut | fee0c68 | 2019-05-19 03:47:07 +0200 | [diff] [blame] | 469 | priv->needs_adjust_hs400 = false; |
| 470 | |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 471 | /* Clear SCC_RVSREQ */ |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 472 | tmio_sd_writel(priv, 0, RENESAS_SDHI_SCC_RVSREQ); |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 473 | |
| 474 | /* Merge the results */ |
Marek Vasut | d9d09e3 | 2019-11-23 13:36:17 +0100 | [diff] [blame] | 475 | for (i = 0; i < priv->tap_num * 2; i++) { |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 476 | if (!(taps & BIT(i))) { |
Marek Vasut | d9d09e3 | 2019-11-23 13:36:17 +0100 | [diff] [blame] | 477 | taps &= ~BIT(i % priv->tap_num); |
| 478 | taps &= ~BIT((i % priv->tap_num) + priv->tap_num); |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 479 | } |
Marek Vasut | 0555dc6 | 2019-11-23 13:36:18 +0100 | [diff] [blame] | 480 | if (!(priv->smpcmp & BIT(i))) { |
| 481 | priv->smpcmp &= ~BIT(i % priv->tap_num); |
| 482 | priv->smpcmp &= ~BIT((i % priv->tap_num) + priv->tap_num); |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 483 | } |
| 484 | } |
| 485 | |
| 486 | /* |
| 487 | * Find the longest consecutive run of successful probes. If that |
| 488 | * is more than RENESAS_SDHI_MAX_TAP probes long then use the |
| 489 | * center index as the tap. |
| 490 | */ |
| 491 | tap_cnt = 0; |
| 492 | ntap = 0; |
| 493 | tap_start = 0; |
| 494 | tap_end = 0; |
Marek Vasut | d9d09e3 | 2019-11-23 13:36:17 +0100 | [diff] [blame] | 495 | for (i = 0; i < priv->tap_num * 2; i++) { |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 496 | if (taps & BIT(i)) |
| 497 | ntap++; |
| 498 | else { |
| 499 | if (ntap > tap_cnt) { |
| 500 | tap_start = i - ntap; |
| 501 | tap_end = i - 1; |
| 502 | tap_cnt = ntap; |
| 503 | } |
| 504 | ntap = 0; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | if (ntap > tap_cnt) { |
| 509 | tap_start = i - ntap; |
| 510 | tap_end = i - 1; |
| 511 | tap_cnt = ntap; |
| 512 | } |
| 513 | |
| 514 | /* |
| 515 | * If all of the TAP is OK, the sampling clock position is selected by |
| 516 | * identifying the change point of data. |
| 517 | */ |
Marek Vasut | d9d09e3 | 2019-11-23 13:36:17 +0100 | [diff] [blame] | 518 | if (tap_cnt == priv->tap_num * 2) { |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 519 | match_cnt = 0; |
| 520 | ntap = 0; |
| 521 | tap_start = 0; |
| 522 | tap_end = 0; |
Marek Vasut | d9d09e3 | 2019-11-23 13:36:17 +0100 | [diff] [blame] | 523 | for (i = 0; i < priv->tap_num * 2; i++) { |
Marek Vasut | 0555dc6 | 2019-11-23 13:36:18 +0100 | [diff] [blame] | 524 | if (priv->smpcmp & BIT(i)) |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 525 | ntap++; |
| 526 | else { |
| 527 | if (ntap > match_cnt) { |
| 528 | tap_start = i - ntap; |
| 529 | tap_end = i - 1; |
| 530 | match_cnt = ntap; |
| 531 | } |
| 532 | ntap = 0; |
| 533 | } |
| 534 | } |
| 535 | if (ntap > match_cnt) { |
| 536 | tap_start = i - ntap; |
| 537 | tap_end = i - 1; |
| 538 | match_cnt = ntap; |
| 539 | } |
| 540 | if (match_cnt) |
| 541 | select = true; |
| 542 | } else if (tap_cnt >= RENESAS_SDHI_MAX_TAP) |
| 543 | select = true; |
| 544 | |
| 545 | if (select) |
Marek Vasut | d9d09e3 | 2019-11-23 13:36:17 +0100 | [diff] [blame] | 546 | priv->tap_set = ((tap_start + tap_end) / 2) % priv->tap_num; |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 547 | else |
| 548 | return -EIO; |
| 549 | |
| 550 | /* Set SCC */ |
Marek Vasut | 1ebb9d6 | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 551 | tmio_sd_writel(priv, priv->tap_set, RENESAS_SDHI_SCC_TAPSET); |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 552 | |
| 553 | /* Enable auto re-tuning */ |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 554 | reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSCNTL); |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 555 | reg |= RENESAS_SDHI_SCC_RVSCNTL_RVSEN; |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 556 | tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_RVSCNTL); |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 557 | |
| 558 | return 0; |
| 559 | } |
| 560 | |
| 561 | int renesas_sdhi_execute_tuning(struct udevice *dev, uint opcode) |
| 562 | { |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 563 | struct tmio_sd_priv *priv = dev_get_priv(dev); |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 564 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
| 565 | struct mmc *mmc = upriv->mmc; |
| 566 | unsigned int tap_num; |
Marek Vasut | 0555dc6 | 2019-11-23 13:36:18 +0100 | [diff] [blame] | 567 | unsigned int taps = 0; |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 568 | int i, ret = 0; |
| 569 | u32 caps; |
| 570 | |
| 571 | /* Only supported on Renesas RCar */ |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 572 | if (!(priv->caps & TMIO_SD_CAP_RCAR_UHS)) |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 573 | return -EINVAL; |
| 574 | |
| 575 | /* clock tuning is not needed for upto 52MHz */ |
| 576 | if (!((mmc->selected_mode == MMC_HS_200) || |
Marek Vasut | 10d77ed | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 577 | (mmc->selected_mode == MMC_HS_400) || |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 578 | (mmc->selected_mode == UHS_SDR104) || |
| 579 | (mmc->selected_mode == UHS_SDR50))) |
| 580 | return 0; |
| 581 | |
| 582 | tap_num = renesas_sdhi_init_tuning(priv); |
| 583 | if (!tap_num) |
| 584 | /* Tuning is not supported */ |
| 585 | goto out; |
| 586 | |
Marek Vasut | d9d09e3 | 2019-11-23 13:36:17 +0100 | [diff] [blame] | 587 | priv->tap_num = tap_num; |
| 588 | |
| 589 | if (priv->tap_num * 2 >= sizeof(taps) * 8) { |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 590 | dev_err(dev, |
| 591 | "Too many taps, skipping tuning. Please consider updating size of taps field of tmio_mmc_host\n"); |
| 592 | goto out; |
| 593 | } |
| 594 | |
Marek Vasut | 0555dc6 | 2019-11-23 13:36:18 +0100 | [diff] [blame] | 595 | priv->smpcmp = 0; |
| 596 | |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 597 | /* Issue CMD19 twice for each tap */ |
Marek Vasut | d9d09e3 | 2019-11-23 13:36:17 +0100 | [diff] [blame] | 598 | for (i = 0; i < 2 * priv->tap_num; i++) { |
| 599 | renesas_sdhi_prepare_tuning(priv, i % priv->tap_num); |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 600 | |
| 601 | /* Force PIO for the tuning */ |
| 602 | caps = priv->caps; |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 603 | priv->caps &= ~TMIO_SD_CAP_DMA_INTERNAL; |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 604 | |
| 605 | ret = mmc_send_tuning(mmc, opcode, NULL); |
| 606 | |
| 607 | priv->caps = caps; |
| 608 | |
| 609 | if (ret == 0) |
| 610 | taps |= BIT(i); |
| 611 | |
| 612 | ret = renesas_sdhi_compare_scc_data(priv); |
| 613 | if (ret == 0) |
Marek Vasut | 0555dc6 | 2019-11-23 13:36:18 +0100 | [diff] [blame] | 614 | priv->smpcmp |= BIT(i); |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 615 | |
| 616 | mdelay(1); |
| 617 | } |
| 618 | |
Marek Vasut | 0555dc6 | 2019-11-23 13:36:18 +0100 | [diff] [blame] | 619 | ret = renesas_sdhi_select_tuning(priv, taps); |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 620 | |
| 621 | out: |
| 622 | if (ret < 0) { |
| 623 | dev_warn(dev, "Tuning procedure failed\n"); |
| 624 | renesas_sdhi_reset_tuning(priv); |
| 625 | } |
| 626 | |
| 627 | return ret; |
| 628 | } |
Marek Vasut | 10d77ed | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 629 | #else |
| 630 | static int renesas_sdhi_hs400(struct udevice *dev) |
| 631 | { |
| 632 | return 0; |
| 633 | } |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 634 | #endif |
| 635 | |
| 636 | static int renesas_sdhi_set_ios(struct udevice *dev) |
| 637 | { |
Marek Vasut | 10d77ed | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 638 | struct tmio_sd_priv *priv = dev_get_priv(dev); |
| 639 | u32 tmp; |
| 640 | int ret; |
Marek Vasut | 33d3818 | 2018-04-09 20:47:31 +0200 | [diff] [blame] | 641 | |
Marek Vasut | 10d77ed | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 642 | /* Stop the clock before changing its rate to avoid a glitch signal */ |
| 643 | tmp = tmio_sd_readl(priv, TMIO_SD_CLKCTL); |
| 644 | tmp &= ~TMIO_SD_CLKCTL_SCLKEN; |
| 645 | tmio_sd_writel(priv, tmp, TMIO_SD_CLKCTL); |
Marek Vasut | 33d3818 | 2018-04-09 20:47:31 +0200 | [diff] [blame] | 646 | |
Marek Vasut | 10d77ed | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 647 | ret = renesas_sdhi_hs400(dev); |
| 648 | if (ret) |
| 649 | return ret; |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 650 | |
Marek Vasut | 10d77ed | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 651 | ret = tmio_sd_set_ios(dev); |
| 652 | |
| 653 | mdelay(10); |
| 654 | |
| 655 | #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \ |
| 656 | CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \ |
| 657 | CONFIG_IS_ENABLED(MMC_HS400_SUPPORT) |
| 658 | struct mmc *mmc = mmc_get_mmc_dev(dev); |
| 659 | if ((priv->caps & TMIO_SD_CAP_RCAR_UHS) && |
| 660 | (mmc->selected_mode != UHS_SDR104) && |
| 661 | (mmc->selected_mode != MMC_HS_200) && |
| 662 | (mmc->selected_mode != MMC_HS_400)) { |
Marek Vasut | 3577380 | 2018-10-28 15:30:06 +0100 | [diff] [blame] | 663 | renesas_sdhi_reset_tuning(priv); |
Marek Vasut | 10d77ed | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 664 | } |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 665 | #endif |
| 666 | |
| 667 | return ret; |
| 668 | } |
| 669 | |
Marek Vasut | 576a6d9 | 2018-10-28 19:28:56 +0100 | [diff] [blame] | 670 | #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) |
Sam Protsenko | db174c6 | 2019-08-14 22:52:51 +0300 | [diff] [blame] | 671 | static int renesas_sdhi_wait_dat0(struct udevice *dev, int state, |
| 672 | int timeout_us) |
Marek Vasut | 576a6d9 | 2018-10-28 19:28:56 +0100 | [diff] [blame] | 673 | { |
| 674 | int ret = -ETIMEDOUT; |
| 675 | bool dat0_high; |
| 676 | bool target_dat0_high = !!state; |
| 677 | struct tmio_sd_priv *priv = dev_get_priv(dev); |
| 678 | |
Sam Protsenko | db174c6 | 2019-08-14 22:52:51 +0300 | [diff] [blame] | 679 | timeout_us = DIV_ROUND_UP(timeout_us, 10); /* check every 10 us. */ |
| 680 | while (timeout_us--) { |
Marek Vasut | 576a6d9 | 2018-10-28 19:28:56 +0100 | [diff] [blame] | 681 | dat0_high = !!(tmio_sd_readl(priv, TMIO_SD_INFO2) & TMIO_SD_INFO2_DAT0); |
| 682 | if (dat0_high == target_dat0_high) { |
| 683 | ret = 0; |
| 684 | break; |
| 685 | } |
| 686 | udelay(10); |
| 687 | } |
| 688 | |
| 689 | return ret; |
| 690 | } |
| 691 | #endif |
| 692 | |
Marek Vasut | 9f7baeb | 2020-04-04 12:45:04 +0200 | [diff] [blame^] | 693 | #define RENESAS_SDHI_DMA_ALIGNMENT 128 |
| 694 | |
| 695 | static int renesas_sdhi_addr_aligned(struct bounce_buffer *state) |
| 696 | { |
| 697 | uintptr_t ubuf = (uintptr_t)state->user_buffer; |
| 698 | |
| 699 | /* Check if start is aligned */ |
| 700 | if (!IS_ALIGNED(ubuf, RENESAS_SDHI_DMA_ALIGNMENT)) { |
| 701 | debug("Unaligned buffer address %p\n", state->user_buffer); |
| 702 | return 0; |
| 703 | } |
| 704 | |
| 705 | /* Check if length is aligned */ |
| 706 | if (state->len != state->len_aligned) { |
| 707 | debug("Unaligned buffer length %zu\n", state->len); |
| 708 | return 0; |
| 709 | } |
| 710 | |
| 711 | #ifdef CONFIG_PHYS_64BIT |
| 712 | /* Check if below 32bit boundary */ |
| 713 | if ((ubuf >> 32) || (ubuf + state->len_aligned) >> 32) { |
| 714 | debug("Buffer above 32bit boundary %p-%p\n", |
| 715 | state->user_buffer, |
| 716 | state->user_buffer + state->len_aligned); |
| 717 | return 0; |
| 718 | } |
| 719 | #endif |
| 720 | |
| 721 | /* Aligned */ |
| 722 | return 1; |
| 723 | } |
| 724 | |
Marek Vasut | fee0c68 | 2019-05-19 03:47:07 +0200 | [diff] [blame] | 725 | static int renesas_sdhi_send_cmd(struct udevice *dev, struct mmc_cmd *cmd, |
| 726 | struct mmc_data *data) |
| 727 | { |
Marek Vasut | 9f7baeb | 2020-04-04 12:45:04 +0200 | [diff] [blame^] | 728 | struct bounce_buffer bbstate; |
| 729 | unsigned int bbflags; |
| 730 | bool bbok = false; |
| 731 | size_t len; |
| 732 | void *buf; |
Marek Vasut | fee0c68 | 2019-05-19 03:47:07 +0200 | [diff] [blame] | 733 | int ret; |
| 734 | |
Marek Vasut | 9f7baeb | 2020-04-04 12:45:04 +0200 | [diff] [blame^] | 735 | if (data) { |
| 736 | if (data->flags & MMC_DATA_READ) { |
| 737 | buf = data->dest; |
| 738 | bbflags = GEN_BB_WRITE; |
| 739 | } else { |
| 740 | buf = (void *)data->src; |
| 741 | bbflags = GEN_BB_READ; |
| 742 | } |
| 743 | len = data->blocks * data->blocksize; |
| 744 | |
| 745 | ret = bounce_buffer_start_extalign(&bbstate, buf, len, bbflags, |
| 746 | RENESAS_SDHI_DMA_ALIGNMENT, |
| 747 | renesas_sdhi_addr_aligned); |
| 748 | /* |
| 749 | * If the amount of data to transfer is too large, we can get |
| 750 | * -ENOMEM when starting the bounce buffer. If that happens, |
| 751 | * fall back to PIO as it was before, otherwise use the BB. |
| 752 | */ |
| 753 | if (!ret) { |
| 754 | bbok = true; |
| 755 | if (data->flags & MMC_DATA_READ) |
| 756 | data->dest = bbstate.bounce_buffer; |
| 757 | else |
| 758 | data->src = bbstate.bounce_buffer; |
| 759 | } |
| 760 | } |
| 761 | |
Marek Vasut | fee0c68 | 2019-05-19 03:47:07 +0200 | [diff] [blame] | 762 | ret = tmio_sd_send_cmd(dev, cmd, data); |
Marek Vasut | 9f7baeb | 2020-04-04 12:45:04 +0200 | [diff] [blame^] | 763 | |
| 764 | if (data && bbok) { |
| 765 | buf = bbstate.user_buffer; |
| 766 | |
| 767 | bounce_buffer_stop(&bbstate); |
| 768 | |
| 769 | if (data->flags & MMC_DATA_READ) |
| 770 | data->dest = buf; |
| 771 | else |
| 772 | data->src = buf; |
| 773 | } |
| 774 | |
Marek Vasut | fee0c68 | 2019-05-19 03:47:07 +0200 | [diff] [blame] | 775 | if (ret) |
| 776 | return ret; |
| 777 | |
| 778 | #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \ |
| 779 | CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \ |
| 780 | CONFIG_IS_ENABLED(MMC_HS400_SUPPORT) |
| 781 | struct tmio_sd_priv *priv = dev_get_priv(dev); |
| 782 | |
Marek Vasut | 52647a0 | 2019-11-23 13:36:23 +0100 | [diff] [blame] | 783 | renesas_sdhi_check_scc_error(dev); |
| 784 | |
Marek Vasut | fee0c68 | 2019-05-19 03:47:07 +0200 | [diff] [blame] | 785 | if (cmd->cmdidx == MMC_CMD_SEND_STATUS) |
| 786 | renesas_sdhi_adjust_hs400_mode_enable(priv); |
| 787 | #endif |
| 788 | |
| 789 | return 0; |
| 790 | } |
| 791 | |
Marek Vasut | 06485cf | 2018-04-08 15:22:58 +0200 | [diff] [blame] | 792 | static const struct dm_mmc_ops renesas_sdhi_ops = { |
Marek Vasut | fee0c68 | 2019-05-19 03:47:07 +0200 | [diff] [blame] | 793 | .send_cmd = renesas_sdhi_send_cmd, |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 794 | .set_ios = renesas_sdhi_set_ios, |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 795 | .get_cd = tmio_sd_get_cd, |
Marek Vasut | 10d77ed | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 796 | #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \ |
| 797 | CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \ |
| 798 | CONFIG_IS_ENABLED(MMC_HS400_SUPPORT) |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 799 | .execute_tuning = renesas_sdhi_execute_tuning, |
| 800 | #endif |
Marek Vasut | 576a6d9 | 2018-10-28 19:28:56 +0100 | [diff] [blame] | 801 | #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) |
| 802 | .wait_dat0 = renesas_sdhi_wait_dat0, |
| 803 | #endif |
Marek Vasut | 06485cf | 2018-04-08 15:22:58 +0200 | [diff] [blame] | 804 | }; |
| 805 | |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 806 | #define RENESAS_GEN2_QUIRKS TMIO_SD_CAP_RCAR_GEN2 |
Marek Vasut | 9db9e6a | 2018-04-08 18:49:52 +0200 | [diff] [blame] | 807 | #define RENESAS_GEN3_QUIRKS \ |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 808 | TMIO_SD_CAP_64BIT | TMIO_SD_CAP_RCAR_GEN3 | TMIO_SD_CAP_RCAR_UHS |
Marek Vasut | 9db9e6a | 2018-04-08 18:49:52 +0200 | [diff] [blame] | 809 | |
Marek Vasut | 06485cf | 2018-04-08 15:22:58 +0200 | [diff] [blame] | 810 | static const struct udevice_id renesas_sdhi_match[] = { |
Marek Vasut | 9db9e6a | 2018-04-08 18:49:52 +0200 | [diff] [blame] | 811 | { .compatible = "renesas,sdhi-r8a7790", .data = RENESAS_GEN2_QUIRKS }, |
| 812 | { .compatible = "renesas,sdhi-r8a7791", .data = RENESAS_GEN2_QUIRKS }, |
| 813 | { .compatible = "renesas,sdhi-r8a7792", .data = RENESAS_GEN2_QUIRKS }, |
| 814 | { .compatible = "renesas,sdhi-r8a7793", .data = RENESAS_GEN2_QUIRKS }, |
| 815 | { .compatible = "renesas,sdhi-r8a7794", .data = RENESAS_GEN2_QUIRKS }, |
| 816 | { .compatible = "renesas,sdhi-r8a7795", .data = RENESAS_GEN3_QUIRKS }, |
| 817 | { .compatible = "renesas,sdhi-r8a7796", .data = RENESAS_GEN3_QUIRKS }, |
| 818 | { .compatible = "renesas,sdhi-r8a77965", .data = RENESAS_GEN3_QUIRKS }, |
| 819 | { .compatible = "renesas,sdhi-r8a77970", .data = RENESAS_GEN3_QUIRKS }, |
Marek Vasut | 8b2ae7d | 2018-04-26 13:19:29 +0200 | [diff] [blame] | 820 | { .compatible = "renesas,sdhi-r8a77990", .data = RENESAS_GEN3_QUIRKS }, |
Marek Vasut | 9db9e6a | 2018-04-08 18:49:52 +0200 | [diff] [blame] | 821 | { .compatible = "renesas,sdhi-r8a77995", .data = RENESAS_GEN3_QUIRKS }, |
Marek Vasut | 06485cf | 2018-04-08 15:22:58 +0200 | [diff] [blame] | 822 | { /* sentinel */ } |
| 823 | }; |
| 824 | |
Marek Vasut | da90a1b | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 825 | static ulong renesas_sdhi_clk_get_rate(struct tmio_sd_priv *priv) |
| 826 | { |
| 827 | return clk_get_rate(&priv->clk); |
| 828 | } |
| 829 | |
Marek Vasut | b59180a | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 830 | static void renesas_sdhi_filter_caps(struct udevice *dev) |
| 831 | { |
Marek Vasut | b59180a | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 832 | struct tmio_sd_priv *priv = dev_get_priv(dev); |
| 833 | |
| 834 | if (!(priv->caps & TMIO_SD_CAP_RCAR_GEN3)) |
| 835 | return; |
| 836 | |
Marek Vasut | 7b46176 | 2019-11-23 13:36:25 +0100 | [diff] [blame] | 837 | #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \ |
| 838 | CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \ |
| 839 | CONFIG_IS_ENABLED(MMC_HS400_SUPPORT) |
| 840 | struct tmio_sd_plat *plat = dev_get_platdata(dev); |
| 841 | |
| 842 | /* HS400 is not supported on H3 ES1.x and M3W ES1.0, ES1.1 */ |
Marek Vasut | b59180a | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 843 | if (((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7795) && |
| 844 | (rmobile_get_cpu_rev_integer() <= 1)) || |
| 845 | ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7796) && |
| 846 | (rmobile_get_cpu_rev_integer() == 1) && |
Marek Vasut | 7b46176 | 2019-11-23 13:36:25 +0100 | [diff] [blame] | 847 | (rmobile_get_cpu_rev_fraction() < 2))) |
Marek Vasut | b59180a | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 848 | plat->cfg.host_caps &= ~MMC_MODE_HS400; |
Marek Vasut | 10d77ed | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 849 | |
Marek Vasut | 531fc99 | 2019-11-23 13:36:24 +0100 | [diff] [blame] | 850 | /* H3 ES2.0, ES3.0 and M3W ES1.2 and M3N bad taps */ |
| 851 | if (((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7795) && |
| 852 | (rmobile_get_cpu_rev_integer() >= 2)) || |
| 853 | ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7796) && |
| 854 | (rmobile_get_cpu_rev_integer() == 1) && |
| 855 | (rmobile_get_cpu_rev_fraction() == 2)) || |
| 856 | (rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A77965)) |
| 857 | priv->hs400_bad_tap = BIT(2) | BIT(3) | BIT(6) | BIT(7); |
| 858 | |
Marek Vasut | 7b46176 | 2019-11-23 13:36:25 +0100 | [diff] [blame] | 859 | /* H3 ES3.0 can use HS400 with manual adjustment */ |
| 860 | if ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7795) && |
| 861 | (rmobile_get_cpu_rev_integer() >= 3)) { |
| 862 | priv->adjust_hs400_enable = true; |
| 863 | priv->adjust_hs400_offset = 0; |
| 864 | priv->adjust_hs400_calib_table = |
| 865 | r8a7795_calib_table[!rmobile_is_gen3_mmc0(priv)]; |
| 866 | } |
| 867 | |
| 868 | /* M3W ES1.2 can use HS400 with manual adjustment */ |
| 869 | if ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7796) && |
| 870 | (rmobile_get_cpu_rev_integer() == 1) && |
| 871 | (rmobile_get_cpu_rev_fraction() == 2)) { |
| 872 | priv->adjust_hs400_enable = true; |
| 873 | priv->adjust_hs400_offset = 3; |
| 874 | priv->adjust_hs400_calib_table = |
| 875 | r8a7796_rev1_calib_table[!rmobile_is_gen3_mmc0(priv)]; |
| 876 | } |
| 877 | |
Marek Vasut | 531fc99 | 2019-11-23 13:36:24 +0100 | [diff] [blame] | 878 | /* M3W ES1.x for x>2 can use HS400 with manual adjustment and taps */ |
Marek Vasut | fee0c68 | 2019-05-19 03:47:07 +0200 | [diff] [blame] | 879 | if ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7796) && |
| 880 | (rmobile_get_cpu_rev_integer() == 1) && |
| 881 | (rmobile_get_cpu_rev_fraction() > 2)) { |
| 882 | priv->adjust_hs400_enable = true; |
Marek Vasut | 7b46176 | 2019-11-23 13:36:25 +0100 | [diff] [blame] | 883 | priv->adjust_hs400_offset = 0; |
Marek Vasut | 531fc99 | 2019-11-23 13:36:24 +0100 | [diff] [blame] | 884 | priv->hs400_bad_tap = BIT(1) | BIT(3) | BIT(5) | BIT(7); |
Marek Vasut | 7b46176 | 2019-11-23 13:36:25 +0100 | [diff] [blame] | 885 | priv->adjust_hs400_calib_table = |
| 886 | r8a7796_rev3_calib_table[!rmobile_is_gen3_mmc0(priv)]; |
Marek Vasut | fee0c68 | 2019-05-19 03:47:07 +0200 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | /* M3N can use HS400 with manual adjustment */ |
| 890 | if (rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A77965) { |
| 891 | priv->adjust_hs400_enable = true; |
Marek Vasut | cf643b0 | 2019-11-23 13:36:21 +0100 | [diff] [blame] | 892 | priv->adjust_hs400_offset = 3; |
Marek Vasut | 7b46176 | 2019-11-23 13:36:25 +0100 | [diff] [blame] | 893 | priv->adjust_hs400_calib_table = |
| 894 | r8a77965_calib_table[!rmobile_is_gen3_mmc0(priv)]; |
Marek Vasut | fee0c68 | 2019-05-19 03:47:07 +0200 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | /* E3 can use HS400 with manual adjustment */ |
| 898 | if (rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A77990) { |
| 899 | priv->adjust_hs400_enable = true; |
Marek Vasut | cf643b0 | 2019-11-23 13:36:21 +0100 | [diff] [blame] | 900 | priv->adjust_hs400_offset = 3; |
Marek Vasut | 7b46176 | 2019-11-23 13:36:25 +0100 | [diff] [blame] | 901 | priv->adjust_hs400_calib_table = |
| 902 | r8a77990_calib_table[!rmobile_is_gen3_mmc0(priv)]; |
Marek Vasut | fee0c68 | 2019-05-19 03:47:07 +0200 | [diff] [blame] | 903 | } |
| 904 | |
Marek Vasut | 715b325 | 2019-11-23 13:36:19 +0100 | [diff] [blame] | 905 | /* H3 ES1.x, ES2.0 and M3W ES1.0, ES1.1, ES1.2 uses 4 tuning taps */ |
| 906 | if (((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7795) && |
| 907 | (rmobile_get_cpu_rev_integer() <= 2)) || |
| 908 | ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7796) && |
| 909 | (rmobile_get_cpu_rev_integer() == 1) && |
| 910 | (rmobile_get_cpu_rev_fraction() <= 2))) |
Marek Vasut | 10d77ed | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 911 | priv->nrtaps = 4; |
| 912 | else |
| 913 | priv->nrtaps = 8; |
Marek Vasut | 7b46176 | 2019-11-23 13:36:25 +0100 | [diff] [blame] | 914 | #endif |
Marek Vasut | e9a2822 | 2019-01-11 23:45:54 +0100 | [diff] [blame] | 915 | /* H3 ES1.x and M3W ES1.0 uses bit 17 for DTRAEND */ |
| 916 | if (((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7795) && |
| 917 | (rmobile_get_cpu_rev_integer() <= 1)) || |
| 918 | ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7796) && |
| 919 | (rmobile_get_cpu_rev_integer() == 1) && |
| 920 | (rmobile_get_cpu_rev_fraction() == 0))) |
| 921 | priv->read_poll_flag = TMIO_SD_DMA_INFO1_END_RD; |
| 922 | else |
| 923 | priv->read_poll_flag = TMIO_SD_DMA_INFO1_END_RD2; |
Marek Vasut | b59180a | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 924 | } |
| 925 | |
Marek Vasut | abe3e95 | 2018-04-08 17:45:23 +0200 | [diff] [blame] | 926 | static int renesas_sdhi_probe(struct udevice *dev) |
| 927 | { |
Masahiro Yamada | 19989d83 | 2018-04-20 18:14:24 +0900 | [diff] [blame] | 928 | struct tmio_sd_priv *priv = dev_get_priv(dev); |
Marek Vasut | abe3e95 | 2018-04-08 17:45:23 +0200 | [diff] [blame] | 929 | u32 quirks = dev_get_driver_data(dev); |
Marek Vasut | 1949d48 | 2018-04-08 18:14:22 +0200 | [diff] [blame] | 930 | struct fdt_resource reg_res; |
| 931 | DECLARE_GLOBAL_DATA_PTR; |
| 932 | int ret; |
| 933 | |
Marek Vasut | da90a1b | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 934 | priv->clk_get_rate = renesas_sdhi_clk_get_rate; |
| 935 | |
Marek Vasut | 9db9e6a | 2018-04-08 18:49:52 +0200 | [diff] [blame] | 936 | if (quirks == RENESAS_GEN2_QUIRKS) { |
| 937 | ret = fdt_get_resource(gd->fdt_blob, dev_of_offset(dev), |
| 938 | "reg", 0, ®_res); |
| 939 | if (ret < 0) { |
| 940 | dev_err(dev, "\"reg\" resource not found, ret=%i\n", |
| 941 | ret); |
| 942 | return ret; |
| 943 | } |
Marek Vasut | 1949d48 | 2018-04-08 18:14:22 +0200 | [diff] [blame] | 944 | |
Marek Vasut | 9db9e6a | 2018-04-08 18:49:52 +0200 | [diff] [blame] | 945 | if (fdt_resource_size(®_res) == 0x100) |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 946 | quirks |= TMIO_SD_CAP_16BIT; |
Marek Vasut | 9db9e6a | 2018-04-08 18:49:52 +0200 | [diff] [blame] | 947 | } |
Marek Vasut | abe3e95 | 2018-04-08 17:45:23 +0200 | [diff] [blame] | 948 | |
Marek Vasut | da90a1b | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 949 | ret = clk_get_by_index(dev, 0, &priv->clk); |
Masahiro Yamada | 19989d83 | 2018-04-20 18:14:24 +0900 | [diff] [blame] | 950 | if (ret < 0) { |
| 951 | dev_err(dev, "failed to get host clock\n"); |
| 952 | return ret; |
| 953 | } |
| 954 | |
| 955 | /* set to max rate */ |
Marek Vasut | da90a1b | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 956 | ret = clk_set_rate(&priv->clk, 200000000); |
| 957 | if (ret < 0) { |
Masahiro Yamada | 19989d83 | 2018-04-20 18:14:24 +0900 | [diff] [blame] | 958 | dev_err(dev, "failed to set rate for host clock\n"); |
Marek Vasut | da90a1b | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 959 | clk_free(&priv->clk); |
| 960 | return ret; |
Masahiro Yamada | 19989d83 | 2018-04-20 18:14:24 +0900 | [diff] [blame] | 961 | } |
| 962 | |
Marek Vasut | da90a1b | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 963 | ret = clk_enable(&priv->clk); |
Masahiro Yamada | 19989d83 | 2018-04-20 18:14:24 +0900 | [diff] [blame] | 964 | if (ret) { |
| 965 | dev_err(dev, "failed to enable host clock\n"); |
| 966 | return ret; |
| 967 | } |
| 968 | |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 969 | ret = tmio_sd_probe(dev, quirks); |
Marek Vasut | b59180a | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 970 | |
| 971 | renesas_sdhi_filter_caps(dev); |
| 972 | |
Marek Vasut | 10d77ed | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 973 | #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \ |
| 974 | CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \ |
| 975 | CONFIG_IS_ENABLED(MMC_HS400_SUPPORT) |
Marek Vasut | 3577380 | 2018-10-28 15:30:06 +0100 | [diff] [blame] | 976 | if (!ret && (priv->caps & TMIO_SD_CAP_RCAR_UHS)) |
Marek Vasut | da70ebb | 2018-08-30 15:27:26 +0200 | [diff] [blame] | 977 | renesas_sdhi_reset_tuning(priv); |
Marek Vasut | e0781e4 | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 978 | #endif |
| 979 | return ret; |
Marek Vasut | abe3e95 | 2018-04-08 17:45:23 +0200 | [diff] [blame] | 980 | } |
| 981 | |
Marek Vasut | 06485cf | 2018-04-08 15:22:58 +0200 | [diff] [blame] | 982 | U_BOOT_DRIVER(renesas_sdhi) = { |
| 983 | .name = "renesas-sdhi", |
| 984 | .id = UCLASS_MMC, |
| 985 | .of_match = renesas_sdhi_match, |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 986 | .bind = tmio_sd_bind, |
Marek Vasut | abe3e95 | 2018-04-08 17:45:23 +0200 | [diff] [blame] | 987 | .probe = renesas_sdhi_probe, |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 988 | .priv_auto_alloc_size = sizeof(struct tmio_sd_priv), |
| 989 | .platdata_auto_alloc_size = sizeof(struct tmio_sd_plat), |
Marek Vasut | 06485cf | 2018-04-08 15:22:58 +0200 | [diff] [blame] | 990 | .ops = &renesas_sdhi_ops, |
| 991 | }; |