blob: 68383d06a274d516f196c8faaba91b837242d3df [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 Roese420abea2015-08-10 15:11:27 +020036#define SDRAM_SIZE_MAX 0xc0000000
37
Stefan Roesef3345e62015-08-06 14:43:13 +020038#define SCRUB_MAGIC 0xbeefdead
39
40#define SCRB_XOR_UNIT 0
41#define SCRB_XOR_CHAN 1
42#define SCRB_XOR_WIN 0
43
44#define XEBARX_BASE_OFFS 16
45
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +020046/*
Stefan Roese0b741752014-10-22 12:13:13 +020047 * mvebu_sdram_bar - reads SDRAM Base Address Register
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +020048 */
Stefan Roese0b741752014-10-22 12:13:13 +020049u32 mvebu_sdram_bar(enum memory_bank bank)
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +020050{
Stefan Roese0b741752014-10-22 12:13:13 +020051 struct sdram_addr_dec *base =
52 (struct sdram_addr_dec *)MVEBU_SDRAM_BASE;
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +020053 u32 result = 0;
Holger Brunck67393fc2012-07-20 02:34:24 +000054 u32 enable = 0x01 & readl(&base->sdram_bank[bank].win_sz);
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +020055
56 if ((!enable) || (bank > BANK3))
57 return 0;
58
Holger Brunck67393fc2012-07-20 02:34:24 +000059 result = readl(&base->sdram_bank[bank].win_bar);
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +020060 return result;
61}
62
63/*
Stefan Roese0b741752014-10-22 12:13:13 +020064 * mvebu_sdram_bs_set - writes SDRAM Bank size
Gerlando Falautoac935e22012-07-20 02:34:25 +000065 */
Stefan Roese0b741752014-10-22 12:13:13 +020066static void mvebu_sdram_bs_set(enum memory_bank bank, u32 size)
Gerlando Falautoac935e22012-07-20 02:34:25 +000067{
Stefan Roese0b741752014-10-22 12:13:13 +020068 struct sdram_addr_dec *base =
69 (struct sdram_addr_dec *)MVEBU_SDRAM_BASE;
Gerlando Falautoac935e22012-07-20 02:34:25 +000070 /* Read current register value */
71 u32 reg = readl(&base->sdram_bank[bank].win_sz);
72
73 /* Clear window size */
Stefan Roese0b741752014-10-22 12:13:13 +020074 reg &= ~REG_CPUCS_WIN_SIZE(0xFF);
Gerlando Falautoac935e22012-07-20 02:34:25 +000075
76 /* Set new window size */
Stefan Roese0b741752014-10-22 12:13:13 +020077 reg |= REG_CPUCS_WIN_SIZE((size - 1) >> 24);
Gerlando Falautoac935e22012-07-20 02:34:25 +000078
79 writel(reg, &base->sdram_bank[bank].win_sz);
80}
81
82/*
Stefan Roese0b741752014-10-22 12:13:13 +020083 * mvebu_sdram_bs - reads SDRAM Bank size
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +020084 */
Stefan Roese0b741752014-10-22 12:13:13 +020085u32 mvebu_sdram_bs(enum memory_bank bank)
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +020086{
Stefan Roese0b741752014-10-22 12:13:13 +020087 struct sdram_addr_dec *base =
88 (struct sdram_addr_dec *)MVEBU_SDRAM_BASE;
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +020089 u32 result = 0;
Holger Brunck67393fc2012-07-20 02:34:24 +000090 u32 enable = 0x01 & readl(&base->sdram_bank[bank].win_sz);
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +020091
92 if ((!enable) || (bank > BANK3))
93 return 0;
Holger Brunck67393fc2012-07-20 02:34:24 +000094 result = 0xff000000 & readl(&base->sdram_bank[bank].win_sz);
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +020095 result += 0x01000000;
96 return result;
97}
Prafulla Wadaskarf8e42622010-09-30 19:33:19 +053098
Stefan Roese0b741752014-10-22 12:13:13 +020099void mvebu_sdram_size_adjust(enum memory_bank bank)
Gerlando Falautoea32b7e2012-07-25 06:23:48 +0000100{
101 u32 size;
102
103 /* probe currently equipped RAM size */
Stefan Roese0b741752014-10-22 12:13:13 +0200104 size = get_ram_size((void *)mvebu_sdram_bar(bank),
105 mvebu_sdram_bs(bank));
Gerlando Falautoea32b7e2012-07-25 06:23:48 +0000106
107 /* adjust SDRAM window size accordingly */
Stefan Roese0b741752014-10-22 12:13:13 +0200108 mvebu_sdram_bs_set(bank, size);
Gerlando Falautoea32b7e2012-07-25 06:23:48 +0000109}
110
Stefan Roeseeb083e52015-12-21 13:56:33 +0100111#if defined(CONFIG_ARCH_MVEBU)
Stefan Roesef3345e62015-08-06 14:43:13 +0200112static u32 xor_ctrl_save;
113static u32 xor_base_save;
114static u32 xor_mask_save;
115
116static void mv_xor_init2(u32 cs)
117{
118 u32 reg, base, size, base2;
119 u32 bank_attr[4] = { 0xe00, 0xd00, 0xb00, 0x700 };
120
121 xor_ctrl_save = reg_read(XOR_WINDOW_CTRL_REG(SCRB_XOR_UNIT,
122 SCRB_XOR_CHAN));
123 xor_base_save = reg_read(XOR_BASE_ADDR_REG(SCRB_XOR_UNIT,
124 SCRB_XOR_WIN));
125 xor_mask_save = reg_read(XOR_SIZE_MASK_REG(SCRB_XOR_UNIT,
126 SCRB_XOR_WIN));
127
128 /* Enable Window x for each CS */
129 reg = 0x1;
130 reg |= (0x3 << 16);
131 reg_write(XOR_WINDOW_CTRL_REG(SCRB_XOR_UNIT, SCRB_XOR_CHAN), reg);
132
133 base = 0;
134 size = mvebu_sdram_bs(cs) - 1;
135 if (size) {
136 base2 = ((base / (64 << 10)) << XEBARX_BASE_OFFS) |
137 bank_attr[cs];
138 reg_write(XOR_BASE_ADDR_REG(SCRB_XOR_UNIT, SCRB_XOR_WIN),
139 base2);
140
141 base += size + 1;
142 size = (size / (64 << 10)) << 16;
143 /* Window x - size - 256 MB */
144 reg_write(XOR_SIZE_MASK_REG(SCRB_XOR_UNIT, SCRB_XOR_WIN), size);
145 }
146
147 mv_xor_hal_init(0);
148
149 return;
150}
151
152static void mv_xor_finish2(void)
153{
154 reg_write(XOR_WINDOW_CTRL_REG(SCRB_XOR_UNIT, SCRB_XOR_CHAN),
155 xor_ctrl_save);
156 reg_write(XOR_BASE_ADDR_REG(SCRB_XOR_UNIT, SCRB_XOR_WIN),
157 xor_base_save);
158 reg_write(XOR_SIZE_MASK_REG(SCRB_XOR_UNIT, SCRB_XOR_WIN),
159 xor_mask_save);
160}
161
162static void dram_ecc_scrubbing(void)
163{
164 int cs;
165 u32 size, temp;
166 u32 total_mem = 0;
167 u64 total;
168 u32 start_addr;
169
170 /*
171 * The DDR training code from the bin_hdr / SPL already
172 * scrubbed the DDR till 0x1000000. And the main U-Boot
173 * is loaded to an address < 0x1000000. So we need to
174 * skip this range to not re-scrub this area again.
175 */
176 temp = reg_read(REG_SDRAM_CONFIG_ADDR);
177 temp |= (1 << REG_SDRAM_CONFIG_IERR_OFFS);
178 reg_write(REG_SDRAM_CONFIG_ADDR, temp);
179
180 for (cs = 0; cs < CONFIG_NR_DRAM_BANKS; cs++) {
Chris Packham50df87f2017-09-23 04:50:31 +1200181 size = mvebu_sdram_bs(cs);
Stefan Roesef3345e62015-08-06 14:43:13 +0200182 if (size == 0)
183 continue;
184
Chris Packham50df87f2017-09-23 04:50:31 +1200185 total = (u64)size;
Stefan Roesef3345e62015-08-06 14:43:13 +0200186 total_mem += (u32)(total / (1 << 30));
187 start_addr = 0;
188 mv_xor_init2(cs);
189
190 /* Skip first 16 MiB */
191 if (0 == cs) {
192 start_addr = 0x1000000;
193 size -= start_addr;
194 }
195
Chris Packham50df87f2017-09-23 04:50:31 +1200196 mv_xor_mem_init(SCRB_XOR_CHAN, start_addr, size - 1,
Stefan Roesef3345e62015-08-06 14:43:13 +0200197 SCRUB_MAGIC, SCRUB_MAGIC);
198
199 /* Wait for previous transfer completion */
200 while (mv_xor_state_get(SCRB_XOR_CHAN) != MV_IDLE)
201 ;
202
203 mv_xor_finish2();
204 }
205
206 temp = reg_read(REG_SDRAM_CONFIG_ADDR);
207 temp &= ~(1 << REG_SDRAM_CONFIG_IERR_OFFS);
208 reg_write(REG_SDRAM_CONFIG_ADDR, temp);
209}
210
211static int ecc_enabled(void)
212{
213 if (reg_read(REG_SDRAM_CONFIG_ADDR) & (1 << REG_SDRAM_CONFIG_ECC_OFFS))
214 return 1;
215
216 return 0;
217}
Joshua Scott2e161b92017-09-04 17:38:32 +1200218
219/* Return the width of the DRAM bus, or 0 for unknown. */
220static int bus_width(void)
221{
222 int full_width = 0;
223
224 if (reg_read(REG_SDRAM_CONFIG_ADDR) & (1 << REG_SDRAM_CONFIG_WIDTH_OFFS))
225 full_width = 1;
226
227 switch (mvebu_soc_family()) {
228 case MVEBU_SOC_AXP:
229 return full_width ? 64 : 32;
230 break;
231 case MVEBU_SOC_A375:
232 case MVEBU_SOC_A38X:
233 case MVEBU_SOC_MSYS:
234 return full_width ? 32 : 16;
235 default:
236 return 0;
237 }
238}
239
240static int cycle_mode(void)
241{
242 int val = reg_read(REG_DUNIT_CTRL_LOW_ADDR);
243
244 return (val >> REG_DUNIT_CTRL_LOW_2T_OFFS) & REG_DUNIT_CTRL_LOW_2T_MASK;
245}
246
Stefan Roesef3345e62015-08-06 14:43:13 +0200247#else
248static void dram_ecc_scrubbing(void)
249{
250}
251
252static int ecc_enabled(void)
253{
254 return 0;
255}
256#endif
257
Prafulla Wadaskarf8e42622010-09-30 19:33:19 +0530258int dram_init(void)
259{
Stefan Roese420abea2015-08-10 15:11:27 +0200260 u64 size = 0;
Prafulla Wadaskarf8e42622010-09-30 19:33:19 +0530261 int i;
262
Prafulla Wadaskarf8e42622010-09-30 19:33:19 +0530263 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
Prafulla Wadaskarf8e42622010-09-30 19:33:19 +0530264 /*
265 * It is assumed that all memory banks are consecutive
266 * and without gaps.
267 * If the gap is found, ram_size will be reported for
268 * consecutive memory only
269 */
Stefan Roese420abea2015-08-10 15:11:27 +0200270 if (mvebu_sdram_bar(i) != size)
Prafulla Wadaskarf8e42622010-09-30 19:33:19 +0530271 break;
272
Stefan Roese2cc5fad2014-10-22 12:13:05 +0200273 /*
274 * Don't report more than 3GiB of SDRAM, otherwise there is no
275 * address space left for the internal registers etc.
276 */
Stefan Roese420abea2015-08-10 15:11:27 +0200277 size += mvebu_sdram_bs(i);
278 if (size > SDRAM_SIZE_MAX)
279 size = SDRAM_SIZE_MAX;
Prafulla Wadaskarf8e42622010-09-30 19:33:19 +0530280 }
Tanmay Upadhyay9a5ff782010-10-28 20:06:22 +0530281
282 for (; i < CONFIG_NR_DRAM_BANKS; i++) {
283 /* If above loop terminated prematurely, we need to set
284 * remaining banks' start address & size as 0. Otherwise other
285 * u-boot functions and Linux kernel gets wrong values which
286 * could result in crash */
287 gd->bd->bi_dram[i].start = 0;
288 gd->bd->bi_dram[i].size = 0;
289 }
290
Stefan Roesef3345e62015-08-06 14:43:13 +0200291
292 if (ecc_enabled())
293 dram_ecc_scrubbing();
294
Stefan Roese420abea2015-08-10 15:11:27 +0200295 gd->ram_size = size;
296
Prafulla Wadaskarf8e42622010-09-30 19:33:19 +0530297 return 0;
298}
299
300/*
301 * If this function is not defined here,
302 * board.c alters dram bank zero configuration defined above.
303 */
Simon Glass2f949c32017-03-31 08:40:32 -0600304int dram_init_banksize(void)
Prafulla Wadaskarf8e42622010-09-30 19:33:19 +0530305{
Stefan Roese420abea2015-08-10 15:11:27 +0200306 u64 size = 0;
307 int i;
308
309 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
310 gd->bd->bi_dram[i].start = mvebu_sdram_bar(i);
311 gd->bd->bi_dram[i].size = mvebu_sdram_bs(i);
312
313 /* Clip the banksize to 1GiB if it exceeds the max size */
314 size += gd->bd->bi_dram[i].size;
315 if (size > SDRAM_SIZE_MAX)
316 mvebu_sdram_bs_set(i, 0x40000000);
317 }
Simon Glass2f949c32017-03-31 08:40:32 -0600318
319 return 0;
Prafulla Wadaskarf8e42622010-09-30 19:33:19 +0530320}
Stefan Roese7a96ee92015-08-03 13:15:31 +0200321
Stefan Roeseeb083e52015-12-21 13:56:33 +0100322#if defined(CONFIG_ARCH_MVEBU)
Stefan Roese7a96ee92015-08-03 13:15:31 +0200323void board_add_ram_info(int use_default)
324{
Stefan Roese2a539c82015-12-21 12:36:40 +0100325 struct sar_freq_modes sar_freq;
Joshua Scott2e161b92017-09-04 17:38:32 +1200326 int mode;
327 int width;
Stefan Roese2a539c82015-12-21 12:36:40 +0100328
329 get_sar_freq(&sar_freq);
330 printf(" (%d MHz, ", sar_freq.d_clk);
331
Joshua Scott2e161b92017-09-04 17:38:32 +1200332 width = bus_width();
333 if (width)
334 printf("%d-bit, ", width);
335
336 mode = cycle_mode();
337 /* Mode 0 = Single cycle
338 * Mode 1 = Two cycles (2T)
339 * Mode 2 = Three cycles (3T)
340 */
341 if (mode == 1)
342 printf("2T, ");
343 if (mode == 2)
344 printf("3T, ");
345
Stefan Roesef3345e62015-08-06 14:43:13 +0200346 if (ecc_enabled())
Stefan Roese2a539c82015-12-21 12:36:40 +0100347 printf("ECC");
Stefan Roese7a96ee92015-08-03 13:15:31 +0200348 else
Stefan Roese2a539c82015-12-21 12:36:40 +0100349 printf("ECC not");
Stefan Roese7a96ee92015-08-03 13:15:31 +0200350 printf(" enabled)");
351}
Stefan Roese2a539c82015-12-21 12:36:40 +0100352#endif