xref: /linux/arch/mips/mti-malta/malta-dtshim.c (revision 762f99f4f3cb41a775b5157dd761217beba65873)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2e81a8c7dSPaul Burton /*
3e81a8c7dSPaul Burton  * Copyright (C) 2015 Imagination Technologies
4fb615d61SPaul Burton  * Author: Paul Burton <paul.burton@mips.com>
5e81a8c7dSPaul Burton  */
6e81a8c7dSPaul Burton 
7e81a8c7dSPaul Burton #include <linux/bug.h>
8e81a8c7dSPaul Burton #include <linux/kernel.h>
9e81a8c7dSPaul Burton #include <linux/libfdt.h>
10e81a8c7dSPaul Burton #include <linux/of_fdt.h>
11e81a8c7dSPaul Burton #include <linux/sizes.h>
120051fc2eSPaul Burton #include <asm/addrspace.h>
13e81a8c7dSPaul Burton #include <asm/bootinfo.h>
14e81a8c7dSPaul Burton #include <asm/fw/fw.h>
150051fc2eSPaul Burton #include <asm/mips-boards/generic.h>
1638ec82feSPaul Burton #include <asm/mips-boards/malta.h>
17e83f7e02SPaul Burton #include <asm/mips-cps.h>
18e81a8c7dSPaul Burton #include <asm/page.h>
19e81a8c7dSPaul Burton 
200051fc2eSPaul Burton #define ROCIT_REG_BASE			0x1f403000
210051fc2eSPaul Burton #define ROCIT_CONFIG_GEN1		(ROCIT_REG_BASE + 0x04)
220051fc2eSPaul Burton #define  ROCIT_CONFIG_GEN1_MEMMAP_SHIFT	8
230051fc2eSPaul Burton #define  ROCIT_CONFIG_GEN1_MEMMAP_MASK	(0xf << 8)
240051fc2eSPaul Burton 
25bea6a94aSOleksij Rempel static unsigned char fdt_buf[16 << 10] __initdata __aligned(8);
26e81a8c7dSPaul Burton 
27e81a8c7dSPaul Burton /* determined physical memory size, not overridden by command line args	 */
28e81a8c7dSPaul Burton extern unsigned long physical_memsize;
29e81a8c7dSPaul Burton 
300051fc2eSPaul Burton enum mem_map {
310051fc2eSPaul Burton 	MEM_MAP_V1 = 0,
320051fc2eSPaul Burton 	MEM_MAP_V2,
330051fc2eSPaul Burton };
34e81a8c7dSPaul Burton 
350051fc2eSPaul Burton #define MAX_MEM_ARRAY_ENTRIES 2
360051fc2eSPaul Burton 
malta_scon(void)370051fc2eSPaul Burton static __init int malta_scon(void)
380051fc2eSPaul Burton {
390051fc2eSPaul Burton 	int scon = MIPS_REVISION_SCONID;
400051fc2eSPaul Burton 
410051fc2eSPaul Burton 	if (scon != MIPS_REVISION_SCON_OTHER)
420051fc2eSPaul Burton 		return scon;
430051fc2eSPaul Burton 
440051fc2eSPaul Burton 	switch (MIPS_REVISION_CORID) {
450051fc2eSPaul Burton 	case MIPS_REVISION_CORID_QED_RM5261:
460051fc2eSPaul Burton 	case MIPS_REVISION_CORID_CORE_LV:
470051fc2eSPaul Burton 	case MIPS_REVISION_CORID_CORE_FPGA:
480051fc2eSPaul Burton 	case MIPS_REVISION_CORID_CORE_FPGAR2:
490051fc2eSPaul Burton 		return MIPS_REVISION_SCON_GT64120;
500051fc2eSPaul Burton 
510051fc2eSPaul Burton 	case MIPS_REVISION_CORID_CORE_EMUL_BON:
520051fc2eSPaul Burton 	case MIPS_REVISION_CORID_BONITO64:
530051fc2eSPaul Burton 	case MIPS_REVISION_CORID_CORE_20K:
540051fc2eSPaul Burton 		return MIPS_REVISION_SCON_BONITO;
550051fc2eSPaul Burton 
560051fc2eSPaul Burton 	case MIPS_REVISION_CORID_CORE_MSC:
570051fc2eSPaul Burton 	case MIPS_REVISION_CORID_CORE_FPGA2:
580051fc2eSPaul Burton 	case MIPS_REVISION_CORID_CORE_24K:
590051fc2eSPaul Burton 		return MIPS_REVISION_SCON_SOCIT;
600051fc2eSPaul Burton 
610051fc2eSPaul Burton 	case MIPS_REVISION_CORID_CORE_FPGA3:
620051fc2eSPaul Burton 	case MIPS_REVISION_CORID_CORE_FPGA4:
630051fc2eSPaul Burton 	case MIPS_REVISION_CORID_CORE_FPGA5:
640051fc2eSPaul Burton 	case MIPS_REVISION_CORID_CORE_EMUL_MSC:
650051fc2eSPaul Burton 	default:
660051fc2eSPaul Burton 		return MIPS_REVISION_SCON_ROCIT;
670051fc2eSPaul Burton 	}
680051fc2eSPaul Burton }
690051fc2eSPaul Burton 
gen_fdt_mem_array(__be32 * mem_array,unsigned long size,enum mem_map map)700051fc2eSPaul Burton static unsigned __init gen_fdt_mem_array(__be32 *mem_array, unsigned long size,
710051fc2eSPaul Burton 					 enum mem_map map)
72e81a8c7dSPaul Burton {
73e81a8c7dSPaul Burton 	unsigned long size_preio;
74e81a8c7dSPaul Burton 	unsigned entries;
75e81a8c7dSPaul Burton 
76e81a8c7dSPaul Burton 	entries = 1;
77e81a8c7dSPaul Burton 	mem_array[0] = cpu_to_be32(PHYS_OFFSET);
7897f2645fSMasahiro Yamada 	if (IS_ENABLED(CONFIG_EVA)) {
79e81a8c7dSPaul Burton 		/*
80e81a8c7dSPaul Burton 		 * The current Malta EVA configuration is "special" in that it
81e81a8c7dSPaul Burton 		 * always makes use of addresses in the upper half of the 32 bit
82e81a8c7dSPaul Burton 		 * physical address map, which gives it a contiguous region of
83e81a8c7dSPaul Burton 		 * DDR but limits it to 2GB.
84e81a8c7dSPaul Burton 		 */
85e81a8c7dSPaul Burton 		mem_array[1] = cpu_to_be32(size);
860051fc2eSPaul Burton 		goto done;
87e81a8c7dSPaul Burton 	}
88e81a8c7dSPaul Burton 
890051fc2eSPaul Burton 	size_preio = min_t(unsigned long, size, SZ_256M);
900051fc2eSPaul Burton 	mem_array[1] = cpu_to_be32(size_preio);
910051fc2eSPaul Burton 	size -= size_preio;
920051fc2eSPaul Burton 	if (!size)
930051fc2eSPaul Burton 		goto done;
940051fc2eSPaul Burton 
950051fc2eSPaul Burton 	if (map == MEM_MAP_V2) {
960051fc2eSPaul Burton 		/*
970051fc2eSPaul Burton 		 * We have a flat 32 bit physical memory map with DDR filling
980051fc2eSPaul Burton 		 * all 4GB of the memory map, apart from the I/O region which
990051fc2eSPaul Burton 		 * obscures 256MB from 0x10000000-0x1fffffff.
1000051fc2eSPaul Burton 		 *
1010051fc2eSPaul Burton 		 * Therefore we discard the 256MB behind the I/O region.
1020051fc2eSPaul Burton 		 */
1030051fc2eSPaul Burton 		if (size <= SZ_256M)
1040051fc2eSPaul Burton 			goto done;
1050051fc2eSPaul Burton 		size -= SZ_256M;
1060051fc2eSPaul Burton 
1070051fc2eSPaul Burton 		/* Make use of the memory following the I/O region */
1080051fc2eSPaul Burton 		entries++;
1090051fc2eSPaul Burton 		mem_array[2] = cpu_to_be32(PHYS_OFFSET + SZ_512M);
1100051fc2eSPaul Burton 		mem_array[3] = cpu_to_be32(size);
1110051fc2eSPaul Burton 	} else {
1120051fc2eSPaul Burton 		/*
1130051fc2eSPaul Burton 		 * We have a 32 bit physical memory map with a 2GB DDR region
1140051fc2eSPaul Burton 		 * aliased in the upper & lower halves of it. The I/O region
1150051fc2eSPaul Burton 		 * obscures 256MB from 0x10000000-0x1fffffff in the low alias
1160051fc2eSPaul Burton 		 * but the DDR it obscures is accessible via the high alias.
1170051fc2eSPaul Burton 		 *
1180051fc2eSPaul Burton 		 * Simply access everything beyond the lowest 256MB of DDR using
1190051fc2eSPaul Burton 		 * the high alias.
1200051fc2eSPaul Burton 		 */
1210051fc2eSPaul Burton 		entries++;
1220051fc2eSPaul Burton 		mem_array[2] = cpu_to_be32(PHYS_OFFSET + SZ_2G + SZ_256M);
1230051fc2eSPaul Burton 		mem_array[3] = cpu_to_be32(size);
1240051fc2eSPaul Burton 	}
1250051fc2eSPaul Burton 
1260051fc2eSPaul Burton done:
127e81a8c7dSPaul Burton 	BUG_ON(entries > MAX_MEM_ARRAY_ENTRIES);
128e81a8c7dSPaul Burton 	return entries;
129e81a8c7dSPaul Burton }
130e81a8c7dSPaul Burton 
append_memory(void * fdt,int root_off)131e81a8c7dSPaul Burton static void __init append_memory(void *fdt, int root_off)
132e81a8c7dSPaul Burton {
133e81a8c7dSPaul Burton 	__be32 mem_array[2 * MAX_MEM_ARRAY_ENTRIES];
134e81a8c7dSPaul Burton 	unsigned long memsize;
135e81a8c7dSPaul Burton 	unsigned mem_entries;
136e81a8c7dSPaul Burton 	int i, err, mem_off;
1370051fc2eSPaul Burton 	enum mem_map mem_map;
1380051fc2eSPaul Burton 	u32 config;
139e81a8c7dSPaul Burton 	char *var, param_name[10], *var_names[] = {
140e81a8c7dSPaul Burton 		"ememsize", "memsize",
141e81a8c7dSPaul Burton 	};
142e81a8c7dSPaul Burton 
143e81a8c7dSPaul Burton 	/* if a memory node already exists, leave it alone */
144e81a8c7dSPaul Burton 	mem_off = fdt_path_offset(fdt, "/memory");
145e81a8c7dSPaul Burton 	if (mem_off >= 0)
146e81a8c7dSPaul Burton 		return;
147e81a8c7dSPaul Burton 
148e81a8c7dSPaul Burton 	/* find memory size from the bootloader environment */
149e81a8c7dSPaul Burton 	for (i = 0; i < ARRAY_SIZE(var_names); i++) {
150e81a8c7dSPaul Burton 		var = fw_getenv(var_names[i]);
151e81a8c7dSPaul Burton 		if (!var)
152e81a8c7dSPaul Burton 			continue;
153e81a8c7dSPaul Burton 
154e81a8c7dSPaul Burton 		err = kstrtoul(var, 0, &physical_memsize);
155e81a8c7dSPaul Burton 		if (!err)
156e81a8c7dSPaul Burton 			break;
157e81a8c7dSPaul Burton 
158e81a8c7dSPaul Burton 		pr_warn("Failed to read the '%s' env variable '%s'\n",
159e81a8c7dSPaul Burton 			var_names[i], var);
160e81a8c7dSPaul Burton 	}
161e81a8c7dSPaul Burton 
162e81a8c7dSPaul Burton 	if (!physical_memsize) {
163e81a8c7dSPaul Burton 		pr_warn("The bootloader didn't provide memsize: defaulting to 32MB\n");
164e81a8c7dSPaul Burton 		physical_memsize = 32 << 20;
165e81a8c7dSPaul Burton 	}
166e81a8c7dSPaul Burton 
16797f2645fSMasahiro Yamada 	if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN)) {
168e81a8c7dSPaul Burton 		/*
169e81a8c7dSPaul Burton 		 * SOC-it swaps, or perhaps doesn't swap, when DMA'ing
170e81a8c7dSPaul Burton 		 * the last word of physical memory.
171e81a8c7dSPaul Burton 		 */
172e81a8c7dSPaul Burton 		physical_memsize -= PAGE_SIZE;
173e81a8c7dSPaul Burton 	}
174e81a8c7dSPaul Burton 
175e81a8c7dSPaul Burton 	/* default to using all available RAM */
176e81a8c7dSPaul Burton 	memsize = physical_memsize;
177e81a8c7dSPaul Burton 
178e81a8c7dSPaul Burton 	/* allow the user to override the usable memory */
179e81a8c7dSPaul Burton 	for (i = 0; i < ARRAY_SIZE(var_names); i++) {
180e81a8c7dSPaul Burton 		snprintf(param_name, sizeof(param_name), "%s=", var_names[i]);
181e81a8c7dSPaul Burton 		var = strstr(arcs_cmdline, param_name);
182e81a8c7dSPaul Burton 		if (!var)
183e81a8c7dSPaul Burton 			continue;
184e81a8c7dSPaul Burton 
185e81a8c7dSPaul Burton 		memsize = memparse(var + strlen(param_name), NULL);
186e81a8c7dSPaul Burton 	}
187e81a8c7dSPaul Burton 
188e81a8c7dSPaul Burton 	/* if the user says there's more RAM than we thought, believe them */
189e81a8c7dSPaul Burton 	physical_memsize = max_t(unsigned long, physical_memsize, memsize);
190e81a8c7dSPaul Burton 
1910051fc2eSPaul Burton 	/* detect the memory map in use */
1920051fc2eSPaul Burton 	if (malta_scon() == MIPS_REVISION_SCON_ROCIT) {
1930051fc2eSPaul Burton 		/* ROCit has a register indicating the memory map in use */
1940051fc2eSPaul Burton 		config = readl((void __iomem *)CKSEG1ADDR(ROCIT_CONFIG_GEN1));
1950051fc2eSPaul Burton 		mem_map = config & ROCIT_CONFIG_GEN1_MEMMAP_MASK;
1960051fc2eSPaul Burton 		mem_map >>= ROCIT_CONFIG_GEN1_MEMMAP_SHIFT;
1970051fc2eSPaul Burton 	} else {
1980051fc2eSPaul Burton 		/* if not using ROCit, presume the v1 memory map */
1990051fc2eSPaul Burton 		mem_map = MEM_MAP_V1;
2000051fc2eSPaul Burton 	}
2010051fc2eSPaul Burton 	if (mem_map > MEM_MAP_V2)
2020051fc2eSPaul Burton 		panic("Unsupported physical memory map v%u detected",
2030051fc2eSPaul Burton 		      (unsigned int)mem_map);
2040051fc2eSPaul Burton 
205e81a8c7dSPaul Burton 	/* append memory to the DT */
206e81a8c7dSPaul Burton 	mem_off = fdt_add_subnode(fdt, root_off, "memory");
207e81a8c7dSPaul Burton 	if (mem_off < 0)
208e81a8c7dSPaul Burton 		panic("Unable to add memory node to DT: %d", mem_off);
209e81a8c7dSPaul Burton 
210e81a8c7dSPaul Burton 	err = fdt_setprop_string(fdt, mem_off, "device_type", "memory");
211e81a8c7dSPaul Burton 	if (err)
212e81a8c7dSPaul Burton 		panic("Unable to set memory node device_type: %d", err);
213e81a8c7dSPaul Burton 
2140051fc2eSPaul Burton 	mem_entries = gen_fdt_mem_array(mem_array, physical_memsize, mem_map);
215e81a8c7dSPaul Burton 	err = fdt_setprop(fdt, mem_off, "reg", mem_array,
216e81a8c7dSPaul Burton 			  mem_entries * 2 * sizeof(mem_array[0]));
217e81a8c7dSPaul Burton 	if (err)
218e81a8c7dSPaul Burton 		panic("Unable to set memory regs property: %d", err);
219e81a8c7dSPaul Burton 
2200051fc2eSPaul Burton 	mem_entries = gen_fdt_mem_array(mem_array, memsize, mem_map);
221e81a8c7dSPaul Burton 	err = fdt_setprop(fdt, mem_off, "linux,usable-memory", mem_array,
222e81a8c7dSPaul Burton 			  mem_entries * 2 * sizeof(mem_array[0]));
223e81a8c7dSPaul Burton 	if (err)
224e81a8c7dSPaul Burton 		panic("Unable to set linux,usable-memory property: %d", err);
225e81a8c7dSPaul Burton }
226e81a8c7dSPaul Burton 
remove_gic(void * fdt)22738ec82feSPaul Burton static void __init remove_gic(void *fdt)
22838ec82feSPaul Burton {
22938ec82feSPaul Burton 	int err, gic_off, i8259_off, cpu_off;
23038ec82feSPaul Burton 	void __iomem *biu_base;
23138ec82feSPaul Burton 	uint32_t cpu_phandle, sc_cfg;
23238ec82feSPaul Burton 
23338ec82feSPaul Burton 	/* if we have a CM which reports a GIC is present, leave the DT alone */
23438ec82feSPaul Burton 	err = mips_cm_probe();
23593c5bba5SPaul Burton 	if (!err && (read_gcr_gic_status() & CM_GCR_GIC_STATUS_EX))
23638ec82feSPaul Burton 		return;
23738ec82feSPaul Burton 
23838ec82feSPaul Burton 	if (malta_scon() == MIPS_REVISION_SCON_ROCIT) {
23938ec82feSPaul Burton 		/*
24038ec82feSPaul Burton 		 * On systems using the RocIT system controller a GIC may be
24138ec82feSPaul Burton 		 * present without a CM. Detect whether that is the case.
24238ec82feSPaul Burton 		 */
2434bdc0d67SChristoph Hellwig 		biu_base = ioremap(MSC01_BIU_REG_BASE,
24438ec82feSPaul Burton 				MSC01_BIU_ADDRSPACE_SZ);
24538ec82feSPaul Burton 		sc_cfg = __raw_readl(biu_base + MSC01_SC_CFG_OFS);
24638ec82feSPaul Burton 		if (sc_cfg & MSC01_SC_CFG_GICPRES_MSK) {
24738ec82feSPaul Burton 			/* enable the GIC at the system controller level */
24838ec82feSPaul Burton 			sc_cfg |= BIT(MSC01_SC_CFG_GICENA_SHF);
24938ec82feSPaul Burton 			__raw_writel(sc_cfg, biu_base + MSC01_SC_CFG_OFS);
25038ec82feSPaul Burton 			return;
25138ec82feSPaul Burton 		}
25238ec82feSPaul Burton 	}
25338ec82feSPaul Burton 
25438ec82feSPaul Burton 	gic_off = fdt_node_offset_by_compatible(fdt, -1, "mti,gic");
25538ec82feSPaul Burton 	if (gic_off < 0) {
25638ec82feSPaul Burton 		pr_warn("malta-dtshim: unable to find DT GIC node: %d\n",
25738ec82feSPaul Burton 			gic_off);
25838ec82feSPaul Burton 		return;
25938ec82feSPaul Burton 	}
26038ec82feSPaul Burton 
26138ec82feSPaul Burton 	err = fdt_nop_node(fdt, gic_off);
26238ec82feSPaul Burton 	if (err)
26338ec82feSPaul Burton 		pr_warn("malta-dtshim: unable to nop GIC node\n");
26438ec82feSPaul Burton 
26538ec82feSPaul Burton 	i8259_off = fdt_node_offset_by_compatible(fdt, -1, "intel,i8259");
26638ec82feSPaul Burton 	if (i8259_off < 0) {
26738ec82feSPaul Burton 		pr_warn("malta-dtshim: unable to find DT i8259 node: %d\n",
26838ec82feSPaul Burton 			i8259_off);
26938ec82feSPaul Burton 		return;
27038ec82feSPaul Burton 	}
27138ec82feSPaul Burton 
27238ec82feSPaul Burton 	cpu_off = fdt_node_offset_by_compatible(fdt, -1,
27338ec82feSPaul Burton 			"mti,cpu-interrupt-controller");
27438ec82feSPaul Burton 	if (cpu_off < 0) {
27538ec82feSPaul Burton 		pr_warn("malta-dtshim: unable to find CPU intc node: %d\n",
27638ec82feSPaul Burton 			cpu_off);
27738ec82feSPaul Burton 		return;
27838ec82feSPaul Burton 	}
27938ec82feSPaul Burton 
28038ec82feSPaul Burton 	cpu_phandle = fdt_get_phandle(fdt, cpu_off);
28138ec82feSPaul Burton 	if (!cpu_phandle) {
28238ec82feSPaul Burton 		pr_warn("malta-dtshim: unable to get CPU intc phandle\n");
28338ec82feSPaul Burton 		return;
28438ec82feSPaul Burton 	}
28538ec82feSPaul Burton 
28638ec82feSPaul Burton 	err = fdt_setprop_u32(fdt, i8259_off, "interrupt-parent", cpu_phandle);
28738ec82feSPaul Burton 	if (err) {
28838ec82feSPaul Burton 		pr_warn("malta-dtshim: unable to set i8259 interrupt-parent: %d\n",
28938ec82feSPaul Burton 			err);
29038ec82feSPaul Burton 		return;
29138ec82feSPaul Burton 	}
29238ec82feSPaul Burton 
29338ec82feSPaul Burton 	err = fdt_setprop_u32(fdt, i8259_off, "interrupts", 2);
29438ec82feSPaul Burton 	if (err) {
29538ec82feSPaul Burton 		pr_warn("malta-dtshim: unable to set i8259 interrupts: %d\n",
29638ec82feSPaul Burton 			err);
29738ec82feSPaul Burton 		return;
29838ec82feSPaul Burton 	}
29938ec82feSPaul Burton }
30038ec82feSPaul Burton 
malta_dt_shim(void * fdt)301e81a8c7dSPaul Burton void __init *malta_dt_shim(void *fdt)
302e81a8c7dSPaul Burton {
303e81a8c7dSPaul Burton 	int root_off, len, err;
304e81a8c7dSPaul Burton 	const char *compat;
305e81a8c7dSPaul Burton 
306e81a8c7dSPaul Burton 	if (fdt_check_header(fdt))
307e81a8c7dSPaul Burton 		panic("Corrupt DT");
308e81a8c7dSPaul Burton 
309e81a8c7dSPaul Burton 	err = fdt_open_into(fdt, fdt_buf, sizeof(fdt_buf));
310e81a8c7dSPaul Burton 	if (err)
311e81a8c7dSPaul Burton 		panic("Unable to open FDT: %d", err);
312e81a8c7dSPaul Burton 
313e81a8c7dSPaul Burton 	root_off = fdt_path_offset(fdt_buf, "/");
314e81a8c7dSPaul Burton 	if (root_off < 0)
315e81a8c7dSPaul Burton 		panic("No / node in DT");
316e81a8c7dSPaul Burton 
317e81a8c7dSPaul Burton 	compat = fdt_getprop(fdt_buf, root_off, "compatible", &len);
318e81a8c7dSPaul Burton 	if (!compat)
319e81a8c7dSPaul Burton 		panic("No root compatible property in DT: %d", len);
320e81a8c7dSPaul Burton 
321e81a8c7dSPaul Burton 	/* if this isn't Malta, leave the DT alone */
322e81a8c7dSPaul Burton 	if (strncmp(compat, "mti,malta", len))
323e81a8c7dSPaul Burton 		return fdt;
324e81a8c7dSPaul Burton 
325e81a8c7dSPaul Burton 	append_memory(fdt_buf, root_off);
32638ec82feSPaul Burton 	remove_gic(fdt_buf);
327e81a8c7dSPaul Burton 
328e81a8c7dSPaul Burton 	err = fdt_pack(fdt_buf);
329e81a8c7dSPaul Burton 	if (err)
330e81a8c7dSPaul Burton 		panic("Unable to pack FDT: %d\n", err);
331e81a8c7dSPaul Burton 
332e81a8c7dSPaul Burton 	return fdt_buf;
333e81a8c7dSPaul Burton }
334