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_array(u16
*blob
, size_t blob_size
,
112 off_t offset
, u16
*data
)
114 if (offset
>= blob_size
)
117 *data
= blob
[offset
];
121 static bool ath9k_hw_nvram_read_firmware(const struct firmware
*eeprom_blob
,
122 off_t offset
, u16
*data
)
124 return ath9k_hw_nvram_read_array((u16
*) eeprom_blob
->data
,
125 eeprom_blob
->size
/ sizeof(u16
),
129 static bool ath9k_hw_nvram_read_nvmem(struct ath_hw
*ah
, off_t offset
,
132 return ath9k_hw_nvram_read_array(ah
->nvmem_blob
,
133 ah
->nvmem_blob_len
/ sizeof(u16
),
137 bool ath9k_hw_nvram_read(struct ath_hw
*ah
, u32 off
, u16
*data
)
139 struct ath_common
*common
= ath9k_hw_common(ah
);
143 ret
= ath9k_hw_nvram_read_nvmem(ah
, off
, data
);
144 else if (ah
->eeprom_blob
)
145 ret
= ath9k_hw_nvram_read_firmware(ah
->eeprom_blob
, off
, data
);
147 ret
= common
->bus_ops
->eeprom_read(common
, off
, data
);
150 ath_dbg(common
, EEPROM
,
151 "unable to read eeprom region at offset %u\n", off
);
156 int ath9k_hw_nvram_swap_data(struct ath_hw
*ah
, bool *swap_needed
, int size
)
161 bool needs_byteswap
= false;
162 struct ath_common
*common
= ath9k_hw_common(ah
);
164 if (!ath9k_hw_nvram_read(ah
, AR5416_EEPROM_MAGIC_OFFSET
, &magic
)) {
165 ath_err(common
, "Reading Magic # failed\n");
169 if (swab16(magic
) == AR5416_EEPROM_MAGIC
) {
170 needs_byteswap
= true;
171 ath_dbg(common
, EEPROM
,
172 "EEPROM needs byte-swapping to correct endianness.\n");
173 } else if (magic
!= AR5416_EEPROM_MAGIC
) {
174 if (ath9k_hw_use_flash(ah
)) {
175 ath_dbg(common
, EEPROM
,
176 "Ignoring invalid EEPROM magic (0x%04x).\n",
180 "Invalid EEPROM magic (0x%04x).\n", magic
);
185 if (needs_byteswap
) {
186 if (ah
->ah_flags
& AH_NO_EEP_SWAP
) {
188 "Ignoring endianness difference in EEPROM magic bytes.\n");
190 eepdata
= (u16
*)(&ah
->eeprom
);
192 for (i
= 0; i
< size
; i
++)
193 eepdata
[i
] = swab16(eepdata
[i
]);
197 if (ah
->eep_ops
->get_eepmisc(ah
) & AR5416_EEPMISC_BIG_ENDIAN
) {
199 ath_dbg(common
, EEPROM
,
200 "Big Endian EEPROM detected according to EEPMISC register.\n");
202 *swap_needed
= false;
208 bool ath9k_hw_nvram_validate_checksum(struct ath_hw
*ah
, int size
)
211 u16
*eepdata
= (u16
*)(&ah
->eeprom
);
212 struct ath_common
*common
= ath9k_hw_common(ah
);
214 for (i
= 0; i
< size
; i
++)
218 ath_err(common
, "Bad EEPROM checksum 0x%x\n", sum
);
225 bool ath9k_hw_nvram_check_version(struct ath_hw
*ah
, int version
, int minrev
)
227 struct ath_common
*common
= ath9k_hw_common(ah
);
229 if (ah
->eep_ops
->get_eeprom_ver(ah
) != version
||
230 ah
->eep_ops
->get_eeprom_rev(ah
) < minrev
) {
231 ath_err(common
, "Bad EEPROM VER 0x%04x or REV 0x%04x\n",
232 ah
->eep_ops
->get_eeprom_ver(ah
),
233 ah
->eep_ops
->get_eeprom_rev(ah
));
240 void ath9k_hw_fill_vpd_table(u8 pwrMin
, u8 pwrMax
, u8
*pPwrList
,
241 u8
*pVpdList
, u16 numIntercepts
,
246 u16 idxL
= 0, idxR
= 0;
248 for (i
= 0; i
<= (pwrMax
- pwrMin
) / 2; i
++) {
249 ath9k_hw_get_lower_upper_index(currPwr
, pPwrList
,
250 numIntercepts
, &(idxL
),
254 if (idxL
== numIntercepts
- 1)
255 idxL
= (u16
) (numIntercepts
- 2);
256 if (pPwrList
[idxL
] == pPwrList
[idxR
])
259 k
= (u16
)(((currPwr
- pPwrList
[idxL
]) * pVpdList
[idxR
] +
260 (pPwrList
[idxR
] - currPwr
) * pVpdList
[idxL
]) /
261 (pPwrList
[idxR
] - pPwrList
[idxL
]));
262 pRetVpdList
[i
] = (u8
) k
;
267 void ath9k_hw_get_legacy_target_powers(struct ath_hw
*ah
,
268 struct ath9k_channel
*chan
,
269 struct cal_target_power_leg
*powInfo
,
271 struct cal_target_power_leg
*pNewPower
,
272 u16 numRates
, bool isExtTarget
)
274 struct chan_centers centers
;
277 int matchIndex
= -1, lowIndex
= -1;
280 ath9k_hw_get_channel_centers(ah
, chan
, ¢ers
);
281 freq
= (isExtTarget
) ? centers
.ext_center
: centers
.ctl_center
;
283 if (freq
<= ath9k_hw_fbin2freq(powInfo
[0].bChannel
,
284 IS_CHAN_2GHZ(chan
))) {
287 for (i
= 0; (i
< numChannels
) &&
288 (powInfo
[i
].bChannel
!= AR5416_BCHAN_UNUSED
); i
++) {
289 if (freq
== ath9k_hw_fbin2freq(powInfo
[i
].bChannel
,
290 IS_CHAN_2GHZ(chan
))) {
293 } else if (freq
< ath9k_hw_fbin2freq(powInfo
[i
].bChannel
,
294 IS_CHAN_2GHZ(chan
)) && i
> 0 &&
295 freq
> ath9k_hw_fbin2freq(powInfo
[i
- 1].bChannel
,
296 IS_CHAN_2GHZ(chan
))) {
301 if ((matchIndex
== -1) && (lowIndex
== -1))
305 if (matchIndex
!= -1) {
306 *pNewPower
= powInfo
[matchIndex
];
308 clo
= ath9k_hw_fbin2freq(powInfo
[lowIndex
].bChannel
,
310 chi
= ath9k_hw_fbin2freq(powInfo
[lowIndex
+ 1].bChannel
,
313 for (i
= 0; i
< numRates
; i
++) {
314 pNewPower
->tPow2x
[i
] =
315 (u8
)ath9k_hw_interpolate(freq
, clo
, chi
,
316 powInfo
[lowIndex
].tPow2x
[i
],
317 powInfo
[lowIndex
+ 1].tPow2x
[i
]);
322 void ath9k_hw_get_target_powers(struct ath_hw
*ah
,
323 struct ath9k_channel
*chan
,
324 struct cal_target_power_ht
*powInfo
,
326 struct cal_target_power_ht
*pNewPower
,
327 u16 numRates
, bool isHt40Target
)
329 struct chan_centers centers
;
332 int matchIndex
= -1, lowIndex
= -1;
335 ath9k_hw_get_channel_centers(ah
, chan
, ¢ers
);
336 freq
= isHt40Target
? centers
.synth_center
: centers
.ctl_center
;
338 if (freq
<= ath9k_hw_fbin2freq(powInfo
[0].bChannel
, IS_CHAN_2GHZ(chan
))) {
341 for (i
= 0; (i
< numChannels
) &&
342 (powInfo
[i
].bChannel
!= AR5416_BCHAN_UNUSED
); i
++) {
343 if (freq
== ath9k_hw_fbin2freq(powInfo
[i
].bChannel
,
344 IS_CHAN_2GHZ(chan
))) {
348 if (freq
< ath9k_hw_fbin2freq(powInfo
[i
].bChannel
,
349 IS_CHAN_2GHZ(chan
)) && i
> 0 &&
350 freq
> ath9k_hw_fbin2freq(powInfo
[i
- 1].bChannel
,
351 IS_CHAN_2GHZ(chan
))) {
356 if ((matchIndex
== -1) && (lowIndex
== -1))
360 if (matchIndex
!= -1) {
361 *pNewPower
= powInfo
[matchIndex
];
363 clo
= ath9k_hw_fbin2freq(powInfo
[lowIndex
].bChannel
,
365 chi
= ath9k_hw_fbin2freq(powInfo
[lowIndex
+ 1].bChannel
,
368 for (i
= 0; i
< numRates
; i
++) {
369 pNewPower
->tPow2x
[i
] = (u8
)ath9k_hw_interpolate(freq
,
371 powInfo
[lowIndex
].tPow2x
[i
],
372 powInfo
[lowIndex
+ 1].tPow2x
[i
]);
377 u16
ath9k_hw_get_max_edge_power(u16 freq
, struct cal_ctl_edges
*pRdEdgesPower
,
378 bool is2GHz
, int num_band_edges
)
380 u16 twiceMaxEdgePower
= MAX_RATE_POWER
;
383 for (i
= 0; (i
< num_band_edges
) &&
384 (pRdEdgesPower
[i
].bChannel
!= AR5416_BCHAN_UNUSED
); i
++) {
385 if (freq
== ath9k_hw_fbin2freq(pRdEdgesPower
[i
].bChannel
, is2GHz
)) {
386 twiceMaxEdgePower
= CTL_EDGE_TPOWER(pRdEdgesPower
[i
].ctl
);
388 } else if ((i
> 0) &&
389 (freq
< ath9k_hw_fbin2freq(pRdEdgesPower
[i
].bChannel
,
391 if (ath9k_hw_fbin2freq(pRdEdgesPower
[i
- 1].bChannel
,
393 CTL_EDGE_FLAGS(pRdEdgesPower
[i
- 1].ctl
)) {
395 CTL_EDGE_TPOWER(pRdEdgesPower
[i
- 1].ctl
);
401 return twiceMaxEdgePower
;
404 u16
ath9k_hw_get_scaled_power(struct ath_hw
*ah
, u16 power_limit
,
405 u8 antenna_reduction
)
407 u16 reduction
= antenna_reduction
;
410 * Reduce scaled Power by number of chains active
411 * to get the per chain tx power level.
413 switch (ar5416_get_ntxchains(ah
->txchainmask
)) {
417 reduction
+= POWER_CORRECTION_FOR_TWO_CHAIN
;
420 reduction
+= POWER_CORRECTION_FOR_THREE_CHAIN
;
424 if (power_limit
> reduction
)
425 power_limit
-= reduction
;
429 return min_t(u16
, power_limit
, MAX_RATE_POWER
);
432 void ath9k_hw_update_regulatory_maxpower(struct ath_hw
*ah
)
434 struct ath_common
*common
= ath9k_hw_common(ah
);
435 struct ath_regulatory
*regulatory
= ath9k_hw_regulatory(ah
);
437 switch (ar5416_get_ntxchains(ah
->txchainmask
)) {
441 regulatory
->max_power_level
+= POWER_CORRECTION_FOR_TWO_CHAIN
;
444 regulatory
->max_power_level
+= POWER_CORRECTION_FOR_THREE_CHAIN
;
447 ath_dbg(common
, EEPROM
, "Invalid chainmask configuration\n");
452 void ath9k_hw_get_gain_boundaries_pdadcs(struct ath_hw
*ah
,
453 struct ath9k_channel
*chan
,
455 u8
*bChans
, u16 availPiers
,
457 u16
*pPdGainBoundaries
, u8
*pPDADCValues
,
462 u16 idxL
= 0, idxR
= 0, numPiers
;
463 static u8 vpdTableL
[AR5416_NUM_PD_GAINS
]
464 [AR5416_MAX_PWR_RANGE_IN_HALF_DB
];
465 static u8 vpdTableR
[AR5416_NUM_PD_GAINS
]
466 [AR5416_MAX_PWR_RANGE_IN_HALF_DB
];
467 static u8 vpdTableI
[AR5416_NUM_PD_GAINS
]
468 [AR5416_MAX_PWR_RANGE_IN_HALF_DB
];
470 u8
*pVpdL
, *pVpdR
, *pPwrL
, *pPwrR
;
471 u8 minPwrT4
[AR5416_NUM_PD_GAINS
];
472 u8 maxPwrT4
[AR5416_NUM_PD_GAINS
];
475 u16 sizeCurrVpdTable
, maxIndex
, tgtIndex
;
477 int16_t minDelta
= 0;
478 struct chan_centers centers
;
479 int pdgain_boundary_default
;
480 struct cal_data_per_freq
*data_def
= pRawDataSet
;
481 struct cal_data_per_freq_4k
*data_4k
= pRawDataSet
;
482 struct cal_data_per_freq_ar9287
*data_9287
= pRawDataSet
;
483 bool eeprom_4k
= AR_SREV_9285(ah
) || AR_SREV_9271(ah
);
486 if (AR_SREV_9287(ah
))
487 intercepts
= AR9287_PD_GAIN_ICEPTS
;
489 intercepts
= AR5416_PD_GAIN_ICEPTS
;
491 memset(&minPwrT4
, 0, AR5416_NUM_PD_GAINS
);
492 ath9k_hw_get_channel_centers(ah
, chan
, ¢ers
);
494 for (numPiers
= 0; numPiers
< availPiers
; numPiers
++) {
495 if (bChans
[numPiers
] == AR5416_BCHAN_UNUSED
)
499 match
= ath9k_hw_get_lower_upper_index((u8
)FREQ2FBIN(centers
.synth_center
,
501 bChans
, numPiers
, &idxL
, &idxR
);
504 if (AR_SREV_9287(ah
)) {
505 for (i
= 0; i
< numXpdGains
; i
++) {
506 minPwrT4
[i
] = data_9287
[idxL
].pwrPdg
[i
][0];
507 maxPwrT4
[i
] = data_9287
[idxL
].pwrPdg
[i
][intercepts
- 1];
508 ath9k_hw_fill_vpd_table(minPwrT4
[i
], maxPwrT4
[i
],
509 data_9287
[idxL
].pwrPdg
[i
],
510 data_9287
[idxL
].vpdPdg
[i
],
514 } else if (eeprom_4k
) {
515 for (i
= 0; i
< numXpdGains
; i
++) {
516 minPwrT4
[i
] = data_4k
[idxL
].pwrPdg
[i
][0];
517 maxPwrT4
[i
] = data_4k
[idxL
].pwrPdg
[i
][intercepts
- 1];
518 ath9k_hw_fill_vpd_table(minPwrT4
[i
], maxPwrT4
[i
],
519 data_4k
[idxL
].pwrPdg
[i
],
520 data_4k
[idxL
].vpdPdg
[i
],
525 for (i
= 0; i
< numXpdGains
; i
++) {
526 minPwrT4
[i
] = data_def
[idxL
].pwrPdg
[i
][0];
527 maxPwrT4
[i
] = data_def
[idxL
].pwrPdg
[i
][intercepts
- 1];
528 ath9k_hw_fill_vpd_table(minPwrT4
[i
], maxPwrT4
[i
],
529 data_def
[idxL
].pwrPdg
[i
],
530 data_def
[idxL
].vpdPdg
[i
],
536 for (i
= 0; i
< numXpdGains
; i
++) {
537 if (AR_SREV_9287(ah
)) {
538 pVpdL
= data_9287
[idxL
].vpdPdg
[i
];
539 pPwrL
= data_9287
[idxL
].pwrPdg
[i
];
540 pVpdR
= data_9287
[idxR
].vpdPdg
[i
];
541 pPwrR
= data_9287
[idxR
].pwrPdg
[i
];
542 } else if (eeprom_4k
) {
543 pVpdL
= data_4k
[idxL
].vpdPdg
[i
];
544 pPwrL
= data_4k
[idxL
].pwrPdg
[i
];
545 pVpdR
= data_4k
[idxR
].vpdPdg
[i
];
546 pPwrR
= data_4k
[idxR
].pwrPdg
[i
];
548 pVpdL
= data_def
[idxL
].vpdPdg
[i
];
549 pPwrL
= data_def
[idxL
].pwrPdg
[i
];
550 pVpdR
= data_def
[idxR
].vpdPdg
[i
];
551 pPwrR
= data_def
[idxR
].pwrPdg
[i
];
554 minPwrT4
[i
] = max(pPwrL
[0], pPwrR
[0]);
557 min(pPwrL
[intercepts
- 1],
558 pPwrR
[intercepts
- 1]);
561 ath9k_hw_fill_vpd_table(minPwrT4
[i
], maxPwrT4
[i
],
565 ath9k_hw_fill_vpd_table(minPwrT4
[i
], maxPwrT4
[i
],
570 for (j
= 0; j
<= (maxPwrT4
[i
] - minPwrT4
[i
]) / 2; j
++) {
572 (u8
)(ath9k_hw_interpolate((u16
)
577 bChans
[idxL
], bChans
[idxR
],
578 vpdTableL
[i
][j
], vpdTableR
[i
][j
]));
585 for (i
= 0; i
< numXpdGains
; i
++) {
586 if (i
== (numXpdGains
- 1))
587 pPdGainBoundaries
[i
] =
588 (u16
)(maxPwrT4
[i
] / 2);
590 pPdGainBoundaries
[i
] =
591 (u16
)((maxPwrT4
[i
] + minPwrT4
[i
+ 1]) / 4);
593 pPdGainBoundaries
[i
] =
594 min((u16
)MAX_RATE_POWER
, pPdGainBoundaries
[i
]);
599 if (AR_SREV_9280_20_OR_LATER(ah
))
600 ss
= (int16_t)(0 - (minPwrT4
[i
] / 2));
604 ss
= (int16_t)((pPdGainBoundaries
[i
- 1] -
606 tPdGainOverlap
+ 1 + minDelta
);
608 vpdStep
= (int16_t)(vpdTableI
[i
][1] - vpdTableI
[i
][0]);
609 vpdStep
= (int16_t)((vpdStep
< 1) ? 1 : vpdStep
);
611 while ((ss
< 0) && (k
< (AR5416_NUM_PDADC_VALUES
- 1))) {
612 tmpVal
= (int16_t)(vpdTableI
[i
][0] + ss
* vpdStep
);
613 pPDADCValues
[k
++] = (u8
)((tmpVal
< 0) ? 0 : tmpVal
);
617 sizeCurrVpdTable
= (u8
) ((maxPwrT4
[i
] - minPwrT4
[i
]) / 2 + 1);
618 tgtIndex
= (u8
)(pPdGainBoundaries
[i
] + tPdGainOverlap
-
620 maxIndex
= (tgtIndex
< sizeCurrVpdTable
) ?
621 tgtIndex
: sizeCurrVpdTable
;
623 while ((ss
< maxIndex
) && (k
< (AR5416_NUM_PDADC_VALUES
- 1))) {
624 pPDADCValues
[k
++] = vpdTableI
[i
][ss
++];
627 vpdStep
= (int16_t)(vpdTableI
[i
][sizeCurrVpdTable
- 1] -
628 vpdTableI
[i
][sizeCurrVpdTable
- 2]);
629 vpdStep
= (int16_t)((vpdStep
< 1) ? 1 : vpdStep
);
631 if (tgtIndex
>= maxIndex
) {
632 while ((ss
<= tgtIndex
) &&
633 (k
< (AR5416_NUM_PDADC_VALUES
- 1))) {
634 tmpVal
= (int16_t)((vpdTableI
[i
][sizeCurrVpdTable
- 1] +
635 (ss
- maxIndex
+ 1) * vpdStep
));
636 pPDADCValues
[k
++] = (u8
)((tmpVal
> 255) ?
644 pdgain_boundary_default
= 58;
646 pdgain_boundary_default
= pPdGainBoundaries
[i
- 1];
648 while (i
< AR5416_PD_GAINS_IN_MASK
) {
649 pPdGainBoundaries
[i
] = pdgain_boundary_default
;
653 while (k
< AR5416_NUM_PDADC_VALUES
) {
654 pPDADCValues
[k
] = pPDADCValues
[k
- 1];
659 int ath9k_hw_eeprom_init(struct ath_hw
*ah
)
661 if (AR_SREV_9300_20_OR_LATER(ah
))
662 ah
->eep_ops
= &eep_ar9300_ops
;
663 else if (AR_SREV_9287(ah
)) {
664 ah
->eep_ops
= &eep_ar9287_ops
;
665 } else if (AR_SREV_9285(ah
) || AR_SREV_9271(ah
)) {
666 ah
->eep_ops
= &eep_4k_ops
;
668 ah
->eep_ops
= &eep_def_ops
;
671 if (!ah
->eep_ops
->fill_eeprom(ah
))
674 return ah
->eep_ops
->check_eeprom(ah
);