blob: fa8c799a462e0c3f0c513bc5b7598973f8b5e6ab [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +02002/*
3 * (C) Copyright 2009
4 * Marvell Semiconductor <www.marvell.com>
5 * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +02006 */
7
8#include <config.h>
Prafulla Wadaskarf8e42622010-09-30 19:33:19 +05309#include <common.h>
Lei Wen298ae912011-10-18 20:11:42 +053010#include <asm/io.h>
11#include <asm/arch/cpu.h>
Stefan Roesec2437842014-10-22 12:13:06 +020012#include <asm/arch/soc.h>
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +020013
Stefan Roeseeb083e52015-12-21 13:56:33 +010014#if defined(CONFIG_ARCH_MVEBU)
15/* Use common XOR definitions for A3x and AXP */
Stefan Roesef3345e62015-08-06 14:43:13 +020016#include "../../../drivers/ddr/marvell/axp/xor.h"
17#include "../../../drivers/ddr/marvell/axp/xor_regs.h"
Stefan Roese7a96ee92015-08-03 13:15:31 +020018#endif
Stefan Roese7a96ee92015-08-03 13:15:31 +020019
Prafulla Wadaskarf8e42622010-09-30 19:33:19 +053020DECLARE_GLOBAL_DATA_PTR;
21
Stefan Roese0b741752014-10-22 12:13:13 +020022struct sdram_bank {
Holger Brunck67393fc2012-07-20 02:34:24 +000023 u32 win_bar;
24 u32 win_sz;
25};
26
Stefan Roese0b741752014-10-22 12:13:13 +020027struct sdram_addr_dec {
28 struct sdram_bank sdram_bank[4];
Holger Brunck67393fc2012-07-20 02:34:24 +000029};
30
Stefan Roese0b741752014-10-22 12:13:13 +020031#define REG_CPUCS_WIN_ENABLE (1 << 0)
32#define REG_CPUCS_WIN_WR_PROTECT (1 << 1)
33#define REG_CPUCS_WIN_WIN0_CS(x) (((x) & 0x3) << 2)
34#define REG_CPUCS_WIN_SIZE(x) (((x) & 0xff) << 24)
Gerlando Falautoac935e22012-07-20 02:34:25 +000035
Stefan Roese10fa44b2018-10-22 14:21:17 +020036#ifndef MVEBU_SDRAM_SIZE_MAX
37#define MVEBU_SDRAM_SIZE_MAX 0xc0000000
38#endif
Stefan Roese420abea2015-08-10 15:11:27 +020039
Stefan Roesef3345e62015-08-06 14:43:13 +020040#define SCRUB_MAGIC 0xbeefdead
41
42#define SCRB_XOR_UNIT 0
43#define SCRB_XOR_CHAN 1
44#define SCRB_XOR_WIN 0
45
46#define XEBARX_BASE_OFFS 16
47
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +020048/*
Stefan Roese0b741752014-10-22 12:13:13 +020049 * mvebu_sdram_bar - reads SDRAM Base Address Register
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +020050 */
Stefan Roese0b741752014-10-22 12:13:13 +020051u32 mvebu_sdram_bar(enum memory_bank bank)
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +020052{
Stefan Roese0b741752014-10-22 12:13:13 +020053 struct sdram_addr_dec *base =
54 (struct sdram_addr_dec *)MVEBU_SDRAM_BASE;
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +020055 u32 result = 0;
Holger Brunck67393fc2012-07-20 02:34:24 +000056 u32 enable = 0x01 & readl(&base->sdram_bank[bank].win_sz);
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +020057
58 if ((!enable) || (bank > BANK3))
59 return 0;
60
Holger Brunck67393fc2012-07-20 02:34:24 +000061 result = readl(&base->sdram_bank[bank].win_bar);
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +020062 return result;
63}
64
65/*
Stefan Roese0b741752014-10-22 12:13:13 +020066 * mvebu_sdram_bs_set - writes SDRAM Bank size
Gerlando Falautoac935e22012-07-20 02:34:25 +000067 */
Stefan Roese0b741752014-10-22 12:13:13 +020068static void mvebu_sdram_bs_set(enum memory_bank bank, u32 size)
Gerlando Falautoac935e22012-07-20 02:34:25 +000069{
Stefan Roese0b741752014-10-22 12:13:13 +020070 struct sdram_addr_dec *base =
71 (struct sdram_addr_dec *)MVEBU_SDRAM_BASE;
Gerlando Falautoac935e22012-07-20 02:34:25 +000072 /* Read current register value */
73 u32 reg = readl(&base->sdram_bank[bank].win_sz);
74
75 /* Clear window size */
Stefan Roese0b741752014-10-22 12:13:13 +020076 reg &= ~REG_CPUCS_WIN_SIZE(0xFF);
Gerlando Falautoac935e22012-07-20 02:34:25 +000077
78 /* Set new window size */
Stefan Roese0b741752014-10-22 12:13:13 +020079 reg |= REG_CPUCS_WIN_SIZE((size - 1) >> 24);
Gerlando Falautoac935e22012-07-20 02:34:25 +000080
81 writel(reg, &base->sdram_bank[bank].win_sz);
82}
83
84/*
Stefan Roese0b741752014-10-22 12:13:13 +020085 * mvebu_sdram_bs - reads SDRAM Bank size
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +020086 */
Stefan Roese0b741752014-10-22 12:13:13 +020087u32 mvebu_sdram_bs(enum memory_bank bank)
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +020088{
Stefan Roese0b741752014-10-22 12:13:13 +020089 struct sdram_addr_dec *base =
90 (struct sdram_addr_dec *)MVEBU_SDRAM_BASE;
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +020091 u32 result = 0;
Holger Brunck67393fc2012-07-20 02:34:24 +000092 u32 enable = 0x01 & readl(&base->sdram_bank[bank].win_sz);
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +020093
94 if ((!enable) || (bank > BANK3))
95 return 0;
Holger Brunck67393fc2012-07-20 02:34:24 +000096 result = 0xff000000 & readl(&base->sdram_bank[bank].win_sz);
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +020097 result += 0x01000000;
98 return result;
99}
Prafulla Wadaskarf8e42622010-09-30 19:33:19 +0530100
Stefan Roese0b741752014-10-22 12:13:13 +0200101void mvebu_sdram_size_adjust(enum memory_bank bank)
Gerlando Falautoea32b7e2012-07-25 06:23:48 +0000102{
103 u32 size;
104
105 /* probe currently equipped RAM size */
Stefan Roese0b741752014-10-22 12:13:13 +0200106 size = get_ram_size((void *)mvebu_sdram_bar(bank),
107 mvebu_sdram_bs(bank));
Gerlando Falautoea32b7e2012-07-25 06:23:48 +0000108
109 /* adjust SDRAM window size accordingly */
Stefan Roese0b741752014-10-22 12:13:13 +0200110 mvebu_sdram_bs_set(bank, size);
Gerlando Falautoea32b7e2012-07-25 06:23:48 +0000111}
112
Stefan Roeseeb083e52015-12-21 13:56:33 +0100113#if defined(CONFIG_ARCH_MVEBU)
Stefan Roesef3345e62015-08-06 14:43:13 +0200114static u32 xor_ctrl_save;
115static u32 xor_base_save;
116static u32 xor_mask_save;
117
118static void mv_xor_init2(u32 cs)
119{
120 u32 reg, base, size, base2;
121 u32 bank_attr[4] = { 0xe00, 0xd00, 0xb00, 0x700 };
122
123 xor_ctrl_save = reg_read(XOR_WINDOW_CTRL_REG(SCRB_XOR_UNIT,
124 SCRB_XOR_CHAN));
125 xor_base_save = reg_read(XOR_BASE_ADDR_REG(SCRB_XOR_UNIT,
126 SCRB_XOR_WIN));
127 xor_mask_save = reg_read(XOR_SIZE_MASK_REG(SCRB_XOR_UNIT,
128 SCRB_XOR_WIN));
129
130 /* Enable Window x for each CS */
131 reg = 0x1;
132 reg |= (0x3 << 16);
133 reg_write(XOR_WINDOW_CTRL_REG(SCRB_XOR_UNIT, SCRB_XOR_CHAN), reg);
134
135 base = 0;
136 size = mvebu_sdram_bs(cs) - 1;
137 if (size) {
138 base2 = ((base / (64 << 10)) << XEBARX_BASE_OFFS) |
139 bank_attr[cs];
140 reg_write(XOR_BASE_ADDR_REG(SCRB_XOR_UNIT, SCRB_XOR_WIN),
141 base2);
142
143 base += size + 1;
144 size = (size / (64 << 10)) << 16;
145 /* Window x - size - 256 MB */
146 reg_write(XOR_SIZE_MASK_REG(SCRB_XOR_UNIT, SCRB_XOR_WIN), size);
147 }
148
149 mv_xor_hal_init(0);
150
151 return;
152}
153
154static void mv_xor_finish2(void)
155{
156 reg_write(XOR_WINDOW_CTRL_REG(SCRB_XOR_UNIT, SCRB_XOR_CHAN),
157 xor_ctrl_save);
158 reg_write(XOR_BASE_ADDR_REG(SCRB_XOR_UNIT, SCRB_XOR_WIN),
159 xor_base_save);
160 reg_write(XOR_SIZE_MASK_REG(SCRB_XOR_UNIT, SCRB_XOR_WIN),
161 xor_mask_save);
162}
163
164static void dram_ecc_scrubbing(void)
165{
166 int cs;
167 u32 size, temp;
168 u32 total_mem = 0;
169 u64 total;
170 u32 start_addr;
171
172 /*
173 * The DDR training code from the bin_hdr / SPL already
174 * scrubbed the DDR till 0x1000000. And the main U-Boot
175 * is loaded to an address < 0x1000000. So we need to
176 * skip this range to not re-scrub this area again.
177 */
178 temp = reg_read(REG_SDRAM_CONFIG_ADDR);
179 temp |= (1 << REG_SDRAM_CONFIG_IERR_OFFS);
180 reg_write(REG_SDRAM_CONFIG_ADDR, temp);
181
182 for (cs = 0; cs < CONFIG_NR_DRAM_BANKS; cs++) {
Chris Packham50df87f2017-09-23 04:50:31 +1200183 size = mvebu_sdram_bs(cs);
Stefan Roesef3345e62015-08-06 14:43:13 +0200184 if (size == 0)
185 continue;
186
Chris Packham50df87f2017-09-23 04:50:31 +1200187 total = (u64)size;
Stefan Roesef3345e62015-08-06 14:43:13 +0200188 total_mem += (u32)(total / (1 << 30));
189 start_addr = 0;
190 mv_xor_init2(cs);
191
192 /* Skip first 16 MiB */
193 if (0 == cs) {
194 start_addr = 0x1000000;
195 size -= start_addr;
196 }
197
Chris Packham50df87f2017-09-23 04:50:31 +1200198 mv_xor_mem_init(SCRB_XOR_CHAN, start_addr, size - 1,
Stefan Roesef3345e62015-08-06 14:43:13 +0200199 SCRUB_MAGIC, SCRUB_MAGIC);
200
201 /* Wait for previous transfer completion */
202 while (mv_xor_state_get(SCRB_XOR_CHAN) != MV_IDLE)
203 ;
204
205 mv_xor_finish2();
206 }
207
208 temp = reg_read(REG_SDRAM_CONFIG_ADDR);
209 temp &= ~(1 << REG_SDRAM_CONFIG_IERR_OFFS);
210 reg_write(REG_SDRAM_CONFIG_ADDR, temp);
211}
212
213static int ecc_enabled(void)
214{
215 if (reg_read(REG_SDRAM_CONFIG_ADDR) & (1 << REG_SDRAM_CONFIG_ECC_OFFS))
216 return 1;
217
218 return 0;
219}
Joshua Scott2e161b92017-09-04 17:38:32 +1200220
221/* Return the width of the DRAM bus, or 0 for unknown. */
222static int bus_width(void)
223{
224 int full_width = 0;
225
226 if (reg_read(REG_SDRAM_CONFIG_ADDR) & (1 << REG_SDRAM_CONFIG_WIDTH_OFFS))
227 full_width = 1;
228
229 switch (mvebu_soc_family()) {
230 case MVEBU_SOC_AXP:
231 return full_width ? 64 : 32;
232 break;
233 case MVEBU_SOC_A375:
234 case MVEBU_SOC_A38X:
235 case MVEBU_SOC_MSYS:
236 return full_width ? 32 : 16;
237 default:
238 return 0;
239 }
240}
241
242static int cycle_mode(void)
243{
244 int val = reg_read(REG_DUNIT_CTRL_LOW_ADDR);
245
246 return (val >> REG_DUNIT_CTRL_LOW_2T_OFFS) & REG_DUNIT_CTRL_LOW_2T_MASK;
247}
248
Stefan Roesef3345e62015-08-06 14:43:13 +0200249#else
250static void dram_ecc_scrubbing(void)
251{
252}
253
254static int ecc_enabled(void)
255{
256 return 0;
257}
258#endif
259
Prafulla Wadaskarf8e42622010-09-30 19:33:19 +0530260int dram_init(void)
261{
Stefan Roese420abea2015-08-10 15:11:27 +0200262 u64 size = 0;
Prafulla Wadaskarf8e42622010-09-30 19:33:19 +0530263 int i;
264
Prafulla Wadaskarf8e42622010-09-30 19:33:19 +0530265 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
Prafulla Wadaskarf8e42622010-09-30 19:33:19 +0530266 /*
267 * It is assumed that all memory banks are consecutive
268 * and without gaps.
269 * If the gap is found, ram_size will be reported for
270 * consecutive memory only
271 */
Stefan Roese420abea2015-08-10 15:11:27 +0200272 if (mvebu_sdram_bar(i) != size)
Prafulla Wadaskarf8e42622010-09-30 19:33:19 +0530273 break;
274
Stefan Roese2cc5fad2014-10-22 12:13:05 +0200275 /*
276 * Don't report more than 3GiB of SDRAM, otherwise there is no
277 * address space left for the internal registers etc.
278 */
Stefan Roese420abea2015-08-10 15:11:27 +0200279 size += mvebu_sdram_bs(i);
Stefan Roese10fa44b2018-10-22 14:21:17 +0200280 if (size > MVEBU_SDRAM_SIZE_MAX)
281 size = MVEBU_SDRAM_SIZE_MAX;
Prafulla Wadaskarf8e42622010-09-30 19:33:19 +0530282 }
Tanmay Upadhyay9a5ff782010-10-28 20:06:22 +0530283
284 for (; i < CONFIG_NR_DRAM_BANKS; i++) {
285 /* If above loop terminated prematurely, we need to set
286 * remaining banks' start address & size as 0. Otherwise other
287 * u-boot functions and Linux kernel gets wrong values which
288 * could result in crash */
289 gd->bd->bi_dram[i].start = 0;
290 gd->bd->bi_dram[i].size = 0;
291 }
292
Stefan Roesef3345e62015-08-06 14:43:13 +0200293
294 if (ecc_enabled())
295 dram_ecc_scrubbing();
296
Stefan Roese420abea2015-08-10 15:11:27 +0200297 gd->ram_size = size;
298
Prafulla Wadaskarf8e42622010-09-30 19:33:19 +0530299 return 0;
300}
301
302/*
303 * If this function is not defined here,
304 * board.c alters dram bank zero configuration defined above.
305 */
Simon Glass2f949c32017-03-31 08:40:32 -0600306int dram_init_banksize(void)
Prafulla Wadaskarf8e42622010-09-30 19:33:19 +0530307{
Stefan Roese420abea2015-08-10 15:11:27 +0200308 u64 size = 0;
309 int i;
310
311 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
312 gd->bd->bi_dram[i].start = mvebu_sdram_bar(i);
313 gd->bd->bi_dram[i].size = mvebu_sdram_bs(i);
314
315 /* Clip the banksize to 1GiB if it exceeds the max size */
316 size += gd->bd->bi_dram[i].size;
Stefan Roese10fa44b2018-10-22 14:21:17 +0200317 if (size > MVEBU_SDRAM_SIZE_MAX)
Stefan Roese420abea2015-08-10 15:11:27 +0200318 mvebu_sdram_bs_set(i, 0x40000000);
319 }
Simon Glass2f949c32017-03-31 08:40:32 -0600320
321 return 0;
Prafulla Wadaskarf8e42622010-09-30 19:33:19 +0530322}
Stefan Roese7a96ee92015-08-03 13:15:31 +0200323
Stefan Roeseeb083e52015-12-21 13:56:33 +0100324#if defined(CONFIG_ARCH_MVEBU)
Stefan Roese7a96ee92015-08-03 13:15:31 +0200325void board_add_ram_info(int use_default)
326{
Stefan Roese2a539c82015-12-21 12:36:40 +0100327 struct sar_freq_modes sar_freq;
Joshua Scott2e161b92017-09-04 17:38:32 +1200328 int mode;
329 int width;
Stefan Roese2a539c82015-12-21 12:36:40 +0100330
331 get_sar_freq(&sar_freq);
332 printf(" (%d MHz, ", sar_freq.d_clk);
333
Joshua Scott2e161b92017-09-04 17:38:32 +1200334 width = bus_width();
335 if (width)
336 printf("%d-bit, ", width);
337
338 mode = cycle_mode();
339 /* Mode 0 = Single cycle
340 * Mode 1 = Two cycles (2T)
341 * Mode 2 = Three cycles (3T)
342 */
343 if (mode == 1)
344 printf("2T, ");
345 if (mode == 2)
346 printf("3T, ");
347
Stefan Roesef3345e62015-08-06 14:43:13 +0200348 if (ecc_enabled())
Stefan Roese2a539c82015-12-21 12:36:40 +0100349 printf("ECC");
Stefan Roese7a96ee92015-08-03 13:15:31 +0200350 else
Stefan Roese2a539c82015-12-21 12:36:40 +0100351 printf("ECC not");
Stefan Roese7a96ee92015-08-03 13:15:31 +0200352 printf(" enabled)");
353}
Stefan Roese2a539c82015-12-21 12:36:40 +0100354#endif