2 * Copyright (C) 2016 Imagination Technologies
3 * Author: Paul Burton <paul.burton@imgtec.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
11 #define pr_fmt(fmt) "sead3: " fmt
13 #include <linux/errno.h>
14 #include <linux/libfdt.h>
15 #include <linux/printk.h>
17 #include <asm/fw/fw.h>
19 #include <asm/machine.h>
21 #define SEAD_CONFIG CKSEG1ADDR(0x1b100110)
22 #define SEAD_CONFIG_GIC_PRESENT BIT(1)
24 #define MIPS_REVISION CKSEG1ADDR(0x1fc00010)
25 #define MIPS_REVISION_MACHINE (0xf << 4)
26 #define MIPS_REVISION_MACHINE_SEAD3 (0x4 << 4)
28 static __init
bool sead3_detect(void)
32 rev
= __raw_readl((void *)MIPS_REVISION
);
33 return (rev
& MIPS_REVISION_MACHINE
) == MIPS_REVISION_MACHINE_SEAD3
;
36 static __init
int append_cmdline(void *fdt
)
40 /* find or add chosen node */
41 chosen_off
= fdt_path_offset(fdt
, "/chosen");
42 if (chosen_off
== -FDT_ERR_NOTFOUND
)
43 chosen_off
= fdt_path_offset(fdt
, "/chosen@0");
44 if (chosen_off
== -FDT_ERR_NOTFOUND
)
45 chosen_off
= fdt_add_subnode(fdt
, 0, "chosen");
47 pr_err("Unable to find or add DT chosen node: %d\n",
52 err
= fdt_setprop_string(fdt
, chosen_off
, "bootargs", fw_getcmdline());
54 pr_err("Unable to set bootargs property: %d\n", err
);
61 static __init
int append_memory(void *fdt
)
63 unsigned long phys_memsize
, memsize
;
68 /* find memory size from the bootloader environment */
69 var
= fw_getenv("memsize");
71 err
= kstrtoul(var
, 0, &phys_memsize
);
73 pr_err("Failed to read memsize env variable '%s'\n",
78 pr_warn("The bootloader didn't provide memsize: defaulting to 32MB\n");
79 phys_memsize
= 32 << 20;
82 /* default to using all available RAM */
83 memsize
= phys_memsize
;
85 /* allow the user to override the usable memory */
86 var
= strstr(arcs_cmdline
, "memsize=");
88 memsize
= memparse(var
+ strlen("memsize="), NULL
);
90 /* if the user says there's more RAM than we thought, believe them */
91 phys_memsize
= max_t(unsigned long, phys_memsize
, memsize
);
93 /* find or add a memory node */
94 mem_off
= fdt_path_offset(fdt
, "/memory");
95 if (mem_off
== -FDT_ERR_NOTFOUND
)
96 mem_off
= fdt_add_subnode(fdt
, 0, "memory");
98 pr_err("Unable to find or add memory DT node: %d\n", mem_off
);
102 err
= fdt_setprop_string(fdt
, mem_off
, "device_type", "memory");
104 pr_err("Unable to set memory node device_type: %d\n", err
);
109 mem_array
[1] = cpu_to_be32(phys_memsize
);
110 err
= fdt_setprop(fdt
, mem_off
, "reg", mem_array
, sizeof(mem_array
));
112 pr_err("Unable to set memory regs property: %d\n", err
);
117 mem_array
[1] = cpu_to_be32(memsize
);
118 err
= fdt_setprop(fdt
, mem_off
, "linux,usable-memory",
119 mem_array
, sizeof(mem_array
));
121 pr_err("Unable to set linux,usable-memory property: %d\n", err
);
128 static __init
int remove_gic(void *fdt
)
130 const unsigned int cpu_ehci_int
= 2;
131 const unsigned int cpu_uart_int
= 4;
132 const unsigned int cpu_eth_int
= 6;
133 int gic_off
, cpu_off
, uart_off
, eth_off
, ehci_off
, err
;
134 uint32_t cfg
, cpu_phandle
;
136 /* leave the GIC node intact if a GIC is present */
137 cfg
= __raw_readl((uint32_t *)SEAD_CONFIG
);
138 if (cfg
& SEAD_CONFIG_GIC_PRESENT
)
141 gic_off
= fdt_node_offset_by_compatible(fdt
, -1, "mti,gic");
143 pr_err("unable to find DT GIC node: %d\n", gic_off
);
147 err
= fdt_nop_node(fdt
, gic_off
);
149 pr_err("unable to nop GIC node\n");
153 cpu_off
= fdt_node_offset_by_compatible(fdt
, -1,
154 "mti,cpu-interrupt-controller");
156 pr_err("unable to find CPU intc node: %d\n", cpu_off
);
160 cpu_phandle
= fdt_get_phandle(fdt
, cpu_off
);
162 pr_err("unable to get CPU intc phandle\n");
166 err
= fdt_setprop_u32(fdt
, 0, "interrupt-parent", cpu_phandle
);
168 pr_err("unable to set root interrupt-parent: %d\n", err
);
172 uart_off
= fdt_node_offset_by_compatible(fdt
, -1, "ns16550a");
173 while (uart_off
>= 0) {
174 err
= fdt_setprop_u32(fdt
, uart_off
, "interrupts",
177 pr_err("unable to set UART interrupts property: %d\n",
182 uart_off
= fdt_node_offset_by_compatible(fdt
, uart_off
,
185 if (uart_off
!= -FDT_ERR_NOTFOUND
) {
186 pr_err("error searching for UART DT node: %d\n", uart_off
);
190 eth_off
= fdt_node_offset_by_compatible(fdt
, -1, "smsc,lan9115");
192 pr_err("unable to find ethernet DT node: %d\n", eth_off
);
196 err
= fdt_setprop_u32(fdt
, eth_off
, "interrupts", cpu_eth_int
);
198 pr_err("unable to set ethernet interrupts property: %d\n", err
);
202 ehci_off
= fdt_node_offset_by_compatible(fdt
, -1, "generic-ehci");
204 pr_err("unable to find EHCI DT node: %d\n", ehci_off
);
208 err
= fdt_setprop_u32(fdt
, ehci_off
, "interrupts", cpu_ehci_int
);
210 pr_err("unable to set EHCI interrupts property: %d\n", err
);
217 static __init
int serial_config(void *fdt
)
219 const char *yamontty
, *mode_var
;
220 char mode_var_name
[9], path
[18], parity
;
221 unsigned int uart
, baud
, stop_bits
;
225 yamontty
= fw_getenv("yamontty");
226 if (!yamontty
|| !strcmp(yamontty
, "tty0")) {
228 } else if (!strcmp(yamontty
, "tty1")) {
231 pr_warn("yamontty environment variable '%s' invalid\n",
236 baud
= stop_bits
= 0;
240 snprintf(mode_var_name
, sizeof(mode_var_name
), "modetty%u", uart
);
241 mode_var
= fw_getenv(mode_var_name
);
243 while (mode_var
[0] >= '0' && mode_var
[0] <= '9') {
245 baud
+= mode_var
[0] - '0';
248 if (mode_var
[0] == ',')
251 parity
= mode_var
[0];
252 if (mode_var
[0] == ',')
255 stop_bits
= mode_var
[0] - '0';
256 if (mode_var
[0] == ',')
258 if (!strcmp(mode_var
, "hw"))
265 if (parity
!= 'e' && parity
!= 'n' && parity
!= 'o')
268 if (stop_bits
!= 7 && stop_bits
!= 8)
271 WARN_ON(snprintf(path
, sizeof(path
), "uart%u:%u%c%u%s",
272 uart
, baud
, parity
, stop_bits
,
273 hw_flow
? "r" : "") >= sizeof(path
));
275 /* find or add chosen node */
276 chosen_off
= fdt_path_offset(fdt
, "/chosen");
277 if (chosen_off
== -FDT_ERR_NOTFOUND
)
278 chosen_off
= fdt_path_offset(fdt
, "/chosen@0");
279 if (chosen_off
== -FDT_ERR_NOTFOUND
)
280 chosen_off
= fdt_add_subnode(fdt
, 0, "chosen");
281 if (chosen_off
< 0) {
282 pr_err("Unable to find or add DT chosen node: %d\n",
287 err
= fdt_setprop_string(fdt
, chosen_off
, "stdout-path", path
);
289 pr_err("Unable to set stdout-path property: %d\n", err
);
296 static __init
const void *sead3_fixup_fdt(const void *fdt
,
297 const void *match_data
)
299 static unsigned char fdt_buf
[16 << 10] __initdata
;
302 if (fdt_check_header(fdt
))
305 /* if this isn't SEAD3, something went wrong */
306 BUG_ON(fdt_node_check_compatible(fdt
, 0, "mti,sead-3"));
310 err
= fdt_open_into(fdt
, fdt_buf
, sizeof(fdt_buf
));
312 panic("Unable to open FDT: %d", err
);
314 err
= append_cmdline(fdt_buf
);
316 panic("Unable to patch FDT: %d", err
);
318 err
= append_memory(fdt_buf
);
320 panic("Unable to patch FDT: %d", err
);
322 err
= remove_gic(fdt_buf
);
324 panic("Unable to patch FDT: %d", err
);
326 err
= serial_config(fdt_buf
);
328 panic("Unable to patch FDT: %d", err
);
330 err
= fdt_pack(fdt_buf
);
332 panic("Unable to pack FDT: %d\n", err
);
337 static __init
unsigned int sead3_measure_hpt_freq(void)
339 void __iomem
*status_reg
= (void __iomem
*)0xbf000410;
340 unsigned int freq
, orig
, tick
= 0;
343 local_irq_save(flags
);
345 orig
= readl(status_reg
) & 0x2; /* get original sample */
346 /* wait for transition */
347 while ((readl(status_reg
) & 0x2) == orig
)
349 orig
= orig
^ 0x2; /* flip the bit */
353 /* wait 1 second (the sampling clock transitions every 10ms) */
355 /* wait for transition */
356 while ((readl(status_reg
) & 0x2) == orig
)
358 orig
= orig
^ 0x2; /* flip the bit */
362 freq
= read_c0_count();
364 local_irq_restore(flags
);
369 extern char __dtb_sead3_begin
[];
371 MIPS_MACHINE(sead3
) = {
372 .fdt
= __dtb_sead3_begin
,
373 .detect
= sead3_detect
,
374 .fixup_fdt
= sead3_fixup_fdt
,
375 .measure_hpt_freq
= sead3_measure_hpt_freq
,