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>
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.
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.
22 /*****************************\
23 Reset functions and helpers
24 \*****************************/
26 #include <asm/unaligned.h>
28 #include <linux/pci.h> /* To determine if a card is pci-e */
29 #include <linux/log2.h>
41 * Check if a register write has been completed
43 int ath5k_hw_register_timeout(struct ath5k_hw *ah, u32 reg, u32 flag, u32 val,
49 for (i = AR5K_TUNE_REGISTER_TIMEOUT; i > 0; i--) {
50 data = ath5k_hw_reg_read(ah, reg);
51 if (is_set && (data & flag))
53 else if ((data & flag) == val)
58 return (i <= 0) ? -EAGAIN : 0;
62 /*************************\
63 * Clock related functions *
64 \*************************/
67 * ath5k_hw_htoclock - Translate usec to hw clock units
69 * @ah: The &struct ath5k_hw
70 * @usec: value in microseconds
72 unsigned int ath5k_hw_htoclock(struct ath5k_hw *ah, unsigned int usec)
74 struct ath_common *common = ath5k_hw_common(ah);
75 return usec * common->clockrate;
79 * ath5k_hw_clocktoh - Translate hw clock units to usec
80 * @clock: value in hw clock units
82 unsigned int ath5k_hw_clocktoh(struct ath5k_hw *ah, unsigned int clock)
84 struct ath_common *common = ath5k_hw_common(ah);
85 return clock / common->clockrate;
89 * ath5k_hw_set_clockrate - Set common->clockrate for the current channel
91 * @ah: The &struct ath5k_hw
93 void ath5k_hw_set_clockrate(struct ath5k_hw *ah)
95 struct ieee80211_channel *channel = ah->ah_current_channel;
96 struct ath_common *common = ath5k_hw_common(ah);
99 if (channel->hw_value & CHANNEL_5GHZ)
100 clock = 40; /* 802.11a */
101 else if (channel->hw_value & CHANNEL_CCK)
102 clock = 22; /* 802.11b */
104 clock = 44; /* 802.11g */
106 /* Clock rate in turbo modes is twice the normal rate */
107 if (channel->hw_value & CHANNEL_TURBO)
110 common->clockrate = clock;
114 * If there is an external 32KHz crystal available, use it
115 * as ref. clock instead of 32/40MHz clock and baseband clocks
116 * to save power during sleep or restore normal 32/40MHz
119 * XXX: When operating on 32KHz certain PHY registers (27 - 31,
120 * 123 - 127) require delay on access.
122 static void ath5k_hw_set_sleep_clock(struct ath5k_hw *ah, bool enable)
124 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
125 u32 scal, spending, usec32;
127 /* Only set 32KHz settings if we have an external
128 * 32KHz crystal present */
129 if ((AR5K_EEPROM_HAS32KHZCRYSTAL(ee->ee_misc1) ||
130 AR5K_EEPROM_HAS32KHZCRYSTAL_OLD(ee->ee_misc1)) &&
134 AR5K_REG_WRITE_BITS(ah, AR5K_USEC_5211, AR5K_USEC_32, 1);
135 /* Set up tsf increment on each cycle */
136 AR5K_REG_WRITE_BITS(ah, AR5K_TSF_PARM, AR5K_TSF_PARM_INC, 61);
138 /* Set baseband sleep control registers
139 * and sleep control rate */
140 ath5k_hw_reg_write(ah, 0x1f, AR5K_PHY_SCR);
142 if ((ah->ah_radio == AR5K_RF5112) ||
143 (ah->ah_radio == AR5K_RF5413) ||
144 (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4)))
148 ath5k_hw_reg_write(ah, spending, AR5K_PHY_SPENDING);
150 if ((ah->ah_radio == AR5K_RF5112) ||
151 (ah->ah_radio == AR5K_RF5413) ||
152 (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4))) {
153 ath5k_hw_reg_write(ah, 0x26, AR5K_PHY_SLMT);
154 ath5k_hw_reg_write(ah, 0x0d, AR5K_PHY_SCAL);
155 ath5k_hw_reg_write(ah, 0x07, AR5K_PHY_SCLOCK);
156 ath5k_hw_reg_write(ah, 0x3f, AR5K_PHY_SDELAY);
157 AR5K_REG_WRITE_BITS(ah, AR5K_PCICFG,
158 AR5K_PCICFG_SLEEP_CLOCK_RATE, 0x02);
160 ath5k_hw_reg_write(ah, 0x0a, AR5K_PHY_SLMT);
161 ath5k_hw_reg_write(ah, 0x0c, AR5K_PHY_SCAL);
162 ath5k_hw_reg_write(ah, 0x03, AR5K_PHY_SCLOCK);
163 ath5k_hw_reg_write(ah, 0x20, AR5K_PHY_SDELAY);
164 AR5K_REG_WRITE_BITS(ah, AR5K_PCICFG,
165 AR5K_PCICFG_SLEEP_CLOCK_RATE, 0x03);
168 /* Enable sleep clock operation */
169 AR5K_REG_ENABLE_BITS(ah, AR5K_PCICFG,
170 AR5K_PCICFG_SLEEP_CLOCK_EN);
174 /* Disable sleep clock operation and
175 * restore default parameters */
176 AR5K_REG_DISABLE_BITS(ah, AR5K_PCICFG,
177 AR5K_PCICFG_SLEEP_CLOCK_EN);
179 AR5K_REG_WRITE_BITS(ah, AR5K_PCICFG,
180 AR5K_PCICFG_SLEEP_CLOCK_RATE, 0);
182 ath5k_hw_reg_write(ah, 0x1f, AR5K_PHY_SCR);
183 ath5k_hw_reg_write(ah, AR5K_PHY_SLMT_32MHZ, AR5K_PHY_SLMT);
185 if (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4))
186 scal = AR5K_PHY_SCAL_32MHZ_2417;
187 else if (ee->ee_is_hb63)
188 scal = AR5K_PHY_SCAL_32MHZ_HB63;
190 scal = AR5K_PHY_SCAL_32MHZ;
191 ath5k_hw_reg_write(ah, scal, AR5K_PHY_SCAL);
193 ath5k_hw_reg_write(ah, AR5K_PHY_SCLOCK_32MHZ, AR5K_PHY_SCLOCK);
194 ath5k_hw_reg_write(ah, AR5K_PHY_SDELAY_32MHZ, AR5K_PHY_SDELAY);
196 if ((ah->ah_radio == AR5K_RF5112) ||
197 (ah->ah_radio == AR5K_RF5413) ||
198 (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4)))
202 ath5k_hw_reg_write(ah, spending, AR5K_PHY_SPENDING);
204 if ((ah->ah_radio == AR5K_RF5112) ||
205 (ah->ah_radio == AR5K_RF5413))
209 AR5K_REG_WRITE_BITS(ah, AR5K_USEC_5211, AR5K_USEC_32, usec32);
211 AR5K_REG_WRITE_BITS(ah, AR5K_TSF_PARM, AR5K_TSF_PARM_INC, 1);
216 /*********************\
217 * Reset/Sleep control *
218 \*********************/
223 static int ath5k_hw_nic_reset(struct ath5k_hw *ah, u32 val)
226 u32 mask = val ? val : ~0U;
228 /* Read-and-clear RX Descriptor Pointer*/
229 ath5k_hw_reg_read(ah, AR5K_RXDP);
232 * Reset the device and wait until success
234 ath5k_hw_reg_write(ah, val, AR5K_RESET_CTL);
236 /* Wait at least 128 PCI clocks */
239 if (ah->ah_version == AR5K_AR5210) {
240 val &= AR5K_RESET_CTL_PCU | AR5K_RESET_CTL_DMA
241 | AR5K_RESET_CTL_MAC | AR5K_RESET_CTL_PHY;
242 mask &= AR5K_RESET_CTL_PCU | AR5K_RESET_CTL_DMA
243 | AR5K_RESET_CTL_MAC | AR5K_RESET_CTL_PHY;
245 val &= AR5K_RESET_CTL_PCU | AR5K_RESET_CTL_BASEBAND;
246 mask &= AR5K_RESET_CTL_PCU | AR5K_RESET_CTL_BASEBAND;
249 ret = ath5k_hw_register_timeout(ah, AR5K_RESET_CTL, mask, val, false);
252 * Reset configuration register (for hw byte-swap). Note that this
253 * is only set for big endian. We do the necessary magic in
256 if ((val & AR5K_RESET_CTL_PCU) == 0)
257 ath5k_hw_reg_write(ah, AR5K_INIT_CFG, AR5K_CFG);
265 static int ath5k_hw_set_power(struct ath5k_hw *ah, enum ath5k_power_mode mode,
266 bool set_chip, u16 sleep_duration)
271 staid = ath5k_hw_reg_read(ah, AR5K_STA_ID1);
275 staid &= ~AR5K_STA_ID1_DEFAULT_ANTENNA;
277 case AR5K_PM_NETWORK_SLEEP:
279 ath5k_hw_reg_write(ah,
280 AR5K_SLEEP_CTL_SLE_ALLOW |
284 staid |= AR5K_STA_ID1_PWR_SV;
287 case AR5K_PM_FULL_SLEEP:
289 ath5k_hw_reg_write(ah, AR5K_SLEEP_CTL_SLE_SLP,
292 staid |= AR5K_STA_ID1_PWR_SV;
297 staid &= ~AR5K_STA_ID1_PWR_SV;
302 data = ath5k_hw_reg_read(ah, AR5K_SLEEP_CTL);
304 /* If card is down we 'll get 0xffff... so we
305 * need to clean this up before we write the register
307 if (data & 0xffc00000)
310 /* Preserve sleep duration etc */
311 data = data & ~AR5K_SLEEP_CTL_SLE;
313 ath5k_hw_reg_write(ah, data | AR5K_SLEEP_CTL_SLE_WAKE,
317 for (i = 200; i > 0; i--) {
318 /* Check if the chip did wake up */
319 if ((ath5k_hw_reg_read(ah, AR5K_PCICFG) &
320 AR5K_PCICFG_SPWR_DN) == 0)
323 /* Wait a bit and retry */
325 ath5k_hw_reg_write(ah, data | AR5K_SLEEP_CTL_SLE_WAKE,
329 /* Fail if the chip didn't wake up */
340 ath5k_hw_reg_write(ah, staid, AR5K_STA_ID1);
348 * Put MAC and Baseband on warm reset and
349 * keep that state (don't clean sleep control
350 * register). After this MAC and Baseband are
351 * disabled and a full reset is needed to come
352 * back. This way we save as much power as possible
353 * without putting the card on full sleep.
355 int ath5k_hw_on_hold(struct ath5k_hw *ah)
357 struct pci_dev *pdev = ah->ah_sc->pdev;
361 /* Make sure device is awake */
362 ret = ath5k_hw_set_power(ah, AR5K_PM_AWAKE, true, 0);
364 ATH5K_ERR(ah->ah_sc, "failed to wakeup the MAC Chip\n");
369 * Put chipset on warm reset...
371 * Note: putting PCI core on warm reset on PCI-E cards
372 * results card to hang and always return 0xffff... so
373 * we ingore that flag for PCI-E cards. On PCI cards
374 * this flag gets cleared after 64 PCI clocks.
376 bus_flags = (pdev->is_pcie) ? 0 : AR5K_RESET_CTL_PCI;
378 if (ah->ah_version == AR5K_AR5210) {
379 ret = ath5k_hw_nic_reset(ah, AR5K_RESET_CTL_PCU |
380 AR5K_RESET_CTL_MAC | AR5K_RESET_CTL_DMA |
381 AR5K_RESET_CTL_PHY | AR5K_RESET_CTL_PCI);
384 ret = ath5k_hw_nic_reset(ah, AR5K_RESET_CTL_PCU |
385 AR5K_RESET_CTL_BASEBAND | bus_flags);
389 ATH5K_ERR(ah->ah_sc, "failed to put device on warm reset\n");
393 /* ...wakeup again!*/
394 ret = ath5k_hw_set_power(ah, AR5K_PM_AWAKE, true, 0);
396 ATH5K_ERR(ah->ah_sc, "failed to put device on hold\n");
404 * Bring up MAC + PHY Chips and program PLL
405 * TODO: Half/Quarter rate support
407 int ath5k_hw_nic_wakeup(struct ath5k_hw *ah, int flags, bool initial)
409 struct pci_dev *pdev = ah->ah_sc->pdev;
410 u32 turbo, mode, clock, bus_flags;
417 /* Wakeup the device */
418 ret = ath5k_hw_set_power(ah, AR5K_PM_AWAKE, true, 0);
420 ATH5K_ERR(ah->ah_sc, "failed to wakeup the MAC Chip\n");
425 * Put chipset on warm reset...
427 * Note: putting PCI core on warm reset on PCI-E cards
428 * results card to hang and always return 0xffff... so
429 * we ingore that flag for PCI-E cards. On PCI cards
430 * this flag gets cleared after 64 PCI clocks.
432 bus_flags = (pdev->is_pcie) ? 0 : AR5K_RESET_CTL_PCI;
434 if (ah->ah_version == AR5K_AR5210) {
435 ret = ath5k_hw_nic_reset(ah, AR5K_RESET_CTL_PCU |
436 AR5K_RESET_CTL_MAC | AR5K_RESET_CTL_DMA |
437 AR5K_RESET_CTL_PHY | AR5K_RESET_CTL_PCI);
440 ret = ath5k_hw_nic_reset(ah, AR5K_RESET_CTL_PCU |
441 AR5K_RESET_CTL_BASEBAND | bus_flags);
445 ATH5K_ERR(ah->ah_sc, "failed to reset the MAC Chip\n");
449 /* ...wakeup again!...*/
450 ret = ath5k_hw_set_power(ah, AR5K_PM_AWAKE, true, 0);
452 ATH5K_ERR(ah->ah_sc, "failed to resume the MAC Chip\n");
456 /* ...clear reset control register and pull device out of
458 if (ath5k_hw_nic_reset(ah, 0)) {
459 ATH5K_ERR(ah->ah_sc, "failed to warm reset the MAC Chip\n");
463 /* On initialization skip PLL programming since we don't have
464 * a channel / mode set yet */
468 if (ah->ah_version != AR5K_AR5210) {
470 * Get channel mode flags
473 if (ah->ah_radio >= AR5K_RF5112) {
474 mode = AR5K_PHY_MODE_RAD_RF5112;
475 clock = AR5K_PHY_PLL_RF5112;
477 mode = AR5K_PHY_MODE_RAD_RF5111; /*Zero*/
478 clock = AR5K_PHY_PLL_RF5111; /*Zero*/
481 if (flags & CHANNEL_2GHZ) {
482 mode |= AR5K_PHY_MODE_FREQ_2GHZ;
483 clock |= AR5K_PHY_PLL_44MHZ;
485 if (flags & CHANNEL_CCK) {
486 mode |= AR5K_PHY_MODE_MOD_CCK;
487 } else if (flags & CHANNEL_OFDM) {
488 /* XXX Dynamic OFDM/CCK is not supported by the
489 * AR5211 so we set MOD_OFDM for plain g (no
490 * CCK headers) operation. We need to test
491 * this, 5211 might support ofdm-only g after
492 * all, there are also initial register values
493 * in the code for g mode (see initvals.c). */
494 if (ah->ah_version == AR5K_AR5211)
495 mode |= AR5K_PHY_MODE_MOD_OFDM;
497 mode |= AR5K_PHY_MODE_MOD_DYN;
500 "invalid radio modulation mode\n");
503 } else if (flags & CHANNEL_5GHZ) {
504 mode |= AR5K_PHY_MODE_FREQ_5GHZ;
506 if (ah->ah_radio == AR5K_RF5413)
507 clock = AR5K_PHY_PLL_40MHZ_5413;
509 clock |= AR5K_PHY_PLL_40MHZ;
511 if (flags & CHANNEL_OFDM)
512 mode |= AR5K_PHY_MODE_MOD_OFDM;
515 "invalid radio modulation mode\n");
519 ATH5K_ERR(ah->ah_sc, "invalid radio frequency mode\n");
523 if (flags & CHANNEL_TURBO)
524 turbo = AR5K_PHY_TURBO_MODE | AR5K_PHY_TURBO_SHORT;
525 } else { /* Reset the device */
527 /* ...enable Atheros turbo mode if requested */
528 if (flags & CHANNEL_TURBO)
529 ath5k_hw_reg_write(ah, AR5K_PHY_TURBO_MODE,
533 if (ah->ah_version != AR5K_AR5210) {
535 /* ...update PLL if needed */
536 if (ath5k_hw_reg_read(ah, AR5K_PHY_PLL) != clock) {
537 ath5k_hw_reg_write(ah, clock, AR5K_PHY_PLL);
541 /* ...set the PHY operating mode */
542 ath5k_hw_reg_write(ah, mode, AR5K_PHY_MODE);
543 ath5k_hw_reg_write(ah, turbo, AR5K_PHY_TURBO);
550 /**************************************\
551 * Post-initvals register modifications *
552 \**************************************/
554 /* TODO: Half/Quarter rate */
555 static void ath5k_hw_tweak_initval_settings(struct ath5k_hw *ah,
556 struct ieee80211_channel *channel)
558 if (ah->ah_version == AR5K_AR5212 &&
559 ah->ah_phy_revision >= AR5K_SREV_PHY_5212A) {
561 /* Setup ADC control */
562 ath5k_hw_reg_write(ah,
564 AR5K_PHY_ADC_CTL_INBUFGAIN_OFF) |
566 AR5K_PHY_ADC_CTL_INBUFGAIN_ON) |
567 AR5K_PHY_ADC_CTL_PWD_DAC_OFF |
568 AR5K_PHY_ADC_CTL_PWD_ADC_OFF),
573 /* Disable barker RSSI threshold */
574 AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_DAG_CCK_CTL,
575 AR5K_PHY_DAG_CCK_CTL_EN_RSSI_THR);
577 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DAG_CCK_CTL,
578 AR5K_PHY_DAG_CCK_CTL_RSSI_THR, 2);
580 /* Set the mute mask */
581 ath5k_hw_reg_write(ah, 0x0000000f, AR5K_SEQ_MASK);
584 /* Clear PHY_BLUETOOTH to allow RX_CLEAR line debug */
585 if (ah->ah_phy_revision >= AR5K_SREV_PHY_5212B)
586 ath5k_hw_reg_write(ah, 0, AR5K_PHY_BLUETOOTH);
588 /* Enable DCU double buffering */
589 if (ah->ah_phy_revision > AR5K_SREV_PHY_5212B)
590 AR5K_REG_DISABLE_BITS(ah, AR5K_TXCFG,
591 AR5K_TXCFG_DCU_DBL_BUF_DIS);
593 /* Set DAC/ADC delays */
594 if (ah->ah_version == AR5K_AR5212) {
596 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
597 if (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4))
598 scal = AR5K_PHY_SCAL_32MHZ_2417;
599 else if (ee->ee_is_hb63)
600 scal = AR5K_PHY_SCAL_32MHZ_HB63;
602 scal = AR5K_PHY_SCAL_32MHZ;
603 ath5k_hw_reg_write(ah, scal, AR5K_PHY_SCAL);
607 if ((ah->ah_radio == AR5K_RF5413) ||
608 (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4))) {
611 if (channel->center_freq == 2462 ||
612 channel->center_freq == 2467)
615 /* Only update if needed */
616 if (ath5k_hw_reg_read(ah, AR5K_PHY_FAST_ADC) != fast_adc)
617 ath5k_hw_reg_write(ah, fast_adc,
621 /* Fix for first revision of the RF5112 RF chipset */
622 if (ah->ah_radio == AR5K_RF5112 &&
623 ah->ah_radio_5ghz_revision <
624 AR5K_SREV_RAD_5112A) {
626 ath5k_hw_reg_write(ah, AR5K_PHY_CCKTXCTL_WORLD,
628 if (channel->hw_value & CHANNEL_5GHZ)
632 ath5k_hw_reg_write(ah, data, AR5K_PHY_FRAME_CTL);
635 if ((ah->ah_radio == AR5K_RF5112) &&
636 (ah->ah_mac_srev < AR5K_SREV_AR5211)) {
638 /* 5311 has different tx/rx latency masks
639 * from 5211, since we deal 5311 the same
640 * as 5211 when setting initvals, shift
641 * values here to their proper locations */
642 usec_reg = ath5k_hw_reg_read(ah, AR5K_USEC_5211);
643 ath5k_hw_reg_write(ah, usec_reg & (AR5K_USEC_1 |
645 AR5K_USEC_TX_LATENCY_5211 |
647 AR5K_USEC_RX_LATENCY_5210)),
649 /* Clear QCU/DCU clock gating register */
650 ath5k_hw_reg_write(ah, 0, AR5K_QCUDCU_CLKGT);
651 /* Set DAC/ADC delays */
652 ath5k_hw_reg_write(ah, 0x08, AR5K_PHY_SCAL);
653 /* Enable PCU FIFO corruption ECO */
654 AR5K_REG_ENABLE_BITS(ah, AR5K_DIAG_SW_5211,
655 AR5K_DIAG_SW_ECO_ENABLE);
659 static void ath5k_hw_commit_eeprom_settings(struct ath5k_hw *ah,
660 struct ieee80211_channel *channel, u8 ee_mode)
662 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
663 s16 cck_ofdm_pwr_delta;
665 /* TODO: Add support for AR5210 EEPROM */
666 if (ah->ah_version == AR5K_AR5210)
669 /* Adjust power delta for channel 14 */
670 if (channel->center_freq == 2484)
672 ((ee->ee_cck_ofdm_power_delta -
673 ee->ee_scaled_cck_delta) * 2) / 10;
676 (ee->ee_cck_ofdm_power_delta * 2) / 10;
678 /* Set CCK to OFDM power delta on tx power
679 * adjustment register */
680 if (ah->ah_phy_revision >= AR5K_SREV_PHY_5212A) {
681 if (channel->hw_value == CHANNEL_G)
682 ath5k_hw_reg_write(ah,
683 AR5K_REG_SM((ee->ee_cck_ofdm_gain_delta * -1),
684 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);
689 ath5k_hw_reg_write(ah, 0, AR5K_PHY_TX_PWR_ADJ);
691 /* For older revs we scale power on sw during tx power
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;
698 /* XXX: necessary here? is called from ath5k_hw_set_antenna_mode()
700 ath5k_hw_set_antenna_switch(ah, ee_mode);
702 /* Noise floor threshold */
703 ath5k_hw_reg_write(ah,
704 AR5K_PHY_NF_SVAL(ee->ee_noise_floor_thr[ee_mode]),
707 if ((channel->hw_value & CHANNEL_TURBO) &&
708 (ah->ah_ee_version >= AR5K_EEPROM_VERSION_5_0)) {
709 /* Switch settling time (Turbo) */
710 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_SETTLING,
711 AR5K_PHY_SETTLING_SWITCH,
712 ee->ee_switch_settling_turbo[ee_mode]);
714 /* Tx/Rx attenuation (Turbo) */
715 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_GAIN,
716 AR5K_PHY_GAIN_TXRX_ATTEN,
717 ee->ee_atn_tx_rx_turbo[ee_mode]);
719 /* ADC/PGA desired size (Turbo) */
720 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DESIRED_SIZE,
721 AR5K_PHY_DESIRED_SIZE_ADC,
722 ee->ee_adc_desired_size_turbo[ee_mode]);
724 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DESIRED_SIZE,
725 AR5K_PHY_DESIRED_SIZE_PGA,
726 ee->ee_pga_desired_size_turbo[ee_mode]);
728 /* Tx/Rx margin (Turbo) */
729 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_GAIN_2GHZ,
730 AR5K_PHY_GAIN_2GHZ_MARGIN_TXRX,
731 ee->ee_margin_tx_rx_turbo[ee_mode]);
734 /* Switch settling time */
735 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_SETTLING,
736 AR5K_PHY_SETTLING_SWITCH,
737 ee->ee_switch_settling[ee_mode]);
739 /* Tx/Rx attenuation */
740 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_GAIN,
741 AR5K_PHY_GAIN_TXRX_ATTEN,
742 ee->ee_atn_tx_rx[ee_mode]);
744 /* ADC/PGA desired size */
745 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DESIRED_SIZE,
746 AR5K_PHY_DESIRED_SIZE_ADC,
747 ee->ee_adc_desired_size[ee_mode]);
749 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DESIRED_SIZE,
750 AR5K_PHY_DESIRED_SIZE_PGA,
751 ee->ee_pga_desired_size[ee_mode]);
754 if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_1)
755 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_GAIN_2GHZ,
756 AR5K_PHY_GAIN_2GHZ_MARGIN_TXRX,
757 ee->ee_margin_tx_rx[ee_mode]);
761 ath5k_hw_reg_write(ah,
762 (ee->ee_tx_end2xpa_disable[ee_mode] << 24) |
763 (ee->ee_tx_end2xpa_disable[ee_mode] << 16) |
764 (ee->ee_tx_frm2xpa_enable[ee_mode] << 8) |
765 (ee->ee_tx_frm2xpa_enable[ee_mode]), AR5K_PHY_RF_CTL4);
768 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_RF_CTL3,
769 AR5K_PHY_RF_CTL3_TXE2XLNA_ON,
770 ee->ee_tx_end2xlna_enable[ee_mode]);
773 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_NF,
774 AR5K_PHY_NF_THRESH62,
775 ee->ee_thr_62[ee_mode]);
777 /* False detect backoff for channels
778 * that have spur noise. Write the new
779 * cyclic power RSSI threshold. */
780 if (ath5k_hw_chan_has_spur_noise(ah, channel))
781 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_OFDM_SELFCORR,
782 AR5K_PHY_OFDM_SELFCORR_CYPWR_THR1,
783 AR5K_INIT_CYCRSSI_THR1 +
784 ee->ee_false_detect[ee_mode]);
786 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_OFDM_SELFCORR,
787 AR5K_PHY_OFDM_SELFCORR_CYPWR_THR1,
788 AR5K_INIT_CYCRSSI_THR1);
790 /* I/Q correction (set enable bit last to match HAL sources) */
791 /* TODO: Per channel i/q infos ? */
792 if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_0) {
793 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_IQ, AR5K_PHY_IQ_CORR_Q_I_COFF,
794 ee->ee_i_cal[ee_mode]);
795 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_IQ, AR5K_PHY_IQ_CORR_Q_Q_COFF,
796 ee->ee_q_cal[ee_mode]);
797 AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ, AR5K_PHY_IQ_CORR_ENABLE);
800 /* Heavy clipping -disable for now */
801 if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_5_1)
802 ath5k_hw_reg_write(ah, 0, AR5K_PHY_HEAVY_CLIP_ENABLE);
806 /*********************\
807 * Main reset function *
808 \*********************/
810 int ath5k_hw_reset(struct ath5k_hw *ah, enum nl80211_iftype op_mode,
811 struct ieee80211_channel *channel, bool change_channel)
813 struct ath_common *common = ath5k_hw_common(ah);
814 u32 s_seq[10], s_led[3], staid1_flags, tsf_up, tsf_lo;
815 u8 mode, freq, ee_mode;
826 * Save some registers before a reset
828 /*DCU/Antenna selection not available on 5210*/
829 if (ah->ah_version != AR5K_AR5210) {
831 switch (channel->hw_value & CHANNEL_MODES) {
833 mode = AR5K_MODE_11A;
834 freq = AR5K_INI_RFGAIN_5GHZ;
835 ee_mode = AR5K_EEPROM_MODE_11A;
838 mode = AR5K_MODE_11G;
839 freq = AR5K_INI_RFGAIN_2GHZ;
840 ee_mode = AR5K_EEPROM_MODE_11G;
843 mode = AR5K_MODE_11B;
844 freq = AR5K_INI_RFGAIN_2GHZ;
845 ee_mode = AR5K_EEPROM_MODE_11B;
848 mode = AR5K_MODE_11A_TURBO;
849 freq = AR5K_INI_RFGAIN_5GHZ;
850 ee_mode = AR5K_EEPROM_MODE_11A;
853 if (ah->ah_version == AR5K_AR5211) {
855 "TurboG mode not available on 5211");
858 mode = AR5K_MODE_11G_TURBO;
859 freq = AR5K_INI_RFGAIN_2GHZ;
860 ee_mode = AR5K_EEPROM_MODE_11G;
863 if (ah->ah_version == AR5K_AR5211) {
865 "XR mode not available on 5211");
869 freq = AR5K_INI_RFGAIN_5GHZ;
870 ee_mode = AR5K_EEPROM_MODE_11A;
874 "invalid channel: %d\n", channel->center_freq);
878 if (change_channel) {
880 * Save frame sequence count
881 * For revs. after Oahu, only save
882 * seq num for DCU 0 (Global seq num)
884 if (ah->ah_mac_srev < AR5K_SREV_AR5211) {
886 for (i = 0; i < 10; i++)
887 s_seq[i] = ath5k_hw_reg_read(ah,
888 AR5K_QUEUE_DCU_SEQNUM(i));
891 s_seq[0] = ath5k_hw_reg_read(ah,
892 AR5K_QUEUE_DCU_SEQNUM(0));
895 /* TSF accelerates on AR5211 during reset
896 * As a workaround save it here and restore
897 * it later so that it's back in time after
898 * reset. This way it'll get re-synced on the
899 * next beacon without breaking ad-hoc.
901 * On AR5212 TSF is almost preserved across a
902 * reset so it stays back in time anyway and
903 * we don't have to save/restore it.
905 * XXX: Since this breaks power saving we have
906 * to disable power saving until we receive the
907 * next beacon, so we can resync beacon timers */
908 if (ah->ah_version == AR5K_AR5211) {
909 tsf_up = ath5k_hw_reg_read(ah, AR5K_TSF_U32);
910 tsf_lo = ath5k_hw_reg_read(ah, AR5K_TSF_L32);
914 if (ah->ah_version == AR5K_AR5212) {
915 /* Restore normal 32/40MHz clock operation
916 * to avoid register access delay on certain
918 ath5k_hw_set_sleep_clock(ah, false);
920 /* Since we are going to write rf buffer
921 * check if we have any pending gain_F
922 * optimization settings */
923 if (change_channel && ah->ah_rf_banks != NULL)
924 ath5k_hw_gainf_calibrate(ah);
929 s_led[0] = ath5k_hw_reg_read(ah, AR5K_PCICFG) &
930 AR5K_PCICFG_LEDSTATE;
931 s_led[1] = ath5k_hw_reg_read(ah, AR5K_GPIOCR);
932 s_led[2] = ath5k_hw_reg_read(ah, AR5K_GPIODO);
934 /* AR5K_STA_ID1 flags, only preserve antenna
935 * settings and ack/cts rate mode */
936 staid1_flags = ath5k_hw_reg_read(ah, AR5K_STA_ID1) &
937 (AR5K_STA_ID1_DEFAULT_ANTENNA |
938 AR5K_STA_ID1_DESC_ANTENNA |
939 AR5K_STA_ID1_RTS_DEF_ANTENNA |
940 AR5K_STA_ID1_ACKCTS_6MB |
941 AR5K_STA_ID1_BASE_RATE_11B |
942 AR5K_STA_ID1_SELFGEN_DEF_ANT);
944 /* Wakeup the device */
945 ret = ath5k_hw_nic_wakeup(ah, channel->hw_value, false);
949 /* PHY access enable */
950 if (ah->ah_mac_srev >= AR5K_SREV_AR5211)
951 ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ, AR5K_PHY(0));
953 ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ | 0x40,
956 /* Write initial settings */
957 ret = ath5k_hw_write_initvals(ah, mode, change_channel);
962 * Tweak initval settings for revised
963 * chipsets and add some more config
966 ath5k_hw_tweak_initval_settings(ah, channel);
968 /* Commit values from EEPROM */
969 ath5k_hw_commit_eeprom_settings(ah, channel, ee_mode);
973 * Restore saved values
976 /*DCU/Antenna selection not available on 5210*/
977 if (ah->ah_version != AR5K_AR5210) {
979 if (change_channel) {
980 if (ah->ah_mac_srev < AR5K_SREV_AR5211) {
981 for (i = 0; i < 10; i++)
982 ath5k_hw_reg_write(ah, s_seq[i],
983 AR5K_QUEUE_DCU_SEQNUM(i));
985 ath5k_hw_reg_write(ah, s_seq[0],
986 AR5K_QUEUE_DCU_SEQNUM(0));
990 if (ah->ah_version == AR5K_AR5211) {
991 ath5k_hw_reg_write(ah, tsf_up, AR5K_TSF_U32);
992 ath5k_hw_reg_write(ah, tsf_lo, AR5K_TSF_L32);
998 AR5K_REG_ENABLE_BITS(ah, AR5K_PCICFG, s_led[0]);
1001 ath5k_hw_reg_write(ah, s_led[1], AR5K_GPIOCR);
1002 ath5k_hw_reg_write(ah, s_led[2], AR5K_GPIODO);
1004 /* Restore sta_id flags and preserve our mac address*/
1005 ath5k_hw_reg_write(ah,
1006 get_unaligned_le32(common->macaddr),
1008 ath5k_hw_reg_write(ah,
1009 staid1_flags | get_unaligned_le16(common->macaddr + 4),
1016 ath5k_hw_pcu_init(ah, op_mode, mode);
1018 /* Clear any pending interrupts
1019 * PISR/SISR Not available on 5210 */
1020 if (ah->ah_version != AR5K_AR5210)
1021 ath5k_hw_reg_write(ah, 0xffffffff, AR5K_PISR);
1026 ret = ath5k_hw_phy_init(ah, channel, mode, ee_mode, freq);
1028 ATH5K_ERR(ah->ah_sc,
1029 "failed to initialize PHY (%i) !\n", ret);
1034 * Configure QCUs/DCUs
1036 ret = ath5k_hw_init_queues(ah);
1042 * Initialize DMA/Interrupts
1044 ath5k_hw_dma_init(ah);
1047 /* Enable 32KHz clock function for AR5212+ chips
1048 * Set clocks to 32KHz operation and use an
1049 * external 32KHz crystal when sleeping if one
1051 if (ah->ah_version == AR5K_AR5212 &&
1052 op_mode != NL80211_IFTYPE_AP)
1053 ath5k_hw_set_sleep_clock(ah, true);
1056 * Disable beacons and reset the TSF
1058 AR5K_REG_DISABLE_BITS(ah, AR5K_BEACON, AR5K_BEACON_ENABLE);
1059 ath5k_hw_reset_tsf(ah);