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