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-dtshim: " fmt
13 #include <linux/errno.h>
14 #include <linux/libfdt.h>
15 #include <linux/printk.h>
17 #include <asm/fw/fw.h>
20 #define SEAD_CONFIG CKSEG1ADDR(0x1b100110)
21 #define SEAD_CONFIG_GIC_PRESENT BIT(1)
23 static unsigned char fdt_buf[16 << 10] __initdata;
25 static int remove_gic(void *fdt)
27 const unsigned int cpu_ehci_int = 2;
28 const unsigned int cpu_uart_int = 4;
29 const unsigned int cpu_eth_int = 6;
30 int gic_off, cpu_off, uart_off, eth_off, ehci_off, err;
31 uint32_t cfg, cpu_phandle;
33 /* leave the GIC node intact if a GIC is present */
34 cfg = __raw_readl((uint32_t *)SEAD_CONFIG);
35 if (cfg & SEAD_CONFIG_GIC_PRESENT)
38 gic_off = fdt_node_offset_by_compatible(fdt, -1, "mti,gic");
40 pr_err("unable to find DT GIC node: %d\n", gic_off);
44 err = fdt_nop_node(fdt, gic_off);
46 pr_err("unable to nop GIC node\n");
50 cpu_off = fdt_node_offset_by_compatible(fdt, -1,
51 "mti,cpu-interrupt-controller");
53 pr_err("unable to find CPU intc node: %d\n", cpu_off);
57 cpu_phandle = fdt_get_phandle(fdt, cpu_off);
59 pr_err("unable to get CPU intc phandle\n");
63 err = fdt_setprop_u32(fdt, 0, "interrupt-parent", cpu_phandle);
65 pr_err("unable to set root interrupt-parent: %d\n", err);
69 uart_off = fdt_node_offset_by_compatible(fdt, -1, "ns16550a");
70 while (uart_off >= 0) {
71 err = fdt_setprop_u32(fdt, uart_off, "interrupts",
74 pr_err("unable to set UART interrupts property: %d\n",
79 uart_off = fdt_node_offset_by_compatible(fdt, uart_off,
82 if (uart_off != -FDT_ERR_NOTFOUND) {
83 pr_err("error searching for UART DT node: %d\n", uart_off);
87 eth_off = fdt_node_offset_by_compatible(fdt, -1, "smsc,lan9115");
89 pr_err("unable to find ethernet DT node: %d\n", eth_off);
93 err = fdt_setprop_u32(fdt, eth_off, "interrupts", cpu_eth_int);
95 pr_err("unable to set ethernet interrupts property: %d\n", err);
99 ehci_off = fdt_node_offset_by_compatible(fdt, -1, "mti,sead3-ehci");
101 pr_err("unable to find EHCI DT node: %d\n", ehci_off);
105 err = fdt_setprop_u32(fdt, ehci_off, "interrupts", cpu_ehci_int);
107 pr_err("unable to set EHCI interrupts property: %d\n", err);
114 static int serial_config(void *fdt)
116 const char *yamontty, *mode_var;
117 char mode_var_name[9], path[18], parity;
118 unsigned int uart, baud, stop_bits;
122 yamontty = fw_getenv("yamontty");
123 if (!yamontty || !strcmp(yamontty, "tty0")) {
125 } else if (!strcmp(yamontty, "tty1")) {
128 pr_warn("yamontty environment variable '%s' invalid\n",
133 baud = stop_bits = 0;
137 snprintf(mode_var_name, sizeof(mode_var_name), "modetty%u", uart);
138 mode_var = fw_getenv(mode_var_name);
140 while (mode_var[0] >= '0' && mode_var[0] <= '9') {
142 baud += mode_var[0] - '0';
145 if (mode_var[0] == ',')
148 parity = mode_var[0];
149 if (mode_var[0] == ',')
152 stop_bits = mode_var[0] - '0';
153 if (mode_var[0] == ',')
155 if (!strcmp(mode_var, "hw"))
162 if (parity != 'e' && parity != 'n' && parity != 'o')
165 if (stop_bits != 7 && stop_bits != 8)
168 WARN_ON(snprintf(path, sizeof(path), "uart%u:%u%c%u%s",
169 uart, baud, parity, stop_bits,
170 hw_flow ? "r" : "") >= sizeof(path));
172 /* find or add chosen node */
173 chosen_off = fdt_path_offset(fdt, "/chosen");
174 if (chosen_off == -FDT_ERR_NOTFOUND)
175 chosen_off = fdt_path_offset(fdt, "/chosen@0");
176 if (chosen_off == -FDT_ERR_NOTFOUND)
177 chosen_off = fdt_add_subnode(fdt, 0, "chosen");
178 if (chosen_off < 0) {
179 pr_err("Unable to find or add DT chosen node: %d\n",
184 err = fdt_setprop_string(fdt, chosen_off, "stdout-path", path);
186 pr_err("Unable to set stdout-path property: %d\n", err);
193 void __init *sead3_dt_shim(void *fdt)
197 if (fdt_check_header(fdt))
200 /* if this isn't SEAD3, leave the DT alone */
201 if (fdt_node_check_compatible(fdt, 0, "mti,sead-3"))
204 err = fdt_open_into(fdt, fdt_buf, sizeof(fdt_buf));
206 panic("Unable to open FDT: %d", err);
208 err = remove_gic(fdt_buf);
210 panic("Unable to patch FDT: %d", err);
212 err = serial_config(fdt_buf);
214 panic("Unable to patch FDT: %d", err);
216 err = fdt_pack(fdt_buf);
218 panic("Unable to pack FDT: %d\n", err);