Home | History | Annotate | Line # | Download | only in ic
rtwphyio.c revision 1.2
      1 /* $NetBSD: rtwphyio.c,v 1.2 2004/12/13 00:48:02 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.2 2004/12/13 00:48:02 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 
    183 	if (rtw_flush_rfio)
    184 		RTW_READ(regs, RTW_PHYADDR);
    185 
    186 	if (rtw_rfio_delay > 0)
    187 		DELAY(rtw_rfio_delay);
    188 
    189 	for (i = rtw_macbangbits_timeout; --i >= 0; delay(1)) {
    190 		if ((RTW_READ(regs, RTW_PHYCFG) & RTW_PHYCFG_MAC_POLL) == 0) {
    191 			RTW_DPRINTF2(("%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 	printf("%s: RTW_PHYCFG_MAC_POLL still set.\n", __func__);
    199 	return -1;
    200 }
    201 
    202 static u_int32_t
    203 rtw_grf5101_host_crypt(u_int addr, u_int32_t val)
    204 {
    205 	/* TBD */
    206 	return 0;
    207 }
    208 
    209 static u_int32_t
    210 rtw_grf5101_mac_crypt(u_int addr, u_int32_t val)
    211 {
    212 	u_int32_t data_and_addr;
    213 #define EXTRACT_NIBBLE(d, which) (((d) >> (4 * (which))) & 0xf)
    214 	static u_int8_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 LSHIFT(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, u_int32_t val)
    253 {
    254 	u_int nbits;
    255 	int lo_to_hi;
    256 	u_int32_t bits;
    257 
    258 	RTW_DPRINTF(("%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 = LSHIFT(val, MAX2820_TWI_DATA_MASK) |
    266 		       LSHIFT(addr, MAX2820_TWI_ADDR_MASK);
    267 		break;
    268 	case RTW_RFCHIPID_PHILIPS:
    269 		KASSERT((addr & ~PRESHIFT(SA2400_TWI_ADDR_MASK)) == 0);
    270 		KASSERT((val & ~PRESHIFT(SA2400_TWI_DATA_MASK)) == 0);
    271 		bits = LSHIFT(val, SA2400_TWI_DATA_MASK) |
    272 		       LSHIFT(addr, SA2400_TWI_ADDR_MASK) | SA2400_TWI_WREN;
    273 		nbits = 32;
    274 		lo_to_hi = 1;
    275 		break;
    276 	case RTW_RFCHIPID_GCT:
    277 	case RTW_RFCHIPID_RFMD:
    278 		KASSERT((addr & ~PRESHIFT(SI4126_TWI_ADDR_MASK)) == 0);
    279 		KASSERT((val & ~PRESHIFT(SI4126_TWI_DATA_MASK)) == 0);
    280 		if (rfchipid == RTW_RFCHIPID_GCT)
    281 			bits = rtw_grf5101_host_crypt(addr, val);
    282 		else {
    283 			bits = LSHIFT(val, SI4126_TWI_DATA_MASK) |
    284 			       LSHIFT(addr, SI4126_TWI_ADDR_MASK);
    285 		}
    286 		nbits = 22;
    287 		lo_to_hi = 0;
    288 		break;
    289 	case RTW_RFCHIPID_INTERSIL:
    290 	default:
    291 		printf("%s: unknown rfchipid %d\n", __func__, rfchipid);
    292 		return -1;
    293 	}
    294 
    295 	rtw_rf_hostbangbits(regs, bits, lo_to_hi, nbits);
    296 
    297 	return 0;
    298 }
    299 
    300 static uint32_t
    301 rtw_maxim_swizzle(u_int addr, uint32_t val)
    302 {
    303 	uint32_t hidata, lodata;
    304 
    305 	KASSERT((val & ~(RTW_MAXIM_LODATA_MASK|RTW_MAXIM_HIDATA_MASK)) == 0);
    306 	lodata = MASK_AND_RSHIFT(val, RTW_MAXIM_LODATA_MASK);
    307 	hidata = MASK_AND_RSHIFT(val, RTW_MAXIM_HIDATA_MASK);
    308 	return LSHIFT(lodata, RTW_PHYCFG_MAC_MAXIM_LODATA_MASK) |
    309 	    LSHIFT(hidata, RTW_PHYCFG_MAC_MAXIM_HIDATA_MASK) |
    310 	    LSHIFT(addr, RTW_PHYCFG_MAC_MAXIM_ADDR_MASK);
    311 }
    312 
    313 /* Tell the MAC what to bang over the 3-wire interface. */
    314 int
    315 rtw_rf_macwrite(struct rtw_regs *regs, enum rtw_rfchipid rfchipid,
    316     u_int addr, u_int32_t val)
    317 {
    318 	uint32_t reg;
    319 
    320 	RTW_DPRINTF(("%s: %s[%u] <- %#08x\n", __func__,
    321 	    rtw_rfchipid_string(rfchipid), addr, val));
    322 
    323 	switch (rfchipid) {
    324 	case RTW_RFCHIPID_GCT:
    325 		reg = rtw_grf5101_mac_crypt(addr, val);
    326 		break;
    327 	case RTW_RFCHIPID_MAXIM:
    328 		reg = rtw_maxim_swizzle(addr, val);
    329 		break;
    330 	default:		/* XXX */
    331 	case RTW_RFCHIPID_PHILIPS:
    332 		KASSERT(
    333 		    (addr & ~PRESHIFT(RTW_PHYCFG_MAC_PHILIPS_ADDR_MASK)) == 0);
    334 		KASSERT(
    335 		    (val & ~PRESHIFT(RTW_PHYCFG_MAC_PHILIPS_DATA_MASK)) == 0);
    336 
    337 		reg = LSHIFT(addr, RTW_PHYCFG_MAC_PHILIPS_ADDR_MASK) |
    338 		      LSHIFT(val, RTW_PHYCFG_MAC_PHILIPS_DATA_MASK);
    339 	}
    340 
    341 	switch (rfchipid) {
    342 	case RTW_RFCHIPID_GCT:
    343 	case RTW_RFCHIPID_MAXIM:
    344 	case RTW_RFCHIPID_RFMD:
    345 		reg |= RTW_PHYCFG_MAC_RFTYPE_RFMD;
    346 		break;
    347 	case RTW_RFCHIPID_INTERSIL:
    348 		reg |= RTW_PHYCFG_MAC_RFTYPE_INTERSIL;
    349 		break;
    350 	case RTW_RFCHIPID_PHILIPS:
    351 		reg |= RTW_PHYCFG_MAC_RFTYPE_PHILIPS;
    352 		break;
    353 	default:
    354 		printf("%s: unknown rfchipid %d\n", __func__, rfchipid);
    355 		return -1;
    356 	}
    357 
    358 	return rtw_rf_macbangbits(regs, reg);
    359 }
    360