blob: b462f65b8ce07b9117b4be23fccdebf568ff33ba [file] [log] [blame]
Paul Burtonb6d5e472016-08-26 15:17:34 +01001/*
2 * Copyright (C) 2016 Imagination Technologies
3 * Author: Paul Burton <paul.burton@imgtec.com>
4 *
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.
9 */
10
11#define pr_fmt(fmt) "sead3-dtshim: " fmt
12
13#include <linux/errno.h>
14#include <linux/libfdt.h>
15#include <linux/printk.h>
16
Paul Burtonc11e3b42016-08-26 15:17:35 +010017#include <asm/fw/fw.h>
Paul Burtonb6d5e472016-08-26 15:17:34 +010018#include <asm/io.h>
19
20#define SEAD_CONFIG CKSEG1ADDR(0x1b100110)
21#define SEAD_CONFIG_GIC_PRESENT BIT(1)
22
23static unsigned char fdt_buf[16 << 10] __initdata;
24
25static int remove_gic(void *fdt)
26{
Paul Burtonc11e3b42016-08-26 15:17:35 +010027 const unsigned int cpu_uart_int = 4;
28 int gic_off, cpu_off, uart_off, err;
Paul Burtonb6d5e472016-08-26 15:17:34 +010029 uint32_t cfg, cpu_phandle;
30
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)
34 return 0;
35
36 gic_off = fdt_node_offset_by_compatible(fdt, -1, "mti,gic");
37 if (gic_off < 0) {
38 pr_err("unable to find DT GIC node: %d\n", gic_off);
39 return gic_off;
40 }
41
42 err = fdt_nop_node(fdt, gic_off);
43 if (err) {
44 pr_err("unable to nop GIC node\n");
45 return err;
46 }
47
48 cpu_off = fdt_node_offset_by_compatible(fdt, -1,
49 "mti,cpu-interrupt-controller");
50 if (cpu_off < 0) {
51 pr_err("unable to find CPU intc node: %d\n", cpu_off);
52 return cpu_off;
53 }
54
55 cpu_phandle = fdt_get_phandle(fdt, cpu_off);
56 if (!cpu_phandle) {
57 pr_err("unable to get CPU intc phandle\n");
58 return -EINVAL;
59 }
60
61 err = fdt_setprop_u32(fdt, 0, "interrupt-parent", cpu_phandle);
62 if (err) {
63 pr_err("unable to set root interrupt-parent: %d\n", err);
64 return err;
65 }
66
Paul Burtonc11e3b42016-08-26 15:17:35 +010067 uart_off = fdt_node_offset_by_compatible(fdt, -1, "ns16550a");
68 while (uart_off >= 0) {
69 err = fdt_setprop_u32(fdt, uart_off, "interrupts",
70 cpu_uart_int);
71 if (err) {
72 pr_err("unable to set UART interrupts property: %d\n",
73 err);
74 return err;
75 }
76
77 uart_off = fdt_node_offset_by_compatible(fdt, uart_off,
78 "ns16550a");
79 }
80 if (uart_off != -FDT_ERR_NOTFOUND) {
81 pr_err("error searching for UART DT node: %d\n", uart_off);
82 return uart_off;
83 }
84
85 return 0;
86}
87
88static int serial_config(void *fdt)
89{
90 const char *yamontty, *mode_var;
91 char mode_var_name[9], path[18], parity;
92 unsigned int uart, baud, stop_bits;
93 bool hw_flow;
94 int chosen_off, err;
95
96 yamontty = fw_getenv("yamontty");
97 if (!yamontty || !strcmp(yamontty, "tty0")) {
98 uart = 0;
99 } else if (!strcmp(yamontty, "tty1")) {
100 uart = 1;
101 } else {
102 pr_warn("yamontty environment variable '%s' invalid\n",
103 yamontty);
104 uart = 0;
105 }
106
107 baud = stop_bits = 0;
108 parity = 0;
109 hw_flow = false;
110
111 snprintf(mode_var_name, sizeof(mode_var_name), "modetty%u", uart);
112 mode_var = fw_getenv(mode_var_name);
113 if (mode_var) {
114 while (mode_var[0] >= '0' && mode_var[0] <= '9') {
115 baud *= 10;
116 baud += mode_var[0] - '0';
117 mode_var++;
118 }
119 if (mode_var[0] == ',')
120 mode_var++;
121 if (mode_var[0])
122 parity = mode_var[0];
123 if (mode_var[0] == ',')
124 mode_var++;
125 if (mode_var[0])
126 stop_bits = mode_var[0] - '0';
127 if (mode_var[0] == ',')
128 mode_var++;
129 if (!strcmp(mode_var, "hw"))
130 hw_flow = true;
131 }
132
133 if (!baud)
134 baud = 38400;
135
136 if (parity != 'e' && parity != 'n' && parity != 'o')
137 parity = 'n';
138
139 if (stop_bits != 7 && stop_bits != 8)
140 stop_bits = 8;
141
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));
145
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",
154 chosen_off);
155 return chosen_off;
156 }
157
158 err = fdt_setprop_string(fdt, chosen_off, "stdout-path", path);
159 if (err) {
160 pr_err("Unable to set stdout-path property: %d\n", err);
161 return err;
162 }
163
Paul Burtonb6d5e472016-08-26 15:17:34 +0100164 return 0;
165}
166
167void __init *sead3_dt_shim(void *fdt)
168{
169 int err;
170
171 if (fdt_check_header(fdt))
172 panic("Corrupt DT");
173
174 /* if this isn't SEAD3, leave the DT alone */
175 if (fdt_node_check_compatible(fdt, 0, "mti,sead-3"))
176 return fdt;
177
178 err = fdt_open_into(fdt, fdt_buf, sizeof(fdt_buf));
179 if (err)
180 panic("Unable to open FDT: %d", err);
181
182 err = remove_gic(fdt_buf);
183 if (err)
184 panic("Unable to patch FDT: %d", err);
185
Paul Burtonc11e3b42016-08-26 15:17:35 +0100186 err = serial_config(fdt_buf);
187 if (err)
188 panic("Unable to patch FDT: %d", err);
189
Paul Burtonb6d5e472016-08-26 15:17:34 +0100190 err = fdt_pack(fdt_buf);
191 if (err)
192 panic("Unable to pack FDT: %d\n", err);
193
194 return fdt_buf;
195}