blob: b9a7924d4e2017bae29612427cf5257c794c0f3d [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>
15
Faiz Abbase9aed582019-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 Abbasd8fb3092019-06-11 00:43:31 +053065#define AM654_SDHCI_MIN_FREQ 400000
Lokesh Vutlabc9979f2018-08-27 15:57:54 +053066
Faiz Abbasd8fb3092019-06-11 00:43:31 +053067struct am654_sdhci_plat {
Lokesh Vutlabc9979f2018-08-27 15:57:54 +053068 struct mmc_config cfg;
69 struct mmc mmc;
Faiz Abbase9aed582019-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 Vutlabc9979f2018-08-27 15:57:54 +053076};
77
Faiz Abbase9aed582019-06-11 00:43:38 +053078static int am654_sdhci_set_ios_post(struct sdhci_host *host)
79{
80 struct udevice *dev = host->mmc->dev;
81 struct am654_sdhci_plat *plat = dev_get_platdata(dev);
82 unsigned int speed = host->mmc->clock;
83 int sel50, sel100;
84 u32 mask, val;
85 int ret;
86
87 /* Reset SD Clock Enable */
88 val = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
89 val &= ~SDHCI_CLOCK_CARD_EN;
90 sdhci_writew(host, val, SDHCI_CLOCK_CONTROL);
91
92 /* power off phy */
93 if (plat->dll_on) {
94 regmap_update_bits(plat->base, PHY_CTRL1, ENDLL_MASK, 0);
95
96 plat->dll_on = false;
97 }
98
99 /* restart clock */
100 sdhci_set_clock(host->mmc, speed);
101
102 /* switch phy back on */
103 if (speed > AM654_SDHCI_MIN_FREQ) {
104 mask = OTAPDLYENA_MASK | OTAPDLYSEL_MASK;
105 val = (1 << OTAPDLYENA_SHIFT) |
106 (plat->otap_del_sel << OTAPDLYSEL_SHIFT);
107 regmap_update_bits(plat->base, PHY_CTRL4, mask, val);
108 switch (speed) {
109 case 200000000:
110 sel50 = 0;
111 sel100 = 0;
112 break;
113 case 100000000:
114 sel50 = 0;
115 sel100 = 1;
116 break;
117 default:
118 sel50 = 1;
119 sel100 = 0;
120 }
121
122 /* Configure PHY DLL frequency */
123 mask = SEL50_MASK | SEL100_MASK;
124 val = (sel50 << SEL50_SHIFT) | (sel100 << SEL100_SHIFT);
125 regmap_update_bits(plat->base, PHY_CTRL5, mask, val);
126
127 /* Enable DLL */
128 regmap_update_bits(plat->base, PHY_CTRL1, ENDLL_MASK,
129 0x1 << ENDLL_SHIFT);
130 /*
131 * Poll for DLL ready. Use a one second timeout.
132 * Works in all experiments done so far
133 */
134 ret = regmap_read_poll_timeout(plat->base, PHY_STAT1, val,
135 val & DLLRDY_MASK, 1000, 1000000);
136 if (ret)
137 return ret;
138
139 plat->dll_on = true;
140 }
141
142 return 0;
143}
144
145const struct sdhci_ops am654_sdhci_ops = {
146 .set_ios_post = &am654_sdhci_set_ios_post,
147};
148
149int am654_sdhci_init(struct am654_sdhci_plat *plat)
150{
151 u32 ctl_cfg_2 = 0;
152 u32 mask, val;
153 int ret;
154
155 /* Reset OTAP to default value */
156 mask = OTAPDLYENA_MASK | OTAPDLYSEL_MASK;
157 regmap_update_bits(plat->base, PHY_CTRL4, mask, 0x0);
158
159 regmap_read(plat->base, PHY_STAT1, &val);
160 if (~val & CALDONE_MASK) {
161 /* Calibrate IO lines */
162 regmap_update_bits(plat->base, PHY_CTRL1, PDB_MASK, PDB_MASK);
163 ret = regmap_read_poll_timeout(plat->base, PHY_STAT1, val,
164 val & CALDONE_MASK, 1, 20);
165 if (ret)
166 return ret;
167 }
168
169 /* Configure DLL TRIM */
170 mask = DLL_TRIM_ICP_MASK;
171 val = plat->trm_icp << DLL_TRIM_ICP_SHIFT;
172
173 /* Configure DLL driver strength */
174 mask |= DR_TY_MASK;
175 val |= plat->drv_strength << DR_TY_SHIFT;
176 regmap_update_bits(plat->base, PHY_CTRL1, mask, val);
177
178 /* Enable pins by setting IO mux to 0 */
179 regmap_update_bits(plat->base, PHY_CTRL1, IOMUX_ENABLE_MASK, 0);
180
181 /* Set slot type based on SD or eMMC */
182 if (plat->non_removable)
183 ctl_cfg_2 = SLOTTYPE_EMBEDDED;
184
185 regmap_update_bits(plat->base, CTL_CFG_2, SLOTTYPE_MASK, ctl_cfg_2);
186
187 return 0;
188}
189
Faiz Abbasd8fb3092019-06-11 00:43:31 +0530190static int am654_sdhci_probe(struct udevice *dev)
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530191{
Faiz Abbasd8fb3092019-06-11 00:43:31 +0530192 struct am654_sdhci_plat *plat = dev_get_platdata(dev);
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530193 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
194 struct sdhci_host *host = dev_get_priv(dev);
Faiz Abbase9aed582019-06-11 00:43:38 +0530195 struct mmc_config *cfg = &plat->cfg;
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530196 struct power_domain sdhci_pwrdmn;
197 struct clk clk;
198 unsigned long clock;
199 int ret;
200
201 ret = power_domain_get_by_index(dev, &sdhci_pwrdmn, 0);
202 if (ret) {
203 dev_err(dev, "failed to get power domain\n");
204 return ret;
205 }
206
207 ret = power_domain_on(&sdhci_pwrdmn);
208 if (ret) {
209 dev_err(dev, "Power domain on failed\n");
210 return ret;
211 }
212
213 ret = clk_get_by_index(dev, 0, &clk);
214 if (ret) {
215 dev_err(dev, "failed to get clock\n");
216 return ret;
217 }
218
219 clock = clk_get_rate(&clk);
220 if (IS_ERR_VALUE(clock)) {
221 dev_err(dev, "failed to get rate\n");
222 return clock;
223 }
224
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530225 host->max_clk = clock;
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530226 host->mmc = &plat->mmc;
Faiz Abbase9aed582019-06-11 00:43:38 +0530227 host->mmc->dev = dev;
228 ret = sdhci_setup_cfg(cfg, host, cfg->f_max,
229 AM654_SDHCI_MIN_FREQ);
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530230 if (ret)
231 return ret;
Faiz Abbase9aed582019-06-11 00:43:38 +0530232 host->ops = &am654_sdhci_ops;
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530233 host->mmc->priv = host;
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530234 upriv->mmc = host->mmc;
235
Faiz Abbase9aed582019-06-11 00:43:38 +0530236 regmap_init_mem_index(dev_ofnode(dev), &plat->base, 1);
237
238 am654_sdhci_init(plat);
239
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530240 return sdhci_probe(dev);
241}
242
Faiz Abbasd8fb3092019-06-11 00:43:31 +0530243static int am654_sdhci_ofdata_to_platdata(struct udevice *dev)
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530244{
Faiz Abbasd8fb3092019-06-11 00:43:31 +0530245 struct am654_sdhci_plat *plat = dev_get_platdata(dev);
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530246 struct sdhci_host *host = dev_get_priv(dev);
Faiz Abbase9aed582019-06-11 00:43:38 +0530247 struct mmc_config *cfg = &plat->cfg;
248 u32 drv_strength;
249 int ret;
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530250
251 host->name = dev->name;
252 host->ioaddr = (void *)dev_read_addr(dev);
Faiz Abbase9aed582019-06-11 00:43:38 +0530253 plat->non_removable = dev_read_bool(dev, "non-removable");
254
255 ret = dev_read_u32(dev, "ti,trm-icp", &plat->trm_icp);
256 if (ret)
257 return ret;
258
259 ret = dev_read_u32(dev, "ti,otap-del-sel", &plat->otap_del_sel);
260 if (ret)
261 return ret;
262
263 ret = dev_read_u32(dev, "ti,driver-strength-ohm", &drv_strength);
264 if (ret)
265 return ret;
266
267 switch (drv_strength) {
268 case 50:
269 plat->drv_strength = DRIVER_STRENGTH_50_OHM;
270 break;
271 case 33:
272 plat->drv_strength = DRIVER_STRENGTH_33_OHM;
273 break;
274 case 66:
275 plat->drv_strength = DRIVER_STRENGTH_66_OHM;
276 break;
277 case 100:
278 plat->drv_strength = DRIVER_STRENGTH_100_OHM;
279 break;
280 case 40:
281 plat->drv_strength = DRIVER_STRENGTH_40_OHM;
282 break;
283 default:
284 dev_err(dev, "Invalid driver strength\n");
285 return -EINVAL;
286 }
287
288 ret = mmc_of_parse(dev, cfg);
289 if (ret)
290 return ret;
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530291
292 return 0;
293}
294
Faiz Abbasd8fb3092019-06-11 00:43:31 +0530295static int am654_sdhci_bind(struct udevice *dev)
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530296{
Faiz Abbasd8fb3092019-06-11 00:43:31 +0530297 struct am654_sdhci_plat *plat = dev_get_platdata(dev);
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530298
299 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
300}
301
Faiz Abbasd8fb3092019-06-11 00:43:31 +0530302static const struct udevice_id am654_sdhci_ids[] = {
303 { .compatible = "ti,am654-sdhci-5.1" },
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530304 { }
305};
306
Faiz Abbasd8fb3092019-06-11 00:43:31 +0530307U_BOOT_DRIVER(am654_sdhci_drv) = {
308 .name = "am654_sdhci",
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530309 .id = UCLASS_MMC,
Faiz Abbasd8fb3092019-06-11 00:43:31 +0530310 .of_match = am654_sdhci_ids,
311 .ofdata_to_platdata = am654_sdhci_ofdata_to_platdata,
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530312 .ops = &sdhci_ops,
Faiz Abbasd8fb3092019-06-11 00:43:31 +0530313 .bind = am654_sdhci_bind,
314 .probe = am654_sdhci_probe,
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530315 .priv_auto_alloc_size = sizeof(struct sdhci_host),
Faiz Abbasd8fb3092019-06-11 00:43:31 +0530316 .platdata_auto_alloc_size = sizeof(struct am654_sdhci_plat),
Lokesh Vutlabc9979f2018-08-27 15:57:54 +0530317};