xref: /dflybsd-src/sys/dev/netif/ath/ath_hal/ar5212/ar5111.c (revision ed183f8c2f9bb14cbccb8377f3cdd29e0971d8a0)
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 "ah_eeprom_v3.h"
25572ff6f6SMatthew Dillon 
26572ff6f6SMatthew Dillon #include "ar5212/ar5212.h"
27572ff6f6SMatthew Dillon #include "ar5212/ar5212reg.h"
28572ff6f6SMatthew Dillon #include "ar5212/ar5212phy.h"
29572ff6f6SMatthew Dillon 
30572ff6f6SMatthew Dillon #define AH_5212_5111
31572ff6f6SMatthew Dillon #include "ar5212/ar5212.ini"
32572ff6f6SMatthew Dillon 
33572ff6f6SMatthew Dillon #define	N(a)	(sizeof(a)/sizeof(a[0]))
34572ff6f6SMatthew Dillon 
35572ff6f6SMatthew Dillon struct ar5111State {
36572ff6f6SMatthew Dillon 	RF_HAL_FUNCS	base;		/* public state, must be first */
37572ff6f6SMatthew Dillon 	uint16_t	pcdacTable[PWR_TABLE_SIZE];
38572ff6f6SMatthew Dillon 
39572ff6f6SMatthew Dillon 	uint32_t	Bank0Data[N(ar5212Bank0_5111)];
40572ff6f6SMatthew Dillon 	uint32_t	Bank1Data[N(ar5212Bank1_5111)];
41572ff6f6SMatthew Dillon 	uint32_t	Bank2Data[N(ar5212Bank2_5111)];
42572ff6f6SMatthew Dillon 	uint32_t	Bank3Data[N(ar5212Bank3_5111)];
43572ff6f6SMatthew Dillon 	uint32_t	Bank6Data[N(ar5212Bank6_5111)];
44572ff6f6SMatthew Dillon 	uint32_t	Bank7Data[N(ar5212Bank7_5111)];
45572ff6f6SMatthew Dillon };
46572ff6f6SMatthew Dillon #define	AR5111(ah)	((struct ar5111State *) AH5212(ah)->ah_rfHal)
47572ff6f6SMatthew Dillon 
48572ff6f6SMatthew Dillon static uint16_t ar5212GetScaledPower(uint16_t channel, uint16_t pcdacValue,
49572ff6f6SMatthew Dillon 		const PCDACS_EEPROM *pSrcStruct);
50572ff6f6SMatthew Dillon static HAL_BOOL ar5212FindValueInList(uint16_t channel, uint16_t pcdacValue,
51572ff6f6SMatthew Dillon 		const PCDACS_EEPROM *pSrcStruct, uint16_t *powerValue);
52572ff6f6SMatthew Dillon static void ar5212GetLowerUpperPcdacs(uint16_t pcdac, uint16_t channel,
53572ff6f6SMatthew Dillon 		const PCDACS_EEPROM *pSrcStruct,
54572ff6f6SMatthew Dillon 		uint16_t *pLowerPcdac, uint16_t *pUpperPcdac);
55572ff6f6SMatthew Dillon 
56572ff6f6SMatthew Dillon extern void ar5212GetLowerUpperValues(uint16_t value,
57572ff6f6SMatthew Dillon 		const uint16_t *pList, uint16_t listSize,
58572ff6f6SMatthew Dillon 		uint16_t *pLowerValue, uint16_t *pUpperValue);
59572ff6f6SMatthew Dillon extern	void ar5212ModifyRfBuffer(uint32_t *rfBuf, uint32_t reg32,
60572ff6f6SMatthew Dillon 		uint32_t numBits, uint32_t firstBit, uint32_t column);
61572ff6f6SMatthew Dillon 
62572ff6f6SMatthew Dillon static void
ar5111WriteRegs(struct ath_hal * ah,u_int modesIndex,u_int freqIndex,int writes)63572ff6f6SMatthew Dillon ar5111WriteRegs(struct ath_hal *ah, u_int modesIndex, u_int freqIndex,
64572ff6f6SMatthew Dillon 	int writes)
65572ff6f6SMatthew Dillon {
66572ff6f6SMatthew Dillon 	HAL_INI_WRITE_ARRAY(ah, ar5212Modes_5111, modesIndex, writes);
67572ff6f6SMatthew Dillon 	HAL_INI_WRITE_ARRAY(ah, ar5212Common_5111, 1, writes);
68572ff6f6SMatthew Dillon 	HAL_INI_WRITE_ARRAY(ah, ar5212BB_RfGain_5111, freqIndex, writes);
69572ff6f6SMatthew Dillon }
70572ff6f6SMatthew Dillon 
71572ff6f6SMatthew Dillon /*
72572ff6f6SMatthew Dillon  * Take the MHz channel value and set the Channel value
73572ff6f6SMatthew Dillon  *
74572ff6f6SMatthew Dillon  * ASSUMES: Writes enabled to analog bus
75572ff6f6SMatthew Dillon  */
76572ff6f6SMatthew Dillon static HAL_BOOL
ar5111SetChannel(struct ath_hal * ah,const struct ieee80211_channel * chan)77572ff6f6SMatthew Dillon ar5111SetChannel(struct ath_hal *ah, const struct ieee80211_channel *chan)
78572ff6f6SMatthew Dillon {
79572ff6f6SMatthew Dillon #define CI_2GHZ_INDEX_CORRECTION 19
80572ff6f6SMatthew Dillon 	uint16_t freq = ath_hal_gethwchannel(ah, chan);
81572ff6f6SMatthew Dillon 	uint32_t refClk, reg32, data2111;
82572ff6f6SMatthew Dillon 	int16_t chan5111, chanIEEE;
83572ff6f6SMatthew Dillon 
84572ff6f6SMatthew Dillon 	/*
85572ff6f6SMatthew Dillon 	 * Structure to hold 11b tuning information for 5111/2111
86572ff6f6SMatthew Dillon 	 * 16 MHz mode, divider ratio = 198 = NP+S. N=16, S=4 or 6, P=12
87572ff6f6SMatthew Dillon 	 */
88572ff6f6SMatthew Dillon 	typedef struct {
89572ff6f6SMatthew Dillon 		uint32_t	refClkSel;	/* reference clock, 1 for 16 MHz */
90572ff6f6SMatthew Dillon 		uint32_t	channelSelect;	/* P[7:4]S[3:0] bits */
91572ff6f6SMatthew Dillon 		uint16_t	channel5111;	/* 11a channel for 5111 */
92572ff6f6SMatthew Dillon 	} CHAN_INFO_2GHZ;
93572ff6f6SMatthew Dillon 
94572ff6f6SMatthew Dillon 	static const CHAN_INFO_2GHZ chan2GHzData[] = {
95572ff6f6SMatthew Dillon 		{ 1, 0x46, 96  },	/* 2312 -19 */
96572ff6f6SMatthew Dillon 		{ 1, 0x46, 97  },	/* 2317 -18 */
97572ff6f6SMatthew Dillon 		{ 1, 0x46, 98  },	/* 2322 -17 */
98572ff6f6SMatthew Dillon 		{ 1, 0x46, 99  },	/* 2327 -16 */
99572ff6f6SMatthew Dillon 		{ 1, 0x46, 100 },	/* 2332 -15 */
100572ff6f6SMatthew Dillon 		{ 1, 0x46, 101 },	/* 2337 -14 */
101572ff6f6SMatthew Dillon 		{ 1, 0x46, 102 },	/* 2342 -13 */
102572ff6f6SMatthew Dillon 		{ 1, 0x46, 103 },	/* 2347 -12 */
103572ff6f6SMatthew Dillon 		{ 1, 0x46, 104 },	/* 2352 -11 */
104572ff6f6SMatthew Dillon 		{ 1, 0x46, 105 },	/* 2357 -10 */
105572ff6f6SMatthew Dillon 		{ 1, 0x46, 106 },	/* 2362  -9 */
106572ff6f6SMatthew Dillon 		{ 1, 0x46, 107 },	/* 2367  -8 */
107572ff6f6SMatthew Dillon 		{ 1, 0x46, 108 },	/* 2372  -7 */
108572ff6f6SMatthew Dillon 		/* index -6 to 0 are pad to make this a nolookup table */
109572ff6f6SMatthew Dillon 		{ 1, 0x46, 116 },	/*       -6 */
110572ff6f6SMatthew Dillon 		{ 1, 0x46, 116 },	/*       -5 */
111572ff6f6SMatthew Dillon 		{ 1, 0x46, 116 },	/*       -4 */
112572ff6f6SMatthew Dillon 		{ 1, 0x46, 116 },	/*       -3 */
113572ff6f6SMatthew Dillon 		{ 1, 0x46, 116 },	/*       -2 */
114572ff6f6SMatthew Dillon 		{ 1, 0x46, 116 },	/*       -1 */
115572ff6f6SMatthew Dillon 		{ 1, 0x46, 116 },	/*        0 */
116572ff6f6SMatthew Dillon 		{ 1, 0x46, 116 },	/* 2412   1 */
117572ff6f6SMatthew Dillon 		{ 1, 0x46, 117 },	/* 2417   2 */
118572ff6f6SMatthew Dillon 		{ 1, 0x46, 118 },	/* 2422   3 */
119572ff6f6SMatthew Dillon 		{ 1, 0x46, 119 },	/* 2427   4 */
120572ff6f6SMatthew Dillon 		{ 1, 0x46, 120 },	/* 2432   5 */
121572ff6f6SMatthew Dillon 		{ 1, 0x46, 121 },	/* 2437   6 */
122572ff6f6SMatthew Dillon 		{ 1, 0x46, 122 },	/* 2442   7 */
123572ff6f6SMatthew Dillon 		{ 1, 0x46, 123 },	/* 2447   8 */
124572ff6f6SMatthew Dillon 		{ 1, 0x46, 124 },	/* 2452   9 */
125572ff6f6SMatthew Dillon 		{ 1, 0x46, 125 },	/* 2457  10 */
126572ff6f6SMatthew Dillon 		{ 1, 0x46, 126 },	/* 2462  11 */
127572ff6f6SMatthew Dillon 		{ 1, 0x46, 127 },	/* 2467  12 */
128572ff6f6SMatthew Dillon 		{ 1, 0x46, 128 },	/* 2472  13 */
129572ff6f6SMatthew Dillon 		{ 1, 0x44, 124 },	/* 2484  14 */
130572ff6f6SMatthew Dillon 		{ 1, 0x46, 136 },	/* 2512  15 */
131572ff6f6SMatthew Dillon 		{ 1, 0x46, 140 },	/* 2532  16 */
132572ff6f6SMatthew Dillon 		{ 1, 0x46, 144 },	/* 2552  17 */
133572ff6f6SMatthew Dillon 		{ 1, 0x46, 148 },	/* 2572  18 */
134572ff6f6SMatthew Dillon 		{ 1, 0x46, 152 },	/* 2592  19 */
135572ff6f6SMatthew Dillon 		{ 1, 0x46, 156 },	/* 2612  20 */
136572ff6f6SMatthew Dillon 		{ 1, 0x46, 160 },	/* 2632  21 */
137572ff6f6SMatthew Dillon 		{ 1, 0x46, 164 },	/* 2652  22 */
138572ff6f6SMatthew Dillon 		{ 1, 0x46, 168 },	/* 2672  23 */
139572ff6f6SMatthew Dillon 		{ 1, 0x46, 172 },	/* 2692  24 */
140572ff6f6SMatthew Dillon 		{ 1, 0x46, 176 },	/* 2712  25 */
141572ff6f6SMatthew Dillon 		{ 1, 0x46, 180 } 	/* 2732  26 */
142572ff6f6SMatthew Dillon 	};
143572ff6f6SMatthew Dillon 
144572ff6f6SMatthew Dillon 	OS_MARK(ah, AH_MARK_SETCHANNEL, freq);
145572ff6f6SMatthew Dillon 
146572ff6f6SMatthew Dillon 	chanIEEE = chan->ic_ieee;
147572ff6f6SMatthew Dillon 	if (IEEE80211_IS_CHAN_2GHZ(chan)) {
148572ff6f6SMatthew Dillon 		const CHAN_INFO_2GHZ* ci =
149572ff6f6SMatthew Dillon 			&chan2GHzData[chanIEEE + CI_2GHZ_INDEX_CORRECTION];
150572ff6f6SMatthew Dillon 		uint32_t txctl;
151572ff6f6SMatthew Dillon 
152572ff6f6SMatthew Dillon 		data2111 = ((ath_hal_reverseBits(ci->channelSelect, 8) & 0xff)
153572ff6f6SMatthew Dillon 				<< 5)
154572ff6f6SMatthew Dillon 			 | (ci->refClkSel << 4);
155572ff6f6SMatthew Dillon 		chan5111 = ci->channel5111;
156572ff6f6SMatthew Dillon 		txctl = OS_REG_READ(ah, AR_PHY_CCK_TX_CTRL);
157572ff6f6SMatthew Dillon 		if (freq == 2484) {
158572ff6f6SMatthew Dillon 			/* Enable channel spreading for channel 14 */
159572ff6f6SMatthew Dillon 			OS_REG_WRITE(ah, AR_PHY_CCK_TX_CTRL,
160572ff6f6SMatthew Dillon 				txctl | AR_PHY_CCK_TX_CTRL_JAPAN);
161572ff6f6SMatthew Dillon 		} else {
162572ff6f6SMatthew Dillon 			OS_REG_WRITE(ah, AR_PHY_CCK_TX_CTRL,
163572ff6f6SMatthew Dillon 				txctl &~ AR_PHY_CCK_TX_CTRL_JAPAN);
164572ff6f6SMatthew Dillon 		}
165572ff6f6SMatthew Dillon 	} else {
166572ff6f6SMatthew Dillon 		chan5111 = chanIEEE;	/* no conversion needed */
167572ff6f6SMatthew Dillon 		data2111 = 0;
168572ff6f6SMatthew Dillon 	}
169572ff6f6SMatthew Dillon 
170572ff6f6SMatthew Dillon 	/* Rest of the code is common for 5 GHz and 2.4 GHz. */
171572ff6f6SMatthew Dillon 	if (chan5111 >= 145 || (chan5111 & 0x1)) {
172572ff6f6SMatthew Dillon 		reg32  = ath_hal_reverseBits(chan5111 - 24, 8) & 0xff;
173572ff6f6SMatthew Dillon 		refClk = 1;
174572ff6f6SMatthew Dillon 	} else {
175572ff6f6SMatthew Dillon 		reg32  = ath_hal_reverseBits(((chan5111 - 24)/2), 8) & 0xff;
176572ff6f6SMatthew Dillon 		refClk = 0;
177572ff6f6SMatthew Dillon 	}
178572ff6f6SMatthew Dillon 
179572ff6f6SMatthew Dillon 	reg32 = (reg32 << 2) | (refClk << 1) | (1 << 10) | 0x1;
180572ff6f6SMatthew Dillon 	OS_REG_WRITE(ah, AR_PHY(0x27), ((data2111 & 0xff) << 8) | (reg32 & 0xff));
181572ff6f6SMatthew Dillon 	reg32 >>= 8;
182572ff6f6SMatthew Dillon 	OS_REG_WRITE(ah, AR_PHY(0x34), (data2111 & 0xff00) | (reg32 & 0xff));
183572ff6f6SMatthew Dillon 
184572ff6f6SMatthew Dillon 	AH_PRIVATE(ah)->ah_curchan = chan;
185572ff6f6SMatthew Dillon 	return AH_TRUE;
186572ff6f6SMatthew Dillon #undef CI_2GHZ_INDEX_CORRECTION
187572ff6f6SMatthew Dillon }
188572ff6f6SMatthew Dillon 
189572ff6f6SMatthew Dillon /*
190572ff6f6SMatthew Dillon  * Return a reference to the requested RF Bank.
191572ff6f6SMatthew Dillon  */
192572ff6f6SMatthew Dillon static uint32_t *
ar5111GetRfBank(struct ath_hal * ah,int bank)193572ff6f6SMatthew Dillon ar5111GetRfBank(struct ath_hal *ah, int bank)
194572ff6f6SMatthew Dillon {
195572ff6f6SMatthew Dillon 	struct ar5111State *priv = AR5111(ah);
196572ff6f6SMatthew Dillon 
197572ff6f6SMatthew Dillon 	HALASSERT(priv != AH_NULL);
198572ff6f6SMatthew Dillon 	switch (bank) {
199572ff6f6SMatthew Dillon 	case 0: return priv->Bank0Data;
200572ff6f6SMatthew Dillon 	case 1: return priv->Bank1Data;
201572ff6f6SMatthew Dillon 	case 2: return priv->Bank2Data;
202572ff6f6SMatthew Dillon 	case 3: return priv->Bank3Data;
203572ff6f6SMatthew Dillon 	case 6: return priv->Bank6Data;
204572ff6f6SMatthew Dillon 	case 7: return priv->Bank7Data;
205572ff6f6SMatthew Dillon 	}
206572ff6f6SMatthew Dillon 	HALDEBUG(ah, HAL_DEBUG_ANY, "%s: unknown RF Bank %d requested\n",
207572ff6f6SMatthew Dillon 	    __func__, bank);
208572ff6f6SMatthew Dillon 	return AH_NULL;
209572ff6f6SMatthew Dillon }
210572ff6f6SMatthew Dillon 
211572ff6f6SMatthew Dillon /*
212572ff6f6SMatthew Dillon  * Reads EEPROM header info from device structure and programs
213572ff6f6SMatthew Dillon  * all rf registers
214572ff6f6SMatthew Dillon  *
215572ff6f6SMatthew Dillon  * REQUIRES: Access to the analog rf device
216572ff6f6SMatthew Dillon  */
217572ff6f6SMatthew Dillon static HAL_BOOL
ar5111SetRfRegs(struct ath_hal * ah,const struct ieee80211_channel * chan,uint16_t modesIndex,uint16_t * rfXpdGain)218572ff6f6SMatthew Dillon ar5111SetRfRegs(struct ath_hal *ah, const struct ieee80211_channel *chan,
219572ff6f6SMatthew Dillon 	uint16_t modesIndex, uint16_t *rfXpdGain)
220572ff6f6SMatthew Dillon {
221572ff6f6SMatthew Dillon 	uint16_t freq = ath_hal_gethwchannel(ah, chan);
222572ff6f6SMatthew Dillon 	struct ath_hal_5212 *ahp = AH5212(ah);
223572ff6f6SMatthew Dillon 	const HAL_EEPROM *ee = AH_PRIVATE(ah)->ah_eeprom;
224572ff6f6SMatthew Dillon 	uint16_t rfXpdGainFixed, rfPloSel, rfPwdXpd, gainI;
225572ff6f6SMatthew Dillon 	uint16_t tempOB, tempDB;
226572ff6f6SMatthew Dillon 	uint32_t ob2GHz, db2GHz, rfReg[N(ar5212Bank6_5111)];
227572ff6f6SMatthew Dillon 	int i, regWrites = 0;
228572ff6f6SMatthew Dillon 
229572ff6f6SMatthew Dillon 	HALDEBUG(ah, HAL_DEBUG_RFPARAM, "%s: chan %u/0x%x modesIndex %u\n",
230572ff6f6SMatthew Dillon 	    __func__, chan->ic_freq, chan->ic_flags, modesIndex);
231572ff6f6SMatthew Dillon 
232572ff6f6SMatthew Dillon 	/* Setup rf parameters */
233572ff6f6SMatthew Dillon 	switch (chan->ic_flags & IEEE80211_CHAN_ALLFULL) {
234572ff6f6SMatthew Dillon 	case IEEE80211_CHAN_A:
235572ff6f6SMatthew Dillon 		if (4000 < freq && freq < 5260) {
236572ff6f6SMatthew Dillon 			tempOB = ee->ee_ob1;
237572ff6f6SMatthew Dillon 			tempDB = ee->ee_db1;
238572ff6f6SMatthew Dillon 		} else if (5260 <= freq && freq < 5500) {
239572ff6f6SMatthew Dillon 			tempOB = ee->ee_ob2;
240572ff6f6SMatthew Dillon 			tempDB = ee->ee_db2;
241572ff6f6SMatthew Dillon 		} else if (5500 <= freq && freq < 5725) {
242572ff6f6SMatthew Dillon 			tempOB = ee->ee_ob3;
243572ff6f6SMatthew Dillon 			tempDB = ee->ee_db3;
244572ff6f6SMatthew Dillon 		} else if (freq >= 5725) {
245572ff6f6SMatthew Dillon 			tempOB = ee->ee_ob4;
246572ff6f6SMatthew Dillon 			tempDB = ee->ee_db4;
247572ff6f6SMatthew Dillon 		} else {
248572ff6f6SMatthew Dillon 			/* XXX when does this happen??? */
249572ff6f6SMatthew Dillon 			tempOB = tempDB = 0;
250572ff6f6SMatthew Dillon 		}
251572ff6f6SMatthew Dillon 		ob2GHz = db2GHz = 0;
252572ff6f6SMatthew Dillon 
253572ff6f6SMatthew Dillon 		rfXpdGainFixed = ee->ee_xgain[headerInfo11A];
254572ff6f6SMatthew Dillon 		rfPloSel = ee->ee_xpd[headerInfo11A];
255572ff6f6SMatthew Dillon 		rfPwdXpd = !ee->ee_xpd[headerInfo11A];
256572ff6f6SMatthew Dillon 		gainI = ee->ee_gainI[headerInfo11A];
257572ff6f6SMatthew Dillon 		break;
258572ff6f6SMatthew Dillon 	case IEEE80211_CHAN_B:
259572ff6f6SMatthew Dillon 		tempOB = ee->ee_obFor24;
260572ff6f6SMatthew Dillon 		tempDB = ee->ee_dbFor24;
261572ff6f6SMatthew Dillon 		ob2GHz = ee->ee_ob2GHz[0];
262572ff6f6SMatthew Dillon 		db2GHz = ee->ee_db2GHz[0];
263572ff6f6SMatthew Dillon 
264572ff6f6SMatthew Dillon 		rfXpdGainFixed = ee->ee_xgain[headerInfo11B];
265572ff6f6SMatthew Dillon 		rfPloSel = ee->ee_xpd[headerInfo11B];
266572ff6f6SMatthew Dillon 		rfPwdXpd = !ee->ee_xpd[headerInfo11B];
267572ff6f6SMatthew Dillon 		gainI = ee->ee_gainI[headerInfo11B];
268572ff6f6SMatthew Dillon 		break;
269572ff6f6SMatthew Dillon 	case IEEE80211_CHAN_G:
270572ff6f6SMatthew Dillon 	case IEEE80211_CHAN_PUREG:	/* NB: really 108G */
271572ff6f6SMatthew Dillon 		tempOB = ee->ee_obFor24g;
272572ff6f6SMatthew Dillon 		tempDB = ee->ee_dbFor24g;
273572ff6f6SMatthew Dillon 		ob2GHz = ee->ee_ob2GHz[1];
274572ff6f6SMatthew Dillon 		db2GHz = ee->ee_db2GHz[1];
275572ff6f6SMatthew Dillon 
276572ff6f6SMatthew Dillon 		rfXpdGainFixed = ee->ee_xgain[headerInfo11G];
277572ff6f6SMatthew Dillon 		rfPloSel = ee->ee_xpd[headerInfo11G];
278572ff6f6SMatthew Dillon 		rfPwdXpd = !ee->ee_xpd[headerInfo11G];
279572ff6f6SMatthew Dillon 		gainI = ee->ee_gainI[headerInfo11G];
280572ff6f6SMatthew Dillon 		break;
281572ff6f6SMatthew Dillon 	default:
282572ff6f6SMatthew Dillon 		HALDEBUG(ah, HAL_DEBUG_ANY, "%s: invalid channel flags 0x%x\n",
283572ff6f6SMatthew Dillon 		    __func__, chan->ic_flags);
284572ff6f6SMatthew Dillon 		return AH_FALSE;
285572ff6f6SMatthew Dillon 	}
286572ff6f6SMatthew Dillon 
287572ff6f6SMatthew Dillon 	HALASSERT(1 <= tempOB && tempOB <= 5);
288572ff6f6SMatthew Dillon 	HALASSERT(1 <= tempDB && tempDB <= 5);
289572ff6f6SMatthew Dillon 
290572ff6f6SMatthew Dillon 	/* Bank 0 Write */
291572ff6f6SMatthew Dillon 	for (i = 0; i < N(ar5212Bank0_5111); i++)
292572ff6f6SMatthew Dillon 		rfReg[i] = ar5212Bank0_5111[i][modesIndex];
293572ff6f6SMatthew Dillon 	if (IEEE80211_IS_CHAN_2GHZ(chan)) {
294572ff6f6SMatthew Dillon 		ar5212ModifyRfBuffer(rfReg, ob2GHz, 3, 119, 0);
295572ff6f6SMatthew Dillon 		ar5212ModifyRfBuffer(rfReg, db2GHz, 3, 122, 0);
296572ff6f6SMatthew Dillon 	}
297572ff6f6SMatthew Dillon 	HAL_INI_WRITE_BANK(ah, ar5212Bank0_5111, rfReg, regWrites);
298572ff6f6SMatthew Dillon 
299572ff6f6SMatthew Dillon 	/* Bank 1 Write */
300572ff6f6SMatthew Dillon 	HAL_INI_WRITE_ARRAY(ah, ar5212Bank1_5111, 1, regWrites);
301572ff6f6SMatthew Dillon 
302572ff6f6SMatthew Dillon 	/* Bank 2 Write */
303572ff6f6SMatthew Dillon 	HAL_INI_WRITE_ARRAY(ah, ar5212Bank2_5111, modesIndex, regWrites);
304572ff6f6SMatthew Dillon 
305572ff6f6SMatthew Dillon 	/* Bank 3 Write */
306572ff6f6SMatthew Dillon 	HAL_INI_WRITE_ARRAY(ah, ar5212Bank3_5111, modesIndex, regWrites);
307572ff6f6SMatthew Dillon 
308572ff6f6SMatthew Dillon 	/* Bank 6 Write */
309572ff6f6SMatthew Dillon 	for (i = 0; i < N(ar5212Bank6_5111); i++)
310572ff6f6SMatthew Dillon 		rfReg[i] = ar5212Bank6_5111[i][modesIndex];
311572ff6f6SMatthew Dillon 	if (IEEE80211_IS_CHAN_A(chan)) {	/* NB: CHANNEL_A | CHANNEL_T */
312572ff6f6SMatthew Dillon 		ar5212ModifyRfBuffer(rfReg, ee->ee_cornerCal.pd84, 1, 51, 3);
313572ff6f6SMatthew Dillon 		ar5212ModifyRfBuffer(rfReg, ee->ee_cornerCal.pd90, 1, 45, 3);
314572ff6f6SMatthew Dillon 	}
315572ff6f6SMatthew Dillon 	ar5212ModifyRfBuffer(rfReg, rfPwdXpd, 1, 95, 0);
316572ff6f6SMatthew Dillon 	ar5212ModifyRfBuffer(rfReg, rfXpdGainFixed, 4, 96, 0);
317572ff6f6SMatthew Dillon 	/* Set 5212 OB & DB */
318572ff6f6SMatthew Dillon 	ar5212ModifyRfBuffer(rfReg, tempOB, 3, 104, 0);
319572ff6f6SMatthew Dillon 	ar5212ModifyRfBuffer(rfReg, tempDB, 3, 107, 0);
320572ff6f6SMatthew Dillon 	HAL_INI_WRITE_BANK(ah, ar5212Bank6_5111, rfReg, regWrites);
321572ff6f6SMatthew Dillon 
322572ff6f6SMatthew Dillon 	/* Bank 7 Write */
323572ff6f6SMatthew Dillon 	for (i = 0; i < N(ar5212Bank7_5111); i++)
324572ff6f6SMatthew Dillon 		rfReg[i] = ar5212Bank7_5111[i][modesIndex];
325572ff6f6SMatthew Dillon 	ar5212ModifyRfBuffer(rfReg, gainI, 6, 29, 0);
326572ff6f6SMatthew Dillon 	ar5212ModifyRfBuffer(rfReg, rfPloSel, 1, 4, 0);
327572ff6f6SMatthew Dillon 
328572ff6f6SMatthew Dillon 	if (IEEE80211_IS_CHAN_QUARTER(chan) || IEEE80211_IS_CHAN_HALF(chan)) {
329572ff6f6SMatthew Dillon         	uint32_t	rfWaitI, rfWaitS, rfMaxTime;
330572ff6f6SMatthew Dillon 
331572ff6f6SMatthew Dillon         	rfWaitS = 0x1f;
332572ff6f6SMatthew Dillon         	rfWaitI = (IEEE80211_IS_CHAN_HALF(chan)) ?  0x10 : 0x1f;
333572ff6f6SMatthew Dillon         	rfMaxTime = 3;
334572ff6f6SMatthew Dillon         	ar5212ModifyRfBuffer(rfReg, rfWaitS, 5, 19, 0);
335572ff6f6SMatthew Dillon         	ar5212ModifyRfBuffer(rfReg, rfWaitI, 5, 24, 0);
336572ff6f6SMatthew Dillon         	ar5212ModifyRfBuffer(rfReg, rfMaxTime, 2, 49, 0);
337572ff6f6SMatthew Dillon 
338572ff6f6SMatthew Dillon 	}
339572ff6f6SMatthew Dillon 
340572ff6f6SMatthew Dillon 	HAL_INI_WRITE_BANK(ah, ar5212Bank7_5111, rfReg, regWrites);
341572ff6f6SMatthew Dillon 
342572ff6f6SMatthew Dillon 	/* Now that we have reprogrammed rfgain value, clear the flag. */
343572ff6f6SMatthew Dillon 	ahp->ah_rfgainState = HAL_RFGAIN_INACTIVE;
344572ff6f6SMatthew Dillon 
345572ff6f6SMatthew Dillon 	return AH_TRUE;
346572ff6f6SMatthew Dillon }
347572ff6f6SMatthew Dillon 
348572ff6f6SMatthew Dillon /*
349572ff6f6SMatthew Dillon  * Returns interpolated or the scaled up interpolated value
350572ff6f6SMatthew Dillon  */
351572ff6f6SMatthew Dillon static uint16_t
interpolate(uint16_t target,uint16_t srcLeft,uint16_t srcRight,uint16_t targetLeft,uint16_t targetRight)352572ff6f6SMatthew Dillon interpolate(uint16_t target, uint16_t srcLeft, uint16_t srcRight,
353572ff6f6SMatthew Dillon 	uint16_t targetLeft, uint16_t targetRight)
354572ff6f6SMatthew Dillon {
355572ff6f6SMatthew Dillon 	uint16_t rv;
356572ff6f6SMatthew Dillon 	int16_t lRatio;
357572ff6f6SMatthew Dillon 
358572ff6f6SMatthew Dillon 	/* to get an accurate ratio, always scale, if want to scale, then don't scale back down */
359572ff6f6SMatthew Dillon 	if ((targetLeft * targetRight) == 0)
360572ff6f6SMatthew Dillon 		return 0;
361572ff6f6SMatthew Dillon 
362572ff6f6SMatthew Dillon 	if (srcRight != srcLeft) {
363572ff6f6SMatthew Dillon 		/*
364572ff6f6SMatthew Dillon 		 * Note the ratio always need to be scaled,
365572ff6f6SMatthew Dillon 		 * since it will be a fraction.
366572ff6f6SMatthew Dillon 		 */
367572ff6f6SMatthew Dillon 		lRatio = (target - srcLeft) * EEP_SCALE / (srcRight - srcLeft);
368572ff6f6SMatthew Dillon 		if (lRatio < 0) {
369572ff6f6SMatthew Dillon 		    /* Return as Left target if value would be negative */
370572ff6f6SMatthew Dillon 		    rv = targetLeft;
371572ff6f6SMatthew Dillon 		} else if (lRatio > EEP_SCALE) {
372572ff6f6SMatthew Dillon 		    /* Return as Right target if Ratio is greater than 100% (SCALE) */
373572ff6f6SMatthew Dillon 		    rv = targetRight;
374572ff6f6SMatthew Dillon 		} else {
375572ff6f6SMatthew Dillon 			rv = (lRatio * targetRight + (EEP_SCALE - lRatio) *
376572ff6f6SMatthew Dillon 					targetLeft) / EEP_SCALE;
377572ff6f6SMatthew Dillon 		}
378572ff6f6SMatthew Dillon 	} else {
379572ff6f6SMatthew Dillon 		rv = targetLeft;
380572ff6f6SMatthew Dillon 	}
381572ff6f6SMatthew Dillon 	return rv;
382572ff6f6SMatthew Dillon }
383572ff6f6SMatthew Dillon 
384572ff6f6SMatthew Dillon /*
385572ff6f6SMatthew Dillon  * Read the transmit power levels from the structures taken from EEPROM
386572ff6f6SMatthew Dillon  * Interpolate read transmit power values for this channel
387572ff6f6SMatthew Dillon  * Organize the transmit power values into a table for writing into the hardware
388572ff6f6SMatthew Dillon  */
389572ff6f6SMatthew Dillon static HAL_BOOL
ar5111SetPowerTable(struct ath_hal * ah,int16_t * pMinPower,int16_t * pMaxPower,const struct ieee80211_channel * chan,uint16_t * rfXpdGain)390572ff6f6SMatthew Dillon ar5111SetPowerTable(struct ath_hal *ah,
391572ff6f6SMatthew Dillon 	int16_t *pMinPower, int16_t *pMaxPower,
392572ff6f6SMatthew Dillon 	const struct ieee80211_channel *chan,
393572ff6f6SMatthew Dillon 	uint16_t *rfXpdGain)
394572ff6f6SMatthew Dillon {
395572ff6f6SMatthew Dillon 	uint16_t freq = ath_hal_gethwchannel(ah, chan);
396572ff6f6SMatthew Dillon 	struct ath_hal_5212 *ahp = AH5212(ah);
397572ff6f6SMatthew Dillon 	const HAL_EEPROM *ee = AH_PRIVATE(ah)->ah_eeprom;
398572ff6f6SMatthew Dillon 	FULL_PCDAC_STRUCT pcdacStruct;
399572ff6f6SMatthew Dillon 	int i, j;
400572ff6f6SMatthew Dillon 
401572ff6f6SMatthew Dillon 	uint16_t     *pPcdacValues;
402572ff6f6SMatthew Dillon 	int16_t      *pScaledUpDbm;
403572ff6f6SMatthew Dillon 	int16_t      minScaledPwr;
404572ff6f6SMatthew Dillon 	int16_t      maxScaledPwr;
405572ff6f6SMatthew Dillon 	int16_t      pwr;
406572ff6f6SMatthew Dillon 	uint16_t     pcdacMin = 0;
407572ff6f6SMatthew Dillon 	uint16_t     pcdacMax = PCDAC_STOP;
408572ff6f6SMatthew Dillon 	uint16_t     pcdacTableIndex;
409572ff6f6SMatthew Dillon 	uint16_t     scaledPcdac;
410572ff6f6SMatthew Dillon 	PCDACS_EEPROM *pSrcStruct;
411572ff6f6SMatthew Dillon 	PCDACS_EEPROM eepromPcdacs;
412572ff6f6SMatthew Dillon 
413572ff6f6SMatthew Dillon 	/* setup the pcdac struct to point to the correct info, based on mode */
414572ff6f6SMatthew Dillon 	switch (chan->ic_flags & IEEE80211_CHAN_ALLTURBOFULL) {
415572ff6f6SMatthew Dillon 	case IEEE80211_CHAN_A:
416572ff6f6SMatthew Dillon 	case IEEE80211_CHAN_ST:
417572ff6f6SMatthew Dillon 		eepromPcdacs.numChannels     = ee->ee_numChannels11a;
418572ff6f6SMatthew Dillon 		eepromPcdacs.pChannelList    = ee->ee_channels11a;
419572ff6f6SMatthew Dillon 		eepromPcdacs.pDataPerChannel = ee->ee_dataPerChannel11a;
420572ff6f6SMatthew Dillon 		break;
421572ff6f6SMatthew Dillon 	case IEEE80211_CHAN_B:
422572ff6f6SMatthew Dillon 		eepromPcdacs.numChannels     = ee->ee_numChannels2_4;
423572ff6f6SMatthew Dillon 		eepromPcdacs.pChannelList    = ee->ee_channels11b;
424572ff6f6SMatthew Dillon 		eepromPcdacs.pDataPerChannel = ee->ee_dataPerChannel11b;
425572ff6f6SMatthew Dillon 		break;
426572ff6f6SMatthew Dillon 	case IEEE80211_CHAN_G:
427572ff6f6SMatthew Dillon 	case IEEE80211_CHAN_108G:
428572ff6f6SMatthew Dillon 		eepromPcdacs.numChannels     = ee->ee_numChannels2_4;
429572ff6f6SMatthew Dillon 		eepromPcdacs.pChannelList    = ee->ee_channels11g;
430572ff6f6SMatthew Dillon 		eepromPcdacs.pDataPerChannel = ee->ee_dataPerChannel11g;
431572ff6f6SMatthew Dillon 		break;
432572ff6f6SMatthew Dillon 	default:
433572ff6f6SMatthew Dillon 		HALDEBUG(ah, HAL_DEBUG_ANY, "%s: invalid channel flags 0x%x\n",
434572ff6f6SMatthew Dillon 		    __func__, chan->ic_flags);
435572ff6f6SMatthew Dillon 		return AH_FALSE;
436572ff6f6SMatthew Dillon 	}
437572ff6f6SMatthew Dillon 
438572ff6f6SMatthew Dillon 	pSrcStruct = &eepromPcdacs;
439572ff6f6SMatthew Dillon 
440572ff6f6SMatthew Dillon 	OS_MEMZERO(&pcdacStruct, sizeof(pcdacStruct));
441572ff6f6SMatthew Dillon 	pPcdacValues = pcdacStruct.PcdacValues;
442572ff6f6SMatthew Dillon 	pScaledUpDbm = pcdacStruct.PwrValues;
443572ff6f6SMatthew Dillon 
444572ff6f6SMatthew Dillon 	/* Initialize the pcdacs to dBM structs pcdacs to be 1 to 63 */
445572ff6f6SMatthew Dillon 	for (i = PCDAC_START, j = 0; i <= PCDAC_STOP; i+= PCDAC_STEP, j++)
446572ff6f6SMatthew Dillon 		pPcdacValues[j] = i;
447572ff6f6SMatthew Dillon 
448572ff6f6SMatthew Dillon 	pcdacStruct.numPcdacValues = j;
449572ff6f6SMatthew Dillon 	pcdacStruct.pcdacMin = PCDAC_START;
450572ff6f6SMatthew Dillon 	pcdacStruct.pcdacMax = PCDAC_STOP;
451572ff6f6SMatthew Dillon 
452572ff6f6SMatthew Dillon 	/* Fill out the power values for this channel */
453572ff6f6SMatthew Dillon 	for (j = 0; j < pcdacStruct.numPcdacValues; j++ )
454572ff6f6SMatthew Dillon 		pScaledUpDbm[j] = ar5212GetScaledPower(freq,
455572ff6f6SMatthew Dillon 			pPcdacValues[j], pSrcStruct);
456572ff6f6SMatthew Dillon 
457572ff6f6SMatthew Dillon 	/* Now scale the pcdac values to fit in the 64 entry power table */
458572ff6f6SMatthew Dillon 	minScaledPwr = pScaledUpDbm[0];
459572ff6f6SMatthew Dillon 	maxScaledPwr = pScaledUpDbm[pcdacStruct.numPcdacValues - 1];
460572ff6f6SMatthew Dillon 
461572ff6f6SMatthew Dillon 	/* find minimum and make monotonic */
462572ff6f6SMatthew Dillon 	for (j = 0; j < pcdacStruct.numPcdacValues; j++) {
463572ff6f6SMatthew Dillon 		if (minScaledPwr >= pScaledUpDbm[j]) {
464572ff6f6SMatthew Dillon 			minScaledPwr = pScaledUpDbm[j];
465572ff6f6SMatthew Dillon 			pcdacMin = j;
466572ff6f6SMatthew Dillon 		}
467572ff6f6SMatthew Dillon 		/*
468572ff6f6SMatthew Dillon 		 * Make the full_hsh monotonically increasing otherwise
469572ff6f6SMatthew Dillon 		 * interpolation algorithm will get fooled gotta start
470572ff6f6SMatthew Dillon 		 * working from the top, hence i = 63 - j.
471572ff6f6SMatthew Dillon 		 */
472572ff6f6SMatthew Dillon 		i = (uint16_t)(pcdacStruct.numPcdacValues - 1 - j);
473572ff6f6SMatthew Dillon 		if (i == 0)
474572ff6f6SMatthew Dillon 			break;
475572ff6f6SMatthew Dillon 		if (pScaledUpDbm[i-1] > pScaledUpDbm[i]) {
476572ff6f6SMatthew Dillon 			/*
477572ff6f6SMatthew Dillon 			 * It could be a glitch, so make the power for
478572ff6f6SMatthew Dillon 			 * this pcdac the same as the power from the
479572ff6f6SMatthew Dillon 			 * next highest pcdac.
480572ff6f6SMatthew Dillon 			 */
481572ff6f6SMatthew Dillon 			pScaledUpDbm[i - 1] = pScaledUpDbm[i];
482572ff6f6SMatthew Dillon 		}
483572ff6f6SMatthew Dillon 	}
484572ff6f6SMatthew Dillon 
485572ff6f6SMatthew Dillon 	for (j = 0; j < pcdacStruct.numPcdacValues; j++)
486572ff6f6SMatthew Dillon 		if (maxScaledPwr < pScaledUpDbm[j]) {
487572ff6f6SMatthew Dillon 			maxScaledPwr = pScaledUpDbm[j];
488572ff6f6SMatthew Dillon 			pcdacMax = j;
489572ff6f6SMatthew Dillon 		}
490572ff6f6SMatthew Dillon 
491572ff6f6SMatthew Dillon 	/* Find the first power level with a pcdac */
492*ed183f8cSSascha Wildner 	pwr = (uint16_t)(rounddown(minScaledPwr - PWR_MIN + PWR_STEP / 2, PWR_STEP) + PWR_MIN);
493572ff6f6SMatthew Dillon 
494572ff6f6SMatthew Dillon 	/* Write all the first pcdac entries based off the pcdacMin */
495572ff6f6SMatthew Dillon 	pcdacTableIndex = 0;
496572ff6f6SMatthew Dillon 	for (i = 0; i < (2 * (pwr - PWR_MIN) / EEP_SCALE + 1); i++) {
497572ff6f6SMatthew Dillon 		HALASSERT(pcdacTableIndex < PWR_TABLE_SIZE);
498572ff6f6SMatthew Dillon 		ahp->ah_pcdacTable[pcdacTableIndex++] = pcdacMin;
499572ff6f6SMatthew Dillon 	}
500572ff6f6SMatthew Dillon 
501572ff6f6SMatthew Dillon 	i = 0;
502572ff6f6SMatthew Dillon 	while (pwr < pScaledUpDbm[pcdacStruct.numPcdacValues - 1] &&
503572ff6f6SMatthew Dillon 	    pcdacTableIndex < PWR_TABLE_SIZE) {
504572ff6f6SMatthew Dillon 		pwr += PWR_STEP;
505572ff6f6SMatthew Dillon 		/* stop if dbM > max_power_possible */
506572ff6f6SMatthew Dillon 		while (pwr < pScaledUpDbm[pcdacStruct.numPcdacValues - 1] &&
507572ff6f6SMatthew Dillon 		       (pwr - pScaledUpDbm[i])*(pwr - pScaledUpDbm[i+1]) > 0)
508572ff6f6SMatthew Dillon 			i++;
509572ff6f6SMatthew Dillon 		/* scale by 2 and add 1 to enable round up or down as needed */
510572ff6f6SMatthew Dillon 		scaledPcdac = (uint16_t)(interpolate(pwr,
511572ff6f6SMatthew Dillon 			pScaledUpDbm[i], pScaledUpDbm[i + 1],
512572ff6f6SMatthew Dillon 			(uint16_t)(pPcdacValues[i] * 2),
513572ff6f6SMatthew Dillon 			(uint16_t)(pPcdacValues[i + 1] * 2)) + 1);
514572ff6f6SMatthew Dillon 
515572ff6f6SMatthew Dillon 		HALASSERT(pcdacTableIndex < PWR_TABLE_SIZE);
516572ff6f6SMatthew Dillon 		ahp->ah_pcdacTable[pcdacTableIndex] = scaledPcdac / 2;
517572ff6f6SMatthew Dillon 		if (ahp->ah_pcdacTable[pcdacTableIndex] > pcdacMax)
518572ff6f6SMatthew Dillon 			ahp->ah_pcdacTable[pcdacTableIndex] = pcdacMax;
519572ff6f6SMatthew Dillon 		pcdacTableIndex++;
520572ff6f6SMatthew Dillon 	}
521572ff6f6SMatthew Dillon 
522572ff6f6SMatthew Dillon 	/* Write all the last pcdac entries based off the last valid pcdac */
523572ff6f6SMatthew Dillon 	while (pcdacTableIndex < PWR_TABLE_SIZE) {
524572ff6f6SMatthew Dillon 		ahp->ah_pcdacTable[pcdacTableIndex] =
525572ff6f6SMatthew Dillon 			ahp->ah_pcdacTable[pcdacTableIndex - 1];
526572ff6f6SMatthew Dillon 		pcdacTableIndex++;
527572ff6f6SMatthew Dillon 	}
528572ff6f6SMatthew Dillon 
529572ff6f6SMatthew Dillon 	/* No power table adjustment for 5111 */
530572ff6f6SMatthew Dillon 	ahp->ah_txPowerIndexOffset = 0;
531572ff6f6SMatthew Dillon 
532572ff6f6SMatthew Dillon 	return AH_TRUE;
533572ff6f6SMatthew Dillon }
534572ff6f6SMatthew Dillon 
535572ff6f6SMatthew Dillon /*
536572ff6f6SMatthew Dillon  * Get or interpolate the pcdac value from the calibrated data.
537572ff6f6SMatthew Dillon  */
538572ff6f6SMatthew Dillon static uint16_t
ar5212GetScaledPower(uint16_t channel,uint16_t pcdacValue,const PCDACS_EEPROM * pSrcStruct)539572ff6f6SMatthew Dillon ar5212GetScaledPower(uint16_t channel, uint16_t pcdacValue,
540572ff6f6SMatthew Dillon 	const PCDACS_EEPROM *pSrcStruct)
541572ff6f6SMatthew Dillon {
542572ff6f6SMatthew Dillon 	uint16_t powerValue;
543572ff6f6SMatthew Dillon 	uint16_t lFreq, rFreq;		/* left and right frequency values */
544572ff6f6SMatthew Dillon 	uint16_t llPcdac, ulPcdac;	/* lower and upper left pcdac values */
545572ff6f6SMatthew Dillon 	uint16_t lrPcdac, urPcdac;	/* lower and upper right pcdac values */
546572ff6f6SMatthew Dillon 	uint16_t lPwr, uPwr;		/* lower and upper temp pwr values */
547572ff6f6SMatthew Dillon 	uint16_t lScaledPwr, rScaledPwr; /* left and right scaled power */
548572ff6f6SMatthew Dillon 
549572ff6f6SMatthew Dillon 	if (ar5212FindValueInList(channel, pcdacValue, pSrcStruct, &powerValue)) {
550572ff6f6SMatthew Dillon 		/* value was copied from srcStruct */
551572ff6f6SMatthew Dillon 		return powerValue;
552572ff6f6SMatthew Dillon 	}
553572ff6f6SMatthew Dillon 
554572ff6f6SMatthew Dillon 	ar5212GetLowerUpperValues(channel,
555572ff6f6SMatthew Dillon 		pSrcStruct->pChannelList, pSrcStruct->numChannels,
556572ff6f6SMatthew Dillon 		&lFreq, &rFreq);
557572ff6f6SMatthew Dillon 	ar5212GetLowerUpperPcdacs(pcdacValue,
558572ff6f6SMatthew Dillon 		lFreq, pSrcStruct, &llPcdac, &ulPcdac);
559572ff6f6SMatthew Dillon 	ar5212GetLowerUpperPcdacs(pcdacValue,
560572ff6f6SMatthew Dillon 		rFreq, pSrcStruct, &lrPcdac, &urPcdac);
561572ff6f6SMatthew Dillon 
56257e09377SMatthew Dillon 	lPwr = 0;	/* avoid gcc warnings */
56357e09377SMatthew Dillon 	uPwr = 0;	/* avoid gcc warnings */
564572ff6f6SMatthew Dillon 	/* get the power index for the pcdac value */
565572ff6f6SMatthew Dillon 	ar5212FindValueInList(lFreq, llPcdac, pSrcStruct, &lPwr);
566572ff6f6SMatthew Dillon 	ar5212FindValueInList(lFreq, ulPcdac, pSrcStruct, &uPwr);
567572ff6f6SMatthew Dillon 	lScaledPwr = interpolate(pcdacValue, llPcdac, ulPcdac, lPwr, uPwr);
568572ff6f6SMatthew Dillon 
569572ff6f6SMatthew Dillon 	ar5212FindValueInList(rFreq, lrPcdac, pSrcStruct, &lPwr);
570572ff6f6SMatthew Dillon 	ar5212FindValueInList(rFreq, urPcdac, pSrcStruct, &uPwr);
571572ff6f6SMatthew Dillon 	rScaledPwr = interpolate(pcdacValue, lrPcdac, urPcdac, lPwr, uPwr);
572572ff6f6SMatthew Dillon 
573572ff6f6SMatthew Dillon 	return interpolate(channel, lFreq, rFreq, lScaledPwr, rScaledPwr);
574572ff6f6SMatthew Dillon }
575572ff6f6SMatthew Dillon 
576572ff6f6SMatthew Dillon /*
577572ff6f6SMatthew Dillon  * Find the value from the calibrated source data struct
578572ff6f6SMatthew Dillon  */
579572ff6f6SMatthew Dillon static HAL_BOOL
ar5212FindValueInList(uint16_t channel,uint16_t pcdacValue,const PCDACS_EEPROM * pSrcStruct,uint16_t * powerValue)580572ff6f6SMatthew Dillon ar5212FindValueInList(uint16_t channel, uint16_t pcdacValue,
581572ff6f6SMatthew Dillon 	const PCDACS_EEPROM *pSrcStruct, uint16_t *powerValue)
582572ff6f6SMatthew Dillon {
583572ff6f6SMatthew Dillon 	const DATA_PER_CHANNEL *pChannelData = pSrcStruct->pDataPerChannel;
584572ff6f6SMatthew Dillon 	int i;
585572ff6f6SMatthew Dillon 
586572ff6f6SMatthew Dillon 	for (i = 0; i < pSrcStruct->numChannels; i++ ) {
587572ff6f6SMatthew Dillon 		if (pChannelData->channelValue == channel) {
588572ff6f6SMatthew Dillon 			const uint16_t* pPcdac = pChannelData->PcdacValues;
589572ff6f6SMatthew Dillon 			int j;
590572ff6f6SMatthew Dillon 
591572ff6f6SMatthew Dillon 			for (j = 0; j < pChannelData->numPcdacValues; j++ ) {
592572ff6f6SMatthew Dillon 				if (*pPcdac == pcdacValue) {
593572ff6f6SMatthew Dillon 					*powerValue = pChannelData->PwrValues[j];
594572ff6f6SMatthew Dillon 					return AH_TRUE;
595572ff6f6SMatthew Dillon 				}
596572ff6f6SMatthew Dillon 				pPcdac++;
597572ff6f6SMatthew Dillon 			}
598572ff6f6SMatthew Dillon 		}
599572ff6f6SMatthew Dillon 		pChannelData++;
600572ff6f6SMatthew Dillon 	}
601572ff6f6SMatthew Dillon 	return AH_FALSE;
602572ff6f6SMatthew Dillon }
603572ff6f6SMatthew Dillon 
604572ff6f6SMatthew Dillon /*
605572ff6f6SMatthew Dillon  * Get the upper and lower pcdac given the channel and the pcdac
606572ff6f6SMatthew Dillon  * used in the search
607572ff6f6SMatthew Dillon  */
608572ff6f6SMatthew Dillon static void
ar5212GetLowerUpperPcdacs(uint16_t pcdac,uint16_t channel,const PCDACS_EEPROM * pSrcStruct,uint16_t * pLowerPcdac,uint16_t * pUpperPcdac)609572ff6f6SMatthew Dillon ar5212GetLowerUpperPcdacs(uint16_t pcdac, uint16_t channel,
610572ff6f6SMatthew Dillon 	const PCDACS_EEPROM *pSrcStruct,
611572ff6f6SMatthew Dillon 	uint16_t *pLowerPcdac, uint16_t *pUpperPcdac)
612572ff6f6SMatthew Dillon {
613572ff6f6SMatthew Dillon 	const DATA_PER_CHANNEL *pChannelData = pSrcStruct->pDataPerChannel;
614572ff6f6SMatthew Dillon 	int i;
615572ff6f6SMatthew Dillon 
616572ff6f6SMatthew Dillon 	/* Find the channel information */
617572ff6f6SMatthew Dillon 	for (i = 0; i < pSrcStruct->numChannels; i++) {
618572ff6f6SMatthew Dillon 		if (pChannelData->channelValue == channel)
619572ff6f6SMatthew Dillon 			break;
620572ff6f6SMatthew Dillon 		pChannelData++;
621572ff6f6SMatthew Dillon 	}
622572ff6f6SMatthew Dillon 	ar5212GetLowerUpperValues(pcdac, pChannelData->PcdacValues,
623572ff6f6SMatthew Dillon 		      pChannelData->numPcdacValues,
624572ff6f6SMatthew Dillon 		      pLowerPcdac, pUpperPcdac);
625572ff6f6SMatthew Dillon }
626572ff6f6SMatthew Dillon 
627572ff6f6SMatthew Dillon static HAL_BOOL
ar5111GetChannelMaxMinPower(struct ath_hal * ah,const struct ieee80211_channel * chan,int16_t * maxPow,int16_t * minPow)628572ff6f6SMatthew Dillon ar5111GetChannelMaxMinPower(struct ath_hal *ah,
629572ff6f6SMatthew Dillon 	const struct ieee80211_channel *chan,
630572ff6f6SMatthew Dillon 	int16_t *maxPow, int16_t *minPow)
631572ff6f6SMatthew Dillon {
632572ff6f6SMatthew Dillon 	/* XXX - Get 5111 power limits! */
633572ff6f6SMatthew Dillon 	/* NB: caller will cope */
634572ff6f6SMatthew Dillon 	return AH_FALSE;
635572ff6f6SMatthew Dillon }
636572ff6f6SMatthew Dillon 
637572ff6f6SMatthew Dillon /*
638572ff6f6SMatthew Dillon  * Adjust NF based on statistical values for 5GHz frequencies.
639572ff6f6SMatthew Dillon  */
640572ff6f6SMatthew Dillon static int16_t
ar5111GetNfAdjust(struct ath_hal * ah,const HAL_CHANNEL_INTERNAL * c)641572ff6f6SMatthew Dillon ar5111GetNfAdjust(struct ath_hal *ah, const HAL_CHANNEL_INTERNAL *c)
642572ff6f6SMatthew Dillon {
643572ff6f6SMatthew Dillon 	static const struct {
644572ff6f6SMatthew Dillon 		uint16_t freqLow;
645572ff6f6SMatthew Dillon 		int16_t	  adjust;
646572ff6f6SMatthew Dillon 	} adjust5111[] = {
647572ff6f6SMatthew Dillon 		{ 5790,	6 },	/* NB: ordered high -> low */
648572ff6f6SMatthew Dillon 		{ 5730, 4 },
649572ff6f6SMatthew Dillon 		{ 5690, 3 },
650572ff6f6SMatthew Dillon 		{ 5660, 2 },
651572ff6f6SMatthew Dillon 		{ 5610, 1 },
652572ff6f6SMatthew Dillon 		{ 5530, 0 },
653572ff6f6SMatthew Dillon 		{ 5450, 0 },
654572ff6f6SMatthew Dillon 		{ 5379, 1 },
655572ff6f6SMatthew Dillon 		{ 5209, 3 },
656572ff6f6SMatthew Dillon 		{ 3000, 5 },
657572ff6f6SMatthew Dillon 		{    0, 0 },
658572ff6f6SMatthew Dillon 	};
659572ff6f6SMatthew Dillon 	int i;
660572ff6f6SMatthew Dillon 
661572ff6f6SMatthew Dillon 	for (i = 0; c->channel <= adjust5111[i].freqLow; i++)
662572ff6f6SMatthew Dillon 		;
663572ff6f6SMatthew Dillon 	return adjust5111[i].adjust;
664572ff6f6SMatthew Dillon }
665572ff6f6SMatthew Dillon 
666572ff6f6SMatthew Dillon /*
667572ff6f6SMatthew Dillon  * Free memory for analog bank scratch buffers
668572ff6f6SMatthew Dillon  */
669572ff6f6SMatthew Dillon static void
ar5111RfDetach(struct ath_hal * ah)670572ff6f6SMatthew Dillon ar5111RfDetach(struct ath_hal *ah)
671572ff6f6SMatthew Dillon {
672572ff6f6SMatthew Dillon 	struct ath_hal_5212 *ahp = AH5212(ah);
673572ff6f6SMatthew Dillon 
674572ff6f6SMatthew Dillon 	HALASSERT(ahp->ah_rfHal != AH_NULL);
675572ff6f6SMatthew Dillon 	ath_hal_free(ahp->ah_rfHal);
676572ff6f6SMatthew Dillon 	ahp->ah_rfHal = AH_NULL;
677572ff6f6SMatthew Dillon }
678572ff6f6SMatthew Dillon 
679572ff6f6SMatthew Dillon /*
680572ff6f6SMatthew Dillon  * Allocate memory for analog bank scratch buffers
681572ff6f6SMatthew Dillon  * Scratch Buffer will be reinitialized every reset so no need to zero now
682572ff6f6SMatthew Dillon  */
683572ff6f6SMatthew Dillon static HAL_BOOL
ar5111RfAttach(struct ath_hal * ah,HAL_STATUS * status)684572ff6f6SMatthew Dillon ar5111RfAttach(struct ath_hal *ah, HAL_STATUS *status)
685572ff6f6SMatthew Dillon {
686572ff6f6SMatthew Dillon 	struct ath_hal_5212 *ahp = AH5212(ah);
687572ff6f6SMatthew Dillon 	struct ar5111State *priv;
688572ff6f6SMatthew Dillon 
689572ff6f6SMatthew Dillon 	HALASSERT(ah->ah_magic == AR5212_MAGIC);
690572ff6f6SMatthew Dillon 
691572ff6f6SMatthew Dillon 	HALASSERT(ahp->ah_rfHal == AH_NULL);
692572ff6f6SMatthew Dillon 	priv = ath_hal_malloc(sizeof(struct ar5111State));
693572ff6f6SMatthew Dillon 	if (priv == AH_NULL) {
694572ff6f6SMatthew Dillon 		HALDEBUG(ah, HAL_DEBUG_ANY,
695572ff6f6SMatthew Dillon 		    "%s: cannot allocate private state\n", __func__);
696572ff6f6SMatthew Dillon 		*status = HAL_ENOMEM;		/* XXX */
697572ff6f6SMatthew Dillon 		return AH_FALSE;
698572ff6f6SMatthew Dillon 	}
699572ff6f6SMatthew Dillon 	priv->base.rfDetach		= ar5111RfDetach;
700572ff6f6SMatthew Dillon 	priv->base.writeRegs		= ar5111WriteRegs;
701572ff6f6SMatthew Dillon 	priv->base.getRfBank		= ar5111GetRfBank;
702572ff6f6SMatthew Dillon 	priv->base.setChannel		= ar5111SetChannel;
703572ff6f6SMatthew Dillon 	priv->base.setRfRegs		= ar5111SetRfRegs;
704572ff6f6SMatthew Dillon 	priv->base.setPowerTable	= ar5111SetPowerTable;
705572ff6f6SMatthew Dillon 	priv->base.getChannelMaxMinPower = ar5111GetChannelMaxMinPower;
706572ff6f6SMatthew Dillon 	priv->base.getNfAdjust		= ar5111GetNfAdjust;
707572ff6f6SMatthew Dillon 
708572ff6f6SMatthew Dillon 	ahp->ah_pcdacTable = priv->pcdacTable;
709572ff6f6SMatthew Dillon 	ahp->ah_pcdacTableSize = sizeof(priv->pcdacTable);
710572ff6f6SMatthew Dillon 	ahp->ah_rfHal = &priv->base;
711572ff6f6SMatthew Dillon 
712572ff6f6SMatthew Dillon 	return AH_TRUE;
713572ff6f6SMatthew Dillon }
714572ff6f6SMatthew Dillon 
715572ff6f6SMatthew Dillon static HAL_BOOL
ar5111Probe(struct ath_hal * ah)716572ff6f6SMatthew Dillon ar5111Probe(struct ath_hal *ah)
717572ff6f6SMatthew Dillon {
718572ff6f6SMatthew Dillon 	return IS_RAD5111(ah);
719572ff6f6SMatthew Dillon }
720572ff6f6SMatthew Dillon AH_RF(RF5111, ar5111Probe, ar5111RfAttach);
721