blob: fb2230430c6247f8c3bd85befde1485053e557e2 [file] [log] [blame]
Rosy Songbd905c32019-03-16 09:24:44 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2019 Rosy Song <rosysong@rosinson.com>
4 *
5 * Based on QSDK
6 */
7
8#include <common.h>
9#include <asm/io.h>
10#include <asm/addrspace.h>
11#include <asm/types.h>
12#include <mach/ar71xx_regs.h>
13#include <mach/ath79.h>
14
15#define DDR_FSM_WAIT_CTRL_VAL 0xa12
16#define DDR_CTL_CONFIG_SRAM_TSEL_LSB 30
17#define DDR_CTL_CONFIG_SRAM_TSEL_MASK 0xc0000000
18#define DDR_CTL_CONFIG_SRAM_TSEL_SET(x) \
19 (((x) << DDR_CTL_CONFIG_SRAM_TSEL_LSB) & DDR_CTL_CONFIG_SRAM_TSEL_MASK)
20#define DDR_CTL_CONFIG_GE0_SRAM_SYNC_LSB 20
21#define DDR_CTL_CONFIG_GE0_SRAM_SYNC_MASK 0x00100000
22#define DDR_CTL_CONFIG_GE0_SRAM_SYNC_SET(x) \
23 (((x) << DDR_CTL_CONFIG_GE0_SRAM_SYNC_LSB) & DDR_CTL_CONFIG_GE0_SRAM_SYNC_MASK)
24#define DDR_CTL_CONFIG_GE1_SRAM_SYNC_LSB 19
25#define DDR_CTL_CONFIG_GE1_SRAM_SYNC_MASK 0x00080000
26#define DDR_CTL_CONFIG_GE1_SRAM_SYNC_SET(x) \
27 (((x) << DDR_CTL_CONFIG_GE1_SRAM_SYNC_LSB) & DDR_CTL_CONFIG_GE1_SRAM_SYNC_MASK)
28#define DDR_CTL_CONFIG_USB_SRAM_SYNC_LSB 18
29#define DDR_CTL_CONFIG_USB_SRAM_SYNC_MASK 0x00040000
30#define DDR_CTL_CONFIG_USB_SRAM_SYNC_SET(x) \
31 (((x) << DDR_CTL_CONFIG_USB_SRAM_SYNC_LSB) & DDR_CTL_CONFIG_USB_SRAM_SYNC_MASK)
32#define DDR_CTL_CONFIG_PCIE_SRAM_SYNC_LSB 17
33#define DDR_CTL_CONFIG_PCIE_SRAM_SYNC_MASK 0x00020000
34#define DDR_CTL_CONFIG_PCIE_SRAM_SYNC_SET(x) \
35 (((x) << DDR_CTL_CONFIG_PCIE_SRAM_SYNC_LSB) & DDR_CTL_CONFIG_PCIE_SRAM_SYNC_MASK)
36#define DDR_CTL_CONFIG_WMAC_SRAM_SYNC_LSB 16
37#define DDR_CTL_CONFIG_WMAC_SRAM_SYNC_MASK 0x00010000
38#define DDR_CTL_CONFIG_WMAC_SRAM_SYNC_SET(x) \
39 (((x) << DDR_CTL_CONFIG_WMAC_SRAM_SYNC_LSB) & DDR_CTL_CONFIG_WMAC_SRAM_SYNC_MASK)
40#define DDR_CTL_CONFIG_MISC_SRC1_SRAM_SYNC_LSB 15
41#define DDR_CTL_CONFIG_MISC_SRC1_SRAM_SYNC_MASK 0x00008000
42#define DDR_CTL_CONFIG_MISC_SRC1_SRAM_SYNC_SET(x) \
43 (((x) << DDR_CTL_CONFIG_MISC_SRC1_SRAM_SYNC_LSB) & DDR_CTL_CONFIG_MISC_SRC1_SRAM_SYNC_MASK)
44#define DDR_CTL_CONFIG_MISC_SRC2_SRAM_SYNC_LSB 14
45#define DDR_CTL_CONFIG_MISC_SRC2_SRAM_SYNC_MASK 0x00004000
46#define DDR_CTL_CONFIG_MISC_SRC2_SRAM_SYNC_SET(x) \
47 (((x) << DDR_CTL_CONFIG_MISC_SRC2_SRAM_SYNC_LSB) & DDR_CTL_CONFIG_MISC_SRC2_SRAM_SYNC_MASK)
48#define DDR_CTL_CONFIG_PAD_DDR2_SEL_LSB 6
49#define DDR_CTL_CONFIG_PAD_DDR2_SEL_MASK 0x00000040
50#define DDR_CTL_CONFIG_PAD_DDR2_SEL_SET(x) \
51 (((x) << DDR_CTL_CONFIG_PAD_DDR2_SEL_LSB) & DDR_CTL_CONFIG_PAD_DDR2_SEL_MASK)
52#define DDR_CTL_CONFIG_CPU_DDR_SYNC_LSB 2
53#define DDR_CTL_CONFIG_CPU_DDR_SYNC_MASK 0x00000004
54#define DDR_CTL_CONFIG_CPU_DDR_SYNC_SET(x) \
55 (((x) << DDR_CTL_CONFIG_CPU_DDR_SYNC_LSB) & DDR_CTL_CONFIG_CPU_DDR_SYNC_MASK)
56#define DDR_CTL_CONFIG_HALF_WIDTH_LSB 1
57#define DDR_CTL_CONFIG_HALF_WIDTH_MASK 0x00000002
58#define DDR_CTL_CONFIG_HALF_WIDTH_SET(x) \
59 (((x) << DDR_CTL_CONFIG_HALF_WIDTH_LSB) & DDR_CTL_CONFIG_HALF_WIDTH_MASK)
60#define DDR_CONFIG_CAS_LATENCY_MSB_LSB 31
61#define DDR_CONFIG_CAS_LATENCY_MSB_MASK 0x80000000
62#define DDR_CONFIG_CAS_LATENCY_MSB_SET(x) \
63 (((x) << DDR_CONFIG_CAS_LATENCY_MSB_LSB) & DDR_CONFIG_CAS_LATENCY_MSB_MASK)
64#define DDR_CONFIG_OPEN_PAGE_LSB 30
65#define DDR_CONFIG_OPEN_PAGE_MASK 0x40000000
66#define DDR_CONFIG_OPEN_PAGE_SET(x) \
67 (((x) << DDR_CONFIG_OPEN_PAGE_LSB) & DDR_CONFIG_OPEN_PAGE_MASK)
68#define DDR_CONFIG_CAS_LATENCY_LSB 27
69#define DDR_CONFIG_CAS_LATENCY_MASK 0x38000000
70#define DDR_CONFIG_CAS_LATENCY_SET(x) \
71 (((x) << DDR_CONFIG_CAS_LATENCY_LSB) & DDR_CONFIG_CAS_LATENCY_MASK)
72#define DDR_CONFIG_TMRD_LSB 23
73#define DDR_CONFIG_TMRD_MASK 0x07800000
74#define DDR_CONFIG_TMRD_SET(x) \
75 (((x) << DDR_CONFIG_TMRD_LSB) & DDR_CONFIG_TMRD_MASK)
76#define DDR_CONFIG_TRFC_LSB 17
77#define DDR_CONFIG_TRFC_MASK 0x007e0000
78#define DDR_CONFIG_TRFC_SET(x) \
79 (((x) << DDR_CONFIG_TRFC_LSB) & DDR_CONFIG_TRFC_MASK)
80#define DDR_CONFIG_TRRD_LSB 13
81#define DDR_CONFIG_TRRD_MASK 0x0001e000
82#define DDR_CONFIG_TRRD_SET(x) \
83 (((x) << DDR_CONFIG_TRRD_LSB) & DDR_CONFIG_TRRD_MASK)
84#define DDR_CONFIG_TRP_LSB 9
85#define DDR_CONFIG_TRP_MASK 0x00001e00
86#define DDR_CONFIG_TRP_SET(x) \
87 (((x) << DDR_CONFIG_TRP_LSB) & DDR_CONFIG_TRP_MASK)
88#define DDR_CONFIG_TRCD_LSB 5
89#define DDR_CONFIG_TRCD_MASK 0x000001e0
90#define DDR_CONFIG_TRCD_SET(x) \
91 (((x) << DDR_CONFIG_TRCD_LSB) & DDR_CONFIG_TRCD_MASK)
92#define DDR_CONFIG_TRAS_LSB 0
93#define DDR_CONFIG_TRAS_MASK 0x0000001f
94#define DDR_CONFIG_TRAS_SET(x) \
95 (((x) << DDR_CONFIG_TRAS_LSB) & DDR_CONFIG_TRAS_MASK)
96#define DDR_CONFIG2_HALF_WIDTH_LOW_LSB 31
97#define DDR_CONFIG2_HALF_WIDTH_LOW_MASK 0x80000000
98#define DDR_CONFIG2_HALF_WIDTH_LOW_SET(x) \
99 (((x) << DDR_CONFIG2_HALF_WIDTH_LOW_LSB) & DDR_CONFIG2_HALF_WIDTH_LOW_MASK)
100#define DDR_CONFIG2_SWAP_A26_A27_LSB 30
101#define DDR_CONFIG2_SWAP_A26_A27_MASK 0x40000000
102#define DDR_CONFIG2_SWAP_A26_A27_SET(x) \
103 (((x) << DDR_CONFIG2_SWAP_A26_A27_LSB) & DDR_CONFIG2_SWAP_A26_A27_MASK)
104#define DDR_CONFIG2_GATE_OPEN_LATENCY_LSB 26
105#define DDR_CONFIG2_GATE_OPEN_LATENCY_MASK 0x3c000000
106#define DDR_CONFIG2_GATE_OPEN_LATENCY_SET(x) \
107 (((x) << DDR_CONFIG2_GATE_OPEN_LATENCY_LSB) & DDR_CONFIG2_GATE_OPEN_LATENCY_MASK)
108#define DDR_CONFIG2_TWTR_LSB 21
109#define DDR_CONFIG2_TWTR_MASK 0x03e00000
110#define DDR_CONFIG2_TWTR_SET(x) \
111 (((x) << DDR_CONFIG2_TWTR_LSB) & DDR_CONFIG2_TWTR_MASK)
112#define DDR_CONFIG2_TRTP_LSB 17
113#define DDR_CONFIG2_TRTP_MASK 0x001e0000
114#define DDR_CONFIG2_TRTP_SET(x) \
115 (((x) << DDR_CONFIG2_TRTP_LSB) & DDR_CONFIG2_TRTP_MASK)
116#define DDR_CONFIG2_TRTW_LSB 12
117#define DDR_CONFIG2_TRTW_MASK 0x0001f000
118#define DDR_CONFIG2_TRTW_SET(x) \
119 (((x) << DDR_CONFIG2_TRTW_LSB) & DDR_CONFIG2_TRTW_MASK)
120#define DDR_CONFIG2_TWR_LSB 8
121#define DDR_CONFIG2_TWR_MASK 0x00000f00
122#define DDR_CONFIG2_TWR_SET(x) \
123 (((x) << DDR_CONFIG2_TWR_LSB) & DDR_CONFIG2_TWR_MASK)
124#define DDR_CONFIG2_CKE_LSB 7
125#define DDR_CONFIG2_CKE_MASK 0x00000080
126#define DDR_CONFIG2_CKE_SET(x) \
127 (((x) << DDR_CONFIG2_CKE_LSB) & DDR_CONFIG2_CKE_MASK)
128#define DDR_CONFIG2_CNTL_OE_EN_LSB 5
129#define DDR_CONFIG2_CNTL_OE_EN_MASK 0x00000020
130#define DDR_CONFIG2_CNTL_OE_EN_SET(x) \
131 (((x) << DDR_CONFIG2_CNTL_OE_EN_LSB) & DDR_CONFIG2_CNTL_OE_EN_MASK)
132#define DDR_CONFIG2_BURST_LENGTH_LSB 0
133#define DDR_CONFIG2_BURST_LENGTH_MASK 0x0000000f
134#define DDR_CONFIG2_BURST_LENGTH_SET(x) \
135 (((x) << DDR_CONFIG2_BURST_LENGTH_LSB) & DDR_CONFIG2_BURST_LENGTH_MASK)
136#define RST_BOOTSTRAP_ADDRESS 0x180600b0
137#define PMU2_SWREGMSB_LSB 22
138#define PMU2_SWREGMSB_MASK 0xffc00000
139#define PMU2_SWREGMSB_SET(x) \
140 (((x) << PMU2_SWREGMSB_LSB) & PMU2_SWREGMSB_MASK)
141#define PMU2_PGM_LSB 21
142#define PMU2_PGM_MASK 0x00200000
143#define PMU2_PGM_SET(x) \
144 (((x) << PMU2_PGM_LSB) & PMU2_PGM_MASK)
145
146#define CPU_DDR_SYNC_MODE DDR_CTL_CONFIG_CPU_DDR_SYNC_SET(0)
147
148/*
149* DDR2 DDR1
150* 0x40c3 25MHz 0x4186 25Mhz
151* 0x4138 40MHz 0x4270 40Mhz
152*/
153#define CFG_DDR2_REFRESH_VAL 0x40c3
154#define CFG_DDR2_CONFIG_VAL DDR_CONFIG_CAS_LATENCY_MSB_SET(0x1) | \
155 DDR_CONFIG_OPEN_PAGE_SET(0x1) | DDR_CONFIG_CAS_LATENCY_SET(0x4) | \
156 DDR_CONFIG_TMRD_SET(0x6) | DDR_CONFIG_TRFC_SET(0x16) | \
157 DDR_CONFIG_TRRD_SET(0x7) | DDR_CONFIG_TRP_SET(0xb) | \
158 DDR_CONFIG_TRCD_SET(0xb) | DDR_CONFIG_TRAS_SET(0)
159#define CFG_DDR2_CONFIG2_VAL DDR_CONFIG2_HALF_WIDTH_LOW_SET(0x1) | \
160 DDR_CONFIG2_SWAP_A26_A27_SET(0x0) | DDR_CONFIG2_GATE_OPEN_LATENCY_SET(0xa) | \
161 DDR_CONFIG2_TWTR_SET(0x16) | DDR_CONFIG2_TRTP_SET(0xa) | \
162 DDR_CONFIG2_TRTW_SET(0xe) | DDR_CONFIG2_TWR_SET(0x2) | \
163 DDR_CONFIG2_CKE_SET(0x1) | DDR_CONFIG2_CNTL_OE_EN_SET(0x1) | \
164 DDR_CONFIG2_BURST_LENGTH_SET(0x8)
165
166#define CFG_DDR2_CONFIG3_VAL 0x0000000e
167#define CFG_DDR2_EXT_MODE_VAL1 0x782
168#define CFG_DDR2_EXT_MODE_VAL2 0x402
169#define CFG_DDR2_MODE_VAL_INIT 0xb53
170#define CFG_DDR2_MODE_VAL 0xa53
171#define CFG_DDR2_TAP_VAL 0x10
172#define CFG_DDR2_EN_TWL_VAL 0x00001e91
173#define CFG_DDR2_RD_DATA_THIS_CYCLE_VAL_16 0xffff
174
175#define CFG_DDR_CTL_CONFIG DDR_CTL_CONFIG_SRAM_TSEL_SET(0x1) | \
176 DDR_CTL_CONFIG_GE0_SRAM_SYNC_SET(0x1) | \
177 DDR_CTL_CONFIG_GE1_SRAM_SYNC_SET(0x1) | \
178 DDR_CTL_CONFIG_USB_SRAM_SYNC_SET(0x1) | \
179 DDR_CTL_CONFIG_PCIE_SRAM_SYNC_SET(0x1) | \
180 DDR_CTL_CONFIG_WMAC_SRAM_SYNC_SET(0x1) | \
181 DDR_CTL_CONFIG_MISC_SRC1_SRAM_SYNC_SET(0x1) | \
182 DDR_CTL_CONFIG_MISC_SRC2_SRAM_SYNC_SET(0x1)
183
184DECLARE_GLOBAL_DATA_PTR;
185
186void qca956x_ddr_init(void)
187{
188 u32 ddr_config, ddr_config2, ddr_config3, mod_val, \
189 mod_val_init, cycle_val, tap_val, ctl_config;
190 void __iomem *ddr_regs = map_physmem(AR71XX_DDR_CTRL_BASE, AR71XX_DDR_CTRL_SIZE,
191 MAP_NOCACHE);
192 void __iomem *srif_regs = map_physmem(QCA956X_SRIF_BASE, QCA956X_SRIF_SIZE,
193 MAP_NOCACHE);
194
195 ddr_config = CFG_DDR2_CONFIG_VAL;
196 ddr_config2 = CFG_DDR2_CONFIG2_VAL;
197 ddr_config3 = CFG_DDR2_CONFIG3_VAL;
198 mod_val_init = CFG_DDR2_MODE_VAL_INIT;
199 mod_val = CFG_DDR2_MODE_VAL;
200 tap_val = CFG_DDR2_TAP_VAL;
201 cycle_val = CFG_DDR2_RD_DATA_THIS_CYCLE_VAL_16;
202 ctl_config = CFG_DDR_CTL_CONFIG | DDR_CTL_CONFIG_PAD_DDR2_SEL_SET(0x1) |
203 DDR_CTL_CONFIG_HALF_WIDTH_SET(0x1) | CPU_DDR_SYNC_MODE;
204
205 writel(0x10, ddr_regs + AR71XX_DDR_REG_CONTROL);
206 udelay(10);
207
208 writel(0x20, ddr_regs + AR71XX_DDR_REG_CONTROL);
209 udelay(10);
210
211 writel(ctl_config, ddr_regs + QCA956X_DDR_REG_CTL_CONF);
212 udelay(10);
213
214 writel(cycle_val, ddr_regs + AR71XX_DDR_REG_RD_CYCLE);
215 udelay(100);
216
217 writel(0x74444444, ddr_regs + QCA956X_DDR_REG_BURST);
218 udelay(100);
219
220 writel(0x44444444, ddr_regs + QCA956X_DDR_REG_BURST2);
221 udelay(100);
222
223 writel(DDR_FSM_WAIT_CTRL_VAL, ddr_regs + QCA956X_DDR_REG_FSM_WAIT_CTRL);
224 udelay(100);
225
226 writel(0xfffff, ddr_regs + QCA956X_DDR_REG_TIMEOUT_MAX);
227 udelay(100);
228
229 writel(ddr_config, ddr_regs + AR71XX_DDR_REG_CONFIG);
230 udelay(100);
231
232 writel(ddr_config2, ddr_regs + AR71XX_DDR_REG_CONFIG2);
233 udelay(100);
234
235 writel(ddr_config3, ddr_regs + QCA956X_DDR_REG_DDR3_CONFIG);
236 udelay(100);
237
238 writel(CFG_DDR2_EN_TWL_VAL, ddr_regs + QCA956X_DDR_REG_DDR2_CONFIG);
239 udelay(100);
240
241 writel(ddr_config2 | 0x80, ddr_regs + AR71XX_DDR_REG_CONFIG2); /* CKE Enable */
242 udelay(100);
243
244 writel(0x8, ddr_regs + AR71XX_DDR_REG_CONTROL); /* Precharge */
245 udelay(10);
246
247 writel(0, ddr_regs + QCA956X_DDR_REG_DDR2_EMR2);
248 writel(0x10, ddr_regs + AR71XX_DDR_REG_CONTROL); /* EMR2 */
249 udelay(10);
250
251 writel(0, ddr_regs + QCA956X_DDR_REG_DDR2_EMR3);
252 writel(0x20, ddr_regs + AR71XX_DDR_REG_CONTROL); /* EMR3 */
253 udelay(10);
254
255 /* EMR DLL enable, Reduced Driver Impedance control, Differential DQS disabled */
256 writel(CFG_DDR2_EXT_MODE_VAL2, ddr_regs + AR71XX_DDR_REG_EMR);
257 udelay(100);
258
259 writel(0x2, ddr_regs + AR71XX_DDR_REG_CONTROL); /* EMR write */
260 udelay(10);
261
262 writel(mod_val_init, ddr_regs + AR71XX_DDR_REG_MODE);
263 udelay(1000);
264
265 writel(0x1, ddr_regs + AR71XX_DDR_REG_CONTROL); /* MR Write */
266 udelay(10);
267
268 writel(0x8, ddr_regs + AR71XX_DDR_REG_CONTROL); /* Precharge */
269 udelay(10);
270
271 writel(0x4, ddr_regs + AR71XX_DDR_REG_CONTROL); /* Auto Refresh */
272 udelay(10);
273
274 writel(0x4, ddr_regs + AR71XX_DDR_REG_CONTROL); /* Auto Refresh */
275 udelay(10);
276
277 /* Issue MRS to remove DLL out-of-reset */
278 writel(mod_val, ddr_regs + AR71XX_DDR_REG_MODE);
279 udelay(100);
280
281 writel(0x1, ddr_regs + AR71XX_DDR_REG_CONTROL); /* MR write */
282 udelay(100);
283
284 writel(CFG_DDR2_EXT_MODE_VAL1, ddr_regs + AR71XX_DDR_REG_EMR);
285 udelay(100);
286
287 writel(0x2, ddr_regs + AR71XX_DDR_REG_CONTROL); /* EMR write */
288 udelay(100);
289
290 writel(CFG_DDR2_EXT_MODE_VAL2, ddr_regs + AR71XX_DDR_REG_EMR);
291 udelay(100);
292
293 writel(0x2, ddr_regs + AR71XX_DDR_REG_CONTROL); /* EMR write */
294 udelay(100);
295
296 writel(CFG_DDR2_REFRESH_VAL, ddr_regs + AR71XX_DDR_REG_REFRESH);
297 udelay(100);
298
299 writel(tap_val, ddr_regs + AR71XX_DDR_REG_TAP_CTRL0);
300 writel(tap_val, ddr_regs + AR71XX_DDR_REG_TAP_CTRL1);
301 writel(tap_val, ddr_regs + QCA956X_DDR_REG_TAP_CTRL2);
302 writel(tap_val, ddr_regs + QCA956X_DDR_REG_TAP_CTRL3);
303
304 writel(0x633c8176, srif_regs + QCA956X_SRIF_PMU1_REG);
305 /* Set DDR2 Voltage to 1.8 volts */
306 writel(PMU2_SWREGMSB_SET(0x40) | PMU2_PGM_SET(0x1),
307 srif_regs + QCA956X_SRIF_PMU2_REG);
308}