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