blob: d419c6a3ded70a2357573916b3dc34376f64d80f [file] [log] [blame]
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001/*
2 * Copyright (c) 2004-2008 Reyk Floeter <reyk@openbsd.org>
3 * Copyright (c) 2006-2008 Nick Kossifidis <mickflemm@gmail.com>
4 * Copyright (c) 2007-2008 Luis Rodriguez <mcgrof@winlab.rutgers.edu>
5 * Copyright (c) 2007-2008 Pavel Roskin <proski@gnu.org>
6 * Copyright (c) 2007-2008 Jiri Slaby <jirislaby@gmail.com>
7 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 *
20 */
21
22#define _ATH5K_RESET
23
24/*****************************\
25 Reset functions and helpers
26\*****************************/
27
Nick Kossifidise8f055f2009-02-09 06:12:58 +020028#include <linux/pci.h> /* To determine if a card is pci-e */
29#include <linux/bitops.h> /* For get_bitmask_order */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030030#include "ath5k.h"
31#include "reg.h"
32#include "base.h"
33#include "debug.h"
34
35/**
36 * ath5k_hw_write_ofdm_timings - set OFDM timings on AR5212
37 *
38 * @ah: the &struct ath5k_hw
39 * @channel: the currently set channel upon reset
40 *
Nick Kossifidise8f055f2009-02-09 06:12:58 +020041 * Write the delta slope coefficient (used on pilot tracking ?) for OFDM
42 * operation on the AR5212 upon reset. This is a helper for ath5k_hw_reset().
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030043 *
Nick Kossifidise8f055f2009-02-09 06:12:58 +020044 * Since delta slope is floating point we split it on its exponent and
45 * mantissa and provide these values on hw.
46 *
47 * For more infos i think this patent is related
48 * http://www.freepatentsonline.com/7184495.html
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030049 */
50static inline int ath5k_hw_write_ofdm_timings(struct ath5k_hw *ah,
51 struct ieee80211_channel *channel)
52{
53 /* Get exponent and mantissa and set it */
54 u32 coef_scaled, coef_exp, coef_man,
55 ds_coef_exp, ds_coef_man, clock;
56
Alexander Beregalov0ee904c2009-04-11 14:50:23 +000057 BUG_ON(!(ah->ah_version == AR5K_AR5212) ||
58 !(channel->hw_value & CHANNEL_OFDM));
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030059
Nick Kossifidise8f055f2009-02-09 06:12:58 +020060 /* Get coefficient
61 * ALGO: coef = (5 * clock * carrier_freq) / 2)
62 * we scale coef by shifting clock value by 24 for
63 * better precision since we use integers */
64 /* TODO: Half/quarter rate */
65 clock = ath5k_hw_htoclock(1, channel->hw_value & CHANNEL_TURBO);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030066
Nick Kossifidise8f055f2009-02-09 06:12:58 +020067 coef_scaled = ((5 * (clock << 24)) / 2) / channel->center_freq;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030068
Nick Kossifidise8f055f2009-02-09 06:12:58 +020069 /* Get exponent
70 * ALGO: coef_exp = 14 - highest set bit position */
71 coef_exp = get_bitmask_order(coef_scaled);
72
73 /* Doesn't make sense if it's zero*/
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030074 if (!coef_exp)
75 return -EINVAL;
76
Nick Kossifidise8f055f2009-02-09 06:12:58 +020077 /* Note: we've shifted coef_scaled by 24 */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030078 coef_exp = 14 - (coef_exp - 24);
Nick Kossifidise8f055f2009-02-09 06:12:58 +020079
80
81 /* Get mantissa (significant digits)
82 * ALGO: coef_mant = floor(coef_scaled* 2^coef_exp+0.5) */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030083 coef_man = coef_scaled +
84 (1 << (24 - coef_exp - 1));
Nick Kossifidise8f055f2009-02-09 06:12:58 +020085
86 /* Calculate delta slope coefficient exponent
87 * and mantissa (remove scaling) and set them on hw */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030088 ds_coef_man = coef_man >> (24 - coef_exp);
89 ds_coef_exp = coef_exp - 16;
90
91 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_TIMING_3,
92 AR5K_PHY_TIMING_3_DSC_MAN, ds_coef_man);
93 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_TIMING_3,
94 AR5K_PHY_TIMING_3_DSC_EXP, ds_coef_exp);
95
96 return 0;
97}
98
99
100/*
101 * index into rates for control rates, we can set it up like this because
102 * this is only used for AR5212 and we know it supports G mode
103 */
Jiri Slaby2c91108c2009-03-07 10:26:41 +0100104static const unsigned int control_rates[] =
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300105 { 0, 1, 1, 1, 4, 4, 6, 6, 8, 8, 8, 8 };
106
107/**
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200108 * ath5k_hw_write_rate_duration - fill rate code to duration table
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300109 *
110 * @ah: the &struct ath5k_hw
111 * @mode: one of enum ath5k_driver_mode
112 *
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200113 * Write the rate code to duration table upon hw reset. This is a helper for
114 * ath5k_hw_reset(). It seems all this is doing is setting an ACK timeout on
115 * the hardware, based on current mode, for each rate. The rates which are
116 * capable of short preamble (802.11b rates 2Mbps, 5.5Mbps, and 11Mbps) have
117 * different rate code so we write their value twice (one for long preample
118 * and one for short).
119 *
120 * Note: Band doesn't matter here, if we set the values for OFDM it works
121 * on both a and g modes. So all we have to do is set values for all g rates
122 * that include all OFDM and CCK rates. If we operate in turbo or xr/half/
123 * quarter rate mode, we need to use another set of bitrates (that's why we
124 * need the mode parameter) but we don't handle these proprietary modes yet.
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300125 */
126static inline void ath5k_hw_write_rate_duration(struct ath5k_hw *ah,
127 unsigned int mode)
128{
129 struct ath5k_softc *sc = ah->ah_sc;
130 struct ieee80211_rate *rate;
131 unsigned int i;
132
133 /* Write rate duration table */
134 for (i = 0; i < sc->sbands[IEEE80211_BAND_2GHZ].n_bitrates; i++) {
135 u32 reg;
136 u16 tx_time;
137
138 rate = &sc->sbands[IEEE80211_BAND_2GHZ].bitrates[control_rates[i]];
139
140 /* Set ACK timeout */
141 reg = AR5K_RATE_DUR(rate->hw_value);
142
143 /* An ACK frame consists of 10 bytes. If you add the FCS,
144 * which ieee80211_generic_frame_duration() adds,
145 * its 14 bytes. Note we use the control rate and not the
146 * actual rate for this rate. See mac80211 tx.c
147 * ieee80211_duration() for a brief description of
148 * what rate we should choose to TX ACKs. */
149 tx_time = le16_to_cpu(ieee80211_generic_frame_duration(sc->hw,
150 sc->vif, 10, rate));
151
152 ath5k_hw_reg_write(ah, tx_time, reg);
153
154 if (!(rate->flags & IEEE80211_RATE_SHORT_PREAMBLE))
155 continue;
156
157 /*
158 * We're not distinguishing short preamble here,
159 * This is true, all we'll get is a longer value here
160 * which is not necessarilly bad. We could use
161 * export ieee80211_frame_duration() but that needs to be
162 * fixed first to be properly used by mac802111 drivers:
163 *
164 * - remove erp stuff and let the routine figure ofdm
165 * erp rates
166 * - remove passing argument ieee80211_local as
167 * drivers don't have access to it
168 * - move drivers using ieee80211_generic_frame_duration()
169 * to this
170 */
171 ath5k_hw_reg_write(ah, tx_time,
172 reg + (AR5K_SET_SHORT_PREAMBLE << 2));
173 }
174}
175
176/*
177 * Reset chipset
178 */
179static int ath5k_hw_nic_reset(struct ath5k_hw *ah, u32 val)
180{
181 int ret;
182 u32 mask = val ? val : ~0U;
183
184 ATH5K_TRACE(ah->ah_sc);
185
186 /* Read-and-clear RX Descriptor Pointer*/
187 ath5k_hw_reg_read(ah, AR5K_RXDP);
188
189 /*
190 * Reset the device and wait until success
191 */
192 ath5k_hw_reg_write(ah, val, AR5K_RESET_CTL);
193
194 /* Wait at least 128 PCI clocks */
195 udelay(15);
196
197 if (ah->ah_version == AR5K_AR5210) {
Nick Kossifidis84e463f2008-09-17 03:33:19 +0300198 val &= AR5K_RESET_CTL_PCU | AR5K_RESET_CTL_DMA
199 | AR5K_RESET_CTL_MAC | AR5K_RESET_CTL_PHY;
200 mask &= AR5K_RESET_CTL_PCU | AR5K_RESET_CTL_DMA
201 | AR5K_RESET_CTL_MAC | AR5K_RESET_CTL_PHY;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300202 } else {
203 val &= AR5K_RESET_CTL_PCU | AR5K_RESET_CTL_BASEBAND;
204 mask &= AR5K_RESET_CTL_PCU | AR5K_RESET_CTL_BASEBAND;
205 }
206
207 ret = ath5k_hw_register_timeout(ah, AR5K_RESET_CTL, mask, val, false);
208
209 /*
210 * Reset configuration register (for hw byte-swap). Note that this
211 * is only set for big endian. We do the necessary magic in
212 * AR5K_INIT_CFG.
213 */
214 if ((val & AR5K_RESET_CTL_PCU) == 0)
215 ath5k_hw_reg_write(ah, AR5K_INIT_CFG, AR5K_CFG);
216
217 return ret;
218}
219
220/*
221 * Sleep control
222 */
223int ath5k_hw_set_power(struct ath5k_hw *ah, enum ath5k_power_mode mode,
224 bool set_chip, u16 sleep_duration)
225{
226 unsigned int i;
227 u32 staid, data;
228
229 ATH5K_TRACE(ah->ah_sc);
230 staid = ath5k_hw_reg_read(ah, AR5K_STA_ID1);
231
232 switch (mode) {
233 case AR5K_PM_AUTO:
234 staid &= ~AR5K_STA_ID1_DEFAULT_ANTENNA;
235 /* fallthrough */
236 case AR5K_PM_NETWORK_SLEEP:
237 if (set_chip)
238 ath5k_hw_reg_write(ah,
239 AR5K_SLEEP_CTL_SLE_ALLOW |
240 sleep_duration,
241 AR5K_SLEEP_CTL);
242
243 staid |= AR5K_STA_ID1_PWR_SV;
244 break;
245
246 case AR5K_PM_FULL_SLEEP:
247 if (set_chip)
248 ath5k_hw_reg_write(ah, AR5K_SLEEP_CTL_SLE_SLP,
249 AR5K_SLEEP_CTL);
250
251 staid |= AR5K_STA_ID1_PWR_SV;
252 break;
253
254 case AR5K_PM_AWAKE:
255
256 staid &= ~AR5K_STA_ID1_PWR_SV;
257
258 if (!set_chip)
259 goto commit;
260
261 /* Preserve sleep duration */
262 data = ath5k_hw_reg_read(ah, AR5K_SLEEP_CTL);
263 if (data & 0xffc00000)
264 data = 0;
265 else
266 data = data & 0xfffcffff;
267
268 ath5k_hw_reg_write(ah, data, AR5K_SLEEP_CTL);
269 udelay(15);
270
271 for (i = 50; i > 0; i--) {
272 /* Check if the chip did wake up */
273 if ((ath5k_hw_reg_read(ah, AR5K_PCICFG) &
274 AR5K_PCICFG_SPWR_DN) == 0)
275 break;
276
277 /* Wait a bit and retry */
278 udelay(200);
279 ath5k_hw_reg_write(ah, data, AR5K_SLEEP_CTL);
280 }
281
282 /* Fail if the chip didn't wake up */
283 if (i <= 0)
284 return -EIO;
285
286 break;
287
288 default:
289 return -EINVAL;
290 }
291
292commit:
293 ah->ah_power_mode = mode;
294 ath5k_hw_reg_write(ah, staid, AR5K_STA_ID1);
295
296 return 0;
297}
298
299/*
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200300 * Bring up MAC + PHY Chips and program PLL
301 * TODO: Half/Quarter rate support
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300302 */
303int ath5k_hw_nic_wakeup(struct ath5k_hw *ah, int flags, bool initial)
304{
305 struct pci_dev *pdev = ah->ah_sc->pdev;
306 u32 turbo, mode, clock, bus_flags;
307 int ret;
308
309 turbo = 0;
310 mode = 0;
311 clock = 0;
312
313 ATH5K_TRACE(ah->ah_sc);
314
315 /* Wakeup the device */
316 ret = ath5k_hw_set_power(ah, AR5K_PM_AWAKE, true, 0);
317 if (ret) {
318 ATH5K_ERR(ah->ah_sc, "failed to wakeup the MAC Chip\n");
319 return ret;
320 }
321
322 if (ah->ah_version != AR5K_AR5210) {
323 /*
324 * Get channel mode flags
325 */
326
327 if (ah->ah_radio >= AR5K_RF5112) {
328 mode = AR5K_PHY_MODE_RAD_RF5112;
329 clock = AR5K_PHY_PLL_RF5112;
330 } else {
331 mode = AR5K_PHY_MODE_RAD_RF5111; /*Zero*/
332 clock = AR5K_PHY_PLL_RF5111; /*Zero*/
333 }
334
335 if (flags & CHANNEL_2GHZ) {
336 mode |= AR5K_PHY_MODE_FREQ_2GHZ;
337 clock |= AR5K_PHY_PLL_44MHZ;
338
339 if (flags & CHANNEL_CCK) {
340 mode |= AR5K_PHY_MODE_MOD_CCK;
341 } else if (flags & CHANNEL_OFDM) {
342 /* XXX Dynamic OFDM/CCK is not supported by the
343 * AR5211 so we set MOD_OFDM for plain g (no
344 * CCK headers) operation. We need to test
345 * this, 5211 might support ofdm-only g after
346 * all, there are also initial register values
347 * in the code for g mode (see initvals.c). */
348 if (ah->ah_version == AR5K_AR5211)
349 mode |= AR5K_PHY_MODE_MOD_OFDM;
350 else
351 mode |= AR5K_PHY_MODE_MOD_DYN;
352 } else {
353 ATH5K_ERR(ah->ah_sc,
354 "invalid radio modulation mode\n");
355 return -EINVAL;
356 }
357 } else if (flags & CHANNEL_5GHZ) {
358 mode |= AR5K_PHY_MODE_FREQ_5GHZ;
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200359
360 if (ah->ah_radio == AR5K_RF5413)
Pavel Roskin807e3732009-03-27 17:47:27 -0400361 clock = AR5K_PHY_PLL_40MHZ_5413;
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200362 else
363 clock |= AR5K_PHY_PLL_40MHZ;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300364
365 if (flags & CHANNEL_OFDM)
366 mode |= AR5K_PHY_MODE_MOD_OFDM;
367 else {
368 ATH5K_ERR(ah->ah_sc,
369 "invalid radio modulation mode\n");
370 return -EINVAL;
371 }
372 } else {
373 ATH5K_ERR(ah->ah_sc, "invalid radio frequency mode\n");
374 return -EINVAL;
375 }
376
377 if (flags & CHANNEL_TURBO)
378 turbo = AR5K_PHY_TURBO_MODE | AR5K_PHY_TURBO_SHORT;
379 } else { /* Reset the device */
380
381 /* ...enable Atheros turbo mode if requested */
382 if (flags & CHANNEL_TURBO)
383 ath5k_hw_reg_write(ah, AR5K_PHY_TURBO_MODE,
384 AR5K_PHY_TURBO);
385 }
386
387 /* reseting PCI on PCI-E cards results card to hang
388 * and always return 0xffff... so we ingore that flag
389 * for PCI-E cards */
390 bus_flags = (pdev->is_pcie) ? 0 : AR5K_RESET_CTL_PCI;
391
392 /* Reset chipset */
Nick Kossifidis84e463f2008-09-17 03:33:19 +0300393 if (ah->ah_version == AR5K_AR5210) {
394 ret = ath5k_hw_nic_reset(ah, AR5K_RESET_CTL_PCU |
395 AR5K_RESET_CTL_MAC | AR5K_RESET_CTL_DMA |
396 AR5K_RESET_CTL_PHY | AR5K_RESET_CTL_PCI);
397 mdelay(2);
398 } else {
399 ret = ath5k_hw_nic_reset(ah, AR5K_RESET_CTL_PCU |
400 AR5K_RESET_CTL_BASEBAND | bus_flags);
401 }
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300402 if (ret) {
403 ATH5K_ERR(ah->ah_sc, "failed to reset the MAC Chip\n");
404 return -EIO;
405 }
406
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300407 /* ...wakeup again!*/
408 ret = ath5k_hw_set_power(ah, AR5K_PM_AWAKE, true, 0);
409 if (ret) {
410 ATH5K_ERR(ah->ah_sc, "failed to resume the MAC Chip\n");
411 return ret;
412 }
413
414 /* ...final warm reset */
415 if (ath5k_hw_nic_reset(ah, 0)) {
416 ATH5K_ERR(ah->ah_sc, "failed to warm reset the MAC Chip\n");
417 return -EIO;
418 }
419
420 if (ah->ah_version != AR5K_AR5210) {
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300421
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200422 /* ...update PLL if needed */
423 if (ath5k_hw_reg_read(ah, AR5K_PHY_PLL) != clock) {
424 ath5k_hw_reg_write(ah, clock, AR5K_PHY_PLL);
425 udelay(300);
426 }
427
428 /* ...set the PHY operating mode */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300429 ath5k_hw_reg_write(ah, mode, AR5K_PHY_MODE);
430 ath5k_hw_reg_write(ah, turbo, AR5K_PHY_TURBO);
431 }
432
433 return 0;
434}
435
436/*
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200437 * If there is an external 32KHz crystal available, use it
438 * as ref. clock instead of 32/40MHz clock and baseband clocks
439 * to save power during sleep or restore normal 32/40MHz
440 * operation.
441 *
442 * XXX: When operating on 32KHz certain PHY registers (27 - 31,
443 * 123 - 127) require delay on access.
444 */
445static void ath5k_hw_set_sleep_clock(struct ath5k_hw *ah, bool enable)
446{
447 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
448 u32 scal, spending, usec32;
449
450 /* Only set 32KHz settings if we have an external
451 * 32KHz crystal present */
452 if ((AR5K_EEPROM_HAS32KHZCRYSTAL(ee->ee_misc1) ||
453 AR5K_EEPROM_HAS32KHZCRYSTAL_OLD(ee->ee_misc1)) &&
454 enable) {
455
456 /* 1 usec/cycle */
457 AR5K_REG_WRITE_BITS(ah, AR5K_USEC_5211, AR5K_USEC_32, 1);
458 /* Set up tsf increment on each cycle */
459 AR5K_REG_WRITE_BITS(ah, AR5K_TSF_PARM, AR5K_TSF_PARM_INC, 61);
460
461 /* Set baseband sleep control registers
462 * and sleep control rate */
463 ath5k_hw_reg_write(ah, 0x1f, AR5K_PHY_SCR);
464
465 if ((ah->ah_radio == AR5K_RF5112) ||
466 (ah->ah_radio == AR5K_RF5413) ||
467 (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4)))
468 spending = 0x14;
469 else
470 spending = 0x18;
471 ath5k_hw_reg_write(ah, spending, AR5K_PHY_SPENDING);
472
473 if ((ah->ah_radio == AR5K_RF5112) ||
474 (ah->ah_radio == AR5K_RF5413) ||
475 (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4))) {
476 ath5k_hw_reg_write(ah, 0x26, AR5K_PHY_SLMT);
477 ath5k_hw_reg_write(ah, 0x0d, AR5K_PHY_SCAL);
478 ath5k_hw_reg_write(ah, 0x07, AR5K_PHY_SCLOCK);
479 ath5k_hw_reg_write(ah, 0x3f, AR5K_PHY_SDELAY);
480 AR5K_REG_WRITE_BITS(ah, AR5K_PCICFG,
481 AR5K_PCICFG_SLEEP_CLOCK_RATE, 0x02);
482 } else {
483 ath5k_hw_reg_write(ah, 0x0a, AR5K_PHY_SLMT);
484 ath5k_hw_reg_write(ah, 0x0c, AR5K_PHY_SCAL);
485 ath5k_hw_reg_write(ah, 0x03, AR5K_PHY_SCLOCK);
486 ath5k_hw_reg_write(ah, 0x20, AR5K_PHY_SDELAY);
487 AR5K_REG_WRITE_BITS(ah, AR5K_PCICFG,
488 AR5K_PCICFG_SLEEP_CLOCK_RATE, 0x03);
489 }
490
491 /* Enable sleep clock operation */
492 AR5K_REG_ENABLE_BITS(ah, AR5K_PCICFG,
493 AR5K_PCICFG_SLEEP_CLOCK_EN);
494
495 } else {
496
497 /* Disable sleep clock operation and
498 * restore default parameters */
499 AR5K_REG_DISABLE_BITS(ah, AR5K_PCICFG,
500 AR5K_PCICFG_SLEEP_CLOCK_EN);
501
502 AR5K_REG_WRITE_BITS(ah, AR5K_PCICFG,
503 AR5K_PCICFG_SLEEP_CLOCK_RATE, 0);
504
505 ath5k_hw_reg_write(ah, 0x1f, AR5K_PHY_SCR);
506 ath5k_hw_reg_write(ah, AR5K_PHY_SLMT_32MHZ, AR5K_PHY_SLMT);
507
508 if (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4))
509 scal = AR5K_PHY_SCAL_32MHZ_2417;
Nick Kossifidis1889ba02009-04-30 15:55:46 -0400510 else if (ee->ee_is_hb63)
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200511 scal = AR5K_PHY_SCAL_32MHZ_HB63;
512 else
513 scal = AR5K_PHY_SCAL_32MHZ;
514 ath5k_hw_reg_write(ah, scal, AR5K_PHY_SCAL);
515
516 ath5k_hw_reg_write(ah, AR5K_PHY_SCLOCK_32MHZ, AR5K_PHY_SCLOCK);
517 ath5k_hw_reg_write(ah, AR5K_PHY_SDELAY_32MHZ, AR5K_PHY_SDELAY);
518
519 if ((ah->ah_radio == AR5K_RF5112) ||
520 (ah->ah_radio == AR5K_RF5413) ||
521 (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4)))
522 spending = 0x14;
523 else
524 spending = 0x18;
525 ath5k_hw_reg_write(ah, spending, AR5K_PHY_SPENDING);
526
527 if ((ah->ah_radio == AR5K_RF5112) ||
528 (ah->ah_radio == AR5K_RF5413))
529 usec32 = 39;
530 else
531 usec32 = 31;
532 AR5K_REG_WRITE_BITS(ah, AR5K_USEC_5211, AR5K_USEC_32, usec32);
533
534 AR5K_REG_WRITE_BITS(ah, AR5K_TSF_PARM, AR5K_TSF_PARM_INC, 1);
535 }
536 return;
537}
538
539static bool ath5k_hw_chan_has_spur_noise(struct ath5k_hw *ah,
540 struct ieee80211_channel *channel)
541{
542 u8 refclk_freq;
543
544 if ((ah->ah_radio == AR5K_RF5112) ||
545 (ah->ah_radio == AR5K_RF5413) ||
546 (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4)))
547 refclk_freq = 40;
548 else
549 refclk_freq = 32;
550
551 if ((channel->center_freq % refclk_freq != 0) &&
552 ((channel->center_freq % refclk_freq < 10) ||
553 (channel->center_freq % refclk_freq > 22)))
554 return true;
555 else
556 return false;
557}
558
559/* TODO: Half/Quarter rate */
560static void ath5k_hw_tweak_initval_settings(struct ath5k_hw *ah,
561 struct ieee80211_channel *channel)
562{
563 if (ah->ah_version == AR5K_AR5212 &&
564 ah->ah_phy_revision >= AR5K_SREV_PHY_5212A) {
565
566 /* Setup ADC control */
567 ath5k_hw_reg_write(ah,
568 (AR5K_REG_SM(2,
569 AR5K_PHY_ADC_CTL_INBUFGAIN_OFF) |
570 AR5K_REG_SM(2,
571 AR5K_PHY_ADC_CTL_INBUFGAIN_ON) |
572 AR5K_PHY_ADC_CTL_PWD_DAC_OFF |
573 AR5K_PHY_ADC_CTL_PWD_ADC_OFF),
574 AR5K_PHY_ADC_CTL);
575
576
577
578 /* Disable barker RSSI threshold */
579 AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_DAG_CCK_CTL,
580 AR5K_PHY_DAG_CCK_CTL_EN_RSSI_THR);
581
582 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DAG_CCK_CTL,
583 AR5K_PHY_DAG_CCK_CTL_RSSI_THR, 2);
584
585 /* Set the mute mask */
586 ath5k_hw_reg_write(ah, 0x0000000f, AR5K_SEQ_MASK);
587 }
588
589 /* Clear PHY_BLUETOOTH to allow RX_CLEAR line debug */
590 if (ah->ah_phy_revision >= AR5K_SREV_PHY_5212B)
591 ath5k_hw_reg_write(ah, 0, AR5K_PHY_BLUETOOTH);
592
593 /* Enable DCU double buffering */
594 if (ah->ah_phy_revision > AR5K_SREV_PHY_5212B)
595 AR5K_REG_DISABLE_BITS(ah, AR5K_TXCFG,
596 AR5K_TXCFG_DCU_DBL_BUF_DIS);
597
598 /* Set DAC/ADC delays */
599 if (ah->ah_version == AR5K_AR5212) {
600 u32 scal;
Nick Kossifidis1889ba02009-04-30 15:55:46 -0400601 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200602 if (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4))
603 scal = AR5K_PHY_SCAL_32MHZ_2417;
Nick Kossifidis1889ba02009-04-30 15:55:46 -0400604 else if (ee->ee_is_hb63)
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200605 scal = AR5K_PHY_SCAL_32MHZ_HB63;
606 else
607 scal = AR5K_PHY_SCAL_32MHZ;
608 ath5k_hw_reg_write(ah, scal, AR5K_PHY_SCAL);
609 }
610
611 /* Set fast ADC */
612 if ((ah->ah_radio == AR5K_RF5413) ||
613 (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4))) {
614 u32 fast_adc = true;
615
616 if (channel->center_freq == 2462 ||
617 channel->center_freq == 2467)
618 fast_adc = 0;
619
620 /* Only update if needed */
621 if (ath5k_hw_reg_read(ah, AR5K_PHY_FAST_ADC) != fast_adc)
622 ath5k_hw_reg_write(ah, fast_adc,
623 AR5K_PHY_FAST_ADC);
624 }
625
626 /* Fix for first revision of the RF5112 RF chipset */
627 if (ah->ah_radio == AR5K_RF5112 &&
628 ah->ah_radio_5ghz_revision <
629 AR5K_SREV_RAD_5112A) {
630 u32 data;
631 ath5k_hw_reg_write(ah, AR5K_PHY_CCKTXCTL_WORLD,
632 AR5K_PHY_CCKTXCTL);
633 if (channel->hw_value & CHANNEL_5GHZ)
634 data = 0xffb81020;
635 else
636 data = 0xffb80d20;
637 ath5k_hw_reg_write(ah, data, AR5K_PHY_FRAME_CTL);
638 }
639
640 if (ah->ah_mac_srev < AR5K_SREV_AR5211) {
641 u32 usec_reg;
642 /* 5311 has different tx/rx latency masks
643 * from 5211, since we deal 5311 the same
644 * as 5211 when setting initvals, shift
645 * values here to their proper locations */
646 usec_reg = ath5k_hw_reg_read(ah, AR5K_USEC_5211);
647 ath5k_hw_reg_write(ah, usec_reg & (AR5K_USEC_1 |
648 AR5K_USEC_32 |
649 AR5K_USEC_TX_LATENCY_5211 |
650 AR5K_REG_SM(29,
651 AR5K_USEC_RX_LATENCY_5210)),
652 AR5K_USEC_5211);
653 /* Clear QCU/DCU clock gating register */
654 ath5k_hw_reg_write(ah, 0, AR5K_QCUDCU_CLKGT);
655 /* Set DAC/ADC delays */
656 ath5k_hw_reg_write(ah, 0x08, AR5K_PHY_SCAL);
657 /* Enable PCU FIFO corruption ECO */
658 AR5K_REG_ENABLE_BITS(ah, AR5K_DIAG_SW_5211,
659 AR5K_DIAG_SW_ECO_ENABLE);
660 }
661}
662
663static void ath5k_hw_commit_eeprom_settings(struct ath5k_hw *ah,
664 struct ieee80211_channel *channel, u8 *ant, u8 ee_mode)
665{
666 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
Nick Kossifidis8f655dd2009-03-15 22:20:35 +0200667 s16 cck_ofdm_pwr_delta;
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200668
Nick Kossifidis8f655dd2009-03-15 22:20:35 +0200669 /* Adjust power delta for channel 14 */
670 if (channel->center_freq == 2484)
671 cck_ofdm_pwr_delta =
672 ((ee->ee_cck_ofdm_power_delta -
673 ee->ee_scaled_cck_delta) * 2) / 10;
674 else
675 cck_ofdm_pwr_delta =
676 (ee->ee_cck_ofdm_power_delta * 2) / 10;
677
678 /* Set CCK to OFDM power delta on tx power
679 * adjustment register */
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200680 if (ah->ah_phy_revision >= AR5K_SREV_PHY_5212A) {
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200681 if (channel->hw_value == CHANNEL_G)
682 ath5k_hw_reg_write(ah,
Nick Kossifidis8f655dd2009-03-15 22:20:35 +0200683 AR5K_REG_SM((ee->ee_cck_ofdm_gain_delta * -1),
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200684 AR5K_PHY_TX_PWR_ADJ_CCK_GAIN_DELTA) |
685 AR5K_REG_SM((cck_ofdm_pwr_delta * -1),
686 AR5K_PHY_TX_PWR_ADJ_CCK_PCDAC_INDEX),
687 AR5K_PHY_TX_PWR_ADJ);
688 else
689 ath5k_hw_reg_write(ah, 0, AR5K_PHY_TX_PWR_ADJ);
Nick Kossifidis8f655dd2009-03-15 22:20:35 +0200690 } else {
691 /* For older revs we scale power on sw during tx power
692 * setup */
693 ah->ah_txpower.txp_cck_ofdm_pwr_delta = cck_ofdm_pwr_delta;
694 ah->ah_txpower.txp_cck_ofdm_gainf_delta =
695 ee->ee_cck_ofdm_gain_delta;
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200696 }
697
698 /* Set antenna idle switch table */
699 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_ANT_CTL,
700 AR5K_PHY_ANT_CTL_SWTABLE_IDLE,
701 (ah->ah_antenna[ee_mode][0] |
702 AR5K_PHY_ANT_CTL_TXRX_EN));
703
704 /* Set antenna switch table */
705 ath5k_hw_reg_write(ah, ah->ah_antenna[ee_mode][ant[0]],
706 AR5K_PHY_ANT_SWITCH_TABLE_0);
707 ath5k_hw_reg_write(ah, ah->ah_antenna[ee_mode][ant[1]],
708 AR5K_PHY_ANT_SWITCH_TABLE_1);
709
710 /* Noise floor threshold */
711 ath5k_hw_reg_write(ah,
712 AR5K_PHY_NF_SVAL(ee->ee_noise_floor_thr[ee_mode]),
713 AR5K_PHY_NFTHRES);
714
715 if ((channel->hw_value & CHANNEL_TURBO) &&
716 (ah->ah_ee_version >= AR5K_EEPROM_VERSION_5_0)) {
717 /* Switch settling time (Turbo) */
718 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_SETTLING,
719 AR5K_PHY_SETTLING_SWITCH,
720 ee->ee_switch_settling_turbo[ee_mode]);
721
722 /* Tx/Rx attenuation (Turbo) */
723 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_GAIN,
724 AR5K_PHY_GAIN_TXRX_ATTEN,
725 ee->ee_atn_tx_rx_turbo[ee_mode]);
726
727 /* ADC/PGA desired size (Turbo) */
728 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DESIRED_SIZE,
729 AR5K_PHY_DESIRED_SIZE_ADC,
730 ee->ee_adc_desired_size_turbo[ee_mode]);
731
732 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DESIRED_SIZE,
733 AR5K_PHY_DESIRED_SIZE_PGA,
734 ee->ee_pga_desired_size_turbo[ee_mode]);
735
736 /* Tx/Rx margin (Turbo) */
737 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_GAIN_2GHZ,
738 AR5K_PHY_GAIN_2GHZ_MARGIN_TXRX,
739 ee->ee_margin_tx_rx_turbo[ee_mode]);
740
741 } else {
742 /* Switch settling time */
743 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_SETTLING,
744 AR5K_PHY_SETTLING_SWITCH,
745 ee->ee_switch_settling[ee_mode]);
746
747 /* Tx/Rx attenuation */
748 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_GAIN,
749 AR5K_PHY_GAIN_TXRX_ATTEN,
750 ee->ee_atn_tx_rx[ee_mode]);
751
752 /* ADC/PGA desired size */
753 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DESIRED_SIZE,
754 AR5K_PHY_DESIRED_SIZE_ADC,
755 ee->ee_adc_desired_size[ee_mode]);
756
757 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DESIRED_SIZE,
758 AR5K_PHY_DESIRED_SIZE_PGA,
759 ee->ee_pga_desired_size[ee_mode]);
760
761 /* Tx/Rx margin */
762 if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_1)
763 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_GAIN_2GHZ,
764 AR5K_PHY_GAIN_2GHZ_MARGIN_TXRX,
765 ee->ee_margin_tx_rx[ee_mode]);
766 }
767
768 /* XPA delays */
769 ath5k_hw_reg_write(ah,
770 (ee->ee_tx_end2xpa_disable[ee_mode] << 24) |
771 (ee->ee_tx_end2xpa_disable[ee_mode] << 16) |
772 (ee->ee_tx_frm2xpa_enable[ee_mode] << 8) |
773 (ee->ee_tx_frm2xpa_enable[ee_mode]), AR5K_PHY_RF_CTL4);
774
775 /* XLNA delay */
776 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_RF_CTL3,
777 AR5K_PHY_RF_CTL3_TXE2XLNA_ON,
778 ee->ee_tx_end2xlna_enable[ee_mode]);
779
780 /* Thresh64 (ANI) */
781 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_NF,
782 AR5K_PHY_NF_THRESH62,
783 ee->ee_thr_62[ee_mode]);
784
785
786 /* False detect backoff for channels
787 * that have spur noise. Write the new
788 * cyclic power RSSI threshold. */
789 if (ath5k_hw_chan_has_spur_noise(ah, channel))
790 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_OFDM_SELFCORR,
791 AR5K_PHY_OFDM_SELFCORR_CYPWR_THR1,
792 AR5K_INIT_CYCRSSI_THR1 +
793 ee->ee_false_detect[ee_mode]);
794 else
795 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_OFDM_SELFCORR,
796 AR5K_PHY_OFDM_SELFCORR_CYPWR_THR1,
797 AR5K_INIT_CYCRSSI_THR1);
798
799 /* I/Q correction
800 * TODO: Per channel i/q infos ? */
801 AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ,
802 AR5K_PHY_IQ_CORR_ENABLE |
803 (ee->ee_i_cal[ee_mode] << AR5K_PHY_IQ_CORR_Q_I_COFF_S) |
804 ee->ee_q_cal[ee_mode]);
805
806 /* Heavy clipping -disable for now */
807 if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_5_1)
808 ath5k_hw_reg_write(ah, 0, AR5K_PHY_HEAVY_CLIP_ENABLE);
809
810 return;
811}
812
813/*
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300814 * Main reset function
815 */
Johannes Berg05c914f2008-09-11 00:01:58 +0200816int ath5k_hw_reset(struct ath5k_hw *ah, enum nl80211_iftype op_mode,
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300817 struct ieee80211_channel *channel, bool change_channel)
818{
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200819 u32 s_seq[10], s_ant, s_led[3], staid1_flags, tsf_up, tsf_lo;
820 u32 phy_tst1;
821 u8 mode, freq, ee_mode, ant[2];
822 int i, ret;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300823
824 ATH5K_TRACE(ah->ah_sc);
825
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300826 s_ant = 0;
827 ee_mode = 0;
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200828 staid1_flags = 0;
829 tsf_up = 0;
830 tsf_lo = 0;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300831 freq = 0;
832 mode = 0;
833
834 /*
835 * Save some registers before a reset
836 */
837 /*DCU/Antenna selection not available on 5210*/
838 if (ah->ah_version != AR5K_AR5210) {
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300839
840 switch (channel->hw_value & CHANNEL_MODES) {
841 case CHANNEL_A:
842 mode = AR5K_MODE_11A;
843 freq = AR5K_INI_RFGAIN_5GHZ;
844 ee_mode = AR5K_EEPROM_MODE_11A;
845 break;
846 case CHANNEL_G:
847 mode = AR5K_MODE_11G;
848 freq = AR5K_INI_RFGAIN_2GHZ;
849 ee_mode = AR5K_EEPROM_MODE_11G;
850 break;
851 case CHANNEL_B:
852 mode = AR5K_MODE_11B;
853 freq = AR5K_INI_RFGAIN_2GHZ;
854 ee_mode = AR5K_EEPROM_MODE_11B;
855 break;
856 case CHANNEL_T:
857 mode = AR5K_MODE_11A_TURBO;
858 freq = AR5K_INI_RFGAIN_5GHZ;
859 ee_mode = AR5K_EEPROM_MODE_11A;
860 break;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300861 case CHANNEL_TG:
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200862 if (ah->ah_version == AR5K_AR5211) {
863 ATH5K_ERR(ah->ah_sc,
864 "TurboG mode not available on 5211");
865 return -EINVAL;
866 }
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300867 mode = AR5K_MODE_11G_TURBO;
868 freq = AR5K_INI_RFGAIN_2GHZ;
869 ee_mode = AR5K_EEPROM_MODE_11G;
870 break;
871 case CHANNEL_XR:
872 if (ah->ah_version == AR5K_AR5211) {
873 ATH5K_ERR(ah->ah_sc,
874 "XR mode not available on 5211");
875 return -EINVAL;
876 }
877 mode = AR5K_MODE_XR;
878 freq = AR5K_INI_RFGAIN_5GHZ;
879 ee_mode = AR5K_EEPROM_MODE_11A;
880 break;
881 default:
882 ATH5K_ERR(ah->ah_sc,
883 "invalid channel: %d\n", channel->center_freq);
884 return -EINVAL;
885 }
886
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200887 if (change_channel) {
888 /*
889 * Save frame sequence count
890 * For revs. after Oahu, only save
891 * seq num for DCU 0 (Global seq num)
892 */
893 if (ah->ah_mac_srev < AR5K_SREV_AR5211) {
894
895 for (i = 0; i < 10; i++)
896 s_seq[i] = ath5k_hw_reg_read(ah,
897 AR5K_QUEUE_DCU_SEQNUM(i));
898
899 } else {
900 s_seq[0] = ath5k_hw_reg_read(ah,
901 AR5K_QUEUE_DCU_SEQNUM(0));
902 }
903
904 /* TSF accelerates on AR5211 durring reset
905 * As a workaround save it here and restore
906 * it later so that it's back in time after
907 * reset. This way it'll get re-synced on the
908 * next beacon without breaking ad-hoc.
909 *
910 * On AR5212 TSF is almost preserved across a
911 * reset so it stays back in time anyway and
912 * we don't have to save/restore it.
913 *
914 * XXX: Since this breaks power saving we have
915 * to disable power saving until we receive the
916 * next beacon, so we can resync beacon timers */
917 if (ah->ah_version == AR5K_AR5211) {
918 tsf_up = ath5k_hw_reg_read(ah, AR5K_TSF_U32);
919 tsf_lo = ath5k_hw_reg_read(ah, AR5K_TSF_L32);
920 }
921 }
922
923 /* Save default antenna */
924 s_ant = ath5k_hw_reg_read(ah, AR5K_DEFAULT_ANTENNA);
925
926 if (ah->ah_version == AR5K_AR5212) {
927 /* Restore normal 32/40MHz clock operation
928 * to avoid register access delay on certain
929 * PHY registers */
930 ath5k_hw_set_sleep_clock(ah, false);
931
932 /* Since we are going to write rf buffer
933 * check if we have any pending gain_F
934 * optimization settings */
935 if (change_channel && ah->ah_rf_banks != NULL)
936 ath5k_hw_gainf_calibrate(ah);
937 }
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300938 }
939
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200940 /*GPIOs*/
941 s_led[0] = ath5k_hw_reg_read(ah, AR5K_PCICFG) &
942 AR5K_PCICFG_LEDSTATE;
943 s_led[1] = ath5k_hw_reg_read(ah, AR5K_GPIOCR);
944 s_led[2] = ath5k_hw_reg_read(ah, AR5K_GPIODO);
Nick Kossifidisa406c132009-02-09 06:08:51 +0200945
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200946 /* AR5K_STA_ID1 flags, only preserve antenna
947 * settings and ack/cts rate mode */
948 staid1_flags = ath5k_hw_reg_read(ah, AR5K_STA_ID1) &
949 (AR5K_STA_ID1_DEFAULT_ANTENNA |
950 AR5K_STA_ID1_DESC_ANTENNA |
951 AR5K_STA_ID1_RTS_DEF_ANTENNA |
952 AR5K_STA_ID1_ACKCTS_6MB |
953 AR5K_STA_ID1_BASE_RATE_11B |
954 AR5K_STA_ID1_SELFGEN_DEF_ANT);
955
956 /* Wakeup the device */
957 ret = ath5k_hw_nic_wakeup(ah, channel->hw_value, false);
958 if (ret)
959 return ret;
960
961 /*
962 * Initialize operating mode
963 */
964 ah->ah_op_mode = op_mode;
965
966 /* PHY access enable */
967 if (ah->ah_mac_srev >= AR5K_SREV_AR5211)
968 ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ, AR5K_PHY(0));
969 else
970 ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ | 0x40,
971 AR5K_PHY(0));
972
973 /* Write initial settings */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300974 ret = ath5k_hw_write_initvals(ah, mode, change_channel);
975 if (ret)
976 return ret;
977
978 /*
979 * 5211/5212 Specific
980 */
981 if (ah->ah_version != AR5K_AR5210) {
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200982
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300983 /*
984 * Write initial RF gain settings
985 * This should work for both 5111/5112
986 */
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200987 ret = ath5k_hw_rfgain_init(ah, freq);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300988 if (ret)
989 return ret;
990
991 mdelay(1);
992
993 /*
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200994 * Tweak initval settings for revised
995 * chipsets and add some more config
996 * bits
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300997 */
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200998 ath5k_hw_tweak_initval_settings(ah, channel);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300999
1000 /*
1001 * Set TX power (FIXME)
1002 */
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001003 ret = ath5k_hw_txpower(ah, channel, ee_mode,
Nick Kossifidisa0823812009-04-30 15:55:44 -04001004 ah->ah_txpower.txp_max_pwr / 2);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001005 if (ret)
1006 return ret;
1007
1008 /* Write rate duration table only on AR5212 and if
1009 * virtual interface has already been brought up
1010 * XXX: rethink this after new mode changes to
1011 * mac80211 are integrated */
1012 if (ah->ah_version == AR5K_AR5212 &&
1013 ah->ah_sc->vif != NULL)
1014 ath5k_hw_write_rate_duration(ah, mode);
1015
1016 /*
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001017 * Write RF buffer
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001018 */
Nick Kossifidis8892e4e2009-02-09 06:06:34 +02001019 ret = ath5k_hw_rfregs_init(ah, channel, mode);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001020 if (ret)
1021 return ret;
1022
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001023
1024 /* Write OFDM timings on 5212*/
1025 if (ah->ah_version == AR5K_AR5212 &&
1026 channel->hw_value & CHANNEL_OFDM) {
1027 ret = ath5k_hw_write_ofdm_timings(ah, channel);
1028 if (ret)
1029 return ret;
1030 }
1031
1032 /*Enable/disable 802.11b mode on 5111
1033 (enable 2111 frequency converter + CCK)*/
1034 if (ah->ah_radio == AR5K_RF5111) {
1035 if (mode == AR5K_MODE_11B)
1036 AR5K_REG_ENABLE_BITS(ah, AR5K_TXCFG,
1037 AR5K_TXCFG_B_MODE);
1038 else
1039 AR5K_REG_DISABLE_BITS(ah, AR5K_TXCFG,
1040 AR5K_TXCFG_B_MODE);
1041 }
1042
1043 /*
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001044 * In case a fixed antenna was set as default
1045 * write the same settings on both AR5K_PHY_ANT_SWITCH_TABLE
1046 * registers.
1047 */
1048 if (s_ant != 0) {
1049 if (s_ant == AR5K_ANT_FIXED_A) /* 1 - Main */
1050 ant[0] = ant[1] = AR5K_ANT_FIXED_A;
1051 else /* 2 - Aux */
1052 ant[0] = ant[1] = AR5K_ANT_FIXED_B;
1053 } else {
1054 ant[0] = AR5K_ANT_FIXED_A;
1055 ant[1] = AR5K_ANT_FIXED_B;
1056 }
1057
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001058 /* Commit values from EEPROM */
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001059 ath5k_hw_commit_eeprom_settings(ah, channel, ant, ee_mode);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001060
1061 } else {
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001062 /*
1063 * For 5210 we do all initialization using
1064 * initvals, so we don't have to modify
1065 * any settings (5210 also only supports
1066 * a/aturbo modes)
1067 */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001068 mdelay(1);
1069 /* Disable phy and wait */
1070 ath5k_hw_reg_write(ah, AR5K_PHY_ACT_DISABLE, AR5K_PHY_ACT);
1071 mdelay(1);
1072 }
1073
1074 /*
1075 * Restore saved values
1076 */
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001077
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001078 /*DCU/Antenna selection not available on 5210*/
1079 if (ah->ah_version != AR5K_AR5210) {
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001080
1081 if (change_channel) {
1082 if (ah->ah_mac_srev < AR5K_SREV_AR5211) {
1083 for (i = 0; i < 10; i++)
1084 ath5k_hw_reg_write(ah, s_seq[i],
1085 AR5K_QUEUE_DCU_SEQNUM(i));
1086 } else {
1087 ath5k_hw_reg_write(ah, s_seq[0],
1088 AR5K_QUEUE_DCU_SEQNUM(0));
1089 }
1090
1091
1092 if (ah->ah_version == AR5K_AR5211) {
1093 ath5k_hw_reg_write(ah, tsf_up, AR5K_TSF_U32);
1094 ath5k_hw_reg_write(ah, tsf_lo, AR5K_TSF_L32);
1095 }
1096 }
1097
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001098 ath5k_hw_reg_write(ah, s_ant, AR5K_DEFAULT_ANTENNA);
1099 }
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001100
1101 /* Ledstate */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001102 AR5K_REG_ENABLE_BITS(ah, AR5K_PCICFG, s_led[0]);
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001103
1104 /* Gpio settings */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001105 ath5k_hw_reg_write(ah, s_led[1], AR5K_GPIOCR);
1106 ath5k_hw_reg_write(ah, s_led[2], AR5K_GPIODO);
1107
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001108 /* Restore sta_id flags and preserve our mac address*/
1109 ath5k_hw_reg_write(ah, AR5K_LOW_ID(ah->ah_sta_id),
1110 AR5K_STA_ID0);
1111 ath5k_hw_reg_write(ah, staid1_flags | AR5K_HIGH_ID(ah->ah_sta_id),
1112 AR5K_STA_ID1);
1113
1114
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001115 /*
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001116 * Configure PCU
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001117 */
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001118
1119 /* Restore bssid and bssid mask */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001120 /* XXX: add ah->aid once mac80211 gives this to us */
1121 ath5k_hw_set_associd(ah, ah->ah_bssid, 0);
1122
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001123 /* Set PCU config */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001124 ath5k_hw_set_opmode(ah);
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001125
1126 /* Clear any pending interrupts
1127 * PISR/SISR Not available on 5210 */
1128 if (ah->ah_version != AR5K_AR5210)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001129 ath5k_hw_reg_write(ah, 0xffffffff, AR5K_PISR);
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001130
1131 /* Set RSSI/BRSSI thresholds
1132 *
1133 * Note: If we decide to set this value
1134 * dynamicaly, have in mind that when AR5K_RSSI_THR
1135 * register is read it might return 0x40 if we haven't
1136 * wrote anything to it plus BMISS RSSI threshold is zeroed.
1137 * So doing a save/restore procedure here isn't the right
1138 * choice. Instead store it on ath5k_hw */
1139 ath5k_hw_reg_write(ah, (AR5K_TUNE_RSSI_THRES |
1140 AR5K_TUNE_BMISS_THRES <<
1141 AR5K_RSSI_THR_BMISS_S),
1142 AR5K_RSSI_THR);
1143
1144 /* MIC QoS support */
1145 if (ah->ah_mac_srev >= AR5K_SREV_AR2413) {
1146 ath5k_hw_reg_write(ah, 0x000100aa, AR5K_MIC_QOS_CTL);
1147 ath5k_hw_reg_write(ah, 0x00003210, AR5K_MIC_QOS_SEL);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001148 }
1149
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001150 /* QoS NOACK Policy */
1151 if (ah->ah_version == AR5K_AR5212) {
1152 ath5k_hw_reg_write(ah,
1153 AR5K_REG_SM(2, AR5K_QOS_NOACK_2BIT_VALUES) |
1154 AR5K_REG_SM(5, AR5K_QOS_NOACK_BIT_OFFSET) |
1155 AR5K_REG_SM(0, AR5K_QOS_NOACK_BYTE_OFFSET),
1156 AR5K_QOS_NOACK);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001157 }
1158
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001159
1160 /*
1161 * Configure PHY
1162 */
1163
1164 /* Set channel on PHY */
1165 ret = ath5k_hw_channel(ah, channel);
1166 if (ret)
1167 return ret;
1168
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001169 /*
1170 * Enable the PHY and wait until completion
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001171 * This includes BaseBand and Synthesizer
1172 * activation.
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001173 */
1174 ath5k_hw_reg_write(ah, AR5K_PHY_ACT_ENABLE, AR5K_PHY_ACT);
1175
1176 /*
1177 * On 5211+ read activation -> rx delay
1178 * and use it.
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001179 *
1180 * TODO: Half/quarter rate support
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001181 */
1182 if (ah->ah_version != AR5K_AR5210) {
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001183 u32 delay;
1184 delay = ath5k_hw_reg_read(ah, AR5K_PHY_RX_DELAY) &
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001185 AR5K_PHY_RX_DELAY_M;
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001186 delay = (channel->hw_value & CHANNEL_CCK) ?
1187 ((delay << 2) / 22) : (delay / 10);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001188
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001189 udelay(100 + (2 * delay));
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001190 } else {
1191 mdelay(1);
1192 }
1193
1194 /*
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001195 * Perform ADC test to see if baseband is ready
1196 * Set tx hold and check adc test register
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001197 */
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001198 phy_tst1 = ath5k_hw_reg_read(ah, AR5K_PHY_TST1);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001199 ath5k_hw_reg_write(ah, AR5K_PHY_TST1_TXHOLD, AR5K_PHY_TST1);
1200 for (i = 0; i <= 20; i++) {
1201 if (!(ath5k_hw_reg_read(ah, AR5K_PHY_ADC_TEST) & 0x10))
1202 break;
1203 udelay(200);
1204 }
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001205 ath5k_hw_reg_write(ah, phy_tst1, AR5K_PHY_TST1);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001206
1207 /*
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001208 * Start automatic gain control calibration
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001209 *
1210 * During AGC calibration RX path is re-routed to
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001211 * a power detector so we don't receive anything.
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001212 *
1213 * This method is used to calibrate some static offsets
1214 * used together with on-the fly I/Q calibration (the
1215 * one performed via ath5k_hw_phy_calibrate), that doesn't
1216 * interrupt rx path.
1217 *
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001218 * While rx path is re-routed to the power detector we also
1219 * start a noise floor calibration, to measure the
1220 * card's noise floor (the noise we measure when we are not
1221 * transmiting or receiving anything).
1222 *
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001223 * If we are in a noisy environment AGC calibration may time
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001224 * out and/or noise floor calibration might timeout.
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001225 */
1226 AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGCCTL,
1227 AR5K_PHY_AGCCTL_CAL);
1228
1229 /* At the same time start I/Q calibration for QAM constellation
1230 * -no need for CCK- */
1231 ah->ah_calibration = false;
1232 if (!(mode == AR5K_MODE_11B)) {
1233 ah->ah_calibration = true;
1234 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_IQ,
1235 AR5K_PHY_IQ_CAL_NUM_LOG_MAX, 15);
1236 AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ,
1237 AR5K_PHY_IQ_RUN);
1238 }
1239
1240 /* Wait for gain calibration to finish (we check for I/Q calibration
1241 * during ath5k_phy_calibrate) */
1242 if (ath5k_hw_register_timeout(ah, AR5K_PHY_AGCCTL,
1243 AR5K_PHY_AGCCTL_CAL, 0, false)) {
1244 ATH5K_ERR(ah->ah_sc, "gain calibration timeout (%uMHz)\n",
1245 channel->center_freq);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001246 }
1247
1248 /*
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001249 * If we run NF calibration before AGC, it always times out.
1250 * Binary HAL starts NF and AGC calibration at the same time
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001251 * and only waits for AGC to finish. Also if AGC or NF cal.
1252 * times out, reset doesn't fail on binary HAL. I believe
1253 * that's wrong because since rx path is routed to a detector,
1254 * if cal. doesn't finish we won't have RX. Sam's HAL for AR5210/5211
1255 * enables noise floor calibration after offset calibration and if noise
1256 * floor calibration fails, reset fails. I believe that's
1257 * a better approach, we just need to find a polling interval
1258 * that suits best, even if reset continues we need to make
1259 * sure that rx path is ready.
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001260 */
Felix Fietkau8b0162a2008-11-03 11:27:38 +01001261 ath5k_hw_noise_floor_calibration(ah, channel->center_freq);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001262
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001263
1264 /*
1265 * Configure QCUs/DCUs
1266 */
1267
1268 /* TODO: HW Compression support for data queues */
1269 /* TODO: Burst prefetch for data queues */
1270
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001271 /*
1272 * Reset queues and start beacon timers at the end of the reset routine
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001273 * This also sets QCU mask on each DCU for 1:1 qcu to dcu mapping
1274 * Note: If we want we can assign multiple qcus on one dcu.
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001275 */
1276 for (i = 0; i < ah->ah_capabilities.cap_queues.q_tx_num; i++) {
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001277 ret = ath5k_hw_reset_tx_queue(ah, i);
1278 if (ret) {
1279 ATH5K_ERR(ah->ah_sc,
1280 "failed to reset TX queue #%d\n", i);
1281 return ret;
1282 }
1283 }
1284
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001285
1286 /*
1287 * Configure DMA/Interrupts
1288 */
1289
1290 /*
1291 * Set Rx/Tx DMA Configuration
1292 *
1293 * Set standard DMA size (128). Note that
1294 * a DMA size of 512 causes rx overruns and tx errors
1295 * on pci-e cards (tested on 5424 but since rx overruns
1296 * also occur on 5416/5418 with madwifi we set 128
1297 * for all PCI-E cards to be safe).
1298 *
1299 * XXX: need to check 5210 for this
1300 * TODO: Check out tx triger level, it's always 64 on dumps but I
1301 * guess we can tweak it and see how it goes ;-)
1302 */
1303 if (ah->ah_version != AR5K_AR5210) {
1304 AR5K_REG_WRITE_BITS(ah, AR5K_TXCFG,
1305 AR5K_TXCFG_SDMAMR, AR5K_DMASIZE_128B);
1306 AR5K_REG_WRITE_BITS(ah, AR5K_RXCFG,
1307 AR5K_RXCFG_SDMAMW, AR5K_DMASIZE_128B);
1308 }
1309
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001310 /* Pre-enable interrupts on 5211/5212*/
1311 if (ah->ah_version != AR5K_AR5210)
Nick Kossifidis4c674c62008-10-26 20:40:25 +02001312 ath5k_hw_set_imr(ah, ah->ah_imr);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001313
1314 /*
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001315 * Setup RFKill interrupt if rfkill flag is set on eeprom.
1316 * TODO: Use gpio pin and polarity infos from eeprom
1317 * TODO: Handle this in ath5k_intr because it'll result
1318 * a nasty interrupt storm.
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001319 */
1320#if 0
1321 if (AR5K_EEPROM_HDR_RFKILL(ah->ah_capabilities.cap_eeprom.ee_header)) {
1322 ath5k_hw_set_gpio_input(ah, 0);
1323 ah->ah_gpio[0] = ath5k_hw_get_gpio(ah, 0);
1324 if (ah->ah_gpio[0] == 0)
1325 ath5k_hw_set_gpio_intr(ah, 0, 1);
1326 else
1327 ath5k_hw_set_gpio_intr(ah, 0, 0);
1328 }
1329#endif
1330
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001331 /* Enable 32KHz clock function for AR5212+ chips
1332 * Set clocks to 32KHz operation and use an
1333 * external 32KHz crystal when sleeping if one
1334 * exists */
1335 if (ah->ah_version == AR5K_AR5212)
1336 ath5k_hw_set_sleep_clock(ah, true);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001337
1338 /*
1339 * Disable beacons and reset the register
1340 */
1341 AR5K_REG_DISABLE_BITS(ah, AR5K_BEACON, AR5K_BEACON_ENABLE |
1342 AR5K_BEACON_RESET_TSF);
1343
1344 return 0;
1345}
1346
1347#undef _ATH5K_RESET