1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (C) 2016 Imagination Technologies
4*4882a593Smuzhiyun * Author: Paul Burton <paul.burton@mips.com>
5*4882a593Smuzhiyun */
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun #define pr_fmt(fmt) "sead3: " fmt
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun #include <linux/errno.h>
10*4882a593Smuzhiyun #include <linux/libfdt.h>
11*4882a593Smuzhiyun #include <linux/printk.h>
12*4882a593Smuzhiyun #include <linux/sizes.h>
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun #include <asm/fw/fw.h>
15*4882a593Smuzhiyun #include <asm/io.h>
16*4882a593Smuzhiyun #include <asm/machine.h>
17*4882a593Smuzhiyun #include <asm/yamon-dt.h>
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun #define SEAD_CONFIG CKSEG1ADDR(0x1b100110)
20*4882a593Smuzhiyun #define SEAD_CONFIG_GIC_PRESENT BIT(1)
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun #define MIPS_REVISION CKSEG1ADDR(0x1fc00010)
23*4882a593Smuzhiyun #define MIPS_REVISION_MACHINE (0xf << 4)
24*4882a593Smuzhiyun #define MIPS_REVISION_MACHINE_SEAD3 (0x4 << 4)
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun /*
27*4882a593Smuzhiyun * Maximum 384MB RAM at physical address 0, preceding any I/O.
28*4882a593Smuzhiyun */
29*4882a593Smuzhiyun static struct yamon_mem_region mem_regions[] __initdata = {
30*4882a593Smuzhiyun /* start size */
31*4882a593Smuzhiyun { 0, SZ_256M + SZ_128M },
32*4882a593Smuzhiyun {}
33*4882a593Smuzhiyun };
34*4882a593Smuzhiyun
sead3_detect(void)35*4882a593Smuzhiyun static __init bool sead3_detect(void)
36*4882a593Smuzhiyun {
37*4882a593Smuzhiyun uint32_t rev;
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun rev = __raw_readl((void *)MIPS_REVISION);
40*4882a593Smuzhiyun return (rev & MIPS_REVISION_MACHINE) == MIPS_REVISION_MACHINE_SEAD3;
41*4882a593Smuzhiyun }
42*4882a593Smuzhiyun
append_memory(void * fdt)43*4882a593Smuzhiyun static __init int append_memory(void *fdt)
44*4882a593Smuzhiyun {
45*4882a593Smuzhiyun return yamon_dt_append_memory(fdt, mem_regions);
46*4882a593Smuzhiyun }
47*4882a593Smuzhiyun
remove_gic(void * fdt)48*4882a593Smuzhiyun static __init int remove_gic(void *fdt)
49*4882a593Smuzhiyun {
50*4882a593Smuzhiyun const unsigned int cpu_ehci_int = 2;
51*4882a593Smuzhiyun const unsigned int cpu_uart_int = 4;
52*4882a593Smuzhiyun const unsigned int cpu_eth_int = 6;
53*4882a593Smuzhiyun int gic_off, cpu_off, uart_off, eth_off, ehci_off, err;
54*4882a593Smuzhiyun uint32_t cfg, cpu_phandle;
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun /* leave the GIC node intact if a GIC is present */
57*4882a593Smuzhiyun cfg = __raw_readl((uint32_t *)SEAD_CONFIG);
58*4882a593Smuzhiyun if (cfg & SEAD_CONFIG_GIC_PRESENT)
59*4882a593Smuzhiyun return 0;
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun gic_off = fdt_node_offset_by_compatible(fdt, -1, "mti,gic");
62*4882a593Smuzhiyun if (gic_off < 0) {
63*4882a593Smuzhiyun pr_err("unable to find DT GIC node: %d\n", gic_off);
64*4882a593Smuzhiyun return gic_off;
65*4882a593Smuzhiyun }
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun err = fdt_nop_node(fdt, gic_off);
68*4882a593Smuzhiyun if (err) {
69*4882a593Smuzhiyun pr_err("unable to nop GIC node\n");
70*4882a593Smuzhiyun return err;
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun cpu_off = fdt_node_offset_by_compatible(fdt, -1,
74*4882a593Smuzhiyun "mti,cpu-interrupt-controller");
75*4882a593Smuzhiyun if (cpu_off < 0) {
76*4882a593Smuzhiyun pr_err("unable to find CPU intc node: %d\n", cpu_off);
77*4882a593Smuzhiyun return cpu_off;
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun cpu_phandle = fdt_get_phandle(fdt, cpu_off);
81*4882a593Smuzhiyun if (!cpu_phandle) {
82*4882a593Smuzhiyun pr_err("unable to get CPU intc phandle\n");
83*4882a593Smuzhiyun return -EINVAL;
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun uart_off = fdt_node_offset_by_compatible(fdt, -1, "ns16550a");
87*4882a593Smuzhiyun while (uart_off >= 0) {
88*4882a593Smuzhiyun err = fdt_setprop_u32(fdt, uart_off, "interrupt-parent",
89*4882a593Smuzhiyun cpu_phandle);
90*4882a593Smuzhiyun if (err) {
91*4882a593Smuzhiyun pr_warn("unable to set UART interrupt-parent: %d\n",
92*4882a593Smuzhiyun err);
93*4882a593Smuzhiyun return err;
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun err = fdt_setprop_u32(fdt, uart_off, "interrupts",
97*4882a593Smuzhiyun cpu_uart_int);
98*4882a593Smuzhiyun if (err) {
99*4882a593Smuzhiyun pr_err("unable to set UART interrupts property: %d\n",
100*4882a593Smuzhiyun err);
101*4882a593Smuzhiyun return err;
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun uart_off = fdt_node_offset_by_compatible(fdt, uart_off,
105*4882a593Smuzhiyun "ns16550a");
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun if (uart_off != -FDT_ERR_NOTFOUND) {
108*4882a593Smuzhiyun pr_err("error searching for UART DT node: %d\n", uart_off);
109*4882a593Smuzhiyun return uart_off;
110*4882a593Smuzhiyun }
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun eth_off = fdt_node_offset_by_compatible(fdt, -1, "smsc,lan9115");
113*4882a593Smuzhiyun if (eth_off < 0) {
114*4882a593Smuzhiyun pr_err("unable to find ethernet DT node: %d\n", eth_off);
115*4882a593Smuzhiyun return eth_off;
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun err = fdt_setprop_u32(fdt, eth_off, "interrupt-parent", cpu_phandle);
119*4882a593Smuzhiyun if (err) {
120*4882a593Smuzhiyun pr_err("unable to set ethernet interrupt-parent: %d\n", err);
121*4882a593Smuzhiyun return err;
122*4882a593Smuzhiyun }
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun err = fdt_setprop_u32(fdt, eth_off, "interrupts", cpu_eth_int);
125*4882a593Smuzhiyun if (err) {
126*4882a593Smuzhiyun pr_err("unable to set ethernet interrupts property: %d\n", err);
127*4882a593Smuzhiyun return err;
128*4882a593Smuzhiyun }
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun ehci_off = fdt_node_offset_by_compatible(fdt, -1, "generic-ehci");
131*4882a593Smuzhiyun if (ehci_off < 0) {
132*4882a593Smuzhiyun pr_err("unable to find EHCI DT node: %d\n", ehci_off);
133*4882a593Smuzhiyun return ehci_off;
134*4882a593Smuzhiyun }
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun err = fdt_setprop_u32(fdt, ehci_off, "interrupt-parent", cpu_phandle);
137*4882a593Smuzhiyun if (err) {
138*4882a593Smuzhiyun pr_err("unable to set EHCI interrupt-parent: %d\n", err);
139*4882a593Smuzhiyun return err;
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun err = fdt_setprop_u32(fdt, ehci_off, "interrupts", cpu_ehci_int);
143*4882a593Smuzhiyun if (err) {
144*4882a593Smuzhiyun pr_err("unable to set EHCI interrupts property: %d\n", err);
145*4882a593Smuzhiyun return err;
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun return 0;
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun static const struct mips_fdt_fixup sead3_fdt_fixups[] __initconst = {
152*4882a593Smuzhiyun { yamon_dt_append_cmdline, "append command line" },
153*4882a593Smuzhiyun { append_memory, "append memory" },
154*4882a593Smuzhiyun { remove_gic, "remove GIC when not present" },
155*4882a593Smuzhiyun { yamon_dt_serial_config, "append serial configuration" },
156*4882a593Smuzhiyun { },
157*4882a593Smuzhiyun };
158*4882a593Smuzhiyun
sead3_fixup_fdt(const void * fdt,const void * match_data)159*4882a593Smuzhiyun static __init const void *sead3_fixup_fdt(const void *fdt,
160*4882a593Smuzhiyun const void *match_data)
161*4882a593Smuzhiyun {
162*4882a593Smuzhiyun static unsigned char fdt_buf[16 << 10] __initdata;
163*4882a593Smuzhiyun int err;
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun if (fdt_check_header(fdt))
166*4882a593Smuzhiyun panic("Corrupt DT");
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun /* if this isn't SEAD3, something went wrong */
169*4882a593Smuzhiyun BUG_ON(fdt_node_check_compatible(fdt, 0, "mti,sead-3"));
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun fw_init_cmdline();
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun err = apply_mips_fdt_fixups(fdt_buf, sizeof(fdt_buf),
174*4882a593Smuzhiyun fdt, sead3_fdt_fixups);
175*4882a593Smuzhiyun if (err)
176*4882a593Smuzhiyun panic("Unable to fixup FDT: %d", err);
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun return fdt_buf;
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun
sead3_measure_hpt_freq(void)181*4882a593Smuzhiyun static __init unsigned int sead3_measure_hpt_freq(void)
182*4882a593Smuzhiyun {
183*4882a593Smuzhiyun void __iomem *status_reg = (void __iomem *)0xbf000410;
184*4882a593Smuzhiyun unsigned int freq, orig, tick = 0;
185*4882a593Smuzhiyun unsigned long flags;
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun local_irq_save(flags);
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun orig = readl(status_reg) & 0x2; /* get original sample */
190*4882a593Smuzhiyun /* wait for transition */
191*4882a593Smuzhiyun while ((readl(status_reg) & 0x2) == orig)
192*4882a593Smuzhiyun ;
193*4882a593Smuzhiyun orig = orig ^ 0x2; /* flip the bit */
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun write_c0_count(0);
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun /* wait 1 second (the sampling clock transitions every 10ms) */
198*4882a593Smuzhiyun while (tick < 100) {
199*4882a593Smuzhiyun /* wait for transition */
200*4882a593Smuzhiyun while ((readl(status_reg) & 0x2) == orig)
201*4882a593Smuzhiyun ;
202*4882a593Smuzhiyun orig = orig ^ 0x2; /* flip the bit */
203*4882a593Smuzhiyun tick++;
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun freq = read_c0_count();
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun local_irq_restore(flags);
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun return freq;
211*4882a593Smuzhiyun }
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun extern char __dtb_sead3_begin[];
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun MIPS_MACHINE(sead3) = {
216*4882a593Smuzhiyun .fdt = __dtb_sead3_begin,
217*4882a593Smuzhiyun .detect = sead3_detect,
218*4882a593Smuzhiyun .fixup_fdt = sead3_fixup_fdt,
219*4882a593Smuzhiyun .measure_hpt_freq = sead3_measure_hpt_freq,
220*4882a593Smuzhiyun };
221