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