Skip to contentSkip to conversation

mips: n64: Clean up platform initialization

PATCH New Thorsten Blum 1 participant 5 messages Oct 29 MIPS

lore
Share & tools
lore
#0 lore raw

cover mips: n64: Clean up platform initialization

Clean up and simplify the Nintendo 64 initialization code.

Thorsten Blum (4):
mips: n64: Use DEFINE_RES_*() helpers
mips: n64: Simplify framebuffer allocation
mips: n64: Rename the video interface register helper
mips: n64: Use SZ_8M in plat_mem_setup()

 arch/mips/n64/init.c | 81 ++++++++++++++------------------------------
 1 file changed, 26 insertions(+), 55 deletions(-)
#1 lore raw

PATCH 1/4 mips: n64: Use DEFINE_RES_*() helpers

Use DEFINE_RES_MEM(), DEFINE_RES_IRQ(), and DEFINE_RES_MEM_NAMED() to simplify resource initialization. Drop the now-unused linux/string.h include.

Also use PLATFORM_DEVID_NONE instead of -1 for devices without an instance ID.

---
 arch/mips/n64/init.c | 44 +++++++++-----------------------------------
 1 file changed, 9 insertions(+), 35 deletions(-)
arch/mips/n64/init.c +9 -35
index ddbd7d1224d3..111d6e6c2de5 100644
@@ -10,7 +10,6 @@
 #include <linux/memblock.h>
 #include <linux/platform_device.h>
 #include <linux/platform_data/simplefb.h>
-#include <linux/string.h>
 
 #include <asm/bootinfo.h>
 #include <asm/fw/fw.h>
