]>
Commit | Line | Data |
---|---|---|
1 | // SPDX-License-Identifier: GPL-2.0+ | |
2 | /* | |
3 | * (C) Copyright 2009 | |
4 | * Marvell Semiconductor <www.marvell.com> | |
5 | * Written-by: Prafulla Wadaskar <[email protected]> | |
6 | */ | |
7 | ||
8 | #include <config.h> | |
9 | #include <init.h> | |
10 | #include <asm/global_data.h> | |
11 | #include <asm/io.h> | |
12 | #include <asm/arch/cpu.h> | |
13 | #include <asm/arch/soc.h> | |
14 | ||
15 | #if defined(CONFIG_ARCH_MVEBU) | |
16 | /* Use common XOR definitions for A3x and AXP */ | |
17 | #include "../../../drivers/ddr/marvell/axp/xor.h" | |
18 | #include "../../../drivers/ddr/marvell/axp/xor_regs.h" | |
19 | #endif | |
20 | ||
21 | DECLARE_GLOBAL_DATA_PTR; | |
22 | ||
23 | struct sdram_bank { | |
24 | u32 win_bar; | |
25 | u32 win_sz; | |
26 | }; | |
27 | ||
28 | struct sdram_addr_dec { | |
29 | struct sdram_bank sdram_bank[4]; | |
30 | }; | |
31 | ||
32 | #define REG_CPUCS_WIN_ENABLE (1 << 0) | |
33 | #define REG_CPUCS_WIN_WR_PROTECT (1 << 1) | |
34 | #define REG_CPUCS_WIN_WIN0_CS(x) (((x) & 0x3) << 2) | |
35 | #define REG_CPUCS_WIN_SIZE(x) (((x) & 0xff) << 24) | |
36 | ||
37 | #ifndef MVEBU_SDRAM_SIZE_MAX | |
38 | #define MVEBU_SDRAM_SIZE_MAX 0xc0000000 | |
39 | #endif | |
40 | ||
41 | #define SCRUB_MAGIC 0xbeefdead | |
42 | ||
43 | #define SCRB_XOR_UNIT 0 | |
44 | #define SCRB_XOR_CHAN 1 | |
45 | #define SCRB_XOR_WIN 0 | |
46 | ||
47 | #define XEBARX_BASE_OFFS 16 | |
48 | ||
49 | /* | |
50 | * mvebu_sdram_bar - reads SDRAM Base Address Register | |
51 | */ | |
52 | u32 mvebu_sdram_bar(enum memory_bank bank) | |
53 | { | |
54 | struct sdram_addr_dec *base = | |
55 | (struct sdram_addr_dec *)MVEBU_SDRAM_BASE; | |
56 | u32 result = 0; | |
57 | u32 enable = 0x01 & readl(&base->sdram_bank[bank].win_sz); | |
58 | ||
59 | if ((!enable) || (bank > BANK3)) | |
60 | return 0; | |
61 | ||
62 | result = readl(&base->sdram_bank[bank].win_bar); | |
63 | return result; | |
64 | } | |
65 | ||
66 | /* | |
67 | * mvebu_sdram_bs_set - writes SDRAM Bank size | |
68 | */ | |
69 | static void mvebu_sdram_bs_set(enum memory_bank bank, u32 size) | |
70 | { | |
71 | struct sdram_addr_dec *base = | |
72 | (struct sdram_addr_dec *)MVEBU_SDRAM_BASE; | |
73 | /* Read current register value */ | |
74 | u32 reg = readl(&base->sdram_bank[bank].win_sz); | |
75 | ||
76 | /* Clear window size */ | |
77 | reg &= ~REG_CPUCS_WIN_SIZE(0xFF); | |
78 | ||
79 | /* Set new window size */ | |
80 | reg |= REG_CPUCS_WIN_SIZE((size - 1) >> 24); | |
81 | ||
82 | writel(reg, &base->sdram_bank[bank].win_sz); | |
83 | } | |
84 | ||
85 | /* | |
86 | * mvebu_sdram_bs - reads SDRAM Bank size | |
87 | */ | |
88 | u32 mvebu_sdram_bs(enum memory_bank bank) | |
89 | { | |
90 | struct sdram_addr_dec *base = | |
91 | (struct sdram_addr_dec *)MVEBU_SDRAM_BASE; | |
92 | u32 result = 0; | |
93 | u32 enable = 0x01 & readl(&base->sdram_bank[bank].win_sz); | |
94 | ||
95 | if ((!enable) || (bank > BANK3)) | |
96 | return 0; | |
97 | result = 0xff000000 & readl(&base->sdram_bank[bank].win_sz); | |
98 | result += 0x01000000; | |
99 | return result; | |
100 | } | |
101 | ||
102 | void mvebu_sdram_size_adjust(enum memory_bank bank) | |
103 | { | |
104 | u32 size; | |
105 | ||
106 | /* probe currently equipped RAM size */ | |
107 | size = get_ram_size((void *)mvebu_sdram_bar(bank), | |
108 | mvebu_sdram_bs(bank)); | |
109 | ||
110 | /* adjust SDRAM window size accordingly */ | |
111 | mvebu_sdram_bs_set(bank, size); | |
112 | } | |
113 | ||
114 | #if defined(CONFIG_ARCH_MVEBU) | |
115 | static u32 xor_ctrl_save; | |
116 | static u32 xor_base_save; | |
117 | static u32 xor_mask_save; | |
118 | ||
119 | static void mv_xor_init2(u32 cs) | |
120 | { | |
121 | u32 reg, base, size, base2; | |
122 | u32 bank_attr[4] = { 0xe00, 0xd00, 0xb00, 0x700 }; | |
123 | ||
124 | xor_ctrl_save = reg_read(XOR_WINDOW_CTRL_REG(SCRB_XOR_UNIT, | |
125 | SCRB_XOR_CHAN)); | |
126 | xor_base_save = reg_read(XOR_BASE_ADDR_REG(SCRB_XOR_UNIT, | |
127 | SCRB_XOR_WIN)); | |
128 | xor_mask_save = reg_read(XOR_SIZE_MASK_REG(SCRB_XOR_UNIT, | |
129 | SCRB_XOR_WIN)); | |
130 | ||
131 | /* Enable Window x for each CS */ | |
132 | reg = 0x1; | |
133 | reg |= (0x3 << 16); | |
134 | reg_write(XOR_WINDOW_CTRL_REG(SCRB_XOR_UNIT, SCRB_XOR_CHAN), reg); | |
135 | ||
136 | base = 0; | |
137 | size = mvebu_sdram_bs(cs) - 1; | |
138 | if (size) { | |
139 | base2 = ((base / (64 << 10)) << XEBARX_BASE_OFFS) | | |
140 | bank_attr[cs]; | |
141 | reg_write(XOR_BASE_ADDR_REG(SCRB_XOR_UNIT, SCRB_XOR_WIN), | |
142 | base2); | |
143 | ||
144 | base += size + 1; | |
145 | size = (size / (64 << 10)) << 16; | |
146 | /* Window x - size - 256 MB */ | |
147 | reg_write(XOR_SIZE_MASK_REG(SCRB_XOR_UNIT, SCRB_XOR_WIN), size); | |
148 | } | |
149 | ||
150 | mv_xor_hal_init(0); | |
151 | ||
152 | return; | |
153 | } | |
154 | ||
155 | static void mv_xor_finish2(void) | |
156 | { | |
157 | reg_write(XOR_WINDOW_CTRL_REG(SCRB_XOR_UNIT, SCRB_XOR_CHAN), | |
158 | xor_ctrl_save); | |
159 | reg_write(XOR_BASE_ADDR_REG(SCRB_XOR_UNIT, SCRB_XOR_WIN), | |
160 | xor_base_save); | |
161 | reg_write(XOR_SIZE_MASK_REG(SCRB_XOR_UNIT, SCRB_XOR_WIN), | |
162 | xor_mask_save); | |
163 | } | |
164 | ||
165 | static void dram_ecc_scrubbing(void) | |
166 | { | |
167 | int cs; | |
168 | u32 size, temp; | |
169 | u32 total_mem = 0; | |
170 | u64 total; | |
171 | u32 start_addr; | |
172 | ||
173 | /* | |
174 | * The DDR training code from the bin_hdr / SPL already | |
175 | * scrubbed the DDR till 0x1000000. And the main U-Boot | |
176 | * is loaded to an address < 0x1000000. So we need to | |
177 | * skip this range to not re-scrub this area again. | |
178 | */ | |
179 | temp = reg_read(REG_SDRAM_CONFIG_ADDR); | |
180 | temp |= (1 << REG_SDRAM_CONFIG_IERR_OFFS); | |
181 | reg_write(REG_SDRAM_CONFIG_ADDR, temp); | |
182 | ||
183 | for (cs = 0; cs < CONFIG_NR_DRAM_BANKS; cs++) { | |
184 | size = mvebu_sdram_bs(cs); | |
185 | if (size == 0) | |
186 | continue; | |
187 | ||
188 | total = (u64)size; | |
189 | total_mem += (u32)(total / (1 << 30)); | |
190 | start_addr = 0; | |
191 | mv_xor_init2(cs); | |
192 | ||
193 | /* Skip first 16 MiB */ | |
194 | if (0 == cs) { | |
195 | start_addr = 0x1000000; | |
196 | size -= start_addr; | |
197 | } | |
198 | ||
199 | mv_xor_mem_init(SCRB_XOR_CHAN, start_addr, size - 1, | |
200 | SCRUB_MAGIC, SCRUB_MAGIC); | |
201 | ||
202 | /* Wait for previous transfer completion */ | |
203 | while (mv_xor_state_get(SCRB_XOR_CHAN) != MV_IDLE) | |
204 | ; | |
205 | ||
206 | mv_xor_finish2(); | |
207 | } | |
208 | ||
209 | temp = reg_read(REG_SDRAM_CONFIG_ADDR); | |
210 | temp &= ~(1 << REG_SDRAM_CONFIG_IERR_OFFS); | |
211 | reg_write(REG_SDRAM_CONFIG_ADDR, temp); | |
212 | } | |
213 | ||
214 | static int ecc_enabled(void) | |
215 | { | |
216 | if (reg_read(REG_SDRAM_CONFIG_ADDR) & (1 << REG_SDRAM_CONFIG_ECC_OFFS)) | |
217 | return 1; | |
218 | ||
219 | return 0; | |
220 | } | |
221 | ||
222 | /* Return the width of the DRAM bus. */ | |
223 | static int bus_width(void) | |
224 | { | |
225 | int full_width = 0; | |
226 | ||
227 | if (reg_read(REG_SDRAM_CONFIG_ADDR) & (1 << REG_SDRAM_CONFIG_WIDTH_OFFS)) | |
228 | full_width = 1; | |
229 | ||
230 | #ifdef CONFIG_ARMADA_XP | |
231 | return full_width ? 64 : 32; | |
232 | #else | |
233 | return full_width ? 32 : 16; | |
234 | #endif | |
235 | } | |
236 | ||
237 | static int cycle_mode(void) | |
238 | { | |
239 | int val = reg_read(REG_DUNIT_CTRL_LOW_ADDR); | |
240 | ||
241 | return (val >> REG_DUNIT_CTRL_LOW_2T_OFFS) & REG_DUNIT_CTRL_LOW_2T_MASK; | |
242 | } | |
243 | ||
244 | #else | |
245 | static void dram_ecc_scrubbing(void) | |
246 | { | |
247 | } | |
248 | ||
249 | static int ecc_enabled(void) | |
250 | { | |
251 | return 0; | |
252 | } | |
253 | #endif | |
254 | ||
255 | int dram_init(void) | |
256 | { | |
257 | u64 size = 0; | |
258 | int i; | |
259 | ||
260 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { | |
261 | /* | |
262 | * It is assumed that all memory banks are consecutive | |
263 | * and without gaps. | |
264 | * If the gap is found, ram_size will be reported for | |
265 | * consecutive memory only | |
266 | */ | |
267 | if (mvebu_sdram_bar(i) != size) | |
268 | break; | |
269 | ||
270 | /* | |
271 | * Don't report more than 3GiB of SDRAM, otherwise there is no | |
272 | * address space left for the internal registers etc. | |
273 | */ | |
274 | size += mvebu_sdram_bs(i); | |
275 | if (size > MVEBU_SDRAM_SIZE_MAX) | |
276 | size = MVEBU_SDRAM_SIZE_MAX; | |
277 | } | |
278 | ||
279 | if (ecc_enabled()) | |
280 | dram_ecc_scrubbing(); | |
281 | ||
282 | gd->ram_size = size; | |
283 | ||
284 | return 0; | |
285 | } | |
286 | ||
287 | /* | |
288 | * If this function is not defined here, | |
289 | * board.c alters dram bank zero configuration defined above. | |
290 | */ | |
291 | int dram_init_banksize(void) | |
292 | { | |
293 | u64 size = 0; | |
294 | int i; | |
295 | ||
296 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { | |
297 | gd->bd->bi_dram[i].start = mvebu_sdram_bar(i); | |
298 | gd->bd->bi_dram[i].size = mvebu_sdram_bs(i); | |
299 | ||
300 | /* Clip the banksize to 1GiB if it exceeds the max size */ | |
301 | size += gd->bd->bi_dram[i].size; | |
302 | if (size > MVEBU_SDRAM_SIZE_MAX) | |
303 | mvebu_sdram_bs_set(i, 0x40000000); | |
304 | } | |
305 | ||
306 | return 0; | |
307 | } | |
308 | ||
309 | #if defined(CONFIG_ARCH_MVEBU) | |
310 | void board_add_ram_info(int use_default) | |
311 | { | |
312 | struct sar_freq_modes sar_freq; | |
313 | int mode; | |
314 | int width; | |
315 | ||
316 | get_sar_freq(&sar_freq); | |
317 | printf(" (%d MHz, ", sar_freq.d_clk); | |
318 | ||
319 | width = bus_width(); | |
320 | if (width) | |
321 | printf("%d-bit, ", width); | |
322 | ||
323 | mode = cycle_mode(); | |
324 | /* Mode 0 = Single cycle | |
325 | * Mode 1 = Two cycles (2T) | |
326 | * Mode 2 = Three cycles (3T) | |
327 | */ | |
328 | if (mode == 1) | |
329 | printf("2T, "); | |
330 | if (mode == 2) | |
331 | printf("3T, "); | |
332 | ||
333 | if (ecc_enabled()) | |
334 | printf("ECC"); | |
335 | else | |
336 | printf("ECC not"); | |
337 | printf(" enabled)"); | |
338 | } | |
339 | #endif |