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_uart_int = 4;
28 int gic_off, cpu_off, uart_off, err;
29 uint32_t cfg, cpu_phandle;
31 /* leave the GIC node intact if a GIC is present */
32 cfg = __raw_readl((uint32_t *)SEAD_CONFIG);
33 if (cfg & SEAD_CONFIG_GIC_PRESENT)
36 gic_off = fdt_node_offset_by_compatible(fdt, -1, "mti,gic");
38 pr_err("unable to find DT GIC node: %d\n", gic_off);
42 err = fdt_nop_node(fdt, gic_off);
44 pr_err("unable to nop GIC node\n");
48 cpu_off = fdt_node_offset_by_compatible(fdt, -1,
49 "mti,cpu-interrupt-controller");
51 pr_err("unable to find CPU intc node: %d\n", cpu_off);
55 cpu_phandle = fdt_get_phandle(fdt, cpu_off);
57 pr_err("unable to get CPU intc phandle\n");
61 err = fdt_setprop_u32(fdt, 0, "interrupt-parent", cpu_phandle);
63 pr_err("unable to set root interrupt-parent: %d\n", err);
67 uart_off = fdt_node_offset_by_compatible(fdt, -1, "ns16550a");
68 while (uart_off >= 0) {
69 err = fdt_setprop_u32(fdt, uart_off, "interrupts",
72 pr_err("unable to set UART interrupts property: %d\n",
77 uart_off = fdt_node_offset_by_compatible(fdt, uart_off,
80 if (uart_off != -FDT_ERR_NOTFOUND) {
81 pr_err("error searching for UART DT node: %d\n", uart_off);
88 static int serial_config(void *fdt)
90 const char *yamontty, *mode_var;
91 char mode_var_name[9], path[18], parity;
92 unsigned int uart, baud, stop_bits;
96 yamontty = fw_getenv("yamontty");
97 if (!yamontty || !strcmp(yamontty, "tty0")) {
99 } else if (!strcmp(yamontty, "tty1")) {
102 pr_warn("yamontty environment variable '%s' invalid\n",
107 baud = stop_bits = 0;
111 snprintf(mode_var_name, sizeof(mode_var_name), "modetty%u", uart);
112 mode_var = fw_getenv(mode_var_name);
114 while (mode_var[0] >= '0' && mode_var[0] <= '9') {
116 baud += mode_var[0] - '0';
119 if (mode_var[0] == ',')
122 parity = mode_var[0];
123 if (mode_var[0] == ',')
126 stop_bits = mode_var[0] - '0';
127 if (mode_var[0] == ',')
129 if (!strcmp(mode_var, "hw"))
136 if (parity != 'e' && parity != 'n' && parity != 'o')
139 if (stop_bits != 7 && stop_bits != 8)
142 WARN_ON(snprintf(path, sizeof(path), "uart%u:%u%c%u%s",
143 uart, baud, parity, stop_bits,
144 hw_flow ? "r" : "") >= sizeof(path));
146 /* find or add chosen node */
147 chosen_off = fdt_path_offset(fdt, "/chosen");
148 if (chosen_off == -FDT_ERR_NOTFOUND)
149 chosen_off = fdt_path_offset(fdt, "/chosen@0");
150 if (chosen_off == -FDT_ERR_NOTFOUND)
151 chosen_off = fdt_add_subnode(fdt, 0, "chosen");
152 if (chosen_off < 0) {
153 pr_err("Unable to find or add DT chosen node: %d\n",
158 err = fdt_setprop_string(fdt, chosen_off, "stdout-path", path);
160 pr_err("Unable to set stdout-path property: %d\n", err);
167 void __init *sead3_dt_shim(void *fdt)
171 if (fdt_check_header(fdt))
174 /* if this isn't SEAD3, leave the DT alone */
175 if (fdt_node_check_compatible(fdt, 0, "mti,sead-3"))
178 err = fdt_open_into(fdt, fdt_buf, sizeof(fdt_buf));
180 panic("Unable to open FDT: %d", err);
182 err = remove_gic(fdt_buf);
184 panic("Unable to patch FDT: %d", err);
186 err = serial_config(fdt_buf);
188 panic("Unable to patch FDT: %d", err);
190 err = fdt_pack(fdt_buf);
192 panic("Unable to pack FDT: %d\n", err);