ar5315.c revision 1.1 1 1.1 gdamore /* $NetBSD: ar5315.c,v 1.1 2006/09/26 06:37:32 gdamore 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 /*
46 1.1 gdamore * This file includes a bunch of implementation specific bits for
47 1.1 gdamore * AR5315, which differs these from other members of the AR531X
48 1.1 gdamore * family.
49 1.1 gdamore */
50 1.1 gdamore #include <sys/cdefs.h>
51 1.1 gdamore __KERNEL_RCSID(0, "$NetBSD: ar5315.c,v 1.1 2006/09/26 06:37:32 gdamore Exp $");
52 1.1 gdamore
53 1.1 gdamore #include "opt_ddb.h"
54 1.1 gdamore #include "opt_kgdb.h"
55 1.1 gdamore
56 1.1 gdamore #include "opt_memsize.h"
57 1.1 gdamore #include <sys/param.h>
58 1.1 gdamore #include <sys/systm.h>
59 1.1 gdamore #include <sys/kernel.h>
60 1.1 gdamore #include <sys/buf.h>
61 1.1 gdamore
62 1.1 gdamore #include <mips/cache.h>
63 1.1 gdamore #include <mips/locore.h>
64 1.1 gdamore #include <mips/cpuregs.h>
65 1.1 gdamore
66 1.1 gdamore #include <net/if.h>
67 1.1 gdamore #include <net/if_ether.h>
68 1.1 gdamore
69 1.1 gdamore #include <contrib/dev/ath/ah_soc.h> /* XXX really doesn't belong in hal */
70 1.1 gdamore
71 1.1 gdamore #include <mips/atheros/include/ar5315reg.h>
72 1.1 gdamore #include <mips/atheros/include/ar531xvar.h>
73 1.1 gdamore #include <mips/atheros/include/arbusvar.h>
74 1.1 gdamore
75 1.1 gdamore #include <machine/locore.h>
76 1.1 gdamore
77 1.1 gdamore /* helper macro for accessing system registers without bus space */
78 1.1 gdamore #define REGVAL(x) *((volatile uint32_t *)(MIPS_PHYS_TO_KSEG1((x))))
79 1.1 gdamore #define GETSYSREG(x) REGVAL((x) + AR5315_SYSREG_BASE)
80 1.1 gdamore #define PUTSYSREG(x,v) (REGVAL((x) + AR5315_SYSREG_BASE)) = (v)
81 1.1 gdamore #define GETPCIREG(x) REGVAL((x) + AR5315_PCI_BASE)
82 1.1 gdamore #define PUTPCIREG(x,v) (REGVAL((x) + AR5315_PCI_BASE)) = (v)
83 1.1 gdamore
84 1.1 gdamore uint32_t
85 1.1 gdamore ar531x_memsize(void)
86 1.1 gdamore {
87 1.1 gdamore uint32_t memsize;
88 1.1 gdamore
89 1.1 gdamore /*
90 1.1 gdamore * Determine the memory size.
91 1.1 gdamore *
92 1.1 gdamore * NB: we allow compile time override
93 1.1 gdamore */
94 1.1 gdamore #if defined(MEMSIZE)
95 1.1 gdamore memsize = MEMSIZE;
96 1.1 gdamore #else
97 1.1 gdamore /* assume default value, may or may not be reasonable */
98 1.1 gdamore /* XXX: it would be nice if this could autodetect */
99 1.1 gdamore memsize = (8 << 20);
100 1.1 gdamore #endif
101 1.1 gdamore
102 1.1 gdamore return (memsize);
103 1.1 gdamore }
104 1.1 gdamore
105 1.1 gdamore const char *
106 1.1 gdamore ar531x_cpuname(void)
107 1.1 gdamore {
108 1.1 gdamore uint16_t rev = GETSYSREG(AR5315_SYSREG_SREV);
109 1.1 gdamore switch (rev) {
110 1.1 gdamore case 0x52: /* AP30 */
111 1.1 gdamore case 0x57: /* AP31 */
112 1.1 gdamore return "Atheros AR5312";
113 1.1 gdamore case 0x58: /* AP43 */
114 1.1 gdamore return "Atheros AR2313";
115 1.1 gdamore case 0x86: /* AP51-Light */
116 1.1 gdamore case 0x87: /* AP51-Full */
117 1.1 gdamore return "Atheros AR2315";
118 1.1 gdamore case 0x91: /* AP61 */
119 1.1 gdamore return "Atheros AR2317";
120 1.1 gdamore }
121 1.1 gdamore return ("Atheros AR531X");
122 1.1 gdamore }
123 1.1 gdamore
124 1.1 gdamore void
125 1.1 gdamore ar531x_wdog(uint32_t period)
126 1.1 gdamore {
127 1.1 gdamore
128 1.1 gdamore if (period == 0) {
129 1.1 gdamore PUTSYSREG(AR5315_SYSREG_WDOG_CTL, AR5315_WDOG_CTL_IGNORE);
130 1.1 gdamore PUTSYSREG(AR5315_SYSREG_WDOG_TIMER, 0);
131 1.1 gdamore } else {
132 1.1 gdamore PUTSYSREG(AR5315_SYSREG_WDOG_TIMER, period);
133 1.1 gdamore PUTSYSREG(AR5315_SYSREG_WDOG_CTL, AR5315_WDOG_CTL_RESET);
134 1.1 gdamore }
135 1.1 gdamore }
136 1.1 gdamore
137 1.1 gdamore void
138 1.1 gdamore ar531x_businit(void)
139 1.1 gdamore {
140 1.1 gdamore /*
141 1.1 gdamore * XXX: clear COP0 config bits 0 and 1 -- Linux sets KSEG0 to either
142 1.1 gdamore * 0 or 4. Why does it do this? It is implementation defined...
143 1.1 gdamore */
144 1.1 gdamore mips3_cp0_config_write(mips3_cp0_config_read() & ~0x3);
145 1.1 gdamore
146 1.1 gdamore PUTSYSREG(AR5315_SYSREG_AHB_ERR0, AR5315_AHB_ERROR_DET);
147 1.1 gdamore GETSYSREG(AR5315_SYSREG_AHB_ERR1);
148 1.1 gdamore }
149 1.1 gdamore
150 1.1 gdamore static uint32_t
151 1.1 gdamore get_freq(uint32_t clkreg)
152 1.1 gdamore {
153 1.1 gdamore uint32_t freq = 0;
154 1.1 gdamore uint32_t clkctl, pllc, pllout, refdiv, fbdiv, div2, cpudiv;
155 1.1 gdamore
156 1.1 gdamore static const int pll_divide_table[] = {
157 1.1 gdamore 2, 3, 4, 6, 3,
158 1.1 gdamore /*
159 1.1 gdamore * these entries are bogus, but it avoids a possible
160 1.1 gdamore * bad table dereference
161 1.1 gdamore */
162 1.1 gdamore 1, 1, 1
163 1.1 gdamore };
164 1.1 gdamore static const int pre_divide_table[] = {
165 1.1 gdamore 1, 2, 4, 5
166 1.1 gdamore };
167 1.1 gdamore
168 1.1 gdamore if (freq)
169 1.1 gdamore return freq;
170 1.1 gdamore
171 1.1 gdamore pllc = GETSYSREG(AR5315_SYSREG_PLLC_CTL);
172 1.1 gdamore clkctl = GETSYSREG(clkreg);
173 1.1 gdamore
174 1.1 gdamore refdiv = pre_divide_table[AR5315_PLLC_REF_DIV(pllc)];
175 1.1 gdamore fbdiv = AR5315_PLLC_FB_DIV(pllc);
176 1.1 gdamore div2 = (AR5315_PLLC_DIV_2(pllc) + 1) * 2; /* results in 2 or 4 */
177 1.1 gdamore
178 1.1 gdamore cpudiv = AR5315_CLOCKCTL_DIV(clkctl);
179 1.1 gdamore cpudiv = cpudiv ? (cpudiv * 2) : 1;
180 1.1 gdamore
181 1.1 gdamore /* 40MHz reference clk, reference and feedback dividers */
182 1.1 gdamore pllout = (40000000 / refdiv) * div2 * fbdiv;
183 1.1 gdamore
184 1.1 gdamore switch (AR5315_CLOCKCTL_SELECT(clkctl)) {
185 1.1 gdamore case 0:
186 1.1 gdamore case 1:
187 1.1 gdamore /* CLKM select */
188 1.1 gdamore pllout /= pll_divide_table[AR5315_PLLC_CLKM(pllc)];
189 1.1 gdamore break;
190 1.1 gdamore case 2:
191 1.1 gdamore /* CLKC select */
192 1.1 gdamore pllout /= pll_divide_table[AR5315_PLLC_CLKC(pllc)];
193 1.1 gdamore break;
194 1.1 gdamore default:
195 1.1 gdamore /* ref_clk select */
196 1.1 gdamore pllout = 40000000; /* use original reference clock */
197 1.1 gdamore break;
198 1.1 gdamore }
199 1.1 gdamore
200 1.1 gdamore freq = pllout/(cpudiv);
201 1.1 gdamore
202 1.1 gdamore return (freq);
203 1.1 gdamore }
204 1.1 gdamore
205 1.1 gdamore uint32_t
206 1.1 gdamore ar531x_cpu_freq(void)
207 1.1 gdamore {
208 1.1 gdamore static uint32_t freq = 0;
209 1.1 gdamore if (freq == 0)
210 1.1 gdamore freq = get_freq(AR5315_SYSREG_CPUCLK);
211 1.1 gdamore return (freq);
212 1.1 gdamore }
213 1.1 gdamore
214 1.1 gdamore uint32_t
215 1.1 gdamore ar531x_bus_freq(void)
216 1.1 gdamore {
217 1.1 gdamore static uint32_t freq = 0;
218 1.1 gdamore if (freq == 0)
219 1.1 gdamore freq = get_freq(AR5315_SYSREG_AMBACLK);
220 1.1 gdamore return (freq);
221 1.1 gdamore }
222 1.1 gdamore
223 1.1 gdamore static void
224 1.1 gdamore addprop_data(struct device *dev, const char *name, const uint8_t *data,
225 1.1 gdamore int len)
226 1.1 gdamore {
227 1.1 gdamore prop_data_t pd;
228 1.1 gdamore pd = prop_data_create_data(data, len);
229 1.1 gdamore KASSERT(pd != NULL);
230 1.1 gdamore if (prop_dictionary_set(device_properties(dev), name, pd) == FALSE) {
231 1.1 gdamore printf("WARNING: unable to set %s property for %s\n",
232 1.1 gdamore name, device_xname(dev));
233 1.1 gdamore }
234 1.1 gdamore prop_object_release(pd);
235 1.1 gdamore }
236 1.1 gdamore
237 1.1 gdamore static void
238 1.1 gdamore addprop_integer(struct device *dev, const char *name, uint32_t val)
239 1.1 gdamore {
240 1.1 gdamore prop_number_t pn;
241 1.1 gdamore pn = prop_number_create_integer(val);
242 1.1 gdamore KASSERT(pn != NULL);
243 1.1 gdamore if (prop_dictionary_set(device_properties(dev), name, pn) == FALSE) {
244 1.1 gdamore printf("WARNING: unable to set %s property for %s",
245 1.1 gdamore name, device_xname(dev));
246 1.1 gdamore }
247 1.1 gdamore prop_object_release(pn);
248 1.1 gdamore }
249 1.1 gdamore
250 1.1 gdamore void
251 1.1 gdamore ar531x_device_register(struct device *dev, void *aux)
252 1.1 gdamore {
253 1.1 gdamore struct arbus_attach_args *aa = aux;
254 1.1 gdamore const struct ar531x_boarddata *info;
255 1.1 gdamore
256 1.1 gdamore info = ar531x_board_info();
257 1.1 gdamore if (info == NULL) {
258 1.1 gdamore /* nothing known about this board! */
259 1.1 gdamore return;
260 1.1 gdamore }
261 1.1 gdamore
262 1.1 gdamore /*
263 1.1 gdamore * We don't ever know the boot device. But that's because the
264 1.1 gdamore * firmware only loads from the network.
265 1.1 gdamore */
266 1.1 gdamore
267 1.1 gdamore /* Fetch the MAC addresses. */
268 1.1 gdamore if (device_is_a(dev, "ae")) {
269 1.1 gdamore const uint8_t *enet;
270 1.1 gdamore
271 1.1 gdamore if (aa->aa_addr == AR5315_ENET_BASE)
272 1.1 gdamore enet = info->enet0Mac;
273 1.1 gdamore else
274 1.1 gdamore return;
275 1.1 gdamore
276 1.1 gdamore addprop_data(dev, "mac-addr", enet, ETHER_ADDR_LEN);
277 1.1 gdamore }
278 1.1 gdamore
279 1.1 gdamore if (device_is_a(dev, "ath")) {
280 1.1 gdamore const uint8_t *enet;
281 1.1 gdamore
282 1.1 gdamore if (aa->aa_addr == AR5315_WLAN_BASE)
283 1.1 gdamore enet = info->wlan0Mac;
284 1.1 gdamore else
285 1.1 gdamore return;
286 1.1 gdamore
287 1.1 gdamore addprop_data(dev, "mac-addr", enet, ETHER_ADDR_LEN);
288 1.1 gdamore
289 1.1 gdamore addprop_integer(dev, "wmac-rev",
290 1.1 gdamore GETSYSREG(AR5315_SYSREG_SREV));
291 1.1 gdamore }
292 1.1 gdamore
293 1.1 gdamore if (device_is_a(dev, "com")) {
294 1.1 gdamore addprop_integer(dev, "frequency", ar531x_bus_freq());
295 1.1 gdamore }
296 1.1 gdamore
297 1.1 gdamore if (device_is_a(dev, "argpio")) {
298 1.1 gdamore if (info->config & BD_RSTFACTORY) {
299 1.1 gdamore addprop_integer(dev, "reset-pin",
300 1.1 gdamore info->resetConfigGpio);
301 1.1 gdamore }
302 1.1 gdamore if (info->config & BD_SYSLED) {
303 1.1 gdamore addprop_integer(dev, "sysled-pin",
304 1.1 gdamore info->sysLedGpio);
305 1.1 gdamore }
306 1.1 gdamore }
307 1.1 gdamore }
308 1.1 gdamore
309 1.1 gdamore const struct ar531x_device *
310 1.1 gdamore ar531x_get_devices(void)
311 1.1 gdamore {
312 1.1 gdamore const static struct ar531x_device devices[] = {
313 1.1 gdamore {
314 1.1 gdamore "com",
315 1.1 gdamore AR5315_UART_BASE, 0x1000,
316 1.1 gdamore AR5315_CPU_IRQ_MISC, AR5315_MISC_IRQ_UART,
317 1.1 gdamore 0, 0, 0
318 1.1 gdamore },
319 1.1 gdamore {
320 1.1 gdamore "ae",
321 1.1 gdamore AR5315_ENET_BASE, 0x100000,
322 1.1 gdamore AR5315_CPU_IRQ_ENET, -1,
323 1.1 gdamore 0, 0, 0
324 1.1 gdamore },
325 1.1 gdamore {
326 1.1 gdamore "ath",
327 1.1 gdamore AR5315_WLAN_BASE, 0x100000,
328 1.1 gdamore AR5315_CPU_IRQ_WLAN, -1,
329 1.1 gdamore 0, 0, 0
330 1.1 gdamore },
331 1.1 gdamore { NULL }
332 1.1 gdamore };
333 1.1 gdamore
334 1.1 gdamore return devices;
335 1.1 gdamore }
336 1.1 gdamore
337 1.1 gdamore int
338 1.1 gdamore ar531x_enable_device(const struct ar531x_device *dev)
339 1.1 gdamore {
340 1.1 gdamore if (dev->addr == AR5315_WLAN_BASE) {
341 1.1 gdamore /* enable arbitration for wlan */
342 1.1 gdamore PUTSYSREG(AR5315_SYSREG_AHB_ARB_CTL,
343 1.1 gdamore GETSYSREG(AR5315_SYSREG_AHB_ARB_CTL) | AR5315_ARB_WLAN);
344 1.1 gdamore
345 1.1 gdamore /* set WLAN for big endian */
346 1.1 gdamore PUTSYSREG(AR5315_SYSREG_ENDIAN,
347 1.1 gdamore GETSYSREG(AR5315_SYSREG_ENDIAN) | AR5315_ENDIAN_WLAN);
348 1.1 gdamore
349 1.1 gdamore /* wake up the mac */
350 1.1 gdamore PUTPCIREG(AR5315_PCI_MAC_SCR,
351 1.1 gdamore (GETPCIREG(AR5315_PCI_MAC_SCR) & ~PCI_MAC_SCR_SLM_MASK) |
352 1.1 gdamore PCI_MAC_SCR_SLM_FWAKE);
353 1.1 gdamore
354 1.1 gdamore /* wait for it to wake up */
355 1.1 gdamore while (GETPCIREG(AR5315_PCI_MAC_PCICFG) &
356 1.1 gdamore PCI_MAC_PCICFG_SPWR_DN);
357 1.1 gdamore }
358 1.1 gdamore return 0;
359 1.1 gdamore }
360