ar5315.c revision 1.2 1 1.2 gdamore /* $NetBSD: ar5315.c,v 1.2 2006/09/26 17:09: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.2 gdamore __KERNEL_RCSID(0, "$NetBSD: ar5315.c,v 1.2 2006/09/26 17:09: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.2 gdamore #define GETSDRAMREG(x) REGVAL((x) + AR5315_SDRAMCTL_BASE)
84 1.1 gdamore
85 1.1 gdamore uint32_t
86 1.1 gdamore ar531x_memsize(void)
87 1.1 gdamore {
88 1.2 gdamore #ifndef MEMSIZE
89 1.2 gdamore uint32_t memsize = 0;
90 1.2 gdamore uint32_t memcfg, cw, rw, dw;
91 1.1 gdamore
92 1.1 gdamore /*
93 1.2 gdamore * Determine the memory size. We query the board info.
94 1.1 gdamore */
95 1.2 gdamore memcfg = GETSDRAMREG(AR5315_SDRAMCTL_MEM_CFG);
96 1.2 gdamore cw = (memcfg & AR5315_MEM_CFG_COL_WIDTH_MASK) >>
97 1.2 gdamore AR5315_MEM_CFG_COL_WIDTH_SHIFT;
98 1.2 gdamore cw += 1;
99 1.2 gdamore rw = (memcfg & AR5315_MEM_CFG_ROW_WIDTH_MASK) >>
100 1.2 gdamore AR5315_MEM_CFG_ROW_WIDTH_SHIFT;
101 1.2 gdamore rw += 1;
102 1.2 gdamore
103 1.2 gdamore /* XXX: according to redboot, this could be wrong if DDR SDRAM */
104 1.2 gdamore dw = (memcfg & AR5315_MEM_CFG_DATA_WIDTH_MASK) >>
105 1.2 gdamore AR5315_MEM_CFG_DATA_WIDTH_SHIFT;
106 1.2 gdamore dw += 1;
107 1.2 gdamore dw *= 8; /* bits */
108 1.2 gdamore
109 1.2 gdamore /* not too sure about this math, but it _seems_ to add up */
110 1.2 gdamore memsize = (1 << cw) * (1 << rw) * dw;
111 1.2 gdamore #if 0
112 1.2 gdamore printf("SDRAM_MEM_CFG =%x, cw=%d rw=%d dw=%d xmemsize=%d\n", memcfg,
113 1.2 gdamore cw, rw, dw, memsize);
114 1.1 gdamore #endif
115 1.1 gdamore
116 1.1 gdamore return (memsize);
117 1.2 gdamore #else
118 1.2 gdamore /* compile time value forced */
119 1.2 gdamore return MEMSIZE;
120 1.2 gdamore #endif
121 1.1 gdamore }
122 1.1 gdamore
123 1.1 gdamore const char *
124 1.1 gdamore ar531x_cpuname(void)
125 1.1 gdamore {
126 1.1 gdamore uint16_t rev = GETSYSREG(AR5315_SYSREG_SREV);
127 1.1 gdamore switch (rev) {
128 1.1 gdamore case 0x52: /* AP30 */
129 1.1 gdamore case 0x57: /* AP31 */
130 1.1 gdamore return "Atheros AR5312";
131 1.1 gdamore case 0x58: /* AP43 */
132 1.1 gdamore return "Atheros AR2313";
133 1.1 gdamore case 0x86: /* AP51-Light */
134 1.1 gdamore case 0x87: /* AP51-Full */
135 1.1 gdamore return "Atheros AR2315";
136 1.1 gdamore case 0x91: /* AP61 */
137 1.1 gdamore return "Atheros AR2317";
138 1.1 gdamore }
139 1.1 gdamore return ("Atheros AR531X");
140 1.1 gdamore }
141 1.1 gdamore
142 1.1 gdamore void
143 1.1 gdamore ar531x_wdog(uint32_t period)
144 1.1 gdamore {
145 1.1 gdamore
146 1.1 gdamore if (period == 0) {
147 1.1 gdamore PUTSYSREG(AR5315_SYSREG_WDOG_CTL, AR5315_WDOG_CTL_IGNORE);
148 1.1 gdamore PUTSYSREG(AR5315_SYSREG_WDOG_TIMER, 0);
149 1.1 gdamore } else {
150 1.1 gdamore PUTSYSREG(AR5315_SYSREG_WDOG_TIMER, period);
151 1.1 gdamore PUTSYSREG(AR5315_SYSREG_WDOG_CTL, AR5315_WDOG_CTL_RESET);
152 1.1 gdamore }
153 1.1 gdamore }
154 1.1 gdamore
155 1.1 gdamore void
156 1.1 gdamore ar531x_businit(void)
157 1.1 gdamore {
158 1.1 gdamore /*
159 1.1 gdamore * XXX: clear COP0 config bits 0 and 1 -- Linux sets KSEG0 to either
160 1.1 gdamore * 0 or 4. Why does it do this? It is implementation defined...
161 1.1 gdamore */
162 1.1 gdamore mips3_cp0_config_write(mips3_cp0_config_read() & ~0x3);
163 1.1 gdamore
164 1.1 gdamore PUTSYSREG(AR5315_SYSREG_AHB_ERR0, AR5315_AHB_ERROR_DET);
165 1.1 gdamore GETSYSREG(AR5315_SYSREG_AHB_ERR1);
166 1.1 gdamore }
167 1.1 gdamore
168 1.1 gdamore static uint32_t
169 1.1 gdamore get_freq(uint32_t clkreg)
170 1.1 gdamore {
171 1.1 gdamore uint32_t freq = 0;
172 1.1 gdamore uint32_t clkctl, pllc, pllout, refdiv, fbdiv, div2, cpudiv;
173 1.1 gdamore
174 1.1 gdamore static const int pll_divide_table[] = {
175 1.1 gdamore 2, 3, 4, 6, 3,
176 1.1 gdamore /*
177 1.1 gdamore * these entries are bogus, but it avoids a possible
178 1.1 gdamore * bad table dereference
179 1.1 gdamore */
180 1.1 gdamore 1, 1, 1
181 1.1 gdamore };
182 1.1 gdamore static const int pre_divide_table[] = {
183 1.1 gdamore 1, 2, 4, 5
184 1.1 gdamore };
185 1.1 gdamore
186 1.1 gdamore if (freq)
187 1.1 gdamore return freq;
188 1.1 gdamore
189 1.1 gdamore pllc = GETSYSREG(AR5315_SYSREG_PLLC_CTL);
190 1.1 gdamore clkctl = GETSYSREG(clkreg);
191 1.1 gdamore
192 1.1 gdamore refdiv = pre_divide_table[AR5315_PLLC_REF_DIV(pllc)];
193 1.1 gdamore fbdiv = AR5315_PLLC_FB_DIV(pllc);
194 1.1 gdamore div2 = (AR5315_PLLC_DIV_2(pllc) + 1) * 2; /* results in 2 or 4 */
195 1.1 gdamore
196 1.1 gdamore cpudiv = AR5315_CLOCKCTL_DIV(clkctl);
197 1.1 gdamore cpudiv = cpudiv ? (cpudiv * 2) : 1;
198 1.1 gdamore
199 1.1 gdamore /* 40MHz reference clk, reference and feedback dividers */
200 1.1 gdamore pllout = (40000000 / refdiv) * div2 * fbdiv;
201 1.1 gdamore
202 1.1 gdamore switch (AR5315_CLOCKCTL_SELECT(clkctl)) {
203 1.1 gdamore case 0:
204 1.1 gdamore case 1:
205 1.1 gdamore /* CLKM select */
206 1.1 gdamore pllout /= pll_divide_table[AR5315_PLLC_CLKM(pllc)];
207 1.1 gdamore break;
208 1.1 gdamore case 2:
209 1.1 gdamore /* CLKC select */
210 1.1 gdamore pllout /= pll_divide_table[AR5315_PLLC_CLKC(pllc)];
211 1.1 gdamore break;
212 1.1 gdamore default:
213 1.1 gdamore /* ref_clk select */
214 1.1 gdamore pllout = 40000000; /* use original reference clock */
215 1.1 gdamore break;
216 1.1 gdamore }
217 1.1 gdamore
218 1.1 gdamore freq = pllout/(cpudiv);
219 1.1 gdamore
220 1.1 gdamore return (freq);
221 1.1 gdamore }
222 1.1 gdamore
223 1.1 gdamore uint32_t
224 1.1 gdamore ar531x_cpu_freq(void)
225 1.1 gdamore {
226 1.1 gdamore static uint32_t freq = 0;
227 1.1 gdamore if (freq == 0)
228 1.1 gdamore freq = get_freq(AR5315_SYSREG_CPUCLK);
229 1.1 gdamore return (freq);
230 1.1 gdamore }
231 1.1 gdamore
232 1.1 gdamore uint32_t
233 1.1 gdamore ar531x_bus_freq(void)
234 1.1 gdamore {
235 1.1 gdamore static uint32_t freq = 0;
236 1.1 gdamore if (freq == 0)
237 1.1 gdamore freq = get_freq(AR5315_SYSREG_AMBACLK);
238 1.1 gdamore return (freq);
239 1.1 gdamore }
240 1.1 gdamore
241 1.1 gdamore static void
242 1.1 gdamore addprop_data(struct device *dev, const char *name, const uint8_t *data,
243 1.1 gdamore int len)
244 1.1 gdamore {
245 1.1 gdamore prop_data_t pd;
246 1.1 gdamore pd = prop_data_create_data(data, len);
247 1.1 gdamore KASSERT(pd != NULL);
248 1.1 gdamore if (prop_dictionary_set(device_properties(dev), name, pd) == FALSE) {
249 1.1 gdamore printf("WARNING: unable to set %s property for %s\n",
250 1.1 gdamore name, device_xname(dev));
251 1.1 gdamore }
252 1.1 gdamore prop_object_release(pd);
253 1.1 gdamore }
254 1.1 gdamore
255 1.1 gdamore static void
256 1.1 gdamore addprop_integer(struct device *dev, const char *name, uint32_t val)
257 1.1 gdamore {
258 1.1 gdamore prop_number_t pn;
259 1.1 gdamore pn = prop_number_create_integer(val);
260 1.1 gdamore KASSERT(pn != NULL);
261 1.1 gdamore if (prop_dictionary_set(device_properties(dev), name, pn) == FALSE) {
262 1.1 gdamore printf("WARNING: unable to set %s property for %s",
263 1.1 gdamore name, device_xname(dev));
264 1.1 gdamore }
265 1.1 gdamore prop_object_release(pn);
266 1.1 gdamore }
267 1.1 gdamore
268 1.1 gdamore void
269 1.1 gdamore ar531x_device_register(struct device *dev, void *aux)
270 1.1 gdamore {
271 1.1 gdamore struct arbus_attach_args *aa = aux;
272 1.1 gdamore const struct ar531x_boarddata *info;
273 1.1 gdamore
274 1.1 gdamore info = ar531x_board_info();
275 1.1 gdamore if (info == NULL) {
276 1.1 gdamore /* nothing known about this board! */
277 1.1 gdamore return;
278 1.1 gdamore }
279 1.1 gdamore
280 1.1 gdamore /*
281 1.1 gdamore * We don't ever know the boot device. But that's because the
282 1.1 gdamore * firmware only loads from the network.
283 1.1 gdamore */
284 1.1 gdamore
285 1.1 gdamore /* Fetch the MAC addresses. */
286 1.1 gdamore if (device_is_a(dev, "ae")) {
287 1.1 gdamore const uint8_t *enet;
288 1.1 gdamore
289 1.1 gdamore if (aa->aa_addr == AR5315_ENET_BASE)
290 1.1 gdamore enet = info->enet0Mac;
291 1.1 gdamore else
292 1.1 gdamore return;
293 1.1 gdamore
294 1.1 gdamore addprop_data(dev, "mac-addr", enet, ETHER_ADDR_LEN);
295 1.1 gdamore }
296 1.1 gdamore
297 1.1 gdamore if (device_is_a(dev, "ath")) {
298 1.1 gdamore const uint8_t *enet;
299 1.1 gdamore
300 1.1 gdamore if (aa->aa_addr == AR5315_WLAN_BASE)
301 1.1 gdamore enet = info->wlan0Mac;
302 1.1 gdamore else
303 1.1 gdamore return;
304 1.1 gdamore
305 1.1 gdamore addprop_data(dev, "mac-addr", enet, ETHER_ADDR_LEN);
306 1.1 gdamore
307 1.1 gdamore addprop_integer(dev, "wmac-rev",
308 1.1 gdamore GETSYSREG(AR5315_SYSREG_SREV));
309 1.1 gdamore }
310 1.1 gdamore
311 1.1 gdamore if (device_is_a(dev, "com")) {
312 1.1 gdamore addprop_integer(dev, "frequency", ar531x_bus_freq());
313 1.1 gdamore }
314 1.1 gdamore
315 1.1 gdamore if (device_is_a(dev, "argpio")) {
316 1.1 gdamore if (info->config & BD_RSTFACTORY) {
317 1.1 gdamore addprop_integer(dev, "reset-pin",
318 1.1 gdamore info->resetConfigGpio);
319 1.1 gdamore }
320 1.1 gdamore if (info->config & BD_SYSLED) {
321 1.1 gdamore addprop_integer(dev, "sysled-pin",
322 1.1 gdamore info->sysLedGpio);
323 1.1 gdamore }
324 1.1 gdamore }
325 1.1 gdamore }
326 1.1 gdamore
327 1.1 gdamore const struct ar531x_device *
328 1.1 gdamore ar531x_get_devices(void)
329 1.1 gdamore {
330 1.1 gdamore const static struct ar531x_device devices[] = {
331 1.1 gdamore {
332 1.1 gdamore "com",
333 1.1 gdamore AR5315_UART_BASE, 0x1000,
334 1.1 gdamore AR5315_CPU_IRQ_MISC, AR5315_MISC_IRQ_UART,
335 1.1 gdamore 0, 0, 0
336 1.1 gdamore },
337 1.1 gdamore {
338 1.1 gdamore "ae",
339 1.1 gdamore AR5315_ENET_BASE, 0x100000,
340 1.1 gdamore AR5315_CPU_IRQ_ENET, -1,
341 1.1 gdamore 0, 0, 0
342 1.1 gdamore },
343 1.1 gdamore {
344 1.1 gdamore "ath",
345 1.1 gdamore AR5315_WLAN_BASE, 0x100000,
346 1.1 gdamore AR5315_CPU_IRQ_WLAN, -1,
347 1.1 gdamore 0, 0, 0
348 1.1 gdamore },
349 1.1 gdamore { NULL }
350 1.1 gdamore };
351 1.1 gdamore
352 1.1 gdamore return devices;
353 1.1 gdamore }
354 1.1 gdamore
355 1.1 gdamore int
356 1.1 gdamore ar531x_enable_device(const struct ar531x_device *dev)
357 1.1 gdamore {
358 1.1 gdamore if (dev->addr == AR5315_WLAN_BASE) {
359 1.1 gdamore /* enable arbitration for wlan */
360 1.1 gdamore PUTSYSREG(AR5315_SYSREG_AHB_ARB_CTL,
361 1.1 gdamore GETSYSREG(AR5315_SYSREG_AHB_ARB_CTL) | AR5315_ARB_WLAN);
362 1.1 gdamore
363 1.1 gdamore /* set WLAN for big endian */
364 1.1 gdamore PUTSYSREG(AR5315_SYSREG_ENDIAN,
365 1.1 gdamore GETSYSREG(AR5315_SYSREG_ENDIAN) | AR5315_ENDIAN_WLAN);
366 1.1 gdamore
367 1.1 gdamore /* wake up the mac */
368 1.1 gdamore PUTPCIREG(AR5315_PCI_MAC_SCR,
369 1.1 gdamore (GETPCIREG(AR5315_PCI_MAC_SCR) & ~PCI_MAC_SCR_SLM_MASK) |
370 1.1 gdamore PCI_MAC_SCR_SLM_FWAKE);
371 1.1 gdamore
372 1.1 gdamore /* wait for it to wake up */
373 1.1 gdamore while (GETPCIREG(AR5315_PCI_MAC_PCICFG) &
374 1.1 gdamore PCI_MAC_PCICFG_SPWR_DN);
375 1.1 gdamore }
376 1.1 gdamore return 0;
377 1.1 gdamore }
378