Add more ar7 fixes by DerAgo. Fix vlnyq initialistion on fritzbox, add prom_printf...
[librecmc/librecmc.git] / target / linux / ar7-2.6 / 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
37 #define MAX_ENTRY 80
38
39 struct env_var {
40         char *name;
41         char *value;
42 };
43
44 struct psp_chip_map {
45         u16 chip;
46         char *names[50];
47 };
48
49 /* I hate this. No. *I* *HATE* *THIS* */
50 static __initdata struct psp_chip_map psp_chip_map[] = {
51         {
52                 .chip = AR7_CHIP_7100,
53                 .names = {
54                         "dummy", "cpufrequency", "memsize",
55                         "flashsize", "modetty0", "modetty1", "prompt",
56                         "bootcfg", "maca", "macb", "usb_rndis_mac",
57                         "macap", "my_ipaddress", "server_ipaddress",
58                         "bline_maca", "bline_macb", "bline_rndis",
59                         "bline_atm", "usb_pid", "usb_vid",
60                         "usb_eppolli", "gateway_ipaddress",
61                         "subnet_mask", "usb_serial", "usb_board_mac",
62                         "remote_user", "remote_pass", "remote_dir",
63                         "sysfrequency", "link_timeout", "mac_port",
64                         "path", "hostname", "tftpcfg", "buildops",
65                         "tftp_fo_fname", "tftp_fo_ports",
66                         "console_state", "mipsfrequency", 
67                 },
68         },
69         {
70                 .chip = AR7_CHIP_7200,
71                 .names = {
72                         "dummy", "cpufrequency", "memsize",
73                         "flashsize", "modetty0", "modetty1", "prompt",
74                         "bootcfg", "maca", "macb", "usb_rndis_mac",
75                         "macap", "my_ipaddress", "server_ipaddress",
76                         "bline_maca", "bline_macb", "bline_rndis",
77                         "bline_atm", "usb_pid", "usb_vid",
78                         "usb_eppolli", "gateway_ipaddress",
79                         "subnet_mask", "usb_serial", "usb_board_mac",
80                         "remote_user", "remote_pass", "remote_dir",
81                         "sysfrequency", "link_timeout", "mac_port",
82                         "path", "hostname", "tftpcfg", "buildops",
83                         "tftp_fo_fname", "tftp_fo_ports",
84                         "console_state", "mipsfrequency", 
85                 },
86         },
87         {
88                 .chip = AR7_CHIP_7300,
89                 .names = {
90                         "dummy", "cpufrequency", "memsize",
91                         "flashsize", "modetty0", "modetty1", "prompt",
92                         "bootcfg", "maca", "macb", "usb_rndis_mac",
93                         "macap", "my_ipaddress", "server_ipaddress",
94                         "bline_maca", "bline_macb", "bline_rndis",
95                         "bline_atm", "usb_pid", "usb_vid",
96                         "usb_eppolli", "gateway_ipaddress",
97                         "subnet_mask", "usb_serial", "usb_board_mac",
98                         "remote_user", "remote_pass", "remote_dir",
99                         "sysfrequency", "link_timeout", "mac_port",
100                         "path", "hostname", "tftpcfg", "buildops",
101                         "tftp_fo_fname", "tftp_fo_ports",
102                         "console_state", "mipsfrequency", 
103                 },
104         },
105         {
106                 .chip = 0x0,
107         },
108 };
109
110 static struct env_var adam2_env[MAX_ENTRY] = { { 0, }, };
111
112 char * prom_getenv(char *name)
113 {
114         int i;
115         for (i = 0; (i < MAX_ENTRY) && adam2_env[i].name; i++)
116                 if (!strcmp(name, adam2_env[i].name))
117                         return adam2_env[i].value;
118
119         return NULL;
120 }
121
122 char * __init prom_getcmdline(void)
123 {
124         return &(arcs_cmdline[0]);
125 }
126
127 static void  __init ar7_init_cmdline(int argc, char *argv[])
128 {
129         char *cp;
130         int actr;
131
132         actr = 1; /* Always ignore argv[0] */
133
134         cp = &(arcs_cmdline[0]);
135         while(actr < argc) {
136                 strcpy(cp, argv[actr]);
137                 cp += strlen(argv[actr]);
138                 *cp++ = ' ';
139                 actr++;
140         }
141         if (cp != &(arcs_cmdline[0])) {
142                 /* get rid of trailing space */
143                 --cp;
144                 *cp = '\0';
145         }
146 }
147
148 struct psbl_rec {
149         u32 psbl_size;
150         u32 env_base;
151         u32 env_size;
152         u32 ffs_base;
153         u32 ffs_size;
154 };
155
156 static __initdata char psp_env_version[] = "TIENV0.8";
157
158 struct psp_env_var {
159         unsigned char num;
160         unsigned char ctrl;
161         unsigned short csum;
162         unsigned char len;
163         unsigned char data[11];
164 };
165
166 static char psp_env_data[2048] = { 0, };
167
168 static void __init add_adam2_var(char *name, char *value)
169 {
170         int i;
171         for (i = 0; i < MAX_ENTRY; i++) {
172                 if (!adam2_env[i].name) {
173                         adam2_env[i].name = name;
174                         adam2_env[i].value = value;
175                         return;
176                 } else if (!strcmp(adam2_env[i].name, name)) {
177                         adam2_env[i].value = value;
178                         return;
179                 }
180         }
181 }
182
183 static int __init parse_psp_env(void *start)
184 {
185         int i, j;
186         u16 chip;
187         struct psp_chip_map *map;
188         char *src, *dest, *name, *value;
189         struct psp_env_var *vars = start;
190
191         chip = ar7_chip_id();
192         for (map = psp_chip_map; map->chip; map++)
193                 if (map->chip == chip)
194                         break;
195
196         if (!map->chip)
197                 return -EINVAL;
198
199         i = 1;
200         j = 0;
201         dest = psp_env_data;
202         while (vars[i].num < 0xff) {
203                 src = vars[i].data;
204                 if (vars[i].num) {
205                         strcpy(dest, map->names[vars[i].num]);
206                         name = dest;
207                 } else {
208                         strcpy(dest, src);
209                         name = dest;
210                         src += strlen(src) + 1;
211                 }                       
212                 dest += strlen(dest) + 1;
213                 strcpy(dest, src);
214                 value = dest;
215                 dest += strlen(dest) + 1;
216                 add_adam2_var(name, value);
217                 i += vars[i].len;
218         }
219         return 0;
220 }
221
222 static void __init ar7_init_env(struct env_var *env)
223 {
224         int i;
225         struct psbl_rec *psbl = (struct psbl_rec *)(KSEG1ADDR(0x14000300));
226         void *psp_env = (void *)KSEG1ADDR(psbl->env_base);
227
228         if(strcmp(psp_env, psp_env_version) == 0) {
229                 parse_psp_env(psp_env);
230         } else {
231                 for (i = 0; i < MAX_ENTRY; i++, env++)
232                         if (env->name)
233                                 add_adam2_var(env->name, env->value);
234         }
235 }
236
237 static void __init console_config(void)
238 {
239 #ifdef CONFIG_SERIAL_8250_CONSOLE
240         char console_string[40];
241         int baud = 0;
242         char parity = '\0', bits = '\0', flow = '\0';
243         char *s, *p;
244
245         if (strstr(prom_getcmdline(), "console="))
246                 return;
247
248 #ifdef CONFIG_KGDB
249         if (!strstr(prom_getcmdline(), "nokgdb"))
250         {
251             strcat(prom_getcmdline(), " console=kgdb");
252             kgdb_enabled = 1;
253             return;
254         }
255 #endif
256
257         if ((s = prom_getenv("modetty0"))) {
258                 baud = simple_strtoul(s, &p, 10);
259                 s = p;
260                 if (*s == ',') s++;
261                 if (*s) parity = *s++;
262                 if (*s == ',') s++;
263                 if (*s) bits = *s++;
264                 if (*s == ',') s++;
265                 if (*s == 'h') flow = 'r';
266         }
267
268         if (baud == 0)
269                 baud = 38400;
270         if (parity != 'n' && parity != 'o' && parity != 'e')
271                 parity = 'n';
272         if (bits != '7' && bits != '8')
273                 bits = '8';
274         if (flow == '\0')
275                 flow = 'r';
276
277         sprintf(console_string, " console=ttyS0,%d%c%c%c", baud,
278                 parity, bits, flow);
279         strcat(prom_getcmdline(), console_string);
280 #endif
281 }
282
283 void __init prom_init(void)
284 {
285         ar7_init_cmdline(fw_arg0, (char **)fw_arg1);
286         ar7_init_env((struct env_var *)fw_arg2);
287         console_config();
288 }
289
290 #define PORT(offset) (KSEG1ADDR(AR7_REGS_UART0 + (offset * 4)))
291 static inline unsigned int serial_in(int offset)
292 {
293         return readb((void *)PORT(offset));
294 }
295
296 static inline void serial_out(int offset, int value)
297 {
298         writeb(value, (void *)PORT(offset));
299 }
300
301 char prom_getchar(void)
302 {
303         while (!(serial_in(UART_LSR) & UART_LSR_DR));
304         return serial_in(UART_RX);
305 }
306
307 int prom_putchar(char c)
308 {
309         while ((serial_in(UART_LSR) & UART_LSR_TEMT) == 0);
310         serial_out(UART_TX, c);
311         return 1;
312 }
313
314 // from adm5120/prom.c
315 void prom_printf(char *fmt, ...)
316 {
317     va_list args;
318     int l;
319     char *p, *buf_end;
320     char buf[1024];
321
322     va_start(args, fmt);
323     l = vsprintf(buf, fmt, args); /* hopefully i < sizeof(buf) */
324     va_end(args);
325
326     buf_end = buf + l;
327
328     for (p = buf; p < buf_end; p++) {
329         /* Crude cr/nl handling is better than none */
330         if (*p == '\n')
331             prom_putchar('\r');
332         prom_putchar(*p);
333     }
334 }
335
336 #ifdef CONFIG_KGDB
337 int putDebugChar(char c)
338 {
339         return prom_putchar(c);
340 }
341
342 char getDebugChar(void)
343 {
344        return prom_getchar();
345 }
346 #endif
347
348 EXPORT_SYMBOL(prom_getenv);