blob: e86ef1a8b21815048ba6bbad4454abb0b31fcc82 [file] [log] [blame]
Lokesh Vutlabc9979f2018-08-27 15:57:54 +05301// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2018 Texas Instruments Incorporated - http://www.ti.com/
4 *
5 * Texas Instruments' K3 SD Host Controller Interface
6 */
7
8#include <clk.h>
9#include <common.h>
10#include <dm.h>
11#include <malloc.h>
12#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
Faiz Abbasd8fb3092019-06-11 00:43:31 +053087struct am654_sdhci_plat {
Lokesh Vutlabc9979f2018-08-27 15:57:54 +053088 struct mmc_config cfg;
89 struct mmc mmc;
Faiz Abbase9aed582019-06-11 00:43:38 +053090 struct regmap *base;
91 bool non_removable;
Faiz Abbas7101e122020-07-29 07:03:41 +053092 u32 otap_del_sel[MMC_MODES_END];
Faiz Abbasdef2a0f2021-02-04 15:10:51 +053093 u32 itap_del_sel[MMC_MODES_END];
Faiz Abbase9aed582019-06-11 00:43:38 +053094 u32 trm_icp;
95 u32 drv_strength;
Faiz Abbas8cc051e2020-01-16 19:42:19 +053096 u32 strb_sel;
Faiz Abbasc73f04e2021-02-04 15:10:52 +053097 u32 clkbuf_sel;
Faiz Abbasfd8be702019-06-13 10:29:51 +053098 u32 flags;
Faiz Abbasb7f57bb2021-02-04 15:10:48 +053099#define DLL_PRESENT BIT(0)
100#define IOMUX_PRESENT BIT(1)
101#define FREQSEL_2_BIT BIT(2)
102#define STRBSEL_4_BIT BIT(3)
Faiz Abbas947e8f32021-02-04 15:10:49 +0530103#define DLL_CALIB BIT(4)
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530104};
105
Faiz Abbasc6eb9e72020-02-26 13:44:33 +0530106struct timing_data {
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530107 const char *otap_binding;
108 const char *itap_binding;
Faiz Abbasc6eb9e72020-02-26 13:44:33 +0530109 u32 capability;
110};
111
112static const struct timing_data td[] = {
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530113 [MMC_LEGACY] = {"ti,otap-del-sel-legacy",
114 "ti,itap-del-sel-legacy",
115 0},
116 [MMC_HS] = {"ti,otap-del-sel-mmc-hs",
117 "ti,itap-del-sel-mms-hs",
118 MMC_CAP(MMC_HS)},
119 [SD_HS] = {"ti,otap-del-sel-sd-hs",
120 "ti,itap-del-sel-sd-hs",
121 MMC_CAP(SD_HS)},
122 [UHS_SDR12] = {"ti,otap-del-sel-sdr12",
123 "ti,itap-del-sel-sdr12",
124 MMC_CAP(UHS_SDR12)},
125 [UHS_SDR25] = {"ti,otap-del-sel-sdr25",
126 "ti,itap-del-sel-sdr25",
127 MMC_CAP(UHS_SDR25)},
128 [UHS_SDR50] = {"ti,otap-del-sel-sdr50",
129 NULL,
130 MMC_CAP(UHS_SDR50)},
131 [UHS_SDR104] = {"ti,otap-del-sel-sdr104",
132 NULL,
133 MMC_CAP(UHS_SDR104)},
134 [UHS_DDR50] = {"ti,otap-del-sel-ddr50",
135 NULL,
136 MMC_CAP(UHS_DDR50)},
137 [MMC_DDR_52] = {"ti,otap-del-sel-ddr52",
138 "ti,itap-del-sel-ddr52",
139 MMC_CAP(MMC_DDR_52)},
140 [MMC_HS_200] = {"ti,otap-del-sel-hs200",
141 NULL,
142 MMC_CAP(MMC_HS_200)},
143 [MMC_HS_400] = {"ti,otap-del-sel-hs400",
144 NULL,
145 MMC_CAP(MMC_HS_400)},
Faiz Abbasc6eb9e72020-02-26 13:44:33 +0530146};
147
Faiz Abbas8cc051e2020-01-16 19:42:19 +0530148struct am654_driver_data {
149 const struct sdhci_ops *ops;
150 u32 flags;
151};
152
Faiz Abbas7eecee62019-06-11 00:43:41 +0530153static void am654_sdhci_set_control_reg(struct sdhci_host *host)
154{
155 struct mmc *mmc = (struct mmc *)host->mmc;
156 u32 reg;
157
158 if (IS_SD(host->mmc) &&
159 mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180) {
160 reg = sdhci_readw(host, SDHCI_HOST_CONTROL2);
161 reg |= SDHCI_CTRL_VDD_180;
162 sdhci_writew(host, reg, SDHCI_HOST_CONTROL2);
163 }
164
165 sdhci_set_uhs_timing(host);
166}
167
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530168static int am654_sdhci_setup_dll(struct am654_sdhci_plat *plat,
169 unsigned int speed)
170{
171 int sel50, sel100, freqsel;
172 u32 mask, val;
173 int ret;
174
175 /* Disable delay chain mode */
176 regmap_update_bits(plat->base, PHY_CTRL5,
177 SELDLYTXCLK_MASK | SELDLYRXCLK_MASK, 0);
178
179 if (plat->flags & FREQSEL_2_BIT) {
180 switch (speed) {
181 case 200000000:
182 sel50 = 0;
183 sel100 = 0;
184 break;
185 case 100000000:
186 sel50 = 0;
187 sel100 = 1;
188 break;
189 default:
190 sel50 = 1;
191 sel100 = 0;
192 }
193
194 /* Configure PHY DLL frequency */
195 mask = SEL50_MASK | SEL100_MASK;
196 val = (sel50 << SEL50_SHIFT) | (sel100 << SEL100_SHIFT);
197 regmap_update_bits(plat->base, PHY_CTRL5, mask, val);
198 } else {
199 switch (speed) {
200 case 200000000:
201 freqsel = 0x0;
202 break;
203 default:
204 freqsel = 0x4;
205 }
206 regmap_update_bits(plat->base, PHY_CTRL5, FREQSEL_MASK,
207 freqsel << FREQSEL_SHIFT);
208 }
209
210 /* Configure DLL TRIM */
211 mask = DLL_TRIM_ICP_MASK;
212 val = plat->trm_icp << DLL_TRIM_ICP_SHIFT;
213
214 /* Configure DLL driver strength */
215 mask |= DR_TY_MASK;
216 val |= plat->drv_strength << DR_TY_SHIFT;
217 regmap_update_bits(plat->base, PHY_CTRL1, mask, val);
218
219 /* Enable DLL */
220 regmap_update_bits(plat->base, PHY_CTRL1, ENDLL_MASK,
221 0x1 << ENDLL_SHIFT);
222 /*
223 * Poll for DLL ready. Use a one second timeout.
224 * Works in all experiments done so far
225 */
226 ret = regmap_read_poll_timeout(plat->base, PHY_STAT1, val,
227 val & DLLRDY_MASK, 1000, 1000000);
228
229 return ret;
230}
231
232static void am654_sdhci_write_itapdly(struct am654_sdhci_plat *plat,
233 u32 itapdly)
234{
235 /* Set ITAPCHGWIN before writing to ITAPDLY */
236 regmap_update_bits(plat->base, PHY_CTRL4, ITAPCHGWIN_MASK,
237 1 << ITAPCHGWIN_SHIFT);
238 regmap_update_bits(plat->base, PHY_CTRL4, ITAPDLYSEL_MASK,
239 itapdly << ITAPDLYSEL_SHIFT);
240 regmap_update_bits(plat->base, PHY_CTRL4, ITAPCHGWIN_MASK, 0);
241}
242
243static void am654_sdhci_setup_delay_chain(struct am654_sdhci_plat *plat,
244 int mode)
245{
246 u32 mask, val;
247
248 val = 1 << SELDLYTXCLK_SHIFT | 1 << SELDLYRXCLK_SHIFT;
249 mask = SELDLYTXCLK_MASK | SELDLYRXCLK_MASK;
250 regmap_update_bits(plat->base, PHY_CTRL5, mask, val);
251
252 am654_sdhci_write_itapdly(plat, plat->itap_del_sel[mode]);
253}
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
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530293 if (mode > UHS_SDR25 && speed >= CLOCK_TOO_SLOW_HZ) {
294 ret = am654_sdhci_setup_dll(plat, speed);
Faiz Abbase9aed582019-06-11 00:43:38 +0530295 if (ret)
296 return ret;
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530297 } else {
298 am654_sdhci_setup_delay_chain(plat, mode);
Faiz Abbase9aed582019-06-11 00:43:38 +0530299 }
300
Faiz Abbasc73f04e2021-02-04 15:10:52 +0530301 regmap_update_bits(plat->base, PHY_CTRL5, CLKBUFSEL_MASK,
302 plat->clkbuf_sel);
303
Faiz Abbase9aed582019-06-11 00:43:38 +0530304 return 0;
305}
306
Faiz Abbase9aed582019-06-11 00:43:38 +0530307int am654_sdhci_init(struct am654_sdhci_plat *plat)
308{
309 u32 ctl_cfg_2 = 0;
310 u32 mask, val;
311 int ret;
312
313 /* Reset OTAP to default value */
314 mask = OTAPDLYENA_MASK | OTAPDLYSEL_MASK;
315 regmap_update_bits(plat->base, PHY_CTRL4, mask, 0x0);
316
Faiz Abbas947e8f32021-02-04 15:10:49 +0530317 if (plat->flags & DLL_CALIB) {
Faiz Abbasfd8be702019-06-13 10:29:51 +0530318 regmap_read(plat->base, PHY_STAT1, &val);
319 if (~val & CALDONE_MASK) {
320 /* Calibrate IO lines */
321 regmap_update_bits(plat->base, PHY_CTRL1, PDB_MASK,
322 PDB_MASK);
323 ret = regmap_read_poll_timeout(plat->base, PHY_STAT1,
324 val, val & CALDONE_MASK,
325 1, 20);
326 if (ret)
327 return ret;
328 }
Faiz Abbasfd8be702019-06-13 10:29:51 +0530329 }
Faiz Abbase9aed582019-06-11 00:43:38 +0530330
331 /* Enable pins by setting IO mux to 0 */
Faiz Abbas8cc051e2020-01-16 19:42:19 +0530332 if (plat->flags & IOMUX_PRESENT)
333 regmap_update_bits(plat->base, PHY_CTRL1, IOMUX_ENABLE_MASK, 0);
Faiz Abbase9aed582019-06-11 00:43:38 +0530334
335 /* Set slot type based on SD or eMMC */
336 if (plat->non_removable)
337 ctl_cfg_2 = SLOTTYPE_EMBEDDED;
338
339 regmap_update_bits(plat->base, CTL_CFG_2, SLOTTYPE_MASK, ctl_cfg_2);
340
341 return 0;
342}
343
Faiz Abbase4425cb2020-02-26 13:44:34 +0530344#define MAX_SDCD_DEBOUNCE_TIME 2000
345static int am654_sdhci_deferred_probe(struct sdhci_host *host)
346{
347 struct udevice *dev = host->mmc->dev;
Simon Glassfa20e932020-12-03 16:55:20 -0700348 struct am654_sdhci_plat *plat = dev_get_plat(dev);
Faiz Abbase4425cb2020-02-26 13:44:34 +0530349 unsigned long start;
350 int val;
351
352 /*
353 * The controller takes about 1 second to debounce the card detect line
354 * and doesn't let us power on until that time is up. Instead of waiting
355 * for 1 second at every stage, poll on the CARD_PRESENT bit upto a
356 * maximum of 2 seconds to be safe..
357 */
358 start = get_timer(0);
359 do {
360 if (get_timer(start) > MAX_SDCD_DEBOUNCE_TIME)
361 return -ENOMEDIUM;
362
363 val = mmc_getcd(host->mmc);
364 } while (!val);
365
366 am654_sdhci_init(plat);
367
368 return sdhci_probe(dev);
369}
370
371const struct sdhci_ops am654_sdhci_ops = {
372 .deferred_probe = am654_sdhci_deferred_probe,
373 .set_ios_post = &am654_sdhci_set_ios_post,
374 .set_control_reg = &am654_sdhci_set_control_reg,
375};
376
377const struct am654_driver_data am654_drv_data = {
378 .ops = &am654_sdhci_ops,
Faiz Abbas2c2fc962021-02-04 15:10:50 +0530379 .flags = DLL_PRESENT | IOMUX_PRESENT | FREQSEL_2_BIT | STRBSEL_4_BIT,
380};
381
382const struct am654_driver_data am654_sr1_drv_data = {
383 .ops = &am654_sdhci_ops,
Faiz Abbas947e8f32021-02-04 15:10:49 +0530384 .flags = IOMUX_PRESENT | FREQSEL_2_BIT | DLL_PRESENT | DLL_CALIB |
385 STRBSEL_4_BIT,
Faiz Abbase4425cb2020-02-26 13:44:34 +0530386};
387
388const struct am654_driver_data j721e_8bit_drv_data = {
389 .ops = &am654_sdhci_ops,
Faiz Abbas947e8f32021-02-04 15:10:49 +0530390 .flags = DLL_PRESENT | DLL_CALIB,
Faiz Abbase4425cb2020-02-26 13:44:34 +0530391};
392
393static int j721e_4bit_sdhci_set_ios_post(struct sdhci_host *host)
394{
395 struct udevice *dev = host->mmc->dev;
Simon Glassfa20e932020-12-03 16:55:20 -0700396 struct am654_sdhci_plat *plat = dev_get_plat(dev);
Faiz Abbase4425cb2020-02-26 13:44:34 +0530397 u32 otap_del_sel, mask, val;
398
399 otap_del_sel = plat->otap_del_sel[host->mmc->selected_mode];
400 mask = OTAPDLYENA_MASK | OTAPDLYSEL_MASK;
401 val = (1 << OTAPDLYENA_SHIFT) | (otap_del_sel << OTAPDLYSEL_SHIFT);
402 regmap_update_bits(plat->base, PHY_CTRL4, mask, val);
403
Faiz Abbasc73f04e2021-02-04 15:10:52 +0530404 regmap_update_bits(plat->base, PHY_CTRL5, CLKBUFSEL_MASK,
405 plat->clkbuf_sel);
406
Faiz Abbase4425cb2020-02-26 13:44:34 +0530407 return 0;
408}
409
410const struct sdhci_ops j721e_4bit_sdhci_ops = {
411 .deferred_probe = am654_sdhci_deferred_probe,
412 .set_ios_post = &j721e_4bit_sdhci_set_ios_post,
413};
414
415const struct am654_driver_data j721e_4bit_drv_data = {
416 .ops = &j721e_4bit_sdhci_ops,
417 .flags = IOMUX_PRESENT,
418};
419
Faiz Abbas2c2fc962021-02-04 15:10:50 +0530420const struct soc_attr am654_sdhci_soc_attr[] = {
421 { .family = "AM65X", .revision = "SR1.0", .data = &am654_sr1_drv_data},
422 {/* sentinel */}
423};
424
Faiz Abbasc6eb9e72020-02-26 13:44:33 +0530425static int sdhci_am654_get_otap_delay(struct udevice *dev,
426 struct mmc_config *cfg)
427{
Simon Glassfa20e932020-12-03 16:55:20 -0700428 struct am654_sdhci_plat *plat = dev_get_plat(dev);
Faiz Abbasc6eb9e72020-02-26 13:44:33 +0530429 int ret;
430 int i;
431
432 /* ti,otap-del-sel-legacy is mandatory */
433 ret = dev_read_u32(dev, "ti,otap-del-sel-legacy",
434 &plat->otap_del_sel[0]);
435 if (ret)
436 return ret;
437 /*
438 * Remove the corresponding capability if an otap-del-sel
439 * value is not found
440 */
441 for (i = MMC_HS; i <= MMC_HS_400; i++) {
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530442 ret = dev_read_u32(dev, td[i].otap_binding,
443 &plat->otap_del_sel[i]);
Faiz Abbasc6eb9e72020-02-26 13:44:33 +0530444 if (ret) {
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530445 dev_dbg(dev, "Couldn't find %s\n", td[i].otap_binding);
Faiz Abbasc6eb9e72020-02-26 13:44:33 +0530446 /*
447 * Remove the corresponding capability
448 * if an otap-del-sel value is not found
449 */
450 cfg->host_caps &= ~td[i].capability;
451 }
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530452
453 if (td[i].itap_binding)
454 dev_read_u32(dev, td[i].itap_binding,
455 &plat->itap_del_sel[i]);
Faiz Abbasc6eb9e72020-02-26 13:44:33 +0530456 }
457
458 return 0;
459}
460
Faiz Abbasd8fb3092019-06-11 00:43:31 +0530461static int am654_sdhci_probe(struct udevice *dev)
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530462{
Faiz Abbas8cc051e2020-01-16 19:42:19 +0530463 struct am654_driver_data *drv_data =
464 (struct am654_driver_data *)dev_get_driver_data(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700465 struct am654_sdhci_plat *plat = dev_get_plat(dev);
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530466 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
467 struct sdhci_host *host = dev_get_priv(dev);
Faiz Abbase9aed582019-06-11 00:43:38 +0530468 struct mmc_config *cfg = &plat->cfg;
Faiz Abbas2c2fc962021-02-04 15:10:50 +0530469 const struct soc_attr *soc;
470 const struct am654_driver_data *soc_drv_data;
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530471 struct clk clk;
472 unsigned long clock;
473 int ret;
474
Faiz Abbasdc2bcc22020-01-16 19:42:18 +0530475 ret = clk_get_by_name(dev, "clk_xin", &clk);
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530476 if (ret) {
477 dev_err(dev, "failed to get clock\n");
478 return ret;
479 }
480
481 clock = clk_get_rate(&clk);
482 if (IS_ERR_VALUE(clock)) {
483 dev_err(dev, "failed to get rate\n");
484 return clock;
485 }
486
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530487 host->max_clk = clock;
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530488 host->mmc = &plat->mmc;
Faiz Abbase9aed582019-06-11 00:43:38 +0530489 host->mmc->dev = dev;
490 ret = sdhci_setup_cfg(cfg, host, cfg->f_max,
491 AM654_SDHCI_MIN_FREQ);
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530492 if (ret)
493 return ret;
Faiz Abbas8cc051e2020-01-16 19:42:19 +0530494
Faiz Abbasc6eb9e72020-02-26 13:44:33 +0530495 ret = sdhci_am654_get_otap_delay(dev, cfg);
496 if (ret)
497 return ret;
498
Faiz Abbas8cc051e2020-01-16 19:42:19 +0530499 host->ops = drv_data->ops;
Faiz Abbas2c2fc962021-02-04 15:10:50 +0530500
501 /* Update ops based on SoC revision */
502 soc = soc_device_match(am654_sdhci_soc_attr);
503 if (soc && soc->data) {
504 soc_drv_data = soc->data;
505 host->ops = soc_drv_data->ops;
506 }
507
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530508 host->mmc->priv = host;
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530509 upriv->mmc = host->mmc;
510
Faiz Abbase9aed582019-06-11 00:43:38 +0530511 regmap_init_mem_index(dev_ofnode(dev), &plat->base, 1);
512
Faiz Abbase4425cb2020-02-26 13:44:34 +0530513 return 0;
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530514}
515
Simon Glassaad29ae2020-12-03 16:55:21 -0700516static int am654_sdhci_of_to_plat(struct udevice *dev)
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530517{
Simon Glassfa20e932020-12-03 16:55:20 -0700518 struct am654_sdhci_plat *plat = dev_get_plat(dev);
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530519 struct sdhci_host *host = dev_get_priv(dev);
Faiz Abbase9aed582019-06-11 00:43:38 +0530520 struct mmc_config *cfg = &plat->cfg;
521 u32 drv_strength;
522 int ret;
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530523
524 host->name = dev->name;
525 host->ioaddr = (void *)dev_read_addr(dev);
Faiz Abbase9aed582019-06-11 00:43:38 +0530526 plat->non_removable = dev_read_bool(dev, "non-removable");
527
Faiz Abbasfd8be702019-06-13 10:29:51 +0530528 if (plat->flags & DLL_PRESENT) {
529 ret = dev_read_u32(dev, "ti,trm-icp", &plat->trm_icp);
530 if (ret)
531 return ret;
532
533 ret = dev_read_u32(dev, "ti,driver-strength-ohm",
534 &drv_strength);
535 if (ret)
536 return ret;
Faiz Abbase9aed582019-06-11 00:43:38 +0530537
Faiz Abbasfd8be702019-06-13 10:29:51 +0530538 switch (drv_strength) {
539 case 50:
540 plat->drv_strength = DRIVER_STRENGTH_50_OHM;
541 break;
542 case 33:
543 plat->drv_strength = DRIVER_STRENGTH_33_OHM;
544 break;
545 case 66:
546 plat->drv_strength = DRIVER_STRENGTH_66_OHM;
547 break;
548 case 100:
549 plat->drv_strength = DRIVER_STRENGTH_100_OHM;
550 break;
551 case 40:
552 plat->drv_strength = DRIVER_STRENGTH_40_OHM;
553 break;
554 default:
555 dev_err(dev, "Invalid driver strength\n");
556 return -EINVAL;
557 }
Faiz Abbase9aed582019-06-11 00:43:38 +0530558 }
559
Faiz Abbasc73f04e2021-02-04 15:10:52 +0530560 dev_read_u32(dev, "ti,clkbuf-sel", &plat->clkbuf_sel);
561
Faiz Abbase9aed582019-06-11 00:43:38 +0530562 ret = mmc_of_parse(dev, cfg);
563 if (ret)
564 return ret;
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530565
566 return 0;
567}
568
Faiz Abbasd8fb3092019-06-11 00:43:31 +0530569static int am654_sdhci_bind(struct udevice *dev)
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530570{
Faiz Abbas8cc051e2020-01-16 19:42:19 +0530571 struct am654_driver_data *drv_data =
572 (struct am654_driver_data *)dev_get_driver_data(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700573 struct am654_sdhci_plat *plat = dev_get_plat(dev);
Faiz Abbas2c2fc962021-02-04 15:10:50 +0530574 const struct soc_attr *soc;
575 const struct am654_driver_data *soc_drv_data;
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530576
Faiz Abbas8cc051e2020-01-16 19:42:19 +0530577 plat->flags = drv_data->flags;
578
Faiz Abbas2c2fc962021-02-04 15:10:50 +0530579 /* Update flags based on SoC revision */
580 soc = soc_device_match(am654_sdhci_soc_attr);
581 if (soc && soc->data) {
582 soc_drv_data = soc->data;
583 plat->flags = soc_drv_data->flags;
584 }
585
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530586 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
587}
588
Faiz Abbasd8fb3092019-06-11 00:43:31 +0530589static const struct udevice_id am654_sdhci_ids[] = {
Faiz Abbasfd8be702019-06-13 10:29:51 +0530590 {
591 .compatible = "ti,am654-sdhci-5.1",
Faiz Abbas8cc051e2020-01-16 19:42:19 +0530592 .data = (ulong)&am654_drv_data,
Faiz Abbasfd8be702019-06-13 10:29:51 +0530593 },
594 {
595 .compatible = "ti,j721e-sdhci-8bit",
Faiz Abbas8cc051e2020-01-16 19:42:19 +0530596 .data = (ulong)&j721e_8bit_drv_data,
Faiz Abbasfd8be702019-06-13 10:29:51 +0530597 },
598 {
599 .compatible = "ti,j721e-sdhci-4bit",
Faiz Abbas8cc051e2020-01-16 19:42:19 +0530600 .data = (ulong)&j721e_4bit_drv_data,
Faiz Abbasfd8be702019-06-13 10:29:51 +0530601 },
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530602 { }
603};
604
Faiz Abbasd8fb3092019-06-11 00:43:31 +0530605U_BOOT_DRIVER(am654_sdhci_drv) = {
606 .name = "am654_sdhci",
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530607 .id = UCLASS_MMC,
Faiz Abbasd8fb3092019-06-11 00:43:31 +0530608 .of_match = am654_sdhci_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700609 .of_to_plat = am654_sdhci_of_to_plat,
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530610 .ops = &sdhci_ops,
Faiz Abbasd8fb3092019-06-11 00:43:31 +0530611 .bind = am654_sdhci_bind,
612 .probe = am654_sdhci_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700613 .priv_auto = sizeof(struct sdhci_host),
Simon Glass71fa5b42020-12-03 16:55:18 -0700614 .plat_auto = sizeof(struct am654_sdhci_plat),
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530615};