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.
22 #include "ah_internal.h"
24 #include "ar5416/ar5416.h"
25 #include "ar5416/ar5416reg.h"
26 #include "ar5416/ar5416phy.h"
28 #define TU_TO_USEC(_tu) ((_tu) << 10)
29 #define ONE_EIGHTH_TU_TO_USEC(_tu8) ((_tu8) << 7)
32 * Return the hardware NextTBTT in TSF
35 ar5416GetNextTBTT(struct ath_hal *ah)
37 return OS_REG_READ(ah, AR_NEXT_TBTT);
41 * Initialize all of the hardware registers used to
42 * send beacons. Note that for station operation the
43 * driver calls ar5416SetStaBeaconTimers instead.
46 ar5416SetBeaconTimers(struct ath_hal *ah, const HAL_BEACON_TIMERS *bt)
50 OS_REG_WRITE(ah, AR_NEXT_TBTT, TU_TO_USEC(bt->bt_nexttbtt));
51 OS_REG_WRITE(ah, AR_NEXT_DBA, ONE_EIGHTH_TU_TO_USEC(bt->bt_nextdba));
52 OS_REG_WRITE(ah, AR_NEXT_SWBA, ONE_EIGHTH_TU_TO_USEC(bt->bt_nextswba));
53 OS_REG_WRITE(ah, AR_NEXT_NDP, TU_TO_USEC(bt->bt_nextatim));
55 bperiod = TU_TO_USEC(bt->bt_intval & HAL_BEACON_PERIOD);
56 OS_REG_WRITE(ah, AR5416_BEACON_PERIOD, bperiod);
57 OS_REG_WRITE(ah, AR_DBA_PERIOD, bperiod);
58 OS_REG_WRITE(ah, AR_SWBA_PERIOD, bperiod);
59 OS_REG_WRITE(ah, AR_NDP_PERIOD, bperiod);
62 * Reset TSF if required.
64 if (bt->bt_intval & AR_BEACON_RESET_TSF)
68 /* NB: flags == 0 handled specially for backwards compatibility */
69 OS_REG_SET_BIT(ah, AR_TIMER_MODE,
70 bt->bt_flags != 0 ? bt->bt_flags :
71 AR_TIMER_MODE_TBTT | AR_TIMER_MODE_DBA | AR_TIMER_MODE_SWBA);
75 * Initializes all of the hardware registers used to
76 * send beacons. Note that for station operation the
77 * driver calls ar5212SetStaBeaconTimers instead.
80 ar5416BeaconInit(struct ath_hal *ah,
81 uint32_t next_beacon, uint32_t beacon_period)
85 bt.bt_nexttbtt = next_beacon;
87 * TIMER1: in AP/adhoc mode this controls the DMA beacon
88 * alert timer; otherwise it controls the next wakeup time.
89 * TIMER2: in AP mode, it controls the SBA beacon alert
90 * interrupt; otherwise it sets the start of the next CFP.
93 switch (AH_PRIVATE(ah)->ah_opmode) {
96 bt.bt_nextdba = 0xffff;
97 bt.bt_nextswba = 0x7ffff;
98 bt.bt_flags |= AR_TIMER_MODE_TBTT;
101 OS_REG_SET_BIT(ah, AR_TXCFG, AR_TXCFG_ATIM_TXPOLICY);
102 bt.bt_flags |= AR_TIMER_MODE_NDP;
105 bt.bt_nextdba = (next_beacon -
106 ah->ah_config.ah_dma_beacon_response_time) << 3; /* 1/8 TU */
107 bt.bt_nextswba = (next_beacon -
108 ah->ah_config.ah_sw_beacon_response_time) << 3; /* 1/8 TU */
109 bt.bt_flags |= AR_TIMER_MODE_TBTT
111 | AR_TIMER_MODE_SWBA;
115 * Set the ATIM window
116 * Our hardware does not support an ATIM window of 0
117 * (beacons will not work). If the ATIM windows is 0,
120 bt.bt_nextatim = next_beacon + 1;
121 bt.bt_intval = beacon_period &
122 (AR_BEACON_PERIOD | AR_BEACON_RESET_TSF | AR_BEACON_EN);
123 ar5416SetBeaconTimers(ah, &bt);
126 #define AR_BEACON_PERIOD_MAX 0xffff
129 ar5416ResetStaBeaconTimers(struct ath_hal *ah)
133 OS_REG_WRITE(ah, AR_NEXT_TBTT, 0); /* no beacons */
134 val = OS_REG_READ(ah, AR_STA_ID1);
135 val |= AR_STA_ID1_PWR_SAV; /* XXX */
136 /* tell the h/w that the associated AP is not PCF capable */
137 OS_REG_WRITE(ah, AR_STA_ID1,
138 val & ~(AR_STA_ID1_USE_DEFANT | AR_STA_ID1_PCF));
139 OS_REG_WRITE(ah, AR5416_BEACON_PERIOD, AR_BEACON_PERIOD_MAX);
140 OS_REG_WRITE(ah, AR_DBA_PERIOD, AR_BEACON_PERIOD_MAX);
144 * Set all the beacon related bits on the h/w for stations
145 * i.e. initializes the corresponding h/w timers;
146 * also tells the h/w whether to anticipate PCF beacons
149 ar5416SetStaBeaconTimers(struct ath_hal *ah, const HAL_BEACON_STATE *bs)
151 uint32_t nextTbtt, nextdtim,beaconintval, dtimperiod;
153 HALASSERT(bs->bs_intval != 0);
155 /* NB: no cfp setting since h/w automatically takes care */
157 OS_REG_WRITE(ah, AR_NEXT_TBTT, TU_TO_USEC(bs->bs_nexttbtt));
160 * Start the beacon timers by setting the BEACON register
161 * to the beacon interval; no need to write tim offset since
164 OS_REG_WRITE(ah, AR5416_BEACON_PERIOD,
165 TU_TO_USEC(bs->bs_intval & HAL_BEACON_PERIOD));
166 OS_REG_WRITE(ah, AR_DBA_PERIOD,
167 TU_TO_USEC(bs->bs_intval & HAL_BEACON_PERIOD));
170 * Configure the BMISS interrupt. Note that we
171 * assume the caller blocks interrupts while enabling
174 HALASSERT(bs->bs_bmissthreshold <=
175 (AR_RSSI_THR_BM_THR >> AR_RSSI_THR_BM_THR_S));
176 OS_REG_RMW_FIELD(ah, AR_RSSI_THR,
177 AR_RSSI_THR_BM_THR, bs->bs_bmissthreshold);
180 * Program the sleep registers to correlate with the beacon setup.
184 * Oahu beacons timers on the station were used for power
185 * save operation (waking up in anticipation of a beacon)
186 * and any CFP function; Venice does sleep/power-save timers
187 * differently - so this is the right place to set them up;
188 * don't think the beacon timers are used by venice sta hw
189 * for any useful purpose anymore
190 * Setup venice's sleep related timers
191 * Current implementation assumes sw processing of beacons -
192 * assuming an interrupt is generated every beacon which
193 * causes the hardware to become awake until the sw tells
194 * it to go to sleep again; beacon timeout is to allow for
195 * beacon jitter; cab timeout is max time to wait for cab
196 * after seeing the last DTIM or MORE CAB bit
198 #define CAB_TIMEOUT_VAL 10 /* in TU */
199 #define BEACON_TIMEOUT_VAL 10 /* in TU */
200 #define SLEEP_SLOP 3 /* in TU */
203 * For max powersave mode we may want to sleep for longer than a
204 * beacon period and not want to receive all beacons; modify the
205 * timers accordingly; make sure to align the next TIM to the
206 * next DTIM if we decide to wake for DTIMs only
208 beaconintval = bs->bs_intval & HAL_BEACON_PERIOD;
209 HALASSERT(beaconintval != 0);
210 if (bs->bs_sleepduration > beaconintval) {
211 HALASSERT(roundup(bs->bs_sleepduration, beaconintval) ==
212 bs->bs_sleepduration);
213 beaconintval = bs->bs_sleepduration;
215 dtimperiod = bs->bs_dtimperiod;
216 if (bs->bs_sleepduration > dtimperiod) {
217 HALASSERT(dtimperiod == 0 ||
218 roundup(bs->bs_sleepduration, dtimperiod) ==
219 bs->bs_sleepduration);
220 dtimperiod = bs->bs_sleepduration;
222 HALASSERT(beaconintval <= dtimperiod);
223 if (beaconintval == dtimperiod)
224 nextTbtt = bs->bs_nextdtim;
226 nextTbtt = bs->bs_nexttbtt;
227 nextdtim = bs->bs_nextdtim;
229 OS_REG_WRITE(ah, AR_NEXT_DTIM,
230 TU_TO_USEC(bs->bs_nextdtim - SLEEP_SLOP));
231 OS_REG_WRITE(ah, AR_NEXT_TIM, TU_TO_USEC(nextTbtt - SLEEP_SLOP));
233 /* cab timeout is now in 1/8 TU */
234 OS_REG_WRITE(ah, AR5416_SLEEP1,
235 SM((CAB_TIMEOUT_VAL << 3), AR5416_SLEEP1_CAB_TIMEOUT)
236 | AR5416_SLEEP1_ASSUME_DTIM);
238 /* XXX autosleep? Use min beacon timeout; check ath9k -adrian */
239 /* beacon timeout is now in 1/8 TU */
240 OS_REG_WRITE(ah, AR5416_SLEEP2,
241 SM((BEACON_TIMEOUT_VAL << 3), AR5416_SLEEP2_BEACON_TIMEOUT));
243 /* TIM_PERIOD and DTIM_PERIOD are now in uS. */
244 OS_REG_WRITE(ah, AR_TIM_PERIOD, TU_TO_USEC(beaconintval));
245 OS_REG_WRITE(ah, AR_DTIM_PERIOD, TU_TO_USEC(dtimperiod));
247 OS_REG_SET_BIT(ah, AR_TIMER_MODE,
248 AR_TIMER_MODE_TBTT | AR_TIMER_MODE_TIM | AR_TIMER_MODE_DTIM);
249 HALDEBUG(ah, HAL_DEBUG_BEACON, "%s: next DTIM %d\n",
250 __func__, bs->bs_nextdtim);
251 HALDEBUG(ah, HAL_DEBUG_BEACON, "%s: next beacon %d\n",
253 HALDEBUG(ah, HAL_DEBUG_BEACON, "%s: beacon period %d\n",
254 __func__, beaconintval);
255 HALDEBUG(ah, HAL_DEBUG_BEACON, "%s: DTIM period %d\n",
256 __func__, dtimperiod);
257 #undef CAB_TIMEOUT_VAL
258 #undef BEACON_TIMEOUT_VAL