]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/mips/mti-sead3/sead3-dtshim.c
MIPS: SEAD3: Probe EHCI controller using DT
[karo-tx-linux.git] / arch / mips / mti-sead3 / sead3-dtshim.c
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
17 #include <asm/fw/fw.h>
18 #include <asm/io.h>
19
20 #define SEAD_CONFIG                     CKSEG1ADDR(0x1b100110)
21 #define SEAD_CONFIG_GIC_PRESENT         BIT(1)
22
23 static unsigned char fdt_buf[16 << 10] __initdata;
24
25 static int remove_gic(void *fdt)
26 {
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;
32
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)
36                 return 0;
37
38         gic_off = fdt_node_offset_by_compatible(fdt, -1, "mti,gic");
39         if (gic_off < 0) {
40                 pr_err("unable to find DT GIC node: %d\n", gic_off);
41                 return gic_off;
42         }
43
44         err = fdt_nop_node(fdt, gic_off);
45         if (err) {
46                 pr_err("unable to nop GIC node\n");
47                 return err;
48         }
49
50         cpu_off = fdt_node_offset_by_compatible(fdt, -1,
51                         "mti,cpu-interrupt-controller");
52         if (cpu_off < 0) {
53                 pr_err("unable to find CPU intc node: %d\n", cpu_off);
54                 return cpu_off;
55         }
56
57         cpu_phandle = fdt_get_phandle(fdt, cpu_off);
58         if (!cpu_phandle) {
59                 pr_err("unable to get CPU intc phandle\n");
60                 return -EINVAL;
61         }
62
63         err = fdt_setprop_u32(fdt, 0, "interrupt-parent", cpu_phandle);
64         if (err) {
65                 pr_err("unable to set root interrupt-parent: %d\n", err);
66                 return err;
67         }
68
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",
72                                       cpu_uart_int);
73                 if (err) {
74                         pr_err("unable to set UART interrupts property: %d\n",
75                                err);
76                         return err;
77                 }
78
79                 uart_off = fdt_node_offset_by_compatible(fdt, uart_off,
80                                                          "ns16550a");
81         }
82         if (uart_off != -FDT_ERR_NOTFOUND) {
83                 pr_err("error searching for UART DT node: %d\n", uart_off);
84                 return uart_off;
85         }
86
87         eth_off = fdt_node_offset_by_compatible(fdt, -1, "smsc,lan9115");
88         if (eth_off < 0) {
89                 pr_err("unable to find ethernet DT node: %d\n", eth_off);
90                 return eth_off;
91         }
92
93         err = fdt_setprop_u32(fdt, eth_off, "interrupts", cpu_eth_int);
94         if (err) {
95                 pr_err("unable to set ethernet interrupts property: %d\n", err);
96                 return err;
97         }
98
99         ehci_off = fdt_node_offset_by_compatible(fdt, -1, "mti,sead3-ehci");
100         if (ehci_off < 0) {
101                 pr_err("unable to find EHCI DT node: %d\n", ehci_off);
102                 return ehci_off;
103         }
104
105         err = fdt_setprop_u32(fdt, ehci_off, "interrupts", cpu_ehci_int);
106         if (err) {
107                 pr_err("unable to set EHCI interrupts property: %d\n", err);
108                 return err;
109         }
110
111         return 0;
112 }
113
114 static int serial_config(void *fdt)
115 {
116         const char *yamontty, *mode_var;
117         char mode_var_name[9], path[18], parity;
118         unsigned int uart, baud, stop_bits;
119         bool hw_flow;
120         int chosen_off, err;
121
122         yamontty = fw_getenv("yamontty");
123         if (!yamontty || !strcmp(yamontty, "tty0")) {
124                 uart = 0;
125         } else if (!strcmp(yamontty, "tty1")) {
126                 uart = 1;
127         } else {
128                 pr_warn("yamontty environment variable '%s' invalid\n",
129                         yamontty);
130                 uart = 0;
131         }
132
133         baud = stop_bits = 0;
134         parity = 0;
135         hw_flow = false;
136
137         snprintf(mode_var_name, sizeof(mode_var_name), "modetty%u", uart);
138         mode_var = fw_getenv(mode_var_name);
139         if (mode_var) {
140                 while (mode_var[0] >= '0' && mode_var[0] <= '9') {
141                         baud *= 10;
142                         baud += mode_var[0] - '0';
143                         mode_var++;
144                 }
145                 if (mode_var[0] == ',')
146                         mode_var++;
147                 if (mode_var[0])
148                         parity = mode_var[0];
149                 if (mode_var[0] == ',')
150                         mode_var++;
151                 if (mode_var[0])
152                         stop_bits = mode_var[0] - '0';
153                 if (mode_var[0] == ',')
154                         mode_var++;
155                 if (!strcmp(mode_var, "hw"))
156                         hw_flow = true;
157         }
158
159         if (!baud)
160                 baud = 38400;
161
162         if (parity != 'e' && parity != 'n' && parity != 'o')
163                 parity = 'n';
164
165         if (stop_bits != 7 && stop_bits != 8)
166                 stop_bits = 8;
167
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));
171
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",
180                        chosen_off);
181                 return chosen_off;
182         }
183
184         err = fdt_setprop_string(fdt, chosen_off, "stdout-path", path);
185         if (err) {
186                 pr_err("Unable to set stdout-path property: %d\n", err);
187                 return err;
188         }
189
190         return 0;
191 }
192
193 void __init *sead3_dt_shim(void *fdt)
194 {
195         int err;
196
197         if (fdt_check_header(fdt))
198                 panic("Corrupt DT");
199
200         /* if this isn't SEAD3, leave the DT alone */
201         if (fdt_node_check_compatible(fdt, 0, "mti,sead-3"))
202                 return fdt;
203
204         err = fdt_open_into(fdt, fdt_buf, sizeof(fdt_buf));
205         if (err)
206                 panic("Unable to open FDT: %d", err);
207
208         err = remove_gic(fdt_buf);
209         if (err)
210                 panic("Unable to patch FDT: %d", err);
211
212         err = serial_config(fdt_buf);
213         if (err)
214                 panic("Unable to patch FDT: %d", err);
215
216         err = fdt_pack(fdt_buf);
217         if (err)
218                 panic("Unable to pack FDT: %d\n", err);
219
220         return fdt_buf;
221 }