ar5312_board.c revision 1.1 1 1.1 gdamore /* $Id: ar5312_board.c,v 1.1 2006/09/26 06:37:32 gdamore Exp $ */
2 1.1 gdamore /*
3 1.1 gdamore * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
4 1.1 gdamore * Copyright (c) 2006 Garrett D'Amore.
5 1.1 gdamore * All rights reserved.
6 1.1 gdamore *
7 1.1 gdamore * This code was written by Garrett D'Amore for the Champaign-Urbana
8 1.1 gdamore * Community Wireless Network Project.
9 1.1 gdamore *
10 1.1 gdamore * Redistribution and use in source and binary forms, with or
11 1.1 gdamore * without modification, are permitted provided that the following
12 1.1 gdamore * conditions are met:
13 1.1 gdamore * 1. Redistributions of source code must retain the above copyright
14 1.1 gdamore * notice, this list of conditions and the following disclaimer.
15 1.1 gdamore * 2. Redistributions in binary form must reproduce the above
16 1.1 gdamore * copyright notice, this list of conditions and the following
17 1.1 gdamore * disclaimer in the documentation and/or other materials provided
18 1.1 gdamore * with the distribution.
19 1.1 gdamore * 3. All advertising materials mentioning features or use of this
20 1.1 gdamore * software must display the following acknowledgements:
21 1.1 gdamore * This product includes software developed by the Urbana-Champaign
22 1.1 gdamore * Independent Media Center.
23 1.1 gdamore * This product includes software developed by Garrett D'Amore.
24 1.1 gdamore * 4. Urbana-Champaign Independent Media Center's name and Garrett
25 1.1 gdamore * D'Amore's name may not be used to endorse or promote products
26 1.1 gdamore * derived from this software without specific prior written permission.
27 1.1 gdamore *
28 1.1 gdamore * THIS SOFTWARE IS PROVIDED BY THE URBANA-CHAMPAIGN INDEPENDENT
29 1.1 gdamore * MEDIA CENTER AND GARRETT D'AMORE ``AS IS'' AND ANY EXPRESS OR
30 1.1 gdamore * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
31 1.1 gdamore * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 1.1 gdamore * ARE DISCLAIMED. IN NO EVENT SHALL THE URBANA-CHAMPAIGN INDEPENDENT
33 1.1 gdamore * MEDIA CENTER OR GARRETT D'AMORE BE LIABLE FOR ANY DIRECT, INDIRECT,
34 1.1 gdamore * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35 1.1 gdamore * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
36 1.1 gdamore * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
37 1.1 gdamore * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
38 1.1 gdamore * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 1.1 gdamore * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
40 1.1 gdamore * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 1.1 gdamore */
42 1.1 gdamore #include <sys/cdefs.h>
43 1.1 gdamore __KERNEL_RCSID(0, "$NetBSD: ar5312_board.c,v 1.1 2006/09/26 06:37:32 gdamore Exp $");
44 1.1 gdamore
45 1.1 gdamore #include <sys/param.h>
46 1.1 gdamore #include <sys/systm.h>
47 1.1 gdamore #include <sys/device.h>
48 1.1 gdamore
49 1.1 gdamore #include <machine/bus.h>
50 1.1 gdamore #include <mips/atheros/include/ar5312reg.h>
51 1.1 gdamore #include <mips/atheros/include/ar531xvar.h>
52 1.1 gdamore
53 1.1 gdamore #include <contrib/dev/ath/ah_soc.h>
54 1.1 gdamore
55 1.1 gdamore extern const char *ether_sprintf(const uint8_t *);
56 1.1 gdamore
57 1.1 gdamore /*
58 1.1 gdamore * Locate the Board Configuration data using heuristics.
59 1.1 gdamore * Search backward from the (aliased) end of flash looking
60 1.1 gdamore * for the signature string that marks the start of the data.
61 1.1 gdamore * We search at most 500KB.
62 1.1 gdamore */
63 1.1 gdamore const struct ar531x_boarddata *
64 1.1 gdamore ar531x_board_info(void)
65 1.1 gdamore {
66 1.1 gdamore static const struct ar531x_boarddata *board = NULL;
67 1.1 gdamore const uint8_t *ptr, *end;
68 1.1 gdamore uint32_t fctl;
69 1.1 gdamore
70 1.1 gdamore if (board == NULL) {
71 1.1 gdamore /* configure flash bank 0 */
72 1.1 gdamore fctl = REGVAL(AR5312_FLASHCTL_BASE + AR5312_FLASHCTL_0) &
73 1.1 gdamore AR5312_FLASHCTL_MW_MASK;
74 1.1 gdamore
75 1.1 gdamore fctl |=
76 1.1 gdamore AR5312_FLASHCTL_E |
77 1.1 gdamore AR5312_FLASHCTL_RBLE |
78 1.1 gdamore AR5312_FLASHCTL_AC_8M |
79 1.1 gdamore (1 << AR5312_FLASHCTL_IDCY_SHIFT) |
80 1.1 gdamore (7 << AR5312_FLASHCTL_WST1_SHIFT) |
81 1.1 gdamore (7 << AR5312_FLASHCTL_WST2_SHIFT);
82 1.1 gdamore
83 1.1 gdamore REGVAL(AR5312_FLASHCTL_BASE + AR5312_FLASHCTL_0) = fctl;
84 1.1 gdamore
85 1.1 gdamore REGVAL(AR5312_FLASHCTL_BASE + AR5312_FLASHCTL_1) &=
86 1.1 gdamore ~(AR5312_FLASHCTL_E | AR5312_FLASHCTL_AC_MASK);
87 1.1 gdamore
88 1.1 gdamore REGVAL(AR5312_FLASHCTL_BASE + AR5312_FLASHCTL_2) &=
89 1.1 gdamore ~(AR5312_FLASHCTL_E | AR5312_FLASHCTL_AC_MASK);
90 1.1 gdamore
91 1.1 gdamore /* search backward in the flash looking for the signature */
92 1.1 gdamore ptr = (const uint8_t *) MIPS_PHYS_TO_KSEG1(AR5312_FLASH_END - 0x1000);
93 1.1 gdamore end = ptr - (500 * 1024); /* NB: max 500KB window */
94 1.1 gdamore /* XXX validate end */
95 1.1 gdamore for (; ptr > end; ptr -= 0x1000)
96 1.1 gdamore if (*(const uint32_t *)ptr == AR531X_BD_MAGIC) {
97 1.1 gdamore board = (const struct ar531x_boarddata *) ptr;
98 1.1 gdamore break;
99 1.1 gdamore }
100 1.1 gdamore }
101 1.1 gdamore return board;
102 1.1 gdamore }
103 1.1 gdamore
104 1.1 gdamore /*
105 1.1 gdamore * Locate the radio configuration data; it is located relative
106 1.1 gdamore * to the board configuration data.
107 1.1 gdamore */
108 1.1 gdamore const void *
109 1.1 gdamore ar531x_radio_info(void)
110 1.1 gdamore {
111 1.1 gdamore static const void *radio = NULL;
112 1.1 gdamore const struct ar531x_boarddata *board;
113 1.1 gdamore const uint8_t *baddr, *ptr, *end;
114 1.1 gdamore
115 1.1 gdamore if (radio == NULL) {
116 1.1 gdamore board = ar531x_board_info();
117 1.1 gdamore if (board == NULL)
118 1.1 gdamore return NULL;
119 1.1 gdamore baddr = (const uint8_t *) board;
120 1.1 gdamore ptr = baddr + 0x1000;
121 1.1 gdamore end = (const uint8_t *)
122 1.1 gdamore MIPS_PHYS_TO_KSEG1(AR5312_FLASH_END-0x1000);
123 1.1 gdamore again:
124 1.1 gdamore for (; ptr < end; ptr += 0x1000)
125 1.1 gdamore if (*(const uint32_t *)ptr != 0xffffffff) {
126 1.1 gdamore radio = ptr;
127 1.1 gdamore goto done;
128 1.1 gdamore }
129 1.1 gdamore /* sort of an Algol-style for loop ... */
130 1.1 gdamore if (end == (uint8_t *) MIPS_PHYS_TO_KSEG1(AR5312_FLASH_END)) {
131 1.1 gdamore /* NB: AR2316 has radio data in a different location */
132 1.1 gdamore ptr = baddr + 0xf8;
133 1.1 gdamore end = (const uint8_t *)
134 1.1 gdamore MIPS_PHYS_TO_KSEG1(AR5312_FLASH_END-0x1000 + 0xf8);
135 1.1 gdamore goto again;
136 1.1 gdamore }
137 1.1 gdamore }
138 1.1 gdamore done:
139 1.1 gdamore return radio;
140 1.1 gdamore }
141 1.1 gdamore
142 1.1 gdamore /*
143 1.1 gdamore * Locate board and radio configuration data in flash.
144 1.1 gdamore */
145 1.1 gdamore int
146 1.1 gdamore ar531x_board_config(struct ar531x_config *config)
147 1.1 gdamore {
148 1.1 gdamore
149 1.1 gdamore config->board = ar531x_board_info();
150 1.1 gdamore if (config->board == NULL)
151 1.1 gdamore return ENOENT;
152 1.1 gdamore config->radio = ar531x_radio_info();
153 1.1 gdamore if (config->radio == NULL)
154 1.1 gdamore return ENOENT; /* XXX distinct code */
155 1.1 gdamore return 0;
156 1.1 gdamore }
157