blob: 8e716f74491f8d66b62a32440b288157b816d107 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marek Vasute94cad92018-04-08 15:22:58 +02002/*
3 * Copyright (C) 2018 Marek Vasut <marek.vasut@gmail.com>
Marek Vasute94cad92018-04-08 15:22:58 +02004 */
5
6#include <common.h>
Marek Vasutd2661d82020-04-04 12:45:04 +02007#include <bouncebuf.h>
Marek Vasute94cad92018-04-08 15:22:58 +02008#include <clk.h>
9#include <fdtdec.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060010#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070011#include <malloc.h>
Marek Vasute94cad92018-04-08 15:22:58 +020012#include <mmc.h>
13#include <dm.h>
Simon Glass401d1c42020-10-30 21:38:53 -060014#include <asm/global_data.h>
Simon Glass336d4612020-02-03 07:36:16 -070015#include <dm/device_compat.h>
Simon Glasscd93d622020-05-10 11:40:13 -060016#include <linux/bitops.h>
Marek Vasute94cad92018-04-08 15:22:58 +020017#include <linux/compat.h>
Simon Glassc05ed002020-05-10 11:40:11 -060018#include <linux/delay.h>
Marek Vasute94cad92018-04-08 15:22:58 +020019#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 Vasutcb0b6b02018-04-13 23:51:33 +020024#include "tmio-common.h"
Marek Vasute94cad92018-04-08 15:22:58 +020025
Marek Vasut50aa1d92018-06-13 08:02:55 +020026#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
27 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
28 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
Marek Vasutf63968b2018-04-08 19:09:17 +020029
30/* SCC registers */
31#define RENESAS_SDHI_SCC_DTCNTL 0x800
Marek Vasut1bac2b62019-05-19 02:33:06 +020032#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 Vasutf63968b2018-04-08 19:09:17 +020035#define RENESAS_SDHI_SCC_TAPSET 0x804
36#define RENESAS_SDHI_SCC_DT2FF 0x808
37#define RENESAS_SDHI_SCC_CKSEL 0x80c
Marek Vasut1bac2b62019-05-19 02:33:06 +020038#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 Vasutf63968b2018-04-08 19:09:17 +020041#define RENESAS_SDHI_SCC_RVSREQ 0x814
Marek Vasut1bac2b62019-05-19 02:33:06 +020042#define RENESAS_SDHI_SCC_RVSREQ_RVSERR BIT(2)
Marek Vasut69000662019-11-23 13:36:23 +010043#define RENESAS_SDHI_SCC_RVSREQ_REQTAPUP BIT(1)
44#define RENESAS_SDHI_SCC_RVSREQ_REQTAPDOWN BIT(0)
Marek Vasutf63968b2018-04-08 19:09:17 +020045#define RENESAS_SDHI_SCC_SMPCMP 0x818
Marek Vasut69000662019-11-23 13:36:23 +010046#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 Vasut1bac2b62019-05-19 02:33:06 +020049#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 Vasutb5900a52019-05-19 03:47:07 +020052#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 Vasutf63968b2018-04-08 19:09:17 +020069
70#define RENESAS_SDHI_MAX_TAP 3
71
Marek Vasut56b0bb92019-11-23 13:36:25 +010072#define CALIB_TABLE_MAX (RENESAS_SDHI_SCC_TMPPORT_CALIB_CODE_MASK + 1)
73
Hai Pham6d7d4c42023-01-26 21:05:56 +010074static const u8 r8a7796_rev13_calib_table[2][CALIB_TABLE_MAX] = {
Hai Phamaf95db52023-01-26 21:05:57 +010075 { 3, 3, 3, 3, 3, 3, 3, 4, 4, 5, 6, 7, 8, 9, 10, 15,
76 16, 16, 16, 16, 16, 16, 17, 18, 18, 19, 20, 21, 22, 23, 24, 25 },
77 { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 7, 8, 11,
78 12, 17, 18, 18, 18, 18, 18, 18, 18, 19, 20, 21, 22, 23, 25, 25 }
Marek Vasut56b0bb92019-11-23 13:36:25 +010079};
80
81static const u8 r8a77965_calib_table[2][CALIB_TABLE_MAX] = {
Hai Phamaf95db52023-01-26 21:05:57 +010082 { 1, 2, 6, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 15, 15, 16,
83 17, 18, 19, 20, 21, 22, 23, 24, 25, 25, 26, 27, 28, 29, 30, 31 },
84 { 2, 3, 4, 4, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 16, 17,
85 17, 17, 20, 21, 22, 23, 24, 25, 27, 28, 29, 30, 31, 31, 31, 31 }
Marek Vasut56b0bb92019-11-23 13:36:25 +010086};
87
88static const u8 r8a77990_calib_table[2][CALIB_TABLE_MAX] = {
89 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
90 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
Hai Phamaf95db52023-01-26 21:05:57 +010091 { 0, 0, 0, 1, 2, 3, 3, 4, 4, 4, 5, 5, 6, 8, 9, 10,
92 11, 12, 13, 15, 16, 17, 17, 18, 18, 19, 20, 22, 24, 25, 26, 26 }
Marek Vasut56b0bb92019-11-23 13:36:25 +010093};
94
95static int rmobile_is_gen3_mmc0(struct tmio_sd_priv *priv)
96{
97 /* On R-Car Gen3, MMC0 is at 0xee140000 */
98 return (uintptr_t)(priv->regbase) == 0xee140000;
99}
100
Marek Vasutb5900a52019-05-19 03:47:07 +0200101static u32 sd_scc_tmpport_read32(struct tmio_sd_priv *priv, u32 addr)
102{
103 /* read mode */
104 tmio_sd_writel(priv, RENESAS_SDHI_SCC_TMPPORT5_DLL_RW_SEL_R |
105 (RENESAS_SDHI_SCC_TMPPORT5_DLL_ADR_MASK & addr),
106 RENESAS_SDHI_SCC_TMPPORT5);
107
108 /* access start and stop */
109 tmio_sd_writel(priv, RENESAS_SDHI_SCC_TMPPORT4_DLL_ACC_START,
110 RENESAS_SDHI_SCC_TMPPORT4);
111 tmio_sd_writel(priv, 0, RENESAS_SDHI_SCC_TMPPORT4);
112
113 return tmio_sd_readl(priv, RENESAS_SDHI_SCC_TMPPORT7);
114}
115
116static void sd_scc_tmpport_write32(struct tmio_sd_priv *priv, u32 addr, u32 val)
117{
118 /* write mode */
119 tmio_sd_writel(priv, RENESAS_SDHI_SCC_TMPPORT5_DLL_RW_SEL_W |
120 (RENESAS_SDHI_SCC_TMPPORT5_DLL_ADR_MASK & addr),
121 RENESAS_SDHI_SCC_TMPPORT5);
122 tmio_sd_writel(priv, val, RENESAS_SDHI_SCC_TMPPORT6);
123
124 /* access start and stop */
125 tmio_sd_writel(priv, RENESAS_SDHI_SCC_TMPPORT4_DLL_ACC_START,
126 RENESAS_SDHI_SCC_TMPPORT4);
127 tmio_sd_writel(priv, 0, RENESAS_SDHI_SCC_TMPPORT4);
128}
129
Marek Vasut69000662019-11-23 13:36:23 +0100130static bool renesas_sdhi_check_scc_error(struct udevice *dev)
131{
132 struct tmio_sd_priv *priv = dev_get_priv(dev);
133 struct mmc *mmc = mmc_get_mmc_dev(dev);
134 unsigned long new_tap = priv->tap_set;
Marek Vasut1bdcb832019-11-23 13:36:24 +0100135 unsigned long error_tap = priv->tap_set;
Marek Vasut69000662019-11-23 13:36:23 +0100136 u32 reg, smpcmp;
137
138 if ((priv->caps & TMIO_SD_CAP_RCAR_UHS) &&
139 (mmc->selected_mode != UHS_SDR104) &&
140 (mmc->selected_mode != MMC_HS_200) &&
141 (mmc->selected_mode != MMC_HS_400) &&
142 (priv->nrtaps != 4))
143 return false;
144
145 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSCNTL);
146 /* Handle automatic tuning correction */
147 if (reg & RENESAS_SDHI_SCC_RVSCNTL_RVSEN) {
148 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSREQ);
149 if (reg & RENESAS_SDHI_SCC_RVSREQ_RVSERR) {
150 tmio_sd_writel(priv, 0, RENESAS_SDHI_SCC_RVSREQ);
151 return true;
152 }
153
154 return false;
155 }
156
157 /* Handle manual tuning correction */
158 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSREQ);
159 if (!reg) /* No error */
160 return false;
161
162 tmio_sd_writel(priv, 0, RENESAS_SDHI_SCC_RVSREQ);
163
164 if (mmc->selected_mode == MMC_HS_400) {
165 /*
166 * Correction Error Status contains CMD and DAT signal status.
167 * In HS400, DAT signal based on DS signal, not CLK.
168 * Therefore, use only CMD status.
169 */
170 smpcmp = tmio_sd_readl(priv, RENESAS_SDHI_SCC_SMPCMP) &
171 RENESAS_SDHI_SCC_SMPCMP_CMD_ERR;
172
173 switch (smpcmp) {
174 case 0:
175 return false; /* No error in CMD signal */
176 case RENESAS_SDHI_SCC_SMPCMP_CMD_REQUP:
177 new_tap = (priv->tap_set +
178 priv->tap_num + 1) % priv->tap_num;
Marek Vasut1bdcb832019-11-23 13:36:24 +0100179 error_tap = (priv->tap_set +
180 priv->tap_num - 1) % priv->tap_num;
Marek Vasut69000662019-11-23 13:36:23 +0100181 break;
182 case RENESAS_SDHI_SCC_SMPCMP_CMD_REQDOWN:
183 new_tap = (priv->tap_set +
184 priv->tap_num - 1) % priv->tap_num;
Marek Vasut1bdcb832019-11-23 13:36:24 +0100185 error_tap = (priv->tap_set +
186 priv->tap_num + 1) % priv->tap_num;
Marek Vasut69000662019-11-23 13:36:23 +0100187 break;
188 default:
189 return true; /* Need re-tune */
190 }
191
Marek Vasut1bdcb832019-11-23 13:36:24 +0100192 if (priv->hs400_bad_tap & BIT(new_tap)) {
193 /*
194 * New tap is bad tap (cannot change).
195 * Compare with HS200 tuning result.
196 * In HS200 tuning, when smpcmp[error_tap]
197 * is OK, retune is executed.
198 */
199 if (priv->smpcmp & BIT(error_tap))
200 return true; /* Need retune */
201
202 return false; /* cannot change */
203 }
204
Marek Vasut69000662019-11-23 13:36:23 +0100205 priv->tap_set = new_tap;
206 } else {
207 if (reg & RENESAS_SDHI_SCC_RVSREQ_RVSERR)
208 return true; /* Need re-tune */
209 else if (reg & RENESAS_SDHI_SCC_RVSREQ_REQTAPUP)
210 priv->tap_set = (priv->tap_set +
211 priv->tap_num + 1) % priv->tap_num;
212 else if (reg & RENESAS_SDHI_SCC_RVSREQ_REQTAPDOWN)
213 priv->tap_set = (priv->tap_set +
214 priv->tap_num - 1) % priv->tap_num;
215 else
216 return false;
217 }
218
219 /* Set TAP position */
220 tmio_sd_writel(priv, priv->tap_set >> ((priv->nrtaps == 4) ? 1 : 0),
221 RENESAS_SDHI_SCC_TAPSET);
222
223 return false;
224}
225
Marek Vasutb5900a52019-05-19 03:47:07 +0200226static void renesas_sdhi_adjust_hs400_mode_enable(struct tmio_sd_priv *priv)
227{
228 u32 calib_code;
229
230 if (!priv->adjust_hs400_enable)
231 return;
232
233 if (!priv->needs_adjust_hs400)
234 return;
235
Marek Vasut56b0bb92019-11-23 13:36:25 +0100236 if (!priv->adjust_hs400_calib_table)
237 return;
238
Marek Vasutb5900a52019-05-19 03:47:07 +0200239 /*
240 * Enabled Manual adjust HS400 mode
241 *
242 * 1) Disabled Write Protect
243 * W(addr=0x00, WP_DISABLE_CODE)
Marek Vasut56b0bb92019-11-23 13:36:25 +0100244 *
245 * 2) Read Calibration code
246 * read_value = R(addr=0x26)
247 * 3) Refer to calibration table
248 * Calibration code = table[read_value]
249 * 4) Enabled Manual Calibration
Marek Vasutb5900a52019-05-19 03:47:07 +0200250 * W(addr=0x22, manual mode | Calibration code)
Marek Vasut56b0bb92019-11-23 13:36:25 +0100251 * 5) Set Offset value to TMPPORT3 Reg
Marek Vasutb5900a52019-05-19 03:47:07 +0200252 */
253 sd_scc_tmpport_write32(priv, 0x00,
254 RENESAS_SDHI_SCC_TMPPORT_DISABLE_WP_CODE);
255 calib_code = sd_scc_tmpport_read32(priv, 0x26);
256 calib_code &= RENESAS_SDHI_SCC_TMPPORT_CALIB_CODE_MASK;
Marek Vasutb5900a52019-05-19 03:47:07 +0200257 sd_scc_tmpport_write32(priv, 0x22,
258 RENESAS_SDHI_SCC_TMPPORT_MANUAL_MODE |
Marek Vasut56b0bb92019-11-23 13:36:25 +0100259 priv->adjust_hs400_calib_table[calib_code]);
Marek Vasutb5900a52019-05-19 03:47:07 +0200260 tmio_sd_writel(priv, priv->adjust_hs400_offset,
261 RENESAS_SDHI_SCC_TMPPORT3);
262
263 /* Clear flag */
264 priv->needs_adjust_hs400 = false;
265}
266
267static void renesas_sdhi_adjust_hs400_mode_disable(struct tmio_sd_priv *priv)
268{
269
270 /* Disabled Manual adjust HS400 mode
271 *
272 * 1) Disabled Write Protect
273 * W(addr=0x00, WP_DISABLE_CODE)
274 * 2) Disabled Manual Calibration
275 * W(addr=0x22, 0)
276 * 3) Clear offset value to TMPPORT3 Reg
277 */
278 sd_scc_tmpport_write32(priv, 0x00,
279 RENESAS_SDHI_SCC_TMPPORT_DISABLE_WP_CODE);
280 sd_scc_tmpport_write32(priv, 0x22, 0);
281 tmio_sd_writel(priv, 0, RENESAS_SDHI_SCC_TMPPORT3);
282}
283
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200284static unsigned int renesas_sdhi_init_tuning(struct tmio_sd_priv *priv)
Marek Vasutf63968b2018-04-08 19:09:17 +0200285{
286 u32 reg;
287
288 /* Initialize SCC */
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200289 tmio_sd_writel(priv, 0, TMIO_SD_INFO1);
Marek Vasutf63968b2018-04-08 19:09:17 +0200290
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200291 reg = tmio_sd_readl(priv, TMIO_SD_CLKCTL);
292 reg &= ~TMIO_SD_CLKCTL_SCLKEN;
293 tmio_sd_writel(priv, reg, TMIO_SD_CLKCTL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200294
295 /* Set sampling clock selection range */
Marek Vasuta376dde2018-06-13 08:02:55 +0200296 tmio_sd_writel(priv, (0x8 << RENESAS_SDHI_SCC_DTCNTL_TAPNUM_SHIFT) |
297 RENESAS_SDHI_SCC_DTCNTL_TAPEN,
298 RENESAS_SDHI_SCC_DTCNTL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200299
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200300 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_CKSEL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200301 reg |= RENESAS_SDHI_SCC_CKSEL_DTSEL;
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200302 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_CKSEL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200303
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200304 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSCNTL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200305 reg &= ~RENESAS_SDHI_SCC_RVSCNTL_RVSEN;
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200306 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_RVSCNTL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200307
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200308 tmio_sd_writel(priv, 0x300 /* scc_tappos */,
Marek Vasutf63968b2018-04-08 19:09:17 +0200309 RENESAS_SDHI_SCC_DT2FF);
310
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200311 reg = tmio_sd_readl(priv, TMIO_SD_CLKCTL);
312 reg |= TMIO_SD_CLKCTL_SCLKEN;
313 tmio_sd_writel(priv, reg, TMIO_SD_CLKCTL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200314
315 /* Read TAPNUM */
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200316 return (tmio_sd_readl(priv, RENESAS_SDHI_SCC_DTCNTL) >>
Marek Vasutf63968b2018-04-08 19:09:17 +0200317 RENESAS_SDHI_SCC_DTCNTL_TAPNUM_SHIFT) &
318 RENESAS_SDHI_SCC_DTCNTL_TAPNUM_MASK;
319}
320
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200321static void renesas_sdhi_reset_tuning(struct tmio_sd_priv *priv)
Marek Vasutf63968b2018-04-08 19:09:17 +0200322{
323 u32 reg;
324
325 /* Reset SCC */
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200326 reg = tmio_sd_readl(priv, TMIO_SD_CLKCTL);
327 reg &= ~TMIO_SD_CLKCTL_SCLKEN;
328 tmio_sd_writel(priv, reg, TMIO_SD_CLKCTL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200329
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200330 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_CKSEL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200331 reg &= ~RENESAS_SDHI_SCC_CKSEL_DTSEL;
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200332 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_CKSEL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200333
Marek Vasutdc1488f2018-06-13 08:02:55 +0200334 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_TMPPORT2);
335 reg &= ~(RENESAS_SDHI_SCC_TMPPORT2_HS400EN |
336 RENESAS_SDHI_SCC_TMPPORT2_HS400OSEL);
337 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_TMPPORT2);
338
Marek Vasutb5900a52019-05-19 03:47:07 +0200339 /* Disable HS400 mode adjustment */
340 renesas_sdhi_adjust_hs400_mode_disable(priv);
341
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200342 reg = tmio_sd_readl(priv, TMIO_SD_CLKCTL);
343 reg |= TMIO_SD_CLKCTL_SCLKEN;
344 tmio_sd_writel(priv, reg, TMIO_SD_CLKCTL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200345
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200346 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSCNTL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200347 reg &= ~RENESAS_SDHI_SCC_RVSCNTL_RVSEN;
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200348 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_RVSCNTL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200349
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200350 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSCNTL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200351 reg &= ~RENESAS_SDHI_SCC_RVSCNTL_RVSEN;
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200352 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_RVSCNTL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200353}
354
Marek Vasut50aa1d92018-06-13 08:02:55 +0200355static int renesas_sdhi_hs400(struct udevice *dev)
356{
357 struct tmio_sd_priv *priv = dev_get_priv(dev);
358 struct mmc *mmc = mmc_get_mmc_dev(dev);
359 bool hs400 = (mmc->selected_mode == MMC_HS_400);
360 int ret, taps = hs400 ? priv->nrtaps : 8;
Hai Pham4dbbc3f2023-01-29 02:50:22 +0100361 const u32 sdn_rate = 200000000;
362 u32 sdnh_rate = 800000000;
Marek Vasut1bdcb832019-11-23 13:36:24 +0100363 unsigned long new_tap;
Marek Vasut50aa1d92018-06-13 08:02:55 +0200364 u32 reg;
365
Hai Pham4dbbc3f2023-01-29 02:50:22 +0100366 if (clk_valid(&priv->clkh) && !priv->needs_clkh_fallback) {
367 /* HS400 on 4tap SoC => SDnH=400 MHz, SDn=200 MHz */
368 if (taps == 4)
369 sdnh_rate /= 2;
370 ret = clk_set_rate(&priv->clkh, sdnh_rate);
371 if (ret < 0)
372 return ret;
373 }
374
375 ret = clk_set_rate(&priv->clk, sdn_rate);
Marek Vasut50aa1d92018-06-13 08:02:55 +0200376 if (ret < 0)
377 return ret;
378
Marek Vasut8f39b032019-11-23 13:36:22 +0100379 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSCNTL);
380 reg &= ~RENESAS_SDHI_SCC_RVSCNTL_RVSEN;
381 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_RVSCNTL);
Marek Vasut50aa1d92018-06-13 08:02:55 +0200382
383 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_TMPPORT2);
384 if (hs400) {
385 reg |= RENESAS_SDHI_SCC_TMPPORT2_HS400EN |
386 RENESAS_SDHI_SCC_TMPPORT2_HS400OSEL;
387 } else {
388 reg &= ~(RENESAS_SDHI_SCC_TMPPORT2_HS400EN |
389 RENESAS_SDHI_SCC_TMPPORT2_HS400OSEL);
390 }
391
392 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_TMPPORT2);
393
Marek Vasutb5900a52019-05-19 03:47:07 +0200394 /* Disable HS400 mode adjustment */
395 if (!hs400)
396 renesas_sdhi_adjust_hs400_mode_disable(priv);
397
Marek Vasutba41c452019-02-19 19:32:28 +0100398 tmio_sd_writel(priv, (0x8 << RENESAS_SDHI_SCC_DTCNTL_TAPNUM_SHIFT) |
Marek Vasut50aa1d92018-06-13 08:02:55 +0200399 RENESAS_SDHI_SCC_DTCNTL_TAPEN,
400 RENESAS_SDHI_SCC_DTCNTL);
401
Marek Vasut1bdcb832019-11-23 13:36:24 +0100402 /* Avoid bad TAP */
403 if (priv->hs400_bad_tap & BIT(priv->tap_set)) {
404 new_tap = (priv->tap_set +
405 priv->tap_num + 1) % priv->tap_num;
406
407 if (priv->hs400_bad_tap & BIT(new_tap))
408 new_tap = (priv->tap_set +
409 priv->tap_num - 1) % priv->tap_num;
410
411 if (priv->hs400_bad_tap & BIT(new_tap)) {
412 new_tap = priv->tap_set;
413 debug("Three consecutive bad tap is prohibited\n");
414 }
415
416 priv->tap_set = new_tap;
417 tmio_sd_writel(priv, priv->tap_set, RENESAS_SDHI_SCC_TAPSET);
418 }
419
Marek Vasut50aa1d92018-06-13 08:02:55 +0200420 if (taps == 4) {
421 tmio_sd_writel(priv, priv->tap_set >> 1,
422 RENESAS_SDHI_SCC_TAPSET);
Marek Vasutdc419fc2019-11-23 13:36:20 +0100423 tmio_sd_writel(priv, hs400 ? 0x100 : 0x300,
424 RENESAS_SDHI_SCC_DT2FF);
Marek Vasut50aa1d92018-06-13 08:02:55 +0200425 } else {
426 tmio_sd_writel(priv, priv->tap_set, RENESAS_SDHI_SCC_TAPSET);
Marek Vasutdc419fc2019-11-23 13:36:20 +0100427 tmio_sd_writel(priv, 0x300, RENESAS_SDHI_SCC_DT2FF);
Marek Vasut50aa1d92018-06-13 08:02:55 +0200428 }
429
430 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_CKSEL);
431 reg |= RENESAS_SDHI_SCC_CKSEL_DTSEL;
432 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_CKSEL);
433
Marek Vasutb5900a52019-05-19 03:47:07 +0200434 /* Execute adjust hs400 offset after setting to HS400 mode */
435 if (hs400)
436 priv->needs_adjust_hs400 = true;
437
Marek Vasut50aa1d92018-06-13 08:02:55 +0200438 return 0;
439}
440
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200441static void renesas_sdhi_prepare_tuning(struct tmio_sd_priv *priv,
Marek Vasutf63968b2018-04-08 19:09:17 +0200442 unsigned long tap)
443{
444 /* Set sampling clock position */
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200445 tmio_sd_writel(priv, tap, RENESAS_SDHI_SCC_TAPSET);
Marek Vasutf63968b2018-04-08 19:09:17 +0200446}
447
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200448static unsigned int renesas_sdhi_compare_scc_data(struct tmio_sd_priv *priv)
Marek Vasutf63968b2018-04-08 19:09:17 +0200449{
450 /* Get comparison of sampling data */
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200451 return tmio_sd_readl(priv, RENESAS_SDHI_SCC_SMPCMP);
Marek Vasutf63968b2018-04-08 19:09:17 +0200452}
453
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200454static int renesas_sdhi_select_tuning(struct tmio_sd_priv *priv,
Marek Vasut37c39902019-11-23 13:36:18 +0100455 unsigned int taps)
Marek Vasutf63968b2018-04-08 19:09:17 +0200456{
457 unsigned long tap_cnt; /* counter of tuning success */
Marek Vasutf63968b2018-04-08 19:09:17 +0200458 unsigned long tap_start;/* start position of tuning success */
459 unsigned long tap_end; /* end position of tuning success */
460 unsigned long ntap; /* temporary counter of tuning success */
461 unsigned long match_cnt;/* counter of matching data */
462 unsigned long i;
463 bool select = false;
464 u32 reg;
465
Marek Vasutb5900a52019-05-19 03:47:07 +0200466 priv->needs_adjust_hs400 = false;
467
Marek Vasutf63968b2018-04-08 19:09:17 +0200468 /* Clear SCC_RVSREQ */
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200469 tmio_sd_writel(priv, 0, RENESAS_SDHI_SCC_RVSREQ);
Marek Vasutf63968b2018-04-08 19:09:17 +0200470
471 /* Merge the results */
Marek Vasut0196a582019-11-23 13:36:17 +0100472 for (i = 0; i < priv->tap_num * 2; i++) {
Marek Vasutf63968b2018-04-08 19:09:17 +0200473 if (!(taps & BIT(i))) {
Marek Vasut0196a582019-11-23 13:36:17 +0100474 taps &= ~BIT(i % priv->tap_num);
475 taps &= ~BIT((i % priv->tap_num) + priv->tap_num);
Marek Vasutf63968b2018-04-08 19:09:17 +0200476 }
Marek Vasut37c39902019-11-23 13:36:18 +0100477 if (!(priv->smpcmp & BIT(i))) {
478 priv->smpcmp &= ~BIT(i % priv->tap_num);
479 priv->smpcmp &= ~BIT((i % priv->tap_num) + priv->tap_num);
Marek Vasutf63968b2018-04-08 19:09:17 +0200480 }
481 }
482
483 /*
484 * Find the longest consecutive run of successful probes. If that
485 * is more than RENESAS_SDHI_MAX_TAP probes long then use the
486 * center index as the tap.
487 */
488 tap_cnt = 0;
489 ntap = 0;
490 tap_start = 0;
491 tap_end = 0;
Marek Vasut0196a582019-11-23 13:36:17 +0100492 for (i = 0; i < priv->tap_num * 2; i++) {
Marek Vasutf63968b2018-04-08 19:09:17 +0200493 if (taps & BIT(i))
494 ntap++;
495 else {
496 if (ntap > tap_cnt) {
497 tap_start = i - ntap;
498 tap_end = i - 1;
499 tap_cnt = ntap;
500 }
501 ntap = 0;
502 }
503 }
504
505 if (ntap > tap_cnt) {
506 tap_start = i - ntap;
507 tap_end = i - 1;
508 tap_cnt = ntap;
509 }
510
511 /*
512 * If all of the TAP is OK, the sampling clock position is selected by
513 * identifying the change point of data.
514 */
Marek Vasut0196a582019-11-23 13:36:17 +0100515 if (tap_cnt == priv->tap_num * 2) {
Marek Vasutf63968b2018-04-08 19:09:17 +0200516 match_cnt = 0;
517 ntap = 0;
518 tap_start = 0;
519 tap_end = 0;
Marek Vasut0196a582019-11-23 13:36:17 +0100520 for (i = 0; i < priv->tap_num * 2; i++) {
Marek Vasut37c39902019-11-23 13:36:18 +0100521 if (priv->smpcmp & BIT(i))
Marek Vasutf63968b2018-04-08 19:09:17 +0200522 ntap++;
523 else {
524 if (ntap > match_cnt) {
525 tap_start = i - ntap;
526 tap_end = i - 1;
527 match_cnt = ntap;
528 }
529 ntap = 0;
530 }
531 }
532 if (ntap > match_cnt) {
533 tap_start = i - ntap;
534 tap_end = i - 1;
535 match_cnt = ntap;
536 }
537 if (match_cnt)
538 select = true;
539 } else if (tap_cnt >= RENESAS_SDHI_MAX_TAP)
540 select = true;
541
542 if (select)
Marek Vasut0196a582019-11-23 13:36:17 +0100543 priv->tap_set = ((tap_start + tap_end) / 2) % priv->tap_num;
Marek Vasutf63968b2018-04-08 19:09:17 +0200544 else
545 return -EIO;
546
547 /* Set SCC */
Marek Vasut95ead3d2018-06-13 08:02:55 +0200548 tmio_sd_writel(priv, priv->tap_set, RENESAS_SDHI_SCC_TAPSET);
Marek Vasutf63968b2018-04-08 19:09:17 +0200549
550 /* Enable auto re-tuning */
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200551 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSCNTL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200552 reg |= RENESAS_SDHI_SCC_RVSCNTL_RVSEN;
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200553 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_RVSCNTL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200554
555 return 0;
556}
557
558int renesas_sdhi_execute_tuning(struct udevice *dev, uint opcode)
559{
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200560 struct tmio_sd_priv *priv = dev_get_priv(dev);
Marek Vasutf63968b2018-04-08 19:09:17 +0200561 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
562 struct mmc *mmc = upriv->mmc;
563 unsigned int tap_num;
Marek Vasut37c39902019-11-23 13:36:18 +0100564 unsigned int taps = 0;
Marek Vasutf63968b2018-04-08 19:09:17 +0200565 int i, ret = 0;
566 u32 caps;
567
568 /* Only supported on Renesas RCar */
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200569 if (!(priv->caps & TMIO_SD_CAP_RCAR_UHS))
Marek Vasutf63968b2018-04-08 19:09:17 +0200570 return -EINVAL;
571
572 /* clock tuning is not needed for upto 52MHz */
573 if (!((mmc->selected_mode == MMC_HS_200) ||
Marek Vasut50aa1d92018-06-13 08:02:55 +0200574 (mmc->selected_mode == MMC_HS_400) ||
Marek Vasutf63968b2018-04-08 19:09:17 +0200575 (mmc->selected_mode == UHS_SDR104) ||
576 (mmc->selected_mode == UHS_SDR50)))
577 return 0;
578
579 tap_num = renesas_sdhi_init_tuning(priv);
580 if (!tap_num)
581 /* Tuning is not supported */
582 goto out;
583
Marek Vasut0196a582019-11-23 13:36:17 +0100584 priv->tap_num = tap_num;
585
586 if (priv->tap_num * 2 >= sizeof(taps) * 8) {
Marek Vasutf63968b2018-04-08 19:09:17 +0200587 dev_err(dev,
588 "Too many taps, skipping tuning. Please consider updating size of taps field of tmio_mmc_host\n");
589 goto out;
590 }
591
Marek Vasut37c39902019-11-23 13:36:18 +0100592 priv->smpcmp = 0;
593
Marek Vasutf63968b2018-04-08 19:09:17 +0200594 /* Issue CMD19 twice for each tap */
Marek Vasut0196a582019-11-23 13:36:17 +0100595 for (i = 0; i < 2 * priv->tap_num; i++) {
596 renesas_sdhi_prepare_tuning(priv, i % priv->tap_num);
Marek Vasutf63968b2018-04-08 19:09:17 +0200597
598 /* Force PIO for the tuning */
599 caps = priv->caps;
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200600 priv->caps &= ~TMIO_SD_CAP_DMA_INTERNAL;
Marek Vasutf63968b2018-04-08 19:09:17 +0200601
602 ret = mmc_send_tuning(mmc, opcode, NULL);
603
604 priv->caps = caps;
605
606 if (ret == 0)
607 taps |= BIT(i);
608
609 ret = renesas_sdhi_compare_scc_data(priv);
610 if (ret == 0)
Marek Vasut37c39902019-11-23 13:36:18 +0100611 priv->smpcmp |= BIT(i);
Marek Vasutf63968b2018-04-08 19:09:17 +0200612
613 mdelay(1);
Hai Pham99ab3d82023-06-20 00:38:25 +0200614
615 /*
616 * eMMC specification specifies that CMD12 can be used to stop a tuning
617 * command, but SD specification does not, so do nothing unless it is
618 * eMMC.
619 */
620 if (ret && (opcode == MMC_CMD_SEND_TUNING_BLOCK_HS200)) {
621 ret = mmc_send_stop_transmission(mmc, false);
622 if (ret < 0)
623 dev_dbg(dev, "Tuning abort fail (%d)\n", ret);
624 }
Marek Vasutf63968b2018-04-08 19:09:17 +0200625 }
626
Marek Vasut37c39902019-11-23 13:36:18 +0100627 ret = renesas_sdhi_select_tuning(priv, taps);
Marek Vasutf63968b2018-04-08 19:09:17 +0200628
629out:
630 if (ret < 0) {
631 dev_warn(dev, "Tuning procedure failed\n");
632 renesas_sdhi_reset_tuning(priv);
633 }
634
635 return ret;
636}
Marek Vasut50aa1d92018-06-13 08:02:55 +0200637#else
638static int renesas_sdhi_hs400(struct udevice *dev)
639{
640 return 0;
641}
Marek Vasutf63968b2018-04-08 19:09:17 +0200642#endif
643
644static int renesas_sdhi_set_ios(struct udevice *dev)
645{
Marek Vasut50aa1d92018-06-13 08:02:55 +0200646 struct tmio_sd_priv *priv = dev_get_priv(dev);
647 u32 tmp;
648 int ret;
649
650 /* Stop the clock before changing its rate to avoid a glitch signal */
651 tmp = tmio_sd_readl(priv, TMIO_SD_CLKCTL);
652 tmp &= ~TMIO_SD_CLKCTL_SCLKEN;
653 tmio_sd_writel(priv, tmp, TMIO_SD_CLKCTL);
654
655 ret = renesas_sdhi_hs400(dev);
656 if (ret)
657 return ret;
658
659 ret = tmio_sd_set_ios(dev);
Marek Vasutcf39f3f2018-04-09 20:47:31 +0200660
661 mdelay(10);
662
Marek Vasut50aa1d92018-06-13 08:02:55 +0200663#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
664 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
665 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
666 struct mmc *mmc = mmc_get_mmc_dev(dev);
667 if ((priv->caps & TMIO_SD_CAP_RCAR_UHS) &&
668 (mmc->selected_mode != UHS_SDR104) &&
669 (mmc->selected_mode != MMC_HS_200) &&
670 (mmc->selected_mode != MMC_HS_400)) {
Marek Vasut52e17962018-10-28 15:30:06 +0100671 renesas_sdhi_reset_tuning(priv);
Marek Vasut50aa1d92018-06-13 08:02:55 +0200672 }
Marek Vasutf63968b2018-04-08 19:09:17 +0200673#endif
674
675 return ret;
676}
677
Marek Vasut2fc10752018-10-28 19:28:56 +0100678#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300679static int renesas_sdhi_wait_dat0(struct udevice *dev, int state,
680 int timeout_us)
Marek Vasut2fc10752018-10-28 19:28:56 +0100681{
682 int ret = -ETIMEDOUT;
683 bool dat0_high;
684 bool target_dat0_high = !!state;
685 struct tmio_sd_priv *priv = dev_get_priv(dev);
686
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300687 timeout_us = DIV_ROUND_UP(timeout_us, 10); /* check every 10 us. */
688 while (timeout_us--) {
Marek Vasut2fc10752018-10-28 19:28:56 +0100689 dat0_high = !!(tmio_sd_readl(priv, TMIO_SD_INFO2) & TMIO_SD_INFO2_DAT0);
690 if (dat0_high == target_dat0_high) {
691 ret = 0;
692 break;
693 }
694 udelay(10);
695 }
696
697 return ret;
698}
699#endif
700
Marek Vasutd2661d82020-04-04 12:45:04 +0200701#define RENESAS_SDHI_DMA_ALIGNMENT 128
702
Marek Vasut4a66d4e2020-04-04 12:45:06 +0200703static int renesas_sdhi_addr_aligned_gen(uintptr_t ubuf,
704 size_t len, size_t len_aligned)
Marek Vasutd2661d82020-04-04 12:45:04 +0200705{
Marek Vasutd2661d82020-04-04 12:45:04 +0200706 /* Check if start is aligned */
707 if (!IS_ALIGNED(ubuf, RENESAS_SDHI_DMA_ALIGNMENT)) {
Marek Vasut4a66d4e2020-04-04 12:45:06 +0200708 debug("Unaligned buffer address %lx\n", ubuf);
Marek Vasutd2661d82020-04-04 12:45:04 +0200709 return 0;
710 }
711
712 /* Check if length is aligned */
Marek Vasut4a66d4e2020-04-04 12:45:06 +0200713 if (len != len_aligned) {
714 debug("Unaligned buffer length %zu\n", len);
Marek Vasutd2661d82020-04-04 12:45:04 +0200715 return 0;
716 }
717
718#ifdef CONFIG_PHYS_64BIT
719 /* Check if below 32bit boundary */
Marek Vasut4a66d4e2020-04-04 12:45:06 +0200720 if ((ubuf >> 32) || (ubuf + len_aligned) >> 32) {
721 debug("Buffer above 32bit boundary %lx-%lx\n",
722 ubuf, ubuf + len_aligned);
Marek Vasutd2661d82020-04-04 12:45:04 +0200723 return 0;
724 }
725#endif
726
727 /* Aligned */
728 return 1;
729}
730
Marek Vasut4a66d4e2020-04-04 12:45:06 +0200731static int renesas_sdhi_addr_aligned(struct bounce_buffer *state)
732{
733 uintptr_t ubuf = (uintptr_t)state->user_buffer;
734
735 return renesas_sdhi_addr_aligned_gen(ubuf, state->len,
736 state->len_aligned);
737}
738
Marek Vasutb5900a52019-05-19 03:47:07 +0200739static int renesas_sdhi_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
740 struct mmc_data *data)
741{
Marek Vasutd2661d82020-04-04 12:45:04 +0200742 struct bounce_buffer bbstate;
743 unsigned int bbflags;
744 bool bbok = false;
745 size_t len;
746 void *buf;
Marek Vasutb5900a52019-05-19 03:47:07 +0200747 int ret;
748
Marek Vasutd2661d82020-04-04 12:45:04 +0200749 if (data) {
750 if (data->flags & MMC_DATA_READ) {
751 buf = data->dest;
752 bbflags = GEN_BB_WRITE;
753 } else {
754 buf = (void *)data->src;
755 bbflags = GEN_BB_READ;
756 }
757 len = data->blocks * data->blocksize;
758
759 ret = bounce_buffer_start_extalign(&bbstate, buf, len, bbflags,
760 RENESAS_SDHI_DMA_ALIGNMENT,
761 renesas_sdhi_addr_aligned);
762 /*
763 * If the amount of data to transfer is too large, we can get
764 * -ENOMEM when starting the bounce buffer. If that happens,
765 * fall back to PIO as it was before, otherwise use the BB.
766 */
767 if (!ret) {
768 bbok = true;
769 if (data->flags & MMC_DATA_READ)
770 data->dest = bbstate.bounce_buffer;
771 else
772 data->src = bbstate.bounce_buffer;
773 }
774 }
775
Marek Vasutb5900a52019-05-19 03:47:07 +0200776 ret = tmio_sd_send_cmd(dev, cmd, data);
Marek Vasutd2661d82020-04-04 12:45:04 +0200777
778 if (data && bbok) {
779 buf = bbstate.user_buffer;
780
781 bounce_buffer_stop(&bbstate);
782
783 if (data->flags & MMC_DATA_READ)
784 data->dest = buf;
785 else
786 data->src = buf;
787 }
788
Marek Vasutb5900a52019-05-19 03:47:07 +0200789 if (ret)
790 return ret;
791
792#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
793 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
794 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
795 struct tmio_sd_priv *priv = dev_get_priv(dev);
796
Marek Vasut69000662019-11-23 13:36:23 +0100797 renesas_sdhi_check_scc_error(dev);
798
Marek Vasutb5900a52019-05-19 03:47:07 +0200799 if (cmd->cmdidx == MMC_CMD_SEND_STATUS)
800 renesas_sdhi_adjust_hs400_mode_enable(priv);
801#endif
802
803 return 0;
804}
805
Marek Vasut4a66d4e2020-04-04 12:45:06 +0200806int renesas_sdhi_get_b_max(struct udevice *dev, void *dst, lbaint_t blkcnt)
807{
808 struct tmio_sd_priv *priv = dev_get_priv(dev);
809 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
810 struct mmc *mmc = upriv->mmc;
811 size_t len = blkcnt * mmc->read_bl_len;
812 size_t len_align = roundup(len, RENESAS_SDHI_DMA_ALIGNMENT);
813
814 if (renesas_sdhi_addr_aligned_gen((uintptr_t)dst, len, len_align)) {
815 if (priv->quirks & TMIO_SD_CAP_16BIT)
816 return U16_MAX;
817 else
818 return U32_MAX;
819 } else {
820 return (CONFIG_SYS_MALLOC_LEN / 4) / mmc->read_bl_len;
821 }
822}
823
Marek Vasute94cad92018-04-08 15:22:58 +0200824static const struct dm_mmc_ops renesas_sdhi_ops = {
Marek Vasutb5900a52019-05-19 03:47:07 +0200825 .send_cmd = renesas_sdhi_send_cmd,
Marek Vasutf63968b2018-04-08 19:09:17 +0200826 .set_ios = renesas_sdhi_set_ios,
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200827 .get_cd = tmio_sd_get_cd,
Marek Vasut50aa1d92018-06-13 08:02:55 +0200828#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
829 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
830 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
Marek Vasutf63968b2018-04-08 19:09:17 +0200831 .execute_tuning = renesas_sdhi_execute_tuning,
832#endif
Marek Vasut2fc10752018-10-28 19:28:56 +0100833#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
834 .wait_dat0 = renesas_sdhi_wait_dat0,
835#endif
Marek Vasut4a66d4e2020-04-04 12:45:06 +0200836 .get_b_max = renesas_sdhi_get_b_max,
Marek Vasute94cad92018-04-08 15:22:58 +0200837};
838
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200839#define RENESAS_GEN2_QUIRKS TMIO_SD_CAP_RCAR_GEN2
Marek Vasutf98833d2018-04-08 18:49:52 +0200840#define RENESAS_GEN3_QUIRKS \
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200841 TMIO_SD_CAP_64BIT | TMIO_SD_CAP_RCAR_GEN3 | TMIO_SD_CAP_RCAR_UHS
Marek Vasutf98833d2018-04-08 18:49:52 +0200842
Marek Vasute94cad92018-04-08 15:22:58 +0200843static const struct udevice_id renesas_sdhi_match[] = {
Marek Vasutf98833d2018-04-08 18:49:52 +0200844 { .compatible = "renesas,sdhi-r8a7790", .data = RENESAS_GEN2_QUIRKS },
845 { .compatible = "renesas,sdhi-r8a7791", .data = RENESAS_GEN2_QUIRKS },
846 { .compatible = "renesas,sdhi-r8a7792", .data = RENESAS_GEN2_QUIRKS },
847 { .compatible = "renesas,sdhi-r8a7793", .data = RENESAS_GEN2_QUIRKS },
848 { .compatible = "renesas,sdhi-r8a7794", .data = RENESAS_GEN2_QUIRKS },
849 { .compatible = "renesas,sdhi-r8a7795", .data = RENESAS_GEN3_QUIRKS },
850 { .compatible = "renesas,sdhi-r8a7796", .data = RENESAS_GEN3_QUIRKS },
Hai Phamd919bb92023-01-26 21:05:59 +0100851 { .compatible = "renesas,sdhi-r8a77961", .data = RENESAS_GEN3_QUIRKS },
Adam Ford4ce95662020-06-30 09:30:10 -0500852 { .compatible = "renesas,rcar-gen3-sdhi", .data = RENESAS_GEN3_QUIRKS },
Marek Vasutf98833d2018-04-08 18:49:52 +0200853 { .compatible = "renesas,sdhi-r8a77965", .data = RENESAS_GEN3_QUIRKS },
854 { .compatible = "renesas,sdhi-r8a77970", .data = RENESAS_GEN3_QUIRKS },
Marek Vasutd6291522018-04-26 13:19:29 +0200855 { .compatible = "renesas,sdhi-r8a77990", .data = RENESAS_GEN3_QUIRKS },
Marek Vasutf98833d2018-04-08 18:49:52 +0200856 { .compatible = "renesas,sdhi-r8a77995", .data = RENESAS_GEN3_QUIRKS },
Hai Pham632a7b12023-02-28 22:24:06 +0100857 { .compatible = "renesas,rcar-gen4-sdhi", .data = RENESAS_GEN3_QUIRKS },
Marek Vasute94cad92018-04-08 15:22:58 +0200858 { /* sentinel */ }
859};
860
Marek Vasut8ec6a042018-06-13 08:02:55 +0200861static ulong renesas_sdhi_clk_get_rate(struct tmio_sd_priv *priv)
862{
863 return clk_get_rate(&priv->clk);
864}
865
Marek Vasutd34bd2d2018-06-13 08:02:55 +0200866static void renesas_sdhi_filter_caps(struct udevice *dev)
867{
Marek Vasutd34bd2d2018-06-13 08:02:55 +0200868 struct tmio_sd_priv *priv = dev_get_priv(dev);
869
870 if (!(priv->caps & TMIO_SD_CAP_RCAR_GEN3))
871 return;
872
Marek Vasut8a73bef2021-01-03 11:38:25 +0100873 if (priv->caps & TMIO_SD_CAP_DMA_INTERNAL)
874 priv->idma_bus_width = TMIO_SD_DMA_MODE_BUS_WIDTH;
875
Marek Vasut56b0bb92019-11-23 13:36:25 +0100876#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
877 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
878 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
Simon Glassc69cda22020-12-03 16:55:20 -0700879 struct tmio_sd_plat *plat = dev_get_plat(dev);
Marek Vasut56b0bb92019-11-23 13:36:25 +0100880
Hai Pham6d7d4c42023-01-26 21:05:56 +0100881 /* HS400 is not supported on H3 ES1.x, M3W ES1.[012], V3M, V3H ES1.x, D3 */
Marek Vasutd34bd2d2018-06-13 08:02:55 +0200882 if (((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7795) &&
883 (rmobile_get_cpu_rev_integer() <= 1)) ||
884 ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7796) &&
885 (rmobile_get_cpu_rev_integer() == 1) &&
Hai Pham6d7d4c42023-01-26 21:05:56 +0100886 (rmobile_get_cpu_rev_fraction() <= 2)) ||
887 (rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A77970) ||
888 ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A77980) &&
889 (rmobile_get_cpu_rev_integer() <= 1)) ||
890 (rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A77995))
Marek Vasutd34bd2d2018-06-13 08:02:55 +0200891 plat->cfg.host_caps &= ~MMC_MODE_HS400;
Marek Vasut50aa1d92018-06-13 08:02:55 +0200892
Marek Vasut1bdcb832019-11-23 13:36:24 +0100893 /* H3 ES2.0, ES3.0 and M3W ES1.2 and M3N bad taps */
894 if (((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7795) &&
895 (rmobile_get_cpu_rev_integer() >= 2)) ||
896 ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7796) &&
897 (rmobile_get_cpu_rev_integer() == 1) &&
898 (rmobile_get_cpu_rev_fraction() == 2)) ||
899 (rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A77965))
900 priv->hs400_bad_tap = BIT(2) | BIT(3) | BIT(6) | BIT(7);
901
902 /* M3W ES1.x for x>2 can use HS400 with manual adjustment and taps */
Marek Vasutb5900a52019-05-19 03:47:07 +0200903 if ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7796) &&
904 (rmobile_get_cpu_rev_integer() == 1) &&
905 (rmobile_get_cpu_rev_fraction() > 2)) {
906 priv->adjust_hs400_enable = true;
Hai Pham1cdabcc2023-01-26 21:05:58 +0100907 priv->adjust_hs400_offset = 3;
Marek Vasut1bdcb832019-11-23 13:36:24 +0100908 priv->hs400_bad_tap = BIT(1) | BIT(3) | BIT(5) | BIT(7);
Marek Vasut56b0bb92019-11-23 13:36:25 +0100909 priv->adjust_hs400_calib_table =
Hai Pham6d7d4c42023-01-26 21:05:56 +0100910 r8a7796_rev13_calib_table[!rmobile_is_gen3_mmc0(priv)];
Marek Vasutb5900a52019-05-19 03:47:07 +0200911 }
912
Hai Phamd919bb92023-01-26 21:05:59 +0100913 /* M3W+ bad taps */
914 if ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7796) &&
915 (rmobile_get_cpu_rev_integer() == 3))
916 priv->hs400_bad_tap = BIT(1) | BIT(3) | BIT(5) | BIT(7);
917
Marek Vasutb5900a52019-05-19 03:47:07 +0200918 /* M3N can use HS400 with manual adjustment */
919 if (rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A77965) {
920 priv->adjust_hs400_enable = true;
Marek Vasute5d3f3d2019-11-23 13:36:21 +0100921 priv->adjust_hs400_offset = 3;
Marek Vasut56b0bb92019-11-23 13:36:25 +0100922 priv->adjust_hs400_calib_table =
923 r8a77965_calib_table[!rmobile_is_gen3_mmc0(priv)];
Marek Vasutb5900a52019-05-19 03:47:07 +0200924 }
925
926 /* E3 can use HS400 with manual adjustment */
927 if (rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A77990) {
928 priv->adjust_hs400_enable = true;
Marek Vasute5d3f3d2019-11-23 13:36:21 +0100929 priv->adjust_hs400_offset = 3;
Marek Vasut56b0bb92019-11-23 13:36:25 +0100930 priv->adjust_hs400_calib_table =
931 r8a77990_calib_table[!rmobile_is_gen3_mmc0(priv)];
Marek Vasutb5900a52019-05-19 03:47:07 +0200932 }
933
Hai Pham01cbce82023-01-26 21:05:55 +0100934 /* H3 ES1.x, ES2.0 and M3W ES1.[0123] uses 4 tuning taps */
Marek Vasut81099882019-11-23 13:36:19 +0100935 if (((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7795) &&
936 (rmobile_get_cpu_rev_integer() <= 2)) ||
937 ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7796) &&
938 (rmobile_get_cpu_rev_integer() == 1) &&
Hai Pham01cbce82023-01-26 21:05:55 +0100939 (rmobile_get_cpu_rev_fraction() <= 3)))
Marek Vasut50aa1d92018-06-13 08:02:55 +0200940 priv->nrtaps = 4;
941 else
942 priv->nrtaps = 8;
Marek Vasut56b0bb92019-11-23 13:36:25 +0100943#endif
Marek Vasut992bcf42019-01-11 23:45:54 +0100944 /* H3 ES1.x and M3W ES1.0 uses bit 17 for DTRAEND */
945 if (((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7795) &&
946 (rmobile_get_cpu_rev_integer() <= 1)) ||
947 ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7796) &&
948 (rmobile_get_cpu_rev_integer() == 1) &&
949 (rmobile_get_cpu_rev_fraction() == 0)))
950 priv->read_poll_flag = TMIO_SD_DMA_INFO1_END_RD;
951 else
952 priv->read_poll_flag = TMIO_SD_DMA_INFO1_END_RD2;
Hai Pham3c78ce82023-01-26 21:06:01 +0100953
954 /* V3M handles SD0H differently than other Gen3 SoCs */
955 if (rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A77970)
956 priv->needs_clkh_fallback = true;
957 else
958 priv->needs_clkh_fallback = false;
Marek Vasutd34bd2d2018-06-13 08:02:55 +0200959}
960
Marek Vasutc769e602018-04-08 17:45:23 +0200961static int renesas_sdhi_probe(struct udevice *dev)
962{
Masahiro Yamada30b5d9a2018-04-20 18:14:24 +0900963 struct tmio_sd_priv *priv = dev_get_priv(dev);
Marek Vasutc769e602018-04-08 17:45:23 +0200964 u32 quirks = dev_get_driver_data(dev);
Marek Vasut7cf7ef82018-04-08 18:14:22 +0200965 struct fdt_resource reg_res;
966 DECLARE_GLOBAL_DATA_PTR;
967 int ret;
968
Marek Vasut8ec6a042018-06-13 08:02:55 +0200969 priv->clk_get_rate = renesas_sdhi_clk_get_rate;
970
Marek Vasutf98833d2018-04-08 18:49:52 +0200971 if (quirks == RENESAS_GEN2_QUIRKS) {
972 ret = fdt_get_resource(gd->fdt_blob, dev_of_offset(dev),
973 "reg", 0, &reg_res);
974 if (ret < 0) {
975 dev_err(dev, "\"reg\" resource not found, ret=%i\n",
976 ret);
977 return ret;
978 }
Marek Vasut7cf7ef82018-04-08 18:14:22 +0200979
Marek Vasutf98833d2018-04-08 18:49:52 +0200980 if (fdt_resource_size(&reg_res) == 0x100)
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200981 quirks |= TMIO_SD_CAP_16BIT;
Marek Vasutf98833d2018-04-08 18:49:52 +0200982 }
Marek Vasutc769e602018-04-08 17:45:23 +0200983
Marek Vasut8ec6a042018-06-13 08:02:55 +0200984 ret = clk_get_by_index(dev, 0, &priv->clk);
Masahiro Yamada30b5d9a2018-04-20 18:14:24 +0900985 if (ret < 0) {
986 dev_err(dev, "failed to get host clock\n");
987 return ret;
988 }
989
Hai Pham4dbbc3f2023-01-29 02:50:22 +0100990 /* optional SDnH clock */
991 ret = clk_get_by_name(dev, "clkh", &priv->clkh);
Marek Vasut6ddffa82023-02-27 23:49:27 +0100992 if (ret < 0) {
Hai Pham4dbbc3f2023-01-29 02:50:22 +0100993 dev_dbg(dev, "failed to get clkh\n");
Marek Vasut6ddffa82023-02-27 23:49:27 +0100994 } else {
995 ret = clk_set_rate(&priv->clkh, 800000000);
996 if (ret < 0) {
Marek Vasutf20a61a2023-02-27 23:49:28 +0100997 dev_err(dev, "failed to set rate for SDnH clock (%d)\n", ret);
998 goto err_clk;
Marek Vasut6ddffa82023-02-27 23:49:27 +0100999 }
1000 }
Hai Pham4dbbc3f2023-01-29 02:50:22 +01001001
Masahiro Yamada30b5d9a2018-04-20 18:14:24 +09001002 /* set to max rate */
Marek Vasut8ec6a042018-06-13 08:02:55 +02001003 ret = clk_set_rate(&priv->clk, 200000000);
1004 if (ret < 0) {
Marek Vasutf20a61a2023-02-27 23:49:28 +01001005 dev_err(dev, "failed to set rate for SDn clock (%d)\n", ret);
1006 goto err_clkh;
Masahiro Yamada30b5d9a2018-04-20 18:14:24 +09001007 }
1008
Marek Vasut8ec6a042018-06-13 08:02:55 +02001009 ret = clk_enable(&priv->clk);
Masahiro Yamada30b5d9a2018-04-20 18:14:24 +09001010 if (ret) {
Marek Vasutf20a61a2023-02-27 23:49:28 +01001011 dev_err(dev, "failed to enable SDn clock (%d)\n", ret);
1012 goto err_clkh;
Masahiro Yamada30b5d9a2018-04-20 18:14:24 +09001013 }
1014
Marek Vasut4a66d4e2020-04-04 12:45:06 +02001015 priv->quirks = quirks;
Marek Vasutcb0b6b02018-04-13 23:51:33 +02001016 ret = tmio_sd_probe(dev, quirks);
Marek Vasutf20a61a2023-02-27 23:49:28 +01001017 if (ret)
1018 goto err_tmio_probe;
Marek Vasutd34bd2d2018-06-13 08:02:55 +02001019
1020 renesas_sdhi_filter_caps(dev);
1021
Marek Vasut50aa1d92018-06-13 08:02:55 +02001022#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
1023 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
1024 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
Marek Vasutf20a61a2023-02-27 23:49:28 +01001025 if (priv->caps & TMIO_SD_CAP_RCAR_UHS)
Marek Vasut65186972018-08-30 15:27:26 +02001026 renesas_sdhi_reset_tuning(priv);
Marek Vasutf63968b2018-04-08 19:09:17 +02001027#endif
Marek Vasutf20a61a2023-02-27 23:49:28 +01001028 return 0;
1029
1030err_tmio_probe:
1031 clk_disable(&priv->clk);
1032err_clkh:
1033 clk_free(&priv->clkh);
1034err_clk:
1035 clk_free(&priv->clk);
Marek Vasutf63968b2018-04-08 19:09:17 +02001036 return ret;
Marek Vasutc769e602018-04-08 17:45:23 +02001037}
1038
Marek Vasute94cad92018-04-08 15:22:58 +02001039U_BOOT_DRIVER(renesas_sdhi) = {
1040 .name = "renesas-sdhi",
1041 .id = UCLASS_MMC,
1042 .of_match = renesas_sdhi_match,
Marek Vasutcb0b6b02018-04-13 23:51:33 +02001043 .bind = tmio_sd_bind,
Marek Vasutc769e602018-04-08 17:45:23 +02001044 .probe = renesas_sdhi_probe,
Simon Glass41575d82020-12-03 16:55:17 -07001045 .priv_auto = sizeof(struct tmio_sd_priv),
Simon Glasscaa4daa2020-12-03 16:55:18 -07001046 .plat_auto = sizeof(struct tmio_sd_plat),
Marek Vasute94cad92018-04-08 15:22:58 +02001047 .ops = &renesas_sdhi_ops,
1048};