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