Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Marek Vasut | e94cad9 | 2018-04-08 15:22:58 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2018 Marek Vasut <marek.vasut@gmail.com> |
Marek Vasut | e94cad9 | 2018-04-08 15:22:58 +0200 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <clk.h> |
| 8 | #include <fdtdec.h> |
| 9 | #include <mmc.h> |
| 10 | #include <dm.h> |
| 11 | #include <linux/compat.h> |
| 12 | #include <linux/dma-direction.h> |
| 13 | #include <linux/io.h> |
| 14 | #include <linux/sizes.h> |
| 15 | #include <power/regulator.h> |
| 16 | #include <asm/unaligned.h> |
| 17 | |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 18 | #include "tmio-common.h" |
Marek Vasut | e94cad9 | 2018-04-08 15:22:58 +0200 | [diff] [blame] | 19 | |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 20 | #if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) |
| 21 | |
| 22 | /* SCC registers */ |
| 23 | #define RENESAS_SDHI_SCC_DTCNTL 0x800 |
| 24 | #define RENESAS_SDHI_SCC_DTCNTL_TAPEN BIT(0) |
| 25 | #define RENESAS_SDHI_SCC_DTCNTL_TAPNUM_SHIFT 16 |
| 26 | #define RENESAS_SDHI_SCC_DTCNTL_TAPNUM_MASK 0xff |
| 27 | #define RENESAS_SDHI_SCC_TAPSET 0x804 |
| 28 | #define RENESAS_SDHI_SCC_DT2FF 0x808 |
| 29 | #define RENESAS_SDHI_SCC_CKSEL 0x80c |
| 30 | #define RENESAS_SDHI_SCC_CKSEL_DTSEL BIT(0) |
| 31 | #define RENESAS_SDHI_SCC_RVSCNTL 0x810 |
| 32 | #define RENESAS_SDHI_SCC_RVSCNTL_RVSEN BIT(0) |
| 33 | #define RENESAS_SDHI_SCC_RVSREQ 0x814 |
| 34 | #define RENESAS_SDHI_SCC_RVSREQ_RVSERR BIT(2) |
| 35 | #define RENESAS_SDHI_SCC_SMPCMP 0x818 |
| 36 | #define RENESAS_SDHI_SCC_TMPPORT2 0x81c |
Marek Vasut | dc1488f | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 37 | #define RENESAS_SDHI_SCC_TMPPORT2_HS400EN BIT(31) |
| 38 | #define RENESAS_SDHI_SCC_TMPPORT2_HS400OSEL BIT(4) |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 39 | |
| 40 | #define RENESAS_SDHI_MAX_TAP 3 |
| 41 | |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 42 | static unsigned int renesas_sdhi_init_tuning(struct tmio_sd_priv *priv) |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 43 | { |
| 44 | u32 reg; |
| 45 | |
| 46 | /* Initialize SCC */ |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 47 | tmio_sd_writel(priv, 0, TMIO_SD_INFO1); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 48 | |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 49 | reg = tmio_sd_readl(priv, TMIO_SD_CLKCTL); |
| 50 | reg &= ~TMIO_SD_CLKCTL_SCLKEN; |
| 51 | tmio_sd_writel(priv, reg, TMIO_SD_CLKCTL); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 52 | |
| 53 | /* Set sampling clock selection range */ |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 54 | tmio_sd_writel(priv, 0x8 << RENESAS_SDHI_SCC_DTCNTL_TAPNUM_SHIFT, |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 55 | RENESAS_SDHI_SCC_DTCNTL); |
| 56 | |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 57 | reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_DTCNTL); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 58 | reg |= RENESAS_SDHI_SCC_DTCNTL_TAPEN; |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 59 | tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_DTCNTL); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 60 | |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 61 | reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_CKSEL); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 62 | reg |= RENESAS_SDHI_SCC_CKSEL_DTSEL; |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 63 | tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_CKSEL); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 64 | |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 65 | reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSCNTL); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 66 | reg &= ~RENESAS_SDHI_SCC_RVSCNTL_RVSEN; |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 67 | tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_RVSCNTL); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 68 | |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 69 | tmio_sd_writel(priv, 0x300 /* scc_tappos */, |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 70 | RENESAS_SDHI_SCC_DT2FF); |
| 71 | |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 72 | reg = tmio_sd_readl(priv, TMIO_SD_CLKCTL); |
| 73 | reg |= TMIO_SD_CLKCTL_SCLKEN; |
| 74 | tmio_sd_writel(priv, reg, TMIO_SD_CLKCTL); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 75 | |
| 76 | /* Read TAPNUM */ |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 77 | return (tmio_sd_readl(priv, RENESAS_SDHI_SCC_DTCNTL) >> |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 78 | RENESAS_SDHI_SCC_DTCNTL_TAPNUM_SHIFT) & |
| 79 | RENESAS_SDHI_SCC_DTCNTL_TAPNUM_MASK; |
| 80 | } |
| 81 | |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 82 | static void renesas_sdhi_reset_tuning(struct tmio_sd_priv *priv) |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 83 | { |
| 84 | u32 reg; |
| 85 | |
| 86 | /* Reset SCC */ |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 87 | reg = tmio_sd_readl(priv, TMIO_SD_CLKCTL); |
| 88 | reg &= ~TMIO_SD_CLKCTL_SCLKEN; |
| 89 | tmio_sd_writel(priv, reg, TMIO_SD_CLKCTL); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 90 | |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 91 | reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_CKSEL); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 92 | reg &= ~RENESAS_SDHI_SCC_CKSEL_DTSEL; |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 93 | tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_CKSEL); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 94 | |
Marek Vasut | dc1488f | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 95 | reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_TMPPORT2); |
| 96 | reg &= ~(RENESAS_SDHI_SCC_TMPPORT2_HS400EN | |
| 97 | RENESAS_SDHI_SCC_TMPPORT2_HS400OSEL); |
| 98 | tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_TMPPORT2); |
| 99 | |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 100 | reg = tmio_sd_readl(priv, TMIO_SD_CLKCTL); |
| 101 | reg |= TMIO_SD_CLKCTL_SCLKEN; |
| 102 | tmio_sd_writel(priv, reg, TMIO_SD_CLKCTL); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 103 | |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 104 | reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSCNTL); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 105 | reg &= ~RENESAS_SDHI_SCC_RVSCNTL_RVSEN; |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 106 | tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_RVSCNTL); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 107 | |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 108 | reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSCNTL); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 109 | reg &= ~RENESAS_SDHI_SCC_RVSCNTL_RVSEN; |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 110 | tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_RVSCNTL); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 111 | } |
| 112 | |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 113 | static void renesas_sdhi_prepare_tuning(struct tmio_sd_priv *priv, |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 114 | unsigned long tap) |
| 115 | { |
| 116 | /* Set sampling clock position */ |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 117 | tmio_sd_writel(priv, tap, RENESAS_SDHI_SCC_TAPSET); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 118 | } |
| 119 | |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 120 | 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] | 121 | { |
| 122 | /* Get comparison of sampling data */ |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 123 | return tmio_sd_readl(priv, RENESAS_SDHI_SCC_SMPCMP); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 124 | } |
| 125 | |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 126 | static int renesas_sdhi_select_tuning(struct tmio_sd_priv *priv, |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 127 | unsigned int tap_num, unsigned int taps, |
| 128 | unsigned int smpcmp) |
| 129 | { |
| 130 | unsigned long tap_cnt; /* counter of tuning success */ |
| 131 | unsigned long tap_set; /* tap position */ |
| 132 | unsigned long tap_start;/* start position of tuning success */ |
| 133 | unsigned long tap_end; /* end position of tuning success */ |
| 134 | unsigned long ntap; /* temporary counter of tuning success */ |
| 135 | unsigned long match_cnt;/* counter of matching data */ |
| 136 | unsigned long i; |
| 137 | bool select = false; |
| 138 | u32 reg; |
| 139 | |
| 140 | /* Clear SCC_RVSREQ */ |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 141 | tmio_sd_writel(priv, 0, RENESAS_SDHI_SCC_RVSREQ); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 142 | |
| 143 | /* Merge the results */ |
| 144 | for (i = 0; i < tap_num * 2; i++) { |
| 145 | if (!(taps & BIT(i))) { |
| 146 | taps &= ~BIT(i % tap_num); |
| 147 | taps &= ~BIT((i % tap_num) + tap_num); |
| 148 | } |
| 149 | if (!(smpcmp & BIT(i))) { |
| 150 | smpcmp &= ~BIT(i % tap_num); |
| 151 | smpcmp &= ~BIT((i % tap_num) + tap_num); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * Find the longest consecutive run of successful probes. If that |
| 157 | * is more than RENESAS_SDHI_MAX_TAP probes long then use the |
| 158 | * center index as the tap. |
| 159 | */ |
| 160 | tap_cnt = 0; |
| 161 | ntap = 0; |
| 162 | tap_start = 0; |
| 163 | tap_end = 0; |
| 164 | for (i = 0; i < tap_num * 2; i++) { |
| 165 | if (taps & BIT(i)) |
| 166 | ntap++; |
| 167 | else { |
| 168 | if (ntap > tap_cnt) { |
| 169 | tap_start = i - ntap; |
| 170 | tap_end = i - 1; |
| 171 | tap_cnt = ntap; |
| 172 | } |
| 173 | ntap = 0; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | if (ntap > tap_cnt) { |
| 178 | tap_start = i - ntap; |
| 179 | tap_end = i - 1; |
| 180 | tap_cnt = ntap; |
| 181 | } |
| 182 | |
| 183 | /* |
| 184 | * If all of the TAP is OK, the sampling clock position is selected by |
| 185 | * identifying the change point of data. |
| 186 | */ |
| 187 | if (tap_cnt == tap_num * 2) { |
| 188 | match_cnt = 0; |
| 189 | ntap = 0; |
| 190 | tap_start = 0; |
| 191 | tap_end = 0; |
| 192 | for (i = 0; i < tap_num * 2; i++) { |
| 193 | if (smpcmp & BIT(i)) |
| 194 | ntap++; |
| 195 | else { |
| 196 | if (ntap > match_cnt) { |
| 197 | tap_start = i - ntap; |
| 198 | tap_end = i - 1; |
| 199 | match_cnt = ntap; |
| 200 | } |
| 201 | ntap = 0; |
| 202 | } |
| 203 | } |
| 204 | if (ntap > match_cnt) { |
| 205 | tap_start = i - ntap; |
| 206 | tap_end = i - 1; |
| 207 | match_cnt = ntap; |
| 208 | } |
| 209 | if (match_cnt) |
| 210 | select = true; |
| 211 | } else if (tap_cnt >= RENESAS_SDHI_MAX_TAP) |
| 212 | select = true; |
| 213 | |
| 214 | if (select) |
| 215 | tap_set = ((tap_start + tap_end) / 2) % tap_num; |
| 216 | else |
| 217 | return -EIO; |
| 218 | |
| 219 | /* Set SCC */ |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 220 | tmio_sd_writel(priv, tap_set, RENESAS_SDHI_SCC_TAPSET); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 221 | |
| 222 | /* Enable auto re-tuning */ |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 223 | reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSCNTL); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 224 | reg |= RENESAS_SDHI_SCC_RVSCNTL_RVSEN; |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 225 | tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_RVSCNTL); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 226 | |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | int renesas_sdhi_execute_tuning(struct udevice *dev, uint opcode) |
| 231 | { |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 232 | struct tmio_sd_priv *priv = dev_get_priv(dev); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 233 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
| 234 | struct mmc *mmc = upriv->mmc; |
| 235 | unsigned int tap_num; |
| 236 | unsigned int taps = 0, smpcmp = 0; |
| 237 | int i, ret = 0; |
| 238 | u32 caps; |
| 239 | |
| 240 | /* Only supported on Renesas RCar */ |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 241 | if (!(priv->caps & TMIO_SD_CAP_RCAR_UHS)) |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 242 | return -EINVAL; |
| 243 | |
| 244 | /* clock tuning is not needed for upto 52MHz */ |
| 245 | if (!((mmc->selected_mode == MMC_HS_200) || |
| 246 | (mmc->selected_mode == UHS_SDR104) || |
| 247 | (mmc->selected_mode == UHS_SDR50))) |
| 248 | return 0; |
| 249 | |
| 250 | tap_num = renesas_sdhi_init_tuning(priv); |
| 251 | if (!tap_num) |
| 252 | /* Tuning is not supported */ |
| 253 | goto out; |
| 254 | |
| 255 | if (tap_num * 2 >= sizeof(taps) * 8) { |
| 256 | dev_err(dev, |
| 257 | "Too many taps, skipping tuning. Please consider updating size of taps field of tmio_mmc_host\n"); |
| 258 | goto out; |
| 259 | } |
| 260 | |
| 261 | /* Issue CMD19 twice for each tap */ |
| 262 | for (i = 0; i < 2 * tap_num; i++) { |
| 263 | renesas_sdhi_prepare_tuning(priv, i % tap_num); |
| 264 | |
| 265 | /* Force PIO for the tuning */ |
| 266 | caps = priv->caps; |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 267 | priv->caps &= ~TMIO_SD_CAP_DMA_INTERNAL; |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 268 | |
| 269 | ret = mmc_send_tuning(mmc, opcode, NULL); |
| 270 | |
| 271 | priv->caps = caps; |
| 272 | |
| 273 | if (ret == 0) |
| 274 | taps |= BIT(i); |
| 275 | |
| 276 | ret = renesas_sdhi_compare_scc_data(priv); |
| 277 | if (ret == 0) |
| 278 | smpcmp |= BIT(i); |
| 279 | |
| 280 | mdelay(1); |
| 281 | } |
| 282 | |
| 283 | ret = renesas_sdhi_select_tuning(priv, tap_num, taps, smpcmp); |
| 284 | |
| 285 | out: |
| 286 | if (ret < 0) { |
| 287 | dev_warn(dev, "Tuning procedure failed\n"); |
| 288 | renesas_sdhi_reset_tuning(priv); |
| 289 | } |
| 290 | |
| 291 | return ret; |
| 292 | } |
| 293 | #endif |
| 294 | |
| 295 | static int renesas_sdhi_set_ios(struct udevice *dev) |
| 296 | { |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 297 | int ret = tmio_sd_set_ios(dev); |
Marek Vasut | cf39f3f | 2018-04-09 20:47:31 +0200 | [diff] [blame] | 298 | |
| 299 | mdelay(10); |
| 300 | |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 301 | #if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 302 | struct tmio_sd_priv *priv = dev_get_priv(dev); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 303 | |
Marek Vasut | 52e1796 | 2018-10-28 15:30:06 +0100 | [diff] [blame] | 304 | if (priv->caps & TMIO_SD_CAP_RCAR_UHS) |
| 305 | renesas_sdhi_reset_tuning(priv); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 306 | #endif |
| 307 | |
| 308 | return ret; |
| 309 | } |
| 310 | |
Marek Vasut | 2fc1075 | 2018-10-28 19:28:56 +0100 | [diff] [blame^] | 311 | #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) |
| 312 | static int renesas_sdhi_wait_dat0(struct udevice *dev, int state, int timeout) |
| 313 | { |
| 314 | int ret = -ETIMEDOUT; |
| 315 | bool dat0_high; |
| 316 | bool target_dat0_high = !!state; |
| 317 | struct tmio_sd_priv *priv = dev_get_priv(dev); |
| 318 | |
| 319 | timeout = DIV_ROUND_UP(timeout, 10); /* check every 10 us. */ |
| 320 | while (timeout--) { |
| 321 | dat0_high = !!(tmio_sd_readl(priv, TMIO_SD_INFO2) & TMIO_SD_INFO2_DAT0); |
| 322 | if (dat0_high == target_dat0_high) { |
| 323 | ret = 0; |
| 324 | break; |
| 325 | } |
| 326 | udelay(10); |
| 327 | } |
| 328 | |
| 329 | return ret; |
| 330 | } |
| 331 | #endif |
| 332 | |
Marek Vasut | e94cad9 | 2018-04-08 15:22:58 +0200 | [diff] [blame] | 333 | static const struct dm_mmc_ops renesas_sdhi_ops = { |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 334 | .send_cmd = tmio_sd_send_cmd, |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 335 | .set_ios = renesas_sdhi_set_ios, |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 336 | .get_cd = tmio_sd_get_cd, |
Marek Vasut | 2fc1075 | 2018-10-28 19:28:56 +0100 | [diff] [blame^] | 337 | #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 338 | .execute_tuning = renesas_sdhi_execute_tuning, |
| 339 | #endif |
Marek Vasut | 2fc1075 | 2018-10-28 19:28:56 +0100 | [diff] [blame^] | 340 | #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) |
| 341 | .wait_dat0 = renesas_sdhi_wait_dat0, |
| 342 | #endif |
Marek Vasut | e94cad9 | 2018-04-08 15:22:58 +0200 | [diff] [blame] | 343 | }; |
| 344 | |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 345 | #define RENESAS_GEN2_QUIRKS TMIO_SD_CAP_RCAR_GEN2 |
Marek Vasut | f98833d | 2018-04-08 18:49:52 +0200 | [diff] [blame] | 346 | #define RENESAS_GEN3_QUIRKS \ |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 347 | 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] | 348 | |
Marek Vasut | e94cad9 | 2018-04-08 15:22:58 +0200 | [diff] [blame] | 349 | static const struct udevice_id renesas_sdhi_match[] = { |
Marek Vasut | f98833d | 2018-04-08 18:49:52 +0200 | [diff] [blame] | 350 | { .compatible = "renesas,sdhi-r8a7790", .data = RENESAS_GEN2_QUIRKS }, |
| 351 | { .compatible = "renesas,sdhi-r8a7791", .data = RENESAS_GEN2_QUIRKS }, |
| 352 | { .compatible = "renesas,sdhi-r8a7792", .data = RENESAS_GEN2_QUIRKS }, |
| 353 | { .compatible = "renesas,sdhi-r8a7793", .data = RENESAS_GEN2_QUIRKS }, |
| 354 | { .compatible = "renesas,sdhi-r8a7794", .data = RENESAS_GEN2_QUIRKS }, |
| 355 | { .compatible = "renesas,sdhi-r8a7795", .data = RENESAS_GEN3_QUIRKS }, |
| 356 | { .compatible = "renesas,sdhi-r8a7796", .data = RENESAS_GEN3_QUIRKS }, |
| 357 | { .compatible = "renesas,sdhi-r8a77965", .data = RENESAS_GEN3_QUIRKS }, |
| 358 | { .compatible = "renesas,sdhi-r8a77970", .data = RENESAS_GEN3_QUIRKS }, |
Marek Vasut | d629152 | 2018-04-26 13:19:29 +0200 | [diff] [blame] | 359 | { .compatible = "renesas,sdhi-r8a77990", .data = RENESAS_GEN3_QUIRKS }, |
Marek Vasut | f98833d | 2018-04-08 18:49:52 +0200 | [diff] [blame] | 360 | { .compatible = "renesas,sdhi-r8a77995", .data = RENESAS_GEN3_QUIRKS }, |
Marek Vasut | e94cad9 | 2018-04-08 15:22:58 +0200 | [diff] [blame] | 361 | { /* sentinel */ } |
| 362 | }; |
| 363 | |
Marek Vasut | c769e60 | 2018-04-08 17:45:23 +0200 | [diff] [blame] | 364 | static int renesas_sdhi_probe(struct udevice *dev) |
| 365 | { |
Masahiro Yamada | 30b5d9a | 2018-04-20 18:14:24 +0900 | [diff] [blame] | 366 | struct tmio_sd_priv *priv = dev_get_priv(dev); |
Marek Vasut | c769e60 | 2018-04-08 17:45:23 +0200 | [diff] [blame] | 367 | u32 quirks = dev_get_driver_data(dev); |
Marek Vasut | 7cf7ef8 | 2018-04-08 18:14:22 +0200 | [diff] [blame] | 368 | struct fdt_resource reg_res; |
Masahiro Yamada | 30b5d9a | 2018-04-20 18:14:24 +0900 | [diff] [blame] | 369 | struct clk clk; |
Marek Vasut | 7cf7ef8 | 2018-04-08 18:14:22 +0200 | [diff] [blame] | 370 | DECLARE_GLOBAL_DATA_PTR; |
| 371 | int ret; |
| 372 | |
Marek Vasut | f98833d | 2018-04-08 18:49:52 +0200 | [diff] [blame] | 373 | if (quirks == RENESAS_GEN2_QUIRKS) { |
| 374 | ret = fdt_get_resource(gd->fdt_blob, dev_of_offset(dev), |
| 375 | "reg", 0, ®_res); |
| 376 | if (ret < 0) { |
| 377 | dev_err(dev, "\"reg\" resource not found, ret=%i\n", |
| 378 | ret); |
| 379 | return ret; |
| 380 | } |
Marek Vasut | 7cf7ef8 | 2018-04-08 18:14:22 +0200 | [diff] [blame] | 381 | |
Marek Vasut | f98833d | 2018-04-08 18:49:52 +0200 | [diff] [blame] | 382 | if (fdt_resource_size(®_res) == 0x100) |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 383 | quirks |= TMIO_SD_CAP_16BIT; |
Marek Vasut | f98833d | 2018-04-08 18:49:52 +0200 | [diff] [blame] | 384 | } |
Marek Vasut | c769e60 | 2018-04-08 17:45:23 +0200 | [diff] [blame] | 385 | |
Masahiro Yamada | 30b5d9a | 2018-04-20 18:14:24 +0900 | [diff] [blame] | 386 | ret = clk_get_by_index(dev, 0, &clk); |
| 387 | if (ret < 0) { |
| 388 | dev_err(dev, "failed to get host clock\n"); |
| 389 | return ret; |
| 390 | } |
| 391 | |
| 392 | /* set to max rate */ |
| 393 | priv->mclk = clk_set_rate(&clk, ULONG_MAX); |
| 394 | if (IS_ERR_VALUE(priv->mclk)) { |
| 395 | dev_err(dev, "failed to set rate for host clock\n"); |
| 396 | clk_free(&clk); |
| 397 | return priv->mclk; |
| 398 | } |
| 399 | |
| 400 | ret = clk_enable(&clk); |
| 401 | clk_free(&clk); |
| 402 | if (ret) { |
| 403 | dev_err(dev, "failed to enable host clock\n"); |
| 404 | return ret; |
| 405 | } |
| 406 | |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 407 | ret = tmio_sd_probe(dev, quirks); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 408 | #if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) |
Marek Vasut | 52e1796 | 2018-10-28 15:30:06 +0100 | [diff] [blame] | 409 | if (!ret && (priv->caps & TMIO_SD_CAP_RCAR_UHS)) |
Marek Vasut | 6518697 | 2018-08-30 15:27:26 +0200 | [diff] [blame] | 410 | renesas_sdhi_reset_tuning(priv); |
Marek Vasut | f63968b | 2018-04-08 19:09:17 +0200 | [diff] [blame] | 411 | #endif |
| 412 | return ret; |
Marek Vasut | c769e60 | 2018-04-08 17:45:23 +0200 | [diff] [blame] | 413 | } |
| 414 | |
Marek Vasut | e94cad9 | 2018-04-08 15:22:58 +0200 | [diff] [blame] | 415 | U_BOOT_DRIVER(renesas_sdhi) = { |
| 416 | .name = "renesas-sdhi", |
| 417 | .id = UCLASS_MMC, |
| 418 | .of_match = renesas_sdhi_match, |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 419 | .bind = tmio_sd_bind, |
Marek Vasut | c769e60 | 2018-04-08 17:45:23 +0200 | [diff] [blame] | 420 | .probe = renesas_sdhi_probe, |
Marek Vasut | cb0b6b0 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 421 | .priv_auto_alloc_size = sizeof(struct tmio_sd_priv), |
| 422 | .platdata_auto_alloc_size = sizeof(struct tmio_sd_plat), |
Marek Vasut | e94cad9 | 2018-04-08 15:22:58 +0200 | [diff] [blame] | 423 | .ops = &renesas_sdhi_ops, |
| 424 | }; |