blob: 88a7160b0a0b7c975ccf51b0a4336f73ebcdd890 [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 Glass336d4612020-02-03 07:36:16 -070010#include <malloc.h>
Marek Vasute94cad92018-04-08 15:22:58 +020011#include <mmc.h>
12#include <dm.h>
Simon Glass336d4612020-02-03 07:36:16 -070013#include <dm/device_compat.h>
Marek Vasute94cad92018-04-08 15:22:58 +020014#include <linux/compat.h>
15#include <linux/dma-direction.h>
16#include <linux/io.h>
17#include <linux/sizes.h>
18#include <power/regulator.h>
19#include <asm/unaligned.h>
20
Marek Vasutcb0b6b02018-04-13 23:51:33 +020021#include "tmio-common.h"
Marek Vasute94cad92018-04-08 15:22:58 +020022
Marek Vasut50aa1d92018-06-13 08:02:55 +020023#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
24 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
25 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
Marek Vasutf63968b2018-04-08 19:09:17 +020026
27/* SCC registers */
28#define RENESAS_SDHI_SCC_DTCNTL 0x800
Marek Vasut1bac2b62019-05-19 02:33:06 +020029#define RENESAS_SDHI_SCC_DTCNTL_TAPEN BIT(0)
30#define RENESAS_SDHI_SCC_DTCNTL_TAPNUM_SHIFT 16
31#define RENESAS_SDHI_SCC_DTCNTL_TAPNUM_MASK 0xff
Marek Vasutf63968b2018-04-08 19:09:17 +020032#define RENESAS_SDHI_SCC_TAPSET 0x804
33#define RENESAS_SDHI_SCC_DT2FF 0x808
34#define RENESAS_SDHI_SCC_CKSEL 0x80c
Marek Vasut1bac2b62019-05-19 02:33:06 +020035#define RENESAS_SDHI_SCC_CKSEL_DTSEL BIT(0)
36#define RENESAS_SDHI_SCC_RVSCNTL 0x810
37#define RENESAS_SDHI_SCC_RVSCNTL_RVSEN BIT(0)
Marek Vasutf63968b2018-04-08 19:09:17 +020038#define RENESAS_SDHI_SCC_RVSREQ 0x814
Marek Vasut1bac2b62019-05-19 02:33:06 +020039#define RENESAS_SDHI_SCC_RVSREQ_RVSERR BIT(2)
Marek Vasut69000662019-11-23 13:36:23 +010040#define RENESAS_SDHI_SCC_RVSREQ_REQTAPUP BIT(1)
41#define RENESAS_SDHI_SCC_RVSREQ_REQTAPDOWN BIT(0)
Marek Vasutf63968b2018-04-08 19:09:17 +020042#define RENESAS_SDHI_SCC_SMPCMP 0x818
Marek Vasut69000662019-11-23 13:36:23 +010043#define RENESAS_SDHI_SCC_SMPCMP_CMD_ERR (BIT(24) | BIT(8))
44#define RENESAS_SDHI_SCC_SMPCMP_CMD_REQUP BIT(24)
45#define RENESAS_SDHI_SCC_SMPCMP_CMD_REQDOWN BIT(8)
Marek Vasut1bac2b62019-05-19 02:33:06 +020046#define RENESAS_SDHI_SCC_TMPPORT2 0x81c
47#define RENESAS_SDHI_SCC_TMPPORT2_HS400EN BIT(31)
48#define RENESAS_SDHI_SCC_TMPPORT2_HS400OSEL BIT(4)
Marek Vasutb5900a52019-05-19 03:47:07 +020049#define RENESAS_SDHI_SCC_TMPPORT3 0x828
50#define RENESAS_SDHI_SCC_TMPPORT3_OFFSET_0 3
51#define RENESAS_SDHI_SCC_TMPPORT3_OFFSET_1 2
52#define RENESAS_SDHI_SCC_TMPPORT3_OFFSET_2 1
53#define RENESAS_SDHI_SCC_TMPPORT3_OFFSET_3 0
54#define RENESAS_SDHI_SCC_TMPPORT3_OFFSET_MASK 0x3
55#define RENESAS_SDHI_SCC_TMPPORT4 0x82c
56#define RENESAS_SDHI_SCC_TMPPORT4_DLL_ACC_START BIT(0)
57#define RENESAS_SDHI_SCC_TMPPORT5 0x830
58#define RENESAS_SDHI_SCC_TMPPORT5_DLL_RW_SEL_R BIT(8)
59#define RENESAS_SDHI_SCC_TMPPORT5_DLL_RW_SEL_W (0 << 8)
60#define RENESAS_SDHI_SCC_TMPPORT5_DLL_ADR_MASK 0x3F
61#define RENESAS_SDHI_SCC_TMPPORT6 0x834
62#define RENESAS_SDHI_SCC_TMPPORT7 0x838
63#define RENESAS_SDHI_SCC_TMPPORT_DISABLE_WP_CODE 0xa5000000
64#define RENESAS_SDHI_SCC_TMPPORT_CALIB_CODE_MASK 0x1f
65#define RENESAS_SDHI_SCC_TMPPORT_MANUAL_MODE BIT(7)
Marek Vasutf63968b2018-04-08 19:09:17 +020066
67#define RENESAS_SDHI_MAX_TAP 3
68
Marek Vasut56b0bb92019-11-23 13:36:25 +010069#define CALIB_TABLE_MAX (RENESAS_SDHI_SCC_TMPPORT_CALIB_CODE_MASK + 1)
70
71static const u8 r8a7795_calib_table[2][CALIB_TABLE_MAX] = {
72 { 0, 0, 0, 0, 0, 1, 1, 2, 3, 4, 5, 5, 6, 6, 7, 11,
73 15, 16, 16, 17, 17, 17, 17, 17, 18, 18, 18, 18, 19, 20, 21, 21 },
74 { 3, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 9, 10, 11, 12, 15,
75 16, 16, 17, 17, 17, 17, 17, 18, 18, 18, 18, 19, 20, 21, 22, 22 }
76};
77
78static const u8 r8a7796_rev1_calib_table[2][CALIB_TABLE_MAX] = {
79 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 3, 4, 9,
80 15, 15, 15, 16, 16, 16, 16, 16, 17, 18, 19, 20, 21, 21, 22, 22 },
81 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
82 2, 9, 16, 17, 17, 17, 18, 18, 18, 18, 19, 20, 21, 22, 23, 24}
83};
84
85static const u8 r8a7796_rev3_calib_table[2][CALIB_TABLE_MAX] = {
86 { 0, 0, 0, 0, 2, 3, 4, 4, 5, 6, 7, 7, 8, 9, 9, 10,
87 11, 12, 13, 15, 16, 17, 17, 18, 19, 19, 20, 21, 21, 22, 23, 23 },
88 { 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 9, 9, 10, 11, 12,
89 13, 14, 15, 16, 17, 17, 18, 19, 20, 20, 21, 22, 22, 23, 24, 24 }
90};
91
92static const u8 r8a77965_calib_table[2][CALIB_TABLE_MAX] = {
93 { 0, 1, 2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15,
94 16, 17, 18, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 29 },
95 { 0, 1, 2, 2, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 13, 15,
96 16, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 31 }
97};
98
99static const u8 r8a77990_calib_table[2][CALIB_TABLE_MAX] = {
100 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
101 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
102 { 0, 0, 1, 2, 3, 4, 4, 4, 4, 5, 5, 6, 7, 8, 10, 11,
103 12, 13, 14, 16, 17, 18, 18, 18, 19, 19, 20, 24, 26, 26, 26, 26 }
104};
105
106static int rmobile_is_gen3_mmc0(struct tmio_sd_priv *priv)
107{
108 /* On R-Car Gen3, MMC0 is at 0xee140000 */
109 return (uintptr_t)(priv->regbase) == 0xee140000;
110}
111
Marek Vasutb5900a52019-05-19 03:47:07 +0200112static u32 sd_scc_tmpport_read32(struct tmio_sd_priv *priv, u32 addr)
113{
114 /* read mode */
115 tmio_sd_writel(priv, RENESAS_SDHI_SCC_TMPPORT5_DLL_RW_SEL_R |
116 (RENESAS_SDHI_SCC_TMPPORT5_DLL_ADR_MASK & addr),
117 RENESAS_SDHI_SCC_TMPPORT5);
118
119 /* access start and stop */
120 tmio_sd_writel(priv, RENESAS_SDHI_SCC_TMPPORT4_DLL_ACC_START,
121 RENESAS_SDHI_SCC_TMPPORT4);
122 tmio_sd_writel(priv, 0, RENESAS_SDHI_SCC_TMPPORT4);
123
124 return tmio_sd_readl(priv, RENESAS_SDHI_SCC_TMPPORT7);
125}
126
127static void sd_scc_tmpport_write32(struct tmio_sd_priv *priv, u32 addr, u32 val)
128{
129 /* write mode */
130 tmio_sd_writel(priv, RENESAS_SDHI_SCC_TMPPORT5_DLL_RW_SEL_W |
131 (RENESAS_SDHI_SCC_TMPPORT5_DLL_ADR_MASK & addr),
132 RENESAS_SDHI_SCC_TMPPORT5);
133 tmio_sd_writel(priv, val, RENESAS_SDHI_SCC_TMPPORT6);
134
135 /* access start and stop */
136 tmio_sd_writel(priv, RENESAS_SDHI_SCC_TMPPORT4_DLL_ACC_START,
137 RENESAS_SDHI_SCC_TMPPORT4);
138 tmio_sd_writel(priv, 0, RENESAS_SDHI_SCC_TMPPORT4);
139}
140
Marek Vasut69000662019-11-23 13:36:23 +0100141static bool renesas_sdhi_check_scc_error(struct udevice *dev)
142{
143 struct tmio_sd_priv *priv = dev_get_priv(dev);
144 struct mmc *mmc = mmc_get_mmc_dev(dev);
145 unsigned long new_tap = priv->tap_set;
Marek Vasut1bdcb832019-11-23 13:36:24 +0100146 unsigned long error_tap = priv->tap_set;
Marek Vasut69000662019-11-23 13:36:23 +0100147 u32 reg, smpcmp;
148
149 if ((priv->caps & TMIO_SD_CAP_RCAR_UHS) &&
150 (mmc->selected_mode != UHS_SDR104) &&
151 (mmc->selected_mode != MMC_HS_200) &&
152 (mmc->selected_mode != MMC_HS_400) &&
153 (priv->nrtaps != 4))
154 return false;
155
156 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSCNTL);
157 /* Handle automatic tuning correction */
158 if (reg & RENESAS_SDHI_SCC_RVSCNTL_RVSEN) {
159 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSREQ);
160 if (reg & RENESAS_SDHI_SCC_RVSREQ_RVSERR) {
161 tmio_sd_writel(priv, 0, RENESAS_SDHI_SCC_RVSREQ);
162 return true;
163 }
164
165 return false;
166 }
167
168 /* Handle manual tuning correction */
169 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSREQ);
170 if (!reg) /* No error */
171 return false;
172
173 tmio_sd_writel(priv, 0, RENESAS_SDHI_SCC_RVSREQ);
174
175 if (mmc->selected_mode == MMC_HS_400) {
176 /*
177 * Correction Error Status contains CMD and DAT signal status.
178 * In HS400, DAT signal based on DS signal, not CLK.
179 * Therefore, use only CMD status.
180 */
181 smpcmp = tmio_sd_readl(priv, RENESAS_SDHI_SCC_SMPCMP) &
182 RENESAS_SDHI_SCC_SMPCMP_CMD_ERR;
183
184 switch (smpcmp) {
185 case 0:
186 return false; /* No error in CMD signal */
187 case RENESAS_SDHI_SCC_SMPCMP_CMD_REQUP:
188 new_tap = (priv->tap_set +
189 priv->tap_num + 1) % priv->tap_num;
Marek Vasut1bdcb832019-11-23 13:36:24 +0100190 error_tap = (priv->tap_set +
191 priv->tap_num - 1) % priv->tap_num;
Marek Vasut69000662019-11-23 13:36:23 +0100192 break;
193 case RENESAS_SDHI_SCC_SMPCMP_CMD_REQDOWN:
194 new_tap = (priv->tap_set +
195 priv->tap_num - 1) % priv->tap_num;
Marek Vasut1bdcb832019-11-23 13:36:24 +0100196 error_tap = (priv->tap_set +
197 priv->tap_num + 1) % priv->tap_num;
Marek Vasut69000662019-11-23 13:36:23 +0100198 break;
199 default:
200 return true; /* Need re-tune */
201 }
202
Marek Vasut1bdcb832019-11-23 13:36:24 +0100203 if (priv->hs400_bad_tap & BIT(new_tap)) {
204 /*
205 * New tap is bad tap (cannot change).
206 * Compare with HS200 tuning result.
207 * In HS200 tuning, when smpcmp[error_tap]
208 * is OK, retune is executed.
209 */
210 if (priv->smpcmp & BIT(error_tap))
211 return true; /* Need retune */
212
213 return false; /* cannot change */
214 }
215
Marek Vasut69000662019-11-23 13:36:23 +0100216 priv->tap_set = new_tap;
217 } else {
218 if (reg & RENESAS_SDHI_SCC_RVSREQ_RVSERR)
219 return true; /* Need re-tune */
220 else if (reg & RENESAS_SDHI_SCC_RVSREQ_REQTAPUP)
221 priv->tap_set = (priv->tap_set +
222 priv->tap_num + 1) % priv->tap_num;
223 else if (reg & RENESAS_SDHI_SCC_RVSREQ_REQTAPDOWN)
224 priv->tap_set = (priv->tap_set +
225 priv->tap_num - 1) % priv->tap_num;
226 else
227 return false;
228 }
229
230 /* Set TAP position */
231 tmio_sd_writel(priv, priv->tap_set >> ((priv->nrtaps == 4) ? 1 : 0),
232 RENESAS_SDHI_SCC_TAPSET);
233
234 return false;
235}
236
Marek Vasutb5900a52019-05-19 03:47:07 +0200237static void renesas_sdhi_adjust_hs400_mode_enable(struct tmio_sd_priv *priv)
238{
239 u32 calib_code;
240
241 if (!priv->adjust_hs400_enable)
242 return;
243
244 if (!priv->needs_adjust_hs400)
245 return;
246
Marek Vasut56b0bb92019-11-23 13:36:25 +0100247 if (!priv->adjust_hs400_calib_table)
248 return;
249
Marek Vasutb5900a52019-05-19 03:47:07 +0200250 /*
251 * Enabled Manual adjust HS400 mode
252 *
253 * 1) Disabled Write Protect
254 * W(addr=0x00, WP_DISABLE_CODE)
Marek Vasut56b0bb92019-11-23 13:36:25 +0100255 *
256 * 2) Read Calibration code
257 * read_value = R(addr=0x26)
258 * 3) Refer to calibration table
259 * Calibration code = table[read_value]
260 * 4) Enabled Manual Calibration
Marek Vasutb5900a52019-05-19 03:47:07 +0200261 * W(addr=0x22, manual mode | Calibration code)
Marek Vasut56b0bb92019-11-23 13:36:25 +0100262 * 5) Set Offset value to TMPPORT3 Reg
Marek Vasutb5900a52019-05-19 03:47:07 +0200263 */
264 sd_scc_tmpport_write32(priv, 0x00,
265 RENESAS_SDHI_SCC_TMPPORT_DISABLE_WP_CODE);
266 calib_code = sd_scc_tmpport_read32(priv, 0x26);
267 calib_code &= RENESAS_SDHI_SCC_TMPPORT_CALIB_CODE_MASK;
Marek Vasutb5900a52019-05-19 03:47:07 +0200268 sd_scc_tmpport_write32(priv, 0x22,
269 RENESAS_SDHI_SCC_TMPPORT_MANUAL_MODE |
Marek Vasut56b0bb92019-11-23 13:36:25 +0100270 priv->adjust_hs400_calib_table[calib_code]);
Marek Vasutb5900a52019-05-19 03:47:07 +0200271 tmio_sd_writel(priv, priv->adjust_hs400_offset,
272 RENESAS_SDHI_SCC_TMPPORT3);
273
274 /* Clear flag */
275 priv->needs_adjust_hs400 = false;
276}
277
278static void renesas_sdhi_adjust_hs400_mode_disable(struct tmio_sd_priv *priv)
279{
280
281 /* Disabled Manual adjust HS400 mode
282 *
283 * 1) Disabled Write Protect
284 * W(addr=0x00, WP_DISABLE_CODE)
285 * 2) Disabled Manual Calibration
286 * W(addr=0x22, 0)
287 * 3) Clear offset value to TMPPORT3 Reg
288 */
289 sd_scc_tmpport_write32(priv, 0x00,
290 RENESAS_SDHI_SCC_TMPPORT_DISABLE_WP_CODE);
291 sd_scc_tmpport_write32(priv, 0x22, 0);
292 tmio_sd_writel(priv, 0, RENESAS_SDHI_SCC_TMPPORT3);
293}
294
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200295static unsigned int renesas_sdhi_init_tuning(struct tmio_sd_priv *priv)
Marek Vasutf63968b2018-04-08 19:09:17 +0200296{
297 u32 reg;
298
299 /* Initialize SCC */
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200300 tmio_sd_writel(priv, 0, TMIO_SD_INFO1);
Marek Vasutf63968b2018-04-08 19:09:17 +0200301
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200302 reg = tmio_sd_readl(priv, TMIO_SD_CLKCTL);
303 reg &= ~TMIO_SD_CLKCTL_SCLKEN;
304 tmio_sd_writel(priv, reg, TMIO_SD_CLKCTL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200305
306 /* Set sampling clock selection range */
Marek Vasuta376dde2018-06-13 08:02:55 +0200307 tmio_sd_writel(priv, (0x8 << RENESAS_SDHI_SCC_DTCNTL_TAPNUM_SHIFT) |
308 RENESAS_SDHI_SCC_DTCNTL_TAPEN,
309 RENESAS_SDHI_SCC_DTCNTL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200310
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200311 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_CKSEL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200312 reg |= RENESAS_SDHI_SCC_CKSEL_DTSEL;
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200313 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_CKSEL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200314
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200315 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSCNTL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200316 reg &= ~RENESAS_SDHI_SCC_RVSCNTL_RVSEN;
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200317 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_RVSCNTL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200318
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200319 tmio_sd_writel(priv, 0x300 /* scc_tappos */,
Marek Vasutf63968b2018-04-08 19:09:17 +0200320 RENESAS_SDHI_SCC_DT2FF);
321
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200322 reg = tmio_sd_readl(priv, TMIO_SD_CLKCTL);
323 reg |= TMIO_SD_CLKCTL_SCLKEN;
324 tmio_sd_writel(priv, reg, TMIO_SD_CLKCTL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200325
326 /* Read TAPNUM */
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200327 return (tmio_sd_readl(priv, RENESAS_SDHI_SCC_DTCNTL) >>
Marek Vasutf63968b2018-04-08 19:09:17 +0200328 RENESAS_SDHI_SCC_DTCNTL_TAPNUM_SHIFT) &
329 RENESAS_SDHI_SCC_DTCNTL_TAPNUM_MASK;
330}
331
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200332static void renesas_sdhi_reset_tuning(struct tmio_sd_priv *priv)
Marek Vasutf63968b2018-04-08 19:09:17 +0200333{
334 u32 reg;
335
336 /* Reset SCC */
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200337 reg = tmio_sd_readl(priv, TMIO_SD_CLKCTL);
338 reg &= ~TMIO_SD_CLKCTL_SCLKEN;
339 tmio_sd_writel(priv, reg, TMIO_SD_CLKCTL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200340
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200341 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_CKSEL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200342 reg &= ~RENESAS_SDHI_SCC_CKSEL_DTSEL;
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200343 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_CKSEL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200344
Marek Vasutdc1488f2018-06-13 08:02:55 +0200345 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_TMPPORT2);
346 reg &= ~(RENESAS_SDHI_SCC_TMPPORT2_HS400EN |
347 RENESAS_SDHI_SCC_TMPPORT2_HS400OSEL);
348 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_TMPPORT2);
349
Marek Vasutb5900a52019-05-19 03:47:07 +0200350 /* Disable HS400 mode adjustment */
351 renesas_sdhi_adjust_hs400_mode_disable(priv);
352
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200353 reg = tmio_sd_readl(priv, TMIO_SD_CLKCTL);
354 reg |= TMIO_SD_CLKCTL_SCLKEN;
355 tmio_sd_writel(priv, reg, TMIO_SD_CLKCTL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200356
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200357 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSCNTL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200358 reg &= ~RENESAS_SDHI_SCC_RVSCNTL_RVSEN;
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200359 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_RVSCNTL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200360
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200361 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSCNTL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200362 reg &= ~RENESAS_SDHI_SCC_RVSCNTL_RVSEN;
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200363 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_RVSCNTL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200364}
365
Marek Vasut50aa1d92018-06-13 08:02:55 +0200366static int renesas_sdhi_hs400(struct udevice *dev)
367{
368 struct tmio_sd_priv *priv = dev_get_priv(dev);
369 struct mmc *mmc = mmc_get_mmc_dev(dev);
370 bool hs400 = (mmc->selected_mode == MMC_HS_400);
371 int ret, taps = hs400 ? priv->nrtaps : 8;
Marek Vasut1bdcb832019-11-23 13:36:24 +0100372 unsigned long new_tap;
Marek Vasut50aa1d92018-06-13 08:02:55 +0200373 u32 reg;
374
375 if (taps == 4) /* HS400 on 4tap SoC needs different clock */
376 ret = clk_set_rate(&priv->clk, 400000000);
377 else
378 ret = clk_set_rate(&priv->clk, 200000000);
379 if (ret < 0)
380 return ret;
381
Marek Vasut8f39b032019-11-23 13:36:22 +0100382 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSCNTL);
383 reg &= ~RENESAS_SDHI_SCC_RVSCNTL_RVSEN;
384 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_RVSCNTL);
Marek Vasut50aa1d92018-06-13 08:02:55 +0200385
386 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_TMPPORT2);
387 if (hs400) {
388 reg |= RENESAS_SDHI_SCC_TMPPORT2_HS400EN |
389 RENESAS_SDHI_SCC_TMPPORT2_HS400OSEL;
390 } else {
391 reg &= ~(RENESAS_SDHI_SCC_TMPPORT2_HS400EN |
392 RENESAS_SDHI_SCC_TMPPORT2_HS400OSEL);
393 }
394
395 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_TMPPORT2);
396
Marek Vasutb5900a52019-05-19 03:47:07 +0200397 /* Disable HS400 mode adjustment */
398 if (!hs400)
399 renesas_sdhi_adjust_hs400_mode_disable(priv);
400
Marek Vasutba41c452019-02-19 19:32:28 +0100401 tmio_sd_writel(priv, (0x8 << RENESAS_SDHI_SCC_DTCNTL_TAPNUM_SHIFT) |
Marek Vasut50aa1d92018-06-13 08:02:55 +0200402 RENESAS_SDHI_SCC_DTCNTL_TAPEN,
403 RENESAS_SDHI_SCC_DTCNTL);
404
Marek Vasut1bdcb832019-11-23 13:36:24 +0100405 /* Avoid bad TAP */
406 if (priv->hs400_bad_tap & BIT(priv->tap_set)) {
407 new_tap = (priv->tap_set +
408 priv->tap_num + 1) % priv->tap_num;
409
410 if (priv->hs400_bad_tap & BIT(new_tap))
411 new_tap = (priv->tap_set +
412 priv->tap_num - 1) % priv->tap_num;
413
414 if (priv->hs400_bad_tap & BIT(new_tap)) {
415 new_tap = priv->tap_set;
416 debug("Three consecutive bad tap is prohibited\n");
417 }
418
419 priv->tap_set = new_tap;
420 tmio_sd_writel(priv, priv->tap_set, RENESAS_SDHI_SCC_TAPSET);
421 }
422
Marek Vasut50aa1d92018-06-13 08:02:55 +0200423 if (taps == 4) {
424 tmio_sd_writel(priv, priv->tap_set >> 1,
425 RENESAS_SDHI_SCC_TAPSET);
Marek Vasutdc419fc2019-11-23 13:36:20 +0100426 tmio_sd_writel(priv, hs400 ? 0x100 : 0x300,
427 RENESAS_SDHI_SCC_DT2FF);
Marek Vasut50aa1d92018-06-13 08:02:55 +0200428 } else {
429 tmio_sd_writel(priv, priv->tap_set, RENESAS_SDHI_SCC_TAPSET);
Marek Vasutdc419fc2019-11-23 13:36:20 +0100430 tmio_sd_writel(priv, 0x300, RENESAS_SDHI_SCC_DT2FF);
Marek Vasut50aa1d92018-06-13 08:02:55 +0200431 }
432
433 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_CKSEL);
434 reg |= RENESAS_SDHI_SCC_CKSEL_DTSEL;
435 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_CKSEL);
436
Marek Vasutb5900a52019-05-19 03:47:07 +0200437 /* Execute adjust hs400 offset after setting to HS400 mode */
438 if (hs400)
439 priv->needs_adjust_hs400 = true;
440
Marek Vasut50aa1d92018-06-13 08:02:55 +0200441 return 0;
442}
443
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200444static void renesas_sdhi_prepare_tuning(struct tmio_sd_priv *priv,
Marek Vasutf63968b2018-04-08 19:09:17 +0200445 unsigned long tap)
446{
447 /* Set sampling clock position */
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200448 tmio_sd_writel(priv, tap, RENESAS_SDHI_SCC_TAPSET);
Marek Vasutf63968b2018-04-08 19:09:17 +0200449}
450
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200451static unsigned int renesas_sdhi_compare_scc_data(struct tmio_sd_priv *priv)
Marek Vasutf63968b2018-04-08 19:09:17 +0200452{
453 /* Get comparison of sampling data */
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200454 return tmio_sd_readl(priv, RENESAS_SDHI_SCC_SMPCMP);
Marek Vasutf63968b2018-04-08 19:09:17 +0200455}
456
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200457static int renesas_sdhi_select_tuning(struct tmio_sd_priv *priv,
Marek Vasut37c39902019-11-23 13:36:18 +0100458 unsigned int taps)
Marek Vasutf63968b2018-04-08 19:09:17 +0200459{
460 unsigned long tap_cnt; /* counter of tuning success */
Marek Vasutf63968b2018-04-08 19:09:17 +0200461 unsigned long tap_start;/* start position of tuning success */
462 unsigned long tap_end; /* end position of tuning success */
463 unsigned long ntap; /* temporary counter of tuning success */
464 unsigned long match_cnt;/* counter of matching data */
465 unsigned long i;
466 bool select = false;
467 u32 reg;
468
Marek Vasutb5900a52019-05-19 03:47:07 +0200469 priv->needs_adjust_hs400 = false;
470
Marek Vasutf63968b2018-04-08 19:09:17 +0200471 /* Clear SCC_RVSREQ */
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200472 tmio_sd_writel(priv, 0, RENESAS_SDHI_SCC_RVSREQ);
Marek Vasutf63968b2018-04-08 19:09:17 +0200473
474 /* Merge the results */
Marek Vasut0196a582019-11-23 13:36:17 +0100475 for (i = 0; i < priv->tap_num * 2; i++) {
Marek Vasutf63968b2018-04-08 19:09:17 +0200476 if (!(taps & BIT(i))) {
Marek Vasut0196a582019-11-23 13:36:17 +0100477 taps &= ~BIT(i % priv->tap_num);
478 taps &= ~BIT((i % priv->tap_num) + priv->tap_num);
Marek Vasutf63968b2018-04-08 19:09:17 +0200479 }
Marek Vasut37c39902019-11-23 13:36:18 +0100480 if (!(priv->smpcmp & BIT(i))) {
481 priv->smpcmp &= ~BIT(i % priv->tap_num);
482 priv->smpcmp &= ~BIT((i % priv->tap_num) + priv->tap_num);
Marek Vasutf63968b2018-04-08 19:09:17 +0200483 }
484 }
485
486 /*
487 * Find the longest consecutive run of successful probes. If that
488 * is more than RENESAS_SDHI_MAX_TAP probes long then use the
489 * center index as the tap.
490 */
491 tap_cnt = 0;
492 ntap = 0;
493 tap_start = 0;
494 tap_end = 0;
Marek Vasut0196a582019-11-23 13:36:17 +0100495 for (i = 0; i < priv->tap_num * 2; i++) {
Marek Vasutf63968b2018-04-08 19:09:17 +0200496 if (taps & BIT(i))
497 ntap++;
498 else {
499 if (ntap > tap_cnt) {
500 tap_start = i - ntap;
501 tap_end = i - 1;
502 tap_cnt = ntap;
503 }
504 ntap = 0;
505 }
506 }
507
508 if (ntap > tap_cnt) {
509 tap_start = i - ntap;
510 tap_end = i - 1;
511 tap_cnt = ntap;
512 }
513
514 /*
515 * If all of the TAP is OK, the sampling clock position is selected by
516 * identifying the change point of data.
517 */
Marek Vasut0196a582019-11-23 13:36:17 +0100518 if (tap_cnt == priv->tap_num * 2) {
Marek Vasutf63968b2018-04-08 19:09:17 +0200519 match_cnt = 0;
520 ntap = 0;
521 tap_start = 0;
522 tap_end = 0;
Marek Vasut0196a582019-11-23 13:36:17 +0100523 for (i = 0; i < priv->tap_num * 2; i++) {
Marek Vasut37c39902019-11-23 13:36:18 +0100524 if (priv->smpcmp & BIT(i))
Marek Vasutf63968b2018-04-08 19:09:17 +0200525 ntap++;
526 else {
527 if (ntap > match_cnt) {
528 tap_start = i - ntap;
529 tap_end = i - 1;
530 match_cnt = ntap;
531 }
532 ntap = 0;
533 }
534 }
535 if (ntap > match_cnt) {
536 tap_start = i - ntap;
537 tap_end = i - 1;
538 match_cnt = ntap;
539 }
540 if (match_cnt)
541 select = true;
542 } else if (tap_cnt >= RENESAS_SDHI_MAX_TAP)
543 select = true;
544
545 if (select)
Marek Vasut0196a582019-11-23 13:36:17 +0100546 priv->tap_set = ((tap_start + tap_end) / 2) % priv->tap_num;
Marek Vasutf63968b2018-04-08 19:09:17 +0200547 else
548 return -EIO;
549
550 /* Set SCC */
Marek Vasut95ead3d2018-06-13 08:02:55 +0200551 tmio_sd_writel(priv, priv->tap_set, RENESAS_SDHI_SCC_TAPSET);
Marek Vasutf63968b2018-04-08 19:09:17 +0200552
553 /* Enable auto re-tuning */
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200554 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSCNTL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200555 reg |= RENESAS_SDHI_SCC_RVSCNTL_RVSEN;
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200556 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_RVSCNTL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200557
558 return 0;
559}
560
561int renesas_sdhi_execute_tuning(struct udevice *dev, uint opcode)
562{
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200563 struct tmio_sd_priv *priv = dev_get_priv(dev);
Marek Vasutf63968b2018-04-08 19:09:17 +0200564 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
565 struct mmc *mmc = upriv->mmc;
566 unsigned int tap_num;
Marek Vasut37c39902019-11-23 13:36:18 +0100567 unsigned int taps = 0;
Marek Vasutf63968b2018-04-08 19:09:17 +0200568 int i, ret = 0;
569 u32 caps;
570
571 /* Only supported on Renesas RCar */
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200572 if (!(priv->caps & TMIO_SD_CAP_RCAR_UHS))
Marek Vasutf63968b2018-04-08 19:09:17 +0200573 return -EINVAL;
574
575 /* clock tuning is not needed for upto 52MHz */
576 if (!((mmc->selected_mode == MMC_HS_200) ||
Marek Vasut50aa1d92018-06-13 08:02:55 +0200577 (mmc->selected_mode == MMC_HS_400) ||
Marek Vasutf63968b2018-04-08 19:09:17 +0200578 (mmc->selected_mode == UHS_SDR104) ||
579 (mmc->selected_mode == UHS_SDR50)))
580 return 0;
581
582 tap_num = renesas_sdhi_init_tuning(priv);
583 if (!tap_num)
584 /* Tuning is not supported */
585 goto out;
586
Marek Vasut0196a582019-11-23 13:36:17 +0100587 priv->tap_num = tap_num;
588
589 if (priv->tap_num * 2 >= sizeof(taps) * 8) {
Marek Vasutf63968b2018-04-08 19:09:17 +0200590 dev_err(dev,
591 "Too many taps, skipping tuning. Please consider updating size of taps field of tmio_mmc_host\n");
592 goto out;
593 }
594
Marek Vasut37c39902019-11-23 13:36:18 +0100595 priv->smpcmp = 0;
596
Marek Vasutf63968b2018-04-08 19:09:17 +0200597 /* Issue CMD19 twice for each tap */
Marek Vasut0196a582019-11-23 13:36:17 +0100598 for (i = 0; i < 2 * priv->tap_num; i++) {
599 renesas_sdhi_prepare_tuning(priv, i % priv->tap_num);
Marek Vasutf63968b2018-04-08 19:09:17 +0200600
601 /* Force PIO for the tuning */
602 caps = priv->caps;
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200603 priv->caps &= ~TMIO_SD_CAP_DMA_INTERNAL;
Marek Vasutf63968b2018-04-08 19:09:17 +0200604
605 ret = mmc_send_tuning(mmc, opcode, NULL);
606
607 priv->caps = caps;
608
609 if (ret == 0)
610 taps |= BIT(i);
611
612 ret = renesas_sdhi_compare_scc_data(priv);
613 if (ret == 0)
Marek Vasut37c39902019-11-23 13:36:18 +0100614 priv->smpcmp |= BIT(i);
Marek Vasutf63968b2018-04-08 19:09:17 +0200615
616 mdelay(1);
617 }
618
Marek Vasut37c39902019-11-23 13:36:18 +0100619 ret = renesas_sdhi_select_tuning(priv, taps);
Marek Vasutf63968b2018-04-08 19:09:17 +0200620
621out:
622 if (ret < 0) {
623 dev_warn(dev, "Tuning procedure failed\n");
624 renesas_sdhi_reset_tuning(priv);
625 }
626
627 return ret;
628}
Marek Vasut50aa1d92018-06-13 08:02:55 +0200629#else
630static int renesas_sdhi_hs400(struct udevice *dev)
631{
632 return 0;
633}
Marek Vasutf63968b2018-04-08 19:09:17 +0200634#endif
635
636static int renesas_sdhi_set_ios(struct udevice *dev)
637{
Marek Vasut50aa1d92018-06-13 08:02:55 +0200638 struct tmio_sd_priv *priv = dev_get_priv(dev);
639 u32 tmp;
640 int ret;
641
642 /* Stop the clock before changing its rate to avoid a glitch signal */
643 tmp = tmio_sd_readl(priv, TMIO_SD_CLKCTL);
644 tmp &= ~TMIO_SD_CLKCTL_SCLKEN;
645 tmio_sd_writel(priv, tmp, TMIO_SD_CLKCTL);
646
647 ret = renesas_sdhi_hs400(dev);
648 if (ret)
649 return ret;
650
651 ret = tmio_sd_set_ios(dev);
Marek Vasutcf39f3f2018-04-09 20:47:31 +0200652
653 mdelay(10);
654
Marek Vasut50aa1d92018-06-13 08:02:55 +0200655#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
656 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
657 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
658 struct mmc *mmc = mmc_get_mmc_dev(dev);
659 if ((priv->caps & TMIO_SD_CAP_RCAR_UHS) &&
660 (mmc->selected_mode != UHS_SDR104) &&
661 (mmc->selected_mode != MMC_HS_200) &&
662 (mmc->selected_mode != MMC_HS_400)) {
Marek Vasut52e17962018-10-28 15:30:06 +0100663 renesas_sdhi_reset_tuning(priv);
Marek Vasut50aa1d92018-06-13 08:02:55 +0200664 }
Marek Vasutf63968b2018-04-08 19:09:17 +0200665#endif
666
667 return ret;
668}
669
Marek Vasut2fc10752018-10-28 19:28:56 +0100670#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300671static int renesas_sdhi_wait_dat0(struct udevice *dev, int state,
672 int timeout_us)
Marek Vasut2fc10752018-10-28 19:28:56 +0100673{
674 int ret = -ETIMEDOUT;
675 bool dat0_high;
676 bool target_dat0_high = !!state;
677 struct tmio_sd_priv *priv = dev_get_priv(dev);
678
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300679 timeout_us = DIV_ROUND_UP(timeout_us, 10); /* check every 10 us. */
680 while (timeout_us--) {
Marek Vasut2fc10752018-10-28 19:28:56 +0100681 dat0_high = !!(tmio_sd_readl(priv, TMIO_SD_INFO2) & TMIO_SD_INFO2_DAT0);
682 if (dat0_high == target_dat0_high) {
683 ret = 0;
684 break;
685 }
686 udelay(10);
687 }
688
689 return ret;
690}
691#endif
692
Marek Vasutd2661d82020-04-04 12:45:04 +0200693#define RENESAS_SDHI_DMA_ALIGNMENT 128
694
Marek Vasut4a66d4e2020-04-04 12:45:06 +0200695static int renesas_sdhi_addr_aligned_gen(uintptr_t ubuf,
696 size_t len, size_t len_aligned)
Marek Vasutd2661d82020-04-04 12:45:04 +0200697{
Marek Vasutd2661d82020-04-04 12:45:04 +0200698 /* Check if start is aligned */
699 if (!IS_ALIGNED(ubuf, RENESAS_SDHI_DMA_ALIGNMENT)) {
Marek Vasut4a66d4e2020-04-04 12:45:06 +0200700 debug("Unaligned buffer address %lx\n", ubuf);
Marek Vasutd2661d82020-04-04 12:45:04 +0200701 return 0;
702 }
703
704 /* Check if length is aligned */
Marek Vasut4a66d4e2020-04-04 12:45:06 +0200705 if (len != len_aligned) {
706 debug("Unaligned buffer length %zu\n", len);
Marek Vasutd2661d82020-04-04 12:45:04 +0200707 return 0;
708 }
709
710#ifdef CONFIG_PHYS_64BIT
711 /* Check if below 32bit boundary */
Marek Vasut4a66d4e2020-04-04 12:45:06 +0200712 if ((ubuf >> 32) || (ubuf + len_aligned) >> 32) {
713 debug("Buffer above 32bit boundary %lx-%lx\n",
714 ubuf, ubuf + len_aligned);
Marek Vasutd2661d82020-04-04 12:45:04 +0200715 return 0;
716 }
717#endif
718
719 /* Aligned */
720 return 1;
721}
722
Marek Vasut4a66d4e2020-04-04 12:45:06 +0200723static int renesas_sdhi_addr_aligned(struct bounce_buffer *state)
724{
725 uintptr_t ubuf = (uintptr_t)state->user_buffer;
726
727 return renesas_sdhi_addr_aligned_gen(ubuf, state->len,
728 state->len_aligned);
729}
730
Marek Vasutb5900a52019-05-19 03:47:07 +0200731static int renesas_sdhi_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
732 struct mmc_data *data)
733{
Marek Vasutd2661d82020-04-04 12:45:04 +0200734 struct bounce_buffer bbstate;
735 unsigned int bbflags;
736 bool bbok = false;
737 size_t len;
738 void *buf;
Marek Vasutb5900a52019-05-19 03:47:07 +0200739 int ret;
740
Marek Vasutd2661d82020-04-04 12:45:04 +0200741 if (data) {
742 if (data->flags & MMC_DATA_READ) {
743 buf = data->dest;
744 bbflags = GEN_BB_WRITE;
745 } else {
746 buf = (void *)data->src;
747 bbflags = GEN_BB_READ;
748 }
749 len = data->blocks * data->blocksize;
750
751 ret = bounce_buffer_start_extalign(&bbstate, buf, len, bbflags,
752 RENESAS_SDHI_DMA_ALIGNMENT,
753 renesas_sdhi_addr_aligned);
754 /*
755 * If the amount of data to transfer is too large, we can get
756 * -ENOMEM when starting the bounce buffer. If that happens,
757 * fall back to PIO as it was before, otherwise use the BB.
758 */
759 if (!ret) {
760 bbok = true;
761 if (data->flags & MMC_DATA_READ)
762 data->dest = bbstate.bounce_buffer;
763 else
764 data->src = bbstate.bounce_buffer;
765 }
766 }
767
Marek Vasutb5900a52019-05-19 03:47:07 +0200768 ret = tmio_sd_send_cmd(dev, cmd, data);
Marek Vasutd2661d82020-04-04 12:45:04 +0200769
770 if (data && bbok) {
771 buf = bbstate.user_buffer;
772
773 bounce_buffer_stop(&bbstate);
774
775 if (data->flags & MMC_DATA_READ)
776 data->dest = buf;
777 else
778 data->src = buf;
779 }
780
Marek Vasutb5900a52019-05-19 03:47:07 +0200781 if (ret)
782 return ret;
783
784#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
785 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
786 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
787 struct tmio_sd_priv *priv = dev_get_priv(dev);
788
Marek Vasut69000662019-11-23 13:36:23 +0100789 renesas_sdhi_check_scc_error(dev);
790
Marek Vasutb5900a52019-05-19 03:47:07 +0200791 if (cmd->cmdidx == MMC_CMD_SEND_STATUS)
792 renesas_sdhi_adjust_hs400_mode_enable(priv);
793#endif
794
795 return 0;
796}
797
Marek Vasut4a66d4e2020-04-04 12:45:06 +0200798int renesas_sdhi_get_b_max(struct udevice *dev, void *dst, lbaint_t blkcnt)
799{
800 struct tmio_sd_priv *priv = dev_get_priv(dev);
801 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
802 struct mmc *mmc = upriv->mmc;
803 size_t len = blkcnt * mmc->read_bl_len;
804 size_t len_align = roundup(len, RENESAS_SDHI_DMA_ALIGNMENT);
805
806 if (renesas_sdhi_addr_aligned_gen((uintptr_t)dst, len, len_align)) {
807 if (priv->quirks & TMIO_SD_CAP_16BIT)
808 return U16_MAX;
809 else
810 return U32_MAX;
811 } else {
812 return (CONFIG_SYS_MALLOC_LEN / 4) / mmc->read_bl_len;
813 }
814}
815
Marek Vasute94cad92018-04-08 15:22:58 +0200816static const struct dm_mmc_ops renesas_sdhi_ops = {
Marek Vasutb5900a52019-05-19 03:47:07 +0200817 .send_cmd = renesas_sdhi_send_cmd,
Marek Vasutf63968b2018-04-08 19:09:17 +0200818 .set_ios = renesas_sdhi_set_ios,
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200819 .get_cd = tmio_sd_get_cd,
Marek Vasut50aa1d92018-06-13 08:02:55 +0200820#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
821 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
822 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
Marek Vasutf63968b2018-04-08 19:09:17 +0200823 .execute_tuning = renesas_sdhi_execute_tuning,
824#endif
Marek Vasut2fc10752018-10-28 19:28:56 +0100825#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
826 .wait_dat0 = renesas_sdhi_wait_dat0,
827#endif
Marek Vasut4a66d4e2020-04-04 12:45:06 +0200828 .get_b_max = renesas_sdhi_get_b_max,
Marek Vasute94cad92018-04-08 15:22:58 +0200829};
830
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200831#define RENESAS_GEN2_QUIRKS TMIO_SD_CAP_RCAR_GEN2
Marek Vasutf98833d2018-04-08 18:49:52 +0200832#define RENESAS_GEN3_QUIRKS \
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200833 TMIO_SD_CAP_64BIT | TMIO_SD_CAP_RCAR_GEN3 | TMIO_SD_CAP_RCAR_UHS
Marek Vasutf98833d2018-04-08 18:49:52 +0200834
Marek Vasute94cad92018-04-08 15:22:58 +0200835static const struct udevice_id renesas_sdhi_match[] = {
Marek Vasutf98833d2018-04-08 18:49:52 +0200836 { .compatible = "renesas,sdhi-r8a7790", .data = RENESAS_GEN2_QUIRKS },
837 { .compatible = "renesas,sdhi-r8a7791", .data = RENESAS_GEN2_QUIRKS },
838 { .compatible = "renesas,sdhi-r8a7792", .data = RENESAS_GEN2_QUIRKS },
839 { .compatible = "renesas,sdhi-r8a7793", .data = RENESAS_GEN2_QUIRKS },
840 { .compatible = "renesas,sdhi-r8a7794", .data = RENESAS_GEN2_QUIRKS },
841 { .compatible = "renesas,sdhi-r8a7795", .data = RENESAS_GEN3_QUIRKS },
842 { .compatible = "renesas,sdhi-r8a7796", .data = RENESAS_GEN3_QUIRKS },
843 { .compatible = "renesas,sdhi-r8a77965", .data = RENESAS_GEN3_QUIRKS },
844 { .compatible = "renesas,sdhi-r8a77970", .data = RENESAS_GEN3_QUIRKS },
Marek Vasutd6291522018-04-26 13:19:29 +0200845 { .compatible = "renesas,sdhi-r8a77990", .data = RENESAS_GEN3_QUIRKS },
Marek Vasutf98833d2018-04-08 18:49:52 +0200846 { .compatible = "renesas,sdhi-r8a77995", .data = RENESAS_GEN3_QUIRKS },
Marek Vasute94cad92018-04-08 15:22:58 +0200847 { /* sentinel */ }
848};
849
Marek Vasut8ec6a042018-06-13 08:02:55 +0200850static ulong renesas_sdhi_clk_get_rate(struct tmio_sd_priv *priv)
851{
852 return clk_get_rate(&priv->clk);
853}
854
Marek Vasutd34bd2d2018-06-13 08:02:55 +0200855static void renesas_sdhi_filter_caps(struct udevice *dev)
856{
Marek Vasutd34bd2d2018-06-13 08:02:55 +0200857 struct tmio_sd_priv *priv = dev_get_priv(dev);
858
859 if (!(priv->caps & TMIO_SD_CAP_RCAR_GEN3))
860 return;
861
Marek Vasut56b0bb92019-11-23 13:36:25 +0100862#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
863 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
864 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
865 struct tmio_sd_plat *plat = dev_get_platdata(dev);
866
867 /* HS400 is not supported on H3 ES1.x and M3W ES1.0, ES1.1 */
Marek Vasutd34bd2d2018-06-13 08:02:55 +0200868 if (((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7795) &&
869 (rmobile_get_cpu_rev_integer() <= 1)) ||
870 ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7796) &&
871 (rmobile_get_cpu_rev_integer() == 1) &&
Marek Vasut56b0bb92019-11-23 13:36:25 +0100872 (rmobile_get_cpu_rev_fraction() < 2)))
Marek Vasutd34bd2d2018-06-13 08:02:55 +0200873 plat->cfg.host_caps &= ~MMC_MODE_HS400;
Marek Vasut50aa1d92018-06-13 08:02:55 +0200874
Marek Vasut1bdcb832019-11-23 13:36:24 +0100875 /* H3 ES2.0, ES3.0 and M3W ES1.2 and M3N bad taps */
876 if (((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7795) &&
877 (rmobile_get_cpu_rev_integer() >= 2)) ||
878 ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7796) &&
879 (rmobile_get_cpu_rev_integer() == 1) &&
880 (rmobile_get_cpu_rev_fraction() == 2)) ||
881 (rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A77965))
882 priv->hs400_bad_tap = BIT(2) | BIT(3) | BIT(6) | BIT(7);
883
Marek Vasut56b0bb92019-11-23 13:36:25 +0100884 /* H3 ES3.0 can use HS400 with manual adjustment */
885 if ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7795) &&
886 (rmobile_get_cpu_rev_integer() >= 3)) {
887 priv->adjust_hs400_enable = true;
888 priv->adjust_hs400_offset = 0;
889 priv->adjust_hs400_calib_table =
890 r8a7795_calib_table[!rmobile_is_gen3_mmc0(priv)];
891 }
892
893 /* M3W ES1.2 can use HS400 with manual adjustment */
894 if ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7796) &&
895 (rmobile_get_cpu_rev_integer() == 1) &&
896 (rmobile_get_cpu_rev_fraction() == 2)) {
897 priv->adjust_hs400_enable = true;
898 priv->adjust_hs400_offset = 3;
899 priv->adjust_hs400_calib_table =
900 r8a7796_rev1_calib_table[!rmobile_is_gen3_mmc0(priv)];
901 }
902
Marek Vasut1bdcb832019-11-23 13:36:24 +0100903 /* M3W ES1.x for x>2 can use HS400 with manual adjustment and taps */
Marek Vasutb5900a52019-05-19 03:47:07 +0200904 if ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7796) &&
905 (rmobile_get_cpu_rev_integer() == 1) &&
906 (rmobile_get_cpu_rev_fraction() > 2)) {
907 priv->adjust_hs400_enable = true;
Marek Vasut56b0bb92019-11-23 13:36:25 +0100908 priv->adjust_hs400_offset = 0;
Marek Vasut1bdcb832019-11-23 13:36:24 +0100909 priv->hs400_bad_tap = BIT(1) | BIT(3) | BIT(5) | BIT(7);
Marek Vasut56b0bb92019-11-23 13:36:25 +0100910 priv->adjust_hs400_calib_table =
911 r8a7796_rev3_calib_table[!rmobile_is_gen3_mmc0(priv)];
Marek Vasutb5900a52019-05-19 03:47:07 +0200912 }
913
914 /* M3N can use HS400 with manual adjustment */
915 if (rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A77965) {
916 priv->adjust_hs400_enable = true;
Marek Vasute5d3f3d2019-11-23 13:36:21 +0100917 priv->adjust_hs400_offset = 3;
Marek Vasut56b0bb92019-11-23 13:36:25 +0100918 priv->adjust_hs400_calib_table =
919 r8a77965_calib_table[!rmobile_is_gen3_mmc0(priv)];
Marek Vasutb5900a52019-05-19 03:47:07 +0200920 }
921
922 /* E3 can use HS400 with manual adjustment */
923 if (rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A77990) {
924 priv->adjust_hs400_enable = true;
Marek Vasute5d3f3d2019-11-23 13:36:21 +0100925 priv->adjust_hs400_offset = 3;
Marek Vasut56b0bb92019-11-23 13:36:25 +0100926 priv->adjust_hs400_calib_table =
927 r8a77990_calib_table[!rmobile_is_gen3_mmc0(priv)];
Marek Vasutb5900a52019-05-19 03:47:07 +0200928 }
929
Marek Vasut81099882019-11-23 13:36:19 +0100930 /* H3 ES1.x, ES2.0 and M3W ES1.0, ES1.1, ES1.2 uses 4 tuning taps */
931 if (((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7795) &&
932 (rmobile_get_cpu_rev_integer() <= 2)) ||
933 ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7796) &&
934 (rmobile_get_cpu_rev_integer() == 1) &&
935 (rmobile_get_cpu_rev_fraction() <= 2)))
Marek Vasut50aa1d92018-06-13 08:02:55 +0200936 priv->nrtaps = 4;
937 else
938 priv->nrtaps = 8;
Marek Vasut56b0bb92019-11-23 13:36:25 +0100939#endif
Marek Vasut992bcf42019-01-11 23:45:54 +0100940 /* H3 ES1.x and M3W ES1.0 uses bit 17 for DTRAEND */
941 if (((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7795) &&
942 (rmobile_get_cpu_rev_integer() <= 1)) ||
943 ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7796) &&
944 (rmobile_get_cpu_rev_integer() == 1) &&
945 (rmobile_get_cpu_rev_fraction() == 0)))
946 priv->read_poll_flag = TMIO_SD_DMA_INFO1_END_RD;
947 else
948 priv->read_poll_flag = TMIO_SD_DMA_INFO1_END_RD2;
Marek Vasutd34bd2d2018-06-13 08:02:55 +0200949}
950
Marek Vasutc769e602018-04-08 17:45:23 +0200951static int renesas_sdhi_probe(struct udevice *dev)
952{
Masahiro Yamada30b5d9a2018-04-20 18:14:24 +0900953 struct tmio_sd_priv *priv = dev_get_priv(dev);
Marek Vasutc769e602018-04-08 17:45:23 +0200954 u32 quirks = dev_get_driver_data(dev);
Marek Vasut7cf7ef82018-04-08 18:14:22 +0200955 struct fdt_resource reg_res;
956 DECLARE_GLOBAL_DATA_PTR;
957 int ret;
958
Marek Vasut8ec6a042018-06-13 08:02:55 +0200959 priv->clk_get_rate = renesas_sdhi_clk_get_rate;
960
Marek Vasutf98833d2018-04-08 18:49:52 +0200961 if (quirks == RENESAS_GEN2_QUIRKS) {
962 ret = fdt_get_resource(gd->fdt_blob, dev_of_offset(dev),
963 "reg", 0, &reg_res);
964 if (ret < 0) {
965 dev_err(dev, "\"reg\" resource not found, ret=%i\n",
966 ret);
967 return ret;
968 }
Marek Vasut7cf7ef82018-04-08 18:14:22 +0200969
Marek Vasutf98833d2018-04-08 18:49:52 +0200970 if (fdt_resource_size(&reg_res) == 0x100)
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200971 quirks |= TMIO_SD_CAP_16BIT;
Marek Vasutf98833d2018-04-08 18:49:52 +0200972 }
Marek Vasutc769e602018-04-08 17:45:23 +0200973
Marek Vasut8ec6a042018-06-13 08:02:55 +0200974 ret = clk_get_by_index(dev, 0, &priv->clk);
Masahiro Yamada30b5d9a2018-04-20 18:14:24 +0900975 if (ret < 0) {
976 dev_err(dev, "failed to get host clock\n");
977 return ret;
978 }
979
980 /* set to max rate */
Marek Vasut8ec6a042018-06-13 08:02:55 +0200981 ret = clk_set_rate(&priv->clk, 200000000);
982 if (ret < 0) {
Masahiro Yamada30b5d9a2018-04-20 18:14:24 +0900983 dev_err(dev, "failed to set rate for host clock\n");
Marek Vasut8ec6a042018-06-13 08:02:55 +0200984 clk_free(&priv->clk);
985 return ret;
Masahiro Yamada30b5d9a2018-04-20 18:14:24 +0900986 }
987
Marek Vasut8ec6a042018-06-13 08:02:55 +0200988 ret = clk_enable(&priv->clk);
Masahiro Yamada30b5d9a2018-04-20 18:14:24 +0900989 if (ret) {
990 dev_err(dev, "failed to enable host clock\n");
991 return ret;
992 }
993
Marek Vasut4a66d4e2020-04-04 12:45:06 +0200994 priv->quirks = quirks;
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200995 ret = tmio_sd_probe(dev, quirks);
Marek Vasutd34bd2d2018-06-13 08:02:55 +0200996
997 renesas_sdhi_filter_caps(dev);
998
Marek Vasut50aa1d92018-06-13 08:02:55 +0200999#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
1000 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
1001 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
Marek Vasut52e17962018-10-28 15:30:06 +01001002 if (!ret && (priv->caps & TMIO_SD_CAP_RCAR_UHS))
Marek Vasut65186972018-08-30 15:27:26 +02001003 renesas_sdhi_reset_tuning(priv);
Marek Vasutf63968b2018-04-08 19:09:17 +02001004#endif
1005 return ret;
Marek Vasutc769e602018-04-08 17:45:23 +02001006}
1007
Marek Vasute94cad92018-04-08 15:22:58 +02001008U_BOOT_DRIVER(renesas_sdhi) = {
1009 .name = "renesas-sdhi",
1010 .id = UCLASS_MMC,
1011 .of_match = renesas_sdhi_match,
Marek Vasutcb0b6b02018-04-13 23:51:33 +02001012 .bind = tmio_sd_bind,
Marek Vasutc769e602018-04-08 17:45:23 +02001013 .probe = renesas_sdhi_probe,
Marek Vasutcb0b6b02018-04-13 23:51:33 +02001014 .priv_auto_alloc_size = sizeof(struct tmio_sd_priv),
1015 .platdata_auto_alloc_size = sizeof(struct tmio_sd_plat),
Marek Vasute94cad92018-04-08 15:22:58 +02001016 .ops = &renesas_sdhi_ops,
1017};