blob: dd0fc37b763d7c416d4ff192fd95399a7b800cc1 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
vishnupatekare302fe62015-11-29 01:07:25 +08002/*
3 * Sun8i a33 platform dram controller init.
4 *
5 * (C) Copyright 2007-2015 Allwinner Technology Co.
6 * Jerry Wang <wangflord@allwinnertech.com>
7 * (C) Copyright 2015 Vishnu Patekar <vishnupatekar0510@gmail.com>
8 * (C) Copyright 2015 Hans de Goede <hdegoede@redhat.com>
vishnupatekare302fe62015-11-29 01:07:25 +08009 */
vishnupatekare302fe62015-11-29 01:07:25 +080010#include <errno.h>
Simon Glass97589732020-05-10 11:40:02 -060011#include <init.h>
vishnupatekare302fe62015-11-29 01:07:25 +080012#include <asm/io.h>
13#include <asm/arch/clock.h>
14#include <asm/arch/dram.h>
15#include <asm/arch/prcm.h>
Simon Glassdbd79542020-05-10 11:40:11 -060016#include <linux/delay.h>
vishnupatekare302fe62015-11-29 01:07:25 +080017
18#define DRAM_CLK_MUL 2
19#define DRAM_CLK_DIV 1
20
21struct dram_para {
22 u8 cs1;
23 u8 seq;
24 u8 bank;
25 u8 rank;
26 u8 rows;
27 u8 bus_width;
Vishnu Patekarc49936f2016-01-12 01:20:58 +080028 u8 dram_type;
vishnupatekare302fe62015-11-29 01:07:25 +080029 u16 page_size;
30};
31
32static void mctl_set_cr(struct dram_para *para)
33{
34 struct sunxi_mctl_com_reg * const mctl_com =
35 (struct sunxi_mctl_com_reg *)SUNXI_DRAM_COM_BASE;
36
37 writel(MCTL_CR_CS1_CONTROL(para->cs1) | MCTL_CR_UNKNOWN |
Vishnu Patekarc49936f2016-01-12 01:20:58 +080038 MCTL_CR_CHANNEL(1) | MCTL_CR_DRAM_TYPE(para->dram_type) |
vishnupatekare302fe62015-11-29 01:07:25 +080039 (para->seq ? MCTL_CR_SEQUENCE : 0) |
40 ((para->bus_width == 16) ? MCTL_CR_BUSW16 : MCTL_CR_BUSW8) |
41 MCTL_CR_PAGE_SIZE(para->page_size) | MCTL_CR_ROW(para->rows) |
42 MCTL_CR_BANK(para->bank) | MCTL_CR_RANK(para->rank),
43 &mctl_com->cr);
44}
45
46static void auto_detect_dram_size(struct dram_para *para)
47{
48 u8 orig_rank = para->rank;
49 int rows, columns;
50
51 /* Row detect */
52 para->page_size = 512;
53 para->seq = 1;
54 para->rows = 16;
55 para->rank = 1;
56 mctl_set_cr(para);
57 for (rows = 11 ; rows < 16 ; rows++) {
58 if (mctl_mem_matches(1 << (rows + 9))) /* row-column */
59 break;
60 }
61
62 /* Column (page size) detect */
63 para->rows = 11;
64 para->page_size = 8192;
65 mctl_set_cr(para);
66 for (columns = 9 ; columns < 13 ; columns++) {
67 if (mctl_mem_matches(1 << columns))
68 break;
69 }
70
71 para->seq = 0;
72 para->rank = orig_rank;
73 para->rows = rows;
74 para->page_size = 1 << columns;
75 mctl_set_cr(para);
76}
77
78static inline int ns_to_t(int nanoseconds)
79{
80 const unsigned int ctrl_freq =
81 CONFIG_DRAM_CLK * DRAM_CLK_MUL / DRAM_CLK_DIV;
82
83 return (ctrl_freq * nanoseconds + 999) / 1000;
84}
85
86static void auto_set_timing_para(struct dram_para *para)
87{
88 struct sunxi_mctl_ctl_reg * const mctl_ctl =
89 (struct sunxi_mctl_ctl_reg *)SUNXI_DRAM_CTL0_BASE;
Vishnu Patekarc49936f2016-01-12 01:20:58 +080090
vishnupatekare302fe62015-11-29 01:07:25 +080091 u32 reg_val;
92
93 u8 tccd = 2;
94 u8 tfaw = ns_to_t(50);
95 u8 trrd = max(ns_to_t(10), 4);
96 u8 trcd = ns_to_t(15);
97 u8 trc = ns_to_t(53);
98 u8 txp = max(ns_to_t(8), 3);
99 u8 twtr = max(ns_to_t(8), 4);
100 u8 trtp = max(ns_to_t(8), 4);
101 u8 twr = max(ns_to_t(15), 3);
102 u8 trp = ns_to_t(15);
103 u8 tras = ns_to_t(38);
104
105 u16 trefi = ns_to_t(7800) / 32;
106 u16 trfc = ns_to_t(350);
107
108 /* Fixed timing parameters */
109 u8 tmrw = 0;
110 u8 tmrd = 4;
111 u8 tmod = 12;
112 u8 tcke = 3;
113 u8 tcksrx = 5;
114 u8 tcksre = 5;
115 u8 tckesr = 4;
116 u8 trasmax = 24;
117 u8 tcl = 6; /* CL 12 */
118 u8 tcwl = 4; /* CWL 8 */
119 u8 t_rdata_en = 4;
120 u8 wr_latency = 2;
121
122 u32 tdinit0 = (500 * CONFIG_DRAM_CLK) + 1; /* 500us */
123 u32 tdinit1 = (360 * CONFIG_DRAM_CLK) / 1000 + 1; /* 360ns */
124 u32 tdinit2 = (200 * CONFIG_DRAM_CLK) + 1; /* 200us */
125 u32 tdinit3 = (1 * CONFIG_DRAM_CLK) + 1; /* 1us */
126
127 u8 twtp = tcwl + 2 + twr; /* WL + BL / 2 + tWR */
128 u8 twr2rd = tcwl + 2 + twtr; /* WL + BL / 2 + tWTR */
129 u8 trd2wr = tcl + 2 + 1 - tcwl; /* RL + BL / 2 + 2 - WL */
130
131 /* Set work mode register */
132 mctl_set_cr(para);
133 /* Set mode register */
Vishnu Patekarad8baac2016-01-12 01:20:59 +0800134 if (para->dram_type == DRAM_TYPE_DDR3) {
135 writel(MCTL_MR0, &mctl_ctl->mr0);
136 writel(MCTL_MR1, &mctl_ctl->mr1);
137 writel(MCTL_MR2, &mctl_ctl->mr2);
138 writel(MCTL_MR3, &mctl_ctl->mr3);
139 } else if (para->dram_type == DRAM_TYPE_LPDDR3) {
140 writel(MCTL_LPDDR3_MR0, &mctl_ctl->mr0);
141 writel(MCTL_LPDDR3_MR1, &mctl_ctl->mr1);
142 writel(MCTL_LPDDR3_MR2, &mctl_ctl->mr2);
143 writel(MCTL_LPDDR3_MR3, &mctl_ctl->mr3);
144
145 /* timing parameters for LPDDR3 */
146 tfaw = max(ns_to_t(50), 4);
147 trrd = max(ns_to_t(10), 2);
148 trcd = max(ns_to_t(24), 2);
149 trc = ns_to_t(70);
150 txp = max(ns_to_t(8), 2);
151 twtr = max(ns_to_t(8), 2);
152 trtp = max(ns_to_t(8), 2);
153 trp = max(ns_to_t(27), 2);
154 tras = ns_to_t(42);
155 trefi = ns_to_t(3900) / 32;
156 trfc = ns_to_t(210);
157 tmrw = 5;
158 tmrd = 5;
159 tckesr = 5;
160 tcwl = 3; /* CWL 8 */
161 t_rdata_en = 5;
162 tdinit0 = (200 * CONFIG_DRAM_CLK) + 1; /* 200us */
163 tdinit1 = (100 * CONFIG_DRAM_CLK) / 1000 + 1; /* 100ns */
164 tdinit2 = (11 * CONFIG_DRAM_CLK) + 1; /* 200us */
165 tdinit3 = (1 * CONFIG_DRAM_CLK) + 1; /* 1us */
166 twtp = tcwl + 4 + twr + 1; /* CWL + BL/2 + tWR */
167 twr2rd = tcwl + 4 + 1 + twtr; /* WL + BL / 2 + tWTR */
168 trd2wr = tcl + 4 + 5 - tcwl + 1; /* RL + BL / 2 + 2 - WL */
169 }
vishnupatekare302fe62015-11-29 01:07:25 +0800170 /* Set dram timing */
171 reg_val = (twtp << 24) | (tfaw << 16) | (trasmax << 8) | (tras << 0);
172 writel(reg_val, &mctl_ctl->dramtmg0);
173 reg_val = (txp << 16) | (trtp << 8) | (trc << 0);
174 writel(reg_val, &mctl_ctl->dramtmg1);
175 reg_val = (tcwl << 24) | (tcl << 16) | (trd2wr << 8) | (twr2rd << 0);
176 writel(reg_val, &mctl_ctl->dramtmg2);
177 reg_val = (tmrw << 16) | (tmrd << 12) | (tmod << 0);
178 writel(reg_val, &mctl_ctl->dramtmg3);
179 reg_val = (trcd << 24) | (tccd << 16) | (trrd << 8) | (trp << 0);
180 writel(reg_val, &mctl_ctl->dramtmg4);
181 reg_val = (tcksrx << 24) | (tcksre << 16) | (tckesr << 8) | (tcke << 0);
182 writel(reg_val, &mctl_ctl->dramtmg5);
183 /* Set two rank timing and exit self-refresh timing */
184 reg_val = readl(&mctl_ctl->dramtmg8);
185 reg_val &= ~(0xff << 8);
186 reg_val &= ~(0xff << 0);
187 reg_val |= (0x33 << 8);
188 reg_val |= (0x8 << 0);
189 writel(reg_val, &mctl_ctl->dramtmg8);
190 /* Set phy interface time */
191 reg_val = (0x2 << 24) | (t_rdata_en << 16) | (0x1 << 8)
192 | (wr_latency << 0);
193 /* PHY interface write latency and read latency configure */
194 writel(reg_val, &mctl_ctl->pitmg0);
195 /* Set phy time PTR0-2 use default */
196 writel(((tdinit0 << 0) | (tdinit1 << 20)), &mctl_ctl->ptr3);
197 writel(((tdinit2 << 0) | (tdinit3 << 20)), &mctl_ctl->ptr4);
198 /* Set refresh timing */
199 reg_val = (trefi << 16) | (trfc << 0);
200 writel(reg_val, &mctl_ctl->rfshtmg);
201}
202
203static void mctl_set_pir(u32 val)
204{
205 struct sunxi_mctl_ctl_reg * const mctl_ctl =
206 (struct sunxi_mctl_ctl_reg *)SUNXI_DRAM_CTL0_BASE;
207
208 writel(val, &mctl_ctl->pir);
209 mctl_await_completion(&mctl_ctl->pgsr0, 0x1, 0x1);
210}
211
212static void mctl_data_train_cfg(struct dram_para *para)
213{
214 struct sunxi_mctl_ctl_reg * const mctl_ctl =
215 (struct sunxi_mctl_ctl_reg *)SUNXI_DRAM_CTL0_BASE;
216
217 if (para->rank == 2)
218 clrsetbits_le32(&mctl_ctl->dtcr, 0x3 << 24, 0x3 << 24);
219 else
220 clrsetbits_le32(&mctl_ctl->dtcr, 0x3 << 24, 0x1 << 24);
221}
222
223static int mctl_train_dram(struct dram_para *para)
224{
225 struct sunxi_mctl_ctl_reg * const mctl_ctl =
226 (struct sunxi_mctl_ctl_reg *)SUNXI_DRAM_CTL0_BASE;
227
228 mctl_data_train_cfg(para);
229 mctl_set_pir(0x5f3);
230
231 return ((readl(&mctl_ctl->pgsr0) >> 20) & 0xff) ? -EIO : 0;
232}
233
234static void set_master_priority(void)
235{
236 writel(0x00a0000d, MCTL_MASTER_CFG0(0));
237 writel(0x00500064, MCTL_MASTER_CFG1(0));
238 writel(0x07000009, MCTL_MASTER_CFG0(1));
239 writel(0x00000600, MCTL_MASTER_CFG1(1));
240 writel(0x01000009, MCTL_MASTER_CFG0(3));
241 writel(0x00000064, MCTL_MASTER_CFG1(3));
242 writel(0x08000009, MCTL_MASTER_CFG0(4));
243 writel(0x00000640, MCTL_MASTER_CFG1(4));
244 writel(0x20000308, MCTL_MASTER_CFG0(8));
245 writel(0x00001000, MCTL_MASTER_CFG1(8));
246 writel(0x02800009, MCTL_MASTER_CFG0(9));
247 writel(0x00000100, MCTL_MASTER_CFG1(9));
248 writel(0x01800009, MCTL_MASTER_CFG0(5));
249 writel(0x00000100, MCTL_MASTER_CFG1(5));
250 writel(0x01800009, MCTL_MASTER_CFG0(7));
251 writel(0x00000100, MCTL_MASTER_CFG1(7));
252 writel(0x00640009, MCTL_MASTER_CFG0(6));
253 writel(0x00000032, MCTL_MASTER_CFG1(6));
254 writel(0x0100000d, MCTL_MASTER_CFG0(2));
255 writel(0x00500080, MCTL_MASTER_CFG1(2));
256}
257
258static int mctl_channel_init(struct dram_para *para)
259{
260 struct sunxi_mctl_ctl_reg * const mctl_ctl =
261 (struct sunxi_mctl_ctl_reg *)SUNXI_DRAM_CTL0_BASE;
262 struct sunxi_mctl_com_reg * const mctl_com =
263 (struct sunxi_mctl_com_reg *)SUNXI_DRAM_COM_BASE;
264 u32 low_data_lines_status; /* Training status of datalines 0 - 7 */
265 u32 high_data_lines_status; /* Training status of datalines 8 - 15 */
266 u32 i, rval;
267
268 auto_set_timing_para(para);
269
270 /* Set dram master access priority */
271 writel(0x000101a0, &mctl_com->bwcr);
272 /* set cpu high priority */
273 writel(0x1, &mctl_com->mapr);
274 set_master_priority();
275 udelay(250);
276
277 /* Disable dram VTC */
278 clrbits_le32(&mctl_ctl->pgcr0, 0x3f << 0 | 0x1 << 30);
279 clrsetbits_le32(&mctl_ctl->pgcr1, 0x1 << 24, 0x1 << 26);
280
281 writel(0x94be6fa3, MCTL_PROTECT);
282 udelay(100);
vishnupatekarca89aa22016-03-24 01:54:33 +0800283 clrsetbits_le32(MX_UPD2, 0xfff << 16, 0x50 << 16);
vishnupatekare302fe62015-11-29 01:07:25 +0800284 writel(0x0, MCTL_PROTECT);
285 udelay(100);
286
vishnupatekare302fe62015-11-29 01:07:25 +0800287 /* Set ODT */
288 if (IS_ENABLED(CONFIG_DRAM_ODT_EN))
289 rval = 0x0;
290 else
291 rval = 0x2;
292
293 for (i = 0 ; i < 11 ; i++) {
294 clrsetbits_le32(DATX0IOCR(i), (0x3 << 24) | (0x3 << 16),
295 rval << 24);
296 clrsetbits_le32(DATX1IOCR(i), (0x3 << 24) | (0x3 << 16),
297 rval << 24);
298 clrsetbits_le32(DATX2IOCR(i), (0x3 << 24) | (0x3 << 16),
299 rval << 24);
300 clrsetbits_le32(DATX3IOCR(i), (0x3 << 24) | (0x3 << 16),
301 rval << 24);
302 }
303
304 for (i = 0; i < 31; i++)
305 clrsetbits_le32(CAIOCR(i), 0x3 << 26 | 0x3 << 16, 0x2 << 26);
306
307 /* set PLL configuration */
308 if (CONFIG_DRAM_CLK >= 480)
309 setbits_le32(&mctl_ctl->pllgcr, 0x1 << 19);
310 else
311 setbits_le32(&mctl_ctl->pllgcr, 0x3 << 19);
312
313 /* Auto detect dram config, set 2 rank and 16bit bus-width */
314 para->cs1 = 0;
315 para->rank = 2;
316 para->bus_width = 16;
317 mctl_set_cr(para);
318
319 /* Open DQS gating */
320 clrbits_le32(&mctl_ctl->pgcr2, (0x3 << 6));
321 clrbits_le32(&mctl_ctl->dqsgmr, (0x1 << 8) | (0x7));
322
Vishnu Patekarad8baac2016-01-12 01:20:59 +0800323 if (para->dram_type == DRAM_TYPE_LPDDR3)
324 clrsetbits_le32(&mctl_ctl->dxccr, (0x1 << 27) | (0x3<<6) ,
325 0x1 << 31);
vishnupatekare302fe62015-11-29 01:07:25 +0800326 if (readl(&mctl_com->cr) & 0x1)
327 writel(0x00000303, &mctl_ctl->odtmap);
328 else
329 writel(0x00000201, &mctl_ctl->odtmap);
330
331 mctl_data_train_cfg(para);
332 /* ZQ calibration */
333 clrsetbits_le32(ZQnPR(0), 0x000000ff, CONFIG_DRAM_ZQ & 0xff);
334 clrsetbits_le32(ZQnPR(1), 0x000000ff, (CONFIG_DRAM_ZQ >> 8) & 0xff);
335 /* CA calibration */
Vishnu Patekarad8baac2016-01-12 01:20:59 +0800336
337 if (para->dram_type == DRAM_TYPE_DDR3)
338 mctl_set_pir(0x0201f3 | 0x1<<10);
339 else
340 mctl_set_pir(0x020173 | 0x1<<10);
vishnupatekare302fe62015-11-29 01:07:25 +0800341
342 /* DQS gate training */
343 if (mctl_train_dram(para) != 0) {
344 low_data_lines_status = (readl(DXnGSR0(0)) >> 24) & 0x03;
345 high_data_lines_status = (readl(DXnGSR0(1)) >> 24) & 0x03;
346
347 if (low_data_lines_status == 0x3)
348 return -EIO;
349
350 /* DRAM has only one rank */
351 para->rank = 1;
352 mctl_set_cr(para);
353
354 if (low_data_lines_status == high_data_lines_status)
355 goto done; /* 16 bit bus, 1 rank */
356
357 if (!(low_data_lines_status & high_data_lines_status)) {
358 /* Retry 16 bit bus-width with CS1 set */
359 para->cs1 = 1;
360 mctl_set_cr(para);
361 if (mctl_train_dram(para) == 0)
362 goto done;
363 }
364
365 /* Try 8 bit bus-width */
366 writel(0x0, DXnGCR0(1)); /* Disable high DQ */
367 para->cs1 = 0;
368 para->bus_width = 8;
369 mctl_set_cr(para);
370 if (mctl_train_dram(para) != 0)
371 return -EIO;
372 }
373done:
374 /* Check the dramc status */
375 mctl_await_completion(&mctl_ctl->statr, 0x1, 0x1);
376
377 /* Close DQS gating */
378 setbits_le32(&mctl_ctl->pgcr2, 0x3 << 6);
379
380 /* set PGCR3,CKE polarity */
381 writel(0x00aa0060, &mctl_ctl->pgcr3);
382 /* Enable master access */
383 writel(0xffffffff, &mctl_com->maer);
384
385 return 0;
386}
387
388static void mctl_sys_init(struct dram_para *para)
389{
390 struct sunxi_ccm_reg * const ccm =
391 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
392 struct sunxi_mctl_ctl_reg * const mctl_ctl =
393 (struct sunxi_mctl_ctl_reg *)SUNXI_DRAM_CTL0_BASE;
394
395 clrbits_le32(&ccm->mbus_clk_cfg, MBUS_CLK_GATE);
396 clrbits_le32(&ccm->mbus_reset, CCM_MBUS_RESET_RESET);
397 clrbits_le32(&ccm->ahb_gate0, 1 << AHB_GATE_OFFSET_MCTL);
398 clrbits_le32(&ccm->ahb_reset0_cfg, 1 << AHB_RESET_OFFSET_MCTL);
399 clrbits_le32(&ccm->pll5_cfg, CCM_PLL5_CTRL_EN);
Vishnu Patekarad8baac2016-01-12 01:20:59 +0800400 udelay(1000);
vishnupatekare302fe62015-11-29 01:07:25 +0800401 clrbits_le32(&ccm->dram_clk_cfg, 0x01<<31);
402
403 clock_set_pll5(CONFIG_DRAM_CLK * 1000000 * DRAM_CLK_MUL);
404
405 clrsetbits_le32(&ccm->dram_clk_cfg, CCM_DRAMCLK_CFG_DIV_MASK,
406 CCM_DRAMCLK_CFG_DIV(DRAM_CLK_DIV) |
407 CCM_DRAMCLK_CFG_RST | CCM_DRAMCLK_CFG_UPD);
408 mctl_await_completion(&ccm->dram_clk_cfg, CCM_DRAMCLK_CFG_UPD, 0);
409
vishnupatekare302fe62015-11-29 01:07:25 +0800410 setbits_le32(&ccm->ahb_reset0_cfg, 1 << AHB_RESET_OFFSET_MCTL);
411 setbits_le32(&ccm->ahb_gate0, 1 << AHB_GATE_OFFSET_MCTL);
412 setbits_le32(&ccm->mbus_reset, CCM_MBUS_RESET_RESET);
413 setbits_le32(&ccm->mbus_clk_cfg, MBUS_CLK_GATE);
414
Vishnu Patekarad8baac2016-01-12 01:20:59 +0800415 para->rank = 2;
416 para->bus_width = 16;
417 mctl_set_cr(para);
418
vishnupatekare302fe62015-11-29 01:07:25 +0800419 /* Set dram master access priority */
420 writel(0x0000e00f, &mctl_ctl->clken); /* normal */
421
422 udelay(250);
423}
424
425unsigned long sunxi_dram_init(void)
426{
427 struct sunxi_mctl_com_reg * const mctl_com =
428 (struct sunxi_mctl_com_reg *)SUNXI_DRAM_COM_BASE;
429 struct sunxi_mctl_ctl_reg * const mctl_ctl =
430 (struct sunxi_mctl_ctl_reg *)SUNXI_DRAM_CTL0_BASE;
431
432 struct dram_para para = {
433 .cs1 = 0,
434 .bank = 1,
435 .rank = 1,
436 .rows = 15,
437 .bus_width = 16,
438 .page_size = 2048,
439 };
440
Vishnu Patekarc49936f2016-01-12 01:20:58 +0800441#if defined(CONFIG_MACH_SUN8I_A83T)
442#if (CONFIG_DRAM_TYPE == 3) || (CONFIG_DRAM_TYPE == 7)
443 para.dram_type = CONFIG_DRAM_TYPE;
444#else
445#error Unsupported DRAM type, Please set DRAM type (3:DDR3, 7:LPDDR3)
446#endif
447#endif
vishnupatekare302fe62015-11-29 01:07:25 +0800448 setbits_le32(SUNXI_PRCM_BASE + 0x1e0, 0x1 << 8);
449
450 writel(0, (SUNXI_PRCM_BASE + 0x1e8));
451 udelay(10);
452
453 mctl_sys_init(&para);
454
455 if (mctl_channel_init(&para) != 0)
456 return 0;
457
458 auto_detect_dram_size(&para);
459
460 /* Enable master software clk */
461 writel(readl(&mctl_com->swonr) | 0x3ffff, &mctl_com->swonr);
462
463 /* Set DRAM ODT MAP */
464 if (para.rank == 2)
465 writel(0x00000303, &mctl_ctl->odtmap);
466 else
467 writel(0x00000201, &mctl_ctl->odtmap);
468
469 return para.page_size * (para.bus_width / 8) *
470 (1 << (para.bank + para.rank + para.rows));
471}