arm64: mvebu: a37xx: improve code determining memory info structures
[oweals/u-boot.git] / arch / arm / mach-mvebu / armada3700 / cpu.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2016 Stefan Roese <sr@denx.de>
4  * Copyright (C) 2020 Marek Behun <marek.behun@nic.cz>
5  */
6
7 #include <common.h>
8 #include <cpu_func.h>
9 #include <dm.h>
10 #include <fdtdec.h>
11 #include <linux/libfdt.h>
12 #include <asm/io.h>
13 #include <asm/system.h>
14 #include <asm/arch/cpu.h>
15 #include <asm/arch/soc.h>
16 #include <asm/armv8/mmu.h>
17 #include <sort.h>
18
19 /* Armada 3700 */
20 #define MVEBU_GPIO_NB_REG_BASE          (MVEBU_REGISTER(0x13800))
21
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
27
28 #define MVEBU_NB_WARM_RST_REG           (MVEBU_GPIO_NB_REG_BASE + 0x40)
29 #define MVEBU_NB_WARM_RST_MAGIC_NUM     0x1d1e
30
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
45
46 #define MAX_MEM_MAP_REGIONS             (MVEBU_CPU_DEC_WINS + 2)
47
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)
52
53 DECLARE_GLOBAL_DATA_PTR;
54
55 static struct mm_region mvebu_mem_map[MAX_MEM_MAP_REGIONS] = {
56         {
57                 /*
58                  * SRAM, MMIO regions
59                  * Don't remove this, a3700_build_mem_map needs it.
60                  */
61                 .phys = SOC_REGS_PHY_BASE,
62                 .virt = SOC_REGS_PHY_BASE,
63                 .size = 0x02000000UL,   /* 32MiB internal registers */
64                 .attrs = A3700_PTE_BLOCK_DEVICE
65         },
66 };
67
68 struct mm_region *mem_map = mvebu_mem_map;
69
70 static int get_cpu_dec_win(int win, u32 *tgt, u32 *base, u32 *size)
71 {
72         u32 reg;
73
74         reg = readl(MVEBU_CPU_DEC_WIN_CTRL(win));
75         if (!(reg & MVEBU_CPU_DEC_WIN_CTRL_EN))
76                 return -1;
77
78         if (tgt) {
79                 reg >>= MVEBU_CPU_DEC_WIN_CTRL_TGT_OFFS;
80                 reg &= MVEBU_CPU_DEC_WIN_CTRL_TGT_MASK;
81                 *tgt = reg;
82         }
83
84         if (base) {
85                 reg = readl(MVEBU_CPU_DEC_WIN_BASE(win));
86                 *base = reg << MVEBU_CPU_DEC_WIN_GRANULARITY;
87         }
88
89         if (size) {
90                 /*
91                  * Window size is encoded as the number of 1s from LSB to MSB,
92                  * followed by 0s. The number of 1s specifies the size in 64 KiB
93                  * granularity.
94                  */
95                 reg = readl(MVEBU_CPU_DEC_WIN_SIZE(win));
96                 *size = ((reg + 1) << MVEBU_CPU_DEC_WIN_GRANULARITY);
97         }
98
99         return 0;
100 }
101
102 /*
103  * Builds mem_map according to CPU Address Decoder settings, which were set by
104  * the TIMH image on the Cortex-M3 secure processor, or by ARM Trusted Firmware
105  */
106 static void build_mem_map(void)
107 {
108         int win, region;
109
110         region = 1;
111         for (win = 0; win < MVEBU_CPU_DEC_WINS; ++win) {
112                 u32 base, tgt, size;
113                 u64 attrs;
114
115                 /* skip disabled windows */
116                 if (get_cpu_dec_win(win, &tgt, &base, &size))
117                         continue;
118
119                 if (tgt == MVEBU_CPU_DEC_WIN_CTRL_TGT_DRAM)
120                         attrs = A3700_PTE_BLOCK_NORMAL;
121                 else if (tgt == MVEBU_CPU_DEC_WIN_CTRL_TGT_PCIE)
122                         attrs = A3700_PTE_BLOCK_DEVICE;
123                 else
124                         /* skip windows with other targets */
125                         continue;
126
127                 mvebu_mem_map[region].phys = base;
128                 mvebu_mem_map[region].virt = base;
129                 mvebu_mem_map[region].size = size;
130                 mvebu_mem_map[region].attrs = attrs;
131                 ++region;
132         }
133
134         /* add list terminator */
135         mvebu_mem_map[region].size = 0;
136         mvebu_mem_map[region].attrs = 0;
137 }
138
139 void enable_caches(void)
140 {
141         build_mem_map();
142
143         icache_enable();
144         dcache_enable();
145 }
146
147 int a3700_dram_init(void)
148 {
149         int win;
150
151         gd->ram_size = 0;
152         for (win = 0; win < MVEBU_CPU_DEC_WINS; ++win) {
153                 u32 base, tgt, size;
154
155                 /* skip disabled windows */
156                 if (get_cpu_dec_win(win, &tgt, &base, &size))
157                         continue;
158
159                 /* skip non-DRAM windows */
160                 if (tgt != MVEBU_CPU_DEC_WIN_CTRL_TGT_DRAM)
161                         continue;
162
163                 /*
164                  * It is possible that one image was built for boards with
165                  * different RAM sizes, for example 512 MiB and 1 GiB.
166                  * We therefore try to determine the actual RAM size in the
167                  * window with get_ram_size.
168                  */
169                 gd->ram_size += get_ram_size((void *)(size_t)base, size);
170         }
171
172         return 0;
173 }
174
175 struct a3700_dram_window {
176         size_t base, size;
177 };
178
179 static int dram_win_cmp(const void *a, const void *b)
180 {
181         size_t ab, bb;
182
183         ab = ((const struct a3700_dram_window *)a)->base;
184         bb = ((const struct a3700_dram_window *)b)->base;
185
186         if (ab < bb)
187                 return -1;
188         else if (ab > bb)
189                 return 1;
190         else
191                 return 0;
192 }
193
194 int a3700_dram_init_banksize(void)
195 {
196         struct a3700_dram_window dram_wins[MVEBU_CPU_DEC_WINS];
197         int bank, win, ndram_wins;
198         u32 last_end;
199         size_t size;
200
201         ndram_wins = 0;
202         for (win = 0; win < MVEBU_CPU_DEC_WINS; ++win) {
203                 u32 base, tgt, size;
204
205                 /* skip disabled windows */
206                 if (get_cpu_dec_win(win, &tgt, &base, &size))
207                         continue;
208
209                 /* skip non-DRAM windows */
210                 if (tgt != MVEBU_CPU_DEC_WIN_CTRL_TGT_DRAM)
211                         continue;
212
213                 dram_wins[win].base = base;
214                 dram_wins[win].size = size;
215                 ++ndram_wins;
216         }
217
218         qsort(dram_wins, ndram_wins, sizeof(dram_wins[0]), dram_win_cmp);
219
220         bank = 0;
221         last_end = -1;
222
223         for (win = 0; win < ndram_wins; ++win) {
224                 /* again determining actual RAM size as in a3700_dram_init */
225                 size = get_ram_size((void *)dram_wins[win].base,
226                                     dram_wins[win].size);
227
228                 /*
229                  * Check if previous window ends as the current starts. If yes,
230                  * merge these windows into one "bank". This is possible by this
231                  * simple check thanks to mem_map regions being qsorted in
232                  * build_mem_map.
233                  */
234                 if (last_end == dram_wins[win].base) {
235                         gd->bd->bi_dram[bank - 1].size += size;
236                         last_end += size;
237                 } else {
238                         if (bank == CONFIG_NR_DRAM_BANKS) {
239                                 printf("Need more CONFIG_NR_DRAM_BANKS\n");
240                                 return -ENOBUFS;
241                         }
242
243                         gd->bd->bi_dram[bank].start = dram_wins[win].base;
244                         gd->bd->bi_dram[bank].size = size;
245                         last_end = dram_wins[win].base + size;
246                         ++bank;
247                 }
248         }
249
250         /*
251          * If there is more place for DRAM BANKS definitions than needed, fill
252          * the rest with zeros.
253          */
254         for (; bank < CONFIG_NR_DRAM_BANKS; ++bank) {
255                 gd->bd->bi_dram[bank].start = 0;
256                 gd->bd->bi_dram[bank].size = 0;
257         }
258
259         return 0;
260 }
261
262 void reset_cpu(ulong ignored)
263 {
264         /*
265          * Write magic number of 0x1d1e to North Bridge Warm Reset register
266          * to trigger warm reset
267          */
268         writel(MVEBU_NB_WARM_RST_MAGIC_NUM, MVEBU_NB_WARM_RST_REG);
269 }
270
271 /*
272  * get_ref_clk
273  *
274  * return: reference clock in MHz (25 or 40)
275  */
276 u32 get_ref_clk(void)
277 {
278         u32 regval;
279
280         regval = (readl(MVEBU_TEST_PIN_LATCH_N) & MVEBU_XTAL_MODE_MASK) >>
281                 MVEBU_XTAL_MODE_OFFS;
282
283         if (regval == MVEBU_XTAL_CLOCK_25MHZ)
284                 return 25;
285         else
286                 return 40;
287 }