mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Magnus Lindholm <linmag7@gmail.com>
To: davem@davemloft.net, andreas@gaisler.com
Cc: sam@ravnborg.org, sparclinux@vger.kernel.org,
	linux-kernel@vger.kernel.org, linmag7@gmail.com
Subject: [PATCH 13/18] sparc32,leon: discover AMBA devices for legacy U-Boot PROMs
Date: Thu, 26 Nov 2026 10:30:18 +0100	[thread overview]
Message-ID: <20261126093106.1345825-14-linmag7@gmail.com> (raw)
In-Reply-To: <20261126093106.1345825-1-linmag7@gmail.com>

Factory LEON U-Boot supplies no AMBA device nodes. For its legacy PROM
signature, discover GRLIB plug-and-play buses and synthesize the missing
nodes, resources and clocks with bounded traversal.

Publish the system ID needed by the existing LEON4 idle workaround.
Reserve a conflict-free IPI only for SMP. Preserve firmware-supplied
trees; reject unsupported required topology rather than continuing
without usable interrupt and timer controllers.

Signed-off-by: Magnus Lindholm <linmag7@gmail.com>
---
 arch/sparc/kernel/Makefile      |   1 +
 arch/sparc/kernel/leon_legacy.c | 343 ++++++++++++++++++++++++++++++++
 arch/sparc/kernel/prom.h        |   5 +
 arch/sparc/kernel/prom_common.c |   1 +
 drivers/of/pdt.c                |   9 +-
 include/linux/of_pdt.h          |   5 +
 6 files changed, 362 insertions(+), 2 deletions(-)
 create mode 100644 arch/sparc/kernel/leon_legacy.c

diff --git a/arch/sparc/kernel/Makefile b/arch/sparc/kernel/Makefile
index 497b5714fa8f..6bfc23ce3b08 100644
--- a/arch/sparc/kernel/Makefile
+++ b/arch/sparc/kernel/Makefile
@@ -53,6 +53,7 @@ obj-$(CONFIG_SPARC64)   += prom_irqtrans.o
 
 obj-$(CONFIG_SPARC32)   += leon_kernel.o
 obj-$(CONFIG_SPARC32)   += leon_pmc.o
+obj-$(CONFIG_SPARC_LEON) += leon_legacy.o
 
 obj-$(CONFIG_SPARC64)   += reboot.o
 obj-$(CONFIG_SPARC64)   += sysfs.o
