1*baec970aSLauri Kasanen // SPDX-License-Identifier: GPL-2.0
2*baec970aSLauri Kasanen /*
3*baec970aSLauri Kasanen * Nintendo 64 init.
4*baec970aSLauri Kasanen *
5*baec970aSLauri Kasanen * Copyright (C) 2021 Lauri Kasanen
6*baec970aSLauri Kasanen */
7*baec970aSLauri Kasanen #include <linux/init.h>
8*baec970aSLauri Kasanen #include <linux/ioport.h>
9*baec970aSLauri Kasanen #include <linux/irq.h>
10*baec970aSLauri Kasanen #include <linux/memblock.h>
11*baec970aSLauri Kasanen #include <linux/platform_device.h>
12*baec970aSLauri Kasanen #include <linux/platform_data/simplefb.h>
13*baec970aSLauri Kasanen #include <linux/string.h>
14*baec970aSLauri Kasanen
15*baec970aSLauri Kasanen #include <asm/bootinfo.h>
16*baec970aSLauri Kasanen #include <asm/fw/fw.h>
17*baec970aSLauri Kasanen #include <asm/time.h>
18*baec970aSLauri Kasanen
19*baec970aSLauri Kasanen #define IO_MEM_RESOURCE_START 0UL
20*baec970aSLauri Kasanen #define IO_MEM_RESOURCE_END 0x1fffffffUL
21*baec970aSLauri Kasanen
22*baec970aSLauri Kasanen /*
23*baec970aSLauri Kasanen * System-specifc irq names for clarity
24*baec970aSLauri Kasanen */
25*baec970aSLauri Kasanen #define MIPS_CPU_IRQ(x) (MIPS_CPU_IRQ_BASE + (x))
26*baec970aSLauri Kasanen #define MIPS_SOFTINT0_IRQ MIPS_CPU_IRQ(0)
27*baec970aSLauri Kasanen #define MIPS_SOFTINT1_IRQ MIPS_CPU_IRQ(1)
28*baec970aSLauri Kasanen #define RCP_IRQ MIPS_CPU_IRQ(2)
29*baec970aSLauri Kasanen #define CART_IRQ MIPS_CPU_IRQ(3)
30*baec970aSLauri Kasanen #define PRENMI_IRQ MIPS_CPU_IRQ(4)
31*baec970aSLauri Kasanen #define RDBR_IRQ MIPS_CPU_IRQ(5)
32*baec970aSLauri Kasanen #define RDBW_IRQ MIPS_CPU_IRQ(6)
33*baec970aSLauri Kasanen #define TIMER_IRQ MIPS_CPU_IRQ(7)
34*baec970aSLauri Kasanen
iomem_resource_init(void)35*baec970aSLauri Kasanen static void __init iomem_resource_init(void)
36*baec970aSLauri Kasanen {
37*baec970aSLauri Kasanen iomem_resource.start = IO_MEM_RESOURCE_START;
38*baec970aSLauri Kasanen iomem_resource.end = IO_MEM_RESOURCE_END;
39*baec970aSLauri Kasanen }
40*baec970aSLauri Kasanen
get_system_type(void)41*baec970aSLauri Kasanen const char *get_system_type(void)
42*baec970aSLauri Kasanen {
43*baec970aSLauri Kasanen return "Nintendo 64";
44*baec970aSLauri Kasanen }
45*baec970aSLauri Kasanen
prom_init(void)46*baec970aSLauri Kasanen void __init prom_init(void)
47*baec970aSLauri Kasanen {
48*baec970aSLauri Kasanen fw_init_cmdline();
49*baec970aSLauri Kasanen }
50*baec970aSLauri Kasanen
51*baec970aSLauri Kasanen #define W 320
52*baec970aSLauri Kasanen #define H 240
53*baec970aSLauri Kasanen #define REG_BASE ((u32 *) CKSEG1ADDR(0x4400000))
54*baec970aSLauri Kasanen
n64rdp_write_reg(const u8 reg,const u32 value)55*baec970aSLauri Kasanen static void __init n64rdp_write_reg(const u8 reg, const u32 value)
56*baec970aSLauri Kasanen {
57*baec970aSLauri Kasanen __raw_writel(value, REG_BASE + reg);
58*baec970aSLauri Kasanen }
59*baec970aSLauri Kasanen
60*baec970aSLauri Kasanen #undef REG_BASE
61*baec970aSLauri Kasanen
62*baec970aSLauri Kasanen static const u32 ntsc_320[] __initconst = {
63*baec970aSLauri Kasanen 0x00013212, 0x00000000, 0x00000140, 0x00000200,
64*baec970aSLauri Kasanen 0x00000000, 0x03e52239, 0x0000020d, 0x00000c15,
65*baec970aSLauri Kasanen 0x0c150c15, 0x006c02ec, 0x002501ff, 0x000e0204,
66*baec970aSLauri Kasanen 0x00000200, 0x00000400
67*baec970aSLauri Kasanen };
68*baec970aSLauri Kasanen
69*baec970aSLauri Kasanen #define MI_REG_BASE 0x4300000
70*baec970aSLauri Kasanen #define NUM_MI_REGS 4
71*baec970aSLauri Kasanen #define AI_REG_BASE 0x4500000
72*baec970aSLauri Kasanen #define NUM_AI_REGS 6
73*baec970aSLauri Kasanen #define PI_REG_BASE 0x4600000
74*baec970aSLauri Kasanen #define NUM_PI_REGS 5
75*baec970aSLauri Kasanen #define SI_REG_BASE 0x4800000
76*baec970aSLauri Kasanen #define NUM_SI_REGS 7
77*baec970aSLauri Kasanen
n64_platform_init(void)78*baec970aSLauri Kasanen static int __init n64_platform_init(void)
79*baec970aSLauri Kasanen {
80*baec970aSLauri Kasanen static const char simplefb_resname[] = "FB";
81*baec970aSLauri Kasanen static const struct simplefb_platform_data mode = {
82*baec970aSLauri Kasanen .width = W,
83*baec970aSLauri Kasanen .height = H,
84*baec970aSLauri Kasanen .stride = W * 2,
85*baec970aSLauri Kasanen .format = "r5g5b5a1"
86*baec970aSLauri Kasanen };
87*baec970aSLauri Kasanen struct resource res[3];
88*baec970aSLauri Kasanen void *orig;
89*baec970aSLauri Kasanen unsigned long phys;
90*baec970aSLauri Kasanen unsigned i;
91*baec970aSLauri Kasanen
92*baec970aSLauri Kasanen memset(res, 0, sizeof(struct resource) * 3);
93*baec970aSLauri Kasanen res[0].flags = IORESOURCE_MEM;
94*baec970aSLauri Kasanen res[0].start = MI_REG_BASE;
95*baec970aSLauri Kasanen res[0].end = MI_REG_BASE + NUM_MI_REGS * 4 - 1;
96*baec970aSLauri Kasanen
97*baec970aSLauri Kasanen res[1].flags = IORESOURCE_MEM;
98*baec970aSLauri Kasanen res[1].start = AI_REG_BASE;
99*baec970aSLauri Kasanen res[1].end = AI_REG_BASE + NUM_AI_REGS * 4 - 1;
100*baec970aSLauri Kasanen
101*baec970aSLauri Kasanen res[2].flags = IORESOURCE_IRQ;
102*baec970aSLauri Kasanen res[2].start = RCP_IRQ;
103*baec970aSLauri Kasanen res[2].end = RCP_IRQ;
104*baec970aSLauri Kasanen
105*baec970aSLauri Kasanen platform_device_register_simple("n64audio", -1, res, 3);
106*baec970aSLauri Kasanen
107*baec970aSLauri Kasanen memset(&res[0], 0, sizeof(res[0]));
108*baec970aSLauri Kasanen res[0].flags = IORESOURCE_MEM;
109*baec970aSLauri Kasanen res[0].start = PI_REG_BASE;
110*baec970aSLauri Kasanen res[0].end = PI_REG_BASE + NUM_PI_REGS * 4 - 1;
111*baec970aSLauri Kasanen
112*baec970aSLauri Kasanen platform_device_register_simple("n64cart", -1, res, 1);
113*baec970aSLauri Kasanen
114*baec970aSLauri Kasanen memset(&res[0], 0, sizeof(res[0]));
115*baec970aSLauri Kasanen res[0].flags = IORESOURCE_MEM;
116*baec970aSLauri Kasanen res[0].start = SI_REG_BASE;
117*baec970aSLauri Kasanen res[0].end = SI_REG_BASE + NUM_SI_REGS * 4 - 1;
118*baec970aSLauri Kasanen
119*baec970aSLauri Kasanen platform_device_register_simple("n64joy", -1, res, 1);
120*baec970aSLauri Kasanen
121*baec970aSLauri Kasanen /* The framebuffer needs 64-byte alignment */
122*baec970aSLauri Kasanen orig = kzalloc(W * H * 2 + 63, GFP_DMA | GFP_KERNEL);
123*baec970aSLauri Kasanen if (!orig)
124*baec970aSLauri Kasanen return -ENOMEM;
125*baec970aSLauri Kasanen phys = virt_to_phys(orig);
126*baec970aSLauri Kasanen phys += 63;
127*baec970aSLauri Kasanen phys &= ~63;
128*baec970aSLauri Kasanen
129*baec970aSLauri Kasanen for (i = 0; i < ARRAY_SIZE(ntsc_320); i++) {
130*baec970aSLauri Kasanen if (i == 1)
131*baec970aSLauri Kasanen n64rdp_write_reg(i, phys);
132*baec970aSLauri Kasanen else
133*baec970aSLauri Kasanen n64rdp_write_reg(i, ntsc_320[i]);
134*baec970aSLauri Kasanen }
135*baec970aSLauri Kasanen
136*baec970aSLauri Kasanen /* setup IORESOURCE_MEM as framebuffer memory */
137*baec970aSLauri Kasanen memset(&res[0], 0, sizeof(res[0]));
138*baec970aSLauri Kasanen res[0].flags = IORESOURCE_MEM;
139*baec970aSLauri Kasanen res[0].name = simplefb_resname;
140*baec970aSLauri Kasanen res[0].start = phys;
141*baec970aSLauri Kasanen res[0].end = phys + W * H * 2 - 1;
142*baec970aSLauri Kasanen
143*baec970aSLauri Kasanen platform_device_register_resndata(NULL, "simple-framebuffer", 0,
144*baec970aSLauri Kasanen &res[0], 1, &mode, sizeof(mode));
145*baec970aSLauri Kasanen
146*baec970aSLauri Kasanen return 0;
147*baec970aSLauri Kasanen }
148*baec970aSLauri Kasanen
149*baec970aSLauri Kasanen #undef W
150*baec970aSLauri Kasanen #undef H
151*baec970aSLauri Kasanen
152*baec970aSLauri Kasanen arch_initcall(n64_platform_init);
153*baec970aSLauri Kasanen
plat_mem_setup(void)154*baec970aSLauri Kasanen void __init plat_mem_setup(void)
155*baec970aSLauri Kasanen {
156*baec970aSLauri Kasanen iomem_resource_init();
157*baec970aSLauri Kasanen memblock_add(0x0, 8 * 1024 * 1024); /* Bootloader blocks the 4mb config */
158*baec970aSLauri Kasanen }
159*baec970aSLauri Kasanen
plat_time_init(void)160*baec970aSLauri Kasanen void __init plat_time_init(void)
161*baec970aSLauri Kasanen {
162*baec970aSLauri Kasanen /* 93.75 MHz cpu, count register runs at half rate */
163*baec970aSLauri Kasanen mips_hpt_frequency = 93750000 / 2;
164*baec970aSLauri Kasanen }
165