xref: /freebsd/sys/dev/ath/ath_hal/ar5212/ar2413.c (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1 /*-
2  * SPDX-License-Identifier: ISC
3  *
4  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
5  * Copyright (c) 2002-2008 Atheros Communications, Inc.
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 #include "opt_ah.h"
20 
21 #include "ah.h"
22 #include "ah_internal.h"
23 
24 #include "ar5212/ar5212.h"
25 #include "ar5212/ar5212reg.h"
26 #include "ar5212/ar5212phy.h"
27 
28 #include "ah_eeprom_v3.h"
29 
30 #define AH_5212_2413
31 #include "ar5212/ar5212.ini"
32 
33 #define	N(a)	(sizeof(a)/sizeof(a[0]))
34 
35 struct ar2413State {
36 	RF_HAL_FUNCS	base;		/* public state, must be first */
37 	uint16_t	pcdacTable[PWR_TABLE_SIZE_2413];
38 
39 	uint32_t	Bank1Data[N(ar5212Bank1_2413)];
40 	uint32_t	Bank2Data[N(ar5212Bank2_2413)];
41 	uint32_t	Bank3Data[N(ar5212Bank3_2413)];
42 	uint32_t	Bank6Data[N(ar5212Bank6_2413)];
43 	uint32_t	Bank7Data[N(ar5212Bank7_2413)];
44 
45 	/*
46 	 * Private state for reduced stack usage.
47 	 */
48 	/* filled out Vpd table for all pdGains (chanL) */
49 	uint16_t vpdTable_L[MAX_NUM_PDGAINS_PER_CHANNEL]
50 			    [MAX_PWR_RANGE_IN_HALF_DB];
51 	/* filled out Vpd table for all pdGains (chanR) */
52 	uint16_t vpdTable_R[MAX_NUM_PDGAINS_PER_CHANNEL]
53 			    [MAX_PWR_RANGE_IN_HALF_DB];
54 	/* filled out Vpd table for all pdGains (interpolated) */
55 	uint16_t vpdTable_I[MAX_NUM_PDGAINS_PER_CHANNEL]
56 			    [MAX_PWR_RANGE_IN_HALF_DB];
57 };
58 #define	AR2413(ah)	((struct ar2413State *) AH5212(ah)->ah_rfHal)
59 
60 extern	void ar5212ModifyRfBuffer(uint32_t *rfBuf, uint32_t reg32,
61 		uint32_t numBits, uint32_t firstBit, uint32_t column);
62 
63 static void
64 ar2413WriteRegs(struct ath_hal *ah, u_int modesIndex, u_int freqIndex,
65 	int writes)
66 {
67 	HAL_INI_WRITE_ARRAY(ah, ar5212Modes_2413, modesIndex, writes);
68 	HAL_INI_WRITE_ARRAY(ah, ar5212Common_2413, 1, writes);
69 	HAL_INI_WRITE_ARRAY(ah, ar5212BB_RfGain_2413, freqIndex, writes);
70 }
71 
72 /*
73  * Take the MHz channel value and set the Channel value
74  *
75  * ASSUMES: Writes enabled to analog bus
76  */
77 static HAL_BOOL
78 ar2413SetChannel(struct ath_hal *ah, const struct ieee80211_channel *chan)
79 {
80 	uint16_t freq = ath_hal_gethwchannel(ah, chan);
81 	uint32_t channelSel  = 0;
82 	uint32_t bModeSynth  = 0;
83 	uint32_t aModeRefSel = 0;
84 	uint32_t reg32       = 0;
85 
86 	OS_MARK(ah, AH_MARK_SETCHANNEL, freq);
87 
88 	if (freq < 4800) {
89 		uint32_t txctl;
90 
91 		if (((freq - 2192) % 5) == 0) {
92 			channelSel = ((freq - 672) * 2 - 3040)/10;
93 			bModeSynth = 0;
94 		} else if (((freq - 2224) % 5) == 0) {
95 			channelSel = ((freq - 704) * 2 - 3040) / 10;
96 			bModeSynth = 1;
97 		} else {
98 			HALDEBUG(ah, HAL_DEBUG_ANY,
99 			    "%s: invalid channel %u MHz\n",
100 			    __func__, freq);
101 			return AH_FALSE;
102 		}
103 
104 		channelSel = (channelSel << 2) & 0xff;
105 		channelSel = ath_hal_reverseBits(channelSel, 8);
106 
107 		txctl = OS_REG_READ(ah, AR_PHY_CCK_TX_CTRL);
108 		if (freq == 2484) {
109 			/* Enable channel spreading for channel 14 */
110 			OS_REG_WRITE(ah, AR_PHY_CCK_TX_CTRL,
111 				txctl | AR_PHY_CCK_TX_CTRL_JAPAN);
112 		} else {
113 			OS_REG_WRITE(ah, AR_PHY_CCK_TX_CTRL,
114 				txctl &~ AR_PHY_CCK_TX_CTRL_JAPAN);
115 		}
116 	} else if (((freq % 5) == 2) && (freq <= 5435)) {
117 		freq = freq - 2; /* Align to even 5MHz raster */
118 		channelSel = ath_hal_reverseBits(
119 			(uint32_t)(((freq - 4800)*10)/25 + 1), 8);
120             	aModeRefSel = ath_hal_reverseBits(0, 2);
121 	} else if ((freq % 20) == 0 && freq >= 5120) {
122 		channelSel = ath_hal_reverseBits(
123 			((freq - 4800) / 20 << 2), 8);
124 		aModeRefSel = ath_hal_reverseBits(3, 2);
125 	} else if ((freq % 10) == 0) {
126 		channelSel = ath_hal_reverseBits(
127 			((freq - 4800) / 10 << 1), 8);
128 		aModeRefSel = ath_hal_reverseBits(2, 2);
129 	} else if ((freq % 5) == 0) {
130 		channelSel = ath_hal_reverseBits(
131 			(freq - 4800) / 5, 8);
132 		aModeRefSel = ath_hal_reverseBits(1, 2);
133 	} else {
134 		HALDEBUG(ah, HAL_DEBUG_ANY, "%s: invalid channel %u MHz\n",
135 		    __func__, freq);
136 		return AH_FALSE;
137 	}
138 
139 	reg32 = (channelSel << 4) | (aModeRefSel << 2) | (bModeSynth << 1) |
140 			(1 << 12) | 0x1;
141 	OS_REG_WRITE(ah, AR_PHY(0x27), reg32 & 0xff);
142 
143 	reg32 >>= 8;
144 	OS_REG_WRITE(ah, AR_PHY(0x36), reg32 & 0x7f);
145 
146 	AH_PRIVATE(ah)->ah_curchan = chan;
147 
148 	return AH_TRUE;
149 }
150 
151 /*
152  * Reads EEPROM header info from device structure and programs
153  * all rf registers
154  *
155  * REQUIRES: Access to the analog rf device
156  */
157 static HAL_BOOL
158 ar2413SetRfRegs(struct ath_hal *ah,
159 	const struct ieee80211_channel *chan,
160 	uint16_t modesIndex, uint16_t *rfXpdGain)
161 {
162 #define	RF_BANK_SETUP(_priv, _ix, _col) do {				    \
163 	int i;								    \
164 	for (i = 0; i < N(ar5212Bank##_ix##_2413); i++)			    \
165 		(_priv)->Bank##_ix##Data[i] = ar5212Bank##_ix##_2413[i][_col];\
166 } while (0)
167 	struct ath_hal_5212 *ahp = AH5212(ah);
168 	const HAL_EEPROM *ee = AH_PRIVATE(ah)->ah_eeprom;
169 	uint16_t ob2GHz = 0, db2GHz = 0;
170 	struct ar2413State *priv = AR2413(ah);
171 	int regWrites = 0;
172 
173 	HALDEBUG(ah, HAL_DEBUG_RFPARAM, "%s: chan %u/0x%x modesIndex %u\n",
174 	    __func__, chan->ic_freq, chan->ic_flags, modesIndex);
175 
176 	HALASSERT(priv);
177 
178 	/* Setup rf parameters */
179 	if (IEEE80211_IS_CHAN_B(chan)) {
180 		ob2GHz = ee->ee_obFor24;
181 		db2GHz = ee->ee_dbFor24;
182 	} else {
183 		ob2GHz = ee->ee_obFor24g;
184 		db2GHz = ee->ee_dbFor24g;
185 	}
186 
187 	/* Bank 1 Write */
188 	RF_BANK_SETUP(priv, 1, 1);
189 
190 	/* Bank 2 Write */
191 	RF_BANK_SETUP(priv, 2, modesIndex);
192 
193 	/* Bank 3 Write */
194 	RF_BANK_SETUP(priv, 3, modesIndex);
195 
196 	/* Bank 6 Write */
197 	RF_BANK_SETUP(priv, 6, modesIndex);
198 
199 	ar5212ModifyRfBuffer(priv->Bank6Data, ob2GHz,   3, 168, 0);
200 	ar5212ModifyRfBuffer(priv->Bank6Data, db2GHz,   3, 165, 0);
201 
202 	/* Bank 7 Setup */
203 	RF_BANK_SETUP(priv, 7, modesIndex);
204 
205 	/* Write Analog registers */
206 	HAL_INI_WRITE_BANK(ah, ar5212Bank1_2413, priv->Bank1Data, regWrites);
207 	HAL_INI_WRITE_BANK(ah, ar5212Bank2_2413, priv->Bank2Data, regWrites);
208 	HAL_INI_WRITE_BANK(ah, ar5212Bank3_2413, priv->Bank3Data, regWrites);
209 	HAL_INI_WRITE_BANK(ah, ar5212Bank6_2413, priv->Bank6Data, regWrites);
210 	HAL_INI_WRITE_BANK(ah, ar5212Bank7_2413, priv->Bank7Data, regWrites);
211 
212 	/* Now that we have reprogrammed rfgain value, clear the flag. */
213 	ahp->ah_rfgainState = HAL_RFGAIN_INACTIVE;
214 
215 	return AH_TRUE;
216 #undef	RF_BANK_SETUP
217 }
218 
219 /*
220  * Return a reference to the requested RF Bank.
221  */
222 static uint32_t *
223 ar2413GetRfBank(struct ath_hal *ah, int bank)
224 {
225 	struct ar2413State *priv = AR2413(ah);
226 
227 	HALASSERT(priv != AH_NULL);
228 	switch (bank) {
229 	case 1: return priv->Bank1Data;
230 	case 2: return priv->Bank2Data;
231 	case 3: return priv->Bank3Data;
232 	case 6: return priv->Bank6Data;
233 	case 7: return priv->Bank7Data;
234 	}
235 	HALDEBUG(ah, HAL_DEBUG_ANY, "%s: unknown RF Bank %d requested\n",
236 	    __func__, bank);
237 	return AH_NULL;
238 }
239 
240 /*
241  * Return indices surrounding the value in sorted integer lists.
242  *
243  * NB: the input list is assumed to be sorted in ascending order
244  */
245 static void
246 GetLowerUpperIndex(int16_t v, const uint16_t *lp, uint16_t listSize,
247                           uint32_t *vlo, uint32_t *vhi)
248 {
249 	int16_t target = v;
250 	const uint16_t *ep = lp+listSize;
251 	const uint16_t *tp;
252 
253 	/*
254 	 * Check first and last elements for out-of-bounds conditions.
255 	 */
256 	if (target < lp[0]) {
257 		*vlo = *vhi = 0;
258 		return;
259 	}
260 	if (target >= ep[-1]) {
261 		*vlo = *vhi = listSize - 1;
262 		return;
263 	}
264 
265 	/* look for value being near or between 2 values in list */
266 	for (tp = lp; tp < ep; tp++) {
267 		/*
268 		 * If value is close to the current value of the list
269 		 * then target is not between values, it is one of the values
270 		 */
271 		if (*tp == target) {
272 			*vlo = *vhi = tp - (const uint16_t *) lp;
273 			return;
274 		}
275 		/*
276 		 * Look for value being between current value and next value
277 		 * if so return these 2 values
278 		 */
279 		if (target < tp[1]) {
280 			*vlo = tp - (const uint16_t *) lp;
281 			*vhi = *vlo + 1;
282 			return;
283 		}
284 	}
285 }
286 
287 /*
288  * Fill the Vpdlist for indices Pmax-Pmin
289  */
290 static HAL_BOOL
291 ar2413FillVpdTable(uint32_t pdGainIdx, int16_t Pmin, int16_t  Pmax,
292 		   const int16_t *pwrList, const uint16_t *VpdList,
293 		   uint16_t numIntercepts, uint16_t retVpdList[][64])
294 {
295 	uint16_t ii, jj, kk;
296 	int16_t currPwr = (int16_t)(2*Pmin);
297 	/* since Pmin is pwr*2 and pwrList is 4*pwr */
298 	uint32_t  idxL, idxR;
299 
300 	ii = 0;
301 	jj = 0;
302 
303 	if (numIntercepts < 2)
304 		return AH_FALSE;
305 
306 	while (ii <= (uint16_t)(Pmax - Pmin)) {
307 		GetLowerUpperIndex(currPwr, (const uint16_t *) pwrList,
308 				   numIntercepts, &(idxL), &(idxR));
309 		if (idxR < 1)
310 			idxR = 1;			/* extrapolate below */
311 		if (idxL == (uint32_t)(numIntercepts - 1))
312 			idxL = numIntercepts - 2;	/* extrapolate above */
313 		if (pwrList[idxL] == pwrList[idxR])
314 			kk = VpdList[idxL];
315 		else
316 			kk = (uint16_t)
317 				(((currPwr - pwrList[idxL])*VpdList[idxR]+
318 				  (pwrList[idxR] - currPwr)*VpdList[idxL])/
319 				 (pwrList[idxR] - pwrList[idxL]));
320 		retVpdList[pdGainIdx][ii] = kk;
321 		ii++;
322 		currPwr += 2;				/* half dB steps */
323 	}
324 
325 	return AH_TRUE;
326 }
327 
328 /*
329  * Returns interpolated or the scaled up interpolated value
330  */
331 static int16_t
332 interpolate_signed(uint16_t target, uint16_t srcLeft, uint16_t srcRight,
333 	int16_t targetLeft, int16_t targetRight)
334 {
335 	int16_t rv;
336 
337 	if (srcRight != srcLeft) {
338 		rv = ((target - srcLeft)*targetRight +
339 		      (srcRight - target)*targetLeft) / (srcRight - srcLeft);
340 	} else {
341 		rv = targetLeft;
342 	}
343 	return rv;
344 }
345 
346 /*
347  * Uses the data points read from EEPROM to reconstruct the pdadc power table
348  * Called by ar2413SetPowerTable()
349  */
350 static int
351 ar2413getGainBoundariesAndPdadcsForPowers(struct ath_hal *ah, uint16_t channel,
352 		const RAW_DATA_STRUCT_2413 *pRawDataset,
353 		uint16_t pdGainOverlap_t2,
354 		int16_t  *pMinCalPower, uint16_t pPdGainBoundaries[],
355 		uint16_t pPdGainValues[], uint16_t pPDADCValues[])
356 {
357 	struct ar2413State *priv = AR2413(ah);
358 #define	VpdTable_L	priv->vpdTable_L
359 #define	VpdTable_R	priv->vpdTable_R
360 #define	VpdTable_I	priv->vpdTable_I
361 	uint32_t ii, jj, kk;
362 	int32_t ss;/* potentially -ve index for taking care of pdGainOverlap */
363 	uint32_t idxL, idxR;
364 	uint32_t numPdGainsUsed = 0;
365 	/*
366 	 * If desired to support -ve power levels in future, just
367 	 * change pwr_I_0 to signed 5-bits.
368 	 */
369 	int16_t Pmin_t2[MAX_NUM_PDGAINS_PER_CHANNEL];
370 	/* to accommodate -ve power levels later on. */
371 	int16_t Pmax_t2[MAX_NUM_PDGAINS_PER_CHANNEL];
372 	/* to accommodate -ve power levels later on */
373 	uint16_t numVpd = 0;
374 	uint16_t Vpd_step;
375 	int16_t tmpVal ;
376 	uint32_t sizeCurrVpdTable, maxIndex, tgtIndex;
377 
378 	/* Get upper lower index */
379 	GetLowerUpperIndex(channel, pRawDataset->pChannels,
380 				 pRawDataset->numChannels, &(idxL), &(idxR));
381 
382 	for (ii = 0; ii < MAX_NUM_PDGAINS_PER_CHANNEL; ii++) {
383 		jj = MAX_NUM_PDGAINS_PER_CHANNEL - ii - 1;
384 		/* work backwards 'cause highest pdGain for lowest power */
385 		numVpd = pRawDataset->pDataPerChannel[idxL].pDataPerPDGain[jj].numVpd;
386 		if (numVpd > 0) {
387 			pPdGainValues[numPdGainsUsed] = pRawDataset->pDataPerChannel[idxL].pDataPerPDGain[jj].pd_gain;
388 			Pmin_t2[numPdGainsUsed] = pRawDataset->pDataPerChannel[idxL].pDataPerPDGain[jj].pwr_t4[0];
389 			if (Pmin_t2[numPdGainsUsed] >pRawDataset->pDataPerChannel[idxR].pDataPerPDGain[jj].pwr_t4[0]) {
390 				Pmin_t2[numPdGainsUsed] = pRawDataset->pDataPerChannel[idxR].pDataPerPDGain[jj].pwr_t4[0];
391 			}
392 			Pmin_t2[numPdGainsUsed] = (int16_t)
393 				(Pmin_t2[numPdGainsUsed] / 2);
394 			Pmax_t2[numPdGainsUsed] = pRawDataset->pDataPerChannel[idxL].pDataPerPDGain[jj].pwr_t4[numVpd-1];
395 			if (Pmax_t2[numPdGainsUsed] > pRawDataset->pDataPerChannel[idxR].pDataPerPDGain[jj].pwr_t4[numVpd-1])
396 				Pmax_t2[numPdGainsUsed] =
397 					pRawDataset->pDataPerChannel[idxR].pDataPerPDGain[jj].pwr_t4[numVpd-1];
398 			Pmax_t2[numPdGainsUsed] = (int16_t)(Pmax_t2[numPdGainsUsed] / 2);
399 			ar2413FillVpdTable(
400 					   numPdGainsUsed, Pmin_t2[numPdGainsUsed], Pmax_t2[numPdGainsUsed],
401 					   &(pRawDataset->pDataPerChannel[idxL].pDataPerPDGain[jj].pwr_t4[0]),
402 					   &(pRawDataset->pDataPerChannel[idxL].pDataPerPDGain[jj].Vpd[0]), numVpd, VpdTable_L
403 					   );
404 			ar2413FillVpdTable(
405 					   numPdGainsUsed, Pmin_t2[numPdGainsUsed], Pmax_t2[numPdGainsUsed],
406 					   &(pRawDataset->pDataPerChannel[idxR].pDataPerPDGain[jj].pwr_t4[0]),
407 					   &(pRawDataset->pDataPerChannel[idxR].pDataPerPDGain[jj].Vpd[0]), numVpd, VpdTable_R
408 					   );
409 			for (kk = 0; kk < (uint16_t)(Pmax_t2[numPdGainsUsed] - Pmin_t2[numPdGainsUsed]); kk++) {
410 				VpdTable_I[numPdGainsUsed][kk] =
411 					interpolate_signed(
412 							   channel, pRawDataset->pChannels[idxL], pRawDataset->pChannels[idxR],
413 							   (int16_t)VpdTable_L[numPdGainsUsed][kk], (int16_t)VpdTable_R[numPdGainsUsed][kk]);
414 			}
415 			/* fill VpdTable_I for this pdGain */
416 			numPdGainsUsed++;
417 		}
418 		/* if this pdGain is used */
419 	}
420 
421 	*pMinCalPower = Pmin_t2[0];
422 	kk = 0; /* index for the final table */
423 	for (ii = 0; ii < numPdGainsUsed; ii++) {
424 		if (ii == (numPdGainsUsed - 1))
425 			pPdGainBoundaries[ii] = Pmax_t2[ii] +
426 				PD_GAIN_BOUNDARY_STRETCH_IN_HALF_DB;
427 		else
428 			pPdGainBoundaries[ii] = (uint16_t)
429 				((Pmax_t2[ii] + Pmin_t2[ii+1]) / 2 );
430 		if (pPdGainBoundaries[ii] > 63) {
431 			HALDEBUG(ah, HAL_DEBUG_ANY,
432 			    "%s: clamp pPdGainBoundaries[%d] %d\n",
433 			    __func__, ii, pPdGainBoundaries[ii]);/*XXX*/
434 			pPdGainBoundaries[ii] = 63;
435 		}
436 
437 		/* Find starting index for this pdGain */
438 		if (ii == 0)
439 			ss = 0; /* for the first pdGain, start from index 0 */
440 		else
441 			ss = (pPdGainBoundaries[ii-1] - Pmin_t2[ii]) -
442 				pdGainOverlap_t2;
443 		Vpd_step = (uint16_t)(VpdTable_I[ii][1] - VpdTable_I[ii][0]);
444 		Vpd_step = (uint16_t)((Vpd_step < 1) ? 1 : Vpd_step);
445 		/*
446 		 *-ve ss indicates need to extrapolate data below for this pdGain
447 		 */
448 		while (ss < 0) {
449 			tmpVal = (int16_t)(VpdTable_I[ii][0] + ss*Vpd_step);
450 			pPDADCValues[kk++] = (uint16_t)((tmpVal < 0) ? 0 : tmpVal);
451 			ss++;
452 		}
453 
454 		sizeCurrVpdTable = Pmax_t2[ii] - Pmin_t2[ii];
455 		tgtIndex = pPdGainBoundaries[ii] + pdGainOverlap_t2 - Pmin_t2[ii];
456 		maxIndex = (tgtIndex < sizeCurrVpdTable) ? tgtIndex : sizeCurrVpdTable;
457 
458 		while (ss < (int16_t)maxIndex)
459 			pPDADCValues[kk++] = VpdTable_I[ii][ss++];
460 
461 		Vpd_step = (uint16_t)(VpdTable_I[ii][sizeCurrVpdTable-1] -
462 				       VpdTable_I[ii][sizeCurrVpdTable-2]);
463 		Vpd_step = (uint16_t)((Vpd_step < 1) ? 1 : Vpd_step);
464 		/*
465 		 * for last gain, pdGainBoundary == Pmax_t2, so will
466 		 * have to extrapolate
467 		 */
468 		if (tgtIndex > maxIndex) {	/* need to extrapolate above */
469 			while(ss < (int16_t)tgtIndex) {
470 				tmpVal = (uint16_t)
471 					(VpdTable_I[ii][sizeCurrVpdTable-1] +
472 					 (ss-maxIndex)*Vpd_step);
473 				pPDADCValues[kk++] = (tmpVal > 127) ?
474 					127 : tmpVal;
475 				ss++;
476 			}
477 		}				/* extrapolated above */
478 	}					/* for all pdGainUsed */
479 
480 	while (ii < MAX_NUM_PDGAINS_PER_CHANNEL) {
481 		pPdGainBoundaries[ii] = pPdGainBoundaries[ii-1];
482 		ii++;
483 	}
484 	while (kk < 128) {
485 		pPDADCValues[kk] = pPDADCValues[kk-1];
486 		kk++;
487 	}
488 
489 	return numPdGainsUsed;
490 #undef VpdTable_L
491 #undef VpdTable_R
492 #undef VpdTable_I
493 }
494 
495 static HAL_BOOL
496 ar2413SetPowerTable(struct ath_hal *ah,
497 	int16_t *minPower, int16_t *maxPower,
498 	const struct ieee80211_channel *chan,
499 	uint16_t *rfXpdGain)
500 {
501 	uint16_t freq = ath_hal_gethwchannel(ah, chan);
502 	struct ath_hal_5212 *ahp = AH5212(ah);
503 	const HAL_EEPROM *ee = AH_PRIVATE(ah)->ah_eeprom;
504 	const RAW_DATA_STRUCT_2413 *pRawDataset = AH_NULL;
505 	uint16_t pdGainOverlap_t2;
506 	int16_t minCalPower2413_t2;
507 	uint16_t *pdadcValues = ahp->ah_pcdacTable;
508 	uint16_t gainBoundaries[4];
509 	uint32_t reg32, regoffset;
510 	int i, numPdGainsUsed;
511 #ifndef AH_USE_INIPDGAIN
512 	uint32_t tpcrg1;
513 #endif
514 
515 	HALDEBUG(ah, HAL_DEBUG_RFPARAM, "%s: chan 0x%x flag 0x%x\n",
516 	    __func__, freq, chan->ic_flags);
517 
518 	if (IEEE80211_IS_CHAN_G(chan) || IEEE80211_IS_CHAN_108G(chan))
519 		pRawDataset = &ee->ee_rawDataset2413[headerInfo11G];
520 	else if (IEEE80211_IS_CHAN_B(chan))
521 		pRawDataset = &ee->ee_rawDataset2413[headerInfo11B];
522 	else {
523 		HALDEBUG(ah, HAL_DEBUG_ANY, "%s: illegal mode\n", __func__);
524 		return AH_FALSE;
525 	}
526 
527 	pdGainOverlap_t2 = (uint16_t) SM(OS_REG_READ(ah, AR_PHY_TPCRG5),
528 					  AR_PHY_TPCRG5_PD_GAIN_OVERLAP);
529 
530 	numPdGainsUsed = ar2413getGainBoundariesAndPdadcsForPowers(ah,
531 		freq, pRawDataset, pdGainOverlap_t2,
532 		&minCalPower2413_t2,gainBoundaries, rfXpdGain, pdadcValues);
533 	HALASSERT(1 <= numPdGainsUsed && numPdGainsUsed <= 3);
534 
535 #ifdef AH_USE_INIPDGAIN
536 	/*
537 	 * Use pd_gains curve from eeprom; Atheros always uses
538 	 * the default curve from the ini file but some vendors
539 	 * (e.g. Zcomax) want to override this curve and not
540 	 * honoring their settings results in tx power 5dBm low.
541 	 */
542 	OS_REG_RMW_FIELD(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_NUM_PD_GAIN,
543 			 (pRawDataset->pDataPerChannel[0].numPdGains - 1));
544 #else
545 	tpcrg1 = OS_REG_READ(ah, AR_PHY_TPCRG1);
546 	tpcrg1 = (tpcrg1 &~ AR_PHY_TPCRG1_NUM_PD_GAIN)
547 		  | SM(numPdGainsUsed-1, AR_PHY_TPCRG1_NUM_PD_GAIN);
548 	switch (numPdGainsUsed) {
549 	case 3:
550 		tpcrg1 &= ~AR_PHY_TPCRG1_PDGAIN_SETTING3;
551 		tpcrg1 |= SM(rfXpdGain[2], AR_PHY_TPCRG1_PDGAIN_SETTING3);
552 		/* fall thru... */
553 	case 2:
554 		tpcrg1 &= ~AR_PHY_TPCRG1_PDGAIN_SETTING2;
555 		tpcrg1 |= SM(rfXpdGain[1], AR_PHY_TPCRG1_PDGAIN_SETTING2);
556 		/* fall thru... */
557 	case 1:
558 		tpcrg1 &= ~AR_PHY_TPCRG1_PDGAIN_SETTING1;
559 		tpcrg1 |= SM(rfXpdGain[0], AR_PHY_TPCRG1_PDGAIN_SETTING1);
560 		break;
561 	}
562 #ifdef AH_DEBUG
563 	if (tpcrg1 != OS_REG_READ(ah, AR_PHY_TPCRG1))
564 		HALDEBUG(ah, HAL_DEBUG_RFPARAM, "%s: using non-default "
565 		    "pd_gains (default 0x%x, calculated 0x%x)\n",
566 		    __func__, OS_REG_READ(ah, AR_PHY_TPCRG1), tpcrg1);
567 #endif
568 	OS_REG_WRITE(ah, AR_PHY_TPCRG1, tpcrg1);
569 #endif
570 
571 	/*
572 	 * Note the pdadc table may not start at 0 dBm power, could be
573 	 * negative or greater than 0.  Need to offset the power
574 	 * values by the amount of minPower for griffin
575 	 */
576 	if (minCalPower2413_t2 != 0)
577 		ahp->ah_txPowerIndexOffset = (int16_t)(0 - minCalPower2413_t2);
578 	else
579 		ahp->ah_txPowerIndexOffset = 0;
580 
581 	/* Finally, write the power values into the baseband power table */
582 	regoffset = 0x9800 + (672 <<2); /* beginning of pdadc table in griffin */
583 	for (i = 0; i < 32; i++) {
584 		reg32 = ((pdadcValues[4*i + 0] & 0xFF) << 0)  |
585 			((pdadcValues[4*i + 1] & 0xFF) << 8)  |
586 			((pdadcValues[4*i + 2] & 0xFF) << 16) |
587 			((pdadcValues[4*i + 3] & 0xFF) << 24) ;
588 		OS_REG_WRITE(ah, regoffset, reg32);
589 		regoffset += 4;
590 	}
591 
592 	OS_REG_WRITE(ah, AR_PHY_TPCRG5,
593 		     SM(pdGainOverlap_t2, AR_PHY_TPCRG5_PD_GAIN_OVERLAP) |
594 		     SM(gainBoundaries[0], AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_1) |
595 		     SM(gainBoundaries[1], AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_2) |
596 		     SM(gainBoundaries[2], AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_3) |
597 		     SM(gainBoundaries[3], AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_4));
598 
599 	return AH_TRUE;
600 }
601 
602 static int16_t
603 ar2413GetMinPower(struct ath_hal *ah, const RAW_DATA_PER_CHANNEL_2413 *data)
604 {
605 	uint32_t ii,jj;
606 	uint16_t Pmin=0,numVpd;
607 
608 	for (ii = 0; ii < MAX_NUM_PDGAINS_PER_CHANNEL; ii++) {
609 		jj = MAX_NUM_PDGAINS_PER_CHANNEL - ii - 1;
610 		/* work backwards 'cause highest pdGain for lowest power */
611 		numVpd = data->pDataPerPDGain[jj].numVpd;
612 		if (numVpd > 0) {
613 			Pmin = data->pDataPerPDGain[jj].pwr_t4[0];
614 			return(Pmin);
615 		}
616 	}
617 	return(Pmin);
618 }
619 
620 static int16_t
621 ar2413GetMaxPower(struct ath_hal *ah, const RAW_DATA_PER_CHANNEL_2413 *data)
622 {
623 	uint32_t ii;
624 	uint16_t Pmax=0,numVpd;
625 
626 	for (ii=0; ii< MAX_NUM_PDGAINS_PER_CHANNEL; ii++) {
627 		/* work forwards cuase lowest pdGain for highest power */
628 		numVpd = data->pDataPerPDGain[ii].numVpd;
629 		if (numVpd > 0) {
630 			Pmax = data->pDataPerPDGain[ii].pwr_t4[numVpd-1];
631 			return(Pmax);
632 		}
633 	}
634 	return(Pmax);
635 }
636 
637 static HAL_BOOL
638 ar2413GetChannelMaxMinPower(struct ath_hal *ah,
639 	const struct ieee80211_channel *chan,
640 	int16_t *maxPow, int16_t *minPow)
641 {
642 	uint16_t freq = chan->ic_freq;		/* NB: never mapped */
643 	const HAL_EEPROM *ee = AH_PRIVATE(ah)->ah_eeprom;
644 	const RAW_DATA_STRUCT_2413 *pRawDataset = AH_NULL;
645 	const RAW_DATA_PER_CHANNEL_2413 *data = AH_NULL;
646 	uint16_t numChannels;
647 	int totalD,totalF, totalMin,last, i;
648 
649 	*maxPow = 0;
650 
651 	if (IEEE80211_IS_CHAN_G(chan) || IEEE80211_IS_CHAN_108G(chan))
652 		pRawDataset = &ee->ee_rawDataset2413[headerInfo11G];
653 	else if (IEEE80211_IS_CHAN_B(chan))
654 		pRawDataset = &ee->ee_rawDataset2413[headerInfo11B];
655 	else
656 		return(AH_FALSE);
657 
658 	numChannels = pRawDataset->numChannels;
659 	data = pRawDataset->pDataPerChannel;
660 
661 	/* Make sure the channel is in the range of the TP values
662 	 *  (freq piers)
663 	 */
664 	if (numChannels < 1)
665 		return(AH_FALSE);
666 
667 	if ((freq < data[0].channelValue) ||
668 	    (freq > data[numChannels-1].channelValue)) {
669 		if (freq < data[0].channelValue) {
670 			*maxPow = ar2413GetMaxPower(ah, &data[0]);
671 			*minPow = ar2413GetMinPower(ah, &data[0]);
672 			return(AH_TRUE);
673 		} else {
674 			*maxPow = ar2413GetMaxPower(ah, &data[numChannels - 1]);
675 			*minPow = ar2413GetMinPower(ah, &data[numChannels - 1]);
676 			return(AH_TRUE);
677 		}
678 	}
679 
680 	/* Linearly interpolate the power value now */
681 	for (last=0,i=0; (i<numChannels) && (freq > data[i].channelValue);
682 	     last = i++);
683 	totalD = data[i].channelValue - data[last].channelValue;
684 	if (totalD > 0) {
685 		totalF = ar2413GetMaxPower(ah, &data[i]) - ar2413GetMaxPower(ah, &data[last]);
686 		*maxPow = (int8_t) ((totalF*(freq-data[last].channelValue) +
687 				     ar2413GetMaxPower(ah, &data[last])*totalD)/totalD);
688 		totalMin = ar2413GetMinPower(ah, &data[i]) - ar2413GetMinPower(ah, &data[last]);
689 		*minPow = (int8_t) ((totalMin*(freq-data[last].channelValue) +
690 				     ar2413GetMinPower(ah, &data[last])*totalD)/totalD);
691 		return(AH_TRUE);
692 	} else {
693 		if (freq == data[i].channelValue) {
694 			*maxPow = ar2413GetMaxPower(ah, &data[i]);
695 			*minPow = ar2413GetMinPower(ah, &data[i]);
696 			return(AH_TRUE);
697 		} else
698 			return(AH_FALSE);
699 	}
700 }
701 
702 /*
703  * Free memory for analog bank scratch buffers
704  */
705 static void
706 ar2413RfDetach(struct ath_hal *ah)
707 {
708 	struct ath_hal_5212 *ahp = AH5212(ah);
709 
710 	HALASSERT(ahp->ah_rfHal != AH_NULL);
711 	ath_hal_free(ahp->ah_rfHal);
712 	ahp->ah_rfHal = AH_NULL;
713 }
714 
715 /*
716  * Allocate memory for analog bank scratch buffers
717  * Scratch Buffer will be reinitialized every reset so no need to zero now
718  */
719 static HAL_BOOL
720 ar2413RfAttach(struct ath_hal *ah, HAL_STATUS *status)
721 {
722 	struct ath_hal_5212 *ahp = AH5212(ah);
723 	struct ar2413State *priv;
724 
725 	HALASSERT(ah->ah_magic == AR5212_MAGIC);
726 
727 	HALASSERT(ahp->ah_rfHal == AH_NULL);
728 	priv = ath_hal_malloc(sizeof(struct ar2413State));
729 	if (priv == AH_NULL) {
730 		HALDEBUG(ah, HAL_DEBUG_ANY,
731 		    "%s: cannot allocate private state\n", __func__);
732 		*status = HAL_ENOMEM;		/* XXX */
733 		return AH_FALSE;
734 	}
735 	priv->base.rfDetach		= ar2413RfDetach;
736 	priv->base.writeRegs		= ar2413WriteRegs;
737 	priv->base.getRfBank		= ar2413GetRfBank;
738 	priv->base.setChannel		= ar2413SetChannel;
739 	priv->base.setRfRegs		= ar2413SetRfRegs;
740 	priv->base.setPowerTable	= ar2413SetPowerTable;
741 	priv->base.getChannelMaxMinPower = ar2413GetChannelMaxMinPower;
742 	priv->base.getNfAdjust		= ar5212GetNfAdjust;
743 
744 	ahp->ah_pcdacTable = priv->pcdacTable;
745 	ahp->ah_pcdacTableSize = sizeof(priv->pcdacTable);
746 	ahp->ah_rfHal = &priv->base;
747 
748 	return AH_TRUE;
749 }
750 
751 static HAL_BOOL
752 ar2413Probe(struct ath_hal *ah)
753 {
754 	return IS_2413(ah);
755 }
756 AH_RF(RF2413, ar2413Probe, ar2413RfAttach);
757