xref: /dragonfly/sys/dev/netif/ath/ath_hal/ar5212/ar2425.c (revision 57e09377)
1572ff6f6SMatthew Dillon /*
2572ff6f6SMatthew Dillon  * Copyright (c) 2002-2009 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/ar5212phy.h"
27572ff6f6SMatthew Dillon 
28572ff6f6SMatthew Dillon #include "ah_eeprom_v3.h"
29572ff6f6SMatthew Dillon 
30572ff6f6SMatthew Dillon #define AH_5212_2425
31572ff6f6SMatthew Dillon #define AH_5212_2417
32572ff6f6SMatthew Dillon #include "ar5212/ar5212.ini"
33572ff6f6SMatthew Dillon 
34572ff6f6SMatthew Dillon #define	N(a)	(sizeof(a)/sizeof(a[0]))
35572ff6f6SMatthew Dillon 
36572ff6f6SMatthew Dillon struct ar2425State {
37572ff6f6SMatthew Dillon 	RF_HAL_FUNCS	base;		/* public state, must be first */
38572ff6f6SMatthew Dillon 	uint16_t	pcdacTable[PWR_TABLE_SIZE_2413];
39572ff6f6SMatthew Dillon 
40572ff6f6SMatthew Dillon 	uint32_t	Bank1Data[N(ar5212Bank1_2425)];
41572ff6f6SMatthew Dillon 	uint32_t	Bank2Data[N(ar5212Bank2_2425)];
42572ff6f6SMatthew Dillon 	uint32_t	Bank3Data[N(ar5212Bank3_2425)];
43572ff6f6SMatthew Dillon 	uint32_t	Bank6Data[N(ar5212Bank6_2425)];	/* 2417 is same size */
44572ff6f6SMatthew Dillon 	uint32_t	Bank7Data[N(ar5212Bank7_2425)];
45572ff6f6SMatthew Dillon };
46572ff6f6SMatthew Dillon #define	AR2425(ah)	((struct ar2425State *) AH5212(ah)->ah_rfHal)
47572ff6f6SMatthew Dillon 
48572ff6f6SMatthew Dillon extern	void ar5212ModifyRfBuffer(uint32_t *rfBuf, uint32_t reg32,
49572ff6f6SMatthew Dillon 		uint32_t numBits, uint32_t firstBit, uint32_t column);
50572ff6f6SMatthew Dillon 
51572ff6f6SMatthew Dillon static void
ar2425WriteRegs(struct ath_hal * ah,u_int modesIndex,u_int freqIndex,int writes)52572ff6f6SMatthew Dillon ar2425WriteRegs(struct ath_hal *ah, u_int modesIndex, u_int freqIndex,
53572ff6f6SMatthew Dillon 	int writes)
54572ff6f6SMatthew Dillon {
55572ff6f6SMatthew Dillon 	HAL_INI_WRITE_ARRAY(ah, ar5212Modes_2425, modesIndex, writes);
56572ff6f6SMatthew Dillon 	HAL_INI_WRITE_ARRAY(ah, ar5212Common_2425, 1, writes);
57572ff6f6SMatthew Dillon 	HAL_INI_WRITE_ARRAY(ah, ar5212BB_RfGain_2425, freqIndex, writes);
58572ff6f6SMatthew Dillon #if 0
59572ff6f6SMatthew Dillon 	/*
60572ff6f6SMatthew Dillon 	 * for SWAN similar to Condor
61572ff6f6SMatthew Dillon 	 * Bit 0 enables link to go to L1 when MAC goes to sleep.
62572ff6f6SMatthew Dillon 	 * Bit 3 enables the loop back the link down to reset.
63572ff6f6SMatthew Dillon 	 */
64572ff6f6SMatthew Dillon 	if (AH_PRIVATE(ah)->ah_ispcie && && ath_hal_pcieL1SKPEnable) {
65572ff6f6SMatthew Dillon 		OS_REG_WRITE(ah, AR_PCIE_PMC,
66572ff6f6SMatthew Dillon 		    AR_PCIE_PMC_ENA_L1 | AR_PCIE_PMC_ENA_RESET);
67572ff6f6SMatthew Dillon 	}
68572ff6f6SMatthew Dillon 	/*
69572ff6f6SMatthew Dillon 	 * for Standby issue in Swan/Condor.
70572ff6f6SMatthew Dillon 	 * Bit 9 (MAC_WOW_PWR_STATE_MASK_D2)to be set to avoid skips
71572ff6f6SMatthew Dillon 	 *	before last Training Sequence 2 (TS2)
72572ff6f6SMatthew Dillon 	 * Bit 8 (MAC_WOW_PWR_STATE_MASK_D1)to be unset to assert
73572ff6f6SMatthew Dillon 	 *	Power Reset along with PCI Reset
74572ff6f6SMatthew Dillon 	 */
75572ff6f6SMatthew Dillon 	OS_REG_SET_BIT(ah, AR_PCIE_PMC, MAC_WOW_PWR_STATE_MASK_D2);
76572ff6f6SMatthew Dillon #endif
77572ff6f6SMatthew Dillon }
78572ff6f6SMatthew Dillon 
79572ff6f6SMatthew Dillon /*
80572ff6f6SMatthew Dillon  * Take the MHz channel value and set the Channel value
81572ff6f6SMatthew Dillon  *
82572ff6f6SMatthew Dillon  * ASSUMES: Writes enabled to analog bus
83572ff6f6SMatthew Dillon  */
84572ff6f6SMatthew Dillon static HAL_BOOL
ar2425SetChannel(struct ath_hal * ah,const struct ieee80211_channel * chan)85572ff6f6SMatthew Dillon ar2425SetChannel(struct ath_hal *ah, const struct ieee80211_channel *chan)
86572ff6f6SMatthew Dillon {
87572ff6f6SMatthew Dillon 	uint16_t freq = ath_hal_gethwchannel(ah, chan);
88572ff6f6SMatthew Dillon 	uint32_t channelSel  = 0;
89572ff6f6SMatthew Dillon 	uint32_t bModeSynth  = 0;
90572ff6f6SMatthew Dillon 	uint32_t aModeRefSel = 0;
91572ff6f6SMatthew Dillon 	uint32_t reg32       = 0;
92572ff6f6SMatthew Dillon 
93572ff6f6SMatthew Dillon 	OS_MARK(ah, AH_MARK_SETCHANNEL, freq);
94572ff6f6SMatthew Dillon 
95572ff6f6SMatthew Dillon 	if (freq < 4800) {
96572ff6f6SMatthew Dillon 		uint32_t txctl;
97572ff6f6SMatthew Dillon 
98572ff6f6SMatthew Dillon         channelSel = freq - 2272;
99572ff6f6SMatthew Dillon         channelSel = ath_hal_reverseBits(channelSel, 8);
100572ff6f6SMatthew Dillon 
101572ff6f6SMatthew Dillon 		txctl = OS_REG_READ(ah, AR_PHY_CCK_TX_CTRL);
102572ff6f6SMatthew Dillon         if (freq == 2484) {
103572ff6f6SMatthew Dillon 			// Enable channel spreading for channel 14
104572ff6f6SMatthew Dillon 			OS_REG_WRITE(ah, AR_PHY_CCK_TX_CTRL,
105572ff6f6SMatthew Dillon 				txctl | AR_PHY_CCK_TX_CTRL_JAPAN);
106572ff6f6SMatthew Dillon 		} else {
107572ff6f6SMatthew Dillon 			OS_REG_WRITE(ah, AR_PHY_CCK_TX_CTRL,
108572ff6f6SMatthew Dillon 				txctl &~ AR_PHY_CCK_TX_CTRL_JAPAN);
109572ff6f6SMatthew Dillon 		}
110572ff6f6SMatthew Dillon 
111572ff6f6SMatthew Dillon 	} else if (((freq % 5) == 2) && (freq <= 5435)) {
112572ff6f6SMatthew Dillon 		freq = freq - 2; /* Align to even 5MHz raster */
113572ff6f6SMatthew Dillon 		channelSel = ath_hal_reverseBits(
114572ff6f6SMatthew Dillon 			(uint32_t)(((freq - 4800)*10)/25 + 1), 8);
115572ff6f6SMatthew Dillon             	aModeRefSel = ath_hal_reverseBits(0, 2);
116572ff6f6SMatthew Dillon 	} else if ((freq % 20) == 0 && freq >= 5120) {
117572ff6f6SMatthew Dillon 		channelSel = ath_hal_reverseBits(
118572ff6f6SMatthew Dillon 			((freq - 4800) / 20 << 2), 8);
119572ff6f6SMatthew Dillon 		aModeRefSel = ath_hal_reverseBits(1, 2);
120572ff6f6SMatthew Dillon 	} else if ((freq % 10) == 0) {
121572ff6f6SMatthew Dillon 		channelSel = ath_hal_reverseBits(
122572ff6f6SMatthew Dillon 			((freq - 4800) / 10 << 1), 8);
123572ff6f6SMatthew Dillon 		aModeRefSel = ath_hal_reverseBits(1, 2);
124572ff6f6SMatthew Dillon 	} else if ((freq % 5) == 0) {
125572ff6f6SMatthew Dillon 		channelSel = ath_hal_reverseBits(
126572ff6f6SMatthew Dillon 			(freq - 4800) / 5, 8);
127572ff6f6SMatthew Dillon 		aModeRefSel = ath_hal_reverseBits(1, 2);
128572ff6f6SMatthew Dillon 	} else {
129572ff6f6SMatthew Dillon 		HALDEBUG(ah, HAL_DEBUG_ANY, "%s: invalid channel %u MHz\n",
130572ff6f6SMatthew Dillon 		    __func__, freq);
131572ff6f6SMatthew Dillon 		return AH_FALSE;
132572ff6f6SMatthew Dillon 	}
133572ff6f6SMatthew Dillon 
134572ff6f6SMatthew Dillon 	reg32 = (channelSel << 4) | (aModeRefSel << 2) | (bModeSynth << 1) |
135572ff6f6SMatthew Dillon 			(1 << 12) | 0x1;
136572ff6f6SMatthew Dillon 	OS_REG_WRITE(ah, AR_PHY(0x27), reg32 & 0xff);
137572ff6f6SMatthew Dillon 
138572ff6f6SMatthew Dillon 	reg32 >>= 8;
139572ff6f6SMatthew Dillon 	OS_REG_WRITE(ah, AR_PHY(0x36), reg32 & 0x7f);
140572ff6f6SMatthew Dillon 
141572ff6f6SMatthew Dillon 	AH_PRIVATE(ah)->ah_curchan = chan;
142572ff6f6SMatthew Dillon 	return AH_TRUE;
143572ff6f6SMatthew Dillon }
144572ff6f6SMatthew Dillon 
145572ff6f6SMatthew Dillon /*
146572ff6f6SMatthew Dillon  * Reads EEPROM header info from device structure and programs
147572ff6f6SMatthew Dillon  * all rf registers
148572ff6f6SMatthew Dillon  *
149572ff6f6SMatthew Dillon  * REQUIRES: Access to the analog rf device
150572ff6f6SMatthew Dillon  */
151572ff6f6SMatthew Dillon static HAL_BOOL
ar2425SetRfRegs(struct ath_hal * ah,const struct ieee80211_channel * chan,uint16_t modesIndex,uint16_t * rfXpdGain)152572ff6f6SMatthew Dillon ar2425SetRfRegs(struct ath_hal *ah,
153572ff6f6SMatthew Dillon 	const struct ieee80211_channel *chan,
154572ff6f6SMatthew Dillon 	uint16_t modesIndex, uint16_t *rfXpdGain)
155572ff6f6SMatthew Dillon {
156572ff6f6SMatthew Dillon #define	RF_BANK_SETUP(_priv, _ix, _col) do {				    \
157572ff6f6SMatthew Dillon 	int i;								    \
158572ff6f6SMatthew Dillon 	for (i = 0; i < N(ar5212Bank##_ix##_2425); i++)			    \
159572ff6f6SMatthew Dillon 		(_priv)->Bank##_ix##Data[i] = ar5212Bank##_ix##_2425[i][_col];\
160572ff6f6SMatthew Dillon } while (0)
161572ff6f6SMatthew Dillon 	struct ath_hal_5212 *ahp = AH5212(ah);
162572ff6f6SMatthew Dillon 	const HAL_EEPROM *ee = AH_PRIVATE(ah)->ah_eeprom;
163572ff6f6SMatthew Dillon 	struct ar2425State *priv = AR2425(ah);
164572ff6f6SMatthew Dillon 	uint16_t ob2GHz = 0, db2GHz = 0;
165572ff6f6SMatthew Dillon 	int regWrites = 0;
166572ff6f6SMatthew Dillon 
167572ff6f6SMatthew Dillon 	HALDEBUG(ah, HAL_DEBUG_RFPARAM, "%s: chan %u/0x%x modesIndex %u\n",
168572ff6f6SMatthew Dillon 	    __func__, chan->ic_freq, chan->ic_flags, modesIndex);
169572ff6f6SMatthew Dillon 
170572ff6f6SMatthew Dillon 	HALASSERT(priv);
171572ff6f6SMatthew Dillon 
172572ff6f6SMatthew Dillon 	/* Setup rf parameters */
173572ff6f6SMatthew Dillon 	if (IEEE80211_IS_CHAN_B(chan)) {
174572ff6f6SMatthew Dillon 		ob2GHz = ee->ee_obFor24;
175572ff6f6SMatthew Dillon 		db2GHz = ee->ee_dbFor24;
176572ff6f6SMatthew Dillon 	} else {
177572ff6f6SMatthew Dillon 		ob2GHz = ee->ee_obFor24g;
178572ff6f6SMatthew Dillon 		db2GHz = ee->ee_dbFor24g;
179572ff6f6SMatthew Dillon 	}
180572ff6f6SMatthew Dillon 
181572ff6f6SMatthew Dillon 	/* Bank 1 Write */
182572ff6f6SMatthew Dillon 	RF_BANK_SETUP(priv, 1, 1);
183572ff6f6SMatthew Dillon 
184572ff6f6SMatthew Dillon 	/* Bank 2 Write */
185572ff6f6SMatthew Dillon 	RF_BANK_SETUP(priv, 2, modesIndex);
186572ff6f6SMatthew Dillon 
187572ff6f6SMatthew Dillon 	/* Bank 3 Write */
188572ff6f6SMatthew Dillon 	RF_BANK_SETUP(priv, 3, modesIndex);
189572ff6f6SMatthew Dillon 
190572ff6f6SMatthew Dillon 	/* Bank 6 Write */
191572ff6f6SMatthew Dillon 	RF_BANK_SETUP(priv, 6, modesIndex);
192572ff6f6SMatthew Dillon 
193572ff6f6SMatthew Dillon         ar5212ModifyRfBuffer(priv->Bank6Data, ob2GHz, 3, 193, 0);
194572ff6f6SMatthew Dillon         ar5212ModifyRfBuffer(priv->Bank6Data, db2GHz, 3, 190, 0);
195572ff6f6SMatthew Dillon 
196572ff6f6SMatthew Dillon 	/* Bank 7 Setup */
197572ff6f6SMatthew Dillon 	RF_BANK_SETUP(priv, 7, modesIndex);
198572ff6f6SMatthew Dillon 
199572ff6f6SMatthew Dillon 	/* Write Analog registers */
200572ff6f6SMatthew Dillon 	HAL_INI_WRITE_BANK(ah, ar5212Bank1_2425, priv->Bank1Data, regWrites);
201572ff6f6SMatthew Dillon 	HAL_INI_WRITE_BANK(ah, ar5212Bank2_2425, priv->Bank2Data, regWrites);
202572ff6f6SMatthew Dillon 	HAL_INI_WRITE_BANK(ah, ar5212Bank3_2425, priv->Bank3Data, regWrites);
203572ff6f6SMatthew Dillon 	if (IS_2417(ah)) {
204572ff6f6SMatthew Dillon 		HALASSERT(N(ar5212Bank6_2425) == N(ar5212Bank6_2417));
205572ff6f6SMatthew Dillon 		HAL_INI_WRITE_BANK(ah, ar5212Bank6_2417, priv->Bank6Data,
206572ff6f6SMatthew Dillon 		    regWrites);
207572ff6f6SMatthew Dillon 	} else
208572ff6f6SMatthew Dillon 		HAL_INI_WRITE_BANK(ah, ar5212Bank6_2425, priv->Bank6Data,
209572ff6f6SMatthew Dillon 		    regWrites);
210572ff6f6SMatthew Dillon 	HAL_INI_WRITE_BANK(ah, ar5212Bank7_2425, priv->Bank7Data, regWrites);
211572ff6f6SMatthew Dillon 
212572ff6f6SMatthew Dillon 	/* Now that we have reprogrammed rfgain value, clear the flag. */
213572ff6f6SMatthew Dillon 	ahp->ah_rfgainState = HAL_RFGAIN_INACTIVE;
214572ff6f6SMatthew Dillon 
215572ff6f6SMatthew Dillon 	HALDEBUG(ah, HAL_DEBUG_RFPARAM, "<==%s\n", __func__);
216572ff6f6SMatthew Dillon 	return AH_TRUE;
217572ff6f6SMatthew Dillon #undef	RF_BANK_SETUP
218572ff6f6SMatthew Dillon }
219572ff6f6SMatthew Dillon 
220572ff6f6SMatthew Dillon /*
221572ff6f6SMatthew Dillon  * Return a reference to the requested RF Bank.
222572ff6f6SMatthew Dillon  */
223572ff6f6SMatthew Dillon static uint32_t *
ar2425GetRfBank(struct ath_hal * ah,int bank)224572ff6f6SMatthew Dillon ar2425GetRfBank(struct ath_hal *ah, int bank)
225572ff6f6SMatthew Dillon {
226572ff6f6SMatthew Dillon 	struct ar2425State *priv = AR2425(ah);
227572ff6f6SMatthew Dillon 
228572ff6f6SMatthew Dillon 	HALASSERT(priv != AH_NULL);
229572ff6f6SMatthew Dillon 	switch (bank) {
230572ff6f6SMatthew Dillon 	case 1: return priv->Bank1Data;
231572ff6f6SMatthew Dillon 	case 2: return priv->Bank2Data;
232572ff6f6SMatthew Dillon 	case 3: return priv->Bank3Data;
233572ff6f6SMatthew Dillon 	case 6: return priv->Bank6Data;
234572ff6f6SMatthew Dillon 	case 7: return priv->Bank7Data;
235572ff6f6SMatthew Dillon 	}
236572ff6f6SMatthew Dillon 	HALDEBUG(ah, HAL_DEBUG_ANY, "%s: unknown RF Bank %d requested\n",
237572ff6f6SMatthew Dillon 	    __func__, bank);
238572ff6f6SMatthew Dillon 	return AH_NULL;
239572ff6f6SMatthew Dillon }
240572ff6f6SMatthew Dillon 
241572ff6f6SMatthew Dillon /*
242572ff6f6SMatthew Dillon  * Return indices surrounding the value in sorted integer lists.
243572ff6f6SMatthew Dillon  *
244572ff6f6SMatthew Dillon  * NB: the input list is assumed to be sorted in ascending order
245572ff6f6SMatthew Dillon  */
246572ff6f6SMatthew Dillon static void
GetLowerUpperIndex(int16_t v,const uint16_t * lp,uint16_t listSize,uint32_t * vlo,uint32_t * vhi)247572ff6f6SMatthew Dillon GetLowerUpperIndex(int16_t v, const uint16_t *lp, uint16_t listSize,
248572ff6f6SMatthew Dillon                           uint32_t *vlo, uint32_t *vhi)
249572ff6f6SMatthew Dillon {
250572ff6f6SMatthew Dillon 	int16_t target = v;
251572ff6f6SMatthew Dillon 	const uint16_t *ep = lp+listSize;
252572ff6f6SMatthew Dillon 	const uint16_t *tp;
253572ff6f6SMatthew Dillon 
254*57e09377SMatthew Dillon 	*vlo = 0;	/* avoid gcc warnings */
255*57e09377SMatthew Dillon 	*vhi = 0;	/* avoid gcc warnings */
256*57e09377SMatthew Dillon 
257572ff6f6SMatthew Dillon 	/*
258572ff6f6SMatthew Dillon 	 * Check first and last elements for out-of-bounds conditions.
259572ff6f6SMatthew Dillon 	 */
260572ff6f6SMatthew Dillon 	if (target < lp[0]) {
261572ff6f6SMatthew Dillon 		*vlo = *vhi = 0;
262572ff6f6SMatthew Dillon 		return;
263572ff6f6SMatthew Dillon 	}
264572ff6f6SMatthew Dillon 	if (target >= ep[-1]) {
265572ff6f6SMatthew Dillon 		*vlo = *vhi = listSize - 1;
266572ff6f6SMatthew Dillon 		return;
267572ff6f6SMatthew Dillon 	}
268572ff6f6SMatthew Dillon 
269572ff6f6SMatthew Dillon 	/* look for value being near or between 2 values in list */
270572ff6f6SMatthew Dillon 	for (tp = lp; tp < ep; tp++) {
271572ff6f6SMatthew Dillon 		/*
272572ff6f6SMatthew Dillon 		 * If value is close to the current value of the list
273572ff6f6SMatthew Dillon 		 * then target is not between values, it is one of the values
274572ff6f6SMatthew Dillon 		 */
275572ff6f6SMatthew Dillon 		if (*tp == target) {
276572ff6f6SMatthew Dillon 			*vlo = *vhi = tp - (const uint16_t *) lp;
277572ff6f6SMatthew Dillon 			return;
278572ff6f6SMatthew Dillon 		}
279572ff6f6SMatthew Dillon 		/*
280572ff6f6SMatthew Dillon 		 * Look for value being between current value and next value
281572ff6f6SMatthew Dillon 		 * if so return these 2 values
282572ff6f6SMatthew Dillon 		 */
283572ff6f6SMatthew Dillon 		if (target < tp[1]) {
284572ff6f6SMatthew Dillon 			*vlo = tp - (const uint16_t *) lp;
285572ff6f6SMatthew Dillon 			*vhi = *vlo + 1;
286572ff6f6SMatthew Dillon 			return;
287572ff6f6SMatthew Dillon 		}
288572ff6f6SMatthew Dillon 	}
289572ff6f6SMatthew Dillon }
290572ff6f6SMatthew Dillon 
291572ff6f6SMatthew Dillon /*
292572ff6f6SMatthew Dillon  * Fill the Vpdlist for indices Pmax-Pmin
293572ff6f6SMatthew Dillon  */
294572ff6f6SMatthew Dillon static HAL_BOOL
ar2425FillVpdTable(uint32_t pdGainIdx,int16_t Pmin,int16_t Pmax,const int16_t * pwrList,const uint16_t * VpdList,uint16_t numIntercepts,uint16_t retVpdList[][64])295572ff6f6SMatthew Dillon ar2425FillVpdTable(uint32_t pdGainIdx, int16_t Pmin, int16_t  Pmax,
296572ff6f6SMatthew Dillon 		   const int16_t *pwrList, const uint16_t *VpdList,
297572ff6f6SMatthew Dillon 		   uint16_t numIntercepts,
298572ff6f6SMatthew Dillon 		   uint16_t retVpdList[][64])
299572ff6f6SMatthew Dillon {
300572ff6f6SMatthew Dillon 	uint16_t ii, jj, kk;
301572ff6f6SMatthew Dillon 	int16_t currPwr = (int16_t)(2*Pmin);
302572ff6f6SMatthew Dillon 	/* since Pmin is pwr*2 and pwrList is 4*pwr */
303572ff6f6SMatthew Dillon 	uint32_t  idxL, idxR;
304572ff6f6SMatthew Dillon 
305572ff6f6SMatthew Dillon 	ii = 0;
306572ff6f6SMatthew Dillon 	jj = 0;
307572ff6f6SMatthew Dillon 
308572ff6f6SMatthew Dillon 	if (numIntercepts < 2)
309572ff6f6SMatthew Dillon 		return AH_FALSE;
310572ff6f6SMatthew Dillon 
311572ff6f6SMatthew Dillon 	while (ii <= (uint16_t)(Pmax - Pmin)) {
312572ff6f6SMatthew Dillon 		GetLowerUpperIndex(currPwr, (const uint16_t *) pwrList,
313572ff6f6SMatthew Dillon 				   numIntercepts, &(idxL), &(idxR));
314572ff6f6SMatthew Dillon 		if (idxR < 1)
315572ff6f6SMatthew Dillon 			idxR = 1;			/* extrapolate below */
316572ff6f6SMatthew Dillon 		if (idxL == (uint32_t)(numIntercepts - 1))
317572ff6f6SMatthew Dillon 			idxL = numIntercepts - 2;	/* extrapolate above */
318572ff6f6SMatthew Dillon 		if (pwrList[idxL] == pwrList[idxR])
319572ff6f6SMatthew Dillon 			kk = VpdList[idxL];
320572ff6f6SMatthew Dillon 		else
321572ff6f6SMatthew Dillon 			kk = (uint16_t)
322572ff6f6SMatthew Dillon 				(((currPwr - pwrList[idxL])*VpdList[idxR]+
323572ff6f6SMatthew Dillon 				  (pwrList[idxR] - currPwr)*VpdList[idxL])/
324572ff6f6SMatthew Dillon 				 (pwrList[idxR] - pwrList[idxL]));
325572ff6f6SMatthew Dillon 		retVpdList[pdGainIdx][ii] = kk;
326572ff6f6SMatthew Dillon 		ii++;
327572ff6f6SMatthew Dillon 		currPwr += 2;				/* half dB steps */
328572ff6f6SMatthew Dillon 	}
329572ff6f6SMatthew Dillon 
330572ff6f6SMatthew Dillon 	return AH_TRUE;
331572ff6f6SMatthew Dillon }
332572ff6f6SMatthew Dillon 
333572ff6f6SMatthew Dillon /*
334572ff6f6SMatthew Dillon  * Returns interpolated or the scaled up interpolated value
335572ff6f6SMatthew Dillon  */
336572ff6f6SMatthew Dillon static int16_t
interpolate_signed(uint16_t target,uint16_t srcLeft,uint16_t srcRight,int16_t targetLeft,int16_t targetRight)337572ff6f6SMatthew Dillon interpolate_signed(uint16_t target, uint16_t srcLeft, uint16_t srcRight,
338572ff6f6SMatthew Dillon 	int16_t targetLeft, int16_t targetRight)
339572ff6f6SMatthew Dillon {
340572ff6f6SMatthew Dillon 	int16_t rv;
341572ff6f6SMatthew Dillon 
342572ff6f6SMatthew Dillon 	if (srcRight != srcLeft) {
343572ff6f6SMatthew Dillon 		rv = ((target - srcLeft)*targetRight +
344572ff6f6SMatthew Dillon 		      (srcRight - target)*targetLeft) / (srcRight - srcLeft);
345572ff6f6SMatthew Dillon 	} else {
346572ff6f6SMatthew Dillon 		rv = targetLeft;
347572ff6f6SMatthew Dillon 	}
348572ff6f6SMatthew Dillon 	return rv;
349572ff6f6SMatthew Dillon }
350572ff6f6SMatthew Dillon 
351572ff6f6SMatthew Dillon /*
352572ff6f6SMatthew Dillon  * Uses the data points read from EEPROM to reconstruct the pdadc power table
353572ff6f6SMatthew Dillon  * Called by ar2425SetPowerTable()
354572ff6f6SMatthew Dillon  */
355572ff6f6SMatthew Dillon static void
ar2425getGainBoundariesAndPdadcsForPowers(struct ath_hal * ah,uint16_t channel,const RAW_DATA_STRUCT_2413 * pRawDataset,uint16_t pdGainOverlap_t2,int16_t * pMinCalPower,uint16_t pPdGainBoundaries[],uint16_t pPdGainValues[],uint16_t pPDADCValues[])356572ff6f6SMatthew Dillon ar2425getGainBoundariesAndPdadcsForPowers(struct ath_hal *ah, uint16_t channel,
357572ff6f6SMatthew Dillon 		const RAW_DATA_STRUCT_2413 *pRawDataset,
358572ff6f6SMatthew Dillon 		uint16_t pdGainOverlap_t2,
359572ff6f6SMatthew Dillon 		int16_t  *pMinCalPower, uint16_t pPdGainBoundaries[],
360572ff6f6SMatthew Dillon 		uint16_t pPdGainValues[], uint16_t pPDADCValues[])
361572ff6f6SMatthew Dillon {
362572ff6f6SMatthew Dillon     /* Note the items statically allocated below are to reduce stack usage */
363572ff6f6SMatthew Dillon 	uint32_t ii, jj, kk;
364572ff6f6SMatthew Dillon 	int32_t ss;/* potentially -ve index for taking care of pdGainOverlap */
365572ff6f6SMatthew Dillon 	uint32_t idxL, idxR;
366572ff6f6SMatthew Dillon 	uint32_t numPdGainsUsed = 0;
367572ff6f6SMatthew Dillon         static uint16_t VpdTable_L[MAX_NUM_PDGAINS_PER_CHANNEL][MAX_PWR_RANGE_IN_HALF_DB];
368572ff6f6SMatthew Dillon 	/* filled out Vpd table for all pdGains (chanL) */
369572ff6f6SMatthew Dillon         static uint16_t VpdTable_R[MAX_NUM_PDGAINS_PER_CHANNEL][MAX_PWR_RANGE_IN_HALF_DB];
370572ff6f6SMatthew Dillon 	/* filled out Vpd table for all pdGains (chanR) */
371572ff6f6SMatthew Dillon         static uint16_t VpdTable_I[MAX_NUM_PDGAINS_PER_CHANNEL][MAX_PWR_RANGE_IN_HALF_DB];
372572ff6f6SMatthew Dillon 	/* filled out Vpd table for all pdGains (interpolated) */
373572ff6f6SMatthew Dillon 	/*
374572ff6f6SMatthew Dillon 	 * If desired to support -ve power levels in future, just
375572ff6f6SMatthew Dillon 	 * change pwr_I_0 to signed 5-bits.
376572ff6f6SMatthew Dillon 	 */
377572ff6f6SMatthew Dillon         static int16_t Pmin_t2[MAX_NUM_PDGAINS_PER_CHANNEL];
378b14ca477SMatthew Dillon 	/* to accommodate -ve power levels later on. */
379572ff6f6SMatthew Dillon         static int16_t Pmax_t2[MAX_NUM_PDGAINS_PER_CHANNEL];
380b14ca477SMatthew Dillon 	/* to accommodate -ve power levels later on */
381572ff6f6SMatthew Dillon 	uint16_t numVpd = 0;
382572ff6f6SMatthew Dillon 	uint16_t Vpd_step;
383572ff6f6SMatthew Dillon 	int16_t tmpVal ;
384572ff6f6SMatthew Dillon 	uint32_t sizeCurrVpdTable, maxIndex, tgtIndex;
385572ff6f6SMatthew Dillon 
386572ff6f6SMatthew Dillon 	HALDEBUG(ah, HAL_DEBUG_RFPARAM, "==>%s:\n", __func__);
387572ff6f6SMatthew Dillon 
388572ff6f6SMatthew Dillon 	/* Get upper lower index */
389572ff6f6SMatthew Dillon 	GetLowerUpperIndex(channel, pRawDataset->pChannels,
390572ff6f6SMatthew Dillon 				 pRawDataset->numChannels, &(idxL), &(idxR));
391572ff6f6SMatthew Dillon 
392572ff6f6SMatthew Dillon 	for (ii = 0; ii < MAX_NUM_PDGAINS_PER_CHANNEL; ii++) {
393572ff6f6SMatthew Dillon 		jj = MAX_NUM_PDGAINS_PER_CHANNEL - ii - 1;
394572ff6f6SMatthew Dillon 		/* work backwards 'cause highest pdGain for lowest power */
395572ff6f6SMatthew Dillon 		numVpd = pRawDataset->pDataPerChannel[idxL].pDataPerPDGain[jj].numVpd;
396572ff6f6SMatthew Dillon 		if (numVpd > 0) {
397572ff6f6SMatthew Dillon 			pPdGainValues[numPdGainsUsed] = pRawDataset->pDataPerChannel[idxL].pDataPerPDGain[jj].pd_gain;
398572ff6f6SMatthew Dillon 			Pmin_t2[numPdGainsUsed] = pRawDataset->pDataPerChannel[idxL].pDataPerPDGain[jj].pwr_t4[0];
399572ff6f6SMatthew Dillon 			if (Pmin_t2[numPdGainsUsed] >pRawDataset->pDataPerChannel[idxR].pDataPerPDGain[jj].pwr_t4[0]) {
400572ff6f6SMatthew Dillon 				Pmin_t2[numPdGainsUsed] = pRawDataset->pDataPerChannel[idxR].pDataPerPDGain[jj].pwr_t4[0];
401572ff6f6SMatthew Dillon 			}
402572ff6f6SMatthew Dillon 			Pmin_t2[numPdGainsUsed] = (int16_t)
403572ff6f6SMatthew Dillon 				(Pmin_t2[numPdGainsUsed] / 2);
404572ff6f6SMatthew Dillon 			Pmax_t2[numPdGainsUsed] = pRawDataset->pDataPerChannel[idxL].pDataPerPDGain[jj].pwr_t4[numVpd-1];
405572ff6f6SMatthew Dillon 			if (Pmax_t2[numPdGainsUsed] > pRawDataset->pDataPerChannel[idxR].pDataPerPDGain[jj].pwr_t4[numVpd-1])
406572ff6f6SMatthew Dillon 				Pmax_t2[numPdGainsUsed] =
407572ff6f6SMatthew Dillon 					pRawDataset->pDataPerChannel[idxR].pDataPerPDGain[jj].pwr_t4[numVpd-1];
408572ff6f6SMatthew Dillon 			Pmax_t2[numPdGainsUsed] = (int16_t)(Pmax_t2[numPdGainsUsed] / 2);
409572ff6f6SMatthew Dillon 			ar2425FillVpdTable(
410572ff6f6SMatthew Dillon 					   numPdGainsUsed, Pmin_t2[numPdGainsUsed], Pmax_t2[numPdGainsUsed],
411572ff6f6SMatthew Dillon 					   &(pRawDataset->pDataPerChannel[idxL].pDataPerPDGain[jj].pwr_t4[0]),
412572ff6f6SMatthew Dillon 					   &(pRawDataset->pDataPerChannel[idxL].pDataPerPDGain[jj].Vpd[0]), numVpd, VpdTable_L
413572ff6f6SMatthew Dillon 					   );
414572ff6f6SMatthew Dillon 			ar2425FillVpdTable(
415572ff6f6SMatthew Dillon 					   numPdGainsUsed, Pmin_t2[numPdGainsUsed], Pmax_t2[numPdGainsUsed],
416572ff6f6SMatthew Dillon 					   &(pRawDataset->pDataPerChannel[idxR].pDataPerPDGain[jj].pwr_t4[0]),
417572ff6f6SMatthew Dillon 					   &(pRawDataset->pDataPerChannel[idxR].pDataPerPDGain[jj].Vpd[0]), numVpd, VpdTable_R
418572ff6f6SMatthew Dillon 					   );
419572ff6f6SMatthew Dillon 			for (kk = 0; kk < (uint16_t)(Pmax_t2[numPdGainsUsed] - Pmin_t2[numPdGainsUsed]); kk++) {
420572ff6f6SMatthew Dillon 				VpdTable_I[numPdGainsUsed][kk] =
421572ff6f6SMatthew Dillon 					interpolate_signed(
422572ff6f6SMatthew Dillon 							   channel, pRawDataset->pChannels[idxL], pRawDataset->pChannels[idxR],
423572ff6f6SMatthew Dillon 							   (int16_t)VpdTable_L[numPdGainsUsed][kk], (int16_t)VpdTable_R[numPdGainsUsed][kk]);
424572ff6f6SMatthew Dillon 			}
425572ff6f6SMatthew Dillon 			/* fill VpdTable_I for this pdGain */
426572ff6f6SMatthew Dillon 			numPdGainsUsed++;
427572ff6f6SMatthew Dillon 		}
428572ff6f6SMatthew Dillon 		/* if this pdGain is used */
429572ff6f6SMatthew Dillon 	}
430572ff6f6SMatthew Dillon 
431572ff6f6SMatthew Dillon 	*pMinCalPower = Pmin_t2[0];
432572ff6f6SMatthew Dillon 	kk = 0; /* index for the final table */
433572ff6f6SMatthew Dillon 	for (ii = 0; ii < numPdGainsUsed; ii++) {
434572ff6f6SMatthew Dillon 		if (ii == (numPdGainsUsed - 1))
435572ff6f6SMatthew Dillon 			pPdGainBoundaries[ii] = Pmax_t2[ii] +
436572ff6f6SMatthew Dillon 				PD_GAIN_BOUNDARY_STRETCH_IN_HALF_DB;
437572ff6f6SMatthew Dillon 		else
438572ff6f6SMatthew Dillon 			pPdGainBoundaries[ii] = (uint16_t)
439572ff6f6SMatthew Dillon 				((Pmax_t2[ii] + Pmin_t2[ii+1]) / 2 );
440572ff6f6SMatthew Dillon 
441572ff6f6SMatthew Dillon 		/* Find starting index for this pdGain */
442572ff6f6SMatthew Dillon 		if (ii == 0)
443572ff6f6SMatthew Dillon 			ss = 0; /* for the first pdGain, start from index 0 */
444572ff6f6SMatthew Dillon 		else
445572ff6f6SMatthew Dillon 			ss = (pPdGainBoundaries[ii-1] - Pmin_t2[ii]) -
446572ff6f6SMatthew Dillon 				pdGainOverlap_t2;
447572ff6f6SMatthew Dillon 		Vpd_step = (uint16_t)(VpdTable_I[ii][1] - VpdTable_I[ii][0]);
448572ff6f6SMatthew Dillon 		Vpd_step = (uint16_t)((Vpd_step < 1) ? 1 : Vpd_step);
449572ff6f6SMatthew Dillon 		/*
450572ff6f6SMatthew Dillon 		 *-ve ss indicates need to extrapolate data below for this pdGain
451572ff6f6SMatthew Dillon 		 */
452572ff6f6SMatthew Dillon 		while (ss < 0) {
453572ff6f6SMatthew Dillon 			tmpVal = (int16_t)(VpdTable_I[ii][0] + ss*Vpd_step);
454572ff6f6SMatthew Dillon 			pPDADCValues[kk++] = (uint16_t)((tmpVal < 0) ? 0 : tmpVal);
455572ff6f6SMatthew Dillon 			ss++;
456572ff6f6SMatthew Dillon 		}
457572ff6f6SMatthew Dillon 
458572ff6f6SMatthew Dillon 		sizeCurrVpdTable = Pmax_t2[ii] - Pmin_t2[ii];
459572ff6f6SMatthew Dillon 		tgtIndex = pPdGainBoundaries[ii] + pdGainOverlap_t2 - Pmin_t2[ii];
460572ff6f6SMatthew Dillon 		maxIndex = (tgtIndex < sizeCurrVpdTable) ? tgtIndex : sizeCurrVpdTable;
461572ff6f6SMatthew Dillon 
462572ff6f6SMatthew Dillon 		while (ss < (int16_t)maxIndex)
463572ff6f6SMatthew Dillon 			pPDADCValues[kk++] = VpdTable_I[ii][ss++];
464572ff6f6SMatthew Dillon 
465572ff6f6SMatthew Dillon 		Vpd_step = (uint16_t)(VpdTable_I[ii][sizeCurrVpdTable-1] -
466572ff6f6SMatthew Dillon 				       VpdTable_I[ii][sizeCurrVpdTable-2]);
467572ff6f6SMatthew Dillon 		Vpd_step = (uint16_t)((Vpd_step < 1) ? 1 : Vpd_step);
468572ff6f6SMatthew Dillon 		/*
469572ff6f6SMatthew Dillon 		 * for last gain, pdGainBoundary == Pmax_t2, so will
470572ff6f6SMatthew Dillon 		 * have to extrapolate
471572ff6f6SMatthew Dillon 		 */
472572ff6f6SMatthew Dillon 		if (tgtIndex > maxIndex) {	/* need to extrapolate above */
473572ff6f6SMatthew Dillon 			while(ss < (int16_t)tgtIndex) {
474572ff6f6SMatthew Dillon 				tmpVal = (uint16_t)
475572ff6f6SMatthew Dillon 					(VpdTable_I[ii][sizeCurrVpdTable-1] +
476572ff6f6SMatthew Dillon 					 (ss-maxIndex)*Vpd_step);
477572ff6f6SMatthew Dillon 				pPDADCValues[kk++] = (tmpVal > 127) ?
478572ff6f6SMatthew Dillon 					127 : tmpVal;
479572ff6f6SMatthew Dillon 				ss++;
480572ff6f6SMatthew Dillon 			}
481572ff6f6SMatthew Dillon 		}				/* extrapolated above */
482572ff6f6SMatthew Dillon 	}					/* for all pdGainUsed */
483572ff6f6SMatthew Dillon 
484572ff6f6SMatthew Dillon 	while (ii < MAX_NUM_PDGAINS_PER_CHANNEL) {
485572ff6f6SMatthew Dillon 		pPdGainBoundaries[ii] = pPdGainBoundaries[ii-1];
486572ff6f6SMatthew Dillon 		ii++;
487572ff6f6SMatthew Dillon 	}
488572ff6f6SMatthew Dillon 	while (kk < 128) {
489572ff6f6SMatthew Dillon 		pPDADCValues[kk] = pPDADCValues[kk-1];
490572ff6f6SMatthew Dillon 		kk++;
491572ff6f6SMatthew Dillon 	}
492572ff6f6SMatthew Dillon 
493572ff6f6SMatthew Dillon 	HALDEBUG(ah, HAL_DEBUG_RFPARAM, "<==%s\n", __func__);
494572ff6f6SMatthew Dillon }
495572ff6f6SMatthew Dillon 
496572ff6f6SMatthew Dillon 
497572ff6f6SMatthew Dillon /* Same as 2413 set power table */
498572ff6f6SMatthew Dillon static HAL_BOOL
ar2425SetPowerTable(struct ath_hal * ah,int16_t * minPower,int16_t * maxPower,const struct ieee80211_channel * chan,uint16_t * rfXpdGain)499572ff6f6SMatthew Dillon ar2425SetPowerTable(struct ath_hal *ah,
500572ff6f6SMatthew Dillon 	int16_t *minPower, int16_t *maxPower,
501572ff6f6SMatthew Dillon 	const struct ieee80211_channel *chan,
502572ff6f6SMatthew Dillon 	uint16_t *rfXpdGain)
503572ff6f6SMatthew Dillon {
504572ff6f6SMatthew Dillon 	uint16_t freq = ath_hal_gethwchannel(ah, chan);
505572ff6f6SMatthew Dillon 	struct ath_hal_5212 *ahp = AH5212(ah);
506572ff6f6SMatthew Dillon 	const HAL_EEPROM *ee = AH_PRIVATE(ah)->ah_eeprom;
507572ff6f6SMatthew Dillon 	const RAW_DATA_STRUCT_2413 *pRawDataset = AH_NULL;
508572ff6f6SMatthew Dillon 	uint16_t pdGainOverlap_t2;
509572ff6f6SMatthew Dillon 	int16_t minCalPower2413_t2;
510572ff6f6SMatthew Dillon 	uint16_t *pdadcValues = ahp->ah_pcdacTable;
511572ff6f6SMatthew Dillon 	uint16_t gainBoundaries[4];
512572ff6f6SMatthew Dillon 	uint32_t i, reg32, regoffset;
513572ff6f6SMatthew Dillon 
514572ff6f6SMatthew Dillon 	HALDEBUG(ah, HAL_DEBUG_RFPARAM, "%s:chan 0x%x flag 0x%x\n",
515572ff6f6SMatthew Dillon 	    __func__, freq, chan->ic_flags);
516572ff6f6SMatthew Dillon 
517572ff6f6SMatthew Dillon 	if (IEEE80211_IS_CHAN_G(chan) || IEEE80211_IS_CHAN_108G(chan))
518572ff6f6SMatthew Dillon 		pRawDataset = &ee->ee_rawDataset2413[headerInfo11G];
519572ff6f6SMatthew Dillon 	else if (IEEE80211_IS_CHAN_B(chan))
520572ff6f6SMatthew Dillon 		pRawDataset = &ee->ee_rawDataset2413[headerInfo11B];
521572ff6f6SMatthew Dillon 	else {
522572ff6f6SMatthew Dillon 		HALDEBUG(ah, HAL_DEBUG_ANY, "%s:illegal mode\n", __func__);
523572ff6f6SMatthew Dillon 		return AH_FALSE;
524572ff6f6SMatthew Dillon 	}
525572ff6f6SMatthew Dillon 
526572ff6f6SMatthew Dillon 	pdGainOverlap_t2 = (uint16_t) SM(OS_REG_READ(ah, AR_PHY_TPCRG5),
527572ff6f6SMatthew Dillon 					  AR_PHY_TPCRG5_PD_GAIN_OVERLAP);
528572ff6f6SMatthew Dillon 
529572ff6f6SMatthew Dillon 	ar2425getGainBoundariesAndPdadcsForPowers(ah, freq,
530572ff6f6SMatthew Dillon 		pRawDataset, pdGainOverlap_t2,&minCalPower2413_t2,gainBoundaries,
531572ff6f6SMatthew Dillon 		rfXpdGain, pdadcValues);
532572ff6f6SMatthew Dillon 
533572ff6f6SMatthew Dillon 	OS_REG_RMW_FIELD(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_NUM_PD_GAIN,
534572ff6f6SMatthew Dillon 			 (pRawDataset->pDataPerChannel[0].numPdGains - 1));
535572ff6f6SMatthew Dillon 
536572ff6f6SMatthew Dillon 	/*
537572ff6f6SMatthew Dillon 	 * Note the pdadc table may not start at 0 dBm power, could be
538572ff6f6SMatthew Dillon 	 * negative or greater than 0.  Need to offset the power
539572ff6f6SMatthew Dillon 	 * values by the amount of minPower for griffin
540572ff6f6SMatthew Dillon 	 */
541572ff6f6SMatthew Dillon 	if (minCalPower2413_t2 != 0)
542572ff6f6SMatthew Dillon 		ahp->ah_txPowerIndexOffset = (int16_t)(0 - minCalPower2413_t2);
543572ff6f6SMatthew Dillon 	else
544572ff6f6SMatthew Dillon 		ahp->ah_txPowerIndexOffset = 0;
545572ff6f6SMatthew Dillon 
546572ff6f6SMatthew Dillon 	/* Finally, write the power values into the baseband power table */
547572ff6f6SMatthew Dillon 	regoffset = 0x9800 + (672 <<2); /* beginning of pdadc table in griffin */
548572ff6f6SMatthew Dillon 	for (i = 0; i < 32; i++) {
549572ff6f6SMatthew Dillon 		reg32 = ((pdadcValues[4*i + 0] & 0xFF) << 0)  |
550572ff6f6SMatthew Dillon 			((pdadcValues[4*i + 1] & 0xFF) << 8)  |
551572ff6f6SMatthew Dillon 			((pdadcValues[4*i + 2] & 0xFF) << 16) |
552572ff6f6SMatthew Dillon 			((pdadcValues[4*i + 3] & 0xFF) << 24) ;
553572ff6f6SMatthew Dillon 		OS_REG_WRITE(ah, regoffset, reg32);
554572ff6f6SMatthew Dillon 		regoffset += 4;
555572ff6f6SMatthew Dillon 	}
556572ff6f6SMatthew Dillon 
557572ff6f6SMatthew Dillon 	OS_REG_WRITE(ah, AR_PHY_TPCRG5,
558572ff6f6SMatthew Dillon 		     SM(pdGainOverlap_t2, AR_PHY_TPCRG5_PD_GAIN_OVERLAP) |
559572ff6f6SMatthew Dillon 		     SM(gainBoundaries[0], AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_1) |
560572ff6f6SMatthew Dillon 		     SM(gainBoundaries[1], AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_2) |
561572ff6f6SMatthew Dillon 		     SM(gainBoundaries[2], AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_3) |
562572ff6f6SMatthew Dillon 		     SM(gainBoundaries[3], AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_4));
563572ff6f6SMatthew Dillon 
564572ff6f6SMatthew Dillon 	return AH_TRUE;
565572ff6f6SMatthew Dillon }
566572ff6f6SMatthew Dillon 
567572ff6f6SMatthew Dillon static int16_t
ar2425GetMinPower(struct ath_hal * ah,const RAW_DATA_PER_CHANNEL_2413 * data)568572ff6f6SMatthew Dillon ar2425GetMinPower(struct ath_hal *ah, const RAW_DATA_PER_CHANNEL_2413 *data)
569572ff6f6SMatthew Dillon {
570572ff6f6SMatthew Dillon 	uint32_t ii,jj;
571572ff6f6SMatthew Dillon 	uint16_t Pmin=0,numVpd;
572572ff6f6SMatthew Dillon 
573572ff6f6SMatthew Dillon 	for (ii = 0; ii < MAX_NUM_PDGAINS_PER_CHANNEL; ii++) {
574572ff6f6SMatthew Dillon 		jj = MAX_NUM_PDGAINS_PER_CHANNEL - ii - 1;
575572ff6f6SMatthew Dillon 		/* work backwards 'cause highest pdGain for lowest power */
576572ff6f6SMatthew Dillon 		numVpd = data->pDataPerPDGain[jj].numVpd;
577572ff6f6SMatthew Dillon 		if (numVpd > 0) {
578572ff6f6SMatthew Dillon 			Pmin = data->pDataPerPDGain[jj].pwr_t4[0];
579572ff6f6SMatthew Dillon 			return(Pmin);
580572ff6f6SMatthew Dillon 		}
581572ff6f6SMatthew Dillon 	}
582572ff6f6SMatthew Dillon 	return(Pmin);
583572ff6f6SMatthew Dillon }
584572ff6f6SMatthew Dillon 
585572ff6f6SMatthew Dillon static int16_t
ar2425GetMaxPower(struct ath_hal * ah,const RAW_DATA_PER_CHANNEL_2413 * data)586572ff6f6SMatthew Dillon ar2425GetMaxPower(struct ath_hal *ah, const RAW_DATA_PER_CHANNEL_2413 *data)
587572ff6f6SMatthew Dillon {
588572ff6f6SMatthew Dillon 	uint32_t ii;
589572ff6f6SMatthew Dillon 	uint16_t Pmax=0,numVpd;
590572ff6f6SMatthew Dillon 
591572ff6f6SMatthew Dillon 	for (ii=0; ii< MAX_NUM_PDGAINS_PER_CHANNEL; ii++) {
592572ff6f6SMatthew Dillon 		/* work forwards cuase lowest pdGain for highest power */
593572ff6f6SMatthew Dillon 		numVpd = data->pDataPerPDGain[ii].numVpd;
594572ff6f6SMatthew Dillon 		if (numVpd > 0) {
595572ff6f6SMatthew Dillon 			Pmax = data->pDataPerPDGain[ii].pwr_t4[numVpd-1];
596572ff6f6SMatthew Dillon 			return(Pmax);
597572ff6f6SMatthew Dillon 		}
598572ff6f6SMatthew Dillon 	}
599572ff6f6SMatthew Dillon 	return(Pmax);
600572ff6f6SMatthew Dillon }
601572ff6f6SMatthew Dillon 
602572ff6f6SMatthew Dillon static
603572ff6f6SMatthew Dillon HAL_BOOL
ar2425GetChannelMaxMinPower(struct ath_hal * ah,const struct ieee80211_channel * chan,int16_t * maxPow,int16_t * minPow)604572ff6f6SMatthew Dillon ar2425GetChannelMaxMinPower(struct ath_hal *ah,
605572ff6f6SMatthew Dillon 	const struct ieee80211_channel *chan,
606572ff6f6SMatthew Dillon 	int16_t *maxPow, int16_t *minPow)
607572ff6f6SMatthew Dillon {
608572ff6f6SMatthew Dillon 	uint16_t freq = chan->ic_freq;		/* NB: never mapped */
609572ff6f6SMatthew Dillon 	const HAL_EEPROM *ee = AH_PRIVATE(ah)->ah_eeprom;
610572ff6f6SMatthew Dillon 	const RAW_DATA_STRUCT_2413 *pRawDataset = AH_NULL;
611572ff6f6SMatthew Dillon 	const RAW_DATA_PER_CHANNEL_2413 *data = AH_NULL;
612572ff6f6SMatthew Dillon 	uint16_t numChannels;
613572ff6f6SMatthew Dillon 	int totalD,totalF, totalMin,last, i;
614572ff6f6SMatthew Dillon 
615572ff6f6SMatthew Dillon 	*maxPow = 0;
616572ff6f6SMatthew Dillon 
617572ff6f6SMatthew Dillon 	if (IEEE80211_IS_CHAN_G(chan) || IEEE80211_IS_CHAN_108G(chan))
618572ff6f6SMatthew Dillon 		pRawDataset = &ee->ee_rawDataset2413[headerInfo11G];
619572ff6f6SMatthew Dillon 	else if (IEEE80211_IS_CHAN_B(chan))
620572ff6f6SMatthew Dillon 		pRawDataset = &ee->ee_rawDataset2413[headerInfo11B];
621572ff6f6SMatthew Dillon 	else
622572ff6f6SMatthew Dillon 		return(AH_FALSE);
623572ff6f6SMatthew Dillon 
624572ff6f6SMatthew Dillon 	numChannels = pRawDataset->numChannels;
625572ff6f6SMatthew Dillon 	data = pRawDataset->pDataPerChannel;
626572ff6f6SMatthew Dillon 
627572ff6f6SMatthew Dillon 	/* Make sure the channel is in the range of the TP values
628572ff6f6SMatthew Dillon 	 *  (freq piers)
629572ff6f6SMatthew Dillon 	 */
630572ff6f6SMatthew Dillon 	if (numChannels < 1)
631572ff6f6SMatthew Dillon 		return(AH_FALSE);
632572ff6f6SMatthew Dillon 
633572ff6f6SMatthew Dillon 	if ((freq < data[0].channelValue) ||
634572ff6f6SMatthew Dillon 	    (freq > data[numChannels-1].channelValue)) {
635572ff6f6SMatthew Dillon 		if (freq < data[0].channelValue) {
636572ff6f6SMatthew Dillon 			*maxPow = ar2425GetMaxPower(ah, &data[0]);
637572ff6f6SMatthew Dillon 			*minPow = ar2425GetMinPower(ah, &data[0]);
638572ff6f6SMatthew Dillon 			return(AH_TRUE);
639572ff6f6SMatthew Dillon 		} else {
640572ff6f6SMatthew Dillon 			*maxPow = ar2425GetMaxPower(ah, &data[numChannels - 1]);
641572ff6f6SMatthew Dillon 			*minPow = ar2425GetMinPower(ah, &data[numChannels - 1]);
642572ff6f6SMatthew Dillon 			return(AH_TRUE);
643572ff6f6SMatthew Dillon 		}
644572ff6f6SMatthew Dillon 	}
645572ff6f6SMatthew Dillon 
646572ff6f6SMatthew Dillon 	/* Linearly interpolate the power value now */
647572ff6f6SMatthew Dillon 	for (last=0,i=0; (i<numChannels) && (freq > data[i].channelValue);
648572ff6f6SMatthew Dillon 	     last = i++);
649572ff6f6SMatthew Dillon 	totalD = data[i].channelValue - data[last].channelValue;
650572ff6f6SMatthew Dillon 	if (totalD > 0) {
651572ff6f6SMatthew Dillon 		totalF = ar2425GetMaxPower(ah, &data[i]) - ar2425GetMaxPower(ah, &data[last]);
652572ff6f6SMatthew Dillon 		*maxPow = (int8_t) ((totalF*(freq-data[last].channelValue) +
653572ff6f6SMatthew Dillon 				     ar2425GetMaxPower(ah, &data[last])*totalD)/totalD);
654572ff6f6SMatthew Dillon 		totalMin = ar2425GetMinPower(ah, &data[i]) - ar2425GetMinPower(ah, &data[last]);
655572ff6f6SMatthew Dillon 		*minPow = (int8_t) ((totalMin*(freq-data[last].channelValue) +
656572ff6f6SMatthew Dillon 				     ar2425GetMinPower(ah, &data[last])*totalD)/totalD);
657572ff6f6SMatthew Dillon 		return(AH_TRUE);
658572ff6f6SMatthew Dillon 	} else {
659572ff6f6SMatthew Dillon 		if (freq == data[i].channelValue) {
660572ff6f6SMatthew Dillon 			*maxPow = ar2425GetMaxPower(ah, &data[i]);
661572ff6f6SMatthew Dillon 			*minPow = ar2425GetMinPower(ah, &data[i]);
662572ff6f6SMatthew Dillon 			return(AH_TRUE);
663572ff6f6SMatthew Dillon 		} else
664572ff6f6SMatthew Dillon 			return(AH_FALSE);
665572ff6f6SMatthew Dillon 	}
666572ff6f6SMatthew Dillon }
667572ff6f6SMatthew Dillon 
668572ff6f6SMatthew Dillon /*
669572ff6f6SMatthew Dillon  * Free memory for analog bank scratch buffers
670572ff6f6SMatthew Dillon  */
671572ff6f6SMatthew Dillon static void
ar2425RfDetach(struct ath_hal * ah)672572ff6f6SMatthew Dillon ar2425RfDetach(struct ath_hal *ah)
673572ff6f6SMatthew Dillon {
674572ff6f6SMatthew Dillon 	struct ath_hal_5212 *ahp = AH5212(ah);
675572ff6f6SMatthew Dillon 
676572ff6f6SMatthew Dillon 	HALASSERT(ahp->ah_rfHal != AH_NULL);
677572ff6f6SMatthew Dillon 	ath_hal_free(ahp->ah_rfHal);
678572ff6f6SMatthew Dillon 	ahp->ah_rfHal = AH_NULL;
679572ff6f6SMatthew Dillon }
680572ff6f6SMatthew Dillon 
681572ff6f6SMatthew Dillon /*
682572ff6f6SMatthew Dillon  * Allocate memory for analog bank scratch buffers
683572ff6f6SMatthew Dillon  * Scratch Buffer will be reinitialized every reset so no need to zero now
684572ff6f6SMatthew Dillon  */
685572ff6f6SMatthew Dillon static HAL_BOOL
ar2425RfAttach(struct ath_hal * ah,HAL_STATUS * status)686572ff6f6SMatthew Dillon ar2425RfAttach(struct ath_hal *ah, HAL_STATUS *status)
687572ff6f6SMatthew Dillon {
688572ff6f6SMatthew Dillon 	struct ath_hal_5212 *ahp = AH5212(ah);
689572ff6f6SMatthew Dillon 	struct ar2425State *priv;
690572ff6f6SMatthew Dillon 
691572ff6f6SMatthew Dillon 	HALASSERT(ah->ah_magic == AR5212_MAGIC);
692572ff6f6SMatthew Dillon 
693572ff6f6SMatthew Dillon 	HALASSERT(ahp->ah_rfHal == AH_NULL);
694572ff6f6SMatthew Dillon 	priv = ath_hal_malloc(sizeof(struct ar2425State));
695572ff6f6SMatthew Dillon 	if (priv == AH_NULL) {
696572ff6f6SMatthew Dillon 		HALDEBUG(ah, HAL_DEBUG_ANY,
697572ff6f6SMatthew Dillon 		    "%s: cannot allocate private state\n", __func__);
698572ff6f6SMatthew Dillon 		*status = HAL_ENOMEM;		/* XXX */
699572ff6f6SMatthew Dillon 		return AH_FALSE;
700572ff6f6SMatthew Dillon 	}
701572ff6f6SMatthew Dillon 	priv->base.rfDetach		= ar2425RfDetach;
702572ff6f6SMatthew Dillon 	priv->base.writeRegs		= ar2425WriteRegs;
703572ff6f6SMatthew Dillon 	priv->base.getRfBank		= ar2425GetRfBank;
704572ff6f6SMatthew Dillon 	priv->base.setChannel		= ar2425SetChannel;
705572ff6f6SMatthew Dillon 	priv->base.setRfRegs		= ar2425SetRfRegs;
706572ff6f6SMatthew Dillon 	priv->base.setPowerTable	= ar2425SetPowerTable;
707572ff6f6SMatthew Dillon 	priv->base.getChannelMaxMinPower = ar2425GetChannelMaxMinPower;
708572ff6f6SMatthew Dillon 	priv->base.getNfAdjust		= ar5212GetNfAdjust;
709572ff6f6SMatthew Dillon 
710572ff6f6SMatthew Dillon 	ahp->ah_pcdacTable = priv->pcdacTable;
711572ff6f6SMatthew Dillon 	ahp->ah_pcdacTableSize = sizeof(priv->pcdacTable);
712572ff6f6SMatthew Dillon 	ahp->ah_rfHal = &priv->base;
713572ff6f6SMatthew Dillon 
714572ff6f6SMatthew Dillon 	return AH_TRUE;
715572ff6f6SMatthew Dillon }
716572ff6f6SMatthew Dillon 
717572ff6f6SMatthew Dillon static HAL_BOOL
ar2425Probe(struct ath_hal * ah)718572ff6f6SMatthew Dillon ar2425Probe(struct ath_hal *ah)
719572ff6f6SMatthew Dillon {
720572ff6f6SMatthew Dillon 	return IS_2425(ah) || IS_2417(ah);
721572ff6f6SMatthew Dillon }
722572ff6f6SMatthew Dillon AH_RF(RF2425, ar2425Probe, ar2425RfAttach);
723