xref: /netbsd-src/sys/dev/ic/rtwphyio.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /* $NetBSD: rtwphyio.c,v 1.15 2007/10/19 12:00:00 ad 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.15 2007/10/19 12:00:00 ad Exp $");
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/types.h>
43 
44 #include <sys/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_netbsd.h>
51 #include <net80211/ieee80211_radiotap.h>
52 #include <net80211/ieee80211_var.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 uint8_t
65 rtw_bbp_read(struct rtw_regs *regs, u_int addr)
66 {
67 	KASSERT((addr & ~__SHIFTOUT_MASK(RTW_BB_ADDR_MASK)) == 0);
68 	RTW_WRITE(regs, RTW_BB,
69 	    __SHIFTIN(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 __SHIFTOUT(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(RTW_DEBUG_PHYIO,
84 	    ("%s: bbp[%u] <- %u\n", __func__, addr, val));
85 
86 	KASSERT((addr & ~__SHIFTOUT_MASK(RTW_BB_ADDR_MASK)) == 0);
87 	KASSERT((val & ~__SHIFTOUT_MASK(RTW_BB_WR_MASK)) == 0);
88 
89 	wrbbp = __SHIFTIN(addr, RTW_BB_ADDR_MASK) | RTW_BB_WREN |
90 	    __SHIFTIN(val, RTW_BB_WR_MASK) | RTW_BB_RD_MASK,
91 
92 	rdbbp = __SHIFTIN(addr, RTW_BB_ADDR_MASK) |
93 	    RTW_BB_WR_MASK | RTW_BB_RD_MASK;
94 
95 	RTW_DPRINTF(RTW_DEBUG_PHYIO,
96 	    ("%s: rdbbp = %#08x, wrbbp = %#08x\n", __func__, rdbbp, wrbbp));
97 
98 	for (i = BBP_WRITE_ITERS; --i >= 0; ) {
99 		RTW_RBW(regs, RTW_BB, RTW_BB);
100 		RTW_WRITE(regs, RTW_BB, wrbbp);
101 		RTW_SYNC(regs, RTW_BB, RTW_BB);
102 		RTW_WRITE(regs, RTW_BB, rdbbp);
103 		RTW_SYNC(regs, RTW_BB, RTW_BB);
104 		delay(BBP_WRITE_DELAY);	/* 1 microsecond */
105 		if (__SHIFTOUT(RTW_READ(regs, RTW_BB),
106 		                    RTW_BB_RD_MASK) == val) {
107 			RTW_DPRINTF(RTW_DEBUG_PHYIO,
108 			    ("%s: finished in %dus\n", __func__,
109 			    BBP_WRITE_DELAY * (BBP_WRITE_ITERS - i)));
110 			return 0;
111 		}
112 		delay(BBP_WRITE_DELAY);	/* again */
113 	}
114 	printf("%s: timeout\n", __func__);
115 	return -1;
116 }
117 
118 /* Help rtw_rf_hostwrite bang bits to RF over 3-wire interface. */
119 static inline void
120 rtw_rf_hostbangbits(struct rtw_regs *regs, uint32_t bits, int lo_to_hi,
121     u_int nbits)
122 {
123 	int i;
124 	uint32_t mask, reg;
125 
126 	KASSERT(nbits <= 32);
127 
128 	RTW_DPRINTF(RTW_DEBUG_PHYIO,
129 	    ("%s: %u bits, %#08x, %s\n", __func__, nbits, bits,
130 	    (lo_to_hi) ? "lo to hi" : "hi to lo"));
131 
132 	reg = RTW_PHYCFG_HST;
133 	RTW_WRITE(regs, RTW_PHYCFG, reg);
134 	RTW_SYNC(regs, RTW_PHYCFG, RTW_PHYCFG);
135 
136 	if (lo_to_hi)
137 		mask = 0x1;
138 	else
139 		mask = 1 << (nbits - 1);
140 
141 	for (i = 0; i < nbits; i++) {
142 		RTW_DPRINTF(RTW_DEBUG_PHYBITIO,
143 		    ("%s: bits %#08x mask %#08x -> bit %#08x\n",
144 		    __func__, bits, mask, bits & mask));
145 
146 		if ((bits & mask) != 0)
147 			reg |= RTW_PHYCFG_HST_DATA;
148 		else
149 			reg &= ~RTW_PHYCFG_HST_DATA;
150 
151 		reg |= RTW_PHYCFG_HST_CLK;
152 		RTW_WRITE(regs, RTW_PHYCFG, reg);
153 		RTW_SYNC(regs, RTW_PHYCFG, RTW_PHYCFG);
154 
155 		DELAY(2);	/* arbitrary delay */
156 
157 		reg &= ~RTW_PHYCFG_HST_CLK;
158 		RTW_WRITE(regs, RTW_PHYCFG, reg);
159 		RTW_SYNC(regs, RTW_PHYCFG, RTW_PHYCFG);
160 
161 		if (lo_to_hi)
162 			mask <<= 1;
163 		else
164 			mask >>= 1;
165 	}
166 
167 	reg |= RTW_PHYCFG_HST_EN;
168 	KASSERT((reg & RTW_PHYCFG_HST_CLK) == 0);
169 	RTW_WRITE(regs, RTW_PHYCFG, reg);
170 	RTW_SYNC(regs, RTW_PHYCFG, RTW_PHYCFG);
171 }
172 
173 /* Help rtw_rf_macwrite: tell MAC to bang bits to RF over the 3-wire
174  * interface.
175  */
176 static inline int
177 rtw_rf_macbangbits(struct rtw_regs *regs, uint32_t reg)
178 {
179 	int i;
180 
181 	RTW_DPRINTF(RTW_DEBUG_PHY, ("%s: %#08x\n", __func__, reg));
182 
183 	RTW_WRITE(regs, RTW_PHYCFG, RTW_PHYCFG_MAC_POLL | reg);
184 
185 	RTW_WBR(regs, RTW_PHYCFG, RTW_PHYCFG);
186 
187 	for (i = rtw_macbangbits_timeout; --i >= 0; delay(1)) {
188 		if ((RTW_READ(regs, RTW_PHYCFG) & RTW_PHYCFG_MAC_POLL) == 0) {
189 			RTW_DPRINTF(RTW_DEBUG_PHY,
190 			    ("%s: finished in %dus\n", __func__,
191 			    rtw_macbangbits_timeout - i));
192 			return 0;
193 		}
194 		RTW_RBR(regs, RTW_PHYCFG, RTW_PHYCFG);	/* XXX paranoia? */
195 	}
196 
197 	printf("%s: RTW_PHYCFG_MAC_POLL still set.\n", __func__);
198 	return -1;
199 }
200 
201 static uint32_t
202 rtw_grf5101_host_crypt(u_int addr, uint32_t val)
203 {
204 	/* TBD */
205 	return 0;
206 }
207 
208 static uint32_t
209 rtw_grf5101_mac_crypt(u_int addr, uint32_t val)
210 {
211 	uint32_t data_and_addr;
212 #define EXTRACT_NIBBLE(d, which) (((d) >> (4 * (which))) & 0xf)
213 	static uint8_t caesar[16] = {0x0, 0x8, 0x4, 0xc,
214 	                              0x2, 0xa, 0x6, 0xe,
215 				      0x1, 0x9, 0x5, 0xd,
216 				      0x3, 0xb, 0x7, 0xf};
217 
218 	data_and_addr =  caesar[EXTRACT_NIBBLE(val, 2)] |
219 	                (caesar[EXTRACT_NIBBLE(val, 1)] <<  4) |
220 	                (caesar[EXTRACT_NIBBLE(val, 0)] <<  8) |
221 	                (caesar[(addr >> 1) & 0xf]      << 12) |
222 	                ((addr & 0x1)                   << 16) |
223 	                (caesar[EXTRACT_NIBBLE(val, 3)] << 24);
224 	return __SHIFTIN(data_and_addr,
225 	    RTW_PHYCFG_MAC_PHILIPS_ADDR_MASK|RTW_PHYCFG_MAC_PHILIPS_DATA_MASK);
226 #undef EXTRACT_NIBBLE
227 }
228 
229 static inline const char *
230 rtw_rfchipid_string(enum rtw_rfchipid rfchipid)
231 {
232 	switch (rfchipid) {
233 	case RTW_RFCHIPID_MAXIM:
234 		return "Maxim";
235 	case RTW_RFCHIPID_PHILIPS:
236 		return "Philips";
237 	case RTW_RFCHIPID_GCT:
238 		return "GCT";
239 	case RTW_RFCHIPID_RFMD:
240 		return "RFMD";
241 	case RTW_RFCHIPID_INTERSIL:
242 		return "Intersil";
243 	default:
244 		return "unknown";
245 	}
246 }
247 
248 /* Bang bits over the 3-wire interface. */
249 int
250 rtw_rf_hostwrite(struct rtw_regs *regs, enum rtw_rfchipid rfchipid,
251     u_int addr, uint32_t val)
252 {
253 	u_int nbits;
254 	int lo_to_hi;
255 	uint32_t bits;
256 
257 	RTW_DPRINTF(RTW_DEBUG_PHYIO, ("%s: %s[%u] <- %#08x\n", __func__,
258 	    rtw_rfchipid_string(rfchipid), addr, val));
259 
260 	switch (rfchipid) {
261 	case RTW_RFCHIPID_MAXIM:
262 		nbits = 16;
263 		lo_to_hi = 0;
264 		bits = __SHIFTIN(val, MAX2820_TWI_DATA_MASK) |
265 		       __SHIFTIN(addr, MAX2820_TWI_ADDR_MASK);
266 		break;
267 	case RTW_RFCHIPID_PHILIPS:
268 		KASSERT((addr & ~__SHIFTOUT_MASK(SA2400_TWI_ADDR_MASK)) == 0);
269 		KASSERT((val & ~__SHIFTOUT_MASK(SA2400_TWI_DATA_MASK)) == 0);
270 		bits = __SHIFTIN(val, SA2400_TWI_DATA_MASK) |
271 		       __SHIFTIN(addr, SA2400_TWI_ADDR_MASK) | SA2400_TWI_WREN;
272 		nbits = 32;
273 		lo_to_hi = 1;
274 		break;
275 	case RTW_RFCHIPID_GCT:
276 		KASSERT((addr & ~__SHIFTOUT_MASK(SI4126_TWI_ADDR_MASK)) == 0);
277 		KASSERT((val & ~__SHIFTOUT_MASK(SI4126_TWI_DATA_MASK)) == 0);
278 		bits = rtw_grf5101_host_crypt(addr, val);
279 		nbits = 21;
280 		lo_to_hi = 1;
281 		break;
282 	case RTW_RFCHIPID_RFMD:
283 		KASSERT((addr & ~__SHIFTOUT_MASK(SI4126_TWI_ADDR_MASK)) == 0);
284 		KASSERT((val & ~__SHIFTOUT_MASK(SI4126_TWI_DATA_MASK)) == 0);
285 		bits = __SHIFTIN(val, SI4126_TWI_DATA_MASK) |
286 		       __SHIFTIN(addr, SI4126_TWI_ADDR_MASK);
287 		nbits = 22;
288 		lo_to_hi = 0;
289 		break;
290 	case RTW_RFCHIPID_INTERSIL:
291 	default:
292 		printf("%s: unknown rfchipid %d\n", __func__, rfchipid);
293 		return -1;
294 	}
295 
296 	rtw_rf_hostbangbits(regs, bits, lo_to_hi, nbits);
297 
298 	return 0;
299 }
300 
301 static uint32_t
302 rtw_maxim_swizzle(u_int addr, uint32_t val)
303 {
304 	uint32_t hidata, lodata;
305 
306 	KASSERT((val & ~(RTW_MAXIM_LODATA_MASK|RTW_MAXIM_HIDATA_MASK)) == 0);
307 	lodata = __SHIFTOUT(val, RTW_MAXIM_LODATA_MASK);
308 	hidata = __SHIFTOUT(val, RTW_MAXIM_HIDATA_MASK);
309 	return __SHIFTIN(lodata, RTW_PHYCFG_MAC_MAXIM_LODATA_MASK) |
310 	    __SHIFTIN(hidata, RTW_PHYCFG_MAC_MAXIM_HIDATA_MASK) |
311 	    __SHIFTIN(addr, RTW_PHYCFG_MAC_MAXIM_ADDR_MASK);
312 }
313 
314 /* Tell the MAC what to bang over the 3-wire interface. */
315 int
316 rtw_rf_macwrite(struct rtw_regs *regs, enum rtw_rfchipid rfchipid,
317     u_int addr, uint32_t val)
318 {
319 	uint32_t reg;
320 
321 	RTW_DPRINTF(RTW_DEBUG_PHYIO, ("%s: %s[%u] <- %#08x\n", __func__,
322 	    rtw_rfchipid_string(rfchipid), addr, val));
323 
324 	switch (rfchipid) {
325 	case RTW_RFCHIPID_GCT:
326 		reg = rtw_grf5101_mac_crypt(addr, val);
327 		break;
328 	case RTW_RFCHIPID_MAXIM:
329 		reg = rtw_maxim_swizzle(addr, val);
330 		break;
331 	default:		/* XXX */
332 	case RTW_RFCHIPID_PHILIPS:
333 		KASSERT(
334 		    (addr & ~__SHIFTOUT_MASK(RTW_PHYCFG_MAC_PHILIPS_ADDR_MASK)) == 0);
335 		KASSERT(
336 		    (val & ~__SHIFTOUT_MASK(RTW_PHYCFG_MAC_PHILIPS_DATA_MASK)) == 0);
337 
338 		reg = __SHIFTIN(addr, RTW_PHYCFG_MAC_PHILIPS_ADDR_MASK) |
339 		      __SHIFTIN(val, RTW_PHYCFG_MAC_PHILIPS_DATA_MASK);
340 	}
341 
342 	switch (rfchipid) {
343 	case RTW_RFCHIPID_GCT:
344 	case RTW_RFCHIPID_MAXIM:
345 	case RTW_RFCHIPID_RFMD:
346 		reg |= RTW_PHYCFG_MAC_RFTYPE_RFMD;
347 		break;
348 	case RTW_RFCHIPID_INTERSIL:
349 		reg |= RTW_PHYCFG_MAC_RFTYPE_INTERSIL;
350 		break;
351 	case RTW_RFCHIPID_PHILIPS:
352 		reg |= RTW_PHYCFG_MAC_RFTYPE_PHILIPS;
353 		break;
354 	default:
355 		printf("%s: unknown rfchipid %d\n", __func__, rfchipid);
356 		return -1;
357 	}
358 
359 	return rtw_rf_macbangbits(regs, reg);
360 }
361