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