1
2
3
4
5
6
7#include <common.h>
8#include <cpu_func.h>
9#include <dm.h>
10#include <fdtdec.h>
11#include <init.h>
12#include <linux/bitops.h>
13#include <linux/libfdt.h>
14#include <asm/io.h>
15#include <asm/system.h>
16#include <asm/arch/cpu.h>
17#include <asm/arch/soc.h>
18#include <asm/armv8/mmu.h>
19#include <sort.h>
20
21
22#define MVEBU_GPIO_NB_REG_BASE (MVEBU_REGISTER(0x13800))
23
24#define MVEBU_TEST_PIN_LATCH_N (MVEBU_GPIO_NB_REG_BASE + 0x8)
25#define MVEBU_XTAL_MODE_MASK BIT(9)
26#define MVEBU_XTAL_MODE_OFFS 9
27#define MVEBU_XTAL_CLOCK_25MHZ 0x0
28#define MVEBU_XTAL_CLOCK_40MHZ 0x1
29
30#define MVEBU_NB_WARM_RST_REG (MVEBU_GPIO_NB_REG_BASE + 0x40)
31#define MVEBU_NB_WARM_RST_MAGIC_NUM 0x1d1e
32
33
34#define MVEBU_CPU_DEC_WIN_REG_BASE (size_t)(MVEBU_REGISTER(0xcf00))
35#define MVEBU_CPU_DEC_WIN_CTRL(w) \
36 (MVEBU_CPU_DEC_WIN_REG_BASE + ((w) << 4))
37#define MVEBU_CPU_DEC_WIN_CTRL_EN BIT(0)
38#define MVEBU_CPU_DEC_WIN_CTRL_TGT_MASK 0xf
39#define MVEBU_CPU_DEC_WIN_CTRL_TGT_OFFS 4
40#define MVEBU_CPU_DEC_WIN_CTRL_TGT_DRAM 0
41#define MVEBU_CPU_DEC_WIN_CTRL_TGT_PCIE 2
42#define MVEBU_CPU_DEC_WIN_SIZE(w) (MVEBU_CPU_DEC_WIN_CTRL(w) + 0x4)
43#define MVEBU_CPU_DEC_WIN_BASE(w) (MVEBU_CPU_DEC_WIN_CTRL(w) + 0x8)
44#define MVEBU_CPU_DEC_WIN_REMAP(w) (MVEBU_CPU_DEC_WIN_CTRL(w) + 0xc)
45#define MVEBU_CPU_DEC_WIN_GRANULARITY 16
46#define MVEBU_CPU_DEC_WINS 5
47
48#define MAX_MEM_MAP_REGIONS (MVEBU_CPU_DEC_WINS + 2)
49
50#define A3700_PTE_BLOCK_NORMAL \
51 (PTE_BLOCK_MEMTYPE(MT_NORMAL) | PTE_BLOCK_INNER_SHARE)
52#define A3700_PTE_BLOCK_DEVICE \
53 (PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | PTE_BLOCK_NON_SHARE)
54
55#define PCIE_PATH "/soc/pcie@d0070000"
56
57DECLARE_GLOBAL_DATA_PTR;
58
59static struct mm_region mvebu_mem_map[MAX_MEM_MAP_REGIONS] = {
60 {
61
62
63
64
65 .phys = SOC_REGS_PHY_BASE,
66 .virt = SOC_REGS_PHY_BASE,
67 .size = 0x02000000UL,
68 .attrs = A3700_PTE_BLOCK_DEVICE
69 },
70};
71
72struct mm_region *mem_map = mvebu_mem_map;
73
74static int get_cpu_dec_win(int win, u32 *tgt, u32 *base, u32 *size)
75{
76 u32 reg;
77
78 reg = readl(MVEBU_CPU_DEC_WIN_CTRL(win));
79 if (!(reg & MVEBU_CPU_DEC_WIN_CTRL_EN))
80 return -1;
81
82 if (tgt) {
83 reg >>= MVEBU_CPU_DEC_WIN_CTRL_TGT_OFFS;
84 reg &= MVEBU_CPU_DEC_WIN_CTRL_TGT_MASK;
85 *tgt = reg;
86 }
87
88 if (base) {
89 reg = readl(MVEBU_CPU_DEC_WIN_BASE(win));
90 *base = reg << MVEBU_CPU_DEC_WIN_GRANULARITY;
91 }
92
93 if (size) {
94
95
96
97
98
99 reg = readl(MVEBU_CPU_DEC_WIN_SIZE(win));
100 *size = ((reg + 1) << MVEBU_CPU_DEC_WIN_GRANULARITY);
101 }
102
103 return 0;
104}
105
106
107
108
109
110static void build_mem_map(void)
111{
112 int win, region;
113
114 region = 1;
115 for (win = 0; win < MVEBU_CPU_DEC_WINS; ++win) {
116 u32 base, tgt, size;
117 u64 attrs;
118
119
120 if (get_cpu_dec_win(win, &tgt, &base, &size))
121 continue;
122
123 if (tgt == MVEBU_CPU_DEC_WIN_CTRL_TGT_DRAM)
124 attrs = A3700_PTE_BLOCK_NORMAL;
125 else if (tgt == MVEBU_CPU_DEC_WIN_CTRL_TGT_PCIE)
126 attrs = A3700_PTE_BLOCK_DEVICE;
127 else
128
129 continue;
130
131 mvebu_mem_map[region].phys = base;
132 mvebu_mem_map[region].virt = base;
133 mvebu_mem_map[region].size = size;
134 mvebu_mem_map[region].attrs = attrs;
135 ++region;
136 }
137
138
139 mvebu_mem_map[region].size = 0;
140 mvebu_mem_map[region].attrs = 0;
141}
142
143void enable_caches(void)
144{
145 build_mem_map();
146
147 icache_enable();
148 dcache_enable();
149}
150
151int a3700_dram_init(void)
152{
153 int win;
154
155 gd->ram_size = 0;
156 for (win = 0; win < MVEBU_CPU_DEC_WINS; ++win) {
157 u32 base, tgt, size;
158
159
160 if (get_cpu_dec_win(win, &tgt, &base, &size))
161 continue;
162
163
164 if (tgt != MVEBU_CPU_DEC_WIN_CTRL_TGT_DRAM)
165 continue;
166
167
168
169
170
171
172
173 gd->ram_size += get_ram_size((void *)(size_t)base, size);
174 }
175
176 return 0;
177}
178
179struct a3700_dram_window {
180 size_t base, size;
181};
182
183static int dram_win_cmp(const void *a, const void *b)
184{
185 size_t ab, bb;
186
187 ab = ((const struct a3700_dram_window *)a)->base;
188 bb = ((const struct a3700_dram_window *)b)->base;
189
190 if (ab < bb)
191 return -1;
192 else if (ab > bb)
193 return 1;
194 else
195 return 0;
196}
197
198int a3700_dram_init_banksize(void)
199{
200 struct a3700_dram_window dram_wins[MVEBU_CPU_DEC_WINS];
201 int bank, win, ndram_wins;
202 u32 last_end;
203 size_t size;
204
205 ndram_wins = 0;
206 for (win = 0; win < MVEBU_CPU_DEC_WINS; ++win) {
207 u32 base, tgt, size;
208
209
210 if (get_cpu_dec_win(win, &tgt, &base, &size))
211 continue;
212
213
214 if (tgt != MVEBU_CPU_DEC_WIN_CTRL_TGT_DRAM)
215 continue;
216
217 dram_wins[win].base = base;
218 dram_wins[win].size = size;
219 ++ndram_wins;
220 }
221
222 qsort(dram_wins, ndram_wins, sizeof(dram_wins[0]), dram_win_cmp);
223
224 bank = 0;
225 last_end = -1;
226
227 for (win = 0; win < ndram_wins; ++win) {
228
229 size = get_ram_size((void *)dram_wins[win].base,
230 dram_wins[win].size);
231
232
233
234
235
236
237
238 if (last_end == dram_wins[win].base) {
239 gd->bd->bi_dram[bank - 1].size += size;
240 last_end += size;
241 } else {
242 if (bank == CONFIG_NR_DRAM_BANKS) {
243 printf("Need more CONFIG_NR_DRAM_BANKS\n");
244 return -ENOBUFS;
245 }
246
247 gd->bd->bi_dram[bank].start = dram_wins[win].base;
248 gd->bd->bi_dram[bank].size = size;
249 last_end = dram_wins[win].base + size;
250 ++bank;
251 }
252 }
253
254
255
256
257
258 for (; bank < CONFIG_NR_DRAM_BANKS; ++bank) {
259 gd->bd->bi_dram[bank].start = 0;
260 gd->bd->bi_dram[bank].size = 0;
261 }
262
263 return 0;
264}
265
266static u32 find_pcie_window_base(void)
267{
268 int win;
269
270 for (win = 0; win < MVEBU_CPU_DEC_WINS; ++win) {
271 u32 base, tgt;
272
273
274 if (get_cpu_dec_win(win, &tgt, &base, NULL))
275 continue;
276
277 if (tgt == MVEBU_CPU_DEC_WIN_CTRL_TGT_PCIE)
278 return base;
279 }
280
281 return -1;
282}
283
284int a3700_fdt_fix_pcie_regions(void *blob)
285{
286 u32 new_ranges[14], base;
287 const u32 *ranges;
288 int node, len;
289
290 node = fdt_path_offset(blob, PCIE_PATH);
291 if (node < 0)
292 return node;
293
294 ranges = fdt_getprop(blob, node, "ranges", &len);
295 if (!ranges)
296 return -ENOENT;
297
298 if (len != sizeof(new_ranges))
299 return -EINVAL;
300
301 memcpy(new_ranges, ranges, len);
302
303 base = find_pcie_window_base();
304 if (base == -1)
305 return -ENOENT;
306
307 new_ranges[2] = cpu_to_fdt32(base);
308 new_ranges[4] = new_ranges[2];
309
310 new_ranges[9] = cpu_to_fdt32(base + 0x1000000);
311 new_ranges[11] = new_ranges[9];
312
313 return fdt_setprop_inplace(blob, node, "ranges", new_ranges, len);
314}
315
316void reset_cpu(ulong ignored)
317{
318
319
320
321
322 writel(MVEBU_NB_WARM_RST_MAGIC_NUM, MVEBU_NB_WARM_RST_REG);
323}
324
325
326
327
328
329
330u32 get_ref_clk(void)
331{
332 u32 regval;
333
334 regval = (readl(MVEBU_TEST_PIN_LATCH_N) & MVEBU_XTAL_MODE_MASK) >>
335 MVEBU_XTAL_MODE_OFFS;
336
337 if (regval == MVEBU_XTAL_CLOCK_25MHZ)
338 return 25;
339 else
340 return 40;
341}
342