2 * Copyright (c) 2008-2011 Atheros Communications Inc.
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 void ath9k_hw_analog_shift_regwrite(struct ath_hw
*ah
, u32 reg
, u32 val
)
21 REG_WRITE(ah
, reg
, val
);
23 if (ah
->config
.analog_shiftreg
)
27 void ath9k_hw_analog_shift_rmw(struct ath_hw
*ah
, u32 reg
, u32 mask
,
30 REG_RMW(ah
, reg
, ((val
<< shift
) & mask
), mask
);
32 if (ah
->config
.analog_shiftreg
)
36 int16_t ath9k_hw_interpolate(u16 target
, u16 srcLeft
, u16 srcRight
,
37 int16_t targetLeft
, int16_t targetRight
)
41 if (srcRight
== srcLeft
) {
44 rv
= (int16_t) (((target
- srcLeft
) * targetRight
+
45 (srcRight
- target
) * targetLeft
) /
46 (srcRight
- srcLeft
));
51 bool ath9k_hw_get_lower_upper_index(u8 target
, u8
*pList
, u16 listSize
,
52 u16
*indexL
, u16
*indexR
)
56 if (target
<= pList
[0]) {
57 *indexL
= *indexR
= 0;
60 if (target
>= pList
[listSize
- 1]) {
61 *indexL
= *indexR
= (u16
) (listSize
- 1);
65 for (i
= 0; i
< listSize
- 1; i
++) {
66 if (pList
[i
] == target
) {
67 *indexL
= *indexR
= i
;
70 if (target
< pList
[i
+ 1]) {
72 *indexR
= (u16
) (i
+ 1);
79 void ath9k_hw_usb_gen_fill_eeprom(struct ath_hw
*ah
, u16
*eep_data
,
80 int eep_start_loc
, int size
)
86 for (addr
= 0; addr
< size
; addr
++) {
87 addrdata
[i
] = AR5416_EEPROM_OFFSET
+
88 ((addr
+ eep_start_loc
) << AR5416_EEPROM_S
);
91 REG_READ_MULTI(ah
, addrdata
, data
, i
);
93 for (j
= 0; j
< i
; j
++) {
102 REG_READ_MULTI(ah
, addrdata
, data
, i
);
104 for (j
= 0; j
< i
; j
++) {
111 static bool ath9k_hw_nvram_read_blob(struct ath_hw
*ah
, u32 off
,
116 if (off
* sizeof(u16
) > ah
->eeprom_blob
->size
)
119 blob_data
= (u16
*)ah
->eeprom_blob
->data
;
120 *data
= blob_data
[off
];
124 bool ath9k_hw_nvram_read(struct ath_hw
*ah
, u32 off
, u16
*data
)
126 struct ath_common
*common
= ath9k_hw_common(ah
);
130 ret
= ath9k_hw_nvram_read_blob(ah
, off
, data
);
132 ret
= common
->bus_ops
->eeprom_read(common
, off
, data
);
135 ath_dbg(common
, EEPROM
,
136 "unable to read eeprom region at offset %u\n", off
);
141 void ath9k_hw_fill_vpd_table(u8 pwrMin
, u8 pwrMax
, u8
*pPwrList
,
142 u8
*pVpdList
, u16 numIntercepts
,
147 u16 idxL
= 0, idxR
= 0;
149 for (i
= 0; i
<= (pwrMax
- pwrMin
) / 2; i
++) {
150 ath9k_hw_get_lower_upper_index(currPwr
, pPwrList
,
151 numIntercepts
, &(idxL
),
155 if (idxL
== numIntercepts
- 1)
156 idxL
= (u16
) (numIntercepts
- 2);
157 if (pPwrList
[idxL
] == pPwrList
[idxR
])
160 k
= (u16
)(((currPwr
- pPwrList
[idxL
]) * pVpdList
[idxR
] +
161 (pPwrList
[idxR
] - currPwr
) * pVpdList
[idxL
]) /
162 (pPwrList
[idxR
] - pPwrList
[idxL
]));
163 pRetVpdList
[i
] = (u8
) k
;
168 void ath9k_hw_get_legacy_target_powers(struct ath_hw
*ah
,
169 struct ath9k_channel
*chan
,
170 struct cal_target_power_leg
*powInfo
,
172 struct cal_target_power_leg
*pNewPower
,
173 u16 numRates
, bool isExtTarget
)
175 struct chan_centers centers
;
178 int matchIndex
= -1, lowIndex
= -1;
181 ath9k_hw_get_channel_centers(ah
, chan
, ¢ers
);
182 freq
= (isExtTarget
) ? centers
.ext_center
: centers
.ctl_center
;
184 if (freq
<= ath9k_hw_fbin2freq(powInfo
[0].bChannel
,
185 IS_CHAN_2GHZ(chan
))) {
188 for (i
= 0; (i
< numChannels
) &&
189 (powInfo
[i
].bChannel
!= AR5416_BCHAN_UNUSED
); i
++) {
190 if (freq
== ath9k_hw_fbin2freq(powInfo
[i
].bChannel
,
191 IS_CHAN_2GHZ(chan
))) {
194 } else if (freq
< ath9k_hw_fbin2freq(powInfo
[i
].bChannel
,
195 IS_CHAN_2GHZ(chan
)) && i
> 0 &&
196 freq
> ath9k_hw_fbin2freq(powInfo
[i
- 1].bChannel
,
197 IS_CHAN_2GHZ(chan
))) {
202 if ((matchIndex
== -1) && (lowIndex
== -1))
206 if (matchIndex
!= -1) {
207 *pNewPower
= powInfo
[matchIndex
];
209 clo
= ath9k_hw_fbin2freq(powInfo
[lowIndex
].bChannel
,
211 chi
= ath9k_hw_fbin2freq(powInfo
[lowIndex
+ 1].bChannel
,
214 for (i
= 0; i
< numRates
; i
++) {
215 pNewPower
->tPow2x
[i
] =
216 (u8
)ath9k_hw_interpolate(freq
, clo
, chi
,
217 powInfo
[lowIndex
].tPow2x
[i
],
218 powInfo
[lowIndex
+ 1].tPow2x
[i
]);
223 void ath9k_hw_get_target_powers(struct ath_hw
*ah
,
224 struct ath9k_channel
*chan
,
225 struct cal_target_power_ht
*powInfo
,
227 struct cal_target_power_ht
*pNewPower
,
228 u16 numRates
, bool isHt40Target
)
230 struct chan_centers centers
;
233 int matchIndex
= -1, lowIndex
= -1;
236 ath9k_hw_get_channel_centers(ah
, chan
, ¢ers
);
237 freq
= isHt40Target
? centers
.synth_center
: centers
.ctl_center
;
239 if (freq
<= ath9k_hw_fbin2freq(powInfo
[0].bChannel
, IS_CHAN_2GHZ(chan
))) {
242 for (i
= 0; (i
< numChannels
) &&
243 (powInfo
[i
].bChannel
!= AR5416_BCHAN_UNUSED
); i
++) {
244 if (freq
== ath9k_hw_fbin2freq(powInfo
[i
].bChannel
,
245 IS_CHAN_2GHZ(chan
))) {
249 if (freq
< ath9k_hw_fbin2freq(powInfo
[i
].bChannel
,
250 IS_CHAN_2GHZ(chan
)) && i
> 0 &&
251 freq
> ath9k_hw_fbin2freq(powInfo
[i
- 1].bChannel
,
252 IS_CHAN_2GHZ(chan
))) {
257 if ((matchIndex
== -1) && (lowIndex
== -1))
261 if (matchIndex
!= -1) {
262 *pNewPower
= powInfo
[matchIndex
];
264 clo
= ath9k_hw_fbin2freq(powInfo
[lowIndex
].bChannel
,
266 chi
= ath9k_hw_fbin2freq(powInfo
[lowIndex
+ 1].bChannel
,
269 for (i
= 0; i
< numRates
; i
++) {
270 pNewPower
->tPow2x
[i
] = (u8
)ath9k_hw_interpolate(freq
,
272 powInfo
[lowIndex
].tPow2x
[i
],
273 powInfo
[lowIndex
+ 1].tPow2x
[i
]);
278 u16
ath9k_hw_get_max_edge_power(u16 freq
, struct cal_ctl_edges
*pRdEdgesPower
,
279 bool is2GHz
, int num_band_edges
)
281 u16 twiceMaxEdgePower
= MAX_RATE_POWER
;
284 for (i
= 0; (i
< num_band_edges
) &&
285 (pRdEdgesPower
[i
].bChannel
!= AR5416_BCHAN_UNUSED
); i
++) {
286 if (freq
== ath9k_hw_fbin2freq(pRdEdgesPower
[i
].bChannel
, is2GHz
)) {
287 twiceMaxEdgePower
= CTL_EDGE_TPOWER(pRdEdgesPower
[i
].ctl
);
289 } else if ((i
> 0) &&
290 (freq
< ath9k_hw_fbin2freq(pRdEdgesPower
[i
].bChannel
,
292 if (ath9k_hw_fbin2freq(pRdEdgesPower
[i
- 1].bChannel
,
294 CTL_EDGE_FLAGS(pRdEdgesPower
[i
- 1].ctl
)) {
296 CTL_EDGE_TPOWER(pRdEdgesPower
[i
- 1].ctl
);
302 return twiceMaxEdgePower
;
305 u16
ath9k_hw_get_scaled_power(struct ath_hw
*ah
, u16 power_limit
,
306 u8 antenna_reduction
)
308 u16 reduction
= antenna_reduction
;
311 * Reduce scaled Power by number of chains active
312 * to get the per chain tx power level.
314 switch (ar5416_get_ntxchains(ah
->txchainmask
)) {
318 reduction
+= POWER_CORRECTION_FOR_TWO_CHAIN
;
321 reduction
+= POWER_CORRECTION_FOR_THREE_CHAIN
;
325 if (power_limit
> reduction
)
326 power_limit
-= reduction
;
333 void ath9k_hw_update_regulatory_maxpower(struct ath_hw
*ah
)
335 struct ath_common
*common
= ath9k_hw_common(ah
);
336 struct ath_regulatory
*regulatory
= ath9k_hw_regulatory(ah
);
338 switch (ar5416_get_ntxchains(ah
->txchainmask
)) {
342 regulatory
->max_power_level
+= POWER_CORRECTION_FOR_TWO_CHAIN
;
345 regulatory
->max_power_level
+= POWER_CORRECTION_FOR_THREE_CHAIN
;
348 ath_dbg(common
, EEPROM
, "Invalid chainmask configuration\n");
353 void ath9k_hw_get_gain_boundaries_pdadcs(struct ath_hw
*ah
,
354 struct ath9k_channel
*chan
,
356 u8
*bChans
, u16 availPiers
,
358 u16
*pPdGainBoundaries
, u8
*pPDADCValues
,
363 u16 idxL
= 0, idxR
= 0, numPiers
;
364 static u8 vpdTableL
[AR5416_NUM_PD_GAINS
]
365 [AR5416_MAX_PWR_RANGE_IN_HALF_DB
];
366 static u8 vpdTableR
[AR5416_NUM_PD_GAINS
]
367 [AR5416_MAX_PWR_RANGE_IN_HALF_DB
];
368 static u8 vpdTableI
[AR5416_NUM_PD_GAINS
]
369 [AR5416_MAX_PWR_RANGE_IN_HALF_DB
];
371 u8
*pVpdL
, *pVpdR
, *pPwrL
, *pPwrR
;
372 u8 minPwrT4
[AR5416_NUM_PD_GAINS
];
373 u8 maxPwrT4
[AR5416_NUM_PD_GAINS
];
376 u16 sizeCurrVpdTable
, maxIndex
, tgtIndex
;
378 int16_t minDelta
= 0;
379 struct chan_centers centers
;
380 int pdgain_boundary_default
;
381 struct cal_data_per_freq
*data_def
= pRawDataSet
;
382 struct cal_data_per_freq_4k
*data_4k
= pRawDataSet
;
383 struct cal_data_per_freq_ar9287
*data_9287
= pRawDataSet
;
384 bool eeprom_4k
= AR_SREV_9285(ah
) || AR_SREV_9271(ah
);
387 if (AR_SREV_9287(ah
))
388 intercepts
= AR9287_PD_GAIN_ICEPTS
;
390 intercepts
= AR5416_PD_GAIN_ICEPTS
;
392 memset(&minPwrT4
, 0, AR5416_NUM_PD_GAINS
);
393 ath9k_hw_get_channel_centers(ah
, chan
, ¢ers
);
395 for (numPiers
= 0; numPiers
< availPiers
; numPiers
++) {
396 if (bChans
[numPiers
] == AR5416_BCHAN_UNUSED
)
400 match
= ath9k_hw_get_lower_upper_index((u8
)FREQ2FBIN(centers
.synth_center
,
402 bChans
, numPiers
, &idxL
, &idxR
);
405 if (AR_SREV_9287(ah
)) {
406 for (i
= 0; i
< numXpdGains
; i
++) {
407 minPwrT4
[i
] = data_9287
[idxL
].pwrPdg
[i
][0];
408 maxPwrT4
[i
] = data_9287
[idxL
].pwrPdg
[i
][intercepts
- 1];
409 ath9k_hw_fill_vpd_table(minPwrT4
[i
], maxPwrT4
[i
],
410 data_9287
[idxL
].pwrPdg
[i
],
411 data_9287
[idxL
].vpdPdg
[i
],
415 } else if (eeprom_4k
) {
416 for (i
= 0; i
< numXpdGains
; i
++) {
417 minPwrT4
[i
] = data_4k
[idxL
].pwrPdg
[i
][0];
418 maxPwrT4
[i
] = data_4k
[idxL
].pwrPdg
[i
][intercepts
- 1];
419 ath9k_hw_fill_vpd_table(minPwrT4
[i
], maxPwrT4
[i
],
420 data_4k
[idxL
].pwrPdg
[i
],
421 data_4k
[idxL
].vpdPdg
[i
],
426 for (i
= 0; i
< numXpdGains
; i
++) {
427 minPwrT4
[i
] = data_def
[idxL
].pwrPdg
[i
][0];
428 maxPwrT4
[i
] = data_def
[idxL
].pwrPdg
[i
][intercepts
- 1];
429 ath9k_hw_fill_vpd_table(minPwrT4
[i
], maxPwrT4
[i
],
430 data_def
[idxL
].pwrPdg
[i
],
431 data_def
[idxL
].vpdPdg
[i
],
437 for (i
= 0; i
< numXpdGains
; i
++) {
438 if (AR_SREV_9287(ah
)) {
439 pVpdL
= data_9287
[idxL
].vpdPdg
[i
];
440 pPwrL
= data_9287
[idxL
].pwrPdg
[i
];
441 pVpdR
= data_9287
[idxR
].vpdPdg
[i
];
442 pPwrR
= data_9287
[idxR
].pwrPdg
[i
];
443 } else if (eeprom_4k
) {
444 pVpdL
= data_4k
[idxL
].vpdPdg
[i
];
445 pPwrL
= data_4k
[idxL
].pwrPdg
[i
];
446 pVpdR
= data_4k
[idxR
].vpdPdg
[i
];
447 pPwrR
= data_4k
[idxR
].pwrPdg
[i
];
449 pVpdL
= data_def
[idxL
].vpdPdg
[i
];
450 pPwrL
= data_def
[idxL
].pwrPdg
[i
];
451 pVpdR
= data_def
[idxR
].vpdPdg
[i
];
452 pPwrR
= data_def
[idxR
].pwrPdg
[i
];
455 minPwrT4
[i
] = max(pPwrL
[0], pPwrR
[0]);
458 min(pPwrL
[intercepts
- 1],
459 pPwrR
[intercepts
- 1]);
462 ath9k_hw_fill_vpd_table(minPwrT4
[i
], maxPwrT4
[i
],
466 ath9k_hw_fill_vpd_table(minPwrT4
[i
], maxPwrT4
[i
],
471 for (j
= 0; j
<= (maxPwrT4
[i
] - minPwrT4
[i
]) / 2; j
++) {
473 (u8
)(ath9k_hw_interpolate((u16
)
478 bChans
[idxL
], bChans
[idxR
],
479 vpdTableL
[i
][j
], vpdTableR
[i
][j
]));
486 for (i
= 0; i
< numXpdGains
; i
++) {
487 if (i
== (numXpdGains
- 1))
488 pPdGainBoundaries
[i
] =
489 (u16
)(maxPwrT4
[i
] / 2);
491 pPdGainBoundaries
[i
] =
492 (u16
)((maxPwrT4
[i
] + minPwrT4
[i
+ 1]) / 4);
494 pPdGainBoundaries
[i
] =
495 min((u16
)MAX_RATE_POWER
, pPdGainBoundaries
[i
]);
500 if (AR_SREV_9280_20_OR_LATER(ah
))
501 ss
= (int16_t)(0 - (minPwrT4
[i
] / 2));
505 ss
= (int16_t)((pPdGainBoundaries
[i
- 1] -
507 tPdGainOverlap
+ 1 + minDelta
);
509 vpdStep
= (int16_t)(vpdTableI
[i
][1] - vpdTableI
[i
][0]);
510 vpdStep
= (int16_t)((vpdStep
< 1) ? 1 : vpdStep
);
512 while ((ss
< 0) && (k
< (AR5416_NUM_PDADC_VALUES
- 1))) {
513 tmpVal
= (int16_t)(vpdTableI
[i
][0] + ss
* vpdStep
);
514 pPDADCValues
[k
++] = (u8
)((tmpVal
< 0) ? 0 : tmpVal
);
518 sizeCurrVpdTable
= (u8
) ((maxPwrT4
[i
] - minPwrT4
[i
]) / 2 + 1);
519 tgtIndex
= (u8
)(pPdGainBoundaries
[i
] + tPdGainOverlap
-
521 maxIndex
= (tgtIndex
< sizeCurrVpdTable
) ?
522 tgtIndex
: sizeCurrVpdTable
;
524 while ((ss
< maxIndex
) && (k
< (AR5416_NUM_PDADC_VALUES
- 1))) {
525 pPDADCValues
[k
++] = vpdTableI
[i
][ss
++];
528 vpdStep
= (int16_t)(vpdTableI
[i
][sizeCurrVpdTable
- 1] -
529 vpdTableI
[i
][sizeCurrVpdTable
- 2]);
530 vpdStep
= (int16_t)((vpdStep
< 1) ? 1 : vpdStep
);
532 if (tgtIndex
>= maxIndex
) {
533 while ((ss
<= tgtIndex
) &&
534 (k
< (AR5416_NUM_PDADC_VALUES
- 1))) {
535 tmpVal
= (int16_t)((vpdTableI
[i
][sizeCurrVpdTable
- 1] +
536 (ss
- maxIndex
+ 1) * vpdStep
));
537 pPDADCValues
[k
++] = (u8
)((tmpVal
> 255) ?
545 pdgain_boundary_default
= 58;
547 pdgain_boundary_default
= pPdGainBoundaries
[i
- 1];
549 while (i
< AR5416_PD_GAINS_IN_MASK
) {
550 pPdGainBoundaries
[i
] = pdgain_boundary_default
;
554 while (k
< AR5416_NUM_PDADC_VALUES
) {
555 pPDADCValues
[k
] = pPDADCValues
[k
- 1];
560 int ath9k_hw_eeprom_init(struct ath_hw
*ah
)
564 if (AR_SREV_9300_20_OR_LATER(ah
))
565 ah
->eep_ops
= &eep_ar9300_ops
;
566 else if (AR_SREV_9287(ah
)) {
567 ah
->eep_ops
= &eep_ar9287_ops
;
568 } else if (AR_SREV_9285(ah
) || AR_SREV_9271(ah
)) {
569 ah
->eep_ops
= &eep_4k_ops
;
571 ah
->eep_ops
= &eep_def_ops
;
574 if (!ah
->eep_ops
->fill_eeprom(ah
))
577 status
= ah
->eep_ops
->check_eeprom(ah
);