3 * Marvell Semiconductor <www.marvell.com>
4 * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
6 * SPDX-License-Identifier: GPL-2.0+
12 #include <asm/arch/cpu.h>
13 #include <asm/arch/soc.h>
15 #ifdef CONFIG_SYS_MVEBU_DDR_A38X
16 #include "../../../drivers/ddr/marvell/a38x/ddr3_init.h"
18 #ifdef CONFIG_SYS_MVEBU_DDR_AXP
19 #include "../../../drivers/ddr/marvell/axp/ddr3_init.h"
22 DECLARE_GLOBAL_DATA_PTR;
29 struct sdram_addr_dec {
30 struct sdram_bank sdram_bank[4];
33 #define REG_CPUCS_WIN_ENABLE (1 << 0)
34 #define REG_CPUCS_WIN_WR_PROTECT (1 << 1)
35 #define REG_CPUCS_WIN_WIN0_CS(x) (((x) & 0x3) << 2)
36 #define REG_CPUCS_WIN_SIZE(x) (((x) & 0xff) << 24)
38 #define SDRAM_SIZE_MAX 0xc0000000
41 * mvebu_sdram_bar - reads SDRAM Base Address Register
43 u32 mvebu_sdram_bar(enum memory_bank bank)
45 struct sdram_addr_dec *base =
46 (struct sdram_addr_dec *)MVEBU_SDRAM_BASE;
48 u32 enable = 0x01 & readl(&base->sdram_bank[bank].win_sz);
50 if ((!enable) || (bank > BANK3))
53 result = readl(&base->sdram_bank[bank].win_bar);
58 * mvebu_sdram_bs_set - writes SDRAM Bank size
60 static void mvebu_sdram_bs_set(enum memory_bank bank, u32 size)
62 struct sdram_addr_dec *base =
63 (struct sdram_addr_dec *)MVEBU_SDRAM_BASE;
64 /* Read current register value */
65 u32 reg = readl(&base->sdram_bank[bank].win_sz);
67 /* Clear window size */
68 reg &= ~REG_CPUCS_WIN_SIZE(0xFF);
70 /* Set new window size */
71 reg |= REG_CPUCS_WIN_SIZE((size - 1) >> 24);
73 writel(reg, &base->sdram_bank[bank].win_sz);
77 * mvebu_sdram_bs - reads SDRAM Bank size
79 u32 mvebu_sdram_bs(enum memory_bank bank)
81 struct sdram_addr_dec *base =
82 (struct sdram_addr_dec *)MVEBU_SDRAM_BASE;
84 u32 enable = 0x01 & readl(&base->sdram_bank[bank].win_sz);
86 if ((!enable) || (bank > BANK3))
88 result = 0xff000000 & readl(&base->sdram_bank[bank].win_sz);
93 void mvebu_sdram_size_adjust(enum memory_bank bank)
97 /* probe currently equipped RAM size */
98 size = get_ram_size((void *)mvebu_sdram_bar(bank),
99 mvebu_sdram_bs(bank));
101 /* adjust SDRAM window size accordingly */
102 mvebu_sdram_bs_set(bank, size);
110 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
112 * It is assumed that all memory banks are consecutive
114 * If the gap is found, ram_size will be reported for
115 * consecutive memory only
117 if (mvebu_sdram_bar(i) != size)
121 * Don't report more than 3GiB of SDRAM, otherwise there is no
122 * address space left for the internal registers etc.
124 size += mvebu_sdram_bs(i);
125 if (size > SDRAM_SIZE_MAX)
126 size = SDRAM_SIZE_MAX;
129 for (; i < CONFIG_NR_DRAM_BANKS; i++) {
130 /* If above loop terminated prematurely, we need to set
131 * remaining banks' start address & size as 0. Otherwise other
132 * u-boot functions and Linux kernel gets wrong values which
133 * could result in crash */
134 gd->bd->bi_dram[i].start = 0;
135 gd->bd->bi_dram[i].size = 0;
144 * If this function is not defined here,
145 * board.c alters dram bank zero configuration defined above.
147 void dram_init_banksize(void)
152 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
153 gd->bd->bi_dram[i].start = mvebu_sdram_bar(i);
154 gd->bd->bi_dram[i].size = mvebu_sdram_bs(i);
156 /* Clip the banksize to 1GiB if it exceeds the max size */
157 size += gd->bd->bi_dram[i].size;
158 if (size > SDRAM_SIZE_MAX)
159 mvebu_sdram_bs_set(i, 0x40000000);
163 void board_add_ram_info(int use_default)
167 reg = reg_read(REG_SDRAM_CONFIG_ADDR);
168 if (reg & (1 << REG_SDRAM_CONFIG_ECC_OFFS))