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