blob: b4c60a48d2e532181b68a921eaad4185a323a4b7 [file] [log] [blame]
Lokesh Vutlabc9979f2018-08-27 15:57:54 +05301// SPDX-License-Identifier: GPL-2.0+
2/*
Nishanth Menoneaa39c62023-11-01 15:56:03 -05003 * Copyright (C) 2018 Texas Instruments Incorporated - https://www.ti.com/
Lokesh Vutlabc9979f2018-08-27 15:57:54 +05304 *
5 * Texas Instruments' K3 SD Host Controller Interface
6 */
7
8#include <clk.h>
Lokesh Vutlabc9979f2018-08-27 15:57:54 +05309#include <dm.h>
10#include <malloc.h>
Faiz Abbas7a7e2c72021-02-04 15:10:53 +053011#include <mmc.h>
Lokesh Vutlabc9979f2018-08-27 15:57:54 +053012#include <power-domain.h>
Faiz Abbase9aed582019-06-11 00:43:38 +053013#include <regmap.h>
Lokesh Vutlabc9979f2018-08-27 15:57:54 +053014#include <sdhci.h>
Faiz Abbas2c2fc962021-02-04 15:10:50 +053015#include <soc.h>
Simon Glass9bc15642020-02-03 07:36:16 -070016#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060017#include <linux/bitops.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070018#include <linux/err.h>
Lokesh Vutlabc9979f2018-08-27 15:57:54 +053019
Faiz Abbase9aed582019-06-11 00:43:38 +053020/* CTL_CFG Registers */
21#define CTL_CFG_2 0x14
22
23#define SLOTTYPE_MASK GENMASK(31, 30)
24#define SLOTTYPE_EMBEDDED BIT(30)
25
26/* PHY Registers */
27#define PHY_CTRL1 0x100
28#define PHY_CTRL2 0x104
29#define PHY_CTRL3 0x108
30#define PHY_CTRL4 0x10C
31#define PHY_CTRL5 0x110
32#define PHY_CTRL6 0x114
33#define PHY_STAT1 0x130
34#define PHY_STAT2 0x134
35
36#define IOMUX_ENABLE_SHIFT 31
37#define IOMUX_ENABLE_MASK BIT(IOMUX_ENABLE_SHIFT)
38#define OTAPDLYENA_SHIFT 20
39#define OTAPDLYENA_MASK BIT(OTAPDLYENA_SHIFT)
40#define OTAPDLYSEL_SHIFT 12
41#define OTAPDLYSEL_MASK GENMASK(15, 12)
42#define STRBSEL_SHIFT 24
Faiz Abbas8cc051e2020-01-16 19:42:19 +053043#define STRBSEL_4BIT_MASK GENMASK(27, 24)
44#define STRBSEL_8BIT_MASK GENMASK(31, 24)
Faiz Abbase9aed582019-06-11 00:43:38 +053045#define SEL50_SHIFT 8
46#define SEL50_MASK BIT(SEL50_SHIFT)
47#define SEL100_SHIFT 9
48#define SEL100_MASK BIT(SEL100_SHIFT)
Faiz Abbas8cc051e2020-01-16 19:42:19 +053049#define FREQSEL_SHIFT 8
50#define FREQSEL_MASK GENMASK(10, 8)
Faiz Abbasc73f04e2021-02-04 15:10:52 +053051#define CLKBUFSEL_SHIFT 0
52#define CLKBUFSEL_MASK GENMASK(2, 0)
Faiz Abbase9aed582019-06-11 00:43:38 +053053#define DLL_TRIM_ICP_SHIFT 4
54#define DLL_TRIM_ICP_MASK GENMASK(7, 4)
55#define DR_TY_SHIFT 20
56#define DR_TY_MASK GENMASK(22, 20)
57#define ENDLL_SHIFT 1
58#define ENDLL_MASK BIT(ENDLL_SHIFT)
59#define DLLRDY_SHIFT 0
60#define DLLRDY_MASK BIT(DLLRDY_SHIFT)
61#define PDB_SHIFT 0
62#define PDB_MASK BIT(PDB_SHIFT)
63#define CALDONE_SHIFT 1
64#define CALDONE_MASK BIT(CALDONE_SHIFT)
65#define RETRIM_SHIFT 17
66#define RETRIM_MASK BIT(RETRIM_SHIFT)
Faiz Abbasdef2a0f2021-02-04 15:10:51 +053067#define SELDLYTXCLK_SHIFT 17
68#define SELDLYTXCLK_MASK BIT(SELDLYTXCLK_SHIFT)
69#define SELDLYRXCLK_SHIFT 16
70#define SELDLYRXCLK_MASK BIT(SELDLYRXCLK_SHIFT)
71#define ITAPDLYSEL_SHIFT 0
72#define ITAPDLYSEL_MASK GENMASK(4, 0)
73#define ITAPDLYENA_SHIFT 8
74#define ITAPDLYENA_MASK BIT(ITAPDLYENA_SHIFT)
75#define ITAPCHGWIN_SHIFT 9
76#define ITAPCHGWIN_MASK BIT(ITAPCHGWIN_SHIFT)
Faiz Abbase9aed582019-06-11 00:43:38 +053077
78#define DRIVER_STRENGTH_50_OHM 0x0
79#define DRIVER_STRENGTH_33_OHM 0x1
80#define DRIVER_STRENGTH_66_OHM 0x2
81#define DRIVER_STRENGTH_100_OHM 0x3
82#define DRIVER_STRENGTH_40_OHM 0x4
83
Faiz Abbasd8fb3092019-06-11 00:43:31 +053084#define AM654_SDHCI_MIN_FREQ 400000
Faiz Abbasdef2a0f2021-02-04 15:10:51 +053085#define CLOCK_TOO_SLOW_HZ 50000000
Lokesh Vutlabc9979f2018-08-27 15:57:54 +053086
Judith Mendez87bcbde2024-04-18 14:00:58 -050087#define ENABLE 0x1
88
Faiz Abbasd8fb3092019-06-11 00:43:31 +053089struct am654_sdhci_plat {
Lokesh Vutlabc9979f2018-08-27 15:57:54 +053090 struct mmc_config cfg;
91 struct mmc mmc;
Faiz Abbase9aed582019-06-11 00:43:38 +053092 struct regmap *base;
93 bool non_removable;
Faiz Abbas7101e122020-07-29 07:03:41 +053094 u32 otap_del_sel[MMC_MODES_END];
Faiz Abbasdef2a0f2021-02-04 15:10:51 +053095 u32 itap_del_sel[MMC_MODES_END];
Judith Mendez87bcbde2024-04-18 14:00:58 -050096 u32 itap_del_ena[MMC_MODES_END];
Faiz Abbase9aed582019-06-11 00:43:38 +053097 u32 trm_icp;
98 u32 drv_strength;
Faiz Abbas8cc051e2020-01-16 19:42:19 +053099 u32 strb_sel;
Faiz Abbasc73f04e2021-02-04 15:10:52 +0530100 u32 clkbuf_sel;
Faiz Abbasfd8be702019-06-13 10:29:51 +0530101 u32 flags;
Judith Mendez81c1d442024-04-18 14:00:56 -0500102 bool dll_enable;
Faiz Abbasb7f57bb2021-02-04 15:10:48 +0530103#define DLL_PRESENT BIT(0)
104#define IOMUX_PRESENT BIT(1)
105#define FREQSEL_2_BIT BIT(2)
106#define STRBSEL_4_BIT BIT(3)
Faiz Abbas947e8f32021-02-04 15:10:49 +0530107#define DLL_CALIB BIT(4)
Emanuele Ghidolifde0dff2024-07-02 21:54:29 +0200108 u32 quirks;
109#define SDHCI_AM654_QUIRK_FORCE_CDTEST BIT(0)
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530110};
111
Faiz Abbasc6eb9e72020-02-26 13:44:33 +0530112struct timing_data {
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530113 const char *otap_binding;
114 const char *itap_binding;
Faiz Abbasc6eb9e72020-02-26 13:44:33 +0530115 u32 capability;
116};
117
Judith Mendez81c1d442024-04-18 14:00:56 -0500118struct window {
119 u8 start;
120 u8 end;
121 u8 length;
122};
123
Faiz Abbasc6eb9e72020-02-26 13:44:33 +0530124static const struct timing_data td[] = {
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530125 [MMC_LEGACY] = {"ti,otap-del-sel-legacy",
126 "ti,itap-del-sel-legacy",
127 0},
128 [MMC_HS] = {"ti,otap-del-sel-mmc-hs",
129 "ti,itap-del-sel-mms-hs",
130 MMC_CAP(MMC_HS)},
131 [SD_HS] = {"ti,otap-del-sel-sd-hs",
132 "ti,itap-del-sel-sd-hs",
133 MMC_CAP(SD_HS)},
134 [UHS_SDR12] = {"ti,otap-del-sel-sdr12",
135 "ti,itap-del-sel-sdr12",
136 MMC_CAP(UHS_SDR12)},
137 [UHS_SDR25] = {"ti,otap-del-sel-sdr25",
138 "ti,itap-del-sel-sdr25",
139 MMC_CAP(UHS_SDR25)},
140 [UHS_SDR50] = {"ti,otap-del-sel-sdr50",
141 NULL,
142 MMC_CAP(UHS_SDR50)},
143 [UHS_SDR104] = {"ti,otap-del-sel-sdr104",
144 NULL,
145 MMC_CAP(UHS_SDR104)},
146 [UHS_DDR50] = {"ti,otap-del-sel-ddr50",
147 NULL,
148 MMC_CAP(UHS_DDR50)},
149 [MMC_DDR_52] = {"ti,otap-del-sel-ddr52",
150 "ti,itap-del-sel-ddr52",
151 MMC_CAP(MMC_DDR_52)},
152 [MMC_HS_200] = {"ti,otap-del-sel-hs200",
153 NULL,
154 MMC_CAP(MMC_HS_200)},
155 [MMC_HS_400] = {"ti,otap-del-sel-hs400",
156 NULL,
157 MMC_CAP(MMC_HS_400)},
Faiz Abbasc6eb9e72020-02-26 13:44:33 +0530158};
159
Faiz Abbas8cc051e2020-01-16 19:42:19 +0530160struct am654_driver_data {
161 const struct sdhci_ops *ops;
162 u32 flags;
163};
164
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530165static int am654_sdhci_setup_dll(struct am654_sdhci_plat *plat,
166 unsigned int speed)
167{
168 int sel50, sel100, freqsel;
169 u32 mask, val;
170 int ret;
171
172 /* Disable delay chain mode */
173 regmap_update_bits(plat->base, PHY_CTRL5,
174 SELDLYTXCLK_MASK | SELDLYRXCLK_MASK, 0);
175
176 if (plat->flags & FREQSEL_2_BIT) {
177 switch (speed) {
178 case 200000000:
179 sel50 = 0;
180 sel100 = 0;
181 break;
182 case 100000000:
183 sel50 = 0;
184 sel100 = 1;
185 break;
186 default:
187 sel50 = 1;
188 sel100 = 0;
189 }
190
191 /* Configure PHY DLL frequency */
192 mask = SEL50_MASK | SEL100_MASK;
193 val = (sel50 << SEL50_SHIFT) | (sel100 << SEL100_SHIFT);
194 regmap_update_bits(plat->base, PHY_CTRL5, mask, val);
195 } else {
196 switch (speed) {
197 case 200000000:
198 freqsel = 0x0;
199 break;
200 default:
201 freqsel = 0x4;
202 }
203 regmap_update_bits(plat->base, PHY_CTRL5, FREQSEL_MASK,
204 freqsel << FREQSEL_SHIFT);
205 }
206
207 /* Configure DLL TRIM */
208 mask = DLL_TRIM_ICP_MASK;
209 val = plat->trm_icp << DLL_TRIM_ICP_SHIFT;
210
211 /* Configure DLL driver strength */
212 mask |= DR_TY_MASK;
213 val |= plat->drv_strength << DR_TY_SHIFT;
214 regmap_update_bits(plat->base, PHY_CTRL1, mask, val);
215
216 /* Enable DLL */
217 regmap_update_bits(plat->base, PHY_CTRL1, ENDLL_MASK,
218 0x1 << ENDLL_SHIFT);
219 /*
220 * Poll for DLL ready. Use a one second timeout.
221 * Works in all experiments done so far
222 */
223 ret = regmap_read_poll_timeout(plat->base, PHY_STAT1, val,
224 val & DLLRDY_MASK, 1000, 1000000);
225
226 return ret;
227}
228
229static void am654_sdhci_write_itapdly(struct am654_sdhci_plat *plat,
Judith Mendez87bcbde2024-04-18 14:00:58 -0500230 u32 itapdly, u32 enable)
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530231{
Judith Mendez87bcbde2024-04-18 14:00:58 -0500232 regmap_update_bits(plat->base, PHY_CTRL4, ITAPDLYENA_MASK,
233 enable << ITAPDLYENA_SHIFT);
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530234 /* Set ITAPCHGWIN before writing to ITAPDLY */
235 regmap_update_bits(plat->base, PHY_CTRL4, ITAPCHGWIN_MASK,
236 1 << ITAPCHGWIN_SHIFT);
237 regmap_update_bits(plat->base, PHY_CTRL4, ITAPDLYSEL_MASK,
238 itapdly << ITAPDLYSEL_SHIFT);
239 regmap_update_bits(plat->base, PHY_CTRL4, ITAPCHGWIN_MASK, 0);
240}
241
242static void am654_sdhci_setup_delay_chain(struct am654_sdhci_plat *plat,
243 int mode)
244{
245 u32 mask, val;
246
247 val = 1 << SELDLYTXCLK_SHIFT | 1 << SELDLYRXCLK_SHIFT;
248 mask = SELDLYTXCLK_MASK | SELDLYRXCLK_MASK;
249 regmap_update_bits(plat->base, PHY_CTRL5, mask, val);
250
Judith Mendez87bcbde2024-04-18 14:00:58 -0500251 am654_sdhci_write_itapdly(plat, plat->itap_del_sel[mode],
252 plat->itap_del_ena[mode]);
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530253}
254
Faiz Abbase9aed582019-06-11 00:43:38 +0530255static int am654_sdhci_set_ios_post(struct sdhci_host *host)
256{
257 struct udevice *dev = host->mmc->dev;
Simon Glassfa20e932020-12-03 16:55:20 -0700258 struct am654_sdhci_plat *plat = dev_get_plat(dev);
Faiz Abbase9aed582019-06-11 00:43:38 +0530259 unsigned int speed = host->mmc->clock;
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530260 int mode = host->mmc->selected_mode;
Faiz Abbasc6eb9e72020-02-26 13:44:33 +0530261 u32 otap_del_sel;
Faiz Abbase9aed582019-06-11 00:43:38 +0530262 u32 mask, val;
263 int ret;
264
265 /* Reset SD Clock Enable */
266 val = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
267 val &= ~SDHCI_CLOCK_CARD_EN;
268 sdhci_writew(host, val, SDHCI_CLOCK_CONTROL);
269
Faiz Abbas2c45a2c2021-02-04 15:10:47 +0530270 regmap_update_bits(plat->base, PHY_CTRL1, ENDLL_MASK, 0);
Faiz Abbase9aed582019-06-11 00:43:38 +0530271
272 /* restart clock */
273 sdhci_set_clock(host->mmc, speed);
274
275 /* switch phy back on */
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530276 otap_del_sel = plat->otap_del_sel[mode];
277 mask = OTAPDLYENA_MASK | OTAPDLYSEL_MASK;
278 val = (1 << OTAPDLYENA_SHIFT) |
279 (otap_del_sel << OTAPDLYSEL_SHIFT);
Faiz Abbas8cc051e2020-01-16 19:42:19 +0530280
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530281 /* Write to STRBSEL for HS400 speed mode */
282 if (host->mmc->selected_mode == MMC_HS_400) {
283 if (plat->flags & STRBSEL_4_BIT)
284 mask |= STRBSEL_4BIT_MASK;
285 else
286 mask |= STRBSEL_8BIT_MASK;
Faiz Abbas8cc051e2020-01-16 19:42:19 +0530287
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530288 val |= plat->strb_sel << STRBSEL_SHIFT;
289 }
Faiz Abbas947e8f32021-02-04 15:10:49 +0530290
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530291 regmap_update_bits(plat->base, PHY_CTRL4, mask, val);
Faiz Abbas947e8f32021-02-04 15:10:49 +0530292
Judith Mendezc9eb1dc2024-04-18 14:00:59 -0500293 if ((mode > UHS_SDR25 || mode == MMC_DDR_52) && speed >= CLOCK_TOO_SLOW_HZ) {
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530294 ret = am654_sdhci_setup_dll(plat, speed);
Faiz Abbase9aed582019-06-11 00:43:38 +0530295 if (ret)
296 return ret;
Judith Mendez81c1d442024-04-18 14:00:56 -0500297
298 plat->dll_enable = true;
Judith Mendez5d217152024-04-18 14:01:00 -0500299 if (mode == MMC_HS_400) {
300 plat->itap_del_ena[mode] = ENABLE;
301 plat->itap_del_sel[mode] = plat->itap_del_sel[mode - 1];
302 }
303
Judith Mendezc9eb1dc2024-04-18 14:00:59 -0500304 am654_sdhci_write_itapdly(plat, plat->itap_del_sel[mode],
305 plat->itap_del_ena[mode]);
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530306 } else {
307 am654_sdhci_setup_delay_chain(plat, mode);
Judith Mendez81c1d442024-04-18 14:00:56 -0500308 plat->dll_enable = false;
Faiz Abbase9aed582019-06-11 00:43:38 +0530309 }
310
Faiz Abbasc73f04e2021-02-04 15:10:52 +0530311 regmap_update_bits(plat->base, PHY_CTRL5, CLKBUFSEL_MASK,
312 plat->clkbuf_sel);
313
Faiz Abbase9aed582019-06-11 00:43:38 +0530314 return 0;
315}
316
Faiz Abbase9aed582019-06-11 00:43:38 +0530317int am654_sdhci_init(struct am654_sdhci_plat *plat)
318{
319 u32 ctl_cfg_2 = 0;
320 u32 mask, val;
321 int ret;
322
323 /* Reset OTAP to default value */
324 mask = OTAPDLYENA_MASK | OTAPDLYSEL_MASK;
325 regmap_update_bits(plat->base, PHY_CTRL4, mask, 0x0);
326
Faiz Abbas947e8f32021-02-04 15:10:49 +0530327 if (plat->flags & DLL_CALIB) {
Faiz Abbasfd8be702019-06-13 10:29:51 +0530328 regmap_read(plat->base, PHY_STAT1, &val);
329 if (~val & CALDONE_MASK) {
330 /* Calibrate IO lines */
331 regmap_update_bits(plat->base, PHY_CTRL1, PDB_MASK,
332 PDB_MASK);
333 ret = regmap_read_poll_timeout(plat->base, PHY_STAT1,
334 val, val & CALDONE_MASK,
335 1, 20);
336 if (ret)
337 return ret;
338 }
Faiz Abbasfd8be702019-06-13 10:29:51 +0530339 }
Faiz Abbase9aed582019-06-11 00:43:38 +0530340
341 /* Enable pins by setting IO mux to 0 */
Faiz Abbas8cc051e2020-01-16 19:42:19 +0530342 if (plat->flags & IOMUX_PRESENT)
343 regmap_update_bits(plat->base, PHY_CTRL1, IOMUX_ENABLE_MASK, 0);
Faiz Abbase9aed582019-06-11 00:43:38 +0530344
345 /* Set slot type based on SD or eMMC */
346 if (plat->non_removable)
347 ctl_cfg_2 = SLOTTYPE_EMBEDDED;
348
349 regmap_update_bits(plat->base, CTL_CFG_2, SLOTTYPE_MASK, ctl_cfg_2);
350
351 return 0;
352}
353
Faiz Abbase4425cb2020-02-26 13:44:34 +0530354#define MAX_SDCD_DEBOUNCE_TIME 2000
Emanuele Ghidolifde0dff2024-07-02 21:54:29 +0200355static int am654_sdhci_cd_poll(struct mmc *mmc)
Faiz Abbase4425cb2020-02-26 13:44:34 +0530356{
Faiz Abbase4425cb2020-02-26 13:44:34 +0530357 unsigned long start;
358 int val;
359
360 /*
361 * The controller takes about 1 second to debounce the card detect line
362 * and doesn't let us power on until that time is up. Instead of waiting
363 * for 1 second at every stage, poll on the CARD_PRESENT bit upto a
364 * maximum of 2 seconds to be safe..
365 */
366 start = get_timer(0);
367 do {
368 if (get_timer(start) > MAX_SDCD_DEBOUNCE_TIME)
369 return -ENOMEDIUM;
370
Emanuele Ghidolifde0dff2024-07-02 21:54:29 +0200371 val = mmc_getcd(mmc);
Faiz Abbase4425cb2020-02-26 13:44:34 +0530372 } while (!val);
373
Emanuele Ghidolifde0dff2024-07-02 21:54:29 +0200374 return 0;
375}
376
377static int am654_sdhci_deferred_probe(struct sdhci_host *host)
378{
379 struct udevice *dev = host->mmc->dev;
380 struct am654_sdhci_plat *plat = dev_get_plat(dev);
381 int ret;
382
383 if (!(plat->quirks & SDHCI_AM654_QUIRK_FORCE_CDTEST)) {
384 if (am654_sdhci_cd_poll(host->mmc))
385 return -ENOMEDIUM;
386 }
387
Faiz Abbase4425cb2020-02-26 13:44:34 +0530388 am654_sdhci_init(plat);
389
Emanuele Ghidolifde0dff2024-07-02 21:54:29 +0200390 ret = sdhci_probe(dev);
391
392 if (plat->quirks & SDHCI_AM654_QUIRK_FORCE_CDTEST) {
393 u8 hostctrlreg = sdhci_readb(host, SDHCI_HOST_CONTROL);
394
395 hostctrlreg |= SDHCI_CTRL_CD_TEST_INS | SDHCI_CTRL_CD_TEST;
396 sdhci_writeb(host, hostctrlreg, SDHCI_HOST_CONTROL);
397 }
398
399 return ret;
Faiz Abbase4425cb2020-02-26 13:44:34 +0530400}
401
Faiz Abbas36c8c5c2021-02-04 15:10:54 +0530402static void am654_sdhci_write_b(struct sdhci_host *host, u8 val, int reg)
403{
404 if (reg == SDHCI_HOST_CONTROL) {
405 switch (host->mmc->selected_mode) {
406 /*
407 * According to the data manual, HISPD bit
408 * should not be set in these speed modes.
409 */
410 case SD_HS:
411 case MMC_HS:
412 case UHS_SDR12:
413 case UHS_SDR25:
414 val &= ~SDHCI_CTRL_HISPD;
415 default:
416 break;
417 }
418 }
419
420 writeb(val, host->ioaddr + reg);
421}
Tom Rinidec7ea02024-05-20 13:35:03 -0600422#if CONFIG_IS_ENABLED(MMC_SUPPORTS_TUNING)
Judith Mendez81c1d442024-04-18 14:00:56 -0500423#define ITAPDLY_LENGTH 32
424#define ITAPDLY_LAST_INDEX (ITAPDLY_LENGTH - 1)
425
426static u32 am654_sdhci_calculate_itap(struct udevice *dev, struct window
427 *fail_window, u8 num_fails, bool circular_buffer)
428{
429 u8 itap = 0, start_fail = 0, end_fail = 0, pass_length = 0;
430 u8 first_fail_start = 0, last_fail_end = 0;
431 struct window pass_window = {0, 0, 0};
432 int prev_fail_end = -1;
433 u8 i;
434
435 if (!num_fails)
436 return ITAPDLY_LAST_INDEX >> 1;
437
438 if (fail_window->length == ITAPDLY_LENGTH) {
439 dev_err(dev, "No passing ITAPDLY, return 0\n");
440 return 0;
441 }
442
443 first_fail_start = fail_window->start;
444 last_fail_end = fail_window[num_fails - 1].end;
445
446 for (i = 0; i < num_fails; i++) {
447 start_fail = fail_window[i].start;
448 end_fail = fail_window[i].end;
449 pass_length = start_fail - (prev_fail_end + 1);
450
451 if (pass_length > pass_window.length) {
452 pass_window.start = prev_fail_end + 1;
453 pass_window.length = pass_length;
454 }
455 prev_fail_end = end_fail;
456 }
457
458 if (!circular_buffer)
459 pass_length = ITAPDLY_LAST_INDEX - last_fail_end;
460 else
461 pass_length = ITAPDLY_LAST_INDEX - last_fail_end + first_fail_start;
462
463 if (pass_length > pass_window.length) {
464 pass_window.start = last_fail_end + 1;
465 pass_window.length = pass_length;
466 }
467
468 if (!circular_buffer)
469 itap = pass_window.start + (pass_window.length >> 1);
470 else
471 itap = (pass_window.start + (pass_window.length >> 1)) % ITAPDLY_LENGTH;
472
473 return (itap > ITAPDLY_LAST_INDEX) ? ITAPDLY_LAST_INDEX >> 1 : itap;
474}
475
Faiz Abbas7a7e2c72021-02-04 15:10:53 +0530476static int am654_sdhci_execute_tuning(struct mmc *mmc, u8 opcode)
477{
478 struct udevice *dev = mmc->dev;
479 struct am654_sdhci_plat *plat = dev_get_plat(dev);
Judith Mendez81c1d442024-04-18 14:00:56 -0500480 struct window fail_window[ITAPDLY_LENGTH];
Judith Mendez87bcbde2024-04-18 14:00:58 -0500481 int mode = mmc->selected_mode;
Judith Mendez81c1d442024-04-18 14:00:56 -0500482 u8 curr_pass, itap;
483 u8 fail_index = 0;
484 u8 prev_pass = 1;
485
486 memset(fail_window, 0, sizeof(fail_window));
Faiz Abbas7a7e2c72021-02-04 15:10:53 +0530487
488 /* Enable ITAPDLY */
Judith Mendez87bcbde2024-04-18 14:00:58 -0500489 plat->itap_del_ena[mode] = ENABLE;
Faiz Abbas7a7e2c72021-02-04 15:10:53 +0530490
Judith Mendez81c1d442024-04-18 14:00:56 -0500491 for (itap = 0; itap < ITAPDLY_LENGTH; itap++) {
Judith Mendez87bcbde2024-04-18 14:00:58 -0500492 am654_sdhci_write_itapdly(plat, itap, plat->itap_del_ena[mode]);
Faiz Abbas7a7e2c72021-02-04 15:10:53 +0530493
Judith Mendez81c1d442024-04-18 14:00:56 -0500494 curr_pass = !mmc_send_tuning(mmc, opcode);
495
496 if (!curr_pass && prev_pass)
497 fail_window[fail_index].start = itap;
Faiz Abbas7a7e2c72021-02-04 15:10:53 +0530498
Judith Mendez81c1d442024-04-18 14:00:56 -0500499 if (!curr_pass) {
500 fail_window[fail_index].end = itap;
501 fail_window[fail_index].length++;
502 }
503
504 if (curr_pass && !prev_pass)
505 fail_index++;
Faiz Abbas7a7e2c72021-02-04 15:10:53 +0530506
Judith Mendez81c1d442024-04-18 14:00:56 -0500507 prev_pass = curr_pass;
Faiz Abbas7a7e2c72021-02-04 15:10:53 +0530508 }
Judith Mendez81c1d442024-04-18 14:00:56 -0500509
510 if (fail_window[fail_index].length != 0)
511 fail_index++;
512
513 itap = am654_sdhci_calculate_itap(dev, fail_window, fail_index,
514 plat->dll_enable);
515
Judith Mendez5d217152024-04-18 14:01:00 -0500516 /* Save ITAPDLY */
517 plat->itap_del_sel[mode] = itap;
518
Judith Mendez87bcbde2024-04-18 14:00:58 -0500519 am654_sdhci_write_itapdly(plat, itap, plat->itap_del_ena[mode]);
Faiz Abbas7a7e2c72021-02-04 15:10:53 +0530520
521 return 0;
522}
523#endif
Faiz Abbase4425cb2020-02-26 13:44:34 +0530524const struct sdhci_ops am654_sdhci_ops = {
Tom Rinidec7ea02024-05-20 13:35:03 -0600525#if CONFIG_IS_ENABLED(MMC_SUPPORTS_TUNING)
Faiz Abbas7a7e2c72021-02-04 15:10:53 +0530526 .platform_execute_tuning = am654_sdhci_execute_tuning,
527#endif
Faiz Abbase4425cb2020-02-26 13:44:34 +0530528 .deferred_probe = am654_sdhci_deferred_probe,
529 .set_ios_post = &am654_sdhci_set_ios_post,
Faiz Abbas9c10cfe2021-02-04 15:10:55 +0530530 .set_control_reg = sdhci_set_control_reg,
Faiz Abbas36c8c5c2021-02-04 15:10:54 +0530531 .write_b = am654_sdhci_write_b,
Faiz Abbase4425cb2020-02-26 13:44:34 +0530532};
533
534const struct am654_driver_data am654_drv_data = {
535 .ops = &am654_sdhci_ops,
Faiz Abbas2c2fc962021-02-04 15:10:50 +0530536 .flags = DLL_PRESENT | IOMUX_PRESENT | FREQSEL_2_BIT | STRBSEL_4_BIT,
537};
538
539const struct am654_driver_data am654_sr1_drv_data = {
540 .ops = &am654_sdhci_ops,
Faiz Abbas947e8f32021-02-04 15:10:49 +0530541 .flags = IOMUX_PRESENT | FREQSEL_2_BIT | DLL_PRESENT | DLL_CALIB |
542 STRBSEL_4_BIT,
Faiz Abbase4425cb2020-02-26 13:44:34 +0530543};
544
545const struct am654_driver_data j721e_8bit_drv_data = {
546 .ops = &am654_sdhci_ops,
Faiz Abbas947e8f32021-02-04 15:10:49 +0530547 .flags = DLL_PRESENT | DLL_CALIB,
Faiz Abbase4425cb2020-02-26 13:44:34 +0530548};
549
550static int j721e_4bit_sdhci_set_ios_post(struct sdhci_host *host)
551{
552 struct udevice *dev = host->mmc->dev;
Simon Glassfa20e932020-12-03 16:55:20 -0700553 struct am654_sdhci_plat *plat = dev_get_plat(dev);
Nitin Yadav83db1f92024-04-18 14:00:57 -0500554 int mode = host->mmc->selected_mode;
555 u32 otap_del_sel;
Judith Mendez87bcbde2024-04-18 14:00:58 -0500556 u32 itap_del_ena;
Nitin Yadav83db1f92024-04-18 14:00:57 -0500557 u32 itap_del_sel;
558 u32 mask, val;
Faiz Abbase4425cb2020-02-26 13:44:34 +0530559
Nitin Yadav83db1f92024-04-18 14:00:57 -0500560 otap_del_sel = plat->otap_del_sel[mode];
561
Faiz Abbase4425cb2020-02-26 13:44:34 +0530562 mask = OTAPDLYENA_MASK | OTAPDLYSEL_MASK;
Nitin Yadav83db1f92024-04-18 14:00:57 -0500563 val = (1 << OTAPDLYENA_SHIFT) |
564 (otap_del_sel << OTAPDLYSEL_SHIFT);
565
Judith Mendez87bcbde2024-04-18 14:00:58 -0500566 itap_del_ena = plat->itap_del_ena[mode];
Nitin Yadav83db1f92024-04-18 14:00:57 -0500567 itap_del_sel = plat->itap_del_sel[mode];
568
569 mask |= ITAPDLYENA_MASK | ITAPDLYSEL_MASK;
Judith Mendez87bcbde2024-04-18 14:00:58 -0500570 val |= (itap_del_ena << ITAPDLYENA_SHIFT) |
Nitin Yadav83db1f92024-04-18 14:00:57 -0500571 (itap_del_sel << ITAPDLYSEL_SHIFT);
572
573 regmap_update_bits(plat->base, PHY_CTRL4, ITAPCHGWIN_MASK,
574 1 << ITAPCHGWIN_SHIFT);
Faiz Abbase4425cb2020-02-26 13:44:34 +0530575 regmap_update_bits(plat->base, PHY_CTRL4, mask, val);
Nitin Yadav83db1f92024-04-18 14:00:57 -0500576 regmap_update_bits(plat->base, PHY_CTRL4, ITAPCHGWIN_MASK, 0);
Faiz Abbase4425cb2020-02-26 13:44:34 +0530577
Faiz Abbasc73f04e2021-02-04 15:10:52 +0530578 regmap_update_bits(plat->base, PHY_CTRL5, CLKBUFSEL_MASK,
579 plat->clkbuf_sel);
580
Faiz Abbase4425cb2020-02-26 13:44:34 +0530581 return 0;
582}
583
584const struct sdhci_ops j721e_4bit_sdhci_ops = {
Tom Rinidec7ea02024-05-20 13:35:03 -0600585#if CONFIG_IS_ENABLED(MMC_SUPPORTS_TUNING)
Faiz Abbas7a7e2c72021-02-04 15:10:53 +0530586 .platform_execute_tuning = am654_sdhci_execute_tuning,
587#endif
Faiz Abbase4425cb2020-02-26 13:44:34 +0530588 .deferred_probe = am654_sdhci_deferred_probe,
589 .set_ios_post = &j721e_4bit_sdhci_set_ios_post,
Faiz Abbas9c10cfe2021-02-04 15:10:55 +0530590 .set_control_reg = sdhci_set_control_reg,
Faiz Abbas36c8c5c2021-02-04 15:10:54 +0530591 .write_b = am654_sdhci_write_b,
Faiz Abbase4425cb2020-02-26 13:44:34 +0530592};
593
594const struct am654_driver_data j721e_4bit_drv_data = {
595 .ops = &j721e_4bit_sdhci_ops,
596 .flags = IOMUX_PRESENT,
597};
598
Dave Gerlach057d6af2021-04-23 11:27:40 -0500599static const struct am654_driver_data sdhci_am64_8bit_drvdata = {
600 .ops = &am654_sdhci_ops,
601 .flags = DLL_PRESENT | DLL_CALIB,
602};
603
604static const struct am654_driver_data sdhci_am64_4bit_drvdata = {
605 .ops = &j721e_4bit_sdhci_ops,
606 .flags = IOMUX_PRESENT,
607};
608
Faiz Abbas2c2fc962021-02-04 15:10:50 +0530609const struct soc_attr am654_sdhci_soc_attr[] = {
610 { .family = "AM65X", .revision = "SR1.0", .data = &am654_sr1_drv_data},
611 {/* sentinel */}
612};
613
Faiz Abbasc6eb9e72020-02-26 13:44:33 +0530614static int sdhci_am654_get_otap_delay(struct udevice *dev,
615 struct mmc_config *cfg)
616{
Simon Glassfa20e932020-12-03 16:55:20 -0700617 struct am654_sdhci_plat *plat = dev_get_plat(dev);
Faiz Abbasc6eb9e72020-02-26 13:44:33 +0530618 int ret;
619 int i;
620
621 /* ti,otap-del-sel-legacy is mandatory */
622 ret = dev_read_u32(dev, "ti,otap-del-sel-legacy",
623 &plat->otap_del_sel[0]);
624 if (ret)
625 return ret;
626 /*
627 * Remove the corresponding capability if an otap-del-sel
628 * value is not found
629 */
Nitin Yadav83db1f92024-04-18 14:00:57 -0500630 for (i = MMC_LEGACY; i <= MMC_HS_400; i++) {
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530631 ret = dev_read_u32(dev, td[i].otap_binding,
632 &plat->otap_del_sel[i]);
Faiz Abbasc6eb9e72020-02-26 13:44:33 +0530633 if (ret) {
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530634 dev_dbg(dev, "Couldn't find %s\n", td[i].otap_binding);
Faiz Abbasc6eb9e72020-02-26 13:44:33 +0530635 /*
636 * Remove the corresponding capability
637 * if an otap-del-sel value is not found
638 */
639 cfg->host_caps &= ~td[i].capability;
640 }
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530641
Judith Mendez87bcbde2024-04-18 14:00:58 -0500642 if (td[i].itap_binding) {
643 ret = dev_read_u32(dev, td[i].itap_binding,
644 &plat->itap_del_sel[i]);
645
646 if (!ret)
647 plat->itap_del_ena[i] = ENABLE;
648 }
Faiz Abbasc6eb9e72020-02-26 13:44:33 +0530649 }
650
651 return 0;
652}
653
Faiz Abbasd8fb3092019-06-11 00:43:31 +0530654static int am654_sdhci_probe(struct udevice *dev)
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530655{
Faiz Abbas8cc051e2020-01-16 19:42:19 +0530656 struct am654_driver_data *drv_data =
657 (struct am654_driver_data *)dev_get_driver_data(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700658 struct am654_sdhci_plat *plat = dev_get_plat(dev);
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530659 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
660 struct sdhci_host *host = dev_get_priv(dev);
Faiz Abbase9aed582019-06-11 00:43:38 +0530661 struct mmc_config *cfg = &plat->cfg;
Faiz Abbas2c2fc962021-02-04 15:10:50 +0530662 const struct soc_attr *soc;
663 const struct am654_driver_data *soc_drv_data;
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530664 struct clk clk;
665 unsigned long clock;
666 int ret;
667
Faiz Abbasdc2bcc22020-01-16 19:42:18 +0530668 ret = clk_get_by_name(dev, "clk_xin", &clk);
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530669 if (ret) {
670 dev_err(dev, "failed to get clock\n");
671 return ret;
672 }
673
674 clock = clk_get_rate(&clk);
675 if (IS_ERR_VALUE(clock)) {
676 dev_err(dev, "failed to get rate\n");
677 return clock;
678 }
679
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530680 host->max_clk = clock;
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530681 host->mmc = &plat->mmc;
Faiz Abbase9aed582019-06-11 00:43:38 +0530682 host->mmc->dev = dev;
Faiz Abbas36c8c5c2021-02-04 15:10:54 +0530683 host->ops = drv_data->ops;
Faiz Abbase9aed582019-06-11 00:43:38 +0530684 ret = sdhci_setup_cfg(cfg, host, cfg->f_max,
685 AM654_SDHCI_MIN_FREQ);
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530686 if (ret)
687 return ret;
Faiz Abbas8cc051e2020-01-16 19:42:19 +0530688
Faiz Abbasc6eb9e72020-02-26 13:44:33 +0530689 ret = sdhci_am654_get_otap_delay(dev, cfg);
690 if (ret)
691 return ret;
692
Faiz Abbas2c2fc962021-02-04 15:10:50 +0530693 /* Update ops based on SoC revision */
694 soc = soc_device_match(am654_sdhci_soc_attr);
695 if (soc && soc->data) {
696 soc_drv_data = soc->data;
697 host->ops = soc_drv_data->ops;
698 }
699
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530700 host->mmc->priv = host;
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530701 upriv->mmc = host->mmc;
702
Faiz Abbase9aed582019-06-11 00:43:38 +0530703 regmap_init_mem_index(dev_ofnode(dev), &plat->base, 1);
704
Emanuele Ghidolifde0dff2024-07-02 21:54:29 +0200705 if (plat->quirks & SDHCI_AM654_QUIRK_FORCE_CDTEST)
706 am654_sdhci_deferred_probe(host);
707
Faiz Abbase4425cb2020-02-26 13:44:34 +0530708 return 0;
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530709}
710
Simon Glassaad29ae2020-12-03 16:55:21 -0700711static int am654_sdhci_of_to_plat(struct udevice *dev)
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530712{
Simon Glassfa20e932020-12-03 16:55:20 -0700713 struct am654_sdhci_plat *plat = dev_get_plat(dev);
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530714 struct sdhci_host *host = dev_get_priv(dev);
Faiz Abbase9aed582019-06-11 00:43:38 +0530715 struct mmc_config *cfg = &plat->cfg;
716 u32 drv_strength;
717 int ret;
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530718
719 host->name = dev->name;
Johan Jonker8d5d8e02023-03-13 01:32:04 +0100720 host->ioaddr = dev_read_addr_ptr(dev);
Faiz Abbase9aed582019-06-11 00:43:38 +0530721 plat->non_removable = dev_read_bool(dev, "non-removable");
722
Faiz Abbasfd8be702019-06-13 10:29:51 +0530723 if (plat->flags & DLL_PRESENT) {
724 ret = dev_read_u32(dev, "ti,trm-icp", &plat->trm_icp);
725 if (ret)
726 return ret;
727
728 ret = dev_read_u32(dev, "ti,driver-strength-ohm",
729 &drv_strength);
730 if (ret)
731 return ret;
Faiz Abbase9aed582019-06-11 00:43:38 +0530732
Faiz Abbasfd8be702019-06-13 10:29:51 +0530733 switch (drv_strength) {
734 case 50:
735 plat->drv_strength = DRIVER_STRENGTH_50_OHM;
736 break;
737 case 33:
738 plat->drv_strength = DRIVER_STRENGTH_33_OHM;
739 break;
740 case 66:
741 plat->drv_strength = DRIVER_STRENGTH_66_OHM;
742 break;
743 case 100:
744 plat->drv_strength = DRIVER_STRENGTH_100_OHM;
745 break;
746 case 40:
747 plat->drv_strength = DRIVER_STRENGTH_40_OHM;
748 break;
749 default:
750 dev_err(dev, "Invalid driver strength\n");
751 return -EINVAL;
752 }
Faiz Abbase9aed582019-06-11 00:43:38 +0530753 }
754
Aswath Govindraju4509fb62021-05-25 15:08:23 +0530755 dev_read_u32(dev, "ti,strobe-sel", &plat->strb_sel);
Faiz Abbasc73f04e2021-02-04 15:10:52 +0530756 dev_read_u32(dev, "ti,clkbuf-sel", &plat->clkbuf_sel);
Emanuele Ghidolifde0dff2024-07-02 21:54:29 +0200757 if (dev_read_bool(dev, "ti,fails-without-test-cd"))
758 plat->quirks |= SDHCI_AM654_QUIRK_FORCE_CDTEST;
Faiz Abbasc73f04e2021-02-04 15:10:52 +0530759
Faiz Abbase9aed582019-06-11 00:43:38 +0530760 ret = mmc_of_parse(dev, cfg);
761 if (ret)
762 return ret;
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530763
764 return 0;
765}
766
Faiz Abbasd8fb3092019-06-11 00:43:31 +0530767static int am654_sdhci_bind(struct udevice *dev)
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530768{
Faiz Abbas8cc051e2020-01-16 19:42:19 +0530769 struct am654_driver_data *drv_data =
770 (struct am654_driver_data *)dev_get_driver_data(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700771 struct am654_sdhci_plat *plat = dev_get_plat(dev);
Faiz Abbas2c2fc962021-02-04 15:10:50 +0530772 const struct soc_attr *soc;
773 const struct am654_driver_data *soc_drv_data;
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530774
Faiz Abbas8cc051e2020-01-16 19:42:19 +0530775 plat->flags = drv_data->flags;
776
Faiz Abbas2c2fc962021-02-04 15:10:50 +0530777 /* Update flags based on SoC revision */
778 soc = soc_device_match(am654_sdhci_soc_attr);
779 if (soc && soc->data) {
780 soc_drv_data = soc->data;
781 plat->flags = soc_drv_data->flags;
782 }
783
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530784 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
785}
786
Faiz Abbasd8fb3092019-06-11 00:43:31 +0530787static const struct udevice_id am654_sdhci_ids[] = {
Faiz Abbasfd8be702019-06-13 10:29:51 +0530788 {
789 .compatible = "ti,am654-sdhci-5.1",
Faiz Abbas8cc051e2020-01-16 19:42:19 +0530790 .data = (ulong)&am654_drv_data,
Faiz Abbasfd8be702019-06-13 10:29:51 +0530791 },
792 {
793 .compatible = "ti,j721e-sdhci-8bit",
Faiz Abbas8cc051e2020-01-16 19:42:19 +0530794 .data = (ulong)&j721e_8bit_drv_data,
Faiz Abbasfd8be702019-06-13 10:29:51 +0530795 },
796 {
797 .compatible = "ti,j721e-sdhci-4bit",
Faiz Abbas8cc051e2020-01-16 19:42:19 +0530798 .data = (ulong)&j721e_4bit_drv_data,
Faiz Abbasfd8be702019-06-13 10:29:51 +0530799 },
Dave Gerlach057d6af2021-04-23 11:27:40 -0500800 {
801 .compatible = "ti,am64-sdhci-8bit",
802 .data = (ulong)&sdhci_am64_8bit_drvdata,
803 },
804 {
805 .compatible = "ti,am64-sdhci-4bit",
806 .data = (ulong)&sdhci_am64_4bit_drvdata,
807 },
Aswath Govindraju71b9a7b2022-05-25 13:38:39 +0530808 {
809 .compatible = "ti,am62-sdhci",
810 .data = (ulong)&sdhci_am64_4bit_drvdata,
811 },
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530812 { }
813};
814
Faiz Abbasd8fb3092019-06-11 00:43:31 +0530815U_BOOT_DRIVER(am654_sdhci_drv) = {
816 .name = "am654_sdhci",
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530817 .id = UCLASS_MMC,
Faiz Abbasd8fb3092019-06-11 00:43:31 +0530818 .of_match = am654_sdhci_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700819 .of_to_plat = am654_sdhci_of_to_plat,
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530820 .ops = &sdhci_ops,
Faiz Abbasd8fb3092019-06-11 00:43:31 +0530821 .bind = am654_sdhci_bind,
822 .probe = am654_sdhci_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700823 .priv_auto = sizeof(struct sdhci_host),
Simon Glass71fa5b42020-12-03 16:55:18 -0700824 .plat_auto = sizeof(struct am654_sdhci_plat),
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530825};