1572ff6f6SMatthew Dillon /*
2572ff6f6SMatthew Dillon  * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
3572ff6f6SMatthew Dillon  * Copyright (c) 2002-2006 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 "ar5211/ar5211.h"
25572ff6f6SMatthew Dillon #include "ar5211/ar5211reg.h"
26572ff6f6SMatthew Dillon #include "ar5211/ar5211desc.h"
27572ff6f6SMatthew Dillon 
28572ff6f6SMatthew Dillon /*
29572ff6f6SMatthew Dillon  * Routines used to initialize and generated beacons for the AR5211/AR5311.
30572ff6f6SMatthew Dillon  */
31572ff6f6SMatthew Dillon 
32572ff6f6SMatthew Dillon /*
33572ff6f6SMatthew Dillon  * Return the hardware NextTBTT in TSF
34572ff6f6SMatthew Dillon  */
35572ff6f6SMatthew Dillon uint64_t
ar5211GetNextTBTT(struct ath_hal * ah)36572ff6f6SMatthew Dillon ar5211GetNextTBTT(struct ath_hal *ah)
37572ff6f6SMatthew Dillon {
38572ff6f6SMatthew Dillon #define TU_TO_TSF(_tu)	(((uint64_t)(_tu)) << 10)
39572ff6f6SMatthew Dillon 	return TU_TO_TSF(OS_REG_READ(ah, AR_TIMER0));
40572ff6f6SMatthew Dillon #undef TU_TO_TSF
41572ff6f6SMatthew Dillon }
42572ff6f6SMatthew Dillon 
43572ff6f6SMatthew Dillon /*
44572ff6f6SMatthew Dillon  * Initialize all of the hardware registers used to send beacons.
45572ff6f6SMatthew Dillon  */
46572ff6f6SMatthew Dillon void
ar5211SetBeaconTimers(struct ath_hal * ah,const HAL_BEACON_TIMERS * bt)47572ff6f6SMatthew Dillon ar5211SetBeaconTimers(struct ath_hal *ah, const HAL_BEACON_TIMERS *bt)
48572ff6f6SMatthew Dillon {
49572ff6f6SMatthew Dillon 
50572ff6f6SMatthew Dillon 	OS_REG_WRITE(ah, AR_TIMER0, bt->bt_nexttbtt);
51572ff6f6SMatthew Dillon 	OS_REG_WRITE(ah, AR_TIMER1, bt->bt_nextdba);
52572ff6f6SMatthew Dillon 	OS_REG_WRITE(ah, AR_TIMER2, bt->bt_nextswba);
53572ff6f6SMatthew Dillon 	OS_REG_WRITE(ah, AR_TIMER3, bt->bt_nextatim);
54572ff6f6SMatthew Dillon 	/*
55572ff6f6SMatthew Dillon 	 * Set the Beacon register after setting all timers.
56572ff6f6SMatthew Dillon 	 */
57572ff6f6SMatthew Dillon 	OS_REG_WRITE(ah, AR_BEACON, bt->bt_intval);
58572ff6f6SMatthew Dillon }
59572ff6f6SMatthew Dillon 
60572ff6f6SMatthew Dillon /*
61572ff6f6SMatthew Dillon  * Legacy api to initialize all of the beacon registers.
62572ff6f6SMatthew Dillon  */
63572ff6f6SMatthew Dillon void
ar5211BeaconInit(struct ath_hal * ah,uint32_t next_beacon,uint32_t beacon_period)64572ff6f6SMatthew Dillon ar5211BeaconInit(struct ath_hal *ah,
65572ff6f6SMatthew Dillon 	uint32_t next_beacon, uint32_t beacon_period)
66572ff6f6SMatthew Dillon {
67572ff6f6SMatthew Dillon 	HAL_BEACON_TIMERS bt;
68572ff6f6SMatthew Dillon 
69*57e09377SMatthew Dillon 	bzero(&bt, sizeof(bt));
70572ff6f6SMatthew Dillon 	bt.bt_nexttbtt = next_beacon;
71572ff6f6SMatthew Dillon 	/*
72572ff6f6SMatthew Dillon 	 * TIMER1: in AP/adhoc mode this controls the DMA beacon
73572ff6f6SMatthew Dillon 	 * alert timer; otherwise it controls the next wakeup time.
74572ff6f6SMatthew Dillon 	 * TIMER2: in AP mode, it controls the SBA beacon alert
75572ff6f6SMatthew Dillon 	 * interrupt; otherwise it sets the start of the next CFP.
76572ff6f6SMatthew Dillon 	 */
77572ff6f6SMatthew Dillon 	switch (AH_PRIVATE(ah)->ah_opmode) {
78572ff6f6SMatthew Dillon 	case HAL_M_STA:
79572ff6f6SMatthew Dillon 	case HAL_M_MONITOR:
80572ff6f6SMatthew Dillon 		bt.bt_nextdba = 0xffff;
81572ff6f6SMatthew Dillon 		bt.bt_nextswba = 0x7ffff;
82572ff6f6SMatthew Dillon 		break;
83572ff6f6SMatthew Dillon 	case HAL_M_IBSS:
84572ff6f6SMatthew Dillon 	case HAL_M_HOSTAP:
85572ff6f6SMatthew Dillon 		bt.bt_nextdba = (next_beacon -
86572ff6f6SMatthew Dillon 			ah->ah_config.ah_dma_beacon_response_time) << 3;	/* 1/8 TU */
87572ff6f6SMatthew Dillon 		bt.bt_nextswba = (next_beacon -
88572ff6f6SMatthew Dillon             ah->ah_config.ah_sw_beacon_response_time) << 3;	/* 1/8 TU */
89572ff6f6SMatthew Dillon 		break;
90572ff6f6SMatthew Dillon 	}
91572ff6f6SMatthew Dillon 	/*
92572ff6f6SMatthew Dillon 	 * Set the ATIM window
93572ff6f6SMatthew Dillon 	 * Our hardware does not support an ATIM window of 0
94572ff6f6SMatthew Dillon 	 * (beacons will not work).  If the ATIM windows is 0,
95572ff6f6SMatthew Dillon 	 * force it to 1.
96572ff6f6SMatthew Dillon 	 */
97572ff6f6SMatthew Dillon 	bt.bt_nextatim = next_beacon + 1;
98572ff6f6SMatthew Dillon 	bt.bt_intval = beacon_period &
99572ff6f6SMatthew Dillon 		(AR_BEACON_PERIOD | AR_BEACON_RESET_TSF | AR_BEACON_EN);
100572ff6f6SMatthew Dillon 	ar5211SetBeaconTimers(ah, &bt);
101572ff6f6SMatthew Dillon }
102572ff6f6SMatthew Dillon 
103572ff6f6SMatthew Dillon void
ar5211ResetStaBeaconTimers(struct ath_hal * ah)104572ff6f6SMatthew Dillon ar5211ResetStaBeaconTimers(struct ath_hal *ah)
105572ff6f6SMatthew Dillon {
106572ff6f6SMatthew Dillon 	uint32_t val;
107572ff6f6SMatthew Dillon 
108572ff6f6SMatthew Dillon 	OS_REG_WRITE(ah, AR_TIMER0, 0);		/* no beacons */
109572ff6f6SMatthew Dillon 	val = OS_REG_READ(ah, AR_STA_ID1);
110572ff6f6SMatthew Dillon 	val |= AR_STA_ID1_PWR_SAV;		/* XXX */
111572ff6f6SMatthew Dillon 	/* tell the h/w that the associated AP is not PCF capable */
112572ff6f6SMatthew Dillon 	OS_REG_WRITE(ah, AR_STA_ID1,
113572ff6f6SMatthew Dillon 		val & ~(AR_STA_ID1_DEFAULT_ANTENNA | AR_STA_ID1_PCF));
114572ff6f6SMatthew Dillon 	OS_REG_WRITE(ah, AR_BEACON, AR_BEACON_PERIOD);
115572ff6f6SMatthew Dillon }
116572ff6f6SMatthew Dillon 
117572ff6f6SMatthew Dillon /*
118572ff6f6SMatthew Dillon  * Set all the beacon related bits on the h/w for stations
119572ff6f6SMatthew Dillon  * i.e. initializes the corresponding h/w timers;
120572ff6f6SMatthew Dillon  * also tells the h/w whether to anticipate PCF beacons
121572ff6f6SMatthew Dillon  */
122572ff6f6SMatthew Dillon void
ar5211SetStaBeaconTimers(struct ath_hal * ah,const HAL_BEACON_STATE * bs)123572ff6f6SMatthew Dillon ar5211SetStaBeaconTimers(struct ath_hal *ah, const HAL_BEACON_STATE *bs)
124572ff6f6SMatthew Dillon {
125572ff6f6SMatthew Dillon 	struct ath_hal_5211 *ahp = AH5211(ah);
126572ff6f6SMatthew Dillon 
127572ff6f6SMatthew Dillon 	HALDEBUG(ah, HAL_DEBUG_BEACON, "%s: setting beacon timers\n", __func__);
128572ff6f6SMatthew Dillon 
129572ff6f6SMatthew Dillon 	HALASSERT(bs->bs_intval != 0);
130572ff6f6SMatthew Dillon 	/* if the AP will do PCF */
131572ff6f6SMatthew Dillon 	if (bs->bs_cfpmaxduration != 0) {
132572ff6f6SMatthew Dillon 		/* tell the h/w that the associated AP is PCF capable */
133572ff6f6SMatthew Dillon 		OS_REG_WRITE(ah, AR_STA_ID1,
134572ff6f6SMatthew Dillon 			OS_REG_READ(ah, AR_STA_ID1) | AR_STA_ID1_PCF);
135572ff6f6SMatthew Dillon 
136572ff6f6SMatthew Dillon 		/* set CFP_PERIOD(1.024ms) register */
137572ff6f6SMatthew Dillon 		OS_REG_WRITE(ah, AR_CFP_PERIOD, bs->bs_cfpperiod);
138572ff6f6SMatthew Dillon 
139572ff6f6SMatthew Dillon 		/* set CFP_DUR(1.024ms) register to max cfp duration */
140572ff6f6SMatthew Dillon 		OS_REG_WRITE(ah, AR_CFP_DUR, bs->bs_cfpmaxduration);
141572ff6f6SMatthew Dillon 
142572ff6f6SMatthew Dillon 		/* set TIMER2(128us) to anticipated time of next CFP */
143572ff6f6SMatthew Dillon 		OS_REG_WRITE(ah, AR_TIMER2, bs->bs_cfpnext << 3);
144572ff6f6SMatthew Dillon 	} else {
145572ff6f6SMatthew Dillon 		/* tell the h/w that the associated AP is not PCF capable */
146572ff6f6SMatthew Dillon 		OS_REG_WRITE(ah, AR_STA_ID1,
147572ff6f6SMatthew Dillon 			OS_REG_READ(ah, AR_STA_ID1) &~ AR_STA_ID1_PCF);
148572ff6f6SMatthew Dillon 	}
149572ff6f6SMatthew Dillon 
150572ff6f6SMatthew Dillon 	/*
151572ff6f6SMatthew Dillon 	 * Set TIMER0(1.024ms) to the anticipated time of the next beacon.
152572ff6f6SMatthew Dillon 	 */
153572ff6f6SMatthew Dillon 	OS_REG_WRITE(ah, AR_TIMER0, bs->bs_nexttbtt);
154572ff6f6SMatthew Dillon 
155572ff6f6SMatthew Dillon 	/*
156572ff6f6SMatthew Dillon 	 * Start the beacon timers by setting the BEACON register
157572ff6f6SMatthew Dillon 	 * to the beacon interval; also write the tim offset which
158572ff6f6SMatthew Dillon 	 * we should know by now.  The code, in ar5211WriteAssocid,
159572ff6f6SMatthew Dillon 	 * also sets the tim offset once the AID is known which can
160572ff6f6SMatthew Dillon 	 * be left as such for now.
161572ff6f6SMatthew Dillon 	 */
162572ff6f6SMatthew Dillon 	OS_REG_WRITE(ah, AR_BEACON,
163572ff6f6SMatthew Dillon 		(OS_REG_READ(ah, AR_BEACON) &~ (AR_BEACON_PERIOD|AR_BEACON_TIM))
164572ff6f6SMatthew Dillon 		| SM(bs->bs_intval, AR_BEACON_PERIOD)
165572ff6f6SMatthew Dillon 		| SM(bs->bs_timoffset ? bs->bs_timoffset + 4 : 0, AR_BEACON_TIM)
166572ff6f6SMatthew Dillon 	);
167572ff6f6SMatthew Dillon 
168572ff6f6SMatthew Dillon 	/*
169572ff6f6SMatthew Dillon 	 * Configure the BMISS interrupt.  Note that we
170572ff6f6SMatthew Dillon 	 * assume the caller blocks interrupts while enabling
171572ff6f6SMatthew Dillon 	 * the threshold.
172572ff6f6SMatthew Dillon 	 */
173572ff6f6SMatthew Dillon 	HALASSERT(bs->bs_bmissthreshold <= MS(0xffffffff, AR_RSSI_THR_BM_THR));
174572ff6f6SMatthew Dillon 	ahp->ah_rssiThr = (ahp->ah_rssiThr &~ AR_RSSI_THR_BM_THR)
175572ff6f6SMatthew Dillon 			| SM(bs->bs_bmissthreshold, AR_RSSI_THR_BM_THR);
176572ff6f6SMatthew Dillon 	OS_REG_WRITE(ah, AR_RSSI_THR, ahp->ah_rssiThr);
177572ff6f6SMatthew Dillon 
178572ff6f6SMatthew Dillon 	/*
179572ff6f6SMatthew Dillon 	 * Set the sleep duration in 1/8 TU's.
180572ff6f6SMatthew Dillon 	 */
181572ff6f6SMatthew Dillon #define	SLEEP_SLOP	3
182572ff6f6SMatthew Dillon 	OS_REG_RMW_FIELD(ah, AR_SCR, AR_SCR_SLDUR,
183572ff6f6SMatthew Dillon 		(bs->bs_sleepduration - SLEEP_SLOP) << 3);
184572ff6f6SMatthew Dillon #undef SLEEP_SLOP
185572ff6f6SMatthew Dillon }
186