blob: 90e8aaddb0538ef3a14a7f84524d212d65b02892 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marek Vasute94cad92018-04-08 15:22:58 +02002/*
3 * Copyright (C) 2018 Marek Vasut <marek.vasut@gmail.com>
Marek Vasute94cad92018-04-08 15:22:58 +02004 */
5
6#include <common.h>
Marek Vasutd2661d82020-04-04 12:45:04 +02007#include <bouncebuf.h>
Marek Vasute94cad92018-04-08 15:22:58 +02008#include <clk.h>
9#include <fdtdec.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060010#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070011#include <malloc.h>
Marek Vasute94cad92018-04-08 15:22:58 +020012#include <mmc.h>
13#include <dm.h>
Simon Glass401d1c42020-10-30 21:38:53 -060014#include <asm/global_data.h>
Simon Glass336d4612020-02-03 07:36:16 -070015#include <dm/device_compat.h>
Simon Glasscd93d622020-05-10 11:40:13 -060016#include <linux/bitops.h>
Marek Vasute94cad92018-04-08 15:22:58 +020017#include <linux/compat.h>
Simon Glassc05ed002020-05-10 11:40:11 -060018#include <linux/delay.h>
Marek Vasute94cad92018-04-08 15:22:58 +020019#include <linux/dma-direction.h>
20#include <linux/io.h>
21#include <linux/sizes.h>
22#include <power/regulator.h>
23#include <asm/unaligned.h>
Marek Vasutcb0b6b02018-04-13 23:51:33 +020024#include "tmio-common.h"
Marek Vasute94cad92018-04-08 15:22:58 +020025
Marek Vasut50aa1d92018-06-13 08:02:55 +020026#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
27 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
28 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
Marek Vasutf63968b2018-04-08 19:09:17 +020029
30/* SCC registers */
31#define RENESAS_SDHI_SCC_DTCNTL 0x800
Marek Vasut1bac2b62019-05-19 02:33:06 +020032#define RENESAS_SDHI_SCC_DTCNTL_TAPEN BIT(0)
33#define RENESAS_SDHI_SCC_DTCNTL_TAPNUM_SHIFT 16
34#define RENESAS_SDHI_SCC_DTCNTL_TAPNUM_MASK 0xff
Marek Vasutf63968b2018-04-08 19:09:17 +020035#define RENESAS_SDHI_SCC_TAPSET 0x804
36#define RENESAS_SDHI_SCC_DT2FF 0x808
37#define RENESAS_SDHI_SCC_CKSEL 0x80c
Marek Vasut1bac2b62019-05-19 02:33:06 +020038#define RENESAS_SDHI_SCC_CKSEL_DTSEL BIT(0)
39#define RENESAS_SDHI_SCC_RVSCNTL 0x810
40#define RENESAS_SDHI_SCC_RVSCNTL_RVSEN BIT(0)
Marek Vasutf63968b2018-04-08 19:09:17 +020041#define RENESAS_SDHI_SCC_RVSREQ 0x814
Marek Vasut1bac2b62019-05-19 02:33:06 +020042#define RENESAS_SDHI_SCC_RVSREQ_RVSERR BIT(2)
Marek Vasut69000662019-11-23 13:36:23 +010043#define RENESAS_SDHI_SCC_RVSREQ_REQTAPUP BIT(1)
44#define RENESAS_SDHI_SCC_RVSREQ_REQTAPDOWN BIT(0)
Marek Vasutf63968b2018-04-08 19:09:17 +020045#define RENESAS_SDHI_SCC_SMPCMP 0x818
Marek Vasut69000662019-11-23 13:36:23 +010046#define RENESAS_SDHI_SCC_SMPCMP_CMD_ERR (BIT(24) | BIT(8))
47#define RENESAS_SDHI_SCC_SMPCMP_CMD_REQUP BIT(24)
48#define RENESAS_SDHI_SCC_SMPCMP_CMD_REQDOWN BIT(8)
Marek Vasut1bac2b62019-05-19 02:33:06 +020049#define RENESAS_SDHI_SCC_TMPPORT2 0x81c
50#define RENESAS_SDHI_SCC_TMPPORT2_HS400EN BIT(31)
51#define RENESAS_SDHI_SCC_TMPPORT2_HS400OSEL BIT(4)
Marek Vasutb5900a52019-05-19 03:47:07 +020052#define RENESAS_SDHI_SCC_TMPPORT3 0x828
53#define RENESAS_SDHI_SCC_TMPPORT3_OFFSET_0 3
54#define RENESAS_SDHI_SCC_TMPPORT3_OFFSET_1 2
55#define RENESAS_SDHI_SCC_TMPPORT3_OFFSET_2 1
56#define RENESAS_SDHI_SCC_TMPPORT3_OFFSET_3 0
57#define RENESAS_SDHI_SCC_TMPPORT3_OFFSET_MASK 0x3
58#define RENESAS_SDHI_SCC_TMPPORT4 0x82c
59#define RENESAS_SDHI_SCC_TMPPORT4_DLL_ACC_START BIT(0)
60#define RENESAS_SDHI_SCC_TMPPORT5 0x830
61#define RENESAS_SDHI_SCC_TMPPORT5_DLL_RW_SEL_R BIT(8)
62#define RENESAS_SDHI_SCC_TMPPORT5_DLL_RW_SEL_W (0 << 8)
63#define RENESAS_SDHI_SCC_TMPPORT5_DLL_ADR_MASK 0x3F
64#define RENESAS_SDHI_SCC_TMPPORT6 0x834
65#define RENESAS_SDHI_SCC_TMPPORT7 0x838
66#define RENESAS_SDHI_SCC_TMPPORT_DISABLE_WP_CODE 0xa5000000
67#define RENESAS_SDHI_SCC_TMPPORT_CALIB_CODE_MASK 0x1f
68#define RENESAS_SDHI_SCC_TMPPORT_MANUAL_MODE BIT(7)
Marek Vasutf63968b2018-04-08 19:09:17 +020069
70#define RENESAS_SDHI_MAX_TAP 3
71
Marek Vasut56b0bb92019-11-23 13:36:25 +010072#define CALIB_TABLE_MAX (RENESAS_SDHI_SCC_TMPPORT_CALIB_CODE_MASK + 1)
73
74static const u8 r8a7795_calib_table[2][CALIB_TABLE_MAX] = {
75 { 0, 0, 0, 0, 0, 1, 1, 2, 3, 4, 5, 5, 6, 6, 7, 11,
76 15, 16, 16, 17, 17, 17, 17, 17, 18, 18, 18, 18, 19, 20, 21, 21 },
77 { 3, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 9, 10, 11, 12, 15,
78 16, 16, 17, 17, 17, 17, 17, 18, 18, 18, 18, 19, 20, 21, 22, 22 }
79};
80
Hai Pham6d7d4c42023-01-26 21:05:56 +010081static const u8 r8a7796_rev13_calib_table[2][CALIB_TABLE_MAX] = {
Hai Phamaf95db52023-01-26 21:05:57 +010082 { 3, 3, 3, 3, 3, 3, 3, 4, 4, 5, 6, 7, 8, 9, 10, 15,
83 16, 16, 16, 16, 16, 16, 17, 18, 18, 19, 20, 21, 22, 23, 24, 25 },
84 { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 7, 8, 11,
85 12, 17, 18, 18, 18, 18, 18, 18, 18, 19, 20, 21, 22, 23, 25, 25 }
Marek Vasut56b0bb92019-11-23 13:36:25 +010086};
87
88static const u8 r8a77965_calib_table[2][CALIB_TABLE_MAX] = {
Hai Phamaf95db52023-01-26 21:05:57 +010089 { 1, 2, 6, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 15, 15, 16,
90 17, 18, 19, 20, 21, 22, 23, 24, 25, 25, 26, 27, 28, 29, 30, 31 },
91 { 2, 3, 4, 4, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 16, 17,
92 17, 17, 20, 21, 22, 23, 24, 25, 27, 28, 29, 30, 31, 31, 31, 31 }
Marek Vasut56b0bb92019-11-23 13:36:25 +010093};
94
95static const u8 r8a77990_calib_table[2][CALIB_TABLE_MAX] = {
96 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
97 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
Hai Phamaf95db52023-01-26 21:05:57 +010098 { 0, 0, 0, 1, 2, 3, 3, 4, 4, 4, 5, 5, 6, 8, 9, 10,
99 11, 12, 13, 15, 16, 17, 17, 18, 18, 19, 20, 22, 24, 25, 26, 26 }
Marek Vasut56b0bb92019-11-23 13:36:25 +0100100};
101
102static int rmobile_is_gen3_mmc0(struct tmio_sd_priv *priv)
103{
104 /* On R-Car Gen3, MMC0 is at 0xee140000 */
105 return (uintptr_t)(priv->regbase) == 0xee140000;
106}
107
Marek Vasutb5900a52019-05-19 03:47:07 +0200108static u32 sd_scc_tmpport_read32(struct tmio_sd_priv *priv, u32 addr)
109{
110 /* read mode */
111 tmio_sd_writel(priv, RENESAS_SDHI_SCC_TMPPORT5_DLL_RW_SEL_R |
112 (RENESAS_SDHI_SCC_TMPPORT5_DLL_ADR_MASK & addr),
113 RENESAS_SDHI_SCC_TMPPORT5);
114
115 /* access start and stop */
116 tmio_sd_writel(priv, RENESAS_SDHI_SCC_TMPPORT4_DLL_ACC_START,
117 RENESAS_SDHI_SCC_TMPPORT4);
118 tmio_sd_writel(priv, 0, RENESAS_SDHI_SCC_TMPPORT4);
119
120 return tmio_sd_readl(priv, RENESAS_SDHI_SCC_TMPPORT7);
121}
122
123static void sd_scc_tmpport_write32(struct tmio_sd_priv *priv, u32 addr, u32 val)
124{
125 /* write mode */
126 tmio_sd_writel(priv, RENESAS_SDHI_SCC_TMPPORT5_DLL_RW_SEL_W |
127 (RENESAS_SDHI_SCC_TMPPORT5_DLL_ADR_MASK & addr),
128 RENESAS_SDHI_SCC_TMPPORT5);
129 tmio_sd_writel(priv, val, RENESAS_SDHI_SCC_TMPPORT6);
130
131 /* access start and stop */
132 tmio_sd_writel(priv, RENESAS_SDHI_SCC_TMPPORT4_DLL_ACC_START,
133 RENESAS_SDHI_SCC_TMPPORT4);
134 tmio_sd_writel(priv, 0, RENESAS_SDHI_SCC_TMPPORT4);
135}
136
Marek Vasut69000662019-11-23 13:36:23 +0100137static bool renesas_sdhi_check_scc_error(struct udevice *dev)
138{
139 struct tmio_sd_priv *priv = dev_get_priv(dev);
140 struct mmc *mmc = mmc_get_mmc_dev(dev);
141 unsigned long new_tap = priv->tap_set;
Marek Vasut1bdcb832019-11-23 13:36:24 +0100142 unsigned long error_tap = priv->tap_set;
Marek Vasut69000662019-11-23 13:36:23 +0100143 u32 reg, smpcmp;
144
145 if ((priv->caps & TMIO_SD_CAP_RCAR_UHS) &&
146 (mmc->selected_mode != UHS_SDR104) &&
147 (mmc->selected_mode != MMC_HS_200) &&
148 (mmc->selected_mode != MMC_HS_400) &&
149 (priv->nrtaps != 4))
150 return false;
151
152 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSCNTL);
153 /* Handle automatic tuning correction */
154 if (reg & RENESAS_SDHI_SCC_RVSCNTL_RVSEN) {
155 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSREQ);
156 if (reg & RENESAS_SDHI_SCC_RVSREQ_RVSERR) {
157 tmio_sd_writel(priv, 0, RENESAS_SDHI_SCC_RVSREQ);
158 return true;
159 }
160
161 return false;
162 }
163
164 /* Handle manual tuning correction */
165 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSREQ);
166 if (!reg) /* No error */
167 return false;
168
169 tmio_sd_writel(priv, 0, RENESAS_SDHI_SCC_RVSREQ);
170
171 if (mmc->selected_mode == MMC_HS_400) {
172 /*
173 * Correction Error Status contains CMD and DAT signal status.
174 * In HS400, DAT signal based on DS signal, not CLK.
175 * Therefore, use only CMD status.
176 */
177 smpcmp = tmio_sd_readl(priv, RENESAS_SDHI_SCC_SMPCMP) &
178 RENESAS_SDHI_SCC_SMPCMP_CMD_ERR;
179
180 switch (smpcmp) {
181 case 0:
182 return false; /* No error in CMD signal */
183 case RENESAS_SDHI_SCC_SMPCMP_CMD_REQUP:
184 new_tap = (priv->tap_set +
185 priv->tap_num + 1) % priv->tap_num;
Marek Vasut1bdcb832019-11-23 13:36:24 +0100186 error_tap = (priv->tap_set +
187 priv->tap_num - 1) % priv->tap_num;
Marek Vasut69000662019-11-23 13:36:23 +0100188 break;
189 case RENESAS_SDHI_SCC_SMPCMP_CMD_REQDOWN:
190 new_tap = (priv->tap_set +
191 priv->tap_num - 1) % priv->tap_num;
Marek Vasut1bdcb832019-11-23 13:36:24 +0100192 error_tap = (priv->tap_set +
193 priv->tap_num + 1) % priv->tap_num;
Marek Vasut69000662019-11-23 13:36:23 +0100194 break;
195 default:
196 return true; /* Need re-tune */
197 }
198
Marek Vasut1bdcb832019-11-23 13:36:24 +0100199 if (priv->hs400_bad_tap & BIT(new_tap)) {
200 /*
201 * New tap is bad tap (cannot change).
202 * Compare with HS200 tuning result.
203 * In HS200 tuning, when smpcmp[error_tap]
204 * is OK, retune is executed.
205 */
206 if (priv->smpcmp & BIT(error_tap))
207 return true; /* Need retune */
208
209 return false; /* cannot change */
210 }
211
Marek Vasut69000662019-11-23 13:36:23 +0100212 priv->tap_set = new_tap;
213 } else {
214 if (reg & RENESAS_SDHI_SCC_RVSREQ_RVSERR)
215 return true; /* Need re-tune */
216 else if (reg & RENESAS_SDHI_SCC_RVSREQ_REQTAPUP)
217 priv->tap_set = (priv->tap_set +
218 priv->tap_num + 1) % priv->tap_num;
219 else if (reg & RENESAS_SDHI_SCC_RVSREQ_REQTAPDOWN)
220 priv->tap_set = (priv->tap_set +
221 priv->tap_num - 1) % priv->tap_num;
222 else
223 return false;
224 }
225
226 /* Set TAP position */
227 tmio_sd_writel(priv, priv->tap_set >> ((priv->nrtaps == 4) ? 1 : 0),
228 RENESAS_SDHI_SCC_TAPSET);
229
230 return false;
231}
232
Marek Vasutb5900a52019-05-19 03:47:07 +0200233static void renesas_sdhi_adjust_hs400_mode_enable(struct tmio_sd_priv *priv)
234{
235 u32 calib_code;
236
237 if (!priv->adjust_hs400_enable)
238 return;
239
240 if (!priv->needs_adjust_hs400)
241 return;
242
Marek Vasut56b0bb92019-11-23 13:36:25 +0100243 if (!priv->adjust_hs400_calib_table)
244 return;
245
Marek Vasutb5900a52019-05-19 03:47:07 +0200246 /*
247 * Enabled Manual adjust HS400 mode
248 *
249 * 1) Disabled Write Protect
250 * W(addr=0x00, WP_DISABLE_CODE)
Marek Vasut56b0bb92019-11-23 13:36:25 +0100251 *
252 * 2) Read Calibration code
253 * read_value = R(addr=0x26)
254 * 3) Refer to calibration table
255 * Calibration code = table[read_value]
256 * 4) Enabled Manual Calibration
Marek Vasutb5900a52019-05-19 03:47:07 +0200257 * W(addr=0x22, manual mode | Calibration code)
Marek Vasut56b0bb92019-11-23 13:36:25 +0100258 * 5) Set Offset value to TMPPORT3 Reg
Marek Vasutb5900a52019-05-19 03:47:07 +0200259 */
260 sd_scc_tmpport_write32(priv, 0x00,
261 RENESAS_SDHI_SCC_TMPPORT_DISABLE_WP_CODE);
262 calib_code = sd_scc_tmpport_read32(priv, 0x26);
263 calib_code &= RENESAS_SDHI_SCC_TMPPORT_CALIB_CODE_MASK;
Marek Vasutb5900a52019-05-19 03:47:07 +0200264 sd_scc_tmpport_write32(priv, 0x22,
265 RENESAS_SDHI_SCC_TMPPORT_MANUAL_MODE |
Marek Vasut56b0bb92019-11-23 13:36:25 +0100266 priv->adjust_hs400_calib_table[calib_code]);
Marek Vasutb5900a52019-05-19 03:47:07 +0200267 tmio_sd_writel(priv, priv->adjust_hs400_offset,
268 RENESAS_SDHI_SCC_TMPPORT3);
269
270 /* Clear flag */
271 priv->needs_adjust_hs400 = false;
272}
273
274static void renesas_sdhi_adjust_hs400_mode_disable(struct tmio_sd_priv *priv)
275{
276
277 /* Disabled Manual adjust HS400 mode
278 *
279 * 1) Disabled Write Protect
280 * W(addr=0x00, WP_DISABLE_CODE)
281 * 2) Disabled Manual Calibration
282 * W(addr=0x22, 0)
283 * 3) Clear offset value to TMPPORT3 Reg
284 */
285 sd_scc_tmpport_write32(priv, 0x00,
286 RENESAS_SDHI_SCC_TMPPORT_DISABLE_WP_CODE);
287 sd_scc_tmpport_write32(priv, 0x22, 0);
288 tmio_sd_writel(priv, 0, RENESAS_SDHI_SCC_TMPPORT3);
289}
290
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200291static unsigned int renesas_sdhi_init_tuning(struct tmio_sd_priv *priv)
Marek Vasutf63968b2018-04-08 19:09:17 +0200292{
293 u32 reg;
294
295 /* Initialize SCC */
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200296 tmio_sd_writel(priv, 0, TMIO_SD_INFO1);
Marek Vasutf63968b2018-04-08 19:09:17 +0200297
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200298 reg = tmio_sd_readl(priv, TMIO_SD_CLKCTL);
299 reg &= ~TMIO_SD_CLKCTL_SCLKEN;
300 tmio_sd_writel(priv, reg, TMIO_SD_CLKCTL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200301
302 /* Set sampling clock selection range */
Marek Vasuta376dde2018-06-13 08:02:55 +0200303 tmio_sd_writel(priv, (0x8 << RENESAS_SDHI_SCC_DTCNTL_TAPNUM_SHIFT) |
304 RENESAS_SDHI_SCC_DTCNTL_TAPEN,
305 RENESAS_SDHI_SCC_DTCNTL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200306
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200307 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_CKSEL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200308 reg |= RENESAS_SDHI_SCC_CKSEL_DTSEL;
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200309 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_CKSEL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200310
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200311 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSCNTL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200312 reg &= ~RENESAS_SDHI_SCC_RVSCNTL_RVSEN;
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200313 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_RVSCNTL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200314
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200315 tmio_sd_writel(priv, 0x300 /* scc_tappos */,
Marek Vasutf63968b2018-04-08 19:09:17 +0200316 RENESAS_SDHI_SCC_DT2FF);
317
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200318 reg = tmio_sd_readl(priv, TMIO_SD_CLKCTL);
319 reg |= TMIO_SD_CLKCTL_SCLKEN;
320 tmio_sd_writel(priv, reg, TMIO_SD_CLKCTL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200321
322 /* Read TAPNUM */
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200323 return (tmio_sd_readl(priv, RENESAS_SDHI_SCC_DTCNTL) >>
Marek Vasutf63968b2018-04-08 19:09:17 +0200324 RENESAS_SDHI_SCC_DTCNTL_TAPNUM_SHIFT) &
325 RENESAS_SDHI_SCC_DTCNTL_TAPNUM_MASK;
326}
327
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200328static void renesas_sdhi_reset_tuning(struct tmio_sd_priv *priv)
Marek Vasutf63968b2018-04-08 19:09:17 +0200329{
330 u32 reg;
331
332 /* Reset SCC */
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200333 reg = tmio_sd_readl(priv, TMIO_SD_CLKCTL);
334 reg &= ~TMIO_SD_CLKCTL_SCLKEN;
335 tmio_sd_writel(priv, reg, TMIO_SD_CLKCTL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200336
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200337 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_CKSEL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200338 reg &= ~RENESAS_SDHI_SCC_CKSEL_DTSEL;
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200339 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_CKSEL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200340
Marek Vasutdc1488f2018-06-13 08:02:55 +0200341 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_TMPPORT2);
342 reg &= ~(RENESAS_SDHI_SCC_TMPPORT2_HS400EN |
343 RENESAS_SDHI_SCC_TMPPORT2_HS400OSEL);
344 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_TMPPORT2);
345
Marek Vasutb5900a52019-05-19 03:47:07 +0200346 /* Disable HS400 mode adjustment */
347 renesas_sdhi_adjust_hs400_mode_disable(priv);
348
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200349 reg = tmio_sd_readl(priv, TMIO_SD_CLKCTL);
350 reg |= TMIO_SD_CLKCTL_SCLKEN;
351 tmio_sd_writel(priv, reg, TMIO_SD_CLKCTL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200352
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200353 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSCNTL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200354 reg &= ~RENESAS_SDHI_SCC_RVSCNTL_RVSEN;
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200355 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_RVSCNTL);
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}
361
Marek Vasut50aa1d92018-06-13 08:02:55 +0200362static int renesas_sdhi_hs400(struct udevice *dev)
363{
364 struct tmio_sd_priv *priv = dev_get_priv(dev);
365 struct mmc *mmc = mmc_get_mmc_dev(dev);
366 bool hs400 = (mmc->selected_mode == MMC_HS_400);
367 int ret, taps = hs400 ? priv->nrtaps : 8;
Marek Vasut1bdcb832019-11-23 13:36:24 +0100368 unsigned long new_tap;
Marek Vasut50aa1d92018-06-13 08:02:55 +0200369 u32 reg;
370
371 if (taps == 4) /* HS400 on 4tap SoC needs different clock */
372 ret = clk_set_rate(&priv->clk, 400000000);
373 else
374 ret = clk_set_rate(&priv->clk, 200000000);
375 if (ret < 0)
376 return ret;
377
Marek Vasut8f39b032019-11-23 13:36:22 +0100378 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSCNTL);
379 reg &= ~RENESAS_SDHI_SCC_RVSCNTL_RVSEN;
380 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_RVSCNTL);
Marek Vasut50aa1d92018-06-13 08:02:55 +0200381
382 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_TMPPORT2);
383 if (hs400) {
384 reg |= RENESAS_SDHI_SCC_TMPPORT2_HS400EN |
385 RENESAS_SDHI_SCC_TMPPORT2_HS400OSEL;
386 } else {
387 reg &= ~(RENESAS_SDHI_SCC_TMPPORT2_HS400EN |
388 RENESAS_SDHI_SCC_TMPPORT2_HS400OSEL);
389 }
390
391 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_TMPPORT2);
392
Marek Vasutb5900a52019-05-19 03:47:07 +0200393 /* Disable HS400 mode adjustment */
394 if (!hs400)
395 renesas_sdhi_adjust_hs400_mode_disable(priv);
396
Marek Vasutba41c452019-02-19 19:32:28 +0100397 tmio_sd_writel(priv, (0x8 << RENESAS_SDHI_SCC_DTCNTL_TAPNUM_SHIFT) |
Marek Vasut50aa1d92018-06-13 08:02:55 +0200398 RENESAS_SDHI_SCC_DTCNTL_TAPEN,
399 RENESAS_SDHI_SCC_DTCNTL);
400
Marek Vasut1bdcb832019-11-23 13:36:24 +0100401 /* Avoid bad TAP */
402 if (priv->hs400_bad_tap & BIT(priv->tap_set)) {
403 new_tap = (priv->tap_set +
404 priv->tap_num + 1) % priv->tap_num;
405
406 if (priv->hs400_bad_tap & BIT(new_tap))
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 debug("Three consecutive bad tap is prohibited\n");
413 }
414
415 priv->tap_set = new_tap;
416 tmio_sd_writel(priv, priv->tap_set, RENESAS_SDHI_SCC_TAPSET);
417 }
418
Marek Vasut50aa1d92018-06-13 08:02:55 +0200419 if (taps == 4) {
420 tmio_sd_writel(priv, priv->tap_set >> 1,
421 RENESAS_SDHI_SCC_TAPSET);
Marek Vasutdc419fc2019-11-23 13:36:20 +0100422 tmio_sd_writel(priv, hs400 ? 0x100 : 0x300,
423 RENESAS_SDHI_SCC_DT2FF);
Marek Vasut50aa1d92018-06-13 08:02:55 +0200424 } else {
425 tmio_sd_writel(priv, priv->tap_set, RENESAS_SDHI_SCC_TAPSET);
Marek Vasutdc419fc2019-11-23 13:36:20 +0100426 tmio_sd_writel(priv, 0x300, RENESAS_SDHI_SCC_DT2FF);
Marek Vasut50aa1d92018-06-13 08:02:55 +0200427 }
428
429 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_CKSEL);
430 reg |= RENESAS_SDHI_SCC_CKSEL_DTSEL;
431 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_CKSEL);
432
Marek Vasutb5900a52019-05-19 03:47:07 +0200433 /* Execute adjust hs400 offset after setting to HS400 mode */
434 if (hs400)
435 priv->needs_adjust_hs400 = true;
436
Marek Vasut50aa1d92018-06-13 08:02:55 +0200437 return 0;
438}
439
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200440static void renesas_sdhi_prepare_tuning(struct tmio_sd_priv *priv,
Marek Vasutf63968b2018-04-08 19:09:17 +0200441 unsigned long tap)
442{
443 /* Set sampling clock position */
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200444 tmio_sd_writel(priv, tap, RENESAS_SDHI_SCC_TAPSET);
Marek Vasutf63968b2018-04-08 19:09:17 +0200445}
446
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200447static unsigned int renesas_sdhi_compare_scc_data(struct tmio_sd_priv *priv)
Marek Vasutf63968b2018-04-08 19:09:17 +0200448{
449 /* Get comparison of sampling data */
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200450 return tmio_sd_readl(priv, RENESAS_SDHI_SCC_SMPCMP);
Marek Vasutf63968b2018-04-08 19:09:17 +0200451}
452
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200453static int renesas_sdhi_select_tuning(struct tmio_sd_priv *priv,
Marek Vasut37c39902019-11-23 13:36:18 +0100454 unsigned int taps)
Marek Vasutf63968b2018-04-08 19:09:17 +0200455{
456 unsigned long tap_cnt; /* counter of tuning success */
Marek Vasutf63968b2018-04-08 19:09:17 +0200457 unsigned long tap_start;/* start position of tuning success */
458 unsigned long tap_end; /* end position of tuning success */
459 unsigned long ntap; /* temporary counter of tuning success */
460 unsigned long match_cnt;/* counter of matching data */
461 unsigned long i;
462 bool select = false;
463 u32 reg;
464
Marek Vasutb5900a52019-05-19 03:47:07 +0200465 priv->needs_adjust_hs400 = false;
466
Marek Vasutf63968b2018-04-08 19:09:17 +0200467 /* Clear SCC_RVSREQ */
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200468 tmio_sd_writel(priv, 0, RENESAS_SDHI_SCC_RVSREQ);
Marek Vasutf63968b2018-04-08 19:09:17 +0200469
470 /* Merge the results */
Marek Vasut0196a582019-11-23 13:36:17 +0100471 for (i = 0; i < priv->tap_num * 2; i++) {
Marek Vasutf63968b2018-04-08 19:09:17 +0200472 if (!(taps & BIT(i))) {
Marek Vasut0196a582019-11-23 13:36:17 +0100473 taps &= ~BIT(i % priv->tap_num);
474 taps &= ~BIT((i % priv->tap_num) + priv->tap_num);
Marek Vasutf63968b2018-04-08 19:09:17 +0200475 }
Marek Vasut37c39902019-11-23 13:36:18 +0100476 if (!(priv->smpcmp & BIT(i))) {
477 priv->smpcmp &= ~BIT(i % priv->tap_num);
478 priv->smpcmp &= ~BIT((i % priv->tap_num) + priv->tap_num);
Marek Vasutf63968b2018-04-08 19:09:17 +0200479 }
480 }
481
482 /*
483 * Find the longest consecutive run of successful probes. If that
484 * is more than RENESAS_SDHI_MAX_TAP probes long then use the
485 * center index as the tap.
486 */
487 tap_cnt = 0;
488 ntap = 0;
489 tap_start = 0;
490 tap_end = 0;
Marek Vasut0196a582019-11-23 13:36:17 +0100491 for (i = 0; i < priv->tap_num * 2; i++) {
Marek Vasutf63968b2018-04-08 19:09:17 +0200492 if (taps & BIT(i))
493 ntap++;
494 else {
495 if (ntap > tap_cnt) {
496 tap_start = i - ntap;
497 tap_end = i - 1;
498 tap_cnt = ntap;
499 }
500 ntap = 0;
501 }
502 }
503
504 if (ntap > tap_cnt) {
505 tap_start = i - ntap;
506 tap_end = i - 1;
507 tap_cnt = ntap;
508 }
509
510 /*
511 * If all of the TAP is OK, the sampling clock position is selected by
512 * identifying the change point of data.
513 */
Marek Vasut0196a582019-11-23 13:36:17 +0100514 if (tap_cnt == priv->tap_num * 2) {
Marek Vasutf63968b2018-04-08 19:09:17 +0200515 match_cnt = 0;
516 ntap = 0;
517 tap_start = 0;
518 tap_end = 0;
Marek Vasut0196a582019-11-23 13:36:17 +0100519 for (i = 0; i < priv->tap_num * 2; i++) {
Marek Vasut37c39902019-11-23 13:36:18 +0100520 if (priv->smpcmp & BIT(i))
Marek Vasutf63968b2018-04-08 19:09:17 +0200521 ntap++;
522 else {
523 if (ntap > match_cnt) {
524 tap_start = i - ntap;
525 tap_end = i - 1;
526 match_cnt = ntap;
527 }
528 ntap = 0;
529 }
530 }
531 if (ntap > match_cnt) {
532 tap_start = i - ntap;
533 tap_end = i - 1;
534 match_cnt = ntap;
535 }
536 if (match_cnt)
537 select = true;
538 } else if (tap_cnt >= RENESAS_SDHI_MAX_TAP)
539 select = true;
540
541 if (select)
Marek Vasut0196a582019-11-23 13:36:17 +0100542 priv->tap_set = ((tap_start + tap_end) / 2) % priv->tap_num;
Marek Vasutf63968b2018-04-08 19:09:17 +0200543 else
544 return -EIO;
545
546 /* Set SCC */
Marek Vasut95ead3d2018-06-13 08:02:55 +0200547 tmio_sd_writel(priv, priv->tap_set, RENESAS_SDHI_SCC_TAPSET);
Marek Vasutf63968b2018-04-08 19:09:17 +0200548
549 /* Enable auto re-tuning */
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200550 reg = tmio_sd_readl(priv, RENESAS_SDHI_SCC_RVSCNTL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200551 reg |= RENESAS_SDHI_SCC_RVSCNTL_RVSEN;
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200552 tmio_sd_writel(priv, reg, RENESAS_SDHI_SCC_RVSCNTL);
Marek Vasutf63968b2018-04-08 19:09:17 +0200553
554 return 0;
555}
556
557int renesas_sdhi_execute_tuning(struct udevice *dev, uint opcode)
558{
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200559 struct tmio_sd_priv *priv = dev_get_priv(dev);
Marek Vasutf63968b2018-04-08 19:09:17 +0200560 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
561 struct mmc *mmc = upriv->mmc;
562 unsigned int tap_num;
Marek Vasut37c39902019-11-23 13:36:18 +0100563 unsigned int taps = 0;
Marek Vasutf63968b2018-04-08 19:09:17 +0200564 int i, ret = 0;
565 u32 caps;
566
567 /* Only supported on Renesas RCar */
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200568 if (!(priv->caps & TMIO_SD_CAP_RCAR_UHS))
Marek Vasutf63968b2018-04-08 19:09:17 +0200569 return -EINVAL;
570
571 /* clock tuning is not needed for upto 52MHz */
572 if (!((mmc->selected_mode == MMC_HS_200) ||
Marek Vasut50aa1d92018-06-13 08:02:55 +0200573 (mmc->selected_mode == MMC_HS_400) ||
Marek Vasutf63968b2018-04-08 19:09:17 +0200574 (mmc->selected_mode == UHS_SDR104) ||
575 (mmc->selected_mode == UHS_SDR50)))
576 return 0;
577
578 tap_num = renesas_sdhi_init_tuning(priv);
579 if (!tap_num)
580 /* Tuning is not supported */
581 goto out;
582
Marek Vasut0196a582019-11-23 13:36:17 +0100583 priv->tap_num = tap_num;
584
585 if (priv->tap_num * 2 >= sizeof(taps) * 8) {
Marek Vasutf63968b2018-04-08 19:09:17 +0200586 dev_err(dev,
587 "Too many taps, skipping tuning. Please consider updating size of taps field of tmio_mmc_host\n");
588 goto out;
589 }
590
Marek Vasut37c39902019-11-23 13:36:18 +0100591 priv->smpcmp = 0;
592
Marek Vasutf63968b2018-04-08 19:09:17 +0200593 /* Issue CMD19 twice for each tap */
Marek Vasut0196a582019-11-23 13:36:17 +0100594 for (i = 0; i < 2 * priv->tap_num; i++) {
595 renesas_sdhi_prepare_tuning(priv, i % priv->tap_num);
Marek Vasutf63968b2018-04-08 19:09:17 +0200596
597 /* Force PIO for the tuning */
598 caps = priv->caps;
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200599 priv->caps &= ~TMIO_SD_CAP_DMA_INTERNAL;
Marek Vasutf63968b2018-04-08 19:09:17 +0200600
601 ret = mmc_send_tuning(mmc, opcode, NULL);
602
603 priv->caps = caps;
604
605 if (ret == 0)
606 taps |= BIT(i);
607
608 ret = renesas_sdhi_compare_scc_data(priv);
609 if (ret == 0)
Marek Vasut37c39902019-11-23 13:36:18 +0100610 priv->smpcmp |= BIT(i);
Marek Vasutf63968b2018-04-08 19:09:17 +0200611
612 mdelay(1);
613 }
614
Marek Vasut37c39902019-11-23 13:36:18 +0100615 ret = renesas_sdhi_select_tuning(priv, taps);
Marek Vasutf63968b2018-04-08 19:09:17 +0200616
617out:
618 if (ret < 0) {
619 dev_warn(dev, "Tuning procedure failed\n");
620 renesas_sdhi_reset_tuning(priv);
621 }
622
623 return ret;
624}
Marek Vasut50aa1d92018-06-13 08:02:55 +0200625#else
626static int renesas_sdhi_hs400(struct udevice *dev)
627{
628 return 0;
629}
Marek Vasutf63968b2018-04-08 19:09:17 +0200630#endif
631
632static int renesas_sdhi_set_ios(struct udevice *dev)
633{
Marek Vasut50aa1d92018-06-13 08:02:55 +0200634 struct tmio_sd_priv *priv = dev_get_priv(dev);
635 u32 tmp;
636 int ret;
637
638 /* Stop the clock before changing its rate to avoid a glitch signal */
639 tmp = tmio_sd_readl(priv, TMIO_SD_CLKCTL);
640 tmp &= ~TMIO_SD_CLKCTL_SCLKEN;
641 tmio_sd_writel(priv, tmp, TMIO_SD_CLKCTL);
642
643 ret = renesas_sdhi_hs400(dev);
644 if (ret)
645 return ret;
646
647 ret = tmio_sd_set_ios(dev);
Marek Vasutcf39f3f2018-04-09 20:47:31 +0200648
649 mdelay(10);
650
Marek Vasut50aa1d92018-06-13 08:02:55 +0200651#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
652 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
653 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
654 struct mmc *mmc = mmc_get_mmc_dev(dev);
655 if ((priv->caps & TMIO_SD_CAP_RCAR_UHS) &&
656 (mmc->selected_mode != UHS_SDR104) &&
657 (mmc->selected_mode != MMC_HS_200) &&
658 (mmc->selected_mode != MMC_HS_400)) {
Marek Vasut52e17962018-10-28 15:30:06 +0100659 renesas_sdhi_reset_tuning(priv);
Marek Vasut50aa1d92018-06-13 08:02:55 +0200660 }
Marek Vasutf63968b2018-04-08 19:09:17 +0200661#endif
662
663 return ret;
664}
665
Marek Vasut2fc10752018-10-28 19:28:56 +0100666#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300667static int renesas_sdhi_wait_dat0(struct udevice *dev, int state,
668 int timeout_us)
Marek Vasut2fc10752018-10-28 19:28:56 +0100669{
670 int ret = -ETIMEDOUT;
671 bool dat0_high;
672 bool target_dat0_high = !!state;
673 struct tmio_sd_priv *priv = dev_get_priv(dev);
674
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300675 timeout_us = DIV_ROUND_UP(timeout_us, 10); /* check every 10 us. */
676 while (timeout_us--) {
Marek Vasut2fc10752018-10-28 19:28:56 +0100677 dat0_high = !!(tmio_sd_readl(priv, TMIO_SD_INFO2) & TMIO_SD_INFO2_DAT0);
678 if (dat0_high == target_dat0_high) {
679 ret = 0;
680 break;
681 }
682 udelay(10);
683 }
684
685 return ret;
686}
687#endif
688
Marek Vasutd2661d82020-04-04 12:45:04 +0200689#define RENESAS_SDHI_DMA_ALIGNMENT 128
690
Marek Vasut4a66d4e2020-04-04 12:45:06 +0200691static int renesas_sdhi_addr_aligned_gen(uintptr_t ubuf,
692 size_t len, size_t len_aligned)
Marek Vasutd2661d82020-04-04 12:45:04 +0200693{
Marek Vasutd2661d82020-04-04 12:45:04 +0200694 /* Check if start is aligned */
695 if (!IS_ALIGNED(ubuf, RENESAS_SDHI_DMA_ALIGNMENT)) {
Marek Vasut4a66d4e2020-04-04 12:45:06 +0200696 debug("Unaligned buffer address %lx\n", ubuf);
Marek Vasutd2661d82020-04-04 12:45:04 +0200697 return 0;
698 }
699
700 /* Check if length is aligned */
Marek Vasut4a66d4e2020-04-04 12:45:06 +0200701 if (len != len_aligned) {
702 debug("Unaligned buffer length %zu\n", len);
Marek Vasutd2661d82020-04-04 12:45:04 +0200703 return 0;
704 }
705
706#ifdef CONFIG_PHYS_64BIT
707 /* Check if below 32bit boundary */
Marek Vasut4a66d4e2020-04-04 12:45:06 +0200708 if ((ubuf >> 32) || (ubuf + len_aligned) >> 32) {
709 debug("Buffer above 32bit boundary %lx-%lx\n",
710 ubuf, ubuf + len_aligned);
Marek Vasutd2661d82020-04-04 12:45:04 +0200711 return 0;
712 }
713#endif
714
715 /* Aligned */
716 return 1;
717}
718
Marek Vasut4a66d4e2020-04-04 12:45:06 +0200719static int renesas_sdhi_addr_aligned(struct bounce_buffer *state)
720{
721 uintptr_t ubuf = (uintptr_t)state->user_buffer;
722
723 return renesas_sdhi_addr_aligned_gen(ubuf, state->len,
724 state->len_aligned);
725}
726
Marek Vasutb5900a52019-05-19 03:47:07 +0200727static int renesas_sdhi_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
728 struct mmc_data *data)
729{
Marek Vasutd2661d82020-04-04 12:45:04 +0200730 struct bounce_buffer bbstate;
731 unsigned int bbflags;
732 bool bbok = false;
733 size_t len;
734 void *buf;
Marek Vasutb5900a52019-05-19 03:47:07 +0200735 int ret;
736
Marek Vasutd2661d82020-04-04 12:45:04 +0200737 if (data) {
738 if (data->flags & MMC_DATA_READ) {
739 buf = data->dest;
740 bbflags = GEN_BB_WRITE;
741 } else {
742 buf = (void *)data->src;
743 bbflags = GEN_BB_READ;
744 }
745 len = data->blocks * data->blocksize;
746
747 ret = bounce_buffer_start_extalign(&bbstate, buf, len, bbflags,
748 RENESAS_SDHI_DMA_ALIGNMENT,
749 renesas_sdhi_addr_aligned);
750 /*
751 * If the amount of data to transfer is too large, we can get
752 * -ENOMEM when starting the bounce buffer. If that happens,
753 * fall back to PIO as it was before, otherwise use the BB.
754 */
755 if (!ret) {
756 bbok = true;
757 if (data->flags & MMC_DATA_READ)
758 data->dest = bbstate.bounce_buffer;
759 else
760 data->src = bbstate.bounce_buffer;
761 }
762 }
763
Marek Vasutb5900a52019-05-19 03:47:07 +0200764 ret = tmio_sd_send_cmd(dev, cmd, data);
Marek Vasutd2661d82020-04-04 12:45:04 +0200765
766 if (data && bbok) {
767 buf = bbstate.user_buffer;
768
769 bounce_buffer_stop(&bbstate);
770
771 if (data->flags & MMC_DATA_READ)
772 data->dest = buf;
773 else
774 data->src = buf;
775 }
776
Marek Vasutb5900a52019-05-19 03:47:07 +0200777 if (ret)
778 return ret;
779
780#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
781 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
782 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
783 struct tmio_sd_priv *priv = dev_get_priv(dev);
784
Marek Vasut69000662019-11-23 13:36:23 +0100785 renesas_sdhi_check_scc_error(dev);
786
Marek Vasutb5900a52019-05-19 03:47:07 +0200787 if (cmd->cmdidx == MMC_CMD_SEND_STATUS)
788 renesas_sdhi_adjust_hs400_mode_enable(priv);
789#endif
790
791 return 0;
792}
793
Marek Vasut4a66d4e2020-04-04 12:45:06 +0200794int renesas_sdhi_get_b_max(struct udevice *dev, void *dst, lbaint_t blkcnt)
795{
796 struct tmio_sd_priv *priv = dev_get_priv(dev);
797 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
798 struct mmc *mmc = upriv->mmc;
799 size_t len = blkcnt * mmc->read_bl_len;
800 size_t len_align = roundup(len, RENESAS_SDHI_DMA_ALIGNMENT);
801
802 if (renesas_sdhi_addr_aligned_gen((uintptr_t)dst, len, len_align)) {
803 if (priv->quirks & TMIO_SD_CAP_16BIT)
804 return U16_MAX;
805 else
806 return U32_MAX;
807 } else {
808 return (CONFIG_SYS_MALLOC_LEN / 4) / mmc->read_bl_len;
809 }
810}
811
Marek Vasute94cad92018-04-08 15:22:58 +0200812static const struct dm_mmc_ops renesas_sdhi_ops = {
Marek Vasutb5900a52019-05-19 03:47:07 +0200813 .send_cmd = renesas_sdhi_send_cmd,
Marek Vasutf63968b2018-04-08 19:09:17 +0200814 .set_ios = renesas_sdhi_set_ios,
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200815 .get_cd = tmio_sd_get_cd,
Marek Vasut50aa1d92018-06-13 08:02:55 +0200816#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
817 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
818 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
Marek Vasutf63968b2018-04-08 19:09:17 +0200819 .execute_tuning = renesas_sdhi_execute_tuning,
820#endif
Marek Vasut2fc10752018-10-28 19:28:56 +0100821#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
822 .wait_dat0 = renesas_sdhi_wait_dat0,
823#endif
Marek Vasut4a66d4e2020-04-04 12:45:06 +0200824 .get_b_max = renesas_sdhi_get_b_max,
Marek Vasute94cad92018-04-08 15:22:58 +0200825};
826
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200827#define RENESAS_GEN2_QUIRKS TMIO_SD_CAP_RCAR_GEN2
Marek Vasutf98833d2018-04-08 18:49:52 +0200828#define RENESAS_GEN3_QUIRKS \
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200829 TMIO_SD_CAP_64BIT | TMIO_SD_CAP_RCAR_GEN3 | TMIO_SD_CAP_RCAR_UHS
Marek Vasutf98833d2018-04-08 18:49:52 +0200830
Marek Vasute94cad92018-04-08 15:22:58 +0200831static const struct udevice_id renesas_sdhi_match[] = {
Marek Vasutf98833d2018-04-08 18:49:52 +0200832 { .compatible = "renesas,sdhi-r8a7790", .data = RENESAS_GEN2_QUIRKS },
833 { .compatible = "renesas,sdhi-r8a7791", .data = RENESAS_GEN2_QUIRKS },
834 { .compatible = "renesas,sdhi-r8a7792", .data = RENESAS_GEN2_QUIRKS },
835 { .compatible = "renesas,sdhi-r8a7793", .data = RENESAS_GEN2_QUIRKS },
836 { .compatible = "renesas,sdhi-r8a7794", .data = RENESAS_GEN2_QUIRKS },
837 { .compatible = "renesas,sdhi-r8a7795", .data = RENESAS_GEN3_QUIRKS },
838 { .compatible = "renesas,sdhi-r8a7796", .data = RENESAS_GEN3_QUIRKS },
Adam Ford4ce95662020-06-30 09:30:10 -0500839 { .compatible = "renesas,rcar-gen3-sdhi", .data = RENESAS_GEN3_QUIRKS },
Marek Vasutf98833d2018-04-08 18:49:52 +0200840 { .compatible = "renesas,sdhi-r8a77965", .data = RENESAS_GEN3_QUIRKS },
841 { .compatible = "renesas,sdhi-r8a77970", .data = RENESAS_GEN3_QUIRKS },
Marek Vasutd6291522018-04-26 13:19:29 +0200842 { .compatible = "renesas,sdhi-r8a77990", .data = RENESAS_GEN3_QUIRKS },
Marek Vasutf98833d2018-04-08 18:49:52 +0200843 { .compatible = "renesas,sdhi-r8a77995", .data = RENESAS_GEN3_QUIRKS },
Marek Vasute94cad92018-04-08 15:22:58 +0200844 { /* sentinel */ }
845};
846
Marek Vasut8ec6a042018-06-13 08:02:55 +0200847static ulong renesas_sdhi_clk_get_rate(struct tmio_sd_priv *priv)
848{
849 return clk_get_rate(&priv->clk);
850}
851
Marek Vasutd34bd2d2018-06-13 08:02:55 +0200852static void renesas_sdhi_filter_caps(struct udevice *dev)
853{
Marek Vasutd34bd2d2018-06-13 08:02:55 +0200854 struct tmio_sd_priv *priv = dev_get_priv(dev);
855
856 if (!(priv->caps & TMIO_SD_CAP_RCAR_GEN3))
857 return;
858
Marek Vasut8a73bef2021-01-03 11:38:25 +0100859 if (priv->caps & TMIO_SD_CAP_DMA_INTERNAL)
860 priv->idma_bus_width = TMIO_SD_DMA_MODE_BUS_WIDTH;
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)
Simon Glassc69cda22020-12-03 16:55:20 -0700865 struct tmio_sd_plat *plat = dev_get_plat(dev);
Marek Vasut56b0bb92019-11-23 13:36:25 +0100866
Hai Pham6d7d4c42023-01-26 21:05:56 +0100867 /* HS400 is not supported on H3 ES1.x, M3W ES1.[012], V3M, V3H ES1.x, D3 */
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) &&
Hai Pham6d7d4c42023-01-26 21:05:56 +0100872 (rmobile_get_cpu_rev_fraction() <= 2)) ||
873 (rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A77970) ||
874 ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A77980) &&
875 (rmobile_get_cpu_rev_integer() <= 1)) ||
876 (rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A77995))
Marek Vasutd34bd2d2018-06-13 08:02:55 +0200877 plat->cfg.host_caps &= ~MMC_MODE_HS400;
Marek Vasut50aa1d92018-06-13 08:02:55 +0200878
Marek Vasut1bdcb832019-11-23 13:36:24 +0100879 /* H3 ES2.0, ES3.0 and M3W ES1.2 and M3N bad taps */
880 if (((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7795) &&
881 (rmobile_get_cpu_rev_integer() >= 2)) ||
882 ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7796) &&
883 (rmobile_get_cpu_rev_integer() == 1) &&
884 (rmobile_get_cpu_rev_fraction() == 2)) ||
885 (rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A77965))
886 priv->hs400_bad_tap = BIT(2) | BIT(3) | BIT(6) | BIT(7);
887
Marek Vasut56b0bb92019-11-23 13:36:25 +0100888 /* H3 ES3.0 can use HS400 with manual adjustment */
889 if ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7795) &&
890 (rmobile_get_cpu_rev_integer() >= 3)) {
891 priv->adjust_hs400_enable = true;
892 priv->adjust_hs400_offset = 0;
893 priv->adjust_hs400_calib_table =
894 r8a7795_calib_table[!rmobile_is_gen3_mmc0(priv)];
895 }
896
Marek Vasut1bdcb832019-11-23 13:36:24 +0100897 /* M3W ES1.x for x>2 can use HS400 with manual adjustment and taps */
Marek Vasutb5900a52019-05-19 03:47:07 +0200898 if ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7796) &&
899 (rmobile_get_cpu_rev_integer() == 1) &&
900 (rmobile_get_cpu_rev_fraction() > 2)) {
901 priv->adjust_hs400_enable = true;
Hai Pham1cdabcc2023-01-26 21:05:58 +0100902 priv->adjust_hs400_offset = 3;
Marek Vasut1bdcb832019-11-23 13:36:24 +0100903 priv->hs400_bad_tap = BIT(1) | BIT(3) | BIT(5) | BIT(7);
Marek Vasut56b0bb92019-11-23 13:36:25 +0100904 priv->adjust_hs400_calib_table =
Hai Pham6d7d4c42023-01-26 21:05:56 +0100905 r8a7796_rev13_calib_table[!rmobile_is_gen3_mmc0(priv)];
Marek Vasutb5900a52019-05-19 03:47:07 +0200906 }
907
908 /* M3N can use HS400 with manual adjustment */
909 if (rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A77965) {
910 priv->adjust_hs400_enable = true;
Marek Vasute5d3f3d2019-11-23 13:36:21 +0100911 priv->adjust_hs400_offset = 3;
Marek Vasut56b0bb92019-11-23 13:36:25 +0100912 priv->adjust_hs400_calib_table =
913 r8a77965_calib_table[!rmobile_is_gen3_mmc0(priv)];
Marek Vasutb5900a52019-05-19 03:47:07 +0200914 }
915
916 /* E3 can use HS400 with manual adjustment */
917 if (rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A77990) {
918 priv->adjust_hs400_enable = true;
Marek Vasute5d3f3d2019-11-23 13:36:21 +0100919 priv->adjust_hs400_offset = 3;
Marek Vasut56b0bb92019-11-23 13:36:25 +0100920 priv->adjust_hs400_calib_table =
921 r8a77990_calib_table[!rmobile_is_gen3_mmc0(priv)];
Marek Vasutb5900a52019-05-19 03:47:07 +0200922 }
923
Hai Pham01cbce82023-01-26 21:05:55 +0100924 /* H3 ES1.x, ES2.0 and M3W ES1.[0123] uses 4 tuning taps */
Marek Vasut81099882019-11-23 13:36:19 +0100925 if (((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7795) &&
926 (rmobile_get_cpu_rev_integer() <= 2)) ||
927 ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7796) &&
928 (rmobile_get_cpu_rev_integer() == 1) &&
Hai Pham01cbce82023-01-26 21:05:55 +0100929 (rmobile_get_cpu_rev_fraction() <= 3)))
Marek Vasut50aa1d92018-06-13 08:02:55 +0200930 priv->nrtaps = 4;
931 else
932 priv->nrtaps = 8;
Marek Vasut56b0bb92019-11-23 13:36:25 +0100933#endif
Marek Vasut992bcf42019-01-11 23:45:54 +0100934 /* H3 ES1.x and M3W ES1.0 uses bit 17 for DTRAEND */
935 if (((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7795) &&
936 (rmobile_get_cpu_rev_integer() <= 1)) ||
937 ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A7796) &&
938 (rmobile_get_cpu_rev_integer() == 1) &&
939 (rmobile_get_cpu_rev_fraction() == 0)))
940 priv->read_poll_flag = TMIO_SD_DMA_INFO1_END_RD;
941 else
942 priv->read_poll_flag = TMIO_SD_DMA_INFO1_END_RD2;
Marek Vasutd34bd2d2018-06-13 08:02:55 +0200943}
944
Marek Vasutc769e602018-04-08 17:45:23 +0200945static int renesas_sdhi_probe(struct udevice *dev)
946{
Masahiro Yamada30b5d9a2018-04-20 18:14:24 +0900947 struct tmio_sd_priv *priv = dev_get_priv(dev);
Marek Vasutc769e602018-04-08 17:45:23 +0200948 u32 quirks = dev_get_driver_data(dev);
Marek Vasut7cf7ef82018-04-08 18:14:22 +0200949 struct fdt_resource reg_res;
950 DECLARE_GLOBAL_DATA_PTR;
951 int ret;
952
Marek Vasut8ec6a042018-06-13 08:02:55 +0200953 priv->clk_get_rate = renesas_sdhi_clk_get_rate;
954
Marek Vasutf98833d2018-04-08 18:49:52 +0200955 if (quirks == RENESAS_GEN2_QUIRKS) {
956 ret = fdt_get_resource(gd->fdt_blob, dev_of_offset(dev),
957 "reg", 0, &reg_res);
958 if (ret < 0) {
959 dev_err(dev, "\"reg\" resource not found, ret=%i\n",
960 ret);
961 return ret;
962 }
Marek Vasut7cf7ef82018-04-08 18:14:22 +0200963
Marek Vasutf98833d2018-04-08 18:49:52 +0200964 if (fdt_resource_size(&reg_res) == 0x100)
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200965 quirks |= TMIO_SD_CAP_16BIT;
Marek Vasutf98833d2018-04-08 18:49:52 +0200966 }
Marek Vasutc769e602018-04-08 17:45:23 +0200967
Marek Vasut8ec6a042018-06-13 08:02:55 +0200968 ret = clk_get_by_index(dev, 0, &priv->clk);
Masahiro Yamada30b5d9a2018-04-20 18:14:24 +0900969 if (ret < 0) {
970 dev_err(dev, "failed to get host clock\n");
971 return ret;
972 }
973
974 /* set to max rate */
Marek Vasut8ec6a042018-06-13 08:02:55 +0200975 ret = clk_set_rate(&priv->clk, 200000000);
976 if (ret < 0) {
Masahiro Yamada30b5d9a2018-04-20 18:14:24 +0900977 dev_err(dev, "failed to set rate for host clock\n");
Marek Vasut8ec6a042018-06-13 08:02:55 +0200978 clk_free(&priv->clk);
979 return ret;
Masahiro Yamada30b5d9a2018-04-20 18:14:24 +0900980 }
981
Marek Vasut8ec6a042018-06-13 08:02:55 +0200982 ret = clk_enable(&priv->clk);
Masahiro Yamada30b5d9a2018-04-20 18:14:24 +0900983 if (ret) {
984 dev_err(dev, "failed to enable host clock\n");
985 return ret;
986 }
987
Marek Vasut4a66d4e2020-04-04 12:45:06 +0200988 priv->quirks = quirks;
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200989 ret = tmio_sd_probe(dev, quirks);
Marek Vasutd34bd2d2018-06-13 08:02:55 +0200990
991 renesas_sdhi_filter_caps(dev);
992
Marek Vasut50aa1d92018-06-13 08:02:55 +0200993#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
994 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
995 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
Marek Vasut52e17962018-10-28 15:30:06 +0100996 if (!ret && (priv->caps & TMIO_SD_CAP_RCAR_UHS))
Marek Vasut65186972018-08-30 15:27:26 +0200997 renesas_sdhi_reset_tuning(priv);
Marek Vasutf63968b2018-04-08 19:09:17 +0200998#endif
999 return ret;
Marek Vasutc769e602018-04-08 17:45:23 +02001000}
1001
Marek Vasute94cad92018-04-08 15:22:58 +02001002U_BOOT_DRIVER(renesas_sdhi) = {
1003 .name = "renesas-sdhi",
1004 .id = UCLASS_MMC,
1005 .of_match = renesas_sdhi_match,
Marek Vasutcb0b6b02018-04-13 23:51:33 +02001006 .bind = tmio_sd_bind,
Marek Vasutc769e602018-04-08 17:45:23 +02001007 .probe = renesas_sdhi_probe,
Simon Glass41575d82020-12-03 16:55:17 -07001008 .priv_auto = sizeof(struct tmio_sd_priv),
Simon Glasscaa4daa2020-12-03 16:55:18 -07001009 .plat_auto = sizeof(struct tmio_sd_plat),
Marek Vasute94cad92018-04-08 15:22:58 +02001010 .ops = &renesas_sdhi_ops,
1011};