ar5312.c revision 1.6 1 1.6 martin /* $NetBSD: ar5312.c,v 1.6 2010/01/22 08:56:05 martin Exp $ */
2 1.1 gdamore
3 1.1 gdamore /*
4 1.1 gdamore * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
5 1.1 gdamore * Copyright (c) 2006 Garrett D'Amore.
6 1.1 gdamore * All rights reserved.
7 1.1 gdamore *
8 1.1 gdamore * Portions of this code were written by Garrett D'Amore for the
9 1.1 gdamore * Champaign-Urbana Community Wireless Network Project.
10 1.1 gdamore *
11 1.1 gdamore * Redistribution and use in source and binary forms, with or
12 1.1 gdamore * without modification, are permitted provided that the following
13 1.1 gdamore * conditions are met:
14 1.1 gdamore * 1. Redistributions of source code must retain the above copyright
15 1.1 gdamore * notice, this list of conditions and the following disclaimer.
16 1.1 gdamore * 2. Redistributions in binary form must reproduce the above
17 1.1 gdamore * copyright notice, this list of conditions and the following
18 1.1 gdamore * disclaimer in the documentation and/or other materials provided
19 1.1 gdamore * with the distribution.
20 1.1 gdamore * 3. All advertising materials mentioning features or use of this
21 1.1 gdamore * software must display the following acknowledgements:
22 1.1 gdamore * This product includes software developed by the Urbana-Champaign
23 1.1 gdamore * Independent Media Center.
24 1.1 gdamore * This product includes software developed by Garrett D'Amore.
25 1.1 gdamore * 4. Urbana-Champaign Independent Media Center's name and Garrett
26 1.1 gdamore * D'Amore's name may not be used to endorse or promote products
27 1.1 gdamore * derived from this software without specific prior written permission.
28 1.1 gdamore *
29 1.1 gdamore * THIS SOFTWARE IS PROVIDED BY THE URBANA-CHAMPAIGN INDEPENDENT
30 1.1 gdamore * MEDIA CENTER AND GARRETT D'AMORE ``AS IS'' AND ANY EXPRESS OR
31 1.1 gdamore * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 1.1 gdamore * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 1.1 gdamore * ARE DISCLAIMED. IN NO EVENT SHALL THE URBANA-CHAMPAIGN INDEPENDENT
34 1.1 gdamore * MEDIA CENTER OR GARRETT D'AMORE BE LIABLE FOR ANY DIRECT, INDIRECT,
35 1.1 gdamore * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 1.1 gdamore * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 1.1 gdamore * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38 1.1 gdamore * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 1.1 gdamore * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 1.1 gdamore * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41 1.1 gdamore * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 1.1 gdamore */
43 1.1 gdamore
44 1.1 gdamore /*
45 1.1 gdamore * This file includes a bunch of implementation specific bits for
46 1.1 gdamore * AR5312, which differents these from other members of the AR5315
47 1.1 gdamore * family.
48 1.1 gdamore */
49 1.1 gdamore #include "opt_ddb.h"
50 1.1 gdamore #include "opt_kgdb.h"
51 1.1 gdamore
52 1.1 gdamore #include "opt_memsize.h"
53 1.1 gdamore #include <sys/param.h>
54 1.1 gdamore #include <sys/systm.h>
55 1.1 gdamore #include <sys/kernel.h>
56 1.1 gdamore #include <sys/buf.h>
57 1.1 gdamore
58 1.1 gdamore #include <mips/cache.h>
59 1.1 gdamore #include <mips/locore.h>
60 1.1 gdamore #include <mips/cpuregs.h>
61 1.1 gdamore
62 1.2 gdamore #include <sys/socket.h> /* these three just to get ETHER_ADDR_LEN(!) */
63 1.2 gdamore #include <net/if.h>
64 1.2 gdamore #include <net/if_ether.h>
65 1.2 gdamore
66 1.5 dyoung #include <prop/proplib.h>
67 1.5 dyoung
68 1.1 gdamore #include <mips/atheros/include/ar5312reg.h>
69 1.1 gdamore #include <mips/atheros/include/ar531xvar.h>
70 1.1 gdamore #include <mips/atheros/include/arbusvar.h>
71 1.1 gdamore #include "com.h"
72 1.1 gdamore
73 1.1 gdamore uint32_t
74 1.1 gdamore ar531x_memsize(void)
75 1.1 gdamore {
76 1.1 gdamore uint32_t memsize;
77 1.1 gdamore uint32_t memcfg, bank0, bank1;
78 1.1 gdamore
79 1.1 gdamore /*
80 1.2 gdamore * Determine the memory size as established by system
81 1.2 gdamore * firmware.
82 1.1 gdamore *
83 1.1 gdamore * NB: we allow compile time override
84 1.1 gdamore */
85 1.1 gdamore #if defined(MEMSIZE)
86 1.1 gdamore memsize = MEMSIZE;
87 1.1 gdamore #else
88 1.1 gdamore memcfg = GETSDRAMREG(AR5312_SDRAMCTL_MEM_CFG1);
89 1.1 gdamore bank0 = (memcfg & AR5312_MEM_CFG1_BANK0_MASK) >>
90 1.1 gdamore AR5312_MEM_CFG1_BANK0_SHIFT;
91 1.1 gdamore bank1 = (memcfg & AR5312_MEM_CFG1_BANK1_MASK) >>
92 1.1 gdamore AR5312_MEM_CFG1_BANK1_SHIFT;
93 1.1 gdamore
94 1.1 gdamore memsize = (bank0 ? (1 << (bank0 + 1)) : 0) +
95 1.1 gdamore (bank1 ? (1 << (bank1 + 1)) : 0);
96 1.1 gdamore memsize <<= 20;
97 1.1 gdamore #endif
98 1.1 gdamore
99 1.1 gdamore return (memsize);
100 1.1 gdamore }
101 1.1 gdamore
102 1.1 gdamore void
103 1.1 gdamore ar531x_wdog(uint32_t period)
104 1.1 gdamore {
105 1.1 gdamore
106 1.1 gdamore if (period == 0) {
107 1.1 gdamore PUTSYSREG(AR5312_SYSREG_WDOG_CTL, AR5312_WDOG_CTL_IGNORE);
108 1.1 gdamore PUTSYSREG(AR5312_SYSREG_WDOG_TIMER, 0);
109 1.1 gdamore } else {
110 1.1 gdamore PUTSYSREG(AR5312_SYSREG_WDOG_TIMER, period);
111 1.1 gdamore PUTSYSREG(AR5312_SYSREG_WDOG_CTL, AR5312_WDOG_CTL_RESET);
112 1.1 gdamore }
113 1.1 gdamore }
114 1.1 gdamore
115 1.1 gdamore const char *
116 1.1 gdamore ar531x_cpuname(void)
117 1.1 gdamore {
118 1.1 gdamore uint32_t revision;
119 1.1 gdamore
120 1.1 gdamore revision = GETSYSREG(AR5312_SYSREG_REVISION);
121 1.1 gdamore switch (AR5312_REVISION_MAJOR(revision)) {
122 1.1 gdamore case AR5312_REVISION_MAJ_AR5311:
123 1.1 gdamore return ("Atheros AR5311");
124 1.1 gdamore case AR5312_REVISION_MAJ_AR5312:
125 1.1 gdamore return ("Atheros AR5312");
126 1.1 gdamore case AR5312_REVISION_MAJ_AR2313:
127 1.1 gdamore return ("Atheros AR2313");
128 1.1 gdamore case AR5312_REVISION_MAJ_AR5315:
129 1.1 gdamore return ("Atheros AR5315");
130 1.1 gdamore default:
131 1.1 gdamore return ("Atheros AR531X");
132 1.1 gdamore }
133 1.1 gdamore }
134 1.1 gdamore
135 1.1 gdamore void
136 1.1 gdamore ar531x_businit(void)
137 1.1 gdamore
138 1.1 gdamore {
139 1.1 gdamore /*
140 1.1 gdamore * Clear previous AHB errors
141 1.1 gdamore */
142 1.1 gdamore GETSYSREG(AR5312_SYSREG_AHBPERR);
143 1.1 gdamore GETSYSREG(AR5312_SYSREG_AHBDMAE);
144 1.1 gdamore }
145 1.1 gdamore
146 1.1 gdamore uint32_t
147 1.1 gdamore ar531x_cpu_freq(void)
148 1.1 gdamore {
149 1.1 gdamore static uint32_t cpufreq;
150 1.1 gdamore uint32_t wisoc = GETSYSREG(AR5312_SYSREG_REVISION);
151 1.1 gdamore
152 1.1 gdamore uint32_t predivmask;
153 1.1 gdamore uint32_t predivshift;
154 1.1 gdamore uint32_t multmask;
155 1.1 gdamore uint32_t multshift;
156 1.1 gdamore uint32_t doublermask;
157 1.1 gdamore uint32_t divisor;
158 1.1 gdamore uint32_t multiplier;
159 1.1 gdamore uint32_t clockctl;
160 1.1 gdamore
161 1.1 gdamore const int predivide_table[4] = { 1, 2, 4, 5 };
162 1.1 gdamore
163 1.1 gdamore /* XXX: in theory we might be able to get clock from bootrom */
164 1.1 gdamore
165 1.1 gdamore /*
166 1.1 gdamore * This logic looks at the clock control register and
167 1.1 gdamore * determines the actual CPU frequency. These parts lack any
168 1.1 gdamore * kind of real-time clock on them, but the cpu clocks should
169 1.1 gdamore * be very accurate -- WiFi requires usec resolution timers.
170 1.1 gdamore */
171 1.1 gdamore
172 1.1 gdamore if (cpufreq) {
173 1.1 gdamore return cpufreq;
174 1.1 gdamore }
175 1.1 gdamore
176 1.1 gdamore if (AR5312_REVISION_MAJOR(wisoc) == AR5312_REVISION_MAJ_AR2313) {
177 1.1 gdamore predivmask = AR2313_CLOCKCTL_PREDIVIDE_MASK;
178 1.1 gdamore predivshift = AR2313_CLOCKCTL_PREDIVIDE_SHIFT;
179 1.1 gdamore multmask = AR2313_CLOCKCTL_MULTIPLIER_MASK;
180 1.1 gdamore multshift = AR2313_CLOCKCTL_MULTIPLIER_SHIFT;
181 1.1 gdamore doublermask = AR2313_CLOCKCTL_DOUBLER_MASK;
182 1.1 gdamore } else {
183 1.1 gdamore predivmask = AR5312_CLOCKCTL_PREDIVIDE_MASK;
184 1.1 gdamore predivshift = AR5312_CLOCKCTL_PREDIVIDE_SHIFT;
185 1.1 gdamore multmask = AR5312_CLOCKCTL_MULTIPLIER_MASK;
186 1.1 gdamore multshift = AR5312_CLOCKCTL_MULTIPLIER_SHIFT;
187 1.1 gdamore doublermask = AR5312_CLOCKCTL_DOUBLER_MASK;
188 1.1 gdamore }
189 1.1 gdamore
190 1.1 gdamore /*
191 1.1 gdamore * Note that the source clock involved here is a 40MHz.
192 1.1 gdamore */
193 1.1 gdamore
194 1.1 gdamore clockctl = GETSYSREG(AR5312_SYSREG_CLOCKCTL);
195 1.1 gdamore divisor = predivide_table[(clockctl & predivmask) >> predivshift];
196 1.1 gdamore multiplier = (clockctl & multmask) >> multshift;
197 1.1 gdamore
198 1.1 gdamore if (clockctl & doublermask)
199 1.1 gdamore multiplier <<= 1;
200 1.1 gdamore
201 1.1 gdamore cpufreq = (40000000 / divisor) * multiplier;
202 1.1 gdamore
203 1.1 gdamore return (cpufreq);
204 1.1 gdamore }
205 1.1 gdamore
206 1.1 gdamore uint32_t
207 1.2 gdamore ar531x_bus_freq(void)
208 1.1 gdamore {
209 1.1 gdamore return (ar531x_cpu_freq() / 4);
210 1.1 gdamore }
211 1.2 gdamore
212 1.2 gdamore static void
213 1.2 gdamore addprop_data(struct device *dev, const char *name, const uint8_t *data,
214 1.2 gdamore int len)
215 1.2 gdamore {
216 1.2 gdamore prop_data_t pd;
217 1.2 gdamore pd = prop_data_create_data(data, len);
218 1.2 gdamore KASSERT(pd != NULL);
219 1.4 thorpej if (prop_dictionary_set(device_properties(dev), name, pd) == false) {
220 1.2 gdamore printf("WARNING: unable to set %s property for %s\n",
221 1.2 gdamore name, device_xname(dev));
222 1.2 gdamore }
223 1.2 gdamore prop_object_release(pd);
224 1.2 gdamore }
225 1.2 gdamore
226 1.2 gdamore static void
227 1.2 gdamore addprop_integer(struct device *dev, const char *name, uint32_t val)
228 1.2 gdamore {
229 1.2 gdamore prop_number_t pn;
230 1.2 gdamore pn = prop_number_create_integer(val);
231 1.2 gdamore KASSERT(pn != NULL);
232 1.4 thorpej if (prop_dictionary_set(device_properties(dev), name, pn) == false) {
233 1.2 gdamore printf("WARNING: unable to set %s property for %s",
234 1.2 gdamore name, device_xname(dev));
235 1.2 gdamore }
236 1.2 gdamore prop_object_release(pn);
237 1.2 gdamore }
238 1.2 gdamore
239 1.2 gdamore void
240 1.2 gdamore ar531x_device_register(struct device *dev, void *aux)
241 1.2 gdamore {
242 1.2 gdamore struct arbus_attach_args *aa = aux;
243 1.2 gdamore const struct ar531x_boarddata *info;
244 1.2 gdamore
245 1.2 gdamore info = ar531x_board_info();
246 1.2 gdamore if (info == NULL) {
247 1.2 gdamore /* nothing known about this board! */
248 1.2 gdamore return;
249 1.2 gdamore }
250 1.2 gdamore
251 1.2 gdamore /*
252 1.2 gdamore * We don't ever know the boot device. But that's because the
253 1.2 gdamore * firmware only loads from the network.
254 1.2 gdamore */
255 1.2 gdamore
256 1.2 gdamore /* Fetch the MAC addresses. */
257 1.2 gdamore if (device_is_a(dev, "ae")) {
258 1.2 gdamore const uint8_t *enet;
259 1.2 gdamore
260 1.2 gdamore if (aa->aa_addr == AR5312_ENET0_BASE)
261 1.2 gdamore enet = info->enet0Mac;
262 1.2 gdamore else if (aa->aa_addr == AR5312_ENET1_BASE)
263 1.2 gdamore enet = info->enet1Mac;
264 1.2 gdamore else
265 1.2 gdamore return;
266 1.2 gdamore
267 1.6 martin addprop_data(dev, "mac-address", enet, ETHER_ADDR_LEN);
268 1.2 gdamore }
269 1.2 gdamore
270 1.2 gdamore if (device_is_a(dev, "ath")) {
271 1.2 gdamore const uint8_t *enet;
272 1.2 gdamore
273 1.2 gdamore if (aa->aa_addr == AR5312_WLAN0_BASE)
274 1.2 gdamore enet = info->wlan0Mac;
275 1.2 gdamore else if (aa->aa_addr == AR5312_WLAN1_BASE)
276 1.2 gdamore enet = info->wlan1Mac;
277 1.2 gdamore else
278 1.2 gdamore return;
279 1.2 gdamore
280 1.6 martin addprop_data(dev, "mac-address", enet, ETHER_ADDR_LEN);
281 1.3 gdamore
282 1.3 gdamore addprop_integer(dev, "wmac-rev",
283 1.3 gdamore AR5312_REVISION_WMAC(GETSYSREG(AR5312_SYSREG_REVISION)));
284 1.3 gdamore
285 1.2 gdamore }
286 1.2 gdamore
287 1.2 gdamore if (device_is_a(dev, "com")) {
288 1.2 gdamore addprop_integer(dev, "frequency", ar531x_cpu_freq() / 4);
289 1.2 gdamore }
290 1.2 gdamore
291 1.2 gdamore if (device_is_a(dev, "argpio")) {
292 1.2 gdamore if (info->config & BD_RSTFACTORY) {
293 1.2 gdamore addprop_integer(dev, "reset-pin",
294 1.2 gdamore info->resetConfigGpio);
295 1.2 gdamore }
296 1.2 gdamore if (info->config & BD_SYSLED) {
297 1.2 gdamore addprop_integer(dev, "sysled-pin",
298 1.2 gdamore info->sysLedGpio);
299 1.2 gdamore }
300 1.2 gdamore }
301 1.2 gdamore }
302 1.2 gdamore
303 1.2 gdamore int
304 1.2 gdamore ar531x_enable_device(const struct ar531x_device *dev)
305 1.2 gdamore {
306 1.2 gdamore const struct ar531x_boarddata *info;
307 1.2 gdamore
308 1.2 gdamore info = ar531x_board_info();
309 1.2 gdamore if (dev->mask && ((dev->mask & info->config) == 0)) {
310 1.2 gdamore return -1;
311 1.2 gdamore }
312 1.2 gdamore if (dev->reset) {
313 1.2 gdamore /* put device into reset */
314 1.2 gdamore PUTSYSREG(AR5312_SYSREG_RESETCTL,
315 1.2 gdamore GETSYSREG(AR5312_SYSREG_RESETCTL) | dev->reset);
316 1.2 gdamore
317 1.2 gdamore delay(15000); /* XXX: tsleep? */
318 1.2 gdamore
319 1.2 gdamore /* take it out of reset */
320 1.2 gdamore PUTSYSREG(AR5312_SYSREG_RESETCTL,
321 1.2 gdamore GETSYSREG(AR5312_SYSREG_RESETCTL) & ~dev->reset);
322 1.2 gdamore
323 1.2 gdamore delay(25);
324 1.2 gdamore }
325 1.2 gdamore if (dev->enable) {
326 1.2 gdamore PUTSYSREG(AR5312_SYSREG_ENABLE,
327 1.2 gdamore GETSYSREG(AR5312_SYSREG_ENABLE) | dev->enable);
328 1.2 gdamore }
329 1.2 gdamore return 0;
330 1.2 gdamore }
331 1.2 gdamore
332 1.2 gdamore const struct ar531x_device *
333 1.2 gdamore ar531x_get_devices(void)
334 1.2 gdamore {
335 1.2 gdamore static const struct ar531x_device devices[] = {
336 1.2 gdamore {
337 1.2 gdamore "ae",
338 1.2 gdamore AR5312_ENET0_BASE, 0x100000,
339 1.2 gdamore AR5312_IRQ_ENET0, -1,
340 1.2 gdamore AR5312_BOARD_CONFIG_ENET0,
341 1.2 gdamore AR5312_RESET_ENET0 | AR5312_RESET_PHY0,
342 1.2 gdamore AR5312_ENABLE_ENET0
343 1.2 gdamore },
344 1.2 gdamore {
345 1.2 gdamore "ae",
346 1.2 gdamore AR5312_ENET1_BASE, 0x100000,
347 1.2 gdamore AR5312_IRQ_ENET1, -1,
348 1.2 gdamore AR5312_BOARD_CONFIG_ENET1,
349 1.2 gdamore AR5312_RESET_ENET1 | AR5312_RESET_PHY1,
350 1.2 gdamore AR5312_ENABLE_ENET1
351 1.2 gdamore },
352 1.2 gdamore {
353 1.2 gdamore "com",
354 1.2 gdamore AR5312_UART0_BASE, 0x1000,
355 1.2 gdamore AR5312_IRQ_MISC, AR5312_MISC_IRQ_UART0,
356 1.2 gdamore AR5312_BOARD_CONFIG_UART0,
357 1.2 gdamore 0,
358 1.2 gdamore 0,
359 1.2 gdamore },
360 1.2 gdamore {
361 1.2 gdamore "com",
362 1.2 gdamore AR5312_UART1_BASE, 0x1000,
363 1.2 gdamore -1, -1,
364 1.2 gdamore AR5312_BOARD_CONFIG_UART1,
365 1.2 gdamore 0,
366 1.2 gdamore 0,
367 1.2 gdamore },
368 1.2 gdamore {
369 1.2 gdamore "ath",
370 1.2 gdamore AR5312_WLAN0_BASE, 0x100000,
371 1.2 gdamore AR5312_IRQ_WLAN0, -1,
372 1.2 gdamore AR5312_BOARD_CONFIG_WLAN0,
373 1.2 gdamore AR5312_RESET_WLAN0 |
374 1.2 gdamore AR5312_RESET_WARM_WLAN0_MAC |
375 1.2 gdamore AR5312_RESET_WARM_WLAN0_BB,
376 1.2 gdamore AR5312_ENABLE_WLAN0
377 1.2 gdamore },
378 1.2 gdamore {
379 1.2 gdamore "ath",
380 1.2 gdamore AR5312_WLAN1_BASE, 0x100000,
381 1.2 gdamore AR5312_IRQ_WLAN1, -1,
382 1.2 gdamore AR5312_BOARD_CONFIG_WLAN1,
383 1.2 gdamore AR5312_RESET_WLAN1 |
384 1.2 gdamore AR5312_RESET_WARM_WLAN1_MAC |
385 1.2 gdamore AR5312_RESET_WARM_WLAN1_BB,
386 1.2 gdamore AR5312_ENABLE_WLAN1
387 1.2 gdamore },
388 1.2 gdamore {
389 1.2 gdamore "athflash",
390 1.2 gdamore AR5312_FLASH_BASE, 0,
391 1.2 gdamore -1, -1,
392 1.2 gdamore 0,
393 1.2 gdamore 0,
394 1.2 gdamore 0,
395 1.2 gdamore },
396 1.2 gdamore {
397 1.2 gdamore "argpio", 0x1000,
398 1.2 gdamore AR5312_GPIO_BASE,
399 1.2 gdamore AR5312_IRQ_MISC, AR5312_MISC_IRQ_GPIO,
400 1.2 gdamore 0,
401 1.2 gdamore 0,
402 1.2 gdamore 0
403 1.2 gdamore },
404 1.2 gdamore { NULL }
405 1.2 gdamore };
406 1.2 gdamore
407 1.2 gdamore return devices;
408 1.2 gdamore }
409 1.2 gdamore
410