rtwphyio.c revision 1.1 1 /* $NetBSD: rtwphyio.c,v 1.1 2004/09/26 02:29:15 dyoung Exp $ */
2 /*-
3 * Copyright (c) 2004, 2005 David Young. All rights reserved.
4 *
5 * Programmed for NetBSD by David Young.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of David Young may not be used to endorse or promote
16 * products derived from this software without specific prior
17 * written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY David Young ``AS IS'' AND ANY
20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL David
23 * Young BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
25 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
30 * OF SUCH DAMAGE.
31 */
32 /*
33 * Control input/output with the Philips SA2400 RF front-end and
34 * the baseband processor built into the Realtek RTL8180.
35 */
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: rtwphyio.c,v 1.1 2004/09/26 02:29:15 dyoung Exp $");
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/types.h>
43
44 #include <machine/bus.h>
45
46 #include <net/if.h>
47 #include <net/if_media.h>
48 #include <net/if_ether.h>
49
50 #include <net80211/ieee80211_var.h>
51 #include <net80211/ieee80211_compat.h>
52 #include <net80211/ieee80211_radiotap.h>
53
54 #include <dev/ic/rtwreg.h>
55 #include <dev/ic/max2820reg.h>
56 #include <dev/ic/sa2400reg.h>
57 #include <dev/ic/si4136reg.h>
58 #include <dev/ic/rtwvar.h>
59 #include <dev/ic/rtwphyio.h>
60 #include <dev/ic/rtwphy.h>
61
62 static int rtw_macbangbits_timeout = 100;
63
64 u_int8_t
65 rtw_bbp_read(struct rtw_regs *regs, u_int addr)
66 {
67 KASSERT((addr & ~PRESHIFT(RTW_BB_ADDR_MASK)) == 0);
68 RTW_WRITE(regs, RTW_BB,
69 LSHIFT(addr, RTW_BB_ADDR_MASK) | RTW_BB_RD_MASK | RTW_BB_WR_MASK);
70 delay(10); /* XXX */
71 RTW_WBR(regs, RTW_BB, RTW_BB);
72 return MASK_AND_RSHIFT(RTW_READ(regs, RTW_BB), RTW_BB_RD_MASK);
73 }
74
75 int
76 rtw_bbp_write(struct rtw_regs *regs, u_int addr, u_int val)
77 {
78 #define BBP_WRITE_ITERS 50
79 #define BBP_WRITE_DELAY 1
80 int i;
81 uint32_t wrbbp, rdbbp;
82
83 RTW_DPRINTF(("%s: bbp[%u] <- %u\n", __func__, addr, val));
84
85 KASSERT((addr & ~PRESHIFT(RTW_BB_ADDR_MASK)) == 0);
86 KASSERT((val & ~PRESHIFT(RTW_BB_WR_MASK)) == 0);
87
88 wrbbp = LSHIFT(addr, RTW_BB_ADDR_MASK) | RTW_BB_WREN |
89 LSHIFT(val, RTW_BB_WR_MASK) | RTW_BB_RD_MASK,
90
91 rdbbp = LSHIFT(addr, RTW_BB_ADDR_MASK) |
92 RTW_BB_WR_MASK | RTW_BB_RD_MASK;
93
94 RTW_DPRINTF2(("%s: rdbbp = %#08x, wrbbp = %#08x\n", __func__,
95 rdbbp, wrbbp));
96
97 for (i = BBP_WRITE_ITERS; --i >= 0; ) {
98 RTW_RBW(regs, RTW_BB, RTW_BB);
99 RTW_WRITE(regs, RTW_BB, wrbbp);
100 RTW_SYNC(regs, RTW_BB, RTW_BB);
101 RTW_WRITE(regs, RTW_BB, rdbbp);
102 RTW_SYNC(regs, RTW_BB, RTW_BB);
103 delay(BBP_WRITE_DELAY); /* 1 microsecond */
104 if (MASK_AND_RSHIFT(RTW_READ(regs, RTW_BB),
105 RTW_BB_RD_MASK) == val) {
106 RTW_DPRINTF2(("%s: finished in %dus\n", __func__,
107 BBP_WRITE_DELAY * (BBP_WRITE_ITERS - i)));
108 return 0;
109 }
110 delay(BBP_WRITE_DELAY); /* again */
111 }
112 printf("%s: timeout\n", __func__);
113 return -1;
114 }
115
116 /* Help rtw_rf_hostwrite bang bits to RF over 3-wire interface. */
117 static __inline void
118 rtw_rf_hostbangbits(struct rtw_regs *regs, u_int32_t bits, int lo_to_hi,
119 u_int nbits)
120 {
121 int i;
122 u_int32_t mask, reg;
123
124 KASSERT(nbits <= 32);
125
126 RTW_DPRINTF(("%s: %u bits, %#08x, %s\n", __func__, nbits, bits,
127 (lo_to_hi) ? "lo to hi" : "hi to lo"));
128
129 reg = RTW_PHYCFG_HST;
130 RTW_WRITE(regs, RTW_PHYCFG, reg);
131 RTW_SYNC(regs, RTW_PHYCFG, RTW_PHYCFG);
132
133 if (lo_to_hi)
134 mask = 0x1;
135 else
136 mask = 1 << (nbits - 1);
137
138 for (i = 0; i < nbits; i++) {
139 RTW_DPRINTF2(("%s: bits %#08x mask %#08x -> bit %#08x\n",
140 __func__, bits, mask, bits & mask));
141
142 if ((bits & mask) != 0)
143 reg |= RTW_PHYCFG_HST_DATA;
144 else
145 reg &= ~RTW_PHYCFG_HST_DATA;
146
147 reg |= RTW_PHYCFG_HST_CLK;
148 RTW_WRITE(regs, RTW_PHYCFG, reg);
149 RTW_SYNC(regs, RTW_PHYCFG, RTW_PHYCFG);
150
151 DELAY(2); /* arbitrary delay */
152
153 reg &= ~RTW_PHYCFG_HST_CLK;
154 RTW_WRITE(regs, RTW_PHYCFG, reg);
155 RTW_SYNC(regs, RTW_PHYCFG, RTW_PHYCFG);
156
157 if (lo_to_hi)
158 mask <<= 1;
159 else
160 mask >>= 1;
161 }
162
163 reg |= RTW_PHYCFG_HST_EN;
164 KASSERT((reg & RTW_PHYCFG_HST_CLK) == 0);
165 RTW_WRITE(regs, RTW_PHYCFG, reg);
166 RTW_SYNC(regs, RTW_PHYCFG, RTW_PHYCFG);
167 }
168
169 /* Help rtw_rf_macwrite: tell MAC to bang bits to RF over the 3-wire
170 * interface.
171 */
172 static __inline int
173 rtw_rf_macbangbits(struct rtw_regs *regs, u_int32_t reg)
174 {
175 int i;
176
177 RTW_DPRINTF(("%s: %#08x\n", __func__, reg));
178
179 RTW_WRITE(regs, RTW_PHYCFG, RTW_PHYCFG_MAC_POLL | reg);
180
181 RTW_WBR(regs, RTW_PHYCFG, RTW_PHYCFG);
182 for (i = rtw_macbangbits_timeout; --i >= 0; delay(1)) {
183 if ((RTW_READ(regs, RTW_PHYCFG) & RTW_PHYCFG_MAC_POLL) == 0) {
184 RTW_DPRINTF2(("%s: finished in %dus\n", __func__,
185 rtw_macbangbits_timeout - i));
186 return 0;
187 }
188 RTW_RBR(regs, RTW_PHYCFG, RTW_PHYCFG); /* XXX paranoia? */
189 }
190
191 printf("%s: RTW_PHYCFG_MAC_POLL still set.\n", __func__);
192 return -1;
193 }
194
195 static u_int32_t
196 rtw_grf5101_host_crypt(u_int addr, u_int32_t val)
197 {
198 /* TBD */
199 return 0;
200 }
201
202 static u_int32_t
203 rtw_grf5101_mac_crypt(u_int addr, u_int32_t val)
204 {
205 u_int32_t data_and_addr;
206 #define EXTRACT_NIBBLE(d, which) (((d) >> (4 * (which))) & 0xf)
207 static u_int8_t caesar[16] = {0x0, 0x8, 0x4, 0xc,
208 0x2, 0xa, 0x6, 0xe,
209 0x1, 0x9, 0x5, 0xd,
210 0x3, 0xb, 0x7, 0xf};
211
212 data_and_addr = caesar[EXTRACT_NIBBLE(val, 2)] |
213 (caesar[EXTRACT_NIBBLE(val, 1)] << 4) |
214 (caesar[EXTRACT_NIBBLE(val, 0)] << 8) |
215 (caesar[(addr >> 1) & 0xf] << 12) |
216 ((addr & 0x1) << 16) |
217 (caesar[EXTRACT_NIBBLE(val, 3)] << 24);
218 return LSHIFT(data_and_addr,
219 RTW_PHYCFG_MAC_PHILIPS_ADDR_MASK|RTW_PHYCFG_MAC_PHILIPS_DATA_MASK);
220 #undef EXTRACT_NIBBLE
221 }
222
223 static __inline const char *
224 rtw_rfchipid_string(enum rtw_rfchipid rfchipid)
225 {
226 switch (rfchipid) {
227 case RTW_RFCHIPID_MAXIM:
228 return "Maxim";
229 case RTW_RFCHIPID_PHILIPS:
230 return "Philips";
231 case RTW_RFCHIPID_GCT:
232 return "GCT";
233 case RTW_RFCHIPID_RFMD:
234 return "RFMD";
235 case RTW_RFCHIPID_INTERSIL:
236 return "Intersil";
237 default:
238 return "unknown";
239 }
240 }
241
242 /* Bang bits over the 3-wire interface. */
243 int
244 rtw_rf_hostwrite(struct rtw_regs *regs, enum rtw_rfchipid rfchipid,
245 u_int addr, u_int32_t val)
246 {
247 u_int nbits;
248 int lo_to_hi;
249 u_int32_t bits;
250
251 RTW_DPRINTF(("%s: %s[%u] <- %#08x\n", __func__,
252 rtw_rfchipid_string(rfchipid), addr, val));
253
254 switch (rfchipid) {
255 case RTW_RFCHIPID_MAXIM:
256 nbits = 16;
257 lo_to_hi = 0;
258 bits = LSHIFT(val, MAX2820_TWI_DATA_MASK) |
259 LSHIFT(addr, MAX2820_TWI_ADDR_MASK);
260 break;
261 case RTW_RFCHIPID_PHILIPS:
262 KASSERT((addr & ~PRESHIFT(SA2400_TWI_ADDR_MASK)) == 0);
263 KASSERT((val & ~PRESHIFT(SA2400_TWI_DATA_MASK)) == 0);
264 bits = LSHIFT(val, SA2400_TWI_DATA_MASK) |
265 LSHIFT(addr, SA2400_TWI_ADDR_MASK) | SA2400_TWI_WREN;
266 nbits = 32;
267 lo_to_hi = 1;
268 break;
269 case RTW_RFCHIPID_GCT:
270 case RTW_RFCHIPID_RFMD:
271 KASSERT((addr & ~PRESHIFT(SI4126_TWI_ADDR_MASK)) == 0);
272 KASSERT((val & ~PRESHIFT(SI4126_TWI_DATA_MASK)) == 0);
273 if (rfchipid == RTW_RFCHIPID_GCT)
274 bits = rtw_grf5101_host_crypt(addr, val);
275 else {
276 bits = LSHIFT(val, SI4126_TWI_DATA_MASK) |
277 LSHIFT(addr, SI4126_TWI_ADDR_MASK);
278 }
279 nbits = 22;
280 lo_to_hi = 0;
281 break;
282 case RTW_RFCHIPID_INTERSIL:
283 default:
284 printf("%s: unknown rfchipid %d\n", __func__, rfchipid);
285 return -1;
286 }
287
288 rtw_rf_hostbangbits(regs, bits, lo_to_hi, nbits);
289
290 return 0;
291 }
292
293 static uint32_t
294 rtw_maxim_swizzle(u_int addr, uint32_t val)
295 {
296 uint32_t hidata, lodata;
297
298 KASSERT((val & ~(RTW_MAXIM_LODATA_MASK|RTW_MAXIM_HIDATA_MASK)) == 0);
299 lodata = MASK_AND_RSHIFT(val, RTW_MAXIM_LODATA_MASK);
300 hidata = MASK_AND_RSHIFT(val, RTW_MAXIM_HIDATA_MASK);
301 return LSHIFT(lodata, RTW_PHYCFG_MAC_MAXIM_LODATA_MASK) |
302 LSHIFT(hidata, RTW_PHYCFG_MAC_MAXIM_HIDATA_MASK) |
303 LSHIFT(addr, RTW_PHYCFG_MAC_MAXIM_ADDR_MASK);
304 }
305
306 /* Tell the MAC what to bang over the 3-wire interface. */
307 int
308 rtw_rf_macwrite(struct rtw_regs *regs, enum rtw_rfchipid rfchipid,
309 u_int addr, u_int32_t val)
310 {
311 uint32_t reg;
312
313 RTW_DPRINTF(("%s: %s[%u] <- %#08x\n", __func__,
314 rtw_rfchipid_string(rfchipid), addr, val));
315
316 switch (rfchipid) {
317 case RTW_RFCHIPID_GCT:
318 reg = rtw_grf5101_mac_crypt(addr, val);
319 break;
320 case RTW_RFCHIPID_MAXIM:
321 reg = rtw_maxim_swizzle(addr, val);
322 break;
323 default: /* XXX */
324 case RTW_RFCHIPID_PHILIPS:
325 KASSERT(
326 (addr & ~PRESHIFT(RTW_PHYCFG_MAC_PHILIPS_ADDR_MASK)) == 0);
327 KASSERT(
328 (val & ~PRESHIFT(RTW_PHYCFG_MAC_PHILIPS_DATA_MASK)) == 0);
329
330 reg = LSHIFT(addr, RTW_PHYCFG_MAC_PHILIPS_ADDR_MASK) |
331 LSHIFT(val, RTW_PHYCFG_MAC_PHILIPS_DATA_MASK);
332 }
333
334 switch (rfchipid) {
335 case RTW_RFCHIPID_GCT:
336 case RTW_RFCHIPID_MAXIM:
337 case RTW_RFCHIPID_RFMD:
338 reg |= RTW_PHYCFG_MAC_RFTYPE_RFMD;
339 break;
340 case RTW_RFCHIPID_INTERSIL:
341 reg |= RTW_PHYCFG_MAC_RFTYPE_INTERSIL;
342 break;
343 case RTW_RFCHIPID_PHILIPS:
344 reg |= RTW_PHYCFG_MAC_RFTYPE_PHILIPS;
345 break;
346 default:
347 printf("%s: unknown rfchipid %d\n", __func__, rfchipid);
348 return -1;
349 }
350
351 return rtw_rf_macbangbits(regs, reg);
352 }
353