mailer.conf - Add missing send-mail for dma.
[dragonfly.git] / sys / dev / netif / rtw / rtwphyio.c
1 /*
2  * Copyright (c) 2004, 2005 David Young.  All rights reserved.
3  *
4  * Programmed for NetBSD by David Young.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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
16  *    written permission.
17  *
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
29  * OF SUCH DAMAGE.
30  *
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 $
33  */
34
35 /*
36  * Control input/output with the Philips SA2400 RF front-end and
37  * the baseband processor built into the Realtek RTL8180.
38  */
39
40 #include <sys/param.h>
41 #include <sys/bitops.h>
42 #include <sys/bus.h>
43 #include <sys/socket.h>
44
45 #include <net/if.h>
46 #include <net/if_arp.h>
47 #include <net/if_var.h>
48 #include <net/if_media.h>
49
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>
53
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>
61
62 static int      rtw_macbangbits_timeout = 100;
63
64 uint8_t
65 rtw_bbp_read(struct rtw_regs *regs, u_int addr)
66 {
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);
71         DELAY(10);      /* XXX */
72         RTW_WBR(regs, RTW_BB, RTW_BB);
73         return __SHIFTOUT(RTW_READ(regs, RTW_BB), RTW_BB_RD_MASK);
74 }
75
76 int
77 rtw_bbp_write(struct rtw_regs *regs, u_int addr, u_int val)
78 {
79 #define BBP_WRITE_ITERS 50
80 #define BBP_WRITE_DELAY 1
81         int i;
82         uint32_t wrbbp, rdbbp;
83
84         RTW_DPRINTF(RTW_DEBUG_PHYIO,
85             ("%s: bbp[%u] <- %u\n", __func__, addr, val));
86
87         KKASSERT((addr & ~__SHIFTOUT_MASK(RTW_BB_ADDR_MASK)) == 0);
88         KKASSERT((val & ~__SHIFTOUT_MASK(RTW_BB_WR_MASK)) == 0);
89
90         wrbbp = __SHIFTIN(addr, RTW_BB_ADDR_MASK) | RTW_BB_WREN |
91                 __SHIFTIN(val, RTW_BB_WR_MASK) | RTW_BB_RD_MASK,
92
93         rdbbp = __SHIFTIN(addr, RTW_BB_ADDR_MASK) |
94                 RTW_BB_WR_MASK | RTW_BB_RD_MASK;
95
96         RTW_DPRINTF(RTW_DEBUG_PHYIO,
97             ("%s: rdbbp = %#08x, wrbbp = %#08x\n", __func__, rdbbp, wrbbp));
98
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)));
111                         return 0;
112                 }
113                 DELAY(BBP_WRITE_DELAY); /* again */
114         }
115         kprintf("%s: timeout\n", __func__);
116         return -1;
117 }
118
119 /* Help rtw_rf_hostwrite bang bits to RF over 3-wire interface. */
120 static __inline void
121 rtw_rf_hostbangbits(struct rtw_regs *regs, uint32_t bits, int lo_to_hi,
122                     u_int nbits)
123 {
124         int i;
125         uint32_t mask, reg;
126
127         KKASSERT(nbits <= 32);
128
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"));
132
133         reg = RTW_PHYCFG_HST;
134         RTW_WRITE(regs, RTW_PHYCFG, reg);
135         RTW_SYNC(regs, RTW_PHYCFG, RTW_PHYCFG);
136
137         if (lo_to_hi)
138                 mask = 0x1;
139         else
140                 mask = 1 << (nbits - 1);
141
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));
146
147                 if ((bits & mask) != 0)
148                         reg |= RTW_PHYCFG_HST_DATA;
149                 else
150                         reg &= ~RTW_PHYCFG_HST_DATA;
151
152                 reg |= RTW_PHYCFG_HST_CLK;
153                 RTW_WRITE(regs, RTW_PHYCFG, reg);
154                 RTW_SYNC(regs, RTW_PHYCFG, RTW_PHYCFG);
155
156                 DELAY(2);       /* arbitrary delay */
157
158                 reg &= ~RTW_PHYCFG_HST_CLK;
159                 RTW_WRITE(regs, RTW_PHYCFG, reg);
160                 RTW_SYNC(regs, RTW_PHYCFG, RTW_PHYCFG);
161
162                 if (lo_to_hi)
163                         mask <<= 1;
164                 else
165                         mask >>= 1;
166         }
167
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);
172 }
173
174 /* Help rtw_rf_macwrite: tell MAC to bang bits to RF over the 3-wire
175  * interface.
176  */
177 static __inline int
178 rtw_rf_macbangbits(struct rtw_regs *regs, uint32_t reg)
179 {
180         int i;
181
182         RTW_DPRINTF(RTW_DEBUG_PHY, ("%s: %#08x\n", __func__, reg));
183
184         RTW_WRITE(regs, RTW_PHYCFG, RTW_PHYCFG_MAC_POLL | reg);
185
186         RTW_WBR(regs, RTW_PHYCFG, RTW_PHYCFG);
187
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));
193                         return 0;
194                 }
195                 RTW_RBR(regs, RTW_PHYCFG, RTW_PHYCFG);  /* XXX paranoia? */
196         }
197
198         kprintf("%s: RTW_PHYCFG_MAC_POLL still set.\n", __func__);
199         return -1;
200 }
201
202 static uint32_t
203 rtw_grf5101_host_crypt(u_int addr, uint32_t val)
204 {
205         /* TBD */
206         return 0;
207 }
208
209 static uint32_t
210 rtw_grf5101_mac_crypt(u_int addr, uint32_t val)
211 {
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,
215                                       0x2, 0xa, 0x6, 0xe,
216                                       0x1, 0x9, 0x5, 0xd,
217                                       0x3, 0xb, 0x7, 0xf};
218
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
228 }
229
230 static __inline const char *
231 rtw_rfchipid_string(enum rtw_rfchipid rfchipid)
232 {
233         switch (rfchipid) {
234         case RTW_RFCHIPID_MAXIM:
235                 return "Maxim";
236         case RTW_RFCHIPID_PHILIPS:
237                 return "Philips";
238         case RTW_RFCHIPID_GCT:
239                 return "GCT";
240         case RTW_RFCHIPID_RFMD:
241                 return "RFMD";
242         case RTW_RFCHIPID_INTERSIL:
243                 return "Intersil";
244         default:
245                 return "unknown";
246         }
247 }
248
249 /* Bang bits over the 3-wire interface. */
250 int
251 rtw_rf_hostwrite(struct rtw_regs *regs, enum rtw_rfchipid rfchipid,
252                  u_int addr, uint32_t val)
253 {
254         u_int nbits;
255         int lo_to_hi;
256         uint32_t bits;
257
258         RTW_DPRINTF(RTW_DEBUG_PHYIO, ("%s: %s[%u] <- %#08x\n", __func__,
259             rtw_rfchipid_string(rfchipid), addr, val));
260
261         switch (rfchipid) {
262         case RTW_RFCHIPID_MAXIM:
263                 nbits = 16;
264                 lo_to_hi = 0;
265                 bits = __SHIFTIN(val, MAX2820_TWI_DATA_MASK) |
266                        __SHIFTIN(addr, MAX2820_TWI_ADDR_MASK);
267                 break;
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;
273                 nbits = 32;
274                 lo_to_hi = 1;
275                 break;
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);
280                 nbits = 21;
281                 lo_to_hi = 1;
282                 break;
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);
288                 nbits = 22;
289                 lo_to_hi = 0;
290                 break;
291         case RTW_RFCHIPID_INTERSIL:
292         default:
293                 kprintf("%s: unknown rfchipid %d\n", __func__, rfchipid);
294                 return -1;
295         }
296
297         rtw_rf_hostbangbits(regs, bits, lo_to_hi, nbits);
298
299         return 0;
300 }
301
302 static uint32_t
303 rtw_maxim_swizzle(u_int addr, uint32_t val)
304 {
305         uint32_t hidata, lodata;
306
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);
313 }
314
315 /* Tell the MAC what to bang over the 3-wire interface. */
316 int
317 rtw_rf_macwrite(struct rtw_regs *regs, enum rtw_rfchipid rfchipid,
318                 u_int addr, uint32_t val)
319 {
320         uint32_t reg;
321
322         RTW_DPRINTF(RTW_DEBUG_PHYIO, ("%s: %s[%u] <- %#08x\n", __func__,
323             rtw_rfchipid_string(rfchipid), addr, val));
324
325         switch (rfchipid) {
326         case RTW_RFCHIPID_GCT:
327                 reg = rtw_grf5101_mac_crypt(addr, val);
328                 break;
329         case RTW_RFCHIPID_MAXIM:
330                 reg = rtw_maxim_swizzle(addr, val);
331                 break;
332         default:                /* XXX */
333         case RTW_RFCHIPID_PHILIPS:
334                 KKASSERT(
335         (addr & ~__SHIFTOUT_MASK(RTW_PHYCFG_MAC_PHILIPS_ADDR_MASK)) == 0);
336                 KKASSERT(
337         (val & ~__SHIFTOUT_MASK(RTW_PHYCFG_MAC_PHILIPS_DATA_MASK)) == 0);
338
339                 reg = __SHIFTIN(addr, RTW_PHYCFG_MAC_PHILIPS_ADDR_MASK) |
340                       __SHIFTIN(val, RTW_PHYCFG_MAC_PHILIPS_DATA_MASK);
341         }
342
343         switch (rfchipid) {
344         case RTW_RFCHIPID_GCT:
345         case RTW_RFCHIPID_MAXIM:
346         case RTW_RFCHIPID_RFMD:
347                 reg |= RTW_PHYCFG_MAC_RFTYPE_RFMD;
348                 break;
349         case RTW_RFCHIPID_INTERSIL:
350                 reg |= RTW_PHYCFG_MAC_RFTYPE_INTERSIL;
351                 break;
352         case RTW_RFCHIPID_PHILIPS:
353                 reg |= RTW_PHYCFG_MAC_RFTYPE_PHILIPS;
354                 break;
355         default:
356                 kprintf("%s: unknown rfchipid %d\n", __func__, rfchipid);
357                 return -1;
358         }
359
360         return rtw_rf_macbangbits(regs, reg);
361 }