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 * Lightly modified for gPXE, July 2009, by Joshua Oreman <oremanj@rwcr.net>.
10 * Permission to use, copy, modify, and distribute this software for any
11 * purpose with or without fee is hereby granted, provided that the above
12 * copyright notice and this permission notice appear in all copies.
14 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28 /*****************************\
29 Reset functions and helpers
30 \*****************************/
32 #include <gpxe/pci.h> /* To determine if a card is pci-e */
39 /* Find last set bit; fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32 */
46 if (!(x
& 0xffff0000u
)) {
50 if (!(x
& 0xff000000u
)) {
54 if (!(x
& 0xf0000000u
)) {
58 if (!(x
& 0xc0000000u
)) {
62 if (!(x
& 0x80000000u
)) {
71 * ath5k_hw_write_ofdm_timings - set OFDM timings on AR5212
73 * @ah: the &struct ath5k_hw
74 * @channel: the currently set channel upon reset
76 * Write the delta slope coefficient (used on pilot tracking ?) for OFDM
77 * operation on the AR5212 upon reset. This is a helper for ath5k_hw_reset().
79 * Since delta slope is floating point we split it on its exponent and
80 * mantissa and provide these values on hw.
82 * For more infos i think this patent is related
83 * http://www.freepatentsonline.com/7184495.html
85 static int ath5k_hw_write_ofdm_timings(struct ath5k_hw
*ah
,
86 struct net80211_channel
*channel
)
88 /* Get exponent and mantissa and set it */
89 u32 coef_scaled
, coef_exp
, coef_man
,
90 ds_coef_exp
, ds_coef_man
, clock
;
92 if (!(ah
->ah_version
== AR5K_AR5212
) ||
93 !(channel
->hw_value
& CHANNEL_OFDM
)) {
94 DBG("ath5k: attempt to set OFDM timings on non-OFDM channel\n");
99 * ALGO: coef = (5 * clock * carrier_freq) / 2)
100 * we scale coef by shifting clock value by 24 for
101 * better precision since we use integers */
102 /* TODO: Half/quarter rate */
103 clock
= ath5k_hw_htoclock(1, channel
->hw_value
& CHANNEL_TURBO
);
105 coef_scaled
= ((5 * (clock
<< 24)) / 2) / channel
->center_freq
;
108 * ALGO: coef_exp = 14 - highest set bit position */
109 coef_exp
= fls(coef_scaled
) - 1;
111 /* Doesn't make sense if it's zero*/
112 if (!coef_scaled
|| !coef_exp
)
115 /* Note: we've shifted coef_scaled by 24 */
116 coef_exp
= 14 - (coef_exp
- 24);
119 /* Get mantissa (significant digits)
120 * ALGO: coef_mant = floor(coef_scaled* 2^coef_exp+0.5) */
121 coef_man
= coef_scaled
+
122 (1 << (24 - coef_exp
- 1));
124 /* Calculate delta slope coefficient exponent
125 * and mantissa (remove scaling) and set them on hw */
126 ds_coef_man
= coef_man
>> (24 - coef_exp
);
127 ds_coef_exp
= coef_exp
- 16;
129 AR5K_REG_WRITE_BITS(ah
, AR5K_PHY_TIMING_3
,
130 AR5K_PHY_TIMING_3_DSC_MAN
, ds_coef_man
);
131 AR5K_REG_WRITE_BITS(ah
, AR5K_PHY_TIMING_3
,
132 AR5K_PHY_TIMING_3_DSC_EXP
, ds_coef_exp
);
139 * index into rates for control rates, we can set it up like this because
140 * this is only used for AR5212 and we know it supports G mode
142 static const unsigned int control_rates
[] =
143 { 0, 1, 1, 1, 4, 4, 6, 6, 8, 8, 8, 8 };
146 * ath5k_hw_write_rate_duration - fill rate code to duration table
148 * @ah: the &struct ath5k_hw
149 * @mode: one of enum ath5k_driver_mode
151 * Write the rate code to duration table upon hw reset. This is a helper for
152 * ath5k_hw_reset(). It seems all this is doing is setting an ACK timeout on
153 * the hardware, based on current mode, for each rate. The rates which are
154 * capable of short preamble (802.11b rates 2Mbps, 5.5Mbps, and 11Mbps) have
155 * different rate code so we write their value twice (one for long preample
156 * and one for short).
158 * Note: Band doesn't matter here, if we set the values for OFDM it works
159 * on both a and g modes. So all we have to do is set values for all g rates
160 * that include all OFDM and CCK rates. If we operate in turbo or xr/half/
161 * quarter rate mode, we need to use another set of bitrates (that's why we
162 * need the mode parameter) but we don't handle these proprietary modes yet.
164 static inline void ath5k_hw_write_rate_duration(struct ath5k_hw
*ah
,
165 unsigned int mode __unused
)
167 struct ath5k_softc
*sc
= ah
->ah_sc
;
171 /* Write rate duration table */
172 for (i
= 0; i
< sc
->hwinfo
->nr_rates
[NET80211_BAND_2GHZ
]; i
++) {
176 rate
= sc
->hwinfo
->rates
[NET80211_BAND_2GHZ
][i
];
178 /* Set ACK timeout */
179 reg
= AR5K_RATE_DUR(ath5k_bitrate_to_hw_rix(rate
));
181 /* An ACK frame consists of 10 bytes. If you add the FCS,
182 * it's 14 bytes. Note we use the control rate and not the
183 * actual rate for this rate. See mac80211 tx.c
184 * ieee80211_duration() for a brief description of
185 * what rate we should choose to TX ACKs. */
186 tx_time
= net80211_duration(sc
->dev
, 14, rate
);
188 ath5k_hw_reg_write(ah
, tx_time
, reg
);
190 if (rate
!= 20 && rate
!= 55 && rate
!= 110)
194 * We're not distinguishing short preamble here,
195 * This is true, all we'll get is a longer value here
196 * which is not necessarilly bad.
198 ath5k_hw_reg_write(ah
, tx_time
,
199 reg
+ (AR5K_SET_SHORT_PREAMBLE
<< 2));
206 static int ath5k_hw_nic_reset(struct ath5k_hw
*ah
, u32 val
)
209 u32 mask
= val
? val
: ~0U;
211 /* Read-and-clear RX Descriptor Pointer*/
212 ath5k_hw_reg_read(ah
, AR5K_RXDP
);
215 * Reset the device and wait until success
217 ath5k_hw_reg_write(ah
, val
, AR5K_RESET_CTL
);
219 /* Wait at least 128 PCI clocks */
222 if (ah
->ah_version
== AR5K_AR5210
) {
223 val
&= AR5K_RESET_CTL_PCU
| AR5K_RESET_CTL_DMA
224 | AR5K_RESET_CTL_MAC
| AR5K_RESET_CTL_PHY
;
225 mask
&= AR5K_RESET_CTL_PCU
| AR5K_RESET_CTL_DMA
226 | AR5K_RESET_CTL_MAC
| AR5K_RESET_CTL_PHY
;
228 val
&= AR5K_RESET_CTL_PCU
| AR5K_RESET_CTL_BASEBAND
;
229 mask
&= AR5K_RESET_CTL_PCU
| AR5K_RESET_CTL_BASEBAND
;
232 ret
= ath5k_hw_register_timeout(ah
, AR5K_RESET_CTL
, mask
, val
, 0);
235 * Reset configuration register (for hw byte-swap). Note that this
236 * is only set for big endian. We do the necessary magic in
239 if ((val
& AR5K_RESET_CTL_PCU
) == 0)
240 ath5k_hw_reg_write(ah
, AR5K_INIT_CFG
, AR5K_CFG
);
248 int ath5k_hw_wake(struct ath5k_hw
*ah
)
253 staid
= ath5k_hw_reg_read(ah
, AR5K_STA_ID1
);
254 staid
&= ~AR5K_STA_ID1_PWR_SV
;
256 /* Preserve sleep duration */
257 data
= ath5k_hw_reg_read(ah
, AR5K_SLEEP_CTL
);
258 if (data
& 0xffc00000)
261 data
= data
& 0xfffcffff;
263 ath5k_hw_reg_write(ah
, data
, AR5K_SLEEP_CTL
);
266 for (i
= 50; i
> 0; i
--) {
267 /* Check if the chip did wake up */
268 if ((ath5k_hw_reg_read(ah
, AR5K_PCICFG
) &
269 AR5K_PCICFG_SPWR_DN
) == 0)
272 /* Wait a bit and retry */
274 ath5k_hw_reg_write(ah
, data
, AR5K_SLEEP_CTL
);
277 /* Fail if the chip didn't wake up */
281 ath5k_hw_reg_write(ah
, staid
, AR5K_STA_ID1
);
287 * Bring up MAC + PHY Chips and program PLL
288 * TODO: Half/Quarter rate support
290 int ath5k_hw_nic_wakeup(struct ath5k_hw
*ah
, int flags
, int initial __unused
)
292 struct pci_device
*pdev
= ah
->ah_sc
->pdev
;
293 u32 turbo
, mode
, clock
, bus_flags
;
300 /* Wakeup the device */
301 ret
= ath5k_hw_wake(ah
);
303 DBG("ath5k: failed to wake up the MAC chip\n");
307 if (ah
->ah_version
!= AR5K_AR5210
) {
309 * Get channel mode flags
312 if (ah
->ah_radio
>= AR5K_RF5112
) {
313 mode
= AR5K_PHY_MODE_RAD_RF5112
;
314 clock
= AR5K_PHY_PLL_RF5112
;
316 mode
= AR5K_PHY_MODE_RAD_RF5111
; /*Zero*/
317 clock
= AR5K_PHY_PLL_RF5111
; /*Zero*/
320 if (flags
& CHANNEL_2GHZ
) {
321 mode
|= AR5K_PHY_MODE_FREQ_2GHZ
;
322 clock
|= AR5K_PHY_PLL_44MHZ
;
324 if (flags
& CHANNEL_CCK
) {
325 mode
|= AR5K_PHY_MODE_MOD_CCK
;
326 } else if (flags
& CHANNEL_OFDM
) {
327 /* XXX Dynamic OFDM/CCK is not supported by the
328 * AR5211 so we set MOD_OFDM for plain g (no
329 * CCK headers) operation. We need to test
330 * this, 5211 might support ofdm-only g after
331 * all, there are also initial register values
332 * in the code for g mode (see initvals.c). */
333 if (ah
->ah_version
== AR5K_AR5211
)
334 mode
|= AR5K_PHY_MODE_MOD_OFDM
;
336 mode
|= AR5K_PHY_MODE_MOD_DYN
;
338 DBG("ath5k: invalid radio modulation mode\n");
341 } else if (flags
& CHANNEL_5GHZ
) {
342 mode
|= AR5K_PHY_MODE_FREQ_5GHZ
;
344 if (ah
->ah_radio
== AR5K_RF5413
)
345 clock
= AR5K_PHY_PLL_40MHZ_5413
;
347 clock
|= AR5K_PHY_PLL_40MHZ
;
349 if (flags
& CHANNEL_OFDM
)
350 mode
|= AR5K_PHY_MODE_MOD_OFDM
;
352 DBG("ath5k: invalid radio modulation mode\n");
356 DBG("ath5k: invalid radio frequency mode\n");
360 if (flags
& CHANNEL_TURBO
)
361 turbo
= AR5K_PHY_TURBO_MODE
| AR5K_PHY_TURBO_SHORT
;
362 } else { /* Reset the device */
364 /* ...enable Atheros turbo mode if requested */
365 if (flags
& CHANNEL_TURBO
)
366 ath5k_hw_reg_write(ah
, AR5K_PHY_TURBO_MODE
,
370 /* reseting PCI on PCI-E cards results card to hang
371 * and always return 0xffff... so we ingore that flag
373 if (pci_find_capability(pdev
, PCI_CAP_ID_EXP
))
376 bus_flags
= AR5K_RESET_CTL_PCI
;
379 if (ah
->ah_version
== AR5K_AR5210
) {
380 ret
= ath5k_hw_nic_reset(ah
, AR5K_RESET_CTL_PCU
|
381 AR5K_RESET_CTL_MAC
| AR5K_RESET_CTL_DMA
|
382 AR5K_RESET_CTL_PHY
| AR5K_RESET_CTL_PCI
);
385 ret
= ath5k_hw_nic_reset(ah
, AR5K_RESET_CTL_PCU
|
386 AR5K_RESET_CTL_BASEBAND
| bus_flags
);
389 DBG("ath5k: failed to reset the MAC chip\n");
393 /* ...wakeup again!*/
394 ret
= ath5k_hw_wake(ah
);
396 DBG("ath5k: failed to resume the MAC chip\n");
400 /* ...final warm reset */
401 if (ath5k_hw_nic_reset(ah
, 0)) {
402 DBG("ath5k: failed to warm reset the MAC chip\n");
406 if (ah
->ah_version
!= AR5K_AR5210
) {
408 /* ...update PLL if needed */
409 if (ath5k_hw_reg_read(ah
, AR5K_PHY_PLL
) != clock
) {
410 ath5k_hw_reg_write(ah
, clock
, AR5K_PHY_PLL
);
414 /* ...set the PHY operating mode */
415 ath5k_hw_reg_write(ah
, mode
, AR5K_PHY_MODE
);
416 ath5k_hw_reg_write(ah
, turbo
, AR5K_PHY_TURBO
);
422 static int ath5k_hw_chan_has_spur_noise(struct ath5k_hw
*ah
,
423 struct net80211_channel
*channel
)
427 if ((ah
->ah_radio
== AR5K_RF5112
) ||
428 (ah
->ah_radio
== AR5K_RF5413
) ||
429 (ah
->ah_mac_version
== (AR5K_SREV_AR2417
>> 4)))
434 if ((channel
->center_freq
% refclk_freq
!= 0) &&
435 ((channel
->center_freq
% refclk_freq
< 10) ||
436 (channel
->center_freq
% refclk_freq
> 22)))
442 /* TODO: Half/Quarter rate */
443 static void ath5k_hw_tweak_initval_settings(struct ath5k_hw
*ah
,
444 struct net80211_channel
*channel
)
446 if (ah
->ah_version
== AR5K_AR5212
&&
447 ah
->ah_phy_revision
>= AR5K_SREV_PHY_5212A
) {
449 /* Setup ADC control */
450 ath5k_hw_reg_write(ah
,
452 AR5K_PHY_ADC_CTL_INBUFGAIN_OFF
) |
454 AR5K_PHY_ADC_CTL_INBUFGAIN_ON
) |
455 AR5K_PHY_ADC_CTL_PWD_DAC_OFF
|
456 AR5K_PHY_ADC_CTL_PWD_ADC_OFF
),
461 /* Disable barker RSSI threshold */
462 AR5K_REG_DISABLE_BITS(ah
, AR5K_PHY_DAG_CCK_CTL
,
463 AR5K_PHY_DAG_CCK_CTL_EN_RSSI_THR
);
465 AR5K_REG_WRITE_BITS(ah
, AR5K_PHY_DAG_CCK_CTL
,
466 AR5K_PHY_DAG_CCK_CTL_RSSI_THR
, 2);
468 /* Set the mute mask */
469 ath5k_hw_reg_write(ah
, 0x0000000f, AR5K_SEQ_MASK
);
472 /* Clear PHY_BLUETOOTH to allow RX_CLEAR line debug */
473 if (ah
->ah_phy_revision
>= AR5K_SREV_PHY_5212B
)
474 ath5k_hw_reg_write(ah
, 0, AR5K_PHY_BLUETOOTH
);
476 /* Enable DCU double buffering */
477 if (ah
->ah_phy_revision
> AR5K_SREV_PHY_5212B
)
478 AR5K_REG_DISABLE_BITS(ah
, AR5K_TXCFG
,
479 AR5K_TXCFG_DCU_DBL_BUF_DIS
);
481 /* Set DAC/ADC delays */
482 if (ah
->ah_version
== AR5K_AR5212
) {
484 if (ah
->ah_mac_version
== (AR5K_SREV_AR2417
>> 4))
485 scal
= AR5K_PHY_SCAL_32MHZ_2417
;
486 else if (ath5k_eeprom_is_hb63(ah
))
487 scal
= AR5K_PHY_SCAL_32MHZ_HB63
;
489 scal
= AR5K_PHY_SCAL_32MHZ
;
490 ath5k_hw_reg_write(ah
, scal
, AR5K_PHY_SCAL
);
494 if ((ah
->ah_radio
== AR5K_RF5413
) ||
495 (ah
->ah_mac_version
== (AR5K_SREV_AR2417
>> 4))) {
498 if (channel
->center_freq
== 2462 ||
499 channel
->center_freq
== 2467)
502 /* Only update if needed */
503 if (ath5k_hw_reg_read(ah
, AR5K_PHY_FAST_ADC
) != fast_adc
)
504 ath5k_hw_reg_write(ah
, fast_adc
,
508 /* Fix for first revision of the RF5112 RF chipset */
509 if (ah
->ah_radio
== AR5K_RF5112
&&
510 ah
->ah_radio_5ghz_revision
<
511 AR5K_SREV_RAD_5112A
) {
513 ath5k_hw_reg_write(ah
, AR5K_PHY_CCKTXCTL_WORLD
,
515 if (channel
->hw_value
& CHANNEL_5GHZ
)
519 ath5k_hw_reg_write(ah
, data
, AR5K_PHY_FRAME_CTL
);
522 if (ah
->ah_mac_srev
< AR5K_SREV_AR5211
) {
524 /* 5311 has different tx/rx latency masks
525 * from 5211, since we deal 5311 the same
526 * as 5211 when setting initvals, shift
527 * values here to their proper locations */
528 usec_reg
= ath5k_hw_reg_read(ah
, AR5K_USEC_5211
);
529 ath5k_hw_reg_write(ah
, usec_reg
& (AR5K_USEC_1
|
531 AR5K_USEC_TX_LATENCY_5211
|
533 AR5K_USEC_RX_LATENCY_5210
)),
535 /* Clear QCU/DCU clock gating register */
536 ath5k_hw_reg_write(ah
, 0, AR5K_QCUDCU_CLKGT
);
537 /* Set DAC/ADC delays */
538 ath5k_hw_reg_write(ah
, 0x08, AR5K_PHY_SCAL
);
539 /* Enable PCU FIFO corruption ECO */
540 AR5K_REG_ENABLE_BITS(ah
, AR5K_DIAG_SW_5211
,
541 AR5K_DIAG_SW_ECO_ENABLE
);
545 static void ath5k_hw_commit_eeprom_settings(struct ath5k_hw
*ah
,
546 struct net80211_channel
*channel
, u8
*ant
, u8 ee_mode
)
548 struct ath5k_eeprom_info
*ee
= &ah
->ah_capabilities
.cap_eeprom
;
549 s16 cck_ofdm_pwr_delta
;
551 /* Adjust power delta for channel 14 */
552 if (channel
->center_freq
== 2484)
554 ((ee
->ee_cck_ofdm_power_delta
-
555 ee
->ee_scaled_cck_delta
) * 2) / 10;
558 (ee
->ee_cck_ofdm_power_delta
* 2) / 10;
560 /* Set CCK to OFDM power delta on tx power
561 * adjustment register */
562 if (ah
->ah_phy_revision
>= AR5K_SREV_PHY_5212A
) {
563 if (channel
->hw_value
== CHANNEL_G
)
564 ath5k_hw_reg_write(ah
,
565 AR5K_REG_SM((ee
->ee_cck_ofdm_gain_delta
* -1),
566 AR5K_PHY_TX_PWR_ADJ_CCK_GAIN_DELTA
) |
567 AR5K_REG_SM((cck_ofdm_pwr_delta
* -1),
568 AR5K_PHY_TX_PWR_ADJ_CCK_PCDAC_INDEX
),
569 AR5K_PHY_TX_PWR_ADJ
);
571 ath5k_hw_reg_write(ah
, 0, AR5K_PHY_TX_PWR_ADJ
);
573 /* For older revs we scale power on sw during tx power
575 ah
->ah_txpower
.txp_cck_ofdm_pwr_delta
= cck_ofdm_pwr_delta
;
576 ah
->ah_txpower
.txp_cck_ofdm_gainf_delta
=
577 ee
->ee_cck_ofdm_gain_delta
;
580 /* Set antenna idle switch table */
581 AR5K_REG_WRITE_BITS(ah
, AR5K_PHY_ANT_CTL
,
582 AR5K_PHY_ANT_CTL_SWTABLE_IDLE
,
583 (ah
->ah_antenna
[ee_mode
][0] |
584 AR5K_PHY_ANT_CTL_TXRX_EN
));
586 /* Set antenna switch table */
587 ath5k_hw_reg_write(ah
, ah
->ah_antenna
[ee_mode
][ant
[0]],
588 AR5K_PHY_ANT_SWITCH_TABLE_0
);
589 ath5k_hw_reg_write(ah
, ah
->ah_antenna
[ee_mode
][ant
[1]],
590 AR5K_PHY_ANT_SWITCH_TABLE_1
);
592 /* Noise floor threshold */
593 ath5k_hw_reg_write(ah
,
594 AR5K_PHY_NF_SVAL(ee
->ee_noise_floor_thr
[ee_mode
]),
597 if ((channel
->hw_value
& CHANNEL_TURBO
) &&
598 (ah
->ah_ee_version
>= AR5K_EEPROM_VERSION_5_0
)) {
599 /* Switch settling time (Turbo) */
600 AR5K_REG_WRITE_BITS(ah
, AR5K_PHY_SETTLING
,
601 AR5K_PHY_SETTLING_SWITCH
,
602 ee
->ee_switch_settling_turbo
[ee_mode
]);
604 /* Tx/Rx attenuation (Turbo) */
605 AR5K_REG_WRITE_BITS(ah
, AR5K_PHY_GAIN
,
606 AR5K_PHY_GAIN_TXRX_ATTEN
,
607 ee
->ee_atn_tx_rx_turbo
[ee_mode
]);
609 /* ADC/PGA desired size (Turbo) */
610 AR5K_REG_WRITE_BITS(ah
, AR5K_PHY_DESIRED_SIZE
,
611 AR5K_PHY_DESIRED_SIZE_ADC
,
612 ee
->ee_adc_desired_size_turbo
[ee_mode
]);
614 AR5K_REG_WRITE_BITS(ah
, AR5K_PHY_DESIRED_SIZE
,
615 AR5K_PHY_DESIRED_SIZE_PGA
,
616 ee
->ee_pga_desired_size_turbo
[ee_mode
]);
618 /* Tx/Rx margin (Turbo) */
619 AR5K_REG_WRITE_BITS(ah
, AR5K_PHY_GAIN_2GHZ
,
620 AR5K_PHY_GAIN_2GHZ_MARGIN_TXRX
,
621 ee
->ee_margin_tx_rx_turbo
[ee_mode
]);
624 /* Switch settling time */
625 AR5K_REG_WRITE_BITS(ah
, AR5K_PHY_SETTLING
,
626 AR5K_PHY_SETTLING_SWITCH
,
627 ee
->ee_switch_settling
[ee_mode
]);
629 /* Tx/Rx attenuation */
630 AR5K_REG_WRITE_BITS(ah
, AR5K_PHY_GAIN
,
631 AR5K_PHY_GAIN_TXRX_ATTEN
,
632 ee
->ee_atn_tx_rx
[ee_mode
]);
634 /* ADC/PGA desired size */
635 AR5K_REG_WRITE_BITS(ah
, AR5K_PHY_DESIRED_SIZE
,
636 AR5K_PHY_DESIRED_SIZE_ADC
,
637 ee
->ee_adc_desired_size
[ee_mode
]);
639 AR5K_REG_WRITE_BITS(ah
, AR5K_PHY_DESIRED_SIZE
,
640 AR5K_PHY_DESIRED_SIZE_PGA
,
641 ee
->ee_pga_desired_size
[ee_mode
]);
644 if (ah
->ah_ee_version
>= AR5K_EEPROM_VERSION_4_1
)
645 AR5K_REG_WRITE_BITS(ah
, AR5K_PHY_GAIN_2GHZ
,
646 AR5K_PHY_GAIN_2GHZ_MARGIN_TXRX
,
647 ee
->ee_margin_tx_rx
[ee_mode
]);
651 ath5k_hw_reg_write(ah
,
652 (ee
->ee_tx_end2xpa_disable
[ee_mode
] << 24) |
653 (ee
->ee_tx_end2xpa_disable
[ee_mode
] << 16) |
654 (ee
->ee_tx_frm2xpa_enable
[ee_mode
] << 8) |
655 (ee
->ee_tx_frm2xpa_enable
[ee_mode
]), AR5K_PHY_RF_CTL4
);
658 AR5K_REG_WRITE_BITS(ah
, AR5K_PHY_RF_CTL3
,
659 AR5K_PHY_RF_CTL3_TXE2XLNA_ON
,
660 ee
->ee_tx_end2xlna_enable
[ee_mode
]);
663 AR5K_REG_WRITE_BITS(ah
, AR5K_PHY_NF
,
664 AR5K_PHY_NF_THRESH62
,
665 ee
->ee_thr_62
[ee_mode
]);
668 /* False detect backoff for channels
669 * that have spur noise. Write the new
670 * cyclic power RSSI threshold. */
671 if (ath5k_hw_chan_has_spur_noise(ah
, channel
))
672 AR5K_REG_WRITE_BITS(ah
, AR5K_PHY_OFDM_SELFCORR
,
673 AR5K_PHY_OFDM_SELFCORR_CYPWR_THR1
,
674 AR5K_INIT_CYCRSSI_THR1
+
675 ee
->ee_false_detect
[ee_mode
]);
677 AR5K_REG_WRITE_BITS(ah
, AR5K_PHY_OFDM_SELFCORR
,
678 AR5K_PHY_OFDM_SELFCORR_CYPWR_THR1
,
679 AR5K_INIT_CYCRSSI_THR1
);
682 * TODO: Per channel i/q infos ? */
683 AR5K_REG_ENABLE_BITS(ah
, AR5K_PHY_IQ
,
684 AR5K_PHY_IQ_CORR_ENABLE
|
685 (ee
->ee_i_cal
[ee_mode
] << AR5K_PHY_IQ_CORR_Q_I_COFF_S
) |
686 ee
->ee_q_cal
[ee_mode
]);
688 /* Heavy clipping -disable for now */
689 if (ah
->ah_ee_version
>= AR5K_EEPROM_VERSION_5_1
)
690 ath5k_hw_reg_write(ah
, 0, AR5K_PHY_HEAVY_CLIP_ENABLE
);
696 * Main reset function
698 int ath5k_hw_reset(struct ath5k_hw
*ah
,
699 struct net80211_channel
*channel
, int change_channel
)
701 u32 s_seq
[10], s_ant
, s_led
[3], staid1_flags
;
703 u8 mode
, freq
, ee_mode
, ant
[2];
713 * Save some registers before a reset
715 /*DCU/Antenna selection not available on 5210*/
716 if (ah
->ah_version
!= AR5K_AR5210
) {
718 switch (channel
->hw_value
& CHANNEL_MODES
) {
720 mode
= AR5K_MODE_11A
;
721 freq
= AR5K_INI_RFGAIN_5GHZ
;
722 ee_mode
= AR5K_EEPROM_MODE_11A
;
725 mode
= AR5K_MODE_11G
;
726 freq
= AR5K_INI_RFGAIN_2GHZ
;
727 ee_mode
= AR5K_EEPROM_MODE_11G
;
730 mode
= AR5K_MODE_11B
;
731 freq
= AR5K_INI_RFGAIN_2GHZ
;
732 ee_mode
= AR5K_EEPROM_MODE_11B
;
735 mode
= AR5K_MODE_11A_TURBO
;
736 freq
= AR5K_INI_RFGAIN_5GHZ
;
737 ee_mode
= AR5K_EEPROM_MODE_11A
;
740 if (ah
->ah_version
== AR5K_AR5211
) {
741 DBG("ath5k: TurboG not available on 5211\n");
744 mode
= AR5K_MODE_11G_TURBO
;
745 freq
= AR5K_INI_RFGAIN_2GHZ
;
746 ee_mode
= AR5K_EEPROM_MODE_11G
;
749 if (ah
->ah_version
== AR5K_AR5211
) {
750 DBG("ath5k: XR mode not available on 5211\n");
754 freq
= AR5K_INI_RFGAIN_5GHZ
;
755 ee_mode
= AR5K_EEPROM_MODE_11A
;
758 DBG("ath5k: invalid channel (%d MHz)\n",
759 channel
->center_freq
);
763 if (change_channel
) {
765 * Save frame sequence count
766 * For revs. after Oahu, only save
767 * seq num for DCU 0 (Global seq num)
769 if (ah
->ah_mac_srev
< AR5K_SREV_AR5211
) {
771 for (i
= 0; i
< 10; i
++)
772 s_seq
[i
] = ath5k_hw_reg_read(ah
,
773 AR5K_QUEUE_DCU_SEQNUM(i
));
776 s_seq
[0] = ath5k_hw_reg_read(ah
,
777 AR5K_QUEUE_DCU_SEQNUM(0));
781 /* Save default antenna */
782 s_ant
= ath5k_hw_reg_read(ah
, AR5K_DEFAULT_ANTENNA
);
784 if (ah
->ah_version
== AR5K_AR5212
) {
785 /* Since we are going to write rf buffer
786 * check if we have any pending gain_F
787 * optimization settings */
788 if (change_channel
&& ah
->ah_rf_banks
!= NULL
)
789 ath5k_hw_gainf_calibrate(ah
);
794 s_led
[0] = ath5k_hw_reg_read(ah
, AR5K_PCICFG
) &
795 AR5K_PCICFG_LEDSTATE
;
796 s_led
[1] = ath5k_hw_reg_read(ah
, AR5K_GPIOCR
);
797 s_led
[2] = ath5k_hw_reg_read(ah
, AR5K_GPIODO
);
799 /* AR5K_STA_ID1 flags, only preserve antenna
800 * settings and ack/cts rate mode */
801 staid1_flags
= ath5k_hw_reg_read(ah
, AR5K_STA_ID1
) &
802 (AR5K_STA_ID1_DEFAULT_ANTENNA
|
803 AR5K_STA_ID1_DESC_ANTENNA
|
804 AR5K_STA_ID1_RTS_DEF_ANTENNA
|
805 AR5K_STA_ID1_ACKCTS_6MB
|
806 AR5K_STA_ID1_BASE_RATE_11B
|
807 AR5K_STA_ID1_SELFGEN_DEF_ANT
);
809 /* Wakeup the device */
810 ret
= ath5k_hw_nic_wakeup(ah
, channel
->hw_value
, 0);
814 /* PHY access enable */
815 if (ah
->ah_mac_srev
>= AR5K_SREV_AR5211
)
816 ath5k_hw_reg_write(ah
, AR5K_PHY_SHIFT_5GHZ
, AR5K_PHY(0));
818 ath5k_hw_reg_write(ah
, AR5K_PHY_SHIFT_5GHZ
| 0x40,
821 /* Write initial settings */
822 ret
= ath5k_hw_write_initvals(ah
, mode
, change_channel
);
829 if (ah
->ah_version
!= AR5K_AR5210
) {
832 * Write initial RF gain settings
833 * This should work for both 5111/5112
835 ret
= ath5k_hw_rfgain_init(ah
, freq
);
842 * Tweak initval settings for revised
843 * chipsets and add some more config
846 ath5k_hw_tweak_initval_settings(ah
, channel
);
849 * Set TX power (FIXME)
851 ret
= ath5k_hw_txpower(ah
, channel
, ee_mode
,
852 AR5K_TUNE_DEFAULT_TXPOWER
);
856 /* Write rate duration table only on AR5212 */
857 if (ah
->ah_version
== AR5K_AR5212
)
858 ath5k_hw_write_rate_duration(ah
, mode
);
863 ret
= ath5k_hw_rfregs_init(ah
, channel
, mode
);
868 /* Write OFDM timings on 5212*/
869 if (ah
->ah_version
== AR5K_AR5212
&&
870 channel
->hw_value
& CHANNEL_OFDM
) {
871 ret
= ath5k_hw_write_ofdm_timings(ah
, channel
);
876 /*Enable/disable 802.11b mode on 5111
877 (enable 2111 frequency converter + CCK)*/
878 if (ah
->ah_radio
== AR5K_RF5111
) {
879 if (mode
== AR5K_MODE_11B
)
880 AR5K_REG_ENABLE_BITS(ah
, AR5K_TXCFG
,
883 AR5K_REG_DISABLE_BITS(ah
, AR5K_TXCFG
,
888 * In case a fixed antenna was set as default
889 * write the same settings on both AR5K_PHY_ANT_SWITCH_TABLE
893 if (s_ant
== AR5K_ANT_FIXED_A
) /* 1 - Main */
894 ant
[0] = ant
[1] = AR5K_ANT_FIXED_A
;
896 ant
[0] = ant
[1] = AR5K_ANT_FIXED_B
;
898 ant
[0] = AR5K_ANT_FIXED_A
;
899 ant
[1] = AR5K_ANT_FIXED_B
;
902 /* Commit values from EEPROM */
903 ath5k_hw_commit_eeprom_settings(ah
, channel
, ant
, ee_mode
);
907 * For 5210 we do all initialization using
908 * initvals, so we don't have to modify
909 * any settings (5210 also only supports
913 /* Disable phy and wait */
914 ath5k_hw_reg_write(ah
, AR5K_PHY_ACT_DISABLE
, AR5K_PHY_ACT
);
919 * Restore saved values
922 /*DCU/Antenna selection not available on 5210*/
923 if (ah
->ah_version
!= AR5K_AR5210
) {
925 if (change_channel
) {
926 if (ah
->ah_mac_srev
< AR5K_SREV_AR5211
) {
927 for (i
= 0; i
< 10; i
++)
928 ath5k_hw_reg_write(ah
, s_seq
[i
],
929 AR5K_QUEUE_DCU_SEQNUM(i
));
931 ath5k_hw_reg_write(ah
, s_seq
[0],
932 AR5K_QUEUE_DCU_SEQNUM(0));
936 ath5k_hw_reg_write(ah
, s_ant
, AR5K_DEFAULT_ANTENNA
);
940 AR5K_REG_ENABLE_BITS(ah
, AR5K_PCICFG
, s_led
[0]);
943 ath5k_hw_reg_write(ah
, s_led
[1], AR5K_GPIOCR
);
944 ath5k_hw_reg_write(ah
, s_led
[2], AR5K_GPIODO
);
946 /* Restore sta_id flags and preserve our mac address*/
947 ath5k_hw_reg_write(ah
, AR5K_LOW_ID(ah
->ah_sta_id
),
949 ath5k_hw_reg_write(ah
, staid1_flags
| AR5K_HIGH_ID(ah
->ah_sta_id
),
957 /* Restore bssid and bssid mask */
958 /* XXX: add ah->aid once mac80211 gives this to us */
959 ath5k_hw_set_associd(ah
, ah
->ah_bssid
, 0);
962 ath5k_hw_set_opmode(ah
);
964 /* Clear any pending interrupts
965 * PISR/SISR Not available on 5210 */
966 if (ah
->ah_version
!= AR5K_AR5210
)
967 ath5k_hw_reg_write(ah
, 0xffffffff, AR5K_PISR
);
969 /* Set RSSI/BRSSI thresholds
971 * Note: If we decide to set this value
972 * dynamicaly, have in mind that when AR5K_RSSI_THR
973 * register is read it might return 0x40 if we haven't
974 * wrote anything to it plus BMISS RSSI threshold is zeroed.
975 * So doing a save/restore procedure here isn't the right
976 * choice. Instead store it on ath5k_hw */
977 ath5k_hw_reg_write(ah
, (AR5K_TUNE_RSSI_THRES
|
978 AR5K_TUNE_BMISS_THRES
<<
979 AR5K_RSSI_THR_BMISS_S
),
982 /* MIC QoS support */
983 if (ah
->ah_mac_srev
>= AR5K_SREV_AR2413
) {
984 ath5k_hw_reg_write(ah
, 0x000100aa, AR5K_MIC_QOS_CTL
);
985 ath5k_hw_reg_write(ah
, 0x00003210, AR5K_MIC_QOS_SEL
);
988 /* QoS NOACK Policy */
989 if (ah
->ah_version
== AR5K_AR5212
) {
990 ath5k_hw_reg_write(ah
,
991 AR5K_REG_SM(2, AR5K_QOS_NOACK_2BIT_VALUES
) |
992 AR5K_REG_SM(5, AR5K_QOS_NOACK_BIT_OFFSET
) |
993 AR5K_REG_SM(0, AR5K_QOS_NOACK_BYTE_OFFSET
),
1002 /* Set channel on PHY */
1003 ret
= ath5k_hw_channel(ah
, channel
);
1008 * Enable the PHY and wait until completion
1009 * This includes BaseBand and Synthesizer
1012 ath5k_hw_reg_write(ah
, AR5K_PHY_ACT_ENABLE
, AR5K_PHY_ACT
);
1015 * On 5211+ read activation -> rx delay
1018 * TODO: Half/quarter rate support
1020 if (ah
->ah_version
!= AR5K_AR5210
) {
1022 delay
= ath5k_hw_reg_read(ah
, AR5K_PHY_RX_DELAY
) &
1023 AR5K_PHY_RX_DELAY_M
;
1024 delay
= (channel
->hw_value
& CHANNEL_CCK
) ?
1025 ((delay
<< 2) / 22) : (delay
/ 10);
1027 udelay(100 + (2 * delay
));
1033 * Perform ADC test to see if baseband is ready
1034 * Set tx hold and check adc test register
1036 phy_tst1
= ath5k_hw_reg_read(ah
, AR5K_PHY_TST1
);
1037 ath5k_hw_reg_write(ah
, AR5K_PHY_TST1_TXHOLD
, AR5K_PHY_TST1
);
1038 for (i
= 0; i
<= 20; i
++) {
1039 if (!(ath5k_hw_reg_read(ah
, AR5K_PHY_ADC_TEST
) & 0x10))
1043 ath5k_hw_reg_write(ah
, phy_tst1
, AR5K_PHY_TST1
);
1046 * Start automatic gain control calibration
1048 * During AGC calibration RX path is re-routed to
1049 * a power detector so we don't receive anything.
1051 * This method is used to calibrate some static offsets
1052 * used together with on-the fly I/Q calibration (the
1053 * one performed via ath5k_hw_phy_calibrate), that doesn't
1054 * interrupt rx path.
1056 * While rx path is re-routed to the power detector we also
1057 * start a noise floor calibration, to measure the
1058 * card's noise floor (the noise we measure when we are not
1059 * transmiting or receiving anything).
1061 * If we are in a noisy environment AGC calibration may time
1062 * out and/or noise floor calibration might timeout.
1064 AR5K_REG_ENABLE_BITS(ah
, AR5K_PHY_AGCCTL
,
1065 AR5K_PHY_AGCCTL_CAL
);
1067 /* At the same time start I/Q calibration for QAM constellation
1068 * -no need for CCK- */
1069 ah
->ah_calibration
= 0;
1070 if (!(mode
== AR5K_MODE_11B
)) {
1071 ah
->ah_calibration
= 1;
1072 AR5K_REG_WRITE_BITS(ah
, AR5K_PHY_IQ
,
1073 AR5K_PHY_IQ_CAL_NUM_LOG_MAX
, 15);
1074 AR5K_REG_ENABLE_BITS(ah
, AR5K_PHY_IQ
,
1078 /* Wait for gain calibration to finish (we check for I/Q calibration
1079 * during ath5k_phy_calibrate) */
1080 if (ath5k_hw_register_timeout(ah
, AR5K_PHY_AGCCTL
,
1081 AR5K_PHY_AGCCTL_CAL
, 0, 0)) {
1082 DBG("ath5k: gain calibration timeout (%d MHz)\n",
1083 channel
->center_freq
);
1087 * If we run NF calibration before AGC, it always times out.
1088 * Binary HAL starts NF and AGC calibration at the same time
1089 * and only waits for AGC to finish. Also if AGC or NF cal.
1090 * times out, reset doesn't fail on binary HAL. I believe
1091 * that's wrong because since rx path is routed to a detector,
1092 * if cal. doesn't finish we won't have RX. Sam's HAL for AR5210/5211
1093 * enables noise floor calibration after offset calibration and if noise
1094 * floor calibration fails, reset fails. I believe that's
1095 * a better approach, we just need to find a polling interval
1096 * that suits best, even if reset continues we need to make
1097 * sure that rx path is ready.
1099 ath5k_hw_noise_floor_calibration(ah
, channel
->center_freq
);
1103 * Configure QCUs/DCUs
1106 /* TODO: HW Compression support for data queues */
1107 /* TODO: Burst prefetch for data queues */
1110 * Reset queues and start beacon timers at the end of the reset routine
1111 * This also sets QCU mask on each DCU for 1:1 qcu to dcu mapping
1112 * Note: If we want we can assign multiple qcus on one dcu.
1114 ret
= ath5k_hw_reset_tx_queue(ah
);
1116 DBG("ath5k: failed to reset TX queue\n");
1121 * Configure DMA/Interrupts
1125 * Set Rx/Tx DMA Configuration
1127 * Set standard DMA size (128). Note that
1128 * a DMA size of 512 causes rx overruns and tx errors
1129 * on pci-e cards (tested on 5424 but since rx overruns
1130 * also occur on 5416/5418 with madwifi we set 128
1131 * for all PCI-E cards to be safe).
1133 * XXX: need to check 5210 for this
1134 * TODO: Check out tx triger level, it's always 64 on dumps but I
1135 * guess we can tweak it and see how it goes ;-)
1137 if (ah
->ah_version
!= AR5K_AR5210
) {
1138 AR5K_REG_WRITE_BITS(ah
, AR5K_TXCFG
,
1139 AR5K_TXCFG_SDMAMR
, AR5K_DMASIZE_128B
);
1140 AR5K_REG_WRITE_BITS(ah
, AR5K_RXCFG
,
1141 AR5K_RXCFG_SDMAMW
, AR5K_DMASIZE_128B
);
1144 /* Pre-enable interrupts on 5211/5212*/
1145 if (ah
->ah_version
!= AR5K_AR5210
)
1146 ath5k_hw_set_imr(ah
, ah
->ah_imr
);
1149 * Setup RFKill interrupt if rfkill flag is set on eeprom.
1150 * TODO: Use gpio pin and polarity infos from eeprom
1151 * TODO: Handle this in ath5k_intr because it'll result
1152 * a nasty interrupt storm.
1155 if (AR5K_EEPROM_HDR_RFKILL(ah
->ah_capabilities
.cap_eeprom
.ee_header
)) {
1156 ath5k_hw_set_gpio_input(ah
, 0);
1157 ah
->ah_gpio
[0] = ath5k_hw_get_gpio(ah
, 0);
1158 if (ah
->ah_gpio
[0] == 0)
1159 ath5k_hw_set_gpio_intr(ah
, 0, 1);
1161 ath5k_hw_set_gpio_intr(ah
, 0, 0);
1166 * Disable beacons and reset the register
1168 AR5K_REG_DISABLE_BITS(ah
, AR5K_BEACON
, AR5K_BEACON_ENABLE
|
1169 AR5K_BEACON_RESET_TSF
);