1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018 Marek Vasut <marek.vasut@gmail.com>
14 #include <dm/device_compat.h>
15 #include <linux/compat.h>
16 #include <linux/dma-direction.h>
18 #include <linux/sizes.h>
19 #include <power/regulator.h>
20 #include <asm/unaligned.h>
22 #include "tmio-common.h"
24 #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
25 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
26 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
29 #define RENESAS_SDHI_SCC_DTCNTL 0x800
30 #define RENESAS_SDHI_SCC_DTCNTL_TAPEN BIT(0)
31 #define RENESAS_SDHI_SCC_DTCNTL_TAPNUM_SHIFT 16
32 #define RENESAS_SDHI_SCC_DTCNTL_TAPNUM_MASK 0xff
33 #define RENESAS_SDHI_SCC_TAPSET 0x804
34 #define RENESAS_SDHI_SCC_DT2FF 0x808
35 #define RENESAS_SDHI_SCC_CKSEL 0x80c
36 #define RENESAS_SDHI_SCC_CKSEL_DTSEL BIT(0)
37 #define RENESAS_SDHI_SCC_RVSCNTL 0x810
38 #define RENESAS_SDHI_SCC_RVSCNTL_RVSEN BIT(0)
39 #define RENESAS_SDHI_SCC_RVSREQ 0x814
40 #define RENESAS_SDHI_SCC_RVSREQ_RVSERR BIT(2)
41 #define RENESAS_SDHI_SCC_RVSREQ_REQTAPUP BIT(1)
42 #define RENESAS_SDHI_SCC_RVSREQ_REQTAPDOWN BIT(0)
43 #define RENESAS_SDHI_SCC_SMPCMP 0x818
44 #define RENESAS_SDHI_SCC_SMPCMP_CMD_ERR (BIT(24) | BIT(8))
45 #define RENESAS_SDHI_SCC_SMPCMP_CMD_REQUP BIT(24)
46 #define RENESAS_SDHI_SCC_SMPCMP_CMD_REQDOWN BIT(8)
47 #define RENESAS_SDHI_SCC_TMPPORT2 0x81c
48 #define RENESAS_SDHI_SCC_TMPPORT2_HS400EN BIT(31)
49 #define RENESAS_SDHI_SCC_TMPPORT2_HS400OSEL BIT(4)
50 #define RENESAS_SDHI_SCC_TMPPORT3 0x828
51 #define RENESAS_SDHI_SCC_TMPPORT3_OFFSET_0 3
52 #define RENESAS_SDHI_SCC_TMPPORT3_OFFSET_1 2
53 #define RENESAS_SDHI_SCC_TMPPORT3_OFFSET_2 1
54 #define RENESAS_SDHI_SCC_TMPPORT3_OFFSET_3 0
55 #define RENESAS_SDHI_SCC_TMPPORT3_OFFSET_MASK 0x3
56 #define RENESAS_SDHI_SCC_TMPPORT4 0x82c
57 #define RENESAS_SDHI_SCC_TMPPORT4_DLL_ACC_START BIT(0)
58 #define RENESAS_SDHI_SCC_TMPPORT5 0x830
59 #define RENESAS_SDHI_SCC_TMPPORT5_DLL_RW_SEL_R BIT(8)
60 #define RENESAS_SDHI_SCC_TMPPORT5_DLL_RW_SEL_W (0 << 8)
61 #define RENESAS_SDHI_SCC_TMPPORT5_DLL_ADR_MASK 0x3F
62 #define RENESAS_SDHI_SCC_TMPPORT6 0x834
63 #define RENESAS_SDHI_SCC_TMPPORT7 0x838
64 #define RENESAS_SDHI_SCC_TMPPORT_DISABLE_WP_CODE 0xa5000000
65 #define RENESAS_SDHI_SCC_TMPPORT_CALIB_CODE_MASK 0x1f
66 #define RENESAS_SDHI_SCC_TMPPORT_MANUAL_MODE BIT(7)
68 #define RENESAS_SDHI_MAX_TAP 3
70 #define CALIB_TABLE_MAX (RENESAS_SDHI_SCC_TMPPORT_CALIB_CODE_MASK + 1)
72 static const u8 r8a7795_calib_table[2][CALIB_TABLE_MAX] = {
73 { 0, 0, 0, 0, 0, 1, 1, 2, 3, 4, 5, 5, 6, 6, 7, 11,
74 15, 16, 16, 17, 17, 17, 17, 17, 18, 18, 18, 18, 19, 20, 21, 21 },
75 { 3, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 9, 10, 11, 12, 15,
76 16, 16, 17, 17, 17, 17, 17, 18, 18, 18, 18, 19, 20, 21, 22, 22 }
79 static const u8 r8a7796_rev1_calib_table[2][CALIB_TABLE_MAX] = {
80 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 3, 4, 9,
81 15, 15, 15, 16, 16, 16, 16, 16, 17, 18, 19, 20, 21, 21, 22, 22 },
82 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
83 2, 9, 16, 17, 17, 17, 18, 18, 18, 18, 19, 20, 21, 22, 23, 24}
86 static const u8 r8a7796_rev3_calib_table[2][CALIB_TABLE_MAX] = {
87 { 0, 0, 0, 0, 2, 3, 4, 4, 5, 6, 7, 7, 8, 9, 9, 10,
88 11, 12, 13, 15, 16, 17, 17, 18, 19, 19, 20, 21, 21, 22, 23, 23 },
89 { 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 9, 9, 10, 11, 12,
90 13, 14, 15, 16, 17, 17, 18, 19, 20, 20, 21, 22, 22, 23, 24, 24 }
93 static const u8 r8a77965_calib_table[2][CALIB_TABLE_MAX] = {
94 { 0, 1, 2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15,
95 16, 17, 18, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 29 },
96 { 0, 1, 2, 2, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 13, 15,
97 16, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 31 }
100 static const u8 r8a77990_calib_table[2][CALIB_TABLE_MAX] = {
101 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
102 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
103 { 0, 0, 1, 2, 3, 4, 4, 4, 4, 5, 5, 6, 7, 8, 10, 11,
104 12, 13, 14, 16, 17, 18, 18, 18, 19, 19, 20, 24, 26, 26, 26, 26 }
107 static int rmobile_is_gen3_mmc0(struct tmio_sd_priv *priv)
109 /* On R-Car Gen3, MMC0 is at 0xee140000 */
110 return (uintptr_t)(priv->regbase) == 0xee140000;
113 static u32 sd_scc_tmpport_read32(struct tmio_sd_priv *priv, u32 addr)
116 tmio_sd_writel(priv, RENESAS_SDHI_SCC_TMPPORT5_DLL_RW_SEL_R |
117 (RENESAS_SDHI_SCC_TMPPORT5_DLL_ADR_MASK & addr),
118 RENESAS_SDHI_SCC_TMPPORT5);
120 /* access start and stop */
121 tmio_sd_writel(priv, RENESAS_SDHI_SCC_TMPPORT4_DLL_ACC_START,
122 RENESAS_SDHI_SCC_TMPPORT4);
123 tmio_sd_writel(priv, 0, RENESAS_SDHI_SCC_TMPPORT4);
125 return tmio_sd_readl(priv, RENESAS_SDHI_SCC_TMPPORT7);
128 static void sd_scc_tmpport_write32(struct tmio_sd_priv *priv, u32 addr, u32 val)
131 tmio_sd_writel(priv, RENESAS_SDHI_SCC_TMPPORT5_DLL_RW_SEL_W |
132 (RENESAS_SDHI_SCC_TMPPORT5_DLL_ADR_MASK & addr),
133 RENESAS_SDHI_SCC_TMPPORT5);
134 tmio_sd_writel(priv, val, RENESAS_SDHI_SCC_TMPPORT6);
136 /* access start and stop */
137 tmio_sd_writel(priv, RENESAS_SDHI_SCC_TMPPORT4_DLL_ACC_START,
138 RENESAS_SDHI_SCC_TMPPORT4);
139 tmio_sd_writel(priv, 0, RENESAS_SDHI_SCC_TMPPORT4);
142 static bool renesas_sdhi_check_scc_error(struct udevice *dev)
144 struct tmio_sd_priv *priv = dev_get_priv(dev);
145 struct mmc *mmc = mmc_get_mmc_dev(dev);
146 unsigned long new_tap = priv->tap_set;
147 unsigned long error_tap = priv->tap_set;
150 if ((priv->caps & TMIO_SD_CAP_RCAR_UHS) &&
151 (mmc->selected_mode != UHS_SDR104) &&
152 (mmc->selected_mode != MMC_HS_200) &&
153 (mmc->selected_mode != MMC_HS_400) &&
157 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSCNTL);
158 /* Handle automatic tuning correction */
159 if (reg & RENESAS_SDHI_SCC_RVSCNTL_RVSEN) {
160 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSREQ);
161 if (reg & RENESAS_SDHI_SCC_RVSREQ_RVSERR) {
162 tmio_sd_writel(priv, 0, RENESAS_SDHI_SCC_RVSREQ);
169 /* Handle manual tuning correction */
170 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSREQ);
171 if (!reg) /* No error */
174 tmio_sd_writel(priv, 0, RENESAS_SDHI_SCC_RVSREQ);
176 if (mmc->selected_mode == MMC_HS_400) {
178 * Correction Error Status contains CMD and DAT signal status.
179 * In HS400, DAT signal based on DS signal, not CLK.
180 * Therefore, use only CMD status.
182 smpcmp = tmio_sd_readl(priv, RENESAS_SDHI_SCC_SMPCMP) &
183 RENESAS_SDHI_SCC_SMPCMP_CMD_ERR;
187 return false; /* No error in CMD signal */
188 case RENESAS_SDHI_SCC_SMPCMP_CMD_REQUP:
189 new_tap = (priv->tap_set +
190 priv->tap_num + 1) % priv->tap_num;
191 error_tap = (priv->tap_set +
192 priv->tap_num - 1) % priv->tap_num;
194 case RENESAS_SDHI_SCC_SMPCMP_CMD_REQDOWN:
195 new_tap = (priv->tap_set +
196 priv->tap_num - 1) % priv->tap_num;
197 error_tap = (priv->tap_set +
198 priv->tap_num + 1) % priv->tap_num;
201 return true; /* Need re-tune */
204 if (priv->hs400_bad_tap & BIT(new_tap)) {
206 * New tap is bad tap (cannot change).
207 * Compare with HS200 tuning result.
208 * In HS200 tuning, when smpcmp[error_tap]
209 * is OK, retune is executed.
211 if (priv->smpcmp & BIT(error_tap))
212 return true; /* Need retune */
214 return false; /* cannot change */
217 priv->tap_set = new_tap;
219 if (reg & RENESAS_SDHI_SCC_RVSREQ_RVSERR)
220 return true; /* Need re-tune */
221 else if (reg & RENESAS_SDHI_SCC_RVSREQ_REQTAPUP)
222 priv->tap_set = (priv->tap_set +
223 priv->tap_num + 1) % priv->tap_num;
224 else if (reg & RENESAS_SDHI_SCC_RVSREQ_REQTAPDOWN)
225 priv->tap_set = (priv->tap_set +
226 priv->tap_num - 1) % priv->tap_num;
231 /* Set TAP position */
232 tmio_sd_writel(priv, priv->tap_set >> ((priv->nrtaps == 4) ? 1 : 0),
233 RENESAS_SDHI_SCC_TAPSET);
238 static void renesas_sdhi_adjust_hs400_mode_enable(struct tmio_sd_priv *priv)
242 if (!priv->adjust_hs400_enable)
245 if (!priv->needs_adjust_hs400)
248 if (!priv->adjust_hs400_calib_table)
252 * Enabled Manual adjust HS400 mode
254 * 1) Disabled Write Protect
255 * W(addr=0x00, WP_DISABLE_CODE)
257 * 2) Read Calibration code
258 * read_value = R(addr=0x26)
259 * 3) Refer to calibration table
260 * Calibration code = table[read_value]
261 * 4) Enabled Manual Calibration
262 * W(addr=0x22, manual mode | Calibration code)
263 * 5) Set Offset value to TMPPORT3 Reg
265 sd_scc_tmpport_write32(priv, 0x00,
266 RENESAS_SDHI_SCC_TMPPORT_DISABLE_WP_CODE);
267 calib_code = sd_scc_tmpport_read32(priv, 0x26);
268 calib_code &= RENESAS_SDHI_SCC_TMPPORT_CALIB_CODE_MASK;
269 sd_scc_tmpport_write32(priv, 0x22,
270 RENESAS_SDHI_SCC_TMPPORT_MANUAL_MODE |
271 priv->adjust_hs400_calib_table[calib_code]);
272 tmio_sd_writel(priv, priv->adjust_hs400_offset,
273 RENESAS_SDHI_SCC_TMPPORT3);
276 priv->needs_adjust_hs400 = false;
279 static void renesas_sdhi_adjust_hs400_mode_disable(struct tmio_sd_priv *priv)
282 /* Disabled Manual adjust HS400 mode
284 * 1) Disabled Write Protect
285 * W(addr=0x00, WP_DISABLE_CODE)
286 * 2) Disabled Manual Calibration
288 * 3) Clear offset value to TMPPORT3 Reg
290 sd_scc_tmpport_write32(priv, 0x00,
291 RENESAS_SDHI_SCC_TMPPORT_DISABLE_WP_CODE);
292 sd_scc_tmpport_write32(priv, 0x22, 0);
293 tmio_sd_writel(priv, 0, RENESAS_SDHI_SCC_TMPPORT3);
296 static unsigned int renesas_sdhi_init_tuning(struct tmio_sd_priv *priv)
301 tmio_sd_writel(priv, 0, TMIO_SD_INFO1);
303 reg = tmio_sd_readl(priv, TMIO_SD_CLKCTL);
304 reg &= ~TMIO_SD_CLKCTL_SCLKEN;
305 tmio_sd_writel(priv, reg, TMIO_SD_CLKCTL);
307 /* Set sampling clock selection range */
308 tmio_sd_writel(priv, (0x8 << RENESAS_SDHI_SCC_DTCNTL_TAPNUM_SHIFT) |
309 RENESAS_SDHI_SCC_DTCNTL_TAPEN,
310 RENESAS_SDHI_SCC_DTCNTL);
312 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_CKSEL);
313 reg |= RENESAS_SDHI_SCC_CKSEL_DTSEL;
314 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_CKSEL);
316 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSCNTL);
317 reg &= ~RENESAS_SDHI_SCC_RVSCNTL_RVSEN;
318 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_RVSCNTL);
320 tmio_sd_writel(priv, 0x300 /* scc_tappos */,
321 RENESAS_SDHI_SCC_DT2FF);
323 reg = tmio_sd_readl(priv, TMIO_SD_CLKCTL);
324 reg |= TMIO_SD_CLKCTL_SCLKEN;
325 tmio_sd_writel(priv, reg, TMIO_SD_CLKCTL);
328 return (tmio_sd_readl(priv, RENESAS_SDHI_SCC_DTCNTL) >>
329 RENESAS_SDHI_SCC_DTCNTL_TAPNUM_SHIFT) &
330 RENESAS_SDHI_SCC_DTCNTL_TAPNUM_MASK;
333 static void renesas_sdhi_reset_tuning(struct tmio_sd_priv *priv)
338 reg = tmio_sd_readl(priv, TMIO_SD_CLKCTL);
339 reg &= ~TMIO_SD_CLKCTL_SCLKEN;
340 tmio_sd_writel(priv, reg, TMIO_SD_CLKCTL);
342 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_CKSEL);
343 reg &= ~RENESAS_SDHI_SCC_CKSEL_DTSEL;
344 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_CKSEL);
346 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_TMPPORT2);
347 reg &= ~(RENESAS_SDHI_SCC_TMPPORT2_HS400EN |
348 RENESAS_SDHI_SCC_TMPPORT2_HS400OSEL);
349 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_TMPPORT2);
351 /* Disable HS400 mode adjustment */
352 renesas_sdhi_adjust_hs400_mode_disable(priv);
354 reg = tmio_sd_readl(priv, TMIO_SD_CLKCTL);
355 reg |= TMIO_SD_CLKCTL_SCLKEN;
356 tmio_sd_writel(priv, reg, TMIO_SD_CLKCTL);
358 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSCNTL);
359 reg &= ~RENESAS_SDHI_SCC_RVSCNTL_RVSEN;
360 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_RVSCNTL);
362 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSCNTL);
363 reg &= ~RENESAS_SDHI_SCC_RVSCNTL_RVSEN;
364 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_RVSCNTL);
367 static int renesas_sdhi_hs400(struct udevice *dev)
369 struct tmio_sd_priv *priv = dev_get_priv(dev);
370 struct mmc *mmc = mmc_get_mmc_dev(dev);
371 bool hs400 = (mmc->selected_mode == MMC_HS_400);
372 int ret, taps = hs400 ? priv->nrtaps : 8;
373 unsigned long new_tap;
376 if (taps == 4) /* HS400 on 4tap SoC needs different clock */
377 ret = clk_set_rate(&priv->clk, 400000000);
379 ret = clk_set_rate(&priv->clk, 200000000);
383 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSCNTL);
384 reg &= ~RENESAS_SDHI_SCC_RVSCNTL_RVSEN;
385 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_RVSCNTL);
387 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_TMPPORT2);
389 reg |= RENESAS_SDHI_SCC_TMPPORT2_HS400EN |
390 RENESAS_SDHI_SCC_TMPPORT2_HS400OSEL;
392 reg &= ~(RENESAS_SDHI_SCC_TMPPORT2_HS400EN |
393 RENESAS_SDHI_SCC_TMPPORT2_HS400OSEL);
396 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_TMPPORT2);
398 /* Disable HS400 mode adjustment */
400 renesas_sdhi_adjust_hs400_mode_disable(priv);
402 tmio_sd_writel(priv, (0x8 << RENESAS_SDHI_SCC_DTCNTL_TAPNUM_SHIFT) |
403 RENESAS_SDHI_SCC_DTCNTL_TAPEN,
404 RENESAS_SDHI_SCC_DTCNTL);
407 if (priv->hs400_bad_tap & BIT(priv->tap_set)) {
408 new_tap = (priv->tap_set +
409 priv->tap_num + 1) % priv->tap_num;
411 if (priv->hs400_bad_tap & BIT(new_tap))
412 new_tap = (priv->tap_set +
413 priv->tap_num - 1) % priv->tap_num;
415 if (priv->hs400_bad_tap & BIT(new_tap)) {
416 new_tap = priv->tap_set;
417 debug("Three consecutive bad tap is prohibited\n");
420 priv->tap_set = new_tap;
421 tmio_sd_writel(priv, priv->tap_set, RENESAS_SDHI_SCC_TAPSET);
425 tmio_sd_writel(priv, priv->tap_set >> 1,
426 RENESAS_SDHI_SCC_TAPSET);
427 tmio_sd_writel(priv, hs400 ? 0x100 : 0x300,
428 RENESAS_SDHI_SCC_DT2FF);
430 tmio_sd_writel(priv, priv->tap_set, RENESAS_SDHI_SCC_TAPSET);
431 tmio_sd_writel(priv, 0x300, RENESAS_SDHI_SCC_DT2FF);
434 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_CKSEL);
435 reg |= RENESAS_SDHI_SCC_CKSEL_DTSEL;
436 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_CKSEL);
438 /* Execute adjust hs400 offset after setting to HS400 mode */
440 priv->needs_adjust_hs400 = true;
445 static void renesas_sdhi_prepare_tuning(struct tmio_sd_priv *priv,
448 /* Set sampling clock position */
449 tmio_sd_writel(priv, tap, RENESAS_SDHI_SCC_TAPSET);
452 static unsigned int renesas_sdhi_compare_scc_data(struct tmio_sd_priv *priv)
454 /* Get comparison of sampling data */
455 return tmio_sd_readl(priv, RENESAS_SDHI_SCC_SMPCMP);
458 static int renesas_sdhi_select_tuning(struct tmio_sd_priv *priv,
461 unsigned long tap_cnt; /* counter of tuning success */
462 unsigned long tap_start;/* start position of tuning success */
463 unsigned long tap_end; /* end position of tuning success */
464 unsigned long ntap; /* temporary counter of tuning success */
465 unsigned long match_cnt;/* counter of matching data */
470 priv->needs_adjust_hs400 = false;
472 /* Clear SCC_RVSREQ */
473 tmio_sd_writel(priv, 0, RENESAS_SDHI_SCC_RVSREQ);
475 /* Merge the results */
476 for (i = 0; i < priv->tap_num * 2; i++) {
477 if (!(taps & BIT(i))) {
478 taps &= ~BIT(i % priv->tap_num);
479 taps &= ~BIT((i % priv->tap_num) + priv->tap_num);
481 if (!(priv->smpcmp & BIT(i))) {
482 priv->smpcmp &= ~BIT(i % priv->tap_num);
483 priv->smpcmp &= ~BIT((i % priv->tap_num) + priv->tap_num);
488 * Find the longest consecutive run of successful probes. If that
489 * is more than RENESAS_SDHI_MAX_TAP probes long then use the
490 * center index as the tap.
496 for (i = 0; i < priv->tap_num * 2; i++) {
500 if (ntap > tap_cnt) {
501 tap_start = i - ntap;
509 if (ntap > tap_cnt) {
510 tap_start = i - ntap;
516 * If all of the TAP is OK, the sampling clock position is selected by
517 * identifying the change point of data.
519 if (tap_cnt == priv->tap_num * 2) {
524 for (i = 0; i < priv->tap_num * 2; i++) {
525 if (priv->smpcmp & BIT(i))
528 if (ntap > match_cnt) {
529 tap_start = i - ntap;
536 if (ntap > match_cnt) {
537 tap_start = i - ntap;
543 } else if (tap_cnt >= RENESAS_SDHI_MAX_TAP)
547 priv->tap_set = ((tap_start + tap_end) / 2) % priv->tap_num;
552 tmio_sd_writel(priv, priv->tap_set, RENESAS_SDHI_SCC_TAPSET);
554 /* Enable auto re-tuning */
555 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSCNTL);
556 reg |= RENESAS_SDHI_SCC_RVSCNTL_RVSEN;
557 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_RVSCNTL);
562 int renesas_sdhi_execute_tuning(struct udevice *dev, uint opcode)
564 struct tmio_sd_priv *priv = dev_get_priv(dev);
565 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
566 struct mmc *mmc = upriv->mmc;
567 unsigned int tap_num;
568 unsigned int taps = 0;
572 /* Only supported on Renesas RCar */
573 if (!(priv->caps & TMIO_SD_CAP_RCAR_UHS))
576 /* clock tuning is not needed for upto 52MHz */
577 if (!((mmc->selected_mode == MMC_HS_200) ||
578 (mmc->selected_mode == MMC_HS_400) ||
579 (mmc->selected_mode == UHS_SDR104) ||
580 (mmc->selected_mode == UHS_SDR50)))
583 tap_num = renesas_sdhi_init_tuning(priv);
585 /* Tuning is not supported */
588 priv->tap_num = tap_num;
590 if (priv->tap_num * 2 >= sizeof(taps) * 8) {
592 "Too many taps, skipping tuning. Please consider updating size of taps field of tmio_mmc_host\n");
598 /* Issue CMD19 twice for each tap */
599 for (i = 0; i < 2 * priv->tap_num; i++) {
600 renesas_sdhi_prepare_tuning(priv, i % priv->tap_num);
602 /* Force PIO for the tuning */
604 priv->caps &= ~TMIO_SD_CAP_DMA_INTERNAL;
606 ret = mmc_send_tuning(mmc, opcode, NULL);
613 ret = renesas_sdhi_compare_scc_data(priv);
615 priv->smpcmp |= BIT(i);
620 ret = renesas_sdhi_select_tuning(priv, taps);
624 dev_warn(dev, "Tuning procedure failed\n");
625 renesas_sdhi_reset_tuning(priv);
631 static int renesas_sdhi_hs400(struct udevice *dev)
637 static int renesas_sdhi_set_ios(struct udevice *dev)
639 struct tmio_sd_priv *priv = dev_get_priv(dev);
643 /* Stop the clock before changing its rate to avoid a glitch signal */
644 tmp = tmio_sd_readl(priv, TMIO_SD_CLKCTL);
645 tmp &= ~TMIO_SD_CLKCTL_SCLKEN;
646 tmio_sd_writel(priv, tmp, TMIO_SD_CLKCTL);
648 ret = renesas_sdhi_hs400(dev);
652 ret = tmio_sd_set_ios(dev);
656 #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
657 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
658 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
659 struct mmc *mmc = mmc_get_mmc_dev(dev);
660 if ((priv->caps & TMIO_SD_CAP_RCAR_UHS) &&
661 (mmc->selected_mode != UHS_SDR104) &&
662 (mmc->selected_mode != MMC_HS_200) &&
663 (mmc->selected_mode != MMC_HS_400)) {
664 renesas_sdhi_reset_tuning(priv);
671 #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
672 static int renesas_sdhi_wait_dat0(struct udevice *dev, int state,
675 int ret = -ETIMEDOUT;
677 bool target_dat0_high = !!state;
678 struct tmio_sd_priv *priv = dev_get_priv(dev);
680 timeout_us = DIV_ROUND_UP(timeout_us, 10); /* check every 10 us. */
681 while (timeout_us--) {
682 dat0_high = !!(tmio_sd_readl(priv, TMIO_SD_INFO2) & TMIO_SD_INFO2_DAT0);
683 if (dat0_high == target_dat0_high) {
694 #define RENESAS_SDHI_DMA_ALIGNMENT 128
696 static int renesas_sdhi_addr_aligned_gen(uintptr_t ubuf,
697 size_t len, size_t len_aligned)
699 /* Check if start is aligned */
700 if (!IS_ALIGNED(ubuf, RENESAS_SDHI_DMA_ALIGNMENT)) {
701 debug("Unaligned buffer address %lx\n", ubuf);
705 /* Check if length is aligned */
706 if (len != len_aligned) {
707 debug("Unaligned buffer length %zu\n", len);
711 #ifdef CONFIG_PHYS_64BIT
712 /* Check if below 32bit boundary */
713 if ((ubuf >> 32) || (ubuf + len_aligned) >> 32) {
714 debug("Buffer above 32bit boundary %lx-%lx\n",
715 ubuf, ubuf + len_aligned);
724 static int renesas_sdhi_addr_aligned(struct bounce_buffer *state)
726 uintptr_t ubuf = (uintptr_t)state->user_buffer;
728 return renesas_sdhi_addr_aligned_gen(ubuf, state->len,
732 static int renesas_sdhi_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
733 struct mmc_data *data)
735 struct bounce_buffer bbstate;
736 unsigned int bbflags;
743 if (data->flags & MMC_DATA_READ) {
745 bbflags = GEN_BB_WRITE;
747 buf = (void *)data->src;
748 bbflags = GEN_BB_READ;
750 len = data->blocks * data->blocksize;
752 ret = bounce_buffer_start_extalign(&bbstate, buf, len, bbflags,
753 RENESAS_SDHI_DMA_ALIGNMENT,
754 renesas_sdhi_addr_aligned);
756 * If the amount of data to transfer is too large, we can get
757 * -ENOMEM when starting the bounce buffer. If that happens,
758 * fall back to PIO as it was before, otherwise use the BB.
762 if (data->flags & MMC_DATA_READ)
763 data->dest = bbstate.bounce_buffer;
765 data->src = bbstate.bounce_buffer;
769 ret = tmio_sd_send_cmd(dev, cmd, data);
772 buf = bbstate.user_buffer;
774 bounce_buffer_stop(&bbstate);
776 if (data->flags & MMC_DATA_READ)
785 #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
786 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
787 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
788 struct tmio_sd_priv *priv = dev_get_priv(dev);
790 renesas_sdhi_check_scc_error(dev);
792 if (cmd->cmdidx == MMC_CMD_SEND_STATUS)
793 renesas_sdhi_adjust_hs400_mode_enable(priv);
799 int renesas_sdhi_get_b_max(struct udevice *dev, void *dst, lbaint_t blkcnt)
801 struct tmio_sd_priv *priv = dev_get_priv(dev);
802 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
803 struct mmc *mmc = upriv->mmc;
804 size_t len = blkcnt * mmc->read_bl_len;
805 size_t len_align = roundup(len, RENESAS_SDHI_DMA_ALIGNMENT);
807 if (renesas_sdhi_addr_aligned_gen((uintptr_t)dst, len, len_align)) {
808 if (priv->quirks & TMIO_SD_CAP_16BIT)
813 return (CONFIG_SYS_MALLOC_LEN / 4) / mmc->read_bl_len;
817 static const struct dm_mmc_ops renesas_sdhi_ops = {
818 .send_cmd = renesas_sdhi_send_cmd,
819 .set_ios = renesas_sdhi_set_ios,
820 .get_cd = tmio_sd_get_cd,
821 #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
822 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
823 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
824 .execute_tuning = renesas_sdhi_execute_tuning,
826 #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
827 .wait_dat0 = renesas_sdhi_wait_dat0,
829 .get_b_max = renesas_sdhi_get_b_max,
832 #define RENESAS_GEN2_QUIRKS TMIO_SD_CAP_RCAR_GEN2
833 #define RENESAS_GEN3_QUIRKS \
834 TMIO_SD_CAP_64BIT | TMIO_SD_CAP_RCAR_GEN3 | TMIO_SD_CAP_RCAR_UHS
836 static const struct udevice_id renesas_sdhi_match[] = {
837 { .compatible = "renesas,sdhi-r8a7790", .data = RENESAS_GEN2_QUIRKS },
838 { .compatible = "renesas,sdhi-r8a7791", .data = RENESAS_GEN2_QUIRKS },
839 { .compatible = "renesas,sdhi-r8a7792", .data = RENESAS_GEN2_QUIRKS },
840 { .compatible = "renesas,sdhi-r8a7793", .data = RENESAS_GEN2_QUIRKS },
841 { .compatible = "renesas,sdhi-r8a7794", .data = RENESAS_GEN2_QUIRKS },
842 { .compatible = "renesas,sdhi-r8a7795", .data = RENESAS_GEN3_QUIRKS },
843 { .compatible = "renesas,sdhi-r8a7796", .data = RENESAS_GEN3_QUIRKS },
844 { .compatible = "renesas,sdhi-r8a77965", .data = RENESAS_GEN3_QUIRKS },
845 { .compatible = "renesas,sdhi-r8a77970", .data = RENESAS_GEN3_QUIRKS },
846 { .compatible = "renesas,sdhi-r8a77990", .data = RENESAS_GEN3_QUIRKS },
847 { .compatible = "renesas,sdhi-r8a77995", .data = RENESAS_GEN3_QUIRKS },
851 static ulong renesas_sdhi_clk_get_rate(struct tmio_sd_priv *priv)
853 return clk_get_rate(&priv->clk);
856 static void renesas_sdhi_filter_caps(struct udevice *dev)
858 struct tmio_sd_priv *priv = dev_get_priv(dev);
860 if (!(priv->caps & TMIO_SD_CAP_RCAR_GEN3))
863 #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
864 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
865 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
866 struct tmio_sd_plat *plat = dev_get_platdata(dev);
868 /* HS400 is not supported on H3 ES1.x and M3W ES1.0, ES1.1 */
869 if (((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7795) &&
870 (rmobile_get_cpu_rev_integer() <= 1)) ||
871 ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7796) &&
872 (rmobile_get_cpu_rev_integer() == 1) &&
873 (rmobile_get_cpu_rev_fraction() < 2)))
874 plat->cfg.host_caps &= ~MMC_MODE_HS400;
876 /* H3 ES2.0, ES3.0 and M3W ES1.2 and M3N bad taps */
877 if (((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7795) &&
878 (rmobile_get_cpu_rev_integer() >= 2)) ||
879 ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7796) &&
880 (rmobile_get_cpu_rev_integer() == 1) &&
881 (rmobile_get_cpu_rev_fraction() == 2)) ||
882 (rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A77965))
883 priv->hs400_bad_tap = BIT(2) | BIT(3) | BIT(6) | BIT(7);
885 /* H3 ES3.0 can use HS400 with manual adjustment */
886 if ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7795) &&
887 (rmobile_get_cpu_rev_integer() >= 3)) {
888 priv->adjust_hs400_enable = true;
889 priv->adjust_hs400_offset = 0;
890 priv->adjust_hs400_calib_table =
891 r8a7795_calib_table[!rmobile_is_gen3_mmc0(priv)];
894 /* M3W ES1.2 can use HS400 with manual adjustment */
895 if ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7796) &&
896 (rmobile_get_cpu_rev_integer() == 1) &&
897 (rmobile_get_cpu_rev_fraction() == 2)) {
898 priv->adjust_hs400_enable = true;
899 priv->adjust_hs400_offset = 3;
900 priv->adjust_hs400_calib_table =
901 r8a7796_rev1_calib_table[!rmobile_is_gen3_mmc0(priv)];
904 /* M3W ES1.x for x>2 can use HS400 with manual adjustment and taps */
905 if ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7796) &&
906 (rmobile_get_cpu_rev_integer() == 1) &&
907 (rmobile_get_cpu_rev_fraction() > 2)) {
908 priv->adjust_hs400_enable = true;
909 priv->adjust_hs400_offset = 0;
910 priv->hs400_bad_tap = BIT(1) | BIT(3) | BIT(5) | BIT(7);
911 priv->adjust_hs400_calib_table =
912 r8a7796_rev3_calib_table[!rmobile_is_gen3_mmc0(priv)];
915 /* M3N can use HS400 with manual adjustment */
916 if (rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A77965) {
917 priv->adjust_hs400_enable = true;
918 priv->adjust_hs400_offset = 3;
919 priv->adjust_hs400_calib_table =
920 r8a77965_calib_table[!rmobile_is_gen3_mmc0(priv)];
923 /* E3 can use HS400 with manual adjustment */
924 if (rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A77990) {
925 priv->adjust_hs400_enable = true;
926 priv->adjust_hs400_offset = 3;
927 priv->adjust_hs400_calib_table =
928 r8a77990_calib_table[!rmobile_is_gen3_mmc0(priv)];
931 /* H3 ES1.x, ES2.0 and M3W ES1.0, ES1.1, ES1.2 uses 4 tuning taps */
932 if (((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7795) &&
933 (rmobile_get_cpu_rev_integer() <= 2)) ||
934 ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7796) &&
935 (rmobile_get_cpu_rev_integer() == 1) &&
936 (rmobile_get_cpu_rev_fraction() <= 2)))
941 /* H3 ES1.x and M3W ES1.0 uses bit 17 for DTRAEND */
942 if (((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7795) &&
943 (rmobile_get_cpu_rev_integer() <= 1)) ||
944 ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7796) &&
945 (rmobile_get_cpu_rev_integer() == 1) &&
946 (rmobile_get_cpu_rev_fraction() == 0)))
947 priv->read_poll_flag = TMIO_SD_DMA_INFO1_END_RD;
949 priv->read_poll_flag = TMIO_SD_DMA_INFO1_END_RD2;
952 static int renesas_sdhi_probe(struct udevice *dev)
954 struct tmio_sd_priv *priv = dev_get_priv(dev);
955 u32 quirks = dev_get_driver_data(dev);
956 struct fdt_resource reg_res;
957 DECLARE_GLOBAL_DATA_PTR;
960 priv->clk_get_rate = renesas_sdhi_clk_get_rate;
962 if (quirks == RENESAS_GEN2_QUIRKS) {
963 ret = fdt_get_resource(gd->fdt_blob, dev_of_offset(dev),
966 dev_err(dev, "\"reg\" resource not found, ret=%i\n",
971 if (fdt_resource_size(®_res) == 0x100)
972 quirks |= TMIO_SD_CAP_16BIT;
975 ret = clk_get_by_index(dev, 0, &priv->clk);
977 dev_err(dev, "failed to get host clock\n");
981 /* set to max rate */
982 ret = clk_set_rate(&priv->clk, 200000000);
984 dev_err(dev, "failed to set rate for host clock\n");
985 clk_free(&priv->clk);
989 ret = clk_enable(&priv->clk);
991 dev_err(dev, "failed to enable host clock\n");
995 priv->quirks = quirks;
996 ret = tmio_sd_probe(dev, quirks);
998 renesas_sdhi_filter_caps(dev);
1000 #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
1001 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
1002 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
1003 if (!ret && (priv->caps & TMIO_SD_CAP_RCAR_UHS))
1004 renesas_sdhi_reset_tuning(priv);
1009 U_BOOT_DRIVER(renesas_sdhi) = {
1010 .name = "renesas-sdhi",
1012 .of_match = renesas_sdhi_match,
1013 .bind = tmio_sd_bind,
1014 .probe = renesas_sdhi_probe,
1015 .priv_auto_alloc_size = sizeof(struct tmio_sd_priv),
1016 .platdata_auto_alloc_size = sizeof(struct tmio_sd_plat),
1017 .ops = &renesas_sdhi_ops,