blob: 7c53aa221e506eff5bd6caa57d42ee3bce3864b8 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marek Vasut06485cf2018-04-08 15:22:58 +02002/*
3 * Copyright (C) 2018 Marek Vasut <marek.vasut@gmail.com>
Marek Vasut06485cf2018-04-08 15:22:58 +02004 */
5
6#include <common.h>
7#include <clk.h>
8#include <fdtdec.h>
9#include <mmc.h>
10#include <dm.h>
11#include <linux/compat.h>
12#include <linux/dma-direction.h>
13#include <linux/io.h>
14#include <linux/sizes.h>
15#include <power/regulator.h>
16#include <asm/unaligned.h>
17
Marek Vasutfd83e762018-04-13 23:51:33 +020018#include "tmio-common.h"
Marek Vasut06485cf2018-04-08 15:22:58 +020019
Marek Vasut10d77ed2018-06-13 08:02:55 +020020#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
21 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
22 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
Marek Vasute0781e42018-04-08 19:09:17 +020023
24/* SCC registers */
25#define RENESAS_SDHI_SCC_DTCNTL 0x800
Marek Vasutb1d442b2019-05-19 02:33:06 +020026#define RENESAS_SDHI_SCC_DTCNTL_TAPEN BIT(0)
27#define RENESAS_SDHI_SCC_DTCNTL_TAPNUM_SHIFT 16
28#define RENESAS_SDHI_SCC_DTCNTL_TAPNUM_MASK 0xff
Marek Vasute0781e42018-04-08 19:09:17 +020029#define RENESAS_SDHI_SCC_TAPSET 0x804
30#define RENESAS_SDHI_SCC_DT2FF 0x808
31#define RENESAS_SDHI_SCC_CKSEL 0x80c
Marek Vasutb1d442b2019-05-19 02:33:06 +020032#define RENESAS_SDHI_SCC_CKSEL_DTSEL BIT(0)
33#define RENESAS_SDHI_SCC_RVSCNTL 0x810
34#define RENESAS_SDHI_SCC_RVSCNTL_RVSEN BIT(0)
Marek Vasute0781e42018-04-08 19:09:17 +020035#define RENESAS_SDHI_SCC_RVSREQ 0x814
Marek Vasutb1d442b2019-05-19 02:33:06 +020036#define RENESAS_SDHI_SCC_RVSREQ_RVSERR BIT(2)
Marek Vasute0781e42018-04-08 19:09:17 +020037#define RENESAS_SDHI_SCC_SMPCMP 0x818
Marek Vasutb1d442b2019-05-19 02:33:06 +020038#define RENESAS_SDHI_SCC_TMPPORT2 0x81c
39#define RENESAS_SDHI_SCC_TMPPORT2_HS400EN BIT(31)
40#define RENESAS_SDHI_SCC_TMPPORT2_HS400OSEL BIT(4)
Marek Vasutfee0c682019-05-19 03:47:07 +020041#define RENESAS_SDHI_SCC_TMPPORT3 0x828
42#define RENESAS_SDHI_SCC_TMPPORT3_OFFSET_0 3
43#define RENESAS_SDHI_SCC_TMPPORT3_OFFSET_1 2
44#define RENESAS_SDHI_SCC_TMPPORT3_OFFSET_2 1
45#define RENESAS_SDHI_SCC_TMPPORT3_OFFSET_3 0
46#define RENESAS_SDHI_SCC_TMPPORT3_OFFSET_MASK 0x3
47#define RENESAS_SDHI_SCC_TMPPORT4 0x82c
48#define RENESAS_SDHI_SCC_TMPPORT4_DLL_ACC_START BIT(0)
49#define RENESAS_SDHI_SCC_TMPPORT5 0x830
50#define RENESAS_SDHI_SCC_TMPPORT5_DLL_RW_SEL_R BIT(8)
51#define RENESAS_SDHI_SCC_TMPPORT5_DLL_RW_SEL_W (0 << 8)
52#define RENESAS_SDHI_SCC_TMPPORT5_DLL_ADR_MASK 0x3F
53#define RENESAS_SDHI_SCC_TMPPORT6 0x834
54#define RENESAS_SDHI_SCC_TMPPORT7 0x838
55#define RENESAS_SDHI_SCC_TMPPORT_DISABLE_WP_CODE 0xa5000000
56#define RENESAS_SDHI_SCC_TMPPORT_CALIB_CODE_MASK 0x1f
57#define RENESAS_SDHI_SCC_TMPPORT_MANUAL_MODE BIT(7)
Marek Vasute0781e42018-04-08 19:09:17 +020058
59#define RENESAS_SDHI_MAX_TAP 3
60
Marek Vasutfee0c682019-05-19 03:47:07 +020061static u32 sd_scc_tmpport_read32(struct tmio_sd_priv *priv, u32 addr)
62{
63 /* read mode */
64 tmio_sd_writel(priv, RENESAS_SDHI_SCC_TMPPORT5_DLL_RW_SEL_R |
65 (RENESAS_SDHI_SCC_TMPPORT5_DLL_ADR_MASK & addr),
66 RENESAS_SDHI_SCC_TMPPORT5);
67
68 /* access start and stop */
69 tmio_sd_writel(priv, RENESAS_SDHI_SCC_TMPPORT4_DLL_ACC_START,
70 RENESAS_SDHI_SCC_TMPPORT4);
71 tmio_sd_writel(priv, 0, RENESAS_SDHI_SCC_TMPPORT4);
72
73 return tmio_sd_readl(priv, RENESAS_SDHI_SCC_TMPPORT7);
74}
75
76static void sd_scc_tmpport_write32(struct tmio_sd_priv *priv, u32 addr, u32 val)
77{
78 /* write mode */
79 tmio_sd_writel(priv, RENESAS_SDHI_SCC_TMPPORT5_DLL_RW_SEL_W |
80 (RENESAS_SDHI_SCC_TMPPORT5_DLL_ADR_MASK & addr),
81 RENESAS_SDHI_SCC_TMPPORT5);
82 tmio_sd_writel(priv, val, RENESAS_SDHI_SCC_TMPPORT6);
83
84 /* access start and stop */
85 tmio_sd_writel(priv, RENESAS_SDHI_SCC_TMPPORT4_DLL_ACC_START,
86 RENESAS_SDHI_SCC_TMPPORT4);
87 tmio_sd_writel(priv, 0, RENESAS_SDHI_SCC_TMPPORT4);
88}
89
90static void renesas_sdhi_adjust_hs400_mode_enable(struct tmio_sd_priv *priv)
91{
92 u32 calib_code;
93
94 if (!priv->adjust_hs400_enable)
95 return;
96
97 if (!priv->needs_adjust_hs400)
98 return;
99
100 /*
101 * Enabled Manual adjust HS400 mode
102 *
103 * 1) Disabled Write Protect
104 * W(addr=0x00, WP_DISABLE_CODE)
105 * 2) Read Calibration code and adjust
106 * R(addr=0x26) - adjust value
107 * 3) Enabled Manual Calibration
108 * W(addr=0x22, manual mode | Calibration code)
109 * 4) Set Offset value to TMPPORT3 Reg
110 */
111 sd_scc_tmpport_write32(priv, 0x00,
112 RENESAS_SDHI_SCC_TMPPORT_DISABLE_WP_CODE);
113 calib_code = sd_scc_tmpport_read32(priv, 0x26);
114 calib_code &= RENESAS_SDHI_SCC_TMPPORT_CALIB_CODE_MASK;
115 if (calib_code > priv->adjust_hs400_calibrate)
116 calib_code -= priv->adjust_hs400_calibrate;
117 else
118 calib_code = 0;
119 sd_scc_tmpport_write32(priv, 0x22,
120 RENESAS_SDHI_SCC_TMPPORT_MANUAL_MODE |
121 calib_code);
122 tmio_sd_writel(priv, priv->adjust_hs400_offset,
123 RENESAS_SDHI_SCC_TMPPORT3);
124
125 /* Clear flag */
126 priv->needs_adjust_hs400 = false;
127}
128
129static void renesas_sdhi_adjust_hs400_mode_disable(struct tmio_sd_priv *priv)
130{
131
132 /* Disabled Manual adjust HS400 mode
133 *
134 * 1) Disabled Write Protect
135 * W(addr=0x00, WP_DISABLE_CODE)
136 * 2) Disabled Manual Calibration
137 * W(addr=0x22, 0)
138 * 3) Clear offset value to TMPPORT3 Reg
139 */
140 sd_scc_tmpport_write32(priv, 0x00,
141 RENESAS_SDHI_SCC_TMPPORT_DISABLE_WP_CODE);
142 sd_scc_tmpport_write32(priv, 0x22, 0);
143 tmio_sd_writel(priv, 0, RENESAS_SDHI_SCC_TMPPORT3);
144}
145
Marek Vasutfd83e762018-04-13 23:51:33 +0200146static unsigned int renesas_sdhi_init_tuning(struct tmio_sd_priv *priv)
Marek Vasute0781e42018-04-08 19:09:17 +0200147{
148 u32 reg;
149
150 /* Initialize SCC */
Marek Vasutfd83e762018-04-13 23:51:33 +0200151 tmio_sd_writel(priv, 0, TMIO_SD_INFO1);
Marek Vasute0781e42018-04-08 19:09:17 +0200152
Marek Vasutfd83e762018-04-13 23:51:33 +0200153 reg = tmio_sd_readl(priv, TMIO_SD_CLKCTL);
154 reg &= ~TMIO_SD_CLKCTL_SCLKEN;
155 tmio_sd_writel(priv, reg, TMIO_SD_CLKCTL);
Marek Vasute0781e42018-04-08 19:09:17 +0200156
157 /* Set sampling clock selection range */
Marek Vasutda4873d2018-06-13 08:02:55 +0200158 tmio_sd_writel(priv, (0x8 << RENESAS_SDHI_SCC_DTCNTL_TAPNUM_SHIFT) |
159 RENESAS_SDHI_SCC_DTCNTL_TAPEN,
160 RENESAS_SDHI_SCC_DTCNTL);
Marek Vasute0781e42018-04-08 19:09:17 +0200161
Marek Vasutfd83e762018-04-13 23:51:33 +0200162 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_CKSEL);
Marek Vasute0781e42018-04-08 19:09:17 +0200163 reg |= RENESAS_SDHI_SCC_CKSEL_DTSEL;
Marek Vasutfd83e762018-04-13 23:51:33 +0200164 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_CKSEL);
Marek Vasute0781e42018-04-08 19:09:17 +0200165
Marek Vasutfd83e762018-04-13 23:51:33 +0200166 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSCNTL);
Marek Vasute0781e42018-04-08 19:09:17 +0200167 reg &= ~RENESAS_SDHI_SCC_RVSCNTL_RVSEN;
Marek Vasutfd83e762018-04-13 23:51:33 +0200168 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_RVSCNTL);
Marek Vasute0781e42018-04-08 19:09:17 +0200169
Marek Vasutfd83e762018-04-13 23:51:33 +0200170 tmio_sd_writel(priv, 0x300 /* scc_tappos */,
Marek Vasute0781e42018-04-08 19:09:17 +0200171 RENESAS_SDHI_SCC_DT2FF);
172
Marek Vasutfd83e762018-04-13 23:51:33 +0200173 reg = tmio_sd_readl(priv, TMIO_SD_CLKCTL);
174 reg |= TMIO_SD_CLKCTL_SCLKEN;
175 tmio_sd_writel(priv, reg, TMIO_SD_CLKCTL);
Marek Vasute0781e42018-04-08 19:09:17 +0200176
177 /* Read TAPNUM */
Marek Vasutfd83e762018-04-13 23:51:33 +0200178 return (tmio_sd_readl(priv, RENESAS_SDHI_SCC_DTCNTL) >>
Marek Vasute0781e42018-04-08 19:09:17 +0200179 RENESAS_SDHI_SCC_DTCNTL_TAPNUM_SHIFT) &
180 RENESAS_SDHI_SCC_DTCNTL_TAPNUM_MASK;
181}
182
Marek Vasutfd83e762018-04-13 23:51:33 +0200183static void renesas_sdhi_reset_tuning(struct tmio_sd_priv *priv)
Marek Vasute0781e42018-04-08 19:09:17 +0200184{
185 u32 reg;
186
187 /* Reset SCC */
Marek Vasutfd83e762018-04-13 23:51:33 +0200188 reg = tmio_sd_readl(priv, TMIO_SD_CLKCTL);
189 reg &= ~TMIO_SD_CLKCTL_SCLKEN;
190 tmio_sd_writel(priv, reg, TMIO_SD_CLKCTL);
Marek Vasute0781e42018-04-08 19:09:17 +0200191
Marek Vasutfd83e762018-04-13 23:51:33 +0200192 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_CKSEL);
Marek Vasute0781e42018-04-08 19:09:17 +0200193 reg &= ~RENESAS_SDHI_SCC_CKSEL_DTSEL;
Marek Vasutfd83e762018-04-13 23:51:33 +0200194 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_CKSEL);
Marek Vasute0781e42018-04-08 19:09:17 +0200195
Marek Vasutefea7a82018-06-13 08:02:55 +0200196 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_TMPPORT2);
197 reg &= ~(RENESAS_SDHI_SCC_TMPPORT2_HS400EN |
198 RENESAS_SDHI_SCC_TMPPORT2_HS400OSEL);
199 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_TMPPORT2);
200
Marek Vasutfee0c682019-05-19 03:47:07 +0200201 /* Disable HS400 mode adjustment */
202 renesas_sdhi_adjust_hs400_mode_disable(priv);
203
Marek Vasutfd83e762018-04-13 23:51:33 +0200204 reg = tmio_sd_readl(priv, TMIO_SD_CLKCTL);
205 reg |= TMIO_SD_CLKCTL_SCLKEN;
206 tmio_sd_writel(priv, reg, TMIO_SD_CLKCTL);
Marek Vasute0781e42018-04-08 19:09:17 +0200207
Marek Vasutfd83e762018-04-13 23:51:33 +0200208 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSCNTL);
Marek Vasute0781e42018-04-08 19:09:17 +0200209 reg &= ~RENESAS_SDHI_SCC_RVSCNTL_RVSEN;
Marek Vasutfd83e762018-04-13 23:51:33 +0200210 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_RVSCNTL);
Marek Vasute0781e42018-04-08 19:09:17 +0200211
Marek Vasutfd83e762018-04-13 23:51:33 +0200212 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSCNTL);
Marek Vasute0781e42018-04-08 19:09:17 +0200213 reg &= ~RENESAS_SDHI_SCC_RVSCNTL_RVSEN;
Marek Vasutfd83e762018-04-13 23:51:33 +0200214 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_RVSCNTL);
Marek Vasute0781e42018-04-08 19:09:17 +0200215}
216
Marek Vasut10d77ed2018-06-13 08:02:55 +0200217static int renesas_sdhi_hs400(struct udevice *dev)
218{
219 struct tmio_sd_priv *priv = dev_get_priv(dev);
220 struct mmc *mmc = mmc_get_mmc_dev(dev);
221 bool hs400 = (mmc->selected_mode == MMC_HS_400);
222 int ret, taps = hs400 ? priv->nrtaps : 8;
223 u32 reg;
224
225 if (taps == 4) /* HS400 on 4tap SoC needs different clock */
226 ret = clk_set_rate(&priv->clk, 400000000);
227 else
228 ret = clk_set_rate(&priv->clk, 200000000);
229 if (ret < 0)
230 return ret;
231
232 tmio_sd_writel(priv, 0, RENESAS_SDHI_SCC_RVSREQ);
233
234 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_TMPPORT2);
235 if (hs400) {
236 reg |= RENESAS_SDHI_SCC_TMPPORT2_HS400EN |
237 RENESAS_SDHI_SCC_TMPPORT2_HS400OSEL;
238 } else {
239 reg &= ~(RENESAS_SDHI_SCC_TMPPORT2_HS400EN |
240 RENESAS_SDHI_SCC_TMPPORT2_HS400OSEL);
241 }
242
243 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_TMPPORT2);
244
Marek Vasutfee0c682019-05-19 03:47:07 +0200245 /* Disable HS400 mode adjustment */
246 if (!hs400)
247 renesas_sdhi_adjust_hs400_mode_disable(priv);
248
Marek Vasut3d42a072019-02-19 19:32:28 +0100249 tmio_sd_writel(priv, (0x8 << RENESAS_SDHI_SCC_DTCNTL_TAPNUM_SHIFT) |
Marek Vasut10d77ed2018-06-13 08:02:55 +0200250 RENESAS_SDHI_SCC_DTCNTL_TAPEN,
251 RENESAS_SDHI_SCC_DTCNTL);
252
253 if (taps == 4) {
254 tmio_sd_writel(priv, priv->tap_set >> 1,
255 RENESAS_SDHI_SCC_TAPSET);
256 } else {
257 tmio_sd_writel(priv, priv->tap_set, RENESAS_SDHI_SCC_TAPSET);
258 }
259
Marek Vasut1a7b0552019-02-14 15:16:15 +0100260 tmio_sd_writel(priv, hs400 ? 0x704 : 0x300,
261 RENESAS_SDHI_SCC_DT2FF);
262
Marek Vasut10d77ed2018-06-13 08:02:55 +0200263 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_CKSEL);
264 reg |= RENESAS_SDHI_SCC_CKSEL_DTSEL;
265 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_CKSEL);
266
267 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSCNTL);
268 reg |= RENESAS_SDHI_SCC_RVSCNTL_RVSEN;
269 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_RVSCNTL);
270
Marek Vasutfee0c682019-05-19 03:47:07 +0200271 /* Execute adjust hs400 offset after setting to HS400 mode */
272 if (hs400)
273 priv->needs_adjust_hs400 = true;
274
Marek Vasut10d77ed2018-06-13 08:02:55 +0200275 return 0;
276}
277
Marek Vasutfd83e762018-04-13 23:51:33 +0200278static void renesas_sdhi_prepare_tuning(struct tmio_sd_priv *priv,
Marek Vasute0781e42018-04-08 19:09:17 +0200279 unsigned long tap)
280{
281 /* Set sampling clock position */
Marek Vasutfd83e762018-04-13 23:51:33 +0200282 tmio_sd_writel(priv, tap, RENESAS_SDHI_SCC_TAPSET);
Marek Vasute0781e42018-04-08 19:09:17 +0200283}
284
Marek Vasutfd83e762018-04-13 23:51:33 +0200285static unsigned int renesas_sdhi_compare_scc_data(struct tmio_sd_priv *priv)
Marek Vasute0781e42018-04-08 19:09:17 +0200286{
287 /* Get comparison of sampling data */
Marek Vasutfd83e762018-04-13 23:51:33 +0200288 return tmio_sd_readl(priv, RENESAS_SDHI_SCC_SMPCMP);
Marek Vasute0781e42018-04-08 19:09:17 +0200289}
290
Marek Vasutfd83e762018-04-13 23:51:33 +0200291static int renesas_sdhi_select_tuning(struct tmio_sd_priv *priv,
Marek Vasute0781e42018-04-08 19:09:17 +0200292 unsigned int tap_num, unsigned int taps,
293 unsigned int smpcmp)
294{
295 unsigned long tap_cnt; /* counter of tuning success */
Marek Vasute0781e42018-04-08 19:09:17 +0200296 unsigned long tap_start;/* start position of tuning success */
297 unsigned long tap_end; /* end position of tuning success */
298 unsigned long ntap; /* temporary counter of tuning success */
299 unsigned long match_cnt;/* counter of matching data */
300 unsigned long i;
301 bool select = false;
302 u32 reg;
303
Marek Vasutfee0c682019-05-19 03:47:07 +0200304 priv->needs_adjust_hs400 = false;
305
Marek Vasute0781e42018-04-08 19:09:17 +0200306 /* Clear SCC_RVSREQ */
Marek Vasutfd83e762018-04-13 23:51:33 +0200307 tmio_sd_writel(priv, 0, RENESAS_SDHI_SCC_RVSREQ);
Marek Vasute0781e42018-04-08 19:09:17 +0200308
309 /* Merge the results */
310 for (i = 0; i < tap_num * 2; i++) {
311 if (!(taps & BIT(i))) {
312 taps &= ~BIT(i % tap_num);
313 taps &= ~BIT((i % tap_num) + tap_num);
314 }
315 if (!(smpcmp & BIT(i))) {
316 smpcmp &= ~BIT(i % tap_num);
317 smpcmp &= ~BIT((i % tap_num) + tap_num);
318 }
319 }
320
321 /*
322 * Find the longest consecutive run of successful probes. If that
323 * is more than RENESAS_SDHI_MAX_TAP probes long then use the
324 * center index as the tap.
325 */
326 tap_cnt = 0;
327 ntap = 0;
328 tap_start = 0;
329 tap_end = 0;
330 for (i = 0; i < tap_num * 2; i++) {
331 if (taps & BIT(i))
332 ntap++;
333 else {
334 if (ntap > tap_cnt) {
335 tap_start = i - ntap;
336 tap_end = i - 1;
337 tap_cnt = ntap;
338 }
339 ntap = 0;
340 }
341 }
342
343 if (ntap > tap_cnt) {
344 tap_start = i - ntap;
345 tap_end = i - 1;
346 tap_cnt = ntap;
347 }
348
349 /*
350 * If all of the TAP is OK, the sampling clock position is selected by
351 * identifying the change point of data.
352 */
353 if (tap_cnt == tap_num * 2) {
354 match_cnt = 0;
355 ntap = 0;
356 tap_start = 0;
357 tap_end = 0;
358 for (i = 0; i < tap_num * 2; i++) {
359 if (smpcmp & BIT(i))
360 ntap++;
361 else {
362 if (ntap > match_cnt) {
363 tap_start = i - ntap;
364 tap_end = i - 1;
365 match_cnt = ntap;
366 }
367 ntap = 0;
368 }
369 }
370 if (ntap > match_cnt) {
371 tap_start = i - ntap;
372 tap_end = i - 1;
373 match_cnt = ntap;
374 }
375 if (match_cnt)
376 select = true;
377 } else if (tap_cnt >= RENESAS_SDHI_MAX_TAP)
378 select = true;
379
380 if (select)
Marek Vasut1ebb9d62018-06-13 08:02:55 +0200381 priv->tap_set = ((tap_start + tap_end) / 2) % tap_num;
Marek Vasute0781e42018-04-08 19:09:17 +0200382 else
383 return -EIO;
384
385 /* Set SCC */
Marek Vasut1ebb9d62018-06-13 08:02:55 +0200386 tmio_sd_writel(priv, priv->tap_set, RENESAS_SDHI_SCC_TAPSET);
Marek Vasute0781e42018-04-08 19:09:17 +0200387
388 /* Enable auto re-tuning */
Marek Vasutfd83e762018-04-13 23:51:33 +0200389 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSCNTL);
Marek Vasute0781e42018-04-08 19:09:17 +0200390 reg |= RENESAS_SDHI_SCC_RVSCNTL_RVSEN;
Marek Vasutfd83e762018-04-13 23:51:33 +0200391 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_RVSCNTL);
Marek Vasute0781e42018-04-08 19:09:17 +0200392
393 return 0;
394}
395
396int renesas_sdhi_execute_tuning(struct udevice *dev, uint opcode)
397{
Marek Vasutfd83e762018-04-13 23:51:33 +0200398 struct tmio_sd_priv *priv = dev_get_priv(dev);
Marek Vasute0781e42018-04-08 19:09:17 +0200399 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
400 struct mmc *mmc = upriv->mmc;
401 unsigned int tap_num;
402 unsigned int taps = 0, smpcmp = 0;
403 int i, ret = 0;
404 u32 caps;
405
406 /* Only supported on Renesas RCar */
Marek Vasutfd83e762018-04-13 23:51:33 +0200407 if (!(priv->caps & TMIO_SD_CAP_RCAR_UHS))
Marek Vasute0781e42018-04-08 19:09:17 +0200408 return -EINVAL;
409
410 /* clock tuning is not needed for upto 52MHz */
411 if (!((mmc->selected_mode == MMC_HS_200) ||
Marek Vasut10d77ed2018-06-13 08:02:55 +0200412 (mmc->selected_mode == MMC_HS_400) ||
Marek Vasute0781e42018-04-08 19:09:17 +0200413 (mmc->selected_mode == UHS_SDR104) ||
414 (mmc->selected_mode == UHS_SDR50)))
415 return 0;
416
417 tap_num = renesas_sdhi_init_tuning(priv);
418 if (!tap_num)
419 /* Tuning is not supported */
420 goto out;
421
422 if (tap_num * 2 >= sizeof(taps) * 8) {
423 dev_err(dev,
424 "Too many taps, skipping tuning. Please consider updating size of taps field of tmio_mmc_host\n");
425 goto out;
426 }
427
428 /* Issue CMD19 twice for each tap */
429 for (i = 0; i < 2 * tap_num; i++) {
430 renesas_sdhi_prepare_tuning(priv, i % tap_num);
431
432 /* Force PIO for the tuning */
433 caps = priv->caps;
Marek Vasutfd83e762018-04-13 23:51:33 +0200434 priv->caps &= ~TMIO_SD_CAP_DMA_INTERNAL;
Marek Vasute0781e42018-04-08 19:09:17 +0200435
436 ret = mmc_send_tuning(mmc, opcode, NULL);
437
438 priv->caps = caps;
439
440 if (ret == 0)
441 taps |= BIT(i);
442
443 ret = renesas_sdhi_compare_scc_data(priv);
444 if (ret == 0)
445 smpcmp |= BIT(i);
446
447 mdelay(1);
448 }
449
450 ret = renesas_sdhi_select_tuning(priv, tap_num, taps, smpcmp);
451
452out:
453 if (ret < 0) {
454 dev_warn(dev, "Tuning procedure failed\n");
455 renesas_sdhi_reset_tuning(priv);
456 }
457
458 return ret;
459}
Marek Vasut10d77ed2018-06-13 08:02:55 +0200460#else
461static int renesas_sdhi_hs400(struct udevice *dev)
462{
463 return 0;
464}
Marek Vasute0781e42018-04-08 19:09:17 +0200465#endif
466
467static int renesas_sdhi_set_ios(struct udevice *dev)
468{
Marek Vasut10d77ed2018-06-13 08:02:55 +0200469 struct tmio_sd_priv *priv = dev_get_priv(dev);
470 u32 tmp;
471 int ret;
Marek Vasut33d38182018-04-09 20:47:31 +0200472
Marek Vasut10d77ed2018-06-13 08:02:55 +0200473 /* Stop the clock before changing its rate to avoid a glitch signal */
474 tmp = tmio_sd_readl(priv, TMIO_SD_CLKCTL);
475 tmp &= ~TMIO_SD_CLKCTL_SCLKEN;
476 tmio_sd_writel(priv, tmp, TMIO_SD_CLKCTL);
Marek Vasut33d38182018-04-09 20:47:31 +0200477
Marek Vasut10d77ed2018-06-13 08:02:55 +0200478 ret = renesas_sdhi_hs400(dev);
479 if (ret)
480 return ret;
Marek Vasute0781e42018-04-08 19:09:17 +0200481
Marek Vasut10d77ed2018-06-13 08:02:55 +0200482 ret = tmio_sd_set_ios(dev);
483
484 mdelay(10);
485
486#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
487 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
488 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
489 struct mmc *mmc = mmc_get_mmc_dev(dev);
490 if ((priv->caps & TMIO_SD_CAP_RCAR_UHS) &&
491 (mmc->selected_mode != UHS_SDR104) &&
492 (mmc->selected_mode != MMC_HS_200) &&
493 (mmc->selected_mode != MMC_HS_400)) {
Marek Vasut35773802018-10-28 15:30:06 +0100494 renesas_sdhi_reset_tuning(priv);
Marek Vasut10d77ed2018-06-13 08:02:55 +0200495 }
Marek Vasute0781e42018-04-08 19:09:17 +0200496#endif
497
498 return ret;
499}
500
Marek Vasut576a6d92018-10-28 19:28:56 +0100501#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
502static int renesas_sdhi_wait_dat0(struct udevice *dev, int state, int timeout)
503{
504 int ret = -ETIMEDOUT;
505 bool dat0_high;
506 bool target_dat0_high = !!state;
507 struct tmio_sd_priv *priv = dev_get_priv(dev);
508
509 timeout = DIV_ROUND_UP(timeout, 10); /* check every 10 us. */
510 while (timeout--) {
511 dat0_high = !!(tmio_sd_readl(priv, TMIO_SD_INFO2) & TMIO_SD_INFO2_DAT0);
512 if (dat0_high == target_dat0_high) {
513 ret = 0;
514 break;
515 }
516 udelay(10);
517 }
518
519 return ret;
520}
521#endif
522
Marek Vasutfee0c682019-05-19 03:47:07 +0200523static int renesas_sdhi_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
524 struct mmc_data *data)
525{
526 int ret;
527
528 ret = tmio_sd_send_cmd(dev, cmd, data);
529 if (ret)
530 return ret;
531
532#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
533 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
534 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
535 struct tmio_sd_priv *priv = dev_get_priv(dev);
536
537 if (cmd->cmdidx == MMC_CMD_SEND_STATUS)
538 renesas_sdhi_adjust_hs400_mode_enable(priv);
539#endif
540
541 return 0;
542}
543
Marek Vasut06485cf2018-04-08 15:22:58 +0200544static const struct dm_mmc_ops renesas_sdhi_ops = {
Marek Vasutfee0c682019-05-19 03:47:07 +0200545 .send_cmd = renesas_sdhi_send_cmd,
Marek Vasute0781e42018-04-08 19:09:17 +0200546 .set_ios = renesas_sdhi_set_ios,
Marek Vasutfd83e762018-04-13 23:51:33 +0200547 .get_cd = tmio_sd_get_cd,
Marek Vasut10d77ed2018-06-13 08:02:55 +0200548#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
549 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
550 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
Marek Vasute0781e42018-04-08 19:09:17 +0200551 .execute_tuning = renesas_sdhi_execute_tuning,
552#endif
Marek Vasut576a6d92018-10-28 19:28:56 +0100553#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
554 .wait_dat0 = renesas_sdhi_wait_dat0,
555#endif
Marek Vasut06485cf2018-04-08 15:22:58 +0200556};
557
Marek Vasutfd83e762018-04-13 23:51:33 +0200558#define RENESAS_GEN2_QUIRKS TMIO_SD_CAP_RCAR_GEN2
Marek Vasut9db9e6a2018-04-08 18:49:52 +0200559#define RENESAS_GEN3_QUIRKS \
Marek Vasutfd83e762018-04-13 23:51:33 +0200560 TMIO_SD_CAP_64BIT | TMIO_SD_CAP_RCAR_GEN3 | TMIO_SD_CAP_RCAR_UHS
Marek Vasut9db9e6a2018-04-08 18:49:52 +0200561
Marek Vasut06485cf2018-04-08 15:22:58 +0200562static const struct udevice_id renesas_sdhi_match[] = {
Marek Vasut9db9e6a2018-04-08 18:49:52 +0200563 { .compatible = "renesas,sdhi-r8a7790", .data = RENESAS_GEN2_QUIRKS },
564 { .compatible = "renesas,sdhi-r8a7791", .data = RENESAS_GEN2_QUIRKS },
565 { .compatible = "renesas,sdhi-r8a7792", .data = RENESAS_GEN2_QUIRKS },
566 { .compatible = "renesas,sdhi-r8a7793", .data = RENESAS_GEN2_QUIRKS },
567 { .compatible = "renesas,sdhi-r8a7794", .data = RENESAS_GEN2_QUIRKS },
568 { .compatible = "renesas,sdhi-r8a7795", .data = RENESAS_GEN3_QUIRKS },
569 { .compatible = "renesas,sdhi-r8a7796", .data = RENESAS_GEN3_QUIRKS },
570 { .compatible = "renesas,sdhi-r8a77965", .data = RENESAS_GEN3_QUIRKS },
571 { .compatible = "renesas,sdhi-r8a77970", .data = RENESAS_GEN3_QUIRKS },
Marek Vasut8b2ae7d2018-04-26 13:19:29 +0200572 { .compatible = "renesas,sdhi-r8a77990", .data = RENESAS_GEN3_QUIRKS },
Marek Vasut9db9e6a2018-04-08 18:49:52 +0200573 { .compatible = "renesas,sdhi-r8a77995", .data = RENESAS_GEN3_QUIRKS },
Marek Vasut06485cf2018-04-08 15:22:58 +0200574 { /* sentinel */ }
575};
576
Marek Vasutda90a1b2018-06-13 08:02:55 +0200577static ulong renesas_sdhi_clk_get_rate(struct tmio_sd_priv *priv)
578{
579 return clk_get_rate(&priv->clk);
580}
581
Marek Vasutb59180a2018-06-13 08:02:55 +0200582static void renesas_sdhi_filter_caps(struct udevice *dev)
583{
584 struct tmio_sd_plat *plat = dev_get_platdata(dev);
585 struct tmio_sd_priv *priv = dev_get_priv(dev);
586
587 if (!(priv->caps & TMIO_SD_CAP_RCAR_GEN3))
588 return;
589
Marek Vasutfee0c682019-05-19 03:47:07 +0200590 /* HS400 is not supported on H3 ES1.x and M3W ES1.0,ES1.1,ES1.2 */
Marek Vasutb59180a2018-06-13 08:02:55 +0200591 if (((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7795) &&
592 (rmobile_get_cpu_rev_integer() <= 1)) ||
593 ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7796) &&
594 (rmobile_get_cpu_rev_integer() == 1) &&
Marek Vasutfee0c682019-05-19 03:47:07 +0200595 (rmobile_get_cpu_rev_fraction() <= 2)))
Marek Vasutb59180a2018-06-13 08:02:55 +0200596 plat->cfg.host_caps &= ~MMC_MODE_HS400;
Marek Vasut10d77ed2018-06-13 08:02:55 +0200597
Marek Vasutfee0c682019-05-19 03:47:07 +0200598 /* M3W ES1.x for x>2 can use HS400 with manual adjustment */
599 if ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7796) &&
600 (rmobile_get_cpu_rev_integer() == 1) &&
601 (rmobile_get_cpu_rev_fraction() > 2)) {
602 priv->adjust_hs400_enable = true;
603 priv->adjust_hs400_offset = 0;
604 priv->adjust_hs400_calibrate = 0x9;
605 }
606
607 /* M3N can use HS400 with manual adjustment */
608 if (rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A77965) {
609 priv->adjust_hs400_enable = true;
610 priv->adjust_hs400_offset = 0;
611 priv->adjust_hs400_calibrate = 0x0;
612 }
613
614 /* E3 can use HS400 with manual adjustment */
615 if (rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A77990) {
616 priv->adjust_hs400_enable = true;
617 priv->adjust_hs400_offset = 0;
618 priv->adjust_hs400_calibrate = 0x2;
619 }
620
Marek Vasut10d77ed2018-06-13 08:02:55 +0200621 /* H3 ES2.0 uses 4 tuning taps */
622 if ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7795) &&
623 (rmobile_get_cpu_rev_integer() == 2))
624 priv->nrtaps = 4;
625 else
626 priv->nrtaps = 8;
Marek Vasute9a28222019-01-11 23:45:54 +0100627
628 /* H3 ES1.x and M3W ES1.0 uses bit 17 for DTRAEND */
629 if (((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7795) &&
630 (rmobile_get_cpu_rev_integer() <= 1)) ||
631 ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7796) &&
632 (rmobile_get_cpu_rev_integer() == 1) &&
633 (rmobile_get_cpu_rev_fraction() == 0)))
634 priv->read_poll_flag = TMIO_SD_DMA_INFO1_END_RD;
635 else
636 priv->read_poll_flag = TMIO_SD_DMA_INFO1_END_RD2;
Marek Vasutb59180a2018-06-13 08:02:55 +0200637}
638
Marek Vasutabe3e952018-04-08 17:45:23 +0200639static int renesas_sdhi_probe(struct udevice *dev)
640{
Masahiro Yamada19989d832018-04-20 18:14:24 +0900641 struct tmio_sd_priv *priv = dev_get_priv(dev);
Marek Vasutabe3e952018-04-08 17:45:23 +0200642 u32 quirks = dev_get_driver_data(dev);
Marek Vasut1949d482018-04-08 18:14:22 +0200643 struct fdt_resource reg_res;
644 DECLARE_GLOBAL_DATA_PTR;
645 int ret;
646
Marek Vasutda90a1b2018-06-13 08:02:55 +0200647 priv->clk_get_rate = renesas_sdhi_clk_get_rate;
648
Marek Vasut9db9e6a2018-04-08 18:49:52 +0200649 if (quirks == RENESAS_GEN2_QUIRKS) {
650 ret = fdt_get_resource(gd->fdt_blob, dev_of_offset(dev),
651 "reg", 0, &reg_res);
652 if (ret < 0) {
653 dev_err(dev, "\"reg\" resource not found, ret=%i\n",
654 ret);
655 return ret;
656 }
Marek Vasut1949d482018-04-08 18:14:22 +0200657
Marek Vasut9db9e6a2018-04-08 18:49:52 +0200658 if (fdt_resource_size(&reg_res) == 0x100)
Marek Vasutfd83e762018-04-13 23:51:33 +0200659 quirks |= TMIO_SD_CAP_16BIT;
Marek Vasut9db9e6a2018-04-08 18:49:52 +0200660 }
Marek Vasutabe3e952018-04-08 17:45:23 +0200661
Marek Vasutda90a1b2018-06-13 08:02:55 +0200662 ret = clk_get_by_index(dev, 0, &priv->clk);
Masahiro Yamada19989d832018-04-20 18:14:24 +0900663 if (ret < 0) {
664 dev_err(dev, "failed to get host clock\n");
665 return ret;
666 }
667
668 /* set to max rate */
Marek Vasutda90a1b2018-06-13 08:02:55 +0200669 ret = clk_set_rate(&priv->clk, 200000000);
670 if (ret < 0) {
Masahiro Yamada19989d832018-04-20 18:14:24 +0900671 dev_err(dev, "failed to set rate for host clock\n");
Marek Vasutda90a1b2018-06-13 08:02:55 +0200672 clk_free(&priv->clk);
673 return ret;
Masahiro Yamada19989d832018-04-20 18:14:24 +0900674 }
675
Marek Vasutda90a1b2018-06-13 08:02:55 +0200676 ret = clk_enable(&priv->clk);
Masahiro Yamada19989d832018-04-20 18:14:24 +0900677 if (ret) {
678 dev_err(dev, "failed to enable host clock\n");
679 return ret;
680 }
681
Marek Vasutfd83e762018-04-13 23:51:33 +0200682 ret = tmio_sd_probe(dev, quirks);
Marek Vasutb59180a2018-06-13 08:02:55 +0200683
684 renesas_sdhi_filter_caps(dev);
685
Marek Vasut10d77ed2018-06-13 08:02:55 +0200686#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
687 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
688 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
Marek Vasut35773802018-10-28 15:30:06 +0100689 if (!ret && (priv->caps & TMIO_SD_CAP_RCAR_UHS))
Marek Vasutda70ebb2018-08-30 15:27:26 +0200690 renesas_sdhi_reset_tuning(priv);
Marek Vasute0781e42018-04-08 19:09:17 +0200691#endif
692 return ret;
Marek Vasutabe3e952018-04-08 17:45:23 +0200693}
694
Marek Vasut06485cf2018-04-08 15:22:58 +0200695U_BOOT_DRIVER(renesas_sdhi) = {
696 .name = "renesas-sdhi",
697 .id = UCLASS_MMC,
698 .of_match = renesas_sdhi_match,
Marek Vasutfd83e762018-04-13 23:51:33 +0200699 .bind = tmio_sd_bind,
Marek Vasutabe3e952018-04-08 17:45:23 +0200700 .probe = renesas_sdhi_probe,
Marek Vasutfd83e762018-04-13 23:51:33 +0200701 .priv_auto_alloc_size = sizeof(struct tmio_sd_priv),
702 .platdata_auto_alloc_size = sizeof(struct tmio_sd_plat),
Marek Vasut06485cf2018-04-08 15:22:58 +0200703 .ops = &renesas_sdhi_ops,
704};