18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * This file is subject to the terms and conditions of the GNU General Public
38c2ecf20Sopenharmony_ci * License.  See the file "COPYING" in the main directory of this archive
48c2ecf20Sopenharmony_ci * for more details.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
78c2ecf20Sopenharmony_ci * Copyright (C) 2014 Kevin Cernekee <cernekee@gmail.com>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/init.h>
118c2ecf20Sopenharmony_ci#include <linux/bitops.h>
128c2ecf20Sopenharmony_ci#include <linux/memblock.h>
138c2ecf20Sopenharmony_ci#include <linux/ioport.h>
148c2ecf20Sopenharmony_ci#include <linux/kernel.h>
158c2ecf20Sopenharmony_ci#include <linux/io.h>
168c2ecf20Sopenharmony_ci#include <linux/of.h>
178c2ecf20Sopenharmony_ci#include <linux/of_clk.h>
188c2ecf20Sopenharmony_ci#include <linux/of_fdt.h>
198c2ecf20Sopenharmony_ci#include <linux/of_platform.h>
208c2ecf20Sopenharmony_ci#include <linux/libfdt.h>
218c2ecf20Sopenharmony_ci#include <linux/smp.h>
228c2ecf20Sopenharmony_ci#include <asm/addrspace.h>
238c2ecf20Sopenharmony_ci#include <asm/bmips.h>
248c2ecf20Sopenharmony_ci#include <asm/bootinfo.h>
258c2ecf20Sopenharmony_ci#include <asm/cpu-type.h>
268c2ecf20Sopenharmony_ci#include <asm/mipsregs.h>
278c2ecf20Sopenharmony_ci#include <asm/prom.h>
288c2ecf20Sopenharmony_ci#include <asm/smp-ops.h>
298c2ecf20Sopenharmony_ci#include <asm/time.h>
308c2ecf20Sopenharmony_ci#include <asm/traps.h>
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci#define RELO_NORMAL_VEC		BIT(18)
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#define REG_BCM6328_OTP		((void __iomem *)CKSEG1ADDR(0x1000062c))
358c2ecf20Sopenharmony_ci#define BCM6328_TP1_DISABLED	BIT(9)
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ciextern bool bmips_rac_flush_disable;
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cistatic const unsigned long kbase = VMLINUX_LOAD_ADDRESS & 0xfff00000;
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_cistruct bmips_quirk {
428c2ecf20Sopenharmony_ci	const char		*compatible;
438c2ecf20Sopenharmony_ci	void			(*quirk_fn)(void);
448c2ecf20Sopenharmony_ci};
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistatic void kbase_setup(void)
478c2ecf20Sopenharmony_ci{
488c2ecf20Sopenharmony_ci	__raw_writel(kbase | RELO_NORMAL_VEC,
498c2ecf20Sopenharmony_ci		     BMIPS_GET_CBR() + BMIPS_RELO_VECTOR_CONTROL_1);
508c2ecf20Sopenharmony_ci	ebase = kbase;
518c2ecf20Sopenharmony_ci}
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_cistatic void bcm3384_viper_quirks(void)
548c2ecf20Sopenharmony_ci{
558c2ecf20Sopenharmony_ci	/*
568c2ecf20Sopenharmony_ci	 * Some experimental CM boxes are set up to let CM own the Viper TP0
578c2ecf20Sopenharmony_ci	 * and let Linux own TP1.  This requires moving the kernel
588c2ecf20Sopenharmony_ci	 * load address to a non-conflicting region (e.g. via
598c2ecf20Sopenharmony_ci	 * CONFIG_PHYSICAL_START) and supplying an alternate DTB.
608c2ecf20Sopenharmony_ci	 * If we detect this condition, we need to move the MIPS exception
618c2ecf20Sopenharmony_ci	 * vectors up to an area that we own.
628c2ecf20Sopenharmony_ci	 *
638c2ecf20Sopenharmony_ci	 * This is distinct from the OTHER special case mentioned in
648c2ecf20Sopenharmony_ci	 * smp-bmips.c (boot on TP1, but enable SMP, then TP0 becomes our
658c2ecf20Sopenharmony_ci	 * logical CPU#1).  For the Viper TP1 case, SMP is off limits.
668c2ecf20Sopenharmony_ci	 *
678c2ecf20Sopenharmony_ci	 * Also note that many BMIPS435x CPUs do not have a
688c2ecf20Sopenharmony_ci	 * BMIPS_RELO_VECTOR_CONTROL_1 register, so it isn't safe to just
698c2ecf20Sopenharmony_ci	 * write VMLINUX_LOAD_ADDRESS into that register on every SoC.
708c2ecf20Sopenharmony_ci	 */
718c2ecf20Sopenharmony_ci	board_ebase_setup = &kbase_setup;
728c2ecf20Sopenharmony_ci	bmips_smp_enabled = 0;
738c2ecf20Sopenharmony_ci}
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_cistatic void bcm63xx_fixup_cpu1(void)
768c2ecf20Sopenharmony_ci{
778c2ecf20Sopenharmony_ci	/*
788c2ecf20Sopenharmony_ci	 * The bootloader has set up the CPU1 reset vector at
798c2ecf20Sopenharmony_ci	 * 0xa000_0200.
808c2ecf20Sopenharmony_ci	 * This conflicts with the special interrupt vector (IV).
818c2ecf20Sopenharmony_ci	 * The bootloader has also set up CPU1 to respond to the wrong
828c2ecf20Sopenharmony_ci	 * IPI interrupt.
838c2ecf20Sopenharmony_ci	 * Here we will start up CPU1 in the background and ask it to
848c2ecf20Sopenharmony_ci	 * reconfigure itself then go back to sleep.
858c2ecf20Sopenharmony_ci	 */
868c2ecf20Sopenharmony_ci	memcpy((void *)0xa0000200, &bmips_smp_movevec, 0x20);
878c2ecf20Sopenharmony_ci	__sync();
888c2ecf20Sopenharmony_ci	set_c0_cause(C_SW0);
898c2ecf20Sopenharmony_ci	cpumask_set_cpu(1, &bmips_booted_mask);
908c2ecf20Sopenharmony_ci}
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_cistatic void bcm6328_quirks(void)
938c2ecf20Sopenharmony_ci{
948c2ecf20Sopenharmony_ci	/* Check CPU1 status in OTP (it is usually disabled) */
958c2ecf20Sopenharmony_ci	if (__raw_readl(REG_BCM6328_OTP) & BCM6328_TP1_DISABLED)
968c2ecf20Sopenharmony_ci		bmips_smp_enabled = 0;
978c2ecf20Sopenharmony_ci	else
988c2ecf20Sopenharmony_ci		bcm63xx_fixup_cpu1();
998c2ecf20Sopenharmony_ci}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_cistatic void bcm6358_quirks(void)
1028c2ecf20Sopenharmony_ci{
1038c2ecf20Sopenharmony_ci	/*
1048c2ecf20Sopenharmony_ci	 * BCM3368/BCM6358 need special handling for their shared TLB, so
1058c2ecf20Sopenharmony_ci	 * disable SMP for now
1068c2ecf20Sopenharmony_ci	 */
1078c2ecf20Sopenharmony_ci	bmips_smp_enabled = 0;
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	/*
1108c2ecf20Sopenharmony_ci	 * RAC flush causes kernel panics on BCM6358 when booting from TP1
1118c2ecf20Sopenharmony_ci	 * because the bootloader is not initializing it properly.
1128c2ecf20Sopenharmony_ci	 */
1138c2ecf20Sopenharmony_ci	bmips_rac_flush_disable = !!(read_c0_brcm_cmt_local() & (1 << 31));
1148c2ecf20Sopenharmony_ci}
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cistatic void bcm6368_quirks(void)
1178c2ecf20Sopenharmony_ci{
1188c2ecf20Sopenharmony_ci	bcm63xx_fixup_cpu1();
1198c2ecf20Sopenharmony_ci}
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_cistatic const struct bmips_quirk bmips_quirk_list[] = {
1228c2ecf20Sopenharmony_ci	{ "brcm,bcm3368",		&bcm6358_quirks			},
1238c2ecf20Sopenharmony_ci	{ "brcm,bcm3384-viper",		&bcm3384_viper_quirks		},
1248c2ecf20Sopenharmony_ci	{ "brcm,bcm33843-viper",	&bcm3384_viper_quirks		},
1258c2ecf20Sopenharmony_ci	{ "brcm,bcm6328",		&bcm6328_quirks			},
1268c2ecf20Sopenharmony_ci	{ "brcm,bcm6358",		&bcm6358_quirks			},
1278c2ecf20Sopenharmony_ci	{ "brcm,bcm6362",		&bcm6368_quirks			},
1288c2ecf20Sopenharmony_ci	{ "brcm,bcm6368",		&bcm6368_quirks			},
1298c2ecf20Sopenharmony_ci	{ "brcm,bcm63168",		&bcm6368_quirks			},
1308c2ecf20Sopenharmony_ci	{ "brcm,bcm63268",		&bcm6368_quirks			},
1318c2ecf20Sopenharmony_ci	{ },
1328c2ecf20Sopenharmony_ci};
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_civoid __init prom_init(void)
1358c2ecf20Sopenharmony_ci{
1368c2ecf20Sopenharmony_ci	bmips_cpu_setup();
1378c2ecf20Sopenharmony_ci	register_bmips_smp_ops();
1388c2ecf20Sopenharmony_ci}
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_civoid __init prom_free_prom_memory(void)
1418c2ecf20Sopenharmony_ci{
1428c2ecf20Sopenharmony_ci}
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ciconst char *get_system_type(void)
1458c2ecf20Sopenharmony_ci{
1468c2ecf20Sopenharmony_ci	return "Generic BMIPS kernel";
1478c2ecf20Sopenharmony_ci}
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_civoid __init plat_time_init(void)
1508c2ecf20Sopenharmony_ci{
1518c2ecf20Sopenharmony_ci	struct device_node *np;
1528c2ecf20Sopenharmony_ci	u32 freq;
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	np = of_find_node_by_name(NULL, "cpus");
1558c2ecf20Sopenharmony_ci	if (!np)
1568c2ecf20Sopenharmony_ci		panic("missing 'cpus' DT node");
1578c2ecf20Sopenharmony_ci	if (of_property_read_u32(np, "mips-hpt-frequency", &freq) < 0)
1588c2ecf20Sopenharmony_ci		panic("missing 'mips-hpt-frequency' property");
1598c2ecf20Sopenharmony_ci	of_node_put(np);
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	mips_hpt_frequency = freq;
1628c2ecf20Sopenharmony_ci}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_civoid __init plat_mem_setup(void)
1658c2ecf20Sopenharmony_ci{
1668c2ecf20Sopenharmony_ci	void *dtb;
1678c2ecf20Sopenharmony_ci	const struct bmips_quirk *q;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	set_io_port_base(0);
1708c2ecf20Sopenharmony_ci	ioport_resource.start = 0;
1718c2ecf20Sopenharmony_ci	ioport_resource.end = ~0;
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	/* intended to somewhat resemble ARM; see Documentation/arm/booting.rst */
1748c2ecf20Sopenharmony_ci	if (fw_arg0 == 0 && fw_arg1 == 0xffffffff)
1758c2ecf20Sopenharmony_ci		dtb = phys_to_virt(fw_arg2);
1768c2ecf20Sopenharmony_ci	else if (fw_passed_dtb) /* UHI interface or appended dtb */
1778c2ecf20Sopenharmony_ci		dtb = (void *)fw_passed_dtb;
1788c2ecf20Sopenharmony_ci	else if (&__dtb_start != &__dtb_end)
1798c2ecf20Sopenharmony_ci		dtb = (void *)__dtb_start;
1808c2ecf20Sopenharmony_ci	else
1818c2ecf20Sopenharmony_ci		panic("no dtb found");
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	__dt_setup_arch(dtb);
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	for (q = bmips_quirk_list; q->quirk_fn; q++) {
1868c2ecf20Sopenharmony_ci		if (of_flat_dt_is_compatible(of_get_flat_dt_root(),
1878c2ecf20Sopenharmony_ci					     q->compatible)) {
1888c2ecf20Sopenharmony_ci			q->quirk_fn();
1898c2ecf20Sopenharmony_ci		}
1908c2ecf20Sopenharmony_ci	}
1918c2ecf20Sopenharmony_ci}
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_civoid __init device_tree_init(void)
1948c2ecf20Sopenharmony_ci{
1958c2ecf20Sopenharmony_ci	struct device_node *np;
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	unflatten_and_copy_device_tree();
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	/* Disable SMP boot unless both CPUs are listed in DT and !disabled */
2008c2ecf20Sopenharmony_ci	np = of_find_node_by_name(NULL, "cpus");
2018c2ecf20Sopenharmony_ci	if (np && of_get_available_child_count(np) <= 1)
2028c2ecf20Sopenharmony_ci		bmips_smp_enabled = 0;
2038c2ecf20Sopenharmony_ci	of_node_put(np);
2048c2ecf20Sopenharmony_ci}
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_cistatic int __init plat_dev_init(void)
2078c2ecf20Sopenharmony_ci{
2088c2ecf20Sopenharmony_ci	of_clk_init(NULL);
2098c2ecf20Sopenharmony_ci	return 0;
2108c2ecf20Sopenharmony_ci}
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_cidevice_initcall(plat_dev_init);
213