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