xref: /openbmc/u-boot/arch/mips/mach-mscc/include/mach/ddr.h (revision aff66f22d6eeb27c6329c0a3c1ebc52914c8affa)
1dd1033e4SGregory CLEMENT /* SPDX-License-Identifier: (GPL-2.0+ OR MIT) */
2dd1033e4SGregory CLEMENT /*
3dd1033e4SGregory CLEMENT  * Copyright (c) 2018 Microsemi Corporation
4dd1033e4SGregory CLEMENT  */
5dd1033e4SGregory CLEMENT 
6dd1033e4SGregory CLEMENT #ifndef __ASM_MACH_DDR_H
7dd1033e4SGregory CLEMENT #define __ASM_MACH_DDR_H
8dd1033e4SGregory CLEMENT 
9dd1033e4SGregory CLEMENT #include <asm/cacheops.h>
10dd1033e4SGregory CLEMENT #include <asm/io.h>
11dd1033e4SGregory CLEMENT #include <asm/reboot.h>
12dd1033e4SGregory CLEMENT #include <mach/common.h>
13dd1033e4SGregory CLEMENT 
14dd1033e4SGregory CLEMENT #define MIPS_VCOREIII_MEMORY_DDR3
15dd1033e4SGregory CLEMENT #define MIPS_VCOREIII_DDR_SIZE CONFIG_SYS_SDRAM_SIZE
16dd1033e4SGregory CLEMENT 
17dd1033e4SGregory CLEMENT #if defined(CONFIG_DDRTYPE_H5TQ1G63BFA)	/* Serval1 Refboard */
18dd1033e4SGregory CLEMENT 
19dd1033e4SGregory CLEMENT /* Hynix H5TQ1G63BFA (1Gbit DDR3, x16) @ 3.20ns */
20dd1033e4SGregory CLEMENT #define VC3_MPAR_bank_addr_cnt    3
21dd1033e4SGregory CLEMENT #define VC3_MPAR_row_addr_cnt     13
22dd1033e4SGregory CLEMENT #define VC3_MPAR_col_addr_cnt     10
23dd1033e4SGregory CLEMENT #define VC3_MPAR_tREFI            2437
24dd1033e4SGregory CLEMENT #define VC3_MPAR_tRAS_min         12
25dd1033e4SGregory CLEMENT #define VC3_MPAR_CL               6
26dd1033e4SGregory CLEMENT #define VC3_MPAR_tWTR             4
27dd1033e4SGregory CLEMENT #define VC3_MPAR_tRC              16
28*1895b87eSHoratiu Vultur #define VC3_MPAR_tFAW             16
29dd1033e4SGregory CLEMENT #define VC3_MPAR_tRP              5
30dd1033e4SGregory CLEMENT #define VC3_MPAR_tRRD             4
31dd1033e4SGregory CLEMENT #define VC3_MPAR_tRCD             5
32dd1033e4SGregory CLEMENT #define VC3_MPAR_tMRD             4
33dd1033e4SGregory CLEMENT #define VC3_MPAR_tRFC             35
34dd1033e4SGregory CLEMENT #define VC3_MPAR_CWL              5
35dd1033e4SGregory CLEMENT #define VC3_MPAR_tXPR             38
36dd1033e4SGregory CLEMENT #define VC3_MPAR_tMOD             12
37dd1033e4SGregory CLEMENT #define VC3_MPAR_tDLLK            512
38dd1033e4SGregory CLEMENT #define VC3_MPAR_tWR              5
39dd1033e4SGregory CLEMENT 
40dd1033e4SGregory CLEMENT #elif defined(CONFIG_DDRTYPE_MT41J128M16HA)	/* Validation board */
41dd1033e4SGregory CLEMENT 
42dd1033e4SGregory CLEMENT /* Micron MT41J128M16HA-15E:D (2Gbit DDR3, x16) @ 3.20ns */
43dd1033e4SGregory CLEMENT #define VC3_MPAR_bank_addr_cnt    3
44dd1033e4SGregory CLEMENT #define VC3_MPAR_row_addr_cnt     14
45dd1033e4SGregory CLEMENT #define VC3_MPAR_col_addr_cnt     10
46dd1033e4SGregory CLEMENT #define VC3_MPAR_tREFI            2437
47dd1033e4SGregory CLEMENT #define VC3_MPAR_tRAS_min         12
48dd1033e4SGregory CLEMENT #define VC3_MPAR_CL               5
49dd1033e4SGregory CLEMENT #define VC3_MPAR_tWTR             4
50dd1033e4SGregory CLEMENT #define VC3_MPAR_tRC              16
51dd1033e4SGregory CLEMENT #define VC3_MPAR_tFAW             16
52dd1033e4SGregory CLEMENT #define VC3_MPAR_tRP              5
53dd1033e4SGregory CLEMENT #define VC3_MPAR_tRRD             4
54dd1033e4SGregory CLEMENT #define VC3_MPAR_tRCD             5
55dd1033e4SGregory CLEMENT #define VC3_MPAR_tMRD             4
56dd1033e4SGregory CLEMENT #define VC3_MPAR_tRFC             50
57dd1033e4SGregory CLEMENT #define VC3_MPAR_CWL              5
58dd1033e4SGregory CLEMENT #define VC3_MPAR_tXPR             54
59dd1033e4SGregory CLEMENT #define VC3_MPAR_tMOD             12
60dd1033e4SGregory CLEMENT #define VC3_MPAR_tDLLK            512
61dd1033e4SGregory CLEMENT #define VC3_MPAR_tWR              5
62dd1033e4SGregory CLEMENT 
63dd1033e4SGregory CLEMENT #elif defined(CONFIG_DDRTYPE_MT41K256M16)	/* JR2 Validation board */
64dd1033e4SGregory CLEMENT 
65dd1033e4SGregory CLEMENT /* Micron MT41K256M16 (4Gbit, DDR3L-800, 256Mbitx16) @ 3.20ns */
66dd1033e4SGregory CLEMENT #define VC3_MPAR_bank_addr_cnt    3
67dd1033e4SGregory CLEMENT #define VC3_MPAR_row_addr_cnt     15
68dd1033e4SGregory CLEMENT #define VC3_MPAR_col_addr_cnt     10
69dd1033e4SGregory CLEMENT #define VC3_MPAR_tREFI            2437
70dd1033e4SGregory CLEMENT #define VC3_MPAR_tRAS_min         12
71dd1033e4SGregory CLEMENT #define VC3_MPAR_CL               5
72dd1033e4SGregory CLEMENT #define VC3_MPAR_tWTR             4
73dd1033e4SGregory CLEMENT #define VC3_MPAR_tRC              16
74dd1033e4SGregory CLEMENT #define VC3_MPAR_tFAW             16
75dd1033e4SGregory CLEMENT #define VC3_MPAR_tRP              5
76dd1033e4SGregory CLEMENT #define VC3_MPAR_tRRD             4
77dd1033e4SGregory CLEMENT #define VC3_MPAR_tRCD             5
78dd1033e4SGregory CLEMENT #define VC3_MPAR_tMRD             4
79dd1033e4SGregory CLEMENT #define VC3_MPAR_tRFC             82
80dd1033e4SGregory CLEMENT #define VC3_MPAR_CWL              5
81dd1033e4SGregory CLEMENT #define VC3_MPAR_tXPR             85
82dd1033e4SGregory CLEMENT #define VC3_MPAR_tMOD             12
83dd1033e4SGregory CLEMENT #define VC3_MPAR_tDLLK            512
84dd1033e4SGregory CLEMENT #define VC3_MPAR_tWR              5
85dd1033e4SGregory CLEMENT 
86dd1033e4SGregory CLEMENT #elif defined(CONFIG_DDRTYPE_H5TQ4G63MFR)	/* JR2 Reference board */
87dd1033e4SGregory CLEMENT 
88dd1033e4SGregory CLEMENT /* Hynix H5TQ4G63MFR-PBC (4Gbit, DDR3-800, 256Mbitx16) - 2kb pages @ 3.20ns */
89dd1033e4SGregory CLEMENT #define VC3_MPAR_bank_addr_cnt    3
90dd1033e4SGregory CLEMENT #define VC3_MPAR_row_addr_cnt     15
91dd1033e4SGregory CLEMENT #define VC3_MPAR_col_addr_cnt     10
92dd1033e4SGregory CLEMENT #define VC3_MPAR_tREFI            2437
93dd1033e4SGregory CLEMENT #define VC3_MPAR_tRAS_min         12
94dd1033e4SGregory CLEMENT #define VC3_MPAR_CL               6
95dd1033e4SGregory CLEMENT #define VC3_MPAR_tWTR             4
96dd1033e4SGregory CLEMENT #define VC3_MPAR_tRC              17
97dd1033e4SGregory CLEMENT #define VC3_MPAR_tFAW             16
98dd1033e4SGregory CLEMENT #define VC3_MPAR_tRP              5
99dd1033e4SGregory CLEMENT #define VC3_MPAR_tRRD             4
100dd1033e4SGregory CLEMENT #define VC3_MPAR_tRCD             5
101dd1033e4SGregory CLEMENT #define VC3_MPAR_tMRD             4
102dd1033e4SGregory CLEMENT #define VC3_MPAR_tRFC             82
103dd1033e4SGregory CLEMENT #define VC3_MPAR_CWL              5
104dd1033e4SGregory CLEMENT #define VC3_MPAR_tXPR             85
105dd1033e4SGregory CLEMENT #define VC3_MPAR_tMOD             12
106dd1033e4SGregory CLEMENT #define VC3_MPAR_tDLLK            512
107dd1033e4SGregory CLEMENT #define VC3_MPAR_tWR              5
108dd1033e4SGregory CLEMENT 
109dd1033e4SGregory CLEMENT #elif defined(CONFIG_DDRTYPE_MT41K128M16JT)
110dd1033e4SGregory CLEMENT 
111dd1033e4SGregory CLEMENT /* Micron Micron MT41K128M16JT-125 (2Gbit DDR3L, 128Mbitx16) @ 3.20ns */
112dd1033e4SGregory CLEMENT #define VC3_MPAR_bank_addr_cnt    3
113dd1033e4SGregory CLEMENT #define VC3_MPAR_row_addr_cnt     14
114dd1033e4SGregory CLEMENT #define VC3_MPAR_col_addr_cnt     10
115dd1033e4SGregory CLEMENT #define VC3_MPAR_tREFI            2437
116dd1033e4SGregory CLEMENT #define VC3_MPAR_tRAS_min         12
117dd1033e4SGregory CLEMENT #define VC3_MPAR_CL               6
118dd1033e4SGregory CLEMENT #define VC3_MPAR_tWTR             4
119dd1033e4SGregory CLEMENT #define VC3_MPAR_tRC              16
120dd1033e4SGregory CLEMENT #define VC3_MPAR_tFAW             16
121dd1033e4SGregory CLEMENT #define VC3_MPAR_tRP              5
122dd1033e4SGregory CLEMENT #define VC3_MPAR_tRRD             4
123dd1033e4SGregory CLEMENT #define VC3_MPAR_tRCD             5
124dd1033e4SGregory CLEMENT #define VC3_MPAR_tMRD             4
125dd1033e4SGregory CLEMENT #define VC3_MPAR_tRFC             82
126dd1033e4SGregory CLEMENT #define VC3_MPAR_CWL              5
127dd1033e4SGregory CLEMENT #define VC3_MPAR_tXPR             85
128dd1033e4SGregory CLEMENT #define VC3_MPAR_tMOD             12
129dd1033e4SGregory CLEMENT #define VC3_MPAR_tDLLK            512
130dd1033e4SGregory CLEMENT #define VC3_MPAR_tWR              5
131dd1033e4SGregory CLEMENT 
132dd1033e4SGregory CLEMENT #elif defined(CONFIG_DDRTYPE_MT47H128M8HQ)	/* Luton10/26 Refboards */
133dd1033e4SGregory CLEMENT 
134dd1033e4SGregory CLEMENT /* Micron 1Gb MT47H128M8-3 16Meg x 8 x 8 banks, DDR-533@CL4 @ 4.80ns */
135dd1033e4SGregory CLEMENT #define VC3_MPAR_bank_addr_cnt    3
136dd1033e4SGregory CLEMENT #define VC3_MPAR_row_addr_cnt     14
137dd1033e4SGregory CLEMENT #define VC3_MPAR_col_addr_cnt     10
138dd1033e4SGregory CLEMENT #define VC3_MPAR_tREFI            1625
139dd1033e4SGregory CLEMENT #define VC3_MPAR_tRAS_min         9
140dd1033e4SGregory CLEMENT #define VC3_MPAR_CL               4
141dd1033e4SGregory CLEMENT #define VC3_MPAR_tWTR             2
142dd1033e4SGregory CLEMENT #define VC3_MPAR_tRC              12
143dd1033e4SGregory CLEMENT #define VC3_MPAR_tFAW             8
144dd1033e4SGregory CLEMENT #define VC3_MPAR_tRP              4
145dd1033e4SGregory CLEMENT #define VC3_MPAR_tRRD             2
146dd1033e4SGregory CLEMENT #define VC3_MPAR_tRCD             4
147dd1033e4SGregory CLEMENT 
148dd1033e4SGregory CLEMENT #define VC3_MPAR_tRPA             4
149dd1033e4SGregory CLEMENT #define VC3_MPAR_tRP              4
150dd1033e4SGregory CLEMENT 
151dd1033e4SGregory CLEMENT #define VC3_MPAR_tMRD             2
152dd1033e4SGregory CLEMENT #define VC3_MPAR_tRFC             27
153dd1033e4SGregory CLEMENT 
154dd1033e4SGregory CLEMENT #define VC3_MPAR__400_ns_dly      84
155dd1033e4SGregory CLEMENT 
156dd1033e4SGregory CLEMENT #define VC3_MPAR_tWR              4
157dd1033e4SGregory CLEMENT #undef MIPS_VCOREIII_MEMORY_DDR3
158dd1033e4SGregory CLEMENT #else
159dd1033e4SGregory CLEMENT 
160dd1033e4SGregory CLEMENT #error Unknown DDR system configuration - please add!
161dd1033e4SGregory CLEMENT 
162dd1033e4SGregory CLEMENT #endif
163dd1033e4SGregory CLEMENT 
16405512517SHoratiu Vultur #if defined(CONFIG_SOC_OCELOT) || defined(CONFIG_SOC_JR2) || \
165*1895b87eSHoratiu Vultur 	defined(CONFIG_SOC_SERVALT) || defined(CONFIG_SOC_SERVAL)
166dd1033e4SGregory CLEMENT #define MIPS_VCOREIII_MEMORY_16BIT 1
167dd1033e4SGregory CLEMENT #endif
168dd1033e4SGregory CLEMENT 
169dd1033e4SGregory CLEMENT #define MIPS_VCOREIII_MEMORY_SSTL_ODT 7
170dd1033e4SGregory CLEMENT #define MIPS_VCOREIII_MEMORY_SSTL_DRIVE 7
171dd1033e4SGregory CLEMENT #define VCOREIII_DDR_DQS_MODE_CALIBRATE
172dd1033e4SGregory CLEMENT 
173dd1033e4SGregory CLEMENT #ifdef MIPS_VCOREIII_MEMORY_16BIT
174dd1033e4SGregory CLEMENT #define VC3_MPAR_16BIT       1
175dd1033e4SGregory CLEMENT #else
176dd1033e4SGregory CLEMENT #define VC3_MPAR_16BIT       0
177dd1033e4SGregory CLEMENT #endif
178dd1033e4SGregory CLEMENT 
179dd1033e4SGregory CLEMENT #ifdef MIPS_VCOREIII_MEMORY_DDR3
180dd1033e4SGregory CLEMENT #define VC3_MPAR_DDR3_MODE    1	/* DDR3 */
181dd1033e4SGregory CLEMENT #define VC3_MPAR_BURST_LENGTH 8	/* Always 8 (1) for DDR3 */
182dd1033e4SGregory CLEMENT #ifdef MIPS_VCOREIII_MEMORY_16BIT
183dd1033e4SGregory CLEMENT #define VC3_MPAR_BURST_SIZE   1	/* Always 1 for DDR3/16bit */
184dd1033e4SGregory CLEMENT #else
185dd1033e4SGregory CLEMENT #define VC3_MPAR_BURST_SIZE   0
186dd1033e4SGregory CLEMENT #endif
187dd1033e4SGregory CLEMENT #else
188dd1033e4SGregory CLEMENT #define VC3_MPAR_DDR3_MODE    0	/* DDR2 */
189dd1033e4SGregory CLEMENT #ifdef MIPS_VCOREIII_MEMORY_16BIT
190dd1033e4SGregory CLEMENT #define VC3_MPAR_BURST_LENGTH 4	/* in DDR2 16-bit mode, use burstlen 4 */
191dd1033e4SGregory CLEMENT #else
192dd1033e4SGregory CLEMENT #define VC3_MPAR_BURST_LENGTH 8	/* For 8-bit IF we must run burst-8 */
193dd1033e4SGregory CLEMENT #endif
194dd1033e4SGregory CLEMENT #define VC3_MPAR_BURST_SIZE   0	/* Always 0 for DDR2 */
195dd1033e4SGregory CLEMENT #endif
196dd1033e4SGregory CLEMENT 
197dd1033e4SGregory CLEMENT #define VC3_MPAR_RL VC3_MPAR_CL
198dd1033e4SGregory CLEMENT #if !defined(MIPS_VCOREIII_MEMORY_DDR3)
199dd1033e4SGregory CLEMENT #define VC3_MPAR_WL (VC3_MPAR_RL - 1)
200dd1033e4SGregory CLEMENT #define VC3_MPAR_MD VC3_MPAR_tMRD
201dd1033e4SGregory CLEMENT #define VC3_MPAR_ID VC3_MPAR__400_ns_dly
202dd1033e4SGregory CLEMENT #define VC3_MPAR_SD VC3_MPAR_tXSRD
203dd1033e4SGregory CLEMENT #define VC3_MPAR_OW (VC3_MPAR_WL - 2)
204dd1033e4SGregory CLEMENT #define VC3_MPAR_OR (VC3_MPAR_WL - 3)
205dd1033e4SGregory CLEMENT #define VC3_MPAR_RP (VC3_MPAR_bank_addr_cnt < 3 ? VC3_MPAR_tRP : VC3_MPAR_tRPA)
206dd1033e4SGregory CLEMENT #define VC3_MPAR_FAW (VC3_MPAR_bank_addr_cnt < 3 ? 1 : VC3_MPAR_tFAW)
207dd1033e4SGregory CLEMENT #define VC3_MPAR_BL (VC3_MPAR_BURST_LENGTH == 4 ? 2 : 4)
208dd1033e4SGregory CLEMENT #define MSCC_MEMPARM_MR0 \
209dd1033e4SGregory CLEMENT 	(VC3_MPAR_BURST_LENGTH == 8 ? 3 : 2) | (VC3_MPAR_CL << 4) | \
210dd1033e4SGregory CLEMENT 	((VC3_MPAR_tWR - 1) << 9)
211dd1033e4SGregory CLEMENT /* DLL-on, Full-OD, AL=0, RTT=off, nDQS-on, RDQS-off, out-en */
212dd1033e4SGregory CLEMENT #define MSCC_MEMPARM_MR1 0x382
213dd1033e4SGregory CLEMENT #define MSCC_MEMPARM_MR2 0
214dd1033e4SGregory CLEMENT #define MSCC_MEMPARM_MR3 0
215dd1033e4SGregory CLEMENT #else
216dd1033e4SGregory CLEMENT #define VC3_MPAR_WL VC3_MPAR_CWL
217dd1033e4SGregory CLEMENT #define VC3_MPAR_MD VC3_MPAR_tMOD
218dd1033e4SGregory CLEMENT #define VC3_MPAR_ID VC3_MPAR_tXPR
219dd1033e4SGregory CLEMENT #define VC3_MPAR_SD VC3_MPAR_tDLLK
220dd1033e4SGregory CLEMENT #define VC3_MPAR_OW 2
221dd1033e4SGregory CLEMENT #define VC3_MPAR_OR 2
222dd1033e4SGregory CLEMENT #define VC3_MPAR_RP VC3_MPAR_tRP
223dd1033e4SGregory CLEMENT #define VC3_MPAR_FAW VC3_MPAR_tFAW
224dd1033e4SGregory CLEMENT #define VC3_MPAR_BL 4
225dd1033e4SGregory CLEMENT #define MSCC_MEMPARM_MR0 ((VC3_MPAR_RL - 4) << 4) | ((VC3_MPAR_tWR - 4) << 9)
226dd1033e4SGregory CLEMENT /* ODT_RTT: “0x0040” for 120ohm, and “0x0004” for 60ohm. */
227dd1033e4SGregory CLEMENT #define MSCC_MEMPARM_MR1 0x0040
228dd1033e4SGregory CLEMENT #define MSCC_MEMPARM_MR2 ((VC3_MPAR_WL - 5) << 3)
229dd1033e4SGregory CLEMENT #define MSCC_MEMPARM_MR3 0
230dd1033e4SGregory CLEMENT #endif				/* MIPS_VCOREIII_MEMORY_DDR3 */
231dd1033e4SGregory CLEMENT 
232dd1033e4SGregory CLEMENT #define MSCC_MEMPARM_MEMCFG                                             \
233dd1033e4SGregory CLEMENT 	((MIPS_VCOREIII_DDR_SIZE > SZ_512M) ?				\
234dd1033e4SGregory CLEMENT 	 ICPU_MEMCTRL_CFG_DDR_512MBYTE_PLUS : 0) |			\
235dd1033e4SGregory CLEMENT 	(VC3_MPAR_16BIT ? ICPU_MEMCTRL_CFG_DDR_WIDTH : 0) |		\
236dd1033e4SGregory CLEMENT 	(VC3_MPAR_DDR3_MODE ? ICPU_MEMCTRL_CFG_DDR_MODE : 0) |		\
237dd1033e4SGregory CLEMENT 	(VC3_MPAR_BURST_SIZE ? ICPU_MEMCTRL_CFG_BURST_SIZE : 0) |	\
238dd1033e4SGregory CLEMENT 	(VC3_MPAR_BURST_LENGTH == 8 ? ICPU_MEMCTRL_CFG_BURST_LEN : 0) | \
239dd1033e4SGregory CLEMENT 	(VC3_MPAR_bank_addr_cnt == 3 ? ICPU_MEMCTRL_CFG_BANK_CNT : 0) | \
240dd1033e4SGregory CLEMENT 	ICPU_MEMCTRL_CFG_MSB_ROW_ADDR(VC3_MPAR_row_addr_cnt - 1) |	\
241dd1033e4SGregory CLEMENT 	ICPU_MEMCTRL_CFG_MSB_COL_ADDR(VC3_MPAR_col_addr_cnt - 1)
242dd1033e4SGregory CLEMENT 
24305512517SHoratiu Vultur #if defined(CONFIG_SOC_OCELOT) || defined(CONFIG_SOC_JR2) || \
244*1895b87eSHoratiu Vultur 	defined(CONFIG_SOC_SERVALT) || defined(CONFIG_SOC_SERVAL)
245dd1033e4SGregory CLEMENT #define MSCC_MEMPARM_PERIOD					\
246dd1033e4SGregory CLEMENT 	ICPU_MEMCTRL_REF_PERIOD_MAX_PEND_REF(8) |		\
247dd1033e4SGregory CLEMENT 	ICPU_MEMCTRL_REF_PERIOD_REF_PERIOD(VC3_MPAR_tREFI)
248dd1033e4SGregory CLEMENT 
249dd1033e4SGregory CLEMENT #define MSCC_MEMPARM_TIMING0                                            \
250dd1033e4SGregory CLEMENT 	ICPU_MEMCTRL_TIMING0_RD_TO_WR_DLY(VC3_MPAR_RL + VC3_MPAR_BL + 1 - \
251dd1033e4SGregory CLEMENT 					  VC3_MPAR_WL) |		\
252dd1033e4SGregory CLEMENT 	ICPU_MEMCTRL_TIMING0_WR_CS_CHANGE_DLY(VC3_MPAR_BL - 1) |	\
253dd1033e4SGregory CLEMENT 	ICPU_MEMCTRL_TIMING0_RD_CS_CHANGE_DLY(VC3_MPAR_BL) |		\
254dd1033e4SGregory CLEMENT 	ICPU_MEMCTRL_TIMING0_RAS_TO_PRECH_DLY(VC3_MPAR_tRAS_min - 1) |	\
255dd1033e4SGregory CLEMENT 	ICPU_MEMCTRL_TIMING0_WR_TO_PRECH_DLY(VC3_MPAR_WL +		\
256dd1033e4SGregory CLEMENT 					     VC3_MPAR_BL +		\
257dd1033e4SGregory CLEMENT 					     VC3_MPAR_tWR - 1) |	\
258dd1033e4SGregory CLEMENT 	ICPU_MEMCTRL_TIMING0_RD_TO_PRECH_DLY(VC3_MPAR_BL - 1) |		\
259dd1033e4SGregory CLEMENT 		ICPU_MEMCTRL_TIMING0_WR_DATA_XFR_DLY(VC3_MPAR_WL - 1) |	\
260dd1033e4SGregory CLEMENT 	ICPU_MEMCTRL_TIMING0_RD_DATA_XFR_DLY(VC3_MPAR_RL - 3)
261dd1033e4SGregory CLEMENT 
262dd1033e4SGregory CLEMENT #define MSCC_MEMPARM_TIMING1                                            \
263dd1033e4SGregory CLEMENT 	ICPU_MEMCTRL_TIMING1_RAS_TO_RAS_SAME_BANK_DLY(VC3_MPAR_tRC - 1) | \
264dd1033e4SGregory CLEMENT 	ICPU_MEMCTRL_TIMING1_BANK8_FAW_DLY(VC3_MPAR_FAW - 1) |		\
265dd1033e4SGregory CLEMENT 	ICPU_MEMCTRL_TIMING1_PRECH_TO_RAS_DLY(VC3_MPAR_RP - 1) |	\
266dd1033e4SGregory CLEMENT 	ICPU_MEMCTRL_TIMING1_RAS_TO_RAS_DLY(VC3_MPAR_tRRD - 1) |	\
267dd1033e4SGregory CLEMENT 	ICPU_MEMCTRL_TIMING1_RAS_TO_CAS_DLY(VC3_MPAR_tRCD - 1) |	\
268dd1033e4SGregory CLEMENT 	ICPU_MEMCTRL_TIMING1_WR_TO_RD_DLY(VC3_MPAR_WL +			\
269dd1033e4SGregory CLEMENT 					  VC3_MPAR_BL +			\
270dd1033e4SGregory CLEMENT 					  VC3_MPAR_tWTR - 1)
271dd1033e4SGregory CLEMENT 
272dd1033e4SGregory CLEMENT #define MSCC_MEMPARM_TIMING2					\
273dd1033e4SGregory CLEMENT 	ICPU_MEMCTRL_TIMING2_PRECH_ALL_DLY(VC3_MPAR_RP - 1) |	\
274dd1033e4SGregory CLEMENT 	ICPU_MEMCTRL_TIMING2_MDSET_DLY(VC3_MPAR_MD - 1) |		\
275dd1033e4SGregory CLEMENT 	ICPU_MEMCTRL_TIMING2_REF_DLY(VC3_MPAR_tRFC - 1) |		\
276dd1033e4SGregory CLEMENT 	ICPU_MEMCTRL_TIMING2_INIT_DLY(VC3_MPAR_ID - 1)
277dd1033e4SGregory CLEMENT 
278dd1033e4SGregory CLEMENT #define MSCC_MEMPARM_TIMING3						\
279dd1033e4SGregory CLEMENT 	ICPU_MEMCTRL_TIMING3_WR_TO_RD_CS_CHANGE_DLY(VC3_MPAR_WL +	\
280dd1033e4SGregory CLEMENT 						    VC3_MPAR_tWTR - 1) |\
281dd1033e4SGregory CLEMENT 	ICPU_MEMCTRL_TIMING3_ODT_RD_DLY(VC3_MPAR_OR - 1) |		\
282dd1033e4SGregory CLEMENT 	ICPU_MEMCTRL_TIMING3_ODT_WR_DLY(VC3_MPAR_OW - 1) |		\
283dd1033e4SGregory CLEMENT 	ICPU_MEMCTRL_TIMING3_LOCAL_ODT_RD_DLY(VC3_MPAR_RL - 3)
284dd1033e4SGregory CLEMENT 
285dd1033e4SGregory CLEMENT #else
286dd1033e4SGregory CLEMENT #define MSCC_MEMPARM_PERIOD					\
287dd1033e4SGregory CLEMENT 	ICPU_MEMCTRL_REF_PERIOD_MAX_PEND_REF(1) |		\
288dd1033e4SGregory CLEMENT 	ICPU_MEMCTRL_REF_PERIOD_REF_PERIOD(VC3_MPAR_tREFI)
289dd1033e4SGregory CLEMENT 
290dd1033e4SGregory CLEMENT #define MSCC_MEMPARM_TIMING0                                            \
291dd1033e4SGregory CLEMENT 	ICPU_MEMCTRL_TIMING0_RAS_TO_PRECH_DLY(VC3_MPAR_tRAS_min - 1) |	\
292dd1033e4SGregory CLEMENT 	ICPU_MEMCTRL_TIMING0_WR_TO_PRECH_DLY(VC3_MPAR_CL +		\
293dd1033e4SGregory CLEMENT 					     (VC3_MPAR_BURST_LENGTH == 8 ? 2 : 0) + \
294dd1033e4SGregory CLEMENT 					     VC3_MPAR_tWR) |		\
295dd1033e4SGregory CLEMENT 	ICPU_MEMCTRL_TIMING0_RD_TO_PRECH_DLY(VC3_MPAR_BURST_LENGTH == 8 ? 3 : 1) | \
296dd1033e4SGregory CLEMENT 	ICPU_MEMCTRL_TIMING0_WR_DATA_XFR_DLY(VC3_MPAR_CL - 3) |		\
297dd1033e4SGregory CLEMENT 	ICPU_MEMCTRL_TIMING0_RD_DATA_XFR_DLY(VC3_MPAR_CL - 3)
298dd1033e4SGregory CLEMENT 
299dd1033e4SGregory CLEMENT #define MSCC_MEMPARM_TIMING1                                            \
300dd1033e4SGregory CLEMENT 	ICPU_MEMCTRL_TIMING1_RAS_TO_RAS_SAME_BANK_DLY(VC3_MPAR_tRC - 1) | \
301dd1033e4SGregory CLEMENT 	ICPU_MEMCTRL_TIMING1_BANK8_FAW_DLY(VC3_MPAR_tFAW - 1) |		\
302dd1033e4SGregory CLEMENT 	ICPU_MEMCTRL_TIMING1_PRECH_TO_RAS_DLY(VC3_MPAR_tRP - 1) |	\
303dd1033e4SGregory CLEMENT 	ICPU_MEMCTRL_TIMING1_RAS_TO_RAS_DLY(VC3_MPAR_tRRD - 1) |	\
304dd1033e4SGregory CLEMENT 	ICPU_MEMCTRL_TIMING1_RAS_TO_CAS_DLY(VC3_MPAR_tRCD - 1) |	\
305dd1033e4SGregory CLEMENT 	ICPU_MEMCTRL_TIMING1_WR_TO_RD_DLY(VC3_MPAR_CL +			\
306dd1033e4SGregory CLEMENT 					  (VC3_MPAR_BURST_LENGTH == 8 ? 2 : 0) + \
307dd1033e4SGregory CLEMENT 					  VC3_MPAR_tWTR)
308dd1033e4SGregory CLEMENT #define MSCC_MEMPARM_TIMING2                                            \
309dd1033e4SGregory CLEMENT 	ICPU_MEMCTRL_TIMING2_PRECH_ALL_DLY(VC3_MPAR_tRPA - 1) |		\
310dd1033e4SGregory CLEMENT 	ICPU_MEMCTRL_TIMING2_MDSET_DLY(VC3_MPAR_tMRD - 1) |		\
311dd1033e4SGregory CLEMENT 	ICPU_MEMCTRL_TIMING2_REF_DLY(VC3_MPAR_tRFC - 1) |		\
312dd1033e4SGregory CLEMENT 	ICPU_MEMCTRL_TIMING2_FOUR_HUNDRED_NS_DLY(VC3_MPAR__400_ns_dly)
313dd1033e4SGregory CLEMENT 
314dd1033e4SGregory CLEMENT #define MSCC_MEMPARM_TIMING3						\
315dd1033e4SGregory CLEMENT 	ICPU_MEMCTRL_TIMING3_WR_TO_RD_CS_CHANGE_DLY(VC3_MPAR_CL - 1) |	\
316dd1033e4SGregory CLEMENT 	ICPU_MEMCTRL_TIMING3_ODT_WR_DLY(VC3_MPAR_CL - 1) |		\
317dd1033e4SGregory CLEMENT 	ICPU_MEMCTRL_TIMING3_LOCAL_ODT_RD_DLY(VC3_MPAR_CL - 1)
318dd1033e4SGregory CLEMENT 
319dd1033e4SGregory CLEMENT #endif
320dd1033e4SGregory CLEMENT 
321dd1033e4SGregory CLEMENT enum {
322dd1033e4SGregory CLEMENT 	DDR_TRAIN_OK,
323dd1033e4SGregory CLEMENT 	DDR_TRAIN_CONTINUE,
324dd1033e4SGregory CLEMENT 	DDR_TRAIN_ERROR,
325dd1033e4SGregory CLEMENT };
326dd1033e4SGregory CLEMENT 
327dd1033e4SGregory CLEMENT /*
328dd1033e4SGregory CLEMENT  * We actually have very few 'pause' possibilities apart from
329dd1033e4SGregory CLEMENT  * these assembly nops (at this very early stage).
330dd1033e4SGregory CLEMENT  */
331dd1033e4SGregory CLEMENT #define PAUSE() asm volatile("nop; nop; nop; nop; nop; nop; nop; nop")
332dd1033e4SGregory CLEMENT 
333dd1033e4SGregory CLEMENT /* NB: Assumes inlining as no stack is available! */
set_dly(u32 bytelane,u32 dly)334dd1033e4SGregory CLEMENT static inline void set_dly(u32 bytelane, u32 dly)
335dd1033e4SGregory CLEMENT {
336dd1033e4SGregory CLEMENT 	register u32 r = readl(BASE_CFG + ICPU_MEMCTRL_DQS_DLY(bytelane));
337dd1033e4SGregory CLEMENT 
338dd1033e4SGregory CLEMENT 	r &= ~ICPU_MEMCTRL_DQS_DLY_DQS_DLY_M;
339dd1033e4SGregory CLEMENT 	r |= ICPU_MEMCTRL_DQS_DLY_DQS_DLY(dly);
340dd1033e4SGregory CLEMENT 	writel(r, BASE_CFG + ICPU_MEMCTRL_DQS_DLY(bytelane));
341dd1033e4SGregory CLEMENT }
342dd1033e4SGregory CLEMENT 
incr_dly(u32 bytelane)343dd1033e4SGregory CLEMENT static inline bool incr_dly(u32 bytelane)
344dd1033e4SGregory CLEMENT {
345dd1033e4SGregory CLEMENT 	register u32 r = readl(BASE_CFG + ICPU_MEMCTRL_DQS_DLY(bytelane));
346dd1033e4SGregory CLEMENT 
347dd1033e4SGregory CLEMENT 	if (ICPU_MEMCTRL_DQS_DLY_DQS_DLY(r) < 31) {
348dd1033e4SGregory CLEMENT 		writel(r + 1, BASE_CFG + ICPU_MEMCTRL_DQS_DLY(bytelane));
349dd1033e4SGregory CLEMENT 		return true;
350dd1033e4SGregory CLEMENT 	}
351dd1033e4SGregory CLEMENT 
352dd1033e4SGregory CLEMENT 	return false;
353dd1033e4SGregory CLEMENT }
354dd1033e4SGregory CLEMENT 
adjust_dly(int adjust)355dd1033e4SGregory CLEMENT static inline bool adjust_dly(int adjust)
356dd1033e4SGregory CLEMENT {
357dd1033e4SGregory CLEMENT 	register u32 r = readl(BASE_CFG + ICPU_MEMCTRL_DQS_DLY(0));
358dd1033e4SGregory CLEMENT 
359dd1033e4SGregory CLEMENT 	if (ICPU_MEMCTRL_DQS_DLY_DQS_DLY(r) < 31) {
360dd1033e4SGregory CLEMENT 		writel(r + adjust, BASE_CFG + ICPU_MEMCTRL_DQS_DLY(0));
361dd1033e4SGregory CLEMENT 		return true;
362dd1033e4SGregory CLEMENT 	}
363dd1033e4SGregory CLEMENT 
364dd1033e4SGregory CLEMENT 	return false;
365dd1033e4SGregory CLEMENT }
366dd1033e4SGregory CLEMENT 
367dd1033e4SGregory CLEMENT /* NB: Assumes inlining as no stack is available! */
center_dly(u32 bytelane,u32 start)368dd1033e4SGregory CLEMENT static inline void center_dly(u32 bytelane, u32 start)
369dd1033e4SGregory CLEMENT {
370dd1033e4SGregory CLEMENT 	register u32 r = readl(BASE_CFG + ICPU_MEMCTRL_DQS_DLY(bytelane)) - start;
371dd1033e4SGregory CLEMENT 
372dd1033e4SGregory CLEMENT 	writel(start + (r >> 1), BASE_CFG + ICPU_MEMCTRL_DQS_DLY(bytelane));
373dd1033e4SGregory CLEMENT }
374dd1033e4SGregory CLEMENT 
memphy_soft_reset(void)375dd1033e4SGregory CLEMENT static inline void memphy_soft_reset(void)
376dd1033e4SGregory CLEMENT {
377dd1033e4SGregory CLEMENT 	setbits_le32(BASE_CFG + ICPU_MEMPHY_CFG, ICPU_MEMPHY_CFG_PHY_FIFO_RST);
378dd1033e4SGregory CLEMENT 	PAUSE();
379dd1033e4SGregory CLEMENT 	clrbits_le32(BASE_CFG + ICPU_MEMPHY_CFG, ICPU_MEMPHY_CFG_PHY_FIFO_RST);
380dd1033e4SGregory CLEMENT 	PAUSE();
381dd1033e4SGregory CLEMENT }
382dd1033e4SGregory CLEMENT 
38305512517SHoratiu Vultur #if defined(CONFIG_SOC_OCELOT) || defined(CONFIG_SOC_JR2) || \
384*1895b87eSHoratiu Vultur 	defined(CONFIG_SOC_SERVALT) || defined(CONFIG_SOC_SERVAL)
385dd1033e4SGregory CLEMENT static u8 training_data[] = { 0xfe, 0x11, 0x33, 0x55, 0x77, 0x99, 0xbb, 0xdd };
386dd1033e4SGregory CLEMENT 
sleep_100ns(u32 val)387dd1033e4SGregory CLEMENT static inline void sleep_100ns(u32 val)
388dd1033e4SGregory CLEMENT {
389dd1033e4SGregory CLEMENT 	/* Set the timer tick generator to 100 ns */
390dd1033e4SGregory CLEMENT 	writel(VCOREIII_TIMER_DIVIDER - 1, BASE_CFG + ICPU_TIMER_TICK_DIV);
391dd1033e4SGregory CLEMENT 
392dd1033e4SGregory CLEMENT 	/* Set the timer value */
393dd1033e4SGregory CLEMENT 	writel(val, BASE_CFG + ICPU_TIMER_VALUE(0));
394dd1033e4SGregory CLEMENT 
395dd1033e4SGregory CLEMENT 	/* Enable timer 0 for one-shot */
396dd1033e4SGregory CLEMENT 	writel(ICPU_TIMER_CTRL_ONE_SHOT_ENA | ICPU_TIMER_CTRL_TIMER_ENA,
397dd1033e4SGregory CLEMENT 	       BASE_CFG + ICPU_TIMER_CTRL(0));
398dd1033e4SGregory CLEMENT 
399dd1033e4SGregory CLEMENT 	/* Wait for timer 0 to reach 0 */
400dd1033e4SGregory CLEMENT 	while (readl(BASE_CFG + ICPU_TIMER_VALUE(0)) != 0)
401dd1033e4SGregory CLEMENT 		;
402dd1033e4SGregory CLEMENT }
403dd1033e4SGregory CLEMENT 
404e7a0de2cSHoratiu Vultur #if defined(CONFIG_SOC_OCELOT)
hal_vcoreiii_ddr_reset_assert(void)405dd1033e4SGregory CLEMENT static inline void hal_vcoreiii_ddr_reset_assert(void)
406dd1033e4SGregory CLEMENT {
407dd1033e4SGregory CLEMENT 	/* DDR has reset pin on GPIO 19 toggle Low-High to release */
408dd1033e4SGregory CLEMENT 	setbits_le32(BASE_DEVCPU_GCB + PERF_GPIO_OE, BIT(19));
409dd1033e4SGregory CLEMENT 	writel(BIT(19), BASE_DEVCPU_GCB + PERF_GPIO_OUT_CLR);
410dd1033e4SGregory CLEMENT 	sleep_100ns(10000);
411dd1033e4SGregory CLEMENT }
412dd1033e4SGregory CLEMENT 
hal_vcoreiii_ddr_reset_release(void)413dd1033e4SGregory CLEMENT static inline void hal_vcoreiii_ddr_reset_release(void)
414dd1033e4SGregory CLEMENT {
415dd1033e4SGregory CLEMENT 	/* DDR has reset pin on GPIO 19 toggle Low-High to release */
416dd1033e4SGregory CLEMENT 	setbits_le32(BASE_DEVCPU_GCB + PERF_GPIO_OE, BIT(19));
417dd1033e4SGregory CLEMENT 	writel(BIT(19), BASE_DEVCPU_GCB + PERF_GPIO_OUT_SET);
418dd1033e4SGregory CLEMENT 	sleep_100ns(10000);
419dd1033e4SGregory CLEMENT }
420dd1033e4SGregory CLEMENT 
421dd1033e4SGregory CLEMENT /*
422dd1033e4SGregory CLEMENT  * DDR memory sanity checking failed, tally and do hard reset
423dd1033e4SGregory CLEMENT  *
424dd1033e4SGregory CLEMENT  * NB: Assumes inlining as no stack is available!
425dd1033e4SGregory CLEMENT  */
hal_vcoreiii_ddr_failed(void)426dd1033e4SGregory CLEMENT static inline void hal_vcoreiii_ddr_failed(void)
427dd1033e4SGregory CLEMENT {
428dd1033e4SGregory CLEMENT 	register u32 reset;
429dd1033e4SGregory CLEMENT 
430dd1033e4SGregory CLEMENT 	writel(readl(BASE_CFG + ICPU_GPR(6)) + 1, BASE_CFG + ICPU_GPR(6));
431dd1033e4SGregory CLEMENT 
432dd1033e4SGregory CLEMENT 	clrbits_le32(BASE_DEVCPU_GCB + PERF_GPIO_OE, BIT(19));
433dd1033e4SGregory CLEMENT 
434dd1033e4SGregory CLEMENT 	/* We have to execute the reset function from cache. Indeed,
435dd1033e4SGregory CLEMENT 	 * the reboot workaround in _machine_restart() will change the
436dd1033e4SGregory CLEMENT 	 * SPI NOR into SW bitbang.
437dd1033e4SGregory CLEMENT 	 *
438dd1033e4SGregory CLEMENT 	 * This will render the CPU unable to execute directly from
439dd1033e4SGregory CLEMENT 	 * the NOR, which is why the reset instructions are prefetched
440dd1033e4SGregory CLEMENT 	 * into the I-cache.
441dd1033e4SGregory CLEMENT 	 *
442dd1033e4SGregory CLEMENT 	 * When failing the DDR initialization we are executing from
443dd1033e4SGregory CLEMENT 	 * NOR.
444dd1033e4SGregory CLEMENT 	 *
445dd1033e4SGregory CLEMENT 	 * The last instruction in _machine_restart() will reset the
446dd1033e4SGregory CLEMENT 	 * MIPS CPU (and the cache), and the CPU will start executing
447dd1033e4SGregory CLEMENT 	 * from the reset vector.
448dd1033e4SGregory CLEMENT 	 */
449dd1033e4SGregory CLEMENT 	reset = KSEG0ADDR(_machine_restart);
450dd1033e4SGregory CLEMENT 	icache_lock((void *)reset, 128);
451dd1033e4SGregory CLEMENT 	asm volatile ("jr %0"::"r" (reset));
452dd1033e4SGregory CLEMENT 
453dd1033e4SGregory CLEMENT 	panic("DDR init failed\n");
454dd1033e4SGregory CLEMENT }
455*1895b87eSHoratiu Vultur #else				/* JR2 || ServalT || Serval */
hal_vcoreiii_ddr_reset_assert(void)456e7a0de2cSHoratiu Vultur static inline void hal_vcoreiii_ddr_reset_assert(void)
457e7a0de2cSHoratiu Vultur {
458e7a0de2cSHoratiu Vultur 	/* Ensure the memory controller physical iface is forced reset */
459e7a0de2cSHoratiu Vultur 	writel(readl(BASE_CFG + ICPU_MEMPHY_CFG) |
460e7a0de2cSHoratiu Vultur 	       ICPU_MEMPHY_CFG_PHY_RST, BASE_CFG + ICPU_MEMPHY_CFG);
461e7a0de2cSHoratiu Vultur 
462e7a0de2cSHoratiu Vultur 	/* Ensure the memory controller is forced reset */
463e7a0de2cSHoratiu Vultur 	writel(readl(BASE_CFG + ICPU_RESET) |
464e7a0de2cSHoratiu Vultur 	       ICPU_RESET_MEM_RST_FORCE, BASE_CFG + ICPU_RESET);
465e7a0de2cSHoratiu Vultur }
466e7a0de2cSHoratiu Vultur 
hal_vcoreiii_ddr_failed(void)467e7a0de2cSHoratiu Vultur static inline void hal_vcoreiii_ddr_failed(void)
468e7a0de2cSHoratiu Vultur {
469e7a0de2cSHoratiu Vultur 	writel(0, BASE_CFG + ICPU_RESET);
470e7a0de2cSHoratiu Vultur 	writel(PERF_SOFT_RST_SOFT_CHIP_RST, BASE_CFG + PERF_SOFT_RST);
471e7a0de2cSHoratiu Vultur 
472e7a0de2cSHoratiu Vultur 	panic("DDR init failed\n");
473e7a0de2cSHoratiu Vultur }
474*1895b87eSHoratiu Vultur #endif				/* JR2 || ServalT || Serval */
475dd1033e4SGregory CLEMENT 
476dd1033e4SGregory CLEMENT /*
477dd1033e4SGregory CLEMENT  * DDR memory sanity checking done, possibly enable ECC.
478dd1033e4SGregory CLEMENT  *
479dd1033e4SGregory CLEMENT  * NB: Assumes inlining as no stack is available!
480dd1033e4SGregory CLEMENT  */
hal_vcoreiii_ddr_verified(void)481dd1033e4SGregory CLEMENT static inline void hal_vcoreiii_ddr_verified(void)
482dd1033e4SGregory CLEMENT {
483dd1033e4SGregory CLEMENT #ifdef MIPS_VCOREIII_MEMORY_ECC
484dd1033e4SGregory CLEMENT 	/* Finally, enable ECC */
485dd1033e4SGregory CLEMENT 	register u32 val = readl(BASE_CFG + ICPU_MEMCTRL_CFG);
486dd1033e4SGregory CLEMENT 
487dd1033e4SGregory CLEMENT 	val |= ICPU_MEMCTRL_CFG_DDR_ECC_ERR_ENA;
488dd1033e4SGregory CLEMENT 	val &= ~ICPU_MEMCTRL_CFG_BURST_SIZE;
489dd1033e4SGregory CLEMENT 
490dd1033e4SGregory CLEMENT 	writel(val, BASE_CFG + ICPU_MEMCTRL_CFG);
491dd1033e4SGregory CLEMENT #endif
492dd1033e4SGregory CLEMENT 
493dd1033e4SGregory CLEMENT 	/* Reset Status register - sticky bits */
494dd1033e4SGregory CLEMENT 	writel(readl(BASE_CFG + ICPU_MEMCTRL_STAT), BASE_CFG + ICPU_MEMCTRL_STAT);
495dd1033e4SGregory CLEMENT }
496dd1033e4SGregory CLEMENT 
497dd1033e4SGregory CLEMENT /* NB: Assumes inlining as no stack is available! */
look_for(u32 bytelane)498dd1033e4SGregory CLEMENT static inline int look_for(u32 bytelane)
499dd1033e4SGregory CLEMENT {
500dd1033e4SGregory CLEMENT 	register u32 i;
501dd1033e4SGregory CLEMENT 
502dd1033e4SGregory CLEMENT 	/* Reset FIFO in case any previous access failed */
503dd1033e4SGregory CLEMENT 	for (i = 0; i < sizeof(training_data); i++) {
504dd1033e4SGregory CLEMENT 		register u32 byte;
505dd1033e4SGregory CLEMENT 
506dd1033e4SGregory CLEMENT 		memphy_soft_reset();
507dd1033e4SGregory CLEMENT 		/* Reset sticky bits */
508dd1033e4SGregory CLEMENT 		writel(readl(BASE_CFG + ICPU_MEMCTRL_STAT),
509dd1033e4SGregory CLEMENT 		       BASE_CFG + ICPU_MEMCTRL_STAT);
510dd1033e4SGregory CLEMENT 		/* Read data */
511dd1033e4SGregory CLEMENT 		byte = __raw_readb((void __iomem *)MSCC_DDR_TO + bytelane +
512dd1033e4SGregory CLEMENT 				   (i * 4));
513dd1033e4SGregory CLEMENT 
514dd1033e4SGregory CLEMENT 		/*
515dd1033e4SGregory CLEMENT 		 * Prevent the compiler reordering the instruction so
516dd1033e4SGregory CLEMENT 		 * the read of RAM happens after the check of the
517dd1033e4SGregory CLEMENT 		 * errors.
518dd1033e4SGregory CLEMENT 		 */
519dd1033e4SGregory CLEMENT 		rmb();
520dd1033e4SGregory CLEMENT 		if (readl(BASE_CFG + ICPU_MEMCTRL_STAT) &
521dd1033e4SGregory CLEMENT 		    (ICPU_MEMCTRL_STAT_RDATA_MASKED |
522dd1033e4SGregory CLEMENT 		     ICPU_MEMCTRL_STAT_RDATA_DUMMY)) {
523dd1033e4SGregory CLEMENT 			/* Noise on the line */
524dd1033e4SGregory CLEMENT 			goto read_error;
525dd1033e4SGregory CLEMENT 		}
526dd1033e4SGregory CLEMENT 		/* If mismatch, increment DQS - if possible */
527dd1033e4SGregory CLEMENT 		if (byte != training_data[i]) {
528dd1033e4SGregory CLEMENT  read_error:
529dd1033e4SGregory CLEMENT 			if (!incr_dly(bytelane))
530dd1033e4SGregory CLEMENT 				return DDR_TRAIN_ERROR;
531dd1033e4SGregory CLEMENT 			return DDR_TRAIN_CONTINUE;
532dd1033e4SGregory CLEMENT 		}
533dd1033e4SGregory CLEMENT 	}
534dd1033e4SGregory CLEMENT 	return DDR_TRAIN_OK;
535dd1033e4SGregory CLEMENT }
536dd1033e4SGregory CLEMENT 
537dd1033e4SGregory CLEMENT /* NB: Assumes inlining as no stack is available! */
look_past(u32 bytelane)538dd1033e4SGregory CLEMENT static inline int look_past(u32 bytelane)
539dd1033e4SGregory CLEMENT {
540dd1033e4SGregory CLEMENT 	register u32 i;
541dd1033e4SGregory CLEMENT 
542dd1033e4SGregory CLEMENT 	/* Reset FIFO in case any previous access failed */
543dd1033e4SGregory CLEMENT 	for (i = 0; i < sizeof(training_data); i++) {
544dd1033e4SGregory CLEMENT 		register u32 byte;
545dd1033e4SGregory CLEMENT 
546dd1033e4SGregory CLEMENT 		memphy_soft_reset();
547dd1033e4SGregory CLEMENT 		/* Ack sticky bits */
548dd1033e4SGregory CLEMENT 		writel(readl(BASE_CFG + ICPU_MEMCTRL_STAT),
549dd1033e4SGregory CLEMENT 		       BASE_CFG + ICPU_MEMCTRL_STAT);
550dd1033e4SGregory CLEMENT 		byte = __raw_readb((void __iomem *)MSCC_DDR_TO + bytelane +
551dd1033e4SGregory CLEMENT 				   (i * 4));
552dd1033e4SGregory CLEMENT 		/*
553dd1033e4SGregory CLEMENT 		 * Prevent the compiler reordering the instruction so
554dd1033e4SGregory CLEMENT 		 * the read of RAM happens after the check of the
555dd1033e4SGregory CLEMENT 		 * errors.
556dd1033e4SGregory CLEMENT 		 */
557dd1033e4SGregory CLEMENT 		rmb();
558dd1033e4SGregory CLEMENT 		if (readl(BASE_CFG + ICPU_MEMCTRL_STAT) &
559dd1033e4SGregory CLEMENT 		    (ICPU_MEMCTRL_STAT_RDATA_MASKED |
560dd1033e4SGregory CLEMENT 		     ICPU_MEMCTRL_STAT_RDATA_DUMMY)) {
561dd1033e4SGregory CLEMENT 			/* Noise on the line */
562dd1033e4SGregory CLEMENT 			goto read_error;
563dd1033e4SGregory CLEMENT 		}
564dd1033e4SGregory CLEMENT 		/* Bail out when we see first mismatch */
565dd1033e4SGregory CLEMENT 		if (byte != training_data[i]) {
566dd1033e4SGregory CLEMENT  read_error:
567dd1033e4SGregory CLEMENT 			return DDR_TRAIN_OK;
568dd1033e4SGregory CLEMENT 		}
569dd1033e4SGregory CLEMENT 	}
570dd1033e4SGregory CLEMENT 	/* All data compares OK, increase DQS and retry */
571dd1033e4SGregory CLEMENT 	if (!incr_dly(bytelane))
572dd1033e4SGregory CLEMENT 		return DDR_TRAIN_ERROR;
573dd1033e4SGregory CLEMENT 
574dd1033e4SGregory CLEMENT 	return DDR_TRAIN_CONTINUE;
575dd1033e4SGregory CLEMENT }
576dd1033e4SGregory CLEMENT 
hal_vcoreiii_train_bytelane(u32 bytelane)577dd1033e4SGregory CLEMENT static inline int hal_vcoreiii_train_bytelane(u32 bytelane)
578dd1033e4SGregory CLEMENT {
579dd1033e4SGregory CLEMENT 	register int res;
580dd1033e4SGregory CLEMENT 	register u32 dqs_s;
581dd1033e4SGregory CLEMENT 
582dd1033e4SGregory CLEMENT 	set_dly(bytelane, 0);	/* Start training at DQS=0 */
583dd1033e4SGregory CLEMENT 	while ((res = look_for(bytelane)) == DDR_TRAIN_CONTINUE)
584dd1033e4SGregory CLEMENT 		;
585dd1033e4SGregory CLEMENT 	if (res != DDR_TRAIN_OK)
586dd1033e4SGregory CLEMENT 		return res;
587dd1033e4SGregory CLEMENT 
588dd1033e4SGregory CLEMENT 	dqs_s = readl(BASE_CFG + ICPU_MEMCTRL_DQS_DLY(bytelane));
589dd1033e4SGregory CLEMENT 	while ((res = look_past(bytelane)) == DDR_TRAIN_CONTINUE)
590dd1033e4SGregory CLEMENT 		;
591dd1033e4SGregory CLEMENT 	if (res != DDR_TRAIN_OK)
592dd1033e4SGregory CLEMENT 		return res;
593dd1033e4SGregory CLEMENT 	/* Reset FIFO - for good measure */
594dd1033e4SGregory CLEMENT 	memphy_soft_reset();
595dd1033e4SGregory CLEMENT 	/* Adjust to center [dqs_s;cur] */
596dd1033e4SGregory CLEMENT 	center_dly(bytelane, dqs_s);
597dd1033e4SGregory CLEMENT 	return DDR_TRAIN_OK;
598dd1033e4SGregory CLEMENT }
599dd1033e4SGregory CLEMENT 
600dd1033e4SGregory CLEMENT /* This algorithm is converted from the TCL training algorithm used
601dd1033e4SGregory CLEMENT  * during silicon simulation.
602dd1033e4SGregory CLEMENT  * NB: Assumes inlining as no stack is available!
603dd1033e4SGregory CLEMENT  */
hal_vcoreiii_init_dqs(void)604dd1033e4SGregory CLEMENT static inline int hal_vcoreiii_init_dqs(void)
605dd1033e4SGregory CLEMENT {
606dd1033e4SGregory CLEMENT #define MAX_DQS 32
607dd1033e4SGregory CLEMENT 	register u32 i, j;
608dd1033e4SGregory CLEMENT 
609dd1033e4SGregory CLEMENT 	for (i = 0; i < MAX_DQS; i++) {
610dd1033e4SGregory CLEMENT 		set_dly(0, i);	/* Byte-lane 0 */
611dd1033e4SGregory CLEMENT 		for (j = 0; j < MAX_DQS; j++) {
612dd1033e4SGregory CLEMENT 			__maybe_unused register u32  byte;
613dd1033e4SGregory CLEMENT 
614dd1033e4SGregory CLEMENT 			set_dly(1, j);	/* Byte-lane 1 */
615dd1033e4SGregory CLEMENT 			/* Reset FIFO in case any previous access failed */
616dd1033e4SGregory CLEMENT 			memphy_soft_reset();
617dd1033e4SGregory CLEMENT 			writel(readl(BASE_CFG + ICPU_MEMCTRL_STAT),
618dd1033e4SGregory CLEMENT 			       BASE_CFG + ICPU_MEMCTRL_STAT);
619dd1033e4SGregory CLEMENT 			byte = __raw_readb((void __iomem *)MSCC_DDR_TO);
620dd1033e4SGregory CLEMENT 			byte = __raw_readb((void __iomem *)(MSCC_DDR_TO + 1));
621dd1033e4SGregory CLEMENT 			if (!(readl(BASE_CFG + ICPU_MEMCTRL_STAT) &
622dd1033e4SGregory CLEMENT 			    (ICPU_MEMCTRL_STAT_RDATA_MASKED |
623dd1033e4SGregory CLEMENT 			     ICPU_MEMCTRL_STAT_RDATA_DUMMY)))
624dd1033e4SGregory CLEMENT 				return 0;
625dd1033e4SGregory CLEMENT 		}
626dd1033e4SGregory CLEMENT 	}
627dd1033e4SGregory CLEMENT 	return -1;
628dd1033e4SGregory CLEMENT }
629dd1033e4SGregory CLEMENT 
dram_check(void)630dd1033e4SGregory CLEMENT static inline int dram_check(void)
631dd1033e4SGregory CLEMENT {
632dd1033e4SGregory CLEMENT 	register u32 i;
633dd1033e4SGregory CLEMENT 
634dd1033e4SGregory CLEMENT 	for (i = 0; i < 8; i++) {
635dd1033e4SGregory CLEMENT 		__raw_writel(~i, (void __iomem *)(MSCC_DDR_TO + (i * 4)));
636dd1033e4SGregory CLEMENT 		if (__raw_readl((void __iomem *)(MSCC_DDR_TO + (i * 4))) != ~i)
637dd1033e4SGregory CLEMENT 			return 1;
638dd1033e4SGregory CLEMENT 	}
639dd1033e4SGregory CLEMENT 	return 0;
640dd1033e4SGregory CLEMENT }
6416bd8231aSGregory CLEMENT #else				/* Luton */
6426bd8231aSGregory CLEMENT 
sleep_100ns(u32 val)6436bd8231aSGregory CLEMENT static inline void sleep_100ns(u32 val)
6446bd8231aSGregory CLEMENT {
6456bd8231aSGregory CLEMENT }
6466bd8231aSGregory CLEMENT 
hal_vcoreiii_ddr_reset_assert(void)6476bd8231aSGregory CLEMENT static inline void hal_vcoreiii_ddr_reset_assert(void)
6486bd8231aSGregory CLEMENT {
6496bd8231aSGregory CLEMENT 	setbits_le32(BASE_CFG + ICPU_MEMPHY_CFG, ICPU_MEMPHY_CFG_PHY_RST);
6506bd8231aSGregory CLEMENT 	setbits_le32(BASE_CFG + ICPU_RESET, ICPU_RESET_MEM_RST_FORCE);
6516bd8231aSGregory CLEMENT }
6526bd8231aSGregory CLEMENT 
hal_vcoreiii_ddr_reset_release(void)6536bd8231aSGregory CLEMENT static inline void hal_vcoreiii_ddr_reset_release(void)
6546bd8231aSGregory CLEMENT {
6556bd8231aSGregory CLEMENT }
6566bd8231aSGregory CLEMENT 
hal_vcoreiii_ddr_failed(void)6576bd8231aSGregory CLEMENT static inline void hal_vcoreiii_ddr_failed(void)
6586bd8231aSGregory CLEMENT {
6596bd8231aSGregory CLEMENT 	register u32 memphy_cfg = readl(BASE_CFG + ICPU_MEMPHY_CFG);
6606bd8231aSGregory CLEMENT 
6616bd8231aSGregory CLEMENT 	/* Do a fifo reset and start over */
6626bd8231aSGregory CLEMENT 	writel(memphy_cfg | ICPU_MEMPHY_CFG_PHY_FIFO_RST,
6636bd8231aSGregory CLEMENT 	       BASE_CFG + ICPU_MEMPHY_CFG);
6646bd8231aSGregory CLEMENT 	writel(memphy_cfg & ~ICPU_MEMPHY_CFG_PHY_FIFO_RST,
6656bd8231aSGregory CLEMENT 	       BASE_CFG + ICPU_MEMPHY_CFG);
6666bd8231aSGregory CLEMENT 	writel(memphy_cfg | ICPU_MEMPHY_CFG_PHY_FIFO_RST,
6676bd8231aSGregory CLEMENT 	       BASE_CFG + ICPU_MEMPHY_CFG);
6686bd8231aSGregory CLEMENT }
6696bd8231aSGregory CLEMENT 
hal_vcoreiii_ddr_verified(void)6706bd8231aSGregory CLEMENT static inline void hal_vcoreiii_ddr_verified(void)
6716bd8231aSGregory CLEMENT {
6726bd8231aSGregory CLEMENT }
6736bd8231aSGregory CLEMENT 
look_for(u32 data)6746bd8231aSGregory CLEMENT static inline int look_for(u32 data)
6756bd8231aSGregory CLEMENT {
6766bd8231aSGregory CLEMENT 	register u32 byte = __raw_readb((void __iomem *)MSCC_DDR_TO);
6776bd8231aSGregory CLEMENT 
6786bd8231aSGregory CLEMENT 	if (data != byte) {
6796bd8231aSGregory CLEMENT 		if (!incr_dly(0))
6806bd8231aSGregory CLEMENT 			return DDR_TRAIN_ERROR;
6816bd8231aSGregory CLEMENT 		return DDR_TRAIN_CONTINUE;
6826bd8231aSGregory CLEMENT 	}
6836bd8231aSGregory CLEMENT 
6846bd8231aSGregory CLEMENT 	return DDR_TRAIN_OK;
6856bd8231aSGregory CLEMENT }
6866bd8231aSGregory CLEMENT 
6876bd8231aSGregory CLEMENT /* This algorithm is converted from the TCL training algorithm used
6886bd8231aSGregory CLEMENT  * during silicon simulation.
6896bd8231aSGregory CLEMENT  * NB: Assumes inlining as no stack is available!
6906bd8231aSGregory CLEMENT  */
hal_vcoreiii_train_bytelane(u32 bytelane)6916bd8231aSGregory CLEMENT static inline int hal_vcoreiii_train_bytelane(u32 bytelane)
6926bd8231aSGregory CLEMENT {
6936bd8231aSGregory CLEMENT 	register int res;
6946bd8231aSGregory CLEMENT 
6956bd8231aSGregory CLEMENT 	set_dly(bytelane, 0);	/* Start training at DQS=0 */
6966bd8231aSGregory CLEMENT 	while ((res = look_for(0xff)) == DDR_TRAIN_CONTINUE)
6976bd8231aSGregory CLEMENT 		;
6986bd8231aSGregory CLEMENT 	if (res != DDR_TRAIN_OK)
6996bd8231aSGregory CLEMENT 		return res;
7006bd8231aSGregory CLEMENT 
7016bd8231aSGregory CLEMENT 	set_dly(bytelane, 0);	/* Start training at DQS=0 */
7026bd8231aSGregory CLEMENT 	while ((res = look_for(0x00)) == DDR_TRAIN_CONTINUE)
7036bd8231aSGregory CLEMENT 
7046bd8231aSGregory CLEMENT 		;
7056bd8231aSGregory CLEMENT 
7066bd8231aSGregory CLEMENT 	if (res != DDR_TRAIN_OK)
7076bd8231aSGregory CLEMENT 		return res;
7086bd8231aSGregory CLEMENT 
7096bd8231aSGregory CLEMENT 	adjust_dly(-3);
7106bd8231aSGregory CLEMENT 
7116bd8231aSGregory CLEMENT 	return DDR_TRAIN_OK;
7126bd8231aSGregory CLEMENT }
7136bd8231aSGregory CLEMENT 
hal_vcoreiii_init_dqs(void)7146bd8231aSGregory CLEMENT static inline int hal_vcoreiii_init_dqs(void)
7156bd8231aSGregory CLEMENT {
7166bd8231aSGregory CLEMENT 	return 0;
7176bd8231aSGregory CLEMENT }
7186bd8231aSGregory CLEMENT 
dram_check(void)7196bd8231aSGregory CLEMENT static inline int dram_check(void)
7206bd8231aSGregory CLEMENT {
7216bd8231aSGregory CLEMENT 	register u32 i;
7226bd8231aSGregory CLEMENT 
7236bd8231aSGregory CLEMENT 	for (i = 0; i < 8; i++) {
7246bd8231aSGregory CLEMENT 		__raw_writel(~i, (void __iomem *)(MSCC_DDR_TO + (i * 4)));
7256bd8231aSGregory CLEMENT 
7266bd8231aSGregory CLEMENT 		if (__raw_readl((void __iomem *)(MSCC_DDR_TO + (i * 4))) != ~i)
7276bd8231aSGregory CLEMENT 			return 1;
7286bd8231aSGregory CLEMENT 	}
7296bd8231aSGregory CLEMENT 
7306bd8231aSGregory CLEMENT 	return 0;
7316bd8231aSGregory CLEMENT }
7326bd8231aSGregory CLEMENT #endif
733dd1033e4SGregory CLEMENT 
734dd1033e4SGregory CLEMENT /*
735dd1033e4SGregory CLEMENT  * NB: Called *early* to init memory controller - assumes inlining as
736dd1033e4SGregory CLEMENT  * no stack is available!
737dd1033e4SGregory CLEMENT  */
hal_vcoreiii_init_memctl(void)738dd1033e4SGregory CLEMENT static inline void hal_vcoreiii_init_memctl(void)
739dd1033e4SGregory CLEMENT {
740dd1033e4SGregory CLEMENT 	/* Ensure DDR is in reset */
741dd1033e4SGregory CLEMENT 	hal_vcoreiii_ddr_reset_assert();
742dd1033e4SGregory CLEMENT 
743dd1033e4SGregory CLEMENT 	/* Wait maybe not needed, but ... */
744dd1033e4SGregory CLEMENT 	PAUSE();
745dd1033e4SGregory CLEMENT 
746dd1033e4SGregory CLEMENT 	/* Drop sys ctl memory controller forced reset */
747dd1033e4SGregory CLEMENT 	clrbits_le32(BASE_CFG + ICPU_RESET, ICPU_RESET_MEM_RST_FORCE);
748dd1033e4SGregory CLEMENT 
749dd1033e4SGregory CLEMENT 	PAUSE();
750dd1033e4SGregory CLEMENT 
751dd1033e4SGregory CLEMENT 	/* Drop Reset, enable SSTL */
752dd1033e4SGregory CLEMENT 	writel(ICPU_MEMPHY_CFG_PHY_SSTL_ENA, BASE_CFG + ICPU_MEMPHY_CFG);
753dd1033e4SGregory CLEMENT 	PAUSE();
754dd1033e4SGregory CLEMENT 
755dd1033e4SGregory CLEMENT 	/* Start the automatic SSTL output and ODT drive-strength calibration */
756dd1033e4SGregory CLEMENT 	writel(ICPU_MEMPHY_ZCAL_ZCAL_PROG_ODT(MIPS_VCOREIII_MEMORY_SSTL_ODT) |
757dd1033e4SGregory CLEMENT 	       /* drive strength */
758dd1033e4SGregory CLEMENT 	       ICPU_MEMPHY_ZCAL_ZCAL_PROG(MIPS_VCOREIII_MEMORY_SSTL_DRIVE) |
759dd1033e4SGregory CLEMENT 	       /* Start calibration process */
760dd1033e4SGregory CLEMENT 	       ICPU_MEMPHY_ZCAL_ZCAL_ENA, BASE_CFG + ICPU_MEMPHY_ZCAL);
761dd1033e4SGregory CLEMENT 
762dd1033e4SGregory CLEMENT 	/* Wait for ZCAL to clear */
763dd1033e4SGregory CLEMENT 	while (readl(BASE_CFG + ICPU_MEMPHY_ZCAL) & ICPU_MEMPHY_ZCAL_ZCAL_ENA)
764dd1033e4SGregory CLEMENT 		;
76505512517SHoratiu Vultur #if defined(CONFIG_SOC_OCELOT) || defined(CONFIG_SOC_JR2) || \
76605512517SHoratiu Vultur 	defined(CONFIG_SOC_SERVALT)
767dd1033e4SGregory CLEMENT 	/* Check no ZCAL_ERR */
768dd1033e4SGregory CLEMENT 	if (readl(BASE_CFG + ICPU_MEMPHY_ZCAL_STAT)
769dd1033e4SGregory CLEMENT 	    & ICPU_MEMPHY_ZCAL_STAT_ZCAL_ERR)
770dd1033e4SGregory CLEMENT 		hal_vcoreiii_ddr_failed();
7716bd8231aSGregory CLEMENT #endif
772dd1033e4SGregory CLEMENT 	/* Drive CL, CK, ODT */
773dd1033e4SGregory CLEMENT 	setbits_le32(BASE_CFG + ICPU_MEMPHY_CFG, ICPU_MEMPHY_CFG_PHY_ODT_OE |
774dd1033e4SGregory CLEMENT 		     ICPU_MEMPHY_CFG_PHY_CK_OE | ICPU_MEMPHY_CFG_PHY_CL_OE);
775dd1033e4SGregory CLEMENT 
776dd1033e4SGregory CLEMENT 	/* Initialize memory controller */
777dd1033e4SGregory CLEMENT 	writel(MSCC_MEMPARM_MEMCFG, BASE_CFG + ICPU_MEMCTRL_CFG);
778dd1033e4SGregory CLEMENT 	writel(MSCC_MEMPARM_PERIOD, BASE_CFG + ICPU_MEMCTRL_REF_PERIOD);
779dd1033e4SGregory CLEMENT 
78005512517SHoratiu Vultur #if defined(CONFIG_SOC_OCELOT) || defined(CONFIG_SOC_JR2) || \
781*1895b87eSHoratiu Vultur 	defined(CONFIG_SOC_SERVALT) || defined(CONFIG_SOC_SERVAL)
782dd1033e4SGregory CLEMENT 	writel(MSCC_MEMPARM_TIMING0, BASE_CFG + ICPU_MEMCTRL_TIMING0);
7836bd8231aSGregory CLEMENT #else /* Luton */
7846bd8231aSGregory CLEMENT 	clrbits_le32(BASE_CFG + ICPU_MEMCTRL_TIMING0, ((1 << 20) - 1));
7856bd8231aSGregory CLEMENT 	setbits_le32(BASE_CFG + ICPU_MEMCTRL_TIMING0, MSCC_MEMPARM_TIMING0);
7866bd8231aSGregory CLEMENT #endif
787dd1033e4SGregory CLEMENT 
788dd1033e4SGregory CLEMENT 	writel(MSCC_MEMPARM_TIMING1, BASE_CFG + ICPU_MEMCTRL_TIMING1);
789dd1033e4SGregory CLEMENT 	writel(MSCC_MEMPARM_TIMING2, BASE_CFG + ICPU_MEMCTRL_TIMING2);
790dd1033e4SGregory CLEMENT 	writel(MSCC_MEMPARM_TIMING3, BASE_CFG + ICPU_MEMCTRL_TIMING3);
791dd1033e4SGregory CLEMENT 	writel(MSCC_MEMPARM_MR0, BASE_CFG + ICPU_MEMCTRL_MR0_VAL);
792dd1033e4SGregory CLEMENT 	writel(MSCC_MEMPARM_MR1, BASE_CFG + ICPU_MEMCTRL_MR1_VAL);
793dd1033e4SGregory CLEMENT 	writel(MSCC_MEMPARM_MR2, BASE_CFG + ICPU_MEMCTRL_MR2_VAL);
794dd1033e4SGregory CLEMENT 	writel(MSCC_MEMPARM_MR3, BASE_CFG + ICPU_MEMCTRL_MR3_VAL);
795dd1033e4SGregory CLEMENT 
796*1895b87eSHoratiu Vultur #if defined(CONFIG_SOC_OCELOT) || defined(CONFIG_SOC_SERVAL)
797dd1033e4SGregory CLEMENT 	/* Termination setup - enable ODT */
798dd1033e4SGregory CLEMENT 	writel(ICPU_MEMCTRL_TERMRES_CTRL_LOCAL_ODT_RD_ENA |
799dd1033e4SGregory CLEMENT 	       /* Assert ODT0 for any write */
800dd1033e4SGregory CLEMENT 	       ICPU_MEMCTRL_TERMRES_CTRL_ODT_WR_ENA(3),
801dd1033e4SGregory CLEMENT 	       BASE_CFG + ICPU_MEMCTRL_TERMRES_CTRL);
802dd1033e4SGregory CLEMENT 
803dd1033e4SGregory CLEMENT 	/* Release Reset from DDR */
804*1895b87eSHoratiu Vultur #if defined(CONFIG_SOC_OCELOT)
805dd1033e4SGregory CLEMENT 	hal_vcoreiii_ddr_reset_release();
806*1895b87eSHoratiu Vultur #endif
807dd1033e4SGregory CLEMENT 
808dd1033e4SGregory CLEMENT 	writel(readl(BASE_CFG + ICPU_GPR(7)) + 1, BASE_CFG + ICPU_GPR(7));
80905512517SHoratiu Vultur #elif defined(CONFIG_SOC_JR2) || defined(CONFIG_SOC_SERVALT)
810e7a0de2cSHoratiu Vultur 	writel(ICPU_MEMCTRL_TERMRES_CTRL_ODT_WR_ENA(3),
811e7a0de2cSHoratiu Vultur 	       BASE_CFG + ICPU_MEMCTRL_TERMRES_CTRL);
8126bd8231aSGregory CLEMENT #else				/* Luton */
8136bd8231aSGregory CLEMENT 	/* Termination setup - disable ODT */
8146bd8231aSGregory CLEMENT 	writel(0, BASE_CFG + ICPU_MEMCTRL_TERMRES_CTRL);
8156bd8231aSGregory CLEMENT 
8166bd8231aSGregory CLEMENT #endif
817dd1033e4SGregory CLEMENT }
818dd1033e4SGregory CLEMENT 
hal_vcoreiii_wait_memctl(void)819dd1033e4SGregory CLEMENT static inline void hal_vcoreiii_wait_memctl(void)
820dd1033e4SGregory CLEMENT {
821dd1033e4SGregory CLEMENT 	/* Now, rip it! */
822dd1033e4SGregory CLEMENT 	writel(ICPU_MEMCTRL_CTRL_INITIALIZE, BASE_CFG + ICPU_MEMCTRL_CTRL);
823dd1033e4SGregory CLEMENT 
824dd1033e4SGregory CLEMENT 	while (!(readl(BASE_CFG + ICPU_MEMCTRL_STAT)
825dd1033e4SGregory CLEMENT 		 & ICPU_MEMCTRL_STAT_INIT_DONE))
826dd1033e4SGregory CLEMENT 		;
827dd1033e4SGregory CLEMENT 
828dd1033e4SGregory CLEMENT 	/* Settle...? */
829dd1033e4SGregory CLEMENT 	sleep_100ns(10000);
83005512517SHoratiu Vultur #if defined(CONFIG_SOC_OCELOT) || defined(CONFIG_SOC_JR2) || \
831*1895b87eSHoratiu Vultur 	defined(CONFIG_SOC_SERVALT) || defined(CONFIG_SOC_SERVAL)
832dd1033e4SGregory CLEMENT 	/* Establish data contents in DDR RAM for training */
833dd1033e4SGregory CLEMENT 
834dd1033e4SGregory CLEMENT 	__raw_writel(0xcacafefe, ((void __iomem *)MSCC_DDR_TO));
835dd1033e4SGregory CLEMENT 	__raw_writel(0x22221111, ((void __iomem *)MSCC_DDR_TO + 0x4));
836dd1033e4SGregory CLEMENT 	__raw_writel(0x44443333, ((void __iomem *)MSCC_DDR_TO + 0x8));
837dd1033e4SGregory CLEMENT 	__raw_writel(0x66665555, ((void __iomem *)MSCC_DDR_TO + 0xC));
838dd1033e4SGregory CLEMENT 	__raw_writel(0x88887777, ((void __iomem *)MSCC_DDR_TO + 0x10));
839dd1033e4SGregory CLEMENT 	__raw_writel(0xaaaa9999, ((void __iomem *)MSCC_DDR_TO + 0x14));
840dd1033e4SGregory CLEMENT 	__raw_writel(0xccccbbbb, ((void __iomem *)MSCC_DDR_TO + 0x18));
841dd1033e4SGregory CLEMENT 	__raw_writel(0xeeeedddd, ((void __iomem *)MSCC_DDR_TO + 0x1C));
8426bd8231aSGregory CLEMENT #else
8436bd8231aSGregory CLEMENT 	__raw_writel(0xff, ((void __iomem *)MSCC_DDR_TO));
8446bd8231aSGregory CLEMENT #endif
845dd1033e4SGregory CLEMENT }
846dd1033e4SGregory CLEMENT #endif				/* __ASM_MACH_DDR_H */
847