diff --git a/arch/sparc/kernel/leon_legacy.c b/arch/sparc/kernel/leon_legacy.c
new file mode 100644
index 000000000000..a9364708e231
--- /dev/null
+++ b/arch/sparc/kernel/leon_legacy.c
@@ -0,0 +1,343 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Written by Magnus Lindholm, linmag7@gmail.com
+ *
+ * Build the AMBA device tree omitted by the factory LEON U-Boot.
+ */
+
+#include <linux/init.h>
+#include <linux/kernel.h>
+#include <linux/of.h>
+#include <linux/of_pdt.h>
+#include <linux/string.h>
+
+#include <asm/leon.h>
+#include <asm/leon_amba.h>
+#include <asm/oplib.h>
+#include <asm/cpu_type.h>
+
+#include "prom.h"
+
+#define LEGACY_MAX_BUSES 8
+#define LEGACY_AHB_SLOTS 64
+#define LEGACY_APB_SLOTS 16
+
+struct legacy_bus {
+	u32 area;
+	u32 freq;
+	u32 bridge;
+	unsigned int parent;
+};
+
+static struct legacy_bus legacy_buses[LEGACY_MAX_BUSES] __initdata;
+static unsigned int legacy_bus_count __initdata;
+static phandle legacy_phandle __initdata;
+static u32 legacy_irqs __initdata = BIT(0) | BIT(15);
+
+static u32 __init legacy_read(u32 address)
+{
+	return LEON3_BYPASS_LOAD_PA(address);
+}
+
+static bool __init legacy_is(u32 id, unsigned int device)
+{
+	return (id >> 24) == VENDOR_GAISLER &&
+	       ((id >> 12) & 0xfff) == device;
+}
+
+/* GRLIB AHB memory and bus-relative AHB I/O BAR encodings. */
+static bool __init legacy_bar(u32 area, u32 bar, u32 *addr, u32 *size)
+{
+	u32 mask = (bar >> 4) & 0xfff;
+	u32 start = (bar & 0xfff00000) & (mask << 20);
+
+	switch (bar & 0xf) {
+	case 2:
+		*addr = start;
+		*size = -(mask << 20);
+		break;
+	case 3:
+		*addr = area | (start >> 12);
+		*size = ((~mask & 0xfff) + 1) << 8;
+		break;
+	default:
+		return false;
+	}
+	return *size && is_power_of_2(*size);
+}
+
+static void __init legacy_property(struct device_node *np, const char *name,
+				   const void *value, unsigned int length)
+{
+	struct property *p = prom_early_alloc(sizeof(*p));
+
+	p->name = prom_early_alloc(strlen(name) + 1);
+	strscpy(p->name, name, strlen(name) + 1);
+	p->value = prom_early_alloc(length + 1);
+	memcpy(p->value, value, length);
+	p->length = length;
+	p->unique_id = of_pdt_alloc_unique_id();
+	p->next = np->properties;
+	np->properties = p;
+}
+
+static void __init legacy_u32(struct device_node *np, const char *name, u32 val)
+{
+	__be32 cell = cpu_to_be32(val);
+
+	legacy_property(np, name, &cell, sizeof(cell));
+}
+
+static struct device_node * __init legacy_node(struct device_node *parent,
+					       const char *name, const char *path)
+{
+	struct device_node *np = prom_early_alloc(sizeof(*np));
+	struct device_node **tail = &parent->child;
+
+	of_node_init(np);
+	np->name = prom_early_alloc(strlen(name) + 1);
+	strscpy((char *)np->name, name, strlen(name) + 1);
+	np->full_name = prom_early_alloc(strlen(path) + 1);
+	strscpy((char *)np->full_name, path, strlen(path) + 1);
+	np->parent = parent;
+	np->phandle = ++legacy_phandle;
+	np->unique_id = of_pdt_alloc_unique_id();
+	legacy_property(np, "name", name, strlen(name) + 1);
+	legacy_u32(np, ".node", np->phandle);
+	/* Like the PROM builder, publish before OF device registration starts. */
+	while (*tail)
+		tail = &(*tail)->sibling;
+	*tail = np;
+	return np;
+}
+
+/* Follow version-1 AHB2AHB bridges, including reverse links, once per bus. */
+static int __init legacy_scan_buses(void)
+{
+	unsigned int b, slot, seen;
+	int cpu_bus = -1;
+
+	legacy_bus_count = 1;
+	legacy_buses[0].area = LEON3_IO_AREA;
+	for (b = 0; b < legacy_bus_count; b++) {
+		u32 table = legacy_buses[b].area | LEON3_CONF_AREA;
+
+		for (slot = 0; slot < LEGACY_AHB_SLOTS; slot++) {
+			u32 id = legacy_read(table + slot * 32);
+
+			legacy_irqs |= BIT(id & 0x1f);
+			if (legacy_is(id, GAISLER_LEON3) ||
+			    legacy_is(id, GAISLER_LEON4))
+				cpu_bus = b;
+		}
+		for (slot = 0; slot < LEGACY_AHB_SLOTS; slot++) {
+			u32 pnp = table + 0x800 + slot * 32;
+			u32 id = legacy_read(pnp);
+			u32 area;
+
+			if (!legacy_is(id, GAISLER_AHB2AHB))
+				continue;
+			if (((id >> 5) & 0x1f) != 1) {
+				pr_warn("LEON: unsupported legacy AHB bridge at %x\n", pnp);
+				continue;
+			}
+			area = legacy_read(pnp + 8);
+			if (!area || (area & 0xfffff))
+				continue;
+			for (seen = 0; seen < legacy_bus_count; seen++)
+				if (legacy_buses[seen].area == area)
+					break;
+			if (seen < legacy_bus_count)
+				continue;
+			if (legacy_bus_count == LEGACY_MAX_BUSES)
+				panic("LEON: too many legacy AMBA buses");
+			legacy_buses[seen].area = area;
+			legacy_buses[seen].parent = b;
+			legacy_buses[seen].bridge = legacy_read(pnp + 4);
+			legacy_bus_count++;
+		}
+	}
+	return cpu_bus;
+}
+
+/*
+ * This U-Boot puts CONFIG_SYS_CLK_FREQ / 1000 in the CPU property, but
+ * uses CONFIG_SYS_CLK_FREQ for its initial bus at LEON3_IO_AREA. It is
+ * the I/O-bus clock in kHz, not the actual CPU clock on a bridged LEON4.
+ */
+static bool __init legacy_clocks(int cpu_bus)
+{
+	struct device_node *cpu;
+	u32 freq, factor, config;
+	unsigned int b;
+
+	if (cpu_bus < 0)
+		return false;
+	cpu = of_find_node_by_type(NULL, "cpu");
+	if (!cpu)
+		return false;
+	freq = of_getintprop_default(cpu, "clock-frequency", 0);
+	of_node_put(cpu);
+	if (!freq || freq > U32_MAX / 1000)
+		return false;
+	freq *= 1000;
+	legacy_buses[0].freq = freq;
+	for (b = 1; b < legacy_bus_count; b++) {
+		freq = legacy_buses[legacy_buses[b].parent].freq;
+		/* AHB2AHB user[0]: FFACT at 7:4, direction at bit 8. */
+		config = legacy_buses[b].bridge;
+		factor = (config >> 4) & 0xf;
+		if (!factor)
+			factor = 1;
+		if (config & 0x100) {
+			if (freq > U32_MAX / factor)
+				return false;
+			freq *= factor;
+		} else {
+			freq /= factor;
+		}
+		legacy_buses[b].freq = freq;
+	}
+	return true;
+}
+
+static void __init legacy_device(struct device_node *bus, u32 id, u32 freq,
+				 __be32 *regs, unsigned int count)
+{
+	struct device_node *np;
+	char name[16], path[48];
+	u32 device = (id >> 12) & 0xfff;
+	u32 irq = id & 0x1f;
+	u32 addr = be32_to_cpu(regs[0]);
+	u32 config;
+	unsigned int timer;
+
+	legacy_irqs |= BIT(irq);
+	if (legacy_is(id, GAISLER_GPTIMER)) {
+		config = legacy_read(addr + 8);
+		if (config & BIT(8))
+			for (timer = 0; timer < (config & 7); timer++)
+				legacy_irqs |= BIT((irq + timer) & 0x1f);
+	}
+	if (legacy_is(id, GAISLER_IRQMP)) {
+		config = legacy_read(addr + 0x10);
+		legacy_irqs |= BIT((config >> 16) & 0xf);
+	}
+
+	snprintf(name, sizeof(name), "%02x_%03x", id >> 24, device);
+	snprintf(path, sizeof(path), "%s@%x,%x", name, irq,
+		 be32_to_cpu(regs[0]));
+	np = legacy_node(bus, name, path);
+	legacy_property(np, "reg", regs, count * 2 * sizeof(*regs));
+	legacy_u32(np, "freq", freq);
+	legacy_u32(np, "vendor", id >> 24);
+	legacy_u32(np, "device", device);
+	legacy_u32(np, "version", (id >> 5) & 0x1f);
+	if (irq)
+		legacy_u32(np, "interrupts", irq);
+	pr_info("LEON PnP: %s at %08x irq %u clock %u\n", name,
+		be32_to_cpu(regs[0]), irq, freq);
+}
+
+static void __init legacy_apb(struct device_node *bus, u32 base, u32 freq)
+{
+	unsigned int slot;
+
+	for (slot = 0; slot < LEGACY_APB_SLOTS; slot++) {
+		u32 pnp = (base | LEON3_CONF_AREA) + slot * 8;
+		u32 id = legacy_read(pnp), bar, addr, size, mask;
+		__be32 regs[2];
+
+		if (!(id >> 24) || !((id >> 12) & 0xfff))
+			continue;
+		bar = legacy_read(pnp + 4);
+		if ((bar & 0xf) != 1)
+			continue;
+		mask = (bar >> 4) & 0xfff;
+		addr = base | (((bar & 0xfff00000) >> 12) & (mask << 8));
+		size = ((~mask & 0xfff) + 1) << 8;
+		if (!is_power_of_2(size)) {
+			pr_warn("LEON: invalid APB mask at %x\n", pnp);
+			continue;
+		}
+		regs[0] = cpu_to_be32(addr);
+		regs[1] = cpu_to_be32(size);
+		legacy_device(bus, id, freq, regs, 1);
+	}
+}
+
+void __init leon_legacy_amba_init(void)
+{
+	struct device_node *bus, *np;
+	unsigned int b, slot, bar;
+	u32 systemid;
+	int cpu_bus, ipi;
+
+	if (sparc_cpu_model != sparc_leon || !prom_leon_legacy)
+		return;
+	np = of_find_node_by_path("/ambapp0");
+	if (np) {
+		of_node_put(np);
+		return;
+	}
+
+	cpu_bus = legacy_scan_buses();
+	if (!legacy_clocks(cpu_bus))
+		panic("LEON: cannot determine legacy AMBA clocks");
+	for_each_of_allnodes(np)
+		if (np->phandle > legacy_phandle)
+			legacy_phandle = np->phandle;
+	bus = legacy_node(of_root, "ambapp0", "ambapp0");
+	legacy_property(bus, "device_type", "ambapp", sizeof("ambapp"));
+	legacy_u32(bus, "#address-cells", 1);
+	legacy_u32(bus, "#size-cells", 1);
+	legacy_u32(bus, "freq", legacy_buses[0].freq);
+	/* The last four words of the AHB PnP area contain the system ID. */
+	systemid = legacy_read(legacy_buses[0].area | LEON3_CONF_AREA |
+			       LEON3_AHB_SYSTEMID);
+	legacy_u32(bus, "systemid", systemid);
+	pr_info("LEON PnP: system ID %08x\n", systemid);
+
+	for (b = 0; b < legacy_bus_count; b++) {
+		u32 area = legacy_buses[b].area;
+		u32 freq = legacy_buses[b].freq;
+		u32 table = area | LEON3_CONF_AREA;
+
+		pr_info("LEON PnP: bus %08x clock %u\n", area, freq);
+		for (slot = 0; slot < LEGACY_AHB_SLOTS; slot++) {
+			u32 pnp = table + 0x800 + slot * 32;
+			u32 id = legacy_read(pnp), addr, size;
+			__be32 regs[8];
+			unsigned int count = 0;
+
+			if (!(id >> 24) || !((id >> 12) & 0xfff) ||
+			    legacy_is(id, GAISLER_AHB2AHB))
+				continue;
+			for (bar = 0; bar < 4; bar++) {
+				if (!legacy_bar(area, legacy_read(pnp + 16 + bar * 4),
+						&addr, &size))
+					continue;
+				regs[count * 2] = cpu_to_be32(addr);
+				regs[count * 2 + 1] = cpu_to_be32(size);
+				count++;
+			}
+			if (!count)
+				continue;
+			if (legacy_is(id, GAISLER_APBMST)) {
+				legacy_apb(bus, be32_to_cpu(regs[0]), freq);
+				continue;
+			}
+			legacy_device(bus, id, freq, regs, count);
+		}
+	}
+	if (!IS_ENABLED(CONFIG_SMP))
+		return;
+	/* IRQ15 is reserved for cross-calls; avoid all peripheral IRQs. */
+	for (ipi = 14; ipi > 0; ipi--)
+		if (!(legacy_irqs & BIT(ipi)))
+			break;
+	if (!ipi)
+		panic("LEON: no free legacy AMBA IPI interrupt");
+	legacy_u32(bus, "ipi_num", ipi);
+	pr_info("LEON PnP: selected IPI IRQ %d\n", ipi);
+}
diff --git a/arch/sparc/kernel/prom.h b/arch/sparc/kernel/prom.h
index 26a1cca7c761..60ecbd064a4f 100644
--- a/arch/sparc/kernel/prom.h
+++ b/arch/sparc/kernel/prom.h
@@ -6,6 +6,11 @@
 #include <asm/prom.h>
 
 void of_console_init(void);
