1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2020 Marvell International Ltd.
10 #include <asm/global_data.h>
11 #include <linux/bitfield.h>
12 #include <linux/bitops.h>
13 #include <linux/compat.h>
15 #include <mach/clock.h>
16 #include <mach/cavm-reg.h>
17 #include <mach/cvmx-bootmem.h>
18 #include <mach/cvmx-regs.h>
19 #include <mach/cvmx-sata-defs.h>
21 DECLARE_GLOBAL_DATA_PTR;
25 * This address cannot be changed as the PCI console tool relies on exactly
28 #define BOOTLOADER_BOOTMEM_DESC_ADDR 0x6c100
29 #define BOOTLOADER_BOOTMEM_DESC_SPACE (BOOTLOADER_BOOTMEM_DESC_ADDR + 0x8)
31 #define OCTEON_RESERVED_LOW_BOOT_MEM_SIZE (1024 * 1024)
33 #define BOOTCMD_NAME "pci-bootcmd"
34 #define CONSOLE_NAME "pci-console@0"
35 #define OCTEON_BOOTLOADER_LOAD_MEM_NAME "__tmp_load"
38 * TRUE for devices having registers with little-endian byte
39 * order, FALSE for registers with native-endian byte order.
40 * PCI mandates little-endian, USB and SATA are configurable,
41 * but we chose little-endian for these.
43 * This table will be referened in the Octeon platform specific
44 * mangle-port.h header.
46 const bool octeon_should_swizzle_table[256] = {
47 [0x00] = true, /* bootbus/CF */
48 [0x1b] = true, /* PCI mmio window */
49 [0x1c] = true, /* PCI mmio window */
50 [0x1d] = true, /* PCI mmio window */
51 [0x1e] = true, /* PCI mmio window */
52 [0x68] = true, /* OCTEON III USB */
53 [0x69] = true, /* OCTEON III USB */
54 [0x6f] = true, /* OCTEON II USB */
57 static int get_clocks(void)
59 const u64 ref_clock = PLL_REF_CLK;
60 void __iomem *rst_boot;
63 rst_boot = ioremap(CAVM_RST_BOOT, 0);
64 val = ioread64(rst_boot);
65 gd->cpu_clk = ref_clock * FIELD_GET(RST_BOOT_C_MUL, val);
66 gd->bus_clk = ref_clock * FIELD_GET(RST_BOOT_PNR_MUL, val);
68 debug("%s: cpu: %lu, bus: %lu\n", __func__, gd->cpu_clk, gd->bus_clk);
73 /* Early mach init code run from flash */
74 int mach_cpu_init(void)
76 void __iomem *mio_boot_reg_cfg0;
78 /* Remap boot-bus 0x1fc0.0000 -> 0x1f40.0000 */
79 /* ToDo: Move this to an early running bus (bootbus) DM driver */
80 mio_boot_reg_cfg0 = ioremap(CAVM_MIO_BOOT_REG_CFG0, 0);
81 clrsetbits_be64(mio_boot_reg_cfg0, 0xffff, 0x1f40);
83 /* Get clocks and store them in GD */
90 * Returns number of cores
92 * Return: number of CPU cores for the specified node
94 static int cavm_octeon_num_cores(void)
96 void __iomem *ciu_fuse;
98 ciu_fuse = ioremap(CAVM_CIU_FUSE, 0);
99 return fls64(ioread64(ciu_fuse) & 0xffffffffffff);
102 int print_cpuinfo(void)
104 printf("SoC: Octeon CN73xx (%d cores)\n", cavm_octeon_num_cores());
109 static int octeon_bootmem_init(void)
113 /* Call old single-node func: it uses only gd->ram_size */
114 ret = cvmx_bootmem_phy_mem_list_init(gd->ram_size,
115 OCTEON_RESERVED_LOW_BOOT_MEM_SIZE,
116 (void *)CKSEG0ADDR(BOOTLOADER_BOOTMEM_DESC_SPACE));
118 printf("FATAL: Error initializing bootmem list\n");
123 * Put bootmem descriptor address in known location for host.
124 * Make sure it is not in kseg0, as we want physical address
126 writeq((u64)__cvmx_bootmem_internal_get_desc_ptr() & 0x7fffffffull,
127 (void *)CKSEG0ADDR(BOOTLOADER_BOOTMEM_DESC_ADDR));
129 debug("Reserving first 1MB of memory\n");
130 ret = cvmx_bootmem_reserve_memory(0, OCTEON_RESERVED_LOW_BOOT_MEM_SIZE,
131 "__low_reserved", 0);
133 puts("Error reserving low 1MB of memory\n");
136 cvmx_bootmem_phy_list_print();
142 static int octeon_configure_load_memory(void)
149 eptr = env_get("octeon_reserved_mem_load_size");
150 if (!eptr || !strcmp("auto", eptr)) {
152 * Pick a size that we think is appropriate.
153 * Please note that for small memory boards this guess
154 * will likely not be ideal.
155 * Please pick a specific size for boards/applications
158 if (gd->ram_size <= (256 << 20)) {
159 size = min_t(u64, (128 << 20),
160 ((gd->ram_size * 2) / 5) & ~0xFFFFF);
162 size = min_t(u64, (256 << 20),
163 ((gd->ram_size - (256 << 20)) / 3) & ~0xFFFFF);
166 size = simple_strtol(eptr, NULL, 16);
167 debug("octeon_reserved_mem_load_size=0x%08x\n", size);
171 debug("Linux reserved load size 0x%08x\n", size);
172 eptr = env_get("octeon_reserved_mem_load_base");
173 if (!eptr || !strcmp("auto", eptr)) {
176 * Leave some room for previous allocations that
177 * are made starting at the top of the low
180 int adjust = (1 << 20);
182 if (gd->ram_size <= (512 << 20))
185 /* Put block at the top of DDR0, or bottom of DDR2 */
186 if ((gd->ram_size <= (256 << 20)) ||
187 (size > (gd->ram_size - (256 << 20)))) {
188 mem_top = min_t(u64, gd->ram_size - adjust,
189 (256 << 20) - adjust);
190 } else if ((gd->ram_size <= (512 << 20)) ||
191 (size > (gd->ram_size - (512 << 20)))) {
192 mem_top = min_t(u64, gd->ram_size - adjust,
193 (512 << 20) - adjust);
196 * We have enough room, so set
197 * mem_top so that the block is
198 * at the base of the DDR2
201 mem_top = (512 << 20) + size;
205 * Adjust for boot bus memory hole on OCTEON II
208 if ((gd->ram_size > (256 << 20)))
209 mem_top += (256 << 20);
211 debug("Adjusted memory top is 0x%llx\n", mem_top);
212 addr = mem_top - size;
213 if (addr > (512 << 20))
215 if ((addr >= (256 << 20)) && addr < (512 << 20)) {
217 * The address landed in the boot-bus
218 * memory hole. Dig it out of the hole.
223 addr = simple_strtol(eptr, NULL, 16);
226 ret = cvmx_bootmem_phy_named_block_alloc(size, addr,
228 OCTEON_BOOTLOADER_LOAD_MEM_NAME,
231 printf("ERROR: Unable to allocate bootloader reserved memory (addr: 0x%x, size: 0x%x).\n",
235 * Set default load address to base of memory
236 * reserved for loading. The setting of the
237 * env. variable also sets the load_addr global
239 * This environment variable is overridden each
240 * boot if a reserved block is created.
244 snprintf(str, sizeof(str), "0x%x", addr);
245 env_set("loadaddr", str);
246 debug("Setting load address to 0x%08x, size 0x%x\n",
252 printf("WARNING: No reserved memory for image loading.\n");
256 static int init_pcie_console(void)
258 char *stdinname = env_get("stdin");
259 char *stdoutname = env_get("stdout");
260 char *stderrname = env_get("stderr");
261 struct udevice *pcie_console_dev = NULL;
262 bool stdin_set, stdout_set, stderr_set;
263 char iomux_name[128];
266 debug("%s: stdin: %s, stdout: %s, stderr: %s\n", __func__, stdinname,
267 stdoutname, stderrname);
269 env_set("stdin", "serial");
270 stdinname = env_get("stdin");
273 env_set("stdout", "serial");
274 stdoutname = env_get("stdout");
277 env_set("stderr", "serial");
278 stderrname = env_get("stderr");
281 if (!stdinname || !stdoutname || !stderrname) {
282 printf("%s: Error setting environment variables for serial\n",
287 stdin_set = !!strstr(stdinname, CONSOLE_NAME);
288 stdout_set = !!strstr(stdoutname, CONSOLE_NAME);
289 stderr_set = !!strstr(stderrname, CONSOLE_NAME);
291 log_debug("stdin: %d, \"%s\", stdout: %d, \"%s\", stderr: %d, \"%s\"\n",
292 stdin_set, stdinname, stdout_set, stdoutname,
293 stderr_set, stderrname);
294 ret = uclass_get_device_by_name(UCLASS_SERIAL, CONSOLE_NAME,
296 if (ret || !pcie_console_dev) {
297 debug("%s: No PCI console device %s found\n", __func__,
303 strncpy(iomux_name, stdinname, sizeof(iomux_name));
305 snprintf(iomux_name, sizeof(iomux_name), "%s,%s",
306 stdinname, pcie_console_dev->name);
308 ret = iomux_doenv(stdin, iomux_name);
310 log_err("%s: Error setting I/O stdin MUX to %s\n",
311 __func__, iomux_name);
316 env_set("stdin", iomux_name);
319 strncpy(iomux_name, stdoutname, sizeof(iomux_name));
321 snprintf(iomux_name, sizeof(iomux_name), "%s,%s", stdoutname,
322 pcie_console_dev->name);
324 ret = iomux_doenv(stdout, iomux_name);
326 log_err("%s: Error setting I/O stdout MUX to %s\n",
327 __func__, iomux_name);
331 env_set("stdout", iomux_name);
334 strncpy(iomux_name, stderrname, sizeof(iomux_name));
336 snprintf(iomux_name, sizeof(iomux_name), "%s,%s", stderrname,
337 pcie_console_dev->name);
339 ret = iomux_doenv(stderr, iomux_name);
341 log_err("%s: Error setting I/O stderr MUX to %s\n",
342 __func__, iomux_name);
347 env_set("stderr", iomux_name);
349 debug("%s: stdin: %s, stdout: %s, stderr: %s, ret: %d\n",
350 __func__, env_get("stdin"), env_get("stdout"),
351 env_get("stderr"), ret);
356 static int init_bootcmd_console(void)
358 char *stdinname = env_get("stdin");
359 struct udevice *bootcmd_dev = NULL;
361 char iomux_name[128];
364 debug("%s: stdin before: %s\n", __func__,
365 stdinname ? stdinname : "NONE");
367 env_set("stdin", "serial");
368 stdinname = env_get("stdin");
370 stdin_set = !!strstr(stdinname, BOOTCMD_NAME);
371 ret = uclass_get_device_by_driver(UCLASS_SERIAL,
372 DM_DRIVER_GET(octeon_bootcmd),
375 log_err("%s: Error getting %s serial class\n", __func__,
377 } else if (bootcmd_dev) {
379 strncpy(iomux_name, stdinname, sizeof(iomux_name));
381 snprintf(iomux_name, sizeof(iomux_name), "%s,%s",
382 stdinname, bootcmd_dev->name);
383 ret = iomux_doenv(stdin, iomux_name);
385 log_err("%s: Error %d enabling the PCI bootcmd input console \"%s\"\n",
386 __func__, ret, iomux_name);
388 env_set("stdin", iomux_name);
391 debug("%s: Set iomux and stdin to %s (ret: %d)\n",
392 __func__, iomux_name, ret);
396 int arch_early_init_r(void)
401 * Needs to be called pretty early, so that e.g. networking etc
402 * can access the bootmem infrastructure
404 ret = octeon_bootmem_init();
411 int arch_misc_init(void)
415 ret = octeon_configure_load_memory();
419 if (CONFIG_IS_ENABLED(OCTEON_SERIAL_PCIE_CONSOLE))
422 if (CONFIG_IS_ENABLED(OCTEON_SERIAL_BOOTCMD))
423 init_bootcmd_console();
428 int board_ahci_enable(void)
430 cvmx_sata_uctl_shim_cfg_t shim_cfg;
433 * Configure proper endian swapping for the AHCI port so that the
434 * common AHCI code can be used
436 shim_cfg.u64 = csr_rd(CVMX_SATA_UCTL_SHIM_CFG);
437 shim_cfg.s.dma_endian_mode = 1;
438 /* Use 1 for LE mode when running BE, or 3 for BE mode running BE */
439 shim_cfg.s.csr_endian_mode = 3; /* Don't byte swap */
440 shim_cfg.s.dma_read_cmd = 1; /* No allocate L2C */
441 csr_wr(CVMX_SATA_UCTL_SHIM_CFG, shim_cfg.u64);