1572ff6f6SMatthew Dillon /*
2572ff6f6SMatthew Dillon  * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
3572ff6f6SMatthew Dillon  * Copyright (c) 2002-2008 Atheros Communications, Inc.
4572ff6f6SMatthew Dillon  *
5572ff6f6SMatthew Dillon  * Permission to use, copy, modify, and/or distribute this software for any
6572ff6f6SMatthew Dillon  * purpose with or without fee is hereby granted, provided that the above
7572ff6f6SMatthew Dillon  * copyright notice and this permission notice appear in all copies.
8572ff6f6SMatthew Dillon  *
9572ff6f6SMatthew Dillon  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10572ff6f6SMatthew Dillon  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11572ff6f6SMatthew Dillon  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12572ff6f6SMatthew Dillon  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13572ff6f6SMatthew Dillon  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14572ff6f6SMatthew Dillon  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15572ff6f6SMatthew Dillon  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16572ff6f6SMatthew Dillon  *
17572ff6f6SMatthew Dillon  * $FreeBSD$
18572ff6f6SMatthew Dillon  */
19572ff6f6SMatthew Dillon #include "opt_ah.h"
20572ff6f6SMatthew Dillon 
21572ff6f6SMatthew Dillon #include "ah.h"
22572ff6f6SMatthew Dillon #include "ah_internal.h"
23572ff6f6SMatthew Dillon 
24572ff6f6SMatthew Dillon #include "ar5212/ar5212.h"
25572ff6f6SMatthew Dillon #include "ar5212/ar5212reg.h"
26572ff6f6SMatthew Dillon #include "ar5212/ar5212desc.h"
27572ff6f6SMatthew Dillon 
28572ff6f6SMatthew Dillon /*
29572ff6f6SMatthew Dillon  * Return the hardware NextTBTT in TSF
30572ff6f6SMatthew Dillon  */
31572ff6f6SMatthew Dillon uint64_t
ar5212GetNextTBTT(struct ath_hal * ah)32572ff6f6SMatthew Dillon ar5212GetNextTBTT(struct ath_hal *ah)
33572ff6f6SMatthew Dillon {
34572ff6f6SMatthew Dillon #define TU_TO_TSF(_tu)	(((uint64_t)(_tu)) << 10)
35572ff6f6SMatthew Dillon 	return TU_TO_TSF(OS_REG_READ(ah, AR_TIMER0));
36572ff6f6SMatthew Dillon #undef TU_TO_TSF
37572ff6f6SMatthew Dillon }
38572ff6f6SMatthew Dillon 
39572ff6f6SMatthew Dillon /*
40572ff6f6SMatthew Dillon  * Initialize all of the hardware registers used to
41572ff6f6SMatthew Dillon  * send beacons.  Note that for station operation the
42572ff6f6SMatthew Dillon  * driver calls ar5212SetStaBeaconTimers instead.
43572ff6f6SMatthew Dillon  */
44572ff6f6SMatthew Dillon void
ar5212SetBeaconTimers(struct ath_hal * ah,const HAL_BEACON_TIMERS * bt)45572ff6f6SMatthew Dillon ar5212SetBeaconTimers(struct ath_hal *ah, const HAL_BEACON_TIMERS *bt)
46572ff6f6SMatthew Dillon {
47572ff6f6SMatthew Dillon 	struct ath_hal_5212 *ahp = AH5212(ah);
48572ff6f6SMatthew Dillon 
49572ff6f6SMatthew Dillon 	/*
50572ff6f6SMatthew Dillon 	 * Limit the timers to their specific resolutions:
51572ff6f6SMatthew Dillon 	 *
52572ff6f6SMatthew Dillon 	 * + Timer 0 - 0..15 0xffff TU
53572ff6f6SMatthew Dillon 	 * + Timer 1 - 0..18 0x7ffff TU/8
54572ff6f6SMatthew Dillon 	 * + Timer 2 - 0..24 0x1ffffff TU/8
55572ff6f6SMatthew Dillon 	 * + Timer 3 - 0..15 0xffff TU
56572ff6f6SMatthew Dillon 	 */
57572ff6f6SMatthew Dillon 	OS_REG_WRITE(ah, AR_TIMER0, bt->bt_nexttbtt & 0xffff);
58572ff6f6SMatthew Dillon 	OS_REG_WRITE(ah, AR_TIMER1, bt->bt_nextdba & 0x7ffff);
59572ff6f6SMatthew Dillon 	OS_REG_WRITE(ah, AR_TIMER2, bt->bt_nextswba & 0x1ffffff);
60572ff6f6SMatthew Dillon 	/* XXX force nextatim to be non-zero? */
61572ff6f6SMatthew Dillon 	OS_REG_WRITE(ah, AR_TIMER3, bt->bt_nextatim & 0xffff);
62572ff6f6SMatthew Dillon 	/*
63572ff6f6SMatthew Dillon 	 * Set the Beacon register after setting all timers.
64572ff6f6SMatthew Dillon 	 */
65572ff6f6SMatthew Dillon 	if (bt->bt_intval & AR_BEACON_RESET_TSF) {
66572ff6f6SMatthew Dillon 		/*
67572ff6f6SMatthew Dillon 		 * When resetting the TSF,
68572ff6f6SMatthew Dillon 		 * write twice to the corresponding register; each
69572ff6f6SMatthew Dillon 		 * write to the RESET_TSF bit toggles the internal
70572ff6f6SMatthew Dillon 		 * signal to cause a reset of the TSF - but if the signal
71572ff6f6SMatthew Dillon 		 * is left high, it will reset the TSF on the next
72572ff6f6SMatthew Dillon 		 * chip reset also! writing the bit an even number
73572ff6f6SMatthew Dillon 		 * of times fixes this issue
74572ff6f6SMatthew Dillon 		 */
75572ff6f6SMatthew Dillon 		OS_REG_WRITE(ah, AR_BEACON, AR_BEACON_RESET_TSF);
76572ff6f6SMatthew Dillon 	}
77572ff6f6SMatthew Dillon 	OS_REG_WRITE(ah, AR_BEACON, bt->bt_intval);
78572ff6f6SMatthew Dillon 	ahp->ah_beaconInterval = (bt->bt_intval & HAL_BEACON_PERIOD);
79572ff6f6SMatthew Dillon }
80572ff6f6SMatthew Dillon 
81572ff6f6SMatthew Dillon /*
82572ff6f6SMatthew Dillon  * Old api for setting up beacon timer registers when
83572ff6f6SMatthew Dillon  * operating in !station mode.  Note the fixed constants
84572ff6f6SMatthew Dillon  * adjusting the DBA and SWBA timers and the fixed ATIM
85572ff6f6SMatthew Dillon  * window.
86572ff6f6SMatthew Dillon  */
87572ff6f6SMatthew Dillon void
ar5212BeaconInit(struct ath_hal * ah,uint32_t next_beacon,uint32_t beacon_period)88572ff6f6SMatthew Dillon ar5212BeaconInit(struct ath_hal *ah,
89572ff6f6SMatthew Dillon 	uint32_t next_beacon, uint32_t beacon_period)
90572ff6f6SMatthew Dillon {
91572ff6f6SMatthew Dillon 	HAL_BEACON_TIMERS bt;
92572ff6f6SMatthew Dillon 
93*57e09377SMatthew Dillon 	bzero(&bt, sizeof(bt));
94572ff6f6SMatthew Dillon 	bt.bt_nexttbtt = next_beacon;
95572ff6f6SMatthew Dillon 	/*
96572ff6f6SMatthew Dillon 	 * TIMER1: in AP/adhoc mode this controls the DMA beacon
97572ff6f6SMatthew Dillon 	 * alert timer; otherwise it controls the next wakeup time.
98572ff6f6SMatthew Dillon 	 * TIMER2: in AP mode, it controls the SBA beacon alert
99572ff6f6SMatthew Dillon 	 * interrupt; otherwise it sets the start of the next CFP.
100572ff6f6SMatthew Dillon 	 */
101572ff6f6SMatthew Dillon 	switch (AH_PRIVATE(ah)->ah_opmode) {
102572ff6f6SMatthew Dillon 	case HAL_M_STA:
103572ff6f6SMatthew Dillon 	case HAL_M_MONITOR:
104572ff6f6SMatthew Dillon 		bt.bt_nextdba = 0xffff;
105572ff6f6SMatthew Dillon 		bt.bt_nextswba = 0x7ffff;
106572ff6f6SMatthew Dillon 		break;
107572ff6f6SMatthew Dillon 	case HAL_M_HOSTAP:
108572ff6f6SMatthew Dillon 	case HAL_M_IBSS:
109572ff6f6SMatthew Dillon 		bt.bt_nextdba = (next_beacon -
110572ff6f6SMatthew Dillon 		    ah->ah_config.ah_dma_beacon_response_time) << 3; /* 1/8 TU */
111572ff6f6SMatthew Dillon 		bt.bt_nextswba = (next_beacon -
112572ff6f6SMatthew Dillon 		    ah->ah_config.ah_sw_beacon_response_time) << 3;	/* 1/8 TU */
113572ff6f6SMatthew Dillon 		break;
114572ff6f6SMatthew Dillon 	}
115572ff6f6SMatthew Dillon 	/*
116572ff6f6SMatthew Dillon 	 * Set the ATIM window
117572ff6f6SMatthew Dillon 	 * Our hardware does not support an ATIM window of 0
118572ff6f6SMatthew Dillon 	 * (beacons will not work).  If the ATIM windows is 0,
119572ff6f6SMatthew Dillon 	 * force it to 1.
120572ff6f6SMatthew Dillon 	 */
121572ff6f6SMatthew Dillon 	bt.bt_nextatim = next_beacon + 1;
122572ff6f6SMatthew Dillon 	bt.bt_intval = beacon_period &
123572ff6f6SMatthew Dillon 		(AR_BEACON_PERIOD | AR_BEACON_RESET_TSF | AR_BEACON_EN);
124572ff6f6SMatthew Dillon 	ar5212SetBeaconTimers(ah, &bt);
125572ff6f6SMatthew Dillon }
126572ff6f6SMatthew Dillon 
127572ff6f6SMatthew Dillon void
ar5212ResetStaBeaconTimers(struct ath_hal * ah)128572ff6f6SMatthew Dillon ar5212ResetStaBeaconTimers(struct ath_hal *ah)
129572ff6f6SMatthew Dillon {
130572ff6f6SMatthew Dillon 	uint32_t val;
131572ff6f6SMatthew Dillon 
132572ff6f6SMatthew Dillon 	OS_REG_WRITE(ah, AR_TIMER0, 0);		/* no beacons */
133572ff6f6SMatthew Dillon 	val = OS_REG_READ(ah, AR_STA_ID1);
134572ff6f6SMatthew Dillon 	val |= AR_STA_ID1_PWR_SAV;		/* XXX */
135572ff6f6SMatthew Dillon 	/* tell the h/w that the associated AP is not PCF capable */
136572ff6f6SMatthew Dillon 	OS_REG_WRITE(ah, AR_STA_ID1,
137572ff6f6SMatthew Dillon 		val & ~(AR_STA_ID1_USE_DEFANT | AR_STA_ID1_PCF));
138572ff6f6SMatthew Dillon 	OS_REG_WRITE(ah, AR_BEACON, AR_BEACON_PERIOD);
139572ff6f6SMatthew Dillon }
140572ff6f6SMatthew Dillon 
141572ff6f6SMatthew Dillon /*
142572ff6f6SMatthew Dillon  * Set all the beacon related bits on the h/w for stations
143572ff6f6SMatthew Dillon  * i.e. initializes the corresponding h/w timers;
144572ff6f6SMatthew Dillon  * also tells the h/w whether to anticipate PCF beacons
145572ff6f6SMatthew Dillon  */
146572ff6f6SMatthew Dillon void
ar5212SetStaBeaconTimers(struct ath_hal * ah,const HAL_BEACON_STATE * bs)147572ff6f6SMatthew Dillon ar5212SetStaBeaconTimers(struct ath_hal *ah, const HAL_BEACON_STATE *bs)
148572ff6f6SMatthew Dillon {
149572ff6f6SMatthew Dillon 	struct ath_hal_5212 *ahp = AH5212(ah);
150572ff6f6SMatthew Dillon 	uint32_t nextTbtt, nextdtim,beaconintval, dtimperiod;
151572ff6f6SMatthew Dillon 
152572ff6f6SMatthew Dillon 	HALASSERT(bs->bs_intval != 0);
153572ff6f6SMatthew Dillon 	/* if the AP will do PCF */
154572ff6f6SMatthew Dillon 	if (bs->bs_cfpmaxduration != 0) {
155572ff6f6SMatthew Dillon 		/* tell the h/w that the associated AP is PCF capable */
156572ff6f6SMatthew Dillon 		OS_REG_WRITE(ah, AR_STA_ID1,
157572ff6f6SMatthew Dillon 			OS_REG_READ(ah, AR_STA_ID1) | AR_STA_ID1_PCF);
158572ff6f6SMatthew Dillon 
159572ff6f6SMatthew Dillon 		/* set CFP_PERIOD(1.024ms) register */
160572ff6f6SMatthew Dillon 		OS_REG_WRITE(ah, AR_CFP_PERIOD, bs->bs_cfpperiod);
161572ff6f6SMatthew Dillon 
162572ff6f6SMatthew Dillon 		/* set CFP_DUR(1.024ms) register to max cfp duration */
163572ff6f6SMatthew Dillon 		OS_REG_WRITE(ah, AR_CFP_DUR, bs->bs_cfpmaxduration);
164572ff6f6SMatthew Dillon 
165572ff6f6SMatthew Dillon 		/* set TIMER2(128us) to anticipated time of next CFP */
166572ff6f6SMatthew Dillon 		OS_REG_WRITE(ah, AR_TIMER2, bs->bs_cfpnext << 3);
167572ff6f6SMatthew Dillon 	} else {
168572ff6f6SMatthew Dillon 		/* tell the h/w that the associated AP is not PCF capable */
169572ff6f6SMatthew Dillon 		OS_REG_WRITE(ah, AR_STA_ID1,
170572ff6f6SMatthew Dillon 			OS_REG_READ(ah, AR_STA_ID1) &~ AR_STA_ID1_PCF);
171572ff6f6SMatthew Dillon 	}
172572ff6f6SMatthew Dillon 
173572ff6f6SMatthew Dillon 	/*
174572ff6f6SMatthew Dillon 	 * Set TIMER0(1.024ms) to the anticipated time of the next beacon.
175572ff6f6SMatthew Dillon 	 */
176572ff6f6SMatthew Dillon 	OS_REG_WRITE(ah, AR_TIMER0, bs->bs_nexttbtt);
177572ff6f6SMatthew Dillon 
178572ff6f6SMatthew Dillon 	/*
179572ff6f6SMatthew Dillon 	 * Start the beacon timers by setting the BEACON register
180572ff6f6SMatthew Dillon 	 * to the beacon interval; also write the tim offset which
181572ff6f6SMatthew Dillon 	 * we should know by now.  The code, in ar5211WriteAssocid,
182572ff6f6SMatthew Dillon 	 * also sets the tim offset once the AID is known which can
183572ff6f6SMatthew Dillon 	 * be left as such for now.
184572ff6f6SMatthew Dillon 	 */
185572ff6f6SMatthew Dillon 	OS_REG_WRITE(ah, AR_BEACON,
186572ff6f6SMatthew Dillon 		(OS_REG_READ(ah, AR_BEACON) &~ (AR_BEACON_PERIOD|AR_BEACON_TIM))
187572ff6f6SMatthew Dillon 		| SM(bs->bs_intval, AR_BEACON_PERIOD)
188572ff6f6SMatthew Dillon 		| SM(bs->bs_timoffset ? bs->bs_timoffset + 4 : 0, AR_BEACON_TIM)
189572ff6f6SMatthew Dillon 	);
190572ff6f6SMatthew Dillon 
191572ff6f6SMatthew Dillon 	/*
192572ff6f6SMatthew Dillon 	 * Configure the BMISS interrupt.  Note that we
193572ff6f6SMatthew Dillon 	 * assume the caller blocks interrupts while enabling
194572ff6f6SMatthew Dillon 	 * the threshold.
195572ff6f6SMatthew Dillon 	 */
196572ff6f6SMatthew Dillon 	HALASSERT(bs->bs_bmissthreshold <= MS(0xffffffff, AR_RSSI_THR_BM_THR));
197572ff6f6SMatthew Dillon 	ahp->ah_rssiThr = (ahp->ah_rssiThr &~ AR_RSSI_THR_BM_THR)
198572ff6f6SMatthew Dillon 			| SM(bs->bs_bmissthreshold, AR_RSSI_THR_BM_THR);
199572ff6f6SMatthew Dillon 	OS_REG_WRITE(ah, AR_RSSI_THR, ahp->ah_rssiThr);
200572ff6f6SMatthew Dillon 
201572ff6f6SMatthew Dillon 	/*
202572ff6f6SMatthew Dillon 	 * Program the sleep registers to correlate with the beacon setup.
203572ff6f6SMatthew Dillon 	 */
204572ff6f6SMatthew Dillon 
205572ff6f6SMatthew Dillon 	/*
206572ff6f6SMatthew Dillon 	 * Oahu beacons timers on the station were used for power
207572ff6f6SMatthew Dillon 	 * save operation (waking up in anticipation of a beacon)
208572ff6f6SMatthew Dillon 	 * and any CFP function; Venice does sleep/power-save timers
209572ff6f6SMatthew Dillon 	 * differently - so this is the right place to set them up;
210572ff6f6SMatthew Dillon 	 * don't think the beacon timers are used by venice sta hw
211572ff6f6SMatthew Dillon 	 * for any useful purpose anymore
212572ff6f6SMatthew Dillon 	 * Setup venice's sleep related timers
213572ff6f6SMatthew Dillon 	 * Current implementation assumes sw processing of beacons -
214572ff6f6SMatthew Dillon 	 *   assuming an interrupt is generated every beacon which
215572ff6f6SMatthew Dillon 	 *   causes the hardware to become awake until the sw tells
216572ff6f6SMatthew Dillon 	 *   it to go to sleep again; beacon timeout is to allow for
217572ff6f6SMatthew Dillon 	 *   beacon jitter; cab timeout is max time to wait for cab
218572ff6f6SMatthew Dillon 	 *   after seeing the last DTIM or MORE CAB bit
219572ff6f6SMatthew Dillon 	 */
220572ff6f6SMatthew Dillon #define CAB_TIMEOUT_VAL     10 /* in TU */
221572ff6f6SMatthew Dillon #define BEACON_TIMEOUT_VAL  10 /* in TU */
222572ff6f6SMatthew Dillon #define SLEEP_SLOP          3  /* in TU */
223572ff6f6SMatthew Dillon 
224572ff6f6SMatthew Dillon 	/*
225572ff6f6SMatthew Dillon 	 * For max powersave mode we may want to sleep for longer than a
226572ff6f6SMatthew Dillon 	 * beacon period and not want to receive all beacons; modify the
227572ff6f6SMatthew Dillon 	 * timers accordingly; make sure to align the next TIM to the
228572ff6f6SMatthew Dillon 	 * next DTIM if we decide to wake for DTIMs only
229572ff6f6SMatthew Dillon 	 */
230572ff6f6SMatthew Dillon 	beaconintval = bs->bs_intval & HAL_BEACON_PERIOD;
231572ff6f6SMatthew Dillon 	HALASSERT(beaconintval != 0);
232572ff6f6SMatthew Dillon 	if (bs->bs_sleepduration > beaconintval) {
233572ff6f6SMatthew Dillon 		HALASSERT(roundup(bs->bs_sleepduration, beaconintval) ==
234572ff6f6SMatthew Dillon 				bs->bs_sleepduration);
235572ff6f6SMatthew Dillon 		beaconintval = bs->bs_sleepduration;
236572ff6f6SMatthew Dillon 	}
237572ff6f6SMatthew Dillon 	dtimperiod = bs->bs_dtimperiod;
238572ff6f6SMatthew Dillon 	if (bs->bs_sleepduration > dtimperiod) {
239572ff6f6SMatthew Dillon 		HALASSERT(dtimperiod == 0 ||
240572ff6f6SMatthew Dillon 			roundup(bs->bs_sleepduration, dtimperiod) ==
241572ff6f6SMatthew Dillon 				bs->bs_sleepduration);
242572ff6f6SMatthew Dillon 		dtimperiod = bs->bs_sleepduration;
243572ff6f6SMatthew Dillon 	}
244572ff6f6SMatthew Dillon 	HALASSERT(beaconintval <= dtimperiod);
245572ff6f6SMatthew Dillon 	if (beaconintval == dtimperiod)
246572ff6f6SMatthew Dillon 		nextTbtt = bs->bs_nextdtim;
247572ff6f6SMatthew Dillon 	else
248572ff6f6SMatthew Dillon 		nextTbtt = bs->bs_nexttbtt;
249572ff6f6SMatthew Dillon 	nextdtim = bs->bs_nextdtim;
250572ff6f6SMatthew Dillon 
251572ff6f6SMatthew Dillon 	OS_REG_WRITE(ah, AR_SLEEP1,
252572ff6f6SMatthew Dillon 		  SM((nextdtim - SLEEP_SLOP) << 3, AR_SLEEP1_NEXT_DTIM)
253572ff6f6SMatthew Dillon 		| SM(CAB_TIMEOUT_VAL, AR_SLEEP1_CAB_TIMEOUT)
254572ff6f6SMatthew Dillon 		| AR_SLEEP1_ASSUME_DTIM
255572ff6f6SMatthew Dillon 		| AR_SLEEP1_ENH_SLEEP_ENA
256572ff6f6SMatthew Dillon 	);
257572ff6f6SMatthew Dillon 	OS_REG_WRITE(ah, AR_SLEEP2,
258572ff6f6SMatthew Dillon 		  SM((nextTbtt - SLEEP_SLOP) << 3, AR_SLEEP2_NEXT_TIM)
259572ff6f6SMatthew Dillon 		| SM(BEACON_TIMEOUT_VAL, AR_SLEEP2_BEACON_TIMEOUT)
260572ff6f6SMatthew Dillon 	);
261572ff6f6SMatthew Dillon 	OS_REG_WRITE(ah, AR_SLEEP3,
262572ff6f6SMatthew Dillon 		  SM(beaconintval, AR_SLEEP3_TIM_PERIOD)
263572ff6f6SMatthew Dillon 		| SM(dtimperiod, AR_SLEEP3_DTIM_PERIOD)
264572ff6f6SMatthew Dillon 	);
265572ff6f6SMatthew Dillon 	HALDEBUG(ah, HAL_DEBUG_BEACON, "%s: next DTIM %d\n",
266572ff6f6SMatthew Dillon 	    __func__, bs->bs_nextdtim);
267572ff6f6SMatthew Dillon 	HALDEBUG(ah, HAL_DEBUG_BEACON, "%s: next beacon %d\n",
268572ff6f6SMatthew Dillon 	    __func__, nextTbtt);
269572ff6f6SMatthew Dillon 	HALDEBUG(ah, HAL_DEBUG_BEACON, "%s: beacon period %d\n",
270572ff6f6SMatthew Dillon 	    __func__, beaconintval);
271572ff6f6SMatthew Dillon 	HALDEBUG(ah, HAL_DEBUG_BEACON, "%s: DTIM period %d\n",
272572ff6f6SMatthew Dillon 	    __func__, dtimperiod);
273572ff6f6SMatthew Dillon #undef CAB_TIMEOUT_VAL
274572ff6f6SMatthew Dillon #undef BEACON_TIMEOUT_VAL
275572ff6f6SMatthew Dillon #undef SLEEP_SLOP
276572ff6f6SMatthew Dillon }
277