blob: f5ede38c3c175fae3d8c38923b6d5b9ef5989aa5 [file] [log] [blame]
Maksim Kiselev6d6a8902024-12-11 23:10:59 +03001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2024 Maksim Kiselev <bigunclemax@gmail.com>
4 */
5
6#include <clk.h>
7#include <dm.h>
8#include <linux/bitfield.h>
9#include <sdhci.h>
10
11/* DWCMSHC specific Mode Select value */
12#define DWCMSHC_CTRL_HS400 0x7
13/* 400KHz is max freq for card ID etc. Use that as min */
14#define EMMC_MIN_FREQ 400000
15#define SDHCI_TUNING_LOOP_COUNT 128
16
17/* PHY register area pointer */
18#define DWC_MSHC_PTR_PHY_R 0x300
19
20/* PHY general configuration */
21#define PHY_CNFG_R (DWC_MSHC_PTR_PHY_R + 0x00)
22#define PHY_CNFG_RSTN_DEASSERT 0x1 /* Deassert PHY reset */
23#define PHY_CNFG_PAD_SP_MASK GENMASK(19, 16) /* bits [19:16] */
24#define PHY_CNFG_PAD_SP 0x0c /* PMOS TX drive strength */
25#define PHY_CNFG_PAD_SN_MASK GENMASK(23, 20) /* bits [23:20] */
26#define PHY_CNFG_PAD_SN 0x0c /* NMOS TX drive strength */
27
28/* PHY command/response pad settings */
29#define PHY_CMDPAD_CNFG_R (DWC_MSHC_PTR_PHY_R + 0x04)
30
31/* PHY data pad settings */
32#define PHY_DATAPAD_CNFG_R (DWC_MSHC_PTR_PHY_R + 0x06)
33
34/* PHY clock pad settings */
35#define PHY_CLKPAD_CNFG_R (DWC_MSHC_PTR_PHY_R + 0x08)
36
37/* PHY strobe pad settings */
38#define PHY_STBPAD_CNFG_R (DWC_MSHC_PTR_PHY_R + 0x0a)
39
40/* PHY reset pad settings */
41#define PHY_RSTNPAD_CNFG_R (DWC_MSHC_PTR_PHY_R + 0x0c)
42
43/* Bitfields are common for all pad settings */
44#define PHY_PAD_RXSEL_1V8 0x1 /* Receiver type select for 1.8V */
45#define PHY_PAD_RXSEL_3V3 0x2 /* Receiver type select for 3.3V */
46
47#define PHY_PAD_WEAKPULL_MASK GENMASK(4, 3) /* bits [4:3] */
48#define PHY_PAD_WEAKPULL_PULLUP 0x1 /* Weak pull up enabled */
49#define PHY_PAD_WEAKPULL_PULLDOWN 0x2 /* Weak pull down enabled */
50
51#define PHY_PAD_TXSLEW_CTRL_P_MASK GENMASK(8, 5) /* bits [8:5] */
52#define PHY_PAD_TXSLEW_CTRL_P 0x3 /* Slew control for P-Type pad TX */
53#define PHY_PAD_TXSLEW_CTRL_N_MASK GENMASK(12, 9) /* bits [12:9] */
54#define PHY_PAD_TXSLEW_CTRL_N 0x3 /* Slew control for N-Type pad TX */
55
56/* PHY CLK delay line settings */
57#define PHY_SDCLKDL_CNFG_R (DWC_MSHC_PTR_PHY_R + 0x1d)
58#define PHY_SDCLKDL_CNFG_UPDATE BIT(4) /* set before writing to SDCLKDL_DC */
59
60/* PHY CLK delay line delay code */
61#define PHY_SDCLKDL_DC_R (DWC_MSHC_PTR_PHY_R + 0x1e)
62#define PHY_SDCLKDL_DC_INITIAL 0x40 /* initial delay code */
63#define PHY_SDCLKDL_DC_DEFAULT 0x32 /* default delay code */
64#define PHY_SDCLKDL_DC_HS400 0x18 /* delay code for HS400 mode */
65
66/* PHY drift_cclk_rx delay line configuration setting */
67#define PHY_ATDL_CNFG_R (DWC_MSHC_PTR_PHY_R + 0x21)
68#define PHY_ATDL_CNFG_INPSEL_MASK GENMASK(3, 2) /* bits [3:2] */
69#define PHY_ATDL_CNFG_INPSEL 0x3 /* delay line input source */
70
71/* PHY DLL control settings */
72#define PHY_DLL_CTRL_R (DWC_MSHC_PTR_PHY_R + 0x24)
73#define PHY_DLL_CTRL_DISABLE 0x0 /* PHY DLL is enabled */
74#define PHY_DLL_CTRL_ENABLE 0x1 /* PHY DLL is disabled */
75
76/* PHY DLL configuration register 1 */
77#define PHY_DLL_CNFG1_R (DWC_MSHC_PTR_PHY_R + 0x25)
78#define PHY_DLL_CNFG1_SLVDLY_MASK GENMASK(5, 4) /* bits [5:4] */
79#define PHY_DLL_CNFG1_SLVDLY 0x2 /* DLL slave update delay input */
80#define PHY_DLL_CNFG1_WAITCYCLE 0x5 /* DLL wait cycle input */
81
82/* PHY DLL configuration register 2 */
83#define PHY_DLL_CNFG2_R (DWC_MSHC_PTR_PHY_R + 0x26)
84#define PHY_DLL_CNFG2_JUMPSTEP 0xa /* DLL jump step input */
85
86/* PHY DLL master and slave delay line configuration settings */
87#define PHY_DLLDL_CNFG_R (DWC_MSHC_PTR_PHY_R + 0x28)
88#define PHY_DLLDL_CNFG_SLV_INPSEL_MASK GENMASK(6, 5) /* bits [6:5] */
89#define PHY_DLLDL_CNFG_SLV_INPSEL 0x3 /* clock source select for slave DL */
90
91/* Vendor specific Registers */
92#define P_VENDOR_SPECIFIC_AREA 0x500
93
94#define DWCMSHC_EMMC_CONTROL 0x2c
95#define DWCMSHC_CARD_IS_EMMC BIT(0)
96#define DWCMSHC_ENHANCED_STROBE BIT(8)
97#define DWCMSHC_EMMC_ATCTRL 0x40
98/* Tuning and auto-tuning fields in AT_CTRL_R control register */
99#define AT_CTRL_AT_EN BIT(0) /* autotuning is enabled */
100#define AT_CTRL_CI_SEL BIT(1) /* interval to drive center phase select */
101#define AT_CTRL_SWIN_TH_EN BIT(2) /* sampling window threshold enable */
102#define AT_CTRL_RPT_TUNE_ERR BIT(3) /* enable reporting framing errors */
103#define AT_CTRL_SW_TUNE_EN BIT(4) /* enable software managed tuning */
104#define AT_CTRL_WIN_EDGE_SEL_MASK GENMASK(11, 8) /* bits [11:8] */
105#define AT_CTRL_WIN_EDGE_SEL 0xf /* sampling window edge select */
106#define AT_CTRL_TUNE_CLK_STOP_EN BIT(16) /* clocks stopped during phase code change */
107#define AT_CTRL_PRE_CHANGE_DLY_MASK GENMASK(18, 17) /* bits [18:17] */
108#define AT_CTRL_PRE_CHANGE_DLY 0x1 /* 2-cycle latency */
109#define AT_CTRL_POST_CHANGE_DLY_MASK GENMASK(20, 19) /* bits [20:19] */
110#define AT_CTRL_POST_CHANGE_DLY 0x3 /* 4-cycle latency */
111#define AT_CTRL_SWIN_TH_VAL_MASK GENMASK(31, 24) /* bits [31:24] */
112#define AT_CTRL_SWIN_TH_VAL 0x9 /* sampling window threshold */
113
114#define FLAG_IO_FIXED_1V8 BIT(0)
115
116#define BOUNDARY_OK(addr, len) \
117 (((addr) | (SZ_128M - 1)) == (((addr) + (len) - 1) | (SZ_128M - 1)))
118
119struct snps_sdhci_plat {
120 struct mmc_config cfg;
121 struct mmc mmc;
122 u16 delay_line;
123 u16 flags;
124};
125
126/*
127 * If DMA addr spans 128MB boundary, we split the DMA transfer into two
128 * so that each DMA transfer doesn't exceed the boundary.
129 */
130void snps_sdhci_adma_write_desc(struct sdhci_host *host, void **desc,
131 dma_addr_t addr, int len, bool end)
132{
133 int tmplen, offset;
134
135 if (likely(!len || BOUNDARY_OK(addr, len))) {
136 sdhci_adma_write_desc(host, desc, addr, len, end);
137 return;
138 }
139
140 offset = addr & (SZ_128M - 1);
141 tmplen = SZ_128M - offset;
142 sdhci_adma_write_desc(host, desc, addr, tmplen, false);
143
144 addr += tmplen;
145 len -= tmplen;
146 sdhci_adma_write_desc(host, desc, addr, len, end);
147}
148
149static void snps_sdhci_set_phy(struct sdhci_host *host)
150{
151 struct snps_sdhci_plat *plat = dev_get_plat(host->mmc->dev);
152 u32 rxsel = PHY_PAD_RXSEL_3V3;
153 u32 val;
154
155 if (plat->flags & FLAG_IO_FIXED_1V8 ||
156 host->mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180)
157 rxsel = PHY_PAD_RXSEL_1V8;
158
159 /* deassert phy reset & set tx drive strength */
160 val = PHY_CNFG_RSTN_DEASSERT;
161 val |= FIELD_PREP(PHY_CNFG_PAD_SP_MASK, PHY_CNFG_PAD_SP);
162 val |= FIELD_PREP(PHY_CNFG_PAD_SN_MASK, PHY_CNFG_PAD_SN);
163 sdhci_writel(host, val, PHY_CNFG_R);
164
165 /* disable delay line */
166 sdhci_writeb(host, PHY_SDCLKDL_CNFG_UPDATE, PHY_SDCLKDL_CNFG_R);
167
168 /* set delay line */
169 sdhci_writeb(host, plat->delay_line, PHY_SDCLKDL_DC_R);
170 sdhci_writeb(host, PHY_DLL_CNFG2_JUMPSTEP, PHY_DLL_CNFG2_R);
171
172 /* enable delay lane */
173 val = sdhci_readb(host, PHY_SDCLKDL_CNFG_R);
174 val &= ~(PHY_SDCLKDL_CNFG_UPDATE);
175 sdhci_writeb(host, val, PHY_SDCLKDL_CNFG_R);
176
177 /* configure phy pads */
178 val = rxsel;
179 val |= FIELD_PREP(PHY_PAD_WEAKPULL_MASK, PHY_PAD_WEAKPULL_PULLUP);
180 val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_P_MASK, PHY_PAD_TXSLEW_CTRL_P);
181 val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_N_MASK, PHY_PAD_TXSLEW_CTRL_N);
182 sdhci_writew(host, val, PHY_CMDPAD_CNFG_R);
183 sdhci_writew(host, val, PHY_DATAPAD_CNFG_R);
184 sdhci_writew(host, val, PHY_RSTNPAD_CNFG_R);
185
186 val = FIELD_PREP(PHY_PAD_TXSLEW_CTRL_P_MASK, PHY_PAD_TXSLEW_CTRL_P);
187 val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_N_MASK, PHY_PAD_TXSLEW_CTRL_N);
188 sdhci_writew(host, val, PHY_CLKPAD_CNFG_R);
189
190 val = rxsel;
191 val |= FIELD_PREP(PHY_PAD_WEAKPULL_MASK, PHY_PAD_WEAKPULL_PULLDOWN);
192 val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_P_MASK, PHY_PAD_TXSLEW_CTRL_P);
193 val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_N_MASK, PHY_PAD_TXSLEW_CTRL_N);
194 sdhci_writew(host, val, PHY_STBPAD_CNFG_R);
195
196 /* enable data strobe mode */
197 if (plat->flags & FLAG_IO_FIXED_1V8 ||
198 host->mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180) {
199 u8 sel = FIELD_PREP(PHY_DLLDL_CNFG_SLV_INPSEL_MASK, PHY_DLLDL_CNFG_SLV_INPSEL);
200
201 sdhci_writeb(host, sel, PHY_DLLDL_CNFG_R);
202 }
203
204 /* enable phy dll */
205 sdhci_writeb(host, PHY_DLL_CTRL_ENABLE, PHY_DLL_CTRL_R);
206
207 sdhci_writeb(host, FIELD_PREP(PHY_DLL_CNFG1_SLVDLY_MASK, PHY_DLL_CNFG1_SLVDLY) |
208 PHY_DLL_CNFG1_WAITCYCLE, PHY_DLL_CNFG1_R);
209}
210
211static int snps_sdhci_set_ios_post(struct sdhci_host *host)
212{
213 struct snps_sdhci_plat *plat = dev_get_plat(host->mmc->dev);
214 struct mmc *mmc = host->mmc;
215 u32 reg;
216
217 reg = sdhci_readw(host, SDHCI_HOST_CONTROL2);
218 reg &= ~SDHCI_CTRL_UHS_MASK;
219
220 switch (mmc->selected_mode) {
221 case UHS_SDR50:
222 case MMC_HS_52:
223 reg |= SDHCI_CTRL_UHS_SDR50;
224 break;
225 case UHS_DDR50:
226 case MMC_DDR_52:
227 reg |= SDHCI_CTRL_UHS_DDR50;
228 break;
229 case UHS_SDR104:
230 case MMC_HS_200:
231 reg |= SDHCI_CTRL_UHS_SDR104;
232 break;
233 case MMC_HS_400:
234 case MMC_HS_400_ES:
235 reg |= DWCMSHC_CTRL_HS400;
236 break;
237 default:
238 reg |= SDHCI_CTRL_UHS_SDR12;
239 }
240
241 if ((plat->flags & FLAG_IO_FIXED_1V8) ||
242 mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180)
243 reg |= SDHCI_CTRL_VDD_180;
244 else
245 reg &= ~SDHCI_CTRL_VDD_180;
246
247 sdhci_writew(host, reg, SDHCI_HOST_CONTROL2);
248
249 reg = sdhci_readw(host, P_VENDOR_SPECIFIC_AREA + DWCMSHC_EMMC_CONTROL);
250
251 if (IS_MMC(mmc))
252 reg |= DWCMSHC_CARD_IS_EMMC;
253 else
254 reg &= ~DWCMSHC_CARD_IS_EMMC;
255
256 if (mmc->selected_mode == MMC_HS_400_ES)
257 reg |= DWCMSHC_ENHANCED_STROBE;
258 else
259 reg &= ~DWCMSHC_ENHANCED_STROBE;
260
261 sdhci_writeb(host, reg, P_VENDOR_SPECIFIC_AREA + DWCMSHC_EMMC_CONTROL);
262
263 if (mmc->selected_mode == MMC_HS_400 ||
264 mmc->selected_mode == MMC_HS_400_ES)
265 plat->delay_line = PHY_SDCLKDL_DC_HS400;
266 else
267 sdhci_writeb(host, 0, PHY_DLLDL_CNFG_R);
268
269 snps_sdhci_set_phy(host);
270
271 return 0;
272}
273
274static int snps_sdhci_execute_tuning(struct mmc *mmc, u8 opcode)
275{
276 struct sdhci_host *host = dev_get_priv(mmc->dev);
277 char tuning_loop_counter = SDHCI_TUNING_LOOP_COUNT;
278 struct mmc_cmd cmd;
279 u32 ctrl, blk_size, val;
280 int ret;
281
282 sdhci_writeb(host, FIELD_PREP(PHY_ATDL_CNFG_INPSEL_MASK, PHY_ATDL_CNFG_INPSEL),
283 PHY_ATDL_CNFG_R);
284 val = sdhci_readl(host, P_VENDOR_SPECIFIC_AREA + DWCMSHC_EMMC_ATCTRL);
285
286 /*
287 * configure tuning settings:
288 * - center phase select code driven in block gap interval
289 * - disable reporting of framing errors
290 * - disable software managed tuning
291 * - disable user selection of sampling window edges,
292 * instead tuning calculated edges are used
293 */
294 val &= ~(AT_CTRL_CI_SEL | AT_CTRL_RPT_TUNE_ERR | AT_CTRL_SW_TUNE_EN |
295 FIELD_PREP(AT_CTRL_WIN_EDGE_SEL_MASK, AT_CTRL_WIN_EDGE_SEL));
296
297 /*
298 * configure tuning settings:
299 * - enable auto-tuning
300 * - enable sampling window threshold
301 * - stop clocks during phase code change
302 * - set max latency in cycles between tx and rx clocks
303 * - set max latency in cycles to switch output phase
304 * - set max sampling window threshold value
305 */
306 val |= AT_CTRL_AT_EN | AT_CTRL_SWIN_TH_EN | AT_CTRL_TUNE_CLK_STOP_EN;
307 val |= FIELD_PREP(AT_CTRL_PRE_CHANGE_DLY_MASK, AT_CTRL_PRE_CHANGE_DLY);
308 val |= FIELD_PREP(AT_CTRL_POST_CHANGE_DLY_MASK, AT_CTRL_POST_CHANGE_DLY);
309 val |= FIELD_PREP(AT_CTRL_SWIN_TH_VAL_MASK, AT_CTRL_SWIN_TH_VAL);
310
311 sdhci_writel(host, val, P_VENDOR_SPECIFIC_AREA + DWCMSHC_EMMC_ATCTRL);
312 val = sdhci_readl(host, P_VENDOR_SPECIFIC_AREA + DWCMSHC_EMMC_ATCTRL);
313
314 /* perform tuning */
315 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
316 ctrl |= SDHCI_CTRL_EXEC_TUNING;
317 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
318
319 blk_size = SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG, 64);
320 if (opcode == MMC_CMD_SEND_TUNING_BLOCK_HS200 && mmc->bus_width == 8)
321 blk_size = SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG, 128);
322 sdhci_writew(host, blk_size, SDHCI_BLOCK_SIZE);
323 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
324
325 cmd.cmdidx = opcode;
326 cmd.resp_type = MMC_RSP_R1;
327 cmd.cmdarg = 0;
328
329 do {
330 ret = mmc_send_cmd(mmc, &cmd, NULL);
331 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
332 if (ret || tuning_loop_counter-- == 0)
333 break;
334
335 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
336
337 if (ret || tuning_loop_counter < 0 || !(ctrl & SDHCI_CTRL_TUNED_CLK)) {
338 if (!ret)
339 ret = -EIO;
340 printf("%s: Tuning failed: %d\n", __func__, ret);
341
342 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
343 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
344 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
345 }
346
347 return ret;
348}
349
350static int snps_sdhci_set_enhanced_strobe(struct sdhci_host *host)
351{
352 return 0;
353}
354
355static const struct sdhci_ops snps_sdhci_ops = {
356 .set_ios_post = snps_sdhci_set_ios_post,
357 .platform_execute_tuning = snps_sdhci_execute_tuning,
358 .set_enhanced_strobe = snps_sdhci_set_enhanced_strobe,
359#if CONFIG_IS_ENABLED(MMC_SDHCI_ADMA_HELPERS)
360 .adma_write_desc = snps_sdhci_adma_write_desc,
361#endif
362};
363
364static int snps_sdhci_probe(struct udevice *dev)
365{
366 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
367 struct snps_sdhci_plat *plat = dev_get_plat(dev);
368 struct mmc_config *cfg = &plat->cfg;
369 struct sdhci_host *host = dev_get_priv(dev);
370 struct clk clk;
371 int ret;
372
373 plat->delay_line = PHY_SDCLKDL_DC_DEFAULT;
374
375 ret = clk_get_by_name(dev, "core", &clk);
376 if (ret)
377 return ret;
378
379 ret = clk_prepare_enable(&clk);
380 if (ret)
381 return ret;
382
383 host->max_clk = clk_get_rate(&clk);
384
385 host->ops = &snps_sdhci_ops;
386
387 host->mmc = &plat->mmc;
388 host->mmc->priv = host;
389 host->mmc->dev = dev;
390 upriv->mmc = host->mmc;
391
392 ret = sdhci_setup_cfg(cfg, host, cfg->f_max, EMMC_MIN_FREQ);
393 if (ret)
394 return ret;
395
396 if ((dev_read_bool(dev, "mmc-ddr-1_8v")) ||
397 (dev_read_bool(dev, "mmc-hs200-1_8v")) ||
398 (dev_read_bool(dev, "mmc-hs400-1_8v")))
399 plat->flags |= FLAG_IO_FIXED_1V8;
400 else
401 plat->flags &= ~FLAG_IO_FIXED_1V8;
402
403 return sdhci_probe(dev);
404}
405
406static int snps_sdhci_of_to_plat(struct udevice *dev)
407{
408 struct snps_sdhci_plat *plat = dev_get_plat(dev);
409 struct mmc_config *cfg = &plat->cfg;
410 struct sdhci_host *host = dev_get_priv(dev);
411 int ret;
412
413 host->name = dev->name;
414 host->ioaddr = dev_read_addr_ptr(dev);
415
416 ret = mmc_of_parse(dev, cfg);
417 if (ret)
418 return ret;
419
420 return 0;
421}
422
423static int snps_sdhci_bind(struct udevice *dev)
424{
425 struct snps_sdhci_plat *plat = dev_get_plat(dev);
426
427 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
428}
429
430static const struct udevice_id snps_sdhci_ids[] = {
431 { .compatible = "thead,th1520-dwcmshc" }
432};
433
434U_BOOT_DRIVER(snps_sdhci_drv) = {
435 .name = "snps_sdhci",
436 .id = UCLASS_MMC,
437 .of_match = snps_sdhci_ids,
438 .of_to_plat = snps_sdhci_of_to_plat,
439 .ops = &sdhci_ops,
440 .bind = snps_sdhci_bind,
441 .probe = snps_sdhci_probe,
442 .priv_auto = sizeof(struct sdhci_host),
443 .plat_auto = sizeof(struct snps_sdhci_plat),
444};