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