blob: c00c6b9b3fc2c3bf60462d76d198a5d6c49d0fe6 [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>
Simon Glass8e16b1e2019-12-28 10:45:05 -07009#include <init.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060010#include <asm/global_data.h>
Lei Wen298ae912011-10-18 20:11:42 +053011#include <asm/io.h>
12#include <asm/arch/cpu.h>
Stefan Roesec2437842014-10-22 12:13:06 +020013#include <asm/arch/soc.h>
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +020014
Stefan Roeseeb083e52015-12-21 13:56:33 +010015#if defined(CONFIG_ARCH_MVEBU)
16/* Use common XOR definitions for A3x and AXP */
Stefan Roesef3345e62015-08-06 14:43:13 +020017#include "../../../drivers/ddr/marvell/axp/xor.h"
18#include "../../../drivers/ddr/marvell/axp/xor_regs.h"
Stefan Roese7a96ee92015-08-03 13:15:31 +020019#endif
Stefan Roese7a96ee92015-08-03 13:15:31 +020020
Prafulla Wadaskarf8e42622010-09-30 19:33:19 +053021DECLARE_GLOBAL_DATA_PTR;
22
Stefan Roese0b741752014-10-22 12:13:13 +020023struct sdram_bank {
Holger Brunck67393fc2012-07-20 02:34:24 +000024 u32 win_bar;
25 u32 win_sz;
26};
27
Stefan Roese0b741752014-10-22 12:13:13 +020028struct sdram_addr_dec {
29 struct sdram_bank sdram_bank[4];
Holger Brunck67393fc2012-07-20 02:34:24 +000030};
31
Stefan Roese0b741752014-10-22 12:13:13 +020032#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)
Gerlando Falautoac935e22012-07-20 02:34:25 +000036
Stefan Roese10fa44b2018-10-22 14:21:17 +020037#ifndef MVEBU_SDRAM_SIZE_MAX
38#define MVEBU_SDRAM_SIZE_MAX 0xc0000000
39#endif
Stefan Roese420abea2015-08-10 15:11:27 +020040
Stefan Roesef3345e62015-08-06 14:43:13 +020041#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
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +020049/*
Stefan Roese0b741752014-10-22 12:13:13 +020050 * mvebu_sdram_bar - reads SDRAM Base Address Register
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +020051 */
Stefan Roese0b741752014-10-22 12:13:13 +020052u32 mvebu_sdram_bar(enum memory_bank bank)
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +020053{
Stefan Roese0b741752014-10-22 12:13:13 +020054 struct sdram_addr_dec *base =
55 (struct sdram_addr_dec *)MVEBU_SDRAM_BASE;
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +020056 u32 result = 0;
Holger Brunck67393fc2012-07-20 02:34:24 +000057 u32 enable = 0x01 & readl(&base->sdram_bank[bank].win_sz);
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +020058
59 if ((!enable) || (bank > BANK3))
60 return 0;
61
Holger Brunck67393fc2012-07-20 02:34:24 +000062 result = readl(&base->sdram_bank[bank].win_bar);
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +020063 return result;
64}
65
66/*
Stefan Roese0b741752014-10-22 12:13:13 +020067 * mvebu_sdram_bs_set - writes SDRAM Bank size
Gerlando Falautoac935e22012-07-20 02:34:25 +000068 */
Stefan Roese0b741752014-10-22 12:13:13 +020069static void mvebu_sdram_bs_set(enum memory_bank bank, u32 size)
Gerlando Falautoac935e22012-07-20 02:34:25 +000070{
Stefan Roese0b741752014-10-22 12:13:13 +020071 struct sdram_addr_dec *base =
72 (struct sdram_addr_dec *)MVEBU_SDRAM_BASE;
Gerlando Falautoac935e22012-07-20 02:34:25 +000073 /* Read current register value */
74 u32 reg = readl(&base->sdram_bank[bank].win_sz);
75
76 /* Clear window size */
Stefan Roese0b741752014-10-22 12:13:13 +020077 reg &= ~REG_CPUCS_WIN_SIZE(0xFF);
Gerlando Falautoac935e22012-07-20 02:34:25 +000078
79 /* Set new window size */
Stefan Roese0b741752014-10-22 12:13:13 +020080 reg |= REG_CPUCS_WIN_SIZE((size - 1) >> 24);
Gerlando Falautoac935e22012-07-20 02:34:25 +000081
82 writel(reg, &base->sdram_bank[bank].win_sz);
83}
84
85/*
Stefan Roese0b741752014-10-22 12:13:13 +020086 * mvebu_sdram_bs - reads SDRAM Bank size
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +020087 */
Stefan Roese0b741752014-10-22 12:13:13 +020088u32 mvebu_sdram_bs(enum memory_bank bank)
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +020089{
Stefan Roese0b741752014-10-22 12:13:13 +020090 struct sdram_addr_dec *base =
91 (struct sdram_addr_dec *)MVEBU_SDRAM_BASE;
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +020092 u32 result = 0;
Holger Brunck67393fc2012-07-20 02:34:24 +000093 u32 enable = 0x01 & readl(&base->sdram_bank[bank].win_sz);
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +020094
95 if ((!enable) || (bank > BANK3))
96 return 0;
Holger Brunck67393fc2012-07-20 02:34:24 +000097 result = 0xff000000 & readl(&base->sdram_bank[bank].win_sz);
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +020098 result += 0x01000000;
99 return result;
100}
Prafulla Wadaskarf8e42622010-09-30 19:33:19 +0530101
Stefan Roese0b741752014-10-22 12:13:13 +0200102void mvebu_sdram_size_adjust(enum memory_bank bank)
Gerlando Falautoea32b7e2012-07-25 06:23:48 +0000103{
104 u32 size;
105
106 /* probe currently equipped RAM size */
Stefan Roese0b741752014-10-22 12:13:13 +0200107 size = get_ram_size((void *)mvebu_sdram_bar(bank),
108 mvebu_sdram_bs(bank));
Gerlando Falautoea32b7e2012-07-25 06:23:48 +0000109
110 /* adjust SDRAM window size accordingly */
Stefan Roese0b741752014-10-22 12:13:13 +0200111 mvebu_sdram_bs_set(bank, size);
Gerlando Falautoea32b7e2012-07-25 06:23:48 +0000112}
113
Stefan Roeseeb083e52015-12-21 13:56:33 +0100114#if defined(CONFIG_ARCH_MVEBU)
Stefan Roesef3345e62015-08-06 14:43:13 +0200115static u32 xor_ctrl_save;
116static u32 xor_base_save;
117static u32 xor_mask_save;
118
119static 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
155static 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
165static 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++) {
Chris Packham50df87f2017-09-23 04:50:31 +1200184 size = mvebu_sdram_bs(cs);
Stefan Roesef3345e62015-08-06 14:43:13 +0200185 if (size == 0)
186 continue;
187
Chris Packham50df87f2017-09-23 04:50:31 +1200188 total = (u64)size;
Stefan Roesef3345e62015-08-06 14:43:13 +0200189 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
Chris Packham50df87f2017-09-23 04:50:31 +1200199 mv_xor_mem_init(SCRB_XOR_CHAN, start_addr, size - 1,
Stefan Roesef3345e62015-08-06 14:43:13 +0200200 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
214static 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}
Joshua Scott2e161b92017-09-04 17:38:32 +1200221
Pali Rohárfdf415c2022-07-15 10:13:12 +0200222/* Return the width of the DRAM bus. */
Joshua Scott2e161b92017-09-04 17:38:32 +1200223static 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
Pali Rohárfdf415c2022-07-15 10:13:12 +0200230#ifdef CONFIG_ARMADA_XP
231 return full_width ? 64 : 32;
232#else
233 return full_width ? 32 : 16;
234#endif
Joshua Scott2e161b92017-09-04 17:38:32 +1200235}
236
237static 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
Stefan Roesef3345e62015-08-06 14:43:13 +0200244#else
245static void dram_ecc_scrubbing(void)
246{
247}
248
249static int ecc_enabled(void)
250{
251 return 0;
252}
253#endif
254
Prafulla Wadaskarf8e42622010-09-30 19:33:19 +0530255int dram_init(void)
256{
Stefan Roese420abea2015-08-10 15:11:27 +0200257 u64 size = 0;
Prafulla Wadaskarf8e42622010-09-30 19:33:19 +0530258 int i;
259
Prafulla Wadaskarf8e42622010-09-30 19:33:19 +0530260 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
Prafulla Wadaskarf8e42622010-09-30 19:33:19 +0530261 /*
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 */
Stefan Roese420abea2015-08-10 15:11:27 +0200267 if (mvebu_sdram_bar(i) != size)
Prafulla Wadaskarf8e42622010-09-30 19:33:19 +0530268 break;
269
Stefan Roese2cc5fad2014-10-22 12:13:05 +0200270 /*
271 * Don't report more than 3GiB of SDRAM, otherwise there is no
272 * address space left for the internal registers etc.
273 */
Stefan Roese420abea2015-08-10 15:11:27 +0200274 size += mvebu_sdram_bs(i);
Stefan Roese10fa44b2018-10-22 14:21:17 +0200275 if (size > MVEBU_SDRAM_SIZE_MAX)
276 size = MVEBU_SDRAM_SIZE_MAX;
Prafulla Wadaskarf8e42622010-09-30 19:33:19 +0530277 }
Tanmay Upadhyay9a5ff782010-10-28 20:06:22 +0530278
Stefan Roesef3345e62015-08-06 14:43:13 +0200279 if (ecc_enabled())
280 dram_ecc_scrubbing();
281
Stefan Roese420abea2015-08-10 15:11:27 +0200282 gd->ram_size = size;
283
Prafulla Wadaskarf8e42622010-09-30 19:33:19 +0530284 return 0;
285}
286
287/*
288 * If this function is not defined here,
289 * board.c alters dram bank zero configuration defined above.
290 */
Simon Glass2f949c32017-03-31 08:40:32 -0600291int dram_init_banksize(void)
Prafulla Wadaskarf8e42622010-09-30 19:33:19 +0530292{
Stefan Roese420abea2015-08-10 15:11:27 +0200293 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;
Stefan Roese10fa44b2018-10-22 14:21:17 +0200302 if (size > MVEBU_SDRAM_SIZE_MAX)
Stefan Roese420abea2015-08-10 15:11:27 +0200303 mvebu_sdram_bs_set(i, 0x40000000);
304 }
Simon Glass2f949c32017-03-31 08:40:32 -0600305
306 return 0;
Prafulla Wadaskarf8e42622010-09-30 19:33:19 +0530307}
Stefan Roese7a96ee92015-08-03 13:15:31 +0200308
Stefan Roeseeb083e52015-12-21 13:56:33 +0100309#if defined(CONFIG_ARCH_MVEBU)
Stefan Roese7a96ee92015-08-03 13:15:31 +0200310void board_add_ram_info(int use_default)
311{
Stefan Roese2a539c82015-12-21 12:36:40 +0100312 struct sar_freq_modes sar_freq;
Joshua Scott2e161b92017-09-04 17:38:32 +1200313 int mode;
314 int width;
Stefan Roese2a539c82015-12-21 12:36:40 +0100315
316 get_sar_freq(&sar_freq);
317 printf(" (%d MHz, ", sar_freq.d_clk);
318
Joshua Scott2e161b92017-09-04 17:38:32 +1200319 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
Stefan Roesef3345e62015-08-06 14:43:13 +0200333 if (ecc_enabled())
Stefan Roese2a539c82015-12-21 12:36:40 +0100334 printf("ECC");
Stefan Roese7a96ee92015-08-03 13:15:31 +0200335 else
Stefan Roese2a539c82015-12-21 12:36:40 +0100336 printf("ECC not");
Stefan Roese7a96ee92015-08-03 13:15:31 +0200337 printf(" enabled)");
338}
Stefan Roese2a539c82015-12-21 12:36:40 +0100339#endif