1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2016 Stefan Roese <sr@denx.de>
4 * Copyright (C) 2020 Marek Behun <marek.behun@nic.cz>
11 #include <linux/libfdt.h>
13 #include <asm/system.h>
14 #include <asm/arch/cpu.h>
15 #include <asm/arch/soc.h>
16 #include <asm/armv8/mmu.h>
20 #define MVEBU_GPIO_NB_REG_BASE (MVEBU_REGISTER(0x13800))
22 #define MVEBU_TEST_PIN_LATCH_N (MVEBU_GPIO_NB_REG_BASE + 0x8)
23 #define MVEBU_XTAL_MODE_MASK BIT(9)
24 #define MVEBU_XTAL_MODE_OFFS 9
25 #define MVEBU_XTAL_CLOCK_25MHZ 0x0
26 #define MVEBU_XTAL_CLOCK_40MHZ 0x1
28 #define MVEBU_NB_WARM_RST_REG (MVEBU_GPIO_NB_REG_BASE + 0x40)
29 #define MVEBU_NB_WARM_RST_MAGIC_NUM 0x1d1e
31 /* Armada 3700 CPU Address Decoder registers */
32 #define MVEBU_CPU_DEC_WIN_REG_BASE (size_t)(MVEBU_REGISTER(0xcf00))
33 #define MVEBU_CPU_DEC_WIN_CTRL(w) \
34 (MVEBU_CPU_DEC_WIN_REG_BASE + ((w) << 4))
35 #define MVEBU_CPU_DEC_WIN_CTRL_EN BIT(0)
36 #define MVEBU_CPU_DEC_WIN_CTRL_TGT_MASK 0xf
37 #define MVEBU_CPU_DEC_WIN_CTRL_TGT_OFFS 4
38 #define MVEBU_CPU_DEC_WIN_CTRL_TGT_DRAM 0
39 #define MVEBU_CPU_DEC_WIN_CTRL_TGT_PCIE 2
40 #define MVEBU_CPU_DEC_WIN_SIZE(w) (MVEBU_CPU_DEC_WIN_CTRL(w) + 0x4)
41 #define MVEBU_CPU_DEC_WIN_BASE(w) (MVEBU_CPU_DEC_WIN_CTRL(w) + 0x8)
42 #define MVEBU_CPU_DEC_WIN_REMAP(w) (MVEBU_CPU_DEC_WIN_CTRL(w) + 0xc)
43 #define MVEBU_CPU_DEC_WIN_GRANULARITY 16
44 #define MVEBU_CPU_DEC_WINS 5
46 #define MAX_MEM_MAP_REGIONS (MVEBU_CPU_DEC_WINS + 2)
48 #define A3700_PTE_BLOCK_NORMAL \
49 (PTE_BLOCK_MEMTYPE(MT_NORMAL) | PTE_BLOCK_INNER_SHARE)
50 #define A3700_PTE_BLOCK_DEVICE \
51 (PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | PTE_BLOCK_NON_SHARE)
53 #define PCIE_PATH "/soc/pcie@d0070000"
55 DECLARE_GLOBAL_DATA_PTR;
57 static struct mm_region mvebu_mem_map[MAX_MEM_MAP_REGIONS] = {
61 * Don't remove this, a3700_build_mem_map needs it.
63 .phys = SOC_REGS_PHY_BASE,
64 .virt = SOC_REGS_PHY_BASE,
65 .size = 0x02000000UL, /* 32MiB internal registers */
66 .attrs = A3700_PTE_BLOCK_DEVICE
70 struct mm_region *mem_map = mvebu_mem_map;
72 static int get_cpu_dec_win(int win, u32 *tgt, u32 *base, u32 *size)
76 reg = readl(MVEBU_CPU_DEC_WIN_CTRL(win));
77 if (!(reg & MVEBU_CPU_DEC_WIN_CTRL_EN))
81 reg >>= MVEBU_CPU_DEC_WIN_CTRL_TGT_OFFS;
82 reg &= MVEBU_CPU_DEC_WIN_CTRL_TGT_MASK;
87 reg = readl(MVEBU_CPU_DEC_WIN_BASE(win));
88 *base = reg << MVEBU_CPU_DEC_WIN_GRANULARITY;
93 * Window size is encoded as the number of 1s from LSB to MSB,
94 * followed by 0s. The number of 1s specifies the size in 64 KiB
97 reg = readl(MVEBU_CPU_DEC_WIN_SIZE(win));
98 *size = ((reg + 1) << MVEBU_CPU_DEC_WIN_GRANULARITY);
105 * Builds mem_map according to CPU Address Decoder settings, which were set by
106 * the TIMH image on the Cortex-M3 secure processor, or by ARM Trusted Firmware
108 static void build_mem_map(void)
113 for (win = 0; win < MVEBU_CPU_DEC_WINS; ++win) {
117 /* skip disabled windows */
118 if (get_cpu_dec_win(win, &tgt, &base, &size))
121 if (tgt == MVEBU_CPU_DEC_WIN_CTRL_TGT_DRAM)
122 attrs = A3700_PTE_BLOCK_NORMAL;
123 else if (tgt == MVEBU_CPU_DEC_WIN_CTRL_TGT_PCIE)
124 attrs = A3700_PTE_BLOCK_DEVICE;
126 /* skip windows with other targets */
129 mvebu_mem_map[region].phys = base;
130 mvebu_mem_map[region].virt = base;
131 mvebu_mem_map[region].size = size;
132 mvebu_mem_map[region].attrs = attrs;
136 /* add list terminator */
137 mvebu_mem_map[region].size = 0;
138 mvebu_mem_map[region].attrs = 0;
141 void enable_caches(void)
149 int a3700_dram_init(void)
154 for (win = 0; win < MVEBU_CPU_DEC_WINS; ++win) {
157 /* skip disabled windows */
158 if (get_cpu_dec_win(win, &tgt, &base, &size))
161 /* skip non-DRAM windows */
162 if (tgt != MVEBU_CPU_DEC_WIN_CTRL_TGT_DRAM)
166 * It is possible that one image was built for boards with
167 * different RAM sizes, for example 512 MiB and 1 GiB.
168 * We therefore try to determine the actual RAM size in the
169 * window with get_ram_size.
171 gd->ram_size += get_ram_size((void *)(size_t)base, size);
177 struct a3700_dram_window {
181 static int dram_win_cmp(const void *a, const void *b)
185 ab = ((const struct a3700_dram_window *)a)->base;
186 bb = ((const struct a3700_dram_window *)b)->base;
196 int a3700_dram_init_banksize(void)
198 struct a3700_dram_window dram_wins[MVEBU_CPU_DEC_WINS];
199 int bank, win, ndram_wins;
204 for (win = 0; win < MVEBU_CPU_DEC_WINS; ++win) {
207 /* skip disabled windows */
208 if (get_cpu_dec_win(win, &tgt, &base, &size))
211 /* skip non-DRAM windows */
212 if (tgt != MVEBU_CPU_DEC_WIN_CTRL_TGT_DRAM)
215 dram_wins[win].base = base;
216 dram_wins[win].size = size;
220 qsort(dram_wins, ndram_wins, sizeof(dram_wins[0]), dram_win_cmp);
225 for (win = 0; win < ndram_wins; ++win) {
226 /* again determining actual RAM size as in a3700_dram_init */
227 size = get_ram_size((void *)dram_wins[win].base,
228 dram_wins[win].size);
231 * Check if previous window ends as the current starts. If yes,
232 * merge these windows into one "bank". This is possible by this
233 * simple check thanks to mem_map regions being qsorted in
236 if (last_end == dram_wins[win].base) {
237 gd->bd->bi_dram[bank - 1].size += size;
240 if (bank == CONFIG_NR_DRAM_BANKS) {
241 printf("Need more CONFIG_NR_DRAM_BANKS\n");
245 gd->bd->bi_dram[bank].start = dram_wins[win].base;
246 gd->bd->bi_dram[bank].size = size;
247 last_end = dram_wins[win].base + size;
253 * If there is more place for DRAM BANKS definitions than needed, fill
254 * the rest with zeros.
256 for (; bank < CONFIG_NR_DRAM_BANKS; ++bank) {
257 gd->bd->bi_dram[bank].start = 0;
258 gd->bd->bi_dram[bank].size = 0;
264 static u32 find_pcie_window_base(void)
268 for (win = 0; win < MVEBU_CPU_DEC_WINS; ++win) {
271 /* skip disabled windows */
272 if (get_cpu_dec_win(win, &tgt, &base, NULL))
275 if (tgt == MVEBU_CPU_DEC_WIN_CTRL_TGT_PCIE)
282 int a3700_fdt_fix_pcie_regions(void *blob)
284 u32 new_ranges[14], base;
288 node = fdt_path_offset(blob, PCIE_PATH);
292 ranges = fdt_getprop(blob, node, "ranges", &len);
296 if (len != sizeof(new_ranges))
299 memcpy(new_ranges, ranges, len);
301 base = find_pcie_window_base();
305 new_ranges[2] = cpu_to_fdt32(base);
306 new_ranges[4] = new_ranges[2];
308 new_ranges[9] = cpu_to_fdt32(base + 0x1000000);
309 new_ranges[11] = new_ranges[9];
311 return fdt_setprop_inplace(blob, node, "ranges", new_ranges, len);
314 void reset_cpu(ulong ignored)
317 * Write magic number of 0x1d1e to North Bridge Warm Reset register
318 * to trigger warm reset
320 writel(MVEBU_NB_WARM_RST_MAGIC_NUM, MVEBU_NB_WARM_RST_REG);
326 * return: reference clock in MHz (25 or 40)
328 u32 get_ref_clk(void)
332 regval = (readl(MVEBU_TEST_PIN_LATCH_N) & MVEBU_XTAL_MODE_MASK) >>
333 MVEBU_XTAL_MODE_OFFS;
335 if (regval == MVEBU_XTAL_CLOCK_25MHZ)