1 /* SPDX-License-Identifier: (GPL-2.0+ OR MIT) */
3 * Copyright (c) 2018 Microsemi Corporation
6 #ifndef __ASM_MACH_DDR_H
7 #define __ASM_MACH_DDR_H
9 #include <asm/cacheops.h>
11 #include <asm/reboot.h>
12 #include <mach/common.h>
14 #define MIPS_VCOREIII_MEMORY_DDR3
15 #define MIPS_VCOREIII_DDR_SIZE CONFIG_SYS_SDRAM_SIZE
17 #if defined(CONFIG_DDRTYPE_H5TQ1G63BFA) /* Serval1 Refboard */
19 /* Hynix H5TQ1G63BFA (1Gbit DDR3, x16) @ 3.20ns */
20 #define VC3_MPAR_bank_addr_cnt 3
21 #define VC3_MPAR_row_addr_cnt 13
22 #define VC3_MPAR_col_addr_cnt 10
23 #define VC3_MPAR_tREFI 2437
24 #define VC3_MPAR_tRAS_min 12
26 #define VC3_MPAR_tWTR 4
27 #define VC3_MPAR_tRC 16
28 #define VC3_MPR_tFAW 16
29 #define VC3_MPAR_tRP 5
30 #define VC3_MPAR_tRRD 4
31 #define VC3_MPAR_tRCD 5
32 #define VC3_MPAR_tMRD 4
33 #define VC3_MPAR_tRFC 35
34 #define VC3_MPAR_CWL 5
35 #define VC3_MPAR_tXPR 38
36 #define VC3_MPAR_tMOD 12
37 #define VC3_MPAR_tDLLK 512
38 #define VC3_MPAR_tWR 5
40 #elif defined(CONFIG_DDRTYPE_MT41J128M16HA) /* Validation board */
42 /* Micron MT41J128M16HA-15E:D (2Gbit DDR3, x16) @ 3.20ns */
43 #define VC3_MPAR_bank_addr_cnt 3
44 #define VC3_MPAR_row_addr_cnt 14
45 #define VC3_MPAR_col_addr_cnt 10
46 #define VC3_MPAR_tREFI 2437
47 #define VC3_MPAR_tRAS_min 12
49 #define VC3_MPAR_tWTR 4
50 #define VC3_MPAR_tRC 16
51 #define VC3_MPAR_tFAW 16
52 #define VC3_MPAR_tRP 5
53 #define VC3_MPAR_tRRD 4
54 #define VC3_MPAR_tRCD 5
55 #define VC3_MPAR_tMRD 4
56 #define VC3_MPAR_tRFC 50
57 #define VC3_MPAR_CWL 5
58 #define VC3_MPAR_tXPR 54
59 #define VC3_MPAR_tMOD 12
60 #define VC3_MPAR_tDLLK 512
61 #define VC3_MPAR_tWR 5
63 #elif defined(CONFIG_DDRTYPE_MT41K256M16) /* JR2 Validation board */
65 /* Micron MT41K256M16 (4Gbit, DDR3L-800, 256Mbitx16) @ 3.20ns */
66 #define VC3_MPAR_bank_addr_cnt 3
67 #define VC3_MPAR_row_addr_cnt 15
68 #define VC3_MPAR_col_addr_cnt 10
69 #define VC3_MPAR_tREFI 2437
70 #define VC3_MPAR_tRAS_min 12
72 #define VC3_MPAR_tWTR 4
73 #define VC3_MPAR_tRC 16
74 #define VC3_MPAR_tFAW 16
75 #define VC3_MPAR_tRP 5
76 #define VC3_MPAR_tRRD 4
77 #define VC3_MPAR_tRCD 5
78 #define VC3_MPAR_tMRD 4
79 #define VC3_MPAR_tRFC 82
80 #define VC3_MPAR_CWL 5
81 #define VC3_MPAR_tXPR 85
82 #define VC3_MPAR_tMOD 12
83 #define VC3_MPAR_tDLLK 512
84 #define VC3_MPAR_tWR 5
86 #elif defined(CONFIG_DDRTYPE_H5TQ4G63MFR) /* JR2 Reference board */
88 /* Hynix H5TQ4G63MFR-PBC (4Gbit, DDR3-800, 256Mbitx16) - 2kb pages @ 3.20ns */
89 #define VC3_MPAR_bank_addr_cnt 3
90 #define VC3_MPAR_row_addr_cnt 15
91 #define VC3_MPAR_col_addr_cnt 10
92 #define VC3_MPAR_tREFI 2437
93 #define VC3_MPAR_tRAS_min 12
95 #define VC3_MPAR_tWTR 4
96 #define VC3_MPAR_tRC 17
97 #define VC3_MPAR_tFAW 16
98 #define VC3_MPAR_tRP 5
99 #define VC3_MPAR_tRRD 4
100 #define VC3_MPAR_tRCD 5
101 #define VC3_MPAR_tMRD 4
102 #define VC3_MPAR_tRFC 82
103 #define VC3_MPAR_CWL 5
104 #define VC3_MPAR_tXPR 85
105 #define VC3_MPAR_tMOD 12
106 #define VC3_MPAR_tDLLK 512
107 #define VC3_MPAR_tWR 5
109 #elif defined(CONFIG_DDRTYPE_MT41K128M16JT)
111 /* Micron Micron MT41K128M16JT-125 (2Gbit DDR3L, 128Mbitx16) @ 3.20ns */
112 #define VC3_MPAR_bank_addr_cnt 3
113 #define VC3_MPAR_row_addr_cnt 14
114 #define VC3_MPAR_col_addr_cnt 10
115 #define VC3_MPAR_tREFI 2437
116 #define VC3_MPAR_tRAS_min 12
117 #define VC3_MPAR_CL 6
118 #define VC3_MPAR_tWTR 4
119 #define VC3_MPAR_tRC 16
120 #define VC3_MPAR_tFAW 16
121 #define VC3_MPAR_tRP 5
122 #define VC3_MPAR_tRRD 4
123 #define VC3_MPAR_tRCD 5
124 #define VC3_MPAR_tMRD 4
125 #define VC3_MPAR_tRFC 82
126 #define VC3_MPAR_CWL 5
127 #define VC3_MPAR_tXPR 85
128 #define VC3_MPAR_tMOD 12
129 #define VC3_MPAR_tDLLK 512
130 #define VC3_MPAR_tWR 5
132 #elif defined(CONFIG_DDRTYPE_MT47H128M8HQ) /* Luton10/26 Refboards */
134 /* Micron 1Gb MT47H128M8-3 16Meg x 8 x 8 banks, DDR-533@CL4 @ 4.80ns */
135 #define VC3_MPAR_bank_addr_cnt 3
136 #define VC3_MPAR_row_addr_cnt 14
137 #define VC3_MPAR_col_addr_cnt 10
138 #define VC3_MPAR_tREFI 1625
139 #define VC3_MPAR_tRAS_min 9
140 #define VC3_MPAR_CL 4
141 #define VC3_MPAR_tWTR 2
142 #define VC3_MPAR_tRC 12
143 #define VC3_MPAR_tFAW 8
144 #define VC3_MPAR_tRP 4
145 #define VC3_MPAR_tRRD 2
146 #define VC3_MPAR_tRCD 4
148 #define VC3_MPAR_tRPA 4
149 #define VC3_MPAR_tRP 4
151 #define VC3_MPAR_tMRD 2
152 #define VC3_MPAR_tRFC 27
154 #define VC3_MPAR__400_ns_dly 84
156 #define VC3_MPAR_tWR 4
157 #undef MIPS_VCOREIII_MEMORY_DDR3
160 #error Unknown DDR system configuration - please add!
164 #if defined(CONFIG_SOC_OCELOT) || defined(CONFIG_SOC_JR2)
165 #define MIPS_VCOREIII_MEMORY_16BIT 1
168 #define MIPS_VCOREIII_MEMORY_SSTL_ODT 7
169 #define MIPS_VCOREIII_MEMORY_SSTL_DRIVE 7
170 #define VCOREIII_DDR_DQS_MODE_CALIBRATE
172 #ifdef MIPS_VCOREIII_MEMORY_16BIT
173 #define VC3_MPAR_16BIT 1
175 #define VC3_MPAR_16BIT 0
178 #ifdef MIPS_VCOREIII_MEMORY_DDR3
179 #define VC3_MPAR_DDR3_MODE 1 /* DDR3 */
180 #define VC3_MPAR_BURST_LENGTH 8 /* Always 8 (1) for DDR3 */
181 #ifdef MIPS_VCOREIII_MEMORY_16BIT
182 #define VC3_MPAR_BURST_SIZE 1 /* Always 1 for DDR3/16bit */
184 #define VC3_MPAR_BURST_SIZE 0
187 #define VC3_MPAR_DDR3_MODE 0 /* DDR2 */
188 #ifdef MIPS_VCOREIII_MEMORY_16BIT
189 #define VC3_MPAR_BURST_LENGTH 4 /* in DDR2 16-bit mode, use burstlen 4 */
191 #define VC3_MPAR_BURST_LENGTH 8 /* For 8-bit IF we must run burst-8 */
193 #define VC3_MPAR_BURST_SIZE 0 /* Always 0 for DDR2 */
196 #define VC3_MPAR_RL VC3_MPAR_CL
197 #if !defined(MIPS_VCOREIII_MEMORY_DDR3)
198 #define VC3_MPAR_WL (VC3_MPAR_RL - 1)
199 #define VC3_MPAR_MD VC3_MPAR_tMRD
200 #define VC3_MPAR_ID VC3_MPAR__400_ns_dly
201 #define VC3_MPAR_SD VC3_MPAR_tXSRD
202 #define VC3_MPAR_OW (VC3_MPAR_WL - 2)
203 #define VC3_MPAR_OR (VC3_MPAR_WL - 3)
204 #define VC3_MPAR_RP (VC3_MPAR_bank_addr_cnt < 3 ? VC3_MPAR_tRP : VC3_MPAR_tRPA)
205 #define VC3_MPAR_FAW (VC3_MPAR_bank_addr_cnt < 3 ? 1 : VC3_MPAR_tFAW)
206 #define VC3_MPAR_BL (VC3_MPAR_BURST_LENGTH == 4 ? 2 : 4)
207 #define MSCC_MEMPARM_MR0 \
208 (VC3_MPAR_BURST_LENGTH == 8 ? 3 : 2) | (VC3_MPAR_CL << 4) | \
209 ((VC3_MPAR_tWR - 1) << 9)
210 /* DLL-on, Full-OD, AL=0, RTT=off, nDQS-on, RDQS-off, out-en */
211 #define MSCC_MEMPARM_MR1 0x382
212 #define MSCC_MEMPARM_MR2 0
213 #define MSCC_MEMPARM_MR3 0
215 #define VC3_MPAR_WL VC3_MPAR_CWL
216 #define VC3_MPAR_MD VC3_MPAR_tMOD
217 #define VC3_MPAR_ID VC3_MPAR_tXPR
218 #define VC3_MPAR_SD VC3_MPAR_tDLLK
219 #define VC3_MPAR_OW 2
220 #define VC3_MPAR_OR 2
221 #define VC3_MPAR_RP VC3_MPAR_tRP
222 #define VC3_MPAR_FAW VC3_MPAR_tFAW
223 #define VC3_MPAR_BL 4
224 #define MSCC_MEMPARM_MR0 ((VC3_MPAR_RL - 4) << 4) | ((VC3_MPAR_tWR - 4) << 9)
225 /* ODT_RTT: “0x0040” for 120ohm, and “0x0004” for 60ohm. */
226 #define MSCC_MEMPARM_MR1 0x0040
227 #define MSCC_MEMPARM_MR2 ((VC3_MPAR_WL - 5) << 3)
228 #define MSCC_MEMPARM_MR3 0
229 #endif /* MIPS_VCOREIII_MEMORY_DDR3 */
231 #define MSCC_MEMPARM_MEMCFG \
232 ((MIPS_VCOREIII_DDR_SIZE > SZ_512M) ? \
233 ICPU_MEMCTRL_CFG_DDR_512MBYTE_PLUS : 0) | \
234 (VC3_MPAR_16BIT ? ICPU_MEMCTRL_CFG_DDR_WIDTH : 0) | \
235 (VC3_MPAR_DDR3_MODE ? ICPU_MEMCTRL_CFG_DDR_MODE : 0) | \
236 (VC3_MPAR_BURST_SIZE ? ICPU_MEMCTRL_CFG_BURST_SIZE : 0) | \
237 (VC3_MPAR_BURST_LENGTH == 8 ? ICPU_MEMCTRL_CFG_BURST_LEN : 0) | \
238 (VC3_MPAR_bank_addr_cnt == 3 ? ICPU_MEMCTRL_CFG_BANK_CNT : 0) | \
239 ICPU_MEMCTRL_CFG_MSB_ROW_ADDR(VC3_MPAR_row_addr_cnt - 1) | \
240 ICPU_MEMCTRL_CFG_MSB_COL_ADDR(VC3_MPAR_col_addr_cnt - 1)
242 #if defined(CONFIG_SOC_OCELOT) || defined(CONFIG_SOC_JR2)
243 #define MSCC_MEMPARM_PERIOD \
244 ICPU_MEMCTRL_REF_PERIOD_MAX_PEND_REF(8) | \
245 ICPU_MEMCTRL_REF_PERIOD_REF_PERIOD(VC3_MPAR_tREFI)
247 #define MSCC_MEMPARM_TIMING0 \
248 ICPU_MEMCTRL_TIMING0_RD_TO_WR_DLY(VC3_MPAR_RL + VC3_MPAR_BL + 1 - \
250 ICPU_MEMCTRL_TIMING0_WR_CS_CHANGE_DLY(VC3_MPAR_BL - 1) | \
251 ICPU_MEMCTRL_TIMING0_RD_CS_CHANGE_DLY(VC3_MPAR_BL) | \
252 ICPU_MEMCTRL_TIMING0_RAS_TO_PRECH_DLY(VC3_MPAR_tRAS_min - 1) | \
253 ICPU_MEMCTRL_TIMING0_WR_TO_PRECH_DLY(VC3_MPAR_WL + \
255 VC3_MPAR_tWR - 1) | \
256 ICPU_MEMCTRL_TIMING0_RD_TO_PRECH_DLY(VC3_MPAR_BL - 1) | \
257 ICPU_MEMCTRL_TIMING0_WR_DATA_XFR_DLY(VC3_MPAR_WL - 1) | \
258 ICPU_MEMCTRL_TIMING0_RD_DATA_XFR_DLY(VC3_MPAR_RL - 3)
260 #define MSCC_MEMPARM_TIMING1 \
261 ICPU_MEMCTRL_TIMING1_RAS_TO_RAS_SAME_BANK_DLY(VC3_MPAR_tRC - 1) | \
262 ICPU_MEMCTRL_TIMING1_BANK8_FAW_DLY(VC3_MPAR_FAW - 1) | \
263 ICPU_MEMCTRL_TIMING1_PRECH_TO_RAS_DLY(VC3_MPAR_RP - 1) | \
264 ICPU_MEMCTRL_TIMING1_RAS_TO_RAS_DLY(VC3_MPAR_tRRD - 1) | \
265 ICPU_MEMCTRL_TIMING1_RAS_TO_CAS_DLY(VC3_MPAR_tRCD - 1) | \
266 ICPU_MEMCTRL_TIMING1_WR_TO_RD_DLY(VC3_MPAR_WL + \
270 #define MSCC_MEMPARM_TIMING2 \
271 ICPU_MEMCTRL_TIMING2_PRECH_ALL_DLY(VC3_MPAR_RP - 1) | \
272 ICPU_MEMCTRL_TIMING2_MDSET_DLY(VC3_MPAR_MD - 1) | \
273 ICPU_MEMCTRL_TIMING2_REF_DLY(VC3_MPAR_tRFC - 1) | \
274 ICPU_MEMCTRL_TIMING2_INIT_DLY(VC3_MPAR_ID - 1)
276 #define MSCC_MEMPARM_TIMING3 \
277 ICPU_MEMCTRL_TIMING3_WR_TO_RD_CS_CHANGE_DLY(VC3_MPAR_WL + \
278 VC3_MPAR_tWTR - 1) |\
279 ICPU_MEMCTRL_TIMING3_ODT_RD_DLY(VC3_MPAR_OR - 1) | \
280 ICPU_MEMCTRL_TIMING3_ODT_WR_DLY(VC3_MPAR_OW - 1) | \
281 ICPU_MEMCTRL_TIMING3_LOCAL_ODT_RD_DLY(VC3_MPAR_RL - 3)
284 #define MSCC_MEMPARM_PERIOD \
285 ICPU_MEMCTRL_REF_PERIOD_MAX_PEND_REF(1) | \
286 ICPU_MEMCTRL_REF_PERIOD_REF_PERIOD(VC3_MPAR_tREFI)
288 #define MSCC_MEMPARM_TIMING0 \
289 ICPU_MEMCTRL_TIMING0_RAS_TO_PRECH_DLY(VC3_MPAR_tRAS_min - 1) | \
290 ICPU_MEMCTRL_TIMING0_WR_TO_PRECH_DLY(VC3_MPAR_CL + \
291 (VC3_MPAR_BURST_LENGTH == 8 ? 2 : 0) + \
293 ICPU_MEMCTRL_TIMING0_RD_TO_PRECH_DLY(VC3_MPAR_BURST_LENGTH == 8 ? 3 : 1) | \
294 ICPU_MEMCTRL_TIMING0_WR_DATA_XFR_DLY(VC3_MPAR_CL - 3) | \
295 ICPU_MEMCTRL_TIMING0_RD_DATA_XFR_DLY(VC3_MPAR_CL - 3)
297 #define MSCC_MEMPARM_TIMING1 \
298 ICPU_MEMCTRL_TIMING1_RAS_TO_RAS_SAME_BANK_DLY(VC3_MPAR_tRC - 1) | \
299 ICPU_MEMCTRL_TIMING1_BANK8_FAW_DLY(VC3_MPAR_tFAW - 1) | \
300 ICPU_MEMCTRL_TIMING1_PRECH_TO_RAS_DLY(VC3_MPAR_tRP - 1) | \
301 ICPU_MEMCTRL_TIMING1_RAS_TO_RAS_DLY(VC3_MPAR_tRRD - 1) | \
302 ICPU_MEMCTRL_TIMING1_RAS_TO_CAS_DLY(VC3_MPAR_tRCD - 1) | \
303 ICPU_MEMCTRL_TIMING1_WR_TO_RD_DLY(VC3_MPAR_CL + \
304 (VC3_MPAR_BURST_LENGTH == 8 ? 2 : 0) + \
306 #define MSCC_MEMPARM_TIMING2 \
307 ICPU_MEMCTRL_TIMING2_PRECH_ALL_DLY(VC3_MPAR_tRPA - 1) | \
308 ICPU_MEMCTRL_TIMING2_MDSET_DLY(VC3_MPAR_tMRD - 1) | \
309 ICPU_MEMCTRL_TIMING2_REF_DLY(VC3_MPAR_tRFC - 1) | \
310 ICPU_MEMCTRL_TIMING2_FOUR_HUNDRED_NS_DLY(VC3_MPAR__400_ns_dly)
312 #define MSCC_MEMPARM_TIMING3 \
313 ICPU_MEMCTRL_TIMING3_WR_TO_RD_CS_CHANGE_DLY(VC3_MPAR_CL - 1) | \
314 ICPU_MEMCTRL_TIMING3_ODT_WR_DLY(VC3_MPAR_CL - 1) | \
315 ICPU_MEMCTRL_TIMING3_LOCAL_ODT_RD_DLY(VC3_MPAR_CL - 1)
326 * We actually have very few 'pause' possibilities apart from
327 * these assembly nops (at this very early stage).
329 #define PAUSE() asm volatile("nop; nop; nop; nop; nop; nop; nop; nop")
331 /* NB: Assumes inlining as no stack is available! */
332 static inline void set_dly(u32 bytelane, u32 dly)
334 register u32 r = readl(BASE_CFG + ICPU_MEMCTRL_DQS_DLY(bytelane));
336 r &= ~ICPU_MEMCTRL_DQS_DLY_DQS_DLY_M;
337 r |= ICPU_MEMCTRL_DQS_DLY_DQS_DLY(dly);
338 writel(r, BASE_CFG + ICPU_MEMCTRL_DQS_DLY(bytelane));
341 static inline bool incr_dly(u32 bytelane)
343 register u32 r = readl(BASE_CFG + ICPU_MEMCTRL_DQS_DLY(bytelane));
345 if (ICPU_MEMCTRL_DQS_DLY_DQS_DLY(r) < 31) {
346 writel(r + 1, BASE_CFG + ICPU_MEMCTRL_DQS_DLY(bytelane));
353 static inline bool adjust_dly(int adjust)
355 register u32 r = readl(BASE_CFG + ICPU_MEMCTRL_DQS_DLY(0));
357 if (ICPU_MEMCTRL_DQS_DLY_DQS_DLY(r) < 31) {
358 writel(r + adjust, BASE_CFG + ICPU_MEMCTRL_DQS_DLY(0));
365 /* NB: Assumes inlining as no stack is available! */
366 static inline void center_dly(u32 bytelane, u32 start)
368 register u32 r = readl(BASE_CFG + ICPU_MEMCTRL_DQS_DLY(bytelane)) - start;
370 writel(start + (r >> 1), BASE_CFG + ICPU_MEMCTRL_DQS_DLY(bytelane));
373 static inline void memphy_soft_reset(void)
375 setbits_le32(BASE_CFG + ICPU_MEMPHY_CFG, ICPU_MEMPHY_CFG_PHY_FIFO_RST);
377 clrbits_le32(BASE_CFG + ICPU_MEMPHY_CFG, ICPU_MEMPHY_CFG_PHY_FIFO_RST);
381 #if defined(CONFIG_SOC_OCELOT) || defined(CONFIG_SOC_JR2)
382 static u8 training_data[] = { 0xfe, 0x11, 0x33, 0x55, 0x77, 0x99, 0xbb, 0xdd };
384 static inline void sleep_100ns(u32 val)
386 /* Set the timer tick generator to 100 ns */
387 writel(VCOREIII_TIMER_DIVIDER - 1, BASE_CFG + ICPU_TIMER_TICK_DIV);
389 /* Set the timer value */
390 writel(val, BASE_CFG + ICPU_TIMER_VALUE(0));
392 /* Enable timer 0 for one-shot */
393 writel(ICPU_TIMER_CTRL_ONE_SHOT_ENA | ICPU_TIMER_CTRL_TIMER_ENA,
394 BASE_CFG + ICPU_TIMER_CTRL(0));
396 /* Wait for timer 0 to reach 0 */
397 while (readl(BASE_CFG + ICPU_TIMER_VALUE(0)) != 0)
401 #if defined(CONFIG_SOC_OCELOT)
402 static inline void hal_vcoreiii_ddr_reset_assert(void)
404 /* DDR has reset pin on GPIO 19 toggle Low-High to release */
405 setbits_le32(BASE_DEVCPU_GCB + PERF_GPIO_OE, BIT(19));
406 writel(BIT(19), BASE_DEVCPU_GCB + PERF_GPIO_OUT_CLR);
410 static inline void hal_vcoreiii_ddr_reset_release(void)
412 /* DDR has reset pin on GPIO 19 toggle Low-High to release */
413 setbits_le32(BASE_DEVCPU_GCB + PERF_GPIO_OE, BIT(19));
414 writel(BIT(19), BASE_DEVCPU_GCB + PERF_GPIO_OUT_SET);
419 * DDR memory sanity checking failed, tally and do hard reset
421 * NB: Assumes inlining as no stack is available!
423 static inline void hal_vcoreiii_ddr_failed(void)
427 writel(readl(BASE_CFG + ICPU_GPR(6)) + 1, BASE_CFG + ICPU_GPR(6));
429 clrbits_le32(BASE_DEVCPU_GCB + PERF_GPIO_OE, BIT(19));
431 /* We have to execute the reset function from cache. Indeed,
432 * the reboot workaround in _machine_restart() will change the
433 * SPI NOR into SW bitbang.
435 * This will render the CPU unable to execute directly from
436 * the NOR, which is why the reset instructions are prefetched
439 * When failing the DDR initialization we are executing from
442 * The last instruction in _machine_restart() will reset the
443 * MIPS CPU (and the cache), and the CPU will start executing
444 * from the reset vector.
446 reset = KSEG0ADDR(_machine_restart);
447 icache_lock((void *)reset, 128);
448 asm volatile ("jr %0"::"r" (reset));
450 panic("DDR init failed\n");
453 static inline void hal_vcoreiii_ddr_reset_assert(void)
455 /* Ensure the memory controller physical iface is forced reset */
456 writel(readl(BASE_CFG + ICPU_MEMPHY_CFG) |
457 ICPU_MEMPHY_CFG_PHY_RST, BASE_CFG + ICPU_MEMPHY_CFG);
459 /* Ensure the memory controller is forced reset */
460 writel(readl(BASE_CFG + ICPU_RESET) |
461 ICPU_RESET_MEM_RST_FORCE, BASE_CFG + ICPU_RESET);
464 static inline void hal_vcoreiii_ddr_failed(void)
466 writel(0, BASE_CFG + ICPU_RESET);
467 writel(PERF_SOFT_RST_SOFT_CHIP_RST, BASE_CFG + PERF_SOFT_RST);
469 panic("DDR init failed\n");
474 * DDR memory sanity checking done, possibly enable ECC.
476 * NB: Assumes inlining as no stack is available!
478 static inline void hal_vcoreiii_ddr_verified(void)
480 #ifdef MIPS_VCOREIII_MEMORY_ECC
481 /* Finally, enable ECC */
482 register u32 val = readl(BASE_CFG + ICPU_MEMCTRL_CFG);
484 val |= ICPU_MEMCTRL_CFG_DDR_ECC_ERR_ENA;
485 val &= ~ICPU_MEMCTRL_CFG_BURST_SIZE;
487 writel(val, BASE_CFG + ICPU_MEMCTRL_CFG);
490 /* Reset Status register - sticky bits */
491 writel(readl(BASE_CFG + ICPU_MEMCTRL_STAT), BASE_CFG + ICPU_MEMCTRL_STAT);
494 /* NB: Assumes inlining as no stack is available! */
495 static inline int look_for(u32 bytelane)
499 /* Reset FIFO in case any previous access failed */
500 for (i = 0; i < sizeof(training_data); i++) {
504 /* Reset sticky bits */
505 writel(readl(BASE_CFG + ICPU_MEMCTRL_STAT),
506 BASE_CFG + ICPU_MEMCTRL_STAT);
508 byte = __raw_readb((void __iomem *)MSCC_DDR_TO + bytelane +
512 * Prevent the compiler reordering the instruction so
513 * the read of RAM happens after the check of the
517 if (readl(BASE_CFG + ICPU_MEMCTRL_STAT) &
518 (ICPU_MEMCTRL_STAT_RDATA_MASKED |
519 ICPU_MEMCTRL_STAT_RDATA_DUMMY)) {
520 /* Noise on the line */
523 /* If mismatch, increment DQS - if possible */
524 if (byte != training_data[i]) {
526 if (!incr_dly(bytelane))
527 return DDR_TRAIN_ERROR;
528 return DDR_TRAIN_CONTINUE;
534 /* NB: Assumes inlining as no stack is available! */
535 static inline int look_past(u32 bytelane)
539 /* Reset FIFO in case any previous access failed */
540 for (i = 0; i < sizeof(training_data); i++) {
544 /* Ack sticky bits */
545 writel(readl(BASE_CFG + ICPU_MEMCTRL_STAT),
546 BASE_CFG + ICPU_MEMCTRL_STAT);
547 byte = __raw_readb((void __iomem *)MSCC_DDR_TO + bytelane +
550 * Prevent the compiler reordering the instruction so
551 * the read of RAM happens after the check of the
555 if (readl(BASE_CFG + ICPU_MEMCTRL_STAT) &
556 (ICPU_MEMCTRL_STAT_RDATA_MASKED |
557 ICPU_MEMCTRL_STAT_RDATA_DUMMY)) {
558 /* Noise on the line */
561 /* Bail out when we see first mismatch */
562 if (byte != training_data[i]) {
567 /* All data compares OK, increase DQS and retry */
568 if (!incr_dly(bytelane))
569 return DDR_TRAIN_ERROR;
571 return DDR_TRAIN_CONTINUE;
574 static inline int hal_vcoreiii_train_bytelane(u32 bytelane)
579 set_dly(bytelane, 0); /* Start training at DQS=0 */
580 while ((res = look_for(bytelane)) == DDR_TRAIN_CONTINUE)
582 if (res != DDR_TRAIN_OK)
585 dqs_s = readl(BASE_CFG + ICPU_MEMCTRL_DQS_DLY(bytelane));
586 while ((res = look_past(bytelane)) == DDR_TRAIN_CONTINUE)
588 if (res != DDR_TRAIN_OK)
590 /* Reset FIFO - for good measure */
592 /* Adjust to center [dqs_s;cur] */
593 center_dly(bytelane, dqs_s);
597 /* This algorithm is converted from the TCL training algorithm used
598 * during silicon simulation.
599 * NB: Assumes inlining as no stack is available!
601 static inline int hal_vcoreiii_init_dqs(void)
606 for (i = 0; i < MAX_DQS; i++) {
607 set_dly(0, i); /* Byte-lane 0 */
608 for (j = 0; j < MAX_DQS; j++) {
609 __maybe_unused register u32 byte;
611 set_dly(1, j); /* Byte-lane 1 */
612 /* Reset FIFO in case any previous access failed */
614 writel(readl(BASE_CFG + ICPU_MEMCTRL_STAT),
615 BASE_CFG + ICPU_MEMCTRL_STAT);
616 byte = __raw_readb((void __iomem *)MSCC_DDR_TO);
617 byte = __raw_readb((void __iomem *)(MSCC_DDR_TO + 1));
618 if (!(readl(BASE_CFG + ICPU_MEMCTRL_STAT) &
619 (ICPU_MEMCTRL_STAT_RDATA_MASKED |
620 ICPU_MEMCTRL_STAT_RDATA_DUMMY)))
627 static inline int dram_check(void)
631 for (i = 0; i < 8; i++) {
632 __raw_writel(~i, (void __iomem *)(MSCC_DDR_TO + (i * 4)));
633 if (__raw_readl((void __iomem *)(MSCC_DDR_TO + (i * 4))) != ~i)
640 static inline void sleep_100ns(u32 val)
644 static inline void hal_vcoreiii_ddr_reset_assert(void)
646 setbits_le32(BASE_CFG + ICPU_MEMPHY_CFG, ICPU_MEMPHY_CFG_PHY_RST);
647 setbits_le32(BASE_CFG + ICPU_RESET, ICPU_RESET_MEM_RST_FORCE);
650 static inline void hal_vcoreiii_ddr_reset_release(void)
654 static inline void hal_vcoreiii_ddr_failed(void)
656 register u32 memphy_cfg = readl(BASE_CFG + ICPU_MEMPHY_CFG);
658 /* Do a fifo reset and start over */
659 writel(memphy_cfg | ICPU_MEMPHY_CFG_PHY_FIFO_RST,
660 BASE_CFG + ICPU_MEMPHY_CFG);
661 writel(memphy_cfg & ~ICPU_MEMPHY_CFG_PHY_FIFO_RST,
662 BASE_CFG + ICPU_MEMPHY_CFG);
663 writel(memphy_cfg | ICPU_MEMPHY_CFG_PHY_FIFO_RST,
664 BASE_CFG + ICPU_MEMPHY_CFG);
667 static inline void hal_vcoreiii_ddr_verified(void)
671 static inline int look_for(u32 data)
673 register u32 byte = __raw_readb((void __iomem *)MSCC_DDR_TO);
677 return DDR_TRAIN_ERROR;
678 return DDR_TRAIN_CONTINUE;
684 /* This algorithm is converted from the TCL training algorithm used
685 * during silicon simulation.
686 * NB: Assumes inlining as no stack is available!
688 static inline int hal_vcoreiii_train_bytelane(u32 bytelane)
692 set_dly(bytelane, 0); /* Start training at DQS=0 */
693 while ((res = look_for(0xff)) == DDR_TRAIN_CONTINUE)
695 if (res != DDR_TRAIN_OK)
698 set_dly(bytelane, 0); /* Start training at DQS=0 */
699 while ((res = look_for(0x00)) == DDR_TRAIN_CONTINUE)
703 if (res != DDR_TRAIN_OK)
711 static inline int hal_vcoreiii_init_dqs(void)
716 static inline int dram_check(void)
720 for (i = 0; i < 8; i++) {
721 __raw_writel(~i, (void __iomem *)(MSCC_DDR_TO + (i * 4)));
723 if (__raw_readl((void __iomem *)(MSCC_DDR_TO + (i * 4))) != ~i)
732 * NB: Called *early* to init memory controller - assumes inlining as
733 * no stack is available!
735 static inline void hal_vcoreiii_init_memctl(void)
737 /* Ensure DDR is in reset */
738 hal_vcoreiii_ddr_reset_assert();
740 /* Wait maybe not needed, but ... */
743 /* Drop sys ctl memory controller forced reset */
744 clrbits_le32(BASE_CFG + ICPU_RESET, ICPU_RESET_MEM_RST_FORCE);
748 /* Drop Reset, enable SSTL */
749 writel(ICPU_MEMPHY_CFG_PHY_SSTL_ENA, BASE_CFG + ICPU_MEMPHY_CFG);
752 /* Start the automatic SSTL output and ODT drive-strength calibration */
753 writel(ICPU_MEMPHY_ZCAL_ZCAL_PROG_ODT(MIPS_VCOREIII_MEMORY_SSTL_ODT) |
755 ICPU_MEMPHY_ZCAL_ZCAL_PROG(MIPS_VCOREIII_MEMORY_SSTL_DRIVE) |
756 /* Start calibration process */
757 ICPU_MEMPHY_ZCAL_ZCAL_ENA, BASE_CFG + ICPU_MEMPHY_ZCAL);
759 /* Wait for ZCAL to clear */
760 while (readl(BASE_CFG + ICPU_MEMPHY_ZCAL) & ICPU_MEMPHY_ZCAL_ZCAL_ENA)
762 #if defined(CONFIG_SOC_OCELOT) || defined(CONFIG_SOC_JR2)
763 /* Check no ZCAL_ERR */
764 if (readl(BASE_CFG + ICPU_MEMPHY_ZCAL_STAT)
765 & ICPU_MEMPHY_ZCAL_STAT_ZCAL_ERR)
766 hal_vcoreiii_ddr_failed();
768 /* Drive CL, CK, ODT */
769 setbits_le32(BASE_CFG + ICPU_MEMPHY_CFG, ICPU_MEMPHY_CFG_PHY_ODT_OE |
770 ICPU_MEMPHY_CFG_PHY_CK_OE | ICPU_MEMPHY_CFG_PHY_CL_OE);
772 /* Initialize memory controller */
773 writel(MSCC_MEMPARM_MEMCFG, BASE_CFG + ICPU_MEMCTRL_CFG);
774 writel(MSCC_MEMPARM_PERIOD, BASE_CFG + ICPU_MEMCTRL_REF_PERIOD);
776 #if defined(CONFIG_SOC_OCELOT) || defined(CONFIG_SOC_JR2)
777 writel(MSCC_MEMPARM_TIMING0, BASE_CFG + ICPU_MEMCTRL_TIMING0);
779 clrbits_le32(BASE_CFG + ICPU_MEMCTRL_TIMING0, ((1 << 20) - 1));
780 setbits_le32(BASE_CFG + ICPU_MEMCTRL_TIMING0, MSCC_MEMPARM_TIMING0);
783 writel(MSCC_MEMPARM_TIMING1, BASE_CFG + ICPU_MEMCTRL_TIMING1);
784 writel(MSCC_MEMPARM_TIMING2, BASE_CFG + ICPU_MEMCTRL_TIMING2);
785 writel(MSCC_MEMPARM_TIMING3, BASE_CFG + ICPU_MEMCTRL_TIMING3);
786 writel(MSCC_MEMPARM_MR0, BASE_CFG + ICPU_MEMCTRL_MR0_VAL);
787 writel(MSCC_MEMPARM_MR1, BASE_CFG + ICPU_MEMCTRL_MR1_VAL);
788 writel(MSCC_MEMPARM_MR2, BASE_CFG + ICPU_MEMCTRL_MR2_VAL);
789 writel(MSCC_MEMPARM_MR3, BASE_CFG + ICPU_MEMCTRL_MR3_VAL);
791 #if defined(CONFIG_SOC_OCELOT)
792 /* Termination setup - enable ODT */
793 writel(ICPU_MEMCTRL_TERMRES_CTRL_LOCAL_ODT_RD_ENA |
794 /* Assert ODT0 for any write */
795 ICPU_MEMCTRL_TERMRES_CTRL_ODT_WR_ENA(3),
796 BASE_CFG + ICPU_MEMCTRL_TERMRES_CTRL);
798 /* Release Reset from DDR */
799 hal_vcoreiii_ddr_reset_release();
801 writel(readl(BASE_CFG + ICPU_GPR(7)) + 1, BASE_CFG + ICPU_GPR(7));
802 #elif defined(CONFIG_SOC_JR2)
803 writel(ICPU_MEMCTRL_TERMRES_CTRL_ODT_WR_ENA(3),
804 BASE_CFG + ICPU_MEMCTRL_TERMRES_CTRL);
806 /* Termination setup - disable ODT */
807 writel(0, BASE_CFG + ICPU_MEMCTRL_TERMRES_CTRL);
812 static inline void hal_vcoreiii_wait_memctl(void)
815 writel(ICPU_MEMCTRL_CTRL_INITIALIZE, BASE_CFG + ICPU_MEMCTRL_CTRL);
817 while (!(readl(BASE_CFG + ICPU_MEMCTRL_STAT)
818 & ICPU_MEMCTRL_STAT_INIT_DONE))
823 #if defined(CONFIG_SOC_OCELOT) || defined(CONFIG_SOC_JR2)
824 /* Establish data contents in DDR RAM for training */
826 __raw_writel(0xcacafefe, ((void __iomem *)MSCC_DDR_TO));
827 __raw_writel(0x22221111, ((void __iomem *)MSCC_DDR_TO + 0x4));
828 __raw_writel(0x44443333, ((void __iomem *)MSCC_DDR_TO + 0x8));
829 __raw_writel(0x66665555, ((void __iomem *)MSCC_DDR_TO + 0xC));
830 __raw_writel(0x88887777, ((void __iomem *)MSCC_DDR_TO + 0x10));
831 __raw_writel(0xaaaa9999, ((void __iomem *)MSCC_DDR_TO + 0x14));
832 __raw_writel(0xccccbbbb, ((void __iomem *)MSCC_DDR_TO + 0x18));
833 __raw_writel(0xeeeedddd, ((void __iomem *)MSCC_DDR_TO + 0x1C));
835 __raw_writel(0xff, ((void __iomem *)MSCC_DDR_TO));
838 #endif /* __ASM_MACH_DDR_H */