blob: 4a019c4edb147e7b731145960d553075d910def2 [file] [log] [blame]
Chia-Wei Wang8fb52592024-09-10 17:39:19 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) ASPEED Technology Inc.
4 */
5#include <asm/io.h>
6#include <asm/arch/fmc_hdr.h>
7#include <asm/arch/scu.h>
8#include <asm/arch/sdram.h>
9#include <config.h>
10#include <dm.h>
11#include <linux/bitfield.h>
12#include <linux/delay.h>
13#include <linux/err.h>
14#include <linux/sizes.h>
15#include <ram.h>
16
17enum ddr_type {
18 DDR4_1600 = 0x0,
19 DDR4_2400,
20 DDR4_3200,
21 DDR5_3200,
22
23 DDR_TYPES
24};
25
26enum ddr_size {
27 DDR_SIZE_256MB,
28 DDR_SIZE_512MB,
29 DDR_SIZE_1GB,
30 DDR_SIZE_2GB,
31
32 DDR_SIZE_MAX,
33};
34
35#define IS_DDR4(t) \
36 (((t) <= DDR4_3200) ? 1 : 0)
37
38struct sdrammc_ac_timing {
39 u32 t_cl;
40 u32 t_cwl;
41 u32 t_bl;
42 u32 t_rcd; /* ACT-to-read/write command delay */
43 u32 t_rp; /* PRE command period */
44 u32 t_ras; /* ACT-to-PRE command delay */
45 u32 t_rrd; /* ACT-to-ACT delay for different BG */
46 u32 t_rrd_l; /* ACT-to-ACT delay for same BG */
47 u32 t_faw; /* Four active window */
48 u32 t_rtp; /* Read-to-PRE command delay */
49 u32 t_wtr; /* Minimum write to read command for different BG */
50 u32 t_wtr_l; /* Minimum write to read command for same BG */
51 u32 t_wtr_a; /* Write to read command for same BG with auto precharge */
52 u32 t_wtp; /* Minimum write to precharge command delay */
53 u32 t_rtw; /* minimum read to write command */
54 u32 t_ccd_l; /* CAS-to-CAS delay for same BG */
55 u32 t_dllk; /* DLL locking time */
56 u32 t_cksre; /* valid clock before after self-refresh or power-down entry/exit process */
57 u32 t_pd; /* power-down entry to exit minimum width */
58 u32 t_xp; /* exit power-down to valid command delay */
59 u32 t_rfc; /* refresh time period */
60 u32 t_mrd;
61 u32 t_refsbrd;
62 u32 t_rfcsb;
63 u32 t_cshsr;
64 u32 t_zq;
65};
66
67static const struct sdrammc_ac_timing ac_table[] = {
68 [DDR4_1600] = {
69 .t_cl = 10, .t_cwl = 9, .t_bl = 8, .t_rcd = 10,
70 .t_rp = 10, .t_ras = 28, .t_rrd = 5, .t_rrd_l = 6,
71 .t_faw = 28, .t_rtp = 6, .t_wtr = 2, .t_wtr_l = 6,
72 .t_wtr_a = 0, .t_wtp = 12, .t_rtw = 0, .t_ccd_l = 5,
73 .t_dllk = 597, .t_cksre = 8, .t_pd = 4, .t_xp = 5,
74 .t_rfc = 880, .t_mrd = 24, .t_refsbrd = 0, .t_rfcsb = 0,
75 .t_cshsr = 0, .t_zq = 80,
76 },
77 [DDR4_2400] = {
78 .t_cl = 15, .t_cwl = 12, .t_bl = 8, .t_rcd = 16,
79 .t_rp = 16, .t_ras = 39, .t_rrd = 7, .t_rrd_l = 8,
80 .t_faw = 37, .t_rtp = 10, .t_wtr = 4, .t_wtr_l = 10,
81 .t_wtr_a = 0, .t_wtp = 19, .t_rtw = 0, .t_ccd_l = 7,
82 .t_dllk = 768, .t_cksre = 13, .t_pd = 7, .t_xp = 8,
83 .t_rfc = 880, .t_mrd = 24, .t_refsbrd = 0, .t_rfcsb = 0,
84 .t_cshsr = 0, .t_zq = 80,
85 },
86 [DDR4_3200] = {
87 .t_cl = 20, .t_cwl = 16, .t_bl = 8, .t_rcd = 20,
88 .t_rp = 20, .t_ras = 52, .t_rrd = 9, .t_rrd_l = 11,
89 .t_faw = 48, .t_rtp = 12, .t_wtr = 4, .t_wtr_l = 12,
90 .t_wtr_a = 0, .t_wtp = 24, .t_rtw = 0, .t_ccd_l = 8,
91 .t_dllk = 1023, .t_cksre = 16, .t_pd = 8, .t_xp = 10,
92 .t_rfc = 880, .t_mrd = 24, .t_refsbrd = 0, .t_rfcsb = 0,
93 .t_cshsr = 0, .t_zq = 80,
94 },
95 [DDR5_3200] = {
96 .t_cl = 26, .t_cwl = 24, .t_bl = 16, .t_rcd = 26,
97 .t_rp = 26, .t_ras = 52, .t_rrd = 8, .t_rrd_l = 8,
98 .t_faw = 40, .t_rtp = 12, .t_wtr = 4, .t_wtr_l = 16,
99 .t_wtr_a = 36, .t_wtp = 48, .t_rtw = 0, .t_ccd_l = 8,
100 .t_dllk = 1024, .t_cksre = 9, .t_pd = 13, .t_xp = 13,
101 .t_rfc = 880, .t_mrd = 23, .t_refsbrd = 48, .t_rfcsb = 208,
102 .t_cshsr = 30,
103 .t_zq = 48,
104 },
105};
106
107struct sdrammc {
108 u32 type;
109 void __iomem *regs;
110 void __iomem *phy;
111 void __iomem *scu0;
112 void __iomem *scu1;
113 const struct sdrammc_ac_timing *ac;
114 struct ram_info info;
115};
116
117static size_t ast2700_sdrammc_get_vga_mem_size(struct sdrammc *sdrammc)
118{
119 struct sdrammc_regs *regs = sdrammc->regs;
120 void *scu0 = sdrammc->scu0;
121 size_t vga_memsz[] = {
122 SZ_32M,
123 SZ_64M,
124 };
125 u32 reg, sel, dual = 0;
126
127 sel = readl(&regs->gfmcfg) & 0x1;
128
129 reg = readl(scu0 + SCU0_PCI_MISC70);
130 if (reg & SCU0_PCI_MISC70_EN_PCIEVGA0) {
131 debug("VGA0:%dMB\n", vga_memsz[sel] / SZ_1M);
132 dual++;
133 }
134
135 reg = readl(scu0 + SCU0_PCI_MISC80);
136 if (reg & SCU0_PCI_MISC80_EN_PCIEVGA1) {
137 debug("VGA1:%dMB\n", vga_memsz[sel] / SZ_1M);
138 dual++;
139 }
140
141 return vga_memsz[sel] * dual;
142}
143
144static int sdrammc_calc_size(struct sdrammc *sdrammc)
145{
146 struct sdrammc_regs *regs = sdrammc->regs;
147 u32 val, test_pattern = 0xdeadbeef;
148 size_t sz;
149
150 struct {
151 u32 size;
152 int rfc[2];
153 } ddr_capacity[] = {
154 { 0x10000000UL, {208, 256} }, /* 256MB */
155 { 0x20000000UL, {208, 416} }, /* 512MB */
156 { 0x40000000UL, {208, 560} }, /* 1GB */
157 { 0x80000000UL, {472, 880} }, /* 2GB */
158 };
159
160 /* Configure ram size to max to enable whole area */
161 val = readl(&regs->mcfg);
162 val &= ~(0x7 << 2);
163 writel(val | (DDR_SIZE_2GB << 2), &regs->mcfg);
164
165 /* Clear basement. */
166 writel(0, (void *)CFG_SYS_SDRAM_BASE);
167
168 for (sz = DDR_SIZE_2GB - 1; sz > DDR_SIZE_256MB; sz--) {
169 test_pattern = (test_pattern << 4) + sz;
170 writel(test_pattern, (void *)(CFG_SYS_SDRAM_BASE + ddr_capacity[sz].size));
171
172 if (readl((void *)CFG_SYS_SDRAM_BASE) != test_pattern)
173 break;
174 }
175
176 /* re-configure ram size to dramc. */
177 val = readl(&regs->mcfg);
178 val &= ~(0x7 << 2);
179 writel(val | ((sz + 1) << 2), &regs->mcfg);
180
181 /* update rfc in ac_timing5 register. */
182 val = readl(&regs->actime5);
183 val &= ~(0x3ff);
184 val |= (ddr_capacity[sz + 1].rfc[IS_DDR4(sdrammc->type)] >> 1);
185 writel(val, &regs->actime5);
186
187 /* report actual ram base and size to kernel */
188 sdrammc->info.base = CFG_SYS_SDRAM_BASE;
189 sdrammc->info.size = ddr_capacity[sz + 1].size;
190
191 /* reserve the VGA memory */
192 sdrammc->info.size -= ast2700_sdrammc_get_vga_mem_size(sdrammc);
193
194 return 0;
195}
196
197static int sdrammc_bist(struct sdrammc *sdrammc, u32 addr, u32 size, u32 cfg, u32 timeout)
198{
199 struct sdrammc_regs *regs = sdrammc->regs;
200 u32 val;
201 u32 err = 0;
202
203 writel(0, &regs->bistcfg);
204 writel(cfg, &regs->bistcfg);
205 writel(addr >> 4, &regs->bist_addr);
206 writel(size >> 4, &regs->bist_size);
207 writel(0x89abcdef, &regs->bist_patt);
208 writel(cfg | DRAMC_BISTCFG_START, &regs->bistcfg);
209
210 while (!(readl(&regs->intr_status) & DRAMC_IRQSTA_BIST_DONE))
211 ;
212
213 writel(DRAMC_IRQSTA_BIST_DONE, &regs->intr_clear);
214
215 val = readl(&regs->bist_res);
216
217 if (val & DRAMC_BISTRES_DONE) {
218 if (val & DRAMC_BISTRES_FAIL)
219 err++;
220 } else {
221 err++;
222 }
223
224 return err;
225}
226
227static void sdrammc_enable_refresh(struct sdrammc *sdrammc)
228{
229 struct sdrammc_regs *regs = sdrammc->regs;
230
231 /* refresh update */
232 clrbits_le32(&regs->refctl, 0x8000);
233}
234
235static void sdrammc_mr_send(struct sdrammc *sdrammc, u32 ctrl, u32 op)
236{
237 struct sdrammc_regs *regs = sdrammc->regs;
238
239 writel(op, &regs->mrwr);
240 writel(ctrl | DRAMC_MRCTL_CMD_START, &regs->mrctl);
241
242 while (!(readl(&regs->intr_status) & DRAMC_IRQSTA_MR_DONE))
243 ;
244
245 writel(DRAMC_IRQSTA_MR_DONE, &regs->intr_clear);
246}
247
248static void sdrammc_config_mrs(struct sdrammc *sdrammc)
249{
250 const struct sdrammc_ac_timing *ac = sdrammc->ac;
251 struct sdrammc_regs *regs = sdrammc->regs;
252 u32 mr0_cas, mr0_rtp, mr0_val;
253 u32 mr6_tccd_l, mr6_val;
254 u32 mr2_cwl, mr2_val;
255 u32 mr1_val;
256 u32 mr3_val;
257 u32 mr4_val;
258 u32 mr5_val;
259
260 if (!IS_DDR4(sdrammc->type))
261 return;
262
263 //-------------------------------------------------------------------
264 // CAS Latency (Table-15)
265 //-------------------------------------------------------------------
266 switch (ac->t_cl) {
267 case 9:
268 mr0_cas = 0x00; //5'b00000;
269 break;
270 case 10:
271 mr0_cas = 0x01; //5'b00001;
272 break;
273 case 11:
274 mr0_cas = 0x02; //5'b00010;
275 break;
276 case 12:
277 mr0_cas = 0x03; //5'b00011;
278 break;
279 case 13:
280 mr0_cas = 0x04; //5'b00100;
281 break;
282 case 14:
283 mr0_cas = 0x05; //5'b00101;
284 break;
285 case 15:
286 mr0_cas = 0x06; //5'b00110;
287 break;
288 case 16:
289 mr0_cas = 0x07; //5'b00111;
290 break;
291 case 18:
292 mr0_cas = 0x08; //5'b01000;
293 break;
294 case 20:
295 mr0_cas = 0x09; //5'b01001;
296 break;
297 case 22:
298 mr0_cas = 0x0a; //5'b01010;
299 break;
300 case 24:
301 mr0_cas = 0x0b; //5'b01011;
302 break;
303 case 23:
304 mr0_cas = 0x0c; //5'b01100;
305 break;
306 case 17:
307 mr0_cas = 0x0d; //5'b01101;
308 break;
309 case 19:
310 mr0_cas = 0x0e; //5'b01110;
311 break;
312 case 21:
313 mr0_cas = 0x0f; //5'b01111;
314 break;
315 case 25:
316 mr0_cas = 0x10; //5'b10000;
317 break;
318 case 26:
319 mr0_cas = 0x11; //5'b10001;
320 break;
321 case 27:
322 mr0_cas = 0x12; //5'b10010;
323 break;
324 case 28:
325 mr0_cas = 0x13; //5'b10011;
326 break;
327 case 30:
328 mr0_cas = 0x15; //5'b10101;
329 break;
330 case 32:
331 mr0_cas = 0x17; //5'b10111;
332 break;
333 }
334
335 //-------------------------------------------------------------------
336 // WR and RTP (Table-14)
337 //-------------------------------------------------------------------
338 switch (ac->t_rtp) {
339 case 5:
340 mr0_rtp = 0x0; //4'b0000;
341 break;
342 case 6:
343 mr0_rtp = 0x1; //4'b0001;
344 break;
345 case 7:
346 mr0_rtp = 0x2; //4'b0010;
347 break;
348 case 8:
349 mr0_rtp = 0x3; //4'b0011;
350 break;
351 case 9:
352 mr0_rtp = 0x4; //4'b0100;
353 break;
354 case 10:
355 mr0_rtp = 0x5; //4'b0101;
356 break;
357 case 12:
358 mr0_rtp = 0x6; //4'b0110;
359 break;
360 case 11:
361 mr0_rtp = 0x7; //4'b0111;
362 break;
363 case 13:
364 mr0_rtp = 0x8; //4'b1000;
365 break;
366 }
367
368 //-------------------------------------------------------------------
369 // CAS Write Latency (Table-21)
370 //-------------------------------------------------------------------
371 switch (ac->t_cwl) {
372 case 9:
373 mr2_cwl = 0x0; // 3'b000; // 1600
374 break;
375 case 10:
376 mr2_cwl = 0x1; // 3'b001; // 1866
377 break;
378 case 11:
379 mr2_cwl = 0x2; // 3'b010; // 2133
380 break;
381 case 12:
382 mr2_cwl = 0x3; // 3'b011; // 2400
383 break;
384 case 14:
385 mr2_cwl = 0x4; // 3'b100; // 2666
386 break;
387 case 16:
388 mr2_cwl = 0x5; // 3'b101; // 2933/3200
389 break;
390 case 18:
391 mr2_cwl = 0x6; // 3'b110;
392 break;
393 case 20:
394 mr2_cwl = 0x7; // 3'b111;
395 break;
396 }
397
398 //-------------------------------------------------------------------
399 // tCCD_L and tDLLK
400 //-------------------------------------------------------------------
401 switch (ac->t_ccd_l) {
402 case 4:
403 mr6_tccd_l = 0x0; //3'b000; // rate <= 1333
404 break;
405 case 5:
406 mr6_tccd_l = 0x1; //3'b001; // 1333 < rate <= 1866
407 break;
408 case 6:
409 mr6_tccd_l = 0x2; //3'b010; // 1866 < rate <= 2400
410 break;
411 case 7:
412 mr6_tccd_l = 0x3; //3'b011; // 2400 < rate <= 2666
413 break;
414 case 8:
415 mr6_tccd_l = 0x4; //3'b100; // 2666 < rate <= 3200
416 break;
417 }
418
419 /*
420 * mr0_val =
421 * mr0_rtp[3], // 13
422 * mr0_cas[4], // 12
423 * mr0_rtp[2:0], // 13,11-9: WR and RTP
424 * 1'b0, // 8: DLL reset
425 * 1'b0, // 7: TM
426 * mr0_cas[3:1], // 6-4,2: CAS latency
427 * 1'b0, // 3: sequential
428 * mr0_cas[0],
429 * 2'b00 // 1-0: burst length
430 */
431 mr0_val = ((mr0_cas & 0x1) << 2) |
432 (((mr0_cas >> 1) & 0x7) << 4) |
433 (((mr0_cas >> 4) & 0x1) << 12) |
434 ((mr0_rtp & 0x7) << 9) |
435 (((mr0_rtp >> 3) & 0x1) << 13);
436
437 /*
438 * 3'b2 //[10:8]: rtt_nom, 000:disable,001:rzq/4,010:rzq/2,011:rzq/6,100:rzq/1,101:rzq/5,110:rzq/3,111:rzq/7
439 * 1'b0 //[7]: write leveling enable
440 * 2'b0 //[6:5]: reserved
441 * 2'b0 //[4:3]: additive latency
442 * 2'b0 //[2:1]: output driver impedance
443 * 1'b1 //[0]: enable dll
444 */
445 mr1_val = 0x201;
446
447 /*
448 * [10:9]: rtt_wr, 00:dynamic odt off, 01:rzq/2, 10:rzq/1, 11: hi-z
449 * [8]: 0
450 */
451 mr2_val = ((mr2_cwl & 0x7) << 3) | 0x200;
452
453 mr3_val = 0;
454
455 mr4_val = 0;
456
457 /*
458 * mr5_val = {
459 * 1'b0, // 13: RFU
460 * 1'b0, // 12: read DBI
461 * 1'b0, // 11: write DBI
462 * 1'b1, // 10: Data mask
463 * 1'b0, // 9: C/A parity persistent error
464 * 3'b000, // 8-6: RTT_PARK (disable)
465 * 1'b1, // 5: ODT input buffer during power down mode
466 * 1'b0, // 4: C/A parity status
467 * 1'b0, // 3: CRC error clear
468 * 3'b0 // 2-0: C/A parity latency mode
469 * };
470 */
471 mr5_val = 0x420;
472
473 /*
474 * mr6_val = {
475 * 1'b0, // 13, 9-8: RFU
476 * mr6_tccd_l[2:0], // 12-10: tCCD_L
477 * 2'b0, // 13, 9-8: RFU
478 * 1'b0, // 7: VrefDQ training enable
479 * 1'b0, // 6: VrefDQ training range
480 * 6'b0 // 5-0: VrefDQ training value
481 * };
482 */
483 mr6_val = ((mr6_tccd_l & 0x7) << 10);
484
485 writel((mr1_val << 16) + mr0_val, &regs->mr01);
486 writel((mr3_val << 16) + mr2_val, &regs->mr23);
487 writel((mr5_val << 16) + mr4_val, &regs->mr45);
488 writel(mr6_val, &regs->mr67);
489
490 /* Power-up initialization sequence */
491 sdrammc_mr_send(sdrammc, MR_ADDR(3), 0);
492 sdrammc_mr_send(sdrammc, MR_ADDR(6), 0);
493 sdrammc_mr_send(sdrammc, MR_ADDR(5), 0);
494 sdrammc_mr_send(sdrammc, MR_ADDR(4), 0);
495 sdrammc_mr_send(sdrammc, MR_ADDR(2), 0);
496 sdrammc_mr_send(sdrammc, MR_ADDR(1), 0);
497 sdrammc_mr_send(sdrammc, MR_ADDR(0), 0);
498}
499
500static void sdrammc_exit_self_refresh(struct sdrammc *sdrammc)
501{
502 struct sdrammc_regs *regs = sdrammc->regs;
503
504 /* exit self-refresh after phy init */
505 setbits_le32(&regs->mctl, DRAMC_MCTL_SELF_REF_START);
506
507 /* query if self-ref done */
508 while (!(readl(&regs->intr_status) & DRAMC_IRQSTA_REF_DONE))
509 ;
510
511 /* clear status */
512 writel(DRAMC_IRQSTA_REF_DONE, &regs->intr_clear);
513 udelay(1);
514}
515
516/* user-customized functions for the vendor PHY init code */
517#define DWC_PHY_IMEM_OFST 0x50000
518#define DWC_PHY_DMEM_OFST 0x58000
519#define DWC_PHY_MB_START_STREAM_MSG 0x8
520#define DWC_PHY_MB_TRAIN_SUCCESS 0x7
521#define DWC_PHY_MB_TRAIN_FAIL 0xff
522
523#define dwc_ddrphy_apb_wr(addr, data) \
524 writew((data), sdrammc->phy + ((addr) << 1))
525
526#define dwc_ddrphy_apb_rd(addr) \
527 readw(sdrammc->phy + ((addr) << 1))
528
529#define dwc_ddrphy_apb_wr_32b(addr, data) \
530 writel((data), sdrammc->phy + ((addr) << 1))
531
532#define dwc_ddrphy_apb_rd_32b(addr) \
533 readl(sdrammc->phy + ((addr) << 1))
534
535void dwc_get_mailbox(struct sdrammc *sdrammc, const int mode, u32 *mbox)
536{
537 u32 val;
538
539 /* 1. Poll the UctWriteProtShadow, looking for a 0 */
540 while (dwc_ddrphy_apb_rd(0xd0004) & BIT(0))
541 ;
542
543 /* 2. When a 0 is seen, read the UctWriteOnlyShadow register to get the major message number. */
544 *mbox = dwc_ddrphy_apb_rd(0xd0032) & 0xffff;
545
546 /* 3. If reading a streaming or SMBus message, also read the UctDatWriteOnlyShadow register. */
547 if (mode) {
548 val = (dwc_ddrphy_apb_rd(0xd0034)) & 0xffff;
549 *mbox |= (val << 16);
550 }
551
552 /* 4. Write the DctWriteProt to 0 to acknowledge the reception of the message */
553 dwc_ddrphy_apb_wr(0xd0031, 0);
554
555 /* 5. Poll the UctWriteProtShadow, looking for a 1 */
556 while (!(dwc_ddrphy_apb_rd(0xd0004) & BIT(0)))
557 ;
558
559 /* 6. When a 1 is seen, write the DctWriteProt to 1 to complete the protocol */
560 dwc_ddrphy_apb_wr(0xd0031, 1);
561}
562
563uint32_t dwc_readMsgBlock(struct sdrammc *sdrammc, const u32 addr_half)
564{
565 u32 data_word;
566
567 data_word = dwc_ddrphy_apb_rd_32b((addr_half >> 1) << 1);
568
569 if (addr_half & 0x1)
570 data_word = data_word >> 16;
571 else
572 data_word &= 0xffff;
573
574 return data_word;
575}
576
577int dwc_ddrphy_phyinit_userCustom_H_readMsgBlock(struct sdrammc *sdrammc, int train2D)
578{
579 u32 msg;
580
581 if (IS_DDR4(sdrammc->type)) {
582 /* DWC_PHY_DDR4_MB_RESULT */
583 msg = dwc_readMsgBlock(sdrammc, 0x5800a);
584 if (msg & 0xff)
585 debug("%s: Training Failure index (0x%x)\n", __func__, msg);
586 else
587 debug("%s: %dD Training Passed\n", __func__, train2D ? 2 : 1);
588 } else {
589 /* DWC_PHY_DDR5_MB_RESULT */
590 msg = dwc_readMsgBlock(sdrammc, 0x58007);
591 if (msg & 0xff00)
592 debug("%s: Training Failure index (0x%x)\n", __func__, msg);
593 else
594 debug("%s: DDR5 1D/2D Training Passed\n", __func__);
595
596 /* DWC_PHY_DDR5_MB_RESULT_ADR */
597 msg = dwc_readMsgBlock(sdrammc, 0x5800a);
598 debug("%s: Result Address Offset (0x%x)\n", __func__, msg);
599 }
600
601 return 0;
602}
603
604void dwc_ddrphy_phyinit_userCustom_A_bringupPower(void)
605{
606 /* do nothing */
607}
608
609void dwc_ddrphy_phyinit_userCustom_B_startClockResetPhy(struct sdrammc *sdrammc)
610{
611 struct sdrammc_regs *regs = sdrammc->regs;
612
613 /*
614 * 1. Drive PwrOkIn to 0. Note: Reset, DfiClk, and APBCLK can be X.
615 * 2. Start DfiClk and APBCLK
616 * 3. Drive Reset to 1 and PRESETn_APB to 0.
617 * Note: The combination of PwrOkIn=0 and Reset=1 signals a cold reset to the PHY.
618 */
619 writel(DRAMC_MCTL_PHY_RESET, &regs->mctl);
620 udelay(2);
621
622 /*
623 * 5. Drive PwrOkIn to 1. Once the PwrOkIn is asserted (and Reset is still asserted),
624 * DfiClk synchronously switches to any legal input frequency.
625 */
626 writel(DRAMC_MCTL_PHY_RESET | DRAMC_MCTL_PHY_POWER_ON, &regs->mctl);
627 udelay(2);
628
629 /*
630 * 7. Drive Reset to 0. Note: All DFI and APB inputs must be driven at valid reset states
631 * before the deassertion of Reset.
632 */
633 writel(DRAMC_MCTL_PHY_POWER_ON, &regs->mctl);
634 udelay(2);
635
636 /*
637 * 9. Drive PRESETn_APB to 1 to de-assert reset on the ABP bus.
638 * 10. The PHY is now in the reset state and is ready to accept APB transactions.
639 */
640}
641
642void dwc_ddrphy_phyinit_userCustom_overrideUserInput(void)
643{
644 /* do nothing */
645}
646
647void dwc_ddrphy_phyinit_userCustom_customPostTrain(void)
648{
649 /* do nothing */
650}
651
652void dwc_ddrphy_phyinit_userCustom_E_setDfiClk(struct sdrammc *sdrammc)
653{
654 dwc_ddrphy_apb_wr(0xd0031, 1); /* DWC_DCTWRITEPROT */
655 dwc_ddrphy_apb_wr(0xd0033, 1); /* DWC_UCTWRITEPROT */
656}
657
658void dwc_ddrphy_phyinit_userCustom_G_waitFwDone(struct sdrammc *sdrammc)
659{
660 u32 mbox, msg = 0;
661
662 while (msg != DWC_PHY_MB_TRAIN_SUCCESS && msg != DWC_PHY_MB_TRAIN_FAIL) {
663 dwc_get_mailbox(sdrammc, 0, &mbox);
664 msg = mbox & 0xffff;
665 }
666}
667
668void dwc_ddrphy_phyinit_userCustom_J_enterMissionMode(struct sdrammc *sdrammc)
669{
670 struct sdrammc_regs *regs = sdrammc->regs;
671 u32 val;
672
673 /*
674 * 1. Set the PHY input clocks to the desired frequency.
675 * 2. Initialize the PHY to mission mode by performing DFI Initialization.
676 * Please see the DFI specification for more information. See the DFI frequency bus encoding in section <XXX>.
677 * Note: The PHY training firmware initializes the DRAM state. if skip
678 * training is used, the DRAM state is not initialized.
679 */
680
681 writel(0xffffffff, (void *)&regs->intr_mask);
682
683 writel(0x0, (void *)&regs->dcfg);
684
685 if (!IS_DDR4(sdrammc->type)) {
686 dwc_ddrphy_apb_wr(0xd0000, 0); /* DWC_DDRPHYA_APBONLY0_MicroContMuxSel */
687 dwc_ddrphy_apb_wr(0x20240, 0x3900); /* DWC_DDRPHYA_MASTER0_base0_D5ACSMPtr0lat0 */
688 dwc_ddrphy_apb_wr(0x900da, 8); /* DWC_DDRPHYA_INITENG0_base0_SequenceReg0b59s0 */
689 dwc_ddrphy_apb_wr(0xd0000, 1); /* DWC_DDRPHYA_APBONLY0_MicroContMuxSel */
690 }
691
692 /* phy init start */
693 val = readl((void *)&regs->mctl);
694 val = val | DRAMC_MCTL_PHY_INIT_START;
695 writel(val, (void *)&regs->mctl);
696
697 /* wait phy complete */
698 while (1) {
699 val = readl(&regs->intr_status) & DRAMC_IRQSTA_PHY_INIT_DONE;
700 if (val == DRAMC_IRQSTA_PHY_INIT_DONE)
701 break;
702 }
703
704 writel(0xffff, (void *)&regs->intr_clear);
705
706 while (readl((void *)&regs->intr_status))
707 ;
708
709 if (!IS_DDR4(sdrammc->type)) {
710 dwc_ddrphy_apb_wr(0xd0000, 0); /* DWC_DDRPHYA_APBONLY0_MicroContMuxSel */
711 dwc_ddrphy_apb_wr(0x20240, 0x4300); /* DWC_DDRPHYA_MASTER0_base0_D5ACSMPtr0lat0 */
712 dwc_ddrphy_apb_wr(0x900da, 0); /* DWC_DDRPHYA_INITENG0_base0_SequenceReg0b59s0 */
713 dwc_ddrphy_apb_wr(0xd0000, 1); /* DWC_DDRPHYA_APBONLY0_MicroContMuxSel */
714 }
715}
716
717int dwc_ddrphy_phyinit_userCustom_D_loadIMEM(struct sdrammc *sdrammc, const int train2D)
718{
719 u32 imem_ofst, imem_size;
720 u32 pb_type;
721
722 if (IS_DDR4(sdrammc->type))
723 pb_type = (train2D) ? PBT_DDR4_2D_PMU_TRAIN_IMEM : PBT_DDR4_PMU_TRAIN_IMEM;
724 else
725 pb_type = PBT_DDR5_PMU_TRAIN_IMEM;
726
727 fmc_hdr_get_prebuilt(pb_type, &imem_ofst, &imem_size);
728
729 memcpy(sdrammc->phy + (DWC_PHY_IMEM_OFST << 1),
730 (void *)(0x20000000 + imem_ofst), imem_size);
731
732 return 0;
733}
734
735int dwc_ddrphy_phyinit_userCustom_F_loadDMEM(struct sdrammc *sdrammc,
736 const int pState, const int train2D)
737{
738 u32 dmem_ofst, dmem_size;
739 u32 pb_type;
740
741 if (IS_DDR4(sdrammc->type))
742 pb_type = (train2D) ? PBT_DDR4_2D_PMU_TRAIN_DMEM : PBT_DDR4_PMU_TRAIN_DMEM;
743 else
744 pb_type = PBT_DDR5_PMU_TRAIN_DMEM;
745
746 fmc_hdr_get_prebuilt(pb_type, &dmem_ofst, &dmem_size);
747
748 memcpy(sdrammc->phy + (DWC_PHY_DMEM_OFST << 1),
749 (void *)(0x20000000 + dmem_ofst), dmem_size);
750
751 return 0;
752}
753
754static void sdrammc_dwc_phy_init(struct sdrammc *sdrammc)
755{
756 /* enable ddr phy free-run clock */
757 writel(SCU0_CLKGATE1_CLR_DDRPHY, sdrammc->scu0 + SCU0_CLKGATE1_CLR);
758
759 /* include the vendor-provided PHY init code */
760 if (IS_DDR4(sdrammc->type)) {
761 #include "dwc_ddrphy_phyinit_ddr4-3200-nodimm-train2D.c"
762 } else {
763 #include "dwc_ddrphy_phyinit_ddr5-3200-nodimm-train2D.c"
764 }
765}
766
767static void sdrammc_config_ac_timing(struct sdrammc *sdrammc)
768{
769 const struct sdrammc_ac_timing *ac = sdrammc->ac;
770 struct sdrammc_regs *regs = sdrammc->regs;
771 u32 actime;
772
773#define ACTIME1(ccd, rrd_l, rrd, mrd) \
774 (((ccd) << 24) | \
775 (((rrd_l) >> 1) << 16) | \
776 (((rrd) >> 1) << 8) | \
777 ((mrd) >> 1))
778
779#define ACTIME2(faw, rp, ras, rcd) \
780 ((((faw) >> 1) << 24) | \
781 (((rp) >> 1) << 16) | \
782 (((ras) >> 1) << 8) | \
783 ((rcd) >> 1))
784
785#define ACTIME3(wtr, rtw, wtp, rtp) \
786 ((((wtr) >> 1) << 24) | \
787 (((rtw) >> 1) << 16) | \
788 (((wtp) >> 1) << 8) | \
789 ((rtp) >> 1))
790
791#define ACTIME4(wtr_a, wtr_l) \
792 ((((wtr_a) >> 1) << 8) | \
793 ((wtr_l) >> 1))
794
795#define ACTIME5(refsbrd, rfcsb, rfc) \
796 ((((refsbrd) >> 1) << 20) | \
797 (((rfcsb) >> 1) << 10) | \
798 ((rfc) >> 1))
799
800#define ACTIME6(cshsr, pd, xp, cksre) \
801 ((((cshsr) >> 1) << 24) | \
802 (((pd) >> 1) << 16) | \
803 (((xp) >> 1) << 8) | \
804 ((cksre) >> 1))
805
806#define ACTIME7(zqcs, dllk) \
807 ((((zqcs) >> 1) << 10) | \
808 ((dllk) >> 1))
809
810 actime = ACTIME1(ac->t_ccd_l, ac->t_rrd_l, ac->t_rrd, ac->t_mrd);
811 writel(actime, &regs->actime1);
812
813 actime = ACTIME2(ac->t_faw, ac->t_rp, ac->t_ras, ac->t_rcd);
814 writel(actime, &regs->actime2);
815
816 actime = ACTIME3(ac->t_cwl + ac->t_bl / 2 + ac->t_wtr,
817 ac->t_cl - ac->t_cwl + (ac->t_bl / 2) + 2,
818 ac->t_cwl + ac->t_bl / 2 + ac->t_wtp,
819 ac->t_rtp);
820 writel(actime, &regs->actime3);
821
822 actime = ACTIME4(ac->t_cwl + ac->t_bl / 2 + ac->t_wtr_a,
823 ac->t_cwl + ac->t_bl / 2 + ac->t_wtr_l);
824 writel(actime, &regs->actime4);
825
826 actime = ACTIME5(ac->t_refsbrd, ac->t_rfcsb, ac->t_rfc);
827 writel(actime, &regs->actime5);
828
829 actime = ACTIME6(ac->t_cshsr, ac->t_pd, ac->t_xp, ac->t_cksre);
830 writel(actime, &regs->actime6);
831
832 actime = ACTIME7(ac->t_zq, ac->t_dllk);
833 writel(actime, &regs->actime7);
834}
835
836static void sdrammc_config_registers(struct sdrammc *sdrammc)
837{
838 const struct sdrammc_ac_timing *ac = sdrammc->ac;
839 struct sdrammc_regs *regs = sdrammc->regs;
840 u32 reg;
841
842 u32 dram_size = 5;
843 u32 t_phy_wrdata;
844 u32 t_phy_wrlat;
845 u32 t_phy_rddata_en;
846 u32 t_phy_odtlat;
847 u32 t_phy_odtext;
848
849 if (IS_DDR4(sdrammc->type)) {
850 t_phy_wrlat = ac->t_cwl - 5 - 4;
851 t_phy_rddata_en = ac->t_cl - 5 - 4;
852 t_phy_wrdata = 2;
853 t_phy_odtlat = ac->t_cwl - 5 - 4;
854 t_phy_odtext = 0;
855 } else {
856 t_phy_wrlat = ac->t_cwl - 13 - 3;
857 t_phy_rddata_en = ac->t_cl - 13 - 3;
858 t_phy_wrdata = 6;
859 t_phy_odtlat = 0;
860 t_phy_odtext = 0;
861 }
862
863 writel(0x20 + (dram_size << 2) + !!!IS_DDR4(sdrammc->type), &regs->mcfg);
864
865 reg = (t_phy_odtext << 20) + (t_phy_odtlat << 16) +
866 (t_phy_rddata_en << 10) + (t_phy_wrdata << 6) +
867 t_phy_wrlat;
868 writel(reg, &regs->dfi_timing);
869 writel(0, &regs->dctl);
870
871 writel(0x40b48200, &regs->refctl);
872
873 writel(0x42aa1800, &regs->zqctl);
874
875 writel(0, &regs->arbctl);
876
877 if (!IS_DDR4(sdrammc->type))
878 writel(0, &regs->refmng_ctl);
879
880 writel(0xffffffff, &regs->intr_mask);
881}
882
883static void sdrammc_init(struct sdrammc *sdrammc)
884{
885 u32 reg;
886
887 reg = readl(sdrammc->scu1 + SCU1_HWSTRAP1);
888
889 if (reg & SCU1_HWSTRAP1_DDR4) {
890 if (IS_ENABLED(CONFIG_ASPEED_DDR_1600))
891 sdrammc->type = DDR4_1600;
892 else if (IS_ENABLED(CONFIG_ASPEED_DDR_2400))
893 sdrammc->type = DDR4_2400;
894 else if (IS_ENABLED(CONFIG_ASPEED_DDR_3200))
895 sdrammc->type = DDR4_3200;
896 } else {
897 sdrammc->type = DDR5_3200;
898 }
899
900 sdrammc->ac = &ac_table[sdrammc->type];
901
902 sdrammc_config_ac_timing(sdrammc);
903 sdrammc_config_registers(sdrammc);
904}
905
906static int ast2700_sdrammc_probe(struct udevice *dev)
907{
908 struct sdrammc *sdrammc = dev_get_priv(dev);
909 struct sdrammc_regs *regs = sdrammc->regs;
910 u32 bistcfg;
911 u32 reg;
912 int rc;
913
914 /* skip DRAM init if already done */
915 reg = readl(sdrammc->scu0 + SCU0_VGA0_SCRATCH);
916 if (reg & SCU0_VGA0_SCRATCH_DRAM_INIT)
917 goto out;
918
919 /* unlock DRAM controller */
920 writel(DRAMC_UNLK_KEY, &regs->prot_key);
921
922 sdrammc_init(sdrammc);
923
924 sdrammc_dwc_phy_init(sdrammc);
925
926 sdrammc_exit_self_refresh(sdrammc);
927
928 sdrammc_config_mrs(sdrammc);
929
930 sdrammc_enable_refresh(sdrammc);
931
932 bistcfg = FIELD_PREP(DRAMC_BISTCFG_PMODE, BIST_PMODE_CRC) |
933 FIELD_PREP(DRAMC_BISTCFG_BMODE, BIST_BMODE_RW_SWITCH) |
934 DRAMC_BISTCFG_ENABLE;
935
936 rc = sdrammc_bist(sdrammc, 0, 0x10000, bistcfg, 0x200000);
937 if (rc) {
938 debug("bist test failed, type=%d\n", sdrammc->type);
939 return rc;
940 }
941
942 /* set DRAM init flag */
943 reg |= SCU0_VGA0_SCRATCH_DRAM_INIT;
944 writel(reg, sdrammc->scu0 + SCU0_VGA0_SCRATCH);
945
946out:
947 sdrammc_calc_size(sdrammc);
948
949 return 0;
950}
951
952static int ast2700_sdrammc_of_to_plat(struct udevice *dev)
953{
954 struct sdrammc *sdrammc = dev_get_priv(dev);
955 u32 phandle;
956 ofnode node;
957 int rc;
958
959 sdrammc->regs = (struct sdrammc_regs *)devfdt_get_addr_index(dev, 0);
960 if (sdrammc->regs == (void *)FDT_ADDR_T_NONE) {
961 debug("cannot map DRAM register\n");
962 return -ENODEV;
963 }
964
965 sdrammc->phy = (void *)devfdt_get_addr_index(dev, 1);
966 if (sdrammc->phy == (void *)FDT_ADDR_T_NONE) {
967 debug("cannot map PHY memory\n");
968 return -ENODEV;
969 }
970
971 rc = ofnode_read_u32(dev_ofnode(dev), "aspeed,scu0", &phandle);
972 if (rc) {
973 debug("cannot find SCU0 handle\n");
974 return -ENODEV;
975 }
976
977 node = ofnode_get_by_phandle(phandle);
978 if (!ofnode_valid(node)) {
979 debug("cannot get SCU0 node\n");
980 return -ENODEV;
981 }
982
983 sdrammc->scu0 = (void *)ofnode_get_addr(node);
984 if (sdrammc->scu0 == (void *)FDT_ADDR_T_NONE) {
985 debug("cannot map SCU0 register\n");
986 return -ENODEV;
987 }
988
989 rc = ofnode_read_u32(dev_ofnode(dev), "aspeed,scu1", &phandle);
990 if (rc) {
991 debug("cannot find SCU1 handle\n");
992 return -ENODEV;
993 }
994
995 node = ofnode_get_by_phandle(phandle);
996 if (!ofnode_valid(node)) {
997 debug("cannot get SCU1 node\n");
998 return -ENODEV;
999 }
1000
1001 sdrammc->scu1 = (void *)ofnode_get_addr(node);
1002 if (sdrammc->scu1 == (void *)FDT_ADDR_T_NONE) {
1003 debug("cannot map SCU1 register\n");
1004 return -ENODEV;
1005 }
1006
1007 return 0;
1008}
1009
1010static int ast2700_sdrammc_get_info(struct udevice *dev, struct ram_info *info)
1011{
1012 struct sdrammc *sdrammc = dev_get_priv(dev);
1013
1014 *info = sdrammc->info;
1015
1016 return 0;
1017}
1018
1019static struct ram_ops ast2700_sdrammc_ops = {
1020 .get_info = ast2700_sdrammc_get_info,
1021};
1022
1023static const struct udevice_id ast2700_sdrammc_ids[] = {
1024 { .compatible = "aspeed,ast2700-sdrammc" },
1025 { }
1026};
1027
1028U_BOOT_DRIVER(sdrammc_ast2700) = {
1029 .name = "aspeed_ast2700_sdrammc",
1030 .id = UCLASS_RAM,
1031 .of_match = ast2700_sdrammc_ids,
1032 .ops = &ast2700_sdrammc_ops,
1033 .of_to_plat = ast2700_sdrammc_of_to_plat,
1034 .probe = ast2700_sdrammc_probe,
1035 .priv_auto = sizeof(struct sdrammc),
1036};