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