[ar71xx] preliminary support for the Atheros AP81 refrence board
[openwrt/svn-archive/archive.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/serial_reg.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 unsigned long mach_type;
28 };
29
30 static int ar71xx_prom_argc __initdata;
31 static char **ar71xx_prom_argv __initdata;
32 static char **ar71xx_prom_envp __initdata;
33
34 static struct board_rec boards[] __initdata = {
35 {
36 .name = "411",
37 .mach_type = AR71XX_MACH_RB_411,
38 }, {
39 .name = "433",
40 .mach_type = AR71XX_MACH_RB_433,
41 }, {
42 .name = "450",
43 .mach_type = AR71XX_MACH_RB_450,
44 }, {
45 .name = "493",
46 .mach_type = AR71XX_MACH_RB_493,
47 }, {
48 .name = "AP81",
49 .mach_type = AR71XX_MACH_AP81,
50 }, {
51 .name = "AP83",
52 .mach_type = AR71XX_MACH_AP83,
53 }, {
54 .name = "AW-NR580",
55 .mach_type = AR71XX_MACH_AW_NR580,
56 }, {
57 .name = "TEW-632BRP",
58 .mach_type = AR71XX_MACH_TEW_632BRP,
59 }, {
60 .name = "TL-WR941ND",
61 .mach_type = AR71XX_MACH_TL_WR941ND,
62 }, {
63 .name = "UBNT-RS",
64 .mach_type = AR71XX_MACH_UBNT_RS,
65 }, {
66 .name = "UBNT-RSPRO",
67 .mach_type = AR71XX_MACH_UBNT_RSPRO,
68 }, {
69 .name = "Ubiquiti AR71xx-based board",
70 .mach_type = AR71XX_MACH_UBNT_RS,
71 }, {
72 .name = "UBNT-LS-SR71",
73 .mach_type = AR71XX_MACH_UBNT_LSSR71,
74 }, {
75 .name = "UBNT-LSX",
76 .mach_type = AR71XX_MACH_UBNT_LSX,
77 }, {
78 .name = "WNR2000",
79 .mach_type = AR71XX_MACH_WNR2000,
80 }, {
81 .name = "PB42",
82 .mach_type = AR71XX_MACH_PB42,
83 }, {
84 .name = "MZK-W300NH",
85 .mach_type = AR71XX_MACH_MZK_W300NH,
86 }, {
87 .name = "MZK-W04NU",
88 .mach_type = AR71XX_MACH_MZK_W04NU,
89 }
90 };
91
92 static inline int is_valid_ram_addr(void *addr)
93 {
94 if (((u32) addr > KSEG0) &&
95 ((u32) addr < (KSEG0 + AR71XX_MEM_SIZE_MAX)))
96 return 1;
97
98 if (((u32) addr > KSEG1) &&
99 ((u32) addr < (KSEG1 + AR71XX_MEM_SIZE_MAX)))
100 return 1;
101
102 return 0;
103 }
104
105 static __init char *ar71xx_prom_getargv(const char *name)
106 {
107 int len = strlen(name);
108 int i;
109
110 if (!is_valid_ram_addr(ar71xx_prom_argv))
111 return NULL;
112
113 for (i = 0; i < ar71xx_prom_argc; i++) {
114 char *argv = ar71xx_prom_argv[i];
115
116 if (!is_valid_ram_addr(argv))
117 continue;
118
119 if (strncmp(name, argv, len) == 0 && (argv)[len] == '=')
120 return argv + len + 1;
121 }
122
123 return NULL;
124 }
125
126 static __init char *ar71xx_prom_getenv(const char *envname)
127 {
128 int len = strlen(envname);
129 char **env;
130
131 if (!is_valid_ram_addr(ar71xx_prom_envp))
132 return NULL;
133
134 for (env = ar71xx_prom_envp; is_valid_ram_addr(*env); env++) {
135 if (strncmp(envname, *env, len) == 0 && (*env)[len] == '=')
136 return *env + len + 1;
137
138 /* RedBoot env comes in pointer pairs - key, value */
139 if (strncmp(envname, *env, len) == 0 && (*env)[len] == 0)
140 if (is_valid_ram_addr(*(++env)))
141 return *env;
142 }
143
144 return NULL;
145 }
146
147 static __init unsigned long find_board_byname(char *name)
148 {
149 int i;
150
151 for (i = 0; i < ARRAY_SIZE(boards); i++)
152 if (strcmp(name, boards[i].name) == 0)
153 return boards[i].mach_type;
154
155 return AR71XX_MACH_GENERIC;
156 }
157
158 static int ar71xx_prom_init_myloader(void)
159 {
160 struct myloader_info *mylo;
161
162 mylo = myloader_get_info();
163 if (!mylo)
164 return 0;
165
166 switch (mylo->did) {
167 case DEVID_COMPEX_WP543:
168 ar71xx_mach_type = AR71XX_MACH_WP543;
169 break;
170 default:
171 printk(KERN_WARNING "prom: unknown device id: %x\n",
172 mylo->did);
173 }
174 ar71xx_set_mac_base(mylo->macs[0]);
175
176 return 1;
177 }
178
179 static void ar71xx_prom_init_generic(void)
180 {
181 char *p;
182
183 ar71xx_prom_argc = fw_arg0;
184 ar71xx_prom_argv = (char **)fw_arg1;
185 ar71xx_prom_envp = (char **)fw_arg2;
186
187 p = ar71xx_prom_getargv("board");
188 if (!p)
189 p = ar71xx_prom_getenv("board");
190 if (p)
191 ar71xx_mach_type = find_board_byname(p);
192
193 p = ar71xx_prom_getenv("ethaddr");
194 if (!p)
195 p = ar71xx_prom_getargv("kmac");
196 if (p)
197 ar71xx_parse_mac_addr(p);
198 }
199
200 void __init prom_init(void)
201 {
202 printk(KERN_DEBUG "prom: fw_arg0=%08x, fw_arg1=%08x, "
203 "fw_arg2=%08x, fw_arg3=%08x\n",
204 (unsigned int)fw_arg0, (unsigned int)fw_arg1,
205 (unsigned int)fw_arg2, (unsigned int)fw_arg3);
206
207 ar71xx_mach_type = AR71XX_MACH_GENERIC;
208
209 if (ar71xx_prom_init_myloader())
210 return;
211
212 ar71xx_prom_init_generic();
213 }
214
215 void __init prom_free_prom_memory(void)
216 {
217 /* We do not have to prom memory to free */
218 }
219
220 #define UART_READ(r) \
221 __raw_readl((void __iomem *)(KSEG1ADDR(AR71XX_UART_BASE) + 4 * (r)))
222
223 #define UART_WRITE(r, v) \
224 __raw_writel((v), (void __iomem *)(KSEG1ADDR(AR71XX_UART_BASE) + 4*(r)))
225
226 void prom_putchar(unsigned char ch)
227 {
228 while (((UART_READ(UART_LSR)) & UART_LSR_THRE) == 0);
229 UART_WRITE(UART_TX, ch);
230 while (((UART_READ(UART_LSR)) & UART_LSR_THRE) == 0);
231 }