blob: fd93c4e20214728f3351dcdb38b14f64da554d64 [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
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030023#define _ATH5K_PHY
24
Jiri Slabyfa1c1142007-08-12 17:33:16 +020025#include <linux/delay.h>
26
27#include "ath5k.h"
28#include "reg.h"
29#include "base.h"
Nick Kossifidis33a31822009-02-09 06:00:34 +020030#include "rfbuffer.h"
31#include "rfgain.h"
Jiri Slabyfa1c1142007-08-12 17:33:16 +020032
33/*
34 * Used to modify RF Banks before writing them to AR5K_RF_BUFFER
35 */
Nick Kossifidis8892e4e2009-02-09 06:06:34 +020036static unsigned int ath5k_hw_rfb_op(struct ath5k_hw *ah,
37 const struct ath5k_rf_reg *rf_regs,
38 u32 val, u8 reg_id, bool set)
Jiri Slabyfa1c1142007-08-12 17:33:16 +020039{
Nick Kossifidis8892e4e2009-02-09 06:06:34 +020040 const struct ath5k_rf_reg *rfreg = NULL;
41 u8 offset, bank, num_bits, col, position;
42 u16 entry;
43 u32 mask, data, last_bit, bits_shifted, first_bit;
44 u32 *rfb;
45 s32 bits_left;
Jiri Slabyfa1c1142007-08-12 17:33:16 +020046 int i;
47
48 data = 0;
Nick Kossifidis8892e4e2009-02-09 06:06:34 +020049 rfb = ah->ah_rf_banks;
Jiri Slabyfa1c1142007-08-12 17:33:16 +020050
Nick Kossifidis8892e4e2009-02-09 06:06:34 +020051 for (i = 0; i < ah->ah_rf_regs_count; i++) {
52 if (rf_regs[i].index == reg_id) {
53 rfreg = &rf_regs[i];
54 break;
55 }
56 }
57
58 if (rfb == NULL || rfreg == NULL) {
59 ATH5K_PRINTF("Rf register not found!\n");
Jiri Slabyfa1c1142007-08-12 17:33:16 +020060 /* should not happen */
61 return 0;
Nick Kossifidis8892e4e2009-02-09 06:06:34 +020062 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +020063
Nick Kossifidis8892e4e2009-02-09 06:06:34 +020064 bank = rfreg->bank;
65 num_bits = rfreg->field.len;
66 first_bit = rfreg->field.pos;
67 col = rfreg->field.col;
68
69 /* first_bit is an offset from bank's
70 * start. Since we have all banks on
71 * the same array, we use this offset
72 * to mark each bank's start */
73 offset = ah->ah_offset[bank];
74
75 /* Boundary check */
76 if (!(col <= 3 && num_bits <= 32 && first_bit + num_bits <= 319)) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +020077 ATH5K_PRINTF("invalid values at offset %u\n", offset);
78 return 0;
79 }
80
Nick Kossifidis8892e4e2009-02-09 06:06:34 +020081 entry = ((first_bit - 1) / 8) + offset;
82 position = (first_bit - 1) % 8;
Jiri Slabyfa1c1142007-08-12 17:33:16 +020083
Joe Perchese9010e22008-03-07 14:21:16 -080084 if (set)
Nick Kossifidis8892e4e2009-02-09 06:06:34 +020085 data = ath5k_hw_bitswap(val, num_bits);
Jiri Slabyfa1c1142007-08-12 17:33:16 +020086
Nick Kossifidis8892e4e2009-02-09 06:06:34 +020087 for (bits_shifted = 0, bits_left = num_bits; bits_left > 0;
88 position = 0, entry++) {
89
90 last_bit = (position + bits_left > 8) ? 8 :
91 position + bits_left;
92
93 mask = (((1 << last_bit) - 1) ^ ((1 << position) - 1)) <<
94 (col * 8);
Jiri Slabyfa1c1142007-08-12 17:33:16 +020095
Joe Perchese9010e22008-03-07 14:21:16 -080096 if (set) {
Nick Kossifidis8892e4e2009-02-09 06:06:34 +020097 rfb[entry] &= ~mask;
98 rfb[entry] |= ((data << position) << (col * 8)) & mask;
Jiri Slabyfa1c1142007-08-12 17:33:16 +020099 data >>= (8 - position);
100 } else {
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200101 data |= (((rfb[entry] & mask) >> (col * 8)) >> position)
102 << bits_shifted;
103 bits_shifted += last_bit - position;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200104 }
105
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200106 bits_left -= 8 - position;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200107 }
108
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200109 data = set ? 1 : ath5k_hw_bitswap(data, num_bits);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200110
111 return data;
112}
113
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200114/**********************\
115* RF Gain optimization *
116\**********************/
117
118/*
119 * This code is used to optimize rf gain on different environments
120 * (temprature mostly) based on feedback from a power detector.
121 *
122 * It's only used on RF5111 and RF5112, later RF chips seem to have
123 * auto adjustment on hw -notice they have a much smaller BANK 7 and
124 * no gain optimization ladder-.
125 *
126 * For more infos check out this patent doc
127 * http://www.freepatentsonline.com/7400691.html
128 *
129 * This paper describes power drops as seen on the receiver due to
130 * probe packets
131 * http://www.cnri.dit.ie/publications/ICT08%20-%20Practical%20Issues
132 * %20of%20Power%20Control.pdf
133 *
134 * And this is the MadWiFi bug entry related to the above
135 * http://madwifi-project.org/ticket/1659
136 * with various measurements and diagrams
137 *
138 * TODO: Deal with power drops due to probes by setting an apropriate
139 * tx power on the probe packets ! Make this part of the calibration process.
140 */
141
142/* Initialize ah_gain durring attach */
143int ath5k_hw_rfgain_opt_init(struct ath5k_hw *ah)
144{
145 /* Initialize the gain optimization values */
146 switch (ah->ah_radio) {
147 case AR5K_RF5111:
148 ah->ah_gain.g_step_idx = rfgain_opt_5111.go_default;
149 ah->ah_gain.g_low = 20;
150 ah->ah_gain.g_high = 35;
151 ah->ah_gain.g_state = AR5K_RFGAIN_ACTIVE;
152 break;
153 case AR5K_RF5112:
154 ah->ah_gain.g_step_idx = rfgain_opt_5112.go_default;
155 ah->ah_gain.g_low = 20;
156 ah->ah_gain.g_high = 85;
157 ah->ah_gain.g_state = AR5K_RFGAIN_ACTIVE;
158 break;
159 default:
160 return -EINVAL;
161 }
162
163 return 0;
164}
165
166/* Schedule a gain probe check on the next transmited packet.
167 * That means our next packet is going to be sent with lower
168 * tx power and a Peak to Average Power Detector (PAPD) will try
169 * to measure the gain.
170 *
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200171 * XXX: How about forcing a tx packet (bypassing PCU arbitrator etc)
172 * just after we enable the probe so that we don't mess with
173 * standard traffic ? Maybe it's time to use sw interrupts and
174 * a probe tasklet !!!
175 */
176static void ath5k_hw_request_rfgain_probe(struct ath5k_hw *ah)
177{
178
179 /* Skip if gain calibration is inactive or
180 * we already handle a probe request */
181 if (ah->ah_gain.g_state != AR5K_RFGAIN_ACTIVE)
182 return;
183
Nick Kossifidis8f655dd2009-03-15 22:20:35 +0200184 /* Send the packet with 2dB below max power as
185 * patent doc suggest */
Nick Kossifidisa0823812009-04-30 15:55:44 -0400186 ath5k_hw_reg_write(ah, AR5K_REG_SM(ah->ah_txpower.txp_ofdm - 4,
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200187 AR5K_PHY_PAPD_PROBE_TXPOWER) |
188 AR5K_PHY_PAPD_PROBE_TX_NEXT, AR5K_PHY_PAPD_PROBE);
189
190 ah->ah_gain.g_state = AR5K_RFGAIN_READ_REQUESTED;
191
192}
193
194/* Calculate gain_F measurement correction
195 * based on the current step for RF5112 rev. 2 */
196static u32 ath5k_hw_rf_gainf_corr(struct ath5k_hw *ah)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200197{
198 u32 mix, step;
199 u32 *rf;
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200200 const struct ath5k_gain_opt *go;
201 const struct ath5k_gain_opt_step *g_step;
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200202 const struct ath5k_rf_reg *rf_regs;
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200203
204 /* Only RF5112 Rev. 2 supports it */
205 if ((ah->ah_radio != AR5K_RF5112) ||
206 (ah->ah_radio_5ghz_revision <= AR5K_SREV_RAD_5112A))
207 return 0;
208
209 go = &rfgain_opt_5112;
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200210 rf_regs = rf_regs_5112a;
211 ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5112a);
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200212
213 g_step = &go->go_step[ah->ah_gain.g_step_idx];
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200214
215 if (ah->ah_rf_banks == NULL)
216 return 0;
217
218 rf = ah->ah_rf_banks;
219 ah->ah_gain.g_f_corr = 0;
220
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200221 /* No VGA (Variable Gain Amplifier) override, skip */
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200222 if (ath5k_hw_rfb_op(ah, rf_regs, 0, AR5K_RF_MIXVGA_OVR, false) != 1)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200223 return 0;
224
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200225 /* Mix gain stepping */
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200226 step = ath5k_hw_rfb_op(ah, rf_regs, 0, AR5K_RF_MIXGAIN_STEP, false);
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200227
228 /* Mix gain override */
229 mix = g_step->gos_param[0];
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200230
231 switch (mix) {
232 case 3:
233 ah->ah_gain.g_f_corr = step * 2;
234 break;
235 case 2:
236 ah->ah_gain.g_f_corr = (step - 5) * 2;
237 break;
238 case 1:
239 ah->ah_gain.g_f_corr = step;
240 break;
241 default:
242 ah->ah_gain.g_f_corr = 0;
243 break;
244 }
245
246 return ah->ah_gain.g_f_corr;
247}
248
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200249/* Check if current gain_F measurement is in the range of our
250 * power detector windows. If we get a measurement outside range
251 * we know it's not accurate (detectors can't measure anything outside
252 * their detection window) so we must ignore it */
253static bool ath5k_hw_rf_check_gainf_readback(struct ath5k_hw *ah)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200254{
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200255 const struct ath5k_rf_reg *rf_regs;
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200256 u32 step, mix_ovr, level[4];
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200257 u32 *rf;
258
259 if (ah->ah_rf_banks == NULL)
260 return false;
261
262 rf = ah->ah_rf_banks;
263
264 if (ah->ah_radio == AR5K_RF5111) {
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200265
266 rf_regs = rf_regs_5111;
267 ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5111);
268
269 step = ath5k_hw_rfb_op(ah, rf_regs, 0, AR5K_RF_RFGAIN_STEP,
270 false);
271
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200272 level[0] = 0;
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200273 level[1] = (step == 63) ? 50 : step + 4;
274 level[2] = (step != 63) ? 64 : level[0];
275 level[3] = level[2] + 50 ;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200276
277 ah->ah_gain.g_high = level[3] -
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200278 (step == 63 ? AR5K_GAIN_DYN_ADJUST_HI_MARGIN : -5);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200279 ah->ah_gain.g_low = level[0] +
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200280 (step == 63 ? AR5K_GAIN_DYN_ADJUST_LO_MARGIN : 0);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200281 } else {
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200282
283 rf_regs = rf_regs_5112;
284 ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5112);
285
286 mix_ovr = ath5k_hw_rfb_op(ah, rf_regs, 0, AR5K_RF_MIXVGA_OVR,
287 false);
288
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200289 level[0] = level[2] = 0;
290
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200291 if (mix_ovr == 1) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200292 level[1] = level[3] = 83;
293 } else {
294 level[1] = level[3] = 107;
295 ah->ah_gain.g_high = 55;
296 }
297 }
298
299 return (ah->ah_gain.g_current >= level[0] &&
300 ah->ah_gain.g_current <= level[1]) ||
301 (ah->ah_gain.g_current >= level[2] &&
302 ah->ah_gain.g_current <= level[3]);
303}
304
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200305/* Perform gain_F adjustment by choosing the right set
306 * of parameters from rf gain optimization ladder */
307static s8 ath5k_hw_rf_gainf_adjust(struct ath5k_hw *ah)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200308{
309 const struct ath5k_gain_opt *go;
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200310 const struct ath5k_gain_opt_step *g_step;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200311 int ret = 0;
312
313 switch (ah->ah_radio) {
314 case AR5K_RF5111:
315 go = &rfgain_opt_5111;
316 break;
317 case AR5K_RF5112:
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200318 go = &rfgain_opt_5112;
319 break;
320 default:
321 return 0;
322 }
323
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200324 g_step = &go->go_step[ah->ah_gain.g_step_idx];
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200325
326 if (ah->ah_gain.g_current >= ah->ah_gain.g_high) {
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200327
328 /* Reached maximum */
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200329 if (ah->ah_gain.g_step_idx == 0)
330 return -1;
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200331
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200332 for (ah->ah_gain.g_target = ah->ah_gain.g_current;
333 ah->ah_gain.g_target >= ah->ah_gain.g_high &&
334 ah->ah_gain.g_step_idx > 0;
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200335 g_step = &go->go_step[ah->ah_gain.g_step_idx])
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200336 ah->ah_gain.g_target -= 2 *
337 (go->go_step[--(ah->ah_gain.g_step_idx)].gos_gain -
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200338 g_step->gos_gain);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200339
340 ret = 1;
341 goto done;
342 }
343
344 if (ah->ah_gain.g_current <= ah->ah_gain.g_low) {
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200345
346 /* Reached minimum */
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200347 if (ah->ah_gain.g_step_idx == (go->go_steps_count - 1))
348 return -2;
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200349
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200350 for (ah->ah_gain.g_target = ah->ah_gain.g_current;
351 ah->ah_gain.g_target <= ah->ah_gain.g_low &&
352 ah->ah_gain.g_step_idx < go->go_steps_count-1;
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200353 g_step = &go->go_step[ah->ah_gain.g_step_idx])
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200354 ah->ah_gain.g_target -= 2 *
355 (go->go_step[++ah->ah_gain.g_step_idx].gos_gain -
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200356 g_step->gos_gain);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200357
358 ret = 2;
359 goto done;
360 }
361
362done:
363 ATH5K_DBG(ah->ah_sc, ATH5K_DEBUG_CALIBRATE,
364 "ret %d, gain step %u, current gain %u, target gain %u\n",
365 ret, ah->ah_gain.g_step_idx, ah->ah_gain.g_current,
366 ah->ah_gain.g_target);
367
368 return ret;
369}
370
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200371/* Main callback for thermal rf gain calibration engine
372 * Check for a new gain reading and schedule an adjustment
373 * if needed.
374 *
375 * TODO: Use sw interrupt to schedule reset if gain_F needs
376 * adjustment */
377enum ath5k_rfgain ath5k_hw_gainf_calibrate(struct ath5k_hw *ah)
378{
379 u32 data, type;
380 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
381
382 ATH5K_TRACE(ah->ah_sc);
383
384 if (ah->ah_rf_banks == NULL ||
385 ah->ah_gain.g_state == AR5K_RFGAIN_INACTIVE)
386 return AR5K_RFGAIN_INACTIVE;
387
388 /* No check requested, either engine is inactive
389 * or an adjustment is already requested */
390 if (ah->ah_gain.g_state != AR5K_RFGAIN_READ_REQUESTED)
391 goto done;
392
393 /* Read the PAPD (Peak to Average Power Detector)
394 * register */
395 data = ath5k_hw_reg_read(ah, AR5K_PHY_PAPD_PROBE);
396
397 /* No probe is scheduled, read gain_F measurement */
398 if (!(data & AR5K_PHY_PAPD_PROBE_TX_NEXT)) {
399 ah->ah_gain.g_current = data >> AR5K_PHY_PAPD_PROBE_GAINF_S;
400 type = AR5K_REG_MS(data, AR5K_PHY_PAPD_PROBE_TYPE);
401
402 /* If tx packet is CCK correct the gain_F measurement
403 * by cck ofdm gain delta */
404 if (type == AR5K_PHY_PAPD_PROBE_TYPE_CCK) {
405 if (ah->ah_radio_5ghz_revision >= AR5K_SREV_RAD_5112A)
406 ah->ah_gain.g_current +=
407 ee->ee_cck_ofdm_gain_delta;
408 else
409 ah->ah_gain.g_current +=
410 AR5K_GAIN_CCK_PROBE_CORR;
411 }
412
413 /* Further correct gain_F measurement for
414 * RF5112A radios */
415 if (ah->ah_radio_5ghz_revision >= AR5K_SREV_RAD_5112A) {
416 ath5k_hw_rf_gainf_corr(ah);
417 ah->ah_gain.g_current =
418 ah->ah_gain.g_current >= ah->ah_gain.g_f_corr ?
419 (ah->ah_gain.g_current-ah->ah_gain.g_f_corr) :
420 0;
421 }
422
423 /* Check if measurement is ok and if we need
424 * to adjust gain, schedule a gain adjustment,
425 * else switch back to the acive state */
426 if (ath5k_hw_rf_check_gainf_readback(ah) &&
427 AR5K_GAIN_CHECK_ADJUST(&ah->ah_gain) &&
428 ath5k_hw_rf_gainf_adjust(ah)) {
429 ah->ah_gain.g_state = AR5K_RFGAIN_NEED_CHANGE;
430 } else {
431 ah->ah_gain.g_state = AR5K_RFGAIN_ACTIVE;
432 }
433 }
434
435done:
436 return ah->ah_gain.g_state;
437}
438
439/* Write initial rf gain table to set the RF sensitivity
440 * this one works on all RF chips and has nothing to do
441 * with gain_F calibration */
442int ath5k_hw_rfgain_init(struct ath5k_hw *ah, unsigned int freq)
443{
444 const struct ath5k_ini_rfgain *ath5k_rfg;
445 unsigned int i, size;
446
447 switch (ah->ah_radio) {
448 case AR5K_RF5111:
449 ath5k_rfg = rfgain_5111;
450 size = ARRAY_SIZE(rfgain_5111);
451 break;
452 case AR5K_RF5112:
453 ath5k_rfg = rfgain_5112;
454 size = ARRAY_SIZE(rfgain_5112);
455 break;
456 case AR5K_RF2413:
457 ath5k_rfg = rfgain_2413;
458 size = ARRAY_SIZE(rfgain_2413);
459 break;
460 case AR5K_RF2316:
461 ath5k_rfg = rfgain_2316;
462 size = ARRAY_SIZE(rfgain_2316);
463 break;
464 case AR5K_RF5413:
465 ath5k_rfg = rfgain_5413;
466 size = ARRAY_SIZE(rfgain_5413);
467 break;
468 case AR5K_RF2317:
469 case AR5K_RF2425:
470 ath5k_rfg = rfgain_2425;
471 size = ARRAY_SIZE(rfgain_2425);
472 break;
473 default:
474 return -EINVAL;
475 }
476
477 switch (freq) {
478 case AR5K_INI_RFGAIN_2GHZ:
479 case AR5K_INI_RFGAIN_5GHZ:
480 break;
481 default:
482 return -EINVAL;
483 }
484
485 for (i = 0; i < size; i++) {
486 AR5K_REG_WAIT(i);
487 ath5k_hw_reg_write(ah, ath5k_rfg[i].rfg_value[freq],
488 (u32)ath5k_rfg[i].rfg_register);
489 }
490
491 return 0;
492}
493
494
495
496/********************\
497* RF Registers setup *
498\********************/
499
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200500
501/*
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200502 * Setup RF registers by writing rf buffer on hw
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200503 */
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200504int ath5k_hw_rfregs_init(struct ath5k_hw *ah, struct ieee80211_channel *channel,
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200505 unsigned int mode)
506{
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200507 const struct ath5k_rf_reg *rf_regs;
508 const struct ath5k_ini_rfbuffer *ini_rfb;
509 const struct ath5k_gain_opt *go = NULL;
510 const struct ath5k_gain_opt_step *g_step;
511 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
512 u8 ee_mode = 0;
513 u32 *rfb;
514 int i, obdb = -1, bank = -1;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200515
516 switch (ah->ah_radio) {
517 case AR5K_RF5111:
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200518 rf_regs = rf_regs_5111;
519 ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5111);
520 ini_rfb = rfb_5111;
521 ah->ah_rf_banks_size = ARRAY_SIZE(rfb_5111);
522 go = &rfgain_opt_5111;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200523 break;
524 case AR5K_RF5112:
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200525 if (ah->ah_radio_5ghz_revision >= AR5K_SREV_RAD_5112A) {
526 rf_regs = rf_regs_5112a;
527 ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5112a);
528 ini_rfb = rfb_5112a;
529 ah->ah_rf_banks_size = ARRAY_SIZE(rfb_5112a);
530 } else {
531 rf_regs = rf_regs_5112;
532 ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5112);
533 ini_rfb = rfb_5112;
534 ah->ah_rf_banks_size = ARRAY_SIZE(rfb_5112);
535 }
536 go = &rfgain_opt_5112;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200537 break;
Nick Kossifidisf714dd62008-02-28 14:43:51 -0500538 case AR5K_RF2413:
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200539 rf_regs = rf_regs_2413;
540 ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_2413);
541 ini_rfb = rfb_2413;
542 ah->ah_rf_banks_size = ARRAY_SIZE(rfb_2413);
543 break;
544 case AR5K_RF2316:
545 rf_regs = rf_regs_2316;
546 ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_2316);
547 ini_rfb = rfb_2316;
548 ah->ah_rf_banks_size = ARRAY_SIZE(rfb_2316);
549 break;
550 case AR5K_RF5413:
551 rf_regs = rf_regs_5413;
552 ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5413);
553 ini_rfb = rfb_5413;
554 ah->ah_rf_banks_size = ARRAY_SIZE(rfb_5413);
555 break;
556 case AR5K_RF2317:
557 rf_regs = rf_regs_2425;
558 ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_2425);
559 ini_rfb = rfb_2317;
560 ah->ah_rf_banks_size = ARRAY_SIZE(rfb_2317);
Nick Kossifidisf714dd62008-02-28 14:43:51 -0500561 break;
Nick Kossifidis136bfc72008-04-16 18:42:48 +0300562 case AR5K_RF2425:
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200563 rf_regs = rf_regs_2425;
564 ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_2425);
565 if (ah->ah_mac_srev < AR5K_SREV_AR2417) {
566 ini_rfb = rfb_2425;
567 ah->ah_rf_banks_size = ARRAY_SIZE(rfb_2425);
568 } else {
569 ini_rfb = rfb_2417;
570 ah->ah_rf_banks_size = ARRAY_SIZE(rfb_2417);
571 }
Nick Kossifidis136bfc72008-04-16 18:42:48 +0300572 break;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200573 default:
574 return -EINVAL;
575 }
576
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200577 /* If it's the first time we set rf buffer, allocate
578 * ah->ah_rf_banks based on ah->ah_rf_banks_size
579 * we set above */
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200580 if (ah->ah_rf_banks == NULL) {
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200581 ah->ah_rf_banks = kmalloc(sizeof(u32) * ah->ah_rf_banks_size,
582 GFP_KERNEL);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200583 if (ah->ah_rf_banks == NULL) {
584 ATH5K_ERR(ah->ah_sc, "out of memory\n");
585 return -ENOMEM;
586 }
587 }
588
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200589 /* Copy values to modify them */
590 rfb = ah->ah_rf_banks;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200591
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200592 for (i = 0; i < ah->ah_rf_banks_size; i++) {
593 if (ini_rfb[i].rfb_bank >= AR5K_MAX_RF_BANKS) {
594 ATH5K_ERR(ah->ah_sc, "invalid bank\n");
595 return -EINVAL;
596 }
597
598 /* Bank changed, write down the offset */
599 if (bank != ini_rfb[i].rfb_bank) {
600 bank = ini_rfb[i].rfb_bank;
601 ah->ah_offset[bank] = i;
602 }
603
604 rfb[i] = ini_rfb[i].rfb_mode_data[mode];
605 }
606
607 /* Set Output and Driver bias current (OB/DB) */
608 if (channel->hw_value & CHANNEL_2GHZ) {
609
610 if (channel->hw_value & CHANNEL_CCK)
611 ee_mode = AR5K_EEPROM_MODE_11B;
612 else
613 ee_mode = AR5K_EEPROM_MODE_11G;
614
615 /* For RF511X/RF211X combination we
616 * use b_OB and b_DB parameters stored
617 * in eeprom on ee->ee_ob[ee_mode][0]
618 *
619 * For all other chips we use OB/DB for 2Ghz
620 * stored in the b/g modal section just like
621 * 802.11a on ee->ee_ob[ee_mode][1] */
622 if ((ah->ah_radio == AR5K_RF5111) ||
623 (ah->ah_radio == AR5K_RF5112))
624 obdb = 0;
625 else
626 obdb = 1;
627
628 ath5k_hw_rfb_op(ah, rf_regs, ee->ee_ob[ee_mode][obdb],
629 AR5K_RF_OB_2GHZ, true);
630
631 ath5k_hw_rfb_op(ah, rf_regs, ee->ee_db[ee_mode][obdb],
632 AR5K_RF_DB_2GHZ, true);
633
634 /* RF5111 always needs OB/DB for 5GHz, even if we use 2GHz */
635 } else if ((channel->hw_value & CHANNEL_5GHZ) ||
636 (ah->ah_radio == AR5K_RF5111)) {
637
638 /* For 11a, Turbo and XR we need to choose
639 * OB/DB based on frequency range */
640 ee_mode = AR5K_EEPROM_MODE_11A;
641 obdb = channel->center_freq >= 5725 ? 3 :
642 (channel->center_freq >= 5500 ? 2 :
643 (channel->center_freq >= 5260 ? 1 :
644 (channel->center_freq > 4000 ? 0 : -1)));
645
646 if (obdb < 0)
647 return -EINVAL;
648
649 ath5k_hw_rfb_op(ah, rf_regs, ee->ee_ob[ee_mode][obdb],
650 AR5K_RF_OB_5GHZ, true);
651
652 ath5k_hw_rfb_op(ah, rf_regs, ee->ee_db[ee_mode][obdb],
653 AR5K_RF_DB_5GHZ, true);
654 }
655
656 g_step = &go->go_step[ah->ah_gain.g_step_idx];
657
658 /* Bank Modifications (chip-specific) */
659 if (ah->ah_radio == AR5K_RF5111) {
660
661 /* Set gain_F settings according to current step */
662 if (channel->hw_value & CHANNEL_OFDM) {
663
664 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_FRAME_CTL,
665 AR5K_PHY_FRAME_CTL_TX_CLIP,
666 g_step->gos_param[0]);
667
668 ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[1],
669 AR5K_RF_PWD_90, true);
670
671 ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[2],
672 AR5K_RF_PWD_84, true);
673
674 ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[3],
675 AR5K_RF_RFGAIN_SEL, true);
676
677 /* We programmed gain_F parameters, switch back
678 * to active state */
679 ah->ah_gain.g_state = AR5K_RFGAIN_ACTIVE;
680
681 }
682
683 /* Bank 6/7 setup */
684
685 ath5k_hw_rfb_op(ah, rf_regs, !ee->ee_xpd[ee_mode],
686 AR5K_RF_PWD_XPD, true);
687
688 ath5k_hw_rfb_op(ah, rf_regs, ee->ee_x_gain[ee_mode],
689 AR5K_RF_XPD_GAIN, true);
690
691 ath5k_hw_rfb_op(ah, rf_regs, ee->ee_i_gain[ee_mode],
692 AR5K_RF_GAIN_I, true);
693
694 ath5k_hw_rfb_op(ah, rf_regs, ee->ee_xpd[ee_mode],
695 AR5K_RF_PLO_SEL, true);
696
697 /* TODO: Half/quarter channel support */
698 }
699
700 if (ah->ah_radio == AR5K_RF5112) {
701
702 /* Set gain_F settings according to current step */
703 if (channel->hw_value & CHANNEL_OFDM) {
704
705 ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[0],
706 AR5K_RF_MIXGAIN_OVR, true);
707
708 ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[1],
709 AR5K_RF_PWD_138, true);
710
711 ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[2],
712 AR5K_RF_PWD_137, true);
713
714 ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[3],
715 AR5K_RF_PWD_136, true);
716
717 ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[4],
718 AR5K_RF_PWD_132, true);
719
720 ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[5],
721 AR5K_RF_PWD_131, true);
722
723 ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[6],
724 AR5K_RF_PWD_130, true);
725
726 /* We programmed gain_F parameters, switch back
727 * to active state */
728 ah->ah_gain.g_state = AR5K_RFGAIN_ACTIVE;
729 }
730
731 /* Bank 6/7 setup */
732
733 ath5k_hw_rfb_op(ah, rf_regs, ee->ee_xpd[ee_mode],
734 AR5K_RF_XPD_SEL, true);
735
736 if (ah->ah_radio_5ghz_revision < AR5K_SREV_RAD_5112A) {
737 /* Rev. 1 supports only one xpd */
738 ath5k_hw_rfb_op(ah, rf_regs,
739 ee->ee_x_gain[ee_mode],
740 AR5K_RF_XPD_GAIN, true);
741
742 } else {
743 /* TODO: Set high and low gain bits */
744 ath5k_hw_rfb_op(ah, rf_regs,
745 ee->ee_x_gain[ee_mode],
746 AR5K_RF_PD_GAIN_LO, true);
747 ath5k_hw_rfb_op(ah, rf_regs,
748 ee->ee_x_gain[ee_mode],
749 AR5K_RF_PD_GAIN_HI, true);
750
751 /* Lower synth voltage on Rev 2 */
752 ath5k_hw_rfb_op(ah, rf_regs, 2,
753 AR5K_RF_HIGH_VC_CP, true);
754
755 ath5k_hw_rfb_op(ah, rf_regs, 2,
756 AR5K_RF_MID_VC_CP, true);
757
758 ath5k_hw_rfb_op(ah, rf_regs, 2,
759 AR5K_RF_LOW_VC_CP, true);
760
761 ath5k_hw_rfb_op(ah, rf_regs, 2,
762 AR5K_RF_PUSH_UP, true);
763
764 /* Decrease power consumption on 5213+ BaseBand */
765 if (ah->ah_phy_revision >= AR5K_SREV_PHY_5212A) {
766 ath5k_hw_rfb_op(ah, rf_regs, 1,
767 AR5K_RF_PAD2GND, true);
768
769 ath5k_hw_rfb_op(ah, rf_regs, 1,
770 AR5K_RF_XB2_LVL, true);
771
772 ath5k_hw_rfb_op(ah, rf_regs, 1,
773 AR5K_RF_XB5_LVL, true);
774
775 ath5k_hw_rfb_op(ah, rf_regs, 1,
776 AR5K_RF_PWD_167, true);
777
778 ath5k_hw_rfb_op(ah, rf_regs, 1,
779 AR5K_RF_PWD_166, true);
780 }
781 }
782
783 ath5k_hw_rfb_op(ah, rf_regs, ee->ee_i_gain[ee_mode],
784 AR5K_RF_GAIN_I, true);
785
786 /* TODO: Half/quarter channel support */
787
788 }
789
790 if (ah->ah_radio == AR5K_RF5413 &&
791 channel->hw_value & CHANNEL_2GHZ) {
792
793 ath5k_hw_rfb_op(ah, rf_regs, 1, AR5K_RF_DERBY_CHAN_SEL_MODE,
794 true);
795
796 /* Set optimum value for early revisions (on pci-e chips) */
797 if (ah->ah_mac_srev >= AR5K_SREV_AR5424 &&
798 ah->ah_mac_srev < AR5K_SREV_AR5413)
799 ath5k_hw_rfb_op(ah, rf_regs, ath5k_hw_bitswap(6, 3),
800 AR5K_RF_PWD_ICLOBUF_2G, true);
801
802 }
803
804 /* Write RF banks on hw */
805 for (i = 0; i < ah->ah_rf_banks_size; i++) {
806 AR5K_REG_WAIT(i);
807 ath5k_hw_reg_write(ah, rfb[i], ini_rfb[i].rfb_ctrl_register);
808 }
809
810 return 0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200811}
812
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200813
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200814/**************************\
815 PHY/RF channel functions
816\**************************/
817
818/*
819 * Check if a channel is supported
820 */
821bool ath5k_channel_ok(struct ath5k_hw *ah, u16 freq, unsigned int flags)
822{
823 /* Check if the channel is in our supported range */
824 if (flags & CHANNEL_2GHZ) {
825 if ((freq >= ah->ah_capabilities.cap_range.range_2ghz_min) &&
826 (freq <= ah->ah_capabilities.cap_range.range_2ghz_max))
827 return true;
828 } else if (flags & CHANNEL_5GHZ)
829 if ((freq >= ah->ah_capabilities.cap_range.range_5ghz_min) &&
830 (freq <= ah->ah_capabilities.cap_range.range_5ghz_max))
831 return true;
832
833 return false;
834}
835
836/*
837 * Convertion needed for RF5110
838 */
839static u32 ath5k_hw_rf5110_chan2athchan(struct ieee80211_channel *channel)
840{
841 u32 athchan;
842
843 /*
844 * Convert IEEE channel/MHz to an internal channel value used
845 * by the AR5210 chipset. This has not been verified with
846 * newer chipsets like the AR5212A who have a completely
847 * different RF/PHY part.
848 */
Luis R. Rodriguez400ec452008-02-03 21:51:49 -0500849 athchan = (ath5k_hw_bitswap(
850 (ieee80211_frequency_to_channel(
851 channel->center_freq) - 24) / 2, 5)
852 << 1) | (1 << 6) | 0x1;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200853 return athchan;
854}
855
856/*
857 * Set channel on RF5110
858 */
859static int ath5k_hw_rf5110_channel(struct ath5k_hw *ah,
860 struct ieee80211_channel *channel)
861{
862 u32 data;
863
864 /*
865 * Set the channel and wait
866 */
867 data = ath5k_hw_rf5110_chan2athchan(channel);
868 ath5k_hw_reg_write(ah, data, AR5K_RF_BUFFER);
869 ath5k_hw_reg_write(ah, 0, AR5K_RF_BUFFER_CONTROL_0);
870 mdelay(1);
871
872 return 0;
873}
874
875/*
876 * Convertion needed for 5111
877 */
878static int ath5k_hw_rf5111_chan2athchan(unsigned int ieee,
879 struct ath5k_athchan_2ghz *athchan)
880{
881 int channel;
882
883 /* Cast this value to catch negative channel numbers (>= -19) */
884 channel = (int)ieee;
885
886 /*
887 * Map 2GHz IEEE channel to 5GHz Atheros channel
888 */
889 if (channel <= 13) {
890 athchan->a2_athchan = 115 + channel;
891 athchan->a2_flags = 0x46;
892 } else if (channel == 14) {
893 athchan->a2_athchan = 124;
894 athchan->a2_flags = 0x44;
895 } else if (channel >= 15 && channel <= 26) {
896 athchan->a2_athchan = ((channel - 14) * 4) + 132;
897 athchan->a2_flags = 0x46;
898 } else
899 return -EINVAL;
900
901 return 0;
902}
903
904/*
905 * Set channel on 5111
906 */
907static int ath5k_hw_rf5111_channel(struct ath5k_hw *ah,
908 struct ieee80211_channel *channel)
909{
910 struct ath5k_athchan_2ghz ath5k_channel_2ghz;
Luis R. Rodriguez400ec452008-02-03 21:51:49 -0500911 unsigned int ath5k_channel =
912 ieee80211_frequency_to_channel(channel->center_freq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200913 u32 data0, data1, clock;
914 int ret;
915
916 /*
917 * Set the channel on the RF5111 radio
918 */
919 data0 = data1 = 0;
920
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500921 if (channel->hw_value & CHANNEL_2GHZ) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200922 /* Map 2GHz channel to 5GHz Atheros channel ID */
Luis R. Rodriguez400ec452008-02-03 21:51:49 -0500923 ret = ath5k_hw_rf5111_chan2athchan(
924 ieee80211_frequency_to_channel(channel->center_freq),
925 &ath5k_channel_2ghz);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200926 if (ret)
927 return ret;
928
929 ath5k_channel = ath5k_channel_2ghz.a2_athchan;
930 data0 = ((ath5k_hw_bitswap(ath5k_channel_2ghz.a2_flags, 8) & 0xff)
931 << 5) | (1 << 4);
932 }
933
934 if (ath5k_channel < 145 || !(ath5k_channel & 1)) {
935 clock = 1;
936 data1 = ((ath5k_hw_bitswap(ath5k_channel - 24, 8) & 0xff) << 2) |
937 (clock << 1) | (1 << 10) | 1;
938 } else {
939 clock = 0;
940 data1 = ((ath5k_hw_bitswap((ath5k_channel - 24) / 2, 8) & 0xff)
941 << 2) | (clock << 1) | (1 << 10) | 1;
942 }
943
944 ath5k_hw_reg_write(ah, (data1 & 0xff) | ((data0 & 0xff) << 8),
945 AR5K_RF_BUFFER);
946 ath5k_hw_reg_write(ah, ((data1 >> 8) & 0xff) | (data0 & 0xff00),
947 AR5K_RF_BUFFER_CONTROL_3);
948
949 return 0;
950}
951
952/*
953 * Set channel on 5112 and newer
954 */
955static int ath5k_hw_rf5112_channel(struct ath5k_hw *ah,
956 struct ieee80211_channel *channel)
957{
958 u32 data, data0, data1, data2;
959 u16 c;
960
961 data = data0 = data1 = data2 = 0;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500962 c = channel->center_freq;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200963
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200964 if (c < 4800) {
965 if (!((c - 2224) % 5)) {
966 data0 = ((2 * (c - 704)) - 3040) / 10;
967 data1 = 1;
968 } else if (!((c - 2192) % 5)) {
969 data0 = ((2 * (c - 672)) - 3040) / 10;
970 data1 = 0;
971 } else
972 return -EINVAL;
973
974 data0 = ath5k_hw_bitswap((data0 << 2) & 0xff, 8);
Nick Kossifidiscc6323c2008-07-20 06:44:43 +0300975 } else if ((c - (c % 5)) != 2 || c > 5435) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200976 if (!(c % 20) && c >= 5120) {
977 data0 = ath5k_hw_bitswap(((c - 4800) / 20 << 2), 8);
978 data2 = ath5k_hw_bitswap(3, 2);
979 } else if (!(c % 10)) {
980 data0 = ath5k_hw_bitswap(((c - 4800) / 10 << 1), 8);
981 data2 = ath5k_hw_bitswap(2, 2);
982 } else if (!(c % 5)) {
983 data0 = ath5k_hw_bitswap((c - 4800) / 5, 8);
984 data2 = ath5k_hw_bitswap(1, 2);
985 } else
986 return -EINVAL;
Nick Kossifidiscc6323c2008-07-20 06:44:43 +0300987 } else {
988 data0 = ath5k_hw_bitswap((10 * (c - 2) - 4800) / 25 + 1, 8);
989 data2 = ath5k_hw_bitswap(0, 2);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200990 }
991
992 data = (data0 << 4) | (data1 << 1) | (data2 << 2) | 0x1001;
993
994 ath5k_hw_reg_write(ah, data & 0xff, AR5K_RF_BUFFER);
995 ath5k_hw_reg_write(ah, (data >> 8) & 0x7f, AR5K_RF_BUFFER_CONTROL_5);
996
997 return 0;
998}
999
1000/*
Nick Kossifidiscc6323c2008-07-20 06:44:43 +03001001 * Set the channel on the RF2425
1002 */
1003static int ath5k_hw_rf2425_channel(struct ath5k_hw *ah,
1004 struct ieee80211_channel *channel)
1005{
1006 u32 data, data0, data2;
1007 u16 c;
1008
1009 data = data0 = data2 = 0;
1010 c = channel->center_freq;
1011
1012 if (c < 4800) {
1013 data0 = ath5k_hw_bitswap((c - 2272), 8);
1014 data2 = 0;
1015 /* ? 5GHz ? */
1016 } else if ((c - (c % 5)) != 2 || c > 5435) {
1017 if (!(c % 20) && c < 5120)
1018 data0 = ath5k_hw_bitswap(((c - 4800) / 20 << 2), 8);
1019 else if (!(c % 10))
1020 data0 = ath5k_hw_bitswap(((c - 4800) / 10 << 1), 8);
1021 else if (!(c % 5))
1022 data0 = ath5k_hw_bitswap((c - 4800) / 5, 8);
1023 else
1024 return -EINVAL;
1025 data2 = ath5k_hw_bitswap(1, 2);
1026 } else {
1027 data0 = ath5k_hw_bitswap((10 * (c - 2) - 4800) / 25 + 1, 8);
1028 data2 = ath5k_hw_bitswap(0, 2);
1029 }
1030
1031 data = (data0 << 4) | data2 << 2 | 0x1001;
1032
1033 ath5k_hw_reg_write(ah, data & 0xff, AR5K_RF_BUFFER);
1034 ath5k_hw_reg_write(ah, (data >> 8) & 0x7f, AR5K_RF_BUFFER_CONTROL_5);
1035
1036 return 0;
1037}
1038
1039/*
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001040 * Set a channel on the radio chip
1041 */
1042int ath5k_hw_channel(struct ath5k_hw *ah, struct ieee80211_channel *channel)
1043{
1044 int ret;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001045 /*
Luis R. Rodriguez400ec452008-02-03 21:51:49 -05001046 * Check bounds supported by the PHY (we don't care about regultory
1047 * restrictions at this point). Note: hw_value already has the band
1048 * (CHANNEL_2GHZ, or CHANNEL_5GHZ) so we inform ath5k_channel_ok()
1049 * of the band by that */
1050 if (!ath5k_channel_ok(ah, channel->center_freq, channel->hw_value)) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001051 ATH5K_ERR(ah->ah_sc,
Luis R. Rodriguez400ec452008-02-03 21:51:49 -05001052 "channel frequency (%u MHz) out of supported "
1053 "band range\n",
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05001054 channel->center_freq);
Luis R. Rodriguez400ec452008-02-03 21:51:49 -05001055 return -EINVAL;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001056 }
1057
1058 /*
1059 * Set the channel and wait
1060 */
1061 switch (ah->ah_radio) {
1062 case AR5K_RF5110:
1063 ret = ath5k_hw_rf5110_channel(ah, channel);
1064 break;
1065 case AR5K_RF5111:
1066 ret = ath5k_hw_rf5111_channel(ah, channel);
1067 break;
Nick Kossifidiscc6323c2008-07-20 06:44:43 +03001068 case AR5K_RF2425:
1069 ret = ath5k_hw_rf2425_channel(ah, channel);
1070 break;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001071 default:
1072 ret = ath5k_hw_rf5112_channel(ah, channel);
1073 break;
1074 }
1075
1076 if (ret)
1077 return ret;
1078
Nick Kossifidiscc6323c2008-07-20 06:44:43 +03001079 /* Set JAPAN setting for channel 14 */
1080 if (channel->center_freq == 2484) {
1081 AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_CCKTXCTL,
1082 AR5K_PHY_CCKTXCTL_JAPAN);
1083 } else {
1084 AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_CCKTXCTL,
1085 AR5K_PHY_CCKTXCTL_WORLD);
1086 }
1087
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05001088 ah->ah_current_channel.center_freq = channel->center_freq;
1089 ah->ah_current_channel.hw_value = channel->hw_value;
1090 ah->ah_turbo = channel->hw_value == CHANNEL_T ? true : false;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001091
1092 return 0;
1093}
1094
1095/*****************\
1096 PHY calibration
1097\*****************/
1098
1099/**
1100 * ath5k_hw_noise_floor_calibration - perform PHY noise floor calibration
1101 *
1102 * @ah: struct ath5k_hw pointer we are operating on
1103 * @freq: the channel frequency, just used for error logging
1104 *
1105 * This function performs a noise floor calibration of the PHY and waits for
1106 * it to complete. Then the noise floor value is compared to some maximum
1107 * noise floor we consider valid.
1108 *
1109 * Note that this is different from what the madwifi HAL does: it reads the
1110 * noise floor and afterwards initiates the calibration. Since the noise floor
1111 * calibration can take some time to finish, depending on the current channel
1112 * use, that avoids the occasional timeout warnings we are seeing now.
1113 *
1114 * See the following link for an Atheros patent on noise floor calibration:
1115 * http://patft.uspto.gov/netacgi/nph-Parser?Sect1=PTO1&Sect2=HITOFF&d=PALL \
1116 * &p=1&u=%2Fnetahtml%2FPTO%2Fsrchnum.htm&r=1&f=G&l=50&s1=7245893.PN.&OS=PN/7
1117 *
Nick Kossifidisf860ee22008-07-20 06:47:12 +03001118 * XXX: Since during noise floor calibration antennas are detached according to
1119 * the patent, we should stop tx queues here.
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001120 */
1121int
1122ath5k_hw_noise_floor_calibration(struct ath5k_hw *ah, short freq)
1123{
1124 int ret;
1125 unsigned int i;
1126 s32 noise_floor;
1127
1128 /*
Nick Kossifidisf860ee22008-07-20 06:47:12 +03001129 * Enable noise floor calibration
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001130 */
1131 AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGCCTL,
1132 AR5K_PHY_AGCCTL_NF);
1133
1134 ret = ath5k_hw_register_timeout(ah, AR5K_PHY_AGCCTL,
1135 AR5K_PHY_AGCCTL_NF, 0, false);
1136 if (ret) {
1137 ATH5K_ERR(ah->ah_sc,
1138 "noise floor calibration timeout (%uMHz)\n", freq);
Nick Kossifidisf860ee22008-07-20 06:47:12 +03001139 return -EAGAIN;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001140 }
1141
1142 /* Wait until the noise floor is calibrated and read the value */
1143 for (i = 20; i > 0; i--) {
1144 mdelay(1);
1145 noise_floor = ath5k_hw_reg_read(ah, AR5K_PHY_NF);
1146 noise_floor = AR5K_PHY_NF_RVAL(noise_floor);
1147 if (noise_floor & AR5K_PHY_NF_ACTIVE) {
1148 noise_floor = AR5K_PHY_NF_AVAL(noise_floor);
1149
1150 if (noise_floor <= AR5K_TUNE_NOISE_FLOOR)
1151 break;
1152 }
1153 }
1154
1155 ATH5K_DBG_UNLIMIT(ah->ah_sc, ATH5K_DEBUG_CALIBRATE,
1156 "noise floor %d\n", noise_floor);
1157
1158 if (noise_floor > AR5K_TUNE_NOISE_FLOOR) {
1159 ATH5K_ERR(ah->ah_sc,
1160 "noise floor calibration failed (%uMHz)\n", freq);
Nick Kossifidisf860ee22008-07-20 06:47:12 +03001161 return -EAGAIN;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001162 }
1163
1164 ah->ah_noise_floor = noise_floor;
1165
1166 return 0;
1167}
1168
1169/*
1170 * Perform a PHY calibration on RF5110
1171 * -Fix BPSK/QAM Constellation (I/Q correction)
1172 * -Calculate Noise Floor
1173 */
1174static int ath5k_hw_rf5110_calibrate(struct ath5k_hw *ah,
1175 struct ieee80211_channel *channel)
1176{
1177 u32 phy_sig, phy_agc, phy_sat, beacon;
1178 int ret;
1179
1180 /*
1181 * Disable beacons and RX/TX queues, wait
1182 */
1183 AR5K_REG_ENABLE_BITS(ah, AR5K_DIAG_SW_5210,
1184 AR5K_DIAG_SW_DIS_TX | AR5K_DIAG_SW_DIS_RX_5210);
1185 beacon = ath5k_hw_reg_read(ah, AR5K_BEACON_5210);
1186 ath5k_hw_reg_write(ah, beacon & ~AR5K_BEACON_ENABLE, AR5K_BEACON_5210);
1187
Nick Kossifidis84e463f2008-09-17 03:33:19 +03001188 mdelay(2);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001189
1190 /*
1191 * Set the channel (with AGC turned off)
1192 */
1193 AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGC, AR5K_PHY_AGC_DISABLE);
1194 udelay(10);
1195 ret = ath5k_hw_channel(ah, channel);
1196
1197 /*
1198 * Activate PHY and wait
1199 */
1200 ath5k_hw_reg_write(ah, AR5K_PHY_ACT_ENABLE, AR5K_PHY_ACT);
1201 mdelay(1);
1202
1203 AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_AGC, AR5K_PHY_AGC_DISABLE);
1204
1205 if (ret)
1206 return ret;
1207
1208 /*
1209 * Calibrate the radio chip
1210 */
1211
1212 /* Remember normal state */
1213 phy_sig = ath5k_hw_reg_read(ah, AR5K_PHY_SIG);
1214 phy_agc = ath5k_hw_reg_read(ah, AR5K_PHY_AGCCOARSE);
1215 phy_sat = ath5k_hw_reg_read(ah, AR5K_PHY_ADCSAT);
1216
1217 /* Update radio registers */
1218 ath5k_hw_reg_write(ah, (phy_sig & ~(AR5K_PHY_SIG_FIRPWR)) |
1219 AR5K_REG_SM(-1, AR5K_PHY_SIG_FIRPWR), AR5K_PHY_SIG);
1220
1221 ath5k_hw_reg_write(ah, (phy_agc & ~(AR5K_PHY_AGCCOARSE_HI |
1222 AR5K_PHY_AGCCOARSE_LO)) |
1223 AR5K_REG_SM(-1, AR5K_PHY_AGCCOARSE_HI) |
1224 AR5K_REG_SM(-127, AR5K_PHY_AGCCOARSE_LO), AR5K_PHY_AGCCOARSE);
1225
1226 ath5k_hw_reg_write(ah, (phy_sat & ~(AR5K_PHY_ADCSAT_ICNT |
1227 AR5K_PHY_ADCSAT_THR)) |
1228 AR5K_REG_SM(2, AR5K_PHY_ADCSAT_ICNT) |
1229 AR5K_REG_SM(12, AR5K_PHY_ADCSAT_THR), AR5K_PHY_ADCSAT);
1230
1231 udelay(20);
1232
1233 AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGC, AR5K_PHY_AGC_DISABLE);
1234 udelay(10);
1235 ath5k_hw_reg_write(ah, AR5K_PHY_RFSTG_DISABLE, AR5K_PHY_RFSTG);
1236 AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_AGC, AR5K_PHY_AGC_DISABLE);
1237
1238 mdelay(1);
1239
1240 /*
1241 * Enable calibration and wait until completion
1242 */
1243 AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGCCTL, AR5K_PHY_AGCCTL_CAL);
1244
1245 ret = ath5k_hw_register_timeout(ah, AR5K_PHY_AGCCTL,
1246 AR5K_PHY_AGCCTL_CAL, 0, false);
1247
1248 /* Reset to normal state */
1249 ath5k_hw_reg_write(ah, phy_sig, AR5K_PHY_SIG);
1250 ath5k_hw_reg_write(ah, phy_agc, AR5K_PHY_AGCCOARSE);
1251 ath5k_hw_reg_write(ah, phy_sat, AR5K_PHY_ADCSAT);
1252
1253 if (ret) {
1254 ATH5K_ERR(ah->ah_sc, "calibration timeout (%uMHz)\n",
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05001255 channel->center_freq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001256 return ret;
1257 }
1258
Felix Fietkau8b0162a2008-11-03 11:27:38 +01001259 ath5k_hw_noise_floor_calibration(ah, channel->center_freq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001260
1261 /*
1262 * Re-enable RX/TX and beacons
1263 */
1264 AR5K_REG_DISABLE_BITS(ah, AR5K_DIAG_SW_5210,
1265 AR5K_DIAG_SW_DIS_TX | AR5K_DIAG_SW_DIS_RX_5210);
1266 ath5k_hw_reg_write(ah, beacon, AR5K_BEACON_5210);
1267
1268 return 0;
1269}
1270
1271/*
Nick Kossifidisf860ee22008-07-20 06:47:12 +03001272 * Perform a PHY calibration on RF5111/5112 and newer chips
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001273 */
1274static int ath5k_hw_rf511x_calibrate(struct ath5k_hw *ah,
1275 struct ieee80211_channel *channel)
1276{
1277 u32 i_pwr, q_pwr;
1278 s32 iq_corr, i_coff, i_coffd, q_coff, q_coffd;
Nick Kossifidisf860ee22008-07-20 06:47:12 +03001279 int i;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001280 ATH5K_TRACE(ah->ah_sc);
1281
Joe Perchese9010e22008-03-07 14:21:16 -08001282 if (!ah->ah_calibration ||
Nick Kossifidisf860ee22008-07-20 06:47:12 +03001283 ath5k_hw_reg_read(ah, AR5K_PHY_IQ) & AR5K_PHY_IQ_RUN)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001284 goto done;
1285
Nick Kossifidisf860ee22008-07-20 06:47:12 +03001286 /* Calibration has finished, get the results and re-run */
1287 for (i = 0; i <= 10; i++) {
1288 iq_corr = ath5k_hw_reg_read(ah, AR5K_PHY_IQRES_CAL_CORR);
1289 i_pwr = ath5k_hw_reg_read(ah, AR5K_PHY_IQRES_CAL_PWR_I);
1290 q_pwr = ath5k_hw_reg_read(ah, AR5K_PHY_IQRES_CAL_PWR_Q);
1291 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001292
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001293 i_coffd = ((i_pwr >> 1) + (q_pwr >> 1)) >> 7;
Nick Kossifidisf860ee22008-07-20 06:47:12 +03001294 q_coffd = q_pwr >> 7;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001295
Nick Kossifidisf860ee22008-07-20 06:47:12 +03001296 /* No correction */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001297 if (i_coffd == 0 || q_coffd == 0)
1298 goto done;
1299
1300 i_coff = ((-iq_corr) / i_coffd) & 0x3f;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001301
Nick Kossifidisf860ee22008-07-20 06:47:12 +03001302 /* Boundary check */
1303 if (i_coff > 31)
1304 i_coff = 31;
1305 if (i_coff < -32)
1306 i_coff = -32;
1307
1308 q_coff = (((s32)i_pwr / q_coffd) - 128) & 0x1f;
1309
1310 /* Boundary check */
1311 if (q_coff > 15)
1312 q_coff = 15;
1313 if (q_coff < -16)
1314 q_coff = -16;
1315
1316 /* Commit new I/Q value */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001317 AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ, AR5K_PHY_IQ_CORR_ENABLE |
1318 ((u32)q_coff) | ((u32)i_coff << AR5K_PHY_IQ_CORR_Q_I_COFF_S));
1319
Nick Kossifidisf860ee22008-07-20 06:47:12 +03001320 /* Re-enable calibration -if we don't we'll commit
1321 * the same values again and again */
1322 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_IQ,
1323 AR5K_PHY_IQ_CAL_NUM_LOG_MAX, 15);
1324 AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ, AR5K_PHY_IQ_RUN);
1325
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001326done:
Nick Kossifidisf860ee22008-07-20 06:47:12 +03001327
1328 /* TODO: Separate noise floor calibration from I/Q calibration
1329 * since noise floor calibration interrupts rx path while I/Q
1330 * calibration doesn't. We don't need to run noise floor calibration
1331 * as often as I/Q calibration.*/
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05001332 ath5k_hw_noise_floor_calibration(ah, channel->center_freq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001333
Nick Kossifidis6f3b4142009-02-09 06:03:41 +02001334 /* Initiate a gain_F calibration */
1335 ath5k_hw_request_rfgain_probe(ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001336
1337 return 0;
1338}
1339
1340/*
1341 * Perform a PHY calibration
1342 */
1343int ath5k_hw_phy_calibrate(struct ath5k_hw *ah,
1344 struct ieee80211_channel *channel)
1345{
1346 int ret;
1347
1348 if (ah->ah_radio == AR5K_RF5110)
1349 ret = ath5k_hw_rf5110_calibrate(ah, channel);
1350 else
1351 ret = ath5k_hw_rf511x_calibrate(ah, channel);
1352
1353 return ret;
1354}
1355
1356int ath5k_hw_phy_disable(struct ath5k_hw *ah)
1357{
1358 ATH5K_TRACE(ah->ah_sc);
1359 /*Just a try M.F.*/
1360 ath5k_hw_reg_write(ah, AR5K_PHY_ACT_DISABLE, AR5K_PHY_ACT);
1361
1362 return 0;
1363}
1364
1365/********************\
1366 Misc PHY functions
1367\********************/
1368
1369/*
1370 * Get the PHY Chip revision
1371 */
1372u16 ath5k_hw_radio_revision(struct ath5k_hw *ah, unsigned int chan)
1373{
1374 unsigned int i;
1375 u32 srev;
1376 u16 ret;
1377
1378 ATH5K_TRACE(ah->ah_sc);
1379
1380 /*
1381 * Set the radio chip access register
1382 */
1383 switch (chan) {
1384 case CHANNEL_2GHZ:
1385 ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_2GHZ, AR5K_PHY(0));
1386 break;
1387 case CHANNEL_5GHZ:
1388 ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ, AR5K_PHY(0));
1389 break;
1390 default:
1391 return 0;
1392 }
1393
1394 mdelay(2);
1395
1396 /* ...wait until PHY is ready and read the selected radio revision */
1397 ath5k_hw_reg_write(ah, 0x00001c16, AR5K_PHY(0x34));
1398
1399 for (i = 0; i < 8; i++)
1400 ath5k_hw_reg_write(ah, 0x00010000, AR5K_PHY(0x20));
1401
1402 if (ah->ah_version == AR5K_AR5210) {
1403 srev = ath5k_hw_reg_read(ah, AR5K_PHY(256) >> 28) & 0xf;
1404 ret = (u16)ath5k_hw_bitswap(srev, 4) + 1;
1405 } else {
1406 srev = (ath5k_hw_reg_read(ah, AR5K_PHY(0x100)) >> 24) & 0xff;
1407 ret = (u16)ath5k_hw_bitswap(((srev & 0xf0) >> 4) |
1408 ((srev & 0x0f) << 4), 8);
1409 }
1410
1411 /* Reset to the 5GHz mode */
1412 ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ, AR5K_PHY(0));
1413
1414 return ret;
1415}
1416
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001417/*****************\
1418* Antenna control *
1419\*****************/
1420
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001421void /*TODO:Boundary check*/
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001422ath5k_hw_set_def_antenna(struct ath5k_hw *ah, u8 ant)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001423{
1424 ATH5K_TRACE(ah->ah_sc);
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001425
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001426 if (ah->ah_version != AR5K_AR5210)
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001427 ath5k_hw_reg_write(ah, ant & 0x7, AR5K_DEFAULT_ANTENNA);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001428}
1429
1430unsigned int ath5k_hw_get_def_antenna(struct ath5k_hw *ah)
1431{
1432 ATH5K_TRACE(ah->ah_sc);
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001433
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001434 if (ah->ah_version != AR5K_AR5210)
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001435 return ath5k_hw_reg_read(ah, AR5K_DEFAULT_ANTENNA) & 0x7;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001436
1437 return false; /*XXX: What do we return for 5210 ?*/
1438}
1439
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001440/*
1441 * Enable/disable fast rx antenna diversity
1442 */
1443static void
1444ath5k_hw_set_fast_div(struct ath5k_hw *ah, u8 ee_mode, bool enable)
1445{
1446 switch (ee_mode) {
1447 case AR5K_EEPROM_MODE_11G:
1448 /* XXX: This is set to
1449 * disabled on initvals !!! */
1450 case AR5K_EEPROM_MODE_11A:
1451 if (enable)
1452 AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_AGCCTL,
1453 AR5K_PHY_AGCCTL_OFDM_DIV_DIS);
1454 else
1455 AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGCCTL,
1456 AR5K_PHY_AGCCTL_OFDM_DIV_DIS);
1457 break;
1458 case AR5K_EEPROM_MODE_11B:
1459 AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGCCTL,
1460 AR5K_PHY_AGCCTL_OFDM_DIV_DIS);
1461 break;
1462 default:
1463 return;
1464 }
1465
1466 if (enable) {
1467 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_RESTART,
1468 AR5K_PHY_RESTART_DIV_GC, 0xc);
1469
1470 AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_FAST_ANT_DIV,
1471 AR5K_PHY_FAST_ANT_DIV_EN);
1472 } else {
1473 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_RESTART,
1474 AR5K_PHY_RESTART_DIV_GC, 0x8);
1475
1476 AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_FAST_ANT_DIV,
1477 AR5K_PHY_FAST_ANT_DIV_EN);
1478 }
1479}
1480
1481/*
1482 * Set antenna operating mode
1483 */
1484void
1485ath5k_hw_set_antenna_mode(struct ath5k_hw *ah, u8 ant_mode)
1486{
1487 struct ieee80211_channel *channel = &ah->ah_current_channel;
1488 bool use_def_for_tx, update_def_on_tx, use_def_for_rts, fast_div;
1489 bool use_def_for_sg;
1490 u8 def_ant, tx_ant, ee_mode;
1491 u32 sta_id1 = 0;
1492
1493 def_ant = ah->ah_def_ant;
1494
1495 ATH5K_TRACE(ah->ah_sc);
1496
1497 switch (channel->hw_value & CHANNEL_MODES) {
1498 case CHANNEL_A:
1499 case CHANNEL_T:
1500 case CHANNEL_XR:
1501 ee_mode = AR5K_EEPROM_MODE_11A;
1502 break;
1503 case CHANNEL_G:
1504 case CHANNEL_TG:
1505 ee_mode = AR5K_EEPROM_MODE_11G;
1506 break;
1507 case CHANNEL_B:
1508 ee_mode = AR5K_EEPROM_MODE_11B;
1509 break;
1510 default:
1511 ATH5K_ERR(ah->ah_sc,
1512 "invalid channel: %d\n", channel->center_freq);
1513 return;
1514 }
1515
1516 switch (ant_mode) {
1517 case AR5K_ANTMODE_DEFAULT:
1518 tx_ant = 0;
1519 use_def_for_tx = false;
1520 update_def_on_tx = false;
1521 use_def_for_rts = false;
1522 use_def_for_sg = false;
1523 fast_div = true;
1524 break;
1525 case AR5K_ANTMODE_FIXED_A:
1526 def_ant = 1;
1527 tx_ant = 0;
1528 use_def_for_tx = true;
1529 update_def_on_tx = false;
1530 use_def_for_rts = true;
1531 use_def_for_sg = true;
1532 fast_div = false;
1533 break;
1534 case AR5K_ANTMODE_FIXED_B:
1535 def_ant = 2;
1536 tx_ant = 0;
1537 use_def_for_tx = true;
1538 update_def_on_tx = false;
1539 use_def_for_rts = true;
1540 use_def_for_sg = true;
1541 fast_div = false;
1542 break;
1543 case AR5K_ANTMODE_SINGLE_AP:
1544 def_ant = 1; /* updated on tx */
1545 tx_ant = 0;
1546 use_def_for_tx = true;
1547 update_def_on_tx = true;
1548 use_def_for_rts = true;
1549 use_def_for_sg = true;
1550 fast_div = true;
1551 break;
1552 case AR5K_ANTMODE_SECTOR_AP:
1553 tx_ant = 1; /* variable */
1554 use_def_for_tx = false;
1555 update_def_on_tx = false;
1556 use_def_for_rts = true;
1557 use_def_for_sg = false;
1558 fast_div = false;
1559 break;
1560 case AR5K_ANTMODE_SECTOR_STA:
1561 tx_ant = 1; /* variable */
1562 use_def_for_tx = true;
1563 update_def_on_tx = false;
1564 use_def_for_rts = true;
1565 use_def_for_sg = false;
1566 fast_div = true;
1567 break;
1568 case AR5K_ANTMODE_DEBUG:
1569 def_ant = 1;
1570 tx_ant = 2;
1571 use_def_for_tx = false;
1572 update_def_on_tx = false;
1573 use_def_for_rts = false;
1574 use_def_for_sg = false;
1575 fast_div = false;
1576 break;
1577 default:
1578 return;
1579 }
1580
1581 ah->ah_tx_ant = tx_ant;
1582 ah->ah_ant_mode = ant_mode;
1583
1584 sta_id1 |= use_def_for_tx ? AR5K_STA_ID1_DEFAULT_ANTENNA : 0;
1585 sta_id1 |= update_def_on_tx ? AR5K_STA_ID1_DESC_ANTENNA : 0;
1586 sta_id1 |= use_def_for_rts ? AR5K_STA_ID1_RTS_DEF_ANTENNA : 0;
1587 sta_id1 |= use_def_for_sg ? AR5K_STA_ID1_SELFGEN_DEF_ANT : 0;
1588
1589 AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_ANTENNA_SETTINGS);
1590
1591 if (sta_id1)
1592 AR5K_REG_ENABLE_BITS(ah, AR5K_STA_ID1, sta_id1);
1593
1594 /* Note: set diversity before default antenna
1595 * because it won't work correctly */
1596 ath5k_hw_set_fast_div(ah, ee_mode, fast_div);
1597 ath5k_hw_set_def_antenna(ah, def_ant);
1598}
1599
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001600
1601/****************\
1602* TX power setup *
1603\****************/
1604
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001605/*
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001606 * Helper functions
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001607 */
1608
1609/*
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001610 * Do linear interpolation between two given (x, y) points
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001611 */
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001612static s16
1613ath5k_get_interpolated_value(s16 target, s16 x_left, s16 x_right,
1614 s16 y_left, s16 y_right)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001615{
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001616 s16 ratio, result;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001617
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001618 /* Avoid divide by zero and skip interpolation
1619 * if we have the same point */
1620 if ((x_left == x_right) || (y_left == y_right))
1621 return y_left;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001622
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001623 /*
1624 * Since we use ints and not fps, we need to scale up in
1625 * order to get a sane ratio value (or else we 'll eg. get
1626 * always 1 instead of 1.25, 1.75 etc). We scale up by 100
1627 * to have some accuracy both for 0.5 and 0.25 steps.
1628 */
1629 ratio = ((100 * y_right - 100 * y_left)/(x_right - x_left));
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001630
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001631 /* Now scale down to be in range */
1632 result = y_left + (ratio * (target - x_left) / 100);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001633
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001634 return result;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001635}
1636
1637/*
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001638 * Find vertical boundary (min pwr) for the linear PCDAC curve.
1639 *
1640 * Since we have the top of the curve and we draw the line below
1641 * until we reach 1 (1 pcdac step) we need to know which point
1642 * (x value) that is so that we don't go below y axis and have negative
1643 * pcdac values when creating the curve, or fill the table with zeroes.
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001644 */
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001645static s16
1646ath5k_get_linear_pcdac_min(const u8 *stepL, const u8 *stepR,
1647 const s16 *pwrL, const s16 *pwrR)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001648{
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001649 s8 tmp;
1650 s16 min_pwrL, min_pwrR;
Fabio Rossi64cdb0e2009-04-01 20:37:50 +02001651 s16 pwr_i;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001652
Fabio Rossi64cdb0e2009-04-01 20:37:50 +02001653 if (pwrL[0] == pwrL[1])
1654 min_pwrL = pwrL[0];
1655 else {
1656 pwr_i = pwrL[0];
1657 do {
1658 pwr_i--;
1659 tmp = (s8) ath5k_get_interpolated_value(pwr_i,
1660 pwrL[0], pwrL[1],
1661 stepL[0], stepL[1]);
1662 } while (tmp > 1);
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001663
Fabio Rossi64cdb0e2009-04-01 20:37:50 +02001664 min_pwrL = pwr_i;
1665 }
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001666
Fabio Rossi64cdb0e2009-04-01 20:37:50 +02001667 if (pwrR[0] == pwrR[1])
1668 min_pwrR = pwrR[0];
1669 else {
1670 pwr_i = pwrR[0];
1671 do {
1672 pwr_i--;
1673 tmp = (s8) ath5k_get_interpolated_value(pwr_i,
1674 pwrR[0], pwrR[1],
1675 stepR[0], stepR[1]);
1676 } while (tmp > 1);
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001677
Fabio Rossi64cdb0e2009-04-01 20:37:50 +02001678 min_pwrR = pwr_i;
1679 }
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001680
1681 /* Keep the right boundary so that it works for both curves */
1682 return max(min_pwrL, min_pwrR);
1683}
1684
1685/*
1686 * Interpolate (pwr,vpd) points to create a Power to PDADC or a
1687 * Power to PCDAC curve.
1688 *
1689 * Each curve has power on x axis (in 0.5dB units) and PCDAC/PDADC
1690 * steps (offsets) on y axis. Power can go up to 31.5dB and max
1691 * PCDAC/PDADC step for each curve is 64 but we can write more than
1692 * one curves on hw so we can go up to 128 (which is the max step we
1693 * can write on the final table).
1694 *
1695 * We write y values (PCDAC/PDADC steps) on hw.
1696 */
1697static void
1698ath5k_create_power_curve(s16 pmin, s16 pmax,
1699 const s16 *pwr, const u8 *vpd,
1700 u8 num_points,
1701 u8 *vpd_table, u8 type)
1702{
1703 u8 idx[2] = { 0, 1 };
1704 s16 pwr_i = 2*pmin;
1705 int i;
1706
1707 if (num_points < 2)
1708 return;
1709
1710 /* We want the whole line, so adjust boundaries
1711 * to cover the entire power range. Note that
1712 * power values are already 0.25dB so no need
1713 * to multiply pwr_i by 2 */
1714 if (type == AR5K_PWRTABLE_LINEAR_PCDAC) {
1715 pwr_i = pmin;
1716 pmin = 0;
1717 pmax = 63;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001718 }
1719
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001720 /* Find surrounding turning points (TPs)
1721 * and interpolate between them */
1722 for (i = 0; (i <= (u16) (pmax - pmin)) &&
1723 (i < AR5K_EEPROM_POWER_TABLE_SIZE); i++) {
1724
1725 /* We passed the right TP, move to the next set of TPs
1726 * if we pass the last TP, extrapolate above using the last
1727 * two TPs for ratio */
1728 if ((pwr_i > pwr[idx[1]]) && (idx[1] < num_points - 1)) {
1729 idx[0]++;
1730 idx[1]++;
1731 }
1732
1733 vpd_table[i] = (u8) ath5k_get_interpolated_value(pwr_i,
1734 pwr[idx[0]], pwr[idx[1]],
1735 vpd[idx[0]], vpd[idx[1]]);
1736
1737 /* Increase by 0.5dB
1738 * (0.25 dB units) */
1739 pwr_i += 2;
1740 }
1741}
1742
1743/*
1744 * Get the surrounding per-channel power calibration piers
1745 * for a given frequency so that we can interpolate between
1746 * them and come up with an apropriate dataset for our current
1747 * channel.
1748 */
1749static void
1750ath5k_get_chan_pcal_surrounding_piers(struct ath5k_hw *ah,
1751 struct ieee80211_channel *channel,
1752 struct ath5k_chan_pcal_info **pcinfo_l,
1753 struct ath5k_chan_pcal_info **pcinfo_r)
1754{
1755 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
1756 struct ath5k_chan_pcal_info *pcinfo;
1757 u8 idx_l, idx_r;
1758 u8 mode, max, i;
1759 u32 target = channel->center_freq;
1760
1761 idx_l = 0;
1762 idx_r = 0;
1763
1764 if (!(channel->hw_value & CHANNEL_OFDM)) {
1765 pcinfo = ee->ee_pwr_cal_b;
1766 mode = AR5K_EEPROM_MODE_11B;
1767 } else if (channel->hw_value & CHANNEL_2GHZ) {
1768 pcinfo = ee->ee_pwr_cal_g;
1769 mode = AR5K_EEPROM_MODE_11G;
1770 } else {
1771 pcinfo = ee->ee_pwr_cal_a;
1772 mode = AR5K_EEPROM_MODE_11A;
1773 }
1774 max = ee->ee_n_piers[mode] - 1;
1775
1776 /* Frequency is below our calibrated
1777 * range. Use the lowest power curve
1778 * we have */
1779 if (target < pcinfo[0].freq) {
1780 idx_l = idx_r = 0;
1781 goto done;
1782 }
1783
1784 /* Frequency is above our calibrated
1785 * range. Use the highest power curve
1786 * we have */
1787 if (target > pcinfo[max].freq) {
1788 idx_l = idx_r = max;
1789 goto done;
1790 }
1791
1792 /* Frequency is inside our calibrated
1793 * channel range. Pick the surrounding
1794 * calibration piers so that we can
1795 * interpolate */
1796 for (i = 0; i <= max; i++) {
1797
1798 /* Frequency matches one of our calibration
1799 * piers, no need to interpolate, just use
1800 * that calibration pier */
1801 if (pcinfo[i].freq == target) {
1802 idx_l = idx_r = i;
1803 goto done;
1804 }
1805
1806 /* We found a calibration pier that's above
1807 * frequency, use this pier and the previous
1808 * one to interpolate */
1809 if (target < pcinfo[i].freq) {
1810 idx_r = i;
1811 idx_l = idx_r - 1;
1812 goto done;
1813 }
1814 }
1815
1816done:
1817 *pcinfo_l = &pcinfo[idx_l];
1818 *pcinfo_r = &pcinfo[idx_r];
1819
1820 return;
1821}
1822
1823/*
1824 * Get the surrounding per-rate power calibration data
1825 * for a given frequency and interpolate between power
1826 * values to set max target power supported by hw for
1827 * each rate.
1828 */
1829static void
1830ath5k_get_rate_pcal_data(struct ath5k_hw *ah,
1831 struct ieee80211_channel *channel,
1832 struct ath5k_rate_pcal_info *rates)
1833{
1834 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
1835 struct ath5k_rate_pcal_info *rpinfo;
1836 u8 idx_l, idx_r;
1837 u8 mode, max, i;
1838 u32 target = channel->center_freq;
1839
1840 idx_l = 0;
1841 idx_r = 0;
1842
1843 if (!(channel->hw_value & CHANNEL_OFDM)) {
1844 rpinfo = ee->ee_rate_tpwr_b;
1845 mode = AR5K_EEPROM_MODE_11B;
1846 } else if (channel->hw_value & CHANNEL_2GHZ) {
1847 rpinfo = ee->ee_rate_tpwr_g;
1848 mode = AR5K_EEPROM_MODE_11G;
1849 } else {
1850 rpinfo = ee->ee_rate_tpwr_a;
1851 mode = AR5K_EEPROM_MODE_11A;
1852 }
1853 max = ee->ee_rate_target_pwr_num[mode] - 1;
1854
1855 /* Get the surrounding calibration
1856 * piers - same as above */
1857 if (target < rpinfo[0].freq) {
1858 idx_l = idx_r = 0;
1859 goto done;
1860 }
1861
1862 if (target > rpinfo[max].freq) {
1863 idx_l = idx_r = max;
1864 goto done;
1865 }
1866
1867 for (i = 0; i <= max; i++) {
1868
1869 if (rpinfo[i].freq == target) {
1870 idx_l = idx_r = i;
1871 goto done;
1872 }
1873
1874 if (target < rpinfo[i].freq) {
1875 idx_r = i;
1876 idx_l = idx_r - 1;
1877 goto done;
1878 }
1879 }
1880
1881done:
1882 /* Now interpolate power value, based on the frequency */
1883 rates->freq = target;
1884
1885 rates->target_power_6to24 =
1886 ath5k_get_interpolated_value(target, rpinfo[idx_l].freq,
1887 rpinfo[idx_r].freq,
1888 rpinfo[idx_l].target_power_6to24,
1889 rpinfo[idx_r].target_power_6to24);
1890
1891 rates->target_power_36 =
1892 ath5k_get_interpolated_value(target, rpinfo[idx_l].freq,
1893 rpinfo[idx_r].freq,
1894 rpinfo[idx_l].target_power_36,
1895 rpinfo[idx_r].target_power_36);
1896
1897 rates->target_power_48 =
1898 ath5k_get_interpolated_value(target, rpinfo[idx_l].freq,
1899 rpinfo[idx_r].freq,
1900 rpinfo[idx_l].target_power_48,
1901 rpinfo[idx_r].target_power_48);
1902
1903 rates->target_power_54 =
1904 ath5k_get_interpolated_value(target, rpinfo[idx_l].freq,
1905 rpinfo[idx_r].freq,
1906 rpinfo[idx_l].target_power_54,
1907 rpinfo[idx_r].target_power_54);
1908}
1909
1910/*
1911 * Get the max edge power for this channel if
1912 * we have such data from EEPROM's Conformance Test
1913 * Limits (CTL), and limit max power if needed.
1914 *
1915 * FIXME: Only works for world regulatory domains
1916 */
1917static void
1918ath5k_get_max_ctl_power(struct ath5k_hw *ah,
1919 struct ieee80211_channel *channel)
1920{
1921 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
1922 struct ath5k_edge_power *rep = ee->ee_ctl_pwr;
1923 u8 *ctl_val = ee->ee_ctl;
1924 s16 max_chan_pwr = ah->ah_txpower.txp_max_pwr / 4;
1925 s16 edge_pwr = 0;
1926 u8 rep_idx;
1927 u8 i, ctl_mode;
1928 u8 ctl_idx = 0xFF;
1929 u32 target = channel->center_freq;
1930
1931 /* Find out a CTL for our mode that's not mapped
1932 * on a specific reg domain.
Nick Kossifidis136bfc72008-04-16 18:42:48 +03001933 *
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001934 * TODO: Map our current reg domain to one of the 3 available
1935 * reg domain ids so that we can support more CTLs. */
1936 switch (channel->hw_value & CHANNEL_MODES) {
1937 case CHANNEL_A:
1938 ctl_mode = AR5K_CTL_11A | AR5K_CTL_NO_REGDOMAIN;
1939 break;
1940 case CHANNEL_G:
1941 ctl_mode = AR5K_CTL_11G | AR5K_CTL_NO_REGDOMAIN;
1942 break;
1943 case CHANNEL_B:
1944 ctl_mode = AR5K_CTL_11B | AR5K_CTL_NO_REGDOMAIN;
1945 break;
1946 case CHANNEL_T:
1947 ctl_mode = AR5K_CTL_TURBO | AR5K_CTL_NO_REGDOMAIN;
1948 break;
1949 case CHANNEL_TG:
1950 ctl_mode = AR5K_CTL_TURBOG | AR5K_CTL_NO_REGDOMAIN;
1951 break;
1952 case CHANNEL_XR:
1953 /* Fall through */
1954 default:
1955 return;
1956 }
Nick Kossifidis903b4742008-02-28 14:50:50 -05001957
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001958 for (i = 0; i < ee->ee_ctls; i++) {
1959 if (ctl_val[i] == ctl_mode) {
1960 ctl_idx = i;
1961 break;
1962 }
1963 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001964
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001965 /* If we have a CTL dataset available grab it and find the
1966 * edge power for our frequency */
1967 if (ctl_idx == 0xFF)
1968 return;
1969
1970 /* Edge powers are sorted by frequency from lower
1971 * to higher. Each CTL corresponds to 8 edge power
1972 * measurements. */
1973 rep_idx = ctl_idx * AR5K_EEPROM_N_EDGES;
1974
1975 /* Don't do boundaries check because we
1976 * might have more that one bands defined
1977 * for this mode */
1978
1979 /* Get the edge power that's closer to our
1980 * frequency */
1981 for (i = 0; i < AR5K_EEPROM_N_EDGES; i++) {
1982 rep_idx += i;
1983 if (target <= rep[rep_idx].freq)
1984 edge_pwr = (s16) rep[rep_idx].edge;
1985 }
1986
1987 if (edge_pwr)
1988 ah->ah_txpower.txp_max_pwr = 4*min(edge_pwr, max_chan_pwr);
1989}
1990
1991
1992/*
1993 * Power to PCDAC table functions
1994 */
1995
1996/*
1997 * Fill Power to PCDAC table on RF5111
1998 *
1999 * No further processing is needed for RF5111, the only thing we have to
2000 * do is fill the values below and above calibration range since eeprom data
2001 * may not cover the entire PCDAC table.
2002 */
2003static void
2004ath5k_fill_pwr_to_pcdac_table(struct ath5k_hw *ah, s16* table_min,
2005 s16 *table_max)
2006{
2007 u8 *pcdac_out = ah->ah_txpower.txp_pd_table;
2008 u8 *pcdac_tmp = ah->ah_txpower.tmpL[0];
2009 u8 pcdac_0, pcdac_n, pcdac_i, pwr_idx, i;
2010 s16 min_pwr, max_pwr;
2011
2012 /* Get table boundaries */
2013 min_pwr = table_min[0];
2014 pcdac_0 = pcdac_tmp[0];
2015
2016 max_pwr = table_max[0];
2017 pcdac_n = pcdac_tmp[table_max[0] - table_min[0]];
2018
2019 /* Extrapolate below minimum using pcdac_0 */
2020 pcdac_i = 0;
2021 for (i = 0; i < min_pwr; i++)
2022 pcdac_out[pcdac_i++] = pcdac_0;
2023
2024 /* Copy values from pcdac_tmp */
2025 pwr_idx = min_pwr;
2026 for (i = 0 ; pwr_idx <= max_pwr &&
2027 pcdac_i < AR5K_EEPROM_POWER_TABLE_SIZE; i++) {
2028 pcdac_out[pcdac_i++] = pcdac_tmp[i];
2029 pwr_idx++;
2030 }
2031
2032 /* Extrapolate above maximum */
2033 while (pcdac_i < AR5K_EEPROM_POWER_TABLE_SIZE)
2034 pcdac_out[pcdac_i++] = pcdac_n;
2035
2036}
2037
2038/*
2039 * Combine available XPD Curves and fill Linear Power to PCDAC table
2040 * on RF5112
2041 *
2042 * RFX112 can have up to 2 curves (one for low txpower range and one for
2043 * higher txpower range). We need to put them both on pcdac_out and place
2044 * them in the correct location. In case we only have one curve available
2045 * just fit it on pcdac_out (it's supposed to cover the entire range of
2046 * available pwr levels since it's always the higher power curve). Extrapolate
2047 * below and above final table if needed.
2048 */
2049static void
2050ath5k_combine_linear_pcdac_curves(struct ath5k_hw *ah, s16* table_min,
2051 s16 *table_max, u8 pdcurves)
2052{
2053 u8 *pcdac_out = ah->ah_txpower.txp_pd_table;
2054 u8 *pcdac_low_pwr;
2055 u8 *pcdac_high_pwr;
2056 u8 *pcdac_tmp;
2057 u8 pwr;
2058 s16 max_pwr_idx;
2059 s16 min_pwr_idx;
2060 s16 mid_pwr_idx = 0;
2061 /* Edge flag turs on the 7nth bit on the PCDAC
2062 * to delcare the higher power curve (force values
2063 * to be greater than 64). If we only have one curve
2064 * we don't need to set this, if we have 2 curves and
2065 * fill the table backwards this can also be used to
2066 * switch from higher power curve to lower power curve */
2067 u8 edge_flag;
2068 int i;
2069
2070 /* When we have only one curve available
2071 * that's the higher power curve. If we have
2072 * two curves the first is the high power curve
2073 * and the next is the low power curve. */
2074 if (pdcurves > 1) {
2075 pcdac_low_pwr = ah->ah_txpower.tmpL[1];
2076 pcdac_high_pwr = ah->ah_txpower.tmpL[0];
2077 mid_pwr_idx = table_max[1] - table_min[1] - 1;
2078 max_pwr_idx = (table_max[0] - table_min[0]) / 2;
2079
2080 /* If table size goes beyond 31.5dB, keep the
2081 * upper 31.5dB range when setting tx power.
2082 * Note: 126 = 31.5 dB in quarter dB steps */
2083 if (table_max[0] - table_min[1] > 126)
2084 min_pwr_idx = table_max[0] - 126;
2085 else
2086 min_pwr_idx = table_min[1];
2087
2088 /* Since we fill table backwards
2089 * start from high power curve */
2090 pcdac_tmp = pcdac_high_pwr;
2091
2092 edge_flag = 0x40;
2093#if 0
2094 /* If both min and max power limits are in lower
2095 * power curve's range, only use the low power curve.
2096 * TODO: min/max levels are related to target
2097 * power values requested from driver/user
2098 * XXX: Is this really needed ? */
2099 if (min_pwr < table_max[1] &&
2100 max_pwr < table_max[1]) {
2101 edge_flag = 0;
2102 pcdac_tmp = pcdac_low_pwr;
2103 max_pwr_idx = (table_max[1] - table_min[1])/2;
2104 }
2105#endif
2106 } else {
2107 pcdac_low_pwr = ah->ah_txpower.tmpL[1]; /* Zeroed */
2108 pcdac_high_pwr = ah->ah_txpower.tmpL[0];
2109 min_pwr_idx = table_min[0];
2110 max_pwr_idx = (table_max[0] - table_min[0]) / 2;
2111 pcdac_tmp = pcdac_high_pwr;
2112 edge_flag = 0;
2113 }
2114
2115 /* This is used when setting tx power*/
2116 ah->ah_txpower.txp_min_idx = min_pwr_idx/2;
2117
2118 /* Fill Power to PCDAC table backwards */
2119 pwr = max_pwr_idx;
2120 for (i = 63; i >= 0; i--) {
2121 /* Entering lower power range, reset
2122 * edge flag and set pcdac_tmp to lower
2123 * power curve.*/
2124 if (edge_flag == 0x40 &&
2125 (2*pwr <= (table_max[1] - table_min[0]) || pwr == 0)) {
2126 edge_flag = 0x00;
2127 pcdac_tmp = pcdac_low_pwr;
2128 pwr = mid_pwr_idx/2;
2129 }
2130
2131 /* Don't go below 1, extrapolate below if we have
2132 * already swithced to the lower power curve -or
2133 * we only have one curve and edge_flag is zero
2134 * anyway */
2135 if (pcdac_tmp[pwr] < 1 && (edge_flag == 0x00)) {
2136 while (i >= 0) {
2137 pcdac_out[i] = pcdac_out[i + 1];
2138 i--;
2139 }
2140 break;
2141 }
2142
2143 pcdac_out[i] = pcdac_tmp[pwr] | edge_flag;
2144
2145 /* Extrapolate above if pcdac is greater than
2146 * 126 -this can happen because we OR pcdac_out
2147 * value with edge_flag on high power curve */
2148 if (pcdac_out[i] > 126)
2149 pcdac_out[i] = 126;
2150
2151 /* Decrease by a 0.5dB step */
2152 pwr--;
2153 }
2154}
2155
2156/* Write PCDAC values on hw */
2157static void
2158ath5k_setup_pcdac_table(struct ath5k_hw *ah)
2159{
2160 u8 *pcdac_out = ah->ah_txpower.txp_pd_table;
2161 int i;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002162
2163 /*
2164 * Write TX power values
2165 */
2166 for (i = 0; i < (AR5K_EEPROM_POWER_TABLE_SIZE / 2); i++) {
2167 ath5k_hw_reg_write(ah,
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02002168 (((pcdac_out[2*i + 0] << 8 | 0xff) & 0xffff) << 0) |
2169 (((pcdac_out[2*i + 1] << 8 | 0xff) & 0xffff) << 16),
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002170 AR5K_PHY_PCDAC_TXPOWER(i));
2171 }
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02002172}
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002173
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02002174
2175/*
2176 * Power to PDADC table functions
2177 */
2178
2179/*
2180 * Set the gain boundaries and create final Power to PDADC table
2181 *
2182 * We can have up to 4 pd curves, we need to do a simmilar process
2183 * as we do for RF5112. This time we don't have an edge_flag but we
2184 * set the gain boundaries on a separate register.
2185 */
2186static void
2187ath5k_combine_pwr_to_pdadc_curves(struct ath5k_hw *ah,
2188 s16 *pwr_min, s16 *pwr_max, u8 pdcurves)
2189{
2190 u8 gain_boundaries[AR5K_EEPROM_N_PD_GAINS];
2191 u8 *pdadc_out = ah->ah_txpower.txp_pd_table;
2192 u8 *pdadc_tmp;
2193 s16 pdadc_0;
2194 u8 pdadc_i, pdadc_n, pwr_step, pdg, max_idx, table_size;
2195 u8 pd_gain_overlap;
2196
2197 /* Note: Register value is initialized on initvals
2198 * there is no feedback from hw.
2199 * XXX: What about pd_gain_overlap from EEPROM ? */
2200 pd_gain_overlap = (u8) ath5k_hw_reg_read(ah, AR5K_PHY_TPC_RG5) &
2201 AR5K_PHY_TPC_RG5_PD_GAIN_OVERLAP;
2202
2203 /* Create final PDADC table */
2204 for (pdg = 0, pdadc_i = 0; pdg < pdcurves; pdg++) {
2205 pdadc_tmp = ah->ah_txpower.tmpL[pdg];
2206
2207 if (pdg == pdcurves - 1)
2208 /* 2 dB boundary stretch for last
2209 * (higher power) curve */
2210 gain_boundaries[pdg] = pwr_max[pdg] + 4;
2211 else
2212 /* Set gain boundary in the middle
2213 * between this curve and the next one */
2214 gain_boundaries[pdg] =
2215 (pwr_max[pdg] + pwr_min[pdg + 1]) / 2;
2216
2217 /* Sanity check in case our 2 db stretch got out of
2218 * range. */
2219 if (gain_boundaries[pdg] > AR5K_TUNE_MAX_TXPOWER)
2220 gain_boundaries[pdg] = AR5K_TUNE_MAX_TXPOWER;
2221
2222 /* For the first curve (lower power)
2223 * start from 0 dB */
2224 if (pdg == 0)
2225 pdadc_0 = 0;
2226 else
2227 /* For the other curves use the gain overlap */
2228 pdadc_0 = (gain_boundaries[pdg - 1] - pwr_min[pdg]) -
2229 pd_gain_overlap;
2230
2231 /* Force each power step to be at least 0.5 dB */
2232 if ((pdadc_tmp[1] - pdadc_tmp[0]) > 1)
2233 pwr_step = pdadc_tmp[1] - pdadc_tmp[0];
2234 else
2235 pwr_step = 1;
2236
2237 /* If pdadc_0 is negative, we need to extrapolate
2238 * below this pdgain by a number of pwr_steps */
2239 while ((pdadc_0 < 0) && (pdadc_i < 128)) {
2240 s16 tmp = pdadc_tmp[0] + pdadc_0 * pwr_step;
2241 pdadc_out[pdadc_i++] = (tmp < 0) ? 0 : (u8) tmp;
2242 pdadc_0++;
2243 }
2244
2245 /* Set last pwr level, using gain boundaries */
2246 pdadc_n = gain_boundaries[pdg] + pd_gain_overlap - pwr_min[pdg];
2247 /* Limit it to be inside pwr range */
2248 table_size = pwr_max[pdg] - pwr_min[pdg];
2249 max_idx = (pdadc_n < table_size) ? pdadc_n : table_size;
2250
2251 /* Fill pdadc_out table */
2252 while (pdadc_0 < max_idx)
2253 pdadc_out[pdadc_i++] = pdadc_tmp[pdadc_0++];
2254
2255 /* Need to extrapolate above this pdgain? */
2256 if (pdadc_n <= max_idx)
2257 continue;
2258
2259 /* Force each power step to be at least 0.5 dB */
2260 if ((pdadc_tmp[table_size - 1] - pdadc_tmp[table_size - 2]) > 1)
2261 pwr_step = pdadc_tmp[table_size - 1] -
2262 pdadc_tmp[table_size - 2];
2263 else
2264 pwr_step = 1;
2265
2266 /* Extrapolate above */
2267 while ((pdadc_0 < (s16) pdadc_n) &&
2268 (pdadc_i < AR5K_EEPROM_POWER_TABLE_SIZE * 2)) {
2269 s16 tmp = pdadc_tmp[table_size - 1] +
2270 (pdadc_0 - max_idx) * pwr_step;
2271 pdadc_out[pdadc_i++] = (tmp > 127) ? 127 : (u8) tmp;
2272 pdadc_0++;
2273 }
2274 }
2275
2276 while (pdg < AR5K_EEPROM_N_PD_GAINS) {
2277 gain_boundaries[pdg] = gain_boundaries[pdg - 1];
2278 pdg++;
2279 }
2280
2281 while (pdadc_i < AR5K_EEPROM_POWER_TABLE_SIZE * 2) {
2282 pdadc_out[pdadc_i] = pdadc_out[pdadc_i - 1];
2283 pdadc_i++;
2284 }
2285
2286 /* Set gain boundaries */
2287 ath5k_hw_reg_write(ah,
2288 AR5K_REG_SM(pd_gain_overlap,
2289 AR5K_PHY_TPC_RG5_PD_GAIN_OVERLAP) |
2290 AR5K_REG_SM(gain_boundaries[0],
2291 AR5K_PHY_TPC_RG5_PD_GAIN_BOUNDARY_1) |
2292 AR5K_REG_SM(gain_boundaries[1],
2293 AR5K_PHY_TPC_RG5_PD_GAIN_BOUNDARY_2) |
2294 AR5K_REG_SM(gain_boundaries[2],
2295 AR5K_PHY_TPC_RG5_PD_GAIN_BOUNDARY_3) |
2296 AR5K_REG_SM(gain_boundaries[3],
2297 AR5K_PHY_TPC_RG5_PD_GAIN_BOUNDARY_4),
2298 AR5K_PHY_TPC_RG5);
2299
2300 /* Used for setting rate power table */
2301 ah->ah_txpower.txp_min_idx = pwr_min[0];
2302
2303}
2304
2305/* Write PDADC values on hw */
2306static void
2307ath5k_setup_pwr_to_pdadc_table(struct ath5k_hw *ah,
2308 u8 pdcurves, u8 *pdg_to_idx)
2309{
2310 u8 *pdadc_out = ah->ah_txpower.txp_pd_table;
2311 u32 reg;
2312 u8 i;
2313
2314 /* Select the right pdgain curves */
2315
2316 /* Clear current settings */
2317 reg = ath5k_hw_reg_read(ah, AR5K_PHY_TPC_RG1);
2318 reg &= ~(AR5K_PHY_TPC_RG1_PDGAIN_1 |
2319 AR5K_PHY_TPC_RG1_PDGAIN_2 |
2320 AR5K_PHY_TPC_RG1_PDGAIN_3 |
2321 AR5K_PHY_TPC_RG1_NUM_PD_GAIN);
2322
2323 /*
2324 * Use pd_gains curve from eeprom
2325 *
2326 * This overrides the default setting from initvals
2327 * in case some vendors (e.g. Zcomax) don't use the default
2328 * curves. If we don't honor their settings we 'll get a
2329 * 5dB (1 * gain overlap ?) drop.
2330 */
2331 reg |= AR5K_REG_SM(pdcurves, AR5K_PHY_TPC_RG1_NUM_PD_GAIN);
2332
2333 switch (pdcurves) {
2334 case 3:
2335 reg |= AR5K_REG_SM(pdg_to_idx[2], AR5K_PHY_TPC_RG1_PDGAIN_3);
2336 /* Fall through */
2337 case 2:
2338 reg |= AR5K_REG_SM(pdg_to_idx[1], AR5K_PHY_TPC_RG1_PDGAIN_2);
2339 /* Fall through */
2340 case 1:
2341 reg |= AR5K_REG_SM(pdg_to_idx[0], AR5K_PHY_TPC_RG1_PDGAIN_1);
2342 break;
2343 }
2344 ath5k_hw_reg_write(ah, reg, AR5K_PHY_TPC_RG1);
2345
2346 /*
2347 * Write TX power values
2348 */
2349 for (i = 0; i < (AR5K_EEPROM_POWER_TABLE_SIZE / 2); i++) {
2350 ath5k_hw_reg_write(ah,
2351 ((pdadc_out[4*i + 0] & 0xff) << 0) |
2352 ((pdadc_out[4*i + 1] & 0xff) << 8) |
2353 ((pdadc_out[4*i + 2] & 0xff) << 16) |
2354 ((pdadc_out[4*i + 3] & 0xff) << 24),
2355 AR5K_PHY_PDADC_TXPOWER(i));
2356 }
2357}
2358
2359
2360/*
2361 * Common code for PCDAC/PDADC tables
2362 */
2363
2364/*
2365 * This is the main function that uses all of the above
2366 * to set PCDAC/PDADC table on hw for the current channel.
2367 * This table is used for tx power calibration on the basband,
2368 * without it we get weird tx power levels and in some cases
2369 * distorted spectral mask
2370 */
2371static int
2372ath5k_setup_channel_powertable(struct ath5k_hw *ah,
2373 struct ieee80211_channel *channel,
2374 u8 ee_mode, u8 type)
2375{
2376 struct ath5k_pdgain_info *pdg_L, *pdg_R;
2377 struct ath5k_chan_pcal_info *pcinfo_L;
2378 struct ath5k_chan_pcal_info *pcinfo_R;
2379 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
2380 u8 *pdg_curve_to_idx = ee->ee_pdc_to_idx[ee_mode];
2381 s16 table_min[AR5K_EEPROM_N_PD_GAINS];
2382 s16 table_max[AR5K_EEPROM_N_PD_GAINS];
2383 u8 *tmpL;
2384 u8 *tmpR;
2385 u32 target = channel->center_freq;
2386 int pdg, i;
2387
2388 /* Get surounding freq piers for this channel */
2389 ath5k_get_chan_pcal_surrounding_piers(ah, channel,
2390 &pcinfo_L,
2391 &pcinfo_R);
2392
2393 /* Loop over pd gain curves on
2394 * surounding freq piers by index */
2395 for (pdg = 0; pdg < ee->ee_pd_gains[ee_mode]; pdg++) {
2396
2397 /* Fill curves in reverse order
2398 * from lower power (max gain)
2399 * to higher power. Use curve -> idx
2400 * backmaping we did on eeprom init */
2401 u8 idx = pdg_curve_to_idx[pdg];
2402
2403 /* Grab the needed curves by index */
2404 pdg_L = &pcinfo_L->pd_curves[idx];
2405 pdg_R = &pcinfo_R->pd_curves[idx];
2406
2407 /* Initialize the temp tables */
2408 tmpL = ah->ah_txpower.tmpL[pdg];
2409 tmpR = ah->ah_txpower.tmpR[pdg];
2410
2411 /* Set curve's x boundaries and create
2412 * curves so that they cover the same
2413 * range (if we don't do that one table
2414 * will have values on some range and the
2415 * other one won't have any so interpolation
2416 * will fail) */
2417 table_min[pdg] = min(pdg_L->pd_pwr[0],
2418 pdg_R->pd_pwr[0]) / 2;
2419
2420 table_max[pdg] = max(pdg_L->pd_pwr[pdg_L->pd_points - 1],
2421 pdg_R->pd_pwr[pdg_R->pd_points - 1]) / 2;
2422
2423 /* Now create the curves on surrounding channels
2424 * and interpolate if needed to get the final
2425 * curve for this gain on this channel */
2426 switch (type) {
2427 case AR5K_PWRTABLE_LINEAR_PCDAC:
2428 /* Override min/max so that we don't loose
2429 * accuracy (don't divide by 2) */
2430 table_min[pdg] = min(pdg_L->pd_pwr[0],
2431 pdg_R->pd_pwr[0]);
2432
2433 table_max[pdg] =
2434 max(pdg_L->pd_pwr[pdg_L->pd_points - 1],
2435 pdg_R->pd_pwr[pdg_R->pd_points - 1]);
2436
2437 /* Override minimum so that we don't get
2438 * out of bounds while extrapolating
2439 * below. Don't do this when we have 2
2440 * curves and we are on the high power curve
2441 * because table_min is ok in this case */
2442 if (!(ee->ee_pd_gains[ee_mode] > 1 && pdg == 0)) {
2443
2444 table_min[pdg] =
2445 ath5k_get_linear_pcdac_min(pdg_L->pd_step,
2446 pdg_R->pd_step,
2447 pdg_L->pd_pwr,
2448 pdg_R->pd_pwr);
2449
2450 /* Don't go too low because we will
2451 * miss the upper part of the curve.
2452 * Note: 126 = 31.5dB (max power supported)
2453 * in 0.25dB units */
2454 if (table_max[pdg] - table_min[pdg] > 126)
2455 table_min[pdg] = table_max[pdg] - 126;
2456 }
2457
2458 /* Fall through */
2459 case AR5K_PWRTABLE_PWR_TO_PCDAC:
2460 case AR5K_PWRTABLE_PWR_TO_PDADC:
2461
2462 ath5k_create_power_curve(table_min[pdg],
2463 table_max[pdg],
2464 pdg_L->pd_pwr,
2465 pdg_L->pd_step,
2466 pdg_L->pd_points, tmpL, type);
2467
2468 /* We are in a calibration
2469 * pier, no need to interpolate
2470 * between freq piers */
2471 if (pcinfo_L == pcinfo_R)
2472 continue;
2473
2474 ath5k_create_power_curve(table_min[pdg],
2475 table_max[pdg],
2476 pdg_R->pd_pwr,
2477 pdg_R->pd_step,
2478 pdg_R->pd_points, tmpR, type);
2479 break;
2480 default:
2481 return -EINVAL;
2482 }
2483
2484 /* Interpolate between curves
2485 * of surounding freq piers to
2486 * get the final curve for this
2487 * pd gain. Re-use tmpL for interpolation
2488 * output */
2489 for (i = 0; (i < (u16) (table_max[pdg] - table_min[pdg])) &&
2490 (i < AR5K_EEPROM_POWER_TABLE_SIZE); i++) {
2491 tmpL[i] = (u8) ath5k_get_interpolated_value(target,
2492 (s16) pcinfo_L->freq,
2493 (s16) pcinfo_R->freq,
2494 (s16) tmpL[i],
2495 (s16) tmpR[i]);
2496 }
2497 }
2498
2499 /* Now we have a set of curves for this
2500 * channel on tmpL (x range is table_max - table_min
2501 * and y values are tmpL[pdg][]) sorted in the same
2502 * order as EEPROM (because we've used the backmaping).
2503 * So for RF5112 it's from higher power to lower power
2504 * and for RF2413 it's from lower power to higher power.
2505 * For RF5111 we only have one curve. */
2506
2507 /* Fill min and max power levels for this
2508 * channel by interpolating the values on
2509 * surounding channels to complete the dataset */
2510 ah->ah_txpower.txp_min_pwr = ath5k_get_interpolated_value(target,
2511 (s16) pcinfo_L->freq,
2512 (s16) pcinfo_R->freq,
2513 pcinfo_L->min_pwr, pcinfo_R->min_pwr);
2514
2515 ah->ah_txpower.txp_max_pwr = ath5k_get_interpolated_value(target,
2516 (s16) pcinfo_L->freq,
2517 (s16) pcinfo_R->freq,
2518 pcinfo_L->max_pwr, pcinfo_R->max_pwr);
2519
2520 /* We are ready to go, fill PCDAC/PDADC
2521 * table and write settings on hardware */
2522 switch (type) {
2523 case AR5K_PWRTABLE_LINEAR_PCDAC:
2524 /* For RF5112 we can have one or two curves
2525 * and each curve covers a certain power lvl
2526 * range so we need to do some more processing */
2527 ath5k_combine_linear_pcdac_curves(ah, table_min, table_max,
2528 ee->ee_pd_gains[ee_mode]);
2529
2530 /* Set txp.offset so that we can
2531 * match max power value with max
2532 * table index */
2533 ah->ah_txpower.txp_offset = 64 - (table_max[0] / 2);
2534
2535 /* Write settings on hw */
2536 ath5k_setup_pcdac_table(ah);
2537 break;
2538 case AR5K_PWRTABLE_PWR_TO_PCDAC:
2539 /* We are done for RF5111 since it has only
2540 * one curve, just fit the curve on the table */
2541 ath5k_fill_pwr_to_pcdac_table(ah, table_min, table_max);
2542
2543 /* No rate powertable adjustment for RF5111 */
2544 ah->ah_txpower.txp_min_idx = 0;
2545 ah->ah_txpower.txp_offset = 0;
2546
2547 /* Write settings on hw */
2548 ath5k_setup_pcdac_table(ah);
2549 break;
2550 case AR5K_PWRTABLE_PWR_TO_PDADC:
2551 /* Set PDADC boundaries and fill
2552 * final PDADC table */
2553 ath5k_combine_pwr_to_pdadc_curves(ah, table_min, table_max,
2554 ee->ee_pd_gains[ee_mode]);
2555
2556 /* Write settings on hw */
2557 ath5k_setup_pwr_to_pdadc_table(ah, pdg, pdg_curve_to_idx);
2558
2559 /* Set txp.offset, note that table_min
2560 * can be negative */
2561 ah->ah_txpower.txp_offset = table_min[0];
2562 break;
2563 default:
2564 return -EINVAL;
2565 }
2566
2567 return 0;
2568}
2569
2570
2571/*
2572 * Per-rate tx power setting
2573 *
2574 * This is the code that sets the desired tx power (below
2575 * maximum) on hw for each rate (we also have TPC that sets
2576 * power per packet). We do that by providing an index on the
2577 * PCDAC/PDADC table we set up.
2578 */
2579
2580/*
2581 * Set rate power table
2582 *
2583 * For now we only limit txpower based on maximum tx power
2584 * supported by hw (what's inside rate_info). We need to limit
2585 * this even more, based on regulatory domain etc.
2586 *
2587 * Rate power table contains indices to PCDAC/PDADC table (0.5dB steps)
2588 * and is indexed as follows:
2589 * rates[0] - rates[7] -> OFDM rates
2590 * rates[8] - rates[14] -> CCK rates
2591 * rates[15] -> XR rates (they all have the same power)
2592 */
2593static void
2594ath5k_setup_rate_powertable(struct ath5k_hw *ah, u16 max_pwr,
2595 struct ath5k_rate_pcal_info *rate_info,
2596 u8 ee_mode)
2597{
2598 unsigned int i;
2599 u16 *rates;
2600
2601 /* max_pwr is power level we got from driver/user in 0.5dB
2602 * units, switch to 0.25dB units so we can compare */
2603 max_pwr *= 2;
2604 max_pwr = min(max_pwr, (u16) ah->ah_txpower.txp_max_pwr) / 2;
2605
2606 /* apply rate limits */
2607 rates = ah->ah_txpower.txp_rates_power_table;
2608
2609 /* OFDM rates 6 to 24Mb/s */
2610 for (i = 0; i < 5; i++)
2611 rates[i] = min(max_pwr, rate_info->target_power_6to24);
2612
2613 /* Rest OFDM rates */
2614 rates[5] = min(rates[0], rate_info->target_power_36);
2615 rates[6] = min(rates[0], rate_info->target_power_48);
2616 rates[7] = min(rates[0], rate_info->target_power_54);
2617
2618 /* CCK rates */
2619 /* 1L */
2620 rates[8] = min(rates[0], rate_info->target_power_6to24);
2621 /* 2L */
2622 rates[9] = min(rates[0], rate_info->target_power_36);
2623 /* 2S */
2624 rates[10] = min(rates[0], rate_info->target_power_36);
2625 /* 5L */
2626 rates[11] = min(rates[0], rate_info->target_power_48);
2627 /* 5S */
2628 rates[12] = min(rates[0], rate_info->target_power_48);
2629 /* 11L */
2630 rates[13] = min(rates[0], rate_info->target_power_54);
2631 /* 11S */
2632 rates[14] = min(rates[0], rate_info->target_power_54);
2633
2634 /* XR rates */
2635 rates[15] = min(rates[0], rate_info->target_power_6to24);
2636
2637 /* CCK rates have different peak to average ratio
2638 * so we have to tweak their power so that gainf
2639 * correction works ok. For this we use OFDM to
2640 * CCK delta from eeprom */
2641 if ((ee_mode == AR5K_EEPROM_MODE_11G) &&
2642 (ah->ah_phy_revision < AR5K_SREV_PHY_5212A))
2643 for (i = 8; i <= 15; i++)
2644 rates[i] -= ah->ah_txpower.txp_cck_ofdm_gainf_delta;
2645
Nick Kossifidisa0823812009-04-30 15:55:44 -04002646 /* Now that we have all rates setup use table offset to
2647 * match the power range set by user with the power indices
2648 * on PCDAC/PDADC table */
2649 for (i = 0; i < 16; i++) {
2650 rates[i] += ah->ah_txpower.txp_offset;
2651 /* Don't get out of bounds */
2652 if (rates[i] > 63)
2653 rates[i] = 63;
2654 }
2655
2656 /* Min/max in 0.25dB units */
2657 ah->ah_txpower.txp_min_pwr = 2 * rates[7];
2658 ah->ah_txpower.txp_max_pwr = 2 * rates[0];
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02002659 ah->ah_txpower.txp_ofdm = rates[7];
2660}
2661
2662
2663/*
2664 * Set transmition power
2665 */
2666int
2667ath5k_hw_txpower(struct ath5k_hw *ah, struct ieee80211_channel *channel,
2668 u8 ee_mode, u8 txpower)
2669{
2670 struct ath5k_rate_pcal_info rate_info;
2671 u8 type;
2672 int ret;
2673
2674 ATH5K_TRACE(ah->ah_sc);
2675 if (txpower > AR5K_TUNE_MAX_TXPOWER) {
2676 ATH5K_ERR(ah->ah_sc, "invalid tx power: %u\n", txpower);
2677 return -EINVAL;
2678 }
2679 if (txpower == 0)
2680 txpower = AR5K_TUNE_DEFAULT_TXPOWER;
2681
2682 /* Reset TX power values */
2683 memset(&ah->ah_txpower, 0, sizeof(ah->ah_txpower));
2684 ah->ah_txpower.txp_tpc = AR5K_TUNE_TPC_TXPOWER;
2685 ah->ah_txpower.txp_min_pwr = 0;
2686 ah->ah_txpower.txp_max_pwr = AR5K_TUNE_MAX_TXPOWER;
2687
2688 /* Initialize TX power table */
2689 switch (ah->ah_radio) {
2690 case AR5K_RF5111:
2691 type = AR5K_PWRTABLE_PWR_TO_PCDAC;
2692 break;
2693 case AR5K_RF5112:
2694 type = AR5K_PWRTABLE_LINEAR_PCDAC;
2695 break;
2696 case AR5K_RF2413:
2697 case AR5K_RF5413:
2698 case AR5K_RF2316:
2699 case AR5K_RF2317:
2700 case AR5K_RF2425:
2701 type = AR5K_PWRTABLE_PWR_TO_PDADC;
2702 break;
2703 default:
2704 return -EINVAL;
2705 }
2706
2707 /* FIXME: Only on channel/mode change */
2708 ret = ath5k_setup_channel_powertable(ah, channel, ee_mode, type);
2709 if (ret)
2710 return ret;
2711
2712 /* Limit max power if we have a CTL available */
2713 ath5k_get_max_ctl_power(ah, channel);
2714
2715 /* FIXME: Tx power limit for this regdomain
2716 * XXX: Mac80211/CRDA will do that anyway ? */
2717
2718 /* FIXME: Antenna reduction stuff */
2719
2720 /* FIXME: Limit power on turbo modes */
2721
2722 /* FIXME: TPC scale reduction */
2723
2724 /* Get surounding channels for per-rate power table
2725 * calibration */
2726 ath5k_get_rate_pcal_data(ah, channel, &rate_info);
2727
2728 /* Setup rate power table */
2729 ath5k_setup_rate_powertable(ah, txpower, &rate_info, ee_mode);
2730
2731 /* Write rate power table on hw */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002732 ath5k_hw_reg_write(ah, AR5K_TXPOWER_OFDM(3, 24) |
2733 AR5K_TXPOWER_OFDM(2, 16) | AR5K_TXPOWER_OFDM(1, 8) |
2734 AR5K_TXPOWER_OFDM(0, 0), AR5K_PHY_TXPOWER_RATE1);
2735
2736 ath5k_hw_reg_write(ah, AR5K_TXPOWER_OFDM(7, 24) |
2737 AR5K_TXPOWER_OFDM(6, 16) | AR5K_TXPOWER_OFDM(5, 8) |
2738 AR5K_TXPOWER_OFDM(4, 0), AR5K_PHY_TXPOWER_RATE2);
2739
2740 ath5k_hw_reg_write(ah, AR5K_TXPOWER_CCK(10, 24) |
2741 AR5K_TXPOWER_CCK(9, 16) | AR5K_TXPOWER_CCK(15, 8) |
2742 AR5K_TXPOWER_CCK(8, 0), AR5K_PHY_TXPOWER_RATE3);
2743
2744 ath5k_hw_reg_write(ah, AR5K_TXPOWER_CCK(14, 24) |
2745 AR5K_TXPOWER_CCK(13, 16) | AR5K_TXPOWER_CCK(12, 8) |
2746 AR5K_TXPOWER_CCK(11, 0), AR5K_PHY_TXPOWER_RATE4);
2747
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02002748 /* FIXME: TPC support */
2749 if (ah->ah_txpower.txp_tpc) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002750 ath5k_hw_reg_write(ah, AR5K_PHY_TXPOWER_RATE_MAX_TPC_ENABLE |
2751 AR5K_TUNE_MAX_TXPOWER, AR5K_PHY_TXPOWER_RATE_MAX);
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02002752
2753 ath5k_hw_reg_write(ah,
2754 AR5K_REG_MS(AR5K_TUNE_MAX_TXPOWER, AR5K_TPC_ACK) |
2755 AR5K_REG_MS(AR5K_TUNE_MAX_TXPOWER, AR5K_TPC_CTS) |
2756 AR5K_REG_MS(AR5K_TUNE_MAX_TXPOWER, AR5K_TPC_CHIRP),
2757 AR5K_TPC);
2758 } else {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002759 ath5k_hw_reg_write(ah, AR5K_PHY_TXPOWER_RATE_MAX |
2760 AR5K_TUNE_MAX_TXPOWER, AR5K_PHY_TXPOWER_RATE_MAX);
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02002761 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002762
2763 return 0;
2764}
2765
Nick Kossifidisa0823812009-04-30 15:55:44 -04002766int ath5k_hw_set_txpower_limit(struct ath5k_hw *ah, u8 txpower)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002767{
2768 /*Just a try M.F.*/
2769 struct ieee80211_channel *channel = &ah->ah_current_channel;
Nick Kossifidisa0823812009-04-30 15:55:44 -04002770 u8 ee_mode;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002771
2772 ATH5K_TRACE(ah->ah_sc);
Nick Kossifidisa0823812009-04-30 15:55:44 -04002773
2774 switch (channel->hw_value & CHANNEL_MODES) {
2775 case CHANNEL_A:
2776 case CHANNEL_T:
2777 case CHANNEL_XR:
2778 ee_mode = AR5K_EEPROM_MODE_11A;
2779 break;
2780 case CHANNEL_G:
2781 case CHANNEL_TG:
2782 ee_mode = AR5K_EEPROM_MODE_11G;
2783 break;
2784 case CHANNEL_B:
2785 ee_mode = AR5K_EEPROM_MODE_11B;
2786 break;
2787 default:
2788 ATH5K_ERR(ah->ah_sc,
2789 "invalid channel: %d\n", channel->center_freq);
2790 return -EINVAL;
2791 }
2792
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002793 ATH5K_DBG(ah->ah_sc, ATH5K_DEBUG_TXPOWER,
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02002794 "changing txpower to %d\n", txpower);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002795
Nick Kossifidisa0823812009-04-30 15:55:44 -04002796 return ath5k_hw_txpower(ah, channel, ee_mode, txpower);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002797}
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03002798
2799#undef _ATH5K_PHY