]> bbs.cooldavid.org Git - net-next-2.6.git/blob - arch/mips/ar7/prom.c
MIPS: AR7: use strlcat() for the command line arguments
[net-next-2.6.git] / 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
29 #include <asm/mach-ar7/ar7.h>
30 #include <asm/mach-ar7/prom.h>
31
32 #define MAX_ENTRY 80
33
34 struct env_var {
35         char *name;
36         char *value;
37 };
38
39 static struct env_var adam2_env[MAX_ENTRY];
40
41 char *prom_getenv(const char *name)
42 {
43         int i;
44         for (i = 0; (i < MAX_ENTRY) && adam2_env[i].name; i++)
45                 if (!strcmp(name, adam2_env[i].name))
46                         return adam2_env[i].value;
47
48         return NULL;
49 }
50 EXPORT_SYMBOL(prom_getenv);
51
52 char * __init prom_getcmdline(void)
53 {
54         return &(arcs_cmdline[0]);
55 }
56
57 static void  __init ar7_init_cmdline(int argc, char *argv[])
58 {
59         int i;
60
61         for (i = 1; i < argc; i++) {
62                 strlcat(arcs_cmdline, argv[i], COMMAND_LINE_SIZE);
63                 if (i < (argc - 1))
64                         strlcat(arcs_cmdline, " ", COMMAND_LINE_SIZE);
65         }
66 }
67
68 struct psbl_rec {
69         u32 psbl_size;
70         u32 env_base;
71         u32 env_size;
72         u32 ffs_base;
73         u32 ffs_size;
74 };
75
76 static __initdata char psp_env_version[] = "TIENV0.8";
77
78 struct psp_env_chunk {
79         u8 num;
80         u8 ctrl;
81         u16 csum;
82         u8 len;
83         char data[11];
84 } __attribute__ ((packed));
85
86 struct psp_var_map_entry {
87         u8 num;
88         char *value;
89 };
90
91 static struct psp_var_map_entry psp_var_map[] = {
92         { 1, "cpufrequency" },
93         { 2, "memsize" },
94         { 3, "flashsize" },
95         { 4, "modetty0" },
96         { 5, "modetty1" },
97         { 8, "maca" },
98         { 9, "macb" },
99         { 28, "sysfrequency" },
100         { 38, "mipsfrequency" },
101 };
102
103 /*
104
105 Well-known variable (num is looked up in table above for matching variable name)
106 Example: cpufrequency=211968000
107 +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+---
108 | 01 |CTRL|CHECKSUM | 01 | _2 | _1 | _1 | _9 | _6 | _8 | _0 | _0 | _0 | \0 | FF
109 +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+---
110
111 Name=Value pair in a single chunk
112 Example: NAME=VALUE
113 +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+---
114 | 00 |CTRL|CHECKSUM | 01 | _N | _A | _M | _E | _0 | _V | _A | _L | _U | _E | \0
115 +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+---
116
117 Name=Value pair in 2 chunks (len is the number of chunks)
118 Example: bootloaderVersion=1.3.7.15
119 +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+---
120 | 00 |CTRL|CHECKSUM | 02 | _b | _o | _o | _t | _l | _o | _a | _d | _e | _r | _V
121 +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+---
122 | _e | _r | _s | _i | _o | _n | \0 | _1 | _. | _3 | _. | _7 | _. | _1 | _5 | \0
123 +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+---
124
125 Data is padded with 0xFF
126
127 */
128
129 #define PSP_ENV_SIZE  4096
130
131 static char psp_env_data[PSP_ENV_SIZE] = { 0, };
132
133 static char * __init lookup_psp_var_map(u8 num)
134 {
135         int i;
136
137         for (i = 0; i < ARRAY_SIZE(psp_var_map); i++)
138                 if (psp_var_map[i].num == num)
139                         return psp_var_map[i].value;
140
141         return NULL;
142 }
143
144 static void __init add_adam2_var(char *name, char *value)
145 {
146         int i;
147         for (i = 0; i < MAX_ENTRY; i++) {
148                 if (!adam2_env[i].name) {
149                         adam2_env[i].name = name;
150                         adam2_env[i].value = value;
151                         return;
152                 } else if (!strcmp(adam2_env[i].name, name)) {
153                         adam2_env[i].value = value;
154                         return;
155                 }
156         }
157 }
158
159 static int __init parse_psp_env(void *psp_env_base)
160 {
161         int i, n;
162         char *name, *value;
163         struct psp_env_chunk *chunks = (struct psp_env_chunk *)psp_env_data;
164
165         memcpy_fromio(chunks, psp_env_base, PSP_ENV_SIZE);
166
167         i = 1;
168         n = PSP_ENV_SIZE / sizeof(struct psp_env_chunk);
169         while (i < n) {
170                 if ((chunks[i].num == 0xff) || ((i + chunks[i].len) > n))
171                         break;
172                 value = chunks[i].data;
173                 if (chunks[i].num) {
174                         name = lookup_psp_var_map(chunks[i].num);
175                 } else {
176                         name = value;
177                         value += strlen(name) + 1;
178                 }
179                 if (name)
180                         add_adam2_var(name, value);
181                 i += chunks[i].len;
182         }
183         return 0;
184 }
185
186 static void __init ar7_init_env(struct env_var *env)
187 {
188         int i;
189         struct psbl_rec *psbl = (struct psbl_rec *)(KSEG1ADDR(0x14000300));
190         void *psp_env = (void *)KSEG1ADDR(psbl->env_base);
191
192         if (strcmp(psp_env, psp_env_version) == 0) {
193                 parse_psp_env(psp_env);
194         } else {
195                 for (i = 0; i < MAX_ENTRY; i++, env++)
196                         if (env->name)
197                                 add_adam2_var(env->name, env->value);
198         }
199 }
200
201 static void __init console_config(void)
202 {
203 #ifdef CONFIG_SERIAL_8250_CONSOLE
204         char console_string[40];
205         int baud = 0;
206         char parity = '\0', bits = '\0', flow = '\0';
207         char *s, *p;
208
209         if (strstr(prom_getcmdline(), "console="))
210                 return;
211
212         s = prom_getenv("modetty0");
213         if (s) {
214                 baud = simple_strtoul(s, &p, 10);
215                 s = p;
216                 if (*s == ',')
217                         s++;
218                 if (*s)
219                         parity = *s++;
220                 if (*s == ',')
221                         s++;
222                 if (*s)
223                         bits = *s++;
224                 if (*s == ',')
225                         s++;
226                 if (*s == 'h')
227                         flow = 'r';
228         }
229
230         if (baud == 0)
231                 baud = 38400;
232         if (parity != 'n' && parity != 'o' && parity != 'e')
233                 parity = 'n';
234         if (bits != '7' && bits != '8')
235                 bits = '8';
236
237         if (flow == 'r')
238                 sprintf(console_string, " console=ttyS0,%d%c%c%c", baud,
239                         parity, bits, flow);
240         else
241                 sprintf(console_string, " console=ttyS0,%d%c%c", baud, parity,
242                         bits);
243         strlcat(arcs_cmdline, console_string, COMMAND_LINE_SIZE);
244 #endif
245 }
246
247 void __init prom_init(void)
248 {
249         ar7_init_cmdline(fw_arg0, (char **)fw_arg1);
250         ar7_init_env((struct env_var *)fw_arg2);
251         console_config();
252 }
253
254 #define PORT(offset) (KSEG1ADDR(AR7_REGS_UART0 + (offset * 4)))
255 static inline unsigned int serial_in(int offset)
256 {
257         return readl((void *)PORT(offset));
258 }
259
260 static inline void serial_out(int offset, int value)
261 {
262         writel(value, (void *)PORT(offset));
263 }
264
265 int prom_putchar(char c)
266 {
267         while ((serial_in(UART_LSR) & UART_LSR_TEMT) == 0)
268                 ;
269         serial_out(UART_TX, c);
270         return 1;
271 }