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