+#ifdef CONFIG_SPARC_LEON
+void leon_legacy_amba_init(void);
+#else
+static inline void leon_legacy_amba_init(void) { }
+#endif
 
 extern unsigned int prom_early_allocated;
 
diff --git a/arch/sparc/kernel/prom_common.c b/arch/sparc/kernel/prom_common.c
index d258fd10db01..770d780bf830 100644
--- a/arch/sparc/kernel/prom_common.c
+++ b/arch/sparc/kernel/prom_common.c
@@ -154,6 +154,7 @@ static struct of_pdt_ops prom_sparc_ops __initdata = {
 void __init prom_build_devicetree(void)
 {
 	of_pdt_build_devicetree(prom_root_node, &prom_sparc_ops);
+	leon_legacy_amba_init();
 	of_console_init();
 
 	pr_info("PROM: Built device tree with %u bytes of memory.\n",
diff --git a/drivers/of/pdt.c b/drivers/of/pdt.c
index cb0cb374b21f..3c2d4684b7c4 100644
--- a/drivers/of/pdt.c
+++ b/drivers/of/pdt.c
@@ -24,10 +24,15 @@
 static struct of_pdt_ops *of_pdt_prom_ops __initdata;
 
 #if defined(CONFIG_SPARC)
-unsigned int of_pdt_unique_id __initdata;
+static unsigned int of_pdt_unique_id __initdata;
+
+unsigned int __init of_pdt_alloc_unique_id(void)
+{
+	return of_pdt_unique_id++;
+}
 
 #define of_pdt_incr_unique_id(p) do { \
-	(p)->unique_id = of_pdt_unique_id++; \
+	(p)->unique_id = of_pdt_alloc_unique_id(); \
 } while (0)
 
 static char * __init of_pdt_build_full_name(struct device_node *dp)
diff --git a/include/linux/of_pdt.h b/include/linux/of_pdt.h
index 89e4eb076a01..f89999ae68e5 100644
--- a/include/linux/of_pdt.h
+++ b/include/linux/of_pdt.h
@@ -32,6 +32,11 @@ struct of_pdt_ops {
 
 extern void *prom_early_alloc(unsigned long size);
 
+#ifdef CONFIG_SPARC
+/* Share the PROM node/property ID space with synthesized nodes. */
+unsigned int of_pdt_alloc_unique_id(void);
+#endif
+
 /* for building the device tree */
 extern void of_pdt_build_devicetree(phandle root_node, struct of_pdt_ops *ops);
 
-- 
2.43.0


  parent reply	other threads:[~2026-11-26  9:31 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26  9:30 [PATCH 00/18] sparc32: LEON SMP, timer accounting and legacy board support Magnus Lindholm
2026-11-26  9:30 ` [PATCH 01/18] sparc32,leon: give the cross-call IRQ highest priority Magnus Lindholm
2026-11-26  9:30 ` [PATCH 02/18] sparc32,leon: retain SMP TLB shootdowns Magnus Lindholm
2026-11-26  9:30 ` [PATCH 03/18] sparc32,leon: publish instruction updates across CPUs Magnus Lindholm
2026-11-26  9:30 ` [PATCH 04/18] sparc32,leon: avoid nested IRQ accounting in the timer handler Magnus Lindholm
2026-11-26  9:30 ` [PATCH 05/18] sparc32: preserve both LEON PROM character-output conventions Magnus Lindholm
2026-11-26  9:30 ` [PATCH 06/18] sparc32: tolerate firmware nodes without a name property Magnus Lindholm
2026-11-26  9:30 ` [PATCH 07/18] sparc32: skip unavailable PROM V0 console metadata Magnus Lindholm
2026-11-26  9:30 ` [PATCH 08/18] sparc32,leon: identify LEON before the firmware model lookup Magnus Lindholm
2026-11-26  9:30 ` [PATCH 09/18] sparc32,leon: preserve the Linux IRQ14 vector during startup Magnus Lindholm
2026-11-26  9:30 ` [PATCH 10/18] sparc32,leon: use ASI 1 for locks on the GR-LEON4-ITX Magnus Lindholm
2026-11-26  9:30 ` [PATCH 11/18] sparc32,leon: restore the kernel header offsets used by U-Boot Magnus Lindholm
2026-11-26  9:30 ` [PATCH 12/18] sparc32: recognize the legacy LEON firmware machine ID Magnus Lindholm
2026-11-26  9:30 ` Magnus Lindholm [this message]
2026-11-26  9:30 ` [PATCH 14/18] sparc32,leon: describe legacy AMBA I2C controllers Magnus Lindholm
2026-11-26  9:30 ` [PATCH 15/18] sparc32,leon: skip the GR-LEON4-ITX PROM mapping import Magnus Lindholm
2026-11-26  9:30 ` [PATCH 16/18] sparc32,leon: use the upper SDRAM on the GR-LEON4-ITX Magnus Lindholm
2026-11-26  9:30 ` [PATCH 17/18] sparc32,leon: combine legacy GRPCI1 plug-and-play resources Magnus Lindholm
2026-11-26  9:30 ` [PATCH 18/18] sparc32,leon: describe the DS1672 on the GR-LEON4-ITX Magnus Lindholm

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261126093106.1345825-14-linmag7@gmail.com \
    --to=linmag7@gmail.com \
    --cc=andreas@gaisler.com \
    --cc=davem@davemloft.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=sam@ravnborg.org \
    --cc=sparclinux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®