2 * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
3 * Copyright (c) 2002-2008 Atheros Communications, Inc.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 * $FreeBSD: head/sys/dev/ath/ath_hal/ar5416/ar5416_beacon.c 203158 2010-01-29 10:07:17Z rpaulo $
23 #include "ah_internal.h"
25 #include "ar5416/ar5416.h"
26 #include "ar5416/ar5416reg.h"
27 #include "ar5416/ar5416phy.h"
29 #define TU_TO_USEC(_tu) ((_tu) << 10)
32 * Initialize all of the hardware registers used to
33 * send beacons. Note that for station operation the
34 * driver calls ar5416SetStaBeaconTimers instead.
37 ar5416SetBeaconTimers(struct ath_hal *ah, const HAL_BEACON_TIMERS *bt)
41 OS_REG_WRITE(ah, AR_NEXT_TBTT, TU_TO_USEC(bt->bt_nexttbtt));
42 OS_REG_WRITE(ah, AR_NEXT_DBA, TU_TO_USEC(bt->bt_nextdba) >> 3);
43 OS_REG_WRITE(ah, AR_NEXT_SWBA, TU_TO_USEC(bt->bt_nextswba) >> 3);
44 OS_REG_WRITE(ah, AR_NEXT_NDP, TU_TO_USEC(bt->bt_nextatim));
46 bperiod = TU_TO_USEC(bt->bt_intval & HAL_BEACON_PERIOD);
47 OS_REG_WRITE(ah, AR5416_BEACON_PERIOD, bperiod);
48 OS_REG_WRITE(ah, AR_DBA_PERIOD, bperiod);
49 OS_REG_WRITE(ah, AR_SWBA_PERIOD, bperiod);
50 OS_REG_WRITE(ah, AR_NDP_PERIOD, bperiod);
53 * Reset TSF if required.
55 if (bt->bt_intval & AR_BEACON_RESET_TSF)
59 /* NB: flags == 0 handled specially for backwards compatibility */
60 OS_REG_SET_BIT(ah, AR_TIMER_MODE,
61 bt->bt_flags != 0 ? bt->bt_flags :
62 AR_TIMER_MODE_TBTT | AR_TIMER_MODE_DBA | AR_TIMER_MODE_SWBA);
66 * Initializes all of the hardware registers used to
67 * send beacons. Note that for station operation the
68 * driver calls ar5212SetStaBeaconTimers instead.
71 ar5416BeaconInit(struct ath_hal *ah,
72 uint32_t next_beacon, uint32_t beacon_period)
76 bt.bt_nexttbtt = next_beacon;
78 * TIMER1: in AP/adhoc mode this controls the DMA beacon
79 * alert timer; otherwise it controls the next wakeup time.
80 * TIMER2: in AP mode, it controls the SBA beacon alert
81 * interrupt; otherwise it sets the start of the next CFP.
84 switch (AH_PRIVATE(ah)->ah_opmode) {
87 bt.bt_nextdba = 0xffff;
88 bt.bt_nextswba = 0x7ffff;
89 bt.bt_flags |= AR_TIMER_MODE_TBTT;
92 OS_REG_SET_BIT(ah, AR_TXCFG, AR_TXCFG_ATIM_TXPOLICY);
93 bt.bt_flags |= AR_TIMER_MODE_NDP;
96 bt.bt_nextdba = (next_beacon -
97 ath_hal_dma_beacon_response_time) << 3; /* 1/8 TU */
98 bt.bt_nextswba = (next_beacon -
99 ath_hal_sw_beacon_response_time) << 3; /* 1/8 TU */
100 bt.bt_flags |= AR_TIMER_MODE_TBTT
102 | AR_TIMER_MODE_SWBA;
106 * Set the ATIM window
107 * Our hardware does not support an ATIM window of 0
108 * (beacons will not work). If the ATIM windows is 0,
111 bt.bt_nextatim = next_beacon + 1;
112 bt.bt_intval = beacon_period &
113 (AR_BEACON_PERIOD | AR_BEACON_RESET_TSF | AR_BEACON_EN);
114 ar5416SetBeaconTimers(ah, &bt);
117 #define AR_BEACON_PERIOD_MAX 0xffff
120 ar5416ResetStaBeaconTimers(struct ath_hal *ah)
124 OS_REG_WRITE(ah, AR_NEXT_TBTT, 0); /* no beacons */
125 val = OS_REG_READ(ah, AR_STA_ID1);
126 val |= AR_STA_ID1_PWR_SAV; /* XXX */
127 /* tell the h/w that the associated AP is not PCF capable */
128 OS_REG_WRITE(ah, AR_STA_ID1,
129 val & ~(AR_STA_ID1_USE_DEFANT | AR_STA_ID1_PCF));
130 OS_REG_WRITE(ah, AR5416_BEACON_PERIOD, AR_BEACON_PERIOD_MAX);
131 OS_REG_WRITE(ah, AR_DBA_PERIOD, AR_BEACON_PERIOD_MAX);
135 * Set all the beacon related bits on the h/w for stations
136 * i.e. initializes the corresponding h/w timers;
137 * also tells the h/w whether to anticipate PCF beacons
140 ar5416SetStaBeaconTimers(struct ath_hal *ah, const HAL_BEACON_STATE *bs)
142 uint32_t nextTbtt, nextdtim,beaconintval, dtimperiod;
144 HALASSERT(bs->bs_intval != 0);
146 /* NB: no cfp setting since h/w automatically takes care */
148 OS_REG_WRITE(ah, AR_NEXT_TBTT, bs->bs_nexttbtt);
151 * Start the beacon timers by setting the BEACON register
152 * to the beacon interval; no need to write tim offset since
155 OS_REG_WRITE(ah, AR5416_BEACON_PERIOD,
156 TU_TO_USEC(bs->bs_intval & HAL_BEACON_PERIOD));
157 OS_REG_WRITE(ah, AR_DBA_PERIOD,
158 TU_TO_USEC(bs->bs_intval & HAL_BEACON_PERIOD));
161 * Configure the BMISS interrupt. Note that we
162 * assume the caller blocks interrupts while enabling
165 HALASSERT(bs->bs_bmissthreshold <=
166 (AR_RSSI_THR_BM_THR >> AR_RSSI_THR_BM_THR_S));
167 OS_REG_RMW_FIELD(ah, AR_RSSI_THR,
168 AR_RSSI_THR_BM_THR, bs->bs_bmissthreshold);
171 * Program the sleep registers to correlate with the beacon setup.
175 * Oahu beacons timers on the station were used for power
176 * save operation (waking up in anticipation of a beacon)
177 * and any CFP function; Venice does sleep/power-save timers
178 * differently - so this is the right place to set them up;
179 * don't think the beacon timers are used by venice sta hw
180 * for any useful purpose anymore
181 * Setup venice's sleep related timers
182 * Current implementation assumes sw processing of beacons -
183 * assuming an interrupt is generated every beacon which
184 * causes the hardware to become awake until the sw tells
185 * it to go to sleep again; beacon timeout is to allow for
186 * beacon jitter; cab timeout is max time to wait for cab
187 * after seeing the last DTIM or MORE CAB bit
189 #define CAB_TIMEOUT_VAL 10 /* in TU */
190 #define BEACON_TIMEOUT_VAL 10 /* in TU */
191 #define SLEEP_SLOP 3 /* in TU */
194 * For max powersave mode we may want to sleep for longer than a
195 * beacon period and not want to receive all beacons; modify the
196 * timers accordingly; make sure to align the next TIM to the
197 * next DTIM if we decide to wake for DTIMs only
199 beaconintval = bs->bs_intval & HAL_BEACON_PERIOD;
200 HALASSERT(beaconintval != 0);
201 if (bs->bs_sleepduration > beaconintval) {
202 HALASSERT(roundup(bs->bs_sleepduration, beaconintval) ==
203 bs->bs_sleepduration);
204 beaconintval = bs->bs_sleepduration;
206 dtimperiod = bs->bs_dtimperiod;
207 if (bs->bs_sleepduration > dtimperiod) {
208 HALASSERT(dtimperiod == 0 ||
209 roundup(bs->bs_sleepduration, dtimperiod) ==
210 bs->bs_sleepduration);
211 dtimperiod = bs->bs_sleepduration;
213 HALASSERT(beaconintval <= dtimperiod);
214 if (beaconintval == dtimperiod)
215 nextTbtt = bs->bs_nextdtim;
217 nextTbtt = bs->bs_nexttbtt;
218 nextdtim = bs->bs_nextdtim;
220 OS_REG_WRITE(ah, AR_NEXT_DTIM,
221 TU_TO_USEC(bs->bs_nextdtim - SLEEP_SLOP));
222 OS_REG_WRITE(ah, AR_NEXT_TIM, TU_TO_USEC(nextTbtt - SLEEP_SLOP));
224 /* cab timeout is now in 1/8 TU */
225 OS_REG_WRITE(ah, AR_SLEEP1,
226 SM((CAB_TIMEOUT_VAL << 3), AR5416_SLEEP1_CAB_TIMEOUT)
227 | AR_SLEEP1_ASSUME_DTIM);
228 /* beacon timeout is now in 1/8 TU */
229 OS_REG_WRITE(ah, AR_SLEEP2,
230 SM((BEACON_TIMEOUT_VAL << 3), AR5416_SLEEP2_BEACON_TIMEOUT));
232 OS_REG_WRITE(ah, AR_TIM_PERIOD, beaconintval);
233 OS_REG_WRITE(ah, AR_DTIM_PERIOD, dtimperiod);
234 OS_REG_SET_BIT(ah, AR_TIMER_MODE,
235 AR_TIMER_MODE_TBTT | AR_TIMER_MODE_TIM | AR_TIMER_MODE_DTIM);
236 HALDEBUG(ah, HAL_DEBUG_BEACON, "%s: next DTIM %d\n",
237 __func__, bs->bs_nextdtim);
238 HALDEBUG(ah, HAL_DEBUG_BEACON, "%s: next beacon %d\n",
240 HALDEBUG(ah, HAL_DEBUG_BEACON, "%s: beacon period %d\n",
241 __func__, beaconintval);
242 HALDEBUG(ah, HAL_DEBUG_BEACON, "%s: DTIM period %d\n",
243 __func__, dtimperiod);
244 #undef CAB_TIMEOUT_VAL
245 #undef BEACON_TIMEOUT_VAL