2 * Copyright (c) 2004, 2005 David Young. All rights reserved.
4 * Programmed for NetBSD by David Young.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of David Young may not be used to endorse or promote
15 * products derived from this software without specific prior
18 * THIS SOFTWARE IS PROVIDED BY David Young ``AS IS'' AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
21 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL David
22 * Young BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
24 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
31 * $NetBSD: rtwphyio.c,v 1.11 2006/03/08 00:24:06 dyoung Exp $
32 * $DragonFly: src/sys/dev/netif/rtw/rtwphyio.c,v 1.5 2008/01/15 09:01:13 sephe Exp $
36 * Control input/output with the Philips SA2400 RF front-end and
37 * the baseband processor built into the Realtek RTL8180.
40 #include <sys/param.h>
41 #include <sys/bitops.h>
43 #include <sys/socket.h>
46 #include <net/if_arp.h>
47 #include <net/if_var.h>
48 #include <net/if_media.h>
50 #include <netproto/802_11/ieee80211_var.h>
51 #include <netproto/802_11/ieee80211_radiotap.h>
52 #include <netproto/802_11/wlan_ratectl/onoe/ieee80211_onoe_param.h>
54 #include <dev/netif/rtw/rtwreg.h>
55 #include <dev/netif/rtw/max2820reg.h>
56 #include <dev/netif/rtw/sa2400reg.h>
57 #include <dev/netif/rtw/si4136reg.h>
58 #include <dev/netif/rtw/rtwvar.h>
59 #include <dev/netif/rtw/rtwphyio.h>
60 #include <dev/netif/rtw/rtwphy.h>
62 static int rtw_macbangbits_timeout = 100;
65 rtw_bbp_read(struct rtw_regs *regs, u_int addr)
67 KKASSERT((addr & ~__SHIFTOUT_MASK(RTW_BB_ADDR_MASK)) == 0);
68 RTW_WRITE(regs, RTW_BB,
69 __SHIFTIN(addr, RTW_BB_ADDR_MASK) |
70 RTW_BB_RD_MASK | RTW_BB_WR_MASK);
72 RTW_WBR(regs, RTW_BB, RTW_BB);
73 return __SHIFTOUT(RTW_READ(regs, RTW_BB), RTW_BB_RD_MASK);
77 rtw_bbp_write(struct rtw_regs *regs, u_int addr, u_int val)
79 #define BBP_WRITE_ITERS 50
80 #define BBP_WRITE_DELAY 1
82 uint32_t wrbbp, rdbbp;
84 RTW_DPRINTF(RTW_DEBUG_PHYIO,
85 ("%s: bbp[%u] <- %u\n", __func__, addr, val));
87 KKASSERT((addr & ~__SHIFTOUT_MASK(RTW_BB_ADDR_MASK)) == 0);
88 KKASSERT((val & ~__SHIFTOUT_MASK(RTW_BB_WR_MASK)) == 0);
90 wrbbp = __SHIFTIN(addr, RTW_BB_ADDR_MASK) | RTW_BB_WREN |
91 __SHIFTIN(val, RTW_BB_WR_MASK) | RTW_BB_RD_MASK,
93 rdbbp = __SHIFTIN(addr, RTW_BB_ADDR_MASK) |
94 RTW_BB_WR_MASK | RTW_BB_RD_MASK;
96 RTW_DPRINTF(RTW_DEBUG_PHYIO,
97 ("%s: rdbbp = %#08x, wrbbp = %#08x\n", __func__, rdbbp, wrbbp));
99 for (i = BBP_WRITE_ITERS; --i >= 0; ) {
100 RTW_RBW(regs, RTW_BB, RTW_BB);
101 RTW_WRITE(regs, RTW_BB, wrbbp);
102 RTW_SYNC(regs, RTW_BB, RTW_BB);
103 RTW_WRITE(regs, RTW_BB, rdbbp);
104 RTW_SYNC(regs, RTW_BB, RTW_BB);
105 DELAY(BBP_WRITE_DELAY); /* 1 microsecond */
106 if (__SHIFTOUT(RTW_READ(regs, RTW_BB),
107 RTW_BB_RD_MASK) == val) {
108 RTW_DPRINTF(RTW_DEBUG_PHYIO,
109 ("%s: finished in %dus\n", __func__,
110 BBP_WRITE_DELAY * (BBP_WRITE_ITERS - i)));
113 DELAY(BBP_WRITE_DELAY); /* again */
115 kprintf("%s: timeout\n", __func__);
119 /* Help rtw_rf_hostwrite bang bits to RF over 3-wire interface. */
121 rtw_rf_hostbangbits(struct rtw_regs *regs, uint32_t bits, int lo_to_hi,
127 KKASSERT(nbits <= 32);
129 RTW_DPRINTF(RTW_DEBUG_PHYIO,
130 ("%s: %u bits, %#08x, %s\n", __func__, nbits, bits,
131 (lo_to_hi) ? "lo to hi" : "hi to lo"));
133 reg = RTW_PHYCFG_HST;
134 RTW_WRITE(regs, RTW_PHYCFG, reg);
135 RTW_SYNC(regs, RTW_PHYCFG, RTW_PHYCFG);
140 mask = 1 << (nbits - 1);
142 for (i = 0; i < nbits; i++) {
143 RTW_DPRINTF(RTW_DEBUG_PHYBITIO,
144 ("%s: bits %#08x mask %#08x -> bit %#08x\n",
145 __func__, bits, mask, bits & mask));
147 if ((bits & mask) != 0)
148 reg |= RTW_PHYCFG_HST_DATA;
150 reg &= ~RTW_PHYCFG_HST_DATA;
152 reg |= RTW_PHYCFG_HST_CLK;
153 RTW_WRITE(regs, RTW_PHYCFG, reg);
154 RTW_SYNC(regs, RTW_PHYCFG, RTW_PHYCFG);
156 DELAY(2); /* arbitrary delay */
158 reg &= ~RTW_PHYCFG_HST_CLK;
159 RTW_WRITE(regs, RTW_PHYCFG, reg);
160 RTW_SYNC(regs, RTW_PHYCFG, RTW_PHYCFG);
168 reg |= RTW_PHYCFG_HST_EN;
169 KKASSERT((reg & RTW_PHYCFG_HST_CLK) == 0);
170 RTW_WRITE(regs, RTW_PHYCFG, reg);
171 RTW_SYNC(regs, RTW_PHYCFG, RTW_PHYCFG);
174 /* Help rtw_rf_macwrite: tell MAC to bang bits to RF over the 3-wire
178 rtw_rf_macbangbits(struct rtw_regs *regs, uint32_t reg)
182 RTW_DPRINTF(RTW_DEBUG_PHY, ("%s: %#08x\n", __func__, reg));
184 RTW_WRITE(regs, RTW_PHYCFG, RTW_PHYCFG_MAC_POLL | reg);
186 RTW_WBR(regs, RTW_PHYCFG, RTW_PHYCFG);
188 for (i = rtw_macbangbits_timeout; --i >= 0; DELAY(1)) {
189 if ((RTW_READ(regs, RTW_PHYCFG) & RTW_PHYCFG_MAC_POLL) == 0) {
190 RTW_DPRINTF(RTW_DEBUG_PHY,
191 ("%s: finished in %dus\n", __func__,
192 rtw_macbangbits_timeout - i));
195 RTW_RBR(regs, RTW_PHYCFG, RTW_PHYCFG); /* XXX paranoia? */
198 kprintf("%s: RTW_PHYCFG_MAC_POLL still set.\n", __func__);
203 rtw_grf5101_host_crypt(u_int addr, uint32_t val)
210 rtw_grf5101_mac_crypt(u_int addr, uint32_t val)
212 uint32_t data_and_addr;
213 #define EXTRACT_NIBBLE(d, which) (((d) >> (4 * (which))) & 0xf)
214 static uint8_t caesar[16] = {0x0, 0x8, 0x4, 0xc,
219 data_and_addr = caesar[EXTRACT_NIBBLE(val, 2)] |
220 (caesar[EXTRACT_NIBBLE(val, 1)] << 4) |
221 (caesar[EXTRACT_NIBBLE(val, 0)] << 8) |
222 (caesar[(addr >> 1) & 0xf] << 12) |
223 ((addr & 0x1) << 16) |
224 (caesar[EXTRACT_NIBBLE(val, 3)] << 24);
225 return __SHIFTIN(data_and_addr,
226 RTW_PHYCFG_MAC_PHILIPS_ADDR_MASK|RTW_PHYCFG_MAC_PHILIPS_DATA_MASK);
227 #undef EXTRACT_NIBBLE
230 static __inline const char *
231 rtw_rfchipid_string(enum rtw_rfchipid rfchipid)
234 case RTW_RFCHIPID_MAXIM:
236 case RTW_RFCHIPID_PHILIPS:
238 case RTW_RFCHIPID_GCT:
240 case RTW_RFCHIPID_RFMD:
242 case RTW_RFCHIPID_INTERSIL:
249 /* Bang bits over the 3-wire interface. */
251 rtw_rf_hostwrite(struct rtw_regs *regs, enum rtw_rfchipid rfchipid,
252 u_int addr, uint32_t val)
258 RTW_DPRINTF(RTW_DEBUG_PHYIO, ("%s: %s[%u] <- %#08x\n", __func__,
259 rtw_rfchipid_string(rfchipid), addr, val));
262 case RTW_RFCHIPID_MAXIM:
265 bits = __SHIFTIN(val, MAX2820_TWI_DATA_MASK) |
266 __SHIFTIN(addr, MAX2820_TWI_ADDR_MASK);
268 case RTW_RFCHIPID_PHILIPS:
269 KKASSERT((addr & ~__SHIFTOUT_MASK(SA2400_TWI_ADDR_MASK)) == 0);
270 KKASSERT((val & ~__SHIFTOUT_MASK(SA2400_TWI_DATA_MASK)) == 0);
271 bits = __SHIFTIN(val, SA2400_TWI_DATA_MASK) |
272 __SHIFTIN(addr, SA2400_TWI_ADDR_MASK) | SA2400_TWI_WREN;
276 case RTW_RFCHIPID_GCT:
277 KKASSERT((addr & ~__SHIFTOUT_MASK(SI4126_TWI_ADDR_MASK)) == 0);
278 KKASSERT((val & ~__SHIFTOUT_MASK(SI4126_TWI_DATA_MASK)) == 0);
279 bits = rtw_grf5101_host_crypt(addr, val);
283 case RTW_RFCHIPID_RFMD:
284 KKASSERT((addr & ~__SHIFTOUT_MASK(SI4126_TWI_ADDR_MASK)) == 0);
285 KKASSERT((val & ~__SHIFTOUT_MASK(SI4126_TWI_DATA_MASK)) == 0);
286 bits = __SHIFTIN(val, SI4126_TWI_DATA_MASK) |
287 __SHIFTIN(addr, SI4126_TWI_ADDR_MASK);
291 case RTW_RFCHIPID_INTERSIL:
293 kprintf("%s: unknown rfchipid %d\n", __func__, rfchipid);
297 rtw_rf_hostbangbits(regs, bits, lo_to_hi, nbits);
303 rtw_maxim_swizzle(u_int addr, uint32_t val)
305 uint32_t hidata, lodata;
307 KKASSERT((val & ~(RTW_MAXIM_LODATA_MASK|RTW_MAXIM_HIDATA_MASK)) == 0);
308 lodata = __SHIFTOUT(val, RTW_MAXIM_LODATA_MASK);
309 hidata = __SHIFTOUT(val, RTW_MAXIM_HIDATA_MASK);
310 return __SHIFTIN(lodata, RTW_PHYCFG_MAC_MAXIM_LODATA_MASK) |
311 __SHIFTIN(hidata, RTW_PHYCFG_MAC_MAXIM_HIDATA_MASK) |
312 __SHIFTIN(addr, RTW_PHYCFG_MAC_MAXIM_ADDR_MASK);
315 /* Tell the MAC what to bang over the 3-wire interface. */
317 rtw_rf_macwrite(struct rtw_regs *regs, enum rtw_rfchipid rfchipid,
318 u_int addr, uint32_t val)
322 RTW_DPRINTF(RTW_DEBUG_PHYIO, ("%s: %s[%u] <- %#08x\n", __func__,
323 rtw_rfchipid_string(rfchipid), addr, val));
326 case RTW_RFCHIPID_GCT:
327 reg = rtw_grf5101_mac_crypt(addr, val);
329 case RTW_RFCHIPID_MAXIM:
330 reg = rtw_maxim_swizzle(addr, val);
333 case RTW_RFCHIPID_PHILIPS:
335 (addr & ~__SHIFTOUT_MASK(RTW_PHYCFG_MAC_PHILIPS_ADDR_MASK)) == 0);
337 (val & ~__SHIFTOUT_MASK(RTW_PHYCFG_MAC_PHILIPS_DATA_MASK)) == 0);
339 reg = __SHIFTIN(addr, RTW_PHYCFG_MAC_PHILIPS_ADDR_MASK) |
340 __SHIFTIN(val, RTW_PHYCFG_MAC_PHILIPS_DATA_MASK);
344 case RTW_RFCHIPID_GCT:
345 case RTW_RFCHIPID_MAXIM:
346 case RTW_RFCHIPID_RFMD:
347 reg |= RTW_PHYCFG_MAC_RFTYPE_RFMD;
349 case RTW_RFCHIPID_INTERSIL:
350 reg |= RTW_PHYCFG_MAC_RFTYPE_INTERSIL;
352 case RTW_RFCHIPID_PHILIPS:
353 reg |= RTW_PHYCFG_MAC_RFTYPE_PHILIPS;
356 kprintf("%s: unknown rfchipid %d\n", __func__, rfchipid);
360 return rtw_rf_macbangbits(regs, reg);