2 * sun50i H6 platform dram controller init
4 * (C) Copyright 2017 Icenowy Zheng <icenowy@aosc.io>
6 * SPDX-License-Identifier: GPL-2.0+
10 #include <asm/arch/clock.h>
11 #include <asm/arch/dram.h>
12 #include <asm/arch/cpu.h>
13 #include <linux/bitops.h>
14 #include <linux/kconfig.h>
17 * The DRAM controller structure on H6 is similar to the ones on A23/A80:
18 * they all contains 3 parts, COM, CTL and PHY. (As a note on A33/A83T/H3/A64
19 * /H5/R40 CTL and PHY is composed).
21 * COM is allwinner-specific. On H6, the address mapping function is moved
22 * from COM to CTL (with the standard ADDRMAP registers on DesignWare memory
25 * CTL (controller) and PHY is from DesignWare.
27 * The CTL part is a bit similar to the one on A23/A80 (because they all
28 * originate from DesignWare), but gets more registers added.
30 * The PHY part is quite new, not seen in any previous Allwinner SoCs, and
31 * not seen on other SoCs in U-Boot. The only SoC that is also known to have
32 * similar PHY is ZynqMP.
35 static void mctl_sys_init(struct dram_para *para);
36 static void mctl_com_init(struct dram_para *para);
37 static void mctl_channel_init(struct dram_para *para);
39 static void mctl_core_init(struct dram_para *para)
44 case SUNXI_DRAM_TYPE_LPDDR3:
45 case SUNXI_DRAM_TYPE_DDR3:
46 mctl_set_timing_params(para);
49 panic("Unsupported DRAM type!");
51 mctl_channel_init(para);
54 /* PHY initialisation */
55 static void mctl_phy_pir_init(u32 val)
57 struct sunxi_mctl_phy_reg * const mctl_phy =
58 (struct sunxi_mctl_phy_reg *)SUNXI_DRAM_PHY0_BASE;
60 writel(val, &mctl_phy->pir);
61 writel(val | BIT(0), &mctl_phy->pir); /* Start initialisation. */
62 mctl_await_completion(&mctl_phy->pgsr[0], BIT(0), BIT(0));
92 inline void mbus_configure_port(u8 port,
102 struct sunxi_mctl_com_reg * const mctl_com =
103 (struct sunxi_mctl_com_reg *)SUNXI_DRAM_COM_BASE;
105 const u32 cfg0 = ( (bwlimit ? (1 << 0) : 0)
106 | (priority ? (1 << 1) : 0)
108 | ((waittime & 0xf) << 4)
109 | ((acs & 0xff) << 8)
111 const u32 cfg1 = ((u32)bwl2 << 16) | (bwl1 & 0xffff);
113 debug("MBUS port %d cfg0 %08x cfg1 %08x\n", port, cfg0, cfg1);
114 writel(cfg0, &mctl_com->master[port].cfg0);
115 writel(cfg1, &mctl_com->master[port].cfg1);
118 #define MBUS_CONF(port, bwlimit, qos, acs, bwl0, bwl1, bwl2) \
119 mbus_configure_port(MBUS_PORT_ ## port, bwlimit, false, \
120 MBUS_QOS_ ## qos, 0, acs, bwl0, bwl1, bwl2)
122 static void mctl_set_master_priority(void)
124 struct sunxi_mctl_com_reg * const mctl_com =
125 (struct sunxi_mctl_com_reg *)SUNXI_DRAM_COM_BASE;
127 /* enable bandwidth limit windows and set windows size 1us */
128 writel(399, &mctl_com->tmr);
129 writel(BIT(16), &mctl_com->bwcr);
131 MBUS_CONF( CPU, true, HIGHEST, 0, 256, 128, 100);
132 MBUS_CONF( GPU, true, HIGH, 0, 1536, 1400, 256);
133 MBUS_CONF( MAHB, true, HIGHEST, 0, 512, 256, 96);
134 MBUS_CONF( DMA, true, HIGH, 0, 256, 100, 80);
135 MBUS_CONF( VE, true, HIGH, 2, 8192, 5500, 5000);
136 MBUS_CONF( CE, true, HIGH, 2, 100, 64, 32);
137 MBUS_CONF( TSC0, true, HIGH, 2, 100, 64, 32);
138 MBUS_CONF(NDFC0, true, HIGH, 0, 256, 128, 64);
139 MBUS_CONF( CSI0, true, HIGH, 0, 256, 128, 100);
140 MBUS_CONF( DI0, true, HIGH, 0, 1024, 256, 64);
141 MBUS_CONF(DE300, true, HIGHEST, 6, 8192, 2800, 2400);
142 MBUS_CONF(IOMMU, true, HIGHEST, 0, 100, 64, 32);
143 MBUS_CONF( VE2, true, HIGH, 2, 8192, 5500, 5000);
144 MBUS_CONF( USB3, true, HIGH, 0, 256, 128, 64);
145 MBUS_CONF( PCIE, true, HIGH, 2, 100, 64, 32);
146 MBUS_CONF( VP9, true, HIGH, 2, 8192, 5500, 5000);
147 MBUS_CONF(HDCP2, true, HIGH, 2, 100, 64, 32);
150 static void mctl_sys_init(struct dram_para *para)
152 struct sunxi_ccm_reg * const ccm =
153 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
154 struct sunxi_mctl_com_reg * const mctl_com =
155 (struct sunxi_mctl_com_reg *)SUNXI_DRAM_COM_BASE;
156 struct sunxi_mctl_ctl_reg * const mctl_ctl =
157 (struct sunxi_mctl_ctl_reg *)SUNXI_DRAM_CTL0_BASE;
159 /* Put all DRAM-related blocks to reset state */
160 clrbits_le32(&ccm->mbus_cfg, MBUS_ENABLE | MBUS_RESET);
161 clrbits_le32(&ccm->dram_gate_reset, BIT(0));
163 writel(0, &ccm->dram_gate_reset);
164 clrbits_le32(&ccm->pll5_cfg, CCM_PLL5_CTRL_EN);
165 clrbits_le32(&ccm->dram_clk_cfg, DRAM_MOD_RESET);
169 /* Set PLL5 rate to doubled DRAM clock rate */
170 writel(CCM_PLL5_CTRL_EN | CCM_PLL5_LOCK_EN |
171 CCM_PLL5_CTRL_N(para->clk * 2 / 24 - 1), &ccm->pll5_cfg);
172 mctl_await_completion(&ccm->pll5_cfg, CCM_PLL5_LOCK, CCM_PLL5_LOCK);
174 /* Configure DRAM mod clock */
175 writel(DRAM_CLK_SRC_PLL5, &ccm->dram_clk_cfg);
176 setbits_le32(&ccm->dram_clk_cfg, DRAM_CLK_UPDATE);
177 writel(BIT(RESET_SHIFT), &ccm->dram_gate_reset);
179 setbits_le32(&ccm->dram_gate_reset, BIT(0));
181 /* Disable all channels */
182 writel(0, &mctl_com->maer0);
183 writel(0, &mctl_com->maer1);
184 writel(0, &mctl_com->maer2);
186 /* Configure MBUS and enable DRAM mod reset */
187 setbits_le32(&ccm->mbus_cfg, MBUS_RESET);
188 setbits_le32(&ccm->mbus_cfg, MBUS_ENABLE);
189 setbits_le32(&ccm->dram_clk_cfg, DRAM_MOD_RESET);
192 /* Unknown hack from the BSP, which enables access of mctl_ctl regs */
193 writel(0x8000, &mctl_ctl->unk_0x00c);
196 static void mctl_set_addrmap(struct dram_para *para)
198 struct sunxi_mctl_ctl_reg * const mctl_ctl =
199 (struct sunxi_mctl_ctl_reg *)SUNXI_DRAM_CTL0_BASE;
200 u8 cols = para->cols;
201 u8 rows = para->rows;
202 u8 ranks = para->ranks;
204 if (!para->bus_full_width)
209 mctl_ctl->addrmap[0] = rows + cols - 3;
211 mctl_ctl->addrmap[0] = 0x1F;
213 /* Banks, hardcoded to 8 banks now */
214 mctl_ctl->addrmap[1] = (cols - 2) | (cols - 2) << 8 | (cols - 2) << 16;
217 mctl_ctl->addrmap[2] = 0;
220 mctl_ctl->addrmap[3] = 0x1F1F1F00;
221 mctl_ctl->addrmap[4] = 0x1F1F;
224 mctl_ctl->addrmap[3] = 0x1F1F0000;
225 mctl_ctl->addrmap[4] = 0x1F1F;
228 mctl_ctl->addrmap[3] = 0x1F000000;
229 mctl_ctl->addrmap[4] = 0x1F1F;
232 mctl_ctl->addrmap[3] = 0;
233 mctl_ctl->addrmap[4] = 0x1F1F;
236 mctl_ctl->addrmap[3] = 0;
237 mctl_ctl->addrmap[4] = 0x1F00;
240 mctl_ctl->addrmap[3] = 0;
241 mctl_ctl->addrmap[4] = 0;
244 panic("Unsupported DRAM configuration: column number invalid\n");
248 mctl_ctl->addrmap[5] = (cols - 3) | ((cols - 3) << 8) | ((cols - 3) << 16) | ((cols - 3) << 24);
251 mctl_ctl->addrmap[6] = (cols - 3) | 0x0F0F0F00;
252 mctl_ctl->addrmap[7] = 0x0F0F;
255 mctl_ctl->addrmap[6] = (cols - 3) | ((cols - 3) << 8) | 0x0F0F0000;
256 mctl_ctl->addrmap[7] = 0x0F0F;
259 mctl_ctl->addrmap[6] = (cols - 3) | ((cols - 3) << 8) | ((cols - 3) << 16) | 0x0F000000;
260 mctl_ctl->addrmap[7] = 0x0F0F;
263 mctl_ctl->addrmap[6] = (cols - 3) | ((cols - 3) << 8) | ((cols - 3) << 16) | ((cols - 3) << 24);
264 mctl_ctl->addrmap[7] = 0x0F0F;
267 mctl_ctl->addrmap[6] = (cols - 3) | ((cols - 3) << 8) | ((cols - 3) << 16) | ((cols - 3) << 24);
268 mctl_ctl->addrmap[7] = (cols - 3) | 0x0F00;
271 mctl_ctl->addrmap[6] = (cols - 3) | ((cols - 3) << 8) | ((cols - 3) << 16) | ((cols - 3) << 24);
272 mctl_ctl->addrmap[7] = (cols - 3) | ((cols - 3) << 8);
275 panic("Unsupported DRAM configuration: row number invalid\n");
278 /* Bank groups, DDR4 only */
279 mctl_ctl->addrmap[8] = 0x3F3F;
282 static void mctl_com_init(struct dram_para *para)
284 struct sunxi_mctl_com_reg * const mctl_com =
285 (struct sunxi_mctl_com_reg *)SUNXI_DRAM_COM_BASE;
286 struct sunxi_mctl_ctl_reg * const mctl_ctl =
287 (struct sunxi_mctl_ctl_reg *)SUNXI_DRAM_CTL0_BASE;
288 struct sunxi_mctl_phy_reg * const mctl_phy =
289 (struct sunxi_mctl_phy_reg *)SUNXI_DRAM_PHY0_BASE;
292 mctl_set_addrmap(para);
294 setbits_le32(&mctl_com->cr, BIT(31));
296 /* The bonding ID seems to be always 7. */
297 if (readl(SUNXI_SIDC_BASE + 0x100) == 7) /* bonding ID */
298 clrbits_le32(&mctl_com->cr, BIT(27));
299 else if (readl(SUNXI_SIDC_BASE + 0x100) == 3)
300 setbits_le32(&mctl_com->cr, BIT(27));
304 else if (para->clk > 246)
308 clrsetbits_le32(&mctl_com->unk_0x008, 0x3f00, reg_val);
311 reg_val = MSTR_BURST_LENGTH(8) | MSTR_ACTIVE_RANKS(para->ranks);
312 if (para->type == SUNXI_DRAM_TYPE_LPDDR3)
313 reg_val |= MSTR_DEVICETYPE_LPDDR3;
314 if (para->type == SUNXI_DRAM_TYPE_DDR3)
315 reg_val |= MSTR_DEVICETYPE_DDR3 | MSTR_2TMODE;
316 if (para->bus_full_width)
317 reg_val |= MSTR_BUSWIDTH_FULL;
319 reg_val |= MSTR_BUSWIDTH_HALF;
320 writel(reg_val | BIT(31), &mctl_ctl->mstr);
322 if (para->type == SUNXI_DRAM_TYPE_LPDDR3)
323 reg_val = DCR_LPDDR3 | DCR_DDR8BANK;
324 if (para->type == SUNXI_DRAM_TYPE_DDR3)
325 reg_val = DCR_DDR3 | DCR_DDR8BANK | DCR_DDR2T;
326 writel(reg_val | 0x400, &mctl_phy->dcr);
328 if (para->ranks == 2)
329 writel(0x0303, &mctl_ctl->odtmap);
331 writel(0x0201, &mctl_ctl->odtmap);
334 if (para->type == SUNXI_DRAM_TYPE_LPDDR3) {
335 tmp = para->clk * 7 / 2000;
337 reg_val |= (tmp + 7) << 24;
338 reg_val |= (((para->clk < 400) ? 3 : 4) - tmp) << 16;
339 } else if (para->type == SUNXI_DRAM_TYPE_DDR3) {
340 reg_val = 0x06000400; /* TODO?: Use CL - CWL value in [7:0] */
342 panic("Only (LP)DDR3 supported (type = %d)\n", para->type);
344 writel(reg_val, &mctl_ctl->odtcfg);
346 if (!para->bus_full_width) {
347 writel(0x0, &mctl_phy->dx[2].gcr[0]);
348 writel(0x0, &mctl_phy->dx[3].gcr[0]);
352 static void mctl_bit_delay_set(struct dram_para *para)
354 struct sunxi_mctl_phy_reg * const mctl_phy =
355 (struct sunxi_mctl_phy_reg *)SUNXI_DRAM_PHY0_BASE;
359 for (i = 0; i < 4; i++) {
360 val = readl(&mctl_phy->dx[i].bdlr0);
361 for (j = 0; j < 4; j++)
362 val += para->dx_write_delays[i][j] << (j * 8);
363 writel(val, &mctl_phy->dx[i].bdlr0);
365 val = readl(&mctl_phy->dx[i].bdlr1);
366 for (j = 0; j < 4; j++)
367 val += para->dx_write_delays[i][j + 4] << (j * 8);
368 writel(val, &mctl_phy->dx[i].bdlr1);
370 val = readl(&mctl_phy->dx[i].bdlr2);
371 for (j = 0; j < 4; j++)
372 val += para->dx_write_delays[i][j + 8] << (j * 8);
373 writel(val, &mctl_phy->dx[i].bdlr2);
375 clrbits_le32(&mctl_phy->pgcr[0], BIT(26));
377 for (i = 0; i < 4; i++) {
378 val = readl(&mctl_phy->dx[i].bdlr3);
379 for (j = 0; j < 4; j++)
380 val += para->dx_read_delays[i][j] << (j * 8);
381 writel(val, &mctl_phy->dx[i].bdlr3);
383 val = readl(&mctl_phy->dx[i].bdlr4);
384 for (j = 0; j < 4; j++)
385 val += para->dx_read_delays[i][j + 4] << (j * 8);
386 writel(val, &mctl_phy->dx[i].bdlr4);
388 val = readl(&mctl_phy->dx[i].bdlr5);
389 for (j = 0; j < 4; j++)
390 val += para->dx_read_delays[i][j + 8] << (j * 8);
391 writel(val, &mctl_phy->dx[i].bdlr5);
393 val = readl(&mctl_phy->dx[i].bdlr6);
394 val += (para->dx_read_delays[i][12] << 8) |
395 (para->dx_read_delays[i][13] << 16);
396 writel(val, &mctl_phy->dx[i].bdlr6);
398 setbits_le32(&mctl_phy->pgcr[0], BIT(26));
401 if (para->type != SUNXI_DRAM_TYPE_LPDDR3)
404 for (i = 1; i < 14; i++) {
405 val = readl(&mctl_phy->acbdlr[i]);
407 writel(val, &mctl_phy->acbdlr[i]);
411 static void mctl_channel_init(struct dram_para *para)
413 struct sunxi_mctl_com_reg * const mctl_com =
414 (struct sunxi_mctl_com_reg *)SUNXI_DRAM_COM_BASE;
415 struct sunxi_mctl_ctl_reg * const mctl_ctl =
416 (struct sunxi_mctl_ctl_reg *)SUNXI_DRAM_CTL0_BASE;
417 struct sunxi_mctl_phy_reg * const mctl_phy =
418 (struct sunxi_mctl_phy_reg *)SUNXI_DRAM_PHY0_BASE;
422 setbits_le32(&mctl_ctl->dfiupd[0], BIT(31) | BIT(30));
423 setbits_le32(&mctl_ctl->zqctl[0], BIT(31) | BIT(30));
424 writel(0x2f05, &mctl_ctl->sched[0]);
425 setbits_le32(&mctl_ctl->rfshctl3, BIT(0));
426 setbits_le32(&mctl_ctl->dfimisc, BIT(0));
427 setbits_le32(&mctl_ctl->unk_0x00c, BIT(8));
428 clrsetbits_le32(&mctl_phy->pgcr[1], 0x180, 0xc0);
429 /* TODO: non-LPDDR3 types */
430 clrsetbits_le32(&mctl_phy->pgcr[2], GENMASK(17, 0), ns_to_t(7800));
431 clrbits_le32(&mctl_phy->pgcr[6], BIT(0));
432 clrsetbits_le32(&mctl_phy->dxccr, 0xee0, 0x220);
433 /* TODO: VT compensation */
434 clrsetbits_le32(&mctl_phy->dsgcr, BIT(0), 0x440060);
435 clrbits_le32(&mctl_phy->vtcr[1], BIT(1));
437 for (i = 0; i < 4; i++)
438 clrsetbits_le32(&mctl_phy->dx[i].gcr[0], 0xe00, 0x800);
439 for (i = 0; i < 4; i++)
440 clrsetbits_le32(&mctl_phy->dx[i].gcr[2], 0xffff, 0x5555);
441 for (i = 0; i < 4; i++)
442 clrsetbits_le32(&mctl_phy->dx[i].gcr[3], 0x3030, 0x1010);
446 if (para->ranks == 2)
447 setbits_le32(&mctl_phy->dtcr[1], 0x30000);
449 clrsetbits_le32(&mctl_phy->dtcr[1], 0x30000, 0x10000);
451 if (sunxi_dram_is_lpddr(para->type))
452 clrbits_le32(&mctl_phy->dtcr[1], BIT(1));
453 if (para->ranks == 2) {
454 writel(0x00010001, &mctl_phy->rankidr);
455 writel(0x20000, &mctl_phy->odtcr);
457 writel(0x0, &mctl_phy->rankidr);
458 writel(0x10000, &mctl_phy->odtcr);
461 /* set bits [3:0] to 1? 0 not valid in ZynqMP d/s */
462 if (para->type == SUNXI_DRAM_TYPE_LPDDR3)
463 clrsetbits_le32(&mctl_phy->dtcr[0], 0xF0000000, 0x10000040);
465 clrsetbits_le32(&mctl_phy->dtcr[0], 0xF0000000, 0x10000000);
466 if (para->clk <= 792) {
467 if (para->clk <= 672) {
468 if (para->clk <= 600)
478 /* FIXME: NOT REVIEWED YET */
479 clrsetbits_le32(&mctl_phy->zq[0].zqcr, 0x700, val);
480 clrsetbits_le32(&mctl_phy->zq[0].zqpr[0], 0xff,
481 CONFIG_DRAM_ZQ & 0xff);
482 clrbits_le32(&mctl_phy->zq[0].zqor[0], 0xfffff);
483 setbits_le32(&mctl_phy->zq[0].zqor[0], (CONFIG_DRAM_ZQ >> 8) & 0xff);
484 setbits_le32(&mctl_phy->zq[0].zqor[0], (CONFIG_DRAM_ZQ & 0xf00) - 0x100);
485 setbits_le32(&mctl_phy->zq[0].zqor[0], (CONFIG_DRAM_ZQ & 0xff00) << 4);
486 clrbits_le32(&mctl_phy->zq[1].zqpr[0], 0xfffff);
487 setbits_le32(&mctl_phy->zq[1].zqpr[0], (CONFIG_DRAM_ZQ >> 16) & 0xff);
488 setbits_le32(&mctl_phy->zq[1].zqpr[0], ((CONFIG_DRAM_ZQ >> 8) & 0xf00) - 0x100);
489 setbits_le32(&mctl_phy->zq[1].zqpr[0], (CONFIG_DRAM_ZQ & 0xff0000) >> 4);
490 if (para->type == SUNXI_DRAM_TYPE_LPDDR3) {
491 for (i = 1; i < 14; i++)
492 writel(0x06060606, &mctl_phy->acbdlr[i]);
495 val = PIR_ZCAL | PIR_DCAL | PIR_PHYRST | PIR_DRAMINIT | PIR_QSGATE |
496 PIR_RDDSKW | PIR_WRDSKW | PIR_RDEYE | PIR_WREYE;
497 if (para->type == SUNXI_DRAM_TYPE_DDR3)
498 val |= PIR_DRAMRST | PIR_WL;
499 mctl_phy_pir_init(val);
501 /* TODO: DDR4 types ? */
502 for (i = 0; i < 4; i++)
503 writel(0x00000909, &mctl_phy->dx[i].gcr[5]);
505 for (i = 0; i < 4; i++) {
506 if (IS_ENABLED(CONFIG_DRAM_ODT_EN))
510 clrsetbits_le32(&mctl_phy->dx[i].gcr[2], 0xffff, val);
512 if (IS_ENABLED(CONFIG_DRAM_ODT_EN))
516 clrsetbits_le32(&mctl_phy->dx[i].gcr[3], 0x3030, val);
519 mctl_bit_delay_set(para);
522 setbits_le32(&mctl_phy->pgcr[6], BIT(0));
523 clrbits_le32(&mctl_phy->pgcr[6], 0xfff8);
524 for (i = 0; i < 4; i++)
525 clrbits_le32(&mctl_phy->dx[i].gcr[3], ~0x3ffff);
528 if (readl(&mctl_phy->pgsr[0]) & 0x400000)
530 /* Check for single rank and optionally half DQ. */
531 if ((readl(&mctl_phy->dx[0].rsr[0]) & 0x3) == 2 &&
532 (readl(&mctl_phy->dx[1].rsr[0]) & 0x3) == 2) {
535 if ((readl(&mctl_phy->dx[2].rsr[0]) & 0x3) != 2 ||
536 (readl(&mctl_phy->dx[3].rsr[0]) & 0x3) != 2)
537 para->bus_full_width = 0;
539 /* Restart DRAM initialization from scratch. */
540 mctl_core_init(para);
545 * Check for dual rank and half DQ. NOTE: This combination
546 * is highly unlikely and was not tested. Condition is the
547 * same as in libdram, though.
549 if ((readl(&mctl_phy->dx[0].rsr[0]) & 0x3) == 0 &&
550 (readl(&mctl_phy->dx[1].rsr[0]) & 0x3) == 0) {
551 para->bus_full_width = 0;
553 /* Restart DRAM initialization from scratch. */
554 mctl_core_init(para);
558 panic("This DRAM setup is currently not supported.\n");
561 if (readl(&mctl_phy->pgsr[0]) & 0xff00000) {
562 /* Oops! There's something wrong! */
563 debug("PLL = %x\n", readl(0x3001010));
564 debug("DRAM PHY PGSR0 = %x\n", readl(&mctl_phy->pgsr[0]));
565 for (i = 0; i < 4; i++)
566 debug("DRAM PHY DX%dRSR0 = %x\n", i, readl(&mctl_phy->dx[i].rsr[0]));
567 panic("Error while initializing DRAM PHY!\n");
570 if (sunxi_dram_is_lpddr(para->type))
571 clrsetbits_le32(&mctl_phy->dsgcr, 0xc0, 0x40);
572 clrbits_le32(&mctl_phy->pgcr[1], 0x40);
573 clrbits_le32(&mctl_ctl->dfimisc, BIT(0));
574 writel(1, &mctl_ctl->swctl);
575 mctl_await_completion(&mctl_ctl->swstat, 1, 1);
576 clrbits_le32(&mctl_ctl->rfshctl3, BIT(0));
578 setbits_le32(&mctl_com->unk_0x014, BIT(31));
579 writel(0xffffffff, &mctl_com->maer0);
580 writel(0x7ff, &mctl_com->maer1);
581 writel(0xffff, &mctl_com->maer2);
584 static void mctl_auto_detect_dram_size(struct dram_para *para)
586 /* TODO: non-(LP)DDR3 */
587 /* Detect rank number and half DQ by the code in mctl_channel_init. */
588 mctl_core_init(para);
590 /* detect row address bits */
593 mctl_core_init(para);
595 for (para->rows = 13; para->rows < 18; para->rows++) {
596 /* 8 banks, 8 bit per byte and 16/32 bit width */
597 if (mctl_mem_matches((1 << (para->rows + para->cols +
598 4 + para->bus_full_width))))
602 /* detect column address bits */
604 mctl_core_init(para);
606 for (para->cols = 8; para->cols < 11; para->cols++) {
607 /* 8 bits per byte and 16/32 bit width */
608 if (mctl_mem_matches(1 << (para->cols + 1 +
609 para->bus_full_width)))
614 unsigned long mctl_calc_size(struct dram_para *para)
616 u8 width = para->bus_full_width ? 4 : 2;
618 /* TODO: non-(LP)DDR3 */
621 return (1ULL << (para->cols + para->rows + 3)) * width * para->ranks;
624 #define SUN50I_H6_LPDDR3_DX_WRITE_DELAYS \
625 {{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \
626 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \
627 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 0 }, \
628 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}
629 #define SUN50I_H6_LPDDR3_DX_READ_DELAYS \
630 {{ 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0 }, \
631 { 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0 }, \
632 { 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0 }, \
633 { 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0 }}
635 #define SUN50I_H6_DDR3_DX_WRITE_DELAYS \
636 {{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \
637 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \
638 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \
639 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}
640 #define SUN50I_H6_DDR3_DX_READ_DELAYS \
641 {{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \
642 { 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0 }, \
643 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \
644 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}
646 unsigned long sunxi_dram_init(void)
648 struct sunxi_mctl_com_reg * const mctl_com =
649 (struct sunxi_mctl_com_reg *)SUNXI_DRAM_COM_BASE;
650 struct dram_para para = {
651 .clk = CONFIG_DRAM_CLK,
656 #ifdef CONFIG_SUNXI_DRAM_H6_LPDDR3
657 .type = SUNXI_DRAM_TYPE_LPDDR3,
658 .dx_read_delays = SUN50I_H6_LPDDR3_DX_READ_DELAYS,
659 .dx_write_delays = SUN50I_H6_LPDDR3_DX_WRITE_DELAYS,
660 #elif defined(CONFIG_SUNXI_DRAM_H6_DDR3_1333)
661 .type = SUNXI_DRAM_TYPE_DDR3,
662 .dx_read_delays = SUN50I_H6_DDR3_DX_READ_DELAYS,
663 .dx_write_delays = SUN50I_H6_DDR3_DX_WRITE_DELAYS,
669 /* RES_CAL_CTRL_REG in BSP U-boot*/
670 setbits_le32(0x7010310, BIT(8));
671 clrbits_le32(0x7010318, 0x3f);
673 mctl_auto_detect_dram_size(¶);
675 mctl_core_init(¶);
677 size = mctl_calc_size(¶);
679 clrsetbits_le32(&mctl_com->cr, 0xf0, (size >> (10 + 10 + 4)) & 0xf0);
681 mctl_set_master_priority();