blob: 48fac7a11b483aa61736b4db4f52cba63b31b54d [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)
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530108};
109
Faiz Abbasc6eb9e72020-02-26 13:44:33 +0530110struct timing_data {
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530111 const char *otap_binding;
112 const char *itap_binding;
Faiz Abbasc6eb9e72020-02-26 13:44:33 +0530113 u32 capability;
114};
115
Judith Mendez81c1d442024-04-18 14:00:56 -0500116struct window {
117 u8 start;
118 u8 end;
119 u8 length;
120};
121
Faiz Abbasc6eb9e72020-02-26 13:44:33 +0530122static const struct timing_data td[] = {
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530123 [MMC_LEGACY] = {"ti,otap-del-sel-legacy",
124 "ti,itap-del-sel-legacy",
125 0},
126 [MMC_HS] = {"ti,otap-del-sel-mmc-hs",
127 "ti,itap-del-sel-mms-hs",
128 MMC_CAP(MMC_HS)},
129 [SD_HS] = {"ti,otap-del-sel-sd-hs",
130 "ti,itap-del-sel-sd-hs",
131 MMC_CAP(SD_HS)},
132 [UHS_SDR12] = {"ti,otap-del-sel-sdr12",
133 "ti,itap-del-sel-sdr12",
134 MMC_CAP(UHS_SDR12)},
135 [UHS_SDR25] = {"ti,otap-del-sel-sdr25",
136 "ti,itap-del-sel-sdr25",
137 MMC_CAP(UHS_SDR25)},
138 [UHS_SDR50] = {"ti,otap-del-sel-sdr50",
139 NULL,
140 MMC_CAP(UHS_SDR50)},
141 [UHS_SDR104] = {"ti,otap-del-sel-sdr104",
142 NULL,
143 MMC_CAP(UHS_SDR104)},
144 [UHS_DDR50] = {"ti,otap-del-sel-ddr50",
145 NULL,
146 MMC_CAP(UHS_DDR50)},
147 [MMC_DDR_52] = {"ti,otap-del-sel-ddr52",
148 "ti,itap-del-sel-ddr52",
149 MMC_CAP(MMC_DDR_52)},
150 [MMC_HS_200] = {"ti,otap-del-sel-hs200",
151 NULL,
152 MMC_CAP(MMC_HS_200)},
153 [MMC_HS_400] = {"ti,otap-del-sel-hs400",
154 NULL,
155 MMC_CAP(MMC_HS_400)},
Faiz Abbasc6eb9e72020-02-26 13:44:33 +0530156};
157
Faiz Abbas8cc051e2020-01-16 19:42:19 +0530158struct am654_driver_data {
159 const struct sdhci_ops *ops;
160 u32 flags;
161};
162
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530163static int am654_sdhci_setup_dll(struct am654_sdhci_plat *plat,
164 unsigned int speed)
165{
166 int sel50, sel100, freqsel;
167 u32 mask, val;
168 int ret;
169
170 /* Disable delay chain mode */
171 regmap_update_bits(plat->base, PHY_CTRL5,
172 SELDLYTXCLK_MASK | SELDLYRXCLK_MASK, 0);
173
174 if (plat->flags & FREQSEL_2_BIT) {
175 switch (speed) {
176 case 200000000:
177 sel50 = 0;
178 sel100 = 0;
179 break;
180 case 100000000:
181 sel50 = 0;
182 sel100 = 1;
183 break;
184 default:
185 sel50 = 1;
186 sel100 = 0;
187 }
188
189 /* Configure PHY DLL frequency */
190 mask = SEL50_MASK | SEL100_MASK;
191 val = (sel50 << SEL50_SHIFT) | (sel100 << SEL100_SHIFT);
192 regmap_update_bits(plat->base, PHY_CTRL5, mask, val);
193 } else {
194 switch (speed) {
195 case 200000000:
196 freqsel = 0x0;
197 break;
198 default:
199 freqsel = 0x4;
200 }
201 regmap_update_bits(plat->base, PHY_CTRL5, FREQSEL_MASK,
202 freqsel << FREQSEL_SHIFT);
203 }
204
205 /* Configure DLL TRIM */
206 mask = DLL_TRIM_ICP_MASK;
207 val = plat->trm_icp << DLL_TRIM_ICP_SHIFT;
208
209 /* Configure DLL driver strength */
210 mask |= DR_TY_MASK;
211 val |= plat->drv_strength << DR_TY_SHIFT;
212 regmap_update_bits(plat->base, PHY_CTRL1, mask, val);
213
214 /* Enable DLL */
215 regmap_update_bits(plat->base, PHY_CTRL1, ENDLL_MASK,
216 0x1 << ENDLL_SHIFT);
217 /*
218 * Poll for DLL ready. Use a one second timeout.
219 * Works in all experiments done so far
220 */
221 ret = regmap_read_poll_timeout(plat->base, PHY_STAT1, val,
222 val & DLLRDY_MASK, 1000, 1000000);
223
224 return ret;
225}
226
227static void am654_sdhci_write_itapdly(struct am654_sdhci_plat *plat,
Judith Mendez87bcbde2024-04-18 14:00:58 -0500228 u32 itapdly, u32 enable)
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530229{
Judith Mendez87bcbde2024-04-18 14:00:58 -0500230 regmap_update_bits(plat->base, PHY_CTRL4, ITAPDLYENA_MASK,
231 enable << ITAPDLYENA_SHIFT);
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530232 /* Set ITAPCHGWIN before writing to ITAPDLY */
233 regmap_update_bits(plat->base, PHY_CTRL4, ITAPCHGWIN_MASK,
234 1 << ITAPCHGWIN_SHIFT);
235 regmap_update_bits(plat->base, PHY_CTRL4, ITAPDLYSEL_MASK,
236 itapdly << ITAPDLYSEL_SHIFT);
237 regmap_update_bits(plat->base, PHY_CTRL4, ITAPCHGWIN_MASK, 0);
238}
239
240static void am654_sdhci_setup_delay_chain(struct am654_sdhci_plat *plat,
241 int mode)
242{
243 u32 mask, val;
244
245 val = 1 << SELDLYTXCLK_SHIFT | 1 << SELDLYRXCLK_SHIFT;
246 mask = SELDLYTXCLK_MASK | SELDLYRXCLK_MASK;
247 regmap_update_bits(plat->base, PHY_CTRL5, mask, val);
248
Judith Mendez87bcbde2024-04-18 14:00:58 -0500249 am654_sdhci_write_itapdly(plat, plat->itap_del_sel[mode],
250 plat->itap_del_ena[mode]);
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530251}
252
Faiz Abbase9aed582019-06-11 00:43:38 +0530253static int am654_sdhci_set_ios_post(struct sdhci_host *host)
254{
255 struct udevice *dev = host->mmc->dev;
Simon Glassfa20e932020-12-03 16:55:20 -0700256 struct am654_sdhci_plat *plat = dev_get_plat(dev);
Faiz Abbase9aed582019-06-11 00:43:38 +0530257 unsigned int speed = host->mmc->clock;
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530258 int mode = host->mmc->selected_mode;
Faiz Abbasc6eb9e72020-02-26 13:44:33 +0530259 u32 otap_del_sel;
Faiz Abbase9aed582019-06-11 00:43:38 +0530260 u32 mask, val;
261 int ret;
262
263 /* Reset SD Clock Enable */
264 val = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
265 val &= ~SDHCI_CLOCK_CARD_EN;
266 sdhci_writew(host, val, SDHCI_CLOCK_CONTROL);
267
Faiz Abbas2c45a2c2021-02-04 15:10:47 +0530268 regmap_update_bits(plat->base, PHY_CTRL1, ENDLL_MASK, 0);
Faiz Abbase9aed582019-06-11 00:43:38 +0530269
270 /* restart clock */
271 sdhci_set_clock(host->mmc, speed);
272
273 /* switch phy back on */
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530274 otap_del_sel = plat->otap_del_sel[mode];
275 mask = OTAPDLYENA_MASK | OTAPDLYSEL_MASK;
276 val = (1 << OTAPDLYENA_SHIFT) |
277 (otap_del_sel << OTAPDLYSEL_SHIFT);
Faiz Abbas8cc051e2020-01-16 19:42:19 +0530278
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530279 /* Write to STRBSEL for HS400 speed mode */
280 if (host->mmc->selected_mode == MMC_HS_400) {
281 if (plat->flags & STRBSEL_4_BIT)
282 mask |= STRBSEL_4BIT_MASK;
283 else
284 mask |= STRBSEL_8BIT_MASK;
Faiz Abbas8cc051e2020-01-16 19:42:19 +0530285
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530286 val |= plat->strb_sel << STRBSEL_SHIFT;
287 }
Faiz Abbas947e8f32021-02-04 15:10:49 +0530288
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530289 regmap_update_bits(plat->base, PHY_CTRL4, mask, val);
Faiz Abbas947e8f32021-02-04 15:10:49 +0530290
Judith Mendezc9eb1dc2024-04-18 14:00:59 -0500291 if ((mode > UHS_SDR25 || mode == MMC_DDR_52) && speed >= CLOCK_TOO_SLOW_HZ) {
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530292 ret = am654_sdhci_setup_dll(plat, speed);
Faiz Abbase9aed582019-06-11 00:43:38 +0530293 if (ret)
294 return ret;
Judith Mendez81c1d442024-04-18 14:00:56 -0500295
296 plat->dll_enable = true;
Judith Mendez5d217152024-04-18 14:01:00 -0500297 if (mode == MMC_HS_400) {
298 plat->itap_del_ena[mode] = ENABLE;
299 plat->itap_del_sel[mode] = plat->itap_del_sel[mode - 1];
300 }
301
Judith Mendezc9eb1dc2024-04-18 14:00:59 -0500302 am654_sdhci_write_itapdly(plat, plat->itap_del_sel[mode],
303 plat->itap_del_ena[mode]);
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530304 } else {
305 am654_sdhci_setup_delay_chain(plat, mode);
Judith Mendez81c1d442024-04-18 14:00:56 -0500306 plat->dll_enable = false;
Faiz Abbase9aed582019-06-11 00:43:38 +0530307 }
308
Faiz Abbasc73f04e2021-02-04 15:10:52 +0530309 regmap_update_bits(plat->base, PHY_CTRL5, CLKBUFSEL_MASK,
310 plat->clkbuf_sel);
311
Faiz Abbase9aed582019-06-11 00:43:38 +0530312 return 0;
313}
314
Faiz Abbase9aed582019-06-11 00:43:38 +0530315int am654_sdhci_init(struct am654_sdhci_plat *plat)
316{
317 u32 ctl_cfg_2 = 0;
318 u32 mask, val;
319 int ret;
320
321 /* Reset OTAP to default value */
322 mask = OTAPDLYENA_MASK | OTAPDLYSEL_MASK;
323 regmap_update_bits(plat->base, PHY_CTRL4, mask, 0x0);
324
Faiz Abbas947e8f32021-02-04 15:10:49 +0530325 if (plat->flags & DLL_CALIB) {
Faiz Abbasfd8be702019-06-13 10:29:51 +0530326 regmap_read(plat->base, PHY_STAT1, &val);
327 if (~val & CALDONE_MASK) {
328 /* Calibrate IO lines */
329 regmap_update_bits(plat->base, PHY_CTRL1, PDB_MASK,
330 PDB_MASK);
331 ret = regmap_read_poll_timeout(plat->base, PHY_STAT1,
332 val, val & CALDONE_MASK,
333 1, 20);
334 if (ret)
335 return ret;
336 }
Faiz Abbasfd8be702019-06-13 10:29:51 +0530337 }
Faiz Abbase9aed582019-06-11 00:43:38 +0530338
339 /* Enable pins by setting IO mux to 0 */
Faiz Abbas8cc051e2020-01-16 19:42:19 +0530340 if (plat->flags & IOMUX_PRESENT)
341 regmap_update_bits(plat->base, PHY_CTRL1, IOMUX_ENABLE_MASK, 0);
Faiz Abbase9aed582019-06-11 00:43:38 +0530342
343 /* Set slot type based on SD or eMMC */
344 if (plat->non_removable)
345 ctl_cfg_2 = SLOTTYPE_EMBEDDED;
346
347 regmap_update_bits(plat->base, CTL_CFG_2, SLOTTYPE_MASK, ctl_cfg_2);
348
349 return 0;
350}
351
Faiz Abbase4425cb2020-02-26 13:44:34 +0530352#define MAX_SDCD_DEBOUNCE_TIME 2000
353static int am654_sdhci_deferred_probe(struct sdhci_host *host)
354{
355 struct udevice *dev = host->mmc->dev;
Simon Glassfa20e932020-12-03 16:55:20 -0700356 struct am654_sdhci_plat *plat = dev_get_plat(dev);
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
371 val = mmc_getcd(host->mmc);
372 } while (!val);
373
374 am654_sdhci_init(plat);
375
376 return sdhci_probe(dev);
377}
378
Faiz Abbas36c8c5c2021-02-04 15:10:54 +0530379static void am654_sdhci_write_b(struct sdhci_host *host, u8 val, int reg)
380{
381 if (reg == SDHCI_HOST_CONTROL) {
382 switch (host->mmc->selected_mode) {
383 /*
384 * According to the data manual, HISPD bit
385 * should not be set in these speed modes.
386 */
387 case SD_HS:
388 case MMC_HS:
389 case UHS_SDR12:
390 case UHS_SDR25:
391 val &= ~SDHCI_CTRL_HISPD;
392 default:
393 break;
394 }
395 }
396
397 writeb(val, host->ioaddr + reg);
398}
Tom Rinidec7ea02024-05-20 13:35:03 -0600399#if CONFIG_IS_ENABLED(MMC_SUPPORTS_TUNING)
Judith Mendez81c1d442024-04-18 14:00:56 -0500400#define ITAPDLY_LENGTH 32
401#define ITAPDLY_LAST_INDEX (ITAPDLY_LENGTH - 1)
402
403static u32 am654_sdhci_calculate_itap(struct udevice *dev, struct window
404 *fail_window, u8 num_fails, bool circular_buffer)
405{
406 u8 itap = 0, start_fail = 0, end_fail = 0, pass_length = 0;
407 u8 first_fail_start = 0, last_fail_end = 0;
408 struct window pass_window = {0, 0, 0};
409 int prev_fail_end = -1;
410 u8 i;
411
412 if (!num_fails)
413 return ITAPDLY_LAST_INDEX >> 1;
414
415 if (fail_window->length == ITAPDLY_LENGTH) {
416 dev_err(dev, "No passing ITAPDLY, return 0\n");
417 return 0;
418 }
419
420 first_fail_start = fail_window->start;
421 last_fail_end = fail_window[num_fails - 1].end;
422
423 for (i = 0; i < num_fails; i++) {
424 start_fail = fail_window[i].start;
425 end_fail = fail_window[i].end;
426 pass_length = start_fail - (prev_fail_end + 1);
427
428 if (pass_length > pass_window.length) {
429 pass_window.start = prev_fail_end + 1;
430 pass_window.length = pass_length;
431 }
432 prev_fail_end = end_fail;
433 }
434
435 if (!circular_buffer)
436 pass_length = ITAPDLY_LAST_INDEX - last_fail_end;
437 else
438 pass_length = ITAPDLY_LAST_INDEX - last_fail_end + first_fail_start;
439
440 if (pass_length > pass_window.length) {
441 pass_window.start = last_fail_end + 1;
442 pass_window.length = pass_length;
443 }
444
445 if (!circular_buffer)
446 itap = pass_window.start + (pass_window.length >> 1);
447 else
448 itap = (pass_window.start + (pass_window.length >> 1)) % ITAPDLY_LENGTH;
449
450 return (itap > ITAPDLY_LAST_INDEX) ? ITAPDLY_LAST_INDEX >> 1 : itap;
451}
452
Faiz Abbas7a7e2c72021-02-04 15:10:53 +0530453static int am654_sdhci_execute_tuning(struct mmc *mmc, u8 opcode)
454{
455 struct udevice *dev = mmc->dev;
456 struct am654_sdhci_plat *plat = dev_get_plat(dev);
Judith Mendez81c1d442024-04-18 14:00:56 -0500457 struct window fail_window[ITAPDLY_LENGTH];
Judith Mendez87bcbde2024-04-18 14:00:58 -0500458 int mode = mmc->selected_mode;
Judith Mendez81c1d442024-04-18 14:00:56 -0500459 u8 curr_pass, itap;
460 u8 fail_index = 0;
461 u8 prev_pass = 1;
462
463 memset(fail_window, 0, sizeof(fail_window));
Faiz Abbas7a7e2c72021-02-04 15:10:53 +0530464
465 /* Enable ITAPDLY */
Judith Mendez87bcbde2024-04-18 14:00:58 -0500466 plat->itap_del_ena[mode] = ENABLE;
Faiz Abbas7a7e2c72021-02-04 15:10:53 +0530467
Judith Mendez81c1d442024-04-18 14:00:56 -0500468 for (itap = 0; itap < ITAPDLY_LENGTH; itap++) {
Judith Mendez87bcbde2024-04-18 14:00:58 -0500469 am654_sdhci_write_itapdly(plat, itap, plat->itap_del_ena[mode]);
Faiz Abbas7a7e2c72021-02-04 15:10:53 +0530470
Judith Mendez81c1d442024-04-18 14:00:56 -0500471 curr_pass = !mmc_send_tuning(mmc, opcode);
472
473 if (!curr_pass && prev_pass)
474 fail_window[fail_index].start = itap;
Faiz Abbas7a7e2c72021-02-04 15:10:53 +0530475
Judith Mendez81c1d442024-04-18 14:00:56 -0500476 if (!curr_pass) {
477 fail_window[fail_index].end = itap;
478 fail_window[fail_index].length++;
479 }
480
481 if (curr_pass && !prev_pass)
482 fail_index++;
Faiz Abbas7a7e2c72021-02-04 15:10:53 +0530483
Judith Mendez81c1d442024-04-18 14:00:56 -0500484 prev_pass = curr_pass;
Faiz Abbas7a7e2c72021-02-04 15:10:53 +0530485 }
Judith Mendez81c1d442024-04-18 14:00:56 -0500486
487 if (fail_window[fail_index].length != 0)
488 fail_index++;
489
490 itap = am654_sdhci_calculate_itap(dev, fail_window, fail_index,
491 plat->dll_enable);
492
Judith Mendez5d217152024-04-18 14:01:00 -0500493 /* Save ITAPDLY */
494 plat->itap_del_sel[mode] = itap;
495
Judith Mendez87bcbde2024-04-18 14:00:58 -0500496 am654_sdhci_write_itapdly(plat, itap, plat->itap_del_ena[mode]);
Faiz Abbas7a7e2c72021-02-04 15:10:53 +0530497
498 return 0;
499}
500#endif
Faiz Abbase4425cb2020-02-26 13:44:34 +0530501const struct sdhci_ops am654_sdhci_ops = {
Tom Rinidec7ea02024-05-20 13:35:03 -0600502#if CONFIG_IS_ENABLED(MMC_SUPPORTS_TUNING)
Faiz Abbas7a7e2c72021-02-04 15:10:53 +0530503 .platform_execute_tuning = am654_sdhci_execute_tuning,
504#endif
Faiz Abbase4425cb2020-02-26 13:44:34 +0530505 .deferred_probe = am654_sdhci_deferred_probe,
506 .set_ios_post = &am654_sdhci_set_ios_post,
Faiz Abbas9c10cfe2021-02-04 15:10:55 +0530507 .set_control_reg = sdhci_set_control_reg,
Faiz Abbas36c8c5c2021-02-04 15:10:54 +0530508 .write_b = am654_sdhci_write_b,
Faiz Abbase4425cb2020-02-26 13:44:34 +0530509};
510
511const struct am654_driver_data am654_drv_data = {
512 .ops = &am654_sdhci_ops,
Faiz Abbas2c2fc962021-02-04 15:10:50 +0530513 .flags = DLL_PRESENT | IOMUX_PRESENT | FREQSEL_2_BIT | STRBSEL_4_BIT,
514};
515
516const struct am654_driver_data am654_sr1_drv_data = {
517 .ops = &am654_sdhci_ops,
Faiz Abbas947e8f32021-02-04 15:10:49 +0530518 .flags = IOMUX_PRESENT | FREQSEL_2_BIT | DLL_PRESENT | DLL_CALIB |
519 STRBSEL_4_BIT,
Faiz Abbase4425cb2020-02-26 13:44:34 +0530520};
521
522const struct am654_driver_data j721e_8bit_drv_data = {
523 .ops = &am654_sdhci_ops,
Faiz Abbas947e8f32021-02-04 15:10:49 +0530524 .flags = DLL_PRESENT | DLL_CALIB,
Faiz Abbase4425cb2020-02-26 13:44:34 +0530525};
526
527static int j721e_4bit_sdhci_set_ios_post(struct sdhci_host *host)
528{
529 struct udevice *dev = host->mmc->dev;
Simon Glassfa20e932020-12-03 16:55:20 -0700530 struct am654_sdhci_plat *plat = dev_get_plat(dev);
Nitin Yadav83db1f92024-04-18 14:00:57 -0500531 int mode = host->mmc->selected_mode;
532 u32 otap_del_sel;
Judith Mendez87bcbde2024-04-18 14:00:58 -0500533 u32 itap_del_ena;
Nitin Yadav83db1f92024-04-18 14:00:57 -0500534 u32 itap_del_sel;
535 u32 mask, val;
Faiz Abbase4425cb2020-02-26 13:44:34 +0530536
Nitin Yadav83db1f92024-04-18 14:00:57 -0500537 otap_del_sel = plat->otap_del_sel[mode];
538
Faiz Abbase4425cb2020-02-26 13:44:34 +0530539 mask = OTAPDLYENA_MASK | OTAPDLYSEL_MASK;
Nitin Yadav83db1f92024-04-18 14:00:57 -0500540 val = (1 << OTAPDLYENA_SHIFT) |
541 (otap_del_sel << OTAPDLYSEL_SHIFT);
542
Judith Mendez87bcbde2024-04-18 14:00:58 -0500543 itap_del_ena = plat->itap_del_ena[mode];
Nitin Yadav83db1f92024-04-18 14:00:57 -0500544 itap_del_sel = plat->itap_del_sel[mode];
545
546 mask |= ITAPDLYENA_MASK | ITAPDLYSEL_MASK;
Judith Mendez87bcbde2024-04-18 14:00:58 -0500547 val |= (itap_del_ena << ITAPDLYENA_SHIFT) |
Nitin Yadav83db1f92024-04-18 14:00:57 -0500548 (itap_del_sel << ITAPDLYSEL_SHIFT);
549
550 regmap_update_bits(plat->base, PHY_CTRL4, ITAPCHGWIN_MASK,
551 1 << ITAPCHGWIN_SHIFT);
Faiz Abbase4425cb2020-02-26 13:44:34 +0530552 regmap_update_bits(plat->base, PHY_CTRL4, mask, val);
Nitin Yadav83db1f92024-04-18 14:00:57 -0500553 regmap_update_bits(plat->base, PHY_CTRL4, ITAPCHGWIN_MASK, 0);
Faiz Abbase4425cb2020-02-26 13:44:34 +0530554
Faiz Abbasc73f04e2021-02-04 15:10:52 +0530555 regmap_update_bits(plat->base, PHY_CTRL5, CLKBUFSEL_MASK,
556 plat->clkbuf_sel);
557
Faiz Abbase4425cb2020-02-26 13:44:34 +0530558 return 0;
559}
560
561const struct sdhci_ops j721e_4bit_sdhci_ops = {
Tom Rinidec7ea02024-05-20 13:35:03 -0600562#if CONFIG_IS_ENABLED(MMC_SUPPORTS_TUNING)
Faiz Abbas7a7e2c72021-02-04 15:10:53 +0530563 .platform_execute_tuning = am654_sdhci_execute_tuning,
564#endif
Faiz Abbase4425cb2020-02-26 13:44:34 +0530565 .deferred_probe = am654_sdhci_deferred_probe,
566 .set_ios_post = &j721e_4bit_sdhci_set_ios_post,
Faiz Abbas9c10cfe2021-02-04 15:10:55 +0530567 .set_control_reg = sdhci_set_control_reg,
Faiz Abbas36c8c5c2021-02-04 15:10:54 +0530568 .write_b = am654_sdhci_write_b,
Faiz Abbase4425cb2020-02-26 13:44:34 +0530569};
570
571const struct am654_driver_data j721e_4bit_drv_data = {
572 .ops = &j721e_4bit_sdhci_ops,
573 .flags = IOMUX_PRESENT,
574};
575
Dave Gerlach057d6af2021-04-23 11:27:40 -0500576static const struct am654_driver_data sdhci_am64_8bit_drvdata = {
577 .ops = &am654_sdhci_ops,
578 .flags = DLL_PRESENT | DLL_CALIB,
579};
580
581static const struct am654_driver_data sdhci_am64_4bit_drvdata = {
582 .ops = &j721e_4bit_sdhci_ops,
583 .flags = IOMUX_PRESENT,
584};
585
Faiz Abbas2c2fc962021-02-04 15:10:50 +0530586const struct soc_attr am654_sdhci_soc_attr[] = {
587 { .family = "AM65X", .revision = "SR1.0", .data = &am654_sr1_drv_data},
588 {/* sentinel */}
589};
590
Faiz Abbasc6eb9e72020-02-26 13:44:33 +0530591static int sdhci_am654_get_otap_delay(struct udevice *dev,
592 struct mmc_config *cfg)
593{
Simon Glassfa20e932020-12-03 16:55:20 -0700594 struct am654_sdhci_plat *plat = dev_get_plat(dev);
Faiz Abbasc6eb9e72020-02-26 13:44:33 +0530595 int ret;
596 int i;
597
598 /* ti,otap-del-sel-legacy is mandatory */
599 ret = dev_read_u32(dev, "ti,otap-del-sel-legacy",
600 &plat->otap_del_sel[0]);
601 if (ret)
602 return ret;
603 /*
604 * Remove the corresponding capability if an otap-del-sel
605 * value is not found
606 */
Nitin Yadav83db1f92024-04-18 14:00:57 -0500607 for (i = MMC_LEGACY; i <= MMC_HS_400; i++) {
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530608 ret = dev_read_u32(dev, td[i].otap_binding,
609 &plat->otap_del_sel[i]);
Faiz Abbasc6eb9e72020-02-26 13:44:33 +0530610 if (ret) {
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530611 dev_dbg(dev, "Couldn't find %s\n", td[i].otap_binding);
Faiz Abbasc6eb9e72020-02-26 13:44:33 +0530612 /*
613 * Remove the corresponding capability
614 * if an otap-del-sel value is not found
615 */
616 cfg->host_caps &= ~td[i].capability;
617 }
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530618
Judith Mendez87bcbde2024-04-18 14:00:58 -0500619 if (td[i].itap_binding) {
620 ret = dev_read_u32(dev, td[i].itap_binding,
621 &plat->itap_del_sel[i]);
622
623 if (!ret)
624 plat->itap_del_ena[i] = ENABLE;
625 }
Faiz Abbasc6eb9e72020-02-26 13:44:33 +0530626 }
627
628 return 0;
629}
630
Faiz Abbasd8fb3092019-06-11 00:43:31 +0530631static int am654_sdhci_probe(struct udevice *dev)
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530632{
Faiz Abbas8cc051e2020-01-16 19:42:19 +0530633 struct am654_driver_data *drv_data =
634 (struct am654_driver_data *)dev_get_driver_data(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700635 struct am654_sdhci_plat *plat = dev_get_plat(dev);
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530636 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
637 struct sdhci_host *host = dev_get_priv(dev);
Faiz Abbase9aed582019-06-11 00:43:38 +0530638 struct mmc_config *cfg = &plat->cfg;
Faiz Abbas2c2fc962021-02-04 15:10:50 +0530639 const struct soc_attr *soc;
640 const struct am654_driver_data *soc_drv_data;
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530641 struct clk clk;
642 unsigned long clock;
643 int ret;
644
Faiz Abbasdc2bcc22020-01-16 19:42:18 +0530645 ret = clk_get_by_name(dev, "clk_xin", &clk);
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530646 if (ret) {
647 dev_err(dev, "failed to get clock\n");
648 return ret;
649 }
650
651 clock = clk_get_rate(&clk);
652 if (IS_ERR_VALUE(clock)) {
653 dev_err(dev, "failed to get rate\n");
654 return clock;
655 }
656
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530657 host->max_clk = clock;
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530658 host->mmc = &plat->mmc;
Faiz Abbase9aed582019-06-11 00:43:38 +0530659 host->mmc->dev = dev;
Faiz Abbas36c8c5c2021-02-04 15:10:54 +0530660 host->ops = drv_data->ops;
Faiz Abbase9aed582019-06-11 00:43:38 +0530661 ret = sdhci_setup_cfg(cfg, host, cfg->f_max,
662 AM654_SDHCI_MIN_FREQ);
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530663 if (ret)
664 return ret;
Faiz Abbas8cc051e2020-01-16 19:42:19 +0530665
Faiz Abbasc6eb9e72020-02-26 13:44:33 +0530666 ret = sdhci_am654_get_otap_delay(dev, cfg);
667 if (ret)
668 return ret;
669
Faiz Abbas2c2fc962021-02-04 15:10:50 +0530670 /* Update ops based on SoC revision */
671 soc = soc_device_match(am654_sdhci_soc_attr);
672 if (soc && soc->data) {
673 soc_drv_data = soc->data;
674 host->ops = soc_drv_data->ops;
675 }
676
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530677 host->mmc->priv = host;
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530678 upriv->mmc = host->mmc;
679
Faiz Abbase9aed582019-06-11 00:43:38 +0530680 regmap_init_mem_index(dev_ofnode(dev), &plat->base, 1);
681
Faiz Abbase4425cb2020-02-26 13:44:34 +0530682 return 0;
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530683}
684
Simon Glassaad29ae2020-12-03 16:55:21 -0700685static int am654_sdhci_of_to_plat(struct udevice *dev)
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530686{
Simon Glassfa20e932020-12-03 16:55:20 -0700687 struct am654_sdhci_plat *plat = dev_get_plat(dev);
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530688 struct sdhci_host *host = dev_get_priv(dev);
Faiz Abbase9aed582019-06-11 00:43:38 +0530689 struct mmc_config *cfg = &plat->cfg;
690 u32 drv_strength;
691 int ret;
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530692
693 host->name = dev->name;
Johan Jonker8d5d8e02023-03-13 01:32:04 +0100694 host->ioaddr = dev_read_addr_ptr(dev);
Faiz Abbase9aed582019-06-11 00:43:38 +0530695 plat->non_removable = dev_read_bool(dev, "non-removable");
696
Faiz Abbasfd8be702019-06-13 10:29:51 +0530697 if (plat->flags & DLL_PRESENT) {
698 ret = dev_read_u32(dev, "ti,trm-icp", &plat->trm_icp);
699 if (ret)
700 return ret;
701
702 ret = dev_read_u32(dev, "ti,driver-strength-ohm",
703 &drv_strength);
704 if (ret)
705 return ret;
Faiz Abbase9aed582019-06-11 00:43:38 +0530706
Faiz Abbasfd8be702019-06-13 10:29:51 +0530707 switch (drv_strength) {
708 case 50:
709 plat->drv_strength = DRIVER_STRENGTH_50_OHM;
710 break;
711 case 33:
712 plat->drv_strength = DRIVER_STRENGTH_33_OHM;
713 break;
714 case 66:
715 plat->drv_strength = DRIVER_STRENGTH_66_OHM;
716 break;
717 case 100:
718 plat->drv_strength = DRIVER_STRENGTH_100_OHM;
719 break;
720 case 40:
721 plat->drv_strength = DRIVER_STRENGTH_40_OHM;
722 break;
723 default:
724 dev_err(dev, "Invalid driver strength\n");
725 return -EINVAL;
726 }
Faiz Abbase9aed582019-06-11 00:43:38 +0530727 }
728
Aswath Govindraju4509fb62021-05-25 15:08:23 +0530729 dev_read_u32(dev, "ti,strobe-sel", &plat->strb_sel);
Faiz Abbasc73f04e2021-02-04 15:10:52 +0530730 dev_read_u32(dev, "ti,clkbuf-sel", &plat->clkbuf_sel);
731
Faiz Abbase9aed582019-06-11 00:43:38 +0530732 ret = mmc_of_parse(dev, cfg);
733 if (ret)
734 return ret;
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530735
736 return 0;
737}
738
Faiz Abbasd8fb3092019-06-11 00:43:31 +0530739static int am654_sdhci_bind(struct udevice *dev)
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530740{
Faiz Abbas8cc051e2020-01-16 19:42:19 +0530741 struct am654_driver_data *drv_data =
742 (struct am654_driver_data *)dev_get_driver_data(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700743 struct am654_sdhci_plat *plat = dev_get_plat(dev);
Faiz Abbas2c2fc962021-02-04 15:10:50 +0530744 const struct soc_attr *soc;
745 const struct am654_driver_data *soc_drv_data;
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530746
Faiz Abbas8cc051e2020-01-16 19:42:19 +0530747 plat->flags = drv_data->flags;
748
Faiz Abbas2c2fc962021-02-04 15:10:50 +0530749 /* Update flags based on SoC revision */
750 soc = soc_device_match(am654_sdhci_soc_attr);
751 if (soc && soc->data) {
752 soc_drv_data = soc->data;
753 plat->flags = soc_drv_data->flags;
754 }
755
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530756 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
757}
758
Faiz Abbasd8fb3092019-06-11 00:43:31 +0530759static const struct udevice_id am654_sdhci_ids[] = {
Faiz Abbasfd8be702019-06-13 10:29:51 +0530760 {
761 .compatible = "ti,am654-sdhci-5.1",
Faiz Abbas8cc051e2020-01-16 19:42:19 +0530762 .data = (ulong)&am654_drv_data,
Faiz Abbasfd8be702019-06-13 10:29:51 +0530763 },
764 {
765 .compatible = "ti,j721e-sdhci-8bit",
Faiz Abbas8cc051e2020-01-16 19:42:19 +0530766 .data = (ulong)&j721e_8bit_drv_data,
Faiz Abbasfd8be702019-06-13 10:29:51 +0530767 },
768 {
769 .compatible = "ti,j721e-sdhci-4bit",
Faiz Abbas8cc051e2020-01-16 19:42:19 +0530770 .data = (ulong)&j721e_4bit_drv_data,
Faiz Abbasfd8be702019-06-13 10:29:51 +0530771 },
Dave Gerlach057d6af2021-04-23 11:27:40 -0500772 {
773 .compatible = "ti,am64-sdhci-8bit",
774 .data = (ulong)&sdhci_am64_8bit_drvdata,
775 },
776 {
777 .compatible = "ti,am64-sdhci-4bit",
778 .data = (ulong)&sdhci_am64_4bit_drvdata,
779 },
Aswath Govindraju71b9a7b2022-05-25 13:38:39 +0530780 {
781 .compatible = "ti,am62-sdhci",
782 .data = (ulong)&sdhci_am64_4bit_drvdata,
783 },
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530784 { }
785};
786
Faiz Abbasd8fb3092019-06-11 00:43:31 +0530787U_BOOT_DRIVER(am654_sdhci_drv) = {
788 .name = "am654_sdhci",
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530789 .id = UCLASS_MMC,
Faiz Abbasd8fb3092019-06-11 00:43:31 +0530790 .of_match = am654_sdhci_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700791 .of_to_plat = am654_sdhci_of_to_plat,
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530792 .ops = &sdhci_ops,
Faiz Abbasd8fb3092019-06-11 00:43:31 +0530793 .bind = am654_sdhci_bind,
794 .probe = am654_sdhci_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700795 .priv_auto = sizeof(struct sdhci_host),
Simon Glass71fa5b42020-12-03 16:55:18 -0700796 .plat_auto = sizeof(struct am654_sdhci_plat),
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530797};