Home | History | Annotate | Line # | Download | only in ic
rtwphyio.c revision 1.6
      1 /* $NetBSD: rtwphyio.c,v 1.6 2005/01/02 04:23:03 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.6 2005/01/02 04:23:03 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 uint8_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(RTW_DEBUG_PHYIO,
     84 	    ("%s: bbp[%u] <- %u\n", __func__, addr, val));
     85 
     86 	KASSERT((addr & ~PRESHIFT(RTW_BB_ADDR_MASK)) == 0);
     87 	KASSERT((val & ~PRESHIFT(RTW_BB_WR_MASK)) == 0);
     88 
     89 	wrbbp = LSHIFT(addr, RTW_BB_ADDR_MASK) | RTW_BB_WREN |
     90 	    LSHIFT(val, RTW_BB_WR_MASK) | RTW_BB_RD_MASK,
     91 
     92 	rdbbp = LSHIFT(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 (MASK_AND_RSHIFT(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 LSHIFT(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 = LSHIFT(val, MAX2820_TWI_DATA_MASK) |
    265 		       LSHIFT(addr, MAX2820_TWI_ADDR_MASK);
    266 		break;
    267 	case RTW_RFCHIPID_PHILIPS:
    268 		KASSERT((addr & ~PRESHIFT(SA2400_TWI_ADDR_MASK)) == 0);
    269 		KASSERT((val & ~PRESHIFT(SA2400_TWI_DATA_MASK)) == 0);
    270 		bits = LSHIFT(val, SA2400_TWI_DATA_MASK) |
    271 		       LSHIFT(addr, SA2400_TWI_ADDR_MASK) | SA2400_TWI_WREN;
    272 		nbits = 32;
    273 		lo_to_hi = 1;
    274 		break;
    275 	case RTW_RFCHIPID_GCT:
    276 	case RTW_RFCHIPID_RFMD:
    277 		KASSERT((addr & ~PRESHIFT(SI4126_TWI_ADDR_MASK)) == 0);
    278 		KASSERT((val & ~PRESHIFT(SI4126_TWI_DATA_MASK)) == 0);
    279 		if (rfchipid == RTW_RFCHIPID_GCT)
    280 			bits = rtw_grf5101_host_crypt(addr, val);
    281 		else {
    282 			bits = LSHIFT(val, SI4126_TWI_DATA_MASK) |
    283 			       LSHIFT(addr, SI4126_TWI_ADDR_MASK);
    284 		}
    285 		nbits = 22;
    286 		lo_to_hi = 0;
    287 		break;
    288 	case RTW_RFCHIPID_INTERSIL:
    289 	default:
    290 		printf("%s: unknown rfchipid %d\n", __func__, rfchipid);
    291 		return -1;
    292 	}
    293 
    294 	rtw_rf_hostbangbits(regs, bits, lo_to_hi, nbits);
    295 
    296 	return 0;
    297 }
    298 
    299 static uint32_t
    300 rtw_maxim_swizzle(u_int addr, uint32_t val)
    301 {
    302 	uint32_t hidata, lodata;
    303 
    304 	KASSERT((val & ~(RTW_MAXIM_LODATA_MASK|RTW_MAXIM_HIDATA_MASK)) == 0);
    305 	lodata = MASK_AND_RSHIFT(val, RTW_MAXIM_LODATA_MASK);
    306 	hidata = MASK_AND_RSHIFT(val, RTW_MAXIM_HIDATA_MASK);
    307 	return LSHIFT(lodata, RTW_PHYCFG_MAC_MAXIM_LODATA_MASK) |
    308 	    LSHIFT(hidata, RTW_PHYCFG_MAC_MAXIM_HIDATA_MASK) |
    309 	    LSHIFT(addr, RTW_PHYCFG_MAC_MAXIM_ADDR_MASK);
    310 }
    311 
    312 /* Tell the MAC what to bang over the 3-wire interface. */
    313 int
    314 rtw_rf_macwrite(struct rtw_regs *regs, enum rtw_rfchipid rfchipid,
    315     u_int addr, uint32_t val)
    316 {
    317 	uint32_t reg;
    318 
    319 	RTW_DPRINTF(RTW_DEBUG_PHYIO, ("%s: %s[%u] <- %#08x\n", __func__,
    320 	    rtw_rfchipid_string(rfchipid), addr, val));
    321 
    322 	switch (rfchipid) {
    323 	case RTW_RFCHIPID_GCT:
    324 		reg = rtw_grf5101_mac_crypt(addr, val);
    325 		break;
    326 	case RTW_RFCHIPID_MAXIM:
    327 		reg = rtw_maxim_swizzle(addr, val);
    328 		break;
    329 	default:		/* XXX */
    330 	case RTW_RFCHIPID_PHILIPS:
    331 		KASSERT(
    332 		    (addr & ~PRESHIFT(RTW_PHYCFG_MAC_PHILIPS_ADDR_MASK)) == 0);
    333 		KASSERT(
    334 		    (val & ~PRESHIFT(RTW_PHYCFG_MAC_PHILIPS_DATA_MASK)) == 0);
    335 
    336 		reg = LSHIFT(addr, RTW_PHYCFG_MAC_PHILIPS_ADDR_MASK) |
    337 		      LSHIFT(val, RTW_PHYCFG_MAC_PHILIPS_DATA_MASK);
    338 	}
    339 
    340 	switch (rfchipid) {
    341 	case RTW_RFCHIPID_GCT:
    342 	case RTW_RFCHIPID_MAXIM:
    343 	case RTW_RFCHIPID_RFMD:
    344 		reg |= RTW_PHYCFG_MAC_RFTYPE_RFMD;
    345 		break;
    346 	case RTW_RFCHIPID_INTERSIL:
    347 		reg |= RTW_PHYCFG_MAC_RFTYPE_INTERSIL;
    348 		break;
    349 	case RTW_RFCHIPID_PHILIPS:
    350 		reg |= RTW_PHYCFG_MAC_RFTYPE_PHILIPS;
    351 		break;
    352 	default:
    353 		printf("%s: unknown rfchipid %d\n", __func__, rfchipid);
    354 		return -1;
    355 	}
    356 
    357 	return rtw_rf_macbangbits(regs, reg);
    358 }
    359