f522d1ff39dab24b0c989407a0a0572d3ba0fdce
[openwrt/staging/yousong.git] / target / linux / ar71xx / files / arch / mips / ar71xx / prom.c
1 /*
2 * Atheros AR71xx SoC specific prom routines
3 *
4 * Copyright (C) 2008-2009 Gabor Juhos <juhosg@openwrt.org>
5 * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published
9 * by the Free Software Foundation.
10 */
11
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/io.h>
15 #include <linux/string.h>
16
17 #include <asm/bootinfo.h>
18 #include <asm/addrspace.h>
19 #include <asm/fw/myloader/myloader.h>
20
21 #include <asm/mach-ar71xx/ar71xx.h>
22
23 #include "machtype.h"
24 #include "devices.h"
25
26 struct board_rec {
27 char *name;
28 enum ar71xx_mach_type mach_type;
29 };
30
31 static struct board_rec boards[] __initdata = {
32 {
33 .name = "411",
34 .mach_type = AR71XX_MACH_RB_411,
35 }, {
36 .name = "411U",
37 .mach_type = AR71XX_MACH_RB_411U,
38 }, {
39 .name = "433",
40 .mach_type = AR71XX_MACH_RB_433,
41 }, {
42 .name = "433U",
43 .mach_type = AR71XX_MACH_RB_433U,
44 }, {
45 .name = "450",
46 .mach_type = AR71XX_MACH_RB_450,
47 }, {
48 .name = "450G",
49 .mach_type = AR71XX_MACH_RB_450G,
50 }, {
51 .name = "493",
52 .mach_type = AR71XX_MACH_RB_493,
53 }, {
54 .name = "AP81",
55 .mach_type = AR71XX_MACH_AP81,
56 }, {
57 .name = "AP83",
58 .mach_type = AR71XX_MACH_AP83,
59 }, {
60 .name = "AW-NR580",
61 .mach_type = AR71XX_MACH_AW_NR580,
62 }, {
63 .name = "DIR-825-B1",
64 .mach_type = AR71XX_MACH_DIR_825_B1,
65 }, {
66 .name = "TEW-632BRP",
67 .mach_type = AR71XX_MACH_TEW_632BRP,
68 }, {
69 .name = "DIR-615-C1",
70 .mach_type = AR71XX_MACH_DIR_615_C1,
71 }, {
72 .name = "TL-WR741ND",
73 .mach_type = AR71XX_MACH_TL_WR741ND,
74 }, {
75 .name = "TL-WR841N-v1.5",
76 .mach_type = AR71XX_MACH_TL_WR841N_V1,
77 }, {
78 .name = "TL-WR941ND",
79 .mach_type = AR71XX_MACH_TL_WR941ND,
80 }, {
81 .name = "TL-WR1043ND",
82 .mach_type = AR71XX_MACH_TL_WR1043ND,
83 }, {
84 .name = "UBNT-RS",
85 .mach_type = AR71XX_MACH_UBNT_RS,
86 }, {
87 .name = "UBNT-RSPRO",
88 .mach_type = AR71XX_MACH_UBNT_RSPRO,
89 }, {
90 .name = "Ubiquiti AR71xx-based board",
91 .mach_type = AR71XX_MACH_UBNT_RS,
92 }, {
93 .name = "UBNT-LS-SR71",
94 .mach_type = AR71XX_MACH_UBNT_LSSR71,
95 }, {
96 .name = "UBNT-LSX",
97 .mach_type = AR71XX_MACH_UBNT_LSX,
98 }, {
99 .name = "UBNT-BM",
100 .mach_type = AR71XX_MACH_UBNT_BULLET_M,
101 }, {
102 .name = "UBNT-RM",
103 .mach_type = AR71XX_MACH_UBNT_ROCKET_M,
104 }, {
105 .name = "UBNT-NM",
106 .mach_type = AR71XX_MACH_UBNT_NANO_M,
107 }, {
108 .name = "WNDR3700",
109 .mach_type = AR71XX_MACH_WNDR3700,
110 }, {
111 .name = "WNR2000",
112 .mach_type = AR71XX_MACH_WNR2000,
113 }, {
114 .name = "WRT160NL",
115 .mach_type = AR71XX_MACH_WRT160NL,
116 }, {
117 .name = "WP543",
118 .mach_type = AR71XX_MACH_WP543,
119 }, {
120 .name = "WRT400N",
121 .mach_type = AR71XX_MACH_WRT400N,
122 }, {
123 .name = "PB42",
124 .mach_type = AR71XX_MACH_PB42,
125 }, {
126 .name = "PB44",
127 .mach_type = AR71XX_MACH_PB44,
128 }, {
129 .name = "MZK-W300NH",
130 .mach_type = AR71XX_MACH_MZK_W300NH,
131 }, {
132 .name = "MZK-W04NU",
133 .mach_type = AR71XX_MACH_MZK_W04NU,
134 }
135 };
136
137 static int __init ar71xx_board_setup(char *name)
138 {
139 int i;
140
141 for (i = 0; i < ARRAY_SIZE(boards); i++)
142 if (strcmp(name, boards[i].name) == 0) {
143 ar71xx_mach = boards[i].mach_type;
144 break;
145 }
146
147 return 1;
148 }
149 __setup("board=", ar71xx_board_setup);
150
151 static int __init ar71xx_ethaddr_setup(char *str)
152 {
153 ar71xx_parse_mac_addr(str);
154 return 1;
155 }
156 __setup("ethaddr=", ar71xx_ethaddr_setup);
157
158 static int __init ar71xx_kmac_setup(char *str)
159 {
160 ar71xx_parse_mac_addr(str);
161 return 1;
162 }
163 __setup("kmac=", ar71xx_kmac_setup);
164
165 static inline int is_valid_ram_addr(void *addr)
166 {
167 if (((u32) addr > KSEG0) &&
168 ((u32) addr < (KSEG0 + AR71XX_MEM_SIZE_MAX)))
169 return 1;
170
171 if (((u32) addr > KSEG1) &&
172 ((u32) addr < (KSEG1 + AR71XX_MEM_SIZE_MAX)))
173 return 1;
174
175 return 0;
176 }
177
178 static void __init ar71xx_prom_append_cmdline(const char *name,
179 const char *value)
180 {
181 char buf[CL_SIZE];
182
183 snprintf(buf, sizeof(buf), " %s=%s", name, value);
184 strlcat(arcs_cmdline, buf, sizeof(arcs_cmdline));
185 }
186
187 static void __init ar71xx_prom_find_env(char **envp, const char *name)
188 {
189 int len = strlen(name);
190 char **p;
191
192 if (!is_valid_ram_addr(envp))
193 return;
194
195 for (p = envp; is_valid_ram_addr(*p); p++) {
196 if (strncmp(name, *p, len) == 0 && (*p)[len] == '=') {
197 ar71xx_prom_append_cmdline(name, *p + len + 1);
198 break;
199 }
200
201 /* RedBoot env comes in pointer pairs - key, value */
202 if (strncmp(name, *p, len) == 0 && (*p)[len] == 0)
203 if (is_valid_ram_addr(*(++p))) {
204 ar71xx_prom_append_cmdline(name, *p);
205 break;
206 }
207 }
208 }
209
210 static int __init ar71xx_prom_init_myloader(void)
211 {
212 struct myloader_info *mylo;
213 char mac_buf[32];
214 char *mac;
215
216 mylo = myloader_get_info();
217 if (!mylo)
218 return 0;
219
220 switch (mylo->did) {
221 case DEVID_COMPEX_WP543:
222 ar71xx_prom_append_cmdline("board", "WP543");
223 break;
224 default:
225 printk(KERN_WARNING "prom: unknown device id: %x\n",
226 mylo->did);
227 return 0;
228 }
229
230 mac = mylo->macs[0];
231 snprintf(mac_buf, sizeof(mac_buf), "%02x:%02x:%02x:%02x:%02x:%02x",
232 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
233
234 ar71xx_prom_append_cmdline("ethaddr", mac_buf);
235
236 return 1;
237 }
238
239 #ifdef CONFIG_IMAGE_CMDLINE_HACK
240 extern char __image_cmdline[];
241
242 static int __init ar71xx_use__image_cmdline(void)
243 {
244 char *p = __image_cmdline;
245 int replace = 0;
246
247 if (*p == '-') {
248 replace = 1;
249 p++;
250 }
251
252 if (*p == '\0')
253 return 0;
254
255 if (replace) {
256 strlcpy(arcs_cmdline, p, sizeof(arcs_cmdline));
257 } else {
258 strlcat(arcs_cmdline, " ", sizeof(arcs_cmdline));
259 strlcat(arcs_cmdline, p, sizeof(arcs_cmdline));
260 }
261
262 return 1;
263 }
264 #else
265 static int inline ar71xx_use__image_cmdline(void) { return 0; }
266 #endif
267
268 static __init void ar71xx_prom_init_cmdline(int argc, char **argv)
269 {
270 int i;
271
272 if (ar71xx_use__image_cmdline())
273 return;
274
275 if (!is_valid_ram_addr(argv))
276 return;
277
278 for (i = 0; i < argc; i++)
279 if (is_valid_ram_addr(argv[i])) {
280 strlcat(arcs_cmdline, " ", sizeof(arcs_cmdline));
281 strlcat(arcs_cmdline, argv[i], sizeof(arcs_cmdline));
282 }
283 }
284
285 void __init prom_init(void)
286 {
287 char **envp;
288
289 printk(KERN_DEBUG "prom: fw_arg0=%08x, fw_arg1=%08x, "
290 "fw_arg2=%08x, fw_arg3=%08x\n",
291 (unsigned int)fw_arg0, (unsigned int)fw_arg1,
292 (unsigned int)fw_arg2, (unsigned int)fw_arg3);
293
294
295 ar71xx_mach = AR71XX_MACH_GENERIC;
296
297 if (ar71xx_prom_init_myloader())
298 return;
299
300 ar71xx_prom_init_cmdline(fw_arg0, (char **)fw_arg1);
301
302 envp = (char **)fw_arg2;
303 ar71xx_prom_find_env(envp, "board");
304 ar71xx_prom_find_env(envp, "ethaddr");
305 }
306
307 void __init prom_free_prom_memory(void)
308 {
309 /* We do not have to prom memory to free */
310 }