4 * Copyright (c) 2004-2007 Reyk Floeter <reyk@openbsd.org>
5 * Copyright (c) 2006-2009 Nick Kossifidis <mickflemm@gmail.com>
6 * Copyright (c) 2007-2008 Jiri Slaby <jirislaby@gmail.com>
7 * Copyright (c) 2008-2009 Felix Fietkau <nbd@openwrt.org>
9 * Lightly modified for iPXE, July 2009, by Joshua Oreman <oremanj@rwcr.net>.
11 * Permission to use, copy, modify, and distribute this software for any
12 * purpose with or without fee is hereby granted, provided that the above
13 * copyright notice and this permission notice appear in all copies.
15 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
16 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
18 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
38 static inline int min(int x, int y)
40 return (x < y) ? x : y;
43 static inline int max(int x, int y)
45 return (x > y) ? x : y;
49 * Used to modify RF Banks before writing them to AR5K_RF_BUFFER
51 static unsigned int ath5k_hw_rfb_op(struct ath5k_hw *ah,
52 const struct ath5k_rf_reg *rf_regs,
53 u32 val, u8 reg_id, int set)
55 const struct ath5k_rf_reg *rfreg = NULL;
56 u8 offset, bank, num_bits, col, position;
58 u32 mask, data, last_bit, bits_shifted, first_bit;
64 rfb = ah->ah_rf_banks;
66 for (i = 0; i < ah->ah_rf_regs_count; i++) {
67 if (rf_regs[i].index == reg_id) {
73 if (rfb == NULL || rfreg == NULL) {
74 DBG("ath5k: RF register not found!\n");
75 /* should not happen */
80 num_bits = rfreg->field.len;
81 first_bit = rfreg->field.pos;
82 col = rfreg->field.col;
84 /* first_bit is an offset from bank's
85 * start. Since we have all banks on
86 * the same array, we use this offset
87 * to mark each bank's start */
88 offset = ah->ah_offset[bank];
91 if (!(col <= 3 && num_bits <= 32 && first_bit + num_bits <= 319)) {
92 DBG("ath5k: RF invalid values at offset %d\n", offset);
96 entry = ((first_bit - 1) / 8) + offset;
97 position = (first_bit - 1) % 8;
100 data = ath5k_hw_bitswap(val, num_bits);
102 for (bits_shifted = 0, bits_left = num_bits; bits_left > 0;
103 position = 0, entry++) {
105 last_bit = (position + bits_left > 8) ? 8 :
106 position + bits_left;
108 mask = (((1 << last_bit) - 1) ^ ((1 << position) - 1)) <<
113 rfb[entry] |= ((data << position) << (col * 8)) & mask;
114 data >>= (8 - position);
116 data |= (((rfb[entry] & mask) >> (col * 8)) >> position)
118 bits_shifted += last_bit - position;
121 bits_left -= 8 - position;
124 data = set ? 1 : ath5k_hw_bitswap(data, num_bits);
129 /**********************\
130 * RF Gain optimization *
131 \**********************/
134 * This code is used to optimize rf gain on different environments
135 * (temprature mostly) based on feedback from a power detector.
137 * It's only used on RF5111 and RF5112, later RF chips seem to have
138 * auto adjustment on hw -notice they have a much smaller BANK 7 and
139 * no gain optimization ladder-.
141 * For more infos check out this patent doc
142 * http://www.freepatentsonline.com/7400691.html
144 * This paper describes power drops as seen on the receiver due to
146 * http://www.cnri.dit.ie/publications/ICT08%20-%20Practical%20Issues
147 * %20of%20Power%20Control.pdf
149 * And this is the MadWiFi bug entry related to the above
150 * http://madwifi-project.org/ticket/1659
151 * with various measurements and diagrams
153 * TODO: Deal with power drops due to probes by setting an apropriate
154 * tx power on the probe packets ! Make this part of the calibration process.
157 /* Initialize ah_gain durring attach */
158 int ath5k_hw_rfgain_opt_init(struct ath5k_hw *ah)
160 /* Initialize the gain optimization values */
161 switch (ah->ah_radio) {
163 ah->ah_gain.g_step_idx = rfgain_opt_5111.go_default;
164 ah->ah_gain.g_low = 20;
165 ah->ah_gain.g_high = 35;
166 ah->ah_gain.g_state = AR5K_RFGAIN_ACTIVE;
169 ah->ah_gain.g_step_idx = rfgain_opt_5112.go_default;
170 ah->ah_gain.g_low = 20;
171 ah->ah_gain.g_high = 85;
172 ah->ah_gain.g_state = AR5K_RFGAIN_ACTIVE;
181 /* Schedule a gain probe check on the next transmited packet.
182 * That means our next packet is going to be sent with lower
183 * tx power and a Peak to Average Power Detector (PAPD) will try
184 * to measure the gain.
186 * TODO: Use propper tx power setting for the probe packet so
187 * that we don't observe a serious power drop on the receiver
189 * XXX: How about forcing a tx packet (bypassing PCU arbitrator etc)
190 * just after we enable the probe so that we don't mess with
191 * standard traffic ? Maybe it's time to use sw interrupts and
192 * a probe tasklet !!!
194 static void ath5k_hw_request_rfgain_probe(struct ath5k_hw *ah)
197 /* Skip if gain calibration is inactive or
198 * we already handle a probe request */
199 if (ah->ah_gain.g_state != AR5K_RFGAIN_ACTIVE)
202 /* Send the packet with 2dB below max power as
203 * patent doc suggest */
204 ath5k_hw_reg_write(ah, AR5K_REG_SM(ah->ah_txpower.txp_max_pwr - 4,
205 AR5K_PHY_PAPD_PROBE_TXPOWER) |
206 AR5K_PHY_PAPD_PROBE_TX_NEXT, AR5K_PHY_PAPD_PROBE);
208 ah->ah_gain.g_state = AR5K_RFGAIN_READ_REQUESTED;
212 /* Calculate gain_F measurement correction
213 * based on the current step for RF5112 rev. 2 */
214 static u32 ath5k_hw_rf_gainf_corr(struct ath5k_hw *ah)
217 const struct ath5k_gain_opt *go;
218 const struct ath5k_gain_opt_step *g_step;
219 const struct ath5k_rf_reg *rf_regs;
221 /* Only RF5112 Rev. 2 supports it */
222 if ((ah->ah_radio != AR5K_RF5112) ||
223 (ah->ah_radio_5ghz_revision <= AR5K_SREV_RAD_5112A))
226 go = &rfgain_opt_5112;
227 rf_regs = rf_regs_5112a;
228 ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5112a);
230 g_step = &go->go_step[ah->ah_gain.g_step_idx];
232 if (ah->ah_rf_banks == NULL)
235 ah->ah_gain.g_f_corr = 0;
237 /* No VGA (Variable Gain Amplifier) override, skip */
238 if (ath5k_hw_rfb_op(ah, rf_regs, 0, AR5K_RF_MIXVGA_OVR, 0) != 1)
241 /* Mix gain stepping */
242 step = ath5k_hw_rfb_op(ah, rf_regs, 0, AR5K_RF_MIXGAIN_STEP, 0);
244 /* Mix gain override */
245 mix = g_step->gos_param[0];
249 ah->ah_gain.g_f_corr = step * 2;
252 ah->ah_gain.g_f_corr = (step - 5) * 2;
255 ah->ah_gain.g_f_corr = step;
258 ah->ah_gain.g_f_corr = 0;
262 return ah->ah_gain.g_f_corr;
265 /* Check if current gain_F measurement is in the range of our
266 * power detector windows. If we get a measurement outside range
267 * we know it's not accurate (detectors can't measure anything outside
268 * their detection window) so we must ignore it */
269 static int ath5k_hw_rf_check_gainf_readback(struct ath5k_hw *ah)
271 const struct ath5k_rf_reg *rf_regs;
272 u32 step, mix_ovr, level[4];
274 if (ah->ah_rf_banks == NULL)
277 if (ah->ah_radio == AR5K_RF5111) {
279 rf_regs = rf_regs_5111;
280 ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5111);
282 step = ath5k_hw_rfb_op(ah, rf_regs, 0, AR5K_RF_RFGAIN_STEP,
286 level[1] = (step == 63) ? 50 : step + 4;
287 level[2] = (step != 63) ? 64 : level[0];
288 level[3] = level[2] + 50 ;
290 ah->ah_gain.g_high = level[3] -
291 (step == 63 ? AR5K_GAIN_DYN_ADJUST_HI_MARGIN : -5);
292 ah->ah_gain.g_low = level[0] +
293 (step == 63 ? AR5K_GAIN_DYN_ADJUST_LO_MARGIN : 0);
296 rf_regs = rf_regs_5112;
297 ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5112);
299 mix_ovr = ath5k_hw_rfb_op(ah, rf_regs, 0, AR5K_RF_MIXVGA_OVR,
302 level[0] = level[2] = 0;
305 level[1] = level[3] = 83;
307 level[1] = level[3] = 107;
308 ah->ah_gain.g_high = 55;
312 return (ah->ah_gain.g_current >= level[0] &&
313 ah->ah_gain.g_current <= level[1]) ||
314 (ah->ah_gain.g_current >= level[2] &&
315 ah->ah_gain.g_current <= level[3]);
318 /* Perform gain_F adjustment by choosing the right set
319 * of parameters from rf gain optimization ladder */
320 static s8 ath5k_hw_rf_gainf_adjust(struct ath5k_hw *ah)
322 const struct ath5k_gain_opt *go;
323 const struct ath5k_gain_opt_step *g_step;
326 switch (ah->ah_radio) {
328 go = &rfgain_opt_5111;
331 go = &rfgain_opt_5112;
337 g_step = &go->go_step[ah->ah_gain.g_step_idx];
339 if (ah->ah_gain.g_current >= ah->ah_gain.g_high) {
341 /* Reached maximum */
342 if (ah->ah_gain.g_step_idx == 0)
345 for (ah->ah_gain.g_target = ah->ah_gain.g_current;
346 ah->ah_gain.g_target >= ah->ah_gain.g_high &&
347 ah->ah_gain.g_step_idx > 0;
348 g_step = &go->go_step[ah->ah_gain.g_step_idx])
349 ah->ah_gain.g_target -= 2 *
350 (go->go_step[--(ah->ah_gain.g_step_idx)].gos_gain -
357 if (ah->ah_gain.g_current <= ah->ah_gain.g_low) {
359 /* Reached minimum */
360 if (ah->ah_gain.g_step_idx == (go->go_steps_count - 1))
363 for (ah->ah_gain.g_target = ah->ah_gain.g_current;
364 ah->ah_gain.g_target <= ah->ah_gain.g_low &&
365 ah->ah_gain.g_step_idx < go->go_steps_count-1;
366 g_step = &go->go_step[ah->ah_gain.g_step_idx])
367 ah->ah_gain.g_target -= 2 *
368 (go->go_step[++ah->ah_gain.g_step_idx].gos_gain -
376 DBG2("ath5k RF adjust: ret %d, gain step %d, current gain %d, "
377 "target gain %d\n", ret, ah->ah_gain.g_step_idx,
378 ah->ah_gain.g_current, ah->ah_gain.g_target);
383 /* Main callback for thermal rf gain calibration engine
384 * Check for a new gain reading and schedule an adjustment
387 * TODO: Use sw interrupt to schedule reset if gain_F needs
389 enum ath5k_rfgain ath5k_hw_gainf_calibrate(struct ath5k_hw *ah)
392 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
394 if (ah->ah_rf_banks == NULL ||
395 ah->ah_gain.g_state == AR5K_RFGAIN_INACTIVE)
396 return AR5K_RFGAIN_INACTIVE;
398 /* No check requested, either engine is inactive
399 * or an adjustment is already requested */
400 if (ah->ah_gain.g_state != AR5K_RFGAIN_READ_REQUESTED)
403 /* Read the PAPD (Peak to Average Power Detector)
405 data = ath5k_hw_reg_read(ah, AR5K_PHY_PAPD_PROBE);
407 /* No probe is scheduled, read gain_F measurement */
408 if (!(data & AR5K_PHY_PAPD_PROBE_TX_NEXT)) {
409 ah->ah_gain.g_current = data >> AR5K_PHY_PAPD_PROBE_GAINF_S;
410 type = AR5K_REG_MS(data, AR5K_PHY_PAPD_PROBE_TYPE);
412 /* If tx packet is CCK correct the gain_F measurement
413 * by cck ofdm gain delta */
414 if (type == AR5K_PHY_PAPD_PROBE_TYPE_CCK) {
415 if (ah->ah_radio_5ghz_revision >= AR5K_SREV_RAD_5112A)
416 ah->ah_gain.g_current +=
417 ee->ee_cck_ofdm_gain_delta;
419 ah->ah_gain.g_current +=
420 AR5K_GAIN_CCK_PROBE_CORR;
423 /* Further correct gain_F measurement for
425 if (ah->ah_radio_5ghz_revision >= AR5K_SREV_RAD_5112A) {
426 ath5k_hw_rf_gainf_corr(ah);
427 ah->ah_gain.g_current =
428 ah->ah_gain.g_current >= ah->ah_gain.g_f_corr ?
429 (ah->ah_gain.g_current-ah->ah_gain.g_f_corr) :
433 /* Check if measurement is ok and if we need
434 * to adjust gain, schedule a gain adjustment,
435 * else switch back to the acive state */
436 if (ath5k_hw_rf_check_gainf_readback(ah) &&
437 AR5K_GAIN_CHECK_ADJUST(&ah->ah_gain) &&
438 ath5k_hw_rf_gainf_adjust(ah)) {
439 ah->ah_gain.g_state = AR5K_RFGAIN_NEED_CHANGE;
441 ah->ah_gain.g_state = AR5K_RFGAIN_ACTIVE;
446 return ah->ah_gain.g_state;
449 /* Write initial rf gain table to set the RF sensitivity
450 * this one works on all RF chips and has nothing to do
451 * with gain_F calibration */
452 int ath5k_hw_rfgain_init(struct ath5k_hw *ah, unsigned int freq)
454 const struct ath5k_ini_rfgain *ath5k_rfg;
455 unsigned int i, size;
457 switch (ah->ah_radio) {
459 ath5k_rfg = rfgain_5111;
460 size = ARRAY_SIZE(rfgain_5111);
463 ath5k_rfg = rfgain_5112;
464 size = ARRAY_SIZE(rfgain_5112);
467 ath5k_rfg = rfgain_2413;
468 size = ARRAY_SIZE(rfgain_2413);
471 ath5k_rfg = rfgain_2316;
472 size = ARRAY_SIZE(rfgain_2316);
475 ath5k_rfg = rfgain_5413;
476 size = ARRAY_SIZE(rfgain_5413);
480 ath5k_rfg = rfgain_2425;
481 size = ARRAY_SIZE(rfgain_2425);
488 case AR5K_INI_RFGAIN_2GHZ:
489 case AR5K_INI_RFGAIN_5GHZ:
495 for (i = 0; i < size; i++) {
497 ath5k_hw_reg_write(ah, ath5k_rfg[i].rfg_value[freq],
498 (u32)ath5k_rfg[i].rfg_register);
506 /********************\
507 * RF Registers setup *
508 \********************/
512 * Setup RF registers by writing rf buffer on hw
514 int ath5k_hw_rfregs_init(struct ath5k_hw *ah, struct net80211_channel *channel,
517 const struct ath5k_rf_reg *rf_regs;
518 const struct ath5k_ini_rfbuffer *ini_rfb;
519 const struct ath5k_gain_opt *go = NULL;
520 const struct ath5k_gain_opt_step *g_step;
521 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
524 int obdb = -1, bank = -1;
527 switch (ah->ah_radio) {
529 rf_regs = rf_regs_5111;
530 ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5111);
532 ah->ah_rf_banks_size = ARRAY_SIZE(rfb_5111);
533 go = &rfgain_opt_5111;
536 if (ah->ah_radio_5ghz_revision >= AR5K_SREV_RAD_5112A) {
537 rf_regs = rf_regs_5112a;
538 ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5112a);
540 ah->ah_rf_banks_size = ARRAY_SIZE(rfb_5112a);
542 rf_regs = rf_regs_5112;
543 ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5112);
545 ah->ah_rf_banks_size = ARRAY_SIZE(rfb_5112);
547 go = &rfgain_opt_5112;
550 rf_regs = rf_regs_2413;
551 ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_2413);
553 ah->ah_rf_banks_size = ARRAY_SIZE(rfb_2413);
556 rf_regs = rf_regs_2316;
557 ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_2316);
559 ah->ah_rf_banks_size = ARRAY_SIZE(rfb_2316);
562 rf_regs = rf_regs_5413;
563 ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5413);
565 ah->ah_rf_banks_size = ARRAY_SIZE(rfb_5413);
568 rf_regs = rf_regs_2425;
569 ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_2425);
571 ah->ah_rf_banks_size = ARRAY_SIZE(rfb_2317);
574 rf_regs = rf_regs_2425;
575 ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_2425);
576 if (ah->ah_mac_srev < AR5K_SREV_AR2417) {
578 ah->ah_rf_banks_size = ARRAY_SIZE(rfb_2425);
581 ah->ah_rf_banks_size = ARRAY_SIZE(rfb_2417);
588 /* If it's the first time we set rf buffer, allocate
589 * ah->ah_rf_banks based on ah->ah_rf_banks_size
591 if (ah->ah_rf_banks == NULL) {
592 ah->ah_rf_banks = malloc(sizeof(u32) * ah->ah_rf_banks_size);
593 if (ah->ah_rf_banks == NULL) {
598 /* Copy values to modify them */
599 rfb = ah->ah_rf_banks;
601 for (i = 0; i < ah->ah_rf_banks_size; i++) {
602 if (ini_rfb[i].rfb_bank >= AR5K_MAX_RF_BANKS) {
603 DBG("ath5k: invalid RF register bank\n");
607 /* Bank changed, write down the offset */
608 if (bank != ini_rfb[i].rfb_bank) {
609 bank = ini_rfb[i].rfb_bank;
610 ah->ah_offset[bank] = i;
613 rfb[i] = ini_rfb[i].rfb_mode_data[mode];
616 /* Set Output and Driver bias current (OB/DB) */
617 if (channel->hw_value & CHANNEL_2GHZ) {
619 if (channel->hw_value & CHANNEL_CCK)
620 ee_mode = AR5K_EEPROM_MODE_11B;
622 ee_mode = AR5K_EEPROM_MODE_11G;
624 /* For RF511X/RF211X combination we
625 * use b_OB and b_DB parameters stored
626 * in eeprom on ee->ee_ob[ee_mode][0]
628 * For all other chips we use OB/DB for 2Ghz
629 * stored in the b/g modal section just like
630 * 802.11a on ee->ee_ob[ee_mode][1] */
631 if ((ah->ah_radio == AR5K_RF5111) ||
632 (ah->ah_radio == AR5K_RF5112))
637 ath5k_hw_rfb_op(ah, rf_regs, ee->ee_ob[ee_mode][obdb],
640 ath5k_hw_rfb_op(ah, rf_regs, ee->ee_db[ee_mode][obdb],
643 /* RF5111 always needs OB/DB for 5GHz, even if we use 2GHz */
644 } else if ((channel->hw_value & CHANNEL_5GHZ) ||
645 (ah->ah_radio == AR5K_RF5111)) {
647 /* For 11a, Turbo and XR we need to choose
648 * OB/DB based on frequency range */
649 ee_mode = AR5K_EEPROM_MODE_11A;
650 obdb = channel->center_freq >= 5725 ? 3 :
651 (channel->center_freq >= 5500 ? 2 :
652 (channel->center_freq >= 5260 ? 1 :
653 (channel->center_freq > 4000 ? 0 : -1)));
658 ath5k_hw_rfb_op(ah, rf_regs, ee->ee_ob[ee_mode][obdb],
661 ath5k_hw_rfb_op(ah, rf_regs, ee->ee_db[ee_mode][obdb],
665 g_step = &go->go_step[ah->ah_gain.g_step_idx];
667 /* Bank Modifications (chip-specific) */
668 if (ah->ah_radio == AR5K_RF5111) {
670 /* Set gain_F settings according to current step */
671 if (channel->hw_value & CHANNEL_OFDM) {
673 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_FRAME_CTL,
674 AR5K_PHY_FRAME_CTL_TX_CLIP,
675 g_step->gos_param[0]);
677 ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[1],
680 ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[2],
683 ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[3],
684 AR5K_RF_RFGAIN_SEL, 1);
686 /* We programmed gain_F parameters, switch back
688 ah->ah_gain.g_state = AR5K_RFGAIN_ACTIVE;
694 ath5k_hw_rfb_op(ah, rf_regs, !ee->ee_xpd[ee_mode],
697 ath5k_hw_rfb_op(ah, rf_regs, ee->ee_x_gain[ee_mode],
698 AR5K_RF_XPD_GAIN, 1);
700 ath5k_hw_rfb_op(ah, rf_regs, ee->ee_i_gain[ee_mode],
703 ath5k_hw_rfb_op(ah, rf_regs, ee->ee_xpd[ee_mode],
706 /* TODO: Half/quarter channel support */
709 if (ah->ah_radio == AR5K_RF5112) {
711 /* Set gain_F settings according to current step */
712 if (channel->hw_value & CHANNEL_OFDM) {
714 ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[0],
715 AR5K_RF_MIXGAIN_OVR, 1);
717 ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[1],
720 ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[2],
723 ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[3],
726 ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[4],
729 ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[5],
732 ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[6],
735 /* We programmed gain_F parameters, switch back
737 ah->ah_gain.g_state = AR5K_RFGAIN_ACTIVE;
742 ath5k_hw_rfb_op(ah, rf_regs, ee->ee_xpd[ee_mode],
745 if (ah->ah_radio_5ghz_revision < AR5K_SREV_RAD_5112A) {
746 /* Rev. 1 supports only one xpd */
747 ath5k_hw_rfb_op(ah, rf_regs,
748 ee->ee_x_gain[ee_mode],
749 AR5K_RF_XPD_GAIN, 1);
752 /* TODO: Set high and low gain bits */
753 ath5k_hw_rfb_op(ah, rf_regs,
754 ee->ee_x_gain[ee_mode],
755 AR5K_RF_PD_GAIN_LO, 1);
756 ath5k_hw_rfb_op(ah, rf_regs,
757 ee->ee_x_gain[ee_mode],
758 AR5K_RF_PD_GAIN_HI, 1);
760 /* Lower synth voltage on Rev 2 */
761 ath5k_hw_rfb_op(ah, rf_regs, 2,
762 AR5K_RF_HIGH_VC_CP, 1);
764 ath5k_hw_rfb_op(ah, rf_regs, 2,
765 AR5K_RF_MID_VC_CP, 1);
767 ath5k_hw_rfb_op(ah, rf_regs, 2,
768 AR5K_RF_LOW_VC_CP, 1);
770 ath5k_hw_rfb_op(ah, rf_regs, 2,
773 /* Decrease power consumption on 5213+ BaseBand */
774 if (ah->ah_phy_revision >= AR5K_SREV_PHY_5212A) {
775 ath5k_hw_rfb_op(ah, rf_regs, 1,
778 ath5k_hw_rfb_op(ah, rf_regs, 1,
781 ath5k_hw_rfb_op(ah, rf_regs, 1,
784 ath5k_hw_rfb_op(ah, rf_regs, 1,
787 ath5k_hw_rfb_op(ah, rf_regs, 1,
792 ath5k_hw_rfb_op(ah, rf_regs, ee->ee_i_gain[ee_mode],
795 /* TODO: Half/quarter channel support */
799 if (ah->ah_radio == AR5K_RF5413 &&
800 channel->hw_value & CHANNEL_2GHZ) {
802 ath5k_hw_rfb_op(ah, rf_regs, 1, AR5K_RF_DERBY_CHAN_SEL_MODE,
805 /* Set optimum value for early revisions (on pci-e chips) */
806 if (ah->ah_mac_srev >= AR5K_SREV_AR5424 &&
807 ah->ah_mac_srev < AR5K_SREV_AR5413)
808 ath5k_hw_rfb_op(ah, rf_regs, ath5k_hw_bitswap(6, 3),
809 AR5K_RF_PWD_ICLOBUF_2G, 1);
813 /* Write RF banks on hw */
814 for (i = 0; i < ah->ah_rf_banks_size; i++) {
816 ath5k_hw_reg_write(ah, rfb[i], ini_rfb[i].rfb_ctrl_register);
823 /**************************\
824 PHY/RF channel functions
825 \**************************/
828 * Check if a channel is supported
830 int ath5k_channel_ok(struct ath5k_hw *ah, u16 freq, unsigned int flags)
832 /* Check if the channel is in our supported range */
833 if (flags & CHANNEL_2GHZ) {
834 if ((freq >= ah->ah_capabilities.cap_range.range_2ghz_min) &&
835 (freq <= ah->ah_capabilities.cap_range.range_2ghz_max))
837 } else if (flags & CHANNEL_5GHZ)
838 if ((freq >= ah->ah_capabilities.cap_range.range_5ghz_min) &&
839 (freq <= ah->ah_capabilities.cap_range.range_5ghz_max))
846 * Convertion needed for RF5110
848 static u32 ath5k_hw_rf5110_chan2athchan(struct net80211_channel *channel)
853 * Convert IEEE channel/MHz to an internal channel value used
854 * by the AR5210 chipset. This has not been verified with
855 * newer chipsets like the AR5212A who have a completely
856 * different RF/PHY part.
858 athchan = (ath5k_hw_bitswap((ath5k_freq_to_channel(channel->center_freq)
865 * Set channel on RF5110
867 static int ath5k_hw_rf5110_channel(struct ath5k_hw *ah,
868 struct net80211_channel *channel)
873 * Set the channel and wait
875 data = ath5k_hw_rf5110_chan2athchan(channel);
876 ath5k_hw_reg_write(ah, data, AR5K_RF_BUFFER);
877 ath5k_hw_reg_write(ah, 0, AR5K_RF_BUFFER_CONTROL_0);
884 * Convertion needed for 5111
886 static int ath5k_hw_rf5111_chan2athchan(unsigned int ieee,
887 struct ath5k_athchan_2ghz *athchan)
891 /* Cast this value to catch negative channel numbers (>= -19) */
895 * Map 2GHz IEEE channel to 5GHz Atheros channel
898 athchan->a2_athchan = 115 + channel;
899 athchan->a2_flags = 0x46;
900 } else if (channel == 14) {
901 athchan->a2_athchan = 124;
902 athchan->a2_flags = 0x44;
903 } else if (channel >= 15 && channel <= 26) {
904 athchan->a2_athchan = ((channel - 14) * 4) + 132;
905 athchan->a2_flags = 0x46;
913 * Set channel on 5111
915 static int ath5k_hw_rf5111_channel(struct ath5k_hw *ah,
916 struct net80211_channel *channel)
918 struct ath5k_athchan_2ghz ath5k_channel_2ghz;
919 unsigned int ath5k_channel = ath5k_freq_to_channel(channel->center_freq);
920 u32 data0, data1, clock;
924 * Set the channel on the RF5111 radio
928 if (channel->hw_value & CHANNEL_2GHZ) {
929 /* Map 2GHz channel to 5GHz Atheros channel ID */
930 ret = ath5k_hw_rf5111_chan2athchan(ath5k_channel,
931 &ath5k_channel_2ghz);
935 ath5k_channel = ath5k_channel_2ghz.a2_athchan;
936 data0 = ((ath5k_hw_bitswap(ath5k_channel_2ghz.a2_flags, 8) & 0xff)
940 if (ath5k_channel < 145 || !(ath5k_channel & 1)) {
942 data1 = ((ath5k_hw_bitswap(ath5k_channel - 24, 8) & 0xff) << 2) |
943 (clock << 1) | (1 << 10) | 1;
946 data1 = ((ath5k_hw_bitswap((ath5k_channel - 24) / 2, 8) & 0xff)
947 << 2) | (clock << 1) | (1 << 10) | 1;
950 ath5k_hw_reg_write(ah, (data1 & 0xff) | ((data0 & 0xff) << 8),
952 ath5k_hw_reg_write(ah, ((data1 >> 8) & 0xff) | (data0 & 0xff00),
953 AR5K_RF_BUFFER_CONTROL_3);
959 * Set channel on 5112 and newer
961 static int ath5k_hw_rf5112_channel(struct ath5k_hw *ah,
962 struct net80211_channel *channel)
964 u32 data, data0, data1, data2;
967 data = data0 = data1 = data2 = 0;
968 c = channel->center_freq;
971 if (!((c - 2224) % 5)) {
972 data0 = ((2 * (c - 704)) - 3040) / 10;
974 } else if (!((c - 2192) % 5)) {
975 data0 = ((2 * (c - 672)) - 3040) / 10;
980 data0 = ath5k_hw_bitswap((data0 << 2) & 0xff, 8);
981 } else if ((c - (c % 5)) != 2 || c > 5435) {
982 if (!(c % 20) && c >= 5120) {
983 data0 = ath5k_hw_bitswap(((c - 4800) / 20 << 2), 8);
984 data2 = ath5k_hw_bitswap(3, 2);
985 } else if (!(c % 10)) {
986 data0 = ath5k_hw_bitswap(((c - 4800) / 10 << 1), 8);
987 data2 = ath5k_hw_bitswap(2, 2);
988 } else if (!(c % 5)) {
989 data0 = ath5k_hw_bitswap((c - 4800) / 5, 8);
990 data2 = ath5k_hw_bitswap(1, 2);
994 data0 = ath5k_hw_bitswap((10 * (c - 2) - 4800) / 25 + 1, 8);
995 data2 = ath5k_hw_bitswap(0, 2);
998 data = (data0 << 4) | (data1 << 1) | (data2 << 2) | 0x1001;
1000 ath5k_hw_reg_write(ah, data & 0xff, AR5K_RF_BUFFER);
1001 ath5k_hw_reg_write(ah, (data >> 8) & 0x7f, AR5K_RF_BUFFER_CONTROL_5);
1007 * Set the channel on the RF2425
1009 static int ath5k_hw_rf2425_channel(struct ath5k_hw *ah,
1010 struct net80211_channel *channel)
1012 u32 data, data0, data2;
1015 data = data0 = data2 = 0;
1016 c = channel->center_freq;
1019 data0 = ath5k_hw_bitswap((c - 2272), 8);
1022 } else if ((c - (c % 5)) != 2 || c > 5435) {
1023 if (!(c % 20) && c < 5120)
1024 data0 = ath5k_hw_bitswap(((c - 4800) / 20 << 2), 8);
1026 data0 = ath5k_hw_bitswap(((c - 4800) / 10 << 1), 8);
1028 data0 = ath5k_hw_bitswap((c - 4800) / 5, 8);
1031 data2 = ath5k_hw_bitswap(1, 2);
1033 data0 = ath5k_hw_bitswap((10 * (c - 2) - 4800) / 25 + 1, 8);
1034 data2 = ath5k_hw_bitswap(0, 2);
1037 data = (data0 << 4) | data2 << 2 | 0x1001;
1039 ath5k_hw_reg_write(ah, data & 0xff, AR5K_RF_BUFFER);
1040 ath5k_hw_reg_write(ah, (data >> 8) & 0x7f, AR5K_RF_BUFFER_CONTROL_5);
1046 * Set a channel on the radio chip
1048 int ath5k_hw_channel(struct ath5k_hw *ah, struct net80211_channel *channel)
1052 * Check bounds supported by the PHY (we don't care about regultory
1053 * restrictions at this point). Note: hw_value already has the band
1054 * (CHANNEL_2GHZ, or CHANNEL_5GHZ) so we inform ath5k_channel_ok()
1055 * of the band by that */
1056 if (!ath5k_channel_ok(ah, channel->center_freq, channel->hw_value)) {
1057 DBG("ath5k: channel frequency (%d MHz) out of supported "
1058 "range\n", channel->center_freq);
1063 * Set the channel and wait
1065 switch (ah->ah_radio) {
1067 ret = ath5k_hw_rf5110_channel(ah, channel);
1070 ret = ath5k_hw_rf5111_channel(ah, channel);
1073 ret = ath5k_hw_rf2425_channel(ah, channel);
1076 ret = ath5k_hw_rf5112_channel(ah, channel);
1081 DBG("ath5k: setting channel failed: %s\n", strerror(ret));
1085 /* Set JAPAN setting for channel 14 */
1086 if (channel->center_freq == 2484) {
1087 AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_CCKTXCTL,
1088 AR5K_PHY_CCKTXCTL_JAPAN);
1090 AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_CCKTXCTL,
1091 AR5K_PHY_CCKTXCTL_WORLD);
1094 ah->ah_current_channel = channel;
1095 ah->ah_turbo = (channel->hw_value == CHANNEL_T ? 1 : 0);
1105 * ath5k_hw_noise_floor_calibration - perform PHY noise floor calibration
1107 * @ah: struct ath5k_hw pointer we are operating on
1108 * @freq: the channel frequency, just used for error logging
1110 * This function performs a noise floor calibration of the PHY and waits for
1111 * it to complete. Then the noise floor value is compared to some maximum
1112 * noise floor we consider valid.
1114 * Note that this is different from what the madwifi HAL does: it reads the
1115 * noise floor and afterwards initiates the calibration. Since the noise floor
1116 * calibration can take some time to finish, depending on the current channel
1117 * use, that avoids the occasional timeout warnings we are seeing now.
1119 * See the following link for an Atheros patent on noise floor calibration:
1120 * http://patft.uspto.gov/netacgi/nph-Parser?Sect1=PTO1&Sect2=HITOFF&d=PALL \
1121 * &p=1&u=%2Fnetahtml%2FPTO%2Fsrchnum.htm&r=1&f=G&l=50&s1=7245893.PN.&OS=PN/7
1123 * XXX: Since during noise floor calibration antennas are detached according to
1124 * the patent, we should stop tx queues here.
1127 ath5k_hw_noise_floor_calibration(struct ath5k_hw *ah, short freq)
1134 * Enable noise floor calibration
1136 AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGCCTL,
1137 AR5K_PHY_AGCCTL_NF);
1139 ret = ath5k_hw_register_timeout(ah, AR5K_PHY_AGCCTL,
1140 AR5K_PHY_AGCCTL_NF, 0, 0);
1143 DBG("ath5k: noise floor calibration timeout (%d MHz)\n", freq);
1147 /* Wait until the noise floor is calibrated and read the value */
1148 for (i = 20; i > 0; i--) {
1150 noise_floor = ath5k_hw_reg_read(ah, AR5K_PHY_NF);
1151 noise_floor = AR5K_PHY_NF_RVAL(noise_floor);
1152 if (noise_floor & AR5K_PHY_NF_ACTIVE) {
1153 noise_floor = AR5K_PHY_NF_AVAL(noise_floor);
1155 if (noise_floor <= AR5K_TUNE_NOISE_FLOOR)
1160 DBG2("ath5k: noise floor %d\n", noise_floor);
1162 if (noise_floor > AR5K_TUNE_NOISE_FLOOR) {
1163 DBG("ath5k: noise floor calibration failed (%d MHz)\n", freq);
1167 ah->ah_noise_floor = noise_floor;
1173 * Perform a PHY calibration on RF5110
1174 * -Fix BPSK/QAM Constellation (I/Q correction)
1175 * -Calculate Noise Floor
1177 static int ath5k_hw_rf5110_calibrate(struct ath5k_hw *ah,
1178 struct net80211_channel *channel)
1180 u32 phy_sig, phy_agc, phy_sat, beacon;
1184 * Disable beacons and RX/TX queues, wait
1186 AR5K_REG_ENABLE_BITS(ah, AR5K_DIAG_SW_5210,
1187 AR5K_DIAG_SW_DIS_TX | AR5K_DIAG_SW_DIS_RX_5210);
1188 beacon = ath5k_hw_reg_read(ah, AR5K_BEACON_5210);
1189 ath5k_hw_reg_write(ah, beacon & ~AR5K_BEACON_ENABLE, AR5K_BEACON_5210);
1194 * Set the channel (with AGC turned off)
1196 AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGC, AR5K_PHY_AGC_DISABLE);
1198 ret = ath5k_hw_channel(ah, channel);
1201 * Activate PHY and wait
1203 ath5k_hw_reg_write(ah, AR5K_PHY_ACT_ENABLE, AR5K_PHY_ACT);
1206 AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_AGC, AR5K_PHY_AGC_DISABLE);
1212 * Calibrate the radio chip
1215 /* Remember normal state */
1216 phy_sig = ath5k_hw_reg_read(ah, AR5K_PHY_SIG);
1217 phy_agc = ath5k_hw_reg_read(ah, AR5K_PHY_AGCCOARSE);
1218 phy_sat = ath5k_hw_reg_read(ah, AR5K_PHY_ADCSAT);
1220 /* Update radio registers */
1221 ath5k_hw_reg_write(ah, (phy_sig & ~(AR5K_PHY_SIG_FIRPWR)) |
1222 AR5K_REG_SM(-1, AR5K_PHY_SIG_FIRPWR), AR5K_PHY_SIG);
1224 ath5k_hw_reg_write(ah, (phy_agc & ~(AR5K_PHY_AGCCOARSE_HI |
1225 AR5K_PHY_AGCCOARSE_LO)) |
1226 AR5K_REG_SM(-1, AR5K_PHY_AGCCOARSE_HI) |
1227 AR5K_REG_SM(-127, AR5K_PHY_AGCCOARSE_LO), AR5K_PHY_AGCCOARSE);
1229 ath5k_hw_reg_write(ah, (phy_sat & ~(AR5K_PHY_ADCSAT_ICNT |
1230 AR5K_PHY_ADCSAT_THR)) |
1231 AR5K_REG_SM(2, AR5K_PHY_ADCSAT_ICNT) |
1232 AR5K_REG_SM(12, AR5K_PHY_ADCSAT_THR), AR5K_PHY_ADCSAT);
1236 AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGC, AR5K_PHY_AGC_DISABLE);
1238 ath5k_hw_reg_write(ah, AR5K_PHY_RFSTG_DISABLE, AR5K_PHY_RFSTG);
1239 AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_AGC, AR5K_PHY_AGC_DISABLE);
1244 * Enable calibration and wait until completion
1246 AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGCCTL, AR5K_PHY_AGCCTL_CAL);
1248 ret = ath5k_hw_register_timeout(ah, AR5K_PHY_AGCCTL,
1249 AR5K_PHY_AGCCTL_CAL, 0, 0);
1251 /* Reset to normal state */
1252 ath5k_hw_reg_write(ah, phy_sig, AR5K_PHY_SIG);
1253 ath5k_hw_reg_write(ah, phy_agc, AR5K_PHY_AGCCOARSE);
1254 ath5k_hw_reg_write(ah, phy_sat, AR5K_PHY_ADCSAT);
1257 DBG("ath5k: calibration timeout (%d MHz)\n",
1258 channel->center_freq);
1262 ath5k_hw_noise_floor_calibration(ah, channel->center_freq);
1265 * Re-enable RX/TX and beacons
1267 AR5K_REG_DISABLE_BITS(ah, AR5K_DIAG_SW_5210,
1268 AR5K_DIAG_SW_DIS_TX | AR5K_DIAG_SW_DIS_RX_5210);
1269 ath5k_hw_reg_write(ah, beacon, AR5K_BEACON_5210);
1275 * Perform a PHY calibration on RF5111/5112 and newer chips
1277 static int ath5k_hw_rf511x_calibrate(struct ath5k_hw *ah,
1278 struct net80211_channel *channel)
1281 s32 iq_corr, i_coff, i_coffd, q_coff, q_coffd;
1284 if (!ah->ah_calibration ||
1285 ath5k_hw_reg_read(ah, AR5K_PHY_IQ) & AR5K_PHY_IQ_RUN)
1288 /* Calibration has finished, get the results and re-run */
1289 for (i = 0; i <= 10; i++) {
1290 iq_corr = ath5k_hw_reg_read(ah, AR5K_PHY_IQRES_CAL_CORR);
1291 i_pwr = ath5k_hw_reg_read(ah, AR5K_PHY_IQRES_CAL_PWR_I);
1292 q_pwr = ath5k_hw_reg_read(ah, AR5K_PHY_IQRES_CAL_PWR_Q);
1295 i_coffd = ((i_pwr >> 1) + (q_pwr >> 1)) >> 7;
1296 q_coffd = q_pwr >> 7;
1299 if (i_coffd == 0 || q_coffd == 0)
1302 i_coff = ((-iq_corr) / i_coffd) & 0x3f;
1304 /* Boundary check */
1310 q_coff = (((s32)i_pwr / q_coffd) - 128) & 0x1f;
1312 /* Boundary check */
1318 /* Commit new I/Q value */
1319 AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ, AR5K_PHY_IQ_CORR_ENABLE |
1320 ((u32)q_coff) | ((u32)i_coff << AR5K_PHY_IQ_CORR_Q_I_COFF_S));
1322 /* Re-enable calibration -if we don't we'll commit
1323 * the same values again and again */
1324 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_IQ,
1325 AR5K_PHY_IQ_CAL_NUM_LOG_MAX, 15);
1326 AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ, AR5K_PHY_IQ_RUN);
1330 /* TODO: Separate noise floor calibration from I/Q calibration
1331 * since noise floor calibration interrupts rx path while I/Q
1332 * calibration doesn't. We don't need to run noise floor calibration
1333 * as often as I/Q calibration.*/
1334 ath5k_hw_noise_floor_calibration(ah, channel->center_freq);
1336 /* Initiate a gain_F calibration */
1337 ath5k_hw_request_rfgain_probe(ah);
1343 * Perform a PHY calibration
1345 int ath5k_hw_phy_calibrate(struct ath5k_hw *ah,
1346 struct net80211_channel *channel)
1350 if (ah->ah_radio == AR5K_RF5110)
1351 ret = ath5k_hw_rf5110_calibrate(ah, channel);
1353 ret = ath5k_hw_rf511x_calibrate(ah, channel);
1358 int ath5k_hw_phy_disable(struct ath5k_hw *ah)
1360 ath5k_hw_reg_write(ah, AR5K_PHY_ACT_DISABLE, AR5K_PHY_ACT);
1365 /********************\
1367 \********************/
1370 * Get the PHY Chip revision
1372 u16 ath5k_hw_radio_revision(struct ath5k_hw *ah, unsigned int chan)
1379 * Set the radio chip access register
1383 ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_2GHZ, AR5K_PHY(0));
1386 ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ, AR5K_PHY(0));
1394 /* ...wait until PHY is ready and read the selected radio revision */
1395 ath5k_hw_reg_write(ah, 0x00001c16, AR5K_PHY(0x34));
1397 for (i = 0; i < 8; i++)
1398 ath5k_hw_reg_write(ah, 0x00010000, AR5K_PHY(0x20));
1400 if (ah->ah_version == AR5K_AR5210) {
1401 srev = ath5k_hw_reg_read(ah, AR5K_PHY(256) >> 28) & 0xf;
1402 ret = (u16)ath5k_hw_bitswap(srev, 4) + 1;
1404 srev = (ath5k_hw_reg_read(ah, AR5K_PHY(0x100)) >> 24) & 0xff;
1405 ret = (u16)ath5k_hw_bitswap(((srev & 0xf0) >> 4) |
1406 ((srev & 0x0f) << 4), 8);
1409 /* Reset to the 5GHz mode */
1410 ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ, AR5K_PHY(0));
1415 void /*TODO:Boundary check*/
1416 ath5k_hw_set_def_antenna(struct ath5k_hw *ah, unsigned int ant)
1418 if (ah->ah_version != AR5K_AR5210)
1419 ath5k_hw_reg_write(ah, ant, AR5K_DEFAULT_ANTENNA);
1422 unsigned int ath5k_hw_get_def_antenna(struct ath5k_hw *ah)
1424 if (ah->ah_version != AR5K_AR5210)
1425 return ath5k_hw_reg_read(ah, AR5K_DEFAULT_ANTENNA);
1427 return 0; /*XXX: What do we return for 5210 ?*/
1440 * Do linear interpolation between two given (x, y) points
1443 ath5k_get_interpolated_value(s16 target, s16 x_left, s16 x_right,
1444 s16 y_left, s16 y_right)
1448 /* Avoid divide by zero and skip interpolation
1449 * if we have the same point */
1450 if ((x_left == x_right) || (y_left == y_right))
1454 * Since we use ints and not fps, we need to scale up in
1455 * order to get a sane ratio value (or else we 'll eg. get
1456 * always 1 instead of 1.25, 1.75 etc). We scale up by 100
1457 * to have some accuracy both for 0.5 and 0.25 steps.
1459 ratio = ((100 * y_right - 100 * y_left)/(x_right - x_left));
1461 /* Now scale down to be in range */
1462 result = y_left + (ratio * (target - x_left) / 100);
1468 * Find vertical boundary (min pwr) for the linear PCDAC curve.
1470 * Since we have the top of the curve and we draw the line below
1471 * until we reach 1 (1 pcdac step) we need to know which point
1472 * (x value) that is so that we don't go below y axis and have negative
1473 * pcdac values when creating the curve, or fill the table with zeroes.
1476 ath5k_get_linear_pcdac_min(const u8 *stepL, const u8 *stepR,
1477 const s16 *pwrL, const s16 *pwrR)
1480 s16 min_pwrL, min_pwrR;
1483 if (pwrL[0] == pwrL[1])
1489 tmp = (s8) ath5k_get_interpolated_value(pwr_i,
1491 stepL[0], stepL[1]);
1497 if (pwrR[0] == pwrR[1])
1503 tmp = (s8) ath5k_get_interpolated_value(pwr_i,
1505 stepR[0], stepR[1]);
1511 /* Keep the right boundary so that it works for both curves */
1512 return max(min_pwrL, min_pwrR);
1516 * Interpolate (pwr,vpd) points to create a Power to PDADC or a
1517 * Power to PCDAC curve.
1519 * Each curve has power on x axis (in 0.5dB units) and PCDAC/PDADC
1520 * steps (offsets) on y axis. Power can go up to 31.5dB and max
1521 * PCDAC/PDADC step for each curve is 64 but we can write more than
1522 * one curves on hw so we can go up to 128 (which is the max step we
1523 * can write on the final table).
1525 * We write y values (PCDAC/PDADC steps) on hw.
1528 ath5k_create_power_curve(s16 pmin, s16 pmax,
1529 const s16 *pwr, const u8 *vpd,
1531 u8 *vpd_table, u8 type)
1533 u8 idx[2] = { 0, 1 };
1540 /* We want the whole line, so adjust boundaries
1541 * to cover the entire power range. Note that
1542 * power values are already 0.25dB so no need
1543 * to multiply pwr_i by 2 */
1544 if (type == AR5K_PWRTABLE_LINEAR_PCDAC) {
1550 /* Find surrounding turning points (TPs)
1551 * and interpolate between them */
1552 for (i = 0; (i <= (u16) (pmax - pmin)) &&
1553 (i < AR5K_EEPROM_POWER_TABLE_SIZE); i++) {
1555 /* We passed the right TP, move to the next set of TPs
1556 * if we pass the last TP, extrapolate above using the last
1557 * two TPs for ratio */
1558 if ((pwr_i > pwr[idx[1]]) && (idx[1] < num_points - 1)) {
1563 vpd_table[i] = (u8) ath5k_get_interpolated_value(pwr_i,
1564 pwr[idx[0]], pwr[idx[1]],
1565 vpd[idx[0]], vpd[idx[1]]);
1567 /* Increase by 0.5dB
1568 * (0.25 dB units) */
1574 * Get the surrounding per-channel power calibration piers
1575 * for a given frequency so that we can interpolate between
1576 * them and come up with an apropriate dataset for our current
1580 ath5k_get_chan_pcal_surrounding_piers(struct ath5k_hw *ah,
1581 struct net80211_channel *channel,
1582 struct ath5k_chan_pcal_info **pcinfo_l,
1583 struct ath5k_chan_pcal_info **pcinfo_r)
1585 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
1586 struct ath5k_chan_pcal_info *pcinfo;
1589 u32 target = channel->center_freq;
1594 if (!(channel->hw_value & CHANNEL_OFDM)) {
1595 pcinfo = ee->ee_pwr_cal_b;
1596 mode = AR5K_EEPROM_MODE_11B;
1597 } else if (channel->hw_value & CHANNEL_2GHZ) {
1598 pcinfo = ee->ee_pwr_cal_g;
1599 mode = AR5K_EEPROM_MODE_11G;
1601 pcinfo = ee->ee_pwr_cal_a;
1602 mode = AR5K_EEPROM_MODE_11A;
1604 max = ee->ee_n_piers[mode] - 1;
1606 /* Frequency is below our calibrated
1607 * range. Use the lowest power curve
1609 if (target < pcinfo[0].freq) {
1614 /* Frequency is above our calibrated
1615 * range. Use the highest power curve
1617 if (target > pcinfo[max].freq) {
1618 idx_l = idx_r = max;
1622 /* Frequency is inside our calibrated
1623 * channel range. Pick the surrounding
1624 * calibration piers so that we can
1626 for (i = 0; i <= max; i++) {
1628 /* Frequency matches one of our calibration
1629 * piers, no need to interpolate, just use
1630 * that calibration pier */
1631 if (pcinfo[i].freq == target) {
1636 /* We found a calibration pier that's above
1637 * frequency, use this pier and the previous
1638 * one to interpolate */
1639 if (target < pcinfo[i].freq) {
1647 *pcinfo_l = &pcinfo[idx_l];
1648 *pcinfo_r = &pcinfo[idx_r];
1654 * Get the surrounding per-rate power calibration data
1655 * for a given frequency and interpolate between power
1656 * values to set max target power supported by hw for
1660 ath5k_get_rate_pcal_data(struct ath5k_hw *ah,
1661 struct net80211_channel *channel,
1662 struct ath5k_rate_pcal_info *rates)
1664 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
1665 struct ath5k_rate_pcal_info *rpinfo;
1668 u32 target = channel->center_freq;
1673 if (!(channel->hw_value & CHANNEL_OFDM)) {
1674 rpinfo = ee->ee_rate_tpwr_b;
1675 mode = AR5K_EEPROM_MODE_11B;
1676 } else if (channel->hw_value & CHANNEL_2GHZ) {
1677 rpinfo = ee->ee_rate_tpwr_g;
1678 mode = AR5K_EEPROM_MODE_11G;
1680 rpinfo = ee->ee_rate_tpwr_a;
1681 mode = AR5K_EEPROM_MODE_11A;
1683 max = ee->ee_rate_target_pwr_num[mode] - 1;
1685 /* Get the surrounding calibration
1686 * piers - same as above */
1687 if (target < rpinfo[0].freq) {
1692 if (target > rpinfo[max].freq) {
1693 idx_l = idx_r = max;
1697 for (i = 0; i <= max; i++) {
1699 if (rpinfo[i].freq == target) {
1704 if (target < rpinfo[i].freq) {
1712 /* Now interpolate power value, based on the frequency */
1713 rates->freq = target;
1715 rates->target_power_6to24 =
1716 ath5k_get_interpolated_value(target, rpinfo[idx_l].freq,
1718 rpinfo[idx_l].target_power_6to24,
1719 rpinfo[idx_r].target_power_6to24);
1721 rates->target_power_36 =
1722 ath5k_get_interpolated_value(target, rpinfo[idx_l].freq,
1724 rpinfo[idx_l].target_power_36,
1725 rpinfo[idx_r].target_power_36);
1727 rates->target_power_48 =
1728 ath5k_get_interpolated_value(target, rpinfo[idx_l].freq,
1730 rpinfo[idx_l].target_power_48,
1731 rpinfo[idx_r].target_power_48);
1733 rates->target_power_54 =
1734 ath5k_get_interpolated_value(target, rpinfo[idx_l].freq,
1736 rpinfo[idx_l].target_power_54,
1737 rpinfo[idx_r].target_power_54);
1741 * Get the max edge power for this channel if
1742 * we have such data from EEPROM's Conformance Test
1743 * Limits (CTL), and limit max power if needed.
1745 * FIXME: Only works for world regulatory domains
1748 ath5k_get_max_ctl_power(struct ath5k_hw *ah,
1749 struct net80211_channel *channel)
1751 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
1752 struct ath5k_edge_power *rep = ee->ee_ctl_pwr;
1753 u8 *ctl_val = ee->ee_ctl;
1754 s16 max_chan_pwr = ah->ah_txpower.txp_max_pwr / 4;
1759 u32 target = channel->center_freq;
1761 /* Find out a CTL for our mode that's not mapped
1762 * on a specific reg domain.
1764 * TODO: Map our current reg domain to one of the 3 available
1765 * reg domain ids so that we can support more CTLs. */
1766 switch (channel->hw_value & CHANNEL_MODES) {
1768 ctl_mode = AR5K_CTL_11A | AR5K_CTL_NO_REGDOMAIN;
1771 ctl_mode = AR5K_CTL_11G | AR5K_CTL_NO_REGDOMAIN;
1774 ctl_mode = AR5K_CTL_11B | AR5K_CTL_NO_REGDOMAIN;
1777 ctl_mode = AR5K_CTL_TURBO | AR5K_CTL_NO_REGDOMAIN;
1780 ctl_mode = AR5K_CTL_TURBOG | AR5K_CTL_NO_REGDOMAIN;
1788 for (i = 0; i < ee->ee_ctls; i++) {
1789 if (ctl_val[i] == ctl_mode) {
1795 /* If we have a CTL dataset available grab it and find the
1796 * edge power for our frequency */
1797 if (ctl_idx == 0xFF)
1800 /* Edge powers are sorted by frequency from lower
1801 * to higher. Each CTL corresponds to 8 edge power
1803 rep_idx = ctl_idx * AR5K_EEPROM_N_EDGES;
1805 /* Don't do boundaries check because we
1806 * might have more that one bands defined
1809 /* Get the edge power that's closer to our
1811 for (i = 0; i < AR5K_EEPROM_N_EDGES; i++) {
1813 if (target <= rep[rep_idx].freq)
1814 edge_pwr = (s16) rep[rep_idx].edge;
1818 ah->ah_txpower.txp_max_pwr = 4*min(edge_pwr, max_chan_pwr);
1824 * Power to PCDAC table functions
1828 * Fill Power to PCDAC table on RF5111
1830 * No further processing is needed for RF5111, the only thing we have to
1831 * do is fill the values below and above calibration range since eeprom data
1832 * may not cover the entire PCDAC table.
1835 ath5k_fill_pwr_to_pcdac_table(struct ath5k_hw *ah, s16* table_min,
1838 u8 *pcdac_out = ah->ah_txpower.txp_pd_table;
1839 u8 *pcdac_tmp = ah->ah_txpower.tmpL[0];
1840 u8 pcdac_0, pcdac_n, pcdac_i, pwr_idx, i;
1841 s16 min_pwr, max_pwr;
1843 /* Get table boundaries */
1844 min_pwr = table_min[0];
1845 pcdac_0 = pcdac_tmp[0];
1847 max_pwr = table_max[0];
1848 pcdac_n = pcdac_tmp[table_max[0] - table_min[0]];
1850 /* Extrapolate below minimum using pcdac_0 */
1852 for (i = 0; i < min_pwr; i++)
1853 pcdac_out[pcdac_i++] = pcdac_0;
1855 /* Copy values from pcdac_tmp */
1857 for (i = 0 ; pwr_idx <= max_pwr &&
1858 pcdac_i < AR5K_EEPROM_POWER_TABLE_SIZE; i++) {
1859 pcdac_out[pcdac_i++] = pcdac_tmp[i];
1863 /* Extrapolate above maximum */
1864 while (pcdac_i < AR5K_EEPROM_POWER_TABLE_SIZE)
1865 pcdac_out[pcdac_i++] = pcdac_n;
1870 * Combine available XPD Curves and fill Linear Power to PCDAC table
1873 * RFX112 can have up to 2 curves (one for low txpower range and one for
1874 * higher txpower range). We need to put them both on pcdac_out and place
1875 * them in the correct location. In case we only have one curve available
1876 * just fit it on pcdac_out (it's supposed to cover the entire range of
1877 * available pwr levels since it's always the higher power curve). Extrapolate
1878 * below and above final table if needed.
1881 ath5k_combine_linear_pcdac_curves(struct ath5k_hw *ah, s16* table_min,
1882 s16 *table_max, u8 pdcurves)
1884 u8 *pcdac_out = ah->ah_txpower.txp_pd_table;
1891 s16 mid_pwr_idx = 0;
1892 /* Edge flag turs on the 7nth bit on the PCDAC
1893 * to delcare the higher power curve (force values
1894 * to be greater than 64). If we only have one curve
1895 * we don't need to set this, if we have 2 curves and
1896 * fill the table backwards this can also be used to
1897 * switch from higher power curve to lower power curve */
1901 /* When we have only one curve available
1902 * that's the higher power curve. If we have
1903 * two curves the first is the high power curve
1904 * and the next is the low power curve. */
1906 pcdac_low_pwr = ah->ah_txpower.tmpL[1];
1907 pcdac_high_pwr = ah->ah_txpower.tmpL[0];
1908 mid_pwr_idx = table_max[1] - table_min[1] - 1;
1909 max_pwr_idx = (table_max[0] - table_min[0]) / 2;
1911 /* If table size goes beyond 31.5dB, keep the
1912 * upper 31.5dB range when setting tx power.
1913 * Note: 126 = 31.5 dB in quarter dB steps */
1914 if (table_max[0] - table_min[1] > 126)
1915 min_pwr_idx = table_max[0] - 126;
1917 min_pwr_idx = table_min[1];
1919 /* Since we fill table backwards
1920 * start from high power curve */
1921 pcdac_tmp = pcdac_high_pwr;
1925 pcdac_low_pwr = ah->ah_txpower.tmpL[1]; /* Zeroed */
1926 pcdac_high_pwr = ah->ah_txpower.tmpL[0];
1927 min_pwr_idx = table_min[0];
1928 max_pwr_idx = (table_max[0] - table_min[0]) / 2;
1929 pcdac_tmp = pcdac_high_pwr;
1933 /* This is used when setting tx power*/
1934 ah->ah_txpower.txp_min_idx = min_pwr_idx/2;
1936 /* Fill Power to PCDAC table backwards */
1938 for (i = 63; i >= 0; i--) {
1939 /* Entering lower power range, reset
1940 * edge flag and set pcdac_tmp to lower
1942 if (edge_flag == 0x40 &&
1943 (2*pwr <= (table_max[1] - table_min[0]) || pwr == 0)) {
1945 pcdac_tmp = pcdac_low_pwr;
1946 pwr = mid_pwr_idx/2;
1949 /* Don't go below 1, extrapolate below if we have
1950 * already swithced to the lower power curve -or
1951 * we only have one curve and edge_flag is zero
1953 if (pcdac_tmp[pwr] < 1 && (edge_flag == 0x00)) {
1955 pcdac_out[i] = pcdac_out[i + 1];
1961 pcdac_out[i] = pcdac_tmp[pwr] | edge_flag;
1963 /* Extrapolate above if pcdac is greater than
1964 * 126 -this can happen because we OR pcdac_out
1965 * value with edge_flag on high power curve */
1966 if (pcdac_out[i] > 126)
1969 /* Decrease by a 0.5dB step */
1974 /* Write PCDAC values on hw */
1976 ath5k_setup_pcdac_table(struct ath5k_hw *ah)
1978 u8 *pcdac_out = ah->ah_txpower.txp_pd_table;
1982 * Write TX power values
1984 for (i = 0; i < (AR5K_EEPROM_POWER_TABLE_SIZE / 2); i++) {
1985 ath5k_hw_reg_write(ah,
1986 (((pcdac_out[2*i + 0] << 8 | 0xff) & 0xffff) << 0) |
1987 (((pcdac_out[2*i + 1] << 8 | 0xff) & 0xffff) << 16),
1988 AR5K_PHY_PCDAC_TXPOWER(i));
1994 * Power to PDADC table functions
1998 * Set the gain boundaries and create final Power to PDADC table
2000 * We can have up to 4 pd curves, we need to do a simmilar process
2001 * as we do for RF5112. This time we don't have an edge_flag but we
2002 * set the gain boundaries on a separate register.
2005 ath5k_combine_pwr_to_pdadc_curves(struct ath5k_hw *ah,
2006 s16 *pwr_min, s16 *pwr_max, u8 pdcurves)
2008 u8 gain_boundaries[AR5K_EEPROM_N_PD_GAINS];
2009 u8 *pdadc_out = ah->ah_txpower.txp_pd_table;
2012 u8 pdadc_i, pdadc_n, pwr_step, pdg, max_idx, table_size;
2015 /* Note: Register value is initialized on initvals
2016 * there is no feedback from hw.
2017 * XXX: What about pd_gain_overlap from EEPROM ? */
2018 pd_gain_overlap = (u8) ath5k_hw_reg_read(ah, AR5K_PHY_TPC_RG5) &
2019 AR5K_PHY_TPC_RG5_PD_GAIN_OVERLAP;
2021 /* Create final PDADC table */
2022 for (pdg = 0, pdadc_i = 0; pdg < pdcurves; pdg++) {
2023 pdadc_tmp = ah->ah_txpower.tmpL[pdg];
2025 if (pdg == pdcurves - 1)
2026 /* 2 dB boundary stretch for last
2027 * (higher power) curve */
2028 gain_boundaries[pdg] = pwr_max[pdg] + 4;
2030 /* Set gain boundary in the middle
2031 * between this curve and the next one */
2032 gain_boundaries[pdg] =
2033 (pwr_max[pdg] + pwr_min[pdg + 1]) / 2;
2035 /* Sanity check in case our 2 db stretch got out of
2037 if (gain_boundaries[pdg] > AR5K_TUNE_MAX_TXPOWER)
2038 gain_boundaries[pdg] = AR5K_TUNE_MAX_TXPOWER;
2040 /* For the first curve (lower power)
2041 * start from 0 dB */
2045 /* For the other curves use the gain overlap */
2046 pdadc_0 = (gain_boundaries[pdg - 1] - pwr_min[pdg]) -
2049 /* Force each power step to be at least 0.5 dB */
2050 if ((pdadc_tmp[1] - pdadc_tmp[0]) > 1)
2051 pwr_step = pdadc_tmp[1] - pdadc_tmp[0];
2055 /* If pdadc_0 is negative, we need to extrapolate
2056 * below this pdgain by a number of pwr_steps */
2057 while ((pdadc_0 < 0) && (pdadc_i < 128)) {
2058 s16 tmp = pdadc_tmp[0] + pdadc_0 * pwr_step;
2059 pdadc_out[pdadc_i++] = (tmp < 0) ? 0 : (u8) tmp;
2063 /* Set last pwr level, using gain boundaries */
2064 pdadc_n = gain_boundaries[pdg] + pd_gain_overlap - pwr_min[pdg];
2065 /* Limit it to be inside pwr range */
2066 table_size = pwr_max[pdg] - pwr_min[pdg];
2067 max_idx = (pdadc_n < table_size) ? pdadc_n : table_size;
2069 /* Fill pdadc_out table */
2070 while (pdadc_0 < max_idx)
2071 pdadc_out[pdadc_i++] = pdadc_tmp[pdadc_0++];
2073 /* Need to extrapolate above this pdgain? */
2074 if (pdadc_n <= max_idx)
2077 /* Force each power step to be at least 0.5 dB */
2078 if ((pdadc_tmp[table_size - 1] - pdadc_tmp[table_size - 2]) > 1)
2079 pwr_step = pdadc_tmp[table_size - 1] -
2080 pdadc_tmp[table_size - 2];
2084 /* Extrapolate above */
2085 while ((pdadc_0 < (s16) pdadc_n) &&
2086 (pdadc_i < AR5K_EEPROM_POWER_TABLE_SIZE * 2)) {
2087 s16 tmp = pdadc_tmp[table_size - 1] +
2088 (pdadc_0 - max_idx) * pwr_step;
2089 pdadc_out[pdadc_i++] = (tmp > 127) ? 127 : (u8) tmp;
2094 while (pdg < AR5K_EEPROM_N_PD_GAINS) {
2095 gain_boundaries[pdg] = gain_boundaries[pdg - 1];
2099 while (pdadc_i < AR5K_EEPROM_POWER_TABLE_SIZE * 2) {
2100 pdadc_out[pdadc_i] = pdadc_out[pdadc_i - 1];
2104 /* Set gain boundaries */
2105 ath5k_hw_reg_write(ah,
2106 AR5K_REG_SM(pd_gain_overlap,
2107 AR5K_PHY_TPC_RG5_PD_GAIN_OVERLAP) |
2108 AR5K_REG_SM(gain_boundaries[0],
2109 AR5K_PHY_TPC_RG5_PD_GAIN_BOUNDARY_1) |
2110 AR5K_REG_SM(gain_boundaries[1],
2111 AR5K_PHY_TPC_RG5_PD_GAIN_BOUNDARY_2) |
2112 AR5K_REG_SM(gain_boundaries[2],
2113 AR5K_PHY_TPC_RG5_PD_GAIN_BOUNDARY_3) |
2114 AR5K_REG_SM(gain_boundaries[3],
2115 AR5K_PHY_TPC_RG5_PD_GAIN_BOUNDARY_4),
2118 /* Used for setting rate power table */
2119 ah->ah_txpower.txp_min_idx = pwr_min[0];
2123 /* Write PDADC values on hw */
2125 ath5k_setup_pwr_to_pdadc_table(struct ath5k_hw *ah,
2126 u8 pdcurves, u8 *pdg_to_idx)
2128 u8 *pdadc_out = ah->ah_txpower.txp_pd_table;
2132 /* Select the right pdgain curves */
2134 /* Clear current settings */
2135 reg = ath5k_hw_reg_read(ah, AR5K_PHY_TPC_RG1);
2136 reg &= ~(AR5K_PHY_TPC_RG1_PDGAIN_1 |
2137 AR5K_PHY_TPC_RG1_PDGAIN_2 |
2138 AR5K_PHY_TPC_RG1_PDGAIN_3 |
2139 AR5K_PHY_TPC_RG1_NUM_PD_GAIN);
2142 * Use pd_gains curve from eeprom
2144 * This overrides the default setting from initvals
2145 * in case some vendors (e.g. Zcomax) don't use the default
2146 * curves. If we don't honor their settings we 'll get a
2147 * 5dB (1 * gain overlap ?) drop.
2149 reg |= AR5K_REG_SM(pdcurves, AR5K_PHY_TPC_RG1_NUM_PD_GAIN);
2153 reg |= AR5K_REG_SM(pdg_to_idx[2], AR5K_PHY_TPC_RG1_PDGAIN_3);
2156 reg |= AR5K_REG_SM(pdg_to_idx[1], AR5K_PHY_TPC_RG1_PDGAIN_2);
2159 reg |= AR5K_REG_SM(pdg_to_idx[0], AR5K_PHY_TPC_RG1_PDGAIN_1);
2162 ath5k_hw_reg_write(ah, reg, AR5K_PHY_TPC_RG1);
2165 * Write TX power values
2167 for (i = 0; i < (AR5K_EEPROM_POWER_TABLE_SIZE / 2); i++) {
2168 ath5k_hw_reg_write(ah,
2169 ((pdadc_out[4*i + 0] & 0xff) << 0) |
2170 ((pdadc_out[4*i + 1] & 0xff) << 8) |
2171 ((pdadc_out[4*i + 2] & 0xff) << 16) |
2172 ((pdadc_out[4*i + 3] & 0xff) << 24),
2173 AR5K_PHY_PDADC_TXPOWER(i));
2179 * Common code for PCDAC/PDADC tables
2183 * This is the main function that uses all of the above
2184 * to set PCDAC/PDADC table on hw for the current channel.
2185 * This table is used for tx power calibration on the basband,
2186 * without it we get weird tx power levels and in some cases
2187 * distorted spectral mask
2190 ath5k_setup_channel_powertable(struct ath5k_hw *ah,
2191 struct net80211_channel *channel,
2192 u8 ee_mode, u8 type)
2194 struct ath5k_pdgain_info *pdg_L, *pdg_R;
2195 struct ath5k_chan_pcal_info *pcinfo_L;
2196 struct ath5k_chan_pcal_info *pcinfo_R;
2197 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
2198 u8 *pdg_curve_to_idx = ee->ee_pdc_to_idx[ee_mode];
2199 s16 table_min[AR5K_EEPROM_N_PD_GAINS];
2200 s16 table_max[AR5K_EEPROM_N_PD_GAINS];
2203 u32 target = channel->center_freq;
2206 /* Get surounding freq piers for this channel */
2207 ath5k_get_chan_pcal_surrounding_piers(ah, channel,
2211 /* Loop over pd gain curves on
2212 * surounding freq piers by index */
2213 for (pdg = 0; pdg < ee->ee_pd_gains[ee_mode]; pdg++) {
2215 /* Fill curves in reverse order
2216 * from lower power (max gain)
2217 * to higher power. Use curve -> idx
2218 * backmaping we did on eeprom init */
2219 u8 idx = pdg_curve_to_idx[pdg];
2221 /* Grab the needed curves by index */
2222 pdg_L = &pcinfo_L->pd_curves[idx];
2223 pdg_R = &pcinfo_R->pd_curves[idx];
2225 /* Initialize the temp tables */
2226 tmpL = ah->ah_txpower.tmpL[pdg];
2227 tmpR = ah->ah_txpower.tmpR[pdg];
2229 /* Set curve's x boundaries and create
2230 * curves so that they cover the same
2231 * range (if we don't do that one table
2232 * will have values on some range and the
2233 * other one won't have any so interpolation
2235 table_min[pdg] = min(pdg_L->pd_pwr[0],
2236 pdg_R->pd_pwr[0]) / 2;
2238 table_max[pdg] = max(pdg_L->pd_pwr[pdg_L->pd_points - 1],
2239 pdg_R->pd_pwr[pdg_R->pd_points - 1]) / 2;
2241 /* Now create the curves on surrounding channels
2242 * and interpolate if needed to get the final
2243 * curve for this gain on this channel */
2245 case AR5K_PWRTABLE_LINEAR_PCDAC:
2246 /* Override min/max so that we don't loose
2247 * accuracy (don't divide by 2) */
2248 table_min[pdg] = min(pdg_L->pd_pwr[0],
2252 max(pdg_L->pd_pwr[pdg_L->pd_points - 1],
2253 pdg_R->pd_pwr[pdg_R->pd_points - 1]);
2255 /* Override minimum so that we don't get
2256 * out of bounds while extrapolating
2257 * below. Don't do this when we have 2
2258 * curves and we are on the high power curve
2259 * because table_min is ok in this case */
2260 if (!(ee->ee_pd_gains[ee_mode] > 1 && pdg == 0)) {
2263 ath5k_get_linear_pcdac_min(pdg_L->pd_step,
2268 /* Don't go too low because we will
2269 * miss the upper part of the curve.
2270 * Note: 126 = 31.5dB (max power supported)
2271 * in 0.25dB units */
2272 if (table_max[pdg] - table_min[pdg] > 126)
2273 table_min[pdg] = table_max[pdg] - 126;
2277 case AR5K_PWRTABLE_PWR_TO_PCDAC:
2278 case AR5K_PWRTABLE_PWR_TO_PDADC:
2280 ath5k_create_power_curve(table_min[pdg],
2284 pdg_L->pd_points, tmpL, type);
2286 /* We are in a calibration
2287 * pier, no need to interpolate
2288 * between freq piers */
2289 if (pcinfo_L == pcinfo_R)
2292 ath5k_create_power_curve(table_min[pdg],
2296 pdg_R->pd_points, tmpR, type);
2302 /* Interpolate between curves
2303 * of surounding freq piers to
2304 * get the final curve for this
2305 * pd gain. Re-use tmpL for interpolation
2307 for (i = 0; (i < (u16) (table_max[pdg] - table_min[pdg])) &&
2308 (i < AR5K_EEPROM_POWER_TABLE_SIZE); i++) {
2309 tmpL[i] = (u8) ath5k_get_interpolated_value(target,
2310 (s16) pcinfo_L->freq,
2311 (s16) pcinfo_R->freq,
2317 /* Now we have a set of curves for this
2318 * channel on tmpL (x range is table_max - table_min
2319 * and y values are tmpL[pdg][]) sorted in the same
2320 * order as EEPROM (because we've used the backmaping).
2321 * So for RF5112 it's from higher power to lower power
2322 * and for RF2413 it's from lower power to higher power.
2323 * For RF5111 we only have one curve. */
2325 /* Fill min and max power levels for this
2326 * channel by interpolating the values on
2327 * surounding channels to complete the dataset */
2328 ah->ah_txpower.txp_min_pwr = ath5k_get_interpolated_value(target,
2329 (s16) pcinfo_L->freq,
2330 (s16) pcinfo_R->freq,
2331 pcinfo_L->min_pwr, pcinfo_R->min_pwr);
2333 ah->ah_txpower.txp_max_pwr = ath5k_get_interpolated_value(target,
2334 (s16) pcinfo_L->freq,
2335 (s16) pcinfo_R->freq,
2336 pcinfo_L->max_pwr, pcinfo_R->max_pwr);
2338 /* We are ready to go, fill PCDAC/PDADC
2339 * table and write settings on hardware */
2341 case AR5K_PWRTABLE_LINEAR_PCDAC:
2342 /* For RF5112 we can have one or two curves
2343 * and each curve covers a certain power lvl
2344 * range so we need to do some more processing */
2345 ath5k_combine_linear_pcdac_curves(ah, table_min, table_max,
2346 ee->ee_pd_gains[ee_mode]);
2348 /* Set txp.offset so that we can
2349 * match max power value with max
2351 ah->ah_txpower.txp_offset = 64 - (table_max[0] / 2);
2353 /* Write settings on hw */
2354 ath5k_setup_pcdac_table(ah);
2356 case AR5K_PWRTABLE_PWR_TO_PCDAC:
2357 /* We are done for RF5111 since it has only
2358 * one curve, just fit the curve on the table */
2359 ath5k_fill_pwr_to_pcdac_table(ah, table_min, table_max);
2361 /* No rate powertable adjustment for RF5111 */
2362 ah->ah_txpower.txp_min_idx = 0;
2363 ah->ah_txpower.txp_offset = 0;
2365 /* Write settings on hw */
2366 ath5k_setup_pcdac_table(ah);
2368 case AR5K_PWRTABLE_PWR_TO_PDADC:
2369 /* Set PDADC boundaries and fill
2370 * final PDADC table */
2371 ath5k_combine_pwr_to_pdadc_curves(ah, table_min, table_max,
2372 ee->ee_pd_gains[ee_mode]);
2374 /* Write settings on hw */
2375 ath5k_setup_pwr_to_pdadc_table(ah, pdg, pdg_curve_to_idx);
2377 /* Set txp.offset, note that table_min
2378 * can be negative */
2379 ah->ah_txpower.txp_offset = table_min[0];
2390 * Per-rate tx power setting
2392 * This is the code that sets the desired tx power (below
2393 * maximum) on hw for each rate (we also have TPC that sets
2394 * power per packet). We do that by providing an index on the
2395 * PCDAC/PDADC table we set up.
2399 * Set rate power table
2401 * For now we only limit txpower based on maximum tx power
2402 * supported by hw (what's inside rate_info). We need to limit
2403 * this even more, based on regulatory domain etc.
2405 * Rate power table contains indices to PCDAC/PDADC table (0.5dB steps)
2406 * and is indexed as follows:
2407 * rates[0] - rates[7] -> OFDM rates
2408 * rates[8] - rates[14] -> CCK rates
2409 * rates[15] -> XR rates (they all have the same power)
2412 ath5k_setup_rate_powertable(struct ath5k_hw *ah, u16 max_pwr,
2413 struct ath5k_rate_pcal_info *rate_info,
2419 /* max_pwr is power level we got from driver/user in 0.5dB
2420 * units, switch to 0.25dB units so we can compare */
2422 max_pwr = min(max_pwr, (u16) ah->ah_txpower.txp_max_pwr) / 2;
2424 /* apply rate limits */
2425 rates = ah->ah_txpower.txp_rates_power_table;
2427 /* OFDM rates 6 to 24Mb/s */
2428 for (i = 0; i < 5; i++)
2429 rates[i] = min(max_pwr, rate_info->target_power_6to24);
2431 /* Rest OFDM rates */
2432 rates[5] = min(rates[0], rate_info->target_power_36);
2433 rates[6] = min(rates[0], rate_info->target_power_48);
2434 rates[7] = min(rates[0], rate_info->target_power_54);
2438 rates[8] = min(rates[0], rate_info->target_power_6to24);
2440 rates[9] = min(rates[0], rate_info->target_power_36);
2442 rates[10] = min(rates[0], rate_info->target_power_36);
2444 rates[11] = min(rates[0], rate_info->target_power_48);
2446 rates[12] = min(rates[0], rate_info->target_power_48);
2448 rates[13] = min(rates[0], rate_info->target_power_54);
2450 rates[14] = min(rates[0], rate_info->target_power_54);
2453 rates[15] = min(rates[0], rate_info->target_power_6to24);
2455 /* CCK rates have different peak to average ratio
2456 * so we have to tweak their power so that gainf
2457 * correction works ok. For this we use OFDM to
2458 * CCK delta from eeprom */
2459 if ((ee_mode == AR5K_EEPROM_MODE_11G) &&
2460 (ah->ah_phy_revision < AR5K_SREV_PHY_5212A))
2461 for (i = 8; i <= 15; i++)
2462 rates[i] -= ah->ah_txpower.txp_cck_ofdm_gainf_delta;
2464 ah->ah_txpower.txp_min_pwr = rates[7];
2465 ah->ah_txpower.txp_max_pwr = rates[0];
2466 ah->ah_txpower.txp_ofdm = rates[7];
2471 * Set transmition power
2474 ath5k_hw_txpower(struct ath5k_hw *ah, struct net80211_channel *channel,
2475 u8 ee_mode, u8 txpower)
2477 struct ath5k_rate_pcal_info rate_info;
2481 if (txpower > AR5K_TUNE_MAX_TXPOWER) {
2482 DBG("ath5k: invalid tx power %d\n", txpower);
2486 txpower = AR5K_TUNE_DEFAULT_TXPOWER;
2488 /* Reset TX power values */
2489 memset(&ah->ah_txpower, 0, sizeof(ah->ah_txpower));
2490 ah->ah_txpower.txp_tpc = AR5K_TUNE_TPC_TXPOWER;
2491 ah->ah_txpower.txp_min_pwr = 0;
2492 ah->ah_txpower.txp_max_pwr = AR5K_TUNE_MAX_TXPOWER;
2494 /* Initialize TX power table */
2495 switch (ah->ah_radio) {
2497 type = AR5K_PWRTABLE_PWR_TO_PCDAC;
2500 type = AR5K_PWRTABLE_LINEAR_PCDAC;
2507 type = AR5K_PWRTABLE_PWR_TO_PDADC;
2513 /* FIXME: Only on channel/mode change */
2514 ret = ath5k_setup_channel_powertable(ah, channel, ee_mode, type);
2518 /* Limit max power if we have a CTL available */
2519 ath5k_get_max_ctl_power(ah, channel);
2521 /* FIXME: Tx power limit for this regdomain
2522 * XXX: Mac80211/CRDA will do that anyway ? */
2524 /* FIXME: Antenna reduction stuff */
2526 /* FIXME: Limit power on turbo modes */
2528 /* FIXME: TPC scale reduction */
2530 /* Get surounding channels for per-rate power table
2532 ath5k_get_rate_pcal_data(ah, channel, &rate_info);
2534 /* Setup rate power table */
2535 ath5k_setup_rate_powertable(ah, txpower, &rate_info, ee_mode);
2537 /* Write rate power table on hw */
2538 ath5k_hw_reg_write(ah, AR5K_TXPOWER_OFDM(3, 24) |
2539 AR5K_TXPOWER_OFDM(2, 16) | AR5K_TXPOWER_OFDM(1, 8) |
2540 AR5K_TXPOWER_OFDM(0, 0), AR5K_PHY_TXPOWER_RATE1);
2542 ath5k_hw_reg_write(ah, AR5K_TXPOWER_OFDM(7, 24) |
2543 AR5K_TXPOWER_OFDM(6, 16) | AR5K_TXPOWER_OFDM(5, 8) |
2544 AR5K_TXPOWER_OFDM(4, 0), AR5K_PHY_TXPOWER_RATE2);
2546 ath5k_hw_reg_write(ah, AR5K_TXPOWER_CCK(10, 24) |
2547 AR5K_TXPOWER_CCK(9, 16) | AR5K_TXPOWER_CCK(15, 8) |
2548 AR5K_TXPOWER_CCK(8, 0), AR5K_PHY_TXPOWER_RATE3);
2550 ath5k_hw_reg_write(ah, AR5K_TXPOWER_CCK(14, 24) |
2551 AR5K_TXPOWER_CCK(13, 16) | AR5K_TXPOWER_CCK(12, 8) |
2552 AR5K_TXPOWER_CCK(11, 0), AR5K_PHY_TXPOWER_RATE4);
2554 /* FIXME: TPC support */
2555 if (ah->ah_txpower.txp_tpc) {
2556 ath5k_hw_reg_write(ah, AR5K_PHY_TXPOWER_RATE_MAX_TPC_ENABLE |
2557 AR5K_TUNE_MAX_TXPOWER, AR5K_PHY_TXPOWER_RATE_MAX);
2559 ath5k_hw_reg_write(ah,
2560 AR5K_REG_MS(AR5K_TUNE_MAX_TXPOWER, AR5K_TPC_ACK) |
2561 AR5K_REG_MS(AR5K_TUNE_MAX_TXPOWER, AR5K_TPC_CTS) |
2562 AR5K_REG_MS(AR5K_TUNE_MAX_TXPOWER, AR5K_TPC_CHIRP),
2565 ath5k_hw_reg_write(ah, AR5K_PHY_TXPOWER_RATE_MAX |
2566 AR5K_TUNE_MAX_TXPOWER, AR5K_PHY_TXPOWER_RATE_MAX);
2572 int ath5k_hw_set_txpower_limit(struct ath5k_hw *ah, u8 mode, u8 txpower)
2574 struct net80211_channel *channel = ah->ah_current_channel;
2576 DBG2("ath5k: changing txpower to %d\n", txpower);
2578 return ath5k_hw_txpower(ah, channel, mode, txpower);