HID: fix report descriptor handling for MS Wireless model 1028
[sfrench/cifs-2.6.git] / drivers / net / wireless / ath5k / hw.c
1 /*
2  * Copyright (c) 2004-2007 Reyk Floeter <reyk@openbsd.org>
3  * Copyright (c) 2006-2007 Nick Kossifidis <mickflemm@gmail.com>
4  * Copyright (c) 2007 Matthew W. S. Bell  <mentor@madwifi.org>
5  * Copyright (c) 2007 Luis Rodriguez <mcgrof@winlab.rutgers.edu>
6  * Copyright (c) 2007 Pavel Roskin <proski@gnu.org>
7  * Copyright (c) 2007 Jiri Slaby <jirislaby@gmail.com>
8  *
9  * Permission to use, copy, modify, and distribute this software for any
10  * purpose with or without fee is hereby granted, provided that the above
11  * copyright notice and this permission notice appear in all copies.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20  *
21  */
22
23 /*
24  * HW related functions for Atheros Wireless LAN devices.
25  */
26
27 #include <linux/pci.h>
28 #include <linux/delay.h>
29
30 #include "reg.h"
31 #include "base.h"
32 #include "debug.h"
33
34 /*Rate tables*/
35 static const struct ath5k_rate_table ath5k_rt_11a = AR5K_RATES_11A;
36 static const struct ath5k_rate_table ath5k_rt_11b = AR5K_RATES_11B;
37 static const struct ath5k_rate_table ath5k_rt_11g = AR5K_RATES_11G;
38 static const struct ath5k_rate_table ath5k_rt_turbo = AR5K_RATES_TURBO;
39 static const struct ath5k_rate_table ath5k_rt_xr = AR5K_RATES_XR;
40
41 /*Prototypes*/
42 static int ath5k_hw_nic_reset(struct ath5k_hw *, u32);
43 static int ath5k_hw_nic_wakeup(struct ath5k_hw *, int, bool);
44 static int ath5k_hw_setup_4word_tx_desc(struct ath5k_hw *, struct ath5k_desc *,
45         unsigned int, unsigned int, enum ath5k_pkt_type, unsigned int,
46         unsigned int, unsigned int, unsigned int, unsigned int, unsigned int,
47         unsigned int, unsigned int);
48 static int ath5k_hw_setup_xr_tx_desc(struct ath5k_hw *, struct ath5k_desc *,
49         unsigned int, unsigned int, unsigned int, unsigned int, unsigned int,
50         unsigned int);
51 static int ath5k_hw_proc_4word_tx_status(struct ath5k_hw *, struct ath5k_desc *,
52                                          struct ath5k_tx_status *);
53 static int ath5k_hw_setup_2word_tx_desc(struct ath5k_hw *, struct ath5k_desc *,
54         unsigned int, unsigned int, enum ath5k_pkt_type, unsigned int,
55         unsigned int, unsigned int, unsigned int, unsigned int, unsigned int,
56         unsigned int, unsigned int);
57 static int ath5k_hw_proc_2word_tx_status(struct ath5k_hw *, struct ath5k_desc *,
58                                          struct ath5k_tx_status *);
59 static int ath5k_hw_proc_5212_rx_status(struct ath5k_hw *, struct ath5k_desc *,
60                                         struct ath5k_rx_status *);
61 static int ath5k_hw_proc_5210_rx_status(struct ath5k_hw *, struct ath5k_desc *,
62                                         struct ath5k_rx_status *);
63 static int ath5k_hw_get_capabilities(struct ath5k_hw *);
64
65 static int ath5k_eeprom_init(struct ath5k_hw *);
66 static int ath5k_eeprom_read_mac(struct ath5k_hw *, u8 *);
67
68 static int ath5k_hw_enable_pspoll(struct ath5k_hw *, u8 *, u16);
69 static int ath5k_hw_disable_pspoll(struct ath5k_hw *);
70
71 /*
72  * Enable to overwrite the country code (use "00" for debug)
73  */
74 #if 0
75 #define COUNTRYCODE "00"
76 #endif
77
78 /*******************\
79   General Functions
80 \*******************/
81
82 /*
83  * Functions used internaly
84  */
85
86 static inline unsigned int ath5k_hw_htoclock(unsigned int usec, bool turbo)
87 {
88         return turbo ? (usec * 80) : (usec * 40);
89 }
90
91 static inline unsigned int ath5k_hw_clocktoh(unsigned int clock, bool turbo)
92 {
93         return turbo ? (clock / 80) : (clock / 40);
94 }
95
96 /*
97  * Check if a register write has been completed
98  */
99 int ath5k_hw_register_timeout(struct ath5k_hw *ah, u32 reg, u32 flag, u32 val,
100                 bool is_set)
101 {
102         int i;
103         u32 data;
104
105         for (i = AR5K_TUNE_REGISTER_TIMEOUT; i > 0; i--) {
106                 data = ath5k_hw_reg_read(ah, reg);
107                 if (is_set && (data & flag))
108                         break;
109                 else if ((data & flag) == val)
110                         break;
111                 udelay(15);
112         }
113
114         return (i <= 0) ? -EAGAIN : 0;
115 }
116
117
118 /***************************************\
119         Attach/Detach Functions
120 \***************************************/
121
122 /*
123  * Power On Self Test helper function
124  */
125 static int ath5k_hw_post(struct ath5k_hw *ah)
126 {
127
128         int i, c;
129         u16 cur_reg;
130         u16 regs[2] = {AR5K_STA_ID0, AR5K_PHY(8)};
131         u32 var_pattern;
132         u32 static_pattern[4] = {
133                 0x55555555,     0xaaaaaaaa,
134                 0x66666666,     0x99999999
135         };
136         u32 init_val;
137         u32 cur_val;
138
139         for (c = 0; c < 2; c++) {
140
141                 cur_reg = regs[c];
142                 init_val = ath5k_hw_reg_read(ah, cur_reg);
143
144                 for (i = 0; i < 256; i++) {
145                         var_pattern = i << 16 | i;
146                         ath5k_hw_reg_write(ah, var_pattern, cur_reg);
147                         cur_val = ath5k_hw_reg_read(ah, cur_reg);
148
149                         if (cur_val != var_pattern) {
150                                 ATH5K_ERR(ah->ah_sc, "POST Failed !!!\n");
151                                 return -EAGAIN;
152                         }
153
154                         /* Found on ndiswrapper dumps */
155                         var_pattern = 0x0039080f;
156                         ath5k_hw_reg_write(ah, var_pattern, cur_reg);
157                 }
158
159                 for (i = 0; i < 4; i++) {
160                         var_pattern = static_pattern[i];
161                         ath5k_hw_reg_write(ah, var_pattern, cur_reg);
162                         cur_val = ath5k_hw_reg_read(ah, cur_reg);
163
164                         if (cur_val != var_pattern) {
165                                 ATH5K_ERR(ah->ah_sc, "POST Failed !!!\n");
166                                 return -EAGAIN;
167                         }
168
169                         /* Found on ndiswrapper dumps */
170                         var_pattern = 0x003b080f;
171                         ath5k_hw_reg_write(ah, var_pattern, cur_reg);
172                 }
173         }
174
175         return 0;
176
177 }
178
179 /*
180  * Check if the device is supported and initialize the needed structs
181  */
182 struct ath5k_hw *ath5k_hw_attach(struct ath5k_softc *sc, u8 mac_version)
183 {
184         struct ath5k_hw *ah;
185         struct pci_dev *pdev = sc->pdev;
186         u8 mac[ETH_ALEN];
187         int ret;
188         u32 srev;
189
190         /*If we passed the test malloc a ath5k_hw struct*/
191         ah = kzalloc(sizeof(struct ath5k_hw), GFP_KERNEL);
192         if (ah == NULL) {
193                 ret = -ENOMEM;
194                 ATH5K_ERR(sc, "out of memory\n");
195                 goto err;
196         }
197
198         ah->ah_sc = sc;
199         ah->ah_iobase = sc->iobase;
200
201         /*
202          * HW information
203          */
204
205         ah->ah_op_mode = IEEE80211_IF_TYPE_STA;
206         ah->ah_radar.r_enabled = AR5K_TUNE_RADAR_ALERT;
207         ah->ah_turbo = false;
208         ah->ah_txpower.txp_tpc = AR5K_TUNE_TPC_TXPOWER;
209         ah->ah_imr = 0;
210         ah->ah_atim_window = 0;
211         ah->ah_aifs = AR5K_TUNE_AIFS;
212         ah->ah_cw_min = AR5K_TUNE_CWMIN;
213         ah->ah_limit_tx_retries = AR5K_INIT_TX_RETRY;
214         ah->ah_software_retry = false;
215         ah->ah_ant_diversity = AR5K_TUNE_ANT_DIVERSITY;
216
217         /*
218          * Set the mac revision based on the pci id
219          */
220         ah->ah_version = mac_version;
221
222         /*Fill the ath5k_hw struct with the needed functions*/
223         if (ah->ah_version == AR5K_AR5212)
224                 ah->ah_magic = AR5K_EEPROM_MAGIC_5212;
225         else if (ah->ah_version == AR5K_AR5211)
226                 ah->ah_magic = AR5K_EEPROM_MAGIC_5211;
227
228         if (ah->ah_version == AR5K_AR5212) {
229                 ah->ah_setup_tx_desc = ath5k_hw_setup_4word_tx_desc;
230                 ah->ah_setup_xtx_desc = ath5k_hw_setup_xr_tx_desc;
231                 ah->ah_proc_tx_desc = ath5k_hw_proc_4word_tx_status;
232         } else {
233                 ah->ah_setup_tx_desc = ath5k_hw_setup_2word_tx_desc;
234                 ah->ah_setup_xtx_desc = ath5k_hw_setup_xr_tx_desc;
235                 ah->ah_proc_tx_desc = ath5k_hw_proc_2word_tx_status;
236         }
237
238         if (ah->ah_version == AR5K_AR5212)
239                 ah->ah_proc_rx_desc = ath5k_hw_proc_5212_rx_status;
240         else if (ah->ah_version <= AR5K_AR5211)
241                 ah->ah_proc_rx_desc = ath5k_hw_proc_5210_rx_status;
242
243         /* Bring device out of sleep and reset it's units */
244         ret = ath5k_hw_nic_wakeup(ah, AR5K_INIT_MODE, true);
245         if (ret)
246                 goto err_free;
247
248         /* Get MAC, PHY and RADIO revisions */
249         srev = ath5k_hw_reg_read(ah, AR5K_SREV);
250         ah->ah_mac_srev = srev;
251         ah->ah_mac_version = AR5K_REG_MS(srev, AR5K_SREV_VER);
252         ah->ah_mac_revision = AR5K_REG_MS(srev, AR5K_SREV_REV);
253         ah->ah_phy_revision = ath5k_hw_reg_read(ah, AR5K_PHY_CHIP_ID) &
254                         0xffffffff;
255         ah->ah_radio_5ghz_revision = ath5k_hw_radio_revision(ah,
256                         CHANNEL_5GHZ);
257
258         if (ah->ah_version == AR5K_AR5210)
259                 ah->ah_radio_2ghz_revision = 0;
260         else
261                 ah->ah_radio_2ghz_revision = ath5k_hw_radio_revision(ah,
262                                 CHANNEL_2GHZ);
263
264         /* Return on unsuported chips (unsupported eeprom etc) */
265         if ((srev >= AR5K_SREV_VER_AR5416) &&
266         (srev < AR5K_SREV_VER_AR2425)) {
267                 ATH5K_ERR(sc, "Device not yet supported.\n");
268                 ret = -ENODEV;
269                 goto err_free;
270         } else if (srev == AR5K_SREV_VER_AR2425) {
271                 ATH5K_WARN(sc, "Support for RF2425 is under development.\n");
272         }
273
274         /* Identify single chip solutions */
275         if (((srev <= AR5K_SREV_VER_AR5414) &&
276         (srev >= AR5K_SREV_VER_AR2413)) ||
277         (srev == AR5K_SREV_VER_AR2425)) {
278                 ah->ah_single_chip = true;
279         } else {
280                 ah->ah_single_chip = false;
281         }
282
283         /* Single chip radio */
284         if (ah->ah_radio_2ghz_revision == ah->ah_radio_5ghz_revision)
285                 ah->ah_radio_2ghz_revision = 0;
286
287         /* Identify the radio chip*/
288         if (ah->ah_version == AR5K_AR5210) {
289                 ah->ah_radio = AR5K_RF5110;
290         } else if (ah->ah_radio_5ghz_revision < AR5K_SREV_RAD_5112) {
291                 ah->ah_radio = AR5K_RF5111;
292                 ah->ah_phy_spending = AR5K_PHY_SPENDING_RF5111;
293         } else if (ah->ah_radio_5ghz_revision < AR5K_SREV_RAD_SC0) {
294
295                 ah->ah_radio = AR5K_RF5112;
296
297                 if (ah->ah_radio_5ghz_revision < AR5K_SREV_RAD_5112A) {
298                         ah->ah_phy_spending = AR5K_PHY_SPENDING_RF5112;
299                 } else {
300                         ah->ah_phy_spending = AR5K_PHY_SPENDING_RF5112A;
301                 }
302
303         } else if (ah->ah_radio_5ghz_revision < AR5K_SREV_RAD_SC1) {
304                 ah->ah_radio = AR5K_RF2413;
305                 ah->ah_phy_spending = AR5K_PHY_SPENDING_RF5112A;
306         } else if (ah->ah_radio_5ghz_revision < AR5K_SREV_RAD_SC2) {
307
308                 ah->ah_radio = AR5K_RF5413;
309
310                 if (ah->ah_mac_srev <= AR5K_SREV_VER_AR5424 &&
311                         ah->ah_mac_srev >= AR5K_SREV_VER_AR2424)
312                         ah->ah_phy_spending = AR5K_PHY_SPENDING_RF5424;
313                 else
314                         ah->ah_phy_spending = AR5K_PHY_SPENDING_RF5112A;
315         /*
316          * Register returns 0x4 for radio revision
317          * so ath5k_hw_radio_revision doesn't parse the value
318          * correctly. For now we are based on mac's srev to
319          * identify RF2425 radio.
320          */
321         } else if (srev == AR5K_SREV_VER_AR2425) {
322                 ah->ah_radio = AR5K_RF2425;
323                 ah->ah_phy_spending = AR5K_PHY_SPENDING_RF5112;
324         }
325
326         ah->ah_phy = AR5K_PHY(0);
327
328         /*
329          * Identify AR5212-based PCI-E cards
330          * And write some initial settings.
331          *
332          * (doing a "strings" on ndis driver
333          * -ar5211.sys- reveals the following
334          * pci-e related functions:
335          *
336          * pcieClockReq
337          * pcieRxErrNotify
338          * pcieL1SKPEnable
339          * pcieAspm
340          * pcieDisableAspmOnRfWake
341          * pciePowerSaveEnable
342          *
343          * I guess these point to ClockReq but
344          * i'm not sure.)
345          */
346         if ((ah->ah_version == AR5K_AR5212) && (pdev->is_pcie)) {
347                 ath5k_hw_reg_write(ah, 0x9248fc00, 0x4080);
348                 ath5k_hw_reg_write(ah, 0x24924924, 0x4080);
349                 ath5k_hw_reg_write(ah, 0x28000039, 0x4080);
350                 ath5k_hw_reg_write(ah, 0x53160824, 0x4080);
351                 ath5k_hw_reg_write(ah, 0xe5980579, 0x4080);
352                 ath5k_hw_reg_write(ah, 0x001defff, 0x4080);
353                 ath5k_hw_reg_write(ah, 0x1aaabe40, 0x4080);
354                 ath5k_hw_reg_write(ah, 0xbe105554, 0x4080);
355                 ath5k_hw_reg_write(ah, 0x000e3007, 0x4080);
356                 ath5k_hw_reg_write(ah, 0x00000000, 0x4084);
357         }
358
359         /*
360          * POST
361          */
362         ret = ath5k_hw_post(ah);
363         if (ret)
364                 goto err_free;
365
366         /*
367          * Get card capabilities, values, ...
368          */
369
370         ret = ath5k_eeprom_init(ah);
371         if (ret) {
372                 ATH5K_ERR(sc, "unable to init EEPROM\n");
373                 goto err_free;
374         }
375
376         /* Get misc capabilities */
377         ret = ath5k_hw_get_capabilities(ah);
378         if (ret) {
379                 ATH5K_ERR(sc, "unable to get device capabilities: 0x%04x\n",
380                         sc->pdev->device);
381                 goto err_free;
382         }
383
384         /* Get MAC address */
385         ret = ath5k_eeprom_read_mac(ah, mac);
386         if (ret) {
387                 ATH5K_ERR(sc, "unable to read address from EEPROM: 0x%04x\n",
388                         sc->pdev->device);
389                 goto err_free;
390         }
391
392         ath5k_hw_set_lladdr(ah, mac);
393         /* Set BSSID to bcast address: ff:ff:ff:ff:ff:ff for now */
394         memset(ah->ah_bssid, 0xff, ETH_ALEN);
395         ath5k_hw_set_associd(ah, ah->ah_bssid, 0);
396         ath5k_hw_set_opmode(ah);
397
398         ath5k_hw_set_rfgain_opt(ah);
399
400         return ah;
401 err_free:
402         kfree(ah);
403 err:
404         return ERR_PTR(ret);
405 }
406
407 /*
408  * Bring up MAC + PHY Chips
409  */
410 static int ath5k_hw_nic_wakeup(struct ath5k_hw *ah, int flags, bool initial)
411 {
412         struct pci_dev *pdev = ah->ah_sc->pdev;
413         u32 turbo, mode, clock, bus_flags;
414         int ret;
415
416         turbo = 0;
417         mode = 0;
418         clock = 0;
419
420         ATH5K_TRACE(ah->ah_sc);
421
422         /* Wakeup the device */
423         ret = ath5k_hw_set_power(ah, AR5K_PM_AWAKE, true, 0);
424         if (ret) {
425                 ATH5K_ERR(ah->ah_sc, "failed to wakeup the MAC Chip\n");
426                 return ret;
427         }
428
429         if (ah->ah_version != AR5K_AR5210) {
430                 /*
431                  * Get channel mode flags
432                  */
433
434                 if (ah->ah_radio >= AR5K_RF5112) {
435                         mode = AR5K_PHY_MODE_RAD_RF5112;
436                         clock = AR5K_PHY_PLL_RF5112;
437                 } else {
438                         mode = AR5K_PHY_MODE_RAD_RF5111;        /*Zero*/
439                         clock = AR5K_PHY_PLL_RF5111;            /*Zero*/
440                 }
441
442                 if (flags & CHANNEL_2GHZ) {
443                         mode |= AR5K_PHY_MODE_FREQ_2GHZ;
444                         clock |= AR5K_PHY_PLL_44MHZ;
445
446                         if (flags & CHANNEL_CCK) {
447                                 mode |= AR5K_PHY_MODE_MOD_CCK;
448                         } else if (flags & CHANNEL_OFDM) {
449                                 /* XXX Dynamic OFDM/CCK is not supported by the
450                                  * AR5211 so we set MOD_OFDM for plain g (no
451                                  * CCK headers) operation. We need to test
452                                  * this, 5211 might support ofdm-only g after
453                                  * all, there are also initial register values
454                                  * in the code for g mode (see initvals.c). */
455                                 if (ah->ah_version == AR5K_AR5211)
456                                         mode |= AR5K_PHY_MODE_MOD_OFDM;
457                                 else
458                                         mode |= AR5K_PHY_MODE_MOD_DYN;
459                         } else {
460                                 ATH5K_ERR(ah->ah_sc,
461                                         "invalid radio modulation mode\n");
462                                 return -EINVAL;
463                         }
464                 } else if (flags & CHANNEL_5GHZ) {
465                         mode |= AR5K_PHY_MODE_FREQ_5GHZ;
466                         clock |= AR5K_PHY_PLL_40MHZ;
467
468                         if (flags & CHANNEL_OFDM)
469                                 mode |= AR5K_PHY_MODE_MOD_OFDM;
470                         else {
471                                 ATH5K_ERR(ah->ah_sc,
472                                         "invalid radio modulation mode\n");
473                                 return -EINVAL;
474                         }
475                 } else {
476                         ATH5K_ERR(ah->ah_sc, "invalid radio frequency mode\n");
477                         return -EINVAL;
478                 }
479
480                 if (flags & CHANNEL_TURBO)
481                         turbo = AR5K_PHY_TURBO_MODE | AR5K_PHY_TURBO_SHORT;
482         } else { /* Reset the device */
483
484                 /* ...enable Atheros turbo mode if requested */
485                 if (flags & CHANNEL_TURBO)
486                         ath5k_hw_reg_write(ah, AR5K_PHY_TURBO_MODE,
487                                         AR5K_PHY_TURBO);
488         }
489
490         /* reseting PCI on PCI-E cards results card to hang
491          * and always return 0xffff... so we ingore that flag
492          * for PCI-E cards */
493         bus_flags = (pdev->is_pcie) ? 0 : AR5K_RESET_CTL_PCI;
494
495         /* Reset chipset */
496         ret = ath5k_hw_nic_reset(ah, AR5K_RESET_CTL_PCU |
497                 AR5K_RESET_CTL_BASEBAND | bus_flags);
498         if (ret) {
499                 ATH5K_ERR(ah->ah_sc, "failed to reset the MAC Chip\n");
500                 return -EIO;
501         }
502
503         if (ah->ah_version == AR5K_AR5210)
504                 udelay(2300);
505
506         /* ...wakeup again!*/
507         ret = ath5k_hw_set_power(ah, AR5K_PM_AWAKE, true, 0);
508         if (ret) {
509                 ATH5K_ERR(ah->ah_sc, "failed to resume the MAC Chip\n");
510                 return ret;
511         }
512
513         /* ...final warm reset */
514         if (ath5k_hw_nic_reset(ah, 0)) {
515                 ATH5K_ERR(ah->ah_sc, "failed to warm reset the MAC Chip\n");
516                 return -EIO;
517         }
518
519         if (ah->ah_version != AR5K_AR5210) {
520                 /* ...set the PHY operating mode */
521                 ath5k_hw_reg_write(ah, clock, AR5K_PHY_PLL);
522                 udelay(300);
523
524                 ath5k_hw_reg_write(ah, mode, AR5K_PHY_MODE);
525                 ath5k_hw_reg_write(ah, turbo, AR5K_PHY_TURBO);
526         }
527
528         return 0;
529 }
530
531 /*
532  * Get the rate table for a specific operation mode
533  */
534 const struct ath5k_rate_table *ath5k_hw_get_rate_table(struct ath5k_hw *ah,
535                 unsigned int mode)
536 {
537         ATH5K_TRACE(ah->ah_sc);
538
539         if (!test_bit(mode, ah->ah_capabilities.cap_mode))
540                 return NULL;
541
542         /* Get rate tables */
543         switch (mode) {
544         case AR5K_MODE_11A:
545                 return &ath5k_rt_11a;
546         case AR5K_MODE_11A_TURBO:
547                 return &ath5k_rt_turbo;
548         case AR5K_MODE_11B:
549                 return &ath5k_rt_11b;
550         case AR5K_MODE_11G:
551                 return &ath5k_rt_11g;
552         case AR5K_MODE_11G_TURBO:
553                 return &ath5k_rt_xr;
554         }
555
556         return NULL;
557 }
558
559 /*
560  * Free the ath5k_hw struct
561  */
562 void ath5k_hw_detach(struct ath5k_hw *ah)
563 {
564         ATH5K_TRACE(ah->ah_sc);
565
566         __set_bit(ATH_STAT_INVALID, ah->ah_sc->status);
567
568         if (ah->ah_rf_banks != NULL)
569                 kfree(ah->ah_rf_banks);
570
571         /* assume interrupts are down */
572         kfree(ah);
573 }
574
575 /****************************\
576   Reset function and helpers
577 \****************************/
578
579 /**
580  * ath5k_hw_write_ofdm_timings - set OFDM timings on AR5212
581  *
582  * @ah: the &struct ath5k_hw
583  * @channel: the currently set channel upon reset
584  *
585  * Write the OFDM timings for the AR5212 upon reset. This is a helper for
586  * ath5k_hw_reset(). This seems to tune the PLL a specified frequency
587  * depending on the bandwidth of the channel.
588  *
589  */
590 static inline int ath5k_hw_write_ofdm_timings(struct ath5k_hw *ah,
591         struct ieee80211_channel *channel)
592 {
593         /* Get exponent and mantissa and set it */
594         u32 coef_scaled, coef_exp, coef_man,
595                 ds_coef_exp, ds_coef_man, clock;
596
597         if (!(ah->ah_version == AR5K_AR5212) ||
598                 !(channel->hw_value & CHANNEL_OFDM))
599                 BUG();
600
601         /* Seems there are two PLLs, one for baseband sampling and one
602          * for tuning. Tuning basebands are 40 MHz or 80MHz when in
603          * turbo. */
604         clock = channel->hw_value & CHANNEL_TURBO ? 80 : 40;
605         coef_scaled = ((5 * (clock << 24)) / 2) /
606         channel->center_freq;
607
608         for (coef_exp = 31; coef_exp > 0; coef_exp--)
609                 if ((coef_scaled >> coef_exp) & 0x1)
610                         break;
611
612         if (!coef_exp)
613                 return -EINVAL;
614
615         coef_exp = 14 - (coef_exp - 24);
616         coef_man = coef_scaled +
617                 (1 << (24 - coef_exp - 1));
618         ds_coef_man = coef_man >> (24 - coef_exp);
619         ds_coef_exp = coef_exp - 16;
620
621         AR5K_REG_WRITE_BITS(ah, AR5K_PHY_TIMING_3,
622                 AR5K_PHY_TIMING_3_DSC_MAN, ds_coef_man);
623         AR5K_REG_WRITE_BITS(ah, AR5K_PHY_TIMING_3,
624                 AR5K_PHY_TIMING_3_DSC_EXP, ds_coef_exp);
625
626         return 0;
627 }
628
629 /**
630  * ath5k_hw_write_rate_duration - set rate duration during hw resets
631  *
632  * @ah: the &struct ath5k_hw
633  * @mode: one of enum ath5k_driver_mode
634  *
635  * Write the rate duration table for the current mode upon hw reset. This
636  * is a helper for ath5k_hw_reset(). It seems all this is doing is setting
637  * an ACK timeout for the hardware for the current mode for each rate. The
638  * rates which are capable of short preamble (802.11b rates 2Mbps, 5.5Mbps,
639  * and 11Mbps) have another register for the short preamble ACK timeout
640  * calculation.
641  *
642  */
643 static inline void ath5k_hw_write_rate_duration(struct ath5k_hw *ah,
644        unsigned int mode)
645 {
646         struct ath5k_softc *sc = ah->ah_sc;
647         const struct ath5k_rate_table *rt;
648         struct ieee80211_rate srate = {};
649         unsigned int i;
650
651         /* Get rate table for the current operating mode */
652         rt = ath5k_hw_get_rate_table(ah, mode);
653
654         /* Write rate duration table */
655         for (i = 0; i < rt->rate_count; i++) {
656                 const struct ath5k_rate *rate, *control_rate;
657
658                 u32 reg;
659                 u16 tx_time;
660
661                 rate = &rt->rates[i];
662                 control_rate = &rt->rates[rate->control_rate];
663
664                 /* Set ACK timeout */
665                 reg = AR5K_RATE_DUR(rate->rate_code);
666
667                 srate.bitrate = control_rate->rate_kbps/100;
668
669                 /* An ACK frame consists of 10 bytes. If you add the FCS,
670                  * which ieee80211_generic_frame_duration() adds,
671                  * its 14 bytes. Note we use the control rate and not the
672                  * actual rate for this rate. See mac80211 tx.c
673                  * ieee80211_duration() for a brief description of
674                  * what rate we should choose to TX ACKs. */
675                 tx_time = le16_to_cpu(ieee80211_generic_frame_duration(sc->hw,
676                                                         sc->vif, 10, &srate));
677
678                 ath5k_hw_reg_write(ah, tx_time, reg);
679
680                 if (!HAS_SHPREAMBLE(i))
681                         continue;
682
683                 /*
684                  * We're not distinguishing short preamble here,
685                  * This is true, all we'll get is a longer value here
686                  * which is not necessarilly bad. We could use
687                  * export ieee80211_frame_duration() but that needs to be
688                  * fixed first to be properly used by mac802111 drivers:
689                  *
690                  *  - remove erp stuff and let the routine figure ofdm
691                  *    erp rates
692                  *  - remove passing argument ieee80211_local as
693                  *    drivers don't have access to it
694                  *  - move drivers using ieee80211_generic_frame_duration()
695                  *    to this
696                  */
697                 ath5k_hw_reg_write(ah, tx_time,
698                         reg + (AR5K_SET_SHORT_PREAMBLE << 2));
699         }
700 }
701
702 /*
703  * Main reset function
704  */
705 int ath5k_hw_reset(struct ath5k_hw *ah, enum ieee80211_if_types op_mode,
706         struct ieee80211_channel *channel, bool change_channel)
707 {
708         struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
709         struct pci_dev *pdev = ah->ah_sc->pdev;
710         u32 data, s_seq, s_ant, s_led[3], dma_size;
711         unsigned int i, mode, freq, ee_mode, ant[2];
712         int ret;
713
714         ATH5K_TRACE(ah->ah_sc);
715
716         s_seq = 0;
717         s_ant = 0;
718         ee_mode = 0;
719         freq = 0;
720         mode = 0;
721
722         /*
723          * Save some registers before a reset
724          */
725         /*DCU/Antenna selection not available on 5210*/
726         if (ah->ah_version != AR5K_AR5210) {
727                 if (change_channel) {
728                         /* Seq number for queue 0 -do this for all queues ? */
729                         s_seq = ath5k_hw_reg_read(ah,
730                                         AR5K_QUEUE_DFS_SEQNUM(0));
731                         /*Default antenna*/
732                         s_ant = ath5k_hw_reg_read(ah, AR5K_DEFAULT_ANTENNA);
733                 }
734         }
735
736         /*GPIOs*/
737         s_led[0] = ath5k_hw_reg_read(ah, AR5K_PCICFG) & AR5K_PCICFG_LEDSTATE;
738         s_led[1] = ath5k_hw_reg_read(ah, AR5K_GPIOCR);
739         s_led[2] = ath5k_hw_reg_read(ah, AR5K_GPIODO);
740
741         if (change_channel && ah->ah_rf_banks != NULL)
742                 ath5k_hw_get_rf_gain(ah);
743
744
745         /*Wakeup the device*/
746         ret = ath5k_hw_nic_wakeup(ah, channel->hw_value, false);
747         if (ret)
748                 return ret;
749
750         /*
751          * Initialize operating mode
752          */
753         ah->ah_op_mode = op_mode;
754
755         /*
756          * 5111/5112 Settings
757          * 5210 only comes with RF5110
758          */
759         if (ah->ah_version != AR5K_AR5210) {
760                 if (ah->ah_radio != AR5K_RF5111 &&
761                         ah->ah_radio != AR5K_RF5112 &&
762                         ah->ah_radio != AR5K_RF5413 &&
763                         ah->ah_radio != AR5K_RF2413 &&
764                         ah->ah_radio != AR5K_RF2425) {
765                         ATH5K_ERR(ah->ah_sc,
766                                 "invalid phy radio: %u\n", ah->ah_radio);
767                         return -EINVAL;
768                 }
769
770                 switch (channel->hw_value & CHANNEL_MODES) {
771                 case CHANNEL_A:
772                         mode = AR5K_MODE_11A;
773                         freq = AR5K_INI_RFGAIN_5GHZ;
774                         ee_mode = AR5K_EEPROM_MODE_11A;
775                         break;
776                 case CHANNEL_G:
777                         mode = AR5K_MODE_11G;
778                         freq = AR5K_INI_RFGAIN_2GHZ;
779                         ee_mode = AR5K_EEPROM_MODE_11G;
780                         break;
781                 case CHANNEL_B:
782                         mode = AR5K_MODE_11B;
783                         freq = AR5K_INI_RFGAIN_2GHZ;
784                         ee_mode = AR5K_EEPROM_MODE_11B;
785                         break;
786                 case CHANNEL_T:
787                         mode = AR5K_MODE_11A_TURBO;
788                         freq = AR5K_INI_RFGAIN_5GHZ;
789                         ee_mode = AR5K_EEPROM_MODE_11A;
790                         break;
791                 /*Is this ok on 5211 too ?*/
792                 case CHANNEL_TG:
793                         mode = AR5K_MODE_11G_TURBO;
794                         freq = AR5K_INI_RFGAIN_2GHZ;
795                         ee_mode = AR5K_EEPROM_MODE_11G;
796                         break;
797                 case CHANNEL_XR:
798                         if (ah->ah_version == AR5K_AR5211) {
799                                 ATH5K_ERR(ah->ah_sc,
800                                         "XR mode not available on 5211");
801                                 return -EINVAL;
802                         }
803                         mode = AR5K_MODE_XR;
804                         freq = AR5K_INI_RFGAIN_5GHZ;
805                         ee_mode = AR5K_EEPROM_MODE_11A;
806                         break;
807                 default:
808                         ATH5K_ERR(ah->ah_sc,
809                                 "invalid channel: %d\n", channel->center_freq);
810                         return -EINVAL;
811                 }
812
813                 /* PHY access enable */
814                 ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ, AR5K_PHY(0));
815
816         }
817
818         ret = ath5k_hw_write_initvals(ah, mode, change_channel);
819         if (ret)
820                 return ret;
821
822         /*
823          * 5211/5212 Specific
824          */
825         if (ah->ah_version != AR5K_AR5210) {
826                 /*
827                  * Write initial RF gain settings
828                  * This should work for both 5111/5112
829                  */
830                 ret = ath5k_hw_rfgain(ah, freq);
831                 if (ret)
832                         return ret;
833
834                 mdelay(1);
835
836                 /*
837                  * Write some more initial register settings
838                  */
839                 if (ah->ah_version == AR5K_AR5212) {
840                         ath5k_hw_reg_write(ah, 0x0002a002, AR5K_PHY(11));
841
842                         if (channel->hw_value == CHANNEL_G)
843                                 if (ah->ah_mac_srev < AR5K_SREV_VER_AR2413)
844                                         ath5k_hw_reg_write(ah, 0x00f80d80,
845                                                 AR5K_PHY(83));
846                                 else if (ah->ah_mac_srev < AR5K_SREV_VER_AR2424)
847                                         ath5k_hw_reg_write(ah, 0x00380140,
848                                                 AR5K_PHY(83));
849                                 else if (ah->ah_mac_srev < AR5K_SREV_VER_AR2425)
850                                         ath5k_hw_reg_write(ah, 0x00fc0ec0,
851                                                 AR5K_PHY(83));
852                                 else /* 2425 */
853                                         ath5k_hw_reg_write(ah, 0x00fc0fc0,
854                                                 AR5K_PHY(83));
855                         else
856                                 ath5k_hw_reg_write(ah, 0x00000000,
857                                         AR5K_PHY(83));
858
859                         ath5k_hw_reg_write(ah, 0x000009b5, 0xa228);
860                         ath5k_hw_reg_write(ah, 0x0000000f, 0x8060);
861                         ath5k_hw_reg_write(ah, 0x00000000, 0xa254);
862                         ath5k_hw_reg_write(ah, 0x0000000e, AR5K_PHY_SCAL);
863                 }
864
865                 /* Fix for first revision of the RF5112 RF chipset */
866                 if (ah->ah_radio >= AR5K_RF5112 &&
867                                 ah->ah_radio_5ghz_revision <
868                                 AR5K_SREV_RAD_5112A) {
869                         ath5k_hw_reg_write(ah, AR5K_PHY_CCKTXCTL_WORLD,
870                                         AR5K_PHY_CCKTXCTL);
871                         if (channel->hw_value & CHANNEL_5GHZ)
872                                 data = 0xffb81020;
873                         else
874                                 data = 0xffb80d20;
875                         ath5k_hw_reg_write(ah, data, AR5K_PHY_FRAME_CTL);
876                 }
877
878                 /*
879                  * Set TX power (FIXME)
880                  */
881                 ret = ath5k_hw_txpower(ah, channel, AR5K_TUNE_DEFAULT_TXPOWER);
882                 if (ret)
883                         return ret;
884
885                 /* Write rate duration table only on AR5212 and if
886                  * virtual interface has already been brought up
887                  * XXX: rethink this after new mode changes to
888                  * mac80211 are integrated */
889                 if (ah->ah_version == AR5K_AR5212 &&
890                         ah->ah_sc->vif != NULL)
891                         ath5k_hw_write_rate_duration(ah, mode);
892
893                 /*
894                  * Write RF registers
895                  * TODO:Does this work on 5211 (5111) ?
896                  */
897                 ret = ath5k_hw_rfregs(ah, channel, mode);
898                 if (ret)
899                         return ret;
900
901                 /*
902                  * Configure additional registers
903                  */
904
905                 /* Write OFDM timings on 5212*/
906                 if (ah->ah_version == AR5K_AR5212 &&
907                         channel->hw_value & CHANNEL_OFDM) {
908                         ret = ath5k_hw_write_ofdm_timings(ah, channel);
909                         if (ret)
910                                 return ret;
911                 }
912
913                 /*Enable/disable 802.11b mode on 5111
914                 (enable 2111 frequency converter + CCK)*/
915                 if (ah->ah_radio == AR5K_RF5111) {
916                         if (mode == AR5K_MODE_11B)
917                                 AR5K_REG_ENABLE_BITS(ah, AR5K_TXCFG,
918                                     AR5K_TXCFG_B_MODE);
919                         else
920                                 AR5K_REG_DISABLE_BITS(ah, AR5K_TXCFG,
921                                     AR5K_TXCFG_B_MODE);
922                 }
923
924                 /*
925                  * Set channel and calibrate the PHY
926                  */
927                 ret = ath5k_hw_channel(ah, channel);
928                 if (ret)
929                         return ret;
930
931                 /* Set antenna mode */
932                 AR5K_REG_MASKED_BITS(ah, AR5K_PHY(0x44),
933                         ah->ah_antenna[ee_mode][0], 0xfffffc06);
934
935                 /*
936                  * In case a fixed antenna was set as default
937                  * write the same settings on both AR5K_PHY_ANT_SWITCH_TABLE
938                  * registers.
939                  */
940                 if (s_ant != 0){
941                         if (s_ant == AR5K_ANT_FIXED_A) /* 1 - Main */
942                                 ant[0] = ant[1] = AR5K_ANT_FIXED_A;
943                         else    /* 2 - Aux */
944                                 ant[0] = ant[1] = AR5K_ANT_FIXED_B;
945                 } else {
946                         ant[0] = AR5K_ANT_FIXED_A;
947                         ant[1] = AR5K_ANT_FIXED_B;
948                 }
949
950                 ath5k_hw_reg_write(ah, ah->ah_antenna[ee_mode][ant[0]],
951                         AR5K_PHY_ANT_SWITCH_TABLE_0);
952                 ath5k_hw_reg_write(ah, ah->ah_antenna[ee_mode][ant[1]],
953                         AR5K_PHY_ANT_SWITCH_TABLE_1);
954
955                 /* Commit values from EEPROM */
956                 if (ah->ah_radio == AR5K_RF5111)
957                         AR5K_REG_WRITE_BITS(ah, AR5K_PHY_FRAME_CTL,
958                             AR5K_PHY_FRAME_CTL_TX_CLIP, ee->ee_tx_clip);
959
960                 ath5k_hw_reg_write(ah,
961                         AR5K_PHY_NF_SVAL(ee->ee_noise_floor_thr[ee_mode]),
962                         AR5K_PHY(0x5a));
963
964                 AR5K_REG_MASKED_BITS(ah, AR5K_PHY(0x11),
965                         (ee->ee_switch_settling[ee_mode] << 7) & 0x3f80,
966                         0xffffc07f);
967                 AR5K_REG_MASKED_BITS(ah, AR5K_PHY(0x12),
968                         (ee->ee_ant_tx_rx[ee_mode] << 12) & 0x3f000,
969                         0xfffc0fff);
970                 AR5K_REG_MASKED_BITS(ah, AR5K_PHY(0x14),
971                         (ee->ee_adc_desired_size[ee_mode] & 0x00ff) |
972                         ((ee->ee_pga_desired_size[ee_mode] << 8) & 0xff00),
973                         0xffff0000);
974
975                 ath5k_hw_reg_write(ah,
976                         (ee->ee_tx_end2xpa_disable[ee_mode] << 24) |
977                         (ee->ee_tx_end2xpa_disable[ee_mode] << 16) |
978                         (ee->ee_tx_frm2xpa_enable[ee_mode] << 8) |
979                         (ee->ee_tx_frm2xpa_enable[ee_mode]), AR5K_PHY(0x0d));
980
981                 AR5K_REG_MASKED_BITS(ah, AR5K_PHY(0x0a),
982                         ee->ee_tx_end2xlna_enable[ee_mode] << 8, 0xffff00ff);
983                 AR5K_REG_MASKED_BITS(ah, AR5K_PHY(0x19),
984                         (ee->ee_thr_62[ee_mode] << 12) & 0x7f000, 0xfff80fff);
985                 AR5K_REG_MASKED_BITS(ah, AR5K_PHY(0x49), 4, 0xffffff01);
986
987                 AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ,
988                     AR5K_PHY_IQ_CORR_ENABLE |
989                     (ee->ee_i_cal[ee_mode] << AR5K_PHY_IQ_CORR_Q_I_COFF_S) |
990                     ee->ee_q_cal[ee_mode]);
991
992                 if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_1)
993                         AR5K_REG_WRITE_BITS(ah, AR5K_PHY_GAIN_2GHZ,
994                                 AR5K_PHY_GAIN_2GHZ_MARGIN_TXRX,
995                                 ee->ee_margin_tx_rx[ee_mode]);
996
997         } else {
998                 mdelay(1);
999                 /* Disable phy and wait */
1000                 ath5k_hw_reg_write(ah, AR5K_PHY_ACT_DISABLE, AR5K_PHY_ACT);
1001                 mdelay(1);
1002         }
1003
1004         /*
1005          * Restore saved values
1006          */
1007         /*DCU/Antenna selection not available on 5210*/
1008         if (ah->ah_version != AR5K_AR5210) {
1009                 ath5k_hw_reg_write(ah, s_seq, AR5K_QUEUE_DFS_SEQNUM(0));
1010                 ath5k_hw_reg_write(ah, s_ant, AR5K_DEFAULT_ANTENNA);
1011         }
1012         AR5K_REG_ENABLE_BITS(ah, AR5K_PCICFG, s_led[0]);
1013         ath5k_hw_reg_write(ah, s_led[1], AR5K_GPIOCR);
1014         ath5k_hw_reg_write(ah, s_led[2], AR5K_GPIODO);
1015
1016         /*
1017          * Misc
1018          */
1019         /* XXX: add ah->aid once mac80211 gives this to us */
1020         ath5k_hw_set_associd(ah, ah->ah_bssid, 0);
1021
1022         ath5k_hw_set_opmode(ah);
1023         /*PISR/SISR Not available on 5210*/
1024         if (ah->ah_version != AR5K_AR5210) {
1025                 ath5k_hw_reg_write(ah, 0xffffffff, AR5K_PISR);
1026                 /* If we later allow tuning for this, store into sc structure */
1027                 data = AR5K_TUNE_RSSI_THRES |
1028                         AR5K_TUNE_BMISS_THRES << AR5K_RSSI_THR_BMISS_S;
1029                 ath5k_hw_reg_write(ah, data, AR5K_RSSI_THR);
1030         }
1031
1032         /*
1033          * Set Rx/Tx DMA Configuration
1034          *
1035          * Set maximum DMA size (512) except for PCI-E cards since
1036          * it causes rx overruns and tx errors (tested on 5424 but since
1037          * rx overruns also occur on 5416/5418 with madwifi we set 128
1038          * for all PCI-E cards to be safe).
1039          *
1040          * In dumps this is 128 for allchips.
1041          *
1042          * XXX: need to check 5210 for this
1043          * TODO: Check out tx triger level, it's always 64 on dumps but I
1044          * guess we can tweak it and see how it goes ;-)
1045          */
1046         dma_size = (pdev->is_pcie) ? AR5K_DMASIZE_128B : AR5K_DMASIZE_512B;
1047         if (ah->ah_version != AR5K_AR5210) {
1048                 AR5K_REG_WRITE_BITS(ah, AR5K_TXCFG,
1049                         AR5K_TXCFG_SDMAMR, dma_size);
1050                 AR5K_REG_WRITE_BITS(ah, AR5K_RXCFG,
1051                         AR5K_RXCFG_SDMAMW, dma_size);
1052         }
1053
1054         /*
1055          * Enable the PHY and wait until completion
1056          */
1057         ath5k_hw_reg_write(ah, AR5K_PHY_ACT_ENABLE, AR5K_PHY_ACT);
1058
1059         /*
1060          * 5111/5112 Specific
1061          */
1062         if (ah->ah_version != AR5K_AR5210) {
1063                 data = ath5k_hw_reg_read(ah, AR5K_PHY_RX_DELAY) &
1064                         AR5K_PHY_RX_DELAY_M;
1065                 data = (channel->hw_value & CHANNEL_CCK) ?
1066                         ((data << 2) / 22) : (data / 10);
1067
1068                 udelay(100 + data);
1069         } else {
1070                 mdelay(1);
1071         }
1072
1073         /*
1074          * Enable calibration and wait until completion
1075          */
1076         AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGCCTL,
1077                                 AR5K_PHY_AGCCTL_CAL);
1078
1079         if (ath5k_hw_register_timeout(ah, AR5K_PHY_AGCCTL,
1080                         AR5K_PHY_AGCCTL_CAL, 0, false)) {
1081                 ATH5K_ERR(ah->ah_sc, "calibration timeout (%uMHz)\n",
1082                         channel->center_freq);
1083                 return -EAGAIN;
1084         }
1085
1086         ret = ath5k_hw_noise_floor_calibration(ah, channel->center_freq);
1087         if (ret)
1088                 return ret;
1089
1090         ah->ah_calibration = false;
1091
1092         /* A and G modes can use QAM modulation which requires enabling
1093          * I and Q calibration. Don't bother in B mode. */
1094         if (!(mode == AR5K_MODE_11B)) {
1095                 ah->ah_calibration = true;
1096                 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_IQ,
1097                                 AR5K_PHY_IQ_CAL_NUM_LOG_MAX, 15);
1098                 AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ,
1099                                 AR5K_PHY_IQ_RUN);
1100         }
1101
1102         /*
1103          * Reset queues and start beacon timers at the end of the reset routine
1104          */
1105         for (i = 0; i < ah->ah_capabilities.cap_queues.q_tx_num; i++) {
1106                 /*No QCU on 5210*/
1107                 if (ah->ah_version != AR5K_AR5210)
1108                         AR5K_REG_WRITE_Q(ah, AR5K_QUEUE_QCUMASK(i), i);
1109
1110                 ret = ath5k_hw_reset_tx_queue(ah, i);
1111                 if (ret) {
1112                         ATH5K_ERR(ah->ah_sc,
1113                                 "failed to reset TX queue #%d\n", i);
1114                         return ret;
1115                 }
1116         }
1117
1118         /* Pre-enable interrupts on 5211/5212*/
1119         if (ah->ah_version != AR5K_AR5210)
1120                 ath5k_hw_set_intr(ah, AR5K_INT_RX | AR5K_INT_TX |
1121                                 AR5K_INT_FATAL);
1122
1123         /*
1124          * Set RF kill flags if supported by the device (read from the EEPROM)
1125          * Disable gpio_intr for now since it results system hang.
1126          * TODO: Handle this in ath5k_intr
1127          */
1128 #if 0
1129         if (AR5K_EEPROM_HDR_RFKILL(ah->ah_capabilities.cap_eeprom.ee_header)) {
1130                 ath5k_hw_set_gpio_input(ah, 0);
1131                 ah->ah_gpio[0] = ath5k_hw_get_gpio(ah, 0);
1132                 if (ah->ah_gpio[0] == 0)
1133                         ath5k_hw_set_gpio_intr(ah, 0, 1);
1134                 else
1135                         ath5k_hw_set_gpio_intr(ah, 0, 0);
1136         }
1137 #endif
1138
1139         /*
1140          * Set the 32MHz reference clock on 5212 phy clock sleep register
1141          *
1142          * TODO: Find out how to switch to external 32Khz clock to save power
1143          */
1144         if (ah->ah_version == AR5K_AR5212) {
1145                 ath5k_hw_reg_write(ah, AR5K_PHY_SCR_32MHZ, AR5K_PHY_SCR);
1146                 ath5k_hw_reg_write(ah, AR5K_PHY_SLMT_32MHZ, AR5K_PHY_SLMT);
1147                 ath5k_hw_reg_write(ah, AR5K_PHY_SCAL_32MHZ, AR5K_PHY_SCAL);
1148                 ath5k_hw_reg_write(ah, AR5K_PHY_SCLOCK_32MHZ, AR5K_PHY_SCLOCK);
1149                 ath5k_hw_reg_write(ah, AR5K_PHY_SDELAY_32MHZ, AR5K_PHY_SDELAY);
1150                 ath5k_hw_reg_write(ah, ah->ah_phy_spending, AR5K_PHY_SPENDING);
1151         }
1152
1153         if (ah->ah_version == AR5K_AR5212) {
1154                 ath5k_hw_reg_write(ah, 0x000100aa, 0x8118);
1155                 ath5k_hw_reg_write(ah, 0x00003210, 0x811c);
1156                 ath5k_hw_reg_write(ah, 0x00000052, 0x8108);
1157                 if (ah->ah_mac_srev >= AR5K_SREV_VER_AR2413)
1158                         ath5k_hw_reg_write(ah, 0x00000004, 0x8120);
1159         }
1160
1161         /*
1162          * Disable beacons and reset the register
1163          */
1164         AR5K_REG_DISABLE_BITS(ah, AR5K_BEACON, AR5K_BEACON_ENABLE |
1165                         AR5K_BEACON_RESET_TSF);
1166
1167         return 0;
1168 }
1169
1170 /*
1171  * Reset chipset
1172  */
1173 static int ath5k_hw_nic_reset(struct ath5k_hw *ah, u32 val)
1174 {
1175         int ret;
1176         u32 mask = val ? val : ~0U;
1177
1178         ATH5K_TRACE(ah->ah_sc);
1179
1180         /* Read-and-clear RX Descriptor Pointer*/
1181         ath5k_hw_reg_read(ah, AR5K_RXDP);
1182
1183         /*
1184          * Reset the device and wait until success
1185          */
1186         ath5k_hw_reg_write(ah, val, AR5K_RESET_CTL);
1187
1188         /* Wait at least 128 PCI clocks */
1189         udelay(15);
1190
1191         if (ah->ah_version == AR5K_AR5210) {
1192                 val &= AR5K_RESET_CTL_CHIP;
1193                 mask &= AR5K_RESET_CTL_CHIP;
1194         } else {
1195                 val &= AR5K_RESET_CTL_PCU | AR5K_RESET_CTL_BASEBAND;
1196                 mask &= AR5K_RESET_CTL_PCU | AR5K_RESET_CTL_BASEBAND;
1197         }
1198
1199         ret = ath5k_hw_register_timeout(ah, AR5K_RESET_CTL, mask, val, false);
1200
1201         /*
1202          * Reset configuration register (for hw byte-swap). Note that this
1203          * is only set for big endian. We do the necessary magic in
1204          * AR5K_INIT_CFG.
1205          */
1206         if ((val & AR5K_RESET_CTL_PCU) == 0)
1207                 ath5k_hw_reg_write(ah, AR5K_INIT_CFG, AR5K_CFG);
1208
1209         return ret;
1210 }
1211
1212 /*
1213  * Power management functions
1214  */
1215
1216 /*
1217  * Sleep control
1218  */
1219 int ath5k_hw_set_power(struct ath5k_hw *ah, enum ath5k_power_mode mode,
1220                 bool set_chip, u16 sleep_duration)
1221 {
1222         unsigned int i;
1223         u32 staid;
1224
1225         ATH5K_TRACE(ah->ah_sc);
1226         staid = ath5k_hw_reg_read(ah, AR5K_STA_ID1);
1227
1228         switch (mode) {
1229         case AR5K_PM_AUTO:
1230                 staid &= ~AR5K_STA_ID1_DEFAULT_ANTENNA;
1231                 /* fallthrough */
1232         case AR5K_PM_NETWORK_SLEEP:
1233                 if (set_chip)
1234                         ath5k_hw_reg_write(ah,
1235                                 AR5K_SLEEP_CTL_SLE | sleep_duration,
1236                                 AR5K_SLEEP_CTL);
1237
1238                 staid |= AR5K_STA_ID1_PWR_SV;
1239                 break;
1240
1241         case AR5K_PM_FULL_SLEEP:
1242                 if (set_chip)
1243                         ath5k_hw_reg_write(ah, AR5K_SLEEP_CTL_SLE_SLP,
1244                                 AR5K_SLEEP_CTL);
1245
1246                 staid |= AR5K_STA_ID1_PWR_SV;
1247                 break;
1248
1249         case AR5K_PM_AWAKE:
1250                 if (!set_chip)
1251                         goto commit;
1252
1253                 ath5k_hw_reg_write(ah, AR5K_SLEEP_CTL_SLE_WAKE,
1254                                 AR5K_SLEEP_CTL);
1255
1256                 for (i = 5000; i > 0; i--) {
1257                         /* Check if the chip did wake up */
1258                         if ((ath5k_hw_reg_read(ah, AR5K_PCICFG) &
1259                                         AR5K_PCICFG_SPWR_DN) == 0)
1260                                 break;
1261
1262                         /* Wait a bit and retry */
1263                         udelay(200);
1264                         ath5k_hw_reg_write(ah, AR5K_SLEEP_CTL_SLE_WAKE,
1265                                 AR5K_SLEEP_CTL);
1266                 }
1267
1268                 /* Fail if the chip didn't wake up */
1269                 if (i <= 0)
1270                         return -EIO;
1271
1272                 staid &= ~AR5K_STA_ID1_PWR_SV;
1273                 break;
1274
1275         default:
1276                 return -EINVAL;
1277         }
1278
1279 commit:
1280         ah->ah_power_mode = mode;
1281         ath5k_hw_reg_write(ah, staid, AR5K_STA_ID1);
1282
1283         return 0;
1284 }
1285
1286 /***********************\
1287   DMA Related Functions
1288 \***********************/
1289
1290 /*
1291  * Receive functions
1292  */
1293
1294 /*
1295  * Start DMA receive
1296  */
1297 void ath5k_hw_start_rx(struct ath5k_hw *ah)
1298 {
1299         ATH5K_TRACE(ah->ah_sc);
1300         ath5k_hw_reg_write(ah, AR5K_CR_RXE, AR5K_CR);
1301 }
1302
1303 /*
1304  * Stop DMA receive
1305  */
1306 int ath5k_hw_stop_rx_dma(struct ath5k_hw *ah)
1307 {
1308         unsigned int i;
1309
1310         ATH5K_TRACE(ah->ah_sc);
1311         ath5k_hw_reg_write(ah, AR5K_CR_RXD, AR5K_CR);
1312
1313         /*
1314          * It may take some time to disable the DMA receive unit
1315          */
1316         for (i = 2000; i > 0 &&
1317                         (ath5k_hw_reg_read(ah, AR5K_CR) & AR5K_CR_RXE) != 0;
1318                         i--)
1319                 udelay(10);
1320
1321         return i ? 0 : -EBUSY;
1322 }
1323
1324 /*
1325  * Get the address of the RX Descriptor
1326  */
1327 u32 ath5k_hw_get_rx_buf(struct ath5k_hw *ah)
1328 {
1329         return ath5k_hw_reg_read(ah, AR5K_RXDP);
1330 }
1331
1332 /*
1333  * Set the address of the RX Descriptor
1334  */
1335 void ath5k_hw_put_rx_buf(struct ath5k_hw *ah, u32 phys_addr)
1336 {
1337         ATH5K_TRACE(ah->ah_sc);
1338
1339         /*TODO:Shouldn't we check if RX is enabled first ?*/
1340         ath5k_hw_reg_write(ah, phys_addr, AR5K_RXDP);
1341 }
1342
1343 /*
1344  * Transmit functions
1345  */
1346
1347 /*
1348  * Start DMA transmit for a specific queue
1349  * (see also QCU/DCU functions)
1350  */
1351 int ath5k_hw_tx_start(struct ath5k_hw *ah, unsigned int queue)
1352 {
1353         u32 tx_queue;
1354
1355         ATH5K_TRACE(ah->ah_sc);
1356         AR5K_ASSERT_ENTRY(queue, ah->ah_capabilities.cap_queues.q_tx_num);
1357
1358         /* Return if queue is declared inactive */
1359         if (ah->ah_txq[queue].tqi_type == AR5K_TX_QUEUE_INACTIVE)
1360                 return -EIO;
1361
1362         if (ah->ah_version == AR5K_AR5210) {
1363                 tx_queue = ath5k_hw_reg_read(ah, AR5K_CR);
1364
1365                 /*
1366                  * Set the queue by type on 5210
1367                  */
1368                 switch (ah->ah_txq[queue].tqi_type) {
1369                 case AR5K_TX_QUEUE_DATA:
1370                         tx_queue |= AR5K_CR_TXE0 & ~AR5K_CR_TXD0;
1371                         break;
1372                 case AR5K_TX_QUEUE_BEACON:
1373                         tx_queue |= AR5K_CR_TXE1 & ~AR5K_CR_TXD1;
1374                         ath5k_hw_reg_write(ah, AR5K_BCR_TQ1V | AR5K_BCR_BDMAE,
1375                                         AR5K_BSR);
1376                         break;
1377                 case AR5K_TX_QUEUE_CAB:
1378                         tx_queue |= AR5K_CR_TXE1 & ~AR5K_CR_TXD1;
1379                         ath5k_hw_reg_write(ah, AR5K_BCR_TQ1FV | AR5K_BCR_TQ1V |
1380                                 AR5K_BCR_BDMAE, AR5K_BSR);
1381                         break;
1382                 default:
1383                         return -EINVAL;
1384                 }
1385                 /* Start queue */
1386                 ath5k_hw_reg_write(ah, tx_queue, AR5K_CR);
1387         } else {
1388                 /* Return if queue is disabled */
1389                 if (AR5K_REG_READ_Q(ah, AR5K_QCU_TXD, queue))
1390                         return -EIO;
1391
1392                 /* Start queue */
1393                 AR5K_REG_WRITE_Q(ah, AR5K_QCU_TXE, queue);
1394         }
1395
1396         return 0;
1397 }
1398
1399 /*
1400  * Stop DMA transmit for a specific queue
1401  * (see also QCU/DCU functions)
1402  */
1403 int ath5k_hw_stop_tx_dma(struct ath5k_hw *ah, unsigned int queue)
1404 {
1405         unsigned int i = 100;
1406         u32 tx_queue, pending;
1407
1408         ATH5K_TRACE(ah->ah_sc);
1409         AR5K_ASSERT_ENTRY(queue, ah->ah_capabilities.cap_queues.q_tx_num);
1410
1411         /* Return if queue is declared inactive */
1412         if (ah->ah_txq[queue].tqi_type == AR5K_TX_QUEUE_INACTIVE)
1413                 return -EIO;
1414
1415         if (ah->ah_version == AR5K_AR5210) {
1416                 tx_queue = ath5k_hw_reg_read(ah, AR5K_CR);
1417
1418                 /*
1419                  * Set by queue type
1420                  */
1421                 switch (ah->ah_txq[queue].tqi_type) {
1422                 case AR5K_TX_QUEUE_DATA:
1423                         tx_queue |= AR5K_CR_TXD0 & ~AR5K_CR_TXE0;
1424                         break;
1425                 case AR5K_TX_QUEUE_BEACON:
1426                 case AR5K_TX_QUEUE_CAB:
1427                         /* XXX Fix me... */
1428                         tx_queue |= AR5K_CR_TXD1 & ~AR5K_CR_TXD1;
1429                         ath5k_hw_reg_write(ah, 0, AR5K_BSR);
1430                         break;
1431                 default:
1432                         return -EINVAL;
1433                 }
1434
1435                 /* Stop queue */
1436                 ath5k_hw_reg_write(ah, tx_queue, AR5K_CR);
1437         } else {
1438                 /*
1439                  * Schedule TX disable and wait until queue is empty
1440                  */
1441                 AR5K_REG_WRITE_Q(ah, AR5K_QCU_TXD, queue);
1442
1443                 /*Check for pending frames*/
1444                 do {
1445                         pending = ath5k_hw_reg_read(ah,
1446                                 AR5K_QUEUE_STATUS(queue)) &
1447                                 AR5K_QCU_STS_FRMPENDCNT;
1448                         udelay(100);
1449                 } while (--i && pending);
1450
1451                 /* Clear register */
1452                 ath5k_hw_reg_write(ah, 0, AR5K_QCU_TXD);
1453         }
1454
1455         /* TODO: Check for success else return error */
1456         return 0;
1457 }
1458
1459 /*
1460  * Get the address of the TX Descriptor for a specific queue
1461  * (see also QCU/DCU functions)
1462  */
1463 u32 ath5k_hw_get_tx_buf(struct ath5k_hw *ah, unsigned int queue)
1464 {
1465         u16 tx_reg;
1466
1467         ATH5K_TRACE(ah->ah_sc);
1468         AR5K_ASSERT_ENTRY(queue, ah->ah_capabilities.cap_queues.q_tx_num);
1469
1470         /*
1471          * Get the transmit queue descriptor pointer from the selected queue
1472          */
1473         /*5210 doesn't have QCU*/
1474         if (ah->ah_version == AR5K_AR5210) {
1475                 switch (ah->ah_txq[queue].tqi_type) {
1476                 case AR5K_TX_QUEUE_DATA:
1477                         tx_reg = AR5K_NOQCU_TXDP0;
1478                         break;
1479                 case AR5K_TX_QUEUE_BEACON:
1480                 case AR5K_TX_QUEUE_CAB:
1481                         tx_reg = AR5K_NOQCU_TXDP1;
1482                         break;
1483                 default:
1484                         return 0xffffffff;
1485                 }
1486         } else {
1487                 tx_reg = AR5K_QUEUE_TXDP(queue);
1488         }
1489
1490         return ath5k_hw_reg_read(ah, tx_reg);
1491 }
1492
1493 /*
1494  * Set the address of the TX Descriptor for a specific queue
1495  * (see also QCU/DCU functions)
1496  */
1497 int ath5k_hw_put_tx_buf(struct ath5k_hw *ah, unsigned int queue, u32 phys_addr)
1498 {
1499         u16 tx_reg;
1500
1501         ATH5K_TRACE(ah->ah_sc);
1502         AR5K_ASSERT_ENTRY(queue, ah->ah_capabilities.cap_queues.q_tx_num);
1503
1504         /*
1505          * Set the transmit queue descriptor pointer register by type
1506          * on 5210
1507          */
1508         if (ah->ah_version == AR5K_AR5210) {
1509                 switch (ah->ah_txq[queue].tqi_type) {
1510                 case AR5K_TX_QUEUE_DATA:
1511                         tx_reg = AR5K_NOQCU_TXDP0;
1512                         break;
1513                 case AR5K_TX_QUEUE_BEACON:
1514                 case AR5K_TX_QUEUE_CAB:
1515                         tx_reg = AR5K_NOQCU_TXDP1;
1516                         break;
1517                 default:
1518                         return -EINVAL;
1519                 }
1520         } else {
1521                 /*
1522                  * Set the transmit queue descriptor pointer for
1523                  * the selected queue on QCU for 5211+
1524                  * (this won't work if the queue is still active)
1525                  */
1526                 if (AR5K_REG_READ_Q(ah, AR5K_QCU_TXE, queue))
1527                         return -EIO;
1528
1529                 tx_reg = AR5K_QUEUE_TXDP(queue);
1530         }
1531
1532         /* Set descriptor pointer */
1533         ath5k_hw_reg_write(ah, phys_addr, tx_reg);
1534
1535         return 0;
1536 }
1537
1538 /*
1539  * Update tx trigger level
1540  */
1541 int ath5k_hw_update_tx_triglevel(struct ath5k_hw *ah, bool increase)
1542 {
1543         u32 trigger_level, imr;
1544         int ret = -EIO;
1545
1546         ATH5K_TRACE(ah->ah_sc);
1547
1548         /*
1549          * Disable interrupts by setting the mask
1550          */
1551         imr = ath5k_hw_set_intr(ah, ah->ah_imr & ~AR5K_INT_GLOBAL);
1552
1553         /*TODO: Boundary check on trigger_level*/
1554         trigger_level = AR5K_REG_MS(ath5k_hw_reg_read(ah, AR5K_TXCFG),
1555                         AR5K_TXCFG_TXFULL);
1556
1557         if (!increase) {
1558                 if (--trigger_level < AR5K_TUNE_MIN_TX_FIFO_THRES)
1559                         goto done;
1560         } else
1561                 trigger_level +=
1562                         ((AR5K_TUNE_MAX_TX_FIFO_THRES - trigger_level) / 2);
1563
1564         /*
1565          * Update trigger level on success
1566          */
1567         if (ah->ah_version == AR5K_AR5210)
1568                 ath5k_hw_reg_write(ah, trigger_level, AR5K_TRIG_LVL);
1569         else
1570                 AR5K_REG_WRITE_BITS(ah, AR5K_TXCFG,
1571                                 AR5K_TXCFG_TXFULL, trigger_level);
1572
1573         ret = 0;
1574
1575 done:
1576         /*
1577          * Restore interrupt mask
1578          */
1579         ath5k_hw_set_intr(ah, imr);
1580
1581         return ret;
1582 }
1583
1584 /*
1585  * Interrupt handling
1586  */
1587
1588 /*
1589  * Check if we have pending interrupts
1590  */
1591 bool ath5k_hw_is_intr_pending(struct ath5k_hw *ah)
1592 {
1593         ATH5K_TRACE(ah->ah_sc);
1594         return ath5k_hw_reg_read(ah, AR5K_INTPEND);
1595 }
1596
1597 /*
1598  * Get interrupt mask (ISR)
1599  */
1600 int ath5k_hw_get_isr(struct ath5k_hw *ah, enum ath5k_int *interrupt_mask)
1601 {
1602         u32 data;
1603
1604         ATH5K_TRACE(ah->ah_sc);
1605
1606         /*
1607          * Read interrupt status from the Interrupt Status register
1608          * on 5210
1609          */
1610         if (ah->ah_version == AR5K_AR5210) {
1611                 data = ath5k_hw_reg_read(ah, AR5K_ISR);
1612                 if (unlikely(data == AR5K_INT_NOCARD)) {
1613                         *interrupt_mask = data;
1614                         return -ENODEV;
1615                 }
1616         } else {
1617                 /*
1618                  * Read interrupt status from the Read-And-Clear shadow register
1619                  * Note: PISR/SISR Not available on 5210
1620                  */
1621                 data = ath5k_hw_reg_read(ah, AR5K_RAC_PISR);
1622         }
1623
1624         /*
1625          * Get abstract interrupt mask (driver-compatible)
1626          */
1627         *interrupt_mask = (data & AR5K_INT_COMMON) & ah->ah_imr;
1628
1629         if (unlikely(data == AR5K_INT_NOCARD))
1630                 return -ENODEV;
1631
1632         if (data & (AR5K_ISR_RXOK | AR5K_ISR_RXERR))
1633                 *interrupt_mask |= AR5K_INT_RX;
1634
1635         if (data & (AR5K_ISR_TXOK | AR5K_ISR_TXERR
1636                 | AR5K_ISR_TXDESC | AR5K_ISR_TXEOL))
1637                 *interrupt_mask |= AR5K_INT_TX;
1638
1639         if (ah->ah_version != AR5K_AR5210) {
1640                 /*HIU = Host Interface Unit (PCI etc)*/
1641                 if (unlikely(data & (AR5K_ISR_HIUERR)))
1642                         *interrupt_mask |= AR5K_INT_FATAL;
1643
1644                 /*Beacon Not Ready*/
1645                 if (unlikely(data & (AR5K_ISR_BNR)))
1646                         *interrupt_mask |= AR5K_INT_BNR;
1647         }
1648
1649         /*
1650          * XXX: BMISS interrupts may occur after association.
1651          * I found this on 5210 code but it needs testing. If this is
1652          * true we should disable them before assoc and re-enable them
1653          * after a successfull assoc + some jiffies.
1654          */
1655 #if 0
1656         interrupt_mask &= ~AR5K_INT_BMISS;
1657 #endif
1658
1659         /*
1660          * In case we didn't handle anything,
1661          * print the register value.
1662          */
1663         if (unlikely(*interrupt_mask == 0 && net_ratelimit()))
1664                 ATH5K_PRINTF("0x%08x\n", data);
1665
1666         return 0;
1667 }
1668
1669 /*
1670  * Set interrupt mask
1671  */
1672 enum ath5k_int ath5k_hw_set_intr(struct ath5k_hw *ah, enum ath5k_int new_mask)
1673 {
1674         enum ath5k_int old_mask, int_mask;
1675
1676         /*
1677          * Disable card interrupts to prevent any race conditions
1678          * (they will be re-enabled afterwards).
1679          */
1680         ath5k_hw_reg_write(ah, AR5K_IER_DISABLE, AR5K_IER);
1681
1682         old_mask = ah->ah_imr;
1683
1684         /*
1685          * Add additional, chipset-dependent interrupt mask flags
1686          * and write them to the IMR (interrupt mask register).
1687          */
1688         int_mask = new_mask & AR5K_INT_COMMON;
1689
1690         if (new_mask & AR5K_INT_RX)
1691                 int_mask |= AR5K_IMR_RXOK | AR5K_IMR_RXERR | AR5K_IMR_RXORN |
1692                         AR5K_IMR_RXDESC;
1693
1694         if (new_mask & AR5K_INT_TX)
1695                 int_mask |= AR5K_IMR_TXOK | AR5K_IMR_TXERR | AR5K_IMR_TXDESC |
1696                         AR5K_IMR_TXURN;
1697
1698         if (ah->ah_version != AR5K_AR5210) {
1699                 if (new_mask & AR5K_INT_FATAL) {
1700                         int_mask |= AR5K_IMR_HIUERR;
1701                         AR5K_REG_ENABLE_BITS(ah, AR5K_SIMR2, AR5K_SIMR2_MCABT |
1702                                         AR5K_SIMR2_SSERR | AR5K_SIMR2_DPERR);
1703                 }
1704         }
1705
1706         ath5k_hw_reg_write(ah, int_mask, AR5K_PIMR);
1707
1708         /* Store new interrupt mask */
1709         ah->ah_imr = new_mask;
1710
1711         /* ..re-enable interrupts */
1712         ath5k_hw_reg_write(ah, AR5K_IER_ENABLE, AR5K_IER);
1713
1714         return old_mask;
1715 }
1716
1717
1718 /*************************\
1719   EEPROM access functions
1720 \*************************/
1721
1722 /*
1723  * Read from eeprom
1724  */
1725 static int ath5k_hw_eeprom_read(struct ath5k_hw *ah, u32 offset, u16 *data)
1726 {
1727         u32 status, timeout;
1728
1729         ATH5K_TRACE(ah->ah_sc);
1730         /*
1731          * Initialize EEPROM access
1732          */
1733         if (ah->ah_version == AR5K_AR5210) {
1734                 AR5K_REG_ENABLE_BITS(ah, AR5K_PCICFG, AR5K_PCICFG_EEAE);
1735                 (void)ath5k_hw_reg_read(ah, AR5K_EEPROM_BASE + (4 * offset));
1736         } else {
1737                 ath5k_hw_reg_write(ah, offset, AR5K_EEPROM_BASE);
1738                 AR5K_REG_ENABLE_BITS(ah, AR5K_EEPROM_CMD,
1739                                 AR5K_EEPROM_CMD_READ);
1740         }
1741
1742         for (timeout = AR5K_TUNE_REGISTER_TIMEOUT; timeout > 0; timeout--) {
1743                 status = ath5k_hw_reg_read(ah, AR5K_EEPROM_STATUS);
1744                 if (status & AR5K_EEPROM_STAT_RDDONE) {
1745                         if (status & AR5K_EEPROM_STAT_RDERR)
1746                                 return -EIO;
1747                         *data = (u16)(ath5k_hw_reg_read(ah, AR5K_EEPROM_DATA) &
1748                                         0xffff);
1749                         return 0;
1750                 }
1751                 udelay(15);
1752         }
1753
1754         return -ETIMEDOUT;
1755 }
1756
1757 /*
1758  * Write to eeprom - currently disabled, use at your own risk
1759  */
1760 #if 0
1761 static int ath5k_hw_eeprom_write(struct ath5k_hw *ah, u32 offset, u16 data)
1762 {
1763
1764         u32 status, timeout;
1765
1766         ATH5K_TRACE(ah->ah_sc);
1767
1768         /*
1769          * Initialize eeprom access
1770          */
1771
1772         if (ah->ah_version == AR5K_AR5210) {
1773                 AR5K_REG_ENABLE_BITS(ah, AR5K_PCICFG, AR5K_PCICFG_EEAE);
1774         } else {
1775                 AR5K_REG_ENABLE_BITS(ah, AR5K_EEPROM_CMD,
1776                                 AR5K_EEPROM_CMD_RESET);
1777         }
1778
1779         /*
1780          * Write data to data register
1781          */
1782
1783         if (ah->ah_version == AR5K_AR5210) {
1784                 ath5k_hw_reg_write(ah, data, AR5K_EEPROM_BASE + (4 * offset));
1785         } else {
1786                 ath5k_hw_reg_write(ah, offset, AR5K_EEPROM_BASE);
1787                 ath5k_hw_reg_write(ah, data, AR5K_EEPROM_DATA);
1788                 AR5K_REG_ENABLE_BITS(ah, AR5K_EEPROM_CMD,
1789                                 AR5K_EEPROM_CMD_WRITE);
1790         }
1791
1792         /*
1793          * Check status
1794          */
1795
1796         for (timeout = AR5K_TUNE_REGISTER_TIMEOUT; timeout > 0; timeout--) {
1797                 status = ath5k_hw_reg_read(ah, AR5K_EEPROM_STATUS);
1798                 if (status & AR5K_EEPROM_STAT_WRDONE) {
1799                         if (status & AR5K_EEPROM_STAT_WRERR)
1800                                 return EIO;
1801                         return 0;
1802                 }
1803                 udelay(15);
1804         }
1805
1806         ATH5K_ERR(ah->ah_sc, "EEPROM Write is disabled!");
1807         return -EIO;
1808 }
1809 #endif
1810
1811 /*
1812  * Translate binary channel representation in EEPROM to frequency
1813  */
1814 static u16 ath5k_eeprom_bin2freq(struct ath5k_hw *ah, u16 bin, unsigned int mode)
1815 {
1816         u16 val;
1817
1818         if (bin == AR5K_EEPROM_CHANNEL_DIS)
1819                 return bin;
1820
1821         if (mode == AR5K_EEPROM_MODE_11A) {
1822                 if (ah->ah_ee_version > AR5K_EEPROM_VERSION_3_2)
1823                         val = (5 * bin) + 4800;
1824                 else
1825                         val = bin > 62 ? (10 * 62) + (5 * (bin - 62)) + 5100 :
1826                                 (bin * 10) + 5100;
1827         } else {
1828                 if (ah->ah_ee_version > AR5K_EEPROM_VERSION_3_2)
1829                         val = bin + 2300;
1830                 else
1831                         val = bin + 2400;
1832         }
1833
1834         return val;
1835 }
1836
1837 /*
1838  * Read antenna infos from eeprom
1839  */
1840 static int ath5k_eeprom_read_ants(struct ath5k_hw *ah, u32 *offset,
1841                 unsigned int mode)
1842 {
1843         struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
1844         u32 o = *offset;
1845         u16 val;
1846         int ret, i = 0;
1847
1848         AR5K_EEPROM_READ(o++, val);
1849         ee->ee_switch_settling[mode]    = (val >> 8) & 0x7f;
1850         ee->ee_ant_tx_rx[mode]          = (val >> 2) & 0x3f;
1851         ee->ee_ant_control[mode][i]     = (val << 4) & 0x3f;
1852
1853         AR5K_EEPROM_READ(o++, val);
1854         ee->ee_ant_control[mode][i++]   |= (val >> 12) & 0xf;
1855         ee->ee_ant_control[mode][i++]   = (val >> 6) & 0x3f;
1856         ee->ee_ant_control[mode][i++]   = val & 0x3f;
1857
1858         AR5K_EEPROM_READ(o++, val);
1859         ee->ee_ant_control[mode][i++]   = (val >> 10) & 0x3f;
1860         ee->ee_ant_control[mode][i++]   = (val >> 4) & 0x3f;
1861         ee->ee_ant_control[mode][i]     = (val << 2) & 0x3f;
1862
1863         AR5K_EEPROM_READ(o++, val);
1864         ee->ee_ant_control[mode][i++]   |= (val >> 14) & 0x3;
1865         ee->ee_ant_control[mode][i++]   = (val >> 8) & 0x3f;
1866         ee->ee_ant_control[mode][i++]   = (val >> 2) & 0x3f;
1867         ee->ee_ant_control[mode][i]     = (val << 4) & 0x3f;
1868
1869         AR5K_EEPROM_READ(o++, val);
1870         ee->ee_ant_control[mode][i++]   |= (val >> 12) & 0xf;
1871         ee->ee_ant_control[mode][i++]   = (val >> 6) & 0x3f;
1872         ee->ee_ant_control[mode][i++]   = val & 0x3f;
1873
1874         /* Get antenna modes */
1875         ah->ah_antenna[mode][0] =
1876             (ee->ee_ant_control[mode][0] << 4) | 0x1;
1877         ah->ah_antenna[mode][AR5K_ANT_FIXED_A] =
1878              ee->ee_ant_control[mode][1]        |
1879             (ee->ee_ant_control[mode][2] << 6)  |
1880             (ee->ee_ant_control[mode][3] << 12) |
1881             (ee->ee_ant_control[mode][4] << 18) |
1882             (ee->ee_ant_control[mode][5] << 24);
1883         ah->ah_antenna[mode][AR5K_ANT_FIXED_B] =
1884              ee->ee_ant_control[mode][6]        |
1885             (ee->ee_ant_control[mode][7] << 6)  |
1886             (ee->ee_ant_control[mode][8] << 12) |
1887             (ee->ee_ant_control[mode][9] << 18) |
1888             (ee->ee_ant_control[mode][10] << 24);
1889
1890         /* return new offset */
1891         *offset = o;
1892
1893         return 0;
1894 }
1895
1896 /*
1897  * Read supported modes from eeprom
1898  */
1899 static int ath5k_eeprom_read_modes(struct ath5k_hw *ah, u32 *offset,
1900                 unsigned int mode)
1901 {
1902         struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
1903         u32 o = *offset;
1904         u16 val;
1905         int ret;
1906
1907         AR5K_EEPROM_READ(o++, val);
1908         ee->ee_tx_end2xlna_enable[mode] = (val >> 8) & 0xff;
1909         ee->ee_thr_62[mode]             = val & 0xff;
1910
1911         if (ah->ah_ee_version <= AR5K_EEPROM_VERSION_3_2)
1912                 ee->ee_thr_62[mode] = mode == AR5K_EEPROM_MODE_11A ? 15 : 28;
1913
1914         AR5K_EEPROM_READ(o++, val);
1915         ee->ee_tx_end2xpa_disable[mode] = (val >> 8) & 0xff;
1916         ee->ee_tx_frm2xpa_enable[mode]  = val & 0xff;
1917
1918         AR5K_EEPROM_READ(o++, val);
1919         ee->ee_pga_desired_size[mode]   = (val >> 8) & 0xff;
1920
1921         if ((val & 0xff) & 0x80)
1922                 ee->ee_noise_floor_thr[mode] = -((((val & 0xff) ^ 0xff)) + 1);
1923         else
1924                 ee->ee_noise_floor_thr[mode] = val & 0xff;
1925
1926         if (ah->ah_ee_version <= AR5K_EEPROM_VERSION_3_2)
1927                 ee->ee_noise_floor_thr[mode] =
1928                     mode == AR5K_EEPROM_MODE_11A ? -54 : -1;
1929
1930         AR5K_EEPROM_READ(o++, val);
1931         ee->ee_xlna_gain[mode]          = (val >> 5) & 0xff;
1932         ee->ee_x_gain[mode]             = (val >> 1) & 0xf;
1933         ee->ee_xpd[mode]                = val & 0x1;
1934
1935         if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_0)
1936                 ee->ee_fixed_bias[mode] = (val >> 13) & 0x1;
1937
1938         if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_3_3) {
1939                 AR5K_EEPROM_READ(o++, val);
1940                 ee->ee_false_detect[mode] = (val >> 6) & 0x7f;
1941
1942                 if (mode == AR5K_EEPROM_MODE_11A)
1943                         ee->ee_xr_power[mode] = val & 0x3f;
1944                 else {
1945                         ee->ee_ob[mode][0] = val & 0x7;
1946                         ee->ee_db[mode][0] = (val >> 3) & 0x7;
1947                 }
1948         }
1949
1950         if (ah->ah_ee_version < AR5K_EEPROM_VERSION_3_4) {
1951                 ee->ee_i_gain[mode] = AR5K_EEPROM_I_GAIN;
1952                 ee->ee_cck_ofdm_power_delta = AR5K_EEPROM_CCK_OFDM_DELTA;
1953         } else {
1954                 ee->ee_i_gain[mode] = (val >> 13) & 0x7;
1955
1956                 AR5K_EEPROM_READ(o++, val);
1957                 ee->ee_i_gain[mode] |= (val << 3) & 0x38;
1958
1959                 if (mode == AR5K_EEPROM_MODE_11G)
1960                         ee->ee_cck_ofdm_power_delta = (val >> 3) & 0xff;
1961         }
1962
1963         if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_0 &&
1964                         mode == AR5K_EEPROM_MODE_11A) {
1965                 ee->ee_i_cal[mode] = (val >> 8) & 0x3f;
1966                 ee->ee_q_cal[mode] = (val >> 3) & 0x1f;
1967         }
1968
1969         if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_6 &&
1970             mode == AR5K_EEPROM_MODE_11G)
1971                 ee->ee_scaled_cck_delta = (val >> 11) & 0x1f;
1972
1973         /* return new offset */
1974         *offset = o;
1975
1976         return 0;
1977 }
1978
1979 /*
1980  * Initialize eeprom & capabilities structs
1981  */
1982 static int ath5k_eeprom_init(struct ath5k_hw *ah)
1983 {
1984         struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
1985         unsigned int mode, i;
1986         int ret;
1987         u32 offset;
1988         u16 val;
1989
1990         /* Initial TX thermal adjustment values */
1991         ee->ee_tx_clip = 4;
1992         ee->ee_pwd_84 = ee->ee_pwd_90 = 1;
1993         ee->ee_gain_select = 1;
1994
1995         /*
1996          * Read values from EEPROM and store them in the capability structure
1997          */
1998         AR5K_EEPROM_READ_HDR(AR5K_EEPROM_MAGIC, ee_magic);
1999         AR5K_EEPROM_READ_HDR(AR5K_EEPROM_PROTECT, ee_protect);
2000         AR5K_EEPROM_READ_HDR(AR5K_EEPROM_REG_DOMAIN, ee_regdomain);
2001         AR5K_EEPROM_READ_HDR(AR5K_EEPROM_VERSION, ee_version);
2002         AR5K_EEPROM_READ_HDR(AR5K_EEPROM_HDR, ee_header);
2003
2004         /* Return if we have an old EEPROM */
2005         if (ah->ah_ee_version < AR5K_EEPROM_VERSION_3_0)
2006                 return 0;
2007
2008 #ifdef notyet
2009         /*
2010          * Validate the checksum of the EEPROM date. There are some
2011          * devices with invalid EEPROMs.
2012          */
2013         for (cksum = 0, offset = 0; offset < AR5K_EEPROM_INFO_MAX; offset++) {
2014                 AR5K_EEPROM_READ(AR5K_EEPROM_INFO(offset), val);
2015                 cksum ^= val;
2016         }
2017         if (cksum != AR5K_EEPROM_INFO_CKSUM) {
2018                 ATH5K_ERR(ah->ah_sc, "Invalid EEPROM checksum 0x%04x\n", cksum);
2019                 return -EIO;
2020         }
2021 #endif
2022
2023         AR5K_EEPROM_READ_HDR(AR5K_EEPROM_ANT_GAIN(ah->ah_ee_version),
2024             ee_ant_gain);
2025
2026         if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_0) {
2027                 AR5K_EEPROM_READ_HDR(AR5K_EEPROM_MISC0, ee_misc0);
2028                 AR5K_EEPROM_READ_HDR(AR5K_EEPROM_MISC1, ee_misc1);
2029         }
2030
2031         if (ah->ah_ee_version < AR5K_EEPROM_VERSION_3_3) {
2032                 AR5K_EEPROM_READ(AR5K_EEPROM_OBDB0_2GHZ, val);
2033                 ee->ee_ob[AR5K_EEPROM_MODE_11B][0] = val & 0x7;
2034                 ee->ee_db[AR5K_EEPROM_MODE_11B][0] = (val >> 3) & 0x7;
2035
2036                 AR5K_EEPROM_READ(AR5K_EEPROM_OBDB1_2GHZ, val);
2037                 ee->ee_ob[AR5K_EEPROM_MODE_11G][0] = val & 0x7;
2038                 ee->ee_db[AR5K_EEPROM_MODE_11G][0] = (val >> 3) & 0x7;
2039         }
2040
2041         /*
2042          * Get conformance test limit values
2043          */
2044         offset = AR5K_EEPROM_CTL(ah->ah_ee_version);
2045         ee->ee_ctls = AR5K_EEPROM_N_CTLS(ah->ah_ee_version);
2046
2047         for (i = 0; i < ee->ee_ctls; i++) {
2048                 AR5K_EEPROM_READ(offset++, val);
2049                 ee->ee_ctl[i] = (val >> 8) & 0xff;
2050                 ee->ee_ctl[i + 1] = val & 0xff;
2051         }
2052
2053         /*
2054          * Get values for 802.11a (5GHz)
2055          */
2056         mode = AR5K_EEPROM_MODE_11A;
2057
2058         ee->ee_turbo_max_power[mode] =
2059                         AR5K_EEPROM_HDR_T_5GHZ_DBM(ee->ee_header);
2060
2061         offset = AR5K_EEPROM_MODES_11A(ah->ah_ee_version);
2062
2063         ret = ath5k_eeprom_read_ants(ah, &offset, mode);
2064         if (ret)
2065                 return ret;
2066
2067         AR5K_EEPROM_READ(offset++, val);
2068         ee->ee_adc_desired_size[mode]   = (s8)((val >> 8) & 0xff);
2069         ee->ee_ob[mode][3]              = (val >> 5) & 0x7;
2070         ee->ee_db[mode][3]              = (val >> 2) & 0x7;
2071         ee->ee_ob[mode][2]              = (val << 1) & 0x7;
2072
2073         AR5K_EEPROM_READ(offset++, val);
2074         ee->ee_ob[mode][2]              |= (val >> 15) & 0x1;
2075         ee->ee_db[mode][2]              = (val >> 12) & 0x7;
2076         ee->ee_ob[mode][1]              = (val >> 9) & 0x7;
2077         ee->ee_db[mode][1]              = (val >> 6) & 0x7;
2078         ee->ee_ob[mode][0]              = (val >> 3) & 0x7;
2079         ee->ee_db[mode][0]              = val & 0x7;
2080
2081         ret = ath5k_eeprom_read_modes(ah, &offset, mode);
2082         if (ret)
2083                 return ret;
2084
2085         if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_1) {
2086                 AR5K_EEPROM_READ(offset++, val);
2087                 ee->ee_margin_tx_rx[mode] = val & 0x3f;
2088         }
2089
2090         /*
2091          * Get values for 802.11b (2.4GHz)
2092          */
2093         mode = AR5K_EEPROM_MODE_11B;
2094         offset = AR5K_EEPROM_MODES_11B(ah->ah_ee_version);
2095
2096         ret = ath5k_eeprom_read_ants(ah, &offset, mode);
2097         if (ret)
2098                 return ret;
2099
2100         AR5K_EEPROM_READ(offset++, val);
2101         ee->ee_adc_desired_size[mode]   = (s8)((val >> 8) & 0xff);
2102         ee->ee_ob[mode][1]              = (val >> 4) & 0x7;
2103         ee->ee_db[mode][1]              = val & 0x7;
2104
2105         ret = ath5k_eeprom_read_modes(ah, &offset, mode);
2106         if (ret)
2107                 return ret;
2108
2109         if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_0) {
2110                 AR5K_EEPROM_READ(offset++, val);
2111                 ee->ee_cal_pier[mode][0] =
2112                         ath5k_eeprom_bin2freq(ah, val & 0xff, mode);
2113                 ee->ee_cal_pier[mode][1] =
2114                         ath5k_eeprom_bin2freq(ah, (val >> 8) & 0xff, mode);
2115
2116                 AR5K_EEPROM_READ(offset++, val);
2117                 ee->ee_cal_pier[mode][2] =
2118                         ath5k_eeprom_bin2freq(ah, val & 0xff, mode);
2119         }
2120
2121         if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_1)
2122                 ee->ee_margin_tx_rx[mode] = (val >> 8) & 0x3f;
2123
2124         /*
2125          * Get values for 802.11g (2.4GHz)
2126          */
2127         mode = AR5K_EEPROM_MODE_11G;
2128         offset = AR5K_EEPROM_MODES_11G(ah->ah_ee_version);
2129
2130         ret = ath5k_eeprom_read_ants(ah, &offset, mode);
2131         if (ret)
2132                 return ret;
2133
2134         AR5K_EEPROM_READ(offset++, val);
2135         ee->ee_adc_desired_size[mode]   = (s8)((val >> 8) & 0xff);
2136         ee->ee_ob[mode][1]              = (val >> 4) & 0x7;
2137         ee->ee_db[mode][1]              = val & 0x7;
2138
2139         ret = ath5k_eeprom_read_modes(ah, &offset, mode);
2140         if (ret)
2141                 return ret;
2142
2143         if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_0) {
2144                 AR5K_EEPROM_READ(offset++, val);
2145                 ee->ee_cal_pier[mode][0] =
2146                         ath5k_eeprom_bin2freq(ah, val & 0xff, mode);
2147                 ee->ee_cal_pier[mode][1] =
2148                         ath5k_eeprom_bin2freq(ah, (val >> 8) & 0xff, mode);
2149
2150                 AR5K_EEPROM_READ(offset++, val);
2151                 ee->ee_turbo_max_power[mode] = val & 0x7f;
2152                 ee->ee_xr_power[mode] = (val >> 7) & 0x3f;
2153
2154                 AR5K_EEPROM_READ(offset++, val);
2155                 ee->ee_cal_pier[mode][2] =
2156                         ath5k_eeprom_bin2freq(ah, val & 0xff, mode);
2157
2158                 if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_1)
2159                         ee->ee_margin_tx_rx[mode] = (val >> 8) & 0x3f;
2160
2161                 AR5K_EEPROM_READ(offset++, val);
2162                 ee->ee_i_cal[mode] = (val >> 8) & 0x3f;
2163                 ee->ee_q_cal[mode] = (val >> 3) & 0x1f;
2164
2165                 if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_2) {
2166                         AR5K_EEPROM_READ(offset++, val);
2167                         ee->ee_cck_ofdm_gain_delta = val & 0xff;
2168                 }
2169         }
2170
2171         /*
2172          * Read 5GHz EEPROM channels
2173          */
2174
2175         return 0;
2176 }
2177
2178 /*
2179  * Read the MAC address from eeprom
2180  */
2181 static int ath5k_eeprom_read_mac(struct ath5k_hw *ah, u8 *mac)
2182 {
2183         u8 mac_d[ETH_ALEN];
2184         u32 total, offset;
2185         u16 data;
2186         int octet, ret;
2187
2188         memset(mac, 0, ETH_ALEN);
2189         memset(mac_d, 0, ETH_ALEN);
2190
2191         ret = ath5k_hw_eeprom_read(ah, 0x20, &data);
2192         if (ret)
2193                 return ret;
2194
2195         for (offset = 0x1f, octet = 0, total = 0; offset >= 0x1d; offset--) {
2196                 ret = ath5k_hw_eeprom_read(ah, offset, &data);
2197                 if (ret)
2198                         return ret;
2199
2200                 total += data;
2201                 mac_d[octet + 1] = data & 0xff;
2202                 mac_d[octet] = data >> 8;
2203                 octet += 2;
2204         }
2205
2206         memcpy(mac, mac_d, ETH_ALEN);
2207
2208         if (!total || total == 3 * 0xffff)
2209                 return -EINVAL;
2210
2211         return 0;
2212 }
2213
2214 /*
2215  * Fill the capabilities struct
2216  */
2217 static int ath5k_hw_get_capabilities(struct ath5k_hw *ah)
2218 {
2219         u16 ee_header;
2220
2221         ATH5K_TRACE(ah->ah_sc);
2222         /* Capabilities stored in the EEPROM */
2223         ee_header = ah->ah_capabilities.cap_eeprom.ee_header;
2224
2225         if (ah->ah_version == AR5K_AR5210) {
2226                 /*
2227                  * Set radio capabilities
2228                  * (The AR5110 only supports the middle 5GHz band)
2229                  */
2230                 ah->ah_capabilities.cap_range.range_5ghz_min = 5120;
2231                 ah->ah_capabilities.cap_range.range_5ghz_max = 5430;
2232                 ah->ah_capabilities.cap_range.range_2ghz_min = 0;
2233                 ah->ah_capabilities.cap_range.range_2ghz_max = 0;
2234
2235                 /* Set supported modes */
2236                 __set_bit(AR5K_MODE_11A, ah->ah_capabilities.cap_mode);
2237                 __set_bit(AR5K_MODE_11A_TURBO, ah->ah_capabilities.cap_mode);
2238         } else {
2239                 /*
2240                  * XXX The tranceiver supports frequencies from 4920 to 6100GHz
2241                  * XXX and from 2312 to 2732GHz. There are problems with the
2242                  * XXX current ieee80211 implementation because the IEEE
2243                  * XXX channel mapping does not support negative channel
2244                  * XXX numbers (2312MHz is channel -19). Of course, this
2245                  * XXX doesn't matter because these channels are out of range
2246                  * XXX but some regulation domains like MKK (Japan) will
2247                  * XXX support frequencies somewhere around 4.8GHz.
2248                  */
2249
2250                 /*
2251                  * Set radio capabilities
2252                  */
2253
2254                 if (AR5K_EEPROM_HDR_11A(ee_header)) {
2255                         ah->ah_capabilities.cap_range.range_5ghz_min = 5005; /* 4920 */
2256                         ah->ah_capabilities.cap_range.range_5ghz_max = 6100;
2257
2258                         /* Set supported modes */
2259                         __set_bit(AR5K_MODE_11A,
2260                                         ah->ah_capabilities.cap_mode);
2261                         __set_bit(AR5K_MODE_11A_TURBO,
2262                                         ah->ah_capabilities.cap_mode);
2263                         if (ah->ah_version == AR5K_AR5212)
2264                                 __set_bit(AR5K_MODE_11G_TURBO,
2265                                                 ah->ah_capabilities.cap_mode);
2266                 }
2267
2268                 /* Enable  802.11b if a 2GHz capable radio (2111/5112) is
2269                  * connected */
2270                 if (AR5K_EEPROM_HDR_11B(ee_header) ||
2271                                 AR5K_EEPROM_HDR_11G(ee_header)) {
2272                         ah->ah_capabilities.cap_range.range_2ghz_min = 2412; /* 2312 */
2273                         ah->ah_capabilities.cap_range.range_2ghz_max = 2732;
2274
2275                         if (AR5K_EEPROM_HDR_11B(ee_header))
2276                                 __set_bit(AR5K_MODE_11B,
2277                                                 ah->ah_capabilities.cap_mode);
2278
2279                         if (AR5K_EEPROM_HDR_11G(ee_header))
2280                                 __set_bit(AR5K_MODE_11G,
2281                                                 ah->ah_capabilities.cap_mode);
2282                 }
2283         }
2284
2285         /* GPIO */
2286         ah->ah_gpio_npins = AR5K_NUM_GPIO;
2287
2288         /* Set number of supported TX queues */
2289         if (ah->ah_version == AR5K_AR5210)
2290                 ah->ah_capabilities.cap_queues.q_tx_num =
2291                         AR5K_NUM_TX_QUEUES_NOQCU;
2292         else
2293                 ah->ah_capabilities.cap_queues.q_tx_num = AR5K_NUM_TX_QUEUES;
2294
2295         return 0;
2296 }
2297
2298 /*********************************\
2299   Protocol Control Unit Functions
2300 \*********************************/
2301
2302 /*
2303  * Set Operation mode
2304  */
2305 int ath5k_hw_set_opmode(struct ath5k_hw *ah)
2306 {
2307         u32 pcu_reg, beacon_reg, low_id, high_id;
2308
2309         pcu_reg = 0;
2310         beacon_reg = 0;
2311
2312         ATH5K_TRACE(ah->ah_sc);
2313
2314         switch (ah->ah_op_mode) {
2315         case IEEE80211_IF_TYPE_IBSS:
2316                 pcu_reg |= AR5K_STA_ID1_ADHOC | AR5K_STA_ID1_DESC_ANTENNA |
2317                         (ah->ah_version == AR5K_AR5210 ?
2318                                 AR5K_STA_ID1_NO_PSPOLL : 0);
2319                 beacon_reg |= AR5K_BCR_ADHOC;
2320                 break;
2321
2322         case IEEE80211_IF_TYPE_AP:
2323                 pcu_reg |= AR5K_STA_ID1_AP | AR5K_STA_ID1_RTS_DEF_ANTENNA |
2324                         (ah->ah_version == AR5K_AR5210 ?
2325                                 AR5K_STA_ID1_NO_PSPOLL : 0);
2326                 beacon_reg |= AR5K_BCR_AP;
2327                 break;
2328
2329         case IEEE80211_IF_TYPE_STA:
2330                 pcu_reg |= AR5K_STA_ID1_DEFAULT_ANTENNA |
2331                         (ah->ah_version == AR5K_AR5210 ?
2332                                 AR5K_STA_ID1_PWR_SV : 0);
2333         case IEEE80211_IF_TYPE_MNTR:
2334                 pcu_reg |= AR5K_STA_ID1_DEFAULT_ANTENNA |
2335                         (ah->ah_version == AR5K_AR5210 ?
2336                                 AR5K_STA_ID1_NO_PSPOLL : 0);
2337                 break;
2338
2339         default:
2340                 return -EINVAL;
2341         }
2342
2343         /*
2344          * Set PCU registers
2345          */
2346         low_id = AR5K_LOW_ID(ah->ah_sta_id);
2347         high_id = AR5K_HIGH_ID(ah->ah_sta_id);
2348         ath5k_hw_reg_write(ah, low_id, AR5K_STA_ID0);
2349         ath5k_hw_reg_write(ah, pcu_reg | high_id, AR5K_STA_ID1);
2350
2351         /*
2352          * Set Beacon Control Register on 5210
2353          */
2354         if (ah->ah_version == AR5K_AR5210)
2355                 ath5k_hw_reg_write(ah, beacon_reg, AR5K_BCR);
2356
2357         return 0;
2358 }
2359
2360 /*
2361  * BSSID Functions
2362  */
2363
2364 /*
2365  * Get station id
2366  */
2367 void ath5k_hw_get_lladdr(struct ath5k_hw *ah, u8 *mac)
2368 {
2369         ATH5K_TRACE(ah->ah_sc);
2370         memcpy(mac, ah->ah_sta_id, ETH_ALEN);
2371 }
2372
2373 /*
2374  * Set station id
2375  */
2376 int ath5k_hw_set_lladdr(struct ath5k_hw *ah, const u8 *mac)
2377 {
2378         u32 low_id, high_id;
2379
2380         ATH5K_TRACE(ah->ah_sc);
2381         /* Set new station ID */
2382         memcpy(ah->ah_sta_id, mac, ETH_ALEN);
2383
2384         low_id = AR5K_LOW_ID(mac);
2385         high_id = AR5K_HIGH_ID(mac);
2386
2387         ath5k_hw_reg_write(ah, low_id, AR5K_STA_ID0);
2388         ath5k_hw_reg_write(ah, high_id, AR5K_STA_ID1);
2389
2390         return 0;
2391 }
2392
2393 /*
2394  * Set BSSID
2395  */
2396 void ath5k_hw_set_associd(struct ath5k_hw *ah, const u8 *bssid, u16 assoc_id)
2397 {
2398         u32 low_id, high_id;
2399         u16 tim_offset = 0;
2400
2401         /*
2402          * Set simple BSSID mask on 5212
2403          */
2404         if (ah->ah_version == AR5K_AR5212) {
2405                 ath5k_hw_reg_write(ah, 0xffffffff, AR5K_BSS_IDM0);
2406                 ath5k_hw_reg_write(ah, 0xffffffff, AR5K_BSS_IDM1);
2407         }
2408
2409         /*
2410          * Set BSSID which triggers the "SME Join" operation
2411          */
2412         low_id = AR5K_LOW_ID(bssid);
2413         high_id = AR5K_HIGH_ID(bssid);
2414         ath5k_hw_reg_write(ah, low_id, AR5K_BSS_ID0);
2415         ath5k_hw_reg_write(ah, high_id | ((assoc_id & 0x3fff) <<
2416                                 AR5K_BSS_ID1_AID_S), AR5K_BSS_ID1);
2417
2418         if (assoc_id == 0) {
2419                 ath5k_hw_disable_pspoll(ah);
2420                 return;
2421         }
2422
2423         AR5K_REG_WRITE_BITS(ah, AR5K_BEACON, AR5K_BEACON_TIM,
2424                         tim_offset ? tim_offset + 4 : 0);
2425
2426         ath5k_hw_enable_pspoll(ah, NULL, 0);
2427 }
2428 /**
2429  * ath5k_hw_set_bssid_mask - set common bits we should listen to
2430  *
2431  * The bssid_mask is a utility used by AR5212 hardware to inform the hardware
2432  * which bits of the interface's MAC address should be looked at when trying
2433  * to decide which packets to ACK. In station mode every bit matters. In AP
2434  * mode with a single BSS every bit matters as well. In AP mode with
2435  * multiple BSSes not every bit matters.
2436  *
2437  * @ah: the &struct ath5k_hw
2438  * @mask: the bssid_mask, a u8 array of size ETH_ALEN
2439  *
2440  * Note that this is a simple filter and *does* not filter out all
2441  * relevant frames. Some non-relevant frames will get through, probability
2442  * jocks are welcomed to compute.
2443  *
2444  * When handling multiple BSSes (or VAPs) you can get the BSSID mask by
2445  * computing the set of:
2446  *
2447  *     ~ ( MAC XOR BSSID )
2448  *
2449  * When you do this you are essentially computing the common bits. Later it
2450  * is assumed the harware will "and" (&) the BSSID mask with the MAC address
2451  * to obtain the relevant bits which should match on the destination frame.
2452  *
2453  * Simple example: on your card you have have two BSSes you have created with
2454  * BSSID-01 and BSSID-02. Lets assume BSSID-01 will not use the MAC address.
2455  * There is another BSSID-03 but you are not part of it. For simplicity's sake,
2456  * assuming only 4 bits for a mac address and for BSSIDs you can then have:
2457  *
2458  *                  \
2459  * MAC:                0001 |
2460  * BSSID-01:   0100 | --> Belongs to us
2461  * BSSID-02:   1001 |
2462  *                  /
2463  * -------------------
2464  * BSSID-03:   0110  | --> External
2465  * -------------------
2466  *
2467  * Our bssid_mask would then be:
2468  *
2469  *             On loop iteration for BSSID-01:
2470  *             ~(0001 ^ 0100)  -> ~(0101)
2471  *                             ->   1010
2472  *             bssid_mask      =    1010
2473  *
2474  *             On loop iteration for BSSID-02:
2475  *             bssid_mask &= ~(0001   ^   1001)
2476  *             bssid_mask =   (1010)  & ~(0001 ^ 1001)
2477  *             bssid_mask =   (1010)  & ~(1001)
2478  *             bssid_mask =   (1010)  &  (0110)
2479  *             bssid_mask =   0010
2480  *
2481  * A bssid_mask of 0010 means "only pay attention to the second least
2482  * significant bit". This is because its the only bit common
2483  * amongst the MAC and all BSSIDs we support. To findout what the real
2484  * common bit is we can simply "&" the bssid_mask now with any BSSID we have
2485  * or our MAC address (we assume the hardware uses the MAC address).
2486  *
2487  * Now, suppose there's an incoming frame for BSSID-03:
2488  *
2489  * IFRAME-01:  0110
2490  *
2491  * An easy eye-inspeciton of this already should tell you that this frame
2492  * will not pass our check. This is beacuse the bssid_mask tells the
2493  * hardware to only look at the second least significant bit and the
2494  * common bit amongst the MAC and BSSIDs is 0, this frame has the 2nd LSB
2495  * as 1, which does not match 0.
2496  *
2497  * So with IFRAME-01 we *assume* the hardware will do:
2498  *
2499  *     allow = (IFRAME-01 & bssid_mask) == (bssid_mask & MAC) ? 1 : 0;
2500  *  --> allow = (0110 & 0010) == (0010 & 0001) ? 1 : 0;
2501  *  --> allow = (0010) == 0000 ? 1 : 0;
2502  *  --> allow = 0
2503  *
2504  *  Lets now test a frame that should work:
2505  *
2506  * IFRAME-02:  0001 (we should allow)
2507  *
2508  *     allow = (0001 & 1010) == 1010
2509  *
2510  *     allow = (IFRAME-02 & bssid_mask) == (bssid_mask & MAC) ? 1 : 0;
2511  *  --> allow = (0001 & 0010) ==  (0010 & 0001) ? 1 :0;
2512  *  --> allow = (0010) == (0010)
2513  *  --> allow = 1
2514  *
2515  * Other examples:
2516  *
2517  * IFRAME-03:  0100 --> allowed
2518  * IFRAME-04:  1001 --> allowed
2519  * IFRAME-05:  1101 --> allowed but its not for us!!!
2520  *
2521  */
2522 int ath5k_hw_set_bssid_mask(struct ath5k_hw *ah, const u8 *mask)
2523 {
2524         u32 low_id, high_id;
2525         ATH5K_TRACE(ah->ah_sc);
2526
2527         if (ah->ah_version == AR5K_AR5212) {
2528                 low_id = AR5K_LOW_ID(mask);
2529                 high_id = AR5K_HIGH_ID(mask);
2530
2531                 ath5k_hw_reg_write(ah, low_id, AR5K_BSS_IDM0);
2532                 ath5k_hw_reg_write(ah, high_id, AR5K_BSS_IDM1);
2533
2534                 return 0;
2535         }
2536
2537         return -EIO;
2538 }
2539
2540 /*
2541  * Receive start/stop functions
2542  */
2543
2544 /*
2545  * Start receive on PCU
2546  */
2547 void ath5k_hw_start_rx_pcu(struct ath5k_hw *ah)
2548 {
2549         ATH5K_TRACE(ah->ah_sc);
2550         AR5K_REG_DISABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
2551
2552         /* TODO: ANI Support */
2553 }
2554
2555 /*
2556  * Stop receive on PCU
2557  */
2558 void ath5k_hw_stop_pcu_recv(struct ath5k_hw *ah)
2559 {
2560         ATH5K_TRACE(ah->ah_sc);
2561         AR5K_REG_ENABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
2562
2563         /* TODO: ANI Support */
2564 }
2565
2566 /*
2567  * RX Filter functions
2568  */
2569
2570 /*
2571  * Set multicast filter
2572  */
2573 void ath5k_hw_set_mcast_filter(struct ath5k_hw *ah, u32 filter0, u32 filter1)
2574 {
2575         ATH5K_TRACE(ah->ah_sc);
2576         /* Set the multicat filter */
2577         ath5k_hw_reg_write(ah, filter0, AR5K_MCAST_FILTER0);
2578         ath5k_hw_reg_write(ah, filter1, AR5K_MCAST_FILTER1);
2579 }
2580
2581 /*
2582  * Set multicast filter by index
2583  */
2584 int ath5k_hw_set_mcast_filterindex(struct ath5k_hw *ah, u32 index)
2585 {
2586
2587         ATH5K_TRACE(ah->ah_sc);
2588         if (index >= 64)
2589                 return -EINVAL;
2590         else if (index >= 32)
2591                 AR5K_REG_ENABLE_BITS(ah, AR5K_MCAST_FILTER1,
2592                                 (1 << (index - 32)));
2593         else
2594                 AR5K_REG_ENABLE_BITS(ah, AR5K_MCAST_FILTER0, (1 << index));
2595
2596         return 0;
2597 }
2598
2599 /*
2600  * Clear Multicast filter by index
2601  */
2602 int ath5k_hw_clear_mcast_filter_idx(struct ath5k_hw *ah, u32 index)
2603 {
2604
2605         ATH5K_TRACE(ah->ah_sc);
2606         if (index >= 64)
2607                 return -EINVAL;
2608         else if (index >= 32)
2609                 AR5K_REG_DISABLE_BITS(ah, AR5K_MCAST_FILTER1,
2610                                 (1 << (index - 32)));
2611         else
2612                 AR5K_REG_DISABLE_BITS(ah, AR5K_MCAST_FILTER0, (1 << index));
2613
2614         return 0;
2615 }
2616
2617 /*
2618  * Get current rx filter
2619  */
2620 u32 ath5k_hw_get_rx_filter(struct ath5k_hw *ah)
2621 {
2622         u32 data, filter = 0;
2623
2624         ATH5K_TRACE(ah->ah_sc);
2625         filter = ath5k_hw_reg_read(ah, AR5K_RX_FILTER);
2626
2627         /*Radar detection for 5212*/
2628         if (ah->ah_version == AR5K_AR5212) {
2629                 data = ath5k_hw_reg_read(ah, AR5K_PHY_ERR_FIL);
2630
2631                 if (data & AR5K_PHY_ERR_FIL_RADAR)
2632                         filter |= AR5K_RX_FILTER_RADARERR;
2633                 if (data & (AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK))
2634                         filter |= AR5K_RX_FILTER_PHYERR;
2635         }
2636
2637         return filter;
2638 }
2639
2640 /*
2641  * Set rx filter
2642  */
2643 void ath5k_hw_set_rx_filter(struct ath5k_hw *ah, u32 filter)
2644 {
2645         u32 data = 0;
2646
2647         ATH5K_TRACE(ah->ah_sc);
2648
2649         /* Set PHY error filter register on 5212*/
2650         if (ah->ah_version == AR5K_AR5212) {
2651                 if (filter & AR5K_RX_FILTER_RADARERR)
2652                         data |= AR5K_PHY_ERR_FIL_RADAR;
2653                 if (filter & AR5K_RX_FILTER_PHYERR)
2654                         data |= AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK;
2655         }
2656
2657         /*
2658          * The AR5210 uses promiscous mode to detect radar activity
2659          */
2660         if (ah->ah_version == AR5K_AR5210 &&
2661                         (filter & AR5K_RX_FILTER_RADARERR)) {
2662                 filter &= ~AR5K_RX_FILTER_RADARERR;
2663                 filter |= AR5K_RX_FILTER_PROM;
2664         }
2665
2666         /*Zero length DMA*/
2667         if (data)
2668                 AR5K_REG_ENABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);
2669         else
2670                 AR5K_REG_DISABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);
2671
2672         /*Write RX Filter register*/
2673         ath5k_hw_reg_write(ah, filter & 0xff, AR5K_RX_FILTER);
2674
2675         /*Write PHY error filter register on 5212*/
2676         if (ah->ah_version == AR5K_AR5212)
2677                 ath5k_hw_reg_write(ah, data, AR5K_PHY_ERR_FIL);
2678
2679 }
2680
2681 /*
2682  * Beacon related functions
2683  */
2684
2685 /*
2686  * Get a 32bit TSF
2687  */
2688 u32 ath5k_hw_get_tsf32(struct ath5k_hw *ah)
2689 {
2690         ATH5K_TRACE(ah->ah_sc);
2691         return ath5k_hw_reg_read(ah, AR5K_TSF_L32);
2692 }
2693
2694 /*
2695  * Get the full 64bit TSF
2696  */
2697 u64 ath5k_hw_get_tsf64(struct ath5k_hw *ah)
2698 {
2699         u64 tsf = ath5k_hw_reg_read(ah, AR5K_TSF_U32);
2700         ATH5K_TRACE(ah->ah_sc);
2701
2702         return ath5k_hw_reg_read(ah, AR5K_TSF_L32) | (tsf << 32);
2703 }
2704
2705 /*
2706  * Force a TSF reset
2707  */
2708 void ath5k_hw_reset_tsf(struct ath5k_hw *ah)
2709 {
2710         ATH5K_TRACE(ah->ah_sc);
2711         AR5K_REG_ENABLE_BITS(ah, AR5K_BEACON, AR5K_BEACON_RESET_TSF);
2712 }
2713
2714 /*
2715  * Initialize beacon timers
2716  */
2717 void ath5k_hw_init_beacon(struct ath5k_hw *ah, u32 next_beacon, u32 interval)
2718 {
2719         u32 timer1, timer2, timer3;
2720
2721         ATH5K_TRACE(ah->ah_sc);
2722         /*
2723          * Set the additional timers by mode
2724          */
2725         switch (ah->ah_op_mode) {
2726         case IEEE80211_IF_TYPE_STA:
2727                 if (ah->ah_version == AR5K_AR5210) {
2728                         timer1 = 0xffffffff;
2729                         timer2 = 0xffffffff;
2730                 } else {
2731                         timer1 = 0x0000ffff;
2732                         timer2 = 0x0007ffff;
2733                 }
2734                 break;
2735
2736         default:
2737                 timer1 = (next_beacon - AR5K_TUNE_DMA_BEACON_RESP) << 3;
2738                 timer2 = (next_beacon - AR5K_TUNE_SW_BEACON_RESP) << 3;
2739         }
2740
2741         timer3 = next_beacon + (ah->ah_atim_window ? ah->ah_atim_window : 1);
2742
2743         /*
2744          * Set the beacon register and enable all timers.
2745          * (next beacon, DMA beacon, software beacon, ATIM window time)
2746          */
2747         ath5k_hw_reg_write(ah, next_beacon, AR5K_TIMER0);
2748         ath5k_hw_reg_write(ah, timer1, AR5K_TIMER1);
2749         ath5k_hw_reg_write(ah, timer2, AR5K_TIMER2);
2750         ath5k_hw_reg_write(ah, timer3, AR5K_TIMER3);
2751
2752         ath5k_hw_reg_write(ah, interval & (AR5K_BEACON_PERIOD |
2753                         AR5K_BEACON_RESET_TSF | AR5K_BEACON_ENABLE),
2754                 AR5K_BEACON);
2755 }
2756
2757 #if 0
2758 /*
2759  * Set beacon timers
2760  */
2761 int ath5k_hw_set_beacon_timers(struct ath5k_hw *ah,
2762                 const struct ath5k_beacon_state *state)
2763 {
2764         u32 cfp_period, next_cfp, dtim, interval, next_beacon;
2765
2766         /*
2767          * TODO: should be changed through *state
2768          * review struct ath5k_beacon_state struct
2769          *
2770          * XXX: These are used for cfp period bellow, are they
2771          * ok ? Is it O.K. for tsf here to be 0 or should we use
2772          * get_tsf ?
2773          */
2774         u32 dtim_count = 0; /* XXX */
2775         u32 cfp_count = 0; /* XXX */
2776         u32 tsf = 0; /* XXX */
2777
2778         ATH5K_TRACE(ah->ah_sc);
2779         /* Return on an invalid beacon state */
2780         if (state->bs_interval < 1)
2781                 return -EINVAL;
2782
2783         interval = state->bs_interval;
2784         dtim = state->bs_dtim_period;
2785
2786         /*
2787          * PCF support?
2788          */
2789         if (state->bs_cfp_period > 0) {
2790                 /*
2791                  * Enable PCF mode and set the CFP
2792                  * (Contention Free Period) and timer registers
2793                  */
2794                 cfp_period = state->bs_cfp_period * state->bs_dtim_period *
2795                         state->bs_interval;
2796                 next_cfp = (cfp_count * state->bs_dtim_period + dtim_count) *
2797                         state->bs_interval;
2798
2799                 AR5K_REG_ENABLE_BITS(ah, AR5K_STA_ID1,
2800                                 AR5K_STA_ID1_DEFAULT_ANTENNA |
2801                                 AR5K_STA_ID1_PCF);
2802                 ath5k_hw_reg_write(ah, cfp_period, AR5K_CFP_PERIOD);
2803                 ath5k_hw_reg_write(ah, state->bs_cfp_max_duration,
2804                                 AR5K_CFP_DUR);
2805                 ath5k_hw_reg_write(ah, (tsf + (next_cfp == 0 ? cfp_period :
2806                                                 next_cfp)) << 3, AR5K_TIMER2);
2807         } else {
2808                 /* Disable PCF mode */
2809                 AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1,
2810                                 AR5K_STA_ID1_DEFAULT_ANTENNA |
2811                                 AR5K_STA_ID1_PCF);
2812         }
2813
2814         /*
2815          * Enable the beacon timer register
2816          */
2817         ath5k_hw_reg_write(ah, state->bs_next_beacon, AR5K_TIMER0);
2818
2819         /*
2820          * Start the beacon timers
2821          */
2822         ath5k_hw_reg_write(ah, (ath5k_hw_reg_read(ah, AR5K_BEACON) &~
2823                 (AR5K_BEACON_PERIOD | AR5K_BEACON_TIM)) |
2824                 AR5K_REG_SM(state->bs_tim_offset ? state->bs_tim_offset + 4 : 0,
2825                 AR5K_BEACON_TIM) | AR5K_REG_SM(state->bs_interval,
2826                 AR5K_BEACON_PERIOD), AR5K_BEACON);
2827
2828         /*
2829          * Write new beacon miss threshold, if it appears to be valid
2830          * XXX: Figure out right values for min <= bs_bmiss_threshold <= max
2831          * and return if its not in range. We can test this by reading value and
2832          * setting value to a largest value and seeing which values register.
2833          */
2834
2835         AR5K_REG_WRITE_BITS(ah, AR5K_RSSI_THR, AR5K_RSSI_THR_BMISS,
2836                         state->bs_bmiss_threshold);
2837
2838         /*
2839          * Set sleep control register
2840          * XXX: Didn't find this in 5210 code but since this register
2841          * exists also in ar5k's 5210 headers i leave it as common code.
2842          */
2843         AR5K_REG_WRITE_BITS(ah, AR5K_SLEEP_CTL, AR5K_SLEEP_CTL_SLDUR,
2844                         (state->bs_sleep_duration - 3) << 3);
2845
2846         /*
2847          * Set enhanced sleep registers on 5212
2848          */
2849         if (ah->ah_version == AR5K_AR5212) {
2850                 if (state->bs_sleep_duration > state->bs_interval &&
2851                                 roundup(state->bs_sleep_duration, interval) ==
2852                                 state->bs_sleep_duration)
2853                         interval = state->bs_sleep_duration;
2854
2855                 if (state->bs_sleep_duration > dtim && (dtim == 0 ||
2856                                 roundup(state->bs_sleep_duration, dtim) ==
2857                                 state->bs_sleep_duration))
2858                         dtim = state->bs_sleep_duration;
2859
2860                 if (interval > dtim)
2861                         return -EINVAL;
2862
2863                 next_beacon = interval == dtim ? state->bs_next_dtim :
2864                         state->bs_next_beacon;
2865
2866                 ath5k_hw_reg_write(ah,
2867                         AR5K_REG_SM((state->bs_next_dtim - 3) << 3,
2868                         AR5K_SLEEP0_NEXT_DTIM) |
2869                         AR5K_REG_SM(10, AR5K_SLEEP0_CABTO) |
2870                         AR5K_SLEEP0_ENH_SLEEP_EN |
2871                         AR5K_SLEEP0_ASSUME_DTIM, AR5K_SLEEP0);
2872
2873                 ath5k_hw_reg_write(ah, AR5K_REG_SM((next_beacon - 3) << 3,
2874                         AR5K_SLEEP1_NEXT_TIM) |
2875                         AR5K_REG_SM(10, AR5K_SLEEP1_BEACON_TO), AR5K_SLEEP1);
2876
2877                 ath5k_hw_reg_write(ah,
2878                         AR5K_REG_SM(interval, AR5K_SLEEP2_TIM_PER) |
2879                         AR5K_REG_SM(dtim, AR5K_SLEEP2_DTIM_PER), AR5K_SLEEP2);
2880         }
2881
2882         return 0;
2883 }
2884
2885 /*
2886  * Reset beacon timers
2887  */
2888 void ath5k_hw_reset_beacon(struct ath5k_hw *ah)
2889 {
2890         ATH5K_TRACE(ah->ah_sc);
2891         /*
2892          * Disable beacon timer
2893          */
2894         ath5k_hw_reg_write(ah, 0, AR5K_TIMER0);
2895
2896         /*
2897          * Disable some beacon register values
2898          */
2899         AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1,
2900                         AR5K_STA_ID1_DEFAULT_ANTENNA | AR5K_STA_ID1_PCF);
2901         ath5k_hw_reg_write(ah, AR5K_BEACON_PERIOD, AR5K_BEACON);
2902 }
2903
2904 /*
2905  * Wait for beacon queue to finish
2906  */
2907 int ath5k_hw_beaconq_finish(struct ath5k_hw *ah, unsigned long phys_addr)
2908 {
2909         unsigned int i;
2910         int ret;
2911
2912         ATH5K_TRACE(ah->ah_sc);
2913
2914         /* 5210 doesn't have QCU*/
2915         if (ah->ah_version == AR5K_AR5210) {
2916                 /*
2917                  * Wait for beaconn queue to finish by checking
2918                  * Control Register and Beacon Status Register.
2919                  */
2920                 for (i = AR5K_TUNE_BEACON_INTERVAL / 2; i > 0; i--) {
2921                         if (!(ath5k_hw_reg_read(ah, AR5K_BSR) & AR5K_BSR_TXQ1F)
2922                                         ||
2923                             !(ath5k_hw_reg_read(ah, AR5K_CR) & AR5K_BSR_TXQ1F))
2924                                 break;
2925                         udelay(10);
2926                 }
2927
2928                 /* Timeout... */
2929                 if (i <= 0) {
2930                         /*
2931                          * Re-schedule the beacon queue
2932                          */
2933                         ath5k_hw_reg_write(ah, phys_addr, AR5K_NOQCU_TXDP1);
2934                         ath5k_hw_reg_write(ah, AR5K_BCR_TQ1V | AR5K_BCR_BDMAE,
2935                                         AR5K_BCR);
2936
2937                         return -EIO;
2938                 }
2939                 ret = 0;
2940         } else {
2941         /*5211/5212*/
2942                 ret = ath5k_hw_register_timeout(ah,
2943                         AR5K_QUEUE_STATUS(AR5K_TX_QUEUE_ID_BEACON),
2944                         AR5K_QCU_STS_FRMPENDCNT, 0, false);
2945
2946                 if (AR5K_REG_READ_Q(ah, AR5K_QCU_TXE, AR5K_TX_QUEUE_ID_BEACON))
2947                         return -EIO;
2948         }
2949
2950         return ret;
2951 }
2952 #endif
2953
2954 /*
2955  * Update mib counters (statistics)
2956  */
2957 void ath5k_hw_update_mib_counters(struct ath5k_hw *ah,
2958                 struct ieee80211_low_level_stats  *stats)
2959 {
2960         ATH5K_TRACE(ah->ah_sc);
2961
2962         /* Read-And-Clear */
2963         stats->dot11ACKFailureCount += ath5k_hw_reg_read(ah, AR5K_ACK_FAIL);
2964         stats->dot11RTSFailureCount += ath5k_hw_reg_read(ah, AR5K_RTS_FAIL);
2965         stats->dot11RTSSuccessCount += ath5k_hw_reg_read(ah, AR5K_RTS_OK);
2966         stats->dot11FCSErrorCount += ath5k_hw_reg_read(ah, AR5K_FCS_FAIL);
2967
2968         /* XXX: Should we use this to track beacon count ?
2969          * -we read it anyway to clear the register */
2970         ath5k_hw_reg_read(ah, AR5K_BEACON_CNT);
2971
2972         /* Reset profile count registers on 5212*/
2973         if (ah->ah_version == AR5K_AR5212) {
2974                 ath5k_hw_reg_write(ah, 0, AR5K_PROFCNT_TX);
2975                 ath5k_hw_reg_write(ah, 0, AR5K_PROFCNT_RX);
2976                 ath5k_hw_reg_write(ah, 0, AR5K_PROFCNT_RXCLR);
2977                 ath5k_hw_reg_write(ah, 0, AR5K_PROFCNT_CYCLE);
2978         }
2979 }
2980
2981 /** ath5k_hw_set_ack_bitrate - set bitrate for ACKs
2982  *
2983  * @ah: the &struct ath5k_hw
2984  * @high: determines if to use low bit rate or now
2985  */
2986 void ath5k_hw_set_ack_bitrate_high(struct ath5k_hw *ah, bool high)
2987 {
2988         if (ah->ah_version != AR5K_AR5212)
2989                 return;
2990         else {
2991                 u32 val = AR5K_STA_ID1_BASE_RATE_11B | AR5K_STA_ID1_ACKCTS_6MB;
2992                 if (high)
2993                         AR5K_REG_ENABLE_BITS(ah, AR5K_STA_ID1, val);
2994                 else
2995                         AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, val);
2996         }
2997 }
2998
2999
3000 /*
3001  * ACK/CTS Timeouts
3002  */
3003
3004 /*
3005  * Set ACK timeout on PCU
3006  */
3007 int ath5k_hw_set_ack_timeout(struct ath5k_hw *ah, unsigned int timeout)
3008 {
3009         ATH5K_TRACE(ah->ah_sc);
3010         if (ath5k_hw_clocktoh(AR5K_REG_MS(0xffffffff, AR5K_TIME_OUT_ACK),
3011                         ah->ah_turbo) <= timeout)
3012                 return -EINVAL;
3013
3014         AR5K_REG_WRITE_BITS(ah, AR5K_TIME_OUT, AR5K_TIME_OUT_ACK,
3015                 ath5k_hw_htoclock(timeout, ah->ah_turbo));
3016
3017         return 0;
3018 }
3019
3020 /*
3021  * Read the ACK timeout from PCU
3022  */
3023 unsigned int ath5k_hw_get_ack_timeout(struct ath5k_hw *ah)
3024 {
3025         ATH5K_TRACE(ah->ah_sc);
3026
3027         return ath5k_hw_clocktoh(AR5K_REG_MS(ath5k_hw_reg_read(ah,
3028                         AR5K_TIME_OUT), AR5K_TIME_OUT_ACK), ah->ah_turbo);
3029 }
3030
3031 /*
3032  * Set CTS timeout on PCU
3033  */
3034 int ath5k_hw_set_cts_timeout(struct ath5k_hw *ah, unsigned int timeout)
3035 {
3036         ATH5K_TRACE(ah->ah_sc);
3037         if (ath5k_hw_clocktoh(AR5K_REG_MS(0xffffffff, AR5K_TIME_OUT_CTS),
3038                         ah->ah_turbo) <= timeout)
3039                 return -EINVAL;
3040
3041         AR5K_REG_WRITE_BITS(ah, AR5K_TIME_OUT, AR5K_TIME_OUT_CTS,
3042                         ath5k_hw_htoclock(timeout, ah->ah_turbo));
3043
3044         return 0;
3045 }
3046
3047 /*
3048  * Read CTS timeout from PCU
3049  */
3050 unsigned int ath5k_hw_get_cts_timeout(struct ath5k_hw *ah)
3051 {
3052         ATH5K_TRACE(ah->ah_sc);
3053         return ath5k_hw_clocktoh(AR5K_REG_MS(ath5k_hw_reg_read(ah,
3054                         AR5K_TIME_OUT), AR5K_TIME_OUT_CTS), ah->ah_turbo);
3055 }
3056
3057 /*
3058  * Key table (WEP) functions
3059  */
3060
3061 int ath5k_hw_reset_key(struct ath5k_hw *ah, u16 entry)
3062 {
3063         unsigned int i;
3064
3065         ATH5K_TRACE(ah->ah_sc);
3066         AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
3067
3068         for (i = 0; i < AR5K_KEYCACHE_SIZE; i++)
3069                 ath5k_hw_reg_write(ah, 0, AR5K_KEYTABLE_OFF(entry, i));
3070
3071         /*
3072          * Set NULL encryption on AR5212+
3073          *
3074          * Note: AR5K_KEYTABLE_TYPE -> AR5K_KEYTABLE_OFF(entry, 5)
3075          *       AR5K_KEYTABLE_TYPE_NULL -> 0x00000007
3076          *
3077          * Note2: Windows driver (ndiswrapper) sets this to
3078          *        0x00000714 instead of 0x00000007
3079          */
3080         if (ah->ah_version > AR5K_AR5211)
3081                 ath5k_hw_reg_write(ah, AR5K_KEYTABLE_TYPE_NULL,
3082                                 AR5K_KEYTABLE_TYPE(entry));
3083
3084         return 0;
3085 }
3086
3087 int ath5k_hw_is_key_valid(struct ath5k_hw *ah, u16 entry)
3088 {
3089         ATH5K_TRACE(ah->ah_sc);
3090         AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
3091
3092         /* Check the validation flag at the end of the entry */
3093         return ath5k_hw_reg_read(ah, AR5K_KEYTABLE_MAC1(entry)) &
3094                 AR5K_KEYTABLE_VALID;
3095 }
3096
3097 int ath5k_hw_set_key(struct ath5k_hw *ah, u16 entry,
3098                 const struct ieee80211_key_conf *key, const u8 *mac)
3099 {
3100         unsigned int i;
3101         __le32 key_v[5] = {};
3102         u32 keytype;
3103
3104         ATH5K_TRACE(ah->ah_sc);
3105
3106         /* key->keylen comes in from mac80211 in bytes */
3107
3108         if (key->keylen > AR5K_KEYTABLE_SIZE / 8)
3109                 return -EOPNOTSUPP;
3110
3111         switch (key->keylen) {
3112         /* WEP 40-bit   = 40-bit  entered key + 24 bit IV = 64-bit */
3113         case 40 / 8:
3114                 memcpy(&key_v[0], key->key, 5);
3115                 keytype = AR5K_KEYTABLE_TYPE_40;
3116                 break;
3117
3118         /* WEP 104-bit  = 104-bit entered key + 24-bit IV = 128-bit */
3119         case 104 / 8:
3120                 memcpy(&key_v[0], &key->key[0], 6);
3121                 memcpy(&key_v[2], &key->key[6], 6);
3122                 memcpy(&key_v[4], &key->key[12], 1);
3123                 keytype = AR5K_KEYTABLE_TYPE_104;
3124                 break;
3125         /* WEP 128-bit  = 128-bit entered key + 24 bit IV = 152-bit */
3126         case 128 / 8:
3127                 memcpy(&key_v[0], &key->key[0], 6);
3128                 memcpy(&key_v[2], &key->key[6], 6);
3129                 memcpy(&key_v[4], &key->key[12], 4);
3130                 keytype = AR5K_KEYTABLE_TYPE_128;
3131                 break;
3132
3133         default:
3134                 return -EINVAL; /* shouldn't happen */
3135         }
3136
3137         for (i = 0; i < ARRAY_SIZE(key_v); i++)
3138                 ath5k_hw_reg_write(ah, le32_to_cpu(key_v[i]),
3139                                 AR5K_KEYTABLE_OFF(entry, i));
3140
3141         ath5k_hw_reg_write(ah, keytype, AR5K_KEYTABLE_TYPE(entry));
3142
3143         return ath5k_hw_set_key_lladdr(ah, entry, mac);
3144 }
3145
3146 int ath5k_hw_set_key_lladdr(struct ath5k_hw *ah, u16 entry, const u8 *mac)
3147 {
3148         u32 low_id, high_id;
3149
3150         ATH5K_TRACE(ah->ah_sc);
3151          /* Invalid entry (key table overflow) */
3152         AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
3153
3154         /* MAC may be NULL if it's a broadcast key. In this case no need to
3155          * to compute AR5K_LOW_ID and AR5K_HIGH_ID as we already know it. */
3156         if (unlikely(mac == NULL)) {
3157                 low_id = 0xffffffff;
3158                 high_id = 0xffff | AR5K_KEYTABLE_VALID;
3159         } else {
3160                 low_id = AR5K_LOW_ID(mac);
3161                 high_id = AR5K_HIGH_ID(mac) | AR5K_KEYTABLE_VALID;
3162         }
3163
3164         ath5k_hw_reg_write(ah, low_id, AR5K_KEYTABLE_MAC0(entry));
3165         ath5k_hw_reg_write(ah, high_id, AR5K_KEYTABLE_MAC1(entry));
3166
3167         return 0;
3168 }
3169
3170
3171 /********************************************\
3172 Queue Control Unit, DFS Control Unit Functions
3173 \********************************************/
3174
3175 /*
3176  * Initialize a transmit queue
3177  */
3178 int ath5k_hw_setup_tx_queue(struct ath5k_hw *ah, enum ath5k_tx_queue queue_type,
3179                 struct ath5k_txq_info *queue_info)
3180 {
3181         unsigned int queue;
3182         int ret;
3183
3184         ATH5K_TRACE(ah->ah_sc);
3185
3186         /*
3187          * Get queue by type
3188          */
3189         /*5210 only has 2 queues*/
3190         if (ah->ah_version == AR5K_AR5210) {
3191                 switch (queue_type) {
3192                 case AR5K_TX_QUEUE_DATA:
3193                         queue = AR5K_TX_QUEUE_ID_NOQCU_DATA;
3194                         break;
3195                 case AR5K_TX_QUEUE_BEACON:
3196                 case AR5K_TX_QUEUE_CAB:
3197                         queue = AR5K_TX_QUEUE_ID_NOQCU_BEACON;
3198                         break;
3199                 default:
3200                         return -EINVAL;
3201                 }
3202         } else {
3203                 switch (queue_type) {
3204                 case AR5K_TX_QUEUE_DATA:
3205                         for (queue = AR5K_TX_QUEUE_ID_DATA_MIN;
3206                                 ah->ah_txq[queue].tqi_type !=
3207                                 AR5K_TX_QUEUE_INACTIVE; queue++) {
3208
3209                                 if (queue > AR5K_TX_QUEUE_ID_DATA_MAX)
3210                                         return -EINVAL;
3211                         }
3212                         break;
3213                 case AR5K_TX_QUEUE_UAPSD:
3214                         queue = AR5K_TX_QUEUE_ID_UAPSD;
3215                         break;
3216                 case AR5K_TX_QUEUE_BEACON:
3217                         queue = AR5K_TX_QUEUE_ID_BEACON;
3218                         break;
3219                 case AR5K_TX_QUEUE_CAB:
3220                         queue = AR5K_TX_QUEUE_ID_CAB;
3221                         break;
3222                 case AR5K_TX_QUEUE_XR_DATA:
3223                         if (ah->ah_version != AR5K_AR5212)
3224                                 ATH5K_ERR(ah->ah_sc,
3225                                         "XR data queues only supported in"
3226                                         " 5212!\n");
3227                         queue = AR5K_TX_QUEUE_ID_XR_DATA;
3228                         break;
3229                 default:
3230                         return -EINVAL;
3231                 }
3232         }
3233
3234         /*
3235          * Setup internal queue structure
3236          */
3237         memset(&ah->ah_txq[queue], 0, sizeof(struct ath5k_txq_info));
3238         ah->ah_txq[queue].tqi_type = queue_type;
3239
3240         if (queue_info != NULL) {
3241                 queue_info->tqi_type = queue_type;
3242                 ret = ath5k_hw_setup_tx_queueprops(ah, queue, queue_info);
3243                 if (ret)
3244                         return ret;
3245         }
3246         /*
3247          * We use ah_txq_status to hold a temp value for
3248          * the Secondary interrupt mask registers on 5211+
3249          * check out ath5k_hw_reset_tx_queue
3250          */
3251         AR5K_Q_ENABLE_BITS(ah->ah_txq_status, queue);
3252
3253         return queue;
3254 }
3255
3256 /*
3257  * Setup a transmit queue
3258  */
3259 int ath5k_hw_setup_tx_queueprops(struct ath5k_hw *ah, int queue,
3260                                 const struct ath5k_txq_info *queue_info)
3261 {
3262         ATH5K_TRACE(ah->ah_sc);
3263         AR5K_ASSERT_ENTRY(queue, ah->ah_capabilities.cap_queues.q_tx_num);
3264
3265         if (ah->ah_txq[queue].tqi_type == AR5K_TX_QUEUE_INACTIVE)
3266                 return -EIO;
3267
3268         memcpy(&ah->ah_txq[queue], queue_info, sizeof(struct ath5k_txq_info));
3269
3270         /*XXX: Is this supported on 5210 ?*/
3271         if ((queue_info->tqi_type == AR5K_TX_QUEUE_DATA &&
3272                         ((queue_info->tqi_subtype == AR5K_WME_AC_VI) ||
3273                         (queue_info->tqi_subtype == AR5K_WME_AC_VO))) ||
3274                         queue_info->tqi_type == AR5K_TX_QUEUE_UAPSD)
3275                 ah->ah_txq[queue].tqi_flags |= AR5K_TXQ_FLAG_POST_FR_BKOFF_DIS;
3276
3277         return 0;
3278 }
3279
3280 /*
3281  * Get properties for a specific transmit queue
3282  */
3283 int ath5k_hw_get_tx_queueprops(struct ath5k_hw *ah, int queue,
3284                 struct ath5k_txq_info *queue_info)
3285 {
3286         ATH5K_TRACE(ah->ah_sc);
3287         memcpy(queue_info, &ah->ah_txq[queue], sizeof(struct ath5k_txq_info));
3288         return 0;
3289 }
3290
3291 /*
3292  * Set a transmit queue inactive
3293  */
3294 void ath5k_hw_release_tx_queue(struct ath5k_hw *ah, unsigned int queue)
3295 {
3296         ATH5K_TRACE(ah->ah_sc);
3297         if (WARN_ON(queue >= ah->ah_capabilities.cap_queues.q_tx_num))
3298                 return;
3299
3300         /* This queue will be skipped in further operations */
3301         ah->ah_txq[queue].tqi_type = AR5K_TX_QUEUE_INACTIVE;
3302         /*For SIMR setup*/
3303         AR5K_Q_DISABLE_BITS(ah->ah_txq_status, queue);
3304 }
3305
3306 /*
3307  * Set DFS params for a transmit queue
3308  */
3309 int ath5k_hw_reset_tx_queue(struct ath5k_hw *ah, unsigned int queue)
3310 {
3311         u32 cw_min, cw_max, retry_lg, retry_sh;
3312         struct ath5k_txq_info *tq = &ah->ah_txq[queue];
3313
3314         ATH5K_TRACE(ah->ah_sc);
3315         AR5K_ASSERT_ENTRY(queue, ah->ah_capabilities.cap_queues.q_tx_num);
3316
3317         tq = &ah->ah_txq[queue];
3318
3319         if (tq->tqi_type == AR5K_TX_QUEUE_INACTIVE)
3320                 return 0;
3321
3322         if (ah->ah_version == AR5K_AR5210) {
3323                 /* Only handle data queues, others will be ignored */
3324                 if (tq->tqi_type != AR5K_TX_QUEUE_DATA)
3325                         return 0;
3326
3327                 /* Set Slot time */
3328                 ath5k_hw_reg_write(ah, ah->ah_turbo ?
3329                         AR5K_INIT_SLOT_TIME_TURBO : AR5K_INIT_SLOT_TIME,
3330                         AR5K_SLOT_TIME);
3331                 /* Set ACK_CTS timeout */
3332                 ath5k_hw_reg_write(ah, ah->ah_turbo ?
3333                         AR5K_INIT_ACK_CTS_TIMEOUT_TURBO :
3334                         AR5K_INIT_ACK_CTS_TIMEOUT, AR5K_SLOT_TIME);
3335                 /* Set Transmit Latency */
3336                 ath5k_hw_reg_write(ah, ah->ah_turbo ?
3337                         AR5K_INIT_TRANSMIT_LATENCY_TURBO :
3338                         AR5K_INIT_TRANSMIT_LATENCY, AR5K_USEC_5210);
3339                 /* Set IFS0 */
3340                 if (ah->ah_turbo)
3341                          ath5k_hw_reg_write(ah, ((AR5K_INIT_SIFS_TURBO +
3342                                 (ah->ah_aifs + tq->tqi_aifs) *
3343                                 AR5K_INIT_SLOT_TIME_TURBO) <<
3344                                 AR5K_IFS0_DIFS_S) | AR5K_INIT_SIFS_TURBO,
3345                                 AR5K_IFS0);
3346                 else
3347                         ath5k_hw_reg_write(ah, ((AR5K_INIT_SIFS +
3348                                 (ah->ah_aifs + tq->tqi_aifs) *
3349                                 AR5K_INIT_SLOT_TIME) << AR5K_IFS0_DIFS_S) |
3350                                 AR5K_INIT_SIFS, AR5K_IFS0);
3351
3352                 /* Set IFS1 */
3353                 ath5k_hw_reg_write(ah, ah->ah_turbo ?
3354                         AR5K_INIT_PROTO_TIME_CNTRL_TURBO :
3355                         AR5K_INIT_PROTO_TIME_CNTRL, AR5K_IFS1);
3356                 /* Set PHY register 0x9844 (??) */
3357                 ath5k_hw_reg_write(ah, ah->ah_turbo ?
3358                         (ath5k_hw_reg_read(ah, AR5K_PHY(17)) & ~0x7F) | 0x38 :
3359                         (ath5k_hw_reg_read(ah, AR5K_PHY(17)) & ~0x7F) | 0x1C,
3360                         AR5K_PHY(17));
3361                 /* Set Frame Control Register */
3362                 ath5k_hw_reg_write(ah, ah->ah_turbo ?
3363                         (AR5K_PHY_FRAME_CTL_INI | AR5K_PHY_TURBO_MODE |
3364                         AR5K_PHY_TURBO_SHORT | 0x2020) :
3365                         (AR5K_PHY_FRAME_CTL_INI | 0x1020),
3366                         AR5K_PHY_FRAME_CTL_5210);
3367         }
3368
3369         /*
3370          * Calculate cwmin/max by channel mode
3371          */
3372         cw_min = ah->ah_cw_min = AR5K_TUNE_CWMIN;
3373         cw_max = ah->ah_cw_max = AR5K_TUNE_CWMAX;
3374         ah->ah_aifs = AR5K_TUNE_AIFS;
3375         /*XR is only supported on 5212*/
3376         if (IS_CHAN_XR(ah->ah_current_channel) &&
3377                         ah->ah_version == AR5K_AR5212) {
3378                 cw_min = ah->ah_cw_min = AR5K_TUNE_CWMIN_XR;
3379                 cw_max = ah->ah_cw_max = AR5K_TUNE_CWMAX_XR;
3380                 ah->ah_aifs = AR5K_TUNE_AIFS_XR;
3381         /*B mode is not supported on 5210*/
3382         } else if (IS_CHAN_B(ah->ah_current_channel) &&
3383                         ah->ah_version != AR5K_AR5210) {
3384                 cw_min = ah->ah_cw_min = AR5K_TUNE_CWMIN_11B;
3385                 cw_max = ah->ah_cw_max = AR5K_TUNE_CWMAX_11B;
3386                 ah->ah_aifs = AR5K_TUNE_AIFS_11B;
3387         }
3388
3389         cw_min = 1;
3390         while (cw_min < ah->ah_cw_min)
3391                 cw_min = (cw_min << 1) | 1;
3392
3393         cw_min = tq->tqi_cw_min < 0 ? (cw_min >> (-tq->tqi_cw_min)) :
3394                 ((cw_min << tq->tqi_cw_min) + (1 << tq->tqi_cw_min) - 1);
3395         cw_max = tq->tqi_cw_max < 0 ? (cw_max >> (-tq->tqi_cw_max)) :
3396                 ((cw_max << tq->tqi_cw_max) + (1 << tq->tqi_cw_max) - 1);
3397
3398         /*
3399          * Calculate and set retry limits
3400          */
3401         if (ah->ah_software_retry) {
3402                 /* XXX Need to test this */
3403                 retry_lg = ah->ah_limit_tx_retries;
3404                 retry_sh = retry_lg = retry_lg > AR5K_DCU_RETRY_LMT_SH_RETRY ?
3405                         AR5K_DCU_RETRY_LMT_SH_RETRY : retry_lg;
3406         } else {
3407                 retry_lg = AR5K_INIT_LG_RETRY;
3408                 retry_sh = AR5K_INIT_SH_RETRY;
3409         }
3410
3411         /*No QCU/DCU [5210]*/
3412         if (ah->ah_version == AR5K_AR5210) {
3413                 ath5k_hw_reg_write(ah,
3414                         (cw_min << AR5K_NODCU_RETRY_LMT_CW_MIN_S)
3415                         | AR5K_REG_SM(AR5K_INIT_SLG_RETRY,
3416                                 AR5K_NODCU_RETRY_LMT_SLG_RETRY)
3417                         | AR5K_REG_SM(AR5K_INIT_SSH_RETRY,
3418                                 AR5K_NODCU_RETRY_LMT_SSH_RETRY)
3419                         | AR5K_REG_SM(retry_lg, AR5K_NODCU_RETRY_LMT_LG_RETRY)
3420                         | AR5K_REG_SM(retry_sh, AR5K_NODCU_RETRY_LMT_SH_RETRY),
3421                         AR5K_NODCU_RETRY_LMT);
3422         } else {
3423                 /*QCU/DCU [5211+]*/
3424                 ath5k_hw_reg_write(ah,
3425                         AR5K_REG_SM(AR5K_INIT_SLG_RETRY,
3426                                 AR5K_DCU_RETRY_LMT_SLG_RETRY) |
3427                         AR5K_REG_SM(AR5K_INIT_SSH_RETRY,
3428                                 AR5K_DCU_RETRY_LMT_SSH_RETRY) |
3429                         AR5K_REG_SM(retry_lg, AR5K_DCU_RETRY_LMT_LG_RETRY) |
3430                         AR5K_REG_SM(retry_sh, AR5K_DCU_RETRY_LMT_SH_RETRY),
3431                         AR5K_QUEUE_DFS_RETRY_LIMIT(queue));
3432
3433         /*===Rest is also for QCU/DCU only [5211+]===*/
3434
3435                 /*
3436                  * Set initial content window (cw_min/cw_max)
3437                  * and arbitrated interframe space (aifs)...
3438                  */
3439                 ath5k_hw_reg_write(ah,
3440                         AR5K_REG_SM(cw_min, AR5K_DCU_LCL_IFS_CW_MIN) |
3441                         AR5K_REG_SM(cw_max, AR5K_DCU_LCL_IFS_CW_MAX) |
3442                         AR5K_REG_SM(ah->ah_aifs + tq->tqi_aifs,
3443                                 AR5K_DCU_LCL_IFS_AIFS),
3444                         AR5K_QUEUE_DFS_LOCAL_IFS(queue));
3445
3446                 /*
3447                  * Set misc registers
3448                  */
3449                 ath5k_hw_reg_write(ah, AR5K_QCU_MISC_DCU_EARLY,
3450                         AR5K_QUEUE_MISC(queue));
3451
3452                 if (tq->tqi_cbr_period) {
3453                         ath5k_hw_reg_write(ah, AR5K_REG_SM(tq->tqi_cbr_period,
3454                                 AR5K_QCU_CBRCFG_INTVAL) |
3455                                 AR5K_REG_SM(tq->tqi_cbr_overflow_limit,
3456                                 AR5K_QCU_CBRCFG_ORN_THRES),
3457                                 AR5K_QUEUE_CBRCFG(queue));
3458                         AR5K_REG_ENABLE_BITS(ah, AR5K_QUEUE_MISC(queue),
3459                                 AR5K_QCU_MISC_FRSHED_CBR);
3460                         if (tq->tqi_cbr_overflow_limit)
3461                                 AR5K_REG_ENABLE_BITS(ah,
3462                                         AR5K_QUEUE_MISC(queue),
3463                                         AR5K_QCU_MISC_CBR_THRES_ENABLE);
3464                 }
3465
3466                 if (tq->tqi_ready_time)
3467                         ath5k_hw_reg_write(ah, AR5K_REG_SM(tq->tqi_ready_time,
3468                                 AR5K_QCU_RDYTIMECFG_INTVAL) |
3469                                 AR5K_QCU_RDYTIMECFG_ENABLE,
3470                                 AR5K_QUEUE_RDYTIMECFG(queue));
3471
3472                 if (tq->tqi_burst_time) {
3473                         ath5k_hw_reg_write(ah, AR5K_REG_SM(tq->tqi_burst_time,
3474                                 AR5K_DCU_CHAN_TIME_DUR) |
3475                                 AR5K_DCU_CHAN_TIME_ENABLE,
3476                                 AR5K_QUEUE_DFS_CHANNEL_TIME(queue));
3477
3478                         if (tq->tqi_flags & AR5K_TXQ_FLAG_RDYTIME_EXP_POLICY_ENABLE)
3479                                 AR5K_REG_ENABLE_BITS(ah,
3480                                         AR5K_QUEUE_MISC(queue),
3481                                         AR5K_QCU_MISC_TXE);
3482                 }
3483
3484                 if (tq->tqi_flags & AR5K_TXQ_FLAG_BACKOFF_DISABLE)
3485                         ath5k_hw_reg_write(ah, AR5K_DCU_MISC_POST_FR_BKOFF_DIS,
3486                                 AR5K_QUEUE_DFS_MISC(queue));
3487
3488                 if (tq->tqi_flags & AR5K_TXQ_FLAG_FRAG_BURST_BACKOFF_ENABLE)
3489                         ath5k_hw_reg_write(ah, AR5K_DCU_MISC_BACKOFF_FRAG,
3490                                 AR5K_QUEUE_DFS_MISC(queue));
3491
3492                 /*
3493                  * Set registers by queue type
3494                  */
3495                 switch (tq->tqi_type) {
3496                 case AR5K_TX_QUEUE_BEACON:
3497                         AR5K_REG_ENABLE_BITS(ah, AR5K_QUEUE_MISC(queue),
3498                                 AR5K_QCU_MISC_FRSHED_DBA_GT |
3499                                 AR5K_QCU_MISC_CBREXP_BCN |
3500                                 AR5K_QCU_MISC_BCN_ENABLE);
3501
3502                         AR5K_REG_ENABLE_BITS(ah, AR5K_QUEUE_DFS_MISC(queue),
3503                                 (AR5K_DCU_MISC_ARBLOCK_CTL_GLOBAL <<
3504                                 AR5K_DCU_MISC_ARBLOCK_CTL_S) |
3505                                 AR5K_DCU_MISC_POST_FR_BKOFF_DIS |
3506                                 AR5K_DCU_MISC_BCN_ENABLE);
3507
3508                         ath5k_hw_reg_write(ah, ((AR5K_TUNE_BEACON_INTERVAL -
3509                                 (AR5K_TUNE_SW_BEACON_RESP -
3510                                 AR5K_TUNE_DMA_BEACON_RESP) -
3511                                 AR5K_TUNE_ADDITIONAL_SWBA_BACKOFF) * 1024) |
3512                                 AR5K_QCU_RDYTIMECFG_ENABLE,
3513                                 AR5K_QUEUE_RDYTIMECFG(queue));
3514                         break;
3515
3516                 case AR5K_TX_QUEUE_CAB:
3517                         AR5K_REG_ENABLE_BITS(ah, AR5K_QUEUE_MISC(queue),
3518                                 AR5K_QCU_MISC_FRSHED_DBA_GT |
3519                                 AR5K_QCU_MISC_CBREXP |
3520                                 AR5K_QCU_MISC_CBREXP_BCN);
3521
3522                         AR5K_REG_ENABLE_BITS(ah, AR5K_QUEUE_DFS_MISC(queue),
3523                                 (AR5K_DCU_MISC_ARBLOCK_CTL_GLOBAL <<
3524                                 AR5K_DCU_MISC_ARBLOCK_CTL_S));
3525                         break;
3526
3527                 case AR5K_TX_QUEUE_UAPSD:
3528                         AR5K_REG_ENABLE_BITS(ah, AR5K_QUEUE_MISC(queue),
3529                                 AR5K_QCU_MISC_CBREXP);
3530                         break;
3531
3532                 case AR5K_TX_QUEUE_DATA:
3533                 default:
3534                         break;
3535                 }
3536
3537                 /*
3538                  * Enable interrupts for this tx queue
3539                  * in the secondary interrupt mask registers
3540                  */
3541                 if (tq->tqi_flags & AR5K_TXQ_FLAG_TXOKINT_ENABLE)
3542                         AR5K_Q_ENABLE_BITS(ah->ah_txq_imr_txok, queue);
3543
3544                 if (tq->tqi_flags & AR5K_TXQ_FLAG_TXERRINT_ENABLE)
3545                         AR5K_Q_ENABLE_BITS(ah->ah_txq_imr_txerr, queue);
3546
3547                 if (tq->tqi_flags & AR5K_TXQ_FLAG_TXURNINT_ENABLE)
3548                         AR5K_Q_ENABLE_BITS(ah->ah_txq_imr_txurn, queue);
3549
3550                 if (tq->tqi_flags & AR5K_TXQ_FLAG_TXDESCINT_ENABLE)
3551                         AR5K_Q_ENABLE_BITS(ah->ah_txq_imr_txdesc, queue);
3552
3553                 if (tq->tqi_flags & AR5K_TXQ_FLAG_TXEOLINT_ENABLE)
3554                         AR5K_Q_ENABLE_BITS(ah->ah_txq_imr_txeol, queue);
3555
3556
3557                 /* Update secondary interrupt mask registers */
3558                 ah->ah_txq_imr_txok &= ah->ah_txq_status;
3559                 ah->ah_txq_imr_txerr &= ah->ah_txq_status;
3560                 ah->ah_txq_imr_txurn &= ah->ah_txq_status;
3561                 ah->ah_txq_imr_txdesc &= ah->ah_txq_status;
3562                 ah->ah_txq_imr_txeol &= ah->ah_txq_status;
3563
3564                 ath5k_hw_reg_write(ah, AR5K_REG_SM(ah->ah_txq_imr_txok,
3565                         AR5K_SIMR0_QCU_TXOK) |
3566                         AR5K_REG_SM(ah->ah_txq_imr_txdesc,
3567                         AR5K_SIMR0_QCU_TXDESC), AR5K_SIMR0);
3568                 ath5k_hw_reg_write(ah, AR5K_REG_SM(ah->ah_txq_imr_txerr,
3569                         AR5K_SIMR1_QCU_TXERR) |
3570                         AR5K_REG_SM(ah->ah_txq_imr_txeol,
3571                         AR5K_SIMR1_QCU_TXEOL), AR5K_SIMR1);
3572                 ath5k_hw_reg_write(ah, AR5K_REG_SM(ah->ah_txq_imr_txurn,
3573                         AR5K_SIMR2_QCU_TXURN), AR5K_SIMR2);
3574         }
3575
3576         return 0;
3577 }
3578
3579 /*
3580  * Get number of pending frames
3581  * for a specific queue [5211+]
3582  */
3583 u32 ath5k_hw_num_tx_pending(struct ath5k_hw *ah, unsigned int queue) {
3584         ATH5K_TRACE(ah->ah_sc);
3585         AR5K_ASSERT_ENTRY(queue, ah->ah_capabilities.cap_queues.q_tx_num);
3586
3587         /* Return if queue is declared inactive */
3588         if (ah->ah_txq[queue].tqi_type == AR5K_TX_QUEUE_INACTIVE)
3589                 return false;
3590
3591         /* XXX: How about AR5K_CFG_TXCNT ? */
3592         if (ah->ah_version == AR5K_AR5210)
3593                 return false;
3594
3595         return AR5K_QUEUE_STATUS(queue) & AR5K_QCU_STS_FRMPENDCNT;
3596 }
3597
3598 /*
3599  * Set slot time
3600  */
3601 int ath5k_hw_set_slot_time(struct ath5k_hw *ah, unsigned int slot_time)
3602 {
3603         ATH5K_TRACE(ah->ah_sc);
3604         if (slot_time < AR5K_SLOT_TIME_9 || slot_time > AR5K_SLOT_TIME_MAX)
3605                 return -EINVAL;
3606
3607         if (ah->ah_version == AR5K_AR5210)
3608                 ath5k_hw_reg_write(ah, ath5k_hw_htoclock(slot_time,
3609                                 ah->ah_turbo), AR5K_SLOT_TIME);
3610         else
3611                 ath5k_hw_reg_write(ah, slot_time, AR5K_DCU_GBL_IFS_SLOT);
3612
3613         return 0;
3614 }
3615
3616 /*
3617  * Get slot time
3618  */
3619 unsigned int ath5k_hw_get_slot_time(struct ath5k_hw *ah)
3620 {
3621         ATH5K_TRACE(ah->ah_sc);
3622         if (ah->ah_version == AR5K_AR5210)
3623                 return ath5k_hw_clocktoh(ath5k_hw_reg_read(ah,
3624                                 AR5K_SLOT_TIME) & 0xffff, ah->ah_turbo);
3625         else
3626                 return ath5k_hw_reg_read(ah, AR5K_DCU_GBL_IFS_SLOT) & 0xffff;
3627 }
3628
3629
3630 /******************************\
3631  Hardware Descriptor Functions
3632 \******************************/
3633
3634 /*
3635  * TX Descriptor
3636  */
3637
3638 /*
3639  * Initialize the 2-word tx descriptor on 5210/5211
3640  */
3641 static int
3642 ath5k_hw_setup_2word_tx_desc(struct ath5k_hw *ah, struct ath5k_desc *desc,
3643         unsigned int pkt_len, unsigned int hdr_len, enum ath5k_pkt_type type,
3644         unsigned int tx_power, unsigned int tx_rate0, unsigned int tx_tries0,
3645         unsigned int key_index, unsigned int antenna_mode, unsigned int flags,
3646         unsigned int rtscts_rate, unsigned int rtscts_duration)
3647 {
3648         u32 frame_type;
3649         struct ath5k_hw_2w_tx_ctl *tx_ctl;
3650         unsigned int frame_len;
3651
3652         tx_ctl = &desc->ud.ds_tx5210.tx_ctl;
3653
3654         /*
3655          * Validate input
3656          * - Zero retries don't make sense.
3657          * - A zero rate will put the HW into a mode where it continously sends
3658          *   noise on the channel, so it is important to avoid this.
3659          */
3660         if (unlikely(tx_tries0 == 0)) {
3661                 ATH5K_ERR(ah->ah_sc, "zero retries\n");
3662                 WARN_ON(1);
3663                 return -EINVAL;
3664         }
3665         if (unlikely(tx_rate0 == 0)) {
3666                 ATH5K_ERR(ah->ah_sc, "zero rate\n");
3667                 WARN_ON(1);
3668                 return -EINVAL;
3669         }
3670
3671         /* Clear descriptor */
3672         memset(&desc->ud.ds_tx5210, 0, sizeof(struct ath5k_hw_5210_tx_desc));
3673
3674         /* Setup control descriptor */
3675
3676         /* Verify and set frame length */
3677
3678         /* remove padding we might have added before */
3679         frame_len = pkt_len - (hdr_len & 3) + FCS_LEN;
3680
3681         if (frame_len & ~AR5K_2W_TX_DESC_CTL0_FRAME_LEN)
3682                 return -EINVAL;
3683
3684         tx_ctl->tx_control_0 = frame_len & AR5K_2W_TX_DESC_CTL0_FRAME_LEN;
3685
3686         /* Verify and set buffer length */
3687
3688         /* NB: beacon's BufLen must be a multiple of 4 bytes */
3689         if(type == AR5K_PKT_TYPE_BEACON)
3690                 pkt_len = roundup(pkt_len, 4);
3691
3692         if (pkt_len & ~AR5K_2W_TX_DESC_CTL1_BUF_LEN)
3693                 return -EINVAL;
3694
3695         tx_ctl->tx_control_1 = pkt_len & AR5K_2W_TX_DESC_CTL1_BUF_LEN;
3696
3697         /*
3698          * Verify and set header length
3699          * XXX: I only found that on 5210 code, does it work on 5211 ?
3700          */
3701         if (ah->ah_version == AR5K_AR5210) {
3702                 if (hdr_len & ~AR5K_2W_TX_DESC_CTL0_HEADER_LEN)
3703                         return -EINVAL;
3704                 tx_ctl->tx_control_0 |=
3705                         AR5K_REG_SM(hdr_len, AR5K_2W_TX_DESC_CTL0_HEADER_LEN);
3706         }
3707
3708         /*Diferences between 5210-5211*/
3709         if (ah->ah_version == AR5K_AR5210) {
3710                 switch (type) {
3711                 case AR5K_PKT_TYPE_BEACON:
3712                 case AR5K_PKT_TYPE_PROBE_RESP:
3713                         frame_type = AR5K_AR5210_TX_DESC_FRAME_TYPE_NO_DELAY;
3714                 case AR5K_PKT_TYPE_PIFS:
3715                         frame_type = AR5K_AR5210_TX_DESC_FRAME_TYPE_PIFS;
3716                 default:
3717                         frame_type = type /*<< 2 ?*/;
3718                 }
3719
3720                 tx_ctl->tx_control_0 |=
3721                         AR5K_REG_SM(frame_type, AR5K_2W_TX_DESC_CTL0_FRAME_TYPE) |
3722                         AR5K_REG_SM(tx_rate0, AR5K_2W_TX_DESC_CTL0_XMIT_RATE);
3723         } else {
3724                 tx_ctl->tx_control_0 |=
3725                         AR5K_REG_SM(tx_rate0, AR5K_2W_TX_DESC_CTL0_XMIT_RATE) |
3726                         AR5K_REG_SM(antenna_mode, AR5K_2W_TX_DESC_CTL0_ANT_MODE_XMIT);
3727                 tx_ctl->tx_control_1 |=
3728                         AR5K_REG_SM(type, AR5K_2W_TX_DESC_CTL1_FRAME_TYPE);
3729         }
3730 #define _TX_FLAGS(_c, _flag)                                            \
3731         if (flags & AR5K_TXDESC_##_flag)                                \
3732                 tx_ctl->tx_control_##_c |=                              \
3733                         AR5K_2W_TX_DESC_CTL##_c##_##_flag
3734
3735         _TX_FLAGS(0, CLRDMASK);
3736         _TX_FLAGS(0, VEOL);
3737         _TX_FLAGS(0, INTREQ);
3738         _TX_FLAGS(0, RTSENA);
3739         _TX_FLAGS(1, NOACK);
3740
3741 #undef _TX_FLAGS
3742
3743         /*
3744          * WEP crap
3745          */
3746         if (key_index != AR5K_TXKEYIX_INVALID) {
3747                 tx_ctl->tx_control_0 |=
3748                         AR5K_2W_TX_DESC_CTL0_ENCRYPT_KEY_VALID;
3749                 tx_ctl->tx_control_1 |=
3750                         AR5K_REG_SM(key_index,
3751                         AR5K_2W_TX_DESC_CTL1_ENCRYPT_KEY_INDEX);
3752         }
3753
3754         /*
3755          * RTS/CTS Duration [5210 ?]
3756          */
3757         if ((ah->ah_version == AR5K_AR5210) &&
3758                         (flags & (AR5K_TXDESC_RTSENA | AR5K_TXDESC_CTSENA)))
3759                 tx_ctl->tx_control_1 |= rtscts_duration &
3760                                 AR5K_2W_TX_DESC_CTL1_RTS_DURATION;
3761
3762         return 0;
3763 }
3764
3765 /*
3766  * Initialize the 4-word tx descriptor on 5212
3767  */
3768 static int ath5k_hw_setup_4word_tx_desc(struct ath5k_hw *ah,
3769         struct ath5k_desc *desc, unsigned int pkt_len, unsigned int hdr_len,
3770         enum ath5k_pkt_type type, unsigned int tx_power, unsigned int tx_rate0,
3771         unsigned int tx_tries0, unsigned int key_index,
3772         unsigned int antenna_mode, unsigned int flags, unsigned int rtscts_rate,
3773         unsigned int rtscts_duration)
3774 {
3775         struct ath5k_hw_4w_tx_ctl *tx_ctl;
3776         unsigned int frame_len;
3777
3778         ATH5K_TRACE(ah->ah_sc);
3779         tx_ctl = &desc->ud.ds_tx5212.tx_ctl;
3780
3781         /*
3782          * Validate input
3783          * - Zero retries don't make sense.
3784          * - A zero rate will put the HW into a mode where it continously sends
3785          *   noise on the channel, so it is important to avoid this.
3786          */
3787         if (unlikely(tx_tries0 == 0)) {
3788                 ATH5K_ERR(ah->ah_sc, "zero retries\n");
3789                 WARN_ON(1);
3790                 return -EINVAL;
3791         }
3792         if (unlikely(tx_rate0 == 0)) {
3793                 ATH5K_ERR(ah->ah_sc, "zero rate\n");
3794                 WARN_ON(1);
3795                 return -EINVAL;
3796         }
3797
3798         /* Clear descriptor */
3799         memset(&desc->ud.ds_tx5212, 0, sizeof(struct ath5k_hw_5212_tx_desc));
3800
3801         /* Setup control descriptor */
3802
3803         /* Verify and set frame length */
3804
3805         /* remove padding we might have added before */
3806         frame_len = pkt_len - (hdr_len & 3) + FCS_LEN;
3807
3808         if (frame_len & ~AR5K_4W_TX_DESC_CTL0_FRAME_LEN)
3809                 return -EINVAL;
3810
3811         tx_ctl->tx_control_0 = frame_len & AR5K_4W_TX_DESC_CTL0_FRAME_LEN;
3812
3813         /* Verify and set buffer length */
3814
3815         /* NB: beacon's BufLen must be a multiple of 4 bytes */
3816         if(type == AR5K_PKT_TYPE_BEACON)
3817                 pkt_len = roundup(pkt_len, 4);
3818
3819         if (pkt_len & ~AR5K_4W_TX_DESC_CTL1_BUF_LEN)
3820                 return -EINVAL;
3821
3822         tx_ctl->tx_control_1 = pkt_len & AR5K_4W_TX_DESC_CTL1_BUF_LEN;
3823
3824         tx_ctl->tx_control_0 |=
3825                 AR5K_REG_SM(tx_power, AR5K_4W_TX_DESC_CTL0_XMIT_POWER) |
3826                 AR5K_REG_SM(antenna_mode, AR5K_4W_TX_DESC_CTL0_ANT_MODE_XMIT);
3827         tx_ctl->tx_control_1 |= AR5K_REG_SM(type,
3828                                         AR5K_4W_TX_DESC_CTL1_FRAME_TYPE);
3829         tx_ctl->tx_control_2 = AR5K_REG_SM(tx_tries0 + AR5K_TUNE_HWTXTRIES,
3830                                         AR5K_4W_TX_DESC_CTL2_XMIT_TRIES0);
3831         tx_ctl->tx_control_3 = tx_rate0 & AR5K_4W_TX_DESC_CTL3_XMIT_RATE0;
3832
3833 #define _TX_FLAGS(_c, _flag)                    \
3834         if (flags & AR5K_TXDESC_##_flag)        \
3835                 tx_ctl->tx_control_##_c |=      \
3836                         AR5K_4W_TX_DESC_CTL##_c##_##_flag
3837
3838         _TX_FLAGS(0, CLRDMASK);
3839         _TX_FLAGS(0, VEOL);
3840         _TX_FLAGS(0, INTREQ);
3841         _TX_FLAGS(0, RTSENA);
3842         _TX_FLAGS(0, CTSENA);
3843         _TX_FLAGS(1, NOACK);
3844
3845 #undef _TX_FLAGS
3846
3847         /*
3848          * WEP crap
3849          */
3850         if (key_index != AR5K_TXKEYIX_INVALID) {
3851                 tx_ctl->tx_control_0 |= AR5K_4W_TX_DESC_CTL0_ENCRYPT_KEY_VALID;
3852                 tx_ctl->tx_control_1 |= AR5K_REG_SM(key_index,
3853                                 AR5K_4W_TX_DESC_CTL1_ENCRYPT_KEY_INDEX);
3854         }
3855
3856         /*
3857          * RTS/CTS
3858          */
3859         if (flags & (AR5K_TXDESC_RTSENA | AR5K_TXDESC_CTSENA)) {
3860                 if ((flags & AR5K_TXDESC_RTSENA) &&
3861                                 (flags & AR5K_TXDESC_CTSENA))
3862                         return -EINVAL;
3863                 tx_ctl->tx_control_2 |= rtscts_duration &
3864                                 AR5K_4W_TX_DESC_CTL2_RTS_DURATION;
3865                 tx_ctl->tx_control_3 |= AR5K_REG_SM(rtscts_rate,
3866                                 AR5K_4W_TX_DESC_CTL3_RTS_CTS_RATE);
3867         }
3868
3869         return 0;
3870 }
3871
3872 /*
3873  * Initialize a 4-word multirate tx descriptor on 5212
3874  */
3875 static int
3876 ath5k_hw_setup_xr_tx_desc(struct ath5k_hw *ah, struct ath5k_desc *desc,
3877         unsigned int tx_rate1, u_int tx_tries1, u_int tx_rate2, u_int tx_tries2,
3878         unsigned int tx_rate3, u_int tx_tries3)
3879 {
3880         struct ath5k_hw_4w_tx_ctl *tx_ctl;
3881
3882         /*
3883          * Rates can be 0 as long as the retry count is 0 too.
3884          * A zero rate and nonzero retry count will put the HW into a mode where
3885          * it continously sends noise on the channel, so it is important to
3886          * avoid this.
3887          */
3888         if (unlikely((tx_rate1 == 0 && tx_tries1 != 0) ||
3889                      (tx_rate2 == 0 && tx_tries2 != 0) ||
3890                      (tx_rate3 == 0 && tx_tries3 != 0))) {
3891                 ATH5K_ERR(ah->ah_sc, "zero rate\n");
3892                 WARN_ON(1);
3893                 return -EINVAL;
3894         }
3895
3896         if (ah->ah_version == AR5K_AR5212) {
3897                 tx_ctl = &desc->ud.ds_tx5212.tx_ctl;
3898
3899 #define _XTX_TRIES(_n)                                                  \
3900         if (tx_tries##_n) {                                             \
3901                 tx_ctl->tx_control_2 |=                         \
3902                     AR5K_REG_SM(tx_tries##_n,                           \
3903                     AR5K_4W_TX_DESC_CTL2_XMIT_TRIES##_n);               \
3904                 tx_ctl->tx_control_3 |=                         \
3905                     AR5K_REG_SM(tx_rate##_n,                            \
3906                     AR5K_4W_TX_DESC_CTL3_XMIT_RATE##_n);                \
3907         }
3908
3909                 _XTX_TRIES(1);
3910                 _XTX_TRIES(2);
3911                 _XTX_TRIES(3);
3912
3913 #undef _XTX_TRIES
3914
3915                 return 1;
3916         }
3917
3918         return 0;
3919 }
3920
3921 /*
3922  * Proccess the tx status descriptor on 5210/5211
3923  */
3924 static int ath5k_hw_proc_2word_tx_status(struct ath5k_hw *ah,
3925                 struct ath5k_desc *desc, struct ath5k_tx_status *ts)
3926 {
3927         struct ath5k_hw_2w_tx_ctl *tx_ctl;
3928         struct ath5k_hw_tx_status *tx_status;
3929
3930         ATH5K_TRACE(ah->ah_sc);
3931
3932         tx_ctl = &desc->ud.ds_tx5210.tx_ctl;
3933         tx_status = &desc->ud.ds_tx5210.tx_stat;
3934
3935         /* No frame has been send or error */
3936         if (unlikely((tx_status->tx_status_1 & AR5K_DESC_TX_STATUS1_DONE) == 0))
3937                 return -EINPROGRESS;
3938
3939         /*
3940          * Get descriptor status
3941          */
3942         ts->ts_tstamp = AR5K_REG_MS(tx_status->tx_status_0,
3943                 AR5K_DESC_TX_STATUS0_SEND_TIMESTAMP);
3944         ts->ts_shortretry = AR5K_REG_MS(tx_status->tx_status_0,
3945                 AR5K_DESC_TX_STATUS0_SHORT_RETRY_COUNT);
3946         ts->ts_longretry = AR5K_REG_MS(tx_status->tx_status_0,
3947                 AR5K_DESC_TX_STATUS0_LONG_RETRY_COUNT);
3948         /*TODO: ts->ts_virtcol + test*/
3949         ts->ts_seqnum = AR5K_REG_MS(tx_status->tx_status_1,
3950                 AR5K_DESC_TX_STATUS1_SEQ_NUM);
3951         ts->ts_rssi = AR5K_REG_MS(tx_status->tx_status_1,
3952                 AR5K_DESC_TX_STATUS1_ACK_SIG_STRENGTH);
3953         ts->ts_antenna = 1;
3954         ts->ts_status = 0;
3955         ts->ts_rate = AR5K_REG_MS(tx_ctl->tx_control_0,
3956                 AR5K_2W_TX_DESC_CTL0_XMIT_RATE);
3957
3958         if ((tx_status->tx_status_0 & AR5K_DESC_TX_STATUS0_FRAME_XMIT_OK) == 0){
3959                 if (tx_status->tx_status_0 &
3960                                 AR5K_DESC_TX_STATUS0_EXCESSIVE_RETRIES)
3961                         ts->ts_status |= AR5K_TXERR_XRETRY;
3962
3963                 if (tx_status->tx_status_0 & AR5K_DESC_TX_STATUS0_FIFO_UNDERRUN)
3964                         ts->ts_status |= AR5K_TXERR_FIFO;
3965
3966                 if (tx_status->tx_status_0 & AR5K_DESC_TX_STATUS0_FILTERED)
3967                         ts->ts_status |= AR5K_TXERR_FILT;
3968         }
3969
3970         return 0;
3971 }
3972
3973 /*
3974  * Proccess a tx descriptor on 5212
3975  */
3976 static int ath5k_hw_proc_4word_tx_status(struct ath5k_hw *ah,
3977                 struct ath5k_desc *desc, struct ath5k_tx_status *ts)
3978 {
3979         struct ath5k_hw_4w_tx_ctl *tx_ctl;
3980         struct ath5k_hw_tx_status *tx_status;
3981
3982         ATH5K_TRACE(ah->ah_sc);
3983
3984         tx_ctl = &desc->ud.ds_tx5212.tx_ctl;
3985         tx_status = &desc->ud.ds_tx5212.tx_stat;
3986
3987         /* No frame has been send or error */
3988         if (unlikely((tx_status->tx_status_1 & AR5K_DESC_TX_STATUS1_DONE) == 0))
3989                 return -EINPROGRESS;
3990
3991         /*
3992          * Get descriptor status
3993          */
3994         ts->ts_tstamp = AR5K_REG_MS(tx_status->tx_status_0,
3995                 AR5K_DESC_TX_STATUS0_SEND_TIMESTAMP);
3996         ts->ts_shortretry = AR5K_REG_MS(tx_status->tx_status_0,
3997                 AR5K_DESC_TX_STATUS0_SHORT_RETRY_COUNT);
3998         ts->ts_longretry = AR5K_REG_MS(tx_status->tx_status_0,
3999                 AR5K_DESC_TX_STATUS0_LONG_RETRY_COUNT);
4000         ts->ts_seqnum = AR5K_REG_MS(tx_status->tx_status_1,
4001                 AR5K_DESC_TX_STATUS1_SEQ_NUM);
4002         ts->ts_rssi = AR5K_REG_MS(tx_status->tx_status_1,
4003                 AR5K_DESC_TX_STATUS1_ACK_SIG_STRENGTH);
4004         ts->ts_antenna = (tx_status->tx_status_1 &
4005                 AR5K_DESC_TX_STATUS1_XMIT_ANTENNA) ? 2 : 1;
4006         ts->ts_status = 0;
4007
4008         switch (AR5K_REG_MS(tx_status->tx_status_1,
4009                         AR5K_DESC_TX_STATUS1_FINAL_TS_INDEX)) {
4010         case 0:
4011                 ts->ts_rate = tx_ctl->tx_control_3 &
4012                         AR5K_4W_TX_DESC_CTL3_XMIT_RATE0;
4013                 break;
4014         case 1:
4015                 ts->ts_rate = AR5K_REG_MS(tx_ctl->tx_control_3,
4016                         AR5K_4W_TX_DESC_CTL3_XMIT_RATE1);
4017                 ts->ts_longretry += AR5K_REG_MS(tx_ctl->tx_control_2,
4018                         AR5K_4W_TX_DESC_CTL2_XMIT_TRIES1);
4019                 break;
4020         case 2:
4021                 ts->ts_rate = AR5K_REG_MS(tx_ctl->tx_control_3,
4022                         AR5K_4W_TX_DESC_CTL3_XMIT_RATE2);
4023                 ts->ts_longretry += AR5K_REG_MS(tx_ctl->tx_control_2,
4024                         AR5K_4W_TX_DESC_CTL2_XMIT_TRIES2);
4025                 break;
4026         case 3:
4027                 ts->ts_rate = AR5K_REG_MS(tx_ctl->tx_control_3,
4028                         AR5K_4W_TX_DESC_CTL3_XMIT_RATE3);
4029                 ts->ts_longretry += AR5K_REG_MS(tx_ctl->tx_control_2,
4030                         AR5K_4W_TX_DESC_CTL2_XMIT_TRIES3);
4031                 break;
4032         }
4033
4034         if ((tx_status->tx_status_0 & AR5K_DESC_TX_STATUS0_FRAME_XMIT_OK) == 0){
4035                 if (tx_status->tx_status_0 &
4036                                 AR5K_DESC_TX_STATUS0_EXCESSIVE_RETRIES)
4037                         ts->ts_status |= AR5K_TXERR_XRETRY;
4038
4039                 if (tx_status->tx_status_0 & AR5K_DESC_TX_STATUS0_FIFO_UNDERRUN)
4040                         ts->ts_status |= AR5K_TXERR_FIFO;
4041
4042                 if (tx_status->tx_status_0 & AR5K_DESC_TX_STATUS0_FILTERED)
4043                         ts->ts_status |= AR5K_TXERR_FILT;
4044         }
4045
4046         return 0;
4047 }
4048
4049 /*
4050  * RX Descriptor
4051  */
4052
4053 /*
4054  * Initialize an rx descriptor
4055  */
4056 int ath5k_hw_setup_rx_desc(struct ath5k_hw *ah, struct ath5k_desc *desc,
4057                         u32 size, unsigned int flags)
4058 {
4059         struct ath5k_hw_rx_ctl *rx_ctl;
4060
4061         ATH5K_TRACE(ah->ah_sc);
4062         rx_ctl = &desc->ud.ds_rx.rx_ctl;
4063
4064         /*
4065          * Clear the descriptor
4066          * If we don't clean the status descriptor,
4067          * while scanning we get too many results,
4068          * most of them virtual, after some secs
4069          * of scanning system hangs. M.F.
4070         */
4071         memset(&desc->ud.ds_rx, 0, sizeof(struct ath5k_hw_all_rx_desc));
4072
4073         /* Setup descriptor */
4074         rx_ctl->rx_control_1 = size & AR5K_DESC_RX_CTL1_BUF_LEN;
4075         if (unlikely(rx_ctl->rx_control_1 != size))
4076                 return -EINVAL;
4077
4078         if (flags & AR5K_RXDESC_INTREQ)
4079                 rx_ctl->rx_control_1 |= AR5K_DESC_RX_CTL1_INTREQ;
4080
4081         return 0;
4082 }
4083
4084 /*
4085  * Proccess the rx status descriptor on 5210/5211
4086  */
4087 static int ath5k_hw_proc_5210_rx_status(struct ath5k_hw *ah,
4088                 struct ath5k_desc *desc, struct ath5k_rx_status *rs)
4089 {
4090         struct ath5k_hw_rx_status *rx_status;
4091
4092         rx_status = &desc->ud.ds_rx.u.rx_stat;
4093
4094         /* No frame received / not ready */
4095         if (unlikely((rx_status->rx_status_1 & AR5K_5210_RX_DESC_STATUS1_DONE)
4096                                 == 0))
4097                 return -EINPROGRESS;
4098
4099         /*
4100          * Frame receive status
4101          */
4102         rs->rs_datalen = rx_status->rx_status_0 &
4103                 AR5K_5210_RX_DESC_STATUS0_DATA_LEN;
4104         rs->rs_rssi = AR5K_REG_MS(rx_status->rx_status_0,
4105                 AR5K_5210_RX_DESC_STATUS0_RECEIVE_SIGNAL);
4106         rs->rs_rate = AR5K_REG_MS(rx_status->rx_status_0,
4107                 AR5K_5210_RX_DESC_STATUS0_RECEIVE_RATE);
4108         rs->rs_antenna = rx_status->rx_status_0 &
4109                 AR5K_5210_RX_DESC_STATUS0_RECEIVE_ANTENNA;
4110         rs->rs_more = rx_status->rx_status_0 &
4111                 AR5K_5210_RX_DESC_STATUS0_MORE;
4112         /* TODO: this timestamp is 13 bit, later on we assume 15 bit */
4113         rs->rs_tstamp = AR5K_REG_MS(rx_status->rx_status_1,
4114                 AR5K_5210_RX_DESC_STATUS1_RECEIVE_TIMESTAMP);
4115         rs->rs_status = 0;
4116
4117         /*
4118          * Key table status
4119          */
4120         if (rx_status->rx_status_1 & AR5K_5210_RX_DESC_STATUS1_KEY_INDEX_VALID)
4121                 rs->rs_keyix = AR5K_REG_MS(rx_status->rx_status_1,
4122                         AR5K_5210_RX_DESC_STATUS1_KEY_INDEX);
4123         else
4124                 rs->rs_keyix = AR5K_RXKEYIX_INVALID;
4125
4126         /*
4127          * Receive/descriptor errors
4128          */
4129         if ((rx_status->rx_status_1 &
4130                         AR5K_5210_RX_DESC_STATUS1_FRAME_RECEIVE_OK) == 0) {
4131                 if (rx_status->rx_status_1 &
4132                                 AR5K_5210_RX_DESC_STATUS1_CRC_ERROR)
4133                         rs->rs_status |= AR5K_RXERR_CRC;
4134
4135                 if (rx_status->rx_status_1 &
4136                                 AR5K_5210_RX_DESC_STATUS1_FIFO_OVERRUN)
4137                         rs->rs_status |= AR5K_RXERR_FIFO;
4138
4139                 if (rx_status->rx_status_1 &
4140                                 AR5K_5210_RX_DESC_STATUS1_PHY_ERROR) {
4141                         rs->rs_status |= AR5K_RXERR_PHY;
4142                         rs->rs_phyerr = AR5K_REG_MS(rx_status->rx_status_1,
4143                                            AR5K_5210_RX_DESC_STATUS1_PHY_ERROR);
4144                 }
4145
4146                 if (rx_status->rx_status_1 &
4147                                 AR5K_5210_RX_DESC_STATUS1_DECRYPT_CRC_ERROR)
4148                         rs->rs_status |= AR5K_RXERR_DECRYPT;
4149         }
4150
4151         return 0;
4152 }
4153
4154 /*
4155  * Proccess the rx status descriptor on 5212
4156  */
4157 static int ath5k_hw_proc_5212_rx_status(struct ath5k_hw *ah,
4158                 struct ath5k_desc *desc, struct ath5k_rx_status *rs)
4159 {
4160         struct ath5k_hw_rx_status *rx_status;
4161         struct ath5k_hw_rx_error *rx_err;
4162
4163         ATH5K_TRACE(ah->ah_sc);
4164         rx_status = &desc->ud.ds_rx.u.rx_stat;
4165
4166         /* Overlay on error */
4167         rx_err = &desc->ud.ds_rx.u.rx_err;
4168
4169         /* No frame received / not ready */
4170         if (unlikely((rx_status->rx_status_1 & AR5K_5212_RX_DESC_STATUS1_DONE)
4171                                 == 0))
4172                 return -EINPROGRESS;
4173
4174         /*
4175          * Frame receive status
4176          */
4177         rs->rs_datalen = rx_status->rx_status_0 &
4178                 AR5K_5212_RX_DESC_STATUS0_DATA_LEN;
4179         rs->rs_rssi = AR5K_REG_MS(rx_status->rx_status_0,
4180                 AR5K_5212_RX_DESC_STATUS0_RECEIVE_SIGNAL);
4181         rs->rs_rate = AR5K_REG_MS(rx_status->rx_status_0,
4182                 AR5K_5212_RX_DESC_STATUS0_RECEIVE_RATE);
4183         rs->rs_antenna = rx_status->rx_status_0 &
4184                 AR5K_5212_RX_DESC_STATUS0_RECEIVE_ANTENNA;
4185         rs->rs_more = rx_status->rx_status_0 &
4186                 AR5K_5212_RX_DESC_STATUS0_MORE;
4187         rs->rs_tstamp = AR5K_REG_MS(rx_status->rx_status_1,
4188                 AR5K_5212_RX_DESC_STATUS1_RECEIVE_TIMESTAMP);
4189         rs->rs_status = 0;
4190
4191         /*
4192          * Key table status
4193          */
4194         if (rx_status->rx_status_1 & AR5K_5212_RX_DESC_STATUS1_KEY_INDEX_VALID)
4195                 rs->rs_keyix = AR5K_REG_MS(rx_status->rx_status_1,
4196                                 AR5K_5212_RX_DESC_STATUS1_KEY_INDEX);
4197         else
4198                 rs->rs_keyix = AR5K_RXKEYIX_INVALID;
4199
4200         /*
4201          * Receive/descriptor errors
4202          */
4203         if ((rx_status->rx_status_1 &
4204                         AR5K_5212_RX_DESC_STATUS1_FRAME_RECEIVE_OK) == 0) {
4205                 if (rx_status->rx_status_1 &
4206                                 AR5K_5212_RX_DESC_STATUS1_CRC_ERROR)
4207                         rs->rs_status |= AR5K_RXERR_CRC;
4208
4209                 if (rx_status->rx_status_1 &
4210                                 AR5K_5212_RX_DESC_STATUS1_PHY_ERROR) {
4211                         rs->rs_status |= AR5K_RXERR_PHY;
4212                         rs->rs_phyerr = AR5K_REG_MS(rx_err->rx_error_1,
4213                                            AR5K_RX_DESC_ERROR1_PHY_ERROR_CODE);
4214                 }
4215
4216                 if (rx_status->rx_status_1 &
4217                                 AR5K_5212_RX_DESC_STATUS1_DECRYPT_CRC_ERROR)
4218                         rs->rs_status |= AR5K_RXERR_DECRYPT;
4219
4220                 if (rx_status->rx_status_1 &
4221                                 AR5K_5212_RX_DESC_STATUS1_MIC_ERROR)
4222                         rs->rs_status |= AR5K_RXERR_MIC;
4223         }
4224
4225         return 0;
4226 }
4227
4228
4229 /****************\
4230   GPIO Functions
4231 \****************/
4232
4233 /*
4234  * Set led state
4235  */
4236 void ath5k_hw_set_ledstate(struct ath5k_hw *ah, unsigned int state)
4237 {
4238         u32 led;
4239         /*5210 has different led mode handling*/
4240         u32 led_5210;
4241
4242         ATH5K_TRACE(ah->ah_sc);
4243
4244         /*Reset led status*/
4245         if (ah->ah_version != AR5K_AR5210)
4246                 AR5K_REG_DISABLE_BITS(ah, AR5K_PCICFG,
4247                         AR5K_PCICFG_LEDMODE |  AR5K_PCICFG_LED);
4248         else
4249                 AR5K_REG_DISABLE_BITS(ah, AR5K_PCICFG, AR5K_PCICFG_LED);
4250
4251         /*
4252          * Some blinking values, define at your wish
4253          */
4254         switch (state) {
4255         case AR5K_LED_SCAN:
4256         case AR5K_LED_AUTH:
4257                 led = AR5K_PCICFG_LEDMODE_PROP | AR5K_PCICFG_LED_PEND;
4258                 led_5210 = AR5K_PCICFG_LED_PEND | AR5K_PCICFG_LED_BCTL;
4259                 break;
4260
4261         case AR5K_LED_INIT:
4262                 led = AR5K_PCICFG_LEDMODE_PROP | AR5K_PCICFG_LED_NONE;
4263                 led_5210 = AR5K_PCICFG_LED_PEND;
4264                 break;
4265
4266         case AR5K_LED_ASSOC:
4267         case AR5K_LED_RUN:
4268                 led = AR5K_PCICFG_LEDMODE_PROP | AR5K_PCICFG_LED_ASSOC;
4269                 led_5210 = AR5K_PCICFG_LED_ASSOC;
4270                 break;
4271
4272         default:
4273                 led = AR5K_PCICFG_LEDMODE_PROM | AR5K_PCICFG_LED_NONE;
4274                 led_5210 = AR5K_PCICFG_LED_PEND;
4275                 break;
4276         }
4277
4278         /*Write new status to the register*/
4279         if (ah->ah_version != AR5K_AR5210)
4280                 AR5K_REG_ENABLE_BITS(ah, AR5K_PCICFG, led);
4281         else
4282                 AR5K_REG_ENABLE_BITS(ah, AR5K_PCICFG, led_5210);
4283 }
4284
4285 /*
4286  * Set GPIO outputs
4287  */
4288 int ath5k_hw_set_gpio_output(struct ath5k_hw *ah, u32 gpio)
4289 {
4290         ATH5K_TRACE(ah->ah_sc);
4291         if (gpio > AR5K_NUM_GPIO)
4292                 return -EINVAL;
4293
4294         ath5k_hw_reg_write(ah, (ath5k_hw_reg_read(ah, AR5K_GPIOCR) &~
4295                 AR5K_GPIOCR_OUT(gpio)) | AR5K_GPIOCR_OUT(gpio), AR5K_GPIOCR);
4296
4297         return 0;
4298 }
4299
4300 /*
4301  * Set GPIO inputs
4302  */
4303 int ath5k_hw_set_gpio_input(struct ath5k_hw *ah, u32 gpio)
4304 {
4305         ATH5K_TRACE(ah->ah_sc);
4306         if (gpio > AR5K_NUM_GPIO)
4307                 return -EINVAL;
4308
4309         ath5k_hw_reg_write(ah, (ath5k_hw_reg_read(ah, AR5K_GPIOCR) &~
4310                 AR5K_GPIOCR_OUT(gpio)) | AR5K_GPIOCR_IN(gpio), AR5K_GPIOCR);
4311
4312         return 0;
4313 }
4314
4315 /*
4316  * Get GPIO state
4317  */
4318 u32 ath5k_hw_get_gpio(struct ath5k_hw *ah, u32 gpio)
4319 {
4320         ATH5K_TRACE(ah->ah_sc);
4321         if (gpio > AR5K_NUM_GPIO)
4322                 return 0xffffffff;
4323
4324         /* GPIO input magic */
4325         return ((ath5k_hw_reg_read(ah, AR5K_GPIODI) & AR5K_GPIODI_M) >> gpio) &
4326                 0x1;
4327 }
4328
4329 /*
4330  * Set GPIO state
4331  */
4332 int ath5k_hw_set_gpio(struct ath5k_hw *ah, u32 gpio, u32 val)
4333 {
4334         u32 data;
4335         ATH5K_TRACE(ah->ah_sc);
4336
4337         if (gpio > AR5K_NUM_GPIO)
4338                 return -EINVAL;
4339
4340         /* GPIO output magic */
4341         data = ath5k_hw_reg_read(ah, AR5K_GPIODO);
4342
4343         data &= ~(1 << gpio);
4344         data |= (val & 1) << gpio;
4345
4346         ath5k_hw_reg_write(ah, data, AR5K_GPIODO);
4347
4348         return 0;
4349 }
4350
4351 /*
4352  * Initialize the GPIO interrupt (RFKill switch)
4353  */
4354 void ath5k_hw_set_gpio_intr(struct ath5k_hw *ah, unsigned int gpio,
4355                 u32 interrupt_level)
4356 {
4357         u32 data;
4358
4359         ATH5K_TRACE(ah->ah_sc);
4360         if (gpio > AR5K_NUM_GPIO)
4361                 return;
4362
4363         /*
4364          * Set the GPIO interrupt
4365          */
4366         data = (ath5k_hw_reg_read(ah, AR5K_GPIOCR) &
4367                 ~(AR5K_GPIOCR_INT_SEL(gpio) | AR5K_GPIOCR_INT_SELH |
4368                 AR5K_GPIOCR_INT_ENA | AR5K_GPIOCR_OUT(gpio))) |
4369                 (AR5K_GPIOCR_INT_SEL(gpio) | AR5K_GPIOCR_INT_ENA);
4370
4371         ath5k_hw_reg_write(ah, interrupt_level ? data :
4372                 (data | AR5K_GPIOCR_INT_SELH), AR5K_GPIOCR);
4373
4374         ah->ah_imr |= AR5K_IMR_GPIO;
4375
4376         /* Enable GPIO interrupts */
4377         AR5K_REG_ENABLE_BITS(ah, AR5K_PIMR, AR5K_IMR_GPIO);
4378 }
4379
4380
4381
4382
4383 /****************\
4384   Misc functions
4385 \****************/
4386
4387 int ath5k_hw_get_capability(struct ath5k_hw *ah,
4388                 enum ath5k_capability_type cap_type,
4389                 u32 capability, u32 *result)
4390 {
4391         ATH5K_TRACE(ah->ah_sc);
4392
4393         switch (cap_type) {
4394         case AR5K_CAP_NUM_TXQUEUES:
4395                 if (result) {
4396                         if (ah->ah_version == AR5K_AR5210)
4397                                 *result = AR5K_NUM_TX_QUEUES_NOQCU;
4398                         else
4399                                 *result = AR5K_NUM_TX_QUEUES;
4400                         goto yes;
4401                 }
4402         case AR5K_CAP_VEOL:
4403                 goto yes;
4404         case AR5K_CAP_COMPRESSION:
4405                 if (ah->ah_version == AR5K_AR5212)
4406                         goto yes;
4407                 else
4408                         goto no;
4409         case AR5K_CAP_BURST:
4410                 goto yes;
4411         case AR5K_CAP_TPC:
4412                 goto yes;
4413         case AR5K_CAP_BSSIDMASK:
4414                 if (ah->ah_version == AR5K_AR5212)
4415                         goto yes;
4416                 else
4417                         goto no;
4418         case AR5K_CAP_XR:
4419                 if (ah->ah_version == AR5K_AR5212)
4420                         goto yes;
4421                 else
4422                         goto no;
4423         default:
4424                 goto no;
4425         }
4426
4427 no:
4428         return -EINVAL;
4429 yes:
4430         return 0;
4431 }
4432
4433 static int ath5k_hw_enable_pspoll(struct ath5k_hw *ah, u8 *bssid,
4434                 u16 assoc_id)
4435 {
4436         ATH5K_TRACE(ah->ah_sc);
4437
4438         if (ah->ah_version == AR5K_AR5210) {
4439                 AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1,
4440                         AR5K_STA_ID1_NO_PSPOLL | AR5K_STA_ID1_DEFAULT_ANTENNA);
4441                 return 0;
4442         }
4443
4444         return -EIO;
4445 }
4446
4447 static int ath5k_hw_disable_pspoll(struct ath5k_hw *ah)
4448 {
4449         ATH5K_TRACE(ah->ah_sc);
4450
4451         if (ah->ah_version == AR5K_AR5210) {
4452                 AR5K_REG_ENABLE_BITS(ah, AR5K_STA_ID1,
4453                         AR5K_STA_ID1_NO_PSPOLL | AR5K_STA_ID1_DEFAULT_ANTENNA);
4454                 return 0;
4455         }
4456
4457         return -EIO;
4458 }