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