18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2000, 2001, 2002, 2003 Broadcom Corporation
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/init.h>
78c2ecf20Sopenharmony_ci#include <linux/kernel.h>
88c2ecf20Sopenharmony_ci#include <linux/linkage.h>
98c2ecf20Sopenharmony_ci#include <linux/mm.h>
108c2ecf20Sopenharmony_ci#include <linux/blkdev.h>
118c2ecf20Sopenharmony_ci#include <linux/memblock.h>
128c2ecf20Sopenharmony_ci#include <linux/pm.h>
138c2ecf20Sopenharmony_ci#include <linux/smp.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <asm/bootinfo.h>
168c2ecf20Sopenharmony_ci#include <asm/reboot.h>
178c2ecf20Sopenharmony_ci#include <asm/setup.h>
188c2ecf20Sopenharmony_ci#include <asm/sibyte/board.h>
198c2ecf20Sopenharmony_ci#include <asm/smp-ops.h>
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#include <asm/fw/cfe/cfe_api.h>
228c2ecf20Sopenharmony_ci#include <asm/fw/cfe/cfe_error.h>
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci/* Max ram addressable in 32-bit segments */
258c2ecf20Sopenharmony_ci#ifdef CONFIG_64BIT
268c2ecf20Sopenharmony_ci#define MAX_RAM_SIZE (~0ULL)
278c2ecf20Sopenharmony_ci#else
288c2ecf20Sopenharmony_ci#ifdef CONFIG_HIGHMEM
298c2ecf20Sopenharmony_ci#ifdef CONFIG_PHYS_ADDR_T_64BIT
308c2ecf20Sopenharmony_ci#define MAX_RAM_SIZE (~0ULL)
318c2ecf20Sopenharmony_ci#else
328c2ecf20Sopenharmony_ci#define MAX_RAM_SIZE (0xffffffffULL)
338c2ecf20Sopenharmony_ci#endif
348c2ecf20Sopenharmony_ci#else
358c2ecf20Sopenharmony_ci#define MAX_RAM_SIZE (0x1fffffffULL)
368c2ecf20Sopenharmony_ci#endif
378c2ecf20Sopenharmony_ci#endif
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#define SIBYTE_MAX_MEM_REGIONS 8
408c2ecf20Sopenharmony_ciphys_addr_t board_mem_region_addrs[SIBYTE_MAX_MEM_REGIONS];
418c2ecf20Sopenharmony_ciphys_addr_t board_mem_region_sizes[SIBYTE_MAX_MEM_REGIONS];
428c2ecf20Sopenharmony_ciunsigned int board_mem_region_count;
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ciint cfe_cons_handle;
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci#ifdef CONFIG_BLK_DEV_INITRD
478c2ecf20Sopenharmony_ciextern unsigned long initrd_start, initrd_end;
488c2ecf20Sopenharmony_ci#endif
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cistatic void __noreturn cfe_linux_exit(void *arg)
518c2ecf20Sopenharmony_ci{
528c2ecf20Sopenharmony_ci	int warm = *(int *)arg;
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	if (smp_processor_id()) {
558c2ecf20Sopenharmony_ci		static int reboot_smp;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci		/* Don't repeat the process from another CPU */
588c2ecf20Sopenharmony_ci		if (!reboot_smp) {
598c2ecf20Sopenharmony_ci			/* Get CPU 0 to do the cfe_exit */
608c2ecf20Sopenharmony_ci			reboot_smp = 1;
618c2ecf20Sopenharmony_ci			smp_call_function(cfe_linux_exit, arg, 0);
628c2ecf20Sopenharmony_ci		}
638c2ecf20Sopenharmony_ci	} else {
648c2ecf20Sopenharmony_ci		printk("Passing control back to CFE...\n");
658c2ecf20Sopenharmony_ci		cfe_exit(warm, 0);
668c2ecf20Sopenharmony_ci		printk("cfe_exit returned??\n");
678c2ecf20Sopenharmony_ci	}
688c2ecf20Sopenharmony_ci	while (1);
698c2ecf20Sopenharmony_ci}
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_cistatic void __noreturn cfe_linux_restart(char *command)
728c2ecf20Sopenharmony_ci{
738c2ecf20Sopenharmony_ci	static const int zero;
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	cfe_linux_exit((void *)&zero);
768c2ecf20Sopenharmony_ci}
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistatic void __noreturn cfe_linux_halt(void)
798c2ecf20Sopenharmony_ci{
808c2ecf20Sopenharmony_ci	static const int one = 1;
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	cfe_linux_exit((void *)&one);
838c2ecf20Sopenharmony_ci}
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_cistatic __init void prom_meminit(void)
868c2ecf20Sopenharmony_ci{
878c2ecf20Sopenharmony_ci	u64 addr, size, type; /* regardless of PHYS_ADDR_T_64BIT */
888c2ecf20Sopenharmony_ci	int mem_flags = 0;
898c2ecf20Sopenharmony_ci	unsigned int idx;
908c2ecf20Sopenharmony_ci	int rd_flag;
918c2ecf20Sopenharmony_ci#ifdef CONFIG_BLK_DEV_INITRD
928c2ecf20Sopenharmony_ci	unsigned long initrd_pstart;
938c2ecf20Sopenharmony_ci	unsigned long initrd_pend;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	initrd_pstart = CPHYSADDR(initrd_start);
968c2ecf20Sopenharmony_ci	initrd_pend = CPHYSADDR(initrd_end);
978c2ecf20Sopenharmony_ci	if (initrd_start &&
988c2ecf20Sopenharmony_ci	    ((initrd_pstart > MAX_RAM_SIZE)
998c2ecf20Sopenharmony_ci	     || (initrd_pend > MAX_RAM_SIZE))) {
1008c2ecf20Sopenharmony_ci		panic("initrd out of addressable memory");
1018c2ecf20Sopenharmony_ci	}
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci#endif /* INITRD */
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	for (idx = 0; cfe_enummem(idx, mem_flags, &addr, &size, &type) != CFE_ERR_NOMORE;
1068c2ecf20Sopenharmony_ci	     idx++) {
1078c2ecf20Sopenharmony_ci		rd_flag = 0;
1088c2ecf20Sopenharmony_ci		if (type == CFE_MI_AVAILABLE) {
1098c2ecf20Sopenharmony_ci			/*
1108c2ecf20Sopenharmony_ci			 * See if this block contains (any portion of) the
1118c2ecf20Sopenharmony_ci			 * ramdisk
1128c2ecf20Sopenharmony_ci			 */
1138c2ecf20Sopenharmony_ci#ifdef CONFIG_BLK_DEV_INITRD
1148c2ecf20Sopenharmony_ci			if (initrd_start) {
1158c2ecf20Sopenharmony_ci				if ((initrd_pstart > addr) &&
1168c2ecf20Sopenharmony_ci				    (initrd_pstart < (addr + size))) {
1178c2ecf20Sopenharmony_ci					memblock_add(addr,
1188c2ecf20Sopenharmony_ci						     initrd_pstart - addr);
1198c2ecf20Sopenharmony_ci					rd_flag = 1;
1208c2ecf20Sopenharmony_ci				}
1218c2ecf20Sopenharmony_ci				if ((initrd_pend > addr) &&
1228c2ecf20Sopenharmony_ci				    (initrd_pend < (addr + size))) {
1238c2ecf20Sopenharmony_ci					memblock_add(initrd_pend,
1248c2ecf20Sopenharmony_ci						(addr + size) - initrd_pend);
1258c2ecf20Sopenharmony_ci					rd_flag = 1;
1268c2ecf20Sopenharmony_ci				}
1278c2ecf20Sopenharmony_ci			}
1288c2ecf20Sopenharmony_ci#endif
1298c2ecf20Sopenharmony_ci			if (!rd_flag) {
1308c2ecf20Sopenharmony_ci				if (addr > MAX_RAM_SIZE)
1318c2ecf20Sopenharmony_ci					continue;
1328c2ecf20Sopenharmony_ci				if (addr+size > MAX_RAM_SIZE)
1338c2ecf20Sopenharmony_ci					size = MAX_RAM_SIZE - (addr+size) + 1;
1348c2ecf20Sopenharmony_ci				/*
1358c2ecf20Sopenharmony_ci				 * memcpy/__copy_user prefetch, which
1368c2ecf20Sopenharmony_ci				 * will cause a bus error for
1378c2ecf20Sopenharmony_ci				 * KSEG/KUSEG addrs not backed by RAM.
1388c2ecf20Sopenharmony_ci				 * Hence, reserve some padding for the
1398c2ecf20Sopenharmony_ci				 * prefetch distance.
1408c2ecf20Sopenharmony_ci				 */
1418c2ecf20Sopenharmony_ci				if (size > 512)
1428c2ecf20Sopenharmony_ci					size -= 512;
1438c2ecf20Sopenharmony_ci				memblock_add(addr, size);
1448c2ecf20Sopenharmony_ci			}
1458c2ecf20Sopenharmony_ci			board_mem_region_addrs[board_mem_region_count] = addr;
1468c2ecf20Sopenharmony_ci			board_mem_region_sizes[board_mem_region_count] = size;
1478c2ecf20Sopenharmony_ci			board_mem_region_count++;
1488c2ecf20Sopenharmony_ci			if (board_mem_region_count ==
1498c2ecf20Sopenharmony_ci			    SIBYTE_MAX_MEM_REGIONS) {
1508c2ecf20Sopenharmony_ci				/*
1518c2ecf20Sopenharmony_ci				 * Too many regions.  Need to configure more
1528c2ecf20Sopenharmony_ci				 */
1538c2ecf20Sopenharmony_ci				while(1);
1548c2ecf20Sopenharmony_ci			}
1558c2ecf20Sopenharmony_ci		}
1568c2ecf20Sopenharmony_ci	}
1578c2ecf20Sopenharmony_ci#ifdef CONFIG_BLK_DEV_INITRD
1588c2ecf20Sopenharmony_ci	if (initrd_start) {
1598c2ecf20Sopenharmony_ci		memblock_add(initrd_pstart, initrd_pend - initrd_pstart);
1608c2ecf20Sopenharmony_ci		memblock_reserve(initrd_pstart, initrd_pend - initrd_pstart);
1618c2ecf20Sopenharmony_ci	}
1628c2ecf20Sopenharmony_ci#endif
1638c2ecf20Sopenharmony_ci}
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci#ifdef CONFIG_BLK_DEV_INITRD
1668c2ecf20Sopenharmony_cistatic int __init initrd_setup(char *str)
1678c2ecf20Sopenharmony_ci{
1688c2ecf20Sopenharmony_ci	char rdarg[64];
1698c2ecf20Sopenharmony_ci	int idx;
1708c2ecf20Sopenharmony_ci	char *tmp, *endptr;
1718c2ecf20Sopenharmony_ci	unsigned long initrd_size;
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	/* Make a copy of the initrd argument so we can smash it up here */
1748c2ecf20Sopenharmony_ci	for (idx = 0; idx < sizeof(rdarg)-1; idx++) {
1758c2ecf20Sopenharmony_ci		if (!str[idx] || (str[idx] == ' ')) break;
1768c2ecf20Sopenharmony_ci		rdarg[idx] = str[idx];
1778c2ecf20Sopenharmony_ci	}
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	rdarg[idx] = 0;
1808c2ecf20Sopenharmony_ci	str = rdarg;
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	/*
1838c2ecf20Sopenharmony_ci	 *Initrd location comes in the form "<hex size of ramdisk in bytes>@<location in memory>"
1848c2ecf20Sopenharmony_ci	 *  e.g. initrd=3abfd@80010000.	 This is set up by the loader.
1858c2ecf20Sopenharmony_ci	 */
1868c2ecf20Sopenharmony_ci	for (tmp = str; *tmp != '@'; tmp++) {
1878c2ecf20Sopenharmony_ci		if (!*tmp) {
1888c2ecf20Sopenharmony_ci			goto fail;
1898c2ecf20Sopenharmony_ci		}
1908c2ecf20Sopenharmony_ci	}
1918c2ecf20Sopenharmony_ci	*tmp = 0;
1928c2ecf20Sopenharmony_ci	tmp++;
1938c2ecf20Sopenharmony_ci	if (!*tmp) {
1948c2ecf20Sopenharmony_ci		goto fail;
1958c2ecf20Sopenharmony_ci	}
1968c2ecf20Sopenharmony_ci	initrd_size = simple_strtoul(str, &endptr, 16);
1978c2ecf20Sopenharmony_ci	if (*endptr) {
1988c2ecf20Sopenharmony_ci		*(tmp-1) = '@';
1998c2ecf20Sopenharmony_ci		goto fail;
2008c2ecf20Sopenharmony_ci	}
2018c2ecf20Sopenharmony_ci	*(tmp-1) = '@';
2028c2ecf20Sopenharmony_ci	initrd_start = simple_strtoul(tmp, &endptr, 16);
2038c2ecf20Sopenharmony_ci	if (*endptr) {
2048c2ecf20Sopenharmony_ci		goto fail;
2058c2ecf20Sopenharmony_ci	}
2068c2ecf20Sopenharmony_ci	initrd_end = initrd_start + initrd_size;
2078c2ecf20Sopenharmony_ci	printk("Found initrd of %lx@%lx\n", initrd_size, initrd_start);
2088c2ecf20Sopenharmony_ci	return 1;
2098c2ecf20Sopenharmony_ci fail:
2108c2ecf20Sopenharmony_ci	printk("Bad initrd argument.  Disabling initrd\n");
2118c2ecf20Sopenharmony_ci	initrd_start = 0;
2128c2ecf20Sopenharmony_ci	initrd_end = 0;
2138c2ecf20Sopenharmony_ci	return 1;
2148c2ecf20Sopenharmony_ci}
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci#endif
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ciextern const struct plat_smp_ops sb_smp_ops;
2198c2ecf20Sopenharmony_ciextern const struct plat_smp_ops bcm1480_smp_ops;
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci/*
2228c2ecf20Sopenharmony_ci * prom_init is called just after the cpu type is determined, from setup_arch()
2238c2ecf20Sopenharmony_ci */
2248c2ecf20Sopenharmony_civoid __init prom_init(void)
2258c2ecf20Sopenharmony_ci{
2268c2ecf20Sopenharmony_ci	uint64_t cfe_ept, cfe_handle;
2278c2ecf20Sopenharmony_ci	unsigned int cfe_eptseal;
2288c2ecf20Sopenharmony_ci	int argc = fw_arg0;
2298c2ecf20Sopenharmony_ci	char **envp = (char **) fw_arg2;
2308c2ecf20Sopenharmony_ci	int *prom_vec = (int *) fw_arg3;
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	_machine_restart   = cfe_linux_restart;
2338c2ecf20Sopenharmony_ci	_machine_halt	   = cfe_linux_halt;
2348c2ecf20Sopenharmony_ci	pm_power_off = cfe_linux_halt;
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	/*
2378c2ecf20Sopenharmony_ci	 * Check if a loader was used; if NOT, the 4 arguments are
2388c2ecf20Sopenharmony_ci	 * what CFE gives us (handle, 0, EPT and EPTSEAL)
2398c2ecf20Sopenharmony_ci	 */
2408c2ecf20Sopenharmony_ci	if (argc < 0) {
2418c2ecf20Sopenharmony_ci		cfe_handle = (uint64_t)(long)argc;
2428c2ecf20Sopenharmony_ci		cfe_ept = (long)envp;
2438c2ecf20Sopenharmony_ci		cfe_eptseal = (uint32_t)(unsigned long)prom_vec;
2448c2ecf20Sopenharmony_ci	} else {
2458c2ecf20Sopenharmony_ci		if ((int32_t)(long)prom_vec < 0) {
2468c2ecf20Sopenharmony_ci			/*
2478c2ecf20Sopenharmony_ci			 * Old loader; all it gives us is the handle,
2488c2ecf20Sopenharmony_ci			 * so use the "known" entrypoint and assume
2498c2ecf20Sopenharmony_ci			 * the seal.
2508c2ecf20Sopenharmony_ci			 */
2518c2ecf20Sopenharmony_ci			cfe_handle = (uint64_t)(long)prom_vec;
2528c2ecf20Sopenharmony_ci			cfe_ept = (uint64_t)((int32_t)0x9fc00500);
2538c2ecf20Sopenharmony_ci			cfe_eptseal = CFE_EPTSEAL;
2548c2ecf20Sopenharmony_ci		} else {
2558c2ecf20Sopenharmony_ci			/*
2568c2ecf20Sopenharmony_ci			 * Newer loaders bundle the handle/ept/eptseal
2578c2ecf20Sopenharmony_ci			 * Note: prom_vec is in the loader's useg
2588c2ecf20Sopenharmony_ci			 * which is still alive in the TLB.
2598c2ecf20Sopenharmony_ci			 */
2608c2ecf20Sopenharmony_ci			cfe_handle = (uint64_t)((int32_t *)prom_vec)[0];
2618c2ecf20Sopenharmony_ci			cfe_ept = (uint64_t)((int32_t *)prom_vec)[2];
2628c2ecf20Sopenharmony_ci			cfe_eptseal = (unsigned int)((uint32_t *)prom_vec)[3];
2638c2ecf20Sopenharmony_ci		}
2648c2ecf20Sopenharmony_ci	}
2658c2ecf20Sopenharmony_ci	if (cfe_eptseal != CFE_EPTSEAL) {
2668c2ecf20Sopenharmony_ci		/* too early for panic to do any good */
2678c2ecf20Sopenharmony_ci		printk("CFE's entrypoint seal doesn't match. Spinning.");
2688c2ecf20Sopenharmony_ci		while (1) ;
2698c2ecf20Sopenharmony_ci	}
2708c2ecf20Sopenharmony_ci	cfe_init(cfe_handle, cfe_ept);
2718c2ecf20Sopenharmony_ci	/*
2728c2ecf20Sopenharmony_ci	 * Get the handle for (at least) prom_putchar, possibly for
2738c2ecf20Sopenharmony_ci	 * boot console
2748c2ecf20Sopenharmony_ci	 */
2758c2ecf20Sopenharmony_ci	cfe_cons_handle = cfe_getstdhandle(CFE_STDHANDLE_CONSOLE);
2768c2ecf20Sopenharmony_ci	if (cfe_getenv("LINUX_CMDLINE", arcs_cmdline, COMMAND_LINE_SIZE) < 0) {
2778c2ecf20Sopenharmony_ci		if (argc >= 0) {
2788c2ecf20Sopenharmony_ci			/* The loader should have set the command line */
2798c2ecf20Sopenharmony_ci			/* too early for panic to do any good */
2808c2ecf20Sopenharmony_ci			printk("LINUX_CMDLINE not defined in cfe.");
2818c2ecf20Sopenharmony_ci			while (1) ;
2828c2ecf20Sopenharmony_ci		}
2838c2ecf20Sopenharmony_ci	}
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci#ifdef CONFIG_BLK_DEV_INITRD
2868c2ecf20Sopenharmony_ci	{
2878c2ecf20Sopenharmony_ci		char *ptr;
2888c2ecf20Sopenharmony_ci		/* Need to find out early whether we've got an initrd.	So scan
2898c2ecf20Sopenharmony_ci		   the list looking now */
2908c2ecf20Sopenharmony_ci		for (ptr = arcs_cmdline; *ptr; ptr++) {
2918c2ecf20Sopenharmony_ci			while (*ptr == ' ') {
2928c2ecf20Sopenharmony_ci				ptr++;
2938c2ecf20Sopenharmony_ci			}
2948c2ecf20Sopenharmony_ci			if (!strncmp(ptr, "initrd=", 7)) {
2958c2ecf20Sopenharmony_ci				initrd_setup(ptr+7);
2968c2ecf20Sopenharmony_ci				break;
2978c2ecf20Sopenharmony_ci			} else {
2988c2ecf20Sopenharmony_ci				while (*ptr && (*ptr != ' ')) {
2998c2ecf20Sopenharmony_ci					ptr++;
3008c2ecf20Sopenharmony_ci				}
3018c2ecf20Sopenharmony_ci			}
3028c2ecf20Sopenharmony_ci		}
3038c2ecf20Sopenharmony_ci	}
3048c2ecf20Sopenharmony_ci#endif /* CONFIG_BLK_DEV_INITRD */
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	/* Not sure this is needed, but it's the safe way. */
3078c2ecf20Sopenharmony_ci	arcs_cmdline[COMMAND_LINE_SIZE-1] = 0;
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci	prom_meminit();
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci#if defined(CONFIG_SIBYTE_BCM112X) || defined(CONFIG_SIBYTE_SB1250)
3128c2ecf20Sopenharmony_ci	register_smp_ops(&sb_smp_ops);
3138c2ecf20Sopenharmony_ci#endif
3148c2ecf20Sopenharmony_ci#if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
3158c2ecf20Sopenharmony_ci	register_smp_ops(&bcm1480_smp_ops);
3168c2ecf20Sopenharmony_ci#endif
3178c2ecf20Sopenharmony_ci}
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_civoid __init prom_free_prom_memory(void)
3208c2ecf20Sopenharmony_ci{
3218c2ecf20Sopenharmony_ci	/* Not sure what I'm supposed to do here.  Nothing, I think */
3228c2ecf20Sopenharmony_ci}
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_civoid prom_putchar(char c)
3258c2ecf20Sopenharmony_ci{
3268c2ecf20Sopenharmony_ci	int ret;
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci	while ((ret = cfe_write(cfe_cons_handle, &c, 1)) == 0)
3298c2ecf20Sopenharmony_ci		;
3308c2ecf20Sopenharmony_ci}
331