blob: 2b298ea869d6278b5afb7d3e6d0885829408af29 [file] [log] [blame]
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001/*
2 * PHY functions
3 *
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03004 * Copyright (c) 2004-2007 Reyk Floeter <reyk@openbsd.org>
Nick Kossifidis33a31822009-02-09 06:00:34 +02005 * Copyright (c) 2006-2009 Nick Kossifidis <mickflemm@gmail.com>
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03006 * Copyright (c) 2007-2008 Jiri Slaby <jirislaby@gmail.com>
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02007 * Copyright (c) 2008-2009 Felix Fietkau <nbd@openwrt.org>
Jiri Slabyfa1c1142007-08-12 17:33:16 +02008 *
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 *
21 */
22
23#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Jiri Slabyfa1c1142007-08-12 17:33:16 +020025
26#include "ath5k.h"
27#include "reg.h"
28#include "base.h"
Nick Kossifidis33a31822009-02-09 06:00:34 +020029#include "rfbuffer.h"
30#include "rfgain.h"
Jiri Slabyfa1c1142007-08-12 17:33:16 +020031
32/*
33 * Used to modify RF Banks before writing them to AR5K_RF_BUFFER
34 */
Nick Kossifidis8892e4e2009-02-09 06:06:34 +020035static unsigned int ath5k_hw_rfb_op(struct ath5k_hw *ah,
36 const struct ath5k_rf_reg *rf_regs,
37 u32 val, u8 reg_id, bool set)
Jiri Slabyfa1c1142007-08-12 17:33:16 +020038{
Nick Kossifidis8892e4e2009-02-09 06:06:34 +020039 const struct ath5k_rf_reg *rfreg = NULL;
40 u8 offset, bank, num_bits, col, position;
41 u16 entry;
42 u32 mask, data, last_bit, bits_shifted, first_bit;
43 u32 *rfb;
44 s32 bits_left;
Jiri Slabyfa1c1142007-08-12 17:33:16 +020045 int i;
46
47 data = 0;
Nick Kossifidis8892e4e2009-02-09 06:06:34 +020048 rfb = ah->ah_rf_banks;
Jiri Slabyfa1c1142007-08-12 17:33:16 +020049
Nick Kossifidis8892e4e2009-02-09 06:06:34 +020050 for (i = 0; i < ah->ah_rf_regs_count; i++) {
51 if (rf_regs[i].index == reg_id) {
52 rfreg = &rf_regs[i];
53 break;
54 }
55 }
56
57 if (rfb == NULL || rfreg == NULL) {
58 ATH5K_PRINTF("Rf register not found!\n");
Jiri Slabyfa1c1142007-08-12 17:33:16 +020059 /* should not happen */
60 return 0;
Nick Kossifidis8892e4e2009-02-09 06:06:34 +020061 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +020062
Nick Kossifidis8892e4e2009-02-09 06:06:34 +020063 bank = rfreg->bank;
64 num_bits = rfreg->field.len;
65 first_bit = rfreg->field.pos;
66 col = rfreg->field.col;
67
68 /* first_bit is an offset from bank's
69 * start. Since we have all banks on
70 * the same array, we use this offset
71 * to mark each bank's start */
72 offset = ah->ah_offset[bank];
73
74 /* Boundary check */
75 if (!(col <= 3 && num_bits <= 32 && first_bit + num_bits <= 319)) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +020076 ATH5K_PRINTF("invalid values at offset %u\n", offset);
77 return 0;
78 }
79
Nick Kossifidis8892e4e2009-02-09 06:06:34 +020080 entry = ((first_bit - 1) / 8) + offset;
81 position = (first_bit - 1) % 8;
Jiri Slabyfa1c1142007-08-12 17:33:16 +020082
Joe Perchese9010e22008-03-07 14:21:16 -080083 if (set)
Nick Kossifidis8892e4e2009-02-09 06:06:34 +020084 data = ath5k_hw_bitswap(val, num_bits);
Jiri Slabyfa1c1142007-08-12 17:33:16 +020085
Nick Kossifidis8892e4e2009-02-09 06:06:34 +020086 for (bits_shifted = 0, bits_left = num_bits; bits_left > 0;
87 position = 0, entry++) {
88
89 last_bit = (position + bits_left > 8) ? 8 :
90 position + bits_left;
91
92 mask = (((1 << last_bit) - 1) ^ ((1 << position) - 1)) <<
93 (col * 8);
Jiri Slabyfa1c1142007-08-12 17:33:16 +020094
Joe Perchese9010e22008-03-07 14:21:16 -080095 if (set) {
Nick Kossifidis8892e4e2009-02-09 06:06:34 +020096 rfb[entry] &= ~mask;
97 rfb[entry] |= ((data << position) << (col * 8)) & mask;
Jiri Slabyfa1c1142007-08-12 17:33:16 +020098 data >>= (8 - position);
99 } else {
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200100 data |= (((rfb[entry] & mask) >> (col * 8)) >> position)
101 << bits_shifted;
102 bits_shifted += last_bit - position;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200103 }
104
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200105 bits_left -= 8 - position;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200106 }
107
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200108 data = set ? 1 : ath5k_hw_bitswap(data, num_bits);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200109
110 return data;
111}
112
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200113/**********************\
114* RF Gain optimization *
115\**********************/
116
117/*
118 * This code is used to optimize rf gain on different environments
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200119 * (temperature mostly) based on feedback from a power detector.
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200120 *
121 * It's only used on RF5111 and RF5112, later RF chips seem to have
122 * auto adjustment on hw -notice they have a much smaller BANK 7 and
123 * no gain optimization ladder-.
124 *
125 * For more infos check out this patent doc
126 * http://www.freepatentsonline.com/7400691.html
127 *
128 * This paper describes power drops as seen on the receiver due to
129 * probe packets
130 * http://www.cnri.dit.ie/publications/ICT08%20-%20Practical%20Issues
131 * %20of%20Power%20Control.pdf
132 *
133 * And this is the MadWiFi bug entry related to the above
134 * http://madwifi-project.org/ticket/1659
135 * with various measurements and diagrams
136 *
137 * TODO: Deal with power drops due to probes by setting an apropriate
138 * tx power on the probe packets ! Make this part of the calibration process.
139 */
140
141/* Initialize ah_gain durring attach */
142int ath5k_hw_rfgain_opt_init(struct ath5k_hw *ah)
143{
144 /* Initialize the gain optimization values */
145 switch (ah->ah_radio) {
146 case AR5K_RF5111:
147 ah->ah_gain.g_step_idx = rfgain_opt_5111.go_default;
148 ah->ah_gain.g_low = 20;
149 ah->ah_gain.g_high = 35;
150 ah->ah_gain.g_state = AR5K_RFGAIN_ACTIVE;
151 break;
152 case AR5K_RF5112:
153 ah->ah_gain.g_step_idx = rfgain_opt_5112.go_default;
154 ah->ah_gain.g_low = 20;
155 ah->ah_gain.g_high = 85;
156 ah->ah_gain.g_state = AR5K_RFGAIN_ACTIVE;
157 break;
158 default:
159 return -EINVAL;
160 }
161
162 return 0;
163}
164
165/* Schedule a gain probe check on the next transmited packet.
166 * That means our next packet is going to be sent with lower
167 * tx power and a Peak to Average Power Detector (PAPD) will try
168 * to measure the gain.
169 *
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200170 * XXX: How about forcing a tx packet (bypassing PCU arbitrator etc)
171 * just after we enable the probe so that we don't mess with
172 * standard traffic ? Maybe it's time to use sw interrupts and
173 * a probe tasklet !!!
174 */
175static void ath5k_hw_request_rfgain_probe(struct ath5k_hw *ah)
176{
177
178 /* Skip if gain calibration is inactive or
179 * we already handle a probe request */
180 if (ah->ah_gain.g_state != AR5K_RFGAIN_ACTIVE)
181 return;
182
Nick Kossifidis8f655dd2009-03-15 22:20:35 +0200183 /* Send the packet with 2dB below max power as
184 * patent doc suggest */
Nick Kossifidisa0823812009-04-30 15:55:44 -0400185 ath5k_hw_reg_write(ah, AR5K_REG_SM(ah->ah_txpower.txp_ofdm - 4,
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200186 AR5K_PHY_PAPD_PROBE_TXPOWER) |
187 AR5K_PHY_PAPD_PROBE_TX_NEXT, AR5K_PHY_PAPD_PROBE);
188
189 ah->ah_gain.g_state = AR5K_RFGAIN_READ_REQUESTED;
190
191}
192
193/* Calculate gain_F measurement correction
194 * based on the current step for RF5112 rev. 2 */
195static u32 ath5k_hw_rf_gainf_corr(struct ath5k_hw *ah)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200196{
197 u32 mix, step;
198 u32 *rf;
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200199 const struct ath5k_gain_opt *go;
200 const struct ath5k_gain_opt_step *g_step;
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200201 const struct ath5k_rf_reg *rf_regs;
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200202
203 /* Only RF5112 Rev. 2 supports it */
204 if ((ah->ah_radio != AR5K_RF5112) ||
205 (ah->ah_radio_5ghz_revision <= AR5K_SREV_RAD_5112A))
206 return 0;
207
208 go = &rfgain_opt_5112;
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200209 rf_regs = rf_regs_5112a;
210 ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5112a);
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200211
212 g_step = &go->go_step[ah->ah_gain.g_step_idx];
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200213
214 if (ah->ah_rf_banks == NULL)
215 return 0;
216
217 rf = ah->ah_rf_banks;
218 ah->ah_gain.g_f_corr = 0;
219
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200220 /* No VGA (Variable Gain Amplifier) override, skip */
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200221 if (ath5k_hw_rfb_op(ah, rf_regs, 0, AR5K_RF_MIXVGA_OVR, false) != 1)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200222 return 0;
223
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200224 /* Mix gain stepping */
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200225 step = ath5k_hw_rfb_op(ah, rf_regs, 0, AR5K_RF_MIXGAIN_STEP, false);
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200226
227 /* Mix gain override */
228 mix = g_step->gos_param[0];
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200229
230 switch (mix) {
231 case 3:
232 ah->ah_gain.g_f_corr = step * 2;
233 break;
234 case 2:
235 ah->ah_gain.g_f_corr = (step - 5) * 2;
236 break;
237 case 1:
238 ah->ah_gain.g_f_corr = step;
239 break;
240 default:
241 ah->ah_gain.g_f_corr = 0;
242 break;
243 }
244
245 return ah->ah_gain.g_f_corr;
246}
247
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200248/* Check if current gain_F measurement is in the range of our
249 * power detector windows. If we get a measurement outside range
250 * we know it's not accurate (detectors can't measure anything outside
251 * their detection window) so we must ignore it */
252static bool ath5k_hw_rf_check_gainf_readback(struct ath5k_hw *ah)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200253{
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200254 const struct ath5k_rf_reg *rf_regs;
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200255 u32 step, mix_ovr, level[4];
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200256 u32 *rf;
257
258 if (ah->ah_rf_banks == NULL)
259 return false;
260
261 rf = ah->ah_rf_banks;
262
263 if (ah->ah_radio == AR5K_RF5111) {
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200264
265 rf_regs = rf_regs_5111;
266 ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5111);
267
268 step = ath5k_hw_rfb_op(ah, rf_regs, 0, AR5K_RF_RFGAIN_STEP,
269 false);
270
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200271 level[0] = 0;
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200272 level[1] = (step == 63) ? 50 : step + 4;
273 level[2] = (step != 63) ? 64 : level[0];
274 level[3] = level[2] + 50 ;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200275
276 ah->ah_gain.g_high = level[3] -
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200277 (step == 63 ? AR5K_GAIN_DYN_ADJUST_HI_MARGIN : -5);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200278 ah->ah_gain.g_low = level[0] +
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200279 (step == 63 ? AR5K_GAIN_DYN_ADJUST_LO_MARGIN : 0);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200280 } else {
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200281
282 rf_regs = rf_regs_5112;
283 ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5112);
284
285 mix_ovr = ath5k_hw_rfb_op(ah, rf_regs, 0, AR5K_RF_MIXVGA_OVR,
286 false);
287
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200288 level[0] = level[2] = 0;
289
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200290 if (mix_ovr == 1) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200291 level[1] = level[3] = 83;
292 } else {
293 level[1] = level[3] = 107;
294 ah->ah_gain.g_high = 55;
295 }
296 }
297
298 return (ah->ah_gain.g_current >= level[0] &&
299 ah->ah_gain.g_current <= level[1]) ||
300 (ah->ah_gain.g_current >= level[2] &&
301 ah->ah_gain.g_current <= level[3]);
302}
303
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200304/* Perform gain_F adjustment by choosing the right set
305 * of parameters from rf gain optimization ladder */
306static s8 ath5k_hw_rf_gainf_adjust(struct ath5k_hw *ah)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200307{
308 const struct ath5k_gain_opt *go;
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200309 const struct ath5k_gain_opt_step *g_step;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200310 int ret = 0;
311
312 switch (ah->ah_radio) {
313 case AR5K_RF5111:
314 go = &rfgain_opt_5111;
315 break;
316 case AR5K_RF5112:
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200317 go = &rfgain_opt_5112;
318 break;
319 default:
320 return 0;
321 }
322
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200323 g_step = &go->go_step[ah->ah_gain.g_step_idx];
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200324
325 if (ah->ah_gain.g_current >= ah->ah_gain.g_high) {
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200326
327 /* Reached maximum */
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200328 if (ah->ah_gain.g_step_idx == 0)
329 return -1;
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200330
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200331 for (ah->ah_gain.g_target = ah->ah_gain.g_current;
332 ah->ah_gain.g_target >= ah->ah_gain.g_high &&
333 ah->ah_gain.g_step_idx > 0;
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200334 g_step = &go->go_step[ah->ah_gain.g_step_idx])
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200335 ah->ah_gain.g_target -= 2 *
336 (go->go_step[--(ah->ah_gain.g_step_idx)].gos_gain -
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200337 g_step->gos_gain);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200338
339 ret = 1;
340 goto done;
341 }
342
343 if (ah->ah_gain.g_current <= ah->ah_gain.g_low) {
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200344
345 /* Reached minimum */
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200346 if (ah->ah_gain.g_step_idx == (go->go_steps_count - 1))
347 return -2;
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200348
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200349 for (ah->ah_gain.g_target = ah->ah_gain.g_current;
350 ah->ah_gain.g_target <= ah->ah_gain.g_low &&
351 ah->ah_gain.g_step_idx < go->go_steps_count-1;
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200352 g_step = &go->go_step[ah->ah_gain.g_step_idx])
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200353 ah->ah_gain.g_target -= 2 *
354 (go->go_step[++ah->ah_gain.g_step_idx].gos_gain -
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200355 g_step->gos_gain);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200356
357 ret = 2;
358 goto done;
359 }
360
361done:
362 ATH5K_DBG(ah->ah_sc, ATH5K_DEBUG_CALIBRATE,
363 "ret %d, gain step %u, current gain %u, target gain %u\n",
364 ret, ah->ah_gain.g_step_idx, ah->ah_gain.g_current,
365 ah->ah_gain.g_target);
366
367 return ret;
368}
369
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200370/* Main callback for thermal rf gain calibration engine
371 * Check for a new gain reading and schedule an adjustment
372 * if needed.
373 *
374 * TODO: Use sw interrupt to schedule reset if gain_F needs
375 * adjustment */
376enum ath5k_rfgain ath5k_hw_gainf_calibrate(struct ath5k_hw *ah)
377{
378 u32 data, type;
379 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
380
381 ATH5K_TRACE(ah->ah_sc);
382
383 if (ah->ah_rf_banks == NULL ||
384 ah->ah_gain.g_state == AR5K_RFGAIN_INACTIVE)
385 return AR5K_RFGAIN_INACTIVE;
386
387 /* No check requested, either engine is inactive
388 * or an adjustment is already requested */
389 if (ah->ah_gain.g_state != AR5K_RFGAIN_READ_REQUESTED)
390 goto done;
391
392 /* Read the PAPD (Peak to Average Power Detector)
393 * register */
394 data = ath5k_hw_reg_read(ah, AR5K_PHY_PAPD_PROBE);
395
396 /* No probe is scheduled, read gain_F measurement */
397 if (!(data & AR5K_PHY_PAPD_PROBE_TX_NEXT)) {
398 ah->ah_gain.g_current = data >> AR5K_PHY_PAPD_PROBE_GAINF_S;
399 type = AR5K_REG_MS(data, AR5K_PHY_PAPD_PROBE_TYPE);
400
401 /* If tx packet is CCK correct the gain_F measurement
402 * by cck ofdm gain delta */
403 if (type == AR5K_PHY_PAPD_PROBE_TYPE_CCK) {
404 if (ah->ah_radio_5ghz_revision >= AR5K_SREV_RAD_5112A)
405 ah->ah_gain.g_current +=
406 ee->ee_cck_ofdm_gain_delta;
407 else
408 ah->ah_gain.g_current +=
409 AR5K_GAIN_CCK_PROBE_CORR;
410 }
411
412 /* Further correct gain_F measurement for
413 * RF5112A radios */
414 if (ah->ah_radio_5ghz_revision >= AR5K_SREV_RAD_5112A) {
415 ath5k_hw_rf_gainf_corr(ah);
416 ah->ah_gain.g_current =
417 ah->ah_gain.g_current >= ah->ah_gain.g_f_corr ?
418 (ah->ah_gain.g_current-ah->ah_gain.g_f_corr) :
419 0;
420 }
421
422 /* Check if measurement is ok and if we need
423 * to adjust gain, schedule a gain adjustment,
424 * else switch back to the acive state */
425 if (ath5k_hw_rf_check_gainf_readback(ah) &&
426 AR5K_GAIN_CHECK_ADJUST(&ah->ah_gain) &&
427 ath5k_hw_rf_gainf_adjust(ah)) {
428 ah->ah_gain.g_state = AR5K_RFGAIN_NEED_CHANGE;
429 } else {
430 ah->ah_gain.g_state = AR5K_RFGAIN_ACTIVE;
431 }
432 }
433
434done:
435 return ah->ah_gain.g_state;
436}
437
438/* Write initial rf gain table to set the RF sensitivity
439 * this one works on all RF chips and has nothing to do
440 * with gain_F calibration */
441int ath5k_hw_rfgain_init(struct ath5k_hw *ah, unsigned int freq)
442{
443 const struct ath5k_ini_rfgain *ath5k_rfg;
444 unsigned int i, size;
445
446 switch (ah->ah_radio) {
447 case AR5K_RF5111:
448 ath5k_rfg = rfgain_5111;
449 size = ARRAY_SIZE(rfgain_5111);
450 break;
451 case AR5K_RF5112:
452 ath5k_rfg = rfgain_5112;
453 size = ARRAY_SIZE(rfgain_5112);
454 break;
455 case AR5K_RF2413:
456 ath5k_rfg = rfgain_2413;
457 size = ARRAY_SIZE(rfgain_2413);
458 break;
459 case AR5K_RF2316:
460 ath5k_rfg = rfgain_2316;
461 size = ARRAY_SIZE(rfgain_2316);
462 break;
463 case AR5K_RF5413:
464 ath5k_rfg = rfgain_5413;
465 size = ARRAY_SIZE(rfgain_5413);
466 break;
467 case AR5K_RF2317:
468 case AR5K_RF2425:
469 ath5k_rfg = rfgain_2425;
470 size = ARRAY_SIZE(rfgain_2425);
471 break;
472 default:
473 return -EINVAL;
474 }
475
476 switch (freq) {
477 case AR5K_INI_RFGAIN_2GHZ:
478 case AR5K_INI_RFGAIN_5GHZ:
479 break;
480 default:
481 return -EINVAL;
482 }
483
484 for (i = 0; i < size; i++) {
485 AR5K_REG_WAIT(i);
486 ath5k_hw_reg_write(ah, ath5k_rfg[i].rfg_value[freq],
487 (u32)ath5k_rfg[i].rfg_register);
488 }
489
490 return 0;
491}
492
493
494
495/********************\
496* RF Registers setup *
497\********************/
498
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200499
500/*
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200501 * Setup RF registers by writing rf buffer on hw
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200502 */
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200503int ath5k_hw_rfregs_init(struct ath5k_hw *ah, struct ieee80211_channel *channel,
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200504 unsigned int mode)
505{
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200506 const struct ath5k_rf_reg *rf_regs;
507 const struct ath5k_ini_rfbuffer *ini_rfb;
508 const struct ath5k_gain_opt *go = NULL;
509 const struct ath5k_gain_opt_step *g_step;
510 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
511 u8 ee_mode = 0;
512 u32 *rfb;
513 int i, obdb = -1, bank = -1;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200514
515 switch (ah->ah_radio) {
516 case AR5K_RF5111:
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200517 rf_regs = rf_regs_5111;
518 ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5111);
519 ini_rfb = rfb_5111;
520 ah->ah_rf_banks_size = ARRAY_SIZE(rfb_5111);
521 go = &rfgain_opt_5111;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200522 break;
523 case AR5K_RF5112:
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200524 if (ah->ah_radio_5ghz_revision >= AR5K_SREV_RAD_5112A) {
525 rf_regs = rf_regs_5112a;
526 ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5112a);
527 ini_rfb = rfb_5112a;
528 ah->ah_rf_banks_size = ARRAY_SIZE(rfb_5112a);
529 } else {
530 rf_regs = rf_regs_5112;
531 ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5112);
532 ini_rfb = rfb_5112;
533 ah->ah_rf_banks_size = ARRAY_SIZE(rfb_5112);
534 }
535 go = &rfgain_opt_5112;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200536 break;
Nick Kossifidisf714dd62008-02-28 14:43:51 -0500537 case AR5K_RF2413:
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200538 rf_regs = rf_regs_2413;
539 ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_2413);
540 ini_rfb = rfb_2413;
541 ah->ah_rf_banks_size = ARRAY_SIZE(rfb_2413);
542 break;
543 case AR5K_RF2316:
544 rf_regs = rf_regs_2316;
545 ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_2316);
546 ini_rfb = rfb_2316;
547 ah->ah_rf_banks_size = ARRAY_SIZE(rfb_2316);
548 break;
549 case AR5K_RF5413:
550 rf_regs = rf_regs_5413;
551 ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5413);
552 ini_rfb = rfb_5413;
553 ah->ah_rf_banks_size = ARRAY_SIZE(rfb_5413);
554 break;
555 case AR5K_RF2317:
556 rf_regs = rf_regs_2425;
557 ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_2425);
558 ini_rfb = rfb_2317;
559 ah->ah_rf_banks_size = ARRAY_SIZE(rfb_2317);
Nick Kossifidisf714dd62008-02-28 14:43:51 -0500560 break;
Nick Kossifidis136bfc72008-04-16 18:42:48 +0300561 case AR5K_RF2425:
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200562 rf_regs = rf_regs_2425;
563 ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_2425);
564 if (ah->ah_mac_srev < AR5K_SREV_AR2417) {
565 ini_rfb = rfb_2425;
566 ah->ah_rf_banks_size = ARRAY_SIZE(rfb_2425);
567 } else {
568 ini_rfb = rfb_2417;
569 ah->ah_rf_banks_size = ARRAY_SIZE(rfb_2417);
570 }
Nick Kossifidis136bfc72008-04-16 18:42:48 +0300571 break;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200572 default:
573 return -EINVAL;
574 }
575
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200576 /* If it's the first time we set rf buffer, allocate
577 * ah->ah_rf_banks based on ah->ah_rf_banks_size
578 * we set above */
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200579 if (ah->ah_rf_banks == NULL) {
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200580 ah->ah_rf_banks = kmalloc(sizeof(u32) * ah->ah_rf_banks_size,
581 GFP_KERNEL);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200582 if (ah->ah_rf_banks == NULL) {
583 ATH5K_ERR(ah->ah_sc, "out of memory\n");
584 return -ENOMEM;
585 }
586 }
587
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200588 /* Copy values to modify them */
589 rfb = ah->ah_rf_banks;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200590
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200591 for (i = 0; i < ah->ah_rf_banks_size; i++) {
592 if (ini_rfb[i].rfb_bank >= AR5K_MAX_RF_BANKS) {
593 ATH5K_ERR(ah->ah_sc, "invalid bank\n");
594 return -EINVAL;
595 }
596
597 /* Bank changed, write down the offset */
598 if (bank != ini_rfb[i].rfb_bank) {
599 bank = ini_rfb[i].rfb_bank;
600 ah->ah_offset[bank] = i;
601 }
602
603 rfb[i] = ini_rfb[i].rfb_mode_data[mode];
604 }
605
606 /* Set Output and Driver bias current (OB/DB) */
607 if (channel->hw_value & CHANNEL_2GHZ) {
608
609 if (channel->hw_value & CHANNEL_CCK)
610 ee_mode = AR5K_EEPROM_MODE_11B;
611 else
612 ee_mode = AR5K_EEPROM_MODE_11G;
613
614 /* For RF511X/RF211X combination we
615 * use b_OB and b_DB parameters stored
616 * in eeprom on ee->ee_ob[ee_mode][0]
617 *
618 * For all other chips we use OB/DB for 2Ghz
619 * stored in the b/g modal section just like
620 * 802.11a on ee->ee_ob[ee_mode][1] */
621 if ((ah->ah_radio == AR5K_RF5111) ||
622 (ah->ah_radio == AR5K_RF5112))
623 obdb = 0;
624 else
625 obdb = 1;
626
627 ath5k_hw_rfb_op(ah, rf_regs, ee->ee_ob[ee_mode][obdb],
628 AR5K_RF_OB_2GHZ, true);
629
630 ath5k_hw_rfb_op(ah, rf_regs, ee->ee_db[ee_mode][obdb],
631 AR5K_RF_DB_2GHZ, true);
632
633 /* RF5111 always needs OB/DB for 5GHz, even if we use 2GHz */
634 } else if ((channel->hw_value & CHANNEL_5GHZ) ||
635 (ah->ah_radio == AR5K_RF5111)) {
636
637 /* For 11a, Turbo and XR we need to choose
638 * OB/DB based on frequency range */
639 ee_mode = AR5K_EEPROM_MODE_11A;
640 obdb = channel->center_freq >= 5725 ? 3 :
641 (channel->center_freq >= 5500 ? 2 :
642 (channel->center_freq >= 5260 ? 1 :
643 (channel->center_freq > 4000 ? 0 : -1)));
644
645 if (obdb < 0)
646 return -EINVAL;
647
648 ath5k_hw_rfb_op(ah, rf_regs, ee->ee_ob[ee_mode][obdb],
649 AR5K_RF_OB_5GHZ, true);
650
651 ath5k_hw_rfb_op(ah, rf_regs, ee->ee_db[ee_mode][obdb],
652 AR5K_RF_DB_5GHZ, true);
653 }
654
655 g_step = &go->go_step[ah->ah_gain.g_step_idx];
656
657 /* Bank Modifications (chip-specific) */
658 if (ah->ah_radio == AR5K_RF5111) {
659
660 /* Set gain_F settings according to current step */
661 if (channel->hw_value & CHANNEL_OFDM) {
662
663 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_FRAME_CTL,
664 AR5K_PHY_FRAME_CTL_TX_CLIP,
665 g_step->gos_param[0]);
666
667 ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[1],
668 AR5K_RF_PWD_90, true);
669
670 ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[2],
671 AR5K_RF_PWD_84, true);
672
673 ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[3],
674 AR5K_RF_RFGAIN_SEL, true);
675
676 /* We programmed gain_F parameters, switch back
677 * to active state */
678 ah->ah_gain.g_state = AR5K_RFGAIN_ACTIVE;
679
680 }
681
682 /* Bank 6/7 setup */
683
684 ath5k_hw_rfb_op(ah, rf_regs, !ee->ee_xpd[ee_mode],
685 AR5K_RF_PWD_XPD, true);
686
687 ath5k_hw_rfb_op(ah, rf_regs, ee->ee_x_gain[ee_mode],
688 AR5K_RF_XPD_GAIN, true);
689
690 ath5k_hw_rfb_op(ah, rf_regs, ee->ee_i_gain[ee_mode],
691 AR5K_RF_GAIN_I, true);
692
693 ath5k_hw_rfb_op(ah, rf_regs, ee->ee_xpd[ee_mode],
694 AR5K_RF_PLO_SEL, true);
695
696 /* TODO: Half/quarter channel support */
697 }
698
699 if (ah->ah_radio == AR5K_RF5112) {
700
701 /* Set gain_F settings according to current step */
702 if (channel->hw_value & CHANNEL_OFDM) {
703
704 ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[0],
705 AR5K_RF_MIXGAIN_OVR, true);
706
707 ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[1],
708 AR5K_RF_PWD_138, true);
709
710 ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[2],
711 AR5K_RF_PWD_137, true);
712
713 ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[3],
714 AR5K_RF_PWD_136, true);
715
716 ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[4],
717 AR5K_RF_PWD_132, true);
718
719 ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[5],
720 AR5K_RF_PWD_131, true);
721
722 ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[6],
723 AR5K_RF_PWD_130, true);
724
725 /* We programmed gain_F parameters, switch back
726 * to active state */
727 ah->ah_gain.g_state = AR5K_RFGAIN_ACTIVE;
728 }
729
730 /* Bank 6/7 setup */
731
732 ath5k_hw_rfb_op(ah, rf_regs, ee->ee_xpd[ee_mode],
733 AR5K_RF_XPD_SEL, true);
734
735 if (ah->ah_radio_5ghz_revision < AR5K_SREV_RAD_5112A) {
736 /* Rev. 1 supports only one xpd */
737 ath5k_hw_rfb_op(ah, rf_regs,
738 ee->ee_x_gain[ee_mode],
739 AR5K_RF_XPD_GAIN, true);
740
741 } else {
Nick Kossifidisd1cb0bd2009-08-10 03:27:59 +0300742 u8 *pdg_curve_to_idx = ee->ee_pdc_to_idx[ee_mode];
743 if (ee->ee_pd_gains[ee_mode] > 1) {
744 ath5k_hw_rfb_op(ah, rf_regs,
745 pdg_curve_to_idx[0],
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200746 AR5K_RF_PD_GAIN_LO, true);
Nick Kossifidisd1cb0bd2009-08-10 03:27:59 +0300747 ath5k_hw_rfb_op(ah, rf_regs,
748 pdg_curve_to_idx[1],
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200749 AR5K_RF_PD_GAIN_HI, true);
Nick Kossifidisd1cb0bd2009-08-10 03:27:59 +0300750 } else {
751 ath5k_hw_rfb_op(ah, rf_regs,
752 pdg_curve_to_idx[0],
753 AR5K_RF_PD_GAIN_LO, true);
754 ath5k_hw_rfb_op(ah, rf_regs,
755 pdg_curve_to_idx[0],
756 AR5K_RF_PD_GAIN_HI, true);
757 }
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200758
759 /* Lower synth voltage on Rev 2 */
760 ath5k_hw_rfb_op(ah, rf_regs, 2,
761 AR5K_RF_HIGH_VC_CP, true);
762
763 ath5k_hw_rfb_op(ah, rf_regs, 2,
764 AR5K_RF_MID_VC_CP, true);
765
766 ath5k_hw_rfb_op(ah, rf_regs, 2,
767 AR5K_RF_LOW_VC_CP, true);
768
769 ath5k_hw_rfb_op(ah, rf_regs, 2,
770 AR5K_RF_PUSH_UP, true);
771
772 /* Decrease power consumption on 5213+ BaseBand */
773 if (ah->ah_phy_revision >= AR5K_SREV_PHY_5212A) {
774 ath5k_hw_rfb_op(ah, rf_regs, 1,
775 AR5K_RF_PAD2GND, true);
776
777 ath5k_hw_rfb_op(ah, rf_regs, 1,
778 AR5K_RF_XB2_LVL, true);
779
780 ath5k_hw_rfb_op(ah, rf_regs, 1,
781 AR5K_RF_XB5_LVL, true);
782
783 ath5k_hw_rfb_op(ah, rf_regs, 1,
784 AR5K_RF_PWD_167, true);
785
786 ath5k_hw_rfb_op(ah, rf_regs, 1,
787 AR5K_RF_PWD_166, true);
788 }
789 }
790
791 ath5k_hw_rfb_op(ah, rf_regs, ee->ee_i_gain[ee_mode],
792 AR5K_RF_GAIN_I, true);
793
794 /* TODO: Half/quarter channel support */
795
796 }
797
798 if (ah->ah_radio == AR5K_RF5413 &&
799 channel->hw_value & CHANNEL_2GHZ) {
800
801 ath5k_hw_rfb_op(ah, rf_regs, 1, AR5K_RF_DERBY_CHAN_SEL_MODE,
802 true);
803
804 /* Set optimum value for early revisions (on pci-e chips) */
805 if (ah->ah_mac_srev >= AR5K_SREV_AR5424 &&
806 ah->ah_mac_srev < AR5K_SREV_AR5413)
807 ath5k_hw_rfb_op(ah, rf_regs, ath5k_hw_bitswap(6, 3),
808 AR5K_RF_PWD_ICLOBUF_2G, true);
809
810 }
811
812 /* Write RF banks on hw */
813 for (i = 0; i < ah->ah_rf_banks_size; i++) {
814 AR5K_REG_WAIT(i);
815 ath5k_hw_reg_write(ah, rfb[i], ini_rfb[i].rfb_ctrl_register);
816 }
817
818 return 0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200819}
820
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200821
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200822/**************************\
823 PHY/RF channel functions
824\**************************/
825
826/*
827 * Check if a channel is supported
828 */
829bool ath5k_channel_ok(struct ath5k_hw *ah, u16 freq, unsigned int flags)
830{
831 /* Check if the channel is in our supported range */
832 if (flags & CHANNEL_2GHZ) {
833 if ((freq >= ah->ah_capabilities.cap_range.range_2ghz_min) &&
834 (freq <= ah->ah_capabilities.cap_range.range_2ghz_max))
835 return true;
836 } else if (flags & CHANNEL_5GHZ)
837 if ((freq >= ah->ah_capabilities.cap_range.range_5ghz_min) &&
838 (freq <= ah->ah_capabilities.cap_range.range_5ghz_max))
839 return true;
840
841 return false;
842}
843
844/*
845 * Convertion needed for RF5110
846 */
847static u32 ath5k_hw_rf5110_chan2athchan(struct ieee80211_channel *channel)
848{
849 u32 athchan;
850
851 /*
852 * Convert IEEE channel/MHz to an internal channel value used
853 * by the AR5210 chipset. This has not been verified with
854 * newer chipsets like the AR5212A who have a completely
855 * different RF/PHY part.
856 */
Luis R. Rodriguez400ec452008-02-03 21:51:49 -0500857 athchan = (ath5k_hw_bitswap(
858 (ieee80211_frequency_to_channel(
859 channel->center_freq) - 24) / 2, 5)
860 << 1) | (1 << 6) | 0x1;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200861 return athchan;
862}
863
864/*
865 * Set channel on RF5110
866 */
867static int ath5k_hw_rf5110_channel(struct ath5k_hw *ah,
868 struct ieee80211_channel *channel)
869{
870 u32 data;
871
872 /*
873 * Set the channel and wait
874 */
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);
878 mdelay(1);
879
880 return 0;
881}
882
883/*
884 * Convertion needed for 5111
885 */
886static int ath5k_hw_rf5111_chan2athchan(unsigned int ieee,
887 struct ath5k_athchan_2ghz *athchan)
888{
889 int channel;
890
891 /* Cast this value to catch negative channel numbers (>= -19) */
892 channel = (int)ieee;
893
894 /*
895 * Map 2GHz IEEE channel to 5GHz Atheros channel
896 */
897 if (channel <= 13) {
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;
906 } else
907 return -EINVAL;
908
909 return 0;
910}
911
912/*
913 * Set channel on 5111
914 */
915static int ath5k_hw_rf5111_channel(struct ath5k_hw *ah,
916 struct ieee80211_channel *channel)
917{
918 struct ath5k_athchan_2ghz ath5k_channel_2ghz;
Luis R. Rodriguez400ec452008-02-03 21:51:49 -0500919 unsigned int ath5k_channel =
920 ieee80211_frequency_to_channel(channel->center_freq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200921 u32 data0, data1, clock;
922 int ret;
923
924 /*
925 * Set the channel on the RF5111 radio
926 */
927 data0 = data1 = 0;
928
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500929 if (channel->hw_value & CHANNEL_2GHZ) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200930 /* Map 2GHz channel to 5GHz Atheros channel ID */
Luis R. Rodriguez400ec452008-02-03 21:51:49 -0500931 ret = ath5k_hw_rf5111_chan2athchan(
932 ieee80211_frequency_to_channel(channel->center_freq),
933 &ath5k_channel_2ghz);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200934 if (ret)
935 return ret;
936
937 ath5k_channel = ath5k_channel_2ghz.a2_athchan;
938 data0 = ((ath5k_hw_bitswap(ath5k_channel_2ghz.a2_flags, 8) & 0xff)
939 << 5) | (1 << 4);
940 }
941
942 if (ath5k_channel < 145 || !(ath5k_channel & 1)) {
943 clock = 1;
944 data1 = ((ath5k_hw_bitswap(ath5k_channel - 24, 8) & 0xff) << 2) |
945 (clock << 1) | (1 << 10) | 1;
946 } else {
947 clock = 0;
948 data1 = ((ath5k_hw_bitswap((ath5k_channel - 24) / 2, 8) & 0xff)
949 << 2) | (clock << 1) | (1 << 10) | 1;
950 }
951
952 ath5k_hw_reg_write(ah, (data1 & 0xff) | ((data0 & 0xff) << 8),
953 AR5K_RF_BUFFER);
954 ath5k_hw_reg_write(ah, ((data1 >> 8) & 0xff) | (data0 & 0xff00),
955 AR5K_RF_BUFFER_CONTROL_3);
956
957 return 0;
958}
959
960/*
961 * Set channel on 5112 and newer
962 */
963static int ath5k_hw_rf5112_channel(struct ath5k_hw *ah,
964 struct ieee80211_channel *channel)
965{
966 u32 data, data0, data1, data2;
967 u16 c;
968
969 data = data0 = data1 = data2 = 0;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500970 c = channel->center_freq;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200971
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200972 if (c < 4800) {
973 if (!((c - 2224) % 5)) {
974 data0 = ((2 * (c - 704)) - 3040) / 10;
975 data1 = 1;
976 } else if (!((c - 2192) % 5)) {
977 data0 = ((2 * (c - 672)) - 3040) / 10;
978 data1 = 0;
979 } else
980 return -EINVAL;
981
982 data0 = ath5k_hw_bitswap((data0 << 2) & 0xff, 8);
Bob Copeland1968cc72010-04-07 23:55:56 -0400983 } else if ((c % 5) != 2 || c > 5435) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200984 if (!(c % 20) && c >= 5120) {
985 data0 = ath5k_hw_bitswap(((c - 4800) / 20 << 2), 8);
986 data2 = ath5k_hw_bitswap(3, 2);
987 } else if (!(c % 10)) {
988 data0 = ath5k_hw_bitswap(((c - 4800) / 10 << 1), 8);
989 data2 = ath5k_hw_bitswap(2, 2);
990 } else if (!(c % 5)) {
991 data0 = ath5k_hw_bitswap((c - 4800) / 5, 8);
992 data2 = ath5k_hw_bitswap(1, 2);
993 } else
994 return -EINVAL;
Nick Kossifidiscc6323c2008-07-20 06:44:43 +0300995 } else {
Bob Copeland1968cc72010-04-07 23:55:56 -0400996 data0 = ath5k_hw_bitswap((10 * (c - 2 - 4800)) / 25 + 1, 8);
Nick Kossifidiscc6323c2008-07-20 06:44:43 +0300997 data2 = ath5k_hw_bitswap(0, 2);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200998 }
999
1000 data = (data0 << 4) | (data1 << 1) | (data2 << 2) | 0x1001;
1001
1002 ath5k_hw_reg_write(ah, data & 0xff, AR5K_RF_BUFFER);
1003 ath5k_hw_reg_write(ah, (data >> 8) & 0x7f, AR5K_RF_BUFFER_CONTROL_5);
1004
1005 return 0;
1006}
1007
1008/*
Nick Kossifidiscc6323c2008-07-20 06:44:43 +03001009 * Set the channel on the RF2425
1010 */
1011static int ath5k_hw_rf2425_channel(struct ath5k_hw *ah,
1012 struct ieee80211_channel *channel)
1013{
1014 u32 data, data0, data2;
1015 u16 c;
1016
1017 data = data0 = data2 = 0;
1018 c = channel->center_freq;
1019
1020 if (c < 4800) {
1021 data0 = ath5k_hw_bitswap((c - 2272), 8);
1022 data2 = 0;
1023 /* ? 5GHz ? */
Bob Copeland1968cc72010-04-07 23:55:56 -04001024 } else if ((c % 5) != 2 || c > 5435) {
Nick Kossifidiscc6323c2008-07-20 06:44:43 +03001025 if (!(c % 20) && c < 5120)
1026 data0 = ath5k_hw_bitswap(((c - 4800) / 20 << 2), 8);
1027 else if (!(c % 10))
1028 data0 = ath5k_hw_bitswap(((c - 4800) / 10 << 1), 8);
1029 else if (!(c % 5))
1030 data0 = ath5k_hw_bitswap((c - 4800) / 5, 8);
1031 else
1032 return -EINVAL;
1033 data2 = ath5k_hw_bitswap(1, 2);
1034 } else {
Bob Copeland1968cc72010-04-07 23:55:56 -04001035 data0 = ath5k_hw_bitswap((10 * (c - 2 - 4800)) / 25 + 1, 8);
Nick Kossifidiscc6323c2008-07-20 06:44:43 +03001036 data2 = ath5k_hw_bitswap(0, 2);
1037 }
1038
1039 data = (data0 << 4) | data2 << 2 | 0x1001;
1040
1041 ath5k_hw_reg_write(ah, data & 0xff, AR5K_RF_BUFFER);
1042 ath5k_hw_reg_write(ah, (data >> 8) & 0x7f, AR5K_RF_BUFFER_CONTROL_5);
1043
1044 return 0;
1045}
1046
1047/*
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001048 * Set a channel on the radio chip
1049 */
1050int ath5k_hw_channel(struct ath5k_hw *ah, struct ieee80211_channel *channel)
1051{
1052 int ret;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001053 /*
Luis R. Rodriguez400ec452008-02-03 21:51:49 -05001054 * Check bounds supported by the PHY (we don't care about regultory
1055 * restrictions at this point). Note: hw_value already has the band
1056 * (CHANNEL_2GHZ, or CHANNEL_5GHZ) so we inform ath5k_channel_ok()
1057 * of the band by that */
1058 if (!ath5k_channel_ok(ah, channel->center_freq, channel->hw_value)) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001059 ATH5K_ERR(ah->ah_sc,
Luis R. Rodriguez400ec452008-02-03 21:51:49 -05001060 "channel frequency (%u MHz) out of supported "
1061 "band range\n",
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05001062 channel->center_freq);
Luis R. Rodriguez400ec452008-02-03 21:51:49 -05001063 return -EINVAL;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001064 }
1065
1066 /*
1067 * Set the channel and wait
1068 */
1069 switch (ah->ah_radio) {
1070 case AR5K_RF5110:
1071 ret = ath5k_hw_rf5110_channel(ah, channel);
1072 break;
1073 case AR5K_RF5111:
1074 ret = ath5k_hw_rf5111_channel(ah, channel);
1075 break;
Nick Kossifidiscc6323c2008-07-20 06:44:43 +03001076 case AR5K_RF2425:
1077 ret = ath5k_hw_rf2425_channel(ah, channel);
1078 break;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001079 default:
1080 ret = ath5k_hw_rf5112_channel(ah, channel);
1081 break;
1082 }
1083
1084 if (ret)
1085 return ret;
1086
Nick Kossifidiscc6323c2008-07-20 06:44:43 +03001087 /* Set JAPAN setting for channel 14 */
1088 if (channel->center_freq == 2484) {
1089 AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_CCKTXCTL,
1090 AR5K_PHY_CCKTXCTL_JAPAN);
1091 } else {
1092 AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_CCKTXCTL,
1093 AR5K_PHY_CCKTXCTL_WORLD);
1094 }
1095
Bob Copeland46026e82009-06-10 22:22:20 -04001096 ah->ah_current_channel = channel;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05001097 ah->ah_turbo = channel->hw_value == CHANNEL_T ? true : false;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001098
1099 return 0;
1100}
1101
1102/*****************\
1103 PHY calibration
1104\*****************/
1105
Bob Copelande5e26472009-10-14 14:16:30 -04001106static int sign_extend(int val, const int nbits)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001107{
Bob Copelande5e26472009-10-14 14:16:30 -04001108 int order = BIT(nbits-1);
1109 return (val ^ order) - order;
1110}
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001111
Bob Copelande5e26472009-10-14 14:16:30 -04001112static s32 ath5k_hw_read_measured_noise_floor(struct ath5k_hw *ah)
1113{
1114 s32 val;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001115
Bob Copelande5e26472009-10-14 14:16:30 -04001116 val = ath5k_hw_reg_read(ah, AR5K_PHY_NF);
1117 return sign_extend(AR5K_REG_MS(val, AR5K_PHY_NF_MINCCA_PWR), 9);
1118}
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001119
Bob Copelande5e26472009-10-14 14:16:30 -04001120void ath5k_hw_init_nfcal_hist(struct ath5k_hw *ah)
1121{
1122 int i;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001123
Bob Copelande5e26472009-10-14 14:16:30 -04001124 ah->ah_nfcal_hist.index = 0;
1125 for (i = 0; i < ATH5K_NF_CAL_HIST_MAX; i++)
1126 ah->ah_nfcal_hist.nfval[i] = AR5K_TUNE_CCA_MAX_GOOD_VALUE;
1127}
1128
1129static void ath5k_hw_update_nfcal_hist(struct ath5k_hw *ah, s16 noise_floor)
1130{
1131 struct ath5k_nfcal_hist *hist = &ah->ah_nfcal_hist;
1132 hist->index = (hist->index + 1) & (ATH5K_NF_CAL_HIST_MAX-1);
1133 hist->nfval[hist->index] = noise_floor;
1134}
1135
1136static s16 ath5k_hw_get_median_noise_floor(struct ath5k_hw *ah)
1137{
1138 s16 sort[ATH5K_NF_CAL_HIST_MAX];
1139 s16 tmp;
1140 int i, j;
1141
1142 memcpy(sort, ah->ah_nfcal_hist.nfval, sizeof(sort));
1143 for (i = 0; i < ATH5K_NF_CAL_HIST_MAX - 1; i++) {
1144 for (j = 1; j < ATH5K_NF_CAL_HIST_MAX - i; j++) {
1145 if (sort[j] > sort[j-1]) {
1146 tmp = sort[j];
1147 sort[j] = sort[j-1];
1148 sort[j-1] = tmp;
1149 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001150 }
1151 }
Bob Copelande5e26472009-10-14 14:16:30 -04001152 for (i = 0; i < ATH5K_NF_CAL_HIST_MAX; i++) {
1153 ATH5K_DBG(ah->ah_sc, ATH5K_DEBUG_CALIBRATE,
1154 "cal %d:%d\n", i, sort[i]);
1155 }
1156 return sort[(ATH5K_NF_CAL_HIST_MAX-1) / 2];
1157}
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001158
Bob Copelande5e26472009-10-14 14:16:30 -04001159/*
1160 * When we tell the hardware to perform a noise floor calibration
1161 * by setting the AR5K_PHY_AGCCTL_NF bit, it will periodically
1162 * sample-and-hold the minimum noise level seen at the antennas.
1163 * This value is then stored in a ring buffer of recently measured
1164 * noise floor values so we have a moving window of the last few
1165 * samples.
1166 *
1167 * The median of the values in the history is then loaded into the
1168 * hardware for its own use for RSSI and CCA measurements.
1169 */
Bruno Randolf9e04a7e2010-05-19 10:31:00 +09001170void ath5k_hw_update_noise_floor(struct ath5k_hw *ah)
Bob Copelande5e26472009-10-14 14:16:30 -04001171{
1172 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
1173 u32 val;
1174 s16 nf, threshold;
1175 u8 ee_mode;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001176
Bob Copelande5e26472009-10-14 14:16:30 -04001177 /* keep last value if calibration hasn't completed */
1178 if (ath5k_hw_reg_read(ah, AR5K_PHY_AGCCTL) & AR5K_PHY_AGCCTL_NF) {
1179 ATH5K_DBG(ah->ah_sc, ATH5K_DEBUG_CALIBRATE,
1180 "NF did not complete in calibration window\n");
1181
1182 return;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001183 }
1184
Bob Copelande5e26472009-10-14 14:16:30 -04001185 switch (ah->ah_current_channel->hw_value & CHANNEL_MODES) {
1186 case CHANNEL_A:
1187 case CHANNEL_T:
1188 case CHANNEL_XR:
1189 ee_mode = AR5K_EEPROM_MODE_11A;
1190 break;
1191 case CHANNEL_G:
1192 case CHANNEL_TG:
1193 ee_mode = AR5K_EEPROM_MODE_11G;
1194 break;
1195 default:
1196 case CHANNEL_B:
1197 ee_mode = AR5K_EEPROM_MODE_11B;
1198 break;
1199 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001200
Bob Copelande5e26472009-10-14 14:16:30 -04001201
1202 /* completed NF calibration, test threshold */
1203 nf = ath5k_hw_read_measured_noise_floor(ah);
1204 threshold = ee->ee_noise_floor_thr[ee_mode];
1205
1206 if (nf > threshold) {
1207 ATH5K_DBG(ah->ah_sc, ATH5K_DEBUG_CALIBRATE,
1208 "noise floor failure detected; "
1209 "read %d, threshold %d\n",
1210 nf, threshold);
1211
1212 nf = AR5K_TUNE_CCA_MAX_GOOD_VALUE;
1213 }
1214
1215 ath5k_hw_update_nfcal_hist(ah, nf);
1216 nf = ath5k_hw_get_median_noise_floor(ah);
1217
1218 /* load noise floor (in .5 dBm) so the hardware will use it */
1219 val = ath5k_hw_reg_read(ah, AR5K_PHY_NF) & ~AR5K_PHY_NF_M;
1220 val |= (nf * 2) & AR5K_PHY_NF_M;
1221 ath5k_hw_reg_write(ah, val, AR5K_PHY_NF);
1222
1223 AR5K_REG_MASKED_BITS(ah, AR5K_PHY_AGCCTL, AR5K_PHY_AGCCTL_NF,
1224 ~(AR5K_PHY_AGCCTL_NF_EN | AR5K_PHY_AGCCTL_NF_NOUPDATE));
1225
1226 ath5k_hw_register_timeout(ah, AR5K_PHY_AGCCTL, AR5K_PHY_AGCCTL_NF,
1227 0, false);
1228
1229 /*
1230 * Load a high max CCA Power value (-50 dBm in .5 dBm units)
1231 * so that we're not capped by the median we just loaded.
1232 * This will be used as the initial value for the next noise
1233 * floor calibration.
1234 */
1235 val = (val & ~AR5K_PHY_NF_M) | ((-50 * 2) & AR5K_PHY_NF_M);
1236 ath5k_hw_reg_write(ah, val, AR5K_PHY_NF);
1237 AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGCCTL,
1238 AR5K_PHY_AGCCTL_NF_EN |
1239 AR5K_PHY_AGCCTL_NF_NOUPDATE |
1240 AR5K_PHY_AGCCTL_NF);
1241
1242 ah->ah_noise_floor = nf;
1243
1244 ATH5K_DBG(ah->ah_sc, ATH5K_DEBUG_CALIBRATE,
1245 "noise floor calibrated: %d\n", nf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001246}
1247
1248/*
1249 * Perform a PHY calibration on RF5110
1250 * -Fix BPSK/QAM Constellation (I/Q correction)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001251 */
1252static int ath5k_hw_rf5110_calibrate(struct ath5k_hw *ah,
1253 struct ieee80211_channel *channel)
1254{
1255 u32 phy_sig, phy_agc, phy_sat, beacon;
1256 int ret;
1257
1258 /*
1259 * Disable beacons and RX/TX queues, wait
1260 */
1261 AR5K_REG_ENABLE_BITS(ah, AR5K_DIAG_SW_5210,
1262 AR5K_DIAG_SW_DIS_TX | AR5K_DIAG_SW_DIS_RX_5210);
1263 beacon = ath5k_hw_reg_read(ah, AR5K_BEACON_5210);
1264 ath5k_hw_reg_write(ah, beacon & ~AR5K_BEACON_ENABLE, AR5K_BEACON_5210);
1265
Nick Kossifidis84e463f2008-09-17 03:33:19 +03001266 mdelay(2);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001267
1268 /*
1269 * Set the channel (with AGC turned off)
1270 */
1271 AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGC, AR5K_PHY_AGC_DISABLE);
1272 udelay(10);
1273 ret = ath5k_hw_channel(ah, channel);
1274
1275 /*
1276 * Activate PHY and wait
1277 */
1278 ath5k_hw_reg_write(ah, AR5K_PHY_ACT_ENABLE, AR5K_PHY_ACT);
1279 mdelay(1);
1280
1281 AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_AGC, AR5K_PHY_AGC_DISABLE);
1282
1283 if (ret)
1284 return ret;
1285
1286 /*
1287 * Calibrate the radio chip
1288 */
1289
1290 /* Remember normal state */
1291 phy_sig = ath5k_hw_reg_read(ah, AR5K_PHY_SIG);
1292 phy_agc = ath5k_hw_reg_read(ah, AR5K_PHY_AGCCOARSE);
1293 phy_sat = ath5k_hw_reg_read(ah, AR5K_PHY_ADCSAT);
1294
1295 /* Update radio registers */
1296 ath5k_hw_reg_write(ah, (phy_sig & ~(AR5K_PHY_SIG_FIRPWR)) |
1297 AR5K_REG_SM(-1, AR5K_PHY_SIG_FIRPWR), AR5K_PHY_SIG);
1298
1299 ath5k_hw_reg_write(ah, (phy_agc & ~(AR5K_PHY_AGCCOARSE_HI |
1300 AR5K_PHY_AGCCOARSE_LO)) |
1301 AR5K_REG_SM(-1, AR5K_PHY_AGCCOARSE_HI) |
1302 AR5K_REG_SM(-127, AR5K_PHY_AGCCOARSE_LO), AR5K_PHY_AGCCOARSE);
1303
1304 ath5k_hw_reg_write(ah, (phy_sat & ~(AR5K_PHY_ADCSAT_ICNT |
1305 AR5K_PHY_ADCSAT_THR)) |
1306 AR5K_REG_SM(2, AR5K_PHY_ADCSAT_ICNT) |
1307 AR5K_REG_SM(12, AR5K_PHY_ADCSAT_THR), AR5K_PHY_ADCSAT);
1308
1309 udelay(20);
1310
1311 AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGC, AR5K_PHY_AGC_DISABLE);
1312 udelay(10);
1313 ath5k_hw_reg_write(ah, AR5K_PHY_RFSTG_DISABLE, AR5K_PHY_RFSTG);
1314 AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_AGC, AR5K_PHY_AGC_DISABLE);
1315
1316 mdelay(1);
1317
1318 /*
1319 * Enable calibration and wait until completion
1320 */
1321 AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGCCTL, AR5K_PHY_AGCCTL_CAL);
1322
1323 ret = ath5k_hw_register_timeout(ah, AR5K_PHY_AGCCTL,
1324 AR5K_PHY_AGCCTL_CAL, 0, false);
1325
1326 /* Reset to normal state */
1327 ath5k_hw_reg_write(ah, phy_sig, AR5K_PHY_SIG);
1328 ath5k_hw_reg_write(ah, phy_agc, AR5K_PHY_AGCCOARSE);
1329 ath5k_hw_reg_write(ah, phy_sat, AR5K_PHY_ADCSAT);
1330
1331 if (ret) {
1332 ATH5K_ERR(ah->ah_sc, "calibration timeout (%uMHz)\n",
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05001333 channel->center_freq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001334 return ret;
1335 }
1336
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001337 /*
1338 * Re-enable RX/TX and beacons
1339 */
1340 AR5K_REG_DISABLE_BITS(ah, AR5K_DIAG_SW_5210,
1341 AR5K_DIAG_SW_DIS_TX | AR5K_DIAG_SW_DIS_RX_5210);
1342 ath5k_hw_reg_write(ah, beacon, AR5K_BEACON_5210);
1343
1344 return 0;
1345}
1346
1347/*
Bruno Randolf9e04a7e2010-05-19 10:31:00 +09001348 * Perform I/Q calibration on RF5111/5112 and newer chips
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001349 */
Bruno Randolf9e04a7e2010-05-19 10:31:00 +09001350static int
1351ath5k_hw_rf511x_iq_calibrate(struct ath5k_hw *ah)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001352{
1353 u32 i_pwr, q_pwr;
1354 s32 iq_corr, i_coff, i_coffd, q_coff, q_coffd;
Nick Kossifidisf860ee22008-07-20 06:47:12 +03001355 int i;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001356 ATH5K_TRACE(ah->ah_sc);
1357
Joe Perchese9010e22008-03-07 14:21:16 -08001358 if (!ah->ah_calibration ||
Nick Kossifidisf860ee22008-07-20 06:47:12 +03001359 ath5k_hw_reg_read(ah, AR5K_PHY_IQ) & AR5K_PHY_IQ_RUN)
Bruno Randolf9e04a7e2010-05-19 10:31:00 +09001360 return 0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001361
Nick Kossifidisf860ee22008-07-20 06:47:12 +03001362 /* Calibration has finished, get the results and re-run */
Bruno Randolf86415d42010-03-09 16:56:05 +09001363 /* work around empty results which can apparently happen on 5212 */
Nick Kossifidisf860ee22008-07-20 06:47:12 +03001364 for (i = 0; i <= 10; i++) {
1365 iq_corr = ath5k_hw_reg_read(ah, AR5K_PHY_IQRES_CAL_CORR);
1366 i_pwr = ath5k_hw_reg_read(ah, AR5K_PHY_IQRES_CAL_PWR_I);
1367 q_pwr = ath5k_hw_reg_read(ah, AR5K_PHY_IQRES_CAL_PWR_Q);
Bruno Randolf86415d42010-03-09 16:56:05 +09001368 ATH5K_DBG_UNLIMIT(ah->ah_sc, ATH5K_DEBUG_CALIBRATE,
1369 "iq_corr:%x i_pwr:%x q_pwr:%x", iq_corr, i_pwr, q_pwr);
1370 if (i_pwr && q_pwr)
1371 break;
Nick Kossifidisf860ee22008-07-20 06:47:12 +03001372 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001373
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001374 i_coffd = ((i_pwr >> 1) + (q_pwr >> 1)) >> 7;
Bruno Randolf49a85d22010-03-09 16:56:15 +09001375
1376 if (ah->ah_version == AR5K_AR5211)
1377 q_coffd = q_pwr >> 6;
1378 else
1379 q_coffd = q_pwr >> 7;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001380
Bruno Randolf86415d42010-03-09 16:56:05 +09001381 /* protect against divide by 0 and loss of sign bits */
1382 if (i_coffd == 0 || q_coffd < 2)
Bruno Randolf9e04a7e2010-05-19 10:31:00 +09001383 return -1;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001384
Bruno Randolf86415d42010-03-09 16:56:05 +09001385 i_coff = (-iq_corr) / i_coffd;
1386 i_coff = clamp(i_coff, -32, 31); /* signed 6 bit */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001387
John W. Linvilleace5d5d2010-04-08 16:34:49 -04001388 if (ah->ah_version == AR5K_AR5211)
1389 q_coff = (i_pwr / q_coffd) - 64;
1390 else
1391 q_coff = (i_pwr / q_coffd) - 128;
Bruno Randolf86415d42010-03-09 16:56:05 +09001392 q_coff = clamp(q_coff, -16, 15); /* signed 5 bit */
Nick Kossifidisf860ee22008-07-20 06:47:12 +03001393
Bruno Randolf86415d42010-03-09 16:56:05 +09001394 ATH5K_DBG_UNLIMIT(ah->ah_sc, ATH5K_DEBUG_CALIBRATE,
1395 "new I:%d Q:%d (i_coffd:%x q_coffd:%x)",
1396 i_coff, q_coff, i_coffd, q_coffd);
Nick Kossifidisf860ee22008-07-20 06:47:12 +03001397
Bruno Randolf86415d42010-03-09 16:56:05 +09001398 /* Commit new I/Q values (set enable bit last to match HAL sources) */
1399 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_IQ, AR5K_PHY_IQ_CORR_Q_I_COFF, i_coff);
1400 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_IQ, AR5K_PHY_IQ_CORR_Q_Q_COFF, q_coff);
1401 AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ, AR5K_PHY_IQ_CORR_ENABLE);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001402
Nick Kossifidisf860ee22008-07-20 06:47:12 +03001403 /* Re-enable calibration -if we don't we'll commit
1404 * the same values again and again */
1405 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_IQ,
1406 AR5K_PHY_IQ_CAL_NUM_LOG_MAX, 15);
1407 AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ, AR5K_PHY_IQ_RUN);
1408
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001409 return 0;
1410}
1411
1412/*
1413 * Perform a PHY calibration
1414 */
1415int ath5k_hw_phy_calibrate(struct ath5k_hw *ah,
1416 struct ieee80211_channel *channel)
1417{
1418 int ret;
1419
1420 if (ah->ah_radio == AR5K_RF5110)
1421 ret = ath5k_hw_rf5110_calibrate(ah, channel);
Bruno Randolf9e04a7e2010-05-19 10:31:00 +09001422 else {
1423 ret = ath5k_hw_rf511x_iq_calibrate(ah);
1424 ath5k_hw_request_rfgain_probe(ah);
1425 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001426
1427 return ret;
1428}
1429
Nick Kossifidis57e6c562009-04-30 15:55:50 -04001430/***************************\
1431* Spur mitigation functions *
1432\***************************/
1433
1434bool ath5k_hw_chan_has_spur_noise(struct ath5k_hw *ah,
1435 struct ieee80211_channel *channel)
1436{
1437 u8 refclk_freq;
1438
1439 if ((ah->ah_radio == AR5K_RF5112) ||
1440 (ah->ah_radio == AR5K_RF5413) ||
1441 (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4)))
1442 refclk_freq = 40;
1443 else
1444 refclk_freq = 32;
1445
1446 if ((channel->center_freq % refclk_freq != 0) &&
1447 ((channel->center_freq % refclk_freq < 10) ||
1448 (channel->center_freq % refclk_freq > 22)))
1449 return true;
1450 else
1451 return false;
1452}
1453
1454void
1455ath5k_hw_set_spur_mitigation_filter(struct ath5k_hw *ah,
1456 struct ieee80211_channel *channel)
1457{
1458 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
1459 u32 mag_mask[4] = {0, 0, 0, 0};
1460 u32 pilot_mask[2] = {0, 0};
1461 /* Note: fbin values are scaled up by 2 */
1462 u16 spur_chan_fbin, chan_fbin, symbol_width, spur_detection_window;
1463 s32 spur_delta_phase, spur_freq_sigma_delta;
1464 s32 spur_offset, num_symbols_x16;
1465 u8 num_symbol_offsets, i, freq_band;
1466
1467 /* Convert current frequency to fbin value (the same way channels
1468 * are stored on EEPROM, check out ath5k_eeprom_bin2freq) and scale
1469 * up by 2 so we can compare it later */
1470 if (channel->hw_value & CHANNEL_2GHZ) {
1471 chan_fbin = (channel->center_freq - 2300) * 10;
1472 freq_band = AR5K_EEPROM_BAND_2GHZ;
1473 } else {
1474 chan_fbin = (channel->center_freq - 4900) * 10;
1475 freq_band = AR5K_EEPROM_BAND_5GHZ;
1476 }
1477
1478 /* Check if any spur_chan_fbin from EEPROM is
1479 * within our current channel's spur detection range */
1480 spur_chan_fbin = AR5K_EEPROM_NO_SPUR;
1481 spur_detection_window = AR5K_SPUR_CHAN_WIDTH;
1482 /* XXX: Half/Quarter channels ?*/
1483 if (channel->hw_value & CHANNEL_TURBO)
1484 spur_detection_window *= 2;
1485
1486 for (i = 0; i < AR5K_EEPROM_N_SPUR_CHANS; i++) {
1487 spur_chan_fbin = ee->ee_spur_chans[i][freq_band];
1488
1489 /* Note: mask cleans AR5K_EEPROM_NO_SPUR flag
1490 * so it's zero if we got nothing from EEPROM */
1491 if (spur_chan_fbin == AR5K_EEPROM_NO_SPUR) {
1492 spur_chan_fbin &= AR5K_EEPROM_SPUR_CHAN_MASK;
1493 break;
1494 }
1495
1496 if ((chan_fbin - spur_detection_window <=
1497 (spur_chan_fbin & AR5K_EEPROM_SPUR_CHAN_MASK)) &&
1498 (chan_fbin + spur_detection_window >=
1499 (spur_chan_fbin & AR5K_EEPROM_SPUR_CHAN_MASK))) {
1500 spur_chan_fbin &= AR5K_EEPROM_SPUR_CHAN_MASK;
1501 break;
1502 }
1503 }
1504
1505 /* We need to enable spur filter for this channel */
1506 if (spur_chan_fbin) {
1507 spur_offset = spur_chan_fbin - chan_fbin;
1508 /*
1509 * Calculate deltas:
1510 * spur_freq_sigma_delta -> spur_offset / sample_freq << 21
1511 * spur_delta_phase -> spur_offset / chip_freq << 11
1512 * Note: Both values have 100KHz resolution
1513 */
1514 /* XXX: Half/Quarter rate channels ? */
1515 switch (channel->hw_value) {
1516 case CHANNEL_A:
1517 /* Both sample_freq and chip_freq are 40MHz */
1518 spur_delta_phase = (spur_offset << 17) / 25;
1519 spur_freq_sigma_delta = (spur_delta_phase >> 10);
1520 symbol_width = AR5K_SPUR_SYMBOL_WIDTH_BASE_100Hz;
1521 break;
1522 case CHANNEL_G:
1523 /* sample_freq -> 40MHz chip_freq -> 44MHz
1524 * (for b compatibility) */
1525 spur_freq_sigma_delta = (spur_offset << 8) / 55;
1526 spur_delta_phase = (spur_offset << 17) / 25;
1527 symbol_width = AR5K_SPUR_SYMBOL_WIDTH_BASE_100Hz;
1528 break;
1529 case CHANNEL_T:
1530 case CHANNEL_TG:
1531 /* Both sample_freq and chip_freq are 80MHz */
1532 spur_delta_phase = (spur_offset << 16) / 25;
1533 spur_freq_sigma_delta = (spur_delta_phase >> 10);
1534 symbol_width = AR5K_SPUR_SYMBOL_WIDTH_TURBO_100Hz;
1535 break;
1536 default:
1537 return;
1538 }
1539
1540 /* Calculate pilot and magnitude masks */
1541
1542 /* Scale up spur_offset by 1000 to switch to 100HZ resolution
1543 * and divide by symbol_width to find how many symbols we have
1544 * Note: number of symbols is scaled up by 16 */
1545 num_symbols_x16 = ((spur_offset * 1000) << 4) / symbol_width;
1546
1547 /* Spur is on a symbol if num_symbols_x16 % 16 is zero */
1548 if (!(num_symbols_x16 & 0xF))
1549 /* _X_ */
1550 num_symbol_offsets = 3;
1551 else
1552 /* _xx_ */
1553 num_symbol_offsets = 4;
1554
1555 for (i = 0; i < num_symbol_offsets; i++) {
1556
1557 /* Calculate pilot mask */
1558 s32 curr_sym_off =
1559 (num_symbols_x16 / 16) + i + 25;
1560
1561 /* Pilot magnitude mask seems to be a way to
1562 * declare the boundaries for our detection
1563 * window or something, it's 2 for the middle
1564 * value(s) where the symbol is expected to be
1565 * and 1 on the boundary values */
1566 u8 plt_mag_map =
1567 (i == 0 || i == (num_symbol_offsets - 1))
1568 ? 1 : 2;
1569
1570 if (curr_sym_off >= 0 && curr_sym_off <= 32) {
1571 if (curr_sym_off <= 25)
1572 pilot_mask[0] |= 1 << curr_sym_off;
1573 else if (curr_sym_off >= 27)
1574 pilot_mask[0] |= 1 << (curr_sym_off - 1);
1575 } else if (curr_sym_off >= 33 && curr_sym_off <= 52)
1576 pilot_mask[1] |= 1 << (curr_sym_off - 33);
1577
1578 /* Calculate magnitude mask (for viterbi decoder) */
1579 if (curr_sym_off >= -1 && curr_sym_off <= 14)
1580 mag_mask[0] |=
1581 plt_mag_map << (curr_sym_off + 1) * 2;
1582 else if (curr_sym_off >= 15 && curr_sym_off <= 30)
1583 mag_mask[1] |=
1584 plt_mag_map << (curr_sym_off - 15) * 2;
1585 else if (curr_sym_off >= 31 && curr_sym_off <= 46)
1586 mag_mask[2] |=
1587 plt_mag_map << (curr_sym_off - 31) * 2;
1588 else if (curr_sym_off >= 46 && curr_sym_off <= 53)
1589 mag_mask[3] |=
1590 plt_mag_map << (curr_sym_off - 47) * 2;
1591
1592 }
1593
1594 /* Write settings on hw to enable spur filter */
1595 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_BIN_MASK_CTL,
1596 AR5K_PHY_BIN_MASK_CTL_RATE, 0xff);
1597 /* XXX: Self correlator also ? */
1598 AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ,
1599 AR5K_PHY_IQ_PILOT_MASK_EN |
1600 AR5K_PHY_IQ_CHAN_MASK_EN |
1601 AR5K_PHY_IQ_SPUR_FILT_EN);
1602
1603 /* Set delta phase and freq sigma delta */
1604 ath5k_hw_reg_write(ah,
1605 AR5K_REG_SM(spur_delta_phase,
1606 AR5K_PHY_TIMING_11_SPUR_DELTA_PHASE) |
1607 AR5K_REG_SM(spur_freq_sigma_delta,
1608 AR5K_PHY_TIMING_11_SPUR_FREQ_SD) |
1609 AR5K_PHY_TIMING_11_USE_SPUR_IN_AGC,
1610 AR5K_PHY_TIMING_11);
1611
1612 /* Write pilot masks */
1613 ath5k_hw_reg_write(ah, pilot_mask[0], AR5K_PHY_TIMING_7);
1614 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_TIMING_8,
1615 AR5K_PHY_TIMING_8_PILOT_MASK_2,
1616 pilot_mask[1]);
1617
1618 ath5k_hw_reg_write(ah, pilot_mask[0], AR5K_PHY_TIMING_9);
1619 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_TIMING_10,
1620 AR5K_PHY_TIMING_10_PILOT_MASK_2,
1621 pilot_mask[1]);
1622
1623 /* Write magnitude masks */
1624 ath5k_hw_reg_write(ah, mag_mask[0], AR5K_PHY_BIN_MASK_1);
1625 ath5k_hw_reg_write(ah, mag_mask[1], AR5K_PHY_BIN_MASK_2);
1626 ath5k_hw_reg_write(ah, mag_mask[2], AR5K_PHY_BIN_MASK_3);
1627 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_BIN_MASK_CTL,
1628 AR5K_PHY_BIN_MASK_CTL_MASK_4,
1629 mag_mask[3]);
1630
1631 ath5k_hw_reg_write(ah, mag_mask[0], AR5K_PHY_BIN_MASK2_1);
1632 ath5k_hw_reg_write(ah, mag_mask[1], AR5K_PHY_BIN_MASK2_2);
1633 ath5k_hw_reg_write(ah, mag_mask[2], AR5K_PHY_BIN_MASK2_3);
1634 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_BIN_MASK2_4,
1635 AR5K_PHY_BIN_MASK2_4_MASK_4,
1636 mag_mask[3]);
1637
1638 } else if (ath5k_hw_reg_read(ah, AR5K_PHY_IQ) &
1639 AR5K_PHY_IQ_SPUR_FILT_EN) {
1640 /* Clean up spur mitigation settings and disable fliter */
1641 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_BIN_MASK_CTL,
1642 AR5K_PHY_BIN_MASK_CTL_RATE, 0);
1643 AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_IQ,
1644 AR5K_PHY_IQ_PILOT_MASK_EN |
1645 AR5K_PHY_IQ_CHAN_MASK_EN |
1646 AR5K_PHY_IQ_SPUR_FILT_EN);
1647 ath5k_hw_reg_write(ah, 0, AR5K_PHY_TIMING_11);
1648
1649 /* Clear pilot masks */
1650 ath5k_hw_reg_write(ah, 0, AR5K_PHY_TIMING_7);
1651 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_TIMING_8,
1652 AR5K_PHY_TIMING_8_PILOT_MASK_2,
1653 0);
1654
1655 ath5k_hw_reg_write(ah, 0, AR5K_PHY_TIMING_9);
1656 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_TIMING_10,
1657 AR5K_PHY_TIMING_10_PILOT_MASK_2,
1658 0);
1659
1660 /* Clear magnitude masks */
1661 ath5k_hw_reg_write(ah, 0, AR5K_PHY_BIN_MASK_1);
1662 ath5k_hw_reg_write(ah, 0, AR5K_PHY_BIN_MASK_2);
1663 ath5k_hw_reg_write(ah, 0, AR5K_PHY_BIN_MASK_3);
1664 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_BIN_MASK_CTL,
1665 AR5K_PHY_BIN_MASK_CTL_MASK_4,
1666 0);
1667
1668 ath5k_hw_reg_write(ah, 0, AR5K_PHY_BIN_MASK2_1);
1669 ath5k_hw_reg_write(ah, 0, AR5K_PHY_BIN_MASK2_2);
1670 ath5k_hw_reg_write(ah, 0, AR5K_PHY_BIN_MASK2_3);
1671 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_BIN_MASK2_4,
1672 AR5K_PHY_BIN_MASK2_4_MASK_4,
1673 0);
1674 }
1675}
1676
1677/********************\
1678 Misc PHY functions
1679\********************/
1680
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001681int ath5k_hw_phy_disable(struct ath5k_hw *ah)
1682{
1683 ATH5K_TRACE(ah->ah_sc);
1684 /*Just a try M.F.*/
1685 ath5k_hw_reg_write(ah, AR5K_PHY_ACT_DISABLE, AR5K_PHY_ACT);
1686
1687 return 0;
1688}
1689
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001690/*
1691 * Get the PHY Chip revision
1692 */
1693u16 ath5k_hw_radio_revision(struct ath5k_hw *ah, unsigned int chan)
1694{
1695 unsigned int i;
1696 u32 srev;
1697 u16 ret;
1698
1699 ATH5K_TRACE(ah->ah_sc);
1700
1701 /*
1702 * Set the radio chip access register
1703 */
1704 switch (chan) {
1705 case CHANNEL_2GHZ:
1706 ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_2GHZ, AR5K_PHY(0));
1707 break;
1708 case CHANNEL_5GHZ:
1709 ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ, AR5K_PHY(0));
1710 break;
1711 default:
1712 return 0;
1713 }
1714
1715 mdelay(2);
1716
1717 /* ...wait until PHY is ready and read the selected radio revision */
1718 ath5k_hw_reg_write(ah, 0x00001c16, AR5K_PHY(0x34));
1719
1720 for (i = 0; i < 8; i++)
1721 ath5k_hw_reg_write(ah, 0x00010000, AR5K_PHY(0x20));
1722
1723 if (ah->ah_version == AR5K_AR5210) {
1724 srev = ath5k_hw_reg_read(ah, AR5K_PHY(256) >> 28) & 0xf;
1725 ret = (u16)ath5k_hw_bitswap(srev, 4) + 1;
1726 } else {
1727 srev = (ath5k_hw_reg_read(ah, AR5K_PHY(0x100)) >> 24) & 0xff;
1728 ret = (u16)ath5k_hw_bitswap(((srev & 0xf0) >> 4) |
1729 ((srev & 0x0f) << 4), 8);
1730 }
1731
1732 /* Reset to the 5GHz mode */
1733 ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ, AR5K_PHY(0));
1734
1735 return ret;
1736}
1737
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001738/*****************\
1739* Antenna control *
1740\*****************/
1741
Pavel Roskin626ede62010-02-18 20:28:02 -05001742static void /*TODO:Boundary check*/
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001743ath5k_hw_set_def_antenna(struct ath5k_hw *ah, u8 ant)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001744{
1745 ATH5K_TRACE(ah->ah_sc);
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001746
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001747 if (ah->ah_version != AR5K_AR5210)
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001748 ath5k_hw_reg_write(ah, ant & 0x7, AR5K_DEFAULT_ANTENNA);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001749}
1750
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001751/*
1752 * Enable/disable fast rx antenna diversity
1753 */
1754static void
1755ath5k_hw_set_fast_div(struct ath5k_hw *ah, u8 ee_mode, bool enable)
1756{
1757 switch (ee_mode) {
1758 case AR5K_EEPROM_MODE_11G:
1759 /* XXX: This is set to
1760 * disabled on initvals !!! */
1761 case AR5K_EEPROM_MODE_11A:
1762 if (enable)
1763 AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_AGCCTL,
1764 AR5K_PHY_AGCCTL_OFDM_DIV_DIS);
1765 else
1766 AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGCCTL,
1767 AR5K_PHY_AGCCTL_OFDM_DIV_DIS);
1768 break;
1769 case AR5K_EEPROM_MODE_11B:
1770 AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGCCTL,
1771 AR5K_PHY_AGCCTL_OFDM_DIV_DIS);
1772 break;
1773 default:
1774 return;
1775 }
1776
1777 if (enable) {
1778 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_RESTART,
1779 AR5K_PHY_RESTART_DIV_GC, 0xc);
1780
1781 AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_FAST_ANT_DIV,
1782 AR5K_PHY_FAST_ANT_DIV_EN);
1783 } else {
1784 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_RESTART,
1785 AR5K_PHY_RESTART_DIV_GC, 0x8);
1786
1787 AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_FAST_ANT_DIV,
1788 AR5K_PHY_FAST_ANT_DIV_EN);
1789 }
1790}
1791
1792/*
1793 * Set antenna operating mode
1794 */
1795void
1796ath5k_hw_set_antenna_mode(struct ath5k_hw *ah, u8 ant_mode)
1797{
Bob Copeland46026e82009-06-10 22:22:20 -04001798 struct ieee80211_channel *channel = ah->ah_current_channel;
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001799 bool use_def_for_tx, update_def_on_tx, use_def_for_rts, fast_div;
1800 bool use_def_for_sg;
1801 u8 def_ant, tx_ant, ee_mode;
1802 u32 sta_id1 = 0;
1803
1804 def_ant = ah->ah_def_ant;
1805
1806 ATH5K_TRACE(ah->ah_sc);
1807
1808 switch (channel->hw_value & CHANNEL_MODES) {
1809 case CHANNEL_A:
1810 case CHANNEL_T:
1811 case CHANNEL_XR:
1812 ee_mode = AR5K_EEPROM_MODE_11A;
1813 break;
1814 case CHANNEL_G:
1815 case CHANNEL_TG:
1816 ee_mode = AR5K_EEPROM_MODE_11G;
1817 break;
1818 case CHANNEL_B:
1819 ee_mode = AR5K_EEPROM_MODE_11B;
1820 break;
1821 default:
1822 ATH5K_ERR(ah->ah_sc,
1823 "invalid channel: %d\n", channel->center_freq);
1824 return;
1825 }
1826
1827 switch (ant_mode) {
1828 case AR5K_ANTMODE_DEFAULT:
1829 tx_ant = 0;
1830 use_def_for_tx = false;
1831 update_def_on_tx = false;
1832 use_def_for_rts = false;
1833 use_def_for_sg = false;
1834 fast_div = true;
1835 break;
1836 case AR5K_ANTMODE_FIXED_A:
1837 def_ant = 1;
Bruno Randolf8bd8bea2010-03-09 16:55:23 +09001838 tx_ant = 1;
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001839 use_def_for_tx = true;
1840 update_def_on_tx = false;
1841 use_def_for_rts = true;
1842 use_def_for_sg = true;
1843 fast_div = false;
1844 break;
1845 case AR5K_ANTMODE_FIXED_B:
1846 def_ant = 2;
Bruno Randolf8bd8bea2010-03-09 16:55:23 +09001847 tx_ant = 2;
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001848 use_def_for_tx = true;
1849 update_def_on_tx = false;
1850 use_def_for_rts = true;
1851 use_def_for_sg = true;
1852 fast_div = false;
1853 break;
1854 case AR5K_ANTMODE_SINGLE_AP:
1855 def_ant = 1; /* updated on tx */
1856 tx_ant = 0;
1857 use_def_for_tx = true;
1858 update_def_on_tx = true;
1859 use_def_for_rts = true;
1860 use_def_for_sg = true;
1861 fast_div = true;
1862 break;
1863 case AR5K_ANTMODE_SECTOR_AP:
1864 tx_ant = 1; /* variable */
1865 use_def_for_tx = false;
1866 update_def_on_tx = false;
1867 use_def_for_rts = true;
1868 use_def_for_sg = false;
1869 fast_div = false;
1870 break;
1871 case AR5K_ANTMODE_SECTOR_STA:
1872 tx_ant = 1; /* variable */
1873 use_def_for_tx = true;
1874 update_def_on_tx = false;
1875 use_def_for_rts = true;
1876 use_def_for_sg = false;
1877 fast_div = true;
1878 break;
1879 case AR5K_ANTMODE_DEBUG:
1880 def_ant = 1;
1881 tx_ant = 2;
1882 use_def_for_tx = false;
1883 update_def_on_tx = false;
1884 use_def_for_rts = false;
1885 use_def_for_sg = false;
1886 fast_div = false;
1887 break;
1888 default:
1889 return;
1890 }
1891
1892 ah->ah_tx_ant = tx_ant;
1893 ah->ah_ant_mode = ant_mode;
Bruno Randolfcaec9112010-03-09 16:55:28 +09001894 ah->ah_def_ant = def_ant;
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001895
1896 sta_id1 |= use_def_for_tx ? AR5K_STA_ID1_DEFAULT_ANTENNA : 0;
1897 sta_id1 |= update_def_on_tx ? AR5K_STA_ID1_DESC_ANTENNA : 0;
1898 sta_id1 |= use_def_for_rts ? AR5K_STA_ID1_RTS_DEF_ANTENNA : 0;
1899 sta_id1 |= use_def_for_sg ? AR5K_STA_ID1_SELFGEN_DEF_ANT : 0;
1900
1901 AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_ANTENNA_SETTINGS);
1902
1903 if (sta_id1)
1904 AR5K_REG_ENABLE_BITS(ah, AR5K_STA_ID1, sta_id1);
1905
1906 /* Note: set diversity before default antenna
1907 * because it won't work correctly */
1908 ath5k_hw_set_fast_div(ah, ee_mode, fast_div);
1909 ath5k_hw_set_def_antenna(ah, def_ant);
1910}
1911
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001912
1913/****************\
1914* TX power setup *
1915\****************/
1916
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001917/*
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001918 * Helper functions
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001919 */
1920
1921/*
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001922 * Do linear interpolation between two given (x, y) points
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001923 */
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001924static s16
1925ath5k_get_interpolated_value(s16 target, s16 x_left, s16 x_right,
1926 s16 y_left, s16 y_right)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001927{
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001928 s16 ratio, result;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001929
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001930 /* Avoid divide by zero and skip interpolation
1931 * if we have the same point */
1932 if ((x_left == x_right) || (y_left == y_right))
1933 return y_left;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001934
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001935 /*
1936 * Since we use ints and not fps, we need to scale up in
1937 * order to get a sane ratio value (or else we 'll eg. get
1938 * always 1 instead of 1.25, 1.75 etc). We scale up by 100
1939 * to have some accuracy both for 0.5 and 0.25 steps.
1940 */
1941 ratio = ((100 * y_right - 100 * y_left)/(x_right - x_left));
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001942
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001943 /* Now scale down to be in range */
1944 result = y_left + (ratio * (target - x_left) / 100);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001945
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001946 return result;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001947}
1948
1949/*
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001950 * Find vertical boundary (min pwr) for the linear PCDAC curve.
1951 *
1952 * Since we have the top of the curve and we draw the line below
1953 * until we reach 1 (1 pcdac step) we need to know which point
1954 * (x value) that is so that we don't go below y axis and have negative
1955 * pcdac values when creating the curve, or fill the table with zeroes.
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001956 */
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001957static s16
1958ath5k_get_linear_pcdac_min(const u8 *stepL, const u8 *stepR,
1959 const s16 *pwrL, const s16 *pwrR)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001960{
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001961 s8 tmp;
1962 s16 min_pwrL, min_pwrR;
Fabio Rossi64cdb0e2009-04-01 20:37:50 +02001963 s16 pwr_i;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001964
Nick Kossifidisd1cb0bd2009-08-10 03:27:59 +03001965 /* Some vendors write the same pcdac value twice !!! */
1966 if (stepL[0] == stepL[1] || stepR[0] == stepR[1])
1967 return max(pwrL[0], pwrR[0]);
Bob Copeland9c8b3ed2009-05-19 23:37:31 -04001968
Fabio Rossi64cdb0e2009-04-01 20:37:50 +02001969 if (pwrL[0] == pwrL[1])
1970 min_pwrL = pwrL[0];
1971 else {
1972 pwr_i = pwrL[0];
1973 do {
1974 pwr_i--;
1975 tmp = (s8) ath5k_get_interpolated_value(pwr_i,
1976 pwrL[0], pwrL[1],
1977 stepL[0], stepL[1]);
1978 } while (tmp > 1);
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001979
Fabio Rossi64cdb0e2009-04-01 20:37:50 +02001980 min_pwrL = pwr_i;
1981 }
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001982
Fabio Rossi64cdb0e2009-04-01 20:37:50 +02001983 if (pwrR[0] == pwrR[1])
1984 min_pwrR = pwrR[0];
1985 else {
1986 pwr_i = pwrR[0];
1987 do {
1988 pwr_i--;
1989 tmp = (s8) ath5k_get_interpolated_value(pwr_i,
1990 pwrR[0], pwrR[1],
1991 stepR[0], stepR[1]);
1992 } while (tmp > 1);
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001993
Fabio Rossi64cdb0e2009-04-01 20:37:50 +02001994 min_pwrR = pwr_i;
1995 }
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001996
1997 /* Keep the right boundary so that it works for both curves */
1998 return max(min_pwrL, min_pwrR);
1999}
2000
2001/*
2002 * Interpolate (pwr,vpd) points to create a Power to PDADC or a
2003 * Power to PCDAC curve.
2004 *
2005 * Each curve has power on x axis (in 0.5dB units) and PCDAC/PDADC
2006 * steps (offsets) on y axis. Power can go up to 31.5dB and max
2007 * PCDAC/PDADC step for each curve is 64 but we can write more than
2008 * one curves on hw so we can go up to 128 (which is the max step we
2009 * can write on the final table).
2010 *
2011 * We write y values (PCDAC/PDADC steps) on hw.
2012 */
2013static void
2014ath5k_create_power_curve(s16 pmin, s16 pmax,
2015 const s16 *pwr, const u8 *vpd,
2016 u8 num_points,
2017 u8 *vpd_table, u8 type)
2018{
2019 u8 idx[2] = { 0, 1 };
2020 s16 pwr_i = 2*pmin;
2021 int i;
2022
2023 if (num_points < 2)
2024 return;
2025
2026 /* We want the whole line, so adjust boundaries
2027 * to cover the entire power range. Note that
2028 * power values are already 0.25dB so no need
2029 * to multiply pwr_i by 2 */
2030 if (type == AR5K_PWRTABLE_LINEAR_PCDAC) {
2031 pwr_i = pmin;
2032 pmin = 0;
2033 pmax = 63;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002034 }
2035
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02002036 /* Find surrounding turning points (TPs)
2037 * and interpolate between them */
2038 for (i = 0; (i <= (u16) (pmax - pmin)) &&
2039 (i < AR5K_EEPROM_POWER_TABLE_SIZE); i++) {
2040
2041 /* We passed the right TP, move to the next set of TPs
2042 * if we pass the last TP, extrapolate above using the last
2043 * two TPs for ratio */
2044 if ((pwr_i > pwr[idx[1]]) && (idx[1] < num_points - 1)) {
2045 idx[0]++;
2046 idx[1]++;
2047 }
2048
2049 vpd_table[i] = (u8) ath5k_get_interpolated_value(pwr_i,
2050 pwr[idx[0]], pwr[idx[1]],
2051 vpd[idx[0]], vpd[idx[1]]);
2052
2053 /* Increase by 0.5dB
2054 * (0.25 dB units) */
2055 pwr_i += 2;
2056 }
2057}
2058
2059/*
2060 * Get the surrounding per-channel power calibration piers
2061 * for a given frequency so that we can interpolate between
2062 * them and come up with an apropriate dataset for our current
2063 * channel.
2064 */
2065static void
2066ath5k_get_chan_pcal_surrounding_piers(struct ath5k_hw *ah,
2067 struct ieee80211_channel *channel,
2068 struct ath5k_chan_pcal_info **pcinfo_l,
2069 struct ath5k_chan_pcal_info **pcinfo_r)
2070{
2071 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
2072 struct ath5k_chan_pcal_info *pcinfo;
2073 u8 idx_l, idx_r;
2074 u8 mode, max, i;
2075 u32 target = channel->center_freq;
2076
2077 idx_l = 0;
2078 idx_r = 0;
2079
2080 if (!(channel->hw_value & CHANNEL_OFDM)) {
2081 pcinfo = ee->ee_pwr_cal_b;
2082 mode = AR5K_EEPROM_MODE_11B;
2083 } else if (channel->hw_value & CHANNEL_2GHZ) {
2084 pcinfo = ee->ee_pwr_cal_g;
2085 mode = AR5K_EEPROM_MODE_11G;
2086 } else {
2087 pcinfo = ee->ee_pwr_cal_a;
2088 mode = AR5K_EEPROM_MODE_11A;
2089 }
2090 max = ee->ee_n_piers[mode] - 1;
2091
2092 /* Frequency is below our calibrated
2093 * range. Use the lowest power curve
2094 * we have */
2095 if (target < pcinfo[0].freq) {
2096 idx_l = idx_r = 0;
2097 goto done;
2098 }
2099
2100 /* Frequency is above our calibrated
2101 * range. Use the highest power curve
2102 * we have */
2103 if (target > pcinfo[max].freq) {
2104 idx_l = idx_r = max;
2105 goto done;
2106 }
2107
2108 /* Frequency is inside our calibrated
2109 * channel range. Pick the surrounding
2110 * calibration piers so that we can
2111 * interpolate */
2112 for (i = 0; i <= max; i++) {
2113
2114 /* Frequency matches one of our calibration
2115 * piers, no need to interpolate, just use
2116 * that calibration pier */
2117 if (pcinfo[i].freq == target) {
2118 idx_l = idx_r = i;
2119 goto done;
2120 }
2121
2122 /* We found a calibration pier that's above
2123 * frequency, use this pier and the previous
2124 * one to interpolate */
2125 if (target < pcinfo[i].freq) {
2126 idx_r = i;
2127 idx_l = idx_r - 1;
2128 goto done;
2129 }
2130 }
2131
2132done:
2133 *pcinfo_l = &pcinfo[idx_l];
2134 *pcinfo_r = &pcinfo[idx_r];
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02002135}
2136
2137/*
2138 * Get the surrounding per-rate power calibration data
2139 * for a given frequency and interpolate between power
2140 * values to set max target power supported by hw for
2141 * each rate.
2142 */
2143static void
2144ath5k_get_rate_pcal_data(struct ath5k_hw *ah,
2145 struct ieee80211_channel *channel,
2146 struct ath5k_rate_pcal_info *rates)
2147{
2148 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
2149 struct ath5k_rate_pcal_info *rpinfo;
2150 u8 idx_l, idx_r;
2151 u8 mode, max, i;
2152 u32 target = channel->center_freq;
2153
2154 idx_l = 0;
2155 idx_r = 0;
2156
2157 if (!(channel->hw_value & CHANNEL_OFDM)) {
2158 rpinfo = ee->ee_rate_tpwr_b;
2159 mode = AR5K_EEPROM_MODE_11B;
2160 } else if (channel->hw_value & CHANNEL_2GHZ) {
2161 rpinfo = ee->ee_rate_tpwr_g;
2162 mode = AR5K_EEPROM_MODE_11G;
2163 } else {
2164 rpinfo = ee->ee_rate_tpwr_a;
2165 mode = AR5K_EEPROM_MODE_11A;
2166 }
2167 max = ee->ee_rate_target_pwr_num[mode] - 1;
2168
2169 /* Get the surrounding calibration
2170 * piers - same as above */
2171 if (target < rpinfo[0].freq) {
2172 idx_l = idx_r = 0;
2173 goto done;
2174 }
2175
2176 if (target > rpinfo[max].freq) {
2177 idx_l = idx_r = max;
2178 goto done;
2179 }
2180
2181 for (i = 0; i <= max; i++) {
2182
2183 if (rpinfo[i].freq == target) {
2184 idx_l = idx_r = i;
2185 goto done;
2186 }
2187
2188 if (target < rpinfo[i].freq) {
2189 idx_r = i;
2190 idx_l = idx_r - 1;
2191 goto done;
2192 }
2193 }
2194
2195done:
2196 /* Now interpolate power value, based on the frequency */
2197 rates->freq = target;
2198
2199 rates->target_power_6to24 =
2200 ath5k_get_interpolated_value(target, rpinfo[idx_l].freq,
2201 rpinfo[idx_r].freq,
2202 rpinfo[idx_l].target_power_6to24,
2203 rpinfo[idx_r].target_power_6to24);
2204
2205 rates->target_power_36 =
2206 ath5k_get_interpolated_value(target, rpinfo[idx_l].freq,
2207 rpinfo[idx_r].freq,
2208 rpinfo[idx_l].target_power_36,
2209 rpinfo[idx_r].target_power_36);
2210
2211 rates->target_power_48 =
2212 ath5k_get_interpolated_value(target, rpinfo[idx_l].freq,
2213 rpinfo[idx_r].freq,
2214 rpinfo[idx_l].target_power_48,
2215 rpinfo[idx_r].target_power_48);
2216
2217 rates->target_power_54 =
2218 ath5k_get_interpolated_value(target, rpinfo[idx_l].freq,
2219 rpinfo[idx_r].freq,
2220 rpinfo[idx_l].target_power_54,
2221 rpinfo[idx_r].target_power_54);
2222}
2223
2224/*
2225 * Get the max edge power for this channel if
2226 * we have such data from EEPROM's Conformance Test
2227 * Limits (CTL), and limit max power if needed.
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02002228 */
2229static void
2230ath5k_get_max_ctl_power(struct ath5k_hw *ah,
2231 struct ieee80211_channel *channel)
2232{
Luis R. Rodriguez608b88c2009-08-17 18:07:23 -07002233 struct ath_regulatory *regulatory = ath5k_hw_regulatory(ah);
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02002234 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
2235 struct ath5k_edge_power *rep = ee->ee_ctl_pwr;
2236 u8 *ctl_val = ee->ee_ctl;
2237 s16 max_chan_pwr = ah->ah_txpower.txp_max_pwr / 4;
2238 s16 edge_pwr = 0;
2239 u8 rep_idx;
2240 u8 i, ctl_mode;
2241 u8 ctl_idx = 0xFF;
2242 u32 target = channel->center_freq;
2243
Luis R. Rodriguez608b88c2009-08-17 18:07:23 -07002244 ctl_mode = ath_regd_get_band_ctl(regulatory, channel->band);
Bob Copeland6752ee92009-04-30 15:55:51 -04002245
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02002246 switch (channel->hw_value & CHANNEL_MODES) {
2247 case CHANNEL_A:
Bob Copeland6752ee92009-04-30 15:55:51 -04002248 ctl_mode |= AR5K_CTL_11A;
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02002249 break;
2250 case CHANNEL_G:
Bob Copeland6752ee92009-04-30 15:55:51 -04002251 ctl_mode |= AR5K_CTL_11G;
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02002252 break;
2253 case CHANNEL_B:
Bob Copeland6752ee92009-04-30 15:55:51 -04002254 ctl_mode |= AR5K_CTL_11B;
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02002255 break;
2256 case CHANNEL_T:
Bob Copeland6752ee92009-04-30 15:55:51 -04002257 ctl_mode |= AR5K_CTL_TURBO;
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02002258 break;
2259 case CHANNEL_TG:
Bob Copeland6752ee92009-04-30 15:55:51 -04002260 ctl_mode |= AR5K_CTL_TURBOG;
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02002261 break;
2262 case CHANNEL_XR:
2263 /* Fall through */
2264 default:
2265 return;
2266 }
Nick Kossifidis903b4742008-02-28 14:50:50 -05002267
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02002268 for (i = 0; i < ee->ee_ctls; i++) {
2269 if (ctl_val[i] == ctl_mode) {
2270 ctl_idx = i;
2271 break;
2272 }
2273 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002274
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02002275 /* If we have a CTL dataset available grab it and find the
2276 * edge power for our frequency */
2277 if (ctl_idx == 0xFF)
2278 return;
2279
2280 /* Edge powers are sorted by frequency from lower
2281 * to higher. Each CTL corresponds to 8 edge power
2282 * measurements. */
2283 rep_idx = ctl_idx * AR5K_EEPROM_N_EDGES;
2284
2285 /* Don't do boundaries check because we
2286 * might have more that one bands defined
2287 * for this mode */
2288
2289 /* Get the edge power that's closer to our
2290 * frequency */
2291 for (i = 0; i < AR5K_EEPROM_N_EDGES; i++) {
2292 rep_idx += i;
2293 if (target <= rep[rep_idx].freq)
2294 edge_pwr = (s16) rep[rep_idx].edge;
2295 }
2296
2297 if (edge_pwr)
2298 ah->ah_txpower.txp_max_pwr = 4*min(edge_pwr, max_chan_pwr);
2299}
2300
2301
2302/*
2303 * Power to PCDAC table functions
2304 */
2305
2306/*
2307 * Fill Power to PCDAC table on RF5111
2308 *
2309 * No further processing is needed for RF5111, the only thing we have to
2310 * do is fill the values below and above calibration range since eeprom data
2311 * may not cover the entire PCDAC table.
2312 */
2313static void
2314ath5k_fill_pwr_to_pcdac_table(struct ath5k_hw *ah, s16* table_min,
2315 s16 *table_max)
2316{
2317 u8 *pcdac_out = ah->ah_txpower.txp_pd_table;
2318 u8 *pcdac_tmp = ah->ah_txpower.tmpL[0];
2319 u8 pcdac_0, pcdac_n, pcdac_i, pwr_idx, i;
2320 s16 min_pwr, max_pwr;
2321
2322 /* Get table boundaries */
2323 min_pwr = table_min[0];
2324 pcdac_0 = pcdac_tmp[0];
2325
2326 max_pwr = table_max[0];
2327 pcdac_n = pcdac_tmp[table_max[0] - table_min[0]];
2328
2329 /* Extrapolate below minimum using pcdac_0 */
2330 pcdac_i = 0;
2331 for (i = 0; i < min_pwr; i++)
2332 pcdac_out[pcdac_i++] = pcdac_0;
2333
2334 /* Copy values from pcdac_tmp */
2335 pwr_idx = min_pwr;
2336 for (i = 0 ; pwr_idx <= max_pwr &&
2337 pcdac_i < AR5K_EEPROM_POWER_TABLE_SIZE; i++) {
2338 pcdac_out[pcdac_i++] = pcdac_tmp[i];
2339 pwr_idx++;
2340 }
2341
2342 /* Extrapolate above maximum */
2343 while (pcdac_i < AR5K_EEPROM_POWER_TABLE_SIZE)
2344 pcdac_out[pcdac_i++] = pcdac_n;
2345
2346}
2347
2348/*
2349 * Combine available XPD Curves and fill Linear Power to PCDAC table
2350 * on RF5112
2351 *
2352 * RFX112 can have up to 2 curves (one for low txpower range and one for
2353 * higher txpower range). We need to put them both on pcdac_out and place
2354 * them in the correct location. In case we only have one curve available
2355 * just fit it on pcdac_out (it's supposed to cover the entire range of
2356 * available pwr levels since it's always the higher power curve). Extrapolate
2357 * below and above final table if needed.
2358 */
2359static void
2360ath5k_combine_linear_pcdac_curves(struct ath5k_hw *ah, s16* table_min,
2361 s16 *table_max, u8 pdcurves)
2362{
2363 u8 *pcdac_out = ah->ah_txpower.txp_pd_table;
2364 u8 *pcdac_low_pwr;
2365 u8 *pcdac_high_pwr;
2366 u8 *pcdac_tmp;
2367 u8 pwr;
2368 s16 max_pwr_idx;
2369 s16 min_pwr_idx;
2370 s16 mid_pwr_idx = 0;
2371 /* Edge flag turs on the 7nth bit on the PCDAC
2372 * to delcare the higher power curve (force values
2373 * to be greater than 64). If we only have one curve
2374 * we don't need to set this, if we have 2 curves and
2375 * fill the table backwards this can also be used to
2376 * switch from higher power curve to lower power curve */
2377 u8 edge_flag;
2378 int i;
2379
2380 /* When we have only one curve available
2381 * that's the higher power curve. If we have
2382 * two curves the first is the high power curve
2383 * and the next is the low power curve. */
2384 if (pdcurves > 1) {
2385 pcdac_low_pwr = ah->ah_txpower.tmpL[1];
2386 pcdac_high_pwr = ah->ah_txpower.tmpL[0];
2387 mid_pwr_idx = table_max[1] - table_min[1] - 1;
2388 max_pwr_idx = (table_max[0] - table_min[0]) / 2;
2389
2390 /* If table size goes beyond 31.5dB, keep the
2391 * upper 31.5dB range when setting tx power.
2392 * Note: 126 = 31.5 dB in quarter dB steps */
2393 if (table_max[0] - table_min[1] > 126)
2394 min_pwr_idx = table_max[0] - 126;
2395 else
2396 min_pwr_idx = table_min[1];
2397
2398 /* Since we fill table backwards
2399 * start from high power curve */
2400 pcdac_tmp = pcdac_high_pwr;
2401
2402 edge_flag = 0x40;
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02002403 } else {
2404 pcdac_low_pwr = ah->ah_txpower.tmpL[1]; /* Zeroed */
2405 pcdac_high_pwr = ah->ah_txpower.tmpL[0];
2406 min_pwr_idx = table_min[0];
2407 max_pwr_idx = (table_max[0] - table_min[0]) / 2;
2408 pcdac_tmp = pcdac_high_pwr;
2409 edge_flag = 0;
2410 }
2411
2412 /* This is used when setting tx power*/
2413 ah->ah_txpower.txp_min_idx = min_pwr_idx/2;
2414
2415 /* Fill Power to PCDAC table backwards */
2416 pwr = max_pwr_idx;
2417 for (i = 63; i >= 0; i--) {
2418 /* Entering lower power range, reset
2419 * edge flag and set pcdac_tmp to lower
2420 * power curve.*/
2421 if (edge_flag == 0x40 &&
2422 (2*pwr <= (table_max[1] - table_min[0]) || pwr == 0)) {
2423 edge_flag = 0x00;
2424 pcdac_tmp = pcdac_low_pwr;
2425 pwr = mid_pwr_idx/2;
2426 }
2427
2428 /* Don't go below 1, extrapolate below if we have
2429 * already swithced to the lower power curve -or
2430 * we only have one curve and edge_flag is zero
2431 * anyway */
2432 if (pcdac_tmp[pwr] < 1 && (edge_flag == 0x00)) {
2433 while (i >= 0) {
2434 pcdac_out[i] = pcdac_out[i + 1];
2435 i--;
2436 }
2437 break;
2438 }
2439
2440 pcdac_out[i] = pcdac_tmp[pwr] | edge_flag;
2441
2442 /* Extrapolate above if pcdac is greater than
2443 * 126 -this can happen because we OR pcdac_out
2444 * value with edge_flag on high power curve */
2445 if (pcdac_out[i] > 126)
2446 pcdac_out[i] = 126;
2447
2448 /* Decrease by a 0.5dB step */
2449 pwr--;
2450 }
2451}
2452
2453/* Write PCDAC values on hw */
2454static void
2455ath5k_setup_pcdac_table(struct ath5k_hw *ah)
2456{
2457 u8 *pcdac_out = ah->ah_txpower.txp_pd_table;
2458 int i;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002459
2460 /*
2461 * Write TX power values
2462 */
2463 for (i = 0; i < (AR5K_EEPROM_POWER_TABLE_SIZE / 2); i++) {
2464 ath5k_hw_reg_write(ah,
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02002465 (((pcdac_out[2*i + 0] << 8 | 0xff) & 0xffff) << 0) |
2466 (((pcdac_out[2*i + 1] << 8 | 0xff) & 0xffff) << 16),
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002467 AR5K_PHY_PCDAC_TXPOWER(i));
2468 }
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02002469}
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002470
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02002471
2472/*
2473 * Power to PDADC table functions
2474 */
2475
2476/*
2477 * Set the gain boundaries and create final Power to PDADC table
2478 *
2479 * We can have up to 4 pd curves, we need to do a simmilar process
2480 * as we do for RF5112. This time we don't have an edge_flag but we
2481 * set the gain boundaries on a separate register.
2482 */
2483static void
2484ath5k_combine_pwr_to_pdadc_curves(struct ath5k_hw *ah,
2485 s16 *pwr_min, s16 *pwr_max, u8 pdcurves)
2486{
2487 u8 gain_boundaries[AR5K_EEPROM_N_PD_GAINS];
2488 u8 *pdadc_out = ah->ah_txpower.txp_pd_table;
2489 u8 *pdadc_tmp;
2490 s16 pdadc_0;
2491 u8 pdadc_i, pdadc_n, pwr_step, pdg, max_idx, table_size;
2492 u8 pd_gain_overlap;
2493
2494 /* Note: Register value is initialized on initvals
2495 * there is no feedback from hw.
2496 * XXX: What about pd_gain_overlap from EEPROM ? */
2497 pd_gain_overlap = (u8) ath5k_hw_reg_read(ah, AR5K_PHY_TPC_RG5) &
2498 AR5K_PHY_TPC_RG5_PD_GAIN_OVERLAP;
2499
2500 /* Create final PDADC table */
2501 for (pdg = 0, pdadc_i = 0; pdg < pdcurves; pdg++) {
2502 pdadc_tmp = ah->ah_txpower.tmpL[pdg];
2503
2504 if (pdg == pdcurves - 1)
2505 /* 2 dB boundary stretch for last
2506 * (higher power) curve */
2507 gain_boundaries[pdg] = pwr_max[pdg] + 4;
2508 else
2509 /* Set gain boundary in the middle
2510 * between this curve and the next one */
2511 gain_boundaries[pdg] =
2512 (pwr_max[pdg] + pwr_min[pdg + 1]) / 2;
2513
2514 /* Sanity check in case our 2 db stretch got out of
2515 * range. */
2516 if (gain_boundaries[pdg] > AR5K_TUNE_MAX_TXPOWER)
2517 gain_boundaries[pdg] = AR5K_TUNE_MAX_TXPOWER;
2518
2519 /* For the first curve (lower power)
2520 * start from 0 dB */
2521 if (pdg == 0)
2522 pdadc_0 = 0;
2523 else
2524 /* For the other curves use the gain overlap */
2525 pdadc_0 = (gain_boundaries[pdg - 1] - pwr_min[pdg]) -
2526 pd_gain_overlap;
2527
2528 /* Force each power step to be at least 0.5 dB */
2529 if ((pdadc_tmp[1] - pdadc_tmp[0]) > 1)
2530 pwr_step = pdadc_tmp[1] - pdadc_tmp[0];
2531 else
2532 pwr_step = 1;
2533
2534 /* If pdadc_0 is negative, we need to extrapolate
2535 * below this pdgain by a number of pwr_steps */
2536 while ((pdadc_0 < 0) && (pdadc_i < 128)) {
2537 s16 tmp = pdadc_tmp[0] + pdadc_0 * pwr_step;
2538 pdadc_out[pdadc_i++] = (tmp < 0) ? 0 : (u8) tmp;
2539 pdadc_0++;
2540 }
2541
2542 /* Set last pwr level, using gain boundaries */
2543 pdadc_n = gain_boundaries[pdg] + pd_gain_overlap - pwr_min[pdg];
2544 /* Limit it to be inside pwr range */
2545 table_size = pwr_max[pdg] - pwr_min[pdg];
2546 max_idx = (pdadc_n < table_size) ? pdadc_n : table_size;
2547
2548 /* Fill pdadc_out table */
Bob Copeland4f59fce2010-04-07 23:55:59 -04002549 while (pdadc_0 < max_idx && pdadc_i < 128)
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02002550 pdadc_out[pdadc_i++] = pdadc_tmp[pdadc_0++];
2551
2552 /* Need to extrapolate above this pdgain? */
2553 if (pdadc_n <= max_idx)
2554 continue;
2555
2556 /* Force each power step to be at least 0.5 dB */
2557 if ((pdadc_tmp[table_size - 1] - pdadc_tmp[table_size - 2]) > 1)
2558 pwr_step = pdadc_tmp[table_size - 1] -
2559 pdadc_tmp[table_size - 2];
2560 else
2561 pwr_step = 1;
2562
2563 /* Extrapolate above */
2564 while ((pdadc_0 < (s16) pdadc_n) &&
2565 (pdadc_i < AR5K_EEPROM_POWER_TABLE_SIZE * 2)) {
2566 s16 tmp = pdadc_tmp[table_size - 1] +
2567 (pdadc_0 - max_idx) * pwr_step;
2568 pdadc_out[pdadc_i++] = (tmp > 127) ? 127 : (u8) tmp;
2569 pdadc_0++;
2570 }
2571 }
2572
2573 while (pdg < AR5K_EEPROM_N_PD_GAINS) {
2574 gain_boundaries[pdg] = gain_boundaries[pdg - 1];
2575 pdg++;
2576 }
2577
2578 while (pdadc_i < AR5K_EEPROM_POWER_TABLE_SIZE * 2) {
2579 pdadc_out[pdadc_i] = pdadc_out[pdadc_i - 1];
2580 pdadc_i++;
2581 }
2582
2583 /* Set gain boundaries */
2584 ath5k_hw_reg_write(ah,
2585 AR5K_REG_SM(pd_gain_overlap,
2586 AR5K_PHY_TPC_RG5_PD_GAIN_OVERLAP) |
2587 AR5K_REG_SM(gain_boundaries[0],
2588 AR5K_PHY_TPC_RG5_PD_GAIN_BOUNDARY_1) |
2589 AR5K_REG_SM(gain_boundaries[1],
2590 AR5K_PHY_TPC_RG5_PD_GAIN_BOUNDARY_2) |
2591 AR5K_REG_SM(gain_boundaries[2],
2592 AR5K_PHY_TPC_RG5_PD_GAIN_BOUNDARY_3) |
2593 AR5K_REG_SM(gain_boundaries[3],
2594 AR5K_PHY_TPC_RG5_PD_GAIN_BOUNDARY_4),
2595 AR5K_PHY_TPC_RG5);
2596
2597 /* Used for setting rate power table */
2598 ah->ah_txpower.txp_min_idx = pwr_min[0];
2599
2600}
2601
2602/* Write PDADC values on hw */
2603static void
2604ath5k_setup_pwr_to_pdadc_table(struct ath5k_hw *ah,
2605 u8 pdcurves, u8 *pdg_to_idx)
2606{
2607 u8 *pdadc_out = ah->ah_txpower.txp_pd_table;
2608 u32 reg;
2609 u8 i;
2610
2611 /* Select the right pdgain curves */
2612
2613 /* Clear current settings */
2614 reg = ath5k_hw_reg_read(ah, AR5K_PHY_TPC_RG1);
2615 reg &= ~(AR5K_PHY_TPC_RG1_PDGAIN_1 |
2616 AR5K_PHY_TPC_RG1_PDGAIN_2 |
2617 AR5K_PHY_TPC_RG1_PDGAIN_3 |
2618 AR5K_PHY_TPC_RG1_NUM_PD_GAIN);
2619
2620 /*
2621 * Use pd_gains curve from eeprom
2622 *
2623 * This overrides the default setting from initvals
2624 * in case some vendors (e.g. Zcomax) don't use the default
2625 * curves. If we don't honor their settings we 'll get a
2626 * 5dB (1 * gain overlap ?) drop.
2627 */
2628 reg |= AR5K_REG_SM(pdcurves, AR5K_PHY_TPC_RG1_NUM_PD_GAIN);
2629
2630 switch (pdcurves) {
2631 case 3:
2632 reg |= AR5K_REG_SM(pdg_to_idx[2], AR5K_PHY_TPC_RG1_PDGAIN_3);
2633 /* Fall through */
2634 case 2:
2635 reg |= AR5K_REG_SM(pdg_to_idx[1], AR5K_PHY_TPC_RG1_PDGAIN_2);
2636 /* Fall through */
2637 case 1:
2638 reg |= AR5K_REG_SM(pdg_to_idx[0], AR5K_PHY_TPC_RG1_PDGAIN_1);
2639 break;
2640 }
2641 ath5k_hw_reg_write(ah, reg, AR5K_PHY_TPC_RG1);
2642
2643 /*
2644 * Write TX power values
2645 */
2646 for (i = 0; i < (AR5K_EEPROM_POWER_TABLE_SIZE / 2); i++) {
2647 ath5k_hw_reg_write(ah,
2648 ((pdadc_out[4*i + 0] & 0xff) << 0) |
2649 ((pdadc_out[4*i + 1] & 0xff) << 8) |
2650 ((pdadc_out[4*i + 2] & 0xff) << 16) |
2651 ((pdadc_out[4*i + 3] & 0xff) << 24),
2652 AR5K_PHY_PDADC_TXPOWER(i));
2653 }
2654}
2655
2656
2657/*
2658 * Common code for PCDAC/PDADC tables
2659 */
2660
2661/*
2662 * This is the main function that uses all of the above
2663 * to set PCDAC/PDADC table on hw for the current channel.
2664 * This table is used for tx power calibration on the basband,
2665 * without it we get weird tx power levels and in some cases
2666 * distorted spectral mask
2667 */
2668static int
2669ath5k_setup_channel_powertable(struct ath5k_hw *ah,
2670 struct ieee80211_channel *channel,
2671 u8 ee_mode, u8 type)
2672{
2673 struct ath5k_pdgain_info *pdg_L, *pdg_R;
2674 struct ath5k_chan_pcal_info *pcinfo_L;
2675 struct ath5k_chan_pcal_info *pcinfo_R;
2676 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
2677 u8 *pdg_curve_to_idx = ee->ee_pdc_to_idx[ee_mode];
2678 s16 table_min[AR5K_EEPROM_N_PD_GAINS];
2679 s16 table_max[AR5K_EEPROM_N_PD_GAINS];
2680 u8 *tmpL;
2681 u8 *tmpR;
2682 u32 target = channel->center_freq;
2683 int pdg, i;
2684
2685 /* Get surounding freq piers for this channel */
2686 ath5k_get_chan_pcal_surrounding_piers(ah, channel,
2687 &pcinfo_L,
2688 &pcinfo_R);
2689
2690 /* Loop over pd gain curves on
2691 * surounding freq piers by index */
2692 for (pdg = 0; pdg < ee->ee_pd_gains[ee_mode]; pdg++) {
2693
2694 /* Fill curves in reverse order
2695 * from lower power (max gain)
2696 * to higher power. Use curve -> idx
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002697 * backmapping we did on eeprom init */
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02002698 u8 idx = pdg_curve_to_idx[pdg];
2699
2700 /* Grab the needed curves by index */
2701 pdg_L = &pcinfo_L->pd_curves[idx];
2702 pdg_R = &pcinfo_R->pd_curves[idx];
2703
2704 /* Initialize the temp tables */
2705 tmpL = ah->ah_txpower.tmpL[pdg];
2706 tmpR = ah->ah_txpower.tmpR[pdg];
2707
2708 /* Set curve's x boundaries and create
2709 * curves so that they cover the same
2710 * range (if we don't do that one table
2711 * will have values on some range and the
2712 * other one won't have any so interpolation
2713 * will fail) */
2714 table_min[pdg] = min(pdg_L->pd_pwr[0],
2715 pdg_R->pd_pwr[0]) / 2;
2716
2717 table_max[pdg] = max(pdg_L->pd_pwr[pdg_L->pd_points - 1],
2718 pdg_R->pd_pwr[pdg_R->pd_points - 1]) / 2;
2719
2720 /* Now create the curves on surrounding channels
2721 * and interpolate if needed to get the final
2722 * curve for this gain on this channel */
2723 switch (type) {
2724 case AR5K_PWRTABLE_LINEAR_PCDAC:
2725 /* Override min/max so that we don't loose
2726 * accuracy (don't divide by 2) */
2727 table_min[pdg] = min(pdg_L->pd_pwr[0],
2728 pdg_R->pd_pwr[0]);
2729
2730 table_max[pdg] =
2731 max(pdg_L->pd_pwr[pdg_L->pd_points - 1],
2732 pdg_R->pd_pwr[pdg_R->pd_points - 1]);
2733
2734 /* Override minimum so that we don't get
2735 * out of bounds while extrapolating
2736 * below. Don't do this when we have 2
2737 * curves and we are on the high power curve
2738 * because table_min is ok in this case */
2739 if (!(ee->ee_pd_gains[ee_mode] > 1 && pdg == 0)) {
2740
2741 table_min[pdg] =
2742 ath5k_get_linear_pcdac_min(pdg_L->pd_step,
2743 pdg_R->pd_step,
2744 pdg_L->pd_pwr,
2745 pdg_R->pd_pwr);
2746
2747 /* Don't go too low because we will
2748 * miss the upper part of the curve.
2749 * Note: 126 = 31.5dB (max power supported)
2750 * in 0.25dB units */
2751 if (table_max[pdg] - table_min[pdg] > 126)
2752 table_min[pdg] = table_max[pdg] - 126;
2753 }
2754
2755 /* Fall through */
2756 case AR5K_PWRTABLE_PWR_TO_PCDAC:
2757 case AR5K_PWRTABLE_PWR_TO_PDADC:
2758
2759 ath5k_create_power_curve(table_min[pdg],
2760 table_max[pdg],
2761 pdg_L->pd_pwr,
2762 pdg_L->pd_step,
2763 pdg_L->pd_points, tmpL, type);
2764
2765 /* We are in a calibration
2766 * pier, no need to interpolate
2767 * between freq piers */
2768 if (pcinfo_L == pcinfo_R)
2769 continue;
2770
2771 ath5k_create_power_curve(table_min[pdg],
2772 table_max[pdg],
2773 pdg_R->pd_pwr,
2774 pdg_R->pd_step,
2775 pdg_R->pd_points, tmpR, type);
2776 break;
2777 default:
2778 return -EINVAL;
2779 }
2780
2781 /* Interpolate between curves
2782 * of surounding freq piers to
2783 * get the final curve for this
2784 * pd gain. Re-use tmpL for interpolation
2785 * output */
2786 for (i = 0; (i < (u16) (table_max[pdg] - table_min[pdg])) &&
2787 (i < AR5K_EEPROM_POWER_TABLE_SIZE); i++) {
2788 tmpL[i] = (u8) ath5k_get_interpolated_value(target,
2789 (s16) pcinfo_L->freq,
2790 (s16) pcinfo_R->freq,
2791 (s16) tmpL[i],
2792 (s16) tmpR[i]);
2793 }
2794 }
2795
2796 /* Now we have a set of curves for this
2797 * channel on tmpL (x range is table_max - table_min
2798 * and y values are tmpL[pdg][]) sorted in the same
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002799 * order as EEPROM (because we've used the backmapping).
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02002800 * So for RF5112 it's from higher power to lower power
2801 * and for RF2413 it's from lower power to higher power.
2802 * For RF5111 we only have one curve. */
2803
2804 /* Fill min and max power levels for this
2805 * channel by interpolating the values on
2806 * surounding channels to complete the dataset */
2807 ah->ah_txpower.txp_min_pwr = ath5k_get_interpolated_value(target,
2808 (s16) pcinfo_L->freq,
2809 (s16) pcinfo_R->freq,
2810 pcinfo_L->min_pwr, pcinfo_R->min_pwr);
2811
2812 ah->ah_txpower.txp_max_pwr = ath5k_get_interpolated_value(target,
2813 (s16) pcinfo_L->freq,
2814 (s16) pcinfo_R->freq,
2815 pcinfo_L->max_pwr, pcinfo_R->max_pwr);
2816
2817 /* We are ready to go, fill PCDAC/PDADC
2818 * table and write settings on hardware */
2819 switch (type) {
2820 case AR5K_PWRTABLE_LINEAR_PCDAC:
2821 /* For RF5112 we can have one or two curves
2822 * and each curve covers a certain power lvl
2823 * range so we need to do some more processing */
2824 ath5k_combine_linear_pcdac_curves(ah, table_min, table_max,
2825 ee->ee_pd_gains[ee_mode]);
2826
2827 /* Set txp.offset so that we can
2828 * match max power value with max
2829 * table index */
2830 ah->ah_txpower.txp_offset = 64 - (table_max[0] / 2);
2831
2832 /* Write settings on hw */
2833 ath5k_setup_pcdac_table(ah);
2834 break;
2835 case AR5K_PWRTABLE_PWR_TO_PCDAC:
2836 /* We are done for RF5111 since it has only
2837 * one curve, just fit the curve on the table */
2838 ath5k_fill_pwr_to_pcdac_table(ah, table_min, table_max);
2839
2840 /* No rate powertable adjustment for RF5111 */
2841 ah->ah_txpower.txp_min_idx = 0;
2842 ah->ah_txpower.txp_offset = 0;
2843
2844 /* Write settings on hw */
2845 ath5k_setup_pcdac_table(ah);
2846 break;
2847 case AR5K_PWRTABLE_PWR_TO_PDADC:
2848 /* Set PDADC boundaries and fill
2849 * final PDADC table */
2850 ath5k_combine_pwr_to_pdadc_curves(ah, table_min, table_max,
2851 ee->ee_pd_gains[ee_mode]);
2852
2853 /* Write settings on hw */
2854 ath5k_setup_pwr_to_pdadc_table(ah, pdg, pdg_curve_to_idx);
2855
2856 /* Set txp.offset, note that table_min
2857 * can be negative */
2858 ah->ah_txpower.txp_offset = table_min[0];
2859 break;
2860 default:
2861 return -EINVAL;
2862 }
2863
2864 return 0;
2865}
2866
2867
2868/*
2869 * Per-rate tx power setting
2870 *
2871 * This is the code that sets the desired tx power (below
2872 * maximum) on hw for each rate (we also have TPC that sets
2873 * power per packet). We do that by providing an index on the
2874 * PCDAC/PDADC table we set up.
2875 */
2876
2877/*
2878 * Set rate power table
2879 *
2880 * For now we only limit txpower based on maximum tx power
2881 * supported by hw (what's inside rate_info). We need to limit
2882 * this even more, based on regulatory domain etc.
2883 *
2884 * Rate power table contains indices to PCDAC/PDADC table (0.5dB steps)
2885 * and is indexed as follows:
2886 * rates[0] - rates[7] -> OFDM rates
2887 * rates[8] - rates[14] -> CCK rates
2888 * rates[15] -> XR rates (they all have the same power)
2889 */
2890static void
2891ath5k_setup_rate_powertable(struct ath5k_hw *ah, u16 max_pwr,
2892 struct ath5k_rate_pcal_info *rate_info,
2893 u8 ee_mode)
2894{
2895 unsigned int i;
2896 u16 *rates;
2897
2898 /* max_pwr is power level we got from driver/user in 0.5dB
2899 * units, switch to 0.25dB units so we can compare */
2900 max_pwr *= 2;
2901 max_pwr = min(max_pwr, (u16) ah->ah_txpower.txp_max_pwr) / 2;
2902
2903 /* apply rate limits */
2904 rates = ah->ah_txpower.txp_rates_power_table;
2905
2906 /* OFDM rates 6 to 24Mb/s */
2907 for (i = 0; i < 5; i++)
2908 rates[i] = min(max_pwr, rate_info->target_power_6to24);
2909
2910 /* Rest OFDM rates */
2911 rates[5] = min(rates[0], rate_info->target_power_36);
2912 rates[6] = min(rates[0], rate_info->target_power_48);
2913 rates[7] = min(rates[0], rate_info->target_power_54);
2914
2915 /* CCK rates */
2916 /* 1L */
2917 rates[8] = min(rates[0], rate_info->target_power_6to24);
2918 /* 2L */
2919 rates[9] = min(rates[0], rate_info->target_power_36);
2920 /* 2S */
2921 rates[10] = min(rates[0], rate_info->target_power_36);
2922 /* 5L */
2923 rates[11] = min(rates[0], rate_info->target_power_48);
2924 /* 5S */
2925 rates[12] = min(rates[0], rate_info->target_power_48);
2926 /* 11L */
2927 rates[13] = min(rates[0], rate_info->target_power_54);
2928 /* 11S */
2929 rates[14] = min(rates[0], rate_info->target_power_54);
2930
2931 /* XR rates */
2932 rates[15] = min(rates[0], rate_info->target_power_6to24);
2933
2934 /* CCK rates have different peak to average ratio
2935 * so we have to tweak their power so that gainf
2936 * correction works ok. For this we use OFDM to
2937 * CCK delta from eeprom */
2938 if ((ee_mode == AR5K_EEPROM_MODE_11G) &&
2939 (ah->ah_phy_revision < AR5K_SREV_PHY_5212A))
2940 for (i = 8; i <= 15; i++)
2941 rates[i] -= ah->ah_txpower.txp_cck_ofdm_gainf_delta;
2942
Nick Kossifidisa0823812009-04-30 15:55:44 -04002943 /* Now that we have all rates setup use table offset to
2944 * match the power range set by user with the power indices
2945 * on PCDAC/PDADC table */
2946 for (i = 0; i < 16; i++) {
2947 rates[i] += ah->ah_txpower.txp_offset;
2948 /* Don't get out of bounds */
2949 if (rates[i] > 63)
2950 rates[i] = 63;
2951 }
2952
2953 /* Min/max in 0.25dB units */
2954 ah->ah_txpower.txp_min_pwr = 2 * rates[7];
2955 ah->ah_txpower.txp_max_pwr = 2 * rates[0];
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02002956 ah->ah_txpower.txp_ofdm = rates[7];
2957}
2958
2959
2960/*
2961 * Set transmition power
2962 */
2963int
2964ath5k_hw_txpower(struct ath5k_hw *ah, struct ieee80211_channel *channel,
2965 u8 ee_mode, u8 txpower)
2966{
2967 struct ath5k_rate_pcal_info rate_info;
2968 u8 type;
2969 int ret;
2970
2971 ATH5K_TRACE(ah->ah_sc);
2972 if (txpower > AR5K_TUNE_MAX_TXPOWER) {
2973 ATH5K_ERR(ah->ah_sc, "invalid tx power: %u\n", txpower);
2974 return -EINVAL;
2975 }
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02002976
2977 /* Reset TX power values */
2978 memset(&ah->ah_txpower, 0, sizeof(ah->ah_txpower));
2979 ah->ah_txpower.txp_tpc = AR5K_TUNE_TPC_TXPOWER;
2980 ah->ah_txpower.txp_min_pwr = 0;
2981 ah->ah_txpower.txp_max_pwr = AR5K_TUNE_MAX_TXPOWER;
2982
2983 /* Initialize TX power table */
2984 switch (ah->ah_radio) {
2985 case AR5K_RF5111:
2986 type = AR5K_PWRTABLE_PWR_TO_PCDAC;
2987 break;
2988 case AR5K_RF5112:
2989 type = AR5K_PWRTABLE_LINEAR_PCDAC;
2990 break;
2991 case AR5K_RF2413:
2992 case AR5K_RF5413:
2993 case AR5K_RF2316:
2994 case AR5K_RF2317:
2995 case AR5K_RF2425:
2996 type = AR5K_PWRTABLE_PWR_TO_PDADC;
2997 break;
2998 default:
2999 return -EINVAL;
3000 }
3001
3002 /* FIXME: Only on channel/mode change */
3003 ret = ath5k_setup_channel_powertable(ah, channel, ee_mode, type);
3004 if (ret)
3005 return ret;
3006
3007 /* Limit max power if we have a CTL available */
3008 ath5k_get_max_ctl_power(ah, channel);
3009
3010 /* FIXME: Tx power limit for this regdomain
3011 * XXX: Mac80211/CRDA will do that anyway ? */
3012
3013 /* FIXME: Antenna reduction stuff */
3014
3015 /* FIXME: Limit power on turbo modes */
3016
3017 /* FIXME: TPC scale reduction */
3018
3019 /* Get surounding channels for per-rate power table
3020 * calibration */
3021 ath5k_get_rate_pcal_data(ah, channel, &rate_info);
3022
3023 /* Setup rate power table */
3024 ath5k_setup_rate_powertable(ah, txpower, &rate_info, ee_mode);
3025
3026 /* Write rate power table on hw */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003027 ath5k_hw_reg_write(ah, AR5K_TXPOWER_OFDM(3, 24) |
3028 AR5K_TXPOWER_OFDM(2, 16) | AR5K_TXPOWER_OFDM(1, 8) |
3029 AR5K_TXPOWER_OFDM(0, 0), AR5K_PHY_TXPOWER_RATE1);
3030
3031 ath5k_hw_reg_write(ah, AR5K_TXPOWER_OFDM(7, 24) |
3032 AR5K_TXPOWER_OFDM(6, 16) | AR5K_TXPOWER_OFDM(5, 8) |
3033 AR5K_TXPOWER_OFDM(4, 0), AR5K_PHY_TXPOWER_RATE2);
3034
3035 ath5k_hw_reg_write(ah, AR5K_TXPOWER_CCK(10, 24) |
3036 AR5K_TXPOWER_CCK(9, 16) | AR5K_TXPOWER_CCK(15, 8) |
3037 AR5K_TXPOWER_CCK(8, 0), AR5K_PHY_TXPOWER_RATE3);
3038
3039 ath5k_hw_reg_write(ah, AR5K_TXPOWER_CCK(14, 24) |
3040 AR5K_TXPOWER_CCK(13, 16) | AR5K_TXPOWER_CCK(12, 8) |
3041 AR5K_TXPOWER_CCK(11, 0), AR5K_PHY_TXPOWER_RATE4);
3042
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02003043 /* FIXME: TPC support */
3044 if (ah->ah_txpower.txp_tpc) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003045 ath5k_hw_reg_write(ah, AR5K_PHY_TXPOWER_RATE_MAX_TPC_ENABLE |
3046 AR5K_TUNE_MAX_TXPOWER, AR5K_PHY_TXPOWER_RATE_MAX);
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02003047
3048 ath5k_hw_reg_write(ah,
3049 AR5K_REG_MS(AR5K_TUNE_MAX_TXPOWER, AR5K_TPC_ACK) |
3050 AR5K_REG_MS(AR5K_TUNE_MAX_TXPOWER, AR5K_TPC_CTS) |
3051 AR5K_REG_MS(AR5K_TUNE_MAX_TXPOWER, AR5K_TPC_CHIRP),
3052 AR5K_TPC);
3053 } else {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003054 ath5k_hw_reg_write(ah, AR5K_PHY_TXPOWER_RATE_MAX |
3055 AR5K_TUNE_MAX_TXPOWER, AR5K_PHY_TXPOWER_RATE_MAX);
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02003056 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003057
3058 return 0;
3059}
3060
Nick Kossifidisa0823812009-04-30 15:55:44 -04003061int ath5k_hw_set_txpower_limit(struct ath5k_hw *ah, u8 txpower)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003062{
3063 /*Just a try M.F.*/
Bob Copeland46026e82009-06-10 22:22:20 -04003064 struct ieee80211_channel *channel = ah->ah_current_channel;
Nick Kossifidisa0823812009-04-30 15:55:44 -04003065 u8 ee_mode;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003066
3067 ATH5K_TRACE(ah->ah_sc);
Nick Kossifidisa0823812009-04-30 15:55:44 -04003068
3069 switch (channel->hw_value & CHANNEL_MODES) {
3070 case CHANNEL_A:
3071 case CHANNEL_T:
3072 case CHANNEL_XR:
3073 ee_mode = AR5K_EEPROM_MODE_11A;
3074 break;
3075 case CHANNEL_G:
3076 case CHANNEL_TG:
3077 ee_mode = AR5K_EEPROM_MODE_11G;
3078 break;
3079 case CHANNEL_B:
3080 ee_mode = AR5K_EEPROM_MODE_11B;
3081 break;
3082 default:
3083 ATH5K_ERR(ah->ah_sc,
3084 "invalid channel: %d\n", channel->center_freq);
3085 return -EINVAL;
3086 }
3087
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003088 ATH5K_DBG(ah->ah_sc, ATH5K_DEBUG_TXPOWER,
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02003089 "changing txpower to %d\n", txpower);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003090
Nick Kossifidisa0823812009-04-30 15:55:44 -04003091 return ath5k_hw_txpower(ah, channel, ee_mode, txpower);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003092}