162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Device Tree support for MStar/Sigmastar Armv7 SoCs
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (c) 2020 thingy.jp
662306a36Sopenharmony_ci * Author: Daniel Palmer <daniel@thingy.jp>
762306a36Sopenharmony_ci */
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#include <linux/init.h>
1062306a36Sopenharmony_ci#include <asm/mach/arch.h>
1162306a36Sopenharmony_ci#include <asm/mach/map.h>
1262306a36Sopenharmony_ci#include <linux/of.h>
1362306a36Sopenharmony_ci#include <linux/of_address.h>
1462306a36Sopenharmony_ci#include <linux/io.h>
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci/*
1762306a36Sopenharmony_ci * In the u-boot code the area these registers are in is
1862306a36Sopenharmony_ci * called "L3 bridge" and there are register descriptions
1962306a36Sopenharmony_ci * for something in the same area called "AXI".
2062306a36Sopenharmony_ci *
2162306a36Sopenharmony_ci * It's not exactly known what this is but the vendor code
2262306a36Sopenharmony_ci * for both u-boot and linux share calls to "flush the miu pipe".
2362306a36Sopenharmony_ci * This seems to be to force pending CPU writes to memory so that
2462306a36Sopenharmony_ci * the state is right before DMA capable devices try to read
2562306a36Sopenharmony_ci * descriptors and data the CPU has prepared. Without doing this
2662306a36Sopenharmony_ci * ethernet doesn't work reliably for example.
2762306a36Sopenharmony_ci */
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci#define MSTARV7_L3BRIDGE_FLUSH		0x14
3062306a36Sopenharmony_ci#define MSTARV7_L3BRIDGE_STATUS		0x40
3162306a36Sopenharmony_ci#define MSTARV7_L3BRIDGE_FLUSH_TRIGGER	BIT(0)
3262306a36Sopenharmony_ci#define MSTARV7_L3BRIDGE_STATUS_DONE	BIT(12)
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci#ifdef CONFIG_SMP
3562306a36Sopenharmony_ci#define MSTARV7_CPU1_BOOT_ADDR_HIGH	0x4c
3662306a36Sopenharmony_ci#define MSTARV7_CPU1_BOOT_ADDR_LOW	0x50
3762306a36Sopenharmony_ci#define MSTARV7_CPU1_UNLOCK		0x58
3862306a36Sopenharmony_ci#define MSTARV7_CPU1_UNLOCK_MAGIC	0xbabe
3962306a36Sopenharmony_ci#endif
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_cistatic void __iomem *l3bridge;
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_cistatic const char * const mstarv7_board_dt_compat[] __initconst = {
4462306a36Sopenharmony_ci	"mstar,infinity",
4562306a36Sopenharmony_ci	"mstar,infinity2m",
4662306a36Sopenharmony_ci	"mstar,infinity3",
4762306a36Sopenharmony_ci	"mstar,mercury5",
4862306a36Sopenharmony_ci	NULL,
4962306a36Sopenharmony_ci};
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci/*
5262306a36Sopenharmony_ci * This may need locking to deal with situations where an interrupt
5362306a36Sopenharmony_ci * happens while we are in here and mb() gets called by the interrupt handler.
5462306a36Sopenharmony_ci *
5562306a36Sopenharmony_ci * The vendor code did have a spin lock but it doesn't seem to be needed and
5662306a36Sopenharmony_ci * removing it hasn't caused any side effects so far.
5762306a36Sopenharmony_ci *
5862306a36Sopenharmony_ci * [writel|readl]_relaxed have to be used here because otherwise
5962306a36Sopenharmony_ci * we'd end up right back in here.
6062306a36Sopenharmony_ci */
6162306a36Sopenharmony_cistatic void mstarv7_mb(void)
6262306a36Sopenharmony_ci{
6362306a36Sopenharmony_ci	/* toggle the flush miu pipe fire bit */
6462306a36Sopenharmony_ci	writel_relaxed(0, l3bridge + MSTARV7_L3BRIDGE_FLUSH);
6562306a36Sopenharmony_ci	writel_relaxed(MSTARV7_L3BRIDGE_FLUSH_TRIGGER, l3bridge
6662306a36Sopenharmony_ci			+ MSTARV7_L3BRIDGE_FLUSH);
6762306a36Sopenharmony_ci	while (!(readl_relaxed(l3bridge + MSTARV7_L3BRIDGE_STATUS)
6862306a36Sopenharmony_ci			& MSTARV7_L3BRIDGE_STATUS_DONE)) {
6962306a36Sopenharmony_ci		/* wait for flush to complete */
7062306a36Sopenharmony_ci	}
7162306a36Sopenharmony_ci}
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ci#ifdef CONFIG_SMP
7462306a36Sopenharmony_cistatic int mstarv7_boot_secondary(unsigned int cpu, struct task_struct *idle)
7562306a36Sopenharmony_ci{
7662306a36Sopenharmony_ci	struct device_node *np;
7762306a36Sopenharmony_ci	u32 bootaddr = (u32) __pa_symbol(secondary_startup_arm);
7862306a36Sopenharmony_ci	void __iomem *smpctrl;
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci	/*
8162306a36Sopenharmony_ci	 * right now we don't know how to boot anything except
8262306a36Sopenharmony_ci	 * cpu 1.
8362306a36Sopenharmony_ci	 */
8462306a36Sopenharmony_ci	if (cpu != 1)
8562306a36Sopenharmony_ci		return -EINVAL;
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci	np = of_find_compatible_node(NULL, NULL, "mstar,smpctrl");
8862306a36Sopenharmony_ci	smpctrl = of_iomap(np, 0);
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci	if (!smpctrl)
9162306a36Sopenharmony_ci		return -ENODEV;
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci	/* set the boot address for the second cpu */
9462306a36Sopenharmony_ci	writew(bootaddr & 0xffff, smpctrl + MSTARV7_CPU1_BOOT_ADDR_LOW);
9562306a36Sopenharmony_ci	writew((bootaddr >> 16) & 0xffff, smpctrl + MSTARV7_CPU1_BOOT_ADDR_HIGH);
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci	/* unlock the second cpu */
9862306a36Sopenharmony_ci	writew(MSTARV7_CPU1_UNLOCK_MAGIC, smpctrl + MSTARV7_CPU1_UNLOCK);
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci	/* and away we go...*/
10162306a36Sopenharmony_ci	arch_send_wakeup_ipi_mask(cpumask_of(cpu));
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci	iounmap(smpctrl);
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci	return 0;
10662306a36Sopenharmony_ci}
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_cistatic const struct smp_operations __initdata mstarv7_smp_ops = {
10962306a36Sopenharmony_ci	.smp_boot_secondary = mstarv7_boot_secondary,
11062306a36Sopenharmony_ci};
11162306a36Sopenharmony_ci#endif
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_cistatic void __init mstarv7_init(void)
11462306a36Sopenharmony_ci{
11562306a36Sopenharmony_ci	struct device_node *np;
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci	np = of_find_compatible_node(NULL, NULL, "mstar,l3bridge");
11862306a36Sopenharmony_ci	l3bridge = of_iomap(np, 0);
11962306a36Sopenharmony_ci	if (l3bridge)
12062306a36Sopenharmony_ci		soc_mb = mstarv7_mb;
12162306a36Sopenharmony_ci	else
12262306a36Sopenharmony_ci		pr_warn("Failed to install memory barrier, DMA will be broken!\n");
12362306a36Sopenharmony_ci}
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ciDT_MACHINE_START(MSTARV7_DT, "MStar/Sigmastar Armv7 (Device Tree)")
12662306a36Sopenharmony_ci	.dt_compat	= mstarv7_board_dt_compat,
12762306a36Sopenharmony_ci	.init_machine	= mstarv7_init,
12862306a36Sopenharmony_ci	.smp		= smp_ops(mstarv7_smp_ops),
12962306a36Sopenharmony_ciMACHINE_END
130