NetBSD/sys/dev/ic/rtwphyio.c
dyoung f66403a698 Move my bit-twiddling macros to libkern.h from my drivers, where
I had duplicated them.  Improve the macros' names.  Simplify their
implementation.

A brief description of each macro is below.

        BIT(n): Return a bitmask with bit m set, where the least
                significant bit is bit 0.

        BITS(m, n): Return a bitmask with bits m through n, inclusive,
                    set.  It does not matter whether m>n or m<=n.
                    The least significant bit is bit 0.

        A "bitfield" is a span of consecutive bits defined by a
        bitmask, where 1s select the bits in the bitfield.  SHIFTIN,
        SHIFTOUT, and SHIFTOUT_MASK help read and write bitfields
        from device registers.

        SHIFTIN(v, mask): Left-shift bits `v' into the bitfield
                          defined by `mask', and return them.  No
                          side-effects.

        SHIFTOUT(v, mask): Extract and return the bitfield selected
                           by `mask' from `v', right-shifting the
                           bits so that the rightmost selected bit
                           is at bit 0.  No side-effects.

        SHIFTOUT_MASK(mask): Right-shift the bits in `mask' so that
                             the rightmost non-zero bit is at bit
                             0.  This is useful for finding the
                             greatest unsigned value that a bitfield
                             can hold.  No side-effects.  Note that
                             SHIFTOUT_MASK(m) = SHIFTOUT(m, m).

Examples:

/*
 * Register definitions taken from the RFMD RF3000 manual.
 */
#define RF3000_GAINCTL          0x11            /* TX variable gain control */
#define         RF3000_GAINCTL_TXVGC_MASK       BITS(7, 2)
#define         RF3000_GAINCTL_SCRAMBLER        BIT(1)

/*
 * Shift the transmit power into the transmit-power field of the
 * gain-control register and write it to the baseband processor.
 */
atw_rf3000_write(sc, RF3000_GAINCTL,
    SHIFTIN(txpower, RF3000_GAINCTL_TXVGC_MASK));


/*
 * Register definitions taken from the ADMtek ADM8211 manual.
 *
 */
#define ATW_RXSTAT_OWN          BIT(31)         /* 1: NIC may fill descriptor */
/* ... */
#define ATW_RXSTAT_DA1          BIT(17)         /* DA bit 1, admin'd address */
#define ATW_RXSTAT_DA0          BIT(16)         /* DA bit 0, group address */
#define ATW_RXSTAT_RXDR_MASK    BITS(15,12)     /* RX data rate */
#define ATW_RXSTAT_FL_MASK      BITS(11,0)      /* RX frame length, last
                                                 * descriptor only
                                                 */

/* Extract the frame length from the Rx descriptor's
 * status field.
 */
len = SHIFTOUT(rxstat, ATW_RXSTAT_FL_MASK);
2006-03-08 00:24:06 +00:00

361 lines
10 KiB
C

/* $NetBSD: rtwphyio.c,v 1.11 2006/03/08 00:24:06 dyoung Exp $ */
/*-
* Copyright (c) 2004, 2005 David Young. All rights reserved.
*
* Programmed for NetBSD by David Young.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of David Young may not be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY David Young ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL David
* Young BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*/
/*
* Control input/output with the Philips SA2400 RF front-end and
* the baseband processor built into the Realtek RTL8180.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: rtwphyio.c,v 1.11 2006/03/08 00:24:06 dyoung Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/types.h>
#include <machine/bus.h>
#include <net/if.h>
#include <net/if_media.h>
#include <net/if_ether.h>
#include <net80211/ieee80211_netbsd.h>
#include <net80211/ieee80211_radiotap.h>
#include <net80211/ieee80211_var.h>
#include <dev/ic/rtwreg.h>
#include <dev/ic/max2820reg.h>
#include <dev/ic/sa2400reg.h>
#include <dev/ic/si4136reg.h>
#include <dev/ic/rtwvar.h>
#include <dev/ic/rtwphyio.h>
#include <dev/ic/rtwphy.h>
static int rtw_macbangbits_timeout = 100;
uint8_t
rtw_bbp_read(struct rtw_regs *regs, u_int addr)
{
KASSERT((addr & ~SHIFTOUT_MASK(RTW_BB_ADDR_MASK)) == 0);
RTW_WRITE(regs, RTW_BB,
SHIFTIN(addr, RTW_BB_ADDR_MASK) | RTW_BB_RD_MASK | RTW_BB_WR_MASK);
delay(10); /* XXX */
RTW_WBR(regs, RTW_BB, RTW_BB);
return SHIFTOUT(RTW_READ(regs, RTW_BB), RTW_BB_RD_MASK);
}
int
rtw_bbp_write(struct rtw_regs *regs, u_int addr, u_int val)
{
#define BBP_WRITE_ITERS 50
#define BBP_WRITE_DELAY 1
int i;
uint32_t wrbbp, rdbbp;
RTW_DPRINTF(RTW_DEBUG_PHYIO,
("%s: bbp[%u] <- %u\n", __func__, addr, val));
KASSERT((addr & ~SHIFTOUT_MASK(RTW_BB_ADDR_MASK)) == 0);
KASSERT((val & ~SHIFTOUT_MASK(RTW_BB_WR_MASK)) == 0);
wrbbp = SHIFTIN(addr, RTW_BB_ADDR_MASK) | RTW_BB_WREN |
SHIFTIN(val, RTW_BB_WR_MASK) | RTW_BB_RD_MASK,
rdbbp = SHIFTIN(addr, RTW_BB_ADDR_MASK) |
RTW_BB_WR_MASK | RTW_BB_RD_MASK;
RTW_DPRINTF(RTW_DEBUG_PHYIO,
("%s: rdbbp = %#08x, wrbbp = %#08x\n", __func__, rdbbp, wrbbp));
for (i = BBP_WRITE_ITERS; --i >= 0; ) {
RTW_RBW(regs, RTW_BB, RTW_BB);
RTW_WRITE(regs, RTW_BB, wrbbp);
RTW_SYNC(regs, RTW_BB, RTW_BB);
RTW_WRITE(regs, RTW_BB, rdbbp);
RTW_SYNC(regs, RTW_BB, RTW_BB);
delay(BBP_WRITE_DELAY); /* 1 microsecond */
if (SHIFTOUT(RTW_READ(regs, RTW_BB),
RTW_BB_RD_MASK) == val) {
RTW_DPRINTF(RTW_DEBUG_PHYIO,
("%s: finished in %dus\n", __func__,
BBP_WRITE_DELAY * (BBP_WRITE_ITERS - i)));
return 0;
}
delay(BBP_WRITE_DELAY); /* again */
}
printf("%s: timeout\n", __func__);
return -1;
}
/* Help rtw_rf_hostwrite bang bits to RF over 3-wire interface. */
static inline void
rtw_rf_hostbangbits(struct rtw_regs *regs, uint32_t bits, int lo_to_hi,
u_int nbits)
{
int i;
uint32_t mask, reg;
KASSERT(nbits <= 32);
RTW_DPRINTF(RTW_DEBUG_PHYIO,
("%s: %u bits, %#08x, %s\n", __func__, nbits, bits,
(lo_to_hi) ? "lo to hi" : "hi to lo"));
reg = RTW_PHYCFG_HST;
RTW_WRITE(regs, RTW_PHYCFG, reg);
RTW_SYNC(regs, RTW_PHYCFG, RTW_PHYCFG);
if (lo_to_hi)
mask = 0x1;
else
mask = 1 << (nbits - 1);
for (i = 0; i < nbits; i++) {
RTW_DPRINTF(RTW_DEBUG_PHYBITIO,
("%s: bits %#08x mask %#08x -> bit %#08x\n",
__func__, bits, mask, bits & mask));
if ((bits & mask) != 0)
reg |= RTW_PHYCFG_HST_DATA;
else
reg &= ~RTW_PHYCFG_HST_DATA;
reg |= RTW_PHYCFG_HST_CLK;
RTW_WRITE(regs, RTW_PHYCFG, reg);
RTW_SYNC(regs, RTW_PHYCFG, RTW_PHYCFG);
DELAY(2); /* arbitrary delay */
reg &= ~RTW_PHYCFG_HST_CLK;
RTW_WRITE(regs, RTW_PHYCFG, reg);
RTW_SYNC(regs, RTW_PHYCFG, RTW_PHYCFG);
if (lo_to_hi)
mask <<= 1;
else
mask >>= 1;
}
reg |= RTW_PHYCFG_HST_EN;
KASSERT((reg & RTW_PHYCFG_HST_CLK) == 0);
RTW_WRITE(regs, RTW_PHYCFG, reg);
RTW_SYNC(regs, RTW_PHYCFG, RTW_PHYCFG);
}
/* Help rtw_rf_macwrite: tell MAC to bang bits to RF over the 3-wire
* interface.
*/
static inline int
rtw_rf_macbangbits(struct rtw_regs *regs, uint32_t reg)
{
int i;
RTW_DPRINTF(RTW_DEBUG_PHY, ("%s: %#08x\n", __func__, reg));
RTW_WRITE(regs, RTW_PHYCFG, RTW_PHYCFG_MAC_POLL | reg);
RTW_WBR(regs, RTW_PHYCFG, RTW_PHYCFG);
for (i = rtw_macbangbits_timeout; --i >= 0; delay(1)) {
if ((RTW_READ(regs, RTW_PHYCFG) & RTW_PHYCFG_MAC_POLL) == 0) {
RTW_DPRINTF(RTW_DEBUG_PHY,
("%s: finished in %dus\n", __func__,
rtw_macbangbits_timeout - i));
return 0;
}
RTW_RBR(regs, RTW_PHYCFG, RTW_PHYCFG); /* XXX paranoia? */
}
printf("%s: RTW_PHYCFG_MAC_POLL still set.\n", __func__);
return -1;
}
static uint32_t
rtw_grf5101_host_crypt(u_int addr, uint32_t val)
{
/* TBD */
return 0;
}
static uint32_t
rtw_grf5101_mac_crypt(u_int addr, uint32_t val)
{
uint32_t data_and_addr;
#define EXTRACT_NIBBLE(d, which) (((d) >> (4 * (which))) & 0xf)
static uint8_t caesar[16] = {0x0, 0x8, 0x4, 0xc,
0x2, 0xa, 0x6, 0xe,
0x1, 0x9, 0x5, 0xd,
0x3, 0xb, 0x7, 0xf};
data_and_addr = caesar[EXTRACT_NIBBLE(val, 2)] |
(caesar[EXTRACT_NIBBLE(val, 1)] << 4) |
(caesar[EXTRACT_NIBBLE(val, 0)] << 8) |
(caesar[(addr >> 1) & 0xf] << 12) |
((addr & 0x1) << 16) |
(caesar[EXTRACT_NIBBLE(val, 3)] << 24);
return SHIFTIN(data_and_addr,
RTW_PHYCFG_MAC_PHILIPS_ADDR_MASK|RTW_PHYCFG_MAC_PHILIPS_DATA_MASK);
#undef EXTRACT_NIBBLE
}
static inline const char *
rtw_rfchipid_string(enum rtw_rfchipid rfchipid)
{
switch (rfchipid) {
case RTW_RFCHIPID_MAXIM:
return "Maxim";
case RTW_RFCHIPID_PHILIPS:
return "Philips";
case RTW_RFCHIPID_GCT:
return "GCT";
case RTW_RFCHIPID_RFMD:
return "RFMD";
case RTW_RFCHIPID_INTERSIL:
return "Intersil";
default:
return "unknown";
}
}
/* Bang bits over the 3-wire interface. */
int
rtw_rf_hostwrite(struct rtw_regs *regs, enum rtw_rfchipid rfchipid,
u_int addr, uint32_t val)
{
u_int nbits;
int lo_to_hi;
uint32_t bits;
RTW_DPRINTF(RTW_DEBUG_PHYIO, ("%s: %s[%u] <- %#08x\n", __func__,
rtw_rfchipid_string(rfchipid), addr, val));
switch (rfchipid) {
case RTW_RFCHIPID_MAXIM:
nbits = 16;
lo_to_hi = 0;
bits = SHIFTIN(val, MAX2820_TWI_DATA_MASK) |
SHIFTIN(addr, MAX2820_TWI_ADDR_MASK);
break;
case RTW_RFCHIPID_PHILIPS:
KASSERT((addr & ~SHIFTOUT_MASK(SA2400_TWI_ADDR_MASK)) == 0);
KASSERT((val & ~SHIFTOUT_MASK(SA2400_TWI_DATA_MASK)) == 0);
bits = SHIFTIN(val, SA2400_TWI_DATA_MASK) |
SHIFTIN(addr, SA2400_TWI_ADDR_MASK) | SA2400_TWI_WREN;
nbits = 32;
lo_to_hi = 1;
break;
case RTW_RFCHIPID_GCT:
KASSERT((addr & ~SHIFTOUT_MASK(SI4126_TWI_ADDR_MASK)) == 0);
KASSERT((val & ~SHIFTOUT_MASK(SI4126_TWI_DATA_MASK)) == 0);
bits = rtw_grf5101_host_crypt(addr, val);
nbits = 21;
lo_to_hi = 1;
break;
case RTW_RFCHIPID_RFMD:
KASSERT((addr & ~SHIFTOUT_MASK(SI4126_TWI_ADDR_MASK)) == 0);
KASSERT((val & ~SHIFTOUT_MASK(SI4126_TWI_DATA_MASK)) == 0);
bits = SHIFTIN(val, SI4126_TWI_DATA_MASK) |
SHIFTIN(addr, SI4126_TWI_ADDR_MASK);
nbits = 22;
lo_to_hi = 0;
break;
case RTW_RFCHIPID_INTERSIL:
default:
printf("%s: unknown rfchipid %d\n", __func__, rfchipid);
return -1;
}
rtw_rf_hostbangbits(regs, bits, lo_to_hi, nbits);
return 0;
}
static uint32_t
rtw_maxim_swizzle(u_int addr, uint32_t val)
{
uint32_t hidata, lodata;
KASSERT((val & ~(RTW_MAXIM_LODATA_MASK|RTW_MAXIM_HIDATA_MASK)) == 0);
lodata = SHIFTOUT(val, RTW_MAXIM_LODATA_MASK);
hidata = SHIFTOUT(val, RTW_MAXIM_HIDATA_MASK);
return SHIFTIN(lodata, RTW_PHYCFG_MAC_MAXIM_LODATA_MASK) |
SHIFTIN(hidata, RTW_PHYCFG_MAC_MAXIM_HIDATA_MASK) |
SHIFTIN(addr, RTW_PHYCFG_MAC_MAXIM_ADDR_MASK);
}
/* Tell the MAC what to bang over the 3-wire interface. */
int
rtw_rf_macwrite(struct rtw_regs *regs, enum rtw_rfchipid rfchipid,
u_int addr, uint32_t val)
{
uint32_t reg;
RTW_DPRINTF(RTW_DEBUG_PHYIO, ("%s: %s[%u] <- %#08x\n", __func__,
rtw_rfchipid_string(rfchipid), addr, val));
switch (rfchipid) {
case RTW_RFCHIPID_GCT:
reg = rtw_grf5101_mac_crypt(addr, val);
break;
case RTW_RFCHIPID_MAXIM:
reg = rtw_maxim_swizzle(addr, val);
break;
default: /* XXX */
case RTW_RFCHIPID_PHILIPS:
KASSERT(
(addr & ~SHIFTOUT_MASK(RTW_PHYCFG_MAC_PHILIPS_ADDR_MASK)) == 0);
KASSERT(
(val & ~SHIFTOUT_MASK(RTW_PHYCFG_MAC_PHILIPS_DATA_MASK)) == 0);
reg = SHIFTIN(addr, RTW_PHYCFG_MAC_PHILIPS_ADDR_MASK) |
SHIFTIN(val, RTW_PHYCFG_MAC_PHILIPS_DATA_MASK);
}
switch (rfchipid) {
case RTW_RFCHIPID_GCT:
case RTW_RFCHIPID_MAXIM:
case RTW_RFCHIPID_RFMD:
reg |= RTW_PHYCFG_MAC_RFTYPE_RFMD;
break;
case RTW_RFCHIPID_INTERSIL:
reg |= RTW_PHYCFG_MAC_RFTYPE_INTERSIL;
break;
case RTW_RFCHIPID_PHILIPS:
reg |= RTW_PHYCFG_MAC_RFTYPE_PHILIPS;
break;
default:
printf("%s: unknown rfchipid %d\n", __func__, rfchipid);
return -1;
}
return rtw_rf_macbangbits(regs, reg);
}