blob: 231a78178cf7d8bf4abc44d9d692110c138f8429 [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
695static int renesas_sdhi_addr_aligned(struct bounce_buffer *state)
696{
697 uintptr_t ubuf = (uintptr_t)state->user_buffer;
698
699 /* Check if start is aligned */
700 if (!IS_ALIGNED(ubuf, RENESAS_SDHI_DMA_ALIGNMENT)) {
701 debug("Unaligned buffer address %p\n", state->user_buffer);
702 return 0;
703 }
704
705 /* Check if length is aligned */
706 if (state->len != state->len_aligned) {
707 debug("Unaligned buffer length %zu\n", state->len);
708 return 0;
709 }
710
711#ifdef CONFIG_PHYS_64BIT
712 /* Check if below 32bit boundary */
713 if ((ubuf >> 32) || (ubuf + state->len_aligned) >> 32) {
714 debug("Buffer above 32bit boundary %p-%p\n",
715 state->user_buffer,
716 state->user_buffer + state->len_aligned);
717 return 0;
718 }
719#endif
720
721 /* Aligned */
722 return 1;
723}
724
Marek Vasutb5900a52019-05-19 03:47:07 +0200725static int renesas_sdhi_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
726 struct mmc_data *data)
727{
Marek Vasutd2661d82020-04-04 12:45:04 +0200728 struct bounce_buffer bbstate;
729 unsigned int bbflags;
730 bool bbok = false;
731 size_t len;
732 void *buf;
Marek Vasutb5900a52019-05-19 03:47:07 +0200733 int ret;
734
Marek Vasutd2661d82020-04-04 12:45:04 +0200735 if (data) {
736 if (data->flags & MMC_DATA_READ) {
737 buf = data->dest;
738 bbflags = GEN_BB_WRITE;
739 } else {
740 buf = (void *)data->src;
741 bbflags = GEN_BB_READ;
742 }
743 len = data->blocks * data->blocksize;
744
745 ret = bounce_buffer_start_extalign(&bbstate, buf, len, bbflags,
746 RENESAS_SDHI_DMA_ALIGNMENT,
747 renesas_sdhi_addr_aligned);
748 /*
749 * If the amount of data to transfer is too large, we can get
750 * -ENOMEM when starting the bounce buffer. If that happens,
751 * fall back to PIO as it was before, otherwise use the BB.
752 */
753 if (!ret) {
754 bbok = true;
755 if (data->flags & MMC_DATA_READ)
756 data->dest = bbstate.bounce_buffer;
757 else
758 data->src = bbstate.bounce_buffer;
759 }
760 }
761
Marek Vasutb5900a52019-05-19 03:47:07 +0200762 ret = tmio_sd_send_cmd(dev, cmd, data);
Marek Vasutd2661d82020-04-04 12:45:04 +0200763
764 if (data && bbok) {
765 buf = bbstate.user_buffer;
766
767 bounce_buffer_stop(&bbstate);
768
769 if (data->flags & MMC_DATA_READ)
770 data->dest = buf;
771 else
772 data->src = buf;
773 }
774
Marek Vasutb5900a52019-05-19 03:47:07 +0200775 if (ret)
776 return ret;
777
778#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
779 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
780 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
781 struct tmio_sd_priv *priv = dev_get_priv(dev);
782
Marek Vasut69000662019-11-23 13:36:23 +0100783 renesas_sdhi_check_scc_error(dev);
784
Marek Vasutb5900a52019-05-19 03:47:07 +0200785 if (cmd->cmdidx == MMC_CMD_SEND_STATUS)
786 renesas_sdhi_adjust_hs400_mode_enable(priv);
787#endif
788
789 return 0;
790}
791
Marek Vasute94cad92018-04-08 15:22:58 +0200792static const struct dm_mmc_ops renesas_sdhi_ops = {
Marek Vasutb5900a52019-05-19 03:47:07 +0200793 .send_cmd = renesas_sdhi_send_cmd,
Marek Vasutf63968b2018-04-08 19:09:17 +0200794 .set_ios = renesas_sdhi_set_ios,
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200795 .get_cd = tmio_sd_get_cd,
Marek Vasut50aa1d92018-06-13 08:02:55 +0200796#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
797 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
798 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
Marek Vasutf63968b2018-04-08 19:09:17 +0200799 .execute_tuning = renesas_sdhi_execute_tuning,
800#endif
Marek Vasut2fc10752018-10-28 19:28:56 +0100801#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
802 .wait_dat0 = renesas_sdhi_wait_dat0,
803#endif
Marek Vasute94cad92018-04-08 15:22:58 +0200804};
805
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200806#define RENESAS_GEN2_QUIRKS TMIO_SD_CAP_RCAR_GEN2
Marek Vasutf98833d2018-04-08 18:49:52 +0200807#define RENESAS_GEN3_QUIRKS \
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200808 TMIO_SD_CAP_64BIT | TMIO_SD_CAP_RCAR_GEN3 | TMIO_SD_CAP_RCAR_UHS
Marek Vasutf98833d2018-04-08 18:49:52 +0200809
Marek Vasute94cad92018-04-08 15:22:58 +0200810static const struct udevice_id renesas_sdhi_match[] = {
Marek Vasutf98833d2018-04-08 18:49:52 +0200811 { .compatible = "renesas,sdhi-r8a7790", .data = RENESAS_GEN2_QUIRKS },
812 { .compatible = "renesas,sdhi-r8a7791", .data = RENESAS_GEN2_QUIRKS },
813 { .compatible = "renesas,sdhi-r8a7792", .data = RENESAS_GEN2_QUIRKS },
814 { .compatible = "renesas,sdhi-r8a7793", .data = RENESAS_GEN2_QUIRKS },
815 { .compatible = "renesas,sdhi-r8a7794", .data = RENESAS_GEN2_QUIRKS },
816 { .compatible = "renesas,sdhi-r8a7795", .data = RENESAS_GEN3_QUIRKS },
817 { .compatible = "renesas,sdhi-r8a7796", .data = RENESAS_GEN3_QUIRKS },
818 { .compatible = "renesas,sdhi-r8a77965", .data = RENESAS_GEN3_QUIRKS },
819 { .compatible = "renesas,sdhi-r8a77970", .data = RENESAS_GEN3_QUIRKS },
Marek Vasutd6291522018-04-26 13:19:29 +0200820 { .compatible = "renesas,sdhi-r8a77990", .data = RENESAS_GEN3_QUIRKS },
Marek Vasutf98833d2018-04-08 18:49:52 +0200821 { .compatible = "renesas,sdhi-r8a77995", .data = RENESAS_GEN3_QUIRKS },
Marek Vasute94cad92018-04-08 15:22:58 +0200822 { /* sentinel */ }
823};
824
Marek Vasut8ec6a042018-06-13 08:02:55 +0200825static ulong renesas_sdhi_clk_get_rate(struct tmio_sd_priv *priv)
826{
827 return clk_get_rate(&priv->clk);
828}
829
Marek Vasutd34bd2d2018-06-13 08:02:55 +0200830static void renesas_sdhi_filter_caps(struct udevice *dev)
831{
Marek Vasutd34bd2d2018-06-13 08:02:55 +0200832 struct tmio_sd_priv *priv = dev_get_priv(dev);
833
834 if (!(priv->caps & TMIO_SD_CAP_RCAR_GEN3))
835 return;
836
Marek Vasut56b0bb92019-11-23 13:36:25 +0100837#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
838 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
839 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
840 struct tmio_sd_plat *plat = dev_get_platdata(dev);
841
842 /* HS400 is not supported on H3 ES1.x and M3W ES1.0, ES1.1 */
Marek Vasutd34bd2d2018-06-13 08:02:55 +0200843 if (((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7795) &&
844 (rmobile_get_cpu_rev_integer() <= 1)) ||
845 ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7796) &&
846 (rmobile_get_cpu_rev_integer() == 1) &&
Marek Vasut56b0bb92019-11-23 13:36:25 +0100847 (rmobile_get_cpu_rev_fraction() < 2)))
Marek Vasutd34bd2d2018-06-13 08:02:55 +0200848 plat->cfg.host_caps &= ~MMC_MODE_HS400;
Marek Vasut50aa1d92018-06-13 08:02:55 +0200849
Marek Vasut1bdcb832019-11-23 13:36:24 +0100850 /* H3 ES2.0, ES3.0 and M3W ES1.2 and M3N bad taps */
851 if (((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7795) &&
852 (rmobile_get_cpu_rev_integer() >= 2)) ||
853 ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7796) &&
854 (rmobile_get_cpu_rev_integer() == 1) &&
855 (rmobile_get_cpu_rev_fraction() == 2)) ||
856 (rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A77965))
857 priv->hs400_bad_tap = BIT(2) | BIT(3) | BIT(6) | BIT(7);
858
Marek Vasut56b0bb92019-11-23 13:36:25 +0100859 /* H3 ES3.0 can use HS400 with manual adjustment */
860 if ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7795) &&
861 (rmobile_get_cpu_rev_integer() >= 3)) {
862 priv->adjust_hs400_enable = true;
863 priv->adjust_hs400_offset = 0;
864 priv->adjust_hs400_calib_table =
865 r8a7795_calib_table[!rmobile_is_gen3_mmc0(priv)];
866 }
867
868 /* M3W ES1.2 can use HS400 with manual adjustment */
869 if ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7796) &&
870 (rmobile_get_cpu_rev_integer() == 1) &&
871 (rmobile_get_cpu_rev_fraction() == 2)) {
872 priv->adjust_hs400_enable = true;
873 priv->adjust_hs400_offset = 3;
874 priv->adjust_hs400_calib_table =
875 r8a7796_rev1_calib_table[!rmobile_is_gen3_mmc0(priv)];
876 }
877
Marek Vasut1bdcb832019-11-23 13:36:24 +0100878 /* M3W ES1.x for x>2 can use HS400 with manual adjustment and taps */
Marek Vasutb5900a52019-05-19 03:47:07 +0200879 if ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7796) &&
880 (rmobile_get_cpu_rev_integer() == 1) &&
881 (rmobile_get_cpu_rev_fraction() > 2)) {
882 priv->adjust_hs400_enable = true;
Marek Vasut56b0bb92019-11-23 13:36:25 +0100883 priv->adjust_hs400_offset = 0;
Marek Vasut1bdcb832019-11-23 13:36:24 +0100884 priv->hs400_bad_tap = BIT(1) | BIT(3) | BIT(5) | BIT(7);
Marek Vasut56b0bb92019-11-23 13:36:25 +0100885 priv->adjust_hs400_calib_table =
886 r8a7796_rev3_calib_table[!rmobile_is_gen3_mmc0(priv)];
Marek Vasutb5900a52019-05-19 03:47:07 +0200887 }
888
889 /* M3N can use HS400 with manual adjustment */
890 if (rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A77965) {
891 priv->adjust_hs400_enable = true;
Marek Vasute5d3f3d2019-11-23 13:36:21 +0100892 priv->adjust_hs400_offset = 3;
Marek Vasut56b0bb92019-11-23 13:36:25 +0100893 priv->adjust_hs400_calib_table =
894 r8a77965_calib_table[!rmobile_is_gen3_mmc0(priv)];
Marek Vasutb5900a52019-05-19 03:47:07 +0200895 }
896
897 /* E3 can use HS400 with manual adjustment */
898 if (rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A77990) {
899 priv->adjust_hs400_enable = true;
Marek Vasute5d3f3d2019-11-23 13:36:21 +0100900 priv->adjust_hs400_offset = 3;
Marek Vasut56b0bb92019-11-23 13:36:25 +0100901 priv->adjust_hs400_calib_table =
902 r8a77990_calib_table[!rmobile_is_gen3_mmc0(priv)];
Marek Vasutb5900a52019-05-19 03:47:07 +0200903 }
904
Marek Vasut81099882019-11-23 13:36:19 +0100905 /* H3 ES1.x, ES2.0 and M3W ES1.0, ES1.1, ES1.2 uses 4 tuning taps */
906 if (((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7795) &&
907 (rmobile_get_cpu_rev_integer() <= 2)) ||
908 ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7796) &&
909 (rmobile_get_cpu_rev_integer() == 1) &&
910 (rmobile_get_cpu_rev_fraction() <= 2)))
Marek Vasut50aa1d92018-06-13 08:02:55 +0200911 priv->nrtaps = 4;
912 else
913 priv->nrtaps = 8;
Marek Vasut56b0bb92019-11-23 13:36:25 +0100914#endif
Marek Vasut992bcf42019-01-11 23:45:54 +0100915 /* H3 ES1.x and M3W ES1.0 uses bit 17 for DTRAEND */
916 if (((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7795) &&
917 (rmobile_get_cpu_rev_integer() <= 1)) ||
918 ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7796) &&
919 (rmobile_get_cpu_rev_integer() == 1) &&
920 (rmobile_get_cpu_rev_fraction() == 0)))
921 priv->read_poll_flag = TMIO_SD_DMA_INFO1_END_RD;
922 else
923 priv->read_poll_flag = TMIO_SD_DMA_INFO1_END_RD2;
Marek Vasutd34bd2d2018-06-13 08:02:55 +0200924}
925
Marek Vasutc769e602018-04-08 17:45:23 +0200926static int renesas_sdhi_probe(struct udevice *dev)
927{
Masahiro Yamada30b5d9a2018-04-20 18:14:24 +0900928 struct tmio_sd_priv *priv = dev_get_priv(dev);
Marek Vasutc769e602018-04-08 17:45:23 +0200929 u32 quirks = dev_get_driver_data(dev);
Marek Vasut7cf7ef82018-04-08 18:14:22 +0200930 struct fdt_resource reg_res;
931 DECLARE_GLOBAL_DATA_PTR;
932 int ret;
933
Marek Vasut8ec6a042018-06-13 08:02:55 +0200934 priv->clk_get_rate = renesas_sdhi_clk_get_rate;
935
Marek Vasutf98833d2018-04-08 18:49:52 +0200936 if (quirks == RENESAS_GEN2_QUIRKS) {
937 ret = fdt_get_resource(gd->fdt_blob, dev_of_offset(dev),
938 "reg", 0, &reg_res);
939 if (ret < 0) {
940 dev_err(dev, "\"reg\" resource not found, ret=%i\n",
941 ret);
942 return ret;
943 }
Marek Vasut7cf7ef82018-04-08 18:14:22 +0200944
Marek Vasutf98833d2018-04-08 18:49:52 +0200945 if (fdt_resource_size(&reg_res) == 0x100)
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200946 quirks |= TMIO_SD_CAP_16BIT;
Marek Vasutf98833d2018-04-08 18:49:52 +0200947 }
Marek Vasutc769e602018-04-08 17:45:23 +0200948
Marek Vasut8ec6a042018-06-13 08:02:55 +0200949 ret = clk_get_by_index(dev, 0, &priv->clk);
Masahiro Yamada30b5d9a2018-04-20 18:14:24 +0900950 if (ret < 0) {
951 dev_err(dev, "failed to get host clock\n");
952 return ret;
953 }
954
955 /* set to max rate */
Marek Vasut8ec6a042018-06-13 08:02:55 +0200956 ret = clk_set_rate(&priv->clk, 200000000);
957 if (ret < 0) {
Masahiro Yamada30b5d9a2018-04-20 18:14:24 +0900958 dev_err(dev, "failed to set rate for host clock\n");
Marek Vasut8ec6a042018-06-13 08:02:55 +0200959 clk_free(&priv->clk);
960 return ret;
Masahiro Yamada30b5d9a2018-04-20 18:14:24 +0900961 }
962
Marek Vasut8ec6a042018-06-13 08:02:55 +0200963 ret = clk_enable(&priv->clk);
Masahiro Yamada30b5d9a2018-04-20 18:14:24 +0900964 if (ret) {
965 dev_err(dev, "failed to enable host clock\n");
966 return ret;
967 }
968
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200969 ret = tmio_sd_probe(dev, quirks);
Marek Vasutd34bd2d2018-06-13 08:02:55 +0200970
971 renesas_sdhi_filter_caps(dev);
972
Marek Vasut50aa1d92018-06-13 08:02:55 +0200973#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
974 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
975 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
Marek Vasut52e17962018-10-28 15:30:06 +0100976 if (!ret && (priv->caps & TMIO_SD_CAP_RCAR_UHS))
Marek Vasut65186972018-08-30 15:27:26 +0200977 renesas_sdhi_reset_tuning(priv);
Marek Vasutf63968b2018-04-08 19:09:17 +0200978#endif
979 return ret;
Marek Vasutc769e602018-04-08 17:45:23 +0200980}
981
Marek Vasute94cad92018-04-08 15:22:58 +0200982U_BOOT_DRIVER(renesas_sdhi) = {
983 .name = "renesas-sdhi",
984 .id = UCLASS_MMC,
985 .of_match = renesas_sdhi_match,
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200986 .bind = tmio_sd_bind,
Marek Vasutc769e602018-04-08 17:45:23 +0200987 .probe = renesas_sdhi_probe,
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200988 .priv_auto_alloc_size = sizeof(struct tmio_sd_priv),
989 .platdata_auto_alloc_size = sizeof(struct tmio_sd_plat),
Marek Vasute94cad92018-04-08 15:22:58 +0200990 .ops = &renesas_sdhi_ops,
991};