blob: 86733fdb4774372eb5753c46f8fa2d3d8e6ae322 [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 */
Forrest Zhanga54be5d2009-05-13 11:14:39 -040029#include <linux/log2.h>
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 */
Forrest Zhanga54be5d2009-05-13 11:14:39 -040071 coef_exp = ilog2(coef_scaled);
Nick Kossifidise8f055f2009-02-09 06:12:58 +020072
73 /* Doesn't make sense if it's zero*/
Forrest Zhanga54be5d2009-05-13 11:14:39 -040074 if (!coef_scaled || !coef_exp)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030075 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:
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300293 ath5k_hw_reg_write(ah, staid, AR5K_STA_ID1);
294
295 return 0;
296}
297
298/*
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200299 * Bring up MAC + PHY Chips and program PLL
300 * TODO: Half/Quarter rate support
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300301 */
302int ath5k_hw_nic_wakeup(struct ath5k_hw *ah, int flags, bool initial)
303{
304 struct pci_dev *pdev = ah->ah_sc->pdev;
305 u32 turbo, mode, clock, bus_flags;
306 int ret;
307
308 turbo = 0;
309 mode = 0;
310 clock = 0;
311
312 ATH5K_TRACE(ah->ah_sc);
313
314 /* Wakeup the device */
315 ret = ath5k_hw_set_power(ah, AR5K_PM_AWAKE, true, 0);
316 if (ret) {
317 ATH5K_ERR(ah->ah_sc, "failed to wakeup the MAC Chip\n");
318 return ret;
319 }
320
321 if (ah->ah_version != AR5K_AR5210) {
322 /*
323 * Get channel mode flags
324 */
325
326 if (ah->ah_radio >= AR5K_RF5112) {
327 mode = AR5K_PHY_MODE_RAD_RF5112;
328 clock = AR5K_PHY_PLL_RF5112;
329 } else {
330 mode = AR5K_PHY_MODE_RAD_RF5111; /*Zero*/
331 clock = AR5K_PHY_PLL_RF5111; /*Zero*/
332 }
333
334 if (flags & CHANNEL_2GHZ) {
335 mode |= AR5K_PHY_MODE_FREQ_2GHZ;
336 clock |= AR5K_PHY_PLL_44MHZ;
337
338 if (flags & CHANNEL_CCK) {
339 mode |= AR5K_PHY_MODE_MOD_CCK;
340 } else if (flags & CHANNEL_OFDM) {
341 /* XXX Dynamic OFDM/CCK is not supported by the
342 * AR5211 so we set MOD_OFDM for plain g (no
343 * CCK headers) operation. We need to test
344 * this, 5211 might support ofdm-only g after
345 * all, there are also initial register values
346 * in the code for g mode (see initvals.c). */
347 if (ah->ah_version == AR5K_AR5211)
348 mode |= AR5K_PHY_MODE_MOD_OFDM;
349 else
350 mode |= AR5K_PHY_MODE_MOD_DYN;
351 } else {
352 ATH5K_ERR(ah->ah_sc,
353 "invalid radio modulation mode\n");
354 return -EINVAL;
355 }
356 } else if (flags & CHANNEL_5GHZ) {
357 mode |= AR5K_PHY_MODE_FREQ_5GHZ;
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200358
359 if (ah->ah_radio == AR5K_RF5413)
Pavel Roskin807e3732009-03-27 17:47:27 -0400360 clock = AR5K_PHY_PLL_40MHZ_5413;
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200361 else
362 clock |= AR5K_PHY_PLL_40MHZ;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300363
364 if (flags & CHANNEL_OFDM)
365 mode |= AR5K_PHY_MODE_MOD_OFDM;
366 else {
367 ATH5K_ERR(ah->ah_sc,
368 "invalid radio modulation mode\n");
369 return -EINVAL;
370 }
371 } else {
372 ATH5K_ERR(ah->ah_sc, "invalid radio frequency mode\n");
373 return -EINVAL;
374 }
375
376 if (flags & CHANNEL_TURBO)
377 turbo = AR5K_PHY_TURBO_MODE | AR5K_PHY_TURBO_SHORT;
378 } else { /* Reset the device */
379
380 /* ...enable Atheros turbo mode if requested */
381 if (flags & CHANNEL_TURBO)
382 ath5k_hw_reg_write(ah, AR5K_PHY_TURBO_MODE,
383 AR5K_PHY_TURBO);
384 }
385
386 /* reseting PCI on PCI-E cards results card to hang
387 * and always return 0xffff... so we ingore that flag
388 * for PCI-E cards */
389 bus_flags = (pdev->is_pcie) ? 0 : AR5K_RESET_CTL_PCI;
390
391 /* Reset chipset */
Nick Kossifidis84e463f2008-09-17 03:33:19 +0300392 if (ah->ah_version == AR5K_AR5210) {
393 ret = ath5k_hw_nic_reset(ah, AR5K_RESET_CTL_PCU |
394 AR5K_RESET_CTL_MAC | AR5K_RESET_CTL_DMA |
395 AR5K_RESET_CTL_PHY | AR5K_RESET_CTL_PCI);
396 mdelay(2);
397 } else {
398 ret = ath5k_hw_nic_reset(ah, AR5K_RESET_CTL_PCU |
399 AR5K_RESET_CTL_BASEBAND | bus_flags);
400 }
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300401 if (ret) {
402 ATH5K_ERR(ah->ah_sc, "failed to reset the MAC Chip\n");
403 return -EIO;
404 }
405
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300406 /* ...wakeup again!*/
407 ret = ath5k_hw_set_power(ah, AR5K_PM_AWAKE, true, 0);
408 if (ret) {
409 ATH5K_ERR(ah->ah_sc, "failed to resume the MAC Chip\n");
410 return ret;
411 }
412
413 /* ...final warm reset */
414 if (ath5k_hw_nic_reset(ah, 0)) {
415 ATH5K_ERR(ah->ah_sc, "failed to warm reset the MAC Chip\n");
416 return -EIO;
417 }
418
419 if (ah->ah_version != AR5K_AR5210) {
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300420
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200421 /* ...update PLL if needed */
422 if (ath5k_hw_reg_read(ah, AR5K_PHY_PLL) != clock) {
423 ath5k_hw_reg_write(ah, clock, AR5K_PHY_PLL);
424 udelay(300);
425 }
426
427 /* ...set the PHY operating mode */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300428 ath5k_hw_reg_write(ah, mode, AR5K_PHY_MODE);
429 ath5k_hw_reg_write(ah, turbo, AR5K_PHY_TURBO);
430 }
431
432 return 0;
433}
434
435/*
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200436 * If there is an external 32KHz crystal available, use it
437 * as ref. clock instead of 32/40MHz clock and baseband clocks
438 * to save power during sleep or restore normal 32/40MHz
439 * operation.
440 *
441 * XXX: When operating on 32KHz certain PHY registers (27 - 31,
442 * 123 - 127) require delay on access.
443 */
444static void ath5k_hw_set_sleep_clock(struct ath5k_hw *ah, bool enable)
445{
446 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
447 u32 scal, spending, usec32;
448
449 /* Only set 32KHz settings if we have an external
450 * 32KHz crystal present */
451 if ((AR5K_EEPROM_HAS32KHZCRYSTAL(ee->ee_misc1) ||
452 AR5K_EEPROM_HAS32KHZCRYSTAL_OLD(ee->ee_misc1)) &&
453 enable) {
454
455 /* 1 usec/cycle */
456 AR5K_REG_WRITE_BITS(ah, AR5K_USEC_5211, AR5K_USEC_32, 1);
457 /* Set up tsf increment on each cycle */
458 AR5K_REG_WRITE_BITS(ah, AR5K_TSF_PARM, AR5K_TSF_PARM_INC, 61);
459
460 /* Set baseband sleep control registers
461 * and sleep control rate */
462 ath5k_hw_reg_write(ah, 0x1f, AR5K_PHY_SCR);
463
464 if ((ah->ah_radio == AR5K_RF5112) ||
465 (ah->ah_radio == AR5K_RF5413) ||
466 (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4)))
467 spending = 0x14;
468 else
469 spending = 0x18;
470 ath5k_hw_reg_write(ah, spending, AR5K_PHY_SPENDING);
471
472 if ((ah->ah_radio == AR5K_RF5112) ||
473 (ah->ah_radio == AR5K_RF5413) ||
474 (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4))) {
475 ath5k_hw_reg_write(ah, 0x26, AR5K_PHY_SLMT);
476 ath5k_hw_reg_write(ah, 0x0d, AR5K_PHY_SCAL);
477 ath5k_hw_reg_write(ah, 0x07, AR5K_PHY_SCLOCK);
478 ath5k_hw_reg_write(ah, 0x3f, AR5K_PHY_SDELAY);
479 AR5K_REG_WRITE_BITS(ah, AR5K_PCICFG,
480 AR5K_PCICFG_SLEEP_CLOCK_RATE, 0x02);
481 } else {
482 ath5k_hw_reg_write(ah, 0x0a, AR5K_PHY_SLMT);
483 ath5k_hw_reg_write(ah, 0x0c, AR5K_PHY_SCAL);
484 ath5k_hw_reg_write(ah, 0x03, AR5K_PHY_SCLOCK);
485 ath5k_hw_reg_write(ah, 0x20, AR5K_PHY_SDELAY);
486 AR5K_REG_WRITE_BITS(ah, AR5K_PCICFG,
487 AR5K_PCICFG_SLEEP_CLOCK_RATE, 0x03);
488 }
489
490 /* Enable sleep clock operation */
491 AR5K_REG_ENABLE_BITS(ah, AR5K_PCICFG,
492 AR5K_PCICFG_SLEEP_CLOCK_EN);
493
494 } else {
495
496 /* Disable sleep clock operation and
497 * restore default parameters */
498 AR5K_REG_DISABLE_BITS(ah, AR5K_PCICFG,
499 AR5K_PCICFG_SLEEP_CLOCK_EN);
500
501 AR5K_REG_WRITE_BITS(ah, AR5K_PCICFG,
502 AR5K_PCICFG_SLEEP_CLOCK_RATE, 0);
503
504 ath5k_hw_reg_write(ah, 0x1f, AR5K_PHY_SCR);
505 ath5k_hw_reg_write(ah, AR5K_PHY_SLMT_32MHZ, AR5K_PHY_SLMT);
506
507 if (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4))
508 scal = AR5K_PHY_SCAL_32MHZ_2417;
Nick Kossifidis1889ba02009-04-30 15:55:46 -0400509 else if (ee->ee_is_hb63)
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200510 scal = AR5K_PHY_SCAL_32MHZ_HB63;
511 else
512 scal = AR5K_PHY_SCAL_32MHZ;
513 ath5k_hw_reg_write(ah, scal, AR5K_PHY_SCAL);
514
515 ath5k_hw_reg_write(ah, AR5K_PHY_SCLOCK_32MHZ, AR5K_PHY_SCLOCK);
516 ath5k_hw_reg_write(ah, AR5K_PHY_SDELAY_32MHZ, AR5K_PHY_SDELAY);
517
518 if ((ah->ah_radio == AR5K_RF5112) ||
519 (ah->ah_radio == AR5K_RF5413) ||
520 (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4)))
521 spending = 0x14;
522 else
523 spending = 0x18;
524 ath5k_hw_reg_write(ah, spending, AR5K_PHY_SPENDING);
525
526 if ((ah->ah_radio == AR5K_RF5112) ||
527 (ah->ah_radio == AR5K_RF5413))
528 usec32 = 39;
529 else
530 usec32 = 31;
531 AR5K_REG_WRITE_BITS(ah, AR5K_USEC_5211, AR5K_USEC_32, usec32);
532
533 AR5K_REG_WRITE_BITS(ah, AR5K_TSF_PARM, AR5K_TSF_PARM_INC, 1);
534 }
535 return;
536}
537
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200538/* TODO: Half/Quarter rate */
539static void ath5k_hw_tweak_initval_settings(struct ath5k_hw *ah,
540 struct ieee80211_channel *channel)
541{
542 if (ah->ah_version == AR5K_AR5212 &&
543 ah->ah_phy_revision >= AR5K_SREV_PHY_5212A) {
544
545 /* Setup ADC control */
546 ath5k_hw_reg_write(ah,
547 (AR5K_REG_SM(2,
548 AR5K_PHY_ADC_CTL_INBUFGAIN_OFF) |
549 AR5K_REG_SM(2,
550 AR5K_PHY_ADC_CTL_INBUFGAIN_ON) |
551 AR5K_PHY_ADC_CTL_PWD_DAC_OFF |
552 AR5K_PHY_ADC_CTL_PWD_ADC_OFF),
553 AR5K_PHY_ADC_CTL);
554
555
556
557 /* Disable barker RSSI threshold */
558 AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_DAG_CCK_CTL,
559 AR5K_PHY_DAG_CCK_CTL_EN_RSSI_THR);
560
561 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DAG_CCK_CTL,
562 AR5K_PHY_DAG_CCK_CTL_RSSI_THR, 2);
563
564 /* Set the mute mask */
565 ath5k_hw_reg_write(ah, 0x0000000f, AR5K_SEQ_MASK);
566 }
567
568 /* Clear PHY_BLUETOOTH to allow RX_CLEAR line debug */
569 if (ah->ah_phy_revision >= AR5K_SREV_PHY_5212B)
570 ath5k_hw_reg_write(ah, 0, AR5K_PHY_BLUETOOTH);
571
572 /* Enable DCU double buffering */
573 if (ah->ah_phy_revision > AR5K_SREV_PHY_5212B)
574 AR5K_REG_DISABLE_BITS(ah, AR5K_TXCFG,
575 AR5K_TXCFG_DCU_DBL_BUF_DIS);
576
577 /* Set DAC/ADC delays */
578 if (ah->ah_version == AR5K_AR5212) {
579 u32 scal;
Nick Kossifidis1889ba02009-04-30 15:55:46 -0400580 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200581 if (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4))
582 scal = AR5K_PHY_SCAL_32MHZ_2417;
Nick Kossifidis1889ba02009-04-30 15:55:46 -0400583 else if (ee->ee_is_hb63)
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200584 scal = AR5K_PHY_SCAL_32MHZ_HB63;
585 else
586 scal = AR5K_PHY_SCAL_32MHZ;
587 ath5k_hw_reg_write(ah, scal, AR5K_PHY_SCAL);
588 }
589
590 /* Set fast ADC */
591 if ((ah->ah_radio == AR5K_RF5413) ||
592 (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4))) {
593 u32 fast_adc = true;
594
595 if (channel->center_freq == 2462 ||
596 channel->center_freq == 2467)
597 fast_adc = 0;
598
599 /* Only update if needed */
600 if (ath5k_hw_reg_read(ah, AR5K_PHY_FAST_ADC) != fast_adc)
601 ath5k_hw_reg_write(ah, fast_adc,
602 AR5K_PHY_FAST_ADC);
603 }
604
605 /* Fix for first revision of the RF5112 RF chipset */
606 if (ah->ah_radio == AR5K_RF5112 &&
607 ah->ah_radio_5ghz_revision <
608 AR5K_SREV_RAD_5112A) {
609 u32 data;
610 ath5k_hw_reg_write(ah, AR5K_PHY_CCKTXCTL_WORLD,
611 AR5K_PHY_CCKTXCTL);
612 if (channel->hw_value & CHANNEL_5GHZ)
613 data = 0xffb81020;
614 else
615 data = 0xffb80d20;
616 ath5k_hw_reg_write(ah, data, AR5K_PHY_FRAME_CTL);
617 }
618
619 if (ah->ah_mac_srev < AR5K_SREV_AR5211) {
620 u32 usec_reg;
621 /* 5311 has different tx/rx latency masks
622 * from 5211, since we deal 5311 the same
623 * as 5211 when setting initvals, shift
624 * values here to their proper locations */
625 usec_reg = ath5k_hw_reg_read(ah, AR5K_USEC_5211);
626 ath5k_hw_reg_write(ah, usec_reg & (AR5K_USEC_1 |
627 AR5K_USEC_32 |
628 AR5K_USEC_TX_LATENCY_5211 |
629 AR5K_REG_SM(29,
630 AR5K_USEC_RX_LATENCY_5210)),
631 AR5K_USEC_5211);
632 /* Clear QCU/DCU clock gating register */
633 ath5k_hw_reg_write(ah, 0, AR5K_QCUDCU_CLKGT);
634 /* Set DAC/ADC delays */
635 ath5k_hw_reg_write(ah, 0x08, AR5K_PHY_SCAL);
636 /* Enable PCU FIFO corruption ECO */
637 AR5K_REG_ENABLE_BITS(ah, AR5K_DIAG_SW_5211,
638 AR5K_DIAG_SW_ECO_ENABLE);
639 }
640}
641
642static void ath5k_hw_commit_eeprom_settings(struct ath5k_hw *ah,
643 struct ieee80211_channel *channel, u8 *ant, u8 ee_mode)
644{
645 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
Nick Kossifidis8f655dd2009-03-15 22:20:35 +0200646 s16 cck_ofdm_pwr_delta;
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200647
Nick Kossifidis8f655dd2009-03-15 22:20:35 +0200648 /* Adjust power delta for channel 14 */
649 if (channel->center_freq == 2484)
650 cck_ofdm_pwr_delta =
651 ((ee->ee_cck_ofdm_power_delta -
652 ee->ee_scaled_cck_delta) * 2) / 10;
653 else
654 cck_ofdm_pwr_delta =
655 (ee->ee_cck_ofdm_power_delta * 2) / 10;
656
657 /* Set CCK to OFDM power delta on tx power
658 * adjustment register */
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200659 if (ah->ah_phy_revision >= AR5K_SREV_PHY_5212A) {
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200660 if (channel->hw_value == CHANNEL_G)
661 ath5k_hw_reg_write(ah,
Nick Kossifidis8f655dd2009-03-15 22:20:35 +0200662 AR5K_REG_SM((ee->ee_cck_ofdm_gain_delta * -1),
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200663 AR5K_PHY_TX_PWR_ADJ_CCK_GAIN_DELTA) |
664 AR5K_REG_SM((cck_ofdm_pwr_delta * -1),
665 AR5K_PHY_TX_PWR_ADJ_CCK_PCDAC_INDEX),
666 AR5K_PHY_TX_PWR_ADJ);
667 else
668 ath5k_hw_reg_write(ah, 0, AR5K_PHY_TX_PWR_ADJ);
Nick Kossifidis8f655dd2009-03-15 22:20:35 +0200669 } else {
670 /* For older revs we scale power on sw during tx power
671 * setup */
672 ah->ah_txpower.txp_cck_ofdm_pwr_delta = cck_ofdm_pwr_delta;
673 ah->ah_txpower.txp_cck_ofdm_gainf_delta =
674 ee->ee_cck_ofdm_gain_delta;
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200675 }
676
677 /* Set antenna idle switch table */
678 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_ANT_CTL,
679 AR5K_PHY_ANT_CTL_SWTABLE_IDLE,
Nick Kossifidis2bed03e2009-04-30 15:55:49 -0400680 (ah->ah_ant_ctl[ee_mode][0] |
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200681 AR5K_PHY_ANT_CTL_TXRX_EN));
682
Nick Kossifidis2bed03e2009-04-30 15:55:49 -0400683 /* Set antenna switch tables */
684 ath5k_hw_reg_write(ah, ah->ah_ant_ctl[ee_mode][ant[0]],
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200685 AR5K_PHY_ANT_SWITCH_TABLE_0);
Nick Kossifidis2bed03e2009-04-30 15:55:49 -0400686 ath5k_hw_reg_write(ah, ah->ah_ant_ctl[ee_mode][ant[1]],
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200687 AR5K_PHY_ANT_SWITCH_TABLE_1);
688
689 /* Noise floor threshold */
690 ath5k_hw_reg_write(ah,
691 AR5K_PHY_NF_SVAL(ee->ee_noise_floor_thr[ee_mode]),
692 AR5K_PHY_NFTHRES);
693
694 if ((channel->hw_value & CHANNEL_TURBO) &&
695 (ah->ah_ee_version >= AR5K_EEPROM_VERSION_5_0)) {
696 /* Switch settling time (Turbo) */
697 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_SETTLING,
698 AR5K_PHY_SETTLING_SWITCH,
699 ee->ee_switch_settling_turbo[ee_mode]);
700
701 /* Tx/Rx attenuation (Turbo) */
702 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_GAIN,
703 AR5K_PHY_GAIN_TXRX_ATTEN,
704 ee->ee_atn_tx_rx_turbo[ee_mode]);
705
706 /* ADC/PGA desired size (Turbo) */
707 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DESIRED_SIZE,
708 AR5K_PHY_DESIRED_SIZE_ADC,
709 ee->ee_adc_desired_size_turbo[ee_mode]);
710
711 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DESIRED_SIZE,
712 AR5K_PHY_DESIRED_SIZE_PGA,
713 ee->ee_pga_desired_size_turbo[ee_mode]);
714
715 /* Tx/Rx margin (Turbo) */
716 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_GAIN_2GHZ,
717 AR5K_PHY_GAIN_2GHZ_MARGIN_TXRX,
718 ee->ee_margin_tx_rx_turbo[ee_mode]);
719
720 } else {
721 /* Switch settling time */
722 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_SETTLING,
723 AR5K_PHY_SETTLING_SWITCH,
724 ee->ee_switch_settling[ee_mode]);
725
726 /* Tx/Rx attenuation */
727 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_GAIN,
728 AR5K_PHY_GAIN_TXRX_ATTEN,
729 ee->ee_atn_tx_rx[ee_mode]);
730
731 /* ADC/PGA desired size */
732 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DESIRED_SIZE,
733 AR5K_PHY_DESIRED_SIZE_ADC,
734 ee->ee_adc_desired_size[ee_mode]);
735
736 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DESIRED_SIZE,
737 AR5K_PHY_DESIRED_SIZE_PGA,
738 ee->ee_pga_desired_size[ee_mode]);
739
740 /* Tx/Rx margin */
741 if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_1)
742 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_GAIN_2GHZ,
743 AR5K_PHY_GAIN_2GHZ_MARGIN_TXRX,
744 ee->ee_margin_tx_rx[ee_mode]);
745 }
746
747 /* XPA delays */
748 ath5k_hw_reg_write(ah,
749 (ee->ee_tx_end2xpa_disable[ee_mode] << 24) |
750 (ee->ee_tx_end2xpa_disable[ee_mode] << 16) |
751 (ee->ee_tx_frm2xpa_enable[ee_mode] << 8) |
752 (ee->ee_tx_frm2xpa_enable[ee_mode]), AR5K_PHY_RF_CTL4);
753
754 /* XLNA delay */
755 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_RF_CTL3,
756 AR5K_PHY_RF_CTL3_TXE2XLNA_ON,
757 ee->ee_tx_end2xlna_enable[ee_mode]);
758
759 /* Thresh64 (ANI) */
760 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_NF,
761 AR5K_PHY_NF_THRESH62,
762 ee->ee_thr_62[ee_mode]);
763
764
765 /* False detect backoff for channels
766 * that have spur noise. Write the new
767 * cyclic power RSSI threshold. */
768 if (ath5k_hw_chan_has_spur_noise(ah, channel))
769 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_OFDM_SELFCORR,
770 AR5K_PHY_OFDM_SELFCORR_CYPWR_THR1,
771 AR5K_INIT_CYCRSSI_THR1 +
772 ee->ee_false_detect[ee_mode]);
773 else
774 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_OFDM_SELFCORR,
775 AR5K_PHY_OFDM_SELFCORR_CYPWR_THR1,
776 AR5K_INIT_CYCRSSI_THR1);
777
778 /* I/Q correction
779 * TODO: Per channel i/q infos ? */
780 AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ,
781 AR5K_PHY_IQ_CORR_ENABLE |
782 (ee->ee_i_cal[ee_mode] << AR5K_PHY_IQ_CORR_Q_I_COFF_S) |
783 ee->ee_q_cal[ee_mode]);
784
785 /* Heavy clipping -disable for now */
786 if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_5_1)
787 ath5k_hw_reg_write(ah, 0, AR5K_PHY_HEAVY_CLIP_ENABLE);
788
789 return;
790}
791
792/*
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300793 * Main reset function
794 */
Johannes Berg05c914f2008-09-11 00:01:58 +0200795int ath5k_hw_reset(struct ath5k_hw *ah, enum nl80211_iftype op_mode,
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300796 struct ieee80211_channel *channel, bool change_channel)
797{
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200798 u32 s_seq[10], s_ant, s_led[3], staid1_flags, tsf_up, tsf_lo;
799 u32 phy_tst1;
800 u8 mode, freq, ee_mode, ant[2];
801 int i, ret;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300802
803 ATH5K_TRACE(ah->ah_sc);
804
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300805 s_ant = 0;
806 ee_mode = 0;
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200807 staid1_flags = 0;
808 tsf_up = 0;
809 tsf_lo = 0;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300810 freq = 0;
811 mode = 0;
812
813 /*
814 * Save some registers before a reset
815 */
816 /*DCU/Antenna selection not available on 5210*/
817 if (ah->ah_version != AR5K_AR5210) {
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300818
819 switch (channel->hw_value & CHANNEL_MODES) {
820 case CHANNEL_A:
821 mode = AR5K_MODE_11A;
822 freq = AR5K_INI_RFGAIN_5GHZ;
823 ee_mode = AR5K_EEPROM_MODE_11A;
824 break;
825 case CHANNEL_G:
826 mode = AR5K_MODE_11G;
827 freq = AR5K_INI_RFGAIN_2GHZ;
828 ee_mode = AR5K_EEPROM_MODE_11G;
829 break;
830 case CHANNEL_B:
831 mode = AR5K_MODE_11B;
832 freq = AR5K_INI_RFGAIN_2GHZ;
833 ee_mode = AR5K_EEPROM_MODE_11B;
834 break;
835 case CHANNEL_T:
836 mode = AR5K_MODE_11A_TURBO;
837 freq = AR5K_INI_RFGAIN_5GHZ;
838 ee_mode = AR5K_EEPROM_MODE_11A;
839 break;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300840 case CHANNEL_TG:
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200841 if (ah->ah_version == AR5K_AR5211) {
842 ATH5K_ERR(ah->ah_sc,
843 "TurboG mode not available on 5211");
844 return -EINVAL;
845 }
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300846 mode = AR5K_MODE_11G_TURBO;
847 freq = AR5K_INI_RFGAIN_2GHZ;
848 ee_mode = AR5K_EEPROM_MODE_11G;
849 break;
850 case CHANNEL_XR:
851 if (ah->ah_version == AR5K_AR5211) {
852 ATH5K_ERR(ah->ah_sc,
853 "XR mode not available on 5211");
854 return -EINVAL;
855 }
856 mode = AR5K_MODE_XR;
857 freq = AR5K_INI_RFGAIN_5GHZ;
858 ee_mode = AR5K_EEPROM_MODE_11A;
859 break;
860 default:
861 ATH5K_ERR(ah->ah_sc,
862 "invalid channel: %d\n", channel->center_freq);
863 return -EINVAL;
864 }
865
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200866 if (change_channel) {
867 /*
868 * Save frame sequence count
869 * For revs. after Oahu, only save
870 * seq num for DCU 0 (Global seq num)
871 */
872 if (ah->ah_mac_srev < AR5K_SREV_AR5211) {
873
874 for (i = 0; i < 10; i++)
875 s_seq[i] = ath5k_hw_reg_read(ah,
876 AR5K_QUEUE_DCU_SEQNUM(i));
877
878 } else {
879 s_seq[0] = ath5k_hw_reg_read(ah,
880 AR5K_QUEUE_DCU_SEQNUM(0));
881 }
882
883 /* TSF accelerates on AR5211 durring reset
884 * As a workaround save it here and restore
885 * it later so that it's back in time after
886 * reset. This way it'll get re-synced on the
887 * next beacon without breaking ad-hoc.
888 *
889 * On AR5212 TSF is almost preserved across a
890 * reset so it stays back in time anyway and
891 * we don't have to save/restore it.
892 *
893 * XXX: Since this breaks power saving we have
894 * to disable power saving until we receive the
895 * next beacon, so we can resync beacon timers */
896 if (ah->ah_version == AR5K_AR5211) {
897 tsf_up = ath5k_hw_reg_read(ah, AR5K_TSF_U32);
898 tsf_lo = ath5k_hw_reg_read(ah, AR5K_TSF_L32);
899 }
900 }
901
902 /* Save default antenna */
903 s_ant = ath5k_hw_reg_read(ah, AR5K_DEFAULT_ANTENNA);
904
905 if (ah->ah_version == AR5K_AR5212) {
906 /* Restore normal 32/40MHz clock operation
907 * to avoid register access delay on certain
908 * PHY registers */
909 ath5k_hw_set_sleep_clock(ah, false);
910
911 /* Since we are going to write rf buffer
912 * check if we have any pending gain_F
913 * optimization settings */
914 if (change_channel && ah->ah_rf_banks != NULL)
915 ath5k_hw_gainf_calibrate(ah);
916 }
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300917 }
918
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200919 /*GPIOs*/
920 s_led[0] = ath5k_hw_reg_read(ah, AR5K_PCICFG) &
921 AR5K_PCICFG_LEDSTATE;
922 s_led[1] = ath5k_hw_reg_read(ah, AR5K_GPIOCR);
923 s_led[2] = ath5k_hw_reg_read(ah, AR5K_GPIODO);
Nick Kossifidisa406c132009-02-09 06:08:51 +0200924
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200925 /* AR5K_STA_ID1 flags, only preserve antenna
926 * settings and ack/cts rate mode */
927 staid1_flags = ath5k_hw_reg_read(ah, AR5K_STA_ID1) &
928 (AR5K_STA_ID1_DEFAULT_ANTENNA |
929 AR5K_STA_ID1_DESC_ANTENNA |
930 AR5K_STA_ID1_RTS_DEF_ANTENNA |
931 AR5K_STA_ID1_ACKCTS_6MB |
932 AR5K_STA_ID1_BASE_RATE_11B |
933 AR5K_STA_ID1_SELFGEN_DEF_ANT);
934
935 /* Wakeup the device */
936 ret = ath5k_hw_nic_wakeup(ah, channel->hw_value, false);
937 if (ret)
938 return ret;
939
940 /*
941 * Initialize operating mode
942 */
943 ah->ah_op_mode = op_mode;
944
945 /* PHY access enable */
946 if (ah->ah_mac_srev >= AR5K_SREV_AR5211)
947 ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ, AR5K_PHY(0));
948 else
949 ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ | 0x40,
950 AR5K_PHY(0));
951
952 /* Write initial settings */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300953 ret = ath5k_hw_write_initvals(ah, mode, change_channel);
954 if (ret)
955 return ret;
956
957 /*
958 * 5211/5212 Specific
959 */
960 if (ah->ah_version != AR5K_AR5210) {
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200961
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300962 /*
963 * Write initial RF gain settings
964 * This should work for both 5111/5112
965 */
Nick Kossifidis6f3b4142009-02-09 06:03:41 +0200966 ret = ath5k_hw_rfgain_init(ah, freq);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300967 if (ret)
968 return ret;
969
970 mdelay(1);
971
972 /*
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200973 * Tweak initval settings for revised
974 * chipsets and add some more config
975 * bits
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300976 */
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200977 ath5k_hw_tweak_initval_settings(ah, channel);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300978
979 /*
Nick Kossifidis57e6c562009-04-30 15:55:50 -0400980 * Set TX power
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300981 */
Nick Kossifidis8f655dd2009-03-15 22:20:35 +0200982 ret = ath5k_hw_txpower(ah, channel, ee_mode,
Nick Kossifidisa0823812009-04-30 15:55:44 -0400983 ah->ah_txpower.txp_max_pwr / 2);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300984 if (ret)
985 return ret;
986
987 /* Write rate duration table only on AR5212 and if
988 * virtual interface has already been brought up
989 * XXX: rethink this after new mode changes to
990 * mac80211 are integrated */
991 if (ah->ah_version == AR5K_AR5212 &&
992 ah->ah_sc->vif != NULL)
993 ath5k_hw_write_rate_duration(ah, mode);
994
995 /*
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200996 * Write RF buffer
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300997 */
Nick Kossifidis8892e4e2009-02-09 06:06:34 +0200998 ret = ath5k_hw_rfregs_init(ah, channel, mode);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300999 if (ret)
1000 return ret;
1001
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001002
1003 /* Write OFDM timings on 5212*/
1004 if (ah->ah_version == AR5K_AR5212 &&
1005 channel->hw_value & CHANNEL_OFDM) {
Nick Kossifidis57e6c562009-04-30 15:55:50 -04001006 struct ath5k_eeprom_info *ee =
1007 &ah->ah_capabilities.cap_eeprom;
1008
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001009 ret = ath5k_hw_write_ofdm_timings(ah, channel);
1010 if (ret)
1011 return ret;
Nick Kossifidis57e6c562009-04-30 15:55:50 -04001012
1013 /* Note: According to docs we can have a newer
1014 * EEPROM on old hardware, so we need to verify
1015 * that our hardware is new enough to have spur
1016 * mitigation registers (delta phase etc) */
1017 if (ah->ah_mac_srev >= AR5K_SREV_AR5424 ||
1018 (ah->ah_mac_srev >= AR5K_SREV_AR5424 &&
1019 ee->ee_version >= AR5K_EEPROM_VERSION_5_3))
1020 ath5k_hw_set_spur_mitigation_filter(ah,
1021 channel);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001022 }
1023
1024 /*Enable/disable 802.11b mode on 5111
1025 (enable 2111 frequency converter + CCK)*/
1026 if (ah->ah_radio == AR5K_RF5111) {
1027 if (mode == AR5K_MODE_11B)
1028 AR5K_REG_ENABLE_BITS(ah, AR5K_TXCFG,
1029 AR5K_TXCFG_B_MODE);
1030 else
1031 AR5K_REG_DISABLE_BITS(ah, AR5K_TXCFG,
1032 AR5K_TXCFG_B_MODE);
1033 }
1034
1035 /*
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001036 * In case a fixed antenna was set as default
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001037 * use the same switch table twice.
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001038 */
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001039 if (ah->ah_ant_mode == AR5K_ANTMODE_FIXED_A)
1040 ant[0] = ant[1] = AR5K_ANT_SWTABLE_A;
1041 else if (ah->ah_ant_mode == AR5K_ANTMODE_FIXED_B)
1042 ant[0] = ant[1] = AR5K_ANT_SWTABLE_B;
1043 else {
1044 ant[0] = AR5K_ANT_SWTABLE_A;
1045 ant[1] = AR5K_ANT_SWTABLE_B;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001046 }
1047
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001048 /* Commit values from EEPROM */
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001049 ath5k_hw_commit_eeprom_settings(ah, channel, ant, ee_mode);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001050
1051 } else {
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001052 /*
1053 * For 5210 we do all initialization using
1054 * initvals, so we don't have to modify
1055 * any settings (5210 also only supports
1056 * a/aturbo modes)
1057 */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001058 mdelay(1);
1059 /* Disable phy and wait */
1060 ath5k_hw_reg_write(ah, AR5K_PHY_ACT_DISABLE, AR5K_PHY_ACT);
1061 mdelay(1);
1062 }
1063
1064 /*
1065 * Restore saved values
1066 */
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001067
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001068 /*DCU/Antenna selection not available on 5210*/
1069 if (ah->ah_version != AR5K_AR5210) {
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001070
1071 if (change_channel) {
1072 if (ah->ah_mac_srev < AR5K_SREV_AR5211) {
1073 for (i = 0; i < 10; i++)
1074 ath5k_hw_reg_write(ah, s_seq[i],
1075 AR5K_QUEUE_DCU_SEQNUM(i));
1076 } else {
1077 ath5k_hw_reg_write(ah, s_seq[0],
1078 AR5K_QUEUE_DCU_SEQNUM(0));
1079 }
1080
1081
1082 if (ah->ah_version == AR5K_AR5211) {
1083 ath5k_hw_reg_write(ah, tsf_up, AR5K_TSF_U32);
1084 ath5k_hw_reg_write(ah, tsf_lo, AR5K_TSF_L32);
1085 }
1086 }
1087
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001088 ath5k_hw_reg_write(ah, s_ant, AR5K_DEFAULT_ANTENNA);
1089 }
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001090
1091 /* Ledstate */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001092 AR5K_REG_ENABLE_BITS(ah, AR5K_PCICFG, s_led[0]);
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001093
1094 /* Gpio settings */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001095 ath5k_hw_reg_write(ah, s_led[1], AR5K_GPIOCR);
1096 ath5k_hw_reg_write(ah, s_led[2], AR5K_GPIODO);
1097
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001098 /* Restore sta_id flags and preserve our mac address*/
1099 ath5k_hw_reg_write(ah, AR5K_LOW_ID(ah->ah_sta_id),
1100 AR5K_STA_ID0);
1101 ath5k_hw_reg_write(ah, staid1_flags | AR5K_HIGH_ID(ah->ah_sta_id),
1102 AR5K_STA_ID1);
1103
1104
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001105 /*
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001106 * Configure PCU
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001107 */
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001108
1109 /* Restore bssid and bssid mask */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001110 /* XXX: add ah->aid once mac80211 gives this to us */
1111 ath5k_hw_set_associd(ah, ah->ah_bssid, 0);
1112
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001113 /* Set PCU config */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001114 ath5k_hw_set_opmode(ah);
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001115
1116 /* Clear any pending interrupts
1117 * PISR/SISR Not available on 5210 */
1118 if (ah->ah_version != AR5K_AR5210)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001119 ath5k_hw_reg_write(ah, 0xffffffff, AR5K_PISR);
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001120
1121 /* Set RSSI/BRSSI thresholds
1122 *
1123 * Note: If we decide to set this value
1124 * dynamicaly, have in mind that when AR5K_RSSI_THR
1125 * register is read it might return 0x40 if we haven't
1126 * wrote anything to it plus BMISS RSSI threshold is zeroed.
1127 * So doing a save/restore procedure here isn't the right
1128 * choice. Instead store it on ath5k_hw */
1129 ath5k_hw_reg_write(ah, (AR5K_TUNE_RSSI_THRES |
1130 AR5K_TUNE_BMISS_THRES <<
1131 AR5K_RSSI_THR_BMISS_S),
1132 AR5K_RSSI_THR);
1133
1134 /* MIC QoS support */
1135 if (ah->ah_mac_srev >= AR5K_SREV_AR2413) {
1136 ath5k_hw_reg_write(ah, 0x000100aa, AR5K_MIC_QOS_CTL);
1137 ath5k_hw_reg_write(ah, 0x00003210, AR5K_MIC_QOS_SEL);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001138 }
1139
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001140 /* QoS NOACK Policy */
1141 if (ah->ah_version == AR5K_AR5212) {
1142 ath5k_hw_reg_write(ah,
1143 AR5K_REG_SM(2, AR5K_QOS_NOACK_2BIT_VALUES) |
1144 AR5K_REG_SM(5, AR5K_QOS_NOACK_BIT_OFFSET) |
1145 AR5K_REG_SM(0, AR5K_QOS_NOACK_BYTE_OFFSET),
1146 AR5K_QOS_NOACK);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001147 }
1148
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001149
1150 /*
1151 * Configure PHY
1152 */
1153
1154 /* Set channel on PHY */
1155 ret = ath5k_hw_channel(ah, channel);
1156 if (ret)
1157 return ret;
1158
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001159 /*
1160 * Enable the PHY and wait until completion
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001161 * This includes BaseBand and Synthesizer
1162 * activation.
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001163 */
1164 ath5k_hw_reg_write(ah, AR5K_PHY_ACT_ENABLE, AR5K_PHY_ACT);
1165
1166 /*
1167 * On 5211+ read activation -> rx delay
1168 * and use it.
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001169 *
1170 * TODO: Half/quarter rate support
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001171 */
1172 if (ah->ah_version != AR5K_AR5210) {
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001173 u32 delay;
1174 delay = ath5k_hw_reg_read(ah, AR5K_PHY_RX_DELAY) &
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001175 AR5K_PHY_RX_DELAY_M;
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001176 delay = (channel->hw_value & CHANNEL_CCK) ?
1177 ((delay << 2) / 22) : (delay / 10);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001178
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001179 udelay(100 + (2 * delay));
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001180 } else {
1181 mdelay(1);
1182 }
1183
1184 /*
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001185 * Perform ADC test to see if baseband is ready
1186 * Set tx hold and check adc test register
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001187 */
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001188 phy_tst1 = ath5k_hw_reg_read(ah, AR5K_PHY_TST1);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001189 ath5k_hw_reg_write(ah, AR5K_PHY_TST1_TXHOLD, AR5K_PHY_TST1);
1190 for (i = 0; i <= 20; i++) {
1191 if (!(ath5k_hw_reg_read(ah, AR5K_PHY_ADC_TEST) & 0x10))
1192 break;
1193 udelay(200);
1194 }
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001195 ath5k_hw_reg_write(ah, phy_tst1, AR5K_PHY_TST1);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001196
1197 /*
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001198 * Start automatic gain control calibration
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001199 *
1200 * During AGC calibration RX path is re-routed to
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001201 * a power detector so we don't receive anything.
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001202 *
1203 * This method is used to calibrate some static offsets
1204 * used together with on-the fly I/Q calibration (the
1205 * one performed via ath5k_hw_phy_calibrate), that doesn't
1206 * interrupt rx path.
1207 *
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001208 * While rx path is re-routed to the power detector we also
1209 * start a noise floor calibration, to measure the
1210 * card's noise floor (the noise we measure when we are not
1211 * transmiting or receiving anything).
1212 *
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001213 * If we are in a noisy environment AGC calibration may time
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001214 * out and/or noise floor calibration might timeout.
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001215 */
1216 AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGCCTL,
1217 AR5K_PHY_AGCCTL_CAL);
1218
1219 /* At the same time start I/Q calibration for QAM constellation
1220 * -no need for CCK- */
1221 ah->ah_calibration = false;
1222 if (!(mode == AR5K_MODE_11B)) {
1223 ah->ah_calibration = true;
1224 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_IQ,
1225 AR5K_PHY_IQ_CAL_NUM_LOG_MAX, 15);
1226 AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ,
1227 AR5K_PHY_IQ_RUN);
1228 }
1229
1230 /* Wait for gain calibration to finish (we check for I/Q calibration
1231 * during ath5k_phy_calibrate) */
1232 if (ath5k_hw_register_timeout(ah, AR5K_PHY_AGCCTL,
1233 AR5K_PHY_AGCCTL_CAL, 0, false)) {
1234 ATH5K_ERR(ah->ah_sc, "gain calibration timeout (%uMHz)\n",
1235 channel->center_freq);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001236 }
1237
1238 /*
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001239 * If we run NF calibration before AGC, it always times out.
1240 * Binary HAL starts NF and AGC calibration at the same time
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001241 * and only waits for AGC to finish. Also if AGC or NF cal.
1242 * times out, reset doesn't fail on binary HAL. I believe
1243 * that's wrong because since rx path is routed to a detector,
1244 * if cal. doesn't finish we won't have RX. Sam's HAL for AR5210/5211
1245 * enables noise floor calibration after offset calibration and if noise
1246 * floor calibration fails, reset fails. I believe that's
1247 * a better approach, we just need to find a polling interval
1248 * that suits best, even if reset continues we need to make
1249 * sure that rx path is ready.
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001250 */
Felix Fietkau8b0162a2008-11-03 11:27:38 +01001251 ath5k_hw_noise_floor_calibration(ah, channel->center_freq);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001252
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001253 /* Restore antenna mode */
1254 ath5k_hw_set_antenna_mode(ah, ah->ah_ant_mode);
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001255
1256 /*
1257 * Configure QCUs/DCUs
1258 */
1259
1260 /* TODO: HW Compression support for data queues */
1261 /* TODO: Burst prefetch for data queues */
1262
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001263 /*
1264 * Reset queues and start beacon timers at the end of the reset routine
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001265 * This also sets QCU mask on each DCU for 1:1 qcu to dcu mapping
1266 * Note: If we want we can assign multiple qcus on one dcu.
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001267 */
1268 for (i = 0; i < ah->ah_capabilities.cap_queues.q_tx_num; i++) {
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001269 ret = ath5k_hw_reset_tx_queue(ah, i);
1270 if (ret) {
1271 ATH5K_ERR(ah->ah_sc,
1272 "failed to reset TX queue #%d\n", i);
1273 return ret;
1274 }
1275 }
1276
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001277
1278 /*
1279 * Configure DMA/Interrupts
1280 */
1281
1282 /*
1283 * Set Rx/Tx DMA Configuration
1284 *
1285 * Set standard DMA size (128). Note that
1286 * a DMA size of 512 causes rx overruns and tx errors
1287 * on pci-e cards (tested on 5424 but since rx overruns
1288 * also occur on 5416/5418 with madwifi we set 128
1289 * for all PCI-E cards to be safe).
1290 *
1291 * XXX: need to check 5210 for this
1292 * TODO: Check out tx triger level, it's always 64 on dumps but I
1293 * guess we can tweak it and see how it goes ;-)
1294 */
1295 if (ah->ah_version != AR5K_AR5210) {
1296 AR5K_REG_WRITE_BITS(ah, AR5K_TXCFG,
1297 AR5K_TXCFG_SDMAMR, AR5K_DMASIZE_128B);
1298 AR5K_REG_WRITE_BITS(ah, AR5K_RXCFG,
1299 AR5K_RXCFG_SDMAMW, AR5K_DMASIZE_128B);
1300 }
1301
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001302 /* Pre-enable interrupts on 5211/5212*/
1303 if (ah->ah_version != AR5K_AR5210)
Nick Kossifidis4c674c62008-10-26 20:40:25 +02001304 ath5k_hw_set_imr(ah, ah->ah_imr);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001305
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001306 /* Enable 32KHz clock function for AR5212+ chips
1307 * Set clocks to 32KHz operation and use an
1308 * external 32KHz crystal when sleeping if one
1309 * exists */
1310 if (ah->ah_version == AR5K_AR5212)
1311 ath5k_hw_set_sleep_clock(ah, true);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001312
1313 /*
1314 * Disable beacons and reset the register
1315 */
1316 AR5K_REG_DISABLE_BITS(ah, AR5K_BEACON, AR5K_BEACON_ENABLE |
1317 AR5K_BEACON_RESET_TSF);
1318
1319 return 0;
1320}
1321
1322#undef _ATH5K_RESET