@@ -77,7 +76,6 @@ static const u32 ntsc_320[] __initconst = {
 
 static int __init n64_platform_init(void)
 {
-	static const char simplefb_resname[] = "FB";
 	static const struct simplefb_platform_data mode = {
 		.width = W,
 		.height = H,
@@ -89,34 +87,16 @@ static int __init n64_platform_init(void)
 	unsigned long phys;
 	unsigned i;
 
-	memset(res, 0, sizeof(struct resource) * 3);
-	res[0].flags = IORESOURCE_MEM;
-	res[0].start = MI_REG_BASE;
-	res[0].end = MI_REG_BASE + NUM_MI_REGS * 4 - 1;
+	res[0] = DEFINE_RES_MEM(MI_REG_BASE, NUM_MI_REGS * 4);
+	res[1] = DEFINE_RES_MEM(AI_REG_BASE, NUM_AI_REGS * 4);
+	res[2] = DEFINE_RES_IRQ(RCP_IRQ);
+	platform_device_register_simple("n64audio", PLATFORM_DEVID_NONE, res, 3);
 
-	res[1].flags = IORESOURCE_MEM;
-	res[1].start = AI_REG_BASE;
-	res[1].end = AI_REG_BASE + NUM_AI_REGS * 4 - 1;
+	res[0] = DEFINE_RES_MEM(PI_REG_BASE, NUM_PI_REGS * 4);
+	platform_device_register_simple("n64cart", PLATFORM_DEVID_NONE, res, 1);
 
-	res[2].flags = IORESOURCE_IRQ;
-	res[2].start = RCP_IRQ;
-	res[2].end = RCP_IRQ;
-
-	platform_device_register_simple("n64audio", -1, res, 3);
-
-	memset(&res[0], 0, sizeof(res[0]));
-	res[0].flags = IORESOURCE_MEM;
-	res[0].start = PI_REG_BASE;
-	res[0].end = PI_REG_BASE + NUM_PI_REGS * 4 - 1;
-
-	platform_device_register_simple("n64cart", -1, res, 1);
-
-	memset(&res[0], 0, sizeof(res[0]));
-	res[0].flags = IORESOURCE_MEM;
-	res[0].start = SI_REG_BASE;
-	res[0].end = SI_REG_BASE + NUM_SI_REGS * 4 - 1;
-
-	platform_device_register_simple("n64joy", -1, res, 1);
+	res[0] = DEFINE_RES_MEM(SI_REG_BASE, NUM_SI_REGS * 4);
+	platform_device_register_simple("n64joy", PLATFORM_DEVID_NONE, res, 1);
 
 	/* The framebuffer needs 64-byte alignment */
 	orig = kzalloc(W * H * 2 + 63, GFP_DMA | GFP_KERNEL);
@@ -133,13 +113,7 @@ static int __init n64_platform_init(void)
 			n64rdp_write_reg(i, ntsc_320[i]);
 	}
 
-	/* setup IORESOURCE_MEM as framebuffer memory */
-	memset(&res[0], 0, sizeof(res[0]));
-	res[0].flags = IORESOURCE_MEM;
-	res[0].name = simplefb_resname;
-	res[0].start = phys;
-	res[0].end = phys + W * H * 2 - 1;
-
+	res[0] = DEFINE_RES_MEM_NAMED(phys, W * H * 2, "FB");
 	platform_device_register_resndata(NULL, "simple-framebuffer", 0,
 					  &res[0], 1, &mode, sizeof(mode));
 
#2 lore raw

PATCH 2/4 mips: n64: Simplify framebuffer allocation

In reply to Thorsten Blum

The framebuffer size is already a multiple of 64 bytes and kzalloc() guarantees the required alignment. Use ALIGN() to document the 64-byte requirement, and remove the extra 63 bytes and manual address alignment.

While here, make mode automatic since platform device registration copies the data anyway. Also explicitly include linux/align.h for ALIGN() and linux/slab.h for kzalloc().

---
 arch/mips/n64/init.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
arch/mips/n64/init.c +7 -8
index 111d6e6c2de5..a64695454359 100644
@@ -4,12 +4,14 @@
  *
  *  Copyright (C) 2021	Lauri Kasanen
  */
+#include <linux/align.h>
 #include <linux/init.h>
 #include <linux/ioport.h>
 #include <linux/irq.h>
 #include <linux/memblock.h>
 #include <linux/platform_device.h>
 #include <linux/platform_data/simplefb.h>
+#include <linux/slab.h>
 
 #include <asm/bootinfo.h>
 #include <asm/fw/fw.h>
@@ -76,14 +78,14 @@ static const u32 ntsc_320[] __initconst = {
 
 static int __init n64_platform_init(void)
 {
-	static const struct simplefb_platform_data mode = {
+	const struct simplefb_platform_data mode = {
 		.width = W,
 		.height = H,
 		.stride = W * 2,
 		.format = "r5g5b5a1"
 	};
 	struct resource res[3];
-	void *orig;
+	void *fb;
 	unsigned long phys;
 	unsigned i;
 
@@ -98,13 +100,10 @@ static int __init n64_platform_init(void)
 	res[0] = DEFINE_RES_MEM(SI_REG_BASE, NUM_SI_REGS * 4);
 	platform_device_register_simple("n64joy", PLATFORM_DEVID_NONE, res, 1);
 
-	/* The framebuffer needs 64-byte alignment */
-	orig = kzalloc(W * H * 2 + 63, GFP_DMA | GFP_KERNEL);
-	if (!orig)
+	fb = kzalloc(ALIGN(W * H * 2, 64), GFP_DMA | GFP_KERNEL);
+	if (!fb)
 		return -ENOMEM;
-	phys = virt_to_phys(orig);
-	phys += 63;
-	phys &= ~63;
+	phys = virt_to_phys(fb);
 
 	for (i = 0; i < ARRAY_SIZE(ntsc_320); i++) {
 		if (i == 1)
#3 lore raw

PATCH 3/4 mips: n64: Rename the video interface register helper

In reply to Thorsten Blum

The helper writes VI (video interface) registers, but its name refers to the RDP (Reality Display Processor). Rename it to n64_vi_write_reg().

Also rename REG_BASE to VI_REG_BASE and remove the unnecessary #undefs.

---
 arch/mips/n64/init.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)
arch/mips/n64/init.c +5 -10
index a64695454359..995556e4afd9 100644
@@ -51,15 +51,13 @@ void __init prom_init(void)
 
 #define W 320
 #define H 240
-#define REG_BASE ((u32 __iomem *) CKSEG1ADDR(0x4400000))
+#define VI_REG_BASE ((u32 __iomem *)CKSEG1ADDR(0x4400000))
 
-static void __init n64rdp_write_reg(const u8 reg, const u32 value)
+static void __init n64_vi_write_reg(const u8 reg, const u32 value)
 {
-	__raw_writel(value, REG_BASE + reg);
+	__raw_writel(value, VI_REG_BASE + reg);
 }
 
-#undef REG_BASE
-
 static const u32 ntsc_320[] __initconst = {
 	0x00013212, 0x00000000, 0x00000140, 0x00000200,
 	0x00000000, 0x03e52239, 0x0000020d, 0x00000c15,
@@ -107,9 +105,9 @@ static int __init n64_platform_init(void)
 
 	for (i = 0; i < ARRAY_SIZE(ntsc_320); i++) {
 		if (i == 1)
-			n64rdp_write_reg(i, phys);
+			n64_vi_write_reg(i, phys);
 		else
-			n64rdp_write_reg(i, ntsc_320[i]);
+			n64_vi_write_reg(i, ntsc_320[i]);
 	}
 
 	res[0] = DEFINE_RES_MEM_NAMED(phys, W * H * 2, "FB");
@@ -119,9 +117,6 @@ static int __init n64_platform_init(void)
 	return 0;
 }
 
-#undef W
-#undef H
-
 arch_initcall(n64_platform_init);
 
 void __init plat_mem_setup(void)
#4 lore raw

PATCH 4/4 mips: n64: Use SZ_8M in plat_mem_setup()

In reply to Thorsten Blum

Replace the hard-coded value with SZ_8M and improve the comments in plat_mem_setup() and plat_time_init().

---
 arch/mips/n64/init.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
arch/mips/n64/init.c +5 -2
index 995556e4afd9..522b6405b644 100644
@@ -11,6 +11,7 @@
 #include <linux/memblock.h>
 #include <linux/platform_device.h>
 #include <linux/platform_data/simplefb.h>
+#include <linux/sizes.h>
 #include <linux/slab.h>
 
 #include <asm/bootinfo.h>
@@ -122,11 +123,13 @@ arch_initcall(n64_platform_init);
 void __init plat_mem_setup(void)
 {
 	iomem_resource_init();
-	memblock_add(0x0, 8 * 1024 * 1024); /* Bootloader blocks the 4mb config */
+
+	/* The bootloader requires an Expansion Pak (8 MiB of RAM in total). */
+	memblock_add(0x0, SZ_8M);
 }
 
 void __init plat_time_init(void)
 {
-	/* 93.75 MHz cpu, count register runs at half rate */
+	/* The count register runs at half the 93.75 MHz CPU clock rate. */
 	mips_hpt_frequency = 93750000 / 2;
 }