xref: /onnv-gate/usr/src/uts/common/io/rtw/rtwphy.c (revision 10448:fe6b44e693c2)
14689Sql147931 /*
2*10448SMikore.Li@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
34689Sql147931  * Use is subject to license terms.
44689Sql147931  */
54689Sql147931 /*
64689Sql147931  * Copyright (c) 2004, 2005 David Young.  All rights reserved.
74689Sql147931  *
84689Sql147931  * Programmed for NetBSD by David Young.
94689Sql147931  *
104689Sql147931  * Redistribution and use in source and binary forms, with or without
114689Sql147931  * modification, are permitted provided that the following conditions
124689Sql147931  * are met:
134689Sql147931  * 1. Redistributions of source code must retain the above copyright
144689Sql147931  *    notice, this list of conditions and the following disclaimer.
154689Sql147931  * 2. Redistributions in binary form must reproduce the above copyright
164689Sql147931  *    notice, this list of conditions and the following disclaimer in the
174689Sql147931  *    documentation and/or other materials provided with the distribution.
184689Sql147931  * 3. The name of David Young may not be used to endorse or promote
194689Sql147931  *    products derived from this software without specific prior
204689Sql147931  *    written permission.
214689Sql147931  *
224689Sql147931  * THIS SOFTWARE IS PROVIDED BY David Young ``AS IS'' AND ANY
234689Sql147931  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
244689Sql147931  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
254689Sql147931  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL David
264689Sql147931  * Young BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
274689Sql147931  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
284689Sql147931  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
294689Sql147931  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
304689Sql147931  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
314689Sql147931  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
324689Sql147931  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
334689Sql147931  * OF SUCH DAMAGE.
344689Sql147931  */
354689Sql147931 /*
364689Sql147931  * Control the Philips SA2400 RF front-end and the baseband processor
374689Sql147931  * built into the Realtek RTL8180.
384689Sql147931  */
394689Sql147931 #include <sys/types.h>
40*10448SMikore.Li@Sun.COM #include <sys/sysmacros.h>
414689Sql147931 #include "rtwreg.h"
424689Sql147931 #include "rtwvar.h"
434689Sql147931 #include "max2820reg.h"
444689Sql147931 #include "sa2400reg.h"
454689Sql147931 #include "rtwphyio.h"
464689Sql147931 #include "rtwphy.h"
474689Sql147931 
484689Sql147931 static int rtw_max2820_pwrstate(struct rtw_rf *, enum rtw_pwrstate);
494689Sql147931 static int rtw_sa2400_pwrstate(struct rtw_rf *, enum rtw_pwrstate);
504689Sql147931 
514689Sql147931 static int
rtw_rf_init(struct rtw_rf * rf,uint_t freq,uint8_t opaque_txpower,enum rtw_pwrstate power)524689Sql147931 rtw_rf_init(struct rtw_rf *rf, uint_t freq, uint8_t opaque_txpower,
534689Sql147931     enum rtw_pwrstate power)
544689Sql147931 {
554689Sql147931 	return (*rf->rf_init)(rf, freq, opaque_txpower, power);
564689Sql147931 }
574689Sql147931 
584689Sql147931 static int
rtw_rf_tune(struct rtw_rf * rf,uint_t freq)594689Sql147931 rtw_rf_tune(struct rtw_rf *rf, uint_t freq)
604689Sql147931 {
614689Sql147931 	return (*rf->rf_tune)(rf, freq);
624689Sql147931 }
634689Sql147931 
644689Sql147931 static int
rtw_rf_txpower(struct rtw_rf * rf,uint8_t opaque_txpower)654689Sql147931 rtw_rf_txpower(struct rtw_rf *rf, uint8_t opaque_txpower)
664689Sql147931 {
674689Sql147931 	return (*rf->rf_txpower)(rf, opaque_txpower);
684689Sql147931 }
694689Sql147931 
704689Sql147931 static int
rtw_rfbus_write(struct rtw_rfbus * bus,enum rtw_rfchipid rfchipid,uint_t addr,uint32_t val)714689Sql147931 rtw_rfbus_write(struct rtw_rfbus *bus, enum rtw_rfchipid rfchipid, uint_t addr,
724689Sql147931     uint32_t val)
734689Sql147931 {
744689Sql147931 	return (*bus->b_write)(bus->b_regs, rfchipid, addr, val);
754689Sql147931 }
764689Sql147931 
774689Sql147931 static int
rtw_bbp_preinit(struct rtw_regs * regs,uint_t antatten0,int dflantb,uint_t freq)784689Sql147931 rtw_bbp_preinit(struct rtw_regs *regs, uint_t antatten0, int dflantb,
794689Sql147931     uint_t freq)
804689Sql147931 {
814689Sql147931 	uint_t antatten = antatten0;
824689Sql147931 	if (dflantb)
834689Sql147931 		antatten |= RTW_BBP_ANTATTEN_DFLANTB;
844689Sql147931 	if (freq == 2484) /* channel 14 */
854689Sql147931 		antatten |= RTW_BBP_ANTATTEN_CHAN14;
864689Sql147931 	return (rtw_bbp_write(regs, RTW_BBP_ANTATTEN, antatten));
874689Sql147931 }
884689Sql147931 
894689Sql147931 static int
rtw_bbp_init(struct rtw_regs * regs,struct rtw_bbpset * bb,int antdiv,int dflantb,uint8_t cs_threshold,uint_t freq)904689Sql147931 rtw_bbp_init(struct rtw_regs *regs, struct rtw_bbpset *bb, int antdiv,
914689Sql147931     int dflantb, uint8_t cs_threshold, uint_t freq)
924689Sql147931 {
934689Sql147931 	int rc;
944689Sql147931 	uint32_t sys2, sys3;
954689Sql147931 
964689Sql147931 	sys2 = bb->bb_sys2;
974689Sql147931 	if (antdiv)
984689Sql147931 		sys2 |= RTW_BBP_SYS2_ANTDIV;
994689Sql147931 	sys3 = bb->bb_sys3 |
1004689Sql147931 	    LSHIFT(cs_threshold, RTW_BBP_SYS3_CSTHRESH_MASK);
1014689Sql147931 
1024689Sql147931 #define	RTW_BBP_WRITE_OR_RETURN(reg, val) \
1034689Sql147931 	if ((rc = rtw_bbp_write(regs, reg, val)) != 0) \
1044689Sql147931 		return (rc);
1054689Sql147931 
1064689Sql147931 	RTW_BBP_WRITE_OR_RETURN(RTW_BBP_SYS1,		bb->bb_sys1);
1074689Sql147931 	RTW_BBP_WRITE_OR_RETURN(RTW_BBP_TXAGC,		bb->bb_txagc);
1084689Sql147931 	RTW_BBP_WRITE_OR_RETURN(RTW_BBP_LNADET,		bb->bb_lnadet);
1094689Sql147931 	RTW_BBP_WRITE_OR_RETURN(RTW_BBP_IFAGCINI,	bb->bb_ifagcini);
1104689Sql147931 	RTW_BBP_WRITE_OR_RETURN(RTW_BBP_IFAGCLIMIT,	bb->bb_ifagclimit);
1114689Sql147931 	RTW_BBP_WRITE_OR_RETURN(RTW_BBP_IFAGCDET,	bb->bb_ifagcdet);
1124689Sql147931 
1134689Sql147931 	if ((rc = rtw_bbp_preinit(regs, bb->bb_antatten, dflantb, freq)) != 0)
1144689Sql147931 		return (rc);
1154689Sql147931 
1164689Sql147931 	RTW_BBP_WRITE_OR_RETURN(RTW_BBP_TRL,		bb->bb_trl);
1174689Sql147931 	RTW_BBP_WRITE_OR_RETURN(RTW_BBP_SYS2,		sys2);
1184689Sql147931 	RTW_BBP_WRITE_OR_RETURN(RTW_BBP_SYS3,		sys3);
1194689Sql147931 	RTW_BBP_WRITE_OR_RETURN(RTW_BBP_CHESTLIM,	bb->bb_chestlim);
1204689Sql147931 	RTW_BBP_WRITE_OR_RETURN(RTW_BBP_CHSQLIM,	bb->bb_chsqlim);
1214689Sql147931 	return (0);
1224689Sql147931 }
1234689Sql147931 
1244689Sql147931 static int
rtw_sa2400_txpower(struct rtw_rf * rf,uint8_t opaque_txpower)1254689Sql147931 rtw_sa2400_txpower(struct rtw_rf *rf, uint8_t opaque_txpower)
1264689Sql147931 {
1274689Sql147931 	struct rtw_sa2400 *sa = (struct rtw_sa2400 *)rf;
1284689Sql147931 	struct rtw_rfbus *bus = &sa->sa_bus;
1294689Sql147931 
1304689Sql147931 	return (rtw_rfbus_write(bus, RTW_RFCHIPID_PHILIPS, SA2400_TX,
1314689Sql147931 	    opaque_txpower));
1324689Sql147931 }
1334689Sql147931 
1344689Sql147931 #ifdef _RTW_FUTURE_DEBUG_
1354689Sql147931 /*
1364689Sql147931  * make sure we're using the same settings as the reference driver
1374689Sql147931  */
1384689Sql147931 static void
verify_syna(uint_t freq,uint32_t val)1394689Sql147931 verify_syna(uint_t freq, uint32_t val)
1404689Sql147931 {
1414689Sql147931 	uint32_t expected_val = ~val;
1424689Sql147931 
1434689Sql147931 	switch (freq) {
1444689Sql147931 	case 2412:
1454689Sql147931 		expected_val = 0x0000096c; /* ch 1 */
1464689Sql147931 		break;
1474689Sql147931 	case 2417:
1484689Sql147931 		expected_val = 0x00080970; /* ch 2 */
1494689Sql147931 		break;
1504689Sql147931 	case 2422:
1514689Sql147931 		expected_val = 0x00100974; /* ch 3 */
1524689Sql147931 		break;
1534689Sql147931 	case 2427:
1544689Sql147931 		expected_val = 0x00180978; /* ch 4 */
1554689Sql147931 		break;
1564689Sql147931 	case 2432:
1574689Sql147931 		expected_val = 0x00000980; /* ch 5 */
1584689Sql147931 		break;
1594689Sql147931 	case 2437:
1604689Sql147931 		expected_val = 0x00080984; /* ch 6 */
1614689Sql147931 		break;
1624689Sql147931 	case 2442:
1634689Sql147931 		expected_val = 0x00100988; /* ch 7 */
1644689Sql147931 		break;
1654689Sql147931 	case 2447:
1664689Sql147931 		expected_val = 0x0018098c; /* ch 8 */
1674689Sql147931 		break;
1684689Sql147931 	case 2452:
1694689Sql147931 		expected_val = 0x00000994; /* ch 9 */
1704689Sql147931 		break;
1714689Sql147931 	case 2457:
1724689Sql147931 		expected_val = 0x00080998; /* ch 10 */
1734689Sql147931 		break;
1744689Sql147931 	case 2462:
1754689Sql147931 		expected_val = 0x0010099c; /* ch 11 */
1764689Sql147931 		break;
1774689Sql147931 	case 2467:
1784689Sql147931 		expected_val = 0x001809a0; /* ch 12 */
1794689Sql147931 		break;
1804689Sql147931 	case 2472:
1814689Sql147931 		expected_val = 0x000009a8; /* ch 13 */
1824689Sql147931 		break;
1834689Sql147931 	case 2484:
1844689Sql147931 		expected_val = 0x000009b4; /* ch 14 */
1854689Sql147931 		break;
1864689Sql147931 	}
1874689Sql147931 }
1884689Sql147931 #endif /* _RTW_FUTURE_DEBUG_ */
1894689Sql147931 
1904689Sql147931 /* freq is in MHz */
1914689Sql147931 static int
rtw_sa2400_tune(struct rtw_rf * rf,uint_t freq)1924689Sql147931 rtw_sa2400_tune(struct rtw_rf *rf, uint_t freq)
1934689Sql147931 {
1944689Sql147931 	struct rtw_sa2400 *sa = (struct rtw_sa2400 *)rf;
1954689Sql147931 	struct rtw_rfbus *bus = &sa->sa_bus;
1964689Sql147931 	int rc;
1974689Sql147931 	uint32_t syna, synb, sync;
1984689Sql147931 
1994689Sql147931 	/*
2004689Sql147931 	 * XO = 44MHz, R = 11, hence N is in units of XO / R = 4MHz.
2014689Sql147931 	 *
2024689Sql147931 	 * The channel spacing (5MHz) is not divisible by 4MHz, so
2034689Sql147931 	 * we set the fractional part of N to compensate.
2044689Sql147931 	 */
2054689Sql147931 	int n = freq / 4, nf = (freq % 4) * 2;
2064689Sql147931 
2074689Sql147931 	syna = LSHIFT(nf, SA2400_SYNA_NF_MASK) | LSHIFT(n, SA2400_SYNA_N_MASK);
2084689Sql147931 	/* verify_syna(freq, syna); */
2094689Sql147931 
2104689Sql147931 	/*
2114689Sql147931 	 * Divide the 44MHz crystal down to 4MHz. Set the fractional
2124689Sql147931 	 * compensation charge pump value to agree with the fractional
2134689Sql147931 	 * modulus.
2144689Sql147931 	 */
2154689Sql147931 	synb = LSHIFT(11, SA2400_SYNB_R_MASK) | SA2400_SYNB_L_NORMAL |
2164689Sql147931 	    SA2400_SYNB_ON | SA2400_SYNB_ONE |
2174689Sql147931 	    LSHIFT(80, SA2400_SYNB_FC_MASK); /* agrees w/ SA2400_SYNA_FM = 0 */
2184689Sql147931 
2194689Sql147931 	sync = SA2400_SYNC_CP_NORMAL;
2204689Sql147931 
2214689Sql147931 	if ((rc = rtw_rfbus_write(bus, RTW_RFCHIPID_PHILIPS, SA2400_SYNA,
2224689Sql147931 	    syna)) != 0)
2234689Sql147931 		return (rc);
2244689Sql147931 	if ((rc = rtw_rfbus_write(bus, RTW_RFCHIPID_PHILIPS, SA2400_SYNB,
2254689Sql147931 	    synb)) != 0)
2264689Sql147931 		return (rc);
2274689Sql147931 	if ((rc = rtw_rfbus_write(bus, RTW_RFCHIPID_PHILIPS, SA2400_SYNC,
2284689Sql147931 	    sync)) != 0)
2294689Sql147931 		return (rc);
2304689Sql147931 	return (rtw_rfbus_write(bus, RTW_RFCHIPID_PHILIPS, SA2400_SYND, 0x0));
2314689Sql147931 }
2324689Sql147931 
2334689Sql147931 static int
rtw_sa2400_pwrstate(struct rtw_rf * rf,enum rtw_pwrstate power)2344689Sql147931 rtw_sa2400_pwrstate(struct rtw_rf *rf, enum rtw_pwrstate power)
2354689Sql147931 {
2364689Sql147931 	struct rtw_sa2400 *sa = (struct rtw_sa2400 *)rf;
2374689Sql147931 	struct rtw_rfbus *bus = &sa->sa_bus;
2384689Sql147931 	uint32_t opmode;
2394689Sql147931 	opmode = SA2400_OPMODE_DEFAULTS;
2404689Sql147931 	switch (power) {
2414689Sql147931 	case RTW_ON:
2424689Sql147931 		opmode |= SA2400_OPMODE_MODE_TXRX;
2434689Sql147931 		break;
2444689Sql147931 	case RTW_SLEEP:
2454689Sql147931 		opmode |= SA2400_OPMODE_MODE_WAIT;
2464689Sql147931 		break;
2474689Sql147931 	case RTW_OFF:
2484689Sql147931 		opmode |= SA2400_OPMODE_MODE_SLEEP;
2494689Sql147931 		break;
2504689Sql147931 	}
2514689Sql147931 
2524689Sql147931 	if (sa->sa_digphy)
2534689Sql147931 		opmode |= SA2400_OPMODE_DIGIN;
2544689Sql147931 
2554689Sql147931 	return (rtw_rfbus_write(bus, RTW_RFCHIPID_PHILIPS, SA2400_OPMODE,
2564689Sql147931 	    opmode));
2574689Sql147931 }
2584689Sql147931 
2594689Sql147931 static int
rtw_sa2400_manrx_init(struct rtw_sa2400 * sa)2604689Sql147931 rtw_sa2400_manrx_init(struct rtw_sa2400 *sa)
2614689Sql147931 {
2624689Sql147931 	uint32_t manrx;
2634689Sql147931 
2644689Sql147931 	/*
2654689Sql147931 	 * we are not supposed to be in RXMGC mode when we do
2664689Sql147931 	 * this?
2674689Sql147931 	 */
2684689Sql147931 	manrx = SA2400_MANRX_AHSN;
2694689Sql147931 	manrx |= SA2400_MANRX_TEN;
2704689Sql147931 	manrx |= LSHIFT(1023, SA2400_MANRX_RXGAIN_MASK);
2714689Sql147931 
2724689Sql147931 	return (rtw_rfbus_write(&sa->sa_bus, RTW_RFCHIPID_PHILIPS, SA2400_MANRX,
2734689Sql147931 	    manrx));
2744689Sql147931 }
2754689Sql147931 
2764689Sql147931 static int
rtw_sa2400_vcocal_start(struct rtw_sa2400 * sa,int start)2774689Sql147931 rtw_sa2400_vcocal_start(struct rtw_sa2400 *sa, int start)
2784689Sql147931 {
2794689Sql147931 	uint32_t opmode;
2804689Sql147931 
2814689Sql147931 	opmode = SA2400_OPMODE_DEFAULTS;
2824689Sql147931 	if (start)
2834689Sql147931 		opmode |= SA2400_OPMODE_MODE_VCOCALIB;
2844689Sql147931 	else
2854689Sql147931 		opmode |= SA2400_OPMODE_MODE_SLEEP;
2864689Sql147931 
2874689Sql147931 	if (sa->sa_digphy)
2884689Sql147931 		opmode |= SA2400_OPMODE_DIGIN;
2894689Sql147931 
2904689Sql147931 	return (rtw_rfbus_write(&sa->sa_bus, RTW_RFCHIPID_PHILIPS,
2914689Sql147931 	    SA2400_OPMODE, opmode));
2924689Sql147931 }
2934689Sql147931 
2944689Sql147931 static int
rtw_sa2400_vco_calibration(struct rtw_sa2400 * sa)2954689Sql147931 rtw_sa2400_vco_calibration(struct rtw_sa2400 *sa)
2964689Sql147931 {
2974689Sql147931 	int rc;
2984689Sql147931 	/*
2994689Sql147931 	 * calibrate VCO
3004689Sql147931 	 */
3014689Sql147931 	if ((rc = rtw_sa2400_vcocal_start(sa, 1)) != 0)
3024689Sql147931 		return (rc);
3034689Sql147931 	DELAY(2200);	/* 2.2 milliseconds */
3044689Sql147931 	/*
3054689Sql147931 	 * XXX superfluous: SA2400 automatically entered SLEEP mode.
3064689Sql147931 	 */
3074689Sql147931 	return (rtw_sa2400_vcocal_start(sa, 0));
3084689Sql147931 }
3094689Sql147931 
3104689Sql147931 static int
rtw_sa2400_filter_calibration(struct rtw_sa2400 * sa)3114689Sql147931 rtw_sa2400_filter_calibration(struct rtw_sa2400 *sa)
3124689Sql147931 {
3134689Sql147931 	uint32_t opmode;
3144689Sql147931 
3154689Sql147931 	opmode = SA2400_OPMODE_DEFAULTS | SA2400_OPMODE_MODE_FCALIB;
3164689Sql147931 	if (sa->sa_digphy)
3174689Sql147931 		opmode |= SA2400_OPMODE_DIGIN;
3184689Sql147931 
3194689Sql147931 	return (rtw_rfbus_write(&sa->sa_bus, RTW_RFCHIPID_PHILIPS,
3204689Sql147931 	    SA2400_OPMODE, opmode));
3214689Sql147931 }
3224689Sql147931 
3234689Sql147931 static int
rtw_sa2400_dc_calibration(struct rtw_sa2400 * sa)3244689Sql147931 rtw_sa2400_dc_calibration(struct rtw_sa2400 *sa)
3254689Sql147931 {
3264689Sql147931 	struct rtw_rf *rf = &sa->sa_rf;
3274689Sql147931 	int rc;
3284689Sql147931 	uint32_t dccal;
3294689Sql147931 
3304689Sql147931 	(*rf->rf_continuous_tx_cb)(rf->rf_continuous_tx_arg, 1);
3314689Sql147931 
3324689Sql147931 	dccal = SA2400_OPMODE_DEFAULTS | SA2400_OPMODE_MODE_TXRX;
3334689Sql147931 
3344689Sql147931 	rc = rtw_rfbus_write(&sa->sa_bus, RTW_RFCHIPID_PHILIPS, SA2400_OPMODE,
3354689Sql147931 	    dccal);
3364689Sql147931 	if (rc != 0)
3374689Sql147931 		return (rc);
3384689Sql147931 
3394689Sql147931 	DELAY(5); /* DCALIB after being in Tx mode for 5 microseconds */
3404689Sql147931 
3414689Sql147931 	dccal &= ~SA2400_OPMODE_MODE_MASK;
3424689Sql147931 	dccal |= SA2400_OPMODE_MODE_DCALIB;
3434689Sql147931 
3444689Sql147931 	rc = rtw_rfbus_write(&sa->sa_bus, RTW_RFCHIPID_PHILIPS, SA2400_OPMODE,
3454689Sql147931 	    dccal);
3464689Sql147931 	if (rc != 0)
3474689Sql147931 		return (rc);
3484689Sql147931 	DELAY(20); /* calibration takes at most 20 microseconds */
3494689Sql147931 
3504689Sql147931 	(*rf->rf_continuous_tx_cb)(rf->rf_continuous_tx_arg, 0);
3514689Sql147931 
3524689Sql147931 	return (0);
3534689Sql147931 }
3544689Sql147931 
3554689Sql147931 static int
rtw_sa2400_agc_init(struct rtw_sa2400 * sa)3564689Sql147931 rtw_sa2400_agc_init(struct rtw_sa2400 *sa)
3574689Sql147931 {
3584689Sql147931 	uint32_t agc;
3594689Sql147931 
3604689Sql147931 	agc = LSHIFT(25, SA2400_AGC_MAXGAIN_MASK);
3614689Sql147931 	agc |= LSHIFT(7, SA2400_AGC_BBPDELAY_MASK);
3624689Sql147931 	agc |= LSHIFT(15, SA2400_AGC_LNADELAY_MASK);
3634689Sql147931 	agc |= LSHIFT(27, SA2400_AGC_RXONDELAY_MASK);
3644689Sql147931 
3654689Sql147931 	return (rtw_rfbus_write(&sa->sa_bus, RTW_RFCHIPID_PHILIPS, SA2400_AGC,
3664689Sql147931 	    agc));
3674689Sql147931 }
3684689Sql147931 
3694689Sql147931 static void
rtw_sa2400_destroy(struct rtw_rf * rf)3704689Sql147931 rtw_sa2400_destroy(struct rtw_rf *rf)
3714689Sql147931 {
3724689Sql147931 	struct rtw_sa2400 *sa = (struct rtw_sa2400 *)rf;
3734689Sql147931 	kmem_free(sa, sizeof (*sa));
3744689Sql147931 }
3754689Sql147931 
3764689Sql147931 static int
rtw_sa2400_calibrate(struct rtw_rf * rf,uint_t freq)3774689Sql147931 rtw_sa2400_calibrate(struct rtw_rf *rf, uint_t freq)
3784689Sql147931 {
3794689Sql147931 	struct rtw_sa2400 *sa = (struct rtw_sa2400 *)rf;
3804689Sql147931 	int i, rc;
3814689Sql147931 
3824689Sql147931 	/*
3834689Sql147931 	 * XXX reference driver calibrates VCO twice. Is it a bug?
3844689Sql147931 	 */
3854689Sql147931 	for (i = 0; i < 2; i++) {
3864689Sql147931 		if ((rc = rtw_sa2400_vco_calibration(sa)) != 0)
3874689Sql147931 			return (rc);
3884689Sql147931 	}
3894689Sql147931 	/*
3904689Sql147931 	 * VCO calibration erases synthesizer registers, so re-tune
3914689Sql147931 	 */
3924689Sql147931 	if ((rc = rtw_sa2400_tune(rf, freq)) != 0)
3934689Sql147931 		return (rc);
3944689Sql147931 	if ((rc = rtw_sa2400_filter_calibration(sa)) != 0)
3954689Sql147931 		return (rc);
3964689Sql147931 	/*
3974689Sql147931 	 * analog PHY needs DC calibration
3984689Sql147931 	 */
3994689Sql147931 	if (!sa->sa_digphy)
4004689Sql147931 		return (rtw_sa2400_dc_calibration(sa));
4014689Sql147931 	return (0);
4024689Sql147931 }
4034689Sql147931 
4044689Sql147931 static int
rtw_sa2400_init(struct rtw_rf * rf,uint_t freq,uint8_t opaque_txpower,enum rtw_pwrstate power)4054689Sql147931 rtw_sa2400_init(struct rtw_rf *rf, uint_t freq, uint8_t opaque_txpower,
4064689Sql147931     enum rtw_pwrstate power)
4074689Sql147931 {
4084689Sql147931 	struct rtw_sa2400 *sa = (struct rtw_sa2400 *)rf;
4094689Sql147931 	int rc;
4104689Sql147931 
4114689Sql147931 	if ((rc = rtw_sa2400_txpower(rf, opaque_txpower)) != 0)
4124689Sql147931 		return (rc);
4134689Sql147931 
4144689Sql147931 	/*
4154689Sql147931 	 * skip configuration if it's time to sleep or to power-down.
4164689Sql147931 	 */
4174689Sql147931 	if (power == RTW_SLEEP || power == RTW_OFF)
4184689Sql147931 		return (rtw_sa2400_pwrstate(rf, power));
4194689Sql147931 
4204689Sql147931 	/*
4214689Sql147931 	 * go to sleep for configuration
4224689Sql147931 	 */
4234689Sql147931 	if ((rc = rtw_sa2400_pwrstate(rf, RTW_SLEEP)) != 0)
4244689Sql147931 		return (rc);
4254689Sql147931 
4264689Sql147931 	if ((rc = rtw_sa2400_tune(rf, freq)) != 0)
4274689Sql147931 		return (rc);
4284689Sql147931 	if ((rc = rtw_sa2400_agc_init(sa)) != 0)
4294689Sql147931 		return (rc);
4304689Sql147931 	if ((rc = rtw_sa2400_manrx_init(sa)) != 0)
4314689Sql147931 		return (rc);
4324689Sql147931 	if ((rc = rtw_sa2400_calibrate(rf, freq)) != 0)
4334689Sql147931 		return (rc);
4344689Sql147931 
4354689Sql147931 	/*
4364689Sql147931 	 * enter Tx/Rx mode
4374689Sql147931 	 */
4384689Sql147931 	return (rtw_sa2400_pwrstate(rf, power));
4394689Sql147931 }
4404689Sql147931 
4414689Sql147931 struct rtw_rf *
rtw_sa2400_create(struct rtw_regs * regs,rtw_rf_write_t rf_write,int digphy)4424689Sql147931 rtw_sa2400_create(struct rtw_regs *regs, rtw_rf_write_t rf_write, int digphy)
4434689Sql147931 {
4444689Sql147931 	struct rtw_sa2400 *sa;
4454689Sql147931 	struct rtw_rfbus *bus;
4464689Sql147931 	struct rtw_rf *rf;
4474689Sql147931 	struct rtw_bbpset *bb;
4484689Sql147931 
4494689Sql147931 	sa = (struct rtw_sa2400 *)kmem_zalloc(sizeof (*sa), KM_SLEEP);
4504689Sql147931 	if (sa == NULL)
4514689Sql147931 		return (NULL);
4524689Sql147931 
4534689Sql147931 	sa->sa_digphy = digphy;
4544689Sql147931 
4554689Sql147931 	rf = &sa->sa_rf;
4564689Sql147931 	bus = &sa->sa_bus;
4574689Sql147931 
4584689Sql147931 	rf->rf_init = rtw_sa2400_init;
4594689Sql147931 	rf->rf_destroy = rtw_sa2400_destroy;
4604689Sql147931 	rf->rf_txpower = rtw_sa2400_txpower;
4614689Sql147931 	rf->rf_tune = rtw_sa2400_tune;
4624689Sql147931 	rf->rf_pwrstate = rtw_sa2400_pwrstate;
4634689Sql147931 	bb = &rf->rf_bbpset;
4644689Sql147931 
4654689Sql147931 	/*
4664689Sql147931 	 * XXX magic
4674689Sql147931 	 */
4684689Sql147931 	bb->bb_antatten = RTW_BBP_ANTATTEN_PHILIPS_MAGIC;
4694689Sql147931 	bb->bb_chestlim =	0x00;
4704689Sql147931 	bb->bb_chsqlim =	0xa0;
4714689Sql147931 	bb->bb_ifagcdet =	0x64;
4724689Sql147931 	bb->bb_ifagcini =	0x90;
4734689Sql147931 	bb->bb_ifagclimit =	0x1a;
4744689Sql147931 	bb->bb_lnadet =		0xe0;
4754689Sql147931 	bb->bb_sys1 =		0x98;
4764689Sql147931 	bb->bb_sys2 =		0x47;
4774689Sql147931 	bb->bb_sys3 =		0x90;
4784689Sql147931 	bb->bb_trl =		0x88;
4794689Sql147931 	bb->bb_txagc =		0x38;
4804689Sql147931 
4814689Sql147931 	bus->b_regs = regs;
4824689Sql147931 	bus->b_write = rf_write;
4834689Sql147931 
4844689Sql147931 	return (&sa->sa_rf);
4854689Sql147931 }
4864689Sql147931 
4874689Sql147931 /*
4884689Sql147931  * freq is in MHz
4894689Sql147931  */
4904689Sql147931 static int
rtw_max2820_tune(struct rtw_rf * rf,uint_t freq)4914689Sql147931 rtw_max2820_tune(struct rtw_rf *rf, uint_t freq)
4924689Sql147931 {
4934689Sql147931 	struct rtw_max2820 *mx = (struct rtw_max2820 *)rf;
4944689Sql147931 	struct rtw_rfbus *bus = &mx->mx_bus;
4954689Sql147931 
4964689Sql147931 	if (freq < 2400 || freq > 2499)
4974689Sql147931 		return (-1);
4984689Sql147931 
4994689Sql147931 	return (rtw_rfbus_write(bus, RTW_RFCHIPID_MAXIM, MAX2820_CHANNEL,
5004689Sql147931 	    LSHIFT(freq - 2400, MAX2820_CHANNEL_CF_MASK)));
5014689Sql147931 }
5024689Sql147931 
5034689Sql147931 static void
rtw_max2820_destroy(struct rtw_rf * rf)5044689Sql147931 rtw_max2820_destroy(struct rtw_rf *rf)
5054689Sql147931 {
5064689Sql147931 	struct rtw_max2820 *mx = (struct rtw_max2820 *)rf;
5074689Sql147931 	kmem_free(mx, sizeof (*mx));
5084689Sql147931 }
5094689Sql147931 
5104689Sql147931 /*ARGSUSED*/
5114689Sql147931 static int
rtw_max2820_init(struct rtw_rf * rf,uint_t freq,uint8_t opaque_txpower,enum rtw_pwrstate power)5124689Sql147931 rtw_max2820_init(struct rtw_rf *rf, uint_t freq, uint8_t opaque_txpower,
5134689Sql147931     enum rtw_pwrstate power)
5144689Sql147931 {
5154689Sql147931 	struct rtw_max2820 *mx = (struct rtw_max2820 *)rf;
5164689Sql147931 	struct rtw_rfbus *bus = &mx->mx_bus;
5174689Sql147931 	int rc;
5184689Sql147931 
5194689Sql147931 	if ((rc = rtw_rfbus_write(bus, RTW_RFCHIPID_MAXIM, MAX2820_TEST,
5204689Sql147931 	    MAX2820_TEST_DEFAULT)) != 0)
5214689Sql147931 		return (rc);
5224689Sql147931 
5234689Sql147931 	if ((rc = rtw_rfbus_write(bus, RTW_RFCHIPID_MAXIM, MAX2820_ENABLE,
5244689Sql147931 	    MAX2820_ENABLE_DEFAULT)) != 0)
5254689Sql147931 		return (rc);
5264689Sql147931 
5274689Sql147931 	/*
5284689Sql147931 	 * skip configuration if it's time to sleep or to power-down.
5294689Sql147931 	 */
5304689Sql147931 	if ((rc = rtw_max2820_pwrstate(rf, power)) != 0)
5314689Sql147931 		return (rc);
5324689Sql147931 	else if (power == RTW_OFF || power == RTW_SLEEP)
5334689Sql147931 		return (0);
5344689Sql147931 
5354689Sql147931 	if ((rc = rtw_rfbus_write(bus, RTW_RFCHIPID_MAXIM, MAX2820_SYNTH,
5364689Sql147931 	    MAX2820_SYNTH_R_44MHZ)) != 0)
5374689Sql147931 		return (rc);
5384689Sql147931 
5394689Sql147931 	if ((rc = rtw_max2820_tune(rf, freq)) != 0)
5404689Sql147931 		return (rc);
5414689Sql147931 
5424689Sql147931 	/*
5434689Sql147931 	 * XXX The MAX2820 datasheet indicates that 1C and 2C should not
5444689Sql147931 	 * be changed from 7, however, the reference driver sets them
5454689Sql147931 	 * to 4 and 1, respectively.
5464689Sql147931 	 */
5474689Sql147931 	if ((rc = rtw_rfbus_write(bus, RTW_RFCHIPID_MAXIM, MAX2820_RECEIVE,
5484689Sql147931 	    MAX2820_RECEIVE_DL_DEFAULT |
5494689Sql147931 	    LSHIFT(4, MAX2820A_RECEIVE_1C_MASK) |
5504689Sql147931 	    LSHIFT(1, MAX2820A_RECEIVE_2C_MASK))) != 0)
5514689Sql147931 		return (rc);
5524689Sql147931 
5534689Sql147931 	return (rtw_rfbus_write(bus, RTW_RFCHIPID_MAXIM, MAX2820_TRANSMIT,
5544689Sql147931 	    MAX2820_TRANSMIT_PA_DEFAULT));
5554689Sql147931 }
5564689Sql147931 
5574689Sql147931 /*ARGSUSED*/
5584689Sql147931 static int
rtw_max2820_txpower(struct rtw_rf * rf,uint8_t opaque_txpower)5594689Sql147931 rtw_max2820_txpower(struct rtw_rf *rf, uint8_t opaque_txpower)
5604689Sql147931 {
5614689Sql147931 	/* TBD */
5624689Sql147931 	return (0);
5634689Sql147931 }
5644689Sql147931 
5654689Sql147931 static int
rtw_max2820_pwrstate(struct rtw_rf * rf,enum rtw_pwrstate power)5664689Sql147931 rtw_max2820_pwrstate(struct rtw_rf *rf, enum rtw_pwrstate power)
5674689Sql147931 {
5684689Sql147931 	uint32_t enable;
5694689Sql147931 	struct rtw_max2820 *mx;
5704689Sql147931 	struct rtw_rfbus *bus;
5714689Sql147931 
5724689Sql147931 	mx = (struct rtw_max2820 *)rf;
5734689Sql147931 	bus = &mx->mx_bus;
5744689Sql147931 
5754689Sql147931 	switch (power) {
5764689Sql147931 	case RTW_OFF:
5774689Sql147931 	case RTW_SLEEP:
5784689Sql147931 	default:
5794689Sql147931 		enable = 0x0;
5804689Sql147931 		break;
5814689Sql147931 	case RTW_ON:
5824689Sql147931 		enable = MAX2820_ENABLE_DEFAULT;
5834689Sql147931 		break;
5844689Sql147931 	}
5854689Sql147931 	return (rtw_rfbus_write(bus, RTW_RFCHIPID_MAXIM,
5864689Sql147931 	    MAX2820_ENABLE, enable));
5874689Sql147931 }
5884689Sql147931 
5894689Sql147931 struct rtw_rf *
rtw_max2820_create(struct rtw_regs * regs,rtw_rf_write_t rf_write,int is_a)5904689Sql147931 rtw_max2820_create(struct rtw_regs *regs, rtw_rf_write_t rf_write, int is_a)
5914689Sql147931 {
5924689Sql147931 	struct rtw_max2820 *mx;
5934689Sql147931 	struct rtw_rfbus *bus;
5944689Sql147931 	struct rtw_rf *rf;
5954689Sql147931 	struct rtw_bbpset *bb;
5964689Sql147931 
5974689Sql147931 	mx = (struct rtw_max2820 *)kmem_zalloc(sizeof (*mx), KM_SLEEP);
5984689Sql147931 	if (mx == NULL)
5994689Sql147931 		return (NULL);
6004689Sql147931 
6014689Sql147931 	mx->mx_is_a = is_a;
6024689Sql147931 
6034689Sql147931 	rf = &mx->mx_rf;
6044689Sql147931 	bus = &mx->mx_bus;
6054689Sql147931 
6064689Sql147931 	rf->rf_init = rtw_max2820_init;
6074689Sql147931 	rf->rf_destroy = rtw_max2820_destroy;
6084689Sql147931 	rf->rf_txpower = rtw_max2820_txpower;
6094689Sql147931 	rf->rf_tune = rtw_max2820_tune;
6104689Sql147931 	rf->rf_pwrstate = rtw_max2820_pwrstate;
6114689Sql147931 	bb = &rf->rf_bbpset;
6124689Sql147931 
6134689Sql147931 	/*
6144689Sql147931 	 * XXX magic
6154689Sql147931 	 */
6164689Sql147931 	bb->bb_antatten = RTW_BBP_ANTATTEN_MAXIM_MAGIC;
6174689Sql147931 	bb->bb_chestlim =	0;
6184689Sql147931 	bb->bb_chsqlim =	159;
6194689Sql147931 	bb->bb_ifagcdet =	100;
6204689Sql147931 	bb->bb_ifagcini =	144;
6214689Sql147931 	bb->bb_ifagclimit =	26;
6224689Sql147931 	bb->bb_lnadet =		248;
6234689Sql147931 	bb->bb_sys1 =		136;
6244689Sql147931 	bb->bb_sys2 =		71;
6254689Sql147931 	bb->bb_sys3 =		155;
6264689Sql147931 	bb->bb_trl =		136;
6274689Sql147931 	bb->bb_txagc =		8;
6284689Sql147931 
6294689Sql147931 	bus->b_regs = regs;
6304689Sql147931 	bus->b_write = rf_write;
6314689Sql147931 
6324689Sql147931 	return (&mx->mx_rf);
6334689Sql147931 }
6344689Sql147931 
6354689Sql147931 /*
6364689Sql147931  * freq is in MHz
6374689Sql147931  */
6384689Sql147931 int
rtw_phy_init(struct rtw_regs * regs,struct rtw_rf * rf,uint8_t opaque_txpower,uint8_t cs_threshold,uint_t freq,int antdiv,int dflantb,enum rtw_pwrstate power)6394689Sql147931 rtw_phy_init(struct rtw_regs *regs, struct rtw_rf *rf, uint8_t opaque_txpower,
6404689Sql147931     uint8_t cs_threshold, uint_t freq, int antdiv, int dflantb,
6414689Sql147931     enum rtw_pwrstate power)
6424689Sql147931 {
6434689Sql147931 	int rc;
6444689Sql147931 
6454689Sql147931 	/*
6464689Sql147931 	 * XXX is this really necessary?
6474689Sql147931 	 */
6484689Sql147931 	if ((rc = rtw_rf_txpower(rf, opaque_txpower)) != 0)
6494689Sql147931 		return (rc);
6504689Sql147931 	if ((rc = rtw_bbp_preinit(regs, rf->rf_bbpset.bb_antatten, dflantb,
6514689Sql147931 	    freq)) != 0)
6524689Sql147931 		return (rc);
6534689Sql147931 	if ((rc = rtw_rf_tune(rf, freq)) != 0)
6544689Sql147931 		return (rc);
6554689Sql147931 	/*
6564689Sql147931 	 * initialize RF
6574689Sql147931 	 */
6584689Sql147931 	if ((rc = rtw_rf_init(rf, freq, opaque_txpower, power)) != 0)
6594689Sql147931 		return (rc);
6604689Sql147931 #ifdef _RTW_FUTURE_DEBUG_
6614689Sql147931 	/* what is this redundant tx power setting here for? */
6624689Sql147931 	if ((rc = rtw_rf_txpower(rf, opaque_txpower)) != 0)
6634689Sql147931 		return (rc);
6644689Sql147931 #endif /* _RTW_FUTURE_DEBUG */
6654689Sql147931 	return (rtw_bbp_init(regs, &rf->rf_bbpset, antdiv, dflantb,
6664689Sql147931 	    cs_threshold, freq));
6674689Sql147931 }
668