b38b48ba3bd102e304a1d87a4c218e837e624dad
[openwrt.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           = "DIR-825-B1",
63                 .mach_type      = AR71XX_MACH_DIR_825_B1,
64         }, {
65                 .name           = "TEW-632BRP",
66                 .mach_type      = AR71XX_MACH_TEW_632BRP,
67         }, {
68                 .name           = "DIR-615-C1",
69                 .mach_type      = AR71XX_MACH_DIR_615_C1,
70         }, {
71                 .name           = "TL-WR741ND",
72                 .mach_type      = AR71XX_MACH_TL_WR741ND,
73         }, {
74                 .name           = "TL-WR841N-v1.5",
75                 .mach_type      = AR71XX_MACH_TL_WR841N_V1,
76         }, {
77                 .name           = "TL-WR941ND",
78                 .mach_type      = AR71XX_MACH_TL_WR941ND,
79         }, {
80                 .name           = "TL-WR1043ND",
81                 .mach_type      = AR71XX_MACH_TL_WR1043ND,
82         }, {
83                 .name           = "UBNT-RS",
84                 .mach_type      = AR71XX_MACH_UBNT_RS,
85         }, {
86                 .name           = "UBNT-RSPRO",
87                 .mach_type      = AR71XX_MACH_UBNT_RSPRO,
88         }, {
89                 .name           = "Ubiquiti AR71xx-based board",
90                 .mach_type      = AR71XX_MACH_UBNT_RS,
91         }, {
92                 .name           = "UBNT-LS-SR71",
93                 .mach_type      = AR71XX_MACH_UBNT_LSSR71,
94         }, {
95                 .name           = "UBNT-LSX",
96                 .mach_type      = AR71XX_MACH_UBNT_LSX,
97         }, {
98                 .name           = "UBNT-BM",
99                 .mach_type      = AR71XX_MACH_UBNT_BULLET_M,
100         }, {
101                 .name           = "UBNT-RM",
102                 .mach_type      = AR71XX_MACH_UBNT_ROCKET_M,
103         }, {
104                 .name           = "UBNT-NM",
105                 .mach_type      = AR71XX_MACH_UBNT_NANO_M,
106         }, {
107                 .name           = "WNDR3700",
108                 .mach_type      = AR71XX_MACH_WNDR3700,
109         }, {
110                 .name           = "WNR2000",
111                 .mach_type      = AR71XX_MACH_WNR2000,
112         }, {
113                 .name           = "WRT160NL",
114                 .mach_type      = AR71XX_MACH_WRT160NL,
115         }, {
116                 .name           = "WP543",
117                 .mach_type      = AR71XX_MACH_WP543,
118         }, {
119                 .name           = "WRT400N",
120                 .mach_type      = AR71XX_MACH_WRT400N,
121         }, {
122                 .name           = "PB42",
123                 .mach_type      = AR71XX_MACH_PB42,
124         }, {
125                 .name           = "PB44",
126                 .mach_type      = AR71XX_MACH_PB44,
127         }, {
128                 .name           = "MZK-W300NH",
129                 .mach_type      = AR71XX_MACH_MZK_W300NH,
130         }, {
131                 .name           = "MZK-W04NU",
132                 .mach_type      = AR71XX_MACH_MZK_W04NU,
133         }
134 };
135
136 static int __init ar71xx_board_setup(char *name)
137 {
138         int i;
139
140         for (i = 0; i < ARRAY_SIZE(boards); i++)
141                 if (strcmp(name, boards[i].name) == 0) {
142                         ar71xx_mach = boards[i].mach_type;
143                         break;
144                 }
145
146         return 1;
147 }
148 __setup("board=", ar71xx_board_setup);
149
150 static int __init ar71xx_ethaddr_setup(char *str)
151 {
152         ar71xx_parse_mac_addr(str);
153         return 1;
154 }
155 __setup("ethaddr=", ar71xx_ethaddr_setup);
156
157 static int __init ar71xx_kmac_setup(char *str)
158 {
159         ar71xx_parse_mac_addr(str);
160         return 1;
161 }
162 __setup("kmac=", ar71xx_kmac_setup);
163
164 static inline int is_valid_ram_addr(void *addr)
165 {
166         if (((u32) addr > KSEG0) &&
167             ((u32) addr < (KSEG0 + AR71XX_MEM_SIZE_MAX)))
168                 return 1;
169
170         if (((u32) addr > KSEG1) &&
171             ((u32) addr < (KSEG1 + AR71XX_MEM_SIZE_MAX)))
172                 return 1;
173
174         return 0;
175 }
176
177 static void __init ar71xx_prom_append_cmdline(const char *name,
178                                               const char *value)
179 {
180         char buf[CL_SIZE];
181
182         snprintf(buf, sizeof(buf), " %s=%s", name, value);
183         strlcat(arcs_cmdline, buf, sizeof(arcs_cmdline));
184 }
185
186 static void __init ar71xx_prom_find_env(char **envp, const char *name)
187 {
188         int len = strlen(name);
189         char **p;
190
191         if (!is_valid_ram_addr(envp))
192                 return;
193
194         for (p = envp; is_valid_ram_addr(*p); p++) {
195                 if (strncmp(name, *p, len) == 0 && (*p)[len] == '=') {
196                         ar71xx_prom_append_cmdline(name, *p + len + 1);
197                         break;
198                 }
199
200                 /* RedBoot env comes in pointer pairs - key, value */
201                 if (strncmp(name, *p, len) == 0 && (*p)[len] == 0)
202                         if (is_valid_ram_addr(*(++p))) {
203                                 ar71xx_prom_append_cmdline(name, *p);
204                                 break;
205                         }
206         }
207 }
208
209 static int __init ar71xx_prom_init_myloader(void)
210 {
211         struct myloader_info *mylo;
212         char mac_buf[32];
213         char *mac;
214
215         mylo = myloader_get_info();
216         if (!mylo)
217                 return 0;
218
219         switch (mylo->did) {
220         case DEVID_COMPEX_WP543:
221                 ar71xx_prom_append_cmdline("board", "WP543");
222                 break;
223         default:
224                 printk(KERN_WARNING "prom: unknown device id: %x\n",
225                                 mylo->did);
226                 return 0;
227         }
228
229         mac = mylo->macs[0];
230         snprintf(mac_buf, sizeof(mac_buf), "%02x:%02x:%02x:%02x:%02x:%02x",
231                  mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
232
233         ar71xx_prom_append_cmdline("ethaddr", mac_buf);
234
235         return 1;
236 }
237
238 #ifdef CONFIG_IMAGE_CMDLINE_HACK
239 extern char __image_cmdline[];
240
241 static int __init ar71xx_use__image_cmdline(void)
242 {
243         char *p = __image_cmdline;
244         int replace = 0;
245
246         if (*p == '-') {
247                 replace = 1;
248                 p++;
249         }
250
251         if (*p == '\0')
252                 return 0;
253
254         if (replace) {
255                 strlcpy(arcs_cmdline, p, sizeof(arcs_cmdline));
256         } else {
257                 strlcat(arcs_cmdline, " ", sizeof(arcs_cmdline));
258                 strlcat(arcs_cmdline, p, sizeof(arcs_cmdline));
259         }
260
261         return 1;
262 }
263 #else
264 static int inline ar71xx_use__image_cmdline(void) { return 0; }
265 #endif
266
267 static __init void ar71xx_prom_init_cmdline(int argc, char **argv)
268 {
269         int i;
270
271         if (ar71xx_use__image_cmdline())
272                 return;
273
274         if (!is_valid_ram_addr(argv))
275                 return;
276
277         for (i = 0; i < argc; i++)
278                 if (is_valid_ram_addr(argv[i])) {
279                         strlcat(arcs_cmdline, " ", sizeof(arcs_cmdline));
280                         strlcat(arcs_cmdline, argv[i], sizeof(arcs_cmdline));
281                 }
282 }
283
284 void __init prom_init(void)
285 {
286         char **envp;
287
288         printk(KERN_DEBUG "prom: fw_arg0=%08x, fw_arg1=%08x, "
289                         "fw_arg2=%08x, fw_arg3=%08x\n",
290                         (unsigned int)fw_arg0, (unsigned int)fw_arg1,
291                         (unsigned int)fw_arg2, (unsigned int)fw_arg3);
292
293
294         ar71xx_mach = AR71XX_MACH_GENERIC;
295
296         if (ar71xx_prom_init_myloader())
297                 return;
298
299         ar71xx_prom_init_cmdline(fw_arg0, (char **)fw_arg1);
300
301         envp = (char **)fw_arg2;
302         ar71xx_prom_find_env(envp, "board");
303         ar71xx_prom_find_env(envp, "ethaddr");
304 }
305
306 void __init prom_free_prom_memory(void)
307 {
308         /* We do not have to prom memory to free */
309 }