blob: 454c845a00106638248673c7025fcbaa3e779034 [file] [log] [blame]
Jernej Skrabece4aa24b2021-01-11 21:11:43 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * sun50i H616 platform dram controller driver
4 *
5 * While controller is very similar to that in H6, PHY is completely
6 * unknown. That's why this driver has plenty of magic numbers. Some
7 * meaning was nevertheless deduced from strings found in boot0 and
8 * known meaning of some dram parameters.
9 * This driver only supports DDR3 memory and omits logic for all
10 * other supported types supported by hardware.
11 *
12 * (C) Copyright 2020 Jernej Skrabec <jernej.skrabec@siol.net>
13 *
14 */
15#include <common.h>
16#include <init.h>
17#include <log.h>
18#include <asm/io.h>
19#include <asm/arch/clock.h>
20#include <asm/arch/dram.h>
21#include <asm/arch/cpu.h>
Jernej Skrabece04cd492022-01-30 15:27:13 +010022#include <asm/arch/prcm.h>
Jernej Skrabece4aa24b2021-01-11 21:11:43 +010023#include <linux/bitops.h>
24#include <linux/delay.h>
25#include <linux/kconfig.h>
26
27enum {
28 MBUS_QOS_LOWEST = 0,
29 MBUS_QOS_LOW,
30 MBUS_QOS_HIGH,
31 MBUS_QOS_HIGHEST
32};
33
34inline void mbus_configure_port(u8 port,
35 bool bwlimit,
36 bool priority,
37 u8 qos,
38 u8 waittime,
39 u8 acs,
40 u16 bwl0,
41 u16 bwl1,
42 u16 bwl2)
43{
44 struct sunxi_mctl_com_reg * const mctl_com =
45 (struct sunxi_mctl_com_reg *)SUNXI_DRAM_COM_BASE;
46
47 const u32 cfg0 = ( (bwlimit ? (1 << 0) : 0)
48 | (priority ? (1 << 1) : 0)
49 | ((qos & 0x3) << 2)
50 | ((waittime & 0xf) << 4)
51 | ((acs & 0xff) << 8)
52 | (bwl0 << 16) );
53 const u32 cfg1 = ((u32)bwl2 << 16) | (bwl1 & 0xffff);
54
55 debug("MBUS port %d cfg0 %08x cfg1 %08x\n", port, cfg0, cfg1);
56 writel_relaxed(cfg0, &mctl_com->master[port].cfg0);
57 writel_relaxed(cfg1, &mctl_com->master[port].cfg1);
58}
59
60#define MBUS_CONF(port, bwlimit, qos, acs, bwl0, bwl1, bwl2) \
61 mbus_configure_port(port, bwlimit, false, \
62 MBUS_QOS_ ## qos, 0, acs, bwl0, bwl1, bwl2)
63
64static void mctl_set_master_priority(void)
65{
66 struct sunxi_mctl_com_reg * const mctl_com =
67 (struct sunxi_mctl_com_reg *)SUNXI_DRAM_COM_BASE;
68
69 /* enable bandwidth limit windows and set windows size 1us */
70 writel(399, &mctl_com->tmr);
71 writel(BIT(16), &mctl_com->bwcr);
72
73 MBUS_CONF( 0, true, HIGHEST, 0, 256, 128, 100);
74 MBUS_CONF( 1, true, HIGH, 0, 1536, 1400, 256);
75 MBUS_CONF( 2, true, HIGHEST, 0, 512, 256, 96);
76 MBUS_CONF( 3, true, HIGH, 0, 256, 100, 80);
77 MBUS_CONF( 4, true, HIGH, 2, 8192, 5500, 5000);
78 MBUS_CONF( 5, true, HIGH, 2, 100, 64, 32);
79 MBUS_CONF( 6, true, HIGH, 2, 100, 64, 32);
80 MBUS_CONF( 8, true, HIGH, 0, 256, 128, 64);
81 MBUS_CONF(11, true, HIGH, 0, 256, 128, 100);
82 MBUS_CONF(14, true, HIGH, 0, 1024, 256, 64);
83 MBUS_CONF(16, true, HIGHEST, 6, 8192, 2800, 2400);
84 MBUS_CONF(21, true, HIGHEST, 6, 2048, 768, 512);
85 MBUS_CONF(25, true, HIGHEST, 0, 100, 64, 32);
86 MBUS_CONF(26, true, HIGH, 2, 8192, 5500, 5000);
87 MBUS_CONF(37, true, HIGH, 0, 256, 128, 64);
88 MBUS_CONF(38, true, HIGH, 2, 100, 64, 32);
89 MBUS_CONF(39, true, HIGH, 2, 8192, 5500, 5000);
90 MBUS_CONF(40, true, HIGH, 2, 100, 64, 32);
91
92 dmb();
93}
94
95static void mctl_sys_init(struct dram_para *para)
96{
97 struct sunxi_ccm_reg * const ccm =
98 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
99 struct sunxi_mctl_com_reg * const mctl_com =
100 (struct sunxi_mctl_com_reg *)SUNXI_DRAM_COM_BASE;
101 struct sunxi_mctl_ctl_reg * const mctl_ctl =
102 (struct sunxi_mctl_ctl_reg *)SUNXI_DRAM_CTL0_BASE;
103
104 /* Put all DRAM-related blocks to reset state */
105 clrbits_le32(&ccm->mbus_cfg, MBUS_ENABLE);
106 clrbits_le32(&ccm->mbus_cfg, MBUS_RESET);
107 clrbits_le32(&ccm->dram_gate_reset, BIT(GATE_SHIFT));
108 udelay(5);
109 clrbits_le32(&ccm->dram_gate_reset, BIT(RESET_SHIFT));
110 clrbits_le32(&ccm->pll5_cfg, CCM_PLL5_CTRL_EN);
111 clrbits_le32(&ccm->dram_clk_cfg, DRAM_MOD_RESET);
112
113 udelay(5);
114
115 /* Set PLL5 rate to doubled DRAM clock rate */
116 writel(CCM_PLL5_CTRL_EN | CCM_PLL5_LOCK_EN | CCM_PLL5_OUT_EN |
Andre Przywara0f7c8bc2021-05-05 13:53:05 +0100117 CCM_PLL5_CTRL_N(para->clk * 2 / 24), &ccm->pll5_cfg);
Jernej Skrabece4aa24b2021-01-11 21:11:43 +0100118 mctl_await_completion(&ccm->pll5_cfg, CCM_PLL5_LOCK, CCM_PLL5_LOCK);
119
120 /* Configure DRAM mod clock */
121 writel(DRAM_CLK_SRC_PLL5, &ccm->dram_clk_cfg);
122 writel(BIT(RESET_SHIFT), &ccm->dram_gate_reset);
123 udelay(5);
124 setbits_le32(&ccm->dram_gate_reset, BIT(GATE_SHIFT));
125
126 /* Disable all channels */
127 writel(0, &mctl_com->maer0);
128 writel(0, &mctl_com->maer1);
129 writel(0, &mctl_com->maer2);
130
131 /* Configure MBUS and enable DRAM mod reset */
132 setbits_le32(&ccm->mbus_cfg, MBUS_RESET);
133 setbits_le32(&ccm->mbus_cfg, MBUS_ENABLE);
134
135 clrbits_le32(&mctl_com->unk_0x500, BIT(25));
136
137 setbits_le32(&ccm->dram_clk_cfg, DRAM_MOD_RESET);
138 udelay(5);
139
140 /* Unknown hack, which enables access of mctl_ctl regs */
141 writel(0x8000, &mctl_ctl->clken);
142}
143
144static void mctl_set_addrmap(struct dram_para *para)
145{
146 struct sunxi_mctl_ctl_reg * const mctl_ctl =
147 (struct sunxi_mctl_ctl_reg *)SUNXI_DRAM_CTL0_BASE;
148 u8 cols = para->cols;
149 u8 rows = para->rows;
150 u8 ranks = para->ranks;
151
152 if (!para->bus_full_width)
153 cols -= 1;
154
155 /* Ranks */
156 if (ranks == 2)
157 mctl_ctl->addrmap[0] = rows + cols - 3;
158 else
159 mctl_ctl->addrmap[0] = 0x1F;
160
161 /* Banks, hardcoded to 8 banks now */
162 mctl_ctl->addrmap[1] = (cols - 2) | (cols - 2) << 8 | (cols - 2) << 16;
163
164 /* Columns */
165 mctl_ctl->addrmap[2] = 0;
166 switch (cols) {
167 case 7:
168 mctl_ctl->addrmap[3] = 0x1F1F1F00;
169 mctl_ctl->addrmap[4] = 0x1F1F;
170 break;
171 case 8:
172 mctl_ctl->addrmap[3] = 0x1F1F0000;
173 mctl_ctl->addrmap[4] = 0x1F1F;
174 break;
175 case 9:
176 mctl_ctl->addrmap[3] = 0x1F000000;
177 mctl_ctl->addrmap[4] = 0x1F1F;
178 break;
179 case 10:
180 mctl_ctl->addrmap[3] = 0;
181 mctl_ctl->addrmap[4] = 0x1F1F;
182 break;
183 case 11:
184 mctl_ctl->addrmap[3] = 0;
185 mctl_ctl->addrmap[4] = 0x1F00;
186 break;
187 case 12:
188 mctl_ctl->addrmap[3] = 0;
189 mctl_ctl->addrmap[4] = 0;
190 break;
191 default:
192 panic("Unsupported DRAM configuration: column number invalid\n");
193 }
194
195 /* Rows */
196 mctl_ctl->addrmap[5] = (cols - 3) | ((cols - 3) << 8) | ((cols - 3) << 16) | ((cols - 3) << 24);
197 switch (rows) {
198 case 13:
199 mctl_ctl->addrmap[6] = (cols - 3) | 0x0F0F0F00;
200 mctl_ctl->addrmap[7] = 0x0F0F;
201 break;
202 case 14:
203 mctl_ctl->addrmap[6] = (cols - 3) | ((cols - 3) << 8) | 0x0F0F0000;
204 mctl_ctl->addrmap[7] = 0x0F0F;
205 break;
206 case 15:
207 mctl_ctl->addrmap[6] = (cols - 3) | ((cols - 3) << 8) | ((cols - 3) << 16) | 0x0F000000;
208 mctl_ctl->addrmap[7] = 0x0F0F;
209 break;
210 case 16:
211 mctl_ctl->addrmap[6] = (cols - 3) | ((cols - 3) << 8) | ((cols - 3) << 16) | ((cols - 3) << 24);
212 mctl_ctl->addrmap[7] = 0x0F0F;
213 break;
214 case 17:
215 mctl_ctl->addrmap[6] = (cols - 3) | ((cols - 3) << 8) | ((cols - 3) << 16) | ((cols - 3) << 24);
216 mctl_ctl->addrmap[7] = (cols - 3) | 0x0F00;
217 break;
218 case 18:
219 mctl_ctl->addrmap[6] = (cols - 3) | ((cols - 3) << 8) | ((cols - 3) << 16) | ((cols - 3) << 24);
220 mctl_ctl->addrmap[7] = (cols - 3) | ((cols - 3) << 8);
221 break;
222 default:
223 panic("Unsupported DRAM configuration: row number invalid\n");
224 }
225
226 /* Bank groups, DDR4 only */
227 mctl_ctl->addrmap[8] = 0x3F3F;
228}
229
230static const u8 phy_init[] = {
231 0x07, 0x0b, 0x02, 0x16, 0x0d, 0x0e, 0x14, 0x19,
232 0x0a, 0x15, 0x03, 0x13, 0x04, 0x0c, 0x10, 0x06,
233 0x0f, 0x11, 0x1a, 0x01, 0x12, 0x17, 0x00, 0x08,
234 0x09, 0x05, 0x18
235};
236
237static void mctl_phy_configure_odt(void)
238{
239 writel_relaxed(0xe, SUNXI_DRAM_PHY0_BASE + 0x388);
240 writel_relaxed(0xe, SUNXI_DRAM_PHY0_BASE + 0x38c);
241
242 writel_relaxed(0xe, SUNXI_DRAM_PHY0_BASE + 0x3c8);
243 writel_relaxed(0xe, SUNXI_DRAM_PHY0_BASE + 0x3cc);
244
245 writel_relaxed(0xe, SUNXI_DRAM_PHY0_BASE + 0x408);
246 writel_relaxed(0xe, SUNXI_DRAM_PHY0_BASE + 0x40c);
247
248 writel_relaxed(0xe, SUNXI_DRAM_PHY0_BASE + 0x448);
249 writel_relaxed(0xe, SUNXI_DRAM_PHY0_BASE + 0x44c);
250
251 writel_relaxed(0xe, SUNXI_DRAM_PHY0_BASE + 0x340);
252 writel_relaxed(0xe, SUNXI_DRAM_PHY0_BASE + 0x344);
253
254 writel_relaxed(0xe, SUNXI_DRAM_PHY0_BASE + 0x348);
255 writel_relaxed(0xe, SUNXI_DRAM_PHY0_BASE + 0x34c);
256
257 writel_relaxed(0x8, SUNXI_DRAM_PHY0_BASE + 0x380);
258 writel_relaxed(0x8, SUNXI_DRAM_PHY0_BASE + 0x384);
259
260 writel_relaxed(0x8, SUNXI_DRAM_PHY0_BASE + 0x3c0);
261 writel_relaxed(0x8, SUNXI_DRAM_PHY0_BASE + 0x3c4);
262
263 writel_relaxed(0x8, SUNXI_DRAM_PHY0_BASE + 0x400);
264 writel_relaxed(0x8, SUNXI_DRAM_PHY0_BASE + 0x404);
265
266 writel_relaxed(0x8, SUNXI_DRAM_PHY0_BASE + 0x440);
267 writel_relaxed(0x8, SUNXI_DRAM_PHY0_BASE + 0x444);
268
269 dmb();
270}
271
272static bool mctl_phy_write_leveling(struct dram_para *para)
273{
274 bool result = true;
275 u32 val;
276
277 clrsetbits_le32(SUNXI_DRAM_PHY0_BASE + 8, 0xc0, 0x80);
278 writel(4, SUNXI_DRAM_PHY0_BASE + 0xc);
279 writel(0x40, SUNXI_DRAM_PHY0_BASE + 0x10);
280
281 setbits_le32(SUNXI_DRAM_PHY0_BASE + 8, 4);
282
283 if (para->bus_full_width)
284 val = 0xf;
285 else
286 val = 3;
287
288 mctl_await_completion((u32*)(SUNXI_DRAM_PHY0_BASE + 0x188), val, val);
289
290 clrbits_le32(SUNXI_DRAM_PHY0_BASE + 8, 4);
291
292 val = readl(SUNXI_DRAM_PHY0_BASE + 0x258);
293 if (val == 0 || val == 0x3f)
294 result = false;
295 val = readl(SUNXI_DRAM_PHY0_BASE + 0x25c);
296 if (val == 0 || val == 0x3f)
297 result = false;
298 val = readl(SUNXI_DRAM_PHY0_BASE + 0x318);
299 if (val == 0 || val == 0x3f)
300 result = false;
301 val = readl(SUNXI_DRAM_PHY0_BASE + 0x31c);
302 if (val == 0 || val == 0x3f)
303 result = false;
304
305 clrbits_le32(SUNXI_DRAM_PHY0_BASE + 8, 0xc0);
306
307 if (para->ranks == 2) {
308 clrsetbits_le32(SUNXI_DRAM_PHY0_BASE + 8, 0xc0, 0x40);
309
310 setbits_le32(SUNXI_DRAM_PHY0_BASE + 8, 4);
311
312 if (para->bus_full_width)
313 val = 0xf;
314 else
315 val = 3;
316
317 mctl_await_completion((u32*)(SUNXI_DRAM_PHY0_BASE + 0x188), val, val);
318
319 clrbits_le32(SUNXI_DRAM_PHY0_BASE + 8, 4);
320 }
321
322 clrbits_le32(SUNXI_DRAM_PHY0_BASE + 8, 0xc0);
323
324 return result;
325}
326
327static bool mctl_phy_read_calibration(struct dram_para *para)
328{
329 bool result = true;
330 u32 val, tmp;
331
332 clrsetbits_le32(SUNXI_DRAM_PHY0_BASE + 8, 0x30, 0x20);
333
334 setbits_le32(SUNXI_DRAM_PHY0_BASE + 8, 1);
335
336 if (para->bus_full_width)
337 val = 0xf;
338 else
339 val = 3;
340
341 while ((readl(SUNXI_DRAM_PHY0_BASE + 0x184) & val) != val) {
342 if (readl(SUNXI_DRAM_PHY0_BASE + 0x184) & 0x20) {
343 result = false;
344 break;
345 }
346 }
347
348 clrbits_le32(SUNXI_DRAM_PHY0_BASE + 8, 1);
349
350 clrbits_le32(SUNXI_DRAM_PHY0_BASE + 8, 0x30);
351
352 if (para->ranks == 2) {
353 clrsetbits_le32(SUNXI_DRAM_PHY0_BASE + 8, 0x30, 0x10);
354
355 setbits_le32(SUNXI_DRAM_PHY0_BASE + 8, 1);
356
357 while ((readl(SUNXI_DRAM_PHY0_BASE + 0x184) & val) != val) {
358 if (readl(SUNXI_DRAM_PHY0_BASE + 0x184) & 0x20) {
359 result = false;
360 break;
361 }
362 }
363
Jernej Skrabec4e048422022-01-29 16:58:43 +0100364 clrbits_le32(SUNXI_DRAM_PHY0_BASE + 8, 1);
Jernej Skrabece4aa24b2021-01-11 21:11:43 +0100365 }
366
367 clrbits_le32(SUNXI_DRAM_PHY0_BASE + 8, 0x30);
368
369 val = readl(SUNXI_DRAM_PHY0_BASE + 0x274) & 7;
370 tmp = readl(SUNXI_DRAM_PHY0_BASE + 0x26c) & 7;
371 if (val < tmp)
372 val = tmp;
373 tmp = readl(SUNXI_DRAM_PHY0_BASE + 0x32c) & 7;
374 if (val < tmp)
375 val = tmp;
376 tmp = readl(SUNXI_DRAM_PHY0_BASE + 0x334) & 7;
377 if (val < tmp)
378 val = tmp;
379 clrsetbits_le32(SUNXI_DRAM_PHY0_BASE + 0x38, 0x7, (val + 2) & 7);
380
381 setbits_le32(SUNXI_DRAM_PHY0_BASE + 4, 0x20);
382
383 return result;
384}
385
386static bool mctl_phy_read_training(struct dram_para *para)
387{
388 u32 val1, val2, *ptr1, *ptr2;
389 bool result = true;
390 int i;
391
392 clrsetbits_le32(SUNXI_DRAM_PHY0_BASE + 0x198, 3, 2);
393 clrsetbits_le32(SUNXI_DRAM_PHY0_BASE + 0x804, 0x3f, 0xf);
394 clrsetbits_le32(SUNXI_DRAM_PHY0_BASE + 0x808, 0x3f, 0xf);
395 clrsetbits_le32(SUNXI_DRAM_PHY0_BASE + 0xa04, 0x3f, 0xf);
396 clrsetbits_le32(SUNXI_DRAM_PHY0_BASE + 0xa08, 0x3f, 0xf);
397
398 setbits_le32(SUNXI_DRAM_PHY0_BASE + 0x190, 6);
399 setbits_le32(SUNXI_DRAM_PHY0_BASE + 0x190, 1);
400
401 mctl_await_completion((u32*)(SUNXI_DRAM_PHY0_BASE + 0x840), 0xc, 0xc);
402 if (readl(SUNXI_DRAM_PHY0_BASE + 0x840) & 3)
403 result = false;
404
405 if (para->bus_full_width) {
406 mctl_await_completion((u32*)(SUNXI_DRAM_PHY0_BASE + 0xa40), 0xc, 0xc);
407 if (readl(SUNXI_DRAM_PHY0_BASE + 0xa40) & 3)
408 result = false;
409 }
410
411 ptr1 = (u32*)(SUNXI_DRAM_PHY0_BASE + 0x898);
412 ptr2 = (u32*)(SUNXI_DRAM_PHY0_BASE + 0x850);
413 for (i = 0; i < 9; i++) {
414 val1 = readl(&ptr1[i]);
415 val2 = readl(&ptr2[i]);
416 if (val1 - val2 <= 6)
417 result = false;
418 }
419 ptr1 = (u32*)(SUNXI_DRAM_PHY0_BASE + 0x8bc);
420 ptr2 = (u32*)(SUNXI_DRAM_PHY0_BASE + 0x874);
421 for (i = 0; i < 9; i++) {
422 val1 = readl(&ptr1[i]);
423 val2 = readl(&ptr2[i]);
424 if (val1 - val2 <= 6)
425 result = false;
426 }
427
428 if (para->bus_full_width) {
429 ptr1 = (u32*)(SUNXI_DRAM_PHY0_BASE + 0xa98);
430 ptr2 = (u32*)(SUNXI_DRAM_PHY0_BASE + 0xa50);
431 for (i = 0; i < 9; i++) {
432 val1 = readl(&ptr1[i]);
433 val2 = readl(&ptr2[i]);
434 if (val1 - val2 <= 6)
435 result = false;
436 }
437
438 ptr1 = (u32*)(SUNXI_DRAM_PHY0_BASE + 0xabc);
439 ptr2 = (u32*)(SUNXI_DRAM_PHY0_BASE + 0xa74);
440 for (i = 0; i < 9; i++) {
441 val1 = readl(&ptr1[i]);
442 val2 = readl(&ptr2[i]);
443 if (val1 - val2 <= 6)
444 result = false;
445 }
446 }
447
448 clrbits_le32(SUNXI_DRAM_PHY0_BASE + 0x190, 3);
449
450 if (para->ranks == 2) {
451 /* maybe last parameter should be 1? */
452 clrsetbits_le32(SUNXI_DRAM_PHY0_BASE + 0x198, 3, 2);
453
454 setbits_le32(SUNXI_DRAM_PHY0_BASE + 0x190, 6);
455 setbits_le32(SUNXI_DRAM_PHY0_BASE + 0x190, 1);
456
457 mctl_await_completion((u32*)(SUNXI_DRAM_PHY0_BASE + 0x840), 0xc, 0xc);
458 if (readl(SUNXI_DRAM_PHY0_BASE + 0x840) & 3)
459 result = false;
460
461 if (para->bus_full_width) {
462 mctl_await_completion((u32*)(SUNXI_DRAM_PHY0_BASE + 0xa40), 0xc, 0xc);
463 if (readl(SUNXI_DRAM_PHY0_BASE + 0xa40) & 3)
464 result = false;
465 }
466
467 clrbits_le32(SUNXI_DRAM_PHY0_BASE + 0x190, 3);
468 }
469
470 clrbits_le32(SUNXI_DRAM_PHY0_BASE + 0x198, 3);
471
472 return result;
473}
474
475static bool mctl_phy_write_training(struct dram_para *para)
476{
477 u32 val1, val2, *ptr1, *ptr2;
478 bool result = true;
479 int i;
480
481 writel(0, SUNXI_DRAM_PHY0_BASE + 0x134);
482 writel(0, SUNXI_DRAM_PHY0_BASE + 0x138);
483 writel(0, SUNXI_DRAM_PHY0_BASE + 0x19c);
484 writel(0, SUNXI_DRAM_PHY0_BASE + 0x1a0);
485
486 clrsetbits_le32(SUNXI_DRAM_PHY0_BASE + 0x198, 0xc, 8);
487
488 setbits_le32(SUNXI_DRAM_PHY0_BASE + 0x190, 0x10);
489 setbits_le32(SUNXI_DRAM_PHY0_BASE + 0x190, 0x20);
490
491 mctl_await_completion((u32*)(SUNXI_DRAM_PHY0_BASE + 0x8e0), 3, 3);
492 if (readl(SUNXI_DRAM_PHY0_BASE + 0x8e0) & 0xc)
493 result = false;
494
495 if (para->bus_full_width) {
496 mctl_await_completion((u32*)(SUNXI_DRAM_PHY0_BASE + 0xae0), 3, 3);
497 if (readl(SUNXI_DRAM_PHY0_BASE + 0xae0) & 0xc)
498 result = false;
499 }
500
501 ptr1 = (u32*)(SUNXI_DRAM_PHY0_BASE + 0x938);
502 ptr2 = (u32*)(SUNXI_DRAM_PHY0_BASE + 0x8f0);
503 for (i = 0; i < 9; i++) {
504 val1 = readl(&ptr1[i]);
505 val2 = readl(&ptr2[i]);
506 if (val1 - val2 <= 6)
507 result = false;
508 }
509 ptr1 = (u32*)(SUNXI_DRAM_PHY0_BASE + 0x95c);
510 ptr2 = (u32*)(SUNXI_DRAM_PHY0_BASE + 0x914);
511 for (i = 0; i < 9; i++) {
512 val1 = readl(&ptr1[i]);
513 val2 = readl(&ptr2[i]);
514 if (val1 - val2 <= 6)
515 result = false;
516 }
517
518 if (para->bus_full_width) {
519 ptr1 = (u32*)(SUNXI_DRAM_PHY0_BASE + 0xb38);
520 ptr2 = (u32*)(SUNXI_DRAM_PHY0_BASE + 0xaf0);
521 for (i = 0; i < 9; i++) {
522 val1 = readl(&ptr1[i]);
523 val2 = readl(&ptr2[i]);
524 if (val1 - val2 <= 6)
525 result = false;
526 }
527 ptr1 = (u32*)(SUNXI_DRAM_PHY0_BASE + 0xb5c);
528 ptr2 = (u32*)(SUNXI_DRAM_PHY0_BASE + 0xb14);
529 for (i = 0; i < 9; i++) {
530 val1 = readl(&ptr1[i]);
531 val2 = readl(&ptr2[i]);
532 if (val1 - val2 <= 6)
533 result = false;
534 }
535 }
536
537 clrbits_le32(SUNXI_DRAM_PHY0_BASE + 0x190, 0x60);
538
539 if (para->ranks == 2) {
540 clrsetbits_le32(SUNXI_DRAM_PHY0_BASE + 0x198, 0xc, 4);
541
542 setbits_le32(SUNXI_DRAM_PHY0_BASE + 0x190, 0x10);
543 setbits_le32(SUNXI_DRAM_PHY0_BASE + 0x190, 0x20);
544
545 mctl_await_completion((u32*)(SUNXI_DRAM_PHY0_BASE + 0x8e0), 3, 3);
546 if (readl(SUNXI_DRAM_PHY0_BASE + 0x8e0) & 0xc)
547 result = false;
548
549 if (para->bus_full_width) {
550 mctl_await_completion((u32*)(SUNXI_DRAM_PHY0_BASE + 0xae0), 3, 3);
551 if (readl(SUNXI_DRAM_PHY0_BASE + 0xae0) & 0xc)
552 result = false;
553 }
554
555 clrbits_le32(SUNXI_DRAM_PHY0_BASE + 0x190, 0x60);
556 }
557
558 clrbits_le32(SUNXI_DRAM_PHY0_BASE + 0x198, 0xc);
559
560 return result;
561}
562
563static bool mctl_phy_bit_delay_compensation(struct dram_para *para)
564{
565 u32 *ptr;
566 int i;
567
568 clrbits_le32(SUNXI_DRAM_PHY0_BASE + 0x60, 1);
569 setbits_le32(SUNXI_DRAM_PHY0_BASE + 8, 8);
570 clrbits_le32(SUNXI_DRAM_PHY0_BASE + 0x190, 0x10);
571
572 ptr = (u32*)(SUNXI_DRAM_PHY0_BASE + 0x484);
573 for (i = 0; i < 9; i++) {
574 writel_relaxed(0x16, ptr);
575 writel_relaxed(0x16, ptr + 0x30);
576 ptr += 2;
577 }
578 writel_relaxed(0x1c, SUNXI_DRAM_PHY0_BASE + 0x4d0);
579 writel_relaxed(0x1c, SUNXI_DRAM_PHY0_BASE + 0x590);
580 writel_relaxed(0x1c, SUNXI_DRAM_PHY0_BASE + 0x4cc);
581 writel_relaxed(0x1c, SUNXI_DRAM_PHY0_BASE + 0x58c);
582
583 ptr = (u32*)(SUNXI_DRAM_PHY0_BASE + 0x4d8);
584 for (i = 0; i < 9; i++) {
585 writel_relaxed(0x1a, ptr);
586 writel_relaxed(0x1a, ptr + 0x30);
587 ptr += 2;
588 }
589 writel_relaxed(0x1e, SUNXI_DRAM_PHY0_BASE + 0x524);
590 writel_relaxed(0x1e, SUNXI_DRAM_PHY0_BASE + 0x5e4);
591 writel_relaxed(0x1e, SUNXI_DRAM_PHY0_BASE + 0x520);
592 writel_relaxed(0x1e, SUNXI_DRAM_PHY0_BASE + 0x5e0);
593
594 ptr = (u32*)(SUNXI_DRAM_PHY0_BASE + 0x604);
595 for (i = 0; i < 9; i++) {
596 writel_relaxed(0x1a, ptr);
597 writel_relaxed(0x1a, ptr + 0x30);
598 ptr += 2;
599 }
600 writel_relaxed(0x1e, SUNXI_DRAM_PHY0_BASE + 0x650);
601 writel_relaxed(0x1e, SUNXI_DRAM_PHY0_BASE + 0x710);
602 writel_relaxed(0x1e, SUNXI_DRAM_PHY0_BASE + 0x64c);
603 writel_relaxed(0x1e, SUNXI_DRAM_PHY0_BASE + 0x70c);
604
605 ptr = (u32*)(SUNXI_DRAM_PHY0_BASE + 0x658);
606 for (i = 0; i < 9; i++) {
607 writel_relaxed(0x1a, ptr);
608 writel_relaxed(0x1a, ptr + 0x30);
609 ptr += 2;
610 }
611 writel_relaxed(0x1e, SUNXI_DRAM_PHY0_BASE + 0x6a4);
612 writel_relaxed(0x1e, SUNXI_DRAM_PHY0_BASE + 0x764);
613 writel_relaxed(0x1e, SUNXI_DRAM_PHY0_BASE + 0x6a0);
614 writel_relaxed(0x1e, SUNXI_DRAM_PHY0_BASE + 0x760);
615
616 dmb();
617
618 setbits_le32(SUNXI_DRAM_PHY0_BASE + 0x60, 1);
619
620 /* second part */
621 clrbits_le32(SUNXI_DRAM_PHY0_BASE + 0x54, 0x80);
622 clrbits_le32(SUNXI_DRAM_PHY0_BASE + 0x190, 4);
623
624 ptr = (u32*)(SUNXI_DRAM_PHY0_BASE + 0x480);
625 for (i = 0; i < 9; i++) {
626 writel_relaxed(0x10, ptr);
627 writel_relaxed(0x10, ptr + 0x30);
628 ptr += 2;
629 }
630 writel_relaxed(0x18, SUNXI_DRAM_PHY0_BASE + 0x528);
631 writel_relaxed(0x18, SUNXI_DRAM_PHY0_BASE + 0x5e8);
632 writel_relaxed(0x18, SUNXI_DRAM_PHY0_BASE + 0x4c8);
633 writel_relaxed(0x18, SUNXI_DRAM_PHY0_BASE + 0x588);
634
635 ptr = (u32*)(SUNXI_DRAM_PHY0_BASE + 0x4d4);
636 for (i = 0; i < 9; i++) {
637 writel_relaxed(0x12, ptr);
638 writel_relaxed(0x12, ptr + 0x30);
639 ptr += 2;
640 }
641 writel_relaxed(0x1a, SUNXI_DRAM_PHY0_BASE + 0x52c);
642 writel_relaxed(0x1a, SUNXI_DRAM_PHY0_BASE + 0x5ec);
643 writel_relaxed(0x1a, SUNXI_DRAM_PHY0_BASE + 0x51c);
644 writel_relaxed(0x1a, SUNXI_DRAM_PHY0_BASE + 0x5dc);
645
646 ptr = (u32*)(SUNXI_DRAM_PHY0_BASE + 0x600);
647 for (i = 0; i < 9; i++) {
648 writel_relaxed(0x12, ptr);
649 writel_relaxed(0x12, ptr + 0x30);
650 ptr += 2;
651 }
652 writel_relaxed(0x1a, SUNXI_DRAM_PHY0_BASE + 0x6a8);
653 writel_relaxed(0x1a, SUNXI_DRAM_PHY0_BASE + 0x768);
654 writel_relaxed(0x1a, SUNXI_DRAM_PHY0_BASE + 0x648);
655 writel_relaxed(0x1a, SUNXI_DRAM_PHY0_BASE + 0x708);
656
657 ptr = (u32*)(SUNXI_DRAM_PHY0_BASE + 0x654);
658 for (i = 0; i < 9; i++) {
659 writel_relaxed(0x14, ptr);
660 writel_relaxed(0x14, ptr + 0x30);
661 ptr += 2;
662 }
663 writel_relaxed(0x1c, SUNXI_DRAM_PHY0_BASE + 0x6ac);
664 writel_relaxed(0x1c, SUNXI_DRAM_PHY0_BASE + 0x76c);
665 writel_relaxed(0x1c, SUNXI_DRAM_PHY0_BASE + 0x69c);
666 writel_relaxed(0x1c, SUNXI_DRAM_PHY0_BASE + 0x75c);
667
668 dmb();
669
670 setbits_le32(SUNXI_DRAM_PHY0_BASE + 0x54, 0x80);
671
672 return true;
673}
674
675static bool mctl_phy_init(struct dram_para *para)
676{
677 struct sunxi_mctl_com_reg * const mctl_com =
678 (struct sunxi_mctl_com_reg *)SUNXI_DRAM_COM_BASE;
679 struct sunxi_mctl_ctl_reg * const mctl_ctl =
680 (struct sunxi_mctl_ctl_reg *)SUNXI_DRAM_CTL0_BASE;
681 u32 val, *ptr;
682 int i;
683
684 if (para->bus_full_width)
685 val = 0xf;
686 else
687 val = 3;
688 clrsetbits_le32(SUNXI_DRAM_PHY0_BASE + 0x3c, 0xf, val);
689
690 writel(0xd, SUNXI_DRAM_PHY0_BASE + 0x14);
691 writel(0xd, SUNXI_DRAM_PHY0_BASE + 0x35c);
692 writel(0xd, SUNXI_DRAM_PHY0_BASE + 0x368);
693 writel(0xd, SUNXI_DRAM_PHY0_BASE + 0x374);
694
695 writel(0, SUNXI_DRAM_PHY0_BASE + 0x18);
696 writel(0, SUNXI_DRAM_PHY0_BASE + 0x360);
697 writel(0, SUNXI_DRAM_PHY0_BASE + 0x36c);
698 writel(0, SUNXI_DRAM_PHY0_BASE + 0x378);
699
700 writel(9, SUNXI_DRAM_PHY0_BASE + 0x1c);
701 writel(9, SUNXI_DRAM_PHY0_BASE + 0x364);
702 writel(9, SUNXI_DRAM_PHY0_BASE + 0x370);
703 writel(9, SUNXI_DRAM_PHY0_BASE + 0x37c);
704
705 ptr = (u32*)(SUNXI_DRAM_PHY0_BASE + 0xc0);
706 for (i = 0; i < ARRAY_SIZE(phy_init); i++)
707 writel(phy_init[i], &ptr[i]);
708
709 if (IS_ENABLED(CONFIG_DRAM_SUN50I_H616_UNKNOWN_FEATURE)) {
710 ptr = (u32*)(SUNXI_DRAM_PHY0_BASE + 0x780);
711 for (i = 0; i < 32; i++)
712 writel(0x16, &ptr[i]);
713 writel(0xe, SUNXI_DRAM_PHY0_BASE + 0x78c);
714 writel(0xe, SUNXI_DRAM_PHY0_BASE + 0x7a4);
715 writel(0xe, SUNXI_DRAM_PHY0_BASE + 0x7b8);
716 writel(0x8, SUNXI_DRAM_PHY0_BASE + 0x7d4);
717 writel(0xe, SUNXI_DRAM_PHY0_BASE + 0x7dc);
718 writel(0xe, SUNXI_DRAM_PHY0_BASE + 0x7e0);
719 }
720
721 writel(0x80, SUNXI_DRAM_PHY0_BASE + 0x3dc);
722 writel(0x80, SUNXI_DRAM_PHY0_BASE + 0x45c);
723
Jernej Skrabec137ab1c2022-01-29 16:58:42 +0100724 if (IS_ENABLED(CONFIG_DRAM_ODT_EN))
Jernej Skrabece4aa24b2021-01-11 21:11:43 +0100725 mctl_phy_configure_odt();
726
727 clrsetbits_le32(SUNXI_DRAM_PHY0_BASE + 4, 7, 0xa);
728
729 if (para->clk <= 672)
730 writel(0xf, SUNXI_DRAM_PHY0_BASE + 0x20);
731 if (para->clk > 500) {
732 clrbits_le32(SUNXI_DRAM_PHY0_BASE + 0x144, BIT(7));
733 clrbits_le32(SUNXI_DRAM_PHY0_BASE + 0x14c, 0xe0);
734 } else {
735 setbits_le32(SUNXI_DRAM_PHY0_BASE + 0x144, BIT(7));
736 clrsetbits_le32(SUNXI_DRAM_PHY0_BASE + 0x14c, 0xe0, 0x20);
737 }
738
739 clrbits_le32(SUNXI_DRAM_PHY0_BASE + 0x14c, 8);
740
741 mctl_await_completion((u32*)(SUNXI_DRAM_PHY0_BASE + 0x180), 4, 4);
742
743 writel(0x37, SUNXI_DRAM_PHY0_BASE + 0x58);
744 clrbits_le32(&mctl_com->unk_0x500, 0x200);
745
746 writel(0, &mctl_ctl->swctl);
747 setbits_le32(&mctl_ctl->dfimisc, 1);
748
749 /* start DFI init */
750 setbits_le32(&mctl_ctl->dfimisc, 0x20);
751 writel(1, &mctl_ctl->swctl);
752 mctl_await_completion(&mctl_ctl->swstat, 1, 1);
753 /* poll DFI init complete */
754 mctl_await_completion(&mctl_ctl->dfistat, 1, 1);
755 writel(0, &mctl_ctl->swctl);
756 clrbits_le32(&mctl_ctl->dfimisc, 0x20);
757
758 clrbits_le32(&mctl_ctl->pwrctl, 0x20);
759 writel(1, &mctl_ctl->swctl);
760 mctl_await_completion(&mctl_ctl->swstat, 1, 1);
761 mctl_await_completion(&mctl_ctl->statr, 3, 1);
762
763 writel(0, &mctl_ctl->swctl);
764 clrbits_le32(&mctl_ctl->dfimisc, 1);
765
766 writel(1, &mctl_ctl->swctl);
767 mctl_await_completion(&mctl_ctl->swstat, 1, 1);
768
769 writel(0x1f14, &mctl_ctl->mrctrl1);
770 writel(0x80000030, &mctl_ctl->mrctrl0);
771 mctl_await_completion(&mctl_ctl->mrctrl0, BIT(31), 0);
772
773 writel(4, &mctl_ctl->mrctrl1);
774 writel(0x80001030, &mctl_ctl->mrctrl0);
775 mctl_await_completion(&mctl_ctl->mrctrl0, BIT(31), 0);
776
777 writel(0x20, &mctl_ctl->mrctrl1);
778 writel(0x80002030, &mctl_ctl->mrctrl0);
779 mctl_await_completion(&mctl_ctl->mrctrl0, BIT(31), 0);
780
781 writel(0, &mctl_ctl->mrctrl1);
782 writel(0x80003030, &mctl_ctl->mrctrl0);
783 mctl_await_completion(&mctl_ctl->mrctrl0, BIT(31), 0);
784
785 writel(0, SUNXI_DRAM_PHY0_BASE + 0x54);
786
787 writel(0, &mctl_ctl->swctl);
788 clrbits_le32(&mctl_ctl->rfshctl3, 1);
789 writel(1, &mctl_ctl->swctl);
790
791 if (IS_ENABLED(CONFIG_DRAM_SUN50I_H616_WRITE_LEVELING)) {
792 for (i = 0; i < 5; i++)
793 if (mctl_phy_write_leveling(para))
794 break;
795 if (i == 5) {
796 debug("write leveling failed!\n");
797 return false;
798 }
799 }
800
801 if (IS_ENABLED(CONFIG_DRAM_SUN50I_H616_READ_CALIBRATION)) {
802 for (i = 0; i < 5; i++)
803 if (mctl_phy_read_calibration(para))
804 break;
805 if (i == 5) {
806 debug("read calibration failed!\n");
807 return false;
808 }
809 }
810
811 if (IS_ENABLED(CONFIG_DRAM_SUN50I_H616_READ_TRAINING)) {
812 for (i = 0; i < 5; i++)
813 if (mctl_phy_read_training(para))
814 break;
815 if (i == 5) {
816 debug("read training failed!\n");
817 return false;
818 }
819 }
820
821 if (IS_ENABLED(CONFIG_DRAM_SUN50I_H616_WRITE_TRAINING)) {
822 for (i = 0; i < 5; i++)
823 if (mctl_phy_write_training(para))
824 break;
825 if (i == 5) {
826 debug("write training failed!\n");
827 return false;
828 }
829 }
830
831 if (IS_ENABLED(CONFIG_DRAM_SUN50I_H616_BIT_DELAY_COMPENSATION))
832 mctl_phy_bit_delay_compensation(para);
833
834 clrbits_le32(SUNXI_DRAM_PHY0_BASE + 0x60, 4);
835
836 return true;
837}
838
839static bool mctl_ctrl_init(struct dram_para *para)
840{
841 struct sunxi_mctl_com_reg * const mctl_com =
842 (struct sunxi_mctl_com_reg *)SUNXI_DRAM_COM_BASE;
843 struct sunxi_mctl_ctl_reg * const mctl_ctl =
844 (struct sunxi_mctl_ctl_reg *)SUNXI_DRAM_CTL0_BASE;
845 u32 reg_val;
846
847 clrsetbits_le32(&mctl_com->unk_0x500, BIT(24), 0x200);
848 writel(0x8000, &mctl_ctl->clken);
849
850 setbits_le32(&mctl_com->unk_0x008, 0xff00);
851
852 clrsetbits_le32(&mctl_ctl->sched[0], 0xff00, 0x3000);
853
854 writel(0, &mctl_ctl->hwlpctl);
855
856 setbits_le32(&mctl_com->unk_0x008, 0xff00);
857
858 reg_val = MSTR_BURST_LENGTH(8) | MSTR_ACTIVE_RANKS(para->ranks);
859 reg_val |= MSTR_DEVICETYPE_DDR3 | MSTR_2TMODE;
860 if (para->bus_full_width)
861 reg_val |= MSTR_BUSWIDTH_FULL;
862 else
863 reg_val |= MSTR_BUSWIDTH_HALF;
864 writel(BIT(31) | BIT(30) | reg_val, &mctl_ctl->mstr);
865
866 if (para->ranks == 2)
867 writel(0x0303, &mctl_ctl->odtmap);
868 else
869 writel(0x0201, &mctl_ctl->odtmap);
870
871 writel(0x06000400, &mctl_ctl->odtcfg);
872 writel(0x06000400, &mctl_ctl->unk_0x2240);
873 writel(0x06000400, &mctl_ctl->unk_0x3240);
874 writel(0x06000400, &mctl_ctl->unk_0x4240);
875
876 setbits_le32(&mctl_com->cr, BIT(31));
877
878 mctl_set_addrmap(para);
879
880 mctl_set_timing_params(para);
881
882 writel(0, &mctl_ctl->pwrctl);
883
884 setbits_le32(&mctl_ctl->dfiupd[0], BIT(31) | BIT(30));
885 setbits_le32(&mctl_ctl->zqctl[0], BIT(31) | BIT(30));
886 setbits_le32(&mctl_ctl->unk_0x2180, BIT(31) | BIT(30));
887 setbits_le32(&mctl_ctl->unk_0x3180, BIT(31) | BIT(30));
888 setbits_le32(&mctl_ctl->unk_0x4180, BIT(31) | BIT(30));
889
890 setbits_le32(&mctl_ctl->rfshctl3, BIT(0));
891 clrbits_le32(&mctl_ctl->dfimisc, BIT(0));
892
893 writel(0, &mctl_com->maer0);
894 writel(0, &mctl_com->maer1);
895 writel(0, &mctl_com->maer2);
896
897 writel(0x20, &mctl_ctl->pwrctl);
898 setbits_le32(&mctl_ctl->clken, BIT(8));
899
900 clrsetbits_le32(&mctl_com->unk_0x500, BIT(24), 0x300);
901 /* this write seems to enable PHY MMIO region */
902 setbits_le32(&mctl_com->unk_0x500, BIT(24));
903
904 if (!mctl_phy_init(para))
905 return false;
906
907 writel(0, &mctl_ctl->swctl);
908 clrbits_le32(&mctl_ctl->rfshctl3, BIT(0));
909
910 setbits_le32(&mctl_com->unk_0x014, BIT(31));
911 writel(0xffffffff, &mctl_com->maer0);
912 writel(0x7ff, &mctl_com->maer1);
913 writel(0xffff, &mctl_com->maer2);
914
915 writel(1, &mctl_ctl->swctl);
916 mctl_await_completion(&mctl_ctl->swstat, 1, 1);
917
918 return true;
919}
920
921static bool mctl_core_init(struct dram_para *para)
922{
923 mctl_sys_init(para);
924
925 return mctl_ctrl_init(para);
926}
927
928static void mctl_auto_detect_rank_width(struct dram_para *para)
929{
930 /* this is minimum size that it's supported */
931 para->cols = 8;
932 para->rows = 13;
933
934 /*
935 * Strategy here is to test most demanding combination first and least
936 * demanding last, otherwise HW might not be fully utilized. For
937 * example, half bus width and rank = 1 combination would also work
938 * on HW with full bus width and rank = 2, but only 1/4 RAM would be
939 * visible.
940 */
941
942 debug("testing 32-bit width, rank = 2\n");
943 para->bus_full_width = 1;
944 para->ranks = 2;
945 if (mctl_core_init(para))
946 return;
947
948 debug("testing 32-bit width, rank = 1\n");
949 para->bus_full_width = 1;
950 para->ranks = 1;
951 if (mctl_core_init(para))
952 return;
953
954 debug("testing 16-bit width, rank = 2\n");
955 para->bus_full_width = 0;
956 para->ranks = 2;
957 if (mctl_core_init(para))
958 return;
959
960 debug("testing 16-bit width, rank = 1\n");
961 para->bus_full_width = 0;
962 para->ranks = 1;
963 if (mctl_core_init(para))
964 return;
965
966 panic("This DRAM setup is currently not supported.\n");
967}
968
969static void mctl_auto_detect_dram_size(struct dram_para *para)
970{
971 /* detect row address bits */
972 para->cols = 8;
973 para->rows = 18;
974 mctl_core_init(para);
975
976 for (para->rows = 13; para->rows < 18; para->rows++) {
977 /* 8 banks, 8 bit per byte and 16/32 bit width */
978 if (mctl_mem_matches((1 << (para->rows + para->cols +
979 4 + para->bus_full_width))))
980 break;
981 }
982
983 /* detect column address bits */
984 para->cols = 11;
985 mctl_core_init(para);
986
987 for (para->cols = 8; para->cols < 11; para->cols++) {
988 /* 8 bits per byte and 16/32 bit width */
989 if (mctl_mem_matches(1 << (para->cols + 1 +
990 para->bus_full_width)))
991 break;
992 }
993}
994
995static unsigned long mctl_calc_size(struct dram_para *para)
996{
997 u8 width = para->bus_full_width ? 4 : 2;
998
999 /* 8 banks */
1000 return (1ULL << (para->cols + para->rows + 3)) * width * para->ranks;
1001}
1002
1003unsigned long sunxi_dram_init(void)
1004{
Jernej Skrabece04cd492022-01-30 15:27:13 +01001005 struct sunxi_prcm_reg *const prcm =
1006 (struct sunxi_prcm_reg *)SUNXI_PRCM_BASE;
Jernej Skrabece4aa24b2021-01-11 21:11:43 +01001007 struct dram_para para = {
1008 .clk = CONFIG_DRAM_CLK,
1009 .type = SUNXI_DRAM_TYPE_DDR3,
1010 };
1011 unsigned long size;
1012
Jernej Skrabece04cd492022-01-30 15:27:13 +01001013 setbits_le32(&prcm->res_cal_ctrl, BIT(8));
1014 clrbits_le32(&prcm->ohms240, 0x3f);
Jernej Skrabece4aa24b2021-01-11 21:11:43 +01001015
1016 mctl_auto_detect_rank_width(&para);
1017 mctl_auto_detect_dram_size(&para);
1018
1019 mctl_core_init(&para);
1020
1021 size = mctl_calc_size(&para);
1022
1023 mctl_set_master_priority();
1024
1025 return size;
1026};