1 /* $NetBSD: rtwphyio.c,v 1.16 2008/03/03 12:30:57 tsutsui Exp $ */
3 * Copyright (c) 2004, 2005 David Young. All rights reserved.
5 * Programmed for NetBSD by David Young.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
16 * THIS SOFTWARE IS PROVIDED BY David Young ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
19 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL David
20 * Young BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
30 * Control input/output with the Philips SA2400 RF front-end and
31 * the baseband processor built into the Realtek RTL8180.
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: rtwphyio.c,v 1.16 2008/03/03 12:30:57 tsutsui Exp $");
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/types.h>
40 #include <sys/device.h>
45 #include <net/if_media.h>
46 #include <net/if_ether.h>
48 #include <net80211/ieee80211_netbsd.h>
49 #include <net80211/ieee80211_radiotap.h>
50 #include <net80211/ieee80211_var.h>
52 #include <dev/ic/rtwreg.h>
53 #include <dev/ic/max2820reg.h>
54 #include <dev/ic/sa2400reg.h>
55 #include <dev/ic/si4136reg.h>
56 #include <dev/ic/rtwvar.h>
57 #include <dev/ic/rtwphyio.h>
58 #include <dev/ic/rtwphy.h>
60 static int rtw_macbangbits_timeout
= 100;
63 rtw_bbp_read(struct rtw_regs
*regs
, u_int addr
)
65 KASSERT((addr
& ~__SHIFTOUT_MASK(RTW_BB_ADDR_MASK
)) == 0);
66 RTW_WRITE(regs
, RTW_BB
,
67 __SHIFTIN(addr
, RTW_BB_ADDR_MASK
) | RTW_BB_RD_MASK
| RTW_BB_WR_MASK
);
69 RTW_WBR(regs
, RTW_BB
, RTW_BB
);
70 return __SHIFTOUT(RTW_READ(regs
, RTW_BB
), RTW_BB_RD_MASK
);
74 rtw_bbp_write(struct rtw_regs
*regs
, u_int addr
, u_int val
)
76 #define BBP_WRITE_ITERS 50
77 #define BBP_WRITE_DELAY 1
79 uint32_t wrbbp
, rdbbp
;
81 RTW_DPRINTF(RTW_DEBUG_PHYIO
,
82 ("%s: bbp[%u] <- %u\n", __func__
, addr
, val
));
84 KASSERT((addr
& ~__SHIFTOUT_MASK(RTW_BB_ADDR_MASK
)) == 0);
85 KASSERT((val
& ~__SHIFTOUT_MASK(RTW_BB_WR_MASK
)) == 0);
87 wrbbp
= __SHIFTIN(addr
, RTW_BB_ADDR_MASK
) | RTW_BB_WREN
|
88 __SHIFTIN(val
, RTW_BB_WR_MASK
) | RTW_BB_RD_MASK
,
90 rdbbp
= __SHIFTIN(addr
, RTW_BB_ADDR_MASK
) |
91 RTW_BB_WR_MASK
| RTW_BB_RD_MASK
;
93 RTW_DPRINTF(RTW_DEBUG_PHYIO
,
94 ("%s: rdbbp = %#08x, wrbbp = %#08x\n", __func__
, rdbbp
, wrbbp
));
96 for (i
= BBP_WRITE_ITERS
; --i
>= 0; ) {
97 RTW_RBW(regs
, RTW_BB
, RTW_BB
);
98 RTW_WRITE(regs
, RTW_BB
, wrbbp
);
99 RTW_SYNC(regs
, RTW_BB
, RTW_BB
);
100 RTW_WRITE(regs
, RTW_BB
, rdbbp
);
101 RTW_SYNC(regs
, RTW_BB
, RTW_BB
);
102 delay(BBP_WRITE_DELAY
); /* 1 microsecond */
103 if (__SHIFTOUT(RTW_READ(regs
, RTW_BB
),
104 RTW_BB_RD_MASK
) == val
) {
105 RTW_DPRINTF(RTW_DEBUG_PHYIO
,
106 ("%s: finished in %dus\n", __func__
,
107 BBP_WRITE_DELAY
* (BBP_WRITE_ITERS
- i
)));
110 delay(BBP_WRITE_DELAY
); /* again */
112 printf("%s: timeout\n", __func__
);
116 /* Help rtw_rf_hostwrite bang bits to RF over 3-wire interface. */
118 rtw_rf_hostbangbits(struct rtw_regs
*regs
, uint32_t bits
, int lo_to_hi
,
124 KASSERT(nbits
<= 32);
126 RTW_DPRINTF(RTW_DEBUG_PHYIO
,
127 ("%s: %u bits, %#08x, %s\n", __func__
, nbits
, bits
,
128 (lo_to_hi
) ? "lo to hi" : "hi to lo"));
130 reg
= RTW_PHYCFG_HST
;
131 RTW_WRITE(regs
, RTW_PHYCFG
, reg
);
132 RTW_SYNC(regs
, RTW_PHYCFG
, RTW_PHYCFG
);
137 mask
= 1 << (nbits
- 1);
139 for (i
= 0; i
< nbits
; i
++) {
140 RTW_DPRINTF(RTW_DEBUG_PHYBITIO
,
141 ("%s: bits %#08x mask %#08x -> bit %#08x\n",
142 __func__
, bits
, mask
, bits
& mask
));
144 if ((bits
& mask
) != 0)
145 reg
|= RTW_PHYCFG_HST_DATA
;
147 reg
&= ~RTW_PHYCFG_HST_DATA
;
149 reg
|= RTW_PHYCFG_HST_CLK
;
150 RTW_WRITE(regs
, RTW_PHYCFG
, reg
);
151 RTW_SYNC(regs
, RTW_PHYCFG
, RTW_PHYCFG
);
153 DELAY(2); /* arbitrary delay */
155 reg
&= ~RTW_PHYCFG_HST_CLK
;
156 RTW_WRITE(regs
, RTW_PHYCFG
, reg
);
157 RTW_SYNC(regs
, RTW_PHYCFG
, RTW_PHYCFG
);
165 reg
|= RTW_PHYCFG_HST_EN
;
166 KASSERT((reg
& RTW_PHYCFG_HST_CLK
) == 0);
167 RTW_WRITE(regs
, RTW_PHYCFG
, reg
);
168 RTW_SYNC(regs
, RTW_PHYCFG
, RTW_PHYCFG
);
171 /* Help rtw_rf_macwrite: tell MAC to bang bits to RF over the 3-wire
175 rtw_rf_macbangbits(struct rtw_regs
*regs
, uint32_t reg
)
179 RTW_DPRINTF(RTW_DEBUG_PHY
, ("%s: %#08x\n", __func__
, reg
));
181 RTW_WRITE(regs
, RTW_PHYCFG
, RTW_PHYCFG_MAC_POLL
| reg
);
183 RTW_WBR(regs
, RTW_PHYCFG
, RTW_PHYCFG
);
185 for (i
= rtw_macbangbits_timeout
; --i
>= 0; delay(1)) {
186 if ((RTW_READ(regs
, RTW_PHYCFG
) & RTW_PHYCFG_MAC_POLL
) == 0) {
187 RTW_DPRINTF(RTW_DEBUG_PHY
,
188 ("%s: finished in %dus\n", __func__
,
189 rtw_macbangbits_timeout
- i
));
192 RTW_RBR(regs
, RTW_PHYCFG
, RTW_PHYCFG
); /* XXX paranoia? */
195 printf("%s: RTW_PHYCFG_MAC_POLL still set.\n", __func__
);
200 rtw_grf5101_host_crypt(u_int addr
, uint32_t val
)
207 rtw_grf5101_mac_crypt(u_int addr
, uint32_t val
)
209 uint32_t data_and_addr
;
210 #define EXTRACT_NIBBLE(d, which) (((d) >> (4 * (which))) & 0xf)
211 static uint8_t caesar
[16] = {0x0, 0x8, 0x4, 0xc,
216 data_and_addr
= caesar
[EXTRACT_NIBBLE(val
, 2)] |
217 (caesar
[EXTRACT_NIBBLE(val
, 1)] << 4) |
218 (caesar
[EXTRACT_NIBBLE(val
, 0)] << 8) |
219 (caesar
[(addr
>> 1) & 0xf] << 12) |
220 ((addr
& 0x1) << 16) |
221 (caesar
[EXTRACT_NIBBLE(val
, 3)] << 24);
222 return __SHIFTIN(data_and_addr
,
223 RTW_PHYCFG_MAC_PHILIPS_ADDR_MASK
|RTW_PHYCFG_MAC_PHILIPS_DATA_MASK
);
224 #undef EXTRACT_NIBBLE
227 static inline const char *
228 rtw_rfchipid_string(enum rtw_rfchipid rfchipid
)
231 case RTW_RFCHIPID_MAXIM
:
233 case RTW_RFCHIPID_PHILIPS
:
235 case RTW_RFCHIPID_GCT
:
237 case RTW_RFCHIPID_RFMD
:
239 case RTW_RFCHIPID_INTERSIL
:
246 /* Bang bits over the 3-wire interface. */
248 rtw_rf_hostwrite(struct rtw_regs
*regs
, enum rtw_rfchipid rfchipid
,
249 u_int addr
, uint32_t val
)
255 RTW_DPRINTF(RTW_DEBUG_PHYIO
, ("%s: %s[%u] <- %#08x\n", __func__
,
256 rtw_rfchipid_string(rfchipid
), addr
, val
));
259 case RTW_RFCHIPID_MAXIM
:
262 bits
= __SHIFTIN(val
, MAX2820_TWI_DATA_MASK
) |
263 __SHIFTIN(addr
, MAX2820_TWI_ADDR_MASK
);
265 case RTW_RFCHIPID_PHILIPS
:
266 KASSERT((addr
& ~__SHIFTOUT_MASK(SA2400_TWI_ADDR_MASK
)) == 0);
267 KASSERT((val
& ~__SHIFTOUT_MASK(SA2400_TWI_DATA_MASK
)) == 0);
268 bits
= __SHIFTIN(val
, SA2400_TWI_DATA_MASK
) |
269 __SHIFTIN(addr
, SA2400_TWI_ADDR_MASK
) | SA2400_TWI_WREN
;
273 case RTW_RFCHIPID_GCT
:
274 KASSERT((addr
& ~__SHIFTOUT_MASK(SI4126_TWI_ADDR_MASK
)) == 0);
275 KASSERT((val
& ~__SHIFTOUT_MASK(SI4126_TWI_DATA_MASK
)) == 0);
276 bits
= rtw_grf5101_host_crypt(addr
, val
);
280 case RTW_RFCHIPID_RFMD
:
281 KASSERT((addr
& ~__SHIFTOUT_MASK(SI4126_TWI_ADDR_MASK
)) == 0);
282 KASSERT((val
& ~__SHIFTOUT_MASK(SI4126_TWI_DATA_MASK
)) == 0);
283 bits
= __SHIFTIN(val
, SI4126_TWI_DATA_MASK
) |
284 __SHIFTIN(addr
, SI4126_TWI_ADDR_MASK
);
288 case RTW_RFCHIPID_INTERSIL
:
290 printf("%s: unknown rfchipid %d\n", __func__
, rfchipid
);
294 rtw_rf_hostbangbits(regs
, bits
, lo_to_hi
, nbits
);
300 rtw_maxim_swizzle(u_int addr
, uint32_t val
)
302 uint32_t hidata
, lodata
;
304 KASSERT((val
& ~(RTW_MAXIM_LODATA_MASK
|RTW_MAXIM_HIDATA_MASK
)) == 0);
305 lodata
= __SHIFTOUT(val
, RTW_MAXIM_LODATA_MASK
);
306 hidata
= __SHIFTOUT(val
, RTW_MAXIM_HIDATA_MASK
);
307 return __SHIFTIN(lodata
, RTW_PHYCFG_MAC_MAXIM_LODATA_MASK
) |
308 __SHIFTIN(hidata
, RTW_PHYCFG_MAC_MAXIM_HIDATA_MASK
) |
309 __SHIFTIN(addr
, RTW_PHYCFG_MAC_MAXIM_ADDR_MASK
);
312 /* Tell the MAC what to bang over the 3-wire interface. */
314 rtw_rf_macwrite(struct rtw_regs
*regs
, enum rtw_rfchipid rfchipid
,
315 u_int addr
, uint32_t val
)
319 RTW_DPRINTF(RTW_DEBUG_PHYIO
, ("%s: %s[%u] <- %#08x\n", __func__
,
320 rtw_rfchipid_string(rfchipid
), addr
, val
));
323 case RTW_RFCHIPID_GCT
:
324 reg
= rtw_grf5101_mac_crypt(addr
, val
);
326 case RTW_RFCHIPID_MAXIM
:
327 reg
= rtw_maxim_swizzle(addr
, val
);
330 case RTW_RFCHIPID_PHILIPS
:
332 (addr
& ~__SHIFTOUT_MASK(RTW_PHYCFG_MAC_PHILIPS_ADDR_MASK
)) == 0);
334 (val
& ~__SHIFTOUT_MASK(RTW_PHYCFG_MAC_PHILIPS_DATA_MASK
)) == 0);
336 reg
= __SHIFTIN(addr
, RTW_PHYCFG_MAC_PHILIPS_ADDR_MASK
) |
337 __SHIFTIN(val
, RTW_PHYCFG_MAC_PHILIPS_DATA_MASK
);
341 case RTW_RFCHIPID_GCT
:
342 case RTW_RFCHIPID_MAXIM
:
343 case RTW_RFCHIPID_RFMD
:
344 reg
|= RTW_PHYCFG_MAC_RFTYPE_RFMD
;
346 case RTW_RFCHIPID_INTERSIL
:
347 reg
|= RTW_PHYCFG_MAC_RFTYPE_INTERSIL
;
349 case RTW_RFCHIPID_PHILIPS
:
350 reg
|= RTW_PHYCFG_MAC_RFTYPE_PHILIPS
;
353 printf("%s: unknown rfchipid %d\n", __func__
, rfchipid
);
357 return rtw_rf_macbangbits(regs
, reg
);