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