11e10b93dSalc /*
21e10b93dSalc  * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
31e10b93dSalc  * Copyright (c) 2002-2008 Atheros Communications, Inc.
41e10b93dSalc  *
51e10b93dSalc  * Permission to use, copy, modify, and/or distribute this software for any
61e10b93dSalc  * purpose with or without fee is hereby granted, provided that the above
71e10b93dSalc  * copyright notice and this permission notice appear in all copies.
81e10b93dSalc  *
91e10b93dSalc  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
101e10b93dSalc  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
111e10b93dSalc  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
121e10b93dSalc  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
131e10b93dSalc  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
141e10b93dSalc  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
151e10b93dSalc  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
161e10b93dSalc  *
17*750b035cSmartin  * $Id: ar5416_beacon.c,v 1.2 2013/09/12 12:08:49 martin Exp $
181e10b93dSalc  */
191e10b93dSalc #include "opt_ah.h"
201e10b93dSalc 
211e10b93dSalc #include "ah.h"
221e10b93dSalc #include "ah_internal.h"
231e10b93dSalc 
241e10b93dSalc #include "ar5416/ar5416.h"
251e10b93dSalc #include "ar5416/ar5416reg.h"
261e10b93dSalc #include "ar5416/ar5416phy.h"
271e10b93dSalc 
281e10b93dSalc #define TU_TO_USEC(_tu)		((_tu) << 10)
291e10b93dSalc 
301e10b93dSalc /*
311e10b93dSalc  * Initialize all of the hardware registers used to
321e10b93dSalc  * send beacons.  Note that for station operation the
331e10b93dSalc  * driver calls ar5416SetStaBeaconTimers instead.
341e10b93dSalc  */
351e10b93dSalc void
ar5416SetBeaconTimers(struct ath_hal * ah,const HAL_BEACON_TIMERS * bt)361e10b93dSalc ar5416SetBeaconTimers(struct ath_hal *ah, const HAL_BEACON_TIMERS *bt)
371e10b93dSalc {
381e10b93dSalc 	uint32_t bperiod;
391e10b93dSalc 
401e10b93dSalc 	OS_REG_WRITE(ah, AR_NEXT_TBTT, TU_TO_USEC(bt->bt_nexttbtt));
411e10b93dSalc 	OS_REG_WRITE(ah, AR_NEXT_DBA, TU_TO_USEC(bt->bt_nextdba) >> 3);
421e10b93dSalc 	OS_REG_WRITE(ah, AR_NEXT_SWBA, TU_TO_USEC(bt->bt_nextswba) >> 3);
431e10b93dSalc 	OS_REG_WRITE(ah, AR_NEXT_NDP, TU_TO_USEC(bt->bt_nextatim));
441e10b93dSalc 
451e10b93dSalc 	bperiod = TU_TO_USEC(bt->bt_intval & HAL_BEACON_PERIOD);
461e10b93dSalc 	OS_REG_WRITE(ah, AR5416_BEACON_PERIOD, bperiod);
471e10b93dSalc 	OS_REG_WRITE(ah, AR_DBA_PERIOD, bperiod);
481e10b93dSalc 	OS_REG_WRITE(ah, AR_SWBA_PERIOD, bperiod);
491e10b93dSalc 	OS_REG_WRITE(ah, AR_NDP_PERIOD, bperiod);
501e10b93dSalc 
511e10b93dSalc 	/*
521e10b93dSalc 	 * Reset TSF if required.
531e10b93dSalc 	 */
541e10b93dSalc 	if (bt->bt_intval & AR_BEACON_RESET_TSF)
551e10b93dSalc 		ar5416ResetTsf(ah);
561e10b93dSalc 
571e10b93dSalc 	/* enable timers */
581e10b93dSalc 	/* NB: flags == 0 handled specially for backwards compatibility */
591e10b93dSalc 	OS_REG_SET_BIT(ah, AR_TIMER_MODE,
601e10b93dSalc 	    bt->bt_flags != 0 ? bt->bt_flags :
611e10b93dSalc 		AR_TIMER_MODE_TBTT | AR_TIMER_MODE_DBA | AR_TIMER_MODE_SWBA);
621e10b93dSalc }
631e10b93dSalc 
641e10b93dSalc /*
651e10b93dSalc  * Initializes all of the hardware registers used to
661e10b93dSalc  * send beacons.  Note that for station operation the
671e10b93dSalc  * driver calls ar5212SetStaBeaconTimers instead.
681e10b93dSalc  */
691e10b93dSalc void
ar5416BeaconInit(struct ath_hal * ah,uint32_t next_beacon,uint32_t beacon_period)701e10b93dSalc ar5416BeaconInit(struct ath_hal *ah,
711e10b93dSalc 	uint32_t next_beacon, uint32_t beacon_period)
721e10b93dSalc {
731e10b93dSalc 	HAL_BEACON_TIMERS bt;
741e10b93dSalc 
751e10b93dSalc 	bt.bt_nexttbtt = next_beacon;
761e10b93dSalc 	/*
771e10b93dSalc 	 * TIMER1: in AP/adhoc mode this controls the DMA beacon
781e10b93dSalc 	 * alert timer; otherwise it controls the next wakeup time.
791e10b93dSalc 	 * TIMER2: in AP mode, it controls the SBA beacon alert
801e10b93dSalc 	 * interrupt; otherwise it sets the start of the next CFP.
811e10b93dSalc 	 */
821e10b93dSalc 	bt.bt_flags = 0;
831e10b93dSalc 	switch (AH_PRIVATE(ah)->ah_opmode) {
841e10b93dSalc 	case HAL_M_STA:
851e10b93dSalc 	case HAL_M_MONITOR:
861e10b93dSalc 		bt.bt_nextdba = 0xffff;
871e10b93dSalc 		bt.bt_nextswba = 0x7ffff;
881e10b93dSalc 		bt.bt_flags |= AR_TIMER_MODE_TBTT;
891e10b93dSalc 		break;
901e10b93dSalc 	case HAL_M_IBSS:
911e10b93dSalc 		OS_REG_SET_BIT(ah, AR_TXCFG, AR_TXCFG_ATIM_TXPOLICY);
921e10b93dSalc 		bt.bt_flags |= AR_TIMER_MODE_NDP;
931e10b93dSalc 		/* fall thru... */
941e10b93dSalc 	case HAL_M_HOSTAP:
951e10b93dSalc 		bt.bt_nextdba = (next_beacon -
961e10b93dSalc 			ath_hal_dma_beacon_response_time) << 3;	/* 1/8 TU */
971e10b93dSalc 		bt.bt_nextswba = (next_beacon -
981e10b93dSalc 			ath_hal_sw_beacon_response_time) << 3;	/* 1/8 TU */
991e10b93dSalc 		bt.bt_flags |= AR_TIMER_MODE_TBTT
1001e10b93dSalc 			    |  AR_TIMER_MODE_DBA
1011e10b93dSalc 			    |  AR_TIMER_MODE_SWBA;
1021e10b93dSalc 		break;
1031e10b93dSalc 	}
1041e10b93dSalc 	/*
1051e10b93dSalc 	 * Set the ATIM window
1061e10b93dSalc 	 * Our hardware does not support an ATIM window of 0
1071e10b93dSalc 	 * (beacons will not work).  If the ATIM windows is 0,
1081e10b93dSalc 	 * force it to 1.
1091e10b93dSalc 	 */
1101e10b93dSalc 	bt.bt_nextatim = next_beacon + 1;
1111e10b93dSalc 	bt.bt_intval = beacon_period &
1121e10b93dSalc 		(AR_BEACON_PERIOD | AR_BEACON_RESET_TSF | AR_BEACON_EN);
1131e10b93dSalc 	ar5416SetBeaconTimers(ah, &bt);
1141e10b93dSalc }
1151e10b93dSalc 
1161e10b93dSalc #define AR_BEACON_PERIOD_MAX	0xffff
1171e10b93dSalc 
1181e10b93dSalc void
ar5416ResetStaBeaconTimers(struct ath_hal * ah)1191e10b93dSalc ar5416ResetStaBeaconTimers(struct ath_hal *ah)
1201e10b93dSalc {
1211e10b93dSalc 	uint32_t val;
1221e10b93dSalc 
1231e10b93dSalc 	OS_REG_WRITE(ah, AR_NEXT_TBTT, 0);		/* no beacons */
1241e10b93dSalc 	val = OS_REG_READ(ah, AR_STA_ID1);
1251e10b93dSalc 	val |= AR_STA_ID1_PWR_SAV;		/* XXX */
1261e10b93dSalc 	/* tell the h/w that the associated AP is not PCF capable */
1271e10b93dSalc 	OS_REG_WRITE(ah, AR_STA_ID1,
1281e10b93dSalc 		val & ~(AR_STA_ID1_USE_DEFANT | AR_STA_ID1_PCF));
1291e10b93dSalc 	OS_REG_WRITE(ah, AR5416_BEACON_PERIOD, AR_BEACON_PERIOD_MAX);
1301e10b93dSalc 	OS_REG_WRITE(ah, AR_DBA_PERIOD, AR_BEACON_PERIOD_MAX);
1311e10b93dSalc }
1321e10b93dSalc 
1331e10b93dSalc /*
1341e10b93dSalc  * Set all the beacon related bits on the h/w for stations
1351e10b93dSalc  * i.e. initializes the corresponding h/w timers;
1361e10b93dSalc  * also tells the h/w whether to anticipate PCF beacons
1371e10b93dSalc  */
1381e10b93dSalc void
ar5416SetStaBeaconTimers(struct ath_hal * ah,const HAL_BEACON_STATE * bs)1391e10b93dSalc ar5416SetStaBeaconTimers(struct ath_hal *ah, const HAL_BEACON_STATE *bs)
1401e10b93dSalc {
141*750b035cSmartin 	uint32_t nextTbtt,beaconintval, dtimperiod;
1421e10b93dSalc 
1431e10b93dSalc 	HALASSERT(bs->bs_intval != 0);
1441e10b93dSalc 
1451e10b93dSalc 	/* NB: no cfp setting since h/w automatically takes care */
1461e10b93dSalc 
1471e10b93dSalc 	OS_REG_WRITE(ah, AR_NEXT_TBTT, bs->bs_nexttbtt);
1481e10b93dSalc 
1491e10b93dSalc 	/*
1501e10b93dSalc 	 * Start the beacon timers by setting the BEACON register
1511e10b93dSalc 	 * to the beacon interval; no need to write tim offset since
1521e10b93dSalc 	 * h/w parses IEs.
1531e10b93dSalc 	 */
1541e10b93dSalc 	OS_REG_WRITE(ah, AR5416_BEACON_PERIOD,
1551e10b93dSalc 			 TU_TO_USEC(bs->bs_intval & HAL_BEACON_PERIOD));
1561e10b93dSalc 	OS_REG_WRITE(ah, AR_DBA_PERIOD,
1571e10b93dSalc 			 TU_TO_USEC(bs->bs_intval & HAL_BEACON_PERIOD));
1581e10b93dSalc 
1591e10b93dSalc 	/*
1601e10b93dSalc 	 * Configure the BMISS interrupt.  Note that we
1611e10b93dSalc 	 * assume the caller blocks interrupts while enabling
1621e10b93dSalc 	 * the threshold.
1631e10b93dSalc 	 */
1641e10b93dSalc 	HALASSERT(bs->bs_bmissthreshold <=
1651e10b93dSalc 		(AR_RSSI_THR_BM_THR >> AR_RSSI_THR_BM_THR_S));
1661e10b93dSalc 	OS_REG_RMW_FIELD(ah, AR_RSSI_THR,
1671e10b93dSalc 		AR_RSSI_THR_BM_THR, bs->bs_bmissthreshold);
1681e10b93dSalc 
1691e10b93dSalc 	/*
1701e10b93dSalc 	 * Program the sleep registers to correlate with the beacon setup.
1711e10b93dSalc 	 */
1721e10b93dSalc 
1731e10b93dSalc 	/*
1741e10b93dSalc 	 * Oahu beacons timers on the station were used for power
1751e10b93dSalc 	 * save operation (waking up in anticipation of a beacon)
1761e10b93dSalc 	 * and any CFP function; Venice does sleep/power-save timers
1771e10b93dSalc 	 * differently - so this is the right place to set them up;
1781e10b93dSalc 	 * don't think the beacon timers are used by venice sta hw
1791e10b93dSalc 	 * for any useful purpose anymore
1801e10b93dSalc 	 * Setup venice's sleep related timers
1811e10b93dSalc 	 * Current implementation assumes sw processing of beacons -
1821e10b93dSalc 	 *   assuming an interrupt is generated every beacon which
1831e10b93dSalc 	 *   causes the hardware to become awake until the sw tells
1841e10b93dSalc 	 *   it to go to sleep again; beacon timeout is to allow for
1851e10b93dSalc 	 *   beacon jitter; cab timeout is max time to wait for cab
1861e10b93dSalc 	 *   after seeing the last DTIM or MORE CAB bit
1871e10b93dSalc 	 */
1881e10b93dSalc #define CAB_TIMEOUT_VAL     10 /* in TU */
1891e10b93dSalc #define BEACON_TIMEOUT_VAL  10 /* in TU */
1901e10b93dSalc #define SLEEP_SLOP          3  /* in TU */
1911e10b93dSalc 
1921e10b93dSalc 	/*
1931e10b93dSalc 	 * For max powersave mode we may want to sleep for longer than a
1941e10b93dSalc 	 * beacon period and not want to receive all beacons; modify the
1951e10b93dSalc 	 * timers accordingly; make sure to align the next TIM to the
1961e10b93dSalc 	 * next DTIM if we decide to wake for DTIMs only
1971e10b93dSalc 	 */
1981e10b93dSalc 	beaconintval = bs->bs_intval & HAL_BEACON_PERIOD;
1991e10b93dSalc 	HALASSERT(beaconintval != 0);
2001e10b93dSalc 	if (bs->bs_sleepduration > beaconintval) {
2011e10b93dSalc 		HALASSERT(roundup(bs->bs_sleepduration, beaconintval) ==
2021e10b93dSalc 				bs->bs_sleepduration);
2031e10b93dSalc 		beaconintval = bs->bs_sleepduration;
2041e10b93dSalc 	}
2051e10b93dSalc 	dtimperiod = bs->bs_dtimperiod;
2061e10b93dSalc 	if (bs->bs_sleepduration > dtimperiod) {
2071e10b93dSalc 		HALASSERT(dtimperiod == 0 ||
2081e10b93dSalc 			roundup(bs->bs_sleepduration, dtimperiod) ==
2091e10b93dSalc 				bs->bs_sleepduration);
2101e10b93dSalc 		dtimperiod = bs->bs_sleepduration;
2111e10b93dSalc 	}
2121e10b93dSalc 	HALASSERT(beaconintval <= dtimperiod);
2131e10b93dSalc 	if (beaconintval == dtimperiod)
2141e10b93dSalc 		nextTbtt = bs->bs_nextdtim;
2151e10b93dSalc 	else
2161e10b93dSalc 		nextTbtt = bs->bs_nexttbtt;
2171e10b93dSalc 
2181e10b93dSalc 	OS_REG_WRITE(ah, AR_NEXT_DTIM,
2191e10b93dSalc 		TU_TO_USEC(bs->bs_nextdtim - SLEEP_SLOP));
2201e10b93dSalc 	OS_REG_WRITE(ah, AR_NEXT_TIM, TU_TO_USEC(nextTbtt - SLEEP_SLOP));
2211e10b93dSalc 
2221e10b93dSalc 	/* cab timeout is now in 1/8 TU */
2231e10b93dSalc 	OS_REG_WRITE(ah, AR_SLEEP1,
2241e10b93dSalc 		SM((CAB_TIMEOUT_VAL << 3), AR5416_SLEEP1_CAB_TIMEOUT)
2251e10b93dSalc 		| AR_SLEEP1_ASSUME_DTIM);
2261e10b93dSalc 	/* beacon timeout is now in 1/8 TU */
2271e10b93dSalc 	OS_REG_WRITE(ah, AR_SLEEP2,
2281e10b93dSalc 		SM((BEACON_TIMEOUT_VAL << 3), AR5416_SLEEP2_BEACON_TIMEOUT));
2291e10b93dSalc 
2301e10b93dSalc 	OS_REG_WRITE(ah, AR_TIM_PERIOD, beaconintval);
2311e10b93dSalc 	OS_REG_WRITE(ah, AR_DTIM_PERIOD, dtimperiod);
2321e10b93dSalc 	OS_REG_SET_BIT(ah, AR_TIMER_MODE,
2331e10b93dSalc 	     AR_TIMER_MODE_TBTT | AR_TIMER_MODE_TIM | AR_TIMER_MODE_DTIM);
2341e10b93dSalc 	HALDEBUG(ah, HAL_DEBUG_BEACON, "%s: next DTIM %d\n",
2351e10b93dSalc 	    __func__, bs->bs_nextdtim);
2361e10b93dSalc 	HALDEBUG(ah, HAL_DEBUG_BEACON, "%s: next beacon %d\n",
2371e10b93dSalc 	    __func__, nextTbtt);
2381e10b93dSalc 	HALDEBUG(ah, HAL_DEBUG_BEACON, "%s: beacon period %d\n",
2391e10b93dSalc 	    __func__, beaconintval);
2401e10b93dSalc 	HALDEBUG(ah, HAL_DEBUG_BEACON, "%s: DTIM period %d\n",
2411e10b93dSalc 	    __func__, dtimperiod);
2421e10b93dSalc #undef CAB_TIMEOUT_VAL
2431e10b93dSalc #undef BEACON_TIMEOUT_VAL
2441e10b93dSalc #undef SLEEP_SLOP
2451e10b93dSalc }
246