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