blob: bff2e42513cc1b2c27fb9b7027c60718a23a0037 [file] [log] [blame]
Simon Glass97589732020-05-10 11:40:02 -06001// SPDX-License-Identifier: GPL-2.0+
Icenowy Zheng4e287f62018-07-23 06:13:34 +08002/*
3 * sun50i H6 platform dram controller init
4 *
5 * (C) Copyright 2017 Icenowy Zheng <icenowy@aosc.io>
6 *
Icenowy Zheng4e287f62018-07-23 06:13:34 +08007 */
8#include <common.h>
Simon Glass97589732020-05-10 11:40:02 -06009#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Icenowy Zheng4e287f62018-07-23 06:13:34 +080011#include <asm/io.h>
12#include <asm/arch/clock.h>
13#include <asm/arch/dram.h>
14#include <asm/arch/cpu.h>
Jernej Skrabece04cd492022-01-30 15:27:13 +010015#include <asm/arch/prcm.h>
Icenowy Zheng4e287f62018-07-23 06:13:34 +080016#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060017#include <linux/delay.h>
Icenowy Zheng4e287f62018-07-23 06:13:34 +080018#include <linux/kconfig.h>
19
20/*
21 * The DRAM controller structure on H6 is similar to the ones on A23/A80:
22 * they all contains 3 parts, COM, CTL and PHY. (As a note on A33/A83T/H3/A64
23 * /H5/R40 CTL and PHY is composed).
24 *
25 * COM is allwinner-specific. On H6, the address mapping function is moved
26 * from COM to CTL (with the standard ADDRMAP registers on DesignWare memory
27 * controller).
28 *
29 * CTL (controller) and PHY is from DesignWare.
30 *
31 * The CTL part is a bit similar to the one on A23/A80 (because they all
32 * originate from DesignWare), but gets more registers added.
33 *
34 * The PHY part is quite new, not seen in any previous Allwinner SoCs, and
35 * not seen on other SoCs in U-Boot. The only SoC that is also known to have
36 * similar PHY is ZynqMP.
37 */
38
Icenowy Zheng4e287f62018-07-23 06:13:34 +080039static void mctl_sys_init(struct dram_para *para);
40static void mctl_com_init(struct dram_para *para);
Jernej Skrabec738334f2020-03-12 17:46:00 +000041static bool mctl_channel_init(struct dram_para *para);
Icenowy Zheng4e287f62018-07-23 06:13:34 +080042
Jernej Skrabec738334f2020-03-12 17:46:00 +000043static bool mctl_core_init(struct dram_para *para)
Icenowy Zheng4e287f62018-07-23 06:13:34 +080044{
45 mctl_sys_init(para);
46 mctl_com_init(para);
47 switch (para->type) {
48 case SUNXI_DRAM_TYPE_LPDDR3:
Andre Przywarac78a47a2019-07-15 02:27:07 +010049 case SUNXI_DRAM_TYPE_DDR3:
Andre Przywara1c7a7512019-07-15 02:27:06 +010050 mctl_set_timing_params(para);
Icenowy Zheng4e287f62018-07-23 06:13:34 +080051 break;
52 default:
53 panic("Unsupported DRAM type!");
54 };
Jernej Skrabec738334f2020-03-12 17:46:00 +000055 return mctl_channel_init(para);
Icenowy Zheng4e287f62018-07-23 06:13:34 +080056}
57
Andre Przywara595475e2019-07-15 02:27:05 +010058/* PHY initialisation */
Icenowy Zheng4e287f62018-07-23 06:13:34 +080059static void mctl_phy_pir_init(u32 val)
60{
61 struct sunxi_mctl_phy_reg * const mctl_phy =
62 (struct sunxi_mctl_phy_reg *)SUNXI_DRAM_PHY0_BASE;
63
Andre Przywara595475e2019-07-15 02:27:05 +010064 writel(val, &mctl_phy->pir);
65 writel(val | BIT(0), &mctl_phy->pir); /* Start initialisation. */
Icenowy Zheng4e287f62018-07-23 06:13:34 +080066 mctl_await_completion(&mctl_phy->pgsr[0], BIT(0), BIT(0));
67}
68
69enum {
70 MBUS_PORT_CPU = 0,
71 MBUS_PORT_GPU = 1,
72 MBUS_PORT_MAHB = 2,
73 MBUS_PORT_DMA = 3,
74 MBUS_PORT_VE = 4,
75 MBUS_PORT_CE = 5,
76 MBUS_PORT_TSC0 = 6,
77 MBUS_PORT_NDFC0 = 8,
78 MBUS_PORT_CSI0 = 11,
79 MBUS_PORT_DI0 = 14,
80 MBUS_PORT_DI1 = 15,
81 MBUS_PORT_DE300 = 16,
82 MBUS_PORT_IOMMU = 25,
83 MBUS_PORT_VE2 = 26,
84 MBUS_PORT_USB3 = 37,
85 MBUS_PORT_PCIE = 38,
86 MBUS_PORT_VP9 = 39,
87 MBUS_PORT_HDCP2 = 40,
88};
89
90enum {
91 MBUS_QOS_LOWEST = 0,
92 MBUS_QOS_LOW,
93 MBUS_QOS_HIGH,
94 MBUS_QOS_HIGHEST
95};
Andre Przywara16f6f792023-06-07 01:07:41 +010096
97static void mbus_configure_port(u8 port,
Icenowy Zheng4e287f62018-07-23 06:13:34 +080098 bool bwlimit,
99 bool priority,
100 u8 qos,
101 u8 waittime,
102 u8 acs,
103 u16 bwl0,
104 u16 bwl1,
105 u16 bwl2)
106{
107 struct sunxi_mctl_com_reg * const mctl_com =
108 (struct sunxi_mctl_com_reg *)SUNXI_DRAM_COM_BASE;
109
110 const u32 cfg0 = ( (bwlimit ? (1 << 0) : 0)
111 | (priority ? (1 << 1) : 0)
112 | ((qos & 0x3) << 2)
113 | ((waittime & 0xf) << 4)
114 | ((acs & 0xff) << 8)
115 | (bwl0 << 16) );
116 const u32 cfg1 = ((u32)bwl2 << 16) | (bwl1 & 0xffff);
117
118 debug("MBUS port %d cfg0 %08x cfg1 %08x\n", port, cfg0, cfg1);
119 writel(cfg0, &mctl_com->master[port].cfg0);
120 writel(cfg1, &mctl_com->master[port].cfg1);
121}
122
123#define MBUS_CONF(port, bwlimit, qos, acs, bwl0, bwl1, bwl2) \
124 mbus_configure_port(MBUS_PORT_ ## port, bwlimit, false, \
125 MBUS_QOS_ ## qos, 0, acs, bwl0, bwl1, bwl2)
126
127static void mctl_set_master_priority(void)
128{
129 struct sunxi_mctl_com_reg * const mctl_com =
130 (struct sunxi_mctl_com_reg *)SUNXI_DRAM_COM_BASE;
131
132 /* enable bandwidth limit windows and set windows size 1us */
133 writel(399, &mctl_com->tmr);
134 writel(BIT(16), &mctl_com->bwcr);
135
136 MBUS_CONF( CPU, true, HIGHEST, 0, 256, 128, 100);
137 MBUS_CONF( GPU, true, HIGH, 0, 1536, 1400, 256);
138 MBUS_CONF( MAHB, true, HIGHEST, 0, 512, 256, 96);
139 MBUS_CONF( DMA, true, HIGH, 0, 256, 100, 80);
140 MBUS_CONF( VE, true, HIGH, 2, 8192, 5500, 5000);
141 MBUS_CONF( CE, true, HIGH, 2, 100, 64, 32);
142 MBUS_CONF( TSC0, true, HIGH, 2, 100, 64, 32);
143 MBUS_CONF(NDFC0, true, HIGH, 0, 256, 128, 64);
144 MBUS_CONF( CSI0, true, HIGH, 0, 256, 128, 100);
145 MBUS_CONF( DI0, true, HIGH, 0, 1024, 256, 64);
146 MBUS_CONF(DE300, true, HIGHEST, 6, 8192, 2800, 2400);
147 MBUS_CONF(IOMMU, true, HIGHEST, 0, 100, 64, 32);
148 MBUS_CONF( VE2, true, HIGH, 2, 8192, 5500, 5000);
149 MBUS_CONF( USB3, true, HIGH, 0, 256, 128, 64);
150 MBUS_CONF( PCIE, true, HIGH, 2, 100, 64, 32);
151 MBUS_CONF( VP9, true, HIGH, 2, 8192, 5500, 5000);
152 MBUS_CONF(HDCP2, true, HIGH, 2, 100, 64, 32);
153}
154
Icenowy Zheng4e287f62018-07-23 06:13:34 +0800155static void mctl_sys_init(struct dram_para *para)
156{
157 struct sunxi_ccm_reg * const ccm =
158 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
159 struct sunxi_mctl_com_reg * const mctl_com =
160 (struct sunxi_mctl_com_reg *)SUNXI_DRAM_COM_BASE;
161 struct sunxi_mctl_ctl_reg * const mctl_ctl =
162 (struct sunxi_mctl_ctl_reg *)SUNXI_DRAM_CTL0_BASE;
163
164 /* Put all DRAM-related blocks to reset state */
165 clrbits_le32(&ccm->mbus_cfg, MBUS_ENABLE | MBUS_RESET);
Icenowy Zhengac2ed962018-10-06 23:23:32 +0800166 clrbits_le32(&ccm->dram_gate_reset, BIT(0));
167 udelay(5);
Icenowy Zheng4e287f62018-07-23 06:13:34 +0800168 writel(0, &ccm->dram_gate_reset);
169 clrbits_le32(&ccm->pll5_cfg, CCM_PLL5_CTRL_EN);
170 clrbits_le32(&ccm->dram_clk_cfg, DRAM_MOD_RESET);
171
172 udelay(5);
173
174 /* Set PLL5 rate to doubled DRAM clock rate */
175 writel(CCM_PLL5_CTRL_EN | CCM_PLL5_LOCK_EN |
Andre Przywara0f7c8bc2021-05-05 13:53:05 +0100176 CCM_PLL5_CTRL_N(para->clk * 2 / 24), &ccm->pll5_cfg);
Icenowy Zheng4e287f62018-07-23 06:13:34 +0800177 mctl_await_completion(&ccm->pll5_cfg, CCM_PLL5_LOCK, CCM_PLL5_LOCK);
178
179 /* Configure DRAM mod clock */
180 writel(DRAM_CLK_SRC_PLL5, &ccm->dram_clk_cfg);
181 setbits_le32(&ccm->dram_clk_cfg, DRAM_CLK_UPDATE);
Icenowy Zhengac2ed962018-10-06 23:23:32 +0800182 writel(BIT(RESET_SHIFT), &ccm->dram_gate_reset);
183 udelay(5);
184 setbits_le32(&ccm->dram_gate_reset, BIT(0));
Icenowy Zheng4e287f62018-07-23 06:13:34 +0800185
186 /* Disable all channels */
187 writel(0, &mctl_com->maer0);
188 writel(0, &mctl_com->maer1);
189 writel(0, &mctl_com->maer2);
190
191 /* Configure MBUS and enable DRAM mod reset */
192 setbits_le32(&ccm->mbus_cfg, MBUS_RESET);
193 setbits_le32(&ccm->mbus_cfg, MBUS_ENABLE);
194 setbits_le32(&ccm->dram_clk_cfg, DRAM_MOD_RESET);
195 udelay(5);
196
197 /* Unknown hack from the BSP, which enables access of mctl_ctl regs */
198 writel(0x8000, &mctl_ctl->unk_0x00c);
199}
200
201static void mctl_set_addrmap(struct dram_para *para)
202{
203 struct sunxi_mctl_ctl_reg * const mctl_ctl =
204 (struct sunxi_mctl_ctl_reg *)SUNXI_DRAM_CTL0_BASE;
205 u8 cols = para->cols;
206 u8 rows = para->rows;
207 u8 ranks = para->ranks;
208
Jernej Skrabec370245e2019-08-23 19:24:04 +0200209 if (!para->bus_full_width)
210 cols -= 1;
211
Icenowy Zheng4e287f62018-07-23 06:13:34 +0800212 /* Ranks */
213 if (ranks == 2)
214 mctl_ctl->addrmap[0] = rows + cols - 3;
215 else
216 mctl_ctl->addrmap[0] = 0x1F;
217
218 /* Banks, hardcoded to 8 banks now */
219 mctl_ctl->addrmap[1] = (cols - 2) | (cols - 2) << 8 | (cols - 2) << 16;
220
221 /* Columns */
222 mctl_ctl->addrmap[2] = 0;
223 switch (cols) {
Jernej Skrabec370245e2019-08-23 19:24:04 +0200224 case 7:
225 mctl_ctl->addrmap[3] = 0x1F1F1F00;
226 mctl_ctl->addrmap[4] = 0x1F1F;
227 break;
Icenowy Zheng4e287f62018-07-23 06:13:34 +0800228 case 8:
229 mctl_ctl->addrmap[3] = 0x1F1F0000;
230 mctl_ctl->addrmap[4] = 0x1F1F;
231 break;
232 case 9:
233 mctl_ctl->addrmap[3] = 0x1F000000;
234 mctl_ctl->addrmap[4] = 0x1F1F;
235 break;
236 case 10:
237 mctl_ctl->addrmap[3] = 0;
238 mctl_ctl->addrmap[4] = 0x1F1F;
239 break;
240 case 11:
241 mctl_ctl->addrmap[3] = 0;
242 mctl_ctl->addrmap[4] = 0x1F00;
243 break;
244 case 12:
245 mctl_ctl->addrmap[3] = 0;
246 mctl_ctl->addrmap[4] = 0;
247 break;
248 default:
249 panic("Unsupported DRAM configuration: column number invalid\n");
250 }
251
252 /* Rows */
253 mctl_ctl->addrmap[5] = (cols - 3) | ((cols - 3) << 8) | ((cols - 3) << 16) | ((cols - 3) << 24);
254 switch (rows) {
255 case 13:
256 mctl_ctl->addrmap[6] = (cols - 3) | 0x0F0F0F00;
257 mctl_ctl->addrmap[7] = 0x0F0F;
258 break;
259 case 14:
260 mctl_ctl->addrmap[6] = (cols - 3) | ((cols - 3) << 8) | 0x0F0F0000;
261 mctl_ctl->addrmap[7] = 0x0F0F;
262 break;
263 case 15:
264 mctl_ctl->addrmap[6] = (cols - 3) | ((cols - 3) << 8) | ((cols - 3) << 16) | 0x0F000000;
265 mctl_ctl->addrmap[7] = 0x0F0F;
266 break;
267 case 16:
268 mctl_ctl->addrmap[6] = (cols - 3) | ((cols - 3) << 8) | ((cols - 3) << 16) | ((cols - 3) << 24);
269 mctl_ctl->addrmap[7] = 0x0F0F;
270 break;
271 case 17:
272 mctl_ctl->addrmap[6] = (cols - 3) | ((cols - 3) << 8) | ((cols - 3) << 16) | ((cols - 3) << 24);
273 mctl_ctl->addrmap[7] = (cols - 3) | 0x0F00;
274 break;
275 case 18:
276 mctl_ctl->addrmap[6] = (cols - 3) | ((cols - 3) << 8) | ((cols - 3) << 16) | ((cols - 3) << 24);
277 mctl_ctl->addrmap[7] = (cols - 3) | ((cols - 3) << 8);
278 break;
279 default:
280 panic("Unsupported DRAM configuration: row number invalid\n");
281 }
282
283 /* Bank groups, DDR4 only */
284 mctl_ctl->addrmap[8] = 0x3F3F;
285}
286
287static void mctl_com_init(struct dram_para *para)
288{
289 struct sunxi_mctl_com_reg * const mctl_com =
290 (struct sunxi_mctl_com_reg *)SUNXI_DRAM_COM_BASE;
291 struct sunxi_mctl_ctl_reg * const mctl_ctl =
292 (struct sunxi_mctl_ctl_reg *)SUNXI_DRAM_CTL0_BASE;
293 struct sunxi_mctl_phy_reg * const mctl_phy =
294 (struct sunxi_mctl_phy_reg *)SUNXI_DRAM_PHY0_BASE;
295 u32 reg_val, tmp;
296
297 mctl_set_addrmap(para);
298
299 setbits_le32(&mctl_com->cr, BIT(31));
Andre Przywarac78a47a2019-07-15 02:27:07 +0100300
301 /* The bonding ID seems to be always 7. */
302 if (readl(SUNXI_SIDC_BASE + 0x100) == 7) /* bonding ID */
Icenowy Zheng4e287f62018-07-23 06:13:34 +0800303 clrbits_le32(&mctl_com->cr, BIT(27));
Andre Przywarac78a47a2019-07-15 02:27:07 +0100304 else if (readl(SUNXI_SIDC_BASE + 0x100) == 3)
Icenowy Zheng4e287f62018-07-23 06:13:34 +0800305 setbits_le32(&mctl_com->cr, BIT(27));
306
307 if (para->clk > 408)
308 reg_val = 0xf00;
309 else if (para->clk > 246)
310 reg_val = 0x1f00;
311 else
312 reg_val = 0x3f00;
313 clrsetbits_le32(&mctl_com->unk_0x008, 0x3f00, reg_val);
314
Jernej Skrabec370245e2019-08-23 19:24:04 +0200315 /* TODO: DDR4 */
316 reg_val = MSTR_BURST_LENGTH(8) | MSTR_ACTIVE_RANKS(para->ranks);
Andre Przywarac78a47a2019-07-15 02:27:07 +0100317 if (para->type == SUNXI_DRAM_TYPE_LPDDR3)
318 reg_val |= MSTR_DEVICETYPE_LPDDR3;
319 if (para->type == SUNXI_DRAM_TYPE_DDR3)
320 reg_val |= MSTR_DEVICETYPE_DDR3 | MSTR_2TMODE;
Jernej Skrabec370245e2019-08-23 19:24:04 +0200321 if (para->bus_full_width)
322 reg_val |= MSTR_BUSWIDTH_FULL;
323 else
324 reg_val |= MSTR_BUSWIDTH_HALF;
Andre Przywarac78a47a2019-07-15 02:27:07 +0100325 writel(reg_val | BIT(31), &mctl_ctl->mstr);
326
327 if (para->type == SUNXI_DRAM_TYPE_LPDDR3)
328 reg_val = DCR_LPDDR3 | DCR_DDR8BANK;
329 if (para->type == SUNXI_DRAM_TYPE_DDR3)
330 reg_val = DCR_DDR3 | DCR_DDR8BANK | DCR_DDR2T;
331 writel(reg_val | 0x400, &mctl_phy->dcr);
Icenowy Zheng4e287f62018-07-23 06:13:34 +0800332
333 if (para->ranks == 2)
334 writel(0x0303, &mctl_ctl->odtmap);
335 else
336 writel(0x0201, &mctl_ctl->odtmap);
337
Andre Przywarac78a47a2019-07-15 02:27:07 +0100338 /* TODO: DDR4 */
339 if (para->type == SUNXI_DRAM_TYPE_LPDDR3) {
340 tmp = para->clk * 7 / 2000;
341 reg_val = 0x0400;
342 reg_val |= (tmp + 7) << 24;
343 reg_val |= (((para->clk < 400) ? 3 : 4) - tmp) << 16;
344 } else if (para->type == SUNXI_DRAM_TYPE_DDR3) {
345 reg_val = 0x06000400; /* TODO?: Use CL - CWL value in [7:0] */
346 } else {
347 panic("Only (LP)DDR3 supported (type = %d)\n", para->type);
348 }
Icenowy Zheng4e287f62018-07-23 06:13:34 +0800349 writel(reg_val, &mctl_ctl->odtcfg);
350
Jernej Skrabec370245e2019-08-23 19:24:04 +0200351 if (!para->bus_full_width) {
352 writel(0x0, &mctl_phy->dx[2].gcr[0]);
353 writel(0x0, &mctl_phy->dx[3].gcr[0]);
354 }
Icenowy Zheng4e287f62018-07-23 06:13:34 +0800355}
356
357static void mctl_bit_delay_set(struct dram_para *para)
358{
359 struct sunxi_mctl_phy_reg * const mctl_phy =
360 (struct sunxi_mctl_phy_reg *)SUNXI_DRAM_PHY0_BASE;
361 int i, j;
362 u32 val;
363
364 for (i = 0; i < 4; i++) {
365 val = readl(&mctl_phy->dx[i].bdlr0);
366 for (j = 0; j < 4; j++)
367 val += para->dx_write_delays[i][j] << (j * 8);
368 writel(val, &mctl_phy->dx[i].bdlr0);
369
370 val = readl(&mctl_phy->dx[i].bdlr1);
371 for (j = 0; j < 4; j++)
372 val += para->dx_write_delays[i][j + 4] << (j * 8);
373 writel(val, &mctl_phy->dx[i].bdlr1);
374
375 val = readl(&mctl_phy->dx[i].bdlr2);
376 for (j = 0; j < 4; j++)
377 val += para->dx_write_delays[i][j + 8] << (j * 8);
378 writel(val, &mctl_phy->dx[i].bdlr2);
379 }
380 clrbits_le32(&mctl_phy->pgcr[0], BIT(26));
381
382 for (i = 0; i < 4; i++) {
383 val = readl(&mctl_phy->dx[i].bdlr3);
384 for (j = 0; j < 4; j++)
385 val += para->dx_read_delays[i][j] << (j * 8);
386 writel(val, &mctl_phy->dx[i].bdlr3);
387
388 val = readl(&mctl_phy->dx[i].bdlr4);
389 for (j = 0; j < 4; j++)
390 val += para->dx_read_delays[i][j + 4] << (j * 8);
391 writel(val, &mctl_phy->dx[i].bdlr4);
392
393 val = readl(&mctl_phy->dx[i].bdlr5);
394 for (j = 0; j < 4; j++)
395 val += para->dx_read_delays[i][j + 8] << (j * 8);
396 writel(val, &mctl_phy->dx[i].bdlr5);
397
398 val = readl(&mctl_phy->dx[i].bdlr6);
399 val += (para->dx_read_delays[i][12] << 8) |
400 (para->dx_read_delays[i][13] << 16);
401 writel(val, &mctl_phy->dx[i].bdlr6);
402 }
403 setbits_le32(&mctl_phy->pgcr[0], BIT(26));
404 udelay(1);
405
Andre Przywarac78a47a2019-07-15 02:27:07 +0100406 if (para->type != SUNXI_DRAM_TYPE_LPDDR3)
407 return;
408
Icenowy Zheng4e287f62018-07-23 06:13:34 +0800409 for (i = 1; i < 14; i++) {
410 val = readl(&mctl_phy->acbdlr[i]);
411 val += 0x0a0a0a0a;
412 writel(val, &mctl_phy->acbdlr[i]);
413 }
414}
415
Jernej Skrabec738334f2020-03-12 17:46:00 +0000416static bool mctl_channel_init(struct dram_para *para)
Icenowy Zheng4e287f62018-07-23 06:13:34 +0800417{
418 struct sunxi_mctl_com_reg * const mctl_com =
419 (struct sunxi_mctl_com_reg *)SUNXI_DRAM_COM_BASE;
420 struct sunxi_mctl_ctl_reg * const mctl_ctl =
421 (struct sunxi_mctl_ctl_reg *)SUNXI_DRAM_CTL0_BASE;
422 struct sunxi_mctl_phy_reg * const mctl_phy =
423 (struct sunxi_mctl_phy_reg *)SUNXI_DRAM_PHY0_BASE;
424 int i;
425 u32 val;
426
427 setbits_le32(&mctl_ctl->dfiupd[0], BIT(31) | BIT(30));
428 setbits_le32(&mctl_ctl->zqctl[0], BIT(31) | BIT(30));
429 writel(0x2f05, &mctl_ctl->sched[0]);
430 setbits_le32(&mctl_ctl->rfshctl3, BIT(0));
431 setbits_le32(&mctl_ctl->dfimisc, BIT(0));
432 setbits_le32(&mctl_ctl->unk_0x00c, BIT(8));
433 clrsetbits_le32(&mctl_phy->pgcr[1], 0x180, 0xc0);
434 /* TODO: non-LPDDR3 types */
435 clrsetbits_le32(&mctl_phy->pgcr[2], GENMASK(17, 0), ns_to_t(7800));
436 clrbits_le32(&mctl_phy->pgcr[6], BIT(0));
437 clrsetbits_le32(&mctl_phy->dxccr, 0xee0, 0x220);
438 /* TODO: VT compensation */
439 clrsetbits_le32(&mctl_phy->dsgcr, BIT(0), 0x440060);
440 clrbits_le32(&mctl_phy->vtcr[1], BIT(1));
441
442 for (i = 0; i < 4; i++)
443 clrsetbits_le32(&mctl_phy->dx[i].gcr[0], 0xe00, 0x800);
444 for (i = 0; i < 4; i++)
445 clrsetbits_le32(&mctl_phy->dx[i].gcr[2], 0xffff, 0x5555);
446 for (i = 0; i < 4; i++)
447 clrsetbits_le32(&mctl_phy->dx[i].gcr[3], 0x3030, 0x1010);
448
449 udelay(100);
450
451 if (para->ranks == 2)
452 setbits_le32(&mctl_phy->dtcr[1], 0x30000);
453 else
454 clrsetbits_le32(&mctl_phy->dtcr[1], 0x30000, 0x10000);
455
Andre Przywarac78a47a2019-07-15 02:27:07 +0100456 if (sunxi_dram_is_lpddr(para->type))
457 clrbits_le32(&mctl_phy->dtcr[1], BIT(1));
Icenowy Zheng4e287f62018-07-23 06:13:34 +0800458 if (para->ranks == 2) {
459 writel(0x00010001, &mctl_phy->rankidr);
460 writel(0x20000, &mctl_phy->odtcr);
461 } else {
462 writel(0x0, &mctl_phy->rankidr);
463 writel(0x10000, &mctl_phy->odtcr);
464 }
465
Andre Przywarac78a47a2019-07-15 02:27:07 +0100466 /* set bits [3:0] to 1? 0 not valid in ZynqMP d/s */
467 if (para->type == SUNXI_DRAM_TYPE_LPDDR3)
468 clrsetbits_le32(&mctl_phy->dtcr[0], 0xF0000000, 0x10000040);
469 else
470 clrsetbits_le32(&mctl_phy->dtcr[0], 0xF0000000, 0x10000000);
Icenowy Zheng4e287f62018-07-23 06:13:34 +0800471 if (para->clk <= 792) {
472 if (para->clk <= 672) {
473 if (para->clk <= 600)
474 val = 0x300;
475 else
476 val = 0x400;
477 } else {
478 val = 0x500;
479 }
480 } else {
481 val = 0x600;
482 }
483 /* FIXME: NOT REVIEWED YET */
484 clrsetbits_le32(&mctl_phy->zq[0].zqcr, 0x700, val);
485 clrsetbits_le32(&mctl_phy->zq[0].zqpr[0], 0xff,
486 CONFIG_DRAM_ZQ & 0xff);
487 clrbits_le32(&mctl_phy->zq[0].zqor[0], 0xfffff);
488 setbits_le32(&mctl_phy->zq[0].zqor[0], (CONFIG_DRAM_ZQ >> 8) & 0xff);
489 setbits_le32(&mctl_phy->zq[0].zqor[0], (CONFIG_DRAM_ZQ & 0xf00) - 0x100);
490 setbits_le32(&mctl_phy->zq[0].zqor[0], (CONFIG_DRAM_ZQ & 0xff00) << 4);
491 clrbits_le32(&mctl_phy->zq[1].zqpr[0], 0xfffff);
492 setbits_le32(&mctl_phy->zq[1].zqpr[0], (CONFIG_DRAM_ZQ >> 16) & 0xff);
493 setbits_le32(&mctl_phy->zq[1].zqpr[0], ((CONFIG_DRAM_ZQ >> 8) & 0xf00) - 0x100);
494 setbits_le32(&mctl_phy->zq[1].zqpr[0], (CONFIG_DRAM_ZQ & 0xff0000) >> 4);
495 if (para->type == SUNXI_DRAM_TYPE_LPDDR3) {
496 for (i = 1; i < 14; i++)
497 writel(0x06060606, &mctl_phy->acbdlr[i]);
498 }
499
Andre Przywarac78a47a2019-07-15 02:27:07 +0100500 val = PIR_ZCAL | PIR_DCAL | PIR_PHYRST | PIR_DRAMINIT | PIR_QSGATE |
501 PIR_RDDSKW | PIR_WRDSKW | PIR_RDEYE | PIR_WREYE;
502 if (para->type == SUNXI_DRAM_TYPE_DDR3)
503 val |= PIR_DRAMRST | PIR_WL;
504 mctl_phy_pir_init(val);
Icenowy Zheng4e287f62018-07-23 06:13:34 +0800505
Andre Przywarac78a47a2019-07-15 02:27:07 +0100506 /* TODO: DDR4 types ? */
Icenowy Zheng4e287f62018-07-23 06:13:34 +0800507 for (i = 0; i < 4; i++)
508 writel(0x00000909, &mctl_phy->dx[i].gcr[5]);
509
510 for (i = 0; i < 4; i++) {
511 if (IS_ENABLED(CONFIG_DRAM_ODT_EN))
512 val = 0x0;
513 else
514 val = 0xaaaa;
515 clrsetbits_le32(&mctl_phy->dx[i].gcr[2], 0xffff, val);
516
517 if (IS_ENABLED(CONFIG_DRAM_ODT_EN))
518 val = 0x0;
519 else
520 val = 0x2020;
521 clrsetbits_le32(&mctl_phy->dx[i].gcr[3], 0x3030, val);
522 }
523
524 mctl_bit_delay_set(para);
525 udelay(1);
526
527 setbits_le32(&mctl_phy->pgcr[6], BIT(0));
528 clrbits_le32(&mctl_phy->pgcr[6], 0xfff8);
529 for (i = 0; i < 4; i++)
530 clrbits_le32(&mctl_phy->dx[i].gcr[3], ~0x3ffff);
531 udelay(10);
532
Icenowy Zheng4e287f62018-07-23 06:13:34 +0800533 if (readl(&mctl_phy->pgsr[0]) & 0xff00000) {
534 /* Oops! There's something wrong! */
535 debug("PLL = %x\n", readl(0x3001010));
536 debug("DRAM PHY PGSR0 = %x\n", readl(&mctl_phy->pgsr[0]));
537 for (i = 0; i < 4; i++)
538 debug("DRAM PHY DX%dRSR0 = %x\n", i, readl(&mctl_phy->dx[i].rsr[0]));
Jernej Skrabec738334f2020-03-12 17:46:00 +0000539 debug("Error while initializing DRAM PHY!\n");
540
541 return false;
Icenowy Zheng4e287f62018-07-23 06:13:34 +0800542 }
543
Andre Przywarac78a47a2019-07-15 02:27:07 +0100544 if (sunxi_dram_is_lpddr(para->type))
545 clrsetbits_le32(&mctl_phy->dsgcr, 0xc0, 0x40);
Icenowy Zheng4e287f62018-07-23 06:13:34 +0800546 clrbits_le32(&mctl_phy->pgcr[1], 0x40);
547 clrbits_le32(&mctl_ctl->dfimisc, BIT(0));
548 writel(1, &mctl_ctl->swctl);
549 mctl_await_completion(&mctl_ctl->swstat, 1, 1);
550 clrbits_le32(&mctl_ctl->rfshctl3, BIT(0));
551
552 setbits_le32(&mctl_com->unk_0x014, BIT(31));
553 writel(0xffffffff, &mctl_com->maer0);
554 writel(0x7ff, &mctl_com->maer1);
555 writel(0xffff, &mctl_com->maer2);
Jernej Skrabec738334f2020-03-12 17:46:00 +0000556
557 return true;
558}
559
560static void mctl_auto_detect_rank_width(struct dram_para *para)
561{
562 /* this is minimum size that it's supported */
563 para->cols = 8;
564 para->rows = 13;
565
566 /*
567 * Previous versions of this driver tried to auto detect the rank
568 * and width by looking at controller registers. However this proved
569 * to be not reliable, so this approach here is the more robust
570 * solution. Check the git history for details.
571 *
572 * Strategy here is to test most demanding combination first and least
573 * demanding last, otherwise HW might not be fully utilized. For
574 * example, half bus width and rank = 1 combination would also work
575 * on HW with full bus width and rank = 2, but only 1/4 RAM would be
576 * visible.
577 */
578
579 debug("testing 32-bit width, rank = 2\n");
580 para->bus_full_width = 1;
581 para->ranks = 2;
582 if (mctl_core_init(para))
583 return;
584
585 debug("testing 32-bit width, rank = 1\n");
586 para->bus_full_width = 1;
587 para->ranks = 1;
588 if (mctl_core_init(para))
589 return;
590
591 debug("testing 16-bit width, rank = 2\n");
592 para->bus_full_width = 0;
593 para->ranks = 2;
594 if (mctl_core_init(para))
595 return;
596
597 debug("testing 16-bit width, rank = 1\n");
598 para->bus_full_width = 0;
599 para->ranks = 1;
600 if (mctl_core_init(para))
601 return;
602
603 panic("This DRAM setup is currently not supported.\n");
Icenowy Zheng4e287f62018-07-23 06:13:34 +0800604}
605
606static void mctl_auto_detect_dram_size(struct dram_para *para)
607{
Jernej Skrabec370245e2019-08-23 19:24:04 +0200608 /* TODO: non-(LP)DDR3 */
Icenowy Zheng4e287f62018-07-23 06:13:34 +0800609
610 /* detect row address bits */
611 para->cols = 8;
612 para->rows = 18;
613 mctl_core_init(para);
614
615 for (para->rows = 13; para->rows < 18; para->rows++) {
Jernej Skrabec370245e2019-08-23 19:24:04 +0200616 /* 8 banks, 8 bit per byte and 16/32 bit width */
617 if (mctl_mem_matches((1 << (para->rows + para->cols +
618 4 + para->bus_full_width))))
Icenowy Zheng4e287f62018-07-23 06:13:34 +0800619 break;
620 }
621
622 /* detect column address bits */
623 para->cols = 11;
624 mctl_core_init(para);
625
626 for (para->cols = 8; para->cols < 11; para->cols++) {
Jernej Skrabec370245e2019-08-23 19:24:04 +0200627 /* 8 bits per byte and 16/32 bit width */
628 if (mctl_mem_matches(1 << (para->cols + 1 +
629 para->bus_full_width)))
Icenowy Zheng4e287f62018-07-23 06:13:34 +0800630 break;
631 }
632}
633
634unsigned long mctl_calc_size(struct dram_para *para)
635{
Jernej Skrabec370245e2019-08-23 19:24:04 +0200636 u8 width = para->bus_full_width ? 4 : 2;
Icenowy Zheng4e287f62018-07-23 06:13:34 +0800637
Jernej Skrabec370245e2019-08-23 19:24:04 +0200638 /* TODO: non-(LP)DDR3 */
639
640 /* 8 banks */
641 return (1ULL << (para->cols + para->rows + 3)) * width * para->ranks;
Icenowy Zheng4e287f62018-07-23 06:13:34 +0800642}
643
Jernej Skrabecfed1d2f2019-07-15 02:27:09 +0100644#define SUN50I_H6_LPDDR3_DX_WRITE_DELAYS \
Icenowy Zheng4e287f62018-07-23 06:13:34 +0800645 {{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \
646 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \
647 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 0 }, \
648 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}
Jernej Skrabecfed1d2f2019-07-15 02:27:09 +0100649#define SUN50I_H6_LPDDR3_DX_READ_DELAYS \
Icenowy Zheng4e287f62018-07-23 06:13:34 +0800650 {{ 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0 }, \
651 { 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0 }, \
652 { 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0 }, \
653 { 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0 }}
654
Jernej Skrabecfed1d2f2019-07-15 02:27:09 +0100655#define SUN50I_H6_DDR3_DX_WRITE_DELAYS \
656 {{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \
657 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \
658 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \
659 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}
660#define SUN50I_H6_DDR3_DX_READ_DELAYS \
661 {{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \
662 { 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0 }, \
663 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \
664 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}
665
Icenowy Zheng4e287f62018-07-23 06:13:34 +0800666unsigned long sunxi_dram_init(void)
667{
668 struct sunxi_mctl_com_reg * const mctl_com =
669 (struct sunxi_mctl_com_reg *)SUNXI_DRAM_COM_BASE;
Jernej Skrabece04cd492022-01-30 15:27:13 +0100670 struct sunxi_prcm_reg *const prcm =
671 (struct sunxi_prcm_reg *)SUNXI_PRCM_BASE;
Icenowy Zheng4e287f62018-07-23 06:13:34 +0800672 struct dram_para para = {
673 .clk = CONFIG_DRAM_CLK,
Andre Przywarac78a47a2019-07-15 02:27:07 +0100674#ifdef CONFIG_SUNXI_DRAM_H6_LPDDR3
675 .type = SUNXI_DRAM_TYPE_LPDDR3,
Jernej Skrabecfed1d2f2019-07-15 02:27:09 +0100676 .dx_read_delays = SUN50I_H6_LPDDR3_DX_READ_DELAYS,
677 .dx_write_delays = SUN50I_H6_LPDDR3_DX_WRITE_DELAYS,
Andre Przywarac78a47a2019-07-15 02:27:07 +0100678#elif defined(CONFIG_SUNXI_DRAM_H6_DDR3_1333)
679 .type = SUNXI_DRAM_TYPE_DDR3,
Jernej Skrabecfed1d2f2019-07-15 02:27:09 +0100680 .dx_read_delays = SUN50I_H6_DDR3_DX_READ_DELAYS,
681 .dx_write_delays = SUN50I_H6_DDR3_DX_WRITE_DELAYS,
Andre Przywara1c7a7512019-07-15 02:27:06 +0100682#endif
Icenowy Zheng4e287f62018-07-23 06:13:34 +0800683 };
684
685 unsigned long size;
686
Jernej Skrabece04cd492022-01-30 15:27:13 +0100687 setbits_le32(&prcm->res_cal_ctrl, BIT(8));
688 clrbits_le32(&prcm->ohms240, 0x3f);
Icenowy Zheng4e287f62018-07-23 06:13:34 +0800689
Jernej Skrabec738334f2020-03-12 17:46:00 +0000690 mctl_auto_detect_rank_width(&para);
Icenowy Zheng4e287f62018-07-23 06:13:34 +0800691 mctl_auto_detect_dram_size(&para);
692
693 mctl_core_init(&para);
694
695 size = mctl_calc_size(&para);
696
697 clrsetbits_le32(&mctl_com->cr, 0xf0, (size >> (10 + 10 + 4)) & 0xf0);
698
699 mctl_set_master_priority();
700
701 return size;
702};