ar71xx: preliminary Ubiquiti Bullet M support
[openwrt/staging/florian.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 "devices.h"
24
25 struct board_rec {
26 char *name;
27 enum ar71xx_mach_type mach_type;
28 };
29
30 static struct board_rec boards[] __initdata = {
31 {
32 .name = "411",
33 .mach_type = AR71XX_MACH_RB_411,
34 }, {
35 .name = "411U",
36 .mach_type = AR71XX_MACH_RB_411U,
37 }, {
38 .name = "433",
39 .mach_type = AR71XX_MACH_RB_433,
40 }, {
41 .name = "433U",
42 .mach_type = AR71XX_MACH_RB_433U,
43 }, {
44 .name = "450",
45 .mach_type = AR71XX_MACH_RB_450,
46 }, {
47 .name = "450G",
48 .mach_type = AR71XX_MACH_RB_450G,
49 }, {
50 .name = "493",
51 .mach_type = AR71XX_MACH_RB_493,
52 }, {
53 .name = "AP81",
54 .mach_type = AR71XX_MACH_AP81,
55 }, {
56 .name = "AP83",
57 .mach_type = AR71XX_MACH_AP83,
58 }, {
59 .name = "AW-NR580",
60 .mach_type = AR71XX_MACH_AW_NR580,
61 }, {
62 .name = "TEW-632BRP",
63 .mach_type = AR71XX_MACH_TEW_632BRP,
64 }, {
65 .name = "TL-WR741ND",
66 .mach_type = AR71XX_MACH_TL_WR741ND,
67 }, {
68 .name = "TL-WR941ND",
69 .mach_type = AR71XX_MACH_TL_WR941ND,
70 }, {
71 .name = "UBNT-RS",
72 .mach_type = AR71XX_MACH_UBNT_RS,
73 }, {
74 .name = "UBNT-RSPRO",
75 .mach_type = AR71XX_MACH_UBNT_RSPRO,
76 }, {
77 .name = "Ubiquiti AR71xx-based board",
78 .mach_type = AR71XX_MACH_UBNT_RS,
79 }, {
80 .name = "UBNT-LS-SR71",
81 .mach_type = AR71XX_MACH_UBNT_LSSR71,
82 }, {
83 .name = "UBNT-LSX",
84 .mach_type = AR71XX_MACH_UBNT_LSX,
85 }, {
86 .name = "UBNT-BM",
87 .mach_type = AR71XX_MACH_UBNT_BULLET_M,
88 }, {
89 .name = "WNR2000",
90 .mach_type = AR71XX_MACH_WNR2000,
91 }, {
92 .name = "WRT160NL",
93 .mach_type = AR71XX_MACH_WRT160NL,
94 }, {
95 .name = "WP543",
96 .mach_type = AR71XX_MACH_WP543,
97 }, {
98 .name = "WRT400N",
99 .mach_type = AR71XX_MACH_WRT400N,
100 }, {
101 .name = "PB42",
102 .mach_type = AR71XX_MACH_PB42,
103 }, {
104 .name = "PB44",
105 .mach_type = AR71XX_MACH_PB44,
106 }, {
107 .name = "MZK-W300NH",
108 .mach_type = AR71XX_MACH_MZK_W300NH,
109 }, {
110 .name = "MZK-W04NU",
111 .mach_type = AR71XX_MACH_MZK_W04NU,
112 }
113 };
114
115 static int __init ar71xx_board_setup(char *name)
116 {
117 int i;
118
119 for (i = 0; i < ARRAY_SIZE(boards); i++)
120 if (strcmp(name, boards[i].name) == 0) {
121 ar71xx_mach = boards[i].mach_type;
122 break;
123 }
124
125 return 1;
126 }
127 __setup("board=", ar71xx_board_setup);
128
129 static int __init ar71xx_ethaddr_setup(char *str)
130 {
131 ar71xx_parse_mac_addr(str);
132 return 1;
133 }
134 __setup("ethaddr=", ar71xx_ethaddr_setup);
135
136 static int __init ar71xx_kmac_setup(char *str)
137 {
138 ar71xx_parse_mac_addr(str);
139 return 1;
140 }
141 __setup("kmac=", ar71xx_kmac_setup);
142
143 static inline int is_valid_ram_addr(void *addr)
144 {
145 if (((u32) addr > KSEG0) &&
146 ((u32) addr < (KSEG0 + AR71XX_MEM_SIZE_MAX)))
147 return 1;
148
149 if (((u32) addr > KSEG1) &&
150 ((u32) addr < (KSEG1 + AR71XX_MEM_SIZE_MAX)))
151 return 1;
152
153 return 0;
154 }
155
156 static void __init ar71xx_prom_append_cmdline(const char *name,
157 const char *value)
158 {
159 char buf[CL_SIZE];
160
161 snprintf(buf, sizeof(buf), " %s=%s", name, value);
162 strlcat(arcs_cmdline, buf, sizeof(arcs_cmdline));
163 }
164
165 static void __init ar71xx_prom_find_env(char **envp, const char *name)
166 {
167 int len = strlen(name);
168 char **p;
169
170 if (!is_valid_ram_addr(envp))
171 return;
172
173 for (p = envp; is_valid_ram_addr(*p); p++) {
174 if (strncmp(name, *p, len) == 0 && (*p)[len] == '=') {
175 ar71xx_prom_append_cmdline(name, *p + len + 1);
176 break;
177 }
178
179 /* RedBoot env comes in pointer pairs - key, value */
180 if (strncmp(name, *p, len) == 0 && (*p)[len] == 0)
181 if (is_valid_ram_addr(*(++p))) {
182 ar71xx_prom_append_cmdline(name, *p);
183 break;
184 }
185 }
186 }
187
188 static int __init ar71xx_prom_init_myloader(void)
189 {
190 struct myloader_info *mylo;
191 char mac_buf[32];
192 char *mac;
193
194 mylo = myloader_get_info();
195 if (!mylo)
196 return 0;
197
198 switch (mylo->did) {
199 case DEVID_COMPEX_WP543:
200 ar71xx_prom_append_cmdline("board", "WP543");
201 break;
202 default:
203 printk(KERN_WARNING "prom: unknown device id: %x\n",
204 mylo->did);
205 return 0;
206 }
207
208 mac = mylo->macs[0];
209 snprintf(mac_buf, sizeof(mac_buf), "%02x:%02x:%02x:%02x:%02x:%02x",
210 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
211
212 ar71xx_prom_append_cmdline("ethaddr", mac_buf);
213
214 return 1;
215 }
216
217 #ifdef CONFIG_IMAGE_CMDLINE_HACK
218 extern char __image_cmdline[];
219
220 static int __init ar71xx_use__image_cmdline(void)
221 {
222 char *p = __image_cmdline;
223 int replace = 0;
224
225 if (*p == '-') {
226 replace = 1;
227 p++;
228 }
229
230 if (*p == '\0')
231 return 0;
232
233 if (replace) {
234 strlcpy(arcs_cmdline, p, sizeof(arcs_cmdline));
235 } else {
236 strlcat(arcs_cmdline, " ", sizeof(arcs_cmdline));
237 strlcat(arcs_cmdline, p, sizeof(arcs_cmdline));
238 }
239
240 return 1;
241 }
242 #else
243 static int inline ar71xx_use__image_cmdline(void) { return 0; }
244 #endif
245
246 static __init void ar71xx_prom_init_cmdline(int argc, char **argv)
247 {
248 int i;
249
250 if (ar71xx_use__image_cmdline())
251 return;
252
253 if (!is_valid_ram_addr(argv))
254 return;
255
256 for (i = 0; i < argc; i++)
257 if (is_valid_ram_addr(argv[i])) {
258 strlcat(arcs_cmdline, " ", sizeof(arcs_cmdline));
259 strlcat(arcs_cmdline, argv[i], sizeof(arcs_cmdline));
260 }
261 }
262
263 void __init prom_init(void)
264 {
265 char **envp;
266
267 printk(KERN_DEBUG "prom: fw_arg0=%08x, fw_arg1=%08x, "
268 "fw_arg2=%08x, fw_arg3=%08x\n",
269 (unsigned int)fw_arg0, (unsigned int)fw_arg1,
270 (unsigned int)fw_arg2, (unsigned int)fw_arg3);
271
272
273 ar71xx_mach = AR71XX_MACH_GENERIC;
274
275 if (ar71xx_prom_init_myloader())
276 return;
277
278 ar71xx_prom_init_cmdline(fw_arg0, (char **)fw_arg1);
279
280 envp = (char **)fw_arg2;
281 ar71xx_prom_find_env(envp, "board");
282 ar71xx_prom_find_env(envp, "ethaddr");
283 }
284
285 void __init prom_free_prom_memory(void)
286 {
287 /* We do not have to prom memory to free */
288 }