cosmetic fixes for ar7: indent, use lowercase hex notation, format
[openwrt/openwrt.git] / target / linux / ar7 / files / arch / mips / ar7 / prom.c
1 /*
2 * $Id$
3 *
4 * Copyright (C) 2006, 2007 OpenWrt.org
5 *
6 * Carsten Langgaard, carstenl@mips.com
7 * Copyright (C) 1999,2000 MIPS Technologies, Inc. All rights reserved.
8 *
9 * This program is free software; you can distribute it and/or modify it
10 * under the terms of the GNU General Public License (Version 2) as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
21 *
22 * Putting things on the screen/serial line using YAMONs facilities.
23 */
24 #include <linux/init.h>
25 #include <linux/kernel.h>
26 #include <linux/serial_reg.h>
27 #include <linux/spinlock.h>
28 #include <linux/module.h>
29 #include <linux/string.h>
30 #include <asm/io.h>
31 #include <asm/bootinfo.h>
32 #include <asm/mips-boards/prom.h>
33 #include <asm/gdb-stub.h>
34
35 #include <asm/ar7/ar7.h>
36 #include <asm/ar7/prom.h>
37
38 #define MAX_ENTRY 80
39
40 struct env_var {
41 char *name;
42 char *value;
43 };
44
45 static struct env_var adam2_env[MAX_ENTRY] = { { 0, }, };
46
47 char * prom_getenv(char *name)
48 {
49 int i;
50 for (i = 0; (i < MAX_ENTRY) && adam2_env[i].name; i++)
51 if (!strcmp(name, adam2_env[i].name))
52 return adam2_env[i].value;
53
54 return NULL;
55 }
56
57 char * __init prom_getcmdline(void)
58 {
59 return &(arcs_cmdline[0]);
60 }
61
62 static void __init ar7_init_cmdline(int argc, char *argv[])
63 {
64 char *cp;
65 int actr;
66
67 actr = 1; /* Always ignore argv[0] */
68
69 cp = &(arcs_cmdline[0]);
70 while(actr < argc) {
71 strcpy(cp, argv[actr]);
72 cp += strlen(argv[actr]);
73 *cp++ = ' ';
74 actr++;
75 }
76 if (cp != &(arcs_cmdline[0])) {
77 /* get rid of trailing space */
78 --cp;
79 *cp = '\0';
80 }
81 }
82
83 struct psbl_rec {
84 u32 psbl_size;
85 u32 env_base;
86 u32 env_size;
87 u32 ffs_base;
88 u32 ffs_size;
89 };
90
91 static __initdata char psp_env_version[] = "TIENV0.8";
92
93 struct psp_env_chunk {
94 u8 num;
95 u8 ctrl;
96 u16 csum;
97 u8 len;
98 char data[11];
99 } __attribute__ ((packed));
100
101 struct psp_var_map_entry {
102 u8 num;
103 char *value;
104 };
105
106 static struct psp_var_map_entry psp_var_map[] = {
107 { 1, "cpufrequency" },
108 { 2, "memsize" },
109 { 3, "flashsize" },
110 { 4, "modetty0" },
111 { 5, "modetty1" },
112 { 8, "maca" },
113 { 9, "macb" },
114 { 28, "sysfrequency" },
115 { 38, "mipsfrequency" },
116 };
117
118 /*
119
120 Well-known variable (num is looked up in table above for matching variable name)
121 Example: cpufrequency=211968000
122 +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
123 | 01 |CTRL|CHECKSUM | 01 | _2 | _1 | _1 | _9 | _6 | _8 | _0 | _0 | _0 | \0 | FF |
124 +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
125
126 Name=Value pair in a single chunk
127 Example: NAME=VALUE
128 +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
129 | 00 |CTRL|CHECKSUM | 01 | _N | _A | _M | _E | _0 | _V | _A | _L | _U | _E | \0 |
130 +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
131
132 Name=Value pair in 2 chunks (len is the number of chunks)
133 Example: bootloaderVersion=1.3.7.15
134 +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
135 | 00 |CTRL|CHECKSUM | 02 | _b | _o | _o | _t | _l | _o | _a | _d | _e | _r | _V |
136 +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
137 | _e | _r | _s | _i | _o | _n | \0 | _1 | _. | _3 | _. | _7 | _. | _1 | _5 | \0 |
138 +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
139
140 Data is padded with 0xFF
141
142 */
143
144 #define PSP_ENV_SIZE 4096
145
146 static char psp_env_data[PSP_ENV_SIZE] = { 0, };
147
148 static char * __init lookup_psp_var_map(u8 num)
149 {
150 int i;
151
152 for (i = 0; i < sizeof(psp_var_map); i++)
153 if (psp_var_map[i].num == num)
154 return psp_var_map[i].value;
155
156 return NULL;
157 }
158
159 static void __init add_adam2_var(char *name, char *value)
160 {
161 int i;
162 for (i = 0; i < MAX_ENTRY; i++) {
163 if (!adam2_env[i].name) {
164 adam2_env[i].name = name;
165 adam2_env[i].value = value;
166 return;
167 } else if (!strcmp(adam2_env[i].name, name)) {
168 adam2_env[i].value = value;
169 return;
170 }
171 }
172 }
173
174 static int __init parse_psp_env(void *psp_env_base)
175 {
176 int i, n;
177 char *name, *value;
178 struct psp_env_chunk *chunks = (struct psp_env_chunk *)psp_env_data;
179
180 memcpy_fromio(chunks, psp_env_base, PSP_ENV_SIZE);
181
182 i = 1;
183 n = PSP_ENV_SIZE / sizeof(struct psp_env_chunk);
184 while (i < n) {
185 if ((chunks[i].num == 0xff) || ((i + chunks[i].len) > n))
186 break;
187 value = chunks[i].data;
188 if (chunks[i].num) {
189 name = lookup_psp_var_map(chunks[i].num);
190 } else {
191 name = value;
192 value += strlen(name) + 1;
193 }
194 if (name)
195 add_adam2_var(name, value);
196 i += chunks[i].len;
197 }
198 return 0;
199 }
200
201 static void __init ar7_init_env(struct env_var *env)
202 {
203 int i;
204 struct psbl_rec *psbl = (struct psbl_rec *)(KSEG1ADDR(0x14000300));
205 void *psp_env = (void *)KSEG1ADDR(psbl->env_base);
206
207 if(strcmp(psp_env, psp_env_version) == 0) {
208 parse_psp_env(psp_env);
209 } else {
210 for (i = 0; i < MAX_ENTRY; i++, env++)
211 if (env->name)
212 add_adam2_var(env->name, env->value);
213 }
214 }
215
216 static void __init console_config(void)
217 {
218 #ifdef CONFIG_SERIAL_8250_CONSOLE
219 char console_string[40];
220 int baud = 0;
221 char parity = '\0', bits = '\0', flow = '\0';
222 char *s, *p;
223
224 if (strstr(prom_getcmdline(), "console="))
225 return;
226
227 #ifdef CONFIG_KGDB
228 if (!strstr(prom_getcmdline(), "nokgdb")) {
229 strcat(prom_getcmdline(), " console=kgdb");
230 kgdb_enabled = 1;
231 return;
232 }
233 #endif
234
235 if ((s = prom_getenv("modetty0"))) {
236 baud = simple_strtoul(s, &p, 10);
237 s = p;
238 if (*s == ',') s++;
239 if (*s) parity = *s++;
240 if (*s == ',') s++;
241 if (*s) bits = *s++;
242 if (*s == ',') s++;
243 if (*s == 'h') flow = 'r';
244 }
245
246 if (baud == 0)
247 baud = 38400;
248 if (parity != 'n' && parity != 'o' && parity != 'e')
249 parity = 'n';
250 if (bits != '7' && bits != '8')
251 bits = '8';
252 if (flow == '\0')
253 flow = 'r';
254
255 sprintf(console_string, " console=ttyS0,%d%c%c%c", baud,
256 parity, bits, flow);
257 strcat(prom_getcmdline(), console_string);
258 #endif
259 }
260
261 void __init prom_init(void)
262 {
263 ar7_init_cmdline(fw_arg0, (char **)fw_arg1);
264 ar7_init_env((struct env_var *)fw_arg2);
265 console_config();
266 }
267
268 #define PORT(offset) (KSEG1ADDR(AR7_REGS_UART0 + (offset * 4)))
269 static inline unsigned int serial_in(int offset)
270 {
271 return readb((void *)PORT(offset));
272 }
273
274 static inline void serial_out(int offset, int value)
275 {
276 writeb(value, (void *)PORT(offset));
277 }
278
279 char prom_getchar(void)
280 {
281 while (!(serial_in(UART_LSR) & UART_LSR_DR));
282 return serial_in(UART_RX);
283 }
284
285 int prom_putchar(char c)
286 {
287 while ((serial_in(UART_LSR) & UART_LSR_TEMT) == 0);
288 serial_out(UART_TX, c);
289 return 1;
290 }
291
292 // from adm5120/prom.c
293 void prom_printf(char *fmt, ...)
294 {
295 va_list args;
296 int l;
297 char *p, *buf_end;
298 char buf[1024];
299
300 va_start(args, fmt);
301 l = vsprintf(buf, fmt, args); /* hopefully i < sizeof(buf) */
302 va_end(args);
303
304 buf_end = buf + l;
305
306 for (p = buf; p < buf_end; p++) {
307 /* Crude cr/nl handling is better than none */
308 if (*p == '\n')
309 prom_putchar('\r');
310 prom_putchar(*p);
311 }
312 }
313
314 #ifdef CONFIG_KGDB
315 int putDebugChar(char c)
316 {
317 return prom_putchar(c);
318 }
319
320 char getDebugChar(void)
321 {
322 return prom_getchar();
323 }
324 #endif
325
326 EXPORT_SYMBOL(prom_getenv);