blob: 3fde9fbc47083baebc1168b1719828301bab98ce [file] [log] [blame]
Aaron Williams0dc4ab92020-06-30 12:08:56 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2020 Marvell International Ltd.
4 */
5
Stefan Roese47dedfb2021-04-07 09:12:32 +02006#include <dm.h>
7#include <dm/uclass.h>
8#include <env.h>
9#include <iomux.h>
Aaron Williams0dc4ab92020-06-30 12:08:56 +020010#include <asm/global_data.h>
11#include <linux/bitfield.h>
12#include <linux/bitops.h>
13#include <linux/compat.h>
14#include <linux/io.h>
15#include <mach/clock.h>
16#include <mach/cavm-reg.h>
Stefan Roese47dedfb2021-04-07 09:12:32 +020017#include <mach/cvmx-bootmem.h>
Aaron Williams0dc4ab92020-06-30 12:08:56 +020018
19DECLARE_GLOBAL_DATA_PTR;
20
Stefan Roesefd569c82020-08-24 13:04:39 +020021/*
Stefan Roese47dedfb2021-04-07 09:12:32 +020022 * Important:
23 * This address cannot be changed as the PCI console tool relies on exactly
24 * this value!
25 */
26#define BOOTLOADER_BOOTMEM_DESC_ADDR 0x6c100
27#define BOOTLOADER_BOOTMEM_DESC_SPACE (BOOTLOADER_BOOTMEM_DESC_ADDR + 0x8)
28
29#define OCTEON_RESERVED_LOW_BOOT_MEM_SIZE (1024 * 1024)
30
31#define BOOTCMD_NAME "pci-bootcmd"
32#define CONSOLE_NAME "pci-console@0"
33#define OCTEON_BOOTLOADER_LOAD_MEM_NAME "__tmp_load"
34
35/*
Stefan Roesefd569c82020-08-24 13:04:39 +020036 * TRUE for devices having registers with little-endian byte
37 * order, FALSE for registers with native-endian byte order.
38 * PCI mandates little-endian, USB and SATA are configurable,
39 * but we chose little-endian for these.
40 *
41 * This table will be referened in the Octeon platform specific
42 * mangle-port.h header.
43 */
44const bool octeon_should_swizzle_table[256] = {
45 [0x00] = true, /* bootbus/CF */
46 [0x1b] = true, /* PCI mmio window */
47 [0x1c] = true, /* PCI mmio window */
48 [0x1d] = true, /* PCI mmio window */
49 [0x1e] = true, /* PCI mmio window */
50 [0x68] = true, /* OCTEON III USB */
51 [0x69] = true, /* OCTEON III USB */
52 [0x6c] = true, /* OCTEON III SATA */
53 [0x6f] = true, /* OCTEON II USB */
54};
55
Aaron Williams0dc4ab92020-06-30 12:08:56 +020056static int get_clocks(void)
57{
58 const u64 ref_clock = PLL_REF_CLK;
59 void __iomem *rst_boot;
60 u64 val;
61
62 rst_boot = ioremap(CAVM_RST_BOOT, 0);
63 val = ioread64(rst_boot);
64 gd->cpu_clk = ref_clock * FIELD_GET(RST_BOOT_C_MUL, val);
65 gd->bus_clk = ref_clock * FIELD_GET(RST_BOOT_PNR_MUL, val);
66
67 debug("%s: cpu: %lu, bus: %lu\n", __func__, gd->cpu_clk, gd->bus_clk);
68
69 return 0;
70}
71
72/* Early mach init code run from flash */
73int mach_cpu_init(void)
74{
75 void __iomem *mio_boot_reg_cfg0;
76
77 /* Remap boot-bus 0x1fc0.0000 -> 0x1f40.0000 */
78 /* ToDo: Move this to an early running bus (bootbus) DM driver */
79 mio_boot_reg_cfg0 = ioremap(CAVM_MIO_BOOT_REG_CFG0, 0);
80 clrsetbits_be64(mio_boot_reg_cfg0, 0xffff, 0x1f40);
81
82 /* Get clocks and store them in GD */
83 get_clocks();
84
85 return 0;
86}
87
88/**
89 * Returns number of cores
90 *
91 * @return number of CPU cores for the specified node
92 */
93static int cavm_octeon_num_cores(void)
94{
95 void __iomem *ciu_fuse;
96
97 ciu_fuse = ioremap(CAVM_CIU_FUSE, 0);
98 return fls64(ioread64(ciu_fuse) & 0xffffffffffff);
99}
100
101int print_cpuinfo(void)
102{
103 printf("SoC: Octeon CN73xx (%d cores)\n", cavm_octeon_num_cores());
104
105 return 0;
106}
Stefan Roese47dedfb2021-04-07 09:12:32 +0200107
108static int octeon_bootmem_init(void)
109{
110 int ret;
111
112 /* Call old single-node func: it uses only gd->ram_size */
113 ret = cvmx_bootmem_phy_mem_list_init(gd->ram_size,
114 OCTEON_RESERVED_LOW_BOOT_MEM_SIZE,
115 (void *)CKSEG0ADDR(BOOTLOADER_BOOTMEM_DESC_SPACE));
116 if (!ret) {
117 printf("FATAL: Error initializing bootmem list\n");
118 return -ENOSPC;
119 }
120
121 /*
122 * Put bootmem descriptor address in known location for host.
123 * Make sure it is not in kseg0, as we want physical address
124 */
125 writeq((u64)__cvmx_bootmem_internal_get_desc_ptr() & 0x7fffffffull,
126 (void *)CKSEG0ADDR(BOOTLOADER_BOOTMEM_DESC_ADDR));
127
128 debug("Reserving first 1MB of memory\n");
129 ret = cvmx_bootmem_reserve_memory(0, OCTEON_RESERVED_LOW_BOOT_MEM_SIZE,
130 "__low_reserved", 0);
131 if (!ret)
132 puts("Error reserving low 1MB of memory\n");
133
134#ifdef DEBUG
135 cvmx_bootmem_phy_list_print();
136#endif
137
138 return 0;
139}
140
141static int octeon_configure_load_memory(void)
142{
143 char *eptr;
144 u32 addr;
145 u32 size;
146 int ret;
147
148 eptr = env_get("octeon_reserved_mem_load_size");
149 if (!eptr || !strcmp("auto", eptr)) {
150 /*
151 * Pick a size that we think is appropriate.
152 * Please note that for small memory boards this guess
153 * will likely not be ideal.
154 * Please pick a specific size for boards/applications
155 * that require it.
156 */
157 if (gd->ram_size <= (256 << 20)) {
158 size = min_t(u64, (128 << 20),
159 ((gd->ram_size * 2) / 5) & ~0xFFFFF);
160 } else {
161 size = min_t(u64, (256 << 20),
162 ((gd->ram_size - (256 << 20)) / 3) & ~0xFFFFF);
163 }
164 } else {
165 size = simple_strtol(eptr, NULL, 16);
166 debug("octeon_reserved_mem_load_size=0x%08x\n", size);
167 }
168
169 if (size) {
170 debug("Linux reserved load size 0x%08x\n", size);
171 eptr = env_get("octeon_reserved_mem_load_base");
172 if (!eptr || !strcmp("auto", eptr)) {
173 u64 mem_top;
174 /*
175 * Leave some room for previous allocations that
176 * are made starting at the top of the low
177 * 256 Mbytes of DRAM
178 */
179 int adjust = (1 << 20);
180
181 if (gd->ram_size <= (512 << 20))
182 adjust = (17 << 20);
183
184 /* Put block at the top of DDR0, or bottom of DDR2 */
185 if ((gd->ram_size <= (256 << 20)) ||
186 (size > (gd->ram_size - (256 << 20)))) {
187 mem_top = min_t(u64, gd->ram_size - adjust,
188 (256 << 20) - adjust);
189 } else if ((gd->ram_size <= (512 << 20)) ||
190 (size > (gd->ram_size - (512 << 20)))) {
191 mem_top = min_t(u64, gd->ram_size - adjust,
192 (512 << 20) - adjust);
193 } else {
194 /*
195 * We have enough room, so set
196 * mem_top so that the block is
197 * at the base of the DDR2
198 * segment
199 */
200 mem_top = (512 << 20) + size;
201 }
202
203 /*
204 * Adjust for boot bus memory hole on OCTEON II
205 * and later.
206 */
207 if ((gd->ram_size > (256 << 20)))
208 mem_top += (256 << 20);
209
210 debug("Adjusted memory top is 0x%llx\n", mem_top);
211 addr = mem_top - size;
212 if (addr > (512 << 20))
213 addr = (512 << 20);
214 if ((addr >= (256 << 20)) && addr < (512 << 20)) {
215 /*
216 * The address landed in the boot-bus
217 * memory hole. Dig it out of the hole.
218 */
219 addr = (512 << 20);
220 }
221 } else {
222 addr = simple_strtol(eptr, NULL, 16);
223 }
224
225 ret = cvmx_bootmem_phy_named_block_alloc(size, addr,
226 addr + size, 0,
227 OCTEON_BOOTLOADER_LOAD_MEM_NAME,
228 0);
229 if (ret < 0) {
230 printf("ERROR: Unable to allocate bootloader reserved memory (addr: 0x%x, size: 0x%x).\n",
231 addr, size);
232 } else {
233 /*
234 * Set default load address to base of memory
235 * reserved for loading. The setting of the
236 * env. variable also sets the load_addr global
237 * variable.
238 * This environment variable is overridden each
239 * boot if a reserved block is created.
240 */
241 char str[20];
242
243 snprintf(str, sizeof(str), "0x%x", addr);
244 env_set("loadaddr", str);
245 debug("Setting load address to 0x%08x, size 0x%x\n",
246 addr, size);
247 }
248 return 0;
249 }
250
251 printf("WARNING: No reserved memory for image loading.\n");
252 return -1;
253}
254
255static int init_pcie_console(void)
256{
257 char *stdinname = env_get("stdin");
258 char *stdoutname = env_get("stdout");
259 char *stderrname = env_get("stderr");
260 struct udevice *pcie_console_dev = NULL;
261 bool stdin_set, stdout_set, stderr_set;
262 char iomux_name[128];
263 int ret = 0;
264
265 debug("%s: stdin: %s, stdout: %s, stderr: %s\n", __func__, stdinname,
266 stdoutname, stderrname);
267 if (!stdinname) {
268 env_set("stdin", "serial");
269 stdinname = env_get("stdin");
270 }
271 if (!stdoutname) {
272 env_set("stdout", "serial");
273 stdoutname = env_get("stdout");
274 }
275 if (!stderrname) {
276 env_set("stderr", "serial");
277 stderrname = env_get("stderr");
278 }
279
280 if (!stdinname || !stdoutname || !stderrname) {
281 printf("%s: Error setting environment variables for serial\n",
282 __func__);
283 return -1;
284 }
285
286 stdin_set = !!strstr(stdinname, CONSOLE_NAME);
287 stdout_set = !!strstr(stdoutname, CONSOLE_NAME);
288 stderr_set = !!strstr(stderrname, CONSOLE_NAME);
289
290 log_debug("stdin: %d, \"%s\", stdout: %d, \"%s\", stderr: %d, \"%s\"\n",
291 stdin_set, stdinname, stdout_set, stdoutname,
292 stderr_set, stderrname);
293 ret = uclass_get_device_by_name(UCLASS_SERIAL, CONSOLE_NAME,
294 &pcie_console_dev);
295 if (ret || !pcie_console_dev) {
296 debug("%s: No PCI console device %s found\n", __func__,
297 CONSOLE_NAME);
298 return 0;
299 }
300
301 if (stdin_set)
302 strncpy(iomux_name, stdinname, sizeof(iomux_name));
303 else
304 snprintf(iomux_name, sizeof(iomux_name), "%s,%s",
305 stdinname, pcie_console_dev->name);
306
307 ret = iomux_doenv(stdin, iomux_name);
308 if (ret) {
309 log_err("%s: Error setting I/O stdin MUX to %s\n",
310 __func__, iomux_name);
311 return ret;
312 }
313
314 if (!stdin_set)
315 env_set("stdin", iomux_name);
316
317 if (stdout_set)
318 strncpy(iomux_name, stdoutname, sizeof(iomux_name));
319 else
320 snprintf(iomux_name, sizeof(iomux_name), "%s,%s", stdoutname,
321 pcie_console_dev->name);
322
323 ret = iomux_doenv(stdout, iomux_name);
324 if (ret) {
325 log_err("%s: Error setting I/O stdout MUX to %s\n",
326 __func__, iomux_name);
327 return ret;
328 }
329 if (!stdout_set)
330 env_set("stdout", iomux_name);
331
332 if (stderr_set)
333 strncpy(iomux_name, stderrname, sizeof(iomux_name));
334 else
335 snprintf(iomux_name, sizeof(iomux_name), "%s,%s", stderrname,
336 pcie_console_dev->name);
337
338 ret = iomux_doenv(stderr, iomux_name);
339 if (ret) {
340 log_err("%s: Error setting I/O stderr MUX to %s\n",
341 __func__, iomux_name);
342 return ret;
343 }
344
345 if (!stderr_set)
346 env_set("stderr", iomux_name);
347
348 debug("%s: stdin: %s, stdout: %s, stderr: %s, ret: %d\n",
349 __func__, env_get("stdin"), env_get("stdout"),
350 env_get("stderr"), ret);
351
352 return ret;
353}
354
355static int init_bootcmd_console(void)
356{
357 char *stdinname = env_get("stdin");
358 struct udevice *bootcmd_dev = NULL;
359 bool stdin_set;
360 char iomux_name[128];
361 int ret = 0;
362
363 debug("%s: stdin before: %s\n", __func__,
364 stdinname ? stdinname : "NONE");
365 if (!stdinname) {
366 env_set("stdin", "serial");
367 stdinname = env_get("stdin");
368 }
369 stdin_set = !!strstr(stdinname, BOOTCMD_NAME);
370 ret = uclass_get_device_by_driver(UCLASS_SERIAL,
371 DM_DRIVER_GET(octeon_bootcmd),
372 &bootcmd_dev);
373 if (ret) {
374 log_err("%s: Error getting %s serial class\n", __func__,
375 BOOTCMD_NAME);
376 } else if (bootcmd_dev) {
377 if (stdin_set)
378 strncpy(iomux_name, stdinname, sizeof(iomux_name));
379 else
380 snprintf(iomux_name, sizeof(iomux_name), "%s,%s",
381 stdinname, bootcmd_dev->name);
382 ret = iomux_doenv(stdin, iomux_name);
383 if (ret)
384 log_err("%s: Error %d enabling the PCI bootcmd input console \"%s\"\n",
385 __func__, ret, iomux_name);
386 if (!stdin_set)
387 env_set("stdin", iomux_name);
388 }
389
390 debug("%s: Set iomux and stdin to %s (ret: %d)\n",
391 __func__, iomux_name, ret);
392 return ret;
393}
394
395int arch_misc_init(void)
396{
397 int ret;
398
399 ret = octeon_bootmem_init();
400 if (ret)
401 return ret;
402
403 ret = octeon_configure_load_memory();
404 if (ret)
405 return ret;
406
407 if (CONFIG_IS_ENABLED(OCTEON_SERIAL_PCIE_CONSOLE))
408 init_pcie_console();
409
410 if (CONFIG_IS_ENABLED(OCTEON_SERIAL_BOOTCMD))
411 init_bootcmd_console();
412
413 return 0;
414}