]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - arch/mips/mti-sead3/sead3-dtshim.c
MIPS: SEAD3: Probe ethernet controller using DT
[mirror_ubuntu-eoan-kernel.git] / arch / mips / mti-sead3 / sead3-dtshim.c
CommitLineData
b6d5e47e
PB
1/*
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
c11e3b48 17#include <asm/fw/fw.h>
b6d5e47e
PB
18#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{
c11e3b48 27 const unsigned int cpu_uart_int = 4;
a34e9388
PB
28 const unsigned int cpu_eth_int = 6;
29 int gic_off, cpu_off, uart_off, eth_off, err;
b6d5e47e
PB
30 uint32_t cfg, cpu_phandle;
31
32 /* leave the GIC node intact if a GIC is present */
33 cfg = __raw_readl((uint32_t *)SEAD_CONFIG);
34 if (cfg & SEAD_CONFIG_GIC_PRESENT)
35 return 0;
36
37 gic_off = fdt_node_offset_by_compatible(fdt, -1, "mti,gic");
38 if (gic_off < 0) {
39 pr_err("unable to find DT GIC node: %d\n", gic_off);
40 return gic_off;
41 }
42
43 err = fdt_nop_node(fdt, gic_off);
44 if (err) {
45 pr_err("unable to nop GIC node\n");
46 return err;
47 }
48
49 cpu_off = fdt_node_offset_by_compatible(fdt, -1,
50 "mti,cpu-interrupt-controller");
51 if (cpu_off < 0) {
52 pr_err("unable to find CPU intc node: %d\n", cpu_off);
53 return cpu_off;
54 }
55
56 cpu_phandle = fdt_get_phandle(fdt, cpu_off);
57 if (!cpu_phandle) {
58 pr_err("unable to get CPU intc phandle\n");
59 return -EINVAL;
60 }
61
62 err = fdt_setprop_u32(fdt, 0, "interrupt-parent", cpu_phandle);
63 if (err) {
64 pr_err("unable to set root interrupt-parent: %d\n", err);
65 return err;
66 }
67
c11e3b48
PB
68 uart_off = fdt_node_offset_by_compatible(fdt, -1, "ns16550a");
69 while (uart_off >= 0) {
70 err = fdt_setprop_u32(fdt, uart_off, "interrupts",
71 cpu_uart_int);
72 if (err) {
73 pr_err("unable to set UART interrupts property: %d\n",
74 err);
75 return err;
76 }
77
78 uart_off = fdt_node_offset_by_compatible(fdt, uart_off,
79 "ns16550a");
80 }
81 if (uart_off != -FDT_ERR_NOTFOUND) {
82 pr_err("error searching for UART DT node: %d\n", uart_off);
83 return uart_off;
84 }
85
a34e9388
PB
86 eth_off = fdt_node_offset_by_compatible(fdt, -1, "smsc,lan9115");
87 if (eth_off < 0) {
88 pr_err("unable to find ethernet DT node: %d\n", eth_off);
89 return eth_off;
90 }
91
92 err = fdt_setprop_u32(fdt, eth_off, "interrupts", cpu_eth_int);
93 if (err) {
94 pr_err("unable to set ethernet interrupts property: %d\n", err);
95 return err;
96 }
97
c11e3b48
PB
98 return 0;
99}
100
101static int serial_config(void *fdt)
102{
103 const char *yamontty, *mode_var;
104 char mode_var_name[9], path[18], parity;
105 unsigned int uart, baud, stop_bits;
106 bool hw_flow;
107 int chosen_off, err;
108
109 yamontty = fw_getenv("yamontty");
110 if (!yamontty || !strcmp(yamontty, "tty0")) {
111 uart = 0;
112 } else if (!strcmp(yamontty, "tty1")) {
113 uart = 1;
114 } else {
115 pr_warn("yamontty environment variable '%s' invalid\n",
116 yamontty);
117 uart = 0;
118 }
119
120 baud = stop_bits = 0;
121 parity = 0;
122 hw_flow = false;
123
124 snprintf(mode_var_name, sizeof(mode_var_name), "modetty%u", uart);
125 mode_var = fw_getenv(mode_var_name);
126 if (mode_var) {
127 while (mode_var[0] >= '0' && mode_var[0] <= '9') {
128 baud *= 10;
129 baud += mode_var[0] - '0';
130 mode_var++;
131 }
132 if (mode_var[0] == ',')
133 mode_var++;
134 if (mode_var[0])
135 parity = mode_var[0];
136 if (mode_var[0] == ',')
137 mode_var++;
138 if (mode_var[0])
139 stop_bits = mode_var[0] - '0';
140 if (mode_var[0] == ',')
141 mode_var++;
142 if (!strcmp(mode_var, "hw"))
143 hw_flow = true;
144 }
145
146 if (!baud)
147 baud = 38400;
148
149 if (parity != 'e' && parity != 'n' && parity != 'o')
150 parity = 'n';
151
152 if (stop_bits != 7 && stop_bits != 8)
153 stop_bits = 8;
154
155 WARN_ON(snprintf(path, sizeof(path), "uart%u:%u%c%u%s",
156 uart, baud, parity, stop_bits,
157 hw_flow ? "r" : "") >= sizeof(path));
158
159 /* find or add chosen node */
160 chosen_off = fdt_path_offset(fdt, "/chosen");
161 if (chosen_off == -FDT_ERR_NOTFOUND)
162 chosen_off = fdt_path_offset(fdt, "/chosen@0");
163 if (chosen_off == -FDT_ERR_NOTFOUND)
164 chosen_off = fdt_add_subnode(fdt, 0, "chosen");
165 if (chosen_off < 0) {
166 pr_err("Unable to find or add DT chosen node: %d\n",
167 chosen_off);
168 return chosen_off;
169 }
170
171 err = fdt_setprop_string(fdt, chosen_off, "stdout-path", path);
172 if (err) {
173 pr_err("Unable to set stdout-path property: %d\n", err);
174 return err;
175 }
176
b6d5e47e
PB
177 return 0;
178}
179
180void __init *sead3_dt_shim(void *fdt)
181{
182 int err;
183
184 if (fdt_check_header(fdt))
185 panic("Corrupt DT");
186
187 /* if this isn't SEAD3, leave the DT alone */
188 if (fdt_node_check_compatible(fdt, 0, "mti,sead-3"))
189 return fdt;
190
191 err = fdt_open_into(fdt, fdt_buf, sizeof(fdt_buf));
192 if (err)
193 panic("Unable to open FDT: %d", err);
194
195 err = remove_gic(fdt_buf);
196 if (err)
197 panic("Unable to patch FDT: %d", err);
198
c11e3b48
PB
199 err = serial_config(fdt_buf);
200 if (err)
201 panic("Unable to patch FDT: %d", err);
202
b6d5e47e
PB
203 err = fdt_pack(fdt_buf);
204 if (err)
205 panic("Unable to pack FDT: %d\n", err);
206
207 return fdt_buf;
208}