1 // SPDX-License-Identifier: GPL-2.0+
3 * Texas Instruments' AM654 DDRSS driver
5 * Copyright (C) 2018 Texas Instruments Incorporated - http://www.ti.com/
6 * Lokesh Vutla <lokeshvutla@ti.com>
14 #include <power-domain.h>
16 #include <asm/arch/sys_proto.h>
17 #include <power/regulator.h>
18 #include "k3-am654-ddrss.h"
22 /* DDRSS PHY configuration register fixed values */
23 #define DDRSS_DDRPHY_RANKIDR_RANK0 0
26 * struct am654_ddrss_desc - Description of ddrss integration.
27 * @dev: DDRSS device pointer
28 * @ddrss_ss_cfg: DDRSS wrapper logic region base address
29 * @ddrss_ctl_cfg: DDRSS controller region base address
30 * @ddrss_phy_cfg: DDRSS PHY region base address
31 * @ddrss_clk: DDRSS clock description
32 * @vtt_supply: VTT Supply regulator
33 * @ddrss_pwrdmn: DDRSS power domain description
34 * @params: SDRAM configuration parameters
36 struct am654_ddrss_desc {
38 void __iomem *ddrss_ss_cfg;
39 void __iomem *ddrss_ctl_cfg;
40 void __iomem *ddrss_phy_cfg;
42 struct udevice *vtt_supply;
43 struct power_domain ddrcfg_pwrdmn;
44 struct power_domain ddrdata_pwrdmn;
45 struct ddrss_params params;
48 static inline u32 ddrss_readl(void __iomem *addr, unsigned int offset)
50 return readl(addr + offset);
53 static inline void ddrss_writel(void __iomem *addr, unsigned int offset,
56 debug("%s: addr = 0x%p, value = 0x%x\n", __func__, addr + offset, data);
57 writel(data, addr + offset);
60 #define ddrss_ctl_writel(off, val) ddrss_writel(ddrss->ddrss_ctl_cfg, off, val)
61 #define ddrss_ctl_readl(off) ddrss_readl(ddrss->ddrss_ctl_cfg, off)
63 static inline u32 am654_ddrss_get_type(struct am654_ddrss_desc *ddrss)
65 return ddrss_ctl_readl(DDRSS_DDRCTL_MSTR) & MSTR_DDR_TYPE_MASK;
69 * am654_ddrss_dram_wait_for_init_complete() - Wait for init to complete
71 * After detecting the DDR type this function will pause until the
72 * initialization is complete. Each DDR type has mask of multiple bits.
73 * The size of the field depends on the DDR Type. If the initialization
74 * does not complete and error will be returned and will cause the boot to halt.
77 static int am654_ddrss_dram_wait_for_init_complt(struct am654_ddrss_desc *ddrss)
81 val = am654_ddrss_get_type(ddrss);
86 mask = DDR4_STAT_MODE_MASK;
89 mask = DDR3_STAT_MODE_MASK;
92 printf("Unsupported DDR type 0x%x\n", val);
96 if (!wait_on_value(mask, DDR_MODE_NORMAL,
97 ddrss->ddrss_ctl_cfg + DDRSS_DDRCTL_STAT, LDELAY))
104 * am654_ddrss_ctrl_configuration() - Configure Controller specific registers
105 * @dev: corresponding ddrss device
107 static void am654_ddrss_ctrl_configuration(struct am654_ddrss_desc *ddrss)
109 struct ddrss_ddrctl_timing_params *tmg = &ddrss->params.ctl_timing;
110 struct ddrss_ddrctl_reg_params *reg = &ddrss->params.ctl_reg;
111 struct ddrss_ddrctl_ecc_params *ecc = &ddrss->params.ctl_ecc;
112 struct ddrss_ddrctl_crc_params *crc = &ddrss->params.ctl_crc;
113 struct ddrss_ddrctl_map_params *map = &ddrss->params.ctl_map;
116 debug("%s: DDR controller register configuration started\n", __func__);
118 ddrss_ctl_writel(DDRSS_DDRCTL_MSTR, reg->ddrctl_mstr);
119 ddrss_ctl_writel(DDRSS_DDRCTL_RFSHCTL0, reg->ddrctl_rfshctl0);
120 ddrss_ctl_writel(DDRSS_DDRCTL_RFSHTMG, reg->ddrctl_rfshtmg);
122 ddrss_ctl_writel(DDRSS_DDRCTL_ECCCFG0, ecc->ddrctl_ecccfg0);
123 ddrss_ctl_writel(DDRSS_DDRCTL_CRCPARCTL0, crc->ddrctl_crcparctl0);
124 ddrss_ctl_writel(DDRSS_DDRCTL_CRCPARCTL1, crc->ddrctl_crcparctl1);
125 ddrss_ctl_writel(DDRSS_DDRCTL_CRCPARCTL2, crc->ddrctl_crcparctl2);
127 ddrss_ctl_writel(DDRSS_DDRCTL_INIT0, reg->ddrctl_init0);
128 ddrss_ctl_writel(DDRSS_DDRCTL_INIT1, reg->ddrctl_init1);
129 ddrss_ctl_writel(DDRSS_DDRCTL_INIT3, reg->ddrctl_init3);
130 ddrss_ctl_writel(DDRSS_DDRCTL_INIT4, reg->ddrctl_init4);
131 ddrss_ctl_writel(DDRSS_DDRCTL_INIT5, reg->ddrctl_init5);
132 ddrss_ctl_writel(DDRSS_DDRCTL_INIT6, reg->ddrctl_init6);
133 ddrss_ctl_writel(DDRSS_DDRCTL_INIT7, reg->ddrctl_init7);
135 ddrss_ctl_writel(DDRSS_DDRCTL_DRAMTMG0, tmg->ddrctl_dramtmg0);
136 ddrss_ctl_writel(DDRSS_DDRCTL_DRAMTMG1, tmg->ddrctl_dramtmg1);
137 ddrss_ctl_writel(DDRSS_DDRCTL_DRAMTMG2, tmg->ddrctl_dramtmg2);
138 ddrss_ctl_writel(DDRSS_DDRCTL_DRAMTMG3, tmg->ddrctl_dramtmg3);
139 ddrss_ctl_writel(DDRSS_DDRCTL_DRAMTMG4, tmg->ddrctl_dramtmg4);
140 ddrss_ctl_writel(DDRSS_DDRCTL_DRAMTMG5, tmg->ddrctl_dramtmg5);
141 ddrss_ctl_writel(DDRSS_DDRCTL_DRAMTMG8, tmg->ddrctl_dramtmg8);
142 ddrss_ctl_writel(DDRSS_DDRCTL_DRAMTMG9, tmg->ddrctl_dramtmg9);
143 ddrss_ctl_writel(DDRSS_DDRCTL_DRAMTMG11, tmg->ddrctl_dramtmg11);
144 ddrss_ctl_writel(DDRSS_DDRCTL_DRAMTMG12, tmg->ddrctl_dramtmg12);
145 ddrss_ctl_writel(DDRSS_DDRCTL_DRAMTMG13, tmg->ddrctl_dramtmg13);
146 ddrss_ctl_writel(DDRSS_DDRCTL_DRAMTMG14, tmg->ddrctl_dramtmg14);
147 ddrss_ctl_writel(DDRSS_DDRCTL_DRAMTMG15, tmg->ddrctl_dramtmg15);
148 ddrss_ctl_writel(DDRSS_DDRCTL_DRAMTMG17, tmg->ddrctl_dramtmg17);
150 ddrss_ctl_writel(DDRSS_DDRCTL_ZQCTL0, reg->ddrctl_zqctl0);
151 ddrss_ctl_writel(DDRSS_DDRCTL_ZQCTL1, reg->ddrctl_zqctl1);
153 ddrss_ctl_writel(DDRSS_DDRCTL_DFITMG0, reg->ddrctl_dfitmg0);
154 ddrss_ctl_writel(DDRSS_DDRCTL_DFITMG1, reg->ddrctl_dfitmg1);
155 ddrss_ctl_writel(DDRSS_DDRCTL_DFITMG2, reg->ddrctl_dfitmg2);
156 ddrss_ctl_writel(DDRSS_DDRCTL_DFIMISC, reg->ddrctl_dfimisc);
158 ddrss_ctl_writel(DDRSS_DDRCTL_ADDRMAP0, map->ddrctl_addrmap0);
159 ddrss_ctl_writel(DDRSS_DDRCTL_ADDRMAP1, map->ddrctl_addrmap1);
160 ddrss_ctl_writel(DDRSS_DDRCTL_ADDRMAP2, map->ddrctl_addrmap2);
161 ddrss_ctl_writel(DDRSS_DDRCTL_ADDRMAP3, map->ddrctl_addrmap3);
162 ddrss_ctl_writel(DDRSS_DDRCTL_ADDRMAP4, map->ddrctl_addrmap4);
163 ddrss_ctl_writel(DDRSS_DDRCTL_ADDRMAP5, map->ddrctl_addrmap5);
164 ddrss_ctl_writel(DDRSS_DDRCTL_ADDRMAP6, map->ddrctl_addrmap6);
165 ddrss_ctl_writel(DDRSS_DDRCTL_ADDRMAP7, map->ddrctl_addrmap7);
166 ddrss_ctl_writel(DDRSS_DDRCTL_ADDRMAP8, map->ddrctl_addrmap8);
167 ddrss_ctl_writel(DDRSS_DDRCTL_ADDRMAP9, map->ddrctl_addrmap9);
168 ddrss_ctl_writel(DDRSS_DDRCTL_ADDRMAP10, map->ddrctl_addrmap10);
169 ddrss_ctl_writel(DDRSS_DDRCTL_ADDRMAP11, map->ddrctl_addrmap11);
171 ddrss_ctl_writel(DDRSS_DDRCTL_ODTCFG, reg->ddrctl_odtcfg);
172 ddrss_ctl_writel(DDRSS_DDRCTL_ODTMAP, reg->ddrctl_odtmap);
174 /* Disable refreshes */
175 val = ddrss_ctl_readl(DDRSS_DDRCTL_RFSHCTL3);
177 ddrss_ctl_writel(DDRSS_DDRCTL_RFSHCTL3, val);
179 debug("%s: DDR controller configuration completed\n", __func__);
182 #define ddrss_phy_writel(off, val) \
184 ddrss_writel(ddrss->ddrss_phy_cfg, off, val); \
185 sdelay(10); /* Delay at least 20 clock cycles */ \
188 #define ddrss_phy_readl(off) \
190 u32 val = ddrss_readl(ddrss->ddrss_phy_cfg, off); \
191 sdelay(10); /* Delay at least 20 clock cycles */ \
196 * am654_ddrss_phy_configuration() - Configure PHY specific registers
197 * @ddrss: corresponding ddrss device
199 static void am654_ddrss_phy_configuration(struct am654_ddrss_desc *ddrss)
201 struct ddrss_ddrphy_ioctl_params *ioctl = &ddrss->params.phy_ioctl;
202 struct ddrss_ddrphy_timing_params *tmg = &ddrss->params.phy_timing;
203 struct ddrss_ddrphy_ctrl_params *ctrl = &ddrss->params.phy_ctrl;
204 struct ddrss_ddrphy_cfg_params *cfg = &ddrss->params.phy_cfg;
205 struct ddrss_ddrphy_zq_params *zq = &ddrss->params.phy_zq;
207 debug("%s: DDR phy register configuration started\n", __func__);
209 ddrss_phy_writel(DDRSS_DDRPHY_PGCR0, cfg->ddrphy_pgcr0);
210 ddrss_phy_writel(DDRSS_DDRPHY_PGCR1, cfg->ddrphy_pgcr1);
211 ddrss_phy_writel(DDRSS_DDRPHY_PGCR2, cfg->ddrphy_pgcr2);
212 ddrss_phy_writel(DDRSS_DDRPHY_PGCR3, cfg->ddrphy_pgcr3);
213 ddrss_phy_writel(DDRSS_DDRPHY_PGCR6, cfg->ddrphy_pgcr6);
215 ddrss_phy_writel(DDRSS_DDRPHY_PTR2, tmg->ddrphy_ptr2);
216 ddrss_phy_writel(DDRSS_DDRPHY_PTR3, tmg->ddrphy_ptr3);
217 ddrss_phy_writel(DDRSS_DDRPHY_PTR4, tmg->ddrphy_ptr4);
218 ddrss_phy_writel(DDRSS_DDRPHY_PTR5, tmg->ddrphy_ptr5);
219 ddrss_phy_writel(DDRSS_DDRPHY_PTR6, tmg->ddrphy_ptr6);
221 ddrss_phy_writel(DDRSS_DDRPHY_PLLCR0, ctrl->ddrphy_pllcr0);
223 ddrss_phy_writel(DDRSS_DDRPHY_DXCCR, cfg->ddrphy_dxccr);
224 ddrss_phy_writel(DDRSS_DDRPHY_DSGCR, cfg->ddrphy_dsgcr);
226 ddrss_phy_writel(DDRSS_DDRPHY_DCR, cfg->ddrphy_dcr);
228 ddrss_phy_writel(DDRSS_DDRPHY_DTPR0, tmg->ddrphy_dtpr0);
229 ddrss_phy_writel(DDRSS_DDRPHY_DTPR1, tmg->ddrphy_dtpr1);
230 ddrss_phy_writel(DDRSS_DDRPHY_DTPR2, tmg->ddrphy_dtpr2);
231 ddrss_phy_writel(DDRSS_DDRPHY_DTPR3, tmg->ddrphy_dtpr3);
232 ddrss_phy_writel(DDRSS_DDRPHY_DTPR4, tmg->ddrphy_dtpr4);
233 ddrss_phy_writel(DDRSS_DDRPHY_DTPR5, tmg->ddrphy_dtpr5);
234 ddrss_phy_writel(DDRSS_DDRPHY_DTPR6, tmg->ddrphy_dtpr6);
236 ddrss_phy_writel(DDRSS_DDRPHY_ZQCR, zq->ddrphy_zqcr);
237 ddrss_phy_writel(DDRSS_DDRPHY_ZQ0PR0, zq->ddrphy_zq0pr0);
238 ddrss_phy_writel(DDRSS_DDRPHY_ZQ1PR0, zq->ddrphy_zq1pr0);
240 ddrss_phy_writel(DDRSS_DDRPHY_MR0, ctrl->ddrphy_mr0);
241 ddrss_phy_writel(DDRSS_DDRPHY_MR1, ctrl->ddrphy_mr1);
242 ddrss_phy_writel(DDRSS_DDRPHY_MR2, ctrl->ddrphy_mr2);
243 ddrss_phy_writel(DDRSS_DDRPHY_MR3, ctrl->ddrphy_mr3);
244 ddrss_phy_writel(DDRSS_DDRPHY_MR4, ctrl->ddrphy_mr4);
245 ddrss_phy_writel(DDRSS_DDRPHY_MR5, ctrl->ddrphy_mr5);
246 ddrss_phy_writel(DDRSS_DDRPHY_MR6, ctrl->ddrphy_mr6);
247 ddrss_phy_writel(DDRSS_DDRPHY_MR11, ctrl->ddrphy_mr11);
248 ddrss_phy_writel(DDRSS_DDRPHY_MR12, ctrl->ddrphy_mr12);
249 ddrss_phy_writel(DDRSS_DDRPHY_MR13, ctrl->ddrphy_mr13);
250 ddrss_phy_writel(DDRSS_DDRPHY_MR14, ctrl->ddrphy_mr14);
251 ddrss_phy_writel(DDRSS_DDRPHY_MR22, ctrl->ddrphy_mr22);
253 ddrss_phy_writel(DDRSS_DDRPHY_VTCR0, ctrl->ddrphy_vtcr0);
255 ddrss_phy_writel(DDRSS_DDRPHY_DX8SL0PLLCR0, cfg->ddrphy_dx8sl0pllcr0);
256 ddrss_phy_writel(DDRSS_DDRPHY_DX8SL1PLLCR0, cfg->ddrphy_dx8sl1pllcr0);
257 ddrss_phy_writel(DDRSS_DDRPHY_DX8SL2PLLCR0, cfg->ddrphy_dx8sl2pllcr0);
259 ddrss_phy_writel(DDRSS_DDRPHY_DTCR0, ctrl->ddrphy_dtcr0);
260 ddrss_phy_writel(DDRSS_DDRPHY_DTCR1, ctrl->ddrphy_dtcr1);
262 ddrss_phy_writel(DDRSS_DDRPHY_ACIOCR0, ioctl->ddrphy_aciocr0);
263 ddrss_phy_writel(DDRSS_DDRPHY_ACIOCR3, ioctl->ddrphy_aciocr3);
264 ddrss_phy_writel(DDRSS_DDRPHY_ACIOCR5, ioctl->ddrphy_aciocr5);
265 ddrss_phy_writel(DDRSS_DDRPHY_IOVCR0, ioctl->ddrphy_iovcr0);
267 ddrss_phy_writel(DDRSS_DDRPHY_DX4GCR0, cfg->ddrphy_dx4gcr0);
268 ddrss_phy_writel(DDRSS_DDRPHY_DX4GCR1, cfg->ddrphy_dx4gcr1);
269 ddrss_phy_writel(DDRSS_DDRPHY_DX4GCR2, cfg->ddrphy_dx4gcr2);
270 ddrss_phy_writel(DDRSS_DDRPHY_DX4GCR3, cfg->ddrphy_dx4gcr3);
272 ddrss_phy_writel(DDRSS_DDRPHY_DX0GCR4, cfg->ddrphy_dx0gcr4);
273 ddrss_phy_writel(DDRSS_DDRPHY_DX1GCR4, cfg->ddrphy_dx1gcr4);
274 ddrss_phy_writel(DDRSS_DDRPHY_DX2GCR4, cfg->ddrphy_dx2gcr4);
275 ddrss_phy_writel(DDRSS_DDRPHY_DX3GCR4, cfg->ddrphy_dx3gcr4);
277 ddrss_phy_writel(DDRSS_DDRPHY_PGCR5, cfg->ddrphy_pgcr5);
278 ddrss_phy_writel(DDRSS_DDRPHY_DX0GCR5, cfg->ddrphy_dx0gcr5);
279 ddrss_phy_writel(DDRSS_DDRPHY_DX1GCR5, cfg->ddrphy_dx1gcr5);
280 ddrss_phy_writel(DDRSS_DDRPHY_DX2GCR5, cfg->ddrphy_dx2gcr5);
281 ddrss_phy_writel(DDRSS_DDRPHY_DX3GCR5, cfg->ddrphy_dx3gcr5);
283 ddrss_phy_writel(DDRSS_DDRPHY_RANKIDR, DDRSS_DDRPHY_RANKIDR_RANK0);
285 ddrss_phy_writel(DDRSS_DDRPHY_DX0GTR0, cfg->ddrphy_dx0gtr0);
286 ddrss_phy_writel(DDRSS_DDRPHY_DX1GTR0, cfg->ddrphy_dx1gtr0);
287 ddrss_phy_writel(DDRSS_DDRPHY_DX2GTR0, cfg->ddrphy_dx2gtr0);
288 ddrss_phy_writel(DDRSS_DDRPHY_DX3GTR0, cfg->ddrphy_dx3gtr0);
289 ddrss_phy_writel(DDRSS_DDRPHY_ODTCR, cfg->ddrphy_odtcr);
291 ddrss_phy_writel(DDRSS_DDRPHY_DX8SL0IOCR, cfg->ddrphy_dx8sl0iocr);
292 ddrss_phy_writel(DDRSS_DDRPHY_DX8SL1IOCR, cfg->ddrphy_dx8sl1iocr);
293 ddrss_phy_writel(DDRSS_DDRPHY_DX8SL2IOCR, cfg->ddrphy_dx8sl2iocr);
295 ddrss_phy_writel(DDRSS_DDRPHY_DX8SL0DXCTL2, cfg->ddrphy_dx8sl0dxctl2);
296 ddrss_phy_writel(DDRSS_DDRPHY_DX8SL1DXCTL2, cfg->ddrphy_dx8sl1dxctl2);
297 ddrss_phy_writel(DDRSS_DDRPHY_DX8SL2DXCTL2, cfg->ddrphy_dx8sl2dxctl2);
299 ddrss_phy_writel(DDRSS_DDRPHY_DX8SL0DQSCTL, cfg->ddrphy_dx8sl0dqsctl);
300 ddrss_phy_writel(DDRSS_DDRPHY_DX8SL1DQSCTL, cfg->ddrphy_dx8sl1dqsctl);
301 ddrss_phy_writel(DDRSS_DDRPHY_DX8SL2DQSCTL, cfg->ddrphy_dx8sl2dqsctl);
303 debug("%s: DDR phy register configuration completed\n", __func__);
306 static int __phy_builtin_init_routine(struct am654_ddrss_desc *ddrss,
307 u32 init_value, u32 sts_mask,
312 ddrss_phy_writel(DDRSS_DDRPHY_PIR, init_value | PIR_INIT_MASK);
314 sdelay(5); /* Delay at least 10 clock cycles */
316 if (!wait_on_value(sts_mask, sts_mask,
317 ddrss->ddrss_phy_cfg + DDRSS_DDRPHY_PGSR0, LDELAY))
320 sdelay(16); /* Delay at least 32 clock cycles */
322 ret = ddrss_phy_readl(DDRSS_DDRPHY_PGSR0);
323 debug("%s: PGSR0 val = 0x%x\n", __func__, ret);
330 int write_leveling(struct am654_ddrss_desc *ddrss)
334 debug("%s: Write leveling started\n", __func__);
336 ret = __phy_builtin_init_routine(ddrss, PIR_WL_MASK, PGSR0_WLDONE_MASK,
339 if (ret == -ETIMEDOUT)
340 printf("%s: ERROR: Write leveling timedout\n",
343 printf("%s:ERROR: Write leveling failed\n", __func__);
347 debug("%s: Write leveling completed\n", __func__);
351 int read_dqs_training(struct am654_ddrss_desc *ddrss)
355 debug("%s: Read DQS training started\n", __func__);
357 ret = __phy_builtin_init_routine(ddrss, PIR_QSGATE_MASK,
358 PGSR0_QSGDONE_MASK, PGSR0_QSGERR_MASK);
360 if (ret == -ETIMEDOUT)
361 printf("%s: ERROR: Read DQS timedout\n", __func__);
363 printf("%s:ERROR: Read DQS Gate training failed\n",
368 debug("%s: Read DQS training completed\n", __func__);
372 int dqs2dq_training(struct am654_ddrss_desc *ddrss)
376 debug("%s: DQS2DQ training started\n", __func__);
378 ret = __phy_builtin_init_routine(ddrss, PIR_DQS2DQ_MASK,
379 PGSR0_DQS2DQDONE_MASK,
380 PGSR0_DQS2DQERR_MASK);
382 if (ret == -ETIMEDOUT)
383 printf("%s: ERROR: DQS2DQ training timedout\n",
386 printf("%s:ERROR: DQS2DQ training failed\n",
391 debug("%s: DQS2DQ training completed\n", __func__);
395 int write_leveling_adjustment(struct am654_ddrss_desc *ddrss)
399 debug("%s: Write Leveling adjustment\n", __func__);
400 ret = __phy_builtin_init_routine(ddrss, PIR_WLADJ_MASK,
401 PGSR0_WLADONE_MASK, PGSR0_WLAERR_MASK);
403 if (ret == -ETIMEDOUT)
404 printf("%s:ERROR: Write Leveling adjustment timedout\n",
407 printf("%s: ERROR: Write Leveling adjustment failed\n",
414 int rest_training(struct am654_ddrss_desc *ddrss)
418 debug("%s: Rest of the training started\n", __func__);
420 debug("%s: Read Deskew adjustment\n", __func__);
421 ret = __phy_builtin_init_routine(ddrss, PIR_RDDSKW_MASK,
422 PGSR0_RDDONE_MASK, PGSR0_RDERR_MASK);
424 if (ret == -ETIMEDOUT)
425 printf("%s: ERROR: Read Deskew timedout\n", __func__);
427 printf("%s: ERROR: Read Deskew failed\n", __func__);
431 debug("%s: Write Deskew adjustment\n", __func__);
432 ret = __phy_builtin_init_routine(ddrss, PIR_WRDSKW_MASK,
433 PGSR0_WDDONE_MASK, PGSR0_WDERR_MASK);
435 if (ret == -ETIMEDOUT)
436 printf("%s: ERROR: Write Deskew timedout\n", __func__);
438 printf("%s: ERROR: Write Deskew failed\n", __func__);
442 debug("%s: Read Eye training\n", __func__);
443 ret = __phy_builtin_init_routine(ddrss, PIR_RDEYE_MASK,
444 PGSR0_REDONE_MASK, PGSR0_REERR_MASK);
446 if (ret == -ETIMEDOUT)
447 printf("%s: ERROR: Read Eye training timedout\n",
450 printf("%s: ERROR: Read Eye training failed\n",
455 debug("%s: Write Eye training\n", __func__);
456 ret = __phy_builtin_init_routine(ddrss, PIR_WREYE_MASK,
457 PGSR0_WEDONE_MASK, PGSR0_WEERR_MASK);
459 if (ret == -ETIMEDOUT)
460 printf("%s: ERROR: Write Eye training timedout\n",
463 printf("%s: ERROR: Write Eye training failed\n",
470 int VREF_training(struct am654_ddrss_desc *ddrss)
473 debug("%s: VREF training\n", __func__);
474 ret = __phy_builtin_init_routine(ddrss, PIR_VREF_MASK, PGSR0_VDONE_MASK,
477 if (ret == -ETIMEDOUT)
478 printf("%s: ERROR: VREF training timedout\n", __func__);
480 printf("%s: ERROR: VREF training failed\n", __func__);
486 int enable_dqs_pd(struct am654_ddrss_desc *ddrss)
490 val = ddrss_phy_readl(DDRSS_DDRPHY_DX8SL0DQSCTL);
493 ddrss_phy_writel(DDRSS_DDRPHY_DX8SL0DQSCTL, val);
495 val = ddrss_phy_readl(DDRSS_DDRPHY_DX8SL1DQSCTL);
498 ddrss_phy_writel(DDRSS_DDRPHY_DX8SL1DQSCTL, val);
500 val = ddrss_phy_readl(DDRSS_DDRPHY_DX8SL2DQSCTL);
503 ddrss_phy_writel(DDRSS_DDRPHY_DX8SL2DQSCTL, val);
509 int disable_dqs_pd(struct am654_ddrss_desc *ddrss)
513 val = ddrss_phy_readl(DDRSS_DDRPHY_DX8SL0DQSCTL);
515 ddrss_phy_writel(DDRSS_DDRPHY_DX8SL0DQSCTL, val);
517 val = ddrss_phy_readl(DDRSS_DDRPHY_DX8SL1DQSCTL);
519 ddrss_phy_writel(DDRSS_DDRPHY_DX8SL1DQSCTL, val);
521 val = ddrss_phy_readl(DDRSS_DDRPHY_DX8SL2DQSCTL);
523 ddrss_phy_writel(DDRSS_DDRPHY_DX8SL2DQSCTL, val);
529 int cleanup_training(struct am654_ddrss_desc *ddrss)
532 u32 dgsl0, dgsl1, dgsl2, dgsl3, rddly, rd2wr_wr2rd;
534 ddrss_phy_writel(DDRSS_DDRPHY_RANKIDR, 0x00000000);
535 dgsl0 = (ddrss_phy_readl(DDRSS_DDRPHY_DX0GTR0) & 0x1F) >> 2;
536 dgsl1 = (ddrss_phy_readl(DDRSS_DDRPHY_DX1GTR0) & 0x1F) >> 2;
537 dgsl2 = (ddrss_phy_readl(DDRSS_DDRPHY_DX2GTR0) & 0x1F) >> 2;
538 dgsl3 = (ddrss_phy_readl(DDRSS_DDRPHY_DX3GTR0) & 0x1F) >> 2;
550 /* Update rddly based on dgsl values */
551 val = (ddrss_phy_readl(DDRSS_DDRPHY_DX0GCR0) & ~0xF00000);
552 val |= (rddly << 20);
553 ddrss_phy_writel(DDRSS_DDRPHY_DX0GCR0, val);
555 val = (ddrss_phy_readl(DDRSS_DDRPHY_DX1GCR0) & ~0xF00000);
556 val |= (rddly << 20);
557 ddrss_phy_writel(DDRSS_DDRPHY_DX1GCR0, val);
559 val = (ddrss_phy_readl(DDRSS_DDRPHY_DX2GCR0) & ~0xF00000);
560 val |= (rddly << 20);
561 ddrss_phy_writel(DDRSS_DDRPHY_DX2GCR0, val);
563 val = (ddrss_phy_readl(DDRSS_DDRPHY_DX3GCR0) & ~0xF00000);
564 val |= (rddly << 20);
565 ddrss_phy_writel(DDRSS_DDRPHY_DX3GCR0, val);
568 * Add system latency derived from training back into rd2wr and wr2rd
569 * rd2wr = RL + BL/2 + 1 + WR_PREAMBLE - WL + max(DXnGTR0.DGSL) / 2
570 * wr2rd = CWL + PL + BL/2 + tWTR_L + max(DXnGTR0.DGSL) / 2
574 ddrss_phy_writel(DDRSS_DDRPHY_RANKIDR, 0x00000000);
576 dgsl0 = (ddrss_phy_readl(DDRSS_DDRPHY_DX0GTR0) & 0x1F);
577 dgsl1 = (ddrss_phy_readl(DDRSS_DDRPHY_DX1GTR0) & 0x1F);
578 dgsl2 = (ddrss_phy_readl(DDRSS_DDRPHY_DX2GTR0) & 0x1F);
579 dgsl3 = (ddrss_phy_readl(DDRSS_DDRPHY_DX3GTR0) & 0x1F);
581 /* Find maximum value across all bytes */
583 if (dgsl1 > rd2wr_wr2rd)
585 if (dgsl2 > rd2wr_wr2rd)
587 if (dgsl3 > rd2wr_wr2rd)
592 /* Now add in adjustment to DRAMTMG2 bit fields for rd2wr and wr2rd */
593 /* Clear VSWCTL.sw_done */
594 ddrss_ctl_writel(DDRSS_DDRCTL_SWCTL,
595 ddrss_ctl_readl(DDRSS_DDRCTL_SWCTL) & ~0x1);
597 ddrss_ctl_writel(DDRSS_DDRCTL_DRAMTMG2,
598 ddrss_ctl_readl(DDRSS_DDRCTL_DRAMTMG2) +
601 ddrss_ctl_writel(DDRSS_DDRCTL_DRAMTMG2,
602 ddrss_ctl_readl(DDRSS_DDRCTL_DRAMTMG2) +
604 /* Set VSWCTL.sw_done */
605 ddrss_ctl_writel(DDRSS_DDRCTL_SWCTL,
606 ddrss_ctl_readl(DDRSS_DDRCTL_SWCTL) | 0x1);
607 /* Wait until settings are applied */
608 while (!(ddrss_ctl_readl(DDRSS_DDRCTL_SWSTAT) & 0x1)) {
612 debug("%s: Rest of the training completed\n", __func__);
617 * am654_ddrss_init() - Initialization sequence for enabling the SDRAM
618 * device attached to ddrss.
619 * @dev: corresponding ddrss device
621 * Does all the initialization sequence that is required to get attached
622 * ddr in a working state. After this point, ddr should be accessible.
623 * Return: 0 if all went ok, else corresponding error message.
625 static int am654_ddrss_init(struct am654_ddrss_desc *ddrss)
629 struct ddrss_ss_reg_params *reg = &ddrss->params.ss_reg;
631 debug("Starting DDR initialization...\n");
633 debug("%s(ddrss=%p)\n", __func__, ddrss);
635 ddrss_writel(ddrss->ddrss_ss_cfg, DDRSS_V2H_CTL_REG,
636 reg->ddrss_v2h_ctl_reg);
638 am654_ddrss_ctrl_configuration(ddrss);
640 /* Release the reset to the controller */
641 clrbits_le32(ddrss->ddrss_ss_cfg + DDRSS_SS_CTL_REG,
642 SS_CTL_REG_CTL_ARST_MASK);
644 am654_ddrss_phy_configuration(ddrss);
646 debug("Starting DDR training...\n");
647 ret = __phy_builtin_init_routine(ddrss, PIR_PHY_INIT, 0x1, 0);
649 dev_err(ddrss->dev, "PHY initialization failed %d\n", ret);
653 ret = __phy_builtin_init_routine(ddrss, PIR_DRAM_INIT,
654 PGSR0_DRAM_INIT_MASK, 0);
656 dev_err(ddrss->dev, "DRAM initialization failed %d\n", ret);
660 ret = am654_ddrss_dram_wait_for_init_complt(ddrss);
662 printf("%s: ERROR: DRAM Wait for init complete timedout\n",
667 val = am654_ddrss_get_type(ddrss);
670 case DDR_TYPE_LPDDR4:
672 ret = __phy_builtin_init_routine(ddrss, PIR_DRAM_INIT,
673 PGSR0_DRAM_INIT_MASK, 0);
675 dev_err(ddrss->dev, "DRAM initialization failed %d\n",
680 /* must perform DRAM_INIT twice for LPDDR4 */
681 ret = __phy_builtin_init_routine(ddrss, PIR_DRAM_INIT,
682 PGSR0_DRAM_INIT_MASK, 0);
684 dev_err(ddrss->dev, "DRAM initialization failed %d\n",
689 ret = am654_ddrss_dram_wait_for_init_complt(ddrss);
691 printf("%s: ERROR: DRAM Wait for init complete timedout\n",
696 ret = write_leveling(ddrss);
700 ret = enable_dqs_pd(ddrss);
704 ret = read_dqs_training(ddrss);
708 ret = disable_dqs_pd(ddrss);
712 ret = dqs2dq_training(ddrss);
716 ret = write_leveling_adjustment(ddrss);
720 ret = rest_training(ddrss);
724 ret = VREF_training(ddrss);
728 debug("LPDDR4 training complete\n");
733 debug("Starting DDR4 training\n");
735 ret = __phy_builtin_init_routine(ddrss, PIR_DRAM_INIT,
736 PGSR0_DRAM_INIT_MASK, 0);
738 dev_err(ddrss->dev, "DRAM initialization failed %d\n",
743 ret = am654_ddrss_dram_wait_for_init_complt(ddrss);
745 printf("%s: ERROR: DRAM Wait for init complete timedout\n",
750 ret = write_leveling(ddrss);
754 ret = read_dqs_training(ddrss);
758 ret = write_leveling_adjustment(ddrss);
762 ret = rest_training(ddrss);
766 ret = VREF_training(ddrss);
769 debug("DDR4 training complete\n");
774 debug("Starting DDR3 training\n");
776 ret = __phy_builtin_init_routine(ddrss, PIR_DRAM_INIT,
777 PGSR0_DRAM_INIT_MASK, 0);
779 dev_err(ddrss->dev, "DRAM initialization failed %d\n",
784 ret = am654_ddrss_dram_wait_for_init_complt(ddrss);
786 printf("%s: ERROR: DRAM Wait for init complete timedout\n",
791 ret = write_leveling(ddrss);
795 ret = enable_dqs_pd(ddrss);
799 ret = read_dqs_training(ddrss);
803 ret = disable_dqs_pd(ddrss);
807 ret = write_leveling_adjustment(ddrss);
811 ret = rest_training(ddrss);
815 debug("DDR3 training complete\n");
818 printf("%s: ERROR: Unsupported DDR type\n", __func__);
822 ret = cleanup_training(ddrss);
826 /* Enabling refreshes after training is done */
827 ddrss_ctl_writel(DDRSS_DDRCTL_RFSHCTL3,
828 ddrss_ctl_readl(DDRSS_DDRCTL_RFSHCTL3) & ~0x1);
830 /* Disable PUBMODE after training is done */
831 ddrss_phy_writel(DDRSS_DDRPHY_PGCR1,
832 ddrss_phy_readl(DDRSS_DDRPHY_PGCR1) & ~0x40);
834 debug("Completed DDR training\n");
840 * am654_ddrss_power_on() - Enable power and clocks for ddrss
841 * @dev: corresponding ddrss device
843 * Tries to enable all the corresponding clocks to the ddrss and sets it
844 * to the right frequency and then power on the ddrss.
845 * Return: 0 if all went ok, else corresponding error message.
847 static int am654_ddrss_power_on(struct am654_ddrss_desc *ddrss)
851 debug("%s(ddrss=%p)\n", __func__, ddrss);
853 ret = clk_enable(&ddrss->ddrss_clk);
855 dev_err(ddrss->dev, "clk_enable() failed: %d\n", ret);
859 ret = power_domain_on(&ddrss->ddrcfg_pwrdmn);
861 dev_err(ddrss->dev, "power_domain_on() failed: %d\n", ret);
865 ret = power_domain_on(&ddrss->ddrdata_pwrdmn);
867 dev_err(ddrss->dev, "power_domain_on() failed: %d\n", ret);
872 #if CONFIG_IS_ENABLED(DM_REGULATOR)
873 device_get_supply_regulator(ddrss->dev, "vtt-supply",
875 ret = regulator_set_value(ddrss->vtt_supply, 3300000);
878 debug("VTT regulator enabled\n");
885 * am654_ddrss_ofdata_to_priv() - generate private data from device tree
886 * @dev: corresponding ddrss device
888 * Return: 0 if all went ok, else corresponding error message.
890 static int am654_ddrss_ofdata_to_priv(struct udevice *dev)
892 struct am654_ddrss_desc *ddrss = dev_get_priv(dev);
896 debug("%s(dev=%p)\n", __func__, dev);
898 ret = clk_get_by_index(dev, 0, &ddrss->ddrss_clk);
900 dev_err(dev, "clk_get failed: %d\n", ret);
904 ret = power_domain_get_by_index(dev, &ddrss->ddrcfg_pwrdmn, 0);
906 dev_err(dev, "power_domain_get() failed: %d\n", ret);
910 ret = power_domain_get_by_index(dev, &ddrss->ddrdata_pwrdmn, 1);
912 dev_err(dev, "power_domain_get() failed: %d\n", ret);
916 reg = devfdt_get_addr_name(dev, "ss");
917 if (reg == FDT_ADDR_T_NONE) {
918 dev_err(dev, "No reg property for DDRSS wrapper logic\n");
921 ddrss->ddrss_ss_cfg = (void *)reg;
923 reg = devfdt_get_addr_name(dev, "ctl");
924 if (reg == FDT_ADDR_T_NONE) {
925 dev_err(dev, "No reg property for Controller region\n");
928 ddrss->ddrss_ctl_cfg = (void *)reg;
930 reg = devfdt_get_addr_name(dev, "phy");
931 if (reg == FDT_ADDR_T_NONE) {
932 dev_err(dev, "No reg property for PHY region\n");
935 ddrss->ddrss_phy_cfg = (void *)reg;
937 ret = dev_read_u32_array(dev, "ti,ss-reg",
938 (u32 *)&ddrss->params.ss_reg,
939 sizeof(ddrss->params.ss_reg) / sizeof(u32));
941 dev_err(dev, "Cannot read ti,ss-reg params\n");
945 ret = dev_read_u32_array(dev, "ti,ctl-reg",
946 (u32 *)&ddrss->params.ctl_reg,
947 sizeof(ddrss->params.ctl_reg) / sizeof(u32));
949 dev_err(dev, "Cannot read ti,ctl-reg params\n");
953 ret = dev_read_u32_array(dev, "ti,ctl-crc",
954 (u32 *)&ddrss->params.ctl_crc,
955 sizeof(ddrss->params.ctl_crc) / sizeof(u32));
957 dev_err(dev, "Cannot read ti,ctl-crc params\n");
961 ret = dev_read_u32_array(dev, "ti,ctl-ecc",
962 (u32 *)&ddrss->params.ctl_ecc,
963 sizeof(ddrss->params.ctl_ecc) / sizeof(u32));
965 dev_err(dev, "Cannot read ti,ctl-ecc params\n");
969 ret = dev_read_u32_array(dev, "ti,ctl-map",
970 (u32 *)&ddrss->params.ctl_map,
971 sizeof(ddrss->params.ctl_map) / sizeof(u32));
973 dev_err(dev, "Cannot read ti,ctl-map params\n");
977 ret = dev_read_u32_array(dev, "ti,ctl-pwr",
978 (u32 *)&ddrss->params.ctl_pwr,
979 sizeof(ddrss->params.ctl_pwr) / sizeof(u32));
981 dev_err(dev, "Cannot read ti,ctl-pwr params\n");
985 ret = dev_read_u32_array(dev, "ti,ctl-timing",
986 (u32 *)&ddrss->params.ctl_timing,
987 sizeof(ddrss->params.ctl_timing) /
990 dev_err(dev, "Cannot read ti,ctl-timing params\n");
994 ret = dev_read_u32_array(dev, "ti,phy-cfg",
995 (u32 *)&ddrss->params.phy_cfg,
996 sizeof(ddrss->params.phy_cfg) / sizeof(u32));
998 dev_err(dev, "Cannot read ti,phy-cfg params\n");
1002 ret = dev_read_u32_array(dev, "ti,phy-ctl",
1003 (u32 *)&ddrss->params.phy_ctrl,
1004 sizeof(ddrss->params.phy_ctrl) / sizeof(u32));
1006 dev_err(dev, "Cannot read ti,phy-ctl params\n");
1010 ret = dev_read_u32_array(dev, "ti,phy-ioctl",
1011 (u32 *)&ddrss->params.phy_ioctl,
1012 sizeof(ddrss->params.phy_ioctl) / sizeof(u32));
1014 dev_err(dev, "Cannot read ti,phy-ioctl params\n");
1018 ret = dev_read_u32_array(dev, "ti,phy-timing",
1019 (u32 *)&ddrss->params.phy_timing,
1020 sizeof(ddrss->params.phy_timing) /
1023 dev_err(dev, "Cannot read ti,phy-timing params\n");
1027 ret = dev_read_u32_array(dev, "ti,phy-zq", (u32 *)&ddrss->params.phy_zq,
1028 sizeof(ddrss->params.phy_zq) / sizeof(u32));
1030 dev_err(dev, "Cannot read ti,phy-zq params\n");
1038 * am654_ddrss_probe() - Basic probe
1039 * @dev: corresponding ddrss device
1041 * Return: 0 if all went ok, else corresponding error message
1043 static int am654_ddrss_probe(struct udevice *dev)
1045 struct am654_ddrss_desc *ddrss = dev_get_priv(dev);
1048 debug("%s(dev=%p)\n", __func__, dev);
1050 ret = am654_ddrss_ofdata_to_priv(dev);
1055 ret = am654_ddrss_power_on(ddrss);
1059 ret = am654_ddrss_init(ddrss);
1064 static int am654_ddrss_get_info(struct udevice *dev, struct ram_info *info)
1069 static struct ram_ops am654_ddrss_ops = {
1070 .get_info = am654_ddrss_get_info,
1073 static const struct udevice_id am654_ddrss_ids[] = {
1074 { .compatible = "ti,am654-ddrss" },
1078 U_BOOT_DRIVER(am654_ddrss) = {
1079 .name = "am654_ddrss",
1081 .of_match = am654_ddrss_ids,
1082 .ops = &am654_ddrss_ops,
1083 .probe = am654_ddrss_probe,
1084 .priv_auto_alloc_size = sizeof(struct am654_ddrss_desc),