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