ar5312.c revision 1.7 1 1.7 matt /* $NetBSD: ar5312.c,v 1.7 2010/12/15 00:06:47 matt 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.7 matt #include <sys/device.h>
56 1.1 gdamore #include <sys/kernel.h>
57 1.1 gdamore #include <sys/buf.h>
58 1.1 gdamore
59 1.1 gdamore #include <mips/cache.h>
60 1.1 gdamore #include <mips/locore.h>
61 1.1 gdamore #include <mips/cpuregs.h>
62 1.1 gdamore
63 1.2 gdamore #include <sys/socket.h> /* these three just to get ETHER_ADDR_LEN(!) */
64 1.2 gdamore #include <net/if.h>
65 1.2 gdamore #include <net/if_ether.h>
66 1.2 gdamore
67 1.5 dyoung #include <prop/proplib.h>
68 1.5 dyoung
69 1.1 gdamore #include <mips/atheros/include/ar5312reg.h>
70 1.1 gdamore #include <mips/atheros/include/ar531xvar.h>
71 1.1 gdamore #include <mips/atheros/include/arbusvar.h>
72 1.1 gdamore #include "com.h"
73 1.1 gdamore
74 1.1 gdamore uint32_t
75 1.1 gdamore ar531x_memsize(void)
76 1.1 gdamore {
77 1.1 gdamore uint32_t memsize;
78 1.1 gdamore uint32_t memcfg, bank0, bank1;
79 1.1 gdamore
80 1.1 gdamore /*
81 1.2 gdamore * Determine the memory size as established by system
82 1.2 gdamore * firmware.
83 1.1 gdamore *
84 1.1 gdamore * NB: we allow compile time override
85 1.1 gdamore */
86 1.1 gdamore #if defined(MEMSIZE)
87 1.1 gdamore memsize = MEMSIZE;
88 1.1 gdamore #else
89 1.1 gdamore memcfg = GETSDRAMREG(AR5312_SDRAMCTL_MEM_CFG1);
90 1.1 gdamore bank0 = (memcfg & AR5312_MEM_CFG1_BANK0_MASK) >>
91 1.1 gdamore AR5312_MEM_CFG1_BANK0_SHIFT;
92 1.1 gdamore bank1 = (memcfg & AR5312_MEM_CFG1_BANK1_MASK) >>
93 1.1 gdamore AR5312_MEM_CFG1_BANK1_SHIFT;
94 1.1 gdamore
95 1.1 gdamore memsize = (bank0 ? (1 << (bank0 + 1)) : 0) +
96 1.1 gdamore (bank1 ? (1 << (bank1 + 1)) : 0);
97 1.1 gdamore memsize <<= 20;
98 1.1 gdamore #endif
99 1.1 gdamore
100 1.1 gdamore return (memsize);
101 1.1 gdamore }
102 1.1 gdamore
103 1.1 gdamore void
104 1.1 gdamore ar531x_wdog(uint32_t period)
105 1.1 gdamore {
106 1.1 gdamore
107 1.1 gdamore if (period == 0) {
108 1.1 gdamore PUTSYSREG(AR5312_SYSREG_WDOG_CTL, AR5312_WDOG_CTL_IGNORE);
109 1.1 gdamore PUTSYSREG(AR5312_SYSREG_WDOG_TIMER, 0);
110 1.1 gdamore } else {
111 1.1 gdamore PUTSYSREG(AR5312_SYSREG_WDOG_TIMER, period);
112 1.1 gdamore PUTSYSREG(AR5312_SYSREG_WDOG_CTL, AR5312_WDOG_CTL_RESET);
113 1.1 gdamore }
114 1.1 gdamore }
115 1.1 gdamore
116 1.1 gdamore const char *
117 1.1 gdamore ar531x_cpuname(void)
118 1.1 gdamore {
119 1.1 gdamore uint32_t revision;
120 1.1 gdamore
121 1.1 gdamore revision = GETSYSREG(AR5312_SYSREG_REVISION);
122 1.1 gdamore switch (AR5312_REVISION_MAJOR(revision)) {
123 1.1 gdamore case AR5312_REVISION_MAJ_AR5311:
124 1.1 gdamore return ("Atheros AR5311");
125 1.1 gdamore case AR5312_REVISION_MAJ_AR5312:
126 1.1 gdamore return ("Atheros AR5312");
127 1.1 gdamore case AR5312_REVISION_MAJ_AR2313:
128 1.1 gdamore return ("Atheros AR2313");
129 1.1 gdamore case AR5312_REVISION_MAJ_AR5315:
130 1.1 gdamore return ("Atheros AR5315");
131 1.1 gdamore default:
132 1.1 gdamore return ("Atheros AR531X");
133 1.1 gdamore }
134 1.1 gdamore }
135 1.1 gdamore
136 1.1 gdamore void
137 1.1 gdamore ar531x_businit(void)
138 1.1 gdamore
139 1.1 gdamore {
140 1.1 gdamore /*
141 1.1 gdamore * Clear previous AHB errors
142 1.1 gdamore */
143 1.1 gdamore GETSYSREG(AR5312_SYSREG_AHBPERR);
144 1.1 gdamore GETSYSREG(AR5312_SYSREG_AHBDMAE);
145 1.1 gdamore }
146 1.1 gdamore
147 1.1 gdamore uint32_t
148 1.1 gdamore ar531x_cpu_freq(void)
149 1.1 gdamore {
150 1.1 gdamore static uint32_t cpufreq;
151 1.1 gdamore uint32_t wisoc = GETSYSREG(AR5312_SYSREG_REVISION);
152 1.1 gdamore
153 1.1 gdamore uint32_t predivmask;
154 1.1 gdamore uint32_t predivshift;
155 1.1 gdamore uint32_t multmask;
156 1.1 gdamore uint32_t multshift;
157 1.1 gdamore uint32_t doublermask;
158 1.1 gdamore uint32_t divisor;
159 1.1 gdamore uint32_t multiplier;
160 1.1 gdamore uint32_t clockctl;
161 1.1 gdamore
162 1.1 gdamore const int predivide_table[4] = { 1, 2, 4, 5 };
163 1.1 gdamore
164 1.1 gdamore /* XXX: in theory we might be able to get clock from bootrom */
165 1.1 gdamore
166 1.1 gdamore /*
167 1.1 gdamore * This logic looks at the clock control register and
168 1.1 gdamore * determines the actual CPU frequency. These parts lack any
169 1.1 gdamore * kind of real-time clock on them, but the cpu clocks should
170 1.1 gdamore * be very accurate -- WiFi requires usec resolution timers.
171 1.1 gdamore */
172 1.1 gdamore
173 1.1 gdamore if (cpufreq) {
174 1.1 gdamore return cpufreq;
175 1.1 gdamore }
176 1.1 gdamore
177 1.1 gdamore if (AR5312_REVISION_MAJOR(wisoc) == AR5312_REVISION_MAJ_AR2313) {
178 1.1 gdamore predivmask = AR2313_CLOCKCTL_PREDIVIDE_MASK;
179 1.1 gdamore predivshift = AR2313_CLOCKCTL_PREDIVIDE_SHIFT;
180 1.1 gdamore multmask = AR2313_CLOCKCTL_MULTIPLIER_MASK;
181 1.1 gdamore multshift = AR2313_CLOCKCTL_MULTIPLIER_SHIFT;
182 1.1 gdamore doublermask = AR2313_CLOCKCTL_DOUBLER_MASK;
183 1.1 gdamore } else {
184 1.1 gdamore predivmask = AR5312_CLOCKCTL_PREDIVIDE_MASK;
185 1.1 gdamore predivshift = AR5312_CLOCKCTL_PREDIVIDE_SHIFT;
186 1.1 gdamore multmask = AR5312_CLOCKCTL_MULTIPLIER_MASK;
187 1.1 gdamore multshift = AR5312_CLOCKCTL_MULTIPLIER_SHIFT;
188 1.1 gdamore doublermask = AR5312_CLOCKCTL_DOUBLER_MASK;
189 1.1 gdamore }
190 1.1 gdamore
191 1.1 gdamore /*
192 1.1 gdamore * Note that the source clock involved here is a 40MHz.
193 1.1 gdamore */
194 1.1 gdamore
195 1.1 gdamore clockctl = GETSYSREG(AR5312_SYSREG_CLOCKCTL);
196 1.1 gdamore divisor = predivide_table[(clockctl & predivmask) >> predivshift];
197 1.1 gdamore multiplier = (clockctl & multmask) >> multshift;
198 1.1 gdamore
199 1.1 gdamore if (clockctl & doublermask)
200 1.1 gdamore multiplier <<= 1;
201 1.1 gdamore
202 1.1 gdamore cpufreq = (40000000 / divisor) * multiplier;
203 1.1 gdamore
204 1.1 gdamore return (cpufreq);
205 1.1 gdamore }
206 1.1 gdamore
207 1.1 gdamore uint32_t
208 1.2 gdamore ar531x_bus_freq(void)
209 1.1 gdamore {
210 1.1 gdamore return (ar531x_cpu_freq() / 4);
211 1.1 gdamore }
212 1.2 gdamore
213 1.2 gdamore static void
214 1.2 gdamore addprop_data(struct device *dev, const char *name, const uint8_t *data,
215 1.2 gdamore int len)
216 1.2 gdamore {
217 1.2 gdamore prop_data_t pd;
218 1.2 gdamore pd = prop_data_create_data(data, len);
219 1.2 gdamore KASSERT(pd != NULL);
220 1.4 thorpej if (prop_dictionary_set(device_properties(dev), name, pd) == false) {
221 1.2 gdamore printf("WARNING: unable to set %s property for %s\n",
222 1.2 gdamore name, device_xname(dev));
223 1.2 gdamore }
224 1.2 gdamore prop_object_release(pd);
225 1.2 gdamore }
226 1.2 gdamore
227 1.2 gdamore static void
228 1.2 gdamore addprop_integer(struct device *dev, const char *name, uint32_t val)
229 1.2 gdamore {
230 1.2 gdamore prop_number_t pn;
231 1.2 gdamore pn = prop_number_create_integer(val);
232 1.2 gdamore KASSERT(pn != NULL);
233 1.4 thorpej if (prop_dictionary_set(device_properties(dev), name, pn) == false) {
234 1.2 gdamore printf("WARNING: unable to set %s property for %s",
235 1.2 gdamore name, device_xname(dev));
236 1.2 gdamore }
237 1.2 gdamore prop_object_release(pn);
238 1.2 gdamore }
239 1.2 gdamore
240 1.2 gdamore void
241 1.2 gdamore ar531x_device_register(struct device *dev, void *aux)
242 1.2 gdamore {
243 1.2 gdamore struct arbus_attach_args *aa = aux;
244 1.2 gdamore const struct ar531x_boarddata *info;
245 1.2 gdamore
246 1.2 gdamore info = ar531x_board_info();
247 1.2 gdamore if (info == NULL) {
248 1.2 gdamore /* nothing known about this board! */
249 1.2 gdamore return;
250 1.2 gdamore }
251 1.2 gdamore
252 1.2 gdamore /*
253 1.2 gdamore * We don't ever know the boot device. But that's because the
254 1.2 gdamore * firmware only loads from the network.
255 1.2 gdamore */
256 1.2 gdamore
257 1.2 gdamore /* Fetch the MAC addresses. */
258 1.2 gdamore if (device_is_a(dev, "ae")) {
259 1.2 gdamore const uint8_t *enet;
260 1.2 gdamore
261 1.2 gdamore if (aa->aa_addr == AR5312_ENET0_BASE)
262 1.2 gdamore enet = info->enet0Mac;
263 1.2 gdamore else if (aa->aa_addr == AR5312_ENET1_BASE)
264 1.2 gdamore enet = info->enet1Mac;
265 1.2 gdamore else
266 1.2 gdamore return;
267 1.2 gdamore
268 1.6 martin addprop_data(dev, "mac-address", enet, ETHER_ADDR_LEN);
269 1.2 gdamore }
270 1.2 gdamore
271 1.2 gdamore if (device_is_a(dev, "ath")) {
272 1.2 gdamore const uint8_t *enet;
273 1.2 gdamore
274 1.2 gdamore if (aa->aa_addr == AR5312_WLAN0_BASE)
275 1.2 gdamore enet = info->wlan0Mac;
276 1.2 gdamore else if (aa->aa_addr == AR5312_WLAN1_BASE)
277 1.2 gdamore enet = info->wlan1Mac;
278 1.2 gdamore else
279 1.2 gdamore return;
280 1.2 gdamore
281 1.6 martin addprop_data(dev, "mac-address", enet, ETHER_ADDR_LEN);
282 1.3 gdamore
283 1.3 gdamore addprop_integer(dev, "wmac-rev",
284 1.3 gdamore AR5312_REVISION_WMAC(GETSYSREG(AR5312_SYSREG_REVISION)));
285 1.3 gdamore
286 1.2 gdamore }
287 1.2 gdamore
288 1.2 gdamore if (device_is_a(dev, "com")) {
289 1.2 gdamore addprop_integer(dev, "frequency", ar531x_cpu_freq() / 4);
290 1.2 gdamore }
291 1.2 gdamore
292 1.2 gdamore if (device_is_a(dev, "argpio")) {
293 1.2 gdamore if (info->config & BD_RSTFACTORY) {
294 1.2 gdamore addprop_integer(dev, "reset-pin",
295 1.2 gdamore info->resetConfigGpio);
296 1.2 gdamore }
297 1.2 gdamore if (info->config & BD_SYSLED) {
298 1.2 gdamore addprop_integer(dev, "sysled-pin",
299 1.2 gdamore info->sysLedGpio);
300 1.2 gdamore }
301 1.2 gdamore }
302 1.2 gdamore }
303 1.2 gdamore
304 1.2 gdamore int
305 1.2 gdamore ar531x_enable_device(const struct ar531x_device *dev)
306 1.2 gdamore {
307 1.2 gdamore const struct ar531x_boarddata *info;
308 1.2 gdamore
309 1.2 gdamore info = ar531x_board_info();
310 1.2 gdamore if (dev->mask && ((dev->mask & info->config) == 0)) {
311 1.2 gdamore return -1;
312 1.2 gdamore }
313 1.2 gdamore if (dev->reset) {
314 1.2 gdamore /* put device into reset */
315 1.2 gdamore PUTSYSREG(AR5312_SYSREG_RESETCTL,
316 1.2 gdamore GETSYSREG(AR5312_SYSREG_RESETCTL) | dev->reset);
317 1.2 gdamore
318 1.2 gdamore delay(15000); /* XXX: tsleep? */
319 1.2 gdamore
320 1.2 gdamore /* take it out of reset */
321 1.2 gdamore PUTSYSREG(AR5312_SYSREG_RESETCTL,
322 1.2 gdamore GETSYSREG(AR5312_SYSREG_RESETCTL) & ~dev->reset);
323 1.2 gdamore
324 1.2 gdamore delay(25);
325 1.2 gdamore }
326 1.2 gdamore if (dev->enable) {
327 1.2 gdamore PUTSYSREG(AR5312_SYSREG_ENABLE,
328 1.2 gdamore GETSYSREG(AR5312_SYSREG_ENABLE) | dev->enable);
329 1.2 gdamore }
330 1.2 gdamore return 0;
331 1.2 gdamore }
332 1.2 gdamore
333 1.2 gdamore const struct ar531x_device *
334 1.2 gdamore ar531x_get_devices(void)
335 1.2 gdamore {
336 1.2 gdamore static const struct ar531x_device devices[] = {
337 1.2 gdamore {
338 1.2 gdamore "ae",
339 1.2 gdamore AR5312_ENET0_BASE, 0x100000,
340 1.2 gdamore AR5312_IRQ_ENET0, -1,
341 1.2 gdamore AR5312_BOARD_CONFIG_ENET0,
342 1.2 gdamore AR5312_RESET_ENET0 | AR5312_RESET_PHY0,
343 1.2 gdamore AR5312_ENABLE_ENET0
344 1.2 gdamore },
345 1.2 gdamore {
346 1.2 gdamore "ae",
347 1.2 gdamore AR5312_ENET1_BASE, 0x100000,
348 1.2 gdamore AR5312_IRQ_ENET1, -1,
349 1.2 gdamore AR5312_BOARD_CONFIG_ENET1,
350 1.2 gdamore AR5312_RESET_ENET1 | AR5312_RESET_PHY1,
351 1.2 gdamore AR5312_ENABLE_ENET1
352 1.2 gdamore },
353 1.2 gdamore {
354 1.2 gdamore "com",
355 1.2 gdamore AR5312_UART0_BASE, 0x1000,
356 1.2 gdamore AR5312_IRQ_MISC, AR5312_MISC_IRQ_UART0,
357 1.2 gdamore AR5312_BOARD_CONFIG_UART0,
358 1.2 gdamore 0,
359 1.2 gdamore 0,
360 1.2 gdamore },
361 1.2 gdamore {
362 1.2 gdamore "com",
363 1.2 gdamore AR5312_UART1_BASE, 0x1000,
364 1.2 gdamore -1, -1,
365 1.2 gdamore AR5312_BOARD_CONFIG_UART1,
366 1.2 gdamore 0,
367 1.2 gdamore 0,
368 1.2 gdamore },
369 1.2 gdamore {
370 1.2 gdamore "ath",
371 1.2 gdamore AR5312_WLAN0_BASE, 0x100000,
372 1.2 gdamore AR5312_IRQ_WLAN0, -1,
373 1.2 gdamore AR5312_BOARD_CONFIG_WLAN0,
374 1.2 gdamore AR5312_RESET_WLAN0 |
375 1.2 gdamore AR5312_RESET_WARM_WLAN0_MAC |
376 1.2 gdamore AR5312_RESET_WARM_WLAN0_BB,
377 1.2 gdamore AR5312_ENABLE_WLAN0
378 1.2 gdamore },
379 1.2 gdamore {
380 1.2 gdamore "ath",
381 1.2 gdamore AR5312_WLAN1_BASE, 0x100000,
382 1.2 gdamore AR5312_IRQ_WLAN1, -1,
383 1.2 gdamore AR5312_BOARD_CONFIG_WLAN1,
384 1.2 gdamore AR5312_RESET_WLAN1 |
385 1.2 gdamore AR5312_RESET_WARM_WLAN1_MAC |
386 1.2 gdamore AR5312_RESET_WARM_WLAN1_BB,
387 1.2 gdamore AR5312_ENABLE_WLAN1
388 1.2 gdamore },
389 1.2 gdamore {
390 1.2 gdamore "athflash",
391 1.2 gdamore AR5312_FLASH_BASE, 0,
392 1.2 gdamore -1, -1,
393 1.2 gdamore 0,
394 1.2 gdamore 0,
395 1.2 gdamore 0,
396 1.2 gdamore },
397 1.2 gdamore {
398 1.2 gdamore "argpio", 0x1000,
399 1.2 gdamore AR5312_GPIO_BASE,
400 1.2 gdamore AR5312_IRQ_MISC, AR5312_MISC_IRQ_GPIO,
401 1.2 gdamore 0,
402 1.2 gdamore 0,
403 1.2 gdamore 0
404 1.2 gdamore },
405 1.2 gdamore { NULL }
406 1.2 gdamore };
407 1.2 gdamore
408 1.2 gdamore return devices;
409 1.2 gdamore }
410 1.2 gdamore
411