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.
18 #include <linux/ath9k_platform.h>
20 void ath9k_hw_analog_shift_regwrite(struct ath_hw *ah, u32 reg, u32 val)
22 REG_WRITE(ah, reg, val);
24 if (ah->config.analog_shiftreg)
28 void ath9k_hw_analog_shift_rmw(struct ath_hw *ah, u32 reg, u32 mask,
31 REG_RMW(ah, reg, ((val << shift) & mask), mask);
33 if (ah->config.analog_shiftreg)
37 int16_t ath9k_hw_interpolate(u16 target, u16 srcLeft, u16 srcRight,
38 int16_t targetLeft, int16_t targetRight)
42 if (srcRight == srcLeft) {
45 rv = (int16_t) (((target - srcLeft) * targetRight +
46 (srcRight - target) * targetLeft) /
47 (srcRight - srcLeft));
52 bool ath9k_hw_get_lower_upper_index(u8 target, u8 *pList, u16 listSize,
53 u16 *indexL, u16 *indexR)
57 if (target <= pList[0]) {
58 *indexL = *indexR = 0;
61 if (target >= pList[listSize - 1]) {
62 *indexL = *indexR = (u16) (listSize - 1);
66 for (i = 0; i < listSize - 1; i++) {
67 if (pList[i] == target) {
68 *indexL = *indexR = i;
71 if (target < pList[i + 1]) {
73 *indexR = (u16) (i + 1);
80 void ath9k_hw_usb_gen_fill_eeprom(struct ath_hw *ah, u16 *eep_data,
81 int eep_start_loc, int size)
87 for (addr = 0; addr < size; addr++) {
88 addrdata[i] = AR5416_EEPROM_OFFSET +
89 ((addr + eep_start_loc) << AR5416_EEPROM_S);
92 REG_READ_MULTI(ah, addrdata, data, i);
94 for (j = 0; j < i; j++) {
103 REG_READ_MULTI(ah, addrdata, data, i);
105 for (j = 0; j < i; j++) {
112 static bool ath9k_hw_nvram_read_array(u16 *blob, size_t blob_size,
113 off_t offset, u16 *data)
115 if (offset > blob_size)
118 *data = blob[offset];
122 static bool ath9k_hw_nvram_read_pdata(struct ath9k_platform_data *pdata,
123 off_t offset, u16 *data)
125 return ath9k_hw_nvram_read_array(pdata->eeprom_data,
126 ARRAY_SIZE(pdata->eeprom_data),
130 static bool ath9k_hw_nvram_read_firmware(const struct firmware *eeprom_blob,
131 off_t offset, u16 *data)
133 return ath9k_hw_nvram_read_array((u16 *) eeprom_blob->data,
134 eeprom_blob->size / sizeof(u16),
138 bool ath9k_hw_nvram_read(struct ath_hw *ah, u32 off, u16 *data)
140 struct ath_common *common = ath9k_hw_common(ah);
141 struct ath9k_platform_data *pdata = ah->dev->platform_data;
145 ret = ath9k_hw_nvram_read_firmware(ah->eeprom_blob, off, data);
146 else if (pdata && !pdata->use_eeprom && pdata->eeprom_data)
147 ret = ath9k_hw_nvram_read_pdata(pdata, off, data);
149 ret = common->bus_ops->eeprom_read(common, off, data);
152 ath_dbg(common, EEPROM,
153 "unable to read eeprom region at offset %u\n", off);
158 int ath9k_hw_nvram_swap_data(struct ath_hw *ah, bool *swap_needed, int size)
163 struct ath_common *common = ath9k_hw_common(ah);
165 if (!ath9k_hw_nvram_read(ah, AR5416_EEPROM_MAGIC_OFFSET, &magic)) {
166 ath_err(common, "Reading Magic # failed\n");
170 *swap_needed = false;
171 if (swab16(magic) == AR5416_EEPROM_MAGIC) {
172 if (ah->ah_flags & AH_NO_EEP_SWAP) {
174 "Ignoring endianness difference in EEPROM magic bytes.\n");
178 } else if (magic != AR5416_EEPROM_MAGIC) {
179 if (ath9k_hw_use_flash(ah))
183 "Invalid EEPROM Magic (0x%04x).\n", magic);
187 eepdata = (u16 *)(&ah->eeprom);
190 ath_dbg(common, EEPROM,
191 "EEPROM Endianness is not native.. Changing.\n");
193 for (i = 0; i < size; i++)
194 eepdata[i] = swab16(eepdata[i]);
200 bool ath9k_hw_nvram_validate_checksum(struct ath_hw *ah, int size)
203 u16 *eepdata = (u16 *)(&ah->eeprom);
204 struct ath_common *common = ath9k_hw_common(ah);
206 for (i = 0; i < size; i++)
210 ath_err(common, "Bad EEPROM checksum 0x%x\n", sum);
217 bool ath9k_hw_nvram_check_version(struct ath_hw *ah, int version, int minrev)
219 struct ath_common *common = ath9k_hw_common(ah);
221 if (ah->eep_ops->get_eeprom_ver(ah) != version ||
222 ah->eep_ops->get_eeprom_rev(ah) < minrev) {
223 ath_err(common, "Bad EEPROM VER 0x%04x or REV 0x%04x\n",
224 ah->eep_ops->get_eeprom_ver(ah),
225 ah->eep_ops->get_eeprom_rev(ah));
232 void ath9k_hw_fill_vpd_table(u8 pwrMin, u8 pwrMax, u8 *pPwrList,
233 u8 *pVpdList, u16 numIntercepts,
238 u16 idxL = 0, idxR = 0;
240 for (i = 0; i <= (pwrMax - pwrMin) / 2; i++) {
241 ath9k_hw_get_lower_upper_index(currPwr, pPwrList,
242 numIntercepts, &(idxL),
246 if (idxL == numIntercepts - 1)
247 idxL = (u16) (numIntercepts - 2);
248 if (pPwrList[idxL] == pPwrList[idxR])
251 k = (u16)(((currPwr - pPwrList[idxL]) * pVpdList[idxR] +
252 (pPwrList[idxR] - currPwr) * pVpdList[idxL]) /
253 (pPwrList[idxR] - pPwrList[idxL]));
254 pRetVpdList[i] = (u8) k;
259 void ath9k_hw_get_legacy_target_powers(struct ath_hw *ah,
260 struct ath9k_channel *chan,
261 struct cal_target_power_leg *powInfo,
263 struct cal_target_power_leg *pNewPower,
264 u16 numRates, bool isExtTarget)
266 struct chan_centers centers;
269 int matchIndex = -1, lowIndex = -1;
272 ath9k_hw_get_channel_centers(ah, chan, ¢ers);
273 freq = (isExtTarget) ? centers.ext_center : centers.ctl_center;
275 if (freq <= ath9k_hw_fbin2freq(powInfo[0].bChannel,
276 IS_CHAN_2GHZ(chan))) {
279 for (i = 0; (i < numChannels) &&
280 (powInfo[i].bChannel != AR5416_BCHAN_UNUSED); i++) {
281 if (freq == ath9k_hw_fbin2freq(powInfo[i].bChannel,
282 IS_CHAN_2GHZ(chan))) {
285 } else if (freq < ath9k_hw_fbin2freq(powInfo[i].bChannel,
286 IS_CHAN_2GHZ(chan)) && i > 0 &&
287 freq > ath9k_hw_fbin2freq(powInfo[i - 1].bChannel,
288 IS_CHAN_2GHZ(chan))) {
293 if ((matchIndex == -1) && (lowIndex == -1))
297 if (matchIndex != -1) {
298 *pNewPower = powInfo[matchIndex];
300 clo = ath9k_hw_fbin2freq(powInfo[lowIndex].bChannel,
302 chi = ath9k_hw_fbin2freq(powInfo[lowIndex + 1].bChannel,
305 for (i = 0; i < numRates; i++) {
306 pNewPower->tPow2x[i] =
307 (u8)ath9k_hw_interpolate(freq, clo, chi,
308 powInfo[lowIndex].tPow2x[i],
309 powInfo[lowIndex + 1].tPow2x[i]);
314 void ath9k_hw_get_target_powers(struct ath_hw *ah,
315 struct ath9k_channel *chan,
316 struct cal_target_power_ht *powInfo,
318 struct cal_target_power_ht *pNewPower,
319 u16 numRates, bool isHt40Target)
321 struct chan_centers centers;
324 int matchIndex = -1, lowIndex = -1;
327 ath9k_hw_get_channel_centers(ah, chan, ¢ers);
328 freq = isHt40Target ? centers.synth_center : centers.ctl_center;
330 if (freq <= ath9k_hw_fbin2freq(powInfo[0].bChannel, IS_CHAN_2GHZ(chan))) {
333 for (i = 0; (i < numChannels) &&
334 (powInfo[i].bChannel != AR5416_BCHAN_UNUSED); i++) {
335 if (freq == ath9k_hw_fbin2freq(powInfo[i].bChannel,
336 IS_CHAN_2GHZ(chan))) {
340 if (freq < ath9k_hw_fbin2freq(powInfo[i].bChannel,
341 IS_CHAN_2GHZ(chan)) && i > 0 &&
342 freq > ath9k_hw_fbin2freq(powInfo[i - 1].bChannel,
343 IS_CHAN_2GHZ(chan))) {
348 if ((matchIndex == -1) && (lowIndex == -1))
352 if (matchIndex != -1) {
353 *pNewPower = powInfo[matchIndex];
355 clo = ath9k_hw_fbin2freq(powInfo[lowIndex].bChannel,
357 chi = ath9k_hw_fbin2freq(powInfo[lowIndex + 1].bChannel,
360 for (i = 0; i < numRates; i++) {
361 pNewPower->tPow2x[i] = (u8)ath9k_hw_interpolate(freq,
363 powInfo[lowIndex].tPow2x[i],
364 powInfo[lowIndex + 1].tPow2x[i]);
369 u16 ath9k_hw_get_max_edge_power(u16 freq, struct cal_ctl_edges *pRdEdgesPower,
370 bool is2GHz, int num_band_edges)
372 u16 twiceMaxEdgePower = MAX_RATE_POWER;
375 for (i = 0; (i < num_band_edges) &&
376 (pRdEdgesPower[i].bChannel != AR5416_BCHAN_UNUSED); i++) {
377 if (freq == ath9k_hw_fbin2freq(pRdEdgesPower[i].bChannel, is2GHz)) {
378 twiceMaxEdgePower = CTL_EDGE_TPOWER(pRdEdgesPower[i].ctl);
380 } else if ((i > 0) &&
381 (freq < ath9k_hw_fbin2freq(pRdEdgesPower[i].bChannel,
383 if (ath9k_hw_fbin2freq(pRdEdgesPower[i - 1].bChannel,
385 CTL_EDGE_FLAGS(pRdEdgesPower[i - 1].ctl)) {
387 CTL_EDGE_TPOWER(pRdEdgesPower[i - 1].ctl);
393 return twiceMaxEdgePower;
396 u16 ath9k_hw_get_scaled_power(struct ath_hw *ah, u16 power_limit,
397 u8 antenna_reduction)
399 u16 reduction = antenna_reduction;
402 * Reduce scaled Power by number of chains active
403 * to get the per chain tx power level.
405 switch (ar5416_get_ntxchains(ah->txchainmask)) {
409 reduction += POWER_CORRECTION_FOR_TWO_CHAIN;
412 reduction += POWER_CORRECTION_FOR_THREE_CHAIN;
416 if (power_limit > reduction)
417 power_limit -= reduction;
424 void ath9k_hw_update_regulatory_maxpower(struct ath_hw *ah)
426 struct ath_common *common = ath9k_hw_common(ah);
427 struct ath_regulatory *regulatory = ath9k_hw_regulatory(ah);
429 switch (ar5416_get_ntxchains(ah->txchainmask)) {
433 regulatory->max_power_level += POWER_CORRECTION_FOR_TWO_CHAIN;
436 regulatory->max_power_level += POWER_CORRECTION_FOR_THREE_CHAIN;
439 ath_dbg(common, EEPROM, "Invalid chainmask configuration\n");
444 void ath9k_hw_get_gain_boundaries_pdadcs(struct ath_hw *ah,
445 struct ath9k_channel *chan,
447 u8 *bChans, u16 availPiers,
449 u16 *pPdGainBoundaries, u8 *pPDADCValues,
454 u16 idxL = 0, idxR = 0, numPiers;
455 static u8 vpdTableL[AR5416_NUM_PD_GAINS]
456 [AR5416_MAX_PWR_RANGE_IN_HALF_DB];
457 static u8 vpdTableR[AR5416_NUM_PD_GAINS]
458 [AR5416_MAX_PWR_RANGE_IN_HALF_DB];
459 static u8 vpdTableI[AR5416_NUM_PD_GAINS]
460 [AR5416_MAX_PWR_RANGE_IN_HALF_DB];
462 u8 *pVpdL, *pVpdR, *pPwrL, *pPwrR;
463 u8 minPwrT4[AR5416_NUM_PD_GAINS];
464 u8 maxPwrT4[AR5416_NUM_PD_GAINS];
467 u16 sizeCurrVpdTable, maxIndex, tgtIndex;
469 int16_t minDelta = 0;
470 struct chan_centers centers;
471 int pdgain_boundary_default;
472 struct cal_data_per_freq *data_def = pRawDataSet;
473 struct cal_data_per_freq_4k *data_4k = pRawDataSet;
474 struct cal_data_per_freq_ar9287 *data_9287 = pRawDataSet;
475 bool eeprom_4k = AR_SREV_9285(ah) || AR_SREV_9271(ah);
478 if (AR_SREV_9287(ah))
479 intercepts = AR9287_PD_GAIN_ICEPTS;
481 intercepts = AR5416_PD_GAIN_ICEPTS;
483 memset(&minPwrT4, 0, AR5416_NUM_PD_GAINS);
484 ath9k_hw_get_channel_centers(ah, chan, ¢ers);
486 for (numPiers = 0; numPiers < availPiers; numPiers++) {
487 if (bChans[numPiers] == AR5416_BCHAN_UNUSED)
491 match = ath9k_hw_get_lower_upper_index((u8)FREQ2FBIN(centers.synth_center,
493 bChans, numPiers, &idxL, &idxR);
496 if (AR_SREV_9287(ah)) {
497 for (i = 0; i < numXpdGains; i++) {
498 minPwrT4[i] = data_9287[idxL].pwrPdg[i][0];
499 maxPwrT4[i] = data_9287[idxL].pwrPdg[i][intercepts - 1];
500 ath9k_hw_fill_vpd_table(minPwrT4[i], maxPwrT4[i],
501 data_9287[idxL].pwrPdg[i],
502 data_9287[idxL].vpdPdg[i],
506 } else if (eeprom_4k) {
507 for (i = 0; i < numXpdGains; i++) {
508 minPwrT4[i] = data_4k[idxL].pwrPdg[i][0];
509 maxPwrT4[i] = data_4k[idxL].pwrPdg[i][intercepts - 1];
510 ath9k_hw_fill_vpd_table(minPwrT4[i], maxPwrT4[i],
511 data_4k[idxL].pwrPdg[i],
512 data_4k[idxL].vpdPdg[i],
517 for (i = 0; i < numXpdGains; i++) {
518 minPwrT4[i] = data_def[idxL].pwrPdg[i][0];
519 maxPwrT4[i] = data_def[idxL].pwrPdg[i][intercepts - 1];
520 ath9k_hw_fill_vpd_table(minPwrT4[i], maxPwrT4[i],
521 data_def[idxL].pwrPdg[i],
522 data_def[idxL].vpdPdg[i],
528 for (i = 0; i < numXpdGains; i++) {
529 if (AR_SREV_9287(ah)) {
530 pVpdL = data_9287[idxL].vpdPdg[i];
531 pPwrL = data_9287[idxL].pwrPdg[i];
532 pVpdR = data_9287[idxR].vpdPdg[i];
533 pPwrR = data_9287[idxR].pwrPdg[i];
534 } else if (eeprom_4k) {
535 pVpdL = data_4k[idxL].vpdPdg[i];
536 pPwrL = data_4k[idxL].pwrPdg[i];
537 pVpdR = data_4k[idxR].vpdPdg[i];
538 pPwrR = data_4k[idxR].pwrPdg[i];
540 pVpdL = data_def[idxL].vpdPdg[i];
541 pPwrL = data_def[idxL].pwrPdg[i];
542 pVpdR = data_def[idxR].vpdPdg[i];
543 pPwrR = data_def[idxR].pwrPdg[i];
546 minPwrT4[i] = max(pPwrL[0], pPwrR[0]);
549 min(pPwrL[intercepts - 1],
550 pPwrR[intercepts - 1]);
553 ath9k_hw_fill_vpd_table(minPwrT4[i], maxPwrT4[i],
557 ath9k_hw_fill_vpd_table(minPwrT4[i], maxPwrT4[i],
562 for (j = 0; j <= (maxPwrT4[i] - minPwrT4[i]) / 2; j++) {
564 (u8)(ath9k_hw_interpolate((u16)
569 bChans[idxL], bChans[idxR],
570 vpdTableL[i][j], vpdTableR[i][j]));
577 for (i = 0; i < numXpdGains; i++) {
578 if (i == (numXpdGains - 1))
579 pPdGainBoundaries[i] =
580 (u16)(maxPwrT4[i] / 2);
582 pPdGainBoundaries[i] =
583 (u16)((maxPwrT4[i] + minPwrT4[i + 1]) / 4);
585 pPdGainBoundaries[i] =
586 min((u16)MAX_RATE_POWER, pPdGainBoundaries[i]);
591 if (AR_SREV_9280_20_OR_LATER(ah))
592 ss = (int16_t)(0 - (minPwrT4[i] / 2));
596 ss = (int16_t)((pPdGainBoundaries[i - 1] -
598 tPdGainOverlap + 1 + minDelta);
600 vpdStep = (int16_t)(vpdTableI[i][1] - vpdTableI[i][0]);
601 vpdStep = (int16_t)((vpdStep < 1) ? 1 : vpdStep);
603 while ((ss < 0) && (k < (AR5416_NUM_PDADC_VALUES - 1))) {
604 tmpVal = (int16_t)(vpdTableI[i][0] + ss * vpdStep);
605 pPDADCValues[k++] = (u8)((tmpVal < 0) ? 0 : tmpVal);
609 sizeCurrVpdTable = (u8) ((maxPwrT4[i] - minPwrT4[i]) / 2 + 1);
610 tgtIndex = (u8)(pPdGainBoundaries[i] + tPdGainOverlap -
612 maxIndex = (tgtIndex < sizeCurrVpdTable) ?
613 tgtIndex : sizeCurrVpdTable;
615 while ((ss < maxIndex) && (k < (AR5416_NUM_PDADC_VALUES - 1))) {
616 pPDADCValues[k++] = vpdTableI[i][ss++];
619 vpdStep = (int16_t)(vpdTableI[i][sizeCurrVpdTable - 1] -
620 vpdTableI[i][sizeCurrVpdTable - 2]);
621 vpdStep = (int16_t)((vpdStep < 1) ? 1 : vpdStep);
623 if (tgtIndex >= maxIndex) {
624 while ((ss <= tgtIndex) &&
625 (k < (AR5416_NUM_PDADC_VALUES - 1))) {
626 tmpVal = (int16_t)((vpdTableI[i][sizeCurrVpdTable - 1] +
627 (ss - maxIndex + 1) * vpdStep));
628 pPDADCValues[k++] = (u8)((tmpVal > 255) ?
636 pdgain_boundary_default = 58;
638 pdgain_boundary_default = pPdGainBoundaries[i - 1];
640 while (i < AR5416_PD_GAINS_IN_MASK) {
641 pPdGainBoundaries[i] = pdgain_boundary_default;
645 while (k < AR5416_NUM_PDADC_VALUES) {
646 pPDADCValues[k] = pPDADCValues[k - 1];
651 int ath9k_hw_eeprom_init(struct ath_hw *ah)
655 if (AR_SREV_9300_20_OR_LATER(ah))
656 ah->eep_ops = &eep_ar9300_ops;
657 else if (AR_SREV_9287(ah)) {
658 ah->eep_ops = &eep_ar9287_ops;
659 } else if (AR_SREV_9285(ah) || AR_SREV_9271(ah)) {
660 ah->eep_ops = &eep_4k_ops;
662 ah->eep_ops = &eep_def_ops;
665 if (!ah->eep_ops->fill_eeprom(ah))
668 status = ah->eep_ops->check_eeprom(ah);