OSDN Git Service

ath5k: Reimplement clock rate to usec conversion
[uclinux-h8/linux.git] / drivers / net / wireless / ath / 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 <asm/unaligned.h>
29
30 #include <linux/pci.h>          /* To determine if a card is pci-e */
31 #include <linux/log2.h>
32 #include "ath5k.h"
33 #include "reg.h"
34 #include "base.h"
35 #include "debug.h"
36
37 /**
38  * ath5k_hw_write_ofdm_timings - set OFDM timings on AR5212
39  *
40  * @ah: the &struct ath5k_hw
41  * @channel: the currently set channel upon reset
42  *
43  * Write the delta slope coefficient (used on pilot tracking ?) for OFDM
44  * operation on the AR5212 upon reset. This is a helper for ath5k_hw_reset().
45  *
46  * Since delta slope is floating point we split it on its exponent and
47  * mantissa and provide these values on hw.
48  *
49  * For more infos i think this patent is related
50  * http://www.freepatentsonline.com/7184495.html
51  */
52 static inline int ath5k_hw_write_ofdm_timings(struct ath5k_hw *ah,
53         struct ieee80211_channel *channel)
54 {
55         /* Get exponent and mantissa and set it */
56         u32 coef_scaled, coef_exp, coef_man,
57                 ds_coef_exp, ds_coef_man, clock;
58
59         BUG_ON(!(ah->ah_version == AR5K_AR5212) ||
60                 !(channel->hw_value & CHANNEL_OFDM));
61
62         /* Get coefficient
63          * ALGO: coef = (5 * clock / carrier_freq) / 2
64          * we scale coef by shifting clock value by 24 for
65          * better precision since we use integers */
66         /* TODO: Half/quarter rate */
67         clock =  (channel->hw_value & CHANNEL_TURBO) ? 80 : 40;
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 = ilog2(coef_scaled);
73
74         /* Doesn't make sense if it's zero*/
75         if (!coef_scaled || !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                 data = ath5k_hw_reg_read(ah, AR5K_SLEEP_CTL);
263
264                 /* If card is down we 'll get 0xffff... so we
265                  * need to clean this up before we write the register
266                  */
267                 if (data & 0xffc00000)
268                         data = 0;
269                 else
270                         /* Preserve sleep duration etc */
271                         data = data & ~AR5K_SLEEP_CTL_SLE;
272
273                 ath5k_hw_reg_write(ah, data | AR5K_SLEEP_CTL_SLE_WAKE,
274                                                         AR5K_SLEEP_CTL);
275                 udelay(15);
276
277                 for (i = 200; i > 0; i--) {
278                         /* Check if the chip did wake up */
279                         if ((ath5k_hw_reg_read(ah, AR5K_PCICFG) &
280                                         AR5K_PCICFG_SPWR_DN) == 0)
281                                 break;
282
283                         /* Wait a bit and retry */
284                         udelay(50);
285                         ath5k_hw_reg_write(ah, data | AR5K_SLEEP_CTL_SLE_WAKE,
286                                                         AR5K_SLEEP_CTL);
287                 }
288
289                 /* Fail if the chip didn't wake up */
290                 if (i == 0)
291                         return -EIO;
292
293                 break;
294
295         default:
296                 return -EINVAL;
297         }
298
299 commit:
300         ath5k_hw_reg_write(ah, staid, AR5K_STA_ID1);
301
302         return 0;
303 }
304
305 /*
306  * Put device on hold
307  *
308  * Put MAC and Baseband on warm reset and
309  * keep that state (don't clean sleep control
310  * register). After this MAC and Baseband are
311  * disabled and a full reset is needed to come
312  * back. This way we save as much power as possible
313  * without puting the card on full sleep.
314  */
315 int ath5k_hw_on_hold(struct ath5k_hw *ah)
316 {
317         struct pci_dev *pdev = ah->ah_sc->pdev;
318         u32 bus_flags;
319         int ret;
320
321         /* Make sure device is awake */
322         ret = ath5k_hw_set_power(ah, AR5K_PM_AWAKE, true, 0);
323         if (ret) {
324                 ATH5K_ERR(ah->ah_sc, "failed to wakeup the MAC Chip\n");
325                 return ret;
326         }
327
328         /*
329          * Put chipset on warm reset...
330          *
331          * Note: puting PCI core on warm reset on PCI-E cards
332          * results card to hang and always return 0xffff... so
333          * we ingore that flag for PCI-E cards. On PCI cards
334          * this flag gets cleared after 64 PCI clocks.
335          */
336         bus_flags = (pdev->is_pcie) ? 0 : AR5K_RESET_CTL_PCI;
337
338         if (ah->ah_version == AR5K_AR5210) {
339                 ret = ath5k_hw_nic_reset(ah, AR5K_RESET_CTL_PCU |
340                         AR5K_RESET_CTL_MAC | AR5K_RESET_CTL_DMA |
341                         AR5K_RESET_CTL_PHY | AR5K_RESET_CTL_PCI);
342                         mdelay(2);
343         } else {
344                 ret = ath5k_hw_nic_reset(ah, AR5K_RESET_CTL_PCU |
345                         AR5K_RESET_CTL_BASEBAND | bus_flags);
346         }
347
348         if (ret) {
349                 ATH5K_ERR(ah->ah_sc, "failed to put device on warm reset\n");
350                 return -EIO;
351         }
352
353         /* ...wakeup again!*/
354         ret = ath5k_hw_set_power(ah, AR5K_PM_AWAKE, true, 0);
355         if (ret) {
356                 ATH5K_ERR(ah->ah_sc, "failed to put device on hold\n");
357                 return ret;
358         }
359
360         return ret;
361 }
362
363 /*
364  * Bring up MAC + PHY Chips and program PLL
365  * TODO: Half/Quarter rate support
366  */
367 int ath5k_hw_nic_wakeup(struct ath5k_hw *ah, int flags, bool initial)
368 {
369         struct pci_dev *pdev = ah->ah_sc->pdev;
370         u32 turbo, mode, clock, bus_flags;
371         int ret;
372
373         turbo = 0;
374         mode = 0;
375         clock = 0;
376
377         ATH5K_TRACE(ah->ah_sc);
378
379         /* Wakeup the device */
380         ret = ath5k_hw_set_power(ah, AR5K_PM_AWAKE, true, 0);
381         if (ret) {
382                 ATH5K_ERR(ah->ah_sc, "failed to wakeup the MAC Chip\n");
383                 return ret;
384         }
385
386         /*
387          * Put chipset on warm reset...
388          *
389          * Note: puting PCI core on warm reset on PCI-E cards
390          * results card to hang and always return 0xffff... so
391          * we ingore that flag for PCI-E cards. On PCI cards
392          * this flag gets cleared after 64 PCI clocks.
393          */
394         bus_flags = (pdev->is_pcie) ? 0 : AR5K_RESET_CTL_PCI;
395
396         if (ah->ah_version == AR5K_AR5210) {
397                 ret = ath5k_hw_nic_reset(ah, AR5K_RESET_CTL_PCU |
398                         AR5K_RESET_CTL_MAC | AR5K_RESET_CTL_DMA |
399                         AR5K_RESET_CTL_PHY | AR5K_RESET_CTL_PCI);
400                         mdelay(2);
401         } else {
402                 ret = ath5k_hw_nic_reset(ah, AR5K_RESET_CTL_PCU |
403                         AR5K_RESET_CTL_BASEBAND | bus_flags);
404         }
405
406         if (ret) {
407                 ATH5K_ERR(ah->ah_sc, "failed to reset the MAC Chip\n");
408                 return -EIO;
409         }
410
411         /* ...wakeup again!...*/
412         ret = ath5k_hw_set_power(ah, AR5K_PM_AWAKE, true, 0);
413         if (ret) {
414                 ATH5K_ERR(ah->ah_sc, "failed to resume the MAC Chip\n");
415                 return ret;
416         }
417
418         /* ...clear reset control register and pull device out of
419          * warm reset */
420         if (ath5k_hw_nic_reset(ah, 0)) {
421                 ATH5K_ERR(ah->ah_sc, "failed to warm reset the MAC Chip\n");
422                 return -EIO;
423         }
424
425         /* On initialization skip PLL programming since we don't have
426          * a channel / mode set yet */
427         if (initial)
428                 return 0;
429
430         if (ah->ah_version != AR5K_AR5210) {
431                 /*
432                  * Get channel mode flags
433                  */
434
435                 if (ah->ah_radio >= AR5K_RF5112) {
436                         mode = AR5K_PHY_MODE_RAD_RF5112;
437                         clock = AR5K_PHY_PLL_RF5112;
438                 } else {
439                         mode = AR5K_PHY_MODE_RAD_RF5111;        /*Zero*/
440                         clock = AR5K_PHY_PLL_RF5111;            /*Zero*/
441                 }
442
443                 if (flags & CHANNEL_2GHZ) {
444                         mode |= AR5K_PHY_MODE_FREQ_2GHZ;
445                         clock |= AR5K_PHY_PLL_44MHZ;
446
447                         if (flags & CHANNEL_CCK) {
448                                 mode |= AR5K_PHY_MODE_MOD_CCK;
449                         } else if (flags & CHANNEL_OFDM) {
450                                 /* XXX Dynamic OFDM/CCK is not supported by the
451                                  * AR5211 so we set MOD_OFDM for plain g (no
452                                  * CCK headers) operation. We need to test
453                                  * this, 5211 might support ofdm-only g after
454                                  * all, there are also initial register values
455                                  * in the code for g mode (see initvals.c). */
456                                 if (ah->ah_version == AR5K_AR5211)
457                                         mode |= AR5K_PHY_MODE_MOD_OFDM;
458                                 else
459                                         mode |= AR5K_PHY_MODE_MOD_DYN;
460                         } else {
461                                 ATH5K_ERR(ah->ah_sc,
462                                         "invalid radio modulation mode\n");
463                                 return -EINVAL;
464                         }
465                 } else if (flags & CHANNEL_5GHZ) {
466                         mode |= AR5K_PHY_MODE_FREQ_5GHZ;
467
468                         if (ah->ah_radio == AR5K_RF5413)
469                                 clock = AR5K_PHY_PLL_40MHZ_5413;
470                         else
471                                 clock |= AR5K_PHY_PLL_40MHZ;
472
473                         if (flags & CHANNEL_OFDM)
474                                 mode |= AR5K_PHY_MODE_MOD_OFDM;
475                         else {
476                                 ATH5K_ERR(ah->ah_sc,
477                                         "invalid radio modulation mode\n");
478                                 return -EINVAL;
479                         }
480                 } else {
481                         ATH5K_ERR(ah->ah_sc, "invalid radio frequency mode\n");
482                         return -EINVAL;
483                 }
484
485                 if (flags & CHANNEL_TURBO)
486                         turbo = AR5K_PHY_TURBO_MODE | AR5K_PHY_TURBO_SHORT;
487         } else { /* Reset the device */
488
489                 /* ...enable Atheros turbo mode if requested */
490                 if (flags & CHANNEL_TURBO)
491                         ath5k_hw_reg_write(ah, AR5K_PHY_TURBO_MODE,
492                                         AR5K_PHY_TURBO);
493         }
494
495         if (ah->ah_version != AR5K_AR5210) {
496
497                 /* ...update PLL if needed */
498                 if (ath5k_hw_reg_read(ah, AR5K_PHY_PLL) != clock) {
499                         ath5k_hw_reg_write(ah, clock, AR5K_PHY_PLL);
500                         udelay(300);
501                 }
502
503                 /* ...set the PHY operating mode */
504                 ath5k_hw_reg_write(ah, mode, AR5K_PHY_MODE);
505                 ath5k_hw_reg_write(ah, turbo, AR5K_PHY_TURBO);
506         }
507
508         return 0;
509 }
510
511 /*
512  * If there is an external 32KHz crystal available, use it
513  * as ref. clock instead of 32/40MHz clock and baseband clocks
514  * to save power during sleep or restore normal 32/40MHz
515  * operation.
516  *
517  * XXX: When operating on 32KHz certain PHY registers (27 - 31,
518  *      123 - 127) require delay on access.
519  */
520 static void ath5k_hw_set_sleep_clock(struct ath5k_hw *ah, bool enable)
521 {
522         struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
523         u32 scal, spending, usec32;
524
525         /* Only set 32KHz settings if we have an external
526          * 32KHz crystal present */
527         if ((AR5K_EEPROM_HAS32KHZCRYSTAL(ee->ee_misc1) ||
528         AR5K_EEPROM_HAS32KHZCRYSTAL_OLD(ee->ee_misc1)) &&
529         enable) {
530
531                 /* 1 usec/cycle */
532                 AR5K_REG_WRITE_BITS(ah, AR5K_USEC_5211, AR5K_USEC_32, 1);
533                 /* Set up tsf increment on each cycle */
534                 AR5K_REG_WRITE_BITS(ah, AR5K_TSF_PARM, AR5K_TSF_PARM_INC, 61);
535
536                 /* Set baseband sleep control registers
537                  * and sleep control rate */
538                 ath5k_hw_reg_write(ah, 0x1f, AR5K_PHY_SCR);
539
540                 if ((ah->ah_radio == AR5K_RF5112) ||
541                 (ah->ah_radio == AR5K_RF5413) ||
542                 (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4)))
543                         spending = 0x14;
544                 else
545                         spending = 0x18;
546                 ath5k_hw_reg_write(ah, spending, AR5K_PHY_SPENDING);
547
548                 if ((ah->ah_radio == AR5K_RF5112) ||
549                 (ah->ah_radio == AR5K_RF5413) ||
550                 (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4))) {
551                         ath5k_hw_reg_write(ah, 0x26, AR5K_PHY_SLMT);
552                         ath5k_hw_reg_write(ah, 0x0d, AR5K_PHY_SCAL);
553                         ath5k_hw_reg_write(ah, 0x07, AR5K_PHY_SCLOCK);
554                         ath5k_hw_reg_write(ah, 0x3f, AR5K_PHY_SDELAY);
555                         AR5K_REG_WRITE_BITS(ah, AR5K_PCICFG,
556                                 AR5K_PCICFG_SLEEP_CLOCK_RATE, 0x02);
557                 } else {
558                         ath5k_hw_reg_write(ah, 0x0a, AR5K_PHY_SLMT);
559                         ath5k_hw_reg_write(ah, 0x0c, AR5K_PHY_SCAL);
560                         ath5k_hw_reg_write(ah, 0x03, AR5K_PHY_SCLOCK);
561                         ath5k_hw_reg_write(ah, 0x20, AR5K_PHY_SDELAY);
562                         AR5K_REG_WRITE_BITS(ah, AR5K_PCICFG,
563                                 AR5K_PCICFG_SLEEP_CLOCK_RATE, 0x03);
564                 }
565
566                 /* Enable sleep clock operation */
567                 AR5K_REG_ENABLE_BITS(ah, AR5K_PCICFG,
568                                 AR5K_PCICFG_SLEEP_CLOCK_EN);
569
570         } else {
571
572                 /* Disable sleep clock operation and
573                  * restore default parameters */
574                 AR5K_REG_DISABLE_BITS(ah, AR5K_PCICFG,
575                                 AR5K_PCICFG_SLEEP_CLOCK_EN);
576
577                 AR5K_REG_WRITE_BITS(ah, AR5K_PCICFG,
578                                 AR5K_PCICFG_SLEEP_CLOCK_RATE, 0);
579
580                 ath5k_hw_reg_write(ah, 0x1f, AR5K_PHY_SCR);
581                 ath5k_hw_reg_write(ah, AR5K_PHY_SLMT_32MHZ, AR5K_PHY_SLMT);
582
583                 if (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4))
584                         scal = AR5K_PHY_SCAL_32MHZ_2417;
585                 else if (ee->ee_is_hb63)
586                         scal = AR5K_PHY_SCAL_32MHZ_HB63;
587                 else
588                         scal = AR5K_PHY_SCAL_32MHZ;
589                 ath5k_hw_reg_write(ah, scal, AR5K_PHY_SCAL);
590
591                 ath5k_hw_reg_write(ah, AR5K_PHY_SCLOCK_32MHZ, AR5K_PHY_SCLOCK);
592                 ath5k_hw_reg_write(ah, AR5K_PHY_SDELAY_32MHZ, AR5K_PHY_SDELAY);
593
594                 if ((ah->ah_radio == AR5K_RF5112) ||
595                 (ah->ah_radio == AR5K_RF5413) ||
596                 (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4)))
597                         spending = 0x14;
598                 else
599                         spending = 0x18;
600                 ath5k_hw_reg_write(ah, spending, AR5K_PHY_SPENDING);
601
602                 if ((ah->ah_radio == AR5K_RF5112) ||
603                 (ah->ah_radio == AR5K_RF5413))
604                         usec32 = 39;
605                 else
606                         usec32 = 31;
607                 AR5K_REG_WRITE_BITS(ah, AR5K_USEC_5211, AR5K_USEC_32, usec32);
608
609                 AR5K_REG_WRITE_BITS(ah, AR5K_TSF_PARM, AR5K_TSF_PARM_INC, 1);
610         }
611         return;
612 }
613
614 /* TODO: Half/Quarter rate */
615 static void ath5k_hw_tweak_initval_settings(struct ath5k_hw *ah,
616                                 struct ieee80211_channel *channel)
617 {
618         if (ah->ah_version == AR5K_AR5212 &&
619             ah->ah_phy_revision >= AR5K_SREV_PHY_5212A) {
620
621                 /* Setup ADC control */
622                 ath5k_hw_reg_write(ah,
623                                 (AR5K_REG_SM(2,
624                                 AR5K_PHY_ADC_CTL_INBUFGAIN_OFF) |
625                                 AR5K_REG_SM(2,
626                                 AR5K_PHY_ADC_CTL_INBUFGAIN_ON) |
627                                 AR5K_PHY_ADC_CTL_PWD_DAC_OFF |
628                                 AR5K_PHY_ADC_CTL_PWD_ADC_OFF),
629                                 AR5K_PHY_ADC_CTL);
630
631
632
633                 /* Disable barker RSSI threshold */
634                 AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_DAG_CCK_CTL,
635                                 AR5K_PHY_DAG_CCK_CTL_EN_RSSI_THR);
636
637                 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DAG_CCK_CTL,
638                         AR5K_PHY_DAG_CCK_CTL_RSSI_THR, 2);
639
640                 /* Set the mute mask */
641                 ath5k_hw_reg_write(ah, 0x0000000f, AR5K_SEQ_MASK);
642         }
643
644         /* Clear PHY_BLUETOOTH to allow RX_CLEAR line debug */
645         if (ah->ah_phy_revision >= AR5K_SREV_PHY_5212B)
646                 ath5k_hw_reg_write(ah, 0, AR5K_PHY_BLUETOOTH);
647
648         /* Enable DCU double buffering */
649         if (ah->ah_phy_revision > AR5K_SREV_PHY_5212B)
650                 AR5K_REG_DISABLE_BITS(ah, AR5K_TXCFG,
651                                 AR5K_TXCFG_DCU_DBL_BUF_DIS);
652
653         /* Set DAC/ADC delays */
654         if (ah->ah_version == AR5K_AR5212) {
655                 u32 scal;
656                 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
657                 if (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4))
658                         scal = AR5K_PHY_SCAL_32MHZ_2417;
659                 else if (ee->ee_is_hb63)
660                         scal = AR5K_PHY_SCAL_32MHZ_HB63;
661                 else
662                         scal = AR5K_PHY_SCAL_32MHZ;
663                 ath5k_hw_reg_write(ah, scal, AR5K_PHY_SCAL);
664         }
665
666         /* Set fast ADC */
667         if ((ah->ah_radio == AR5K_RF5413) ||
668         (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4))) {
669                 u32 fast_adc = true;
670
671                 if (channel->center_freq == 2462 ||
672                 channel->center_freq == 2467)
673                         fast_adc = 0;
674
675                 /* Only update if needed */
676                 if (ath5k_hw_reg_read(ah, AR5K_PHY_FAST_ADC) != fast_adc)
677                                 ath5k_hw_reg_write(ah, fast_adc,
678                                                 AR5K_PHY_FAST_ADC);
679         }
680
681         /* Fix for first revision of the RF5112 RF chipset */
682         if (ah->ah_radio == AR5K_RF5112 &&
683                         ah->ah_radio_5ghz_revision <
684                         AR5K_SREV_RAD_5112A) {
685                 u32 data;
686                 ath5k_hw_reg_write(ah, AR5K_PHY_CCKTXCTL_WORLD,
687                                 AR5K_PHY_CCKTXCTL);
688                 if (channel->hw_value & CHANNEL_5GHZ)
689                         data = 0xffb81020;
690                 else
691                         data = 0xffb80d20;
692                 ath5k_hw_reg_write(ah, data, AR5K_PHY_FRAME_CTL);
693         }
694
695         if (ah->ah_mac_srev < AR5K_SREV_AR5211) {
696                 u32 usec_reg;
697                 /* 5311 has different tx/rx latency masks
698                  * from 5211, since we deal 5311 the same
699                  * as 5211 when setting initvals, shift
700                  * values here to their proper locations */
701                 usec_reg = ath5k_hw_reg_read(ah, AR5K_USEC_5211);
702                 ath5k_hw_reg_write(ah, usec_reg & (AR5K_USEC_1 |
703                                 AR5K_USEC_32 |
704                                 AR5K_USEC_TX_LATENCY_5211 |
705                                 AR5K_REG_SM(29,
706                                 AR5K_USEC_RX_LATENCY_5210)),
707                                 AR5K_USEC_5211);
708                 /* Clear QCU/DCU clock gating register */
709                 ath5k_hw_reg_write(ah, 0, AR5K_QCUDCU_CLKGT);
710                 /* Set DAC/ADC delays */
711                 ath5k_hw_reg_write(ah, 0x08, AR5K_PHY_SCAL);
712                 /* Enable PCU FIFO corruption ECO */
713                 AR5K_REG_ENABLE_BITS(ah, AR5K_DIAG_SW_5211,
714                                         AR5K_DIAG_SW_ECO_ENABLE);
715         }
716 }
717
718 static void ath5k_hw_commit_eeprom_settings(struct ath5k_hw *ah,
719                 struct ieee80211_channel *channel, u8 *ant, u8 ee_mode)
720 {
721         struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
722         s16 cck_ofdm_pwr_delta;
723
724         /* Adjust power delta for channel 14 */
725         if (channel->center_freq == 2484)
726                 cck_ofdm_pwr_delta =
727                         ((ee->ee_cck_ofdm_power_delta -
728                         ee->ee_scaled_cck_delta) * 2) / 10;
729         else
730                 cck_ofdm_pwr_delta =
731                         (ee->ee_cck_ofdm_power_delta * 2) / 10;
732
733         /* Set CCK to OFDM power delta on tx power
734          * adjustment register */
735         if (ah->ah_phy_revision >= AR5K_SREV_PHY_5212A) {
736                 if (channel->hw_value == CHANNEL_G)
737                         ath5k_hw_reg_write(ah,
738                         AR5K_REG_SM((ee->ee_cck_ofdm_gain_delta * -1),
739                                 AR5K_PHY_TX_PWR_ADJ_CCK_GAIN_DELTA) |
740                         AR5K_REG_SM((cck_ofdm_pwr_delta * -1),
741                                 AR5K_PHY_TX_PWR_ADJ_CCK_PCDAC_INDEX),
742                                 AR5K_PHY_TX_PWR_ADJ);
743                 else
744                         ath5k_hw_reg_write(ah, 0, AR5K_PHY_TX_PWR_ADJ);
745         } else {
746                 /* For older revs we scale power on sw during tx power
747                  * setup */
748                 ah->ah_txpower.txp_cck_ofdm_pwr_delta = cck_ofdm_pwr_delta;
749                 ah->ah_txpower.txp_cck_ofdm_gainf_delta =
750                                                 ee->ee_cck_ofdm_gain_delta;
751         }
752
753         /* Set antenna idle switch table */
754         AR5K_REG_WRITE_BITS(ah, AR5K_PHY_ANT_CTL,
755                         AR5K_PHY_ANT_CTL_SWTABLE_IDLE,
756                         (ah->ah_ant_ctl[ee_mode][0] |
757                         AR5K_PHY_ANT_CTL_TXRX_EN));
758
759         /* Set antenna switch tables */
760         ath5k_hw_reg_write(ah, ah->ah_ant_ctl[ee_mode][ant[0]],
761                 AR5K_PHY_ANT_SWITCH_TABLE_0);
762         ath5k_hw_reg_write(ah, ah->ah_ant_ctl[ee_mode][ant[1]],
763                 AR5K_PHY_ANT_SWITCH_TABLE_1);
764
765         /* Noise floor threshold */
766         ath5k_hw_reg_write(ah,
767                 AR5K_PHY_NF_SVAL(ee->ee_noise_floor_thr[ee_mode]),
768                 AR5K_PHY_NFTHRES);
769
770         if ((channel->hw_value & CHANNEL_TURBO) &&
771         (ah->ah_ee_version >= AR5K_EEPROM_VERSION_5_0)) {
772                 /* Switch settling time (Turbo) */
773                 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_SETTLING,
774                                 AR5K_PHY_SETTLING_SWITCH,
775                                 ee->ee_switch_settling_turbo[ee_mode]);
776
777                 /* Tx/Rx attenuation (Turbo) */
778                 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_GAIN,
779                                 AR5K_PHY_GAIN_TXRX_ATTEN,
780                                 ee->ee_atn_tx_rx_turbo[ee_mode]);
781
782                 /* ADC/PGA desired size (Turbo) */
783                 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DESIRED_SIZE,
784                                 AR5K_PHY_DESIRED_SIZE_ADC,
785                                 ee->ee_adc_desired_size_turbo[ee_mode]);
786
787                 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DESIRED_SIZE,
788                                 AR5K_PHY_DESIRED_SIZE_PGA,
789                                 ee->ee_pga_desired_size_turbo[ee_mode]);
790
791                 /* Tx/Rx margin (Turbo) */
792                 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_GAIN_2GHZ,
793                                 AR5K_PHY_GAIN_2GHZ_MARGIN_TXRX,
794                                 ee->ee_margin_tx_rx_turbo[ee_mode]);
795
796         } else {
797                 /* Switch settling time */
798                 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_SETTLING,
799                                 AR5K_PHY_SETTLING_SWITCH,
800                                 ee->ee_switch_settling[ee_mode]);
801
802                 /* Tx/Rx attenuation */
803                 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_GAIN,
804                                 AR5K_PHY_GAIN_TXRX_ATTEN,
805                                 ee->ee_atn_tx_rx[ee_mode]);
806
807                 /* ADC/PGA desired size */
808                 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DESIRED_SIZE,
809                                 AR5K_PHY_DESIRED_SIZE_ADC,
810                                 ee->ee_adc_desired_size[ee_mode]);
811
812                 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DESIRED_SIZE,
813                                 AR5K_PHY_DESIRED_SIZE_PGA,
814                                 ee->ee_pga_desired_size[ee_mode]);
815
816                 /* Tx/Rx margin */
817                 if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_1)
818                         AR5K_REG_WRITE_BITS(ah, AR5K_PHY_GAIN_2GHZ,
819                                 AR5K_PHY_GAIN_2GHZ_MARGIN_TXRX,
820                                 ee->ee_margin_tx_rx[ee_mode]);
821         }
822
823         /* XPA delays */
824         ath5k_hw_reg_write(ah,
825                 (ee->ee_tx_end2xpa_disable[ee_mode] << 24) |
826                 (ee->ee_tx_end2xpa_disable[ee_mode] << 16) |
827                 (ee->ee_tx_frm2xpa_enable[ee_mode] << 8) |
828                 (ee->ee_tx_frm2xpa_enable[ee_mode]), AR5K_PHY_RF_CTL4);
829
830         /* XLNA delay */
831         AR5K_REG_WRITE_BITS(ah, AR5K_PHY_RF_CTL3,
832                         AR5K_PHY_RF_CTL3_TXE2XLNA_ON,
833                         ee->ee_tx_end2xlna_enable[ee_mode]);
834
835         /* Thresh64 (ANI) */
836         AR5K_REG_WRITE_BITS(ah, AR5K_PHY_NF,
837                         AR5K_PHY_NF_THRESH62,
838                         ee->ee_thr_62[ee_mode]);
839
840
841         /* False detect backoff for channels
842          * that have spur noise. Write the new
843          * cyclic power RSSI threshold. */
844         if (ath5k_hw_chan_has_spur_noise(ah, channel))
845                 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_OFDM_SELFCORR,
846                                 AR5K_PHY_OFDM_SELFCORR_CYPWR_THR1,
847                                 AR5K_INIT_CYCRSSI_THR1 +
848                                 ee->ee_false_detect[ee_mode]);
849         else
850                 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_OFDM_SELFCORR,
851                                 AR5K_PHY_OFDM_SELFCORR_CYPWR_THR1,
852                                 AR5K_INIT_CYCRSSI_THR1);
853
854         /* I/Q correction
855          * TODO: Per channel i/q infos ? */
856         AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ,
857                 AR5K_PHY_IQ_CORR_ENABLE |
858                 (ee->ee_i_cal[ee_mode] << AR5K_PHY_IQ_CORR_Q_I_COFF_S) |
859                 ee->ee_q_cal[ee_mode]);
860
861         /* Heavy clipping -disable for now */
862         if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_5_1)
863                 ath5k_hw_reg_write(ah, 0, AR5K_PHY_HEAVY_CLIP_ENABLE);
864
865         return;
866 }
867
868 /*
869  * Main reset function
870  */
871 int ath5k_hw_reset(struct ath5k_hw *ah, enum nl80211_iftype op_mode,
872         struct ieee80211_channel *channel, bool change_channel)
873 {
874         struct ath_common *common = ath5k_hw_common(ah);
875         u32 s_seq[10], s_ant, s_led[3], staid1_flags, tsf_up, tsf_lo;
876         u32 phy_tst1;
877         u8 mode, freq, ee_mode, ant[2];
878         int i, ret;
879
880         ATH5K_TRACE(ah->ah_sc);
881
882         s_ant = 0;
883         ee_mode = 0;
884         staid1_flags = 0;
885         tsf_up = 0;
886         tsf_lo = 0;
887         freq = 0;
888         mode = 0;
889
890         /*
891          * Save some registers before a reset
892          */
893         /*DCU/Antenna selection not available on 5210*/
894         if (ah->ah_version != AR5K_AR5210) {
895
896                 switch (channel->hw_value & CHANNEL_MODES) {
897                 case CHANNEL_A:
898                         mode = AR5K_MODE_11A;
899                         freq = AR5K_INI_RFGAIN_5GHZ;
900                         ee_mode = AR5K_EEPROM_MODE_11A;
901                         break;
902                 case CHANNEL_G:
903                         mode = AR5K_MODE_11G;
904                         freq = AR5K_INI_RFGAIN_2GHZ;
905                         ee_mode = AR5K_EEPROM_MODE_11G;
906                         break;
907                 case CHANNEL_B:
908                         mode = AR5K_MODE_11B;
909                         freq = AR5K_INI_RFGAIN_2GHZ;
910                         ee_mode = AR5K_EEPROM_MODE_11B;
911                         break;
912                 case CHANNEL_T:
913                         mode = AR5K_MODE_11A_TURBO;
914                         freq = AR5K_INI_RFGAIN_5GHZ;
915                         ee_mode = AR5K_EEPROM_MODE_11A;
916                         break;
917                 case CHANNEL_TG:
918                         if (ah->ah_version == AR5K_AR5211) {
919                                 ATH5K_ERR(ah->ah_sc,
920                                         "TurboG mode not available on 5211");
921                                 return -EINVAL;
922                         }
923                         mode = AR5K_MODE_11G_TURBO;
924                         freq = AR5K_INI_RFGAIN_2GHZ;
925                         ee_mode = AR5K_EEPROM_MODE_11G;
926                         break;
927                 case CHANNEL_XR:
928                         if (ah->ah_version == AR5K_AR5211) {
929                                 ATH5K_ERR(ah->ah_sc,
930                                         "XR mode not available on 5211");
931                                 return -EINVAL;
932                         }
933                         mode = AR5K_MODE_XR;
934                         freq = AR5K_INI_RFGAIN_5GHZ;
935                         ee_mode = AR5K_EEPROM_MODE_11A;
936                         break;
937                 default:
938                         ATH5K_ERR(ah->ah_sc,
939                                 "invalid channel: %d\n", channel->center_freq);
940                         return -EINVAL;
941                 }
942
943                 if (change_channel) {
944                         /*
945                          * Save frame sequence count
946                          * For revs. after Oahu, only save
947                          * seq num for DCU 0 (Global seq num)
948                          */
949                         if (ah->ah_mac_srev < AR5K_SREV_AR5211) {
950
951                                 for (i = 0; i < 10; i++)
952                                         s_seq[i] = ath5k_hw_reg_read(ah,
953                                                 AR5K_QUEUE_DCU_SEQNUM(i));
954
955                         } else {
956                                 s_seq[0] = ath5k_hw_reg_read(ah,
957                                                 AR5K_QUEUE_DCU_SEQNUM(0));
958                         }
959
960                         /* TSF accelerates on AR5211 durring reset
961                          * As a workaround save it here and restore
962                          * it later so that it's back in time after
963                          * reset. This way it'll get re-synced on the
964                          * next beacon without breaking ad-hoc.
965                          *
966                          * On AR5212 TSF is almost preserved across a
967                          * reset so it stays back in time anyway and
968                          * we don't have to save/restore it.
969                          *
970                          * XXX: Since this breaks power saving we have
971                          * to disable power saving until we receive the
972                          * next beacon, so we can resync beacon timers */
973                         if (ah->ah_version == AR5K_AR5211) {
974                                 tsf_up = ath5k_hw_reg_read(ah, AR5K_TSF_U32);
975                                 tsf_lo = ath5k_hw_reg_read(ah, AR5K_TSF_L32);
976                         }
977                 }
978
979                 /* Save default antenna */
980                 s_ant = ath5k_hw_reg_read(ah, AR5K_DEFAULT_ANTENNA);
981
982                 if (ah->ah_version == AR5K_AR5212) {
983                         /* Restore normal 32/40MHz clock operation
984                          * to avoid register access delay on certain
985                          * PHY registers */
986                         ath5k_hw_set_sleep_clock(ah, false);
987
988                         /* Since we are going to write rf buffer
989                          * check if we have any pending gain_F
990                          * optimization settings */
991                         if (change_channel && ah->ah_rf_banks != NULL)
992                                 ath5k_hw_gainf_calibrate(ah);
993                 }
994         }
995
996         /*GPIOs*/
997         s_led[0] = ath5k_hw_reg_read(ah, AR5K_PCICFG) &
998                                         AR5K_PCICFG_LEDSTATE;
999         s_led[1] = ath5k_hw_reg_read(ah, AR5K_GPIOCR);
1000         s_led[2] = ath5k_hw_reg_read(ah, AR5K_GPIODO);
1001
1002         /* AR5K_STA_ID1 flags, only preserve antenna
1003          * settings and ack/cts rate mode */
1004         staid1_flags = ath5k_hw_reg_read(ah, AR5K_STA_ID1) &
1005                         (AR5K_STA_ID1_DEFAULT_ANTENNA |
1006                         AR5K_STA_ID1_DESC_ANTENNA |
1007                         AR5K_STA_ID1_RTS_DEF_ANTENNA |
1008                         AR5K_STA_ID1_ACKCTS_6MB |
1009                         AR5K_STA_ID1_BASE_RATE_11B |
1010                         AR5K_STA_ID1_SELFGEN_DEF_ANT);
1011
1012         /* Wakeup the device */
1013         ret = ath5k_hw_nic_wakeup(ah, channel->hw_value, false);
1014         if (ret)
1015                 return ret;
1016
1017         /*
1018          * Initialize operating mode
1019          */
1020         ah->ah_op_mode = op_mode;
1021
1022         /* PHY access enable */
1023         if (ah->ah_mac_srev >= AR5K_SREV_AR5211)
1024                 ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ, AR5K_PHY(0));
1025         else
1026                 ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ | 0x40,
1027                                                         AR5K_PHY(0));
1028
1029         /* Write initial settings */
1030         ret = ath5k_hw_write_initvals(ah, mode, change_channel);
1031         if (ret)
1032                 return ret;
1033
1034         /*
1035          * 5211/5212 Specific
1036          */
1037         if (ah->ah_version != AR5K_AR5210) {
1038
1039                 /*
1040                  * Write initial RF gain settings
1041                  * This should work for both 5111/5112
1042                  */
1043                 ret = ath5k_hw_rfgain_init(ah, freq);
1044                 if (ret)
1045                         return ret;
1046
1047                 mdelay(1);
1048
1049                 /*
1050                  * Tweak initval settings for revised
1051                  * chipsets and add some more config
1052                  * bits
1053                  */
1054                 ath5k_hw_tweak_initval_settings(ah, channel);
1055
1056                 /*
1057                  * Set TX power
1058                  */
1059                 ret = ath5k_hw_txpower(ah, channel, ee_mode,
1060                                         ah->ah_txpower.txp_max_pwr / 2);
1061                 if (ret)
1062                         return ret;
1063
1064                 /* Write rate duration table only on AR5212 and if
1065                  * virtual interface has already been brought up
1066                  * XXX: rethink this after new mode changes to
1067                  * mac80211 are integrated */
1068                 if (ah->ah_version == AR5K_AR5212 &&
1069                         ah->ah_sc->vif != NULL)
1070                         ath5k_hw_write_rate_duration(ah, mode);
1071
1072                 /*
1073                  * Write RF buffer
1074                  */
1075                 ret = ath5k_hw_rfregs_init(ah, channel, mode);
1076                 if (ret)
1077                         return ret;
1078
1079
1080                 /* Write OFDM timings on 5212*/
1081                 if (ah->ah_version == AR5K_AR5212 &&
1082                         channel->hw_value & CHANNEL_OFDM) {
1083                         struct ath5k_eeprom_info *ee =
1084                                         &ah->ah_capabilities.cap_eeprom;
1085
1086                         ret = ath5k_hw_write_ofdm_timings(ah, channel);
1087                         if (ret)
1088                                 return ret;
1089
1090                         /* Note: According to docs we can have a newer
1091                          * EEPROM on old hardware, so we need to verify
1092                          * that our hardware is new enough to have spur
1093                          * mitigation registers (delta phase etc) */
1094                         if (ah->ah_mac_srev >= AR5K_SREV_AR5424 ||
1095                         (ah->ah_mac_srev >= AR5K_SREV_AR5424 &&
1096                         ee->ee_version >= AR5K_EEPROM_VERSION_5_3))
1097                                 ath5k_hw_set_spur_mitigation_filter(ah,
1098                                                                 channel);
1099                 }
1100
1101                 /*Enable/disable 802.11b mode on 5111
1102                 (enable 2111 frequency converter + CCK)*/
1103                 if (ah->ah_radio == AR5K_RF5111) {
1104                         if (mode == AR5K_MODE_11B)
1105                                 AR5K_REG_ENABLE_BITS(ah, AR5K_TXCFG,
1106                                     AR5K_TXCFG_B_MODE);
1107                         else
1108                                 AR5K_REG_DISABLE_BITS(ah, AR5K_TXCFG,
1109                                     AR5K_TXCFG_B_MODE);
1110                 }
1111
1112                 /*
1113                  * In case a fixed antenna was set as default
1114                  * use the same switch table twice.
1115                  */
1116                 if (ah->ah_ant_mode == AR5K_ANTMODE_FIXED_A)
1117                                 ant[0] = ant[1] = AR5K_ANT_SWTABLE_A;
1118                 else if (ah->ah_ant_mode == AR5K_ANTMODE_FIXED_B)
1119                                 ant[0] = ant[1] = AR5K_ANT_SWTABLE_B;
1120                 else {
1121                         ant[0] = AR5K_ANT_SWTABLE_A;
1122                         ant[1] = AR5K_ANT_SWTABLE_B;
1123                 }
1124
1125                 /* Commit values from EEPROM */
1126                 ath5k_hw_commit_eeprom_settings(ah, channel, ant, ee_mode);
1127
1128         } else {
1129                 /*
1130                  * For 5210 we do all initialization using
1131                  * initvals, so we don't have to modify
1132                  * any settings (5210 also only supports
1133                  * a/aturbo modes)
1134                  */
1135                 mdelay(1);
1136                 /* Disable phy and wait */
1137                 ath5k_hw_reg_write(ah, AR5K_PHY_ACT_DISABLE, AR5K_PHY_ACT);
1138                 mdelay(1);
1139         }
1140
1141         /*
1142          * Restore saved values
1143          */
1144
1145         /*DCU/Antenna selection not available on 5210*/
1146         if (ah->ah_version != AR5K_AR5210) {
1147
1148                 if (change_channel) {
1149                         if (ah->ah_mac_srev < AR5K_SREV_AR5211) {
1150                                 for (i = 0; i < 10; i++)
1151                                         ath5k_hw_reg_write(ah, s_seq[i],
1152                                                 AR5K_QUEUE_DCU_SEQNUM(i));
1153                         } else {
1154                                 ath5k_hw_reg_write(ah, s_seq[0],
1155                                         AR5K_QUEUE_DCU_SEQNUM(0));
1156                         }
1157
1158
1159                         if (ah->ah_version == AR5K_AR5211) {
1160                                 ath5k_hw_reg_write(ah, tsf_up, AR5K_TSF_U32);
1161                                 ath5k_hw_reg_write(ah, tsf_lo, AR5K_TSF_L32);
1162                         }
1163                 }
1164
1165                 ath5k_hw_reg_write(ah, s_ant, AR5K_DEFAULT_ANTENNA);
1166         }
1167
1168         /* Ledstate */
1169         AR5K_REG_ENABLE_BITS(ah, AR5K_PCICFG, s_led[0]);
1170
1171         /* Gpio settings */
1172         ath5k_hw_reg_write(ah, s_led[1], AR5K_GPIOCR);
1173         ath5k_hw_reg_write(ah, s_led[2], AR5K_GPIODO);
1174
1175         /* Restore sta_id flags and preserve our mac address*/
1176         ath5k_hw_reg_write(ah,
1177                            get_unaligned_le32(common->macaddr),
1178                            AR5K_STA_ID0);
1179         ath5k_hw_reg_write(ah,
1180                            staid1_flags | get_unaligned_le16(common->macaddr + 4),
1181                            AR5K_STA_ID1);
1182
1183
1184         /*
1185          * Configure PCU
1186          */
1187
1188         /* Restore bssid and bssid mask */
1189         ath5k_hw_set_associd(ah);
1190
1191         /* Set PCU config */
1192         ath5k_hw_set_opmode(ah);
1193
1194         /* Clear any pending interrupts
1195          * PISR/SISR Not available on 5210 */
1196         if (ah->ah_version != AR5K_AR5210)
1197                 ath5k_hw_reg_write(ah, 0xffffffff, AR5K_PISR);
1198
1199         /* Set RSSI/BRSSI thresholds
1200          *
1201          * Note: If we decide to set this value
1202          * dynamicaly, have in mind that when AR5K_RSSI_THR
1203          * register is read it might return 0x40 if we haven't
1204          * wrote anything to it plus BMISS RSSI threshold is zeroed.
1205          * So doing a save/restore procedure here isn't the right
1206          * choice. Instead store it on ath5k_hw */
1207         ath5k_hw_reg_write(ah, (AR5K_TUNE_RSSI_THRES |
1208                                 AR5K_TUNE_BMISS_THRES <<
1209                                 AR5K_RSSI_THR_BMISS_S),
1210                                 AR5K_RSSI_THR);
1211
1212         /* MIC QoS support */
1213         if (ah->ah_mac_srev >= AR5K_SREV_AR2413) {
1214                 ath5k_hw_reg_write(ah, 0x000100aa, AR5K_MIC_QOS_CTL);
1215                 ath5k_hw_reg_write(ah, 0x00003210, AR5K_MIC_QOS_SEL);
1216         }
1217
1218         /* QoS NOACK Policy */
1219         if (ah->ah_version == AR5K_AR5212) {
1220                 ath5k_hw_reg_write(ah,
1221                         AR5K_REG_SM(2, AR5K_QOS_NOACK_2BIT_VALUES) |
1222                         AR5K_REG_SM(5, AR5K_QOS_NOACK_BIT_OFFSET)  |
1223                         AR5K_REG_SM(0, AR5K_QOS_NOACK_BYTE_OFFSET),
1224                         AR5K_QOS_NOACK);
1225         }
1226
1227
1228         /*
1229          * Configure PHY
1230          */
1231
1232         /* Set channel on PHY */
1233         ret = ath5k_hw_channel(ah, channel);
1234         if (ret)
1235                 return ret;
1236
1237         /*
1238          * Enable the PHY and wait until completion
1239          * This includes BaseBand and Synthesizer
1240          * activation.
1241          */
1242         ath5k_hw_reg_write(ah, AR5K_PHY_ACT_ENABLE, AR5K_PHY_ACT);
1243
1244         /*
1245          * On 5211+ read activation -> rx delay
1246          * and use it.
1247          *
1248          * TODO: Half/quarter rate support
1249          */
1250         if (ah->ah_version != AR5K_AR5210) {
1251                 u32 delay;
1252                 delay = ath5k_hw_reg_read(ah, AR5K_PHY_RX_DELAY) &
1253                         AR5K_PHY_RX_DELAY_M;
1254                 delay = (channel->hw_value & CHANNEL_CCK) ?
1255                         ((delay << 2) / 22) : (delay / 10);
1256
1257                 udelay(100 + (2 * delay));
1258         } else {
1259                 mdelay(1);
1260         }
1261
1262         /*
1263          * Perform ADC test to see if baseband is ready
1264          * Set tx hold and check adc test register
1265          */
1266         phy_tst1 = ath5k_hw_reg_read(ah, AR5K_PHY_TST1);
1267         ath5k_hw_reg_write(ah, AR5K_PHY_TST1_TXHOLD, AR5K_PHY_TST1);
1268         for (i = 0; i <= 20; i++) {
1269                 if (!(ath5k_hw_reg_read(ah, AR5K_PHY_ADC_TEST) & 0x10))
1270                         break;
1271                 udelay(200);
1272         }
1273         ath5k_hw_reg_write(ah, phy_tst1, AR5K_PHY_TST1);
1274
1275         /*
1276          * Start automatic gain control calibration
1277          *
1278          * During AGC calibration RX path is re-routed to
1279          * a power detector so we don't receive anything.
1280          *
1281          * This method is used to calibrate some static offsets
1282          * used together with on-the fly I/Q calibration (the
1283          * one performed via ath5k_hw_phy_calibrate), that doesn't
1284          * interrupt rx path.
1285          *
1286          * While rx path is re-routed to the power detector we also
1287          * start a noise floor calibration, to measure the
1288          * card's noise floor (the noise we measure when we are not
1289          * transmiting or receiving anything).
1290          *
1291          * If we are in a noisy environment AGC calibration may time
1292          * out and/or noise floor calibration might timeout.
1293          */
1294         AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGCCTL,
1295                                 AR5K_PHY_AGCCTL_CAL | AR5K_PHY_AGCCTL_NF);
1296
1297         /* At the same time start I/Q calibration for QAM constellation
1298          * -no need for CCK- */
1299         ah->ah_calibration = false;
1300         if (!(mode == AR5K_MODE_11B)) {
1301                 ah->ah_calibration = true;
1302                 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_IQ,
1303                                 AR5K_PHY_IQ_CAL_NUM_LOG_MAX, 15);
1304                 AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ,
1305                                 AR5K_PHY_IQ_RUN);
1306         }
1307
1308         /* Wait for gain calibration to finish (we check for I/Q calibration
1309          * during ath5k_phy_calibrate) */
1310         if (ath5k_hw_register_timeout(ah, AR5K_PHY_AGCCTL,
1311                         AR5K_PHY_AGCCTL_CAL, 0, false)) {
1312                 ATH5K_ERR(ah->ah_sc, "gain calibration timeout (%uMHz)\n",
1313                         channel->center_freq);
1314         }
1315
1316         /* Restore antenna mode */
1317         ath5k_hw_set_antenna_mode(ah, ah->ah_ant_mode);
1318
1319         /*
1320          * Configure QCUs/DCUs
1321          */
1322
1323         /* TODO: HW Compression support for data queues */
1324         /* TODO: Burst prefetch for data queues */
1325
1326         /*
1327          * Reset queues and start beacon timers at the end of the reset routine
1328          * This also sets QCU mask on each DCU for 1:1 qcu to dcu mapping
1329          * Note: If we want we can assign multiple qcus on one dcu.
1330          */
1331         for (i = 0; i < ah->ah_capabilities.cap_queues.q_tx_num; i++) {
1332                 ret = ath5k_hw_reset_tx_queue(ah, i);
1333                 if (ret) {
1334                         ATH5K_ERR(ah->ah_sc,
1335                                 "failed to reset TX queue #%d\n", i);
1336                         return ret;
1337                 }
1338         }
1339
1340
1341         /*
1342          * Configure DMA/Interrupts
1343          */
1344
1345         /*
1346          * Set Rx/Tx DMA Configuration
1347          *
1348          * Set standard DMA size (128). Note that
1349          * a DMA size of 512 causes rx overruns and tx errors
1350          * on pci-e cards (tested on 5424 but since rx overruns
1351          * also occur on 5416/5418 with madwifi we set 128
1352          * for all PCI-E cards to be safe).
1353          *
1354          * XXX: need to check 5210 for this
1355          * TODO: Check out tx triger level, it's always 64 on dumps but I
1356          * guess we can tweak it and see how it goes ;-)
1357          */
1358         if (ah->ah_version != AR5K_AR5210) {
1359                 AR5K_REG_WRITE_BITS(ah, AR5K_TXCFG,
1360                         AR5K_TXCFG_SDMAMR, AR5K_DMASIZE_128B);
1361                 AR5K_REG_WRITE_BITS(ah, AR5K_RXCFG,
1362                         AR5K_RXCFG_SDMAMW, AR5K_DMASIZE_128B);
1363         }
1364
1365         /* Pre-enable interrupts on 5211/5212*/
1366         if (ah->ah_version != AR5K_AR5210)
1367                 ath5k_hw_set_imr(ah, ah->ah_imr);
1368
1369         /* Enable 32KHz clock function for AR5212+ chips
1370          * Set clocks to 32KHz operation and use an
1371          * external 32KHz crystal when sleeping if one
1372          * exists */
1373         if (ah->ah_version == AR5K_AR5212)
1374                         ath5k_hw_set_sleep_clock(ah, true);
1375
1376         /*
1377          * Disable beacons and reset the register
1378          */
1379         AR5K_REG_DISABLE_BITS(ah, AR5K_BEACON, AR5K_BEACON_ENABLE |
1380                         AR5K_BEACON_RESET_TSF);
1381
1382         return 0;
1383 }
1384
1385 #undef _ATH5K_RESET