blob: fb0fb580706c9cf5bcdcf395f0764514cb3c07d6 [file] [log] [blame]
Lokesh Vutlaaaa449f2018-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 Abbasce142ff2019-06-11 00:43:38 +053013#include <regmap.h>
Lokesh Vutlaaaa449f2018-08-27 15:57:54 +053014#include <sdhci.h>
15
Faiz Abbasce142ff2019-06-11 00:43:38 +053016/* CTL_CFG Registers */
17#define CTL_CFG_2 0x14
18
19#define SLOTTYPE_MASK GENMASK(31, 30)
20#define SLOTTYPE_EMBEDDED BIT(30)
21
22/* PHY Registers */
23#define PHY_CTRL1 0x100
24#define PHY_CTRL2 0x104
25#define PHY_CTRL3 0x108
26#define PHY_CTRL4 0x10C
27#define PHY_CTRL5 0x110
28#define PHY_CTRL6 0x114
29#define PHY_STAT1 0x130
30#define PHY_STAT2 0x134
31
32#define IOMUX_ENABLE_SHIFT 31
33#define IOMUX_ENABLE_MASK BIT(IOMUX_ENABLE_SHIFT)
34#define OTAPDLYENA_SHIFT 20
35#define OTAPDLYENA_MASK BIT(OTAPDLYENA_SHIFT)
36#define OTAPDLYSEL_SHIFT 12
37#define OTAPDLYSEL_MASK GENMASK(15, 12)
38#define STRBSEL_SHIFT 24
39#define STRBSEL_MASK GENMASK(27, 24)
40#define SEL50_SHIFT 8
41#define SEL50_MASK BIT(SEL50_SHIFT)
42#define SEL100_SHIFT 9
43#define SEL100_MASK BIT(SEL100_SHIFT)
44#define DLL_TRIM_ICP_SHIFT 4
45#define DLL_TRIM_ICP_MASK GENMASK(7, 4)
46#define DR_TY_SHIFT 20
47#define DR_TY_MASK GENMASK(22, 20)
48#define ENDLL_SHIFT 1
49#define ENDLL_MASK BIT(ENDLL_SHIFT)
50#define DLLRDY_SHIFT 0
51#define DLLRDY_MASK BIT(DLLRDY_SHIFT)
52#define PDB_SHIFT 0
53#define PDB_MASK BIT(PDB_SHIFT)
54#define CALDONE_SHIFT 1
55#define CALDONE_MASK BIT(CALDONE_SHIFT)
56#define RETRIM_SHIFT 17
57#define RETRIM_MASK BIT(RETRIM_SHIFT)
58
59#define DRIVER_STRENGTH_50_OHM 0x0
60#define DRIVER_STRENGTH_33_OHM 0x1
61#define DRIVER_STRENGTH_66_OHM 0x2
62#define DRIVER_STRENGTH_100_OHM 0x3
63#define DRIVER_STRENGTH_40_OHM 0x4
64
Faiz Abbas3a1a0df2019-06-11 00:43:31 +053065#define AM654_SDHCI_MIN_FREQ 400000
Lokesh Vutlaaaa449f2018-08-27 15:57:54 +053066
Faiz Abbas3a1a0df2019-06-11 00:43:31 +053067struct am654_sdhci_plat {
Lokesh Vutlaaaa449f2018-08-27 15:57:54 +053068 struct mmc_config cfg;
69 struct mmc mmc;
Faiz Abbasce142ff2019-06-11 00:43:38 +053070 struct regmap *base;
71 bool non_removable;
72 u32 otap_del_sel;
73 u32 trm_icp;
74 u32 drv_strength;
75 bool dll_on;
Lokesh Vutlaaaa449f2018-08-27 15:57:54 +053076};
77
Faiz Abbasf6058072019-06-11 00:43:41 +053078static void am654_sdhci_set_control_reg(struct sdhci_host *host)
79{
80 struct mmc *mmc = (struct mmc *)host->mmc;
81 u32 reg;
82
83 if (IS_SD(host->mmc) &&
84 mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180) {
85 reg = sdhci_readw(host, SDHCI_HOST_CONTROL2);
86 reg |= SDHCI_CTRL_VDD_180;
87 sdhci_writew(host, reg, SDHCI_HOST_CONTROL2);
88 }
89
90 sdhci_set_uhs_timing(host);
91}
92
Faiz Abbasce142ff2019-06-11 00:43:38 +053093static int am654_sdhci_set_ios_post(struct sdhci_host *host)
94{
95 struct udevice *dev = host->mmc->dev;
96 struct am654_sdhci_plat *plat = dev_get_platdata(dev);
97 unsigned int speed = host->mmc->clock;
98 int sel50, sel100;
99 u32 mask, val;
100 int ret;
101
102 /* Reset SD Clock Enable */
103 val = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
104 val &= ~SDHCI_CLOCK_CARD_EN;
105 sdhci_writew(host, val, SDHCI_CLOCK_CONTROL);
106
107 /* power off phy */
108 if (plat->dll_on) {
109 regmap_update_bits(plat->base, PHY_CTRL1, ENDLL_MASK, 0);
110
111 plat->dll_on = false;
112 }
113
114 /* restart clock */
115 sdhci_set_clock(host->mmc, speed);
116
117 /* switch phy back on */
118 if (speed > AM654_SDHCI_MIN_FREQ) {
119 mask = OTAPDLYENA_MASK | OTAPDLYSEL_MASK;
120 val = (1 << OTAPDLYENA_SHIFT) |
121 (plat->otap_del_sel << OTAPDLYSEL_SHIFT);
122 regmap_update_bits(plat->base, PHY_CTRL4, mask, val);
123 switch (speed) {
124 case 200000000:
125 sel50 = 0;
126 sel100 = 0;
127 break;
128 case 100000000:
129 sel50 = 0;
130 sel100 = 1;
131 break;
132 default:
133 sel50 = 1;
134 sel100 = 0;
135 }
136
137 /* Configure PHY DLL frequency */
138 mask = SEL50_MASK | SEL100_MASK;
139 val = (sel50 << SEL50_SHIFT) | (sel100 << SEL100_SHIFT);
140 regmap_update_bits(plat->base, PHY_CTRL5, mask, val);
141
142 /* Enable DLL */
143 regmap_update_bits(plat->base, PHY_CTRL1, ENDLL_MASK,
144 0x1 << ENDLL_SHIFT);
145 /*
146 * Poll for DLL ready. Use a one second timeout.
147 * Works in all experiments done so far
148 */
149 ret = regmap_read_poll_timeout(plat->base, PHY_STAT1, val,
150 val & DLLRDY_MASK, 1000, 1000000);
151 if (ret)
152 return ret;
153
154 plat->dll_on = true;
155 }
156
157 return 0;
158}
159
160const struct sdhci_ops am654_sdhci_ops = {
Faiz Abbasf6058072019-06-11 00:43:41 +0530161 .set_ios_post = &am654_sdhci_set_ios_post,
162 .set_control_reg = &am654_sdhci_set_control_reg,
Faiz Abbasce142ff2019-06-11 00:43:38 +0530163};
164
165int am654_sdhci_init(struct am654_sdhci_plat *plat)
166{
167 u32 ctl_cfg_2 = 0;
168 u32 mask, val;
169 int ret;
170
171 /* Reset OTAP to default value */
172 mask = OTAPDLYENA_MASK | OTAPDLYSEL_MASK;
173 regmap_update_bits(plat->base, PHY_CTRL4, mask, 0x0);
174
175 regmap_read(plat->base, PHY_STAT1, &val);
176 if (~val & CALDONE_MASK) {
177 /* Calibrate IO lines */
178 regmap_update_bits(plat->base, PHY_CTRL1, PDB_MASK, PDB_MASK);
179 ret = regmap_read_poll_timeout(plat->base, PHY_STAT1, val,
180 val & CALDONE_MASK, 1, 20);
181 if (ret)
182 return ret;
183 }
184
185 /* Configure DLL TRIM */
186 mask = DLL_TRIM_ICP_MASK;
187 val = plat->trm_icp << DLL_TRIM_ICP_SHIFT;
188
189 /* Configure DLL driver strength */
190 mask |= DR_TY_MASK;
191 val |= plat->drv_strength << DR_TY_SHIFT;
192 regmap_update_bits(plat->base, PHY_CTRL1, mask, val);
193
194 /* Enable pins by setting IO mux to 0 */
195 regmap_update_bits(plat->base, PHY_CTRL1, IOMUX_ENABLE_MASK, 0);
196
197 /* Set slot type based on SD or eMMC */
198 if (plat->non_removable)
199 ctl_cfg_2 = SLOTTYPE_EMBEDDED;
200
201 regmap_update_bits(plat->base, CTL_CFG_2, SLOTTYPE_MASK, ctl_cfg_2);
202
203 return 0;
204}
205
Faiz Abbas3a1a0df2019-06-11 00:43:31 +0530206static int am654_sdhci_probe(struct udevice *dev)
Lokesh Vutlaaaa449f2018-08-27 15:57:54 +0530207{
Faiz Abbas3a1a0df2019-06-11 00:43:31 +0530208 struct am654_sdhci_plat *plat = dev_get_platdata(dev);
Lokesh Vutlaaaa449f2018-08-27 15:57:54 +0530209 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
210 struct sdhci_host *host = dev_get_priv(dev);
Faiz Abbasce142ff2019-06-11 00:43:38 +0530211 struct mmc_config *cfg = &plat->cfg;
Lokesh Vutlaaaa449f2018-08-27 15:57:54 +0530212 struct power_domain sdhci_pwrdmn;
213 struct clk clk;
214 unsigned long clock;
215 int ret;
216
217 ret = power_domain_get_by_index(dev, &sdhci_pwrdmn, 0);
Andreas Dannenberg70942db2019-06-04 17:55:44 -0500218 if (!ret) {
219 ret = power_domain_on(&sdhci_pwrdmn);
220 if (ret) {
221 dev_err(dev, "Power domain on failed (%d)\n", ret);
222 return ret;
223 }
224 } else if (ret != -ENOENT && ret != -ENODEV && ret != -ENOSYS) {
225 dev_err(dev, "failed to get power domain (%d)\n", ret);
Lokesh Vutlaaaa449f2018-08-27 15:57:54 +0530226 return ret;
227 }
228
229 ret = clk_get_by_index(dev, 0, &clk);
230 if (ret) {
231 dev_err(dev, "failed to get clock\n");
232 return ret;
233 }
234
235 clock = clk_get_rate(&clk);
236 if (IS_ERR_VALUE(clock)) {
237 dev_err(dev, "failed to get rate\n");
238 return clock;
239 }
240
Lokesh Vutlaaaa449f2018-08-27 15:57:54 +0530241 host->max_clk = clock;
Lokesh Vutlaaaa449f2018-08-27 15:57:54 +0530242 host->mmc = &plat->mmc;
Faiz Abbasce142ff2019-06-11 00:43:38 +0530243 host->mmc->dev = dev;
244 ret = sdhci_setup_cfg(cfg, host, cfg->f_max,
245 AM654_SDHCI_MIN_FREQ);
Lokesh Vutlaaaa449f2018-08-27 15:57:54 +0530246 if (ret)
247 return ret;
Faiz Abbasce142ff2019-06-11 00:43:38 +0530248 host->ops = &am654_sdhci_ops;
Lokesh Vutlaaaa449f2018-08-27 15:57:54 +0530249 host->mmc->priv = host;
Lokesh Vutlaaaa449f2018-08-27 15:57:54 +0530250 upriv->mmc = host->mmc;
251
Faiz Abbasce142ff2019-06-11 00:43:38 +0530252 regmap_init_mem_index(dev_ofnode(dev), &plat->base, 1);
253
254 am654_sdhci_init(plat);
255
Lokesh Vutlaaaa449f2018-08-27 15:57:54 +0530256 return sdhci_probe(dev);
257}
258
Faiz Abbas3a1a0df2019-06-11 00:43:31 +0530259static int am654_sdhci_ofdata_to_platdata(struct udevice *dev)
Lokesh Vutlaaaa449f2018-08-27 15:57:54 +0530260{
Faiz Abbas3a1a0df2019-06-11 00:43:31 +0530261 struct am654_sdhci_plat *plat = dev_get_platdata(dev);
Lokesh Vutlaaaa449f2018-08-27 15:57:54 +0530262 struct sdhci_host *host = dev_get_priv(dev);
Faiz Abbasce142ff2019-06-11 00:43:38 +0530263 struct mmc_config *cfg = &plat->cfg;
264 u32 drv_strength;
265 int ret;
Lokesh Vutlaaaa449f2018-08-27 15:57:54 +0530266
267 host->name = dev->name;
268 host->ioaddr = (void *)dev_read_addr(dev);
Faiz Abbasce142ff2019-06-11 00:43:38 +0530269 plat->non_removable = dev_read_bool(dev, "non-removable");
270
271 ret = dev_read_u32(dev, "ti,trm-icp", &plat->trm_icp);
272 if (ret)
273 return ret;
274
275 ret = dev_read_u32(dev, "ti,otap-del-sel", &plat->otap_del_sel);
276 if (ret)
277 return ret;
278
279 ret = dev_read_u32(dev, "ti,driver-strength-ohm", &drv_strength);
280 if (ret)
281 return ret;
282
283 switch (drv_strength) {
284 case 50:
285 plat->drv_strength = DRIVER_STRENGTH_50_OHM;
286 break;
287 case 33:
288 plat->drv_strength = DRIVER_STRENGTH_33_OHM;
289 break;
290 case 66:
291 plat->drv_strength = DRIVER_STRENGTH_66_OHM;
292 break;
293 case 100:
294 plat->drv_strength = DRIVER_STRENGTH_100_OHM;
295 break;
296 case 40:
297 plat->drv_strength = DRIVER_STRENGTH_40_OHM;
298 break;
299 default:
300 dev_err(dev, "Invalid driver strength\n");
301 return -EINVAL;
302 }
303
304 ret = mmc_of_parse(dev, cfg);
305 if (ret)
306 return ret;
Lokesh Vutlaaaa449f2018-08-27 15:57:54 +0530307
308 return 0;
309}
310
Faiz Abbas3a1a0df2019-06-11 00:43:31 +0530311static int am654_sdhci_bind(struct udevice *dev)
Lokesh Vutlaaaa449f2018-08-27 15:57:54 +0530312{
Faiz Abbas3a1a0df2019-06-11 00:43:31 +0530313 struct am654_sdhci_plat *plat = dev_get_platdata(dev);
Lokesh Vutlaaaa449f2018-08-27 15:57:54 +0530314
315 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
316}
317
Faiz Abbas3a1a0df2019-06-11 00:43:31 +0530318static const struct udevice_id am654_sdhci_ids[] = {
319 { .compatible = "ti,am654-sdhci-5.1" },
Lokesh Vutlaaaa449f2018-08-27 15:57:54 +0530320 { }
321};
322
Faiz Abbas3a1a0df2019-06-11 00:43:31 +0530323U_BOOT_DRIVER(am654_sdhci_drv) = {
324 .name = "am654_sdhci",
Lokesh Vutlaaaa449f2018-08-27 15:57:54 +0530325 .id = UCLASS_MMC,
Faiz Abbas3a1a0df2019-06-11 00:43:31 +0530326 .of_match = am654_sdhci_ids,
327 .ofdata_to_platdata = am654_sdhci_ofdata_to_platdata,
Lokesh Vutlaaaa449f2018-08-27 15:57:54 +0530328 .ops = &sdhci_ops,
Faiz Abbas3a1a0df2019-06-11 00:43:31 +0530329 .bind = am654_sdhci_bind,
330 .probe = am654_sdhci_probe,
Lokesh Vutlaaaa449f2018-08-27 15:57:54 +0530331 .priv_auto_alloc_size = sizeof(struct sdhci_host),
Faiz Abbas3a1a0df2019-06-11 00:43:31 +0530332 .platdata_auto_alloc_size = sizeof(struct am654_sdhci_plat),
Lokesh Vutlaaaa449f2018-08-27 15:57:54 +0530333};