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