blob: b82558254ebbd2a157a467668a855d74dd7a6ec6 [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 Abbase9aed582019-06-11 00:43:38 +053051#define DLL_TRIM_ICP_SHIFT 4
52#define DLL_TRIM_ICP_MASK GENMASK(7, 4)
53#define DR_TY_SHIFT 20
54#define DR_TY_MASK GENMASK(22, 20)
55#define ENDLL_SHIFT 1
56#define ENDLL_MASK BIT(ENDLL_SHIFT)
57#define DLLRDY_SHIFT 0
58#define DLLRDY_MASK BIT(DLLRDY_SHIFT)
59#define PDB_SHIFT 0
60#define PDB_MASK BIT(PDB_SHIFT)
61#define CALDONE_SHIFT 1
62#define CALDONE_MASK BIT(CALDONE_SHIFT)
63#define RETRIM_SHIFT 17
64#define RETRIM_MASK BIT(RETRIM_SHIFT)
Faiz Abbasdef2a0f2021-02-04 15:10:51 +053065#define SELDLYTXCLK_SHIFT 17
66#define SELDLYTXCLK_MASK BIT(SELDLYTXCLK_SHIFT)
67#define SELDLYRXCLK_SHIFT 16
68#define SELDLYRXCLK_MASK BIT(SELDLYRXCLK_SHIFT)
69#define ITAPDLYSEL_SHIFT 0
70#define ITAPDLYSEL_MASK GENMASK(4, 0)
71#define ITAPDLYENA_SHIFT 8
72#define ITAPDLYENA_MASK BIT(ITAPDLYENA_SHIFT)
73#define ITAPCHGWIN_SHIFT 9
74#define ITAPCHGWIN_MASK BIT(ITAPCHGWIN_SHIFT)
Faiz Abbase9aed582019-06-11 00:43:38 +053075
76#define DRIVER_STRENGTH_50_OHM 0x0
77#define DRIVER_STRENGTH_33_OHM 0x1
78#define DRIVER_STRENGTH_66_OHM 0x2
79#define DRIVER_STRENGTH_100_OHM 0x3
80#define DRIVER_STRENGTH_40_OHM 0x4
81
Faiz Abbasd8fb3092019-06-11 00:43:31 +053082#define AM654_SDHCI_MIN_FREQ 400000
Faiz Abbasdef2a0f2021-02-04 15:10:51 +053083#define CLOCK_TOO_SLOW_HZ 50000000
Lokesh Vutlabc9979f2018-08-27 15:57:54 +053084
Faiz Abbasd8fb3092019-06-11 00:43:31 +053085struct am654_sdhci_plat {
Lokesh Vutlabc9979f2018-08-27 15:57:54 +053086 struct mmc_config cfg;
87 struct mmc mmc;
Faiz Abbase9aed582019-06-11 00:43:38 +053088 struct regmap *base;
89 bool non_removable;
Faiz Abbas7101e122020-07-29 07:03:41 +053090 u32 otap_del_sel[MMC_MODES_END];
Faiz Abbasdef2a0f2021-02-04 15:10:51 +053091 u32 itap_del_sel[MMC_MODES_END];
Faiz Abbase9aed582019-06-11 00:43:38 +053092 u32 trm_icp;
93 u32 drv_strength;
Faiz Abbas8cc051e2020-01-16 19:42:19 +053094 u32 strb_sel;
Faiz Abbasfd8be702019-06-13 10:29:51 +053095 u32 flags;
Faiz Abbasb7f57bb2021-02-04 15:10:48 +053096#define DLL_PRESENT BIT(0)
97#define IOMUX_PRESENT BIT(1)
98#define FREQSEL_2_BIT BIT(2)
99#define STRBSEL_4_BIT BIT(3)
Faiz Abbas947e8f32021-02-04 15:10:49 +0530100#define DLL_CALIB BIT(4)
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530101};
102
Faiz Abbasc6eb9e72020-02-26 13:44:33 +0530103struct timing_data {
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530104 const char *otap_binding;
105 const char *itap_binding;
Faiz Abbasc6eb9e72020-02-26 13:44:33 +0530106 u32 capability;
107};
108
109static const struct timing_data td[] = {
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530110 [MMC_LEGACY] = {"ti,otap-del-sel-legacy",
111 "ti,itap-del-sel-legacy",
112 0},
113 [MMC_HS] = {"ti,otap-del-sel-mmc-hs",
114 "ti,itap-del-sel-mms-hs",
115 MMC_CAP(MMC_HS)},
116 [SD_HS] = {"ti,otap-del-sel-sd-hs",
117 "ti,itap-del-sel-sd-hs",
118 MMC_CAP(SD_HS)},
119 [UHS_SDR12] = {"ti,otap-del-sel-sdr12",
120 "ti,itap-del-sel-sdr12",
121 MMC_CAP(UHS_SDR12)},
122 [UHS_SDR25] = {"ti,otap-del-sel-sdr25",
123 "ti,itap-del-sel-sdr25",
124 MMC_CAP(UHS_SDR25)},
125 [UHS_SDR50] = {"ti,otap-del-sel-sdr50",
126 NULL,
127 MMC_CAP(UHS_SDR50)},
128 [UHS_SDR104] = {"ti,otap-del-sel-sdr104",
129 NULL,
130 MMC_CAP(UHS_SDR104)},
131 [UHS_DDR50] = {"ti,otap-del-sel-ddr50",
132 NULL,
133 MMC_CAP(UHS_DDR50)},
134 [MMC_DDR_52] = {"ti,otap-del-sel-ddr52",
135 "ti,itap-del-sel-ddr52",
136 MMC_CAP(MMC_DDR_52)},
137 [MMC_HS_200] = {"ti,otap-del-sel-hs200",
138 NULL,
139 MMC_CAP(MMC_HS_200)},
140 [MMC_HS_400] = {"ti,otap-del-sel-hs400",
141 NULL,
142 MMC_CAP(MMC_HS_400)},
Faiz Abbasc6eb9e72020-02-26 13:44:33 +0530143};
144
Faiz Abbas8cc051e2020-01-16 19:42:19 +0530145struct am654_driver_data {
146 const struct sdhci_ops *ops;
147 u32 flags;
148};
149
Faiz Abbas7eecee62019-06-11 00:43:41 +0530150static void am654_sdhci_set_control_reg(struct sdhci_host *host)
151{
152 struct mmc *mmc = (struct mmc *)host->mmc;
153 u32 reg;
154
155 if (IS_SD(host->mmc) &&
156 mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180) {
157 reg = sdhci_readw(host, SDHCI_HOST_CONTROL2);
158 reg |= SDHCI_CTRL_VDD_180;
159 sdhci_writew(host, reg, SDHCI_HOST_CONTROL2);
160 }
161
162 sdhci_set_uhs_timing(host);
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,
230 u32 itapdly)
231{
232 /* 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
249 am654_sdhci_write_itapdly(plat, plat->itap_del_sel[mode]);
250}
251
Faiz Abbase9aed582019-06-11 00:43:38 +0530252static int am654_sdhci_set_ios_post(struct sdhci_host *host)
253{
254 struct udevice *dev = host->mmc->dev;
Simon Glassfa20e932020-12-03 16:55:20 -0700255 struct am654_sdhci_plat *plat = dev_get_plat(dev);
Faiz Abbase9aed582019-06-11 00:43:38 +0530256 unsigned int speed = host->mmc->clock;
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530257 int mode = host->mmc->selected_mode;
Faiz Abbasc6eb9e72020-02-26 13:44:33 +0530258 u32 otap_del_sel;
Faiz Abbase9aed582019-06-11 00:43:38 +0530259 u32 mask, val;
260 int ret;
261
262 /* Reset SD Clock Enable */
263 val = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
264 val &= ~SDHCI_CLOCK_CARD_EN;
265 sdhci_writew(host, val, SDHCI_CLOCK_CONTROL);
266
Faiz Abbas2c45a2c2021-02-04 15:10:47 +0530267 regmap_update_bits(plat->base, PHY_CTRL1, ENDLL_MASK, 0);
Faiz Abbase9aed582019-06-11 00:43:38 +0530268
269 /* restart clock */
270 sdhci_set_clock(host->mmc, speed);
271
272 /* switch phy back on */
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530273 otap_del_sel = plat->otap_del_sel[mode];
274 mask = OTAPDLYENA_MASK | OTAPDLYSEL_MASK;
275 val = (1 << OTAPDLYENA_SHIFT) |
276 (otap_del_sel << OTAPDLYSEL_SHIFT);
Faiz Abbas8cc051e2020-01-16 19:42:19 +0530277
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530278 /* Write to STRBSEL for HS400 speed mode */
279 if (host->mmc->selected_mode == MMC_HS_400) {
280 if (plat->flags & STRBSEL_4_BIT)
281 mask |= STRBSEL_4BIT_MASK;
282 else
283 mask |= STRBSEL_8BIT_MASK;
Faiz Abbas8cc051e2020-01-16 19:42:19 +0530284
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530285 val |= plat->strb_sel << STRBSEL_SHIFT;
286 }
Faiz Abbas947e8f32021-02-04 15:10:49 +0530287
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530288 regmap_update_bits(plat->base, PHY_CTRL4, mask, val);
Faiz Abbas947e8f32021-02-04 15:10:49 +0530289
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530290 if (mode > UHS_SDR25 && speed >= CLOCK_TOO_SLOW_HZ) {
291 ret = am654_sdhci_setup_dll(plat, speed);
Faiz Abbase9aed582019-06-11 00:43:38 +0530292 if (ret)
293 return ret;
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530294 } else {
295 am654_sdhci_setup_delay_chain(plat, mode);
Faiz Abbase9aed582019-06-11 00:43:38 +0530296 }
297
298 return 0;
299}
300
Faiz Abbase9aed582019-06-11 00:43:38 +0530301int am654_sdhci_init(struct am654_sdhci_plat *plat)
302{
303 u32 ctl_cfg_2 = 0;
304 u32 mask, val;
305 int ret;
306
307 /* Reset OTAP to default value */
308 mask = OTAPDLYENA_MASK | OTAPDLYSEL_MASK;
309 regmap_update_bits(plat->base, PHY_CTRL4, mask, 0x0);
310
Faiz Abbas947e8f32021-02-04 15:10:49 +0530311 if (plat->flags & DLL_CALIB) {
Faiz Abbasfd8be702019-06-13 10:29:51 +0530312 regmap_read(plat->base, PHY_STAT1, &val);
313 if (~val & CALDONE_MASK) {
314 /* Calibrate IO lines */
315 regmap_update_bits(plat->base, PHY_CTRL1, PDB_MASK,
316 PDB_MASK);
317 ret = regmap_read_poll_timeout(plat->base, PHY_STAT1,
318 val, val & CALDONE_MASK,
319 1, 20);
320 if (ret)
321 return ret;
322 }
Faiz Abbasfd8be702019-06-13 10:29:51 +0530323 }
Faiz Abbase9aed582019-06-11 00:43:38 +0530324
325 /* Enable pins by setting IO mux to 0 */
Faiz Abbas8cc051e2020-01-16 19:42:19 +0530326 if (plat->flags & IOMUX_PRESENT)
327 regmap_update_bits(plat->base, PHY_CTRL1, IOMUX_ENABLE_MASK, 0);
Faiz Abbase9aed582019-06-11 00:43:38 +0530328
329 /* Set slot type based on SD or eMMC */
330 if (plat->non_removable)
331 ctl_cfg_2 = SLOTTYPE_EMBEDDED;
332
333 regmap_update_bits(plat->base, CTL_CFG_2, SLOTTYPE_MASK, ctl_cfg_2);
334
335 return 0;
336}
337
Faiz Abbase4425cb2020-02-26 13:44:34 +0530338#define MAX_SDCD_DEBOUNCE_TIME 2000
339static int am654_sdhci_deferred_probe(struct sdhci_host *host)
340{
341 struct udevice *dev = host->mmc->dev;
Simon Glassfa20e932020-12-03 16:55:20 -0700342 struct am654_sdhci_plat *plat = dev_get_plat(dev);
Faiz Abbase4425cb2020-02-26 13:44:34 +0530343 unsigned long start;
344 int val;
345
346 /*
347 * The controller takes about 1 second to debounce the card detect line
348 * and doesn't let us power on until that time is up. Instead of waiting
349 * for 1 second at every stage, poll on the CARD_PRESENT bit upto a
350 * maximum of 2 seconds to be safe..
351 */
352 start = get_timer(0);
353 do {
354 if (get_timer(start) > MAX_SDCD_DEBOUNCE_TIME)
355 return -ENOMEDIUM;
356
357 val = mmc_getcd(host->mmc);
358 } while (!val);
359
360 am654_sdhci_init(plat);
361
362 return sdhci_probe(dev);
363}
364
365const struct sdhci_ops am654_sdhci_ops = {
366 .deferred_probe = am654_sdhci_deferred_probe,
367 .set_ios_post = &am654_sdhci_set_ios_post,
368 .set_control_reg = &am654_sdhci_set_control_reg,
369};
370
371const struct am654_driver_data am654_drv_data = {
372 .ops = &am654_sdhci_ops,
Faiz Abbas2c2fc962021-02-04 15:10:50 +0530373 .flags = DLL_PRESENT | IOMUX_PRESENT | FREQSEL_2_BIT | STRBSEL_4_BIT,
374};
375
376const struct am654_driver_data am654_sr1_drv_data = {
377 .ops = &am654_sdhci_ops,
Faiz Abbas947e8f32021-02-04 15:10:49 +0530378 .flags = IOMUX_PRESENT | FREQSEL_2_BIT | DLL_PRESENT | DLL_CALIB |
379 STRBSEL_4_BIT,
Faiz Abbase4425cb2020-02-26 13:44:34 +0530380};
381
382const struct am654_driver_data j721e_8bit_drv_data = {
383 .ops = &am654_sdhci_ops,
Faiz Abbas947e8f32021-02-04 15:10:49 +0530384 .flags = DLL_PRESENT | DLL_CALIB,
Faiz Abbase4425cb2020-02-26 13:44:34 +0530385};
386
387static int j721e_4bit_sdhci_set_ios_post(struct sdhci_host *host)
388{
389 struct udevice *dev = host->mmc->dev;
Simon Glassfa20e932020-12-03 16:55:20 -0700390 struct am654_sdhci_plat *plat = dev_get_plat(dev);
Faiz Abbase4425cb2020-02-26 13:44:34 +0530391 u32 otap_del_sel, mask, val;
392
393 otap_del_sel = plat->otap_del_sel[host->mmc->selected_mode];
394 mask = OTAPDLYENA_MASK | OTAPDLYSEL_MASK;
395 val = (1 << OTAPDLYENA_SHIFT) | (otap_del_sel << OTAPDLYSEL_SHIFT);
396 regmap_update_bits(plat->base, PHY_CTRL4, mask, val);
397
398 return 0;
399}
400
401const struct sdhci_ops j721e_4bit_sdhci_ops = {
402 .deferred_probe = am654_sdhci_deferred_probe,
403 .set_ios_post = &j721e_4bit_sdhci_set_ios_post,
404};
405
406const struct am654_driver_data j721e_4bit_drv_data = {
407 .ops = &j721e_4bit_sdhci_ops,
408 .flags = IOMUX_PRESENT,
409};
410
Faiz Abbas2c2fc962021-02-04 15:10:50 +0530411const struct soc_attr am654_sdhci_soc_attr[] = {
412 { .family = "AM65X", .revision = "SR1.0", .data = &am654_sr1_drv_data},
413 {/* sentinel */}
414};
415
Faiz Abbasc6eb9e72020-02-26 13:44:33 +0530416static int sdhci_am654_get_otap_delay(struct udevice *dev,
417 struct mmc_config *cfg)
418{
Simon Glassfa20e932020-12-03 16:55:20 -0700419 struct am654_sdhci_plat *plat = dev_get_plat(dev);
Faiz Abbasc6eb9e72020-02-26 13:44:33 +0530420 int ret;
421 int i;
422
423 /* ti,otap-del-sel-legacy is mandatory */
424 ret = dev_read_u32(dev, "ti,otap-del-sel-legacy",
425 &plat->otap_del_sel[0]);
426 if (ret)
427 return ret;
428 /*
429 * Remove the corresponding capability if an otap-del-sel
430 * value is not found
431 */
432 for (i = MMC_HS; i <= MMC_HS_400; i++) {
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530433 ret = dev_read_u32(dev, td[i].otap_binding,
434 &plat->otap_del_sel[i]);
Faiz Abbasc6eb9e72020-02-26 13:44:33 +0530435 if (ret) {
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530436 dev_dbg(dev, "Couldn't find %s\n", td[i].otap_binding);
Faiz Abbasc6eb9e72020-02-26 13:44:33 +0530437 /*
438 * Remove the corresponding capability
439 * if an otap-del-sel value is not found
440 */
441 cfg->host_caps &= ~td[i].capability;
442 }
Faiz Abbasdef2a0f2021-02-04 15:10:51 +0530443
444 if (td[i].itap_binding)
445 dev_read_u32(dev, td[i].itap_binding,
446 &plat->itap_del_sel[i]);
Faiz Abbasc6eb9e72020-02-26 13:44:33 +0530447 }
448
449 return 0;
450}
451
Faiz Abbasd8fb3092019-06-11 00:43:31 +0530452static int am654_sdhci_probe(struct udevice *dev)
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530453{
Faiz Abbas8cc051e2020-01-16 19:42:19 +0530454 struct am654_driver_data *drv_data =
455 (struct am654_driver_data *)dev_get_driver_data(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700456 struct am654_sdhci_plat *plat = dev_get_plat(dev);
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530457 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
458 struct sdhci_host *host = dev_get_priv(dev);
Faiz Abbase9aed582019-06-11 00:43:38 +0530459 struct mmc_config *cfg = &plat->cfg;
Faiz Abbas2c2fc962021-02-04 15:10:50 +0530460 const struct soc_attr *soc;
461 const struct am654_driver_data *soc_drv_data;
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530462 struct clk clk;
463 unsigned long clock;
464 int ret;
465
Faiz Abbasdc2bcc22020-01-16 19:42:18 +0530466 ret = clk_get_by_name(dev, "clk_xin", &clk);
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530467 if (ret) {
468 dev_err(dev, "failed to get clock\n");
469 return ret;
470 }
471
472 clock = clk_get_rate(&clk);
473 if (IS_ERR_VALUE(clock)) {
474 dev_err(dev, "failed to get rate\n");
475 return clock;
476 }
477
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530478 host->max_clk = clock;
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530479 host->mmc = &plat->mmc;
Faiz Abbase9aed582019-06-11 00:43:38 +0530480 host->mmc->dev = dev;
481 ret = sdhci_setup_cfg(cfg, host, cfg->f_max,
482 AM654_SDHCI_MIN_FREQ);
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530483 if (ret)
484 return ret;
Faiz Abbas8cc051e2020-01-16 19:42:19 +0530485
Faiz Abbasc6eb9e72020-02-26 13:44:33 +0530486 ret = sdhci_am654_get_otap_delay(dev, cfg);
487 if (ret)
488 return ret;
489
Faiz Abbas8cc051e2020-01-16 19:42:19 +0530490 host->ops = drv_data->ops;
Faiz Abbas2c2fc962021-02-04 15:10:50 +0530491
492 /* Update ops based on SoC revision */
493 soc = soc_device_match(am654_sdhci_soc_attr);
494 if (soc && soc->data) {
495 soc_drv_data = soc->data;
496 host->ops = soc_drv_data->ops;
497 }
498
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530499 host->mmc->priv = host;
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530500 upriv->mmc = host->mmc;
501
Faiz Abbase9aed582019-06-11 00:43:38 +0530502 regmap_init_mem_index(dev_ofnode(dev), &plat->base, 1);
503
Faiz Abbase4425cb2020-02-26 13:44:34 +0530504 return 0;
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530505}
506
Simon Glassaad29ae2020-12-03 16:55:21 -0700507static int am654_sdhci_of_to_plat(struct udevice *dev)
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530508{
Simon Glassfa20e932020-12-03 16:55:20 -0700509 struct am654_sdhci_plat *plat = dev_get_plat(dev);
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530510 struct sdhci_host *host = dev_get_priv(dev);
Faiz Abbase9aed582019-06-11 00:43:38 +0530511 struct mmc_config *cfg = &plat->cfg;
512 u32 drv_strength;
513 int ret;
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530514
515 host->name = dev->name;
516 host->ioaddr = (void *)dev_read_addr(dev);
Faiz Abbase9aed582019-06-11 00:43:38 +0530517 plat->non_removable = dev_read_bool(dev, "non-removable");
518
Faiz Abbasfd8be702019-06-13 10:29:51 +0530519 if (plat->flags & DLL_PRESENT) {
520 ret = dev_read_u32(dev, "ti,trm-icp", &plat->trm_icp);
521 if (ret)
522 return ret;
523
524 ret = dev_read_u32(dev, "ti,driver-strength-ohm",
525 &drv_strength);
526 if (ret)
527 return ret;
Faiz Abbase9aed582019-06-11 00:43:38 +0530528
Faiz Abbasfd8be702019-06-13 10:29:51 +0530529 switch (drv_strength) {
530 case 50:
531 plat->drv_strength = DRIVER_STRENGTH_50_OHM;
532 break;
533 case 33:
534 plat->drv_strength = DRIVER_STRENGTH_33_OHM;
535 break;
536 case 66:
537 plat->drv_strength = DRIVER_STRENGTH_66_OHM;
538 break;
539 case 100:
540 plat->drv_strength = DRIVER_STRENGTH_100_OHM;
541 break;
542 case 40:
543 plat->drv_strength = DRIVER_STRENGTH_40_OHM;
544 break;
545 default:
546 dev_err(dev, "Invalid driver strength\n");
547 return -EINVAL;
548 }
Faiz Abbase9aed582019-06-11 00:43:38 +0530549 }
550
551 ret = mmc_of_parse(dev, cfg);
552 if (ret)
553 return ret;
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530554
555 return 0;
556}
557
Faiz Abbasd8fb3092019-06-11 00:43:31 +0530558static int am654_sdhci_bind(struct udevice *dev)
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530559{
Faiz Abbas8cc051e2020-01-16 19:42:19 +0530560 struct am654_driver_data *drv_data =
561 (struct am654_driver_data *)dev_get_driver_data(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700562 struct am654_sdhci_plat *plat = dev_get_plat(dev);
Faiz Abbas2c2fc962021-02-04 15:10:50 +0530563 const struct soc_attr *soc;
564 const struct am654_driver_data *soc_drv_data;
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530565
Faiz Abbas8cc051e2020-01-16 19:42:19 +0530566 plat->flags = drv_data->flags;
567
Faiz Abbas2c2fc962021-02-04 15:10:50 +0530568 /* Update flags based on SoC revision */
569 soc = soc_device_match(am654_sdhci_soc_attr);
570 if (soc && soc->data) {
571 soc_drv_data = soc->data;
572 plat->flags = soc_drv_data->flags;
573 }
574
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530575 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
576}
577
Faiz Abbasd8fb3092019-06-11 00:43:31 +0530578static const struct udevice_id am654_sdhci_ids[] = {
Faiz Abbasfd8be702019-06-13 10:29:51 +0530579 {
580 .compatible = "ti,am654-sdhci-5.1",
Faiz Abbas8cc051e2020-01-16 19:42:19 +0530581 .data = (ulong)&am654_drv_data,
Faiz Abbasfd8be702019-06-13 10:29:51 +0530582 },
583 {
584 .compatible = "ti,j721e-sdhci-8bit",
Faiz Abbas8cc051e2020-01-16 19:42:19 +0530585 .data = (ulong)&j721e_8bit_drv_data,
Faiz Abbasfd8be702019-06-13 10:29:51 +0530586 },
587 {
588 .compatible = "ti,j721e-sdhci-4bit",
Faiz Abbas8cc051e2020-01-16 19:42:19 +0530589 .data = (ulong)&j721e_4bit_drv_data,
Faiz Abbasfd8be702019-06-13 10:29:51 +0530590 },
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530591 { }
592};
593
Faiz Abbasd8fb3092019-06-11 00:43:31 +0530594U_BOOT_DRIVER(am654_sdhci_drv) = {
595 .name = "am654_sdhci",
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530596 .id = UCLASS_MMC,
Faiz Abbasd8fb3092019-06-11 00:43:31 +0530597 .of_match = am654_sdhci_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700598 .of_to_plat = am654_sdhci_of_to_plat,
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530599 .ops = &sdhci_ops,
Faiz Abbasd8fb3092019-06-11 00:43:31 +0530600 .bind = am654_sdhci_bind,
601 .probe = am654_sdhci_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700602 .priv_auto = sizeof(struct sdhci_host),
Simon Glass71fa5b42020-12-03 16:55:18 -0700603 .plat_auto = sizeof(struct am654_sdhci_plat),
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530604};