Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[linux-2.6.git] / drivers / net / wireless / rt2x00 / rt2800lib.c
1 /*
2         Copyright (C) 2010 Willow Garage <http://www.willowgarage.com>
3         Copyright (C) 2010 Ivo van Doorn <IvDoorn@gmail.com>
4         Copyright (C) 2009 Bartlomiej Zolnierkiewicz <bzolnier@gmail.com>
5         Copyright (C) 2009 Gertjan van Wingerde <gwingerde@gmail.com>
6
7         Based on the original rt2800pci.c and rt2800usb.c.
8           Copyright (C) 2009 Alban Browaeys <prahal@yahoo.com>
9           Copyright (C) 2009 Felix Fietkau <nbd@openwrt.org>
10           Copyright (C) 2009 Luis Correia <luis.f.correia@gmail.com>
11           Copyright (C) 2009 Mattias Nissler <mattias.nissler@gmx.de>
12           Copyright (C) 2009 Mark Asselstine <asselsm@gmail.com>
13           Copyright (C) 2009 Xose Vazquez Perez <xose.vazquez@gmail.com>
14           <http://rt2x00.serialmonkey.com>
15
16         This program is free software; you can redistribute it and/or modify
17         it under the terms of the GNU General Public License as published by
18         the Free Software Foundation; either version 2 of the License, or
19         (at your option) any later version.
20
21         This program is distributed in the hope that it will be useful,
22         but WITHOUT ANY WARRANTY; without even the implied warranty of
23         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24         GNU General Public License for more details.
25
26         You should have received a copy of the GNU General Public License
27         along with this program; if not, write to the
28         Free Software Foundation, Inc.,
29         59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30  */
31
32 /*
33         Module: rt2800lib
34         Abstract: rt2800 generic device routines.
35  */
36
37 #include <linux/crc-ccitt.h>
38 #include <linux/kernel.h>
39 #include <linux/module.h>
40 #include <linux/slab.h>
41
42 #include "rt2x00.h"
43 #include "rt2800lib.h"
44 #include "rt2800.h"
45
46 /*
47  * Register access.
48  * All access to the CSR registers will go through the methods
49  * rt2800_register_read and rt2800_register_write.
50  * BBP and RF register require indirect register access,
51  * and use the CSR registers BBPCSR and RFCSR to achieve this.
52  * These indirect registers work with busy bits,
53  * and we will try maximal REGISTER_BUSY_COUNT times to access
54  * the register while taking a REGISTER_BUSY_DELAY us delay
55  * between each attampt. When the busy bit is still set at that time,
56  * the access attempt is considered to have failed,
57  * and we will print an error.
58  * The _lock versions must be used if you already hold the csr_mutex
59  */
60 #define WAIT_FOR_BBP(__dev, __reg) \
61         rt2800_regbusy_read((__dev), BBP_CSR_CFG, BBP_CSR_CFG_BUSY, (__reg))
62 #define WAIT_FOR_RFCSR(__dev, __reg) \
63         rt2800_regbusy_read((__dev), RF_CSR_CFG, RF_CSR_CFG_BUSY, (__reg))
64 #define WAIT_FOR_RF(__dev, __reg) \
65         rt2800_regbusy_read((__dev), RF_CSR_CFG0, RF_CSR_CFG0_BUSY, (__reg))
66 #define WAIT_FOR_MCU(__dev, __reg) \
67         rt2800_regbusy_read((__dev), H2M_MAILBOX_CSR, \
68                             H2M_MAILBOX_CSR_OWNER, (__reg))
69
70 static inline bool rt2800_is_305x_soc(struct rt2x00_dev *rt2x00dev)
71 {
72         /* check for rt2872 on SoC */
73         if (!rt2x00_is_soc(rt2x00dev) ||
74             !rt2x00_rt(rt2x00dev, RT2872))
75                 return false;
76
77         /* we know for sure that these rf chipsets are used on rt305x boards */
78         if (rt2x00_rf(rt2x00dev, RF3020) ||
79             rt2x00_rf(rt2x00dev, RF3021) ||
80             rt2x00_rf(rt2x00dev, RF3022))
81                 return true;
82
83         NOTICE(rt2x00dev, "Unknown RF chipset on rt305x\n");
84         return false;
85 }
86
87 static void rt2800_bbp_write(struct rt2x00_dev *rt2x00dev,
88                              const unsigned int word, const u8 value)
89 {
90         u32 reg;
91
92         mutex_lock(&rt2x00dev->csr_mutex);
93
94         /*
95          * Wait until the BBP becomes available, afterwards we
96          * can safely write the new data into the register.
97          */
98         if (WAIT_FOR_BBP(rt2x00dev, &reg)) {
99                 reg = 0;
100                 rt2x00_set_field32(&reg, BBP_CSR_CFG_VALUE, value);
101                 rt2x00_set_field32(&reg, BBP_CSR_CFG_REGNUM, word);
102                 rt2x00_set_field32(&reg, BBP_CSR_CFG_BUSY, 1);
103                 rt2x00_set_field32(&reg, BBP_CSR_CFG_READ_CONTROL, 0);
104                 rt2x00_set_field32(&reg, BBP_CSR_CFG_BBP_RW_MODE, 1);
105
106                 rt2800_register_write_lock(rt2x00dev, BBP_CSR_CFG, reg);
107         }
108
109         mutex_unlock(&rt2x00dev->csr_mutex);
110 }
111
112 static void rt2800_bbp_read(struct rt2x00_dev *rt2x00dev,
113                             const unsigned int word, u8 *value)
114 {
115         u32 reg;
116
117         mutex_lock(&rt2x00dev->csr_mutex);
118
119         /*
120          * Wait until the BBP becomes available, afterwards we
121          * can safely write the read request into the register.
122          * After the data has been written, we wait until hardware
123          * returns the correct value, if at any time the register
124          * doesn't become available in time, reg will be 0xffffffff
125          * which means we return 0xff to the caller.
126          */
127         if (WAIT_FOR_BBP(rt2x00dev, &reg)) {
128                 reg = 0;
129                 rt2x00_set_field32(&reg, BBP_CSR_CFG_REGNUM, word);
130                 rt2x00_set_field32(&reg, BBP_CSR_CFG_BUSY, 1);
131                 rt2x00_set_field32(&reg, BBP_CSR_CFG_READ_CONTROL, 1);
132                 rt2x00_set_field32(&reg, BBP_CSR_CFG_BBP_RW_MODE, 1);
133
134                 rt2800_register_write_lock(rt2x00dev, BBP_CSR_CFG, reg);
135
136                 WAIT_FOR_BBP(rt2x00dev, &reg);
137         }
138
139         *value = rt2x00_get_field32(reg, BBP_CSR_CFG_VALUE);
140
141         mutex_unlock(&rt2x00dev->csr_mutex);
142 }
143
144 static void rt2800_rfcsr_write(struct rt2x00_dev *rt2x00dev,
145                                const unsigned int word, const u8 value)
146 {
147         u32 reg;
148
149         mutex_lock(&rt2x00dev->csr_mutex);
150
151         /*
152          * Wait until the RFCSR becomes available, afterwards we
153          * can safely write the new data into the register.
154          */
155         if (WAIT_FOR_RFCSR(rt2x00dev, &reg)) {
156                 reg = 0;
157                 rt2x00_set_field32(&reg, RF_CSR_CFG_DATA, value);
158                 rt2x00_set_field32(&reg, RF_CSR_CFG_REGNUM, word);
159                 rt2x00_set_field32(&reg, RF_CSR_CFG_WRITE, 1);
160                 rt2x00_set_field32(&reg, RF_CSR_CFG_BUSY, 1);
161
162                 rt2800_register_write_lock(rt2x00dev, RF_CSR_CFG, reg);
163         }
164
165         mutex_unlock(&rt2x00dev->csr_mutex);
166 }
167
168 static void rt2800_rfcsr_read(struct rt2x00_dev *rt2x00dev,
169                               const unsigned int word, u8 *value)
170 {
171         u32 reg;
172
173         mutex_lock(&rt2x00dev->csr_mutex);
174
175         /*
176          * Wait until the RFCSR becomes available, afterwards we
177          * can safely write the read request into the register.
178          * After the data has been written, we wait until hardware
179          * returns the correct value, if at any time the register
180          * doesn't become available in time, reg will be 0xffffffff
181          * which means we return 0xff to the caller.
182          */
183         if (WAIT_FOR_RFCSR(rt2x00dev, &reg)) {
184                 reg = 0;
185                 rt2x00_set_field32(&reg, RF_CSR_CFG_REGNUM, word);
186                 rt2x00_set_field32(&reg, RF_CSR_CFG_WRITE, 0);
187                 rt2x00_set_field32(&reg, RF_CSR_CFG_BUSY, 1);
188
189                 rt2800_register_write_lock(rt2x00dev, RF_CSR_CFG, reg);
190
191                 WAIT_FOR_RFCSR(rt2x00dev, &reg);
192         }
193
194         *value = rt2x00_get_field32(reg, RF_CSR_CFG_DATA);
195
196         mutex_unlock(&rt2x00dev->csr_mutex);
197 }
198
199 static void rt2800_rf_write(struct rt2x00_dev *rt2x00dev,
200                             const unsigned int word, const u32 value)
201 {
202         u32 reg;
203
204         mutex_lock(&rt2x00dev->csr_mutex);
205
206         /*
207          * Wait until the RF becomes available, afterwards we
208          * can safely write the new data into the register.
209          */
210         if (WAIT_FOR_RF(rt2x00dev, &reg)) {
211                 reg = 0;
212                 rt2x00_set_field32(&reg, RF_CSR_CFG0_REG_VALUE_BW, value);
213                 rt2x00_set_field32(&reg, RF_CSR_CFG0_STANDBYMODE, 0);
214                 rt2x00_set_field32(&reg, RF_CSR_CFG0_SEL, 0);
215                 rt2x00_set_field32(&reg, RF_CSR_CFG0_BUSY, 1);
216
217                 rt2800_register_write_lock(rt2x00dev, RF_CSR_CFG0, reg);
218                 rt2x00_rf_write(rt2x00dev, word, value);
219         }
220
221         mutex_unlock(&rt2x00dev->csr_mutex);
222 }
223
224 void rt2800_mcu_request(struct rt2x00_dev *rt2x00dev,
225                         const u8 command, const u8 token,
226                         const u8 arg0, const u8 arg1)
227 {
228         u32 reg;
229
230         /*
231          * SOC devices don't support MCU requests.
232          */
233         if (rt2x00_is_soc(rt2x00dev))
234                 return;
235
236         mutex_lock(&rt2x00dev->csr_mutex);
237
238         /*
239          * Wait until the MCU becomes available, afterwards we
240          * can safely write the new data into the register.
241          */
242         if (WAIT_FOR_MCU(rt2x00dev, &reg)) {
243                 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_OWNER, 1);
244                 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_CMD_TOKEN, token);
245                 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_ARG0, arg0);
246                 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_ARG1, arg1);
247                 rt2800_register_write_lock(rt2x00dev, H2M_MAILBOX_CSR, reg);
248
249                 reg = 0;
250                 rt2x00_set_field32(&reg, HOST_CMD_CSR_HOST_COMMAND, command);
251                 rt2800_register_write_lock(rt2x00dev, HOST_CMD_CSR, reg);
252         }
253
254         mutex_unlock(&rt2x00dev->csr_mutex);
255 }
256 EXPORT_SYMBOL_GPL(rt2800_mcu_request);
257
258 int rt2800_wait_csr_ready(struct rt2x00_dev *rt2x00dev)
259 {
260         unsigned int i = 0;
261         u32 reg;
262
263         for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
264                 rt2800_register_read(rt2x00dev, MAC_CSR0, &reg);
265                 if (reg && reg != ~0)
266                         return 0;
267                 msleep(1);
268         }
269
270         ERROR(rt2x00dev, "Unstable hardware.\n");
271         return -EBUSY;
272 }
273 EXPORT_SYMBOL_GPL(rt2800_wait_csr_ready);
274
275 int rt2800_wait_wpdma_ready(struct rt2x00_dev *rt2x00dev)
276 {
277         unsigned int i;
278         u32 reg;
279
280         /*
281          * Some devices are really slow to respond here. Wait a whole second
282          * before timing out.
283          */
284         for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
285                 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
286                 if (!rt2x00_get_field32(reg, WPDMA_GLO_CFG_TX_DMA_BUSY) &&
287                     !rt2x00_get_field32(reg, WPDMA_GLO_CFG_RX_DMA_BUSY))
288                         return 0;
289
290                 msleep(10);
291         }
292
293         ERROR(rt2x00dev, "WPDMA TX/RX busy, aborting.\n");
294         return -EACCES;
295 }
296 EXPORT_SYMBOL_GPL(rt2800_wait_wpdma_ready);
297
298 static bool rt2800_check_firmware_crc(const u8 *data, const size_t len)
299 {
300         u16 fw_crc;
301         u16 crc;
302
303         /*
304          * The last 2 bytes in the firmware array are the crc checksum itself,
305          * this means that we should never pass those 2 bytes to the crc
306          * algorithm.
307          */
308         fw_crc = (data[len - 2] << 8 | data[len - 1]);
309
310         /*
311          * Use the crc ccitt algorithm.
312          * This will return the same value as the legacy driver which
313          * used bit ordering reversion on the both the firmware bytes
314          * before input input as well as on the final output.
315          * Obviously using crc ccitt directly is much more efficient.
316          */
317         crc = crc_ccitt(~0, data, len - 2);
318
319         /*
320          * There is a small difference between the crc-itu-t + bitrev and
321          * the crc-ccitt crc calculation. In the latter method the 2 bytes
322          * will be swapped, use swab16 to convert the crc to the correct
323          * value.
324          */
325         crc = swab16(crc);
326
327         return fw_crc == crc;
328 }
329
330 int rt2800_check_firmware(struct rt2x00_dev *rt2x00dev,
331                           const u8 *data, const size_t len)
332 {
333         size_t offset = 0;
334         size_t fw_len;
335         bool multiple;
336
337         /*
338          * PCI(e) & SOC devices require firmware with a length
339          * of 8kb. USB devices require firmware files with a length
340          * of 4kb. Certain USB chipsets however require different firmware,
341          * which Ralink only provides attached to the original firmware
342          * file. Thus for USB devices, firmware files have a length
343          * which is a multiple of 4kb.
344          */
345         if (rt2x00_is_usb(rt2x00dev)) {
346                 fw_len = 4096;
347                 multiple = true;
348         } else {
349                 fw_len = 8192;
350                 multiple = true;
351         }
352
353         /*
354          * Validate the firmware length
355          */
356         if (len != fw_len && (!multiple || (len % fw_len) != 0))
357                 return FW_BAD_LENGTH;
358
359         /*
360          * Check if the chipset requires one of the upper parts
361          * of the firmware.
362          */
363         if (rt2x00_is_usb(rt2x00dev) &&
364             !rt2x00_rt(rt2x00dev, RT2860) &&
365             !rt2x00_rt(rt2x00dev, RT2872) &&
366             !rt2x00_rt(rt2x00dev, RT3070) &&
367             ((len / fw_len) == 1))
368                 return FW_BAD_VERSION;
369
370         /*
371          * 8kb firmware files must be checked as if it were
372          * 2 separate firmware files.
373          */
374         while (offset < len) {
375                 if (!rt2800_check_firmware_crc(data + offset, fw_len))
376                         return FW_BAD_CRC;
377
378                 offset += fw_len;
379         }
380
381         return FW_OK;
382 }
383 EXPORT_SYMBOL_GPL(rt2800_check_firmware);
384
385 int rt2800_load_firmware(struct rt2x00_dev *rt2x00dev,
386                          const u8 *data, const size_t len)
387 {
388         unsigned int i;
389         u32 reg;
390
391         /*
392          * If driver doesn't wake up firmware here,
393          * rt2800_load_firmware will hang forever when interface is up again.
394          */
395         rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, 0x00000000);
396
397         /*
398          * Wait for stable hardware.
399          */
400         if (rt2800_wait_csr_ready(rt2x00dev))
401                 return -EBUSY;
402
403         if (rt2x00_is_pci(rt2x00dev)) {
404                 if (rt2x00_rt(rt2x00dev, RT5390)) {
405                         rt2800_register_read(rt2x00dev, AUX_CTRL, &reg);
406                         rt2x00_set_field32(&reg, AUX_CTRL_FORCE_PCIE_CLK, 1);
407                         rt2x00_set_field32(&reg, AUX_CTRL_WAKE_PCIE_EN, 1);
408                         rt2800_register_write(rt2x00dev, AUX_CTRL, reg);
409                 }
410                 rt2800_register_write(rt2x00dev, PWR_PIN_CFG, 0x00000002);
411         }
412
413         /*
414          * Disable DMA, will be reenabled later when enabling
415          * the radio.
416          */
417         rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
418         rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
419         rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
420         rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
421         rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
422         rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
423         rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
424
425         /*
426          * Write firmware to the device.
427          */
428         rt2800_drv_write_firmware(rt2x00dev, data, len);
429
430         /*
431          * Wait for device to stabilize.
432          */
433         for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
434                 rt2800_register_read(rt2x00dev, PBF_SYS_CTRL, &reg);
435                 if (rt2x00_get_field32(reg, PBF_SYS_CTRL_READY))
436                         break;
437                 msleep(1);
438         }
439
440         if (i == REGISTER_BUSY_COUNT) {
441                 ERROR(rt2x00dev, "PBF system register not ready.\n");
442                 return -EBUSY;
443         }
444
445         /*
446          * Initialize firmware.
447          */
448         rt2800_register_write(rt2x00dev, H2M_BBP_AGENT, 0);
449         rt2800_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
450         msleep(1);
451
452         return 0;
453 }
454 EXPORT_SYMBOL_GPL(rt2800_load_firmware);
455
456 void rt2800_write_tx_data(struct queue_entry *entry,
457                           struct txentry_desc *txdesc)
458 {
459         __le32 *txwi = rt2800_drv_get_txwi(entry);
460         u32 word;
461
462         /*
463          * Initialize TX Info descriptor
464          */
465         rt2x00_desc_read(txwi, 0, &word);
466         rt2x00_set_field32(&word, TXWI_W0_FRAG,
467                            test_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags));
468         rt2x00_set_field32(&word, TXWI_W0_MIMO_PS,
469                            test_bit(ENTRY_TXD_HT_MIMO_PS, &txdesc->flags));
470         rt2x00_set_field32(&word, TXWI_W0_CF_ACK, 0);
471         rt2x00_set_field32(&word, TXWI_W0_TS,
472                            test_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags));
473         rt2x00_set_field32(&word, TXWI_W0_AMPDU,
474                            test_bit(ENTRY_TXD_HT_AMPDU, &txdesc->flags));
475         rt2x00_set_field32(&word, TXWI_W0_MPDU_DENSITY,
476                            txdesc->u.ht.mpdu_density);
477         rt2x00_set_field32(&word, TXWI_W0_TX_OP, txdesc->u.ht.txop);
478         rt2x00_set_field32(&word, TXWI_W0_MCS, txdesc->u.ht.mcs);
479         rt2x00_set_field32(&word, TXWI_W0_BW,
480                            test_bit(ENTRY_TXD_HT_BW_40, &txdesc->flags));
481         rt2x00_set_field32(&word, TXWI_W0_SHORT_GI,
482                            test_bit(ENTRY_TXD_HT_SHORT_GI, &txdesc->flags));
483         rt2x00_set_field32(&word, TXWI_W0_STBC, txdesc->u.ht.stbc);
484         rt2x00_set_field32(&word, TXWI_W0_PHYMODE, txdesc->rate_mode);
485         rt2x00_desc_write(txwi, 0, word);
486
487         rt2x00_desc_read(txwi, 1, &word);
488         rt2x00_set_field32(&word, TXWI_W1_ACK,
489                            test_bit(ENTRY_TXD_ACK, &txdesc->flags));
490         rt2x00_set_field32(&word, TXWI_W1_NSEQ,
491                            test_bit(ENTRY_TXD_GENERATE_SEQ, &txdesc->flags));
492         rt2x00_set_field32(&word, TXWI_W1_BW_WIN_SIZE, txdesc->u.ht.ba_size);
493         rt2x00_set_field32(&word, TXWI_W1_WIRELESS_CLI_ID,
494                            test_bit(ENTRY_TXD_ENCRYPT, &txdesc->flags) ?
495                            txdesc->key_idx : 0xff);
496         rt2x00_set_field32(&word, TXWI_W1_MPDU_TOTAL_BYTE_COUNT,
497                            txdesc->length);
498         rt2x00_set_field32(&word, TXWI_W1_PACKETID_QUEUE, entry->queue->qid);
499         rt2x00_set_field32(&word, TXWI_W1_PACKETID_ENTRY, (entry->entry_idx % 3) + 1);
500         rt2x00_desc_write(txwi, 1, word);
501
502         /*
503          * Always write 0 to IV/EIV fields, hardware will insert the IV
504          * from the IVEIV register when TXD_W3_WIV is set to 0.
505          * When TXD_W3_WIV is set to 1 it will use the IV data
506          * from the descriptor. The TXWI_W1_WIRELESS_CLI_ID indicates which
507          * crypto entry in the registers should be used to encrypt the frame.
508          */
509         _rt2x00_desc_write(txwi, 2, 0 /* skbdesc->iv[0] */);
510         _rt2x00_desc_write(txwi, 3, 0 /* skbdesc->iv[1] */);
511 }
512 EXPORT_SYMBOL_GPL(rt2800_write_tx_data);
513
514 static int rt2800_agc_to_rssi(struct rt2x00_dev *rt2x00dev, u32 rxwi_w2)
515 {
516         int rssi0 = rt2x00_get_field32(rxwi_w2, RXWI_W2_RSSI0);
517         int rssi1 = rt2x00_get_field32(rxwi_w2, RXWI_W2_RSSI1);
518         int rssi2 = rt2x00_get_field32(rxwi_w2, RXWI_W2_RSSI2);
519         u16 eeprom;
520         u8 offset0;
521         u8 offset1;
522         u8 offset2;
523
524         if (rt2x00dev->curr_band == IEEE80211_BAND_2GHZ) {
525                 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG, &eeprom);
526                 offset0 = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG_OFFSET0);
527                 offset1 = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG_OFFSET1);
528                 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &eeprom);
529                 offset2 = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG2_OFFSET2);
530         } else {
531                 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A, &eeprom);
532                 offset0 = rt2x00_get_field16(eeprom, EEPROM_RSSI_A_OFFSET0);
533                 offset1 = rt2x00_get_field16(eeprom, EEPROM_RSSI_A_OFFSET1);
534                 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &eeprom);
535                 offset2 = rt2x00_get_field16(eeprom, EEPROM_RSSI_A2_OFFSET2);
536         }
537
538         /*
539          * Convert the value from the descriptor into the RSSI value
540          * If the value in the descriptor is 0, it is considered invalid
541          * and the default (extremely low) rssi value is assumed
542          */
543         rssi0 = (rssi0) ? (-12 - offset0 - rt2x00dev->lna_gain - rssi0) : -128;
544         rssi1 = (rssi1) ? (-12 - offset1 - rt2x00dev->lna_gain - rssi1) : -128;
545         rssi2 = (rssi2) ? (-12 - offset2 - rt2x00dev->lna_gain - rssi2) : -128;
546
547         /*
548          * mac80211 only accepts a single RSSI value. Calculating the
549          * average doesn't deliver a fair answer either since -60:-60 would
550          * be considered equally good as -50:-70 while the second is the one
551          * which gives less energy...
552          */
553         rssi0 = max(rssi0, rssi1);
554         return max(rssi0, rssi2);
555 }
556
557 void rt2800_process_rxwi(struct queue_entry *entry,
558                          struct rxdone_entry_desc *rxdesc)
559 {
560         __le32 *rxwi = (__le32 *) entry->skb->data;
561         u32 word;
562
563         rt2x00_desc_read(rxwi, 0, &word);
564
565         rxdesc->cipher = rt2x00_get_field32(word, RXWI_W0_UDF);
566         rxdesc->size = rt2x00_get_field32(word, RXWI_W0_MPDU_TOTAL_BYTE_COUNT);
567
568         rt2x00_desc_read(rxwi, 1, &word);
569
570         if (rt2x00_get_field32(word, RXWI_W1_SHORT_GI))
571                 rxdesc->flags |= RX_FLAG_SHORT_GI;
572
573         if (rt2x00_get_field32(word, RXWI_W1_BW))
574                 rxdesc->flags |= RX_FLAG_40MHZ;
575
576         /*
577          * Detect RX rate, always use MCS as signal type.
578          */
579         rxdesc->dev_flags |= RXDONE_SIGNAL_MCS;
580         rxdesc->signal = rt2x00_get_field32(word, RXWI_W1_MCS);
581         rxdesc->rate_mode = rt2x00_get_field32(word, RXWI_W1_PHYMODE);
582
583         /*
584          * Mask of 0x8 bit to remove the short preamble flag.
585          */
586         if (rxdesc->rate_mode == RATE_MODE_CCK)
587                 rxdesc->signal &= ~0x8;
588
589         rt2x00_desc_read(rxwi, 2, &word);
590
591         /*
592          * Convert descriptor AGC value to RSSI value.
593          */
594         rxdesc->rssi = rt2800_agc_to_rssi(entry->queue->rt2x00dev, word);
595
596         /*
597          * Remove RXWI descriptor from start of buffer.
598          */
599         skb_pull(entry->skb, RXWI_DESC_SIZE);
600 }
601 EXPORT_SYMBOL_GPL(rt2800_process_rxwi);
602
603 static bool rt2800_txdone_entry_check(struct queue_entry *entry, u32 reg)
604 {
605         __le32 *txwi;
606         u32 word;
607         int wcid, ack, pid;
608         int tx_wcid, tx_ack, tx_pid;
609
610         wcid    = rt2x00_get_field32(reg, TX_STA_FIFO_WCID);
611         ack     = rt2x00_get_field32(reg, TX_STA_FIFO_TX_ACK_REQUIRED);
612         pid     = rt2x00_get_field32(reg, TX_STA_FIFO_PID_TYPE);
613
614         /*
615          * This frames has returned with an IO error,
616          * so the status report is not intended for this
617          * frame.
618          */
619         if (test_bit(ENTRY_DATA_IO_FAILED, &entry->flags)) {
620                 rt2x00lib_txdone_noinfo(entry, TXDONE_FAILURE);
621                 return false;
622         }
623
624         /*
625          * Validate if this TX status report is intended for
626          * this entry by comparing the WCID/ACK/PID fields.
627          */
628         txwi = rt2800_drv_get_txwi(entry);
629
630         rt2x00_desc_read(txwi, 1, &word);
631         tx_wcid = rt2x00_get_field32(word, TXWI_W1_WIRELESS_CLI_ID);
632         tx_ack  = rt2x00_get_field32(word, TXWI_W1_ACK);
633         tx_pid  = rt2x00_get_field32(word, TXWI_W1_PACKETID);
634
635         if ((wcid != tx_wcid) || (ack != tx_ack) || (pid != tx_pid)) {
636                 WARNING(entry->queue->rt2x00dev,
637                         "TX status report missed for queue %d entry %d\n",
638                 entry->queue->qid, entry->entry_idx);
639                 rt2x00lib_txdone_noinfo(entry, TXDONE_UNKNOWN);
640                 return false;
641         }
642
643         return true;
644 }
645
646 void rt2800_txdone_entry(struct queue_entry *entry, u32 status)
647 {
648         struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
649         struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
650         struct txdone_entry_desc txdesc;
651         u32 word;
652         u16 mcs, real_mcs;
653         int aggr, ampdu;
654         __le32 *txwi;
655
656         /*
657          * Obtain the status about this packet.
658          */
659         txdesc.flags = 0;
660         txwi = rt2800_drv_get_txwi(entry);
661         rt2x00_desc_read(txwi, 0, &word);
662
663         mcs = rt2x00_get_field32(word, TXWI_W0_MCS);
664         ampdu = rt2x00_get_field32(word, TXWI_W0_AMPDU);
665
666         real_mcs = rt2x00_get_field32(status, TX_STA_FIFO_MCS);
667         aggr = rt2x00_get_field32(status, TX_STA_FIFO_TX_AGGRE);
668
669         /*
670          * If a frame was meant to be sent as a single non-aggregated MPDU
671          * but ended up in an aggregate the used tx rate doesn't correlate
672          * with the one specified in the TXWI as the whole aggregate is sent
673          * with the same rate.
674          *
675          * For example: two frames are sent to rt2x00, the first one sets
676          * AMPDU=1 and requests MCS7 whereas the second frame sets AMDPU=0
677          * and requests MCS15. If the hw aggregates both frames into one
678          * AMDPU the tx status for both frames will contain MCS7 although
679          * the frame was sent successfully.
680          *
681          * Hence, replace the requested rate with the real tx rate to not
682          * confuse the rate control algortihm by providing clearly wrong
683          * data.
684          */
685         if (unlikely(aggr == 1 && ampdu == 0 && real_mcs != mcs)) {
686                 skbdesc->tx_rate_idx = real_mcs;
687                 mcs = real_mcs;
688         }
689
690         if (aggr == 1 || ampdu == 1)
691                 __set_bit(TXDONE_AMPDU, &txdesc.flags);
692
693         /*
694          * Ralink has a retry mechanism using a global fallback
695          * table. We setup this fallback table to try the immediate
696          * lower rate for all rates. In the TX_STA_FIFO, the MCS field
697          * always contains the MCS used for the last transmission, be
698          * it successful or not.
699          */
700         if (rt2x00_get_field32(status, TX_STA_FIFO_TX_SUCCESS)) {
701                 /*
702                  * Transmission succeeded. The number of retries is
703                  * mcs - real_mcs
704                  */
705                 __set_bit(TXDONE_SUCCESS, &txdesc.flags);
706                 txdesc.retry = ((mcs > real_mcs) ? mcs - real_mcs : 0);
707         } else {
708                 /*
709                  * Transmission failed. The number of retries is
710                  * always 7 in this case (for a total number of 8
711                  * frames sent).
712                  */
713                 __set_bit(TXDONE_FAILURE, &txdesc.flags);
714                 txdesc.retry = rt2x00dev->long_retry;
715         }
716
717         /*
718          * the frame was retried at least once
719          * -> hw used fallback rates
720          */
721         if (txdesc.retry)
722                 __set_bit(TXDONE_FALLBACK, &txdesc.flags);
723
724         rt2x00lib_txdone(entry, &txdesc);
725 }
726 EXPORT_SYMBOL_GPL(rt2800_txdone_entry);
727
728 void rt2800_txdone(struct rt2x00_dev *rt2x00dev)
729 {
730         struct data_queue *queue;
731         struct queue_entry *entry;
732         u32 reg;
733         u8 qid;
734
735         while (kfifo_get(&rt2x00dev->txstatus_fifo, &reg)) {
736
737                 /* TX_STA_FIFO_PID_QUEUE is a 2-bit field, thus
738                  * qid is guaranteed to be one of the TX QIDs
739                  */
740                 qid = rt2x00_get_field32(reg, TX_STA_FIFO_PID_QUEUE);
741                 queue = rt2x00queue_get_tx_queue(rt2x00dev, qid);
742                 if (unlikely(!queue)) {
743                         WARNING(rt2x00dev, "Got TX status for an unavailable "
744                                            "queue %u, dropping\n", qid);
745                         continue;
746                 }
747
748                 /*
749                  * Inside each queue, we process each entry in a chronological
750                  * order. We first check that the queue is not empty.
751                  */
752                 entry = NULL;
753                 while (!rt2x00queue_empty(queue)) {
754                         entry = rt2x00queue_get_entry(queue, Q_INDEX_DONE);
755                         if (rt2800_txdone_entry_check(entry, reg))
756                                 break;
757                 }
758
759                 if (!entry || rt2x00queue_empty(queue))
760                         break;
761
762                 rt2800_txdone_entry(entry, reg);
763         }
764 }
765 EXPORT_SYMBOL_GPL(rt2800_txdone);
766
767 void rt2800_write_beacon(struct queue_entry *entry, struct txentry_desc *txdesc)
768 {
769         struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
770         struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
771         unsigned int beacon_base;
772         unsigned int padding_len;
773         u32 orig_reg, reg;
774
775         /*
776          * Disable beaconing while we are reloading the beacon data,
777          * otherwise we might be sending out invalid data.
778          */
779         rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
780         orig_reg = reg;
781         rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
782         rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
783
784         /*
785          * Add space for the TXWI in front of the skb.
786          */
787         skb_push(entry->skb, TXWI_DESC_SIZE);
788         memset(entry->skb, 0, TXWI_DESC_SIZE);
789
790         /*
791          * Register descriptor details in skb frame descriptor.
792          */
793         skbdesc->flags |= SKBDESC_DESC_IN_SKB;
794         skbdesc->desc = entry->skb->data;
795         skbdesc->desc_len = TXWI_DESC_SIZE;
796
797         /*
798          * Add the TXWI for the beacon to the skb.
799          */
800         rt2800_write_tx_data(entry, txdesc);
801
802         /*
803          * Dump beacon to userspace through debugfs.
804          */
805         rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_BEACON, entry->skb);
806
807         /*
808          * Write entire beacon with TXWI and padding to register.
809          */
810         padding_len = roundup(entry->skb->len, 4) - entry->skb->len;
811         if (padding_len && skb_pad(entry->skb, padding_len)) {
812                 ERROR(rt2x00dev, "Failure padding beacon, aborting\n");
813                 /* skb freed by skb_pad() on failure */
814                 entry->skb = NULL;
815                 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, orig_reg);
816                 return;
817         }
818
819         beacon_base = HW_BEACON_OFFSET(entry->entry_idx);
820         rt2800_register_multiwrite(rt2x00dev, beacon_base, entry->skb->data,
821                                    entry->skb->len + padding_len);
822
823         /*
824          * Enable beaconing again.
825          */
826         rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 1);
827         rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
828
829         /*
830          * Clean up beacon skb.
831          */
832         dev_kfree_skb_any(entry->skb);
833         entry->skb = NULL;
834 }
835 EXPORT_SYMBOL_GPL(rt2800_write_beacon);
836
837 static inline void rt2800_clear_beacon_register(struct rt2x00_dev *rt2x00dev,
838                                                 unsigned int beacon_base)
839 {
840         int i;
841
842         /*
843          * For the Beacon base registers we only need to clear
844          * the whole TXWI which (when set to 0) will invalidate
845          * the entire beacon.
846          */
847         for (i = 0; i < TXWI_DESC_SIZE; i += sizeof(__le32))
848                 rt2800_register_write(rt2x00dev, beacon_base + i, 0);
849 }
850
851 void rt2800_clear_beacon(struct queue_entry *entry)
852 {
853         struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
854         u32 reg;
855
856         /*
857          * Disable beaconing while we are reloading the beacon data,
858          * otherwise we might be sending out invalid data.
859          */
860         rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
861         rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
862         rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
863
864         /*
865          * Clear beacon.
866          */
867         rt2800_clear_beacon_register(rt2x00dev,
868                                      HW_BEACON_OFFSET(entry->entry_idx));
869
870         /*
871          * Enabled beaconing again.
872          */
873         rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 1);
874         rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
875 }
876 EXPORT_SYMBOL_GPL(rt2800_clear_beacon);
877
878 #ifdef CONFIG_RT2X00_LIB_DEBUGFS
879 const struct rt2x00debug rt2800_rt2x00debug = {
880         .owner  = THIS_MODULE,
881         .csr    = {
882                 .read           = rt2800_register_read,
883                 .write          = rt2800_register_write,
884                 .flags          = RT2X00DEBUGFS_OFFSET,
885                 .word_base      = CSR_REG_BASE,
886                 .word_size      = sizeof(u32),
887                 .word_count     = CSR_REG_SIZE / sizeof(u32),
888         },
889         .eeprom = {
890                 .read           = rt2x00_eeprom_read,
891                 .write          = rt2x00_eeprom_write,
892                 .word_base      = EEPROM_BASE,
893                 .word_size      = sizeof(u16),
894                 .word_count     = EEPROM_SIZE / sizeof(u16),
895         },
896         .bbp    = {
897                 .read           = rt2800_bbp_read,
898                 .write          = rt2800_bbp_write,
899                 .word_base      = BBP_BASE,
900                 .word_size      = sizeof(u8),
901                 .word_count     = BBP_SIZE / sizeof(u8),
902         },
903         .rf     = {
904                 .read           = rt2x00_rf_read,
905                 .write          = rt2800_rf_write,
906                 .word_base      = RF_BASE,
907                 .word_size      = sizeof(u32),
908                 .word_count     = RF_SIZE / sizeof(u32),
909         },
910 };
911 EXPORT_SYMBOL_GPL(rt2800_rt2x00debug);
912 #endif /* CONFIG_RT2X00_LIB_DEBUGFS */
913
914 int rt2800_rfkill_poll(struct rt2x00_dev *rt2x00dev)
915 {
916         u32 reg;
917
918         rt2800_register_read(rt2x00dev, GPIO_CTRL_CFG, &reg);
919         return rt2x00_get_field32(reg, GPIO_CTRL_CFG_BIT2);
920 }
921 EXPORT_SYMBOL_GPL(rt2800_rfkill_poll);
922
923 #ifdef CONFIG_RT2X00_LIB_LEDS
924 static void rt2800_brightness_set(struct led_classdev *led_cdev,
925                                   enum led_brightness brightness)
926 {
927         struct rt2x00_led *led =
928             container_of(led_cdev, struct rt2x00_led, led_dev);
929         unsigned int enabled = brightness != LED_OFF;
930         unsigned int bg_mode =
931             (enabled && led->rt2x00dev->curr_band == IEEE80211_BAND_2GHZ);
932         unsigned int polarity =
933                 rt2x00_get_field16(led->rt2x00dev->led_mcu_reg,
934                                    EEPROM_FREQ_LED_POLARITY);
935         unsigned int ledmode =
936                 rt2x00_get_field16(led->rt2x00dev->led_mcu_reg,
937                                    EEPROM_FREQ_LED_MODE);
938         u32 reg;
939
940         /* Check for SoC (SOC devices don't support MCU requests) */
941         if (rt2x00_is_soc(led->rt2x00dev)) {
942                 rt2800_register_read(led->rt2x00dev, LED_CFG, &reg);
943
944                 /* Set LED Polarity */
945                 rt2x00_set_field32(&reg, LED_CFG_LED_POLAR, polarity);
946
947                 /* Set LED Mode */
948                 if (led->type == LED_TYPE_RADIO) {
949                         rt2x00_set_field32(&reg, LED_CFG_G_LED_MODE,
950                                            enabled ? 3 : 0);
951                 } else if (led->type == LED_TYPE_ASSOC) {
952                         rt2x00_set_field32(&reg, LED_CFG_Y_LED_MODE,
953                                            enabled ? 3 : 0);
954                 } else if (led->type == LED_TYPE_QUALITY) {
955                         rt2x00_set_field32(&reg, LED_CFG_R_LED_MODE,
956                                            enabled ? 3 : 0);
957                 }
958
959                 rt2800_register_write(led->rt2x00dev, LED_CFG, reg);
960
961         } else {
962                 if (led->type == LED_TYPE_RADIO) {
963                         rt2800_mcu_request(led->rt2x00dev, MCU_LED, 0xff, ledmode,
964                                               enabled ? 0x20 : 0);
965                 } else if (led->type == LED_TYPE_ASSOC) {
966                         rt2800_mcu_request(led->rt2x00dev, MCU_LED, 0xff, ledmode,
967                                               enabled ? (bg_mode ? 0x60 : 0xa0) : 0x20);
968                 } else if (led->type == LED_TYPE_QUALITY) {
969                         /*
970                          * The brightness is divided into 6 levels (0 - 5),
971                          * The specs tell us the following levels:
972                          *      0, 1 ,3, 7, 15, 31
973                          * to determine the level in a simple way we can simply
974                          * work with bitshifting:
975                          *      (1 << level) - 1
976                          */
977                         rt2800_mcu_request(led->rt2x00dev, MCU_LED_STRENGTH, 0xff,
978                                               (1 << brightness / (LED_FULL / 6)) - 1,
979                                               polarity);
980                 }
981         }
982 }
983
984 static int rt2800_blink_set(struct led_classdev *led_cdev,
985                             unsigned long *delay_on, unsigned long *delay_off)
986 {
987         struct rt2x00_led *led =
988             container_of(led_cdev, struct rt2x00_led, led_dev);
989         u32 reg;
990
991         rt2800_register_read(led->rt2x00dev, LED_CFG, &reg);
992         rt2x00_set_field32(&reg, LED_CFG_ON_PERIOD, *delay_on);
993         rt2x00_set_field32(&reg, LED_CFG_OFF_PERIOD, *delay_off);
994         rt2800_register_write(led->rt2x00dev, LED_CFG, reg);
995
996         return 0;
997 }
998
999 static void rt2800_init_led(struct rt2x00_dev *rt2x00dev,
1000                      struct rt2x00_led *led, enum led_type type)
1001 {
1002         led->rt2x00dev = rt2x00dev;
1003         led->type = type;
1004         led->led_dev.brightness_set = rt2800_brightness_set;
1005         led->led_dev.blink_set = rt2800_blink_set;
1006         led->flags = LED_INITIALIZED;
1007 }
1008 #endif /* CONFIG_RT2X00_LIB_LEDS */
1009
1010 /*
1011  * Configuration handlers.
1012  */
1013 static void rt2800_config_wcid_attr(struct rt2x00_dev *rt2x00dev,
1014                                     struct rt2x00lib_crypto *crypto,
1015                                     struct ieee80211_key_conf *key)
1016 {
1017         struct mac_wcid_entry wcid_entry;
1018         struct mac_iveiv_entry iveiv_entry;
1019         u32 offset;
1020         u32 reg;
1021
1022         offset = MAC_WCID_ATTR_ENTRY(key->hw_key_idx);
1023
1024         if (crypto->cmd == SET_KEY) {
1025                 rt2800_register_read(rt2x00dev, offset, &reg);
1026                 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_KEYTAB,
1027                                    !!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE));
1028                 /*
1029                  * Both the cipher as the BSS Idx numbers are split in a main
1030                  * value of 3 bits, and a extended field for adding one additional
1031                  * bit to the value.
1032                  */
1033                 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_CIPHER,
1034                                    (crypto->cipher & 0x7));
1035                 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_CIPHER_EXT,
1036                                    (crypto->cipher & 0x8) >> 3);
1037                 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_BSS_IDX,
1038                                    (crypto->bssidx & 0x7));
1039                 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_BSS_IDX_EXT,
1040                                    (crypto->bssidx & 0x8) >> 3);
1041                 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_RX_WIUDF, crypto->cipher);
1042                 rt2800_register_write(rt2x00dev, offset, reg);
1043         } else {
1044                 rt2800_register_write(rt2x00dev, offset, 0);
1045         }
1046
1047         offset = MAC_IVEIV_ENTRY(key->hw_key_idx);
1048
1049         memset(&iveiv_entry, 0, sizeof(iveiv_entry));
1050         if ((crypto->cipher == CIPHER_TKIP) ||
1051             (crypto->cipher == CIPHER_TKIP_NO_MIC) ||
1052             (crypto->cipher == CIPHER_AES))
1053                 iveiv_entry.iv[3] |= 0x20;
1054         iveiv_entry.iv[3] |= key->keyidx << 6;
1055         rt2800_register_multiwrite(rt2x00dev, offset,
1056                                       &iveiv_entry, sizeof(iveiv_entry));
1057
1058         offset = MAC_WCID_ENTRY(key->hw_key_idx);
1059
1060         memset(&wcid_entry, 0, sizeof(wcid_entry));
1061         if (crypto->cmd == SET_KEY)
1062                 memcpy(wcid_entry.mac, crypto->address, ETH_ALEN);
1063         rt2800_register_multiwrite(rt2x00dev, offset,
1064                                       &wcid_entry, sizeof(wcid_entry));
1065 }
1066
1067 int rt2800_config_shared_key(struct rt2x00_dev *rt2x00dev,
1068                              struct rt2x00lib_crypto *crypto,
1069                              struct ieee80211_key_conf *key)
1070 {
1071         struct hw_key_entry key_entry;
1072         struct rt2x00_field32 field;
1073         u32 offset;
1074         u32 reg;
1075
1076         if (crypto->cmd == SET_KEY) {
1077                 key->hw_key_idx = (4 * crypto->bssidx) + key->keyidx;
1078
1079                 memcpy(key_entry.key, crypto->key,
1080                        sizeof(key_entry.key));
1081                 memcpy(key_entry.tx_mic, crypto->tx_mic,
1082                        sizeof(key_entry.tx_mic));
1083                 memcpy(key_entry.rx_mic, crypto->rx_mic,
1084                        sizeof(key_entry.rx_mic));
1085
1086                 offset = SHARED_KEY_ENTRY(key->hw_key_idx);
1087                 rt2800_register_multiwrite(rt2x00dev, offset,
1088                                               &key_entry, sizeof(key_entry));
1089         }
1090
1091         /*
1092          * The cipher types are stored over multiple registers
1093          * starting with SHARED_KEY_MODE_BASE each word will have
1094          * 32 bits and contains the cipher types for 2 bssidx each.
1095          * Using the correct defines correctly will cause overhead,
1096          * so just calculate the correct offset.
1097          */
1098         field.bit_offset = 4 * (key->hw_key_idx % 8);
1099         field.bit_mask = 0x7 << field.bit_offset;
1100
1101         offset = SHARED_KEY_MODE_ENTRY(key->hw_key_idx / 8);
1102
1103         rt2800_register_read(rt2x00dev, offset, &reg);
1104         rt2x00_set_field32(&reg, field,
1105                            (crypto->cmd == SET_KEY) * crypto->cipher);
1106         rt2800_register_write(rt2x00dev, offset, reg);
1107
1108         /*
1109          * Update WCID information
1110          */
1111         rt2800_config_wcid_attr(rt2x00dev, crypto, key);
1112
1113         return 0;
1114 }
1115 EXPORT_SYMBOL_GPL(rt2800_config_shared_key);
1116
1117 static inline int rt2800_find_pairwise_keyslot(struct rt2x00_dev *rt2x00dev)
1118 {
1119         int idx;
1120         u32 offset, reg;
1121
1122         /*
1123          * Search for the first free pairwise key entry and return the
1124          * corresponding index.
1125          *
1126          * Make sure the WCID starts _after_ the last possible shared key
1127          * entry (>32).
1128          *
1129          * Since parts of the pairwise key table might be shared with
1130          * the beacon frame buffers 6 & 7 we should only write into the
1131          * first 222 entries.
1132          */
1133         for (idx = 33; idx <= 222; idx++) {
1134                 offset = MAC_WCID_ATTR_ENTRY(idx);
1135                 rt2800_register_read(rt2x00dev, offset, &reg);
1136                 if (!reg)
1137                         return idx;
1138         }
1139         return -1;
1140 }
1141
1142 int rt2800_config_pairwise_key(struct rt2x00_dev *rt2x00dev,
1143                                struct rt2x00lib_crypto *crypto,
1144                                struct ieee80211_key_conf *key)
1145 {
1146         struct hw_key_entry key_entry;
1147         u32 offset;
1148         int idx;
1149
1150         if (crypto->cmd == SET_KEY) {
1151                 idx = rt2800_find_pairwise_keyslot(rt2x00dev);
1152                 if (idx < 0)
1153                         return -ENOSPC;
1154                 key->hw_key_idx = idx;
1155
1156                 memcpy(key_entry.key, crypto->key,
1157                        sizeof(key_entry.key));
1158                 memcpy(key_entry.tx_mic, crypto->tx_mic,
1159                        sizeof(key_entry.tx_mic));
1160                 memcpy(key_entry.rx_mic, crypto->rx_mic,
1161                        sizeof(key_entry.rx_mic));
1162
1163                 offset = PAIRWISE_KEY_ENTRY(key->hw_key_idx);
1164                 rt2800_register_multiwrite(rt2x00dev, offset,
1165                                               &key_entry, sizeof(key_entry));
1166         }
1167
1168         /*
1169          * Update WCID information
1170          */
1171         rt2800_config_wcid_attr(rt2x00dev, crypto, key);
1172
1173         return 0;
1174 }
1175 EXPORT_SYMBOL_GPL(rt2800_config_pairwise_key);
1176
1177 void rt2800_config_filter(struct rt2x00_dev *rt2x00dev,
1178                           const unsigned int filter_flags)
1179 {
1180         u32 reg;
1181
1182         /*
1183          * Start configuration steps.
1184          * Note that the version error will always be dropped
1185          * and broadcast frames will always be accepted since
1186          * there is no filter for it at this time.
1187          */
1188         rt2800_register_read(rt2x00dev, RX_FILTER_CFG, &reg);
1189         rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CRC_ERROR,
1190                            !(filter_flags & FIF_FCSFAIL));
1191         rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_PHY_ERROR,
1192                            !(filter_flags & FIF_PLCPFAIL));
1193         rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_NOT_TO_ME,
1194                            !(filter_flags & FIF_PROMISC_IN_BSS));
1195         rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_NOT_MY_BSSD, 0);
1196         rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_VER_ERROR, 1);
1197         rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_MULTICAST,
1198                            !(filter_flags & FIF_ALLMULTI));
1199         rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BROADCAST, 0);
1200         rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_DUPLICATE, 1);
1201         rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CF_END_ACK,
1202                            !(filter_flags & FIF_CONTROL));
1203         rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CF_END,
1204                            !(filter_flags & FIF_CONTROL));
1205         rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_ACK,
1206                            !(filter_flags & FIF_CONTROL));
1207         rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CTS,
1208                            !(filter_flags & FIF_CONTROL));
1209         rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_RTS,
1210                            !(filter_flags & FIF_CONTROL));
1211         rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_PSPOLL,
1212                            !(filter_flags & FIF_PSPOLL));
1213         rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BA, 1);
1214         rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BAR, 0);
1215         rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CNTL,
1216                            !(filter_flags & FIF_CONTROL));
1217         rt2800_register_write(rt2x00dev, RX_FILTER_CFG, reg);
1218 }
1219 EXPORT_SYMBOL_GPL(rt2800_config_filter);
1220
1221 void rt2800_config_intf(struct rt2x00_dev *rt2x00dev, struct rt2x00_intf *intf,
1222                         struct rt2x00intf_conf *conf, const unsigned int flags)
1223 {
1224         u32 reg;
1225         bool update_bssid = false;
1226
1227         if (flags & CONFIG_UPDATE_TYPE) {
1228                 /*
1229                  * Enable synchronisation.
1230                  */
1231                 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
1232                 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_SYNC, conf->sync);
1233                 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
1234
1235                 if (conf->sync == TSF_SYNC_AP_NONE) {
1236                         /*
1237                          * Tune beacon queue transmit parameters for AP mode
1238                          */
1239                         rt2800_register_read(rt2x00dev, TBTT_SYNC_CFG, &reg);
1240                         rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_CWMIN, 0);
1241                         rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_AIFSN, 1);
1242                         rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_EXP_WIN, 32);
1243                         rt2x00_set_field32(&reg, TBTT_SYNC_CFG_TBTT_ADJUST, 0);
1244                         rt2800_register_write(rt2x00dev, TBTT_SYNC_CFG, reg);
1245                 } else {
1246                         rt2800_register_read(rt2x00dev, TBTT_SYNC_CFG, &reg);
1247                         rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_CWMIN, 4);
1248                         rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_AIFSN, 2);
1249                         rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_EXP_WIN, 32);
1250                         rt2x00_set_field32(&reg, TBTT_SYNC_CFG_TBTT_ADJUST, 16);
1251                         rt2800_register_write(rt2x00dev, TBTT_SYNC_CFG, reg);
1252                 }
1253         }
1254
1255         if (flags & CONFIG_UPDATE_MAC) {
1256                 if (flags & CONFIG_UPDATE_TYPE &&
1257                     conf->sync == TSF_SYNC_AP_NONE) {
1258                         /*
1259                          * The BSSID register has to be set to our own mac
1260                          * address in AP mode.
1261                          */
1262                         memcpy(conf->bssid, conf->mac, sizeof(conf->mac));
1263                         update_bssid = true;
1264                 }
1265
1266                 if (!is_zero_ether_addr((const u8 *)conf->mac)) {
1267                         reg = le32_to_cpu(conf->mac[1]);
1268                         rt2x00_set_field32(&reg, MAC_ADDR_DW1_UNICAST_TO_ME_MASK, 0xff);
1269                         conf->mac[1] = cpu_to_le32(reg);
1270                 }
1271
1272                 rt2800_register_multiwrite(rt2x00dev, MAC_ADDR_DW0,
1273                                               conf->mac, sizeof(conf->mac));
1274         }
1275
1276         if ((flags & CONFIG_UPDATE_BSSID) || update_bssid) {
1277                 if (!is_zero_ether_addr((const u8 *)conf->bssid)) {
1278                         reg = le32_to_cpu(conf->bssid[1]);
1279                         rt2x00_set_field32(&reg, MAC_BSSID_DW1_BSS_ID_MASK, 3);
1280                         rt2x00_set_field32(&reg, MAC_BSSID_DW1_BSS_BCN_NUM, 7);
1281                         conf->bssid[1] = cpu_to_le32(reg);
1282                 }
1283
1284                 rt2800_register_multiwrite(rt2x00dev, MAC_BSSID_DW0,
1285                                               conf->bssid, sizeof(conf->bssid));
1286         }
1287 }
1288 EXPORT_SYMBOL_GPL(rt2800_config_intf);
1289
1290 static void rt2800_config_ht_opmode(struct rt2x00_dev *rt2x00dev,
1291                                     struct rt2x00lib_erp *erp)
1292 {
1293         bool any_sta_nongf = !!(erp->ht_opmode &
1294                                 IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
1295         u8 protection = erp->ht_opmode & IEEE80211_HT_OP_MODE_PROTECTION;
1296         u8 mm20_mode, mm40_mode, gf20_mode, gf40_mode;
1297         u16 mm20_rate, mm40_rate, gf20_rate, gf40_rate;
1298         u32 reg;
1299
1300         /* default protection rate for HT20: OFDM 24M */
1301         mm20_rate = gf20_rate = 0x4004;
1302
1303         /* default protection rate for HT40: duplicate OFDM 24M */
1304         mm40_rate = gf40_rate = 0x4084;
1305
1306         switch (protection) {
1307         case IEEE80211_HT_OP_MODE_PROTECTION_NONE:
1308                 /*
1309                  * All STAs in this BSS are HT20/40 but there might be
1310                  * STAs not supporting greenfield mode.
1311                  * => Disable protection for HT transmissions.
1312                  */
1313                 mm20_mode = mm40_mode = gf20_mode = gf40_mode = 0;
1314
1315                 break;
1316         case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
1317                 /*
1318                  * All STAs in this BSS are HT20 or HT20/40 but there
1319                  * might be STAs not supporting greenfield mode.
1320                  * => Protect all HT40 transmissions.
1321                  */
1322                 mm20_mode = gf20_mode = 0;
1323                 mm40_mode = gf40_mode = 2;
1324
1325                 break;
1326         case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:
1327                 /*
1328                  * Nonmember protection:
1329                  * According to 802.11n we _should_ protect all
1330                  * HT transmissions (but we don't have to).
1331                  *
1332                  * But if cts_protection is enabled we _shall_ protect
1333                  * all HT transmissions using a CCK rate.
1334                  *
1335                  * And if any station is non GF we _shall_ protect
1336                  * GF transmissions.
1337                  *
1338                  * We decide to protect everything
1339                  * -> fall through to mixed mode.
1340                  */
1341         case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
1342                 /*
1343                  * Legacy STAs are present
1344                  * => Protect all HT transmissions.
1345                  */
1346                 mm20_mode = mm40_mode = gf20_mode = gf40_mode = 2;
1347
1348                 /*
1349                  * If erp protection is needed we have to protect HT
1350                  * transmissions with CCK 11M long preamble.
1351                  */
1352                 if (erp->cts_protection) {
1353                         /* don't duplicate RTS/CTS in CCK mode */
1354                         mm20_rate = mm40_rate = 0x0003;
1355                         gf20_rate = gf40_rate = 0x0003;
1356                 }
1357                 break;
1358         };
1359
1360         /* check for STAs not supporting greenfield mode */
1361         if (any_sta_nongf)
1362                 gf20_mode = gf40_mode = 2;
1363
1364         /* Update HT protection config */
1365         rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
1366         rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_RATE, mm20_rate);
1367         rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_CTRL, mm20_mode);
1368         rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
1369
1370         rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
1371         rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_RATE, mm40_rate);
1372         rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_CTRL, mm40_mode);
1373         rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
1374
1375         rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
1376         rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_RATE, gf20_rate);
1377         rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_CTRL, gf20_mode);
1378         rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
1379
1380         rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
1381         rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_RATE, gf40_rate);
1382         rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_CTRL, gf40_mode);
1383         rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
1384 }
1385
1386 void rt2800_config_erp(struct rt2x00_dev *rt2x00dev, struct rt2x00lib_erp *erp,
1387                        u32 changed)
1388 {
1389         u32 reg;
1390
1391         if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1392                 rt2800_register_read(rt2x00dev, AUTO_RSP_CFG, &reg);
1393                 rt2x00_set_field32(&reg, AUTO_RSP_CFG_BAC_ACK_POLICY,
1394                                    !!erp->short_preamble);
1395                 rt2x00_set_field32(&reg, AUTO_RSP_CFG_AR_PREAMBLE,
1396                                    !!erp->short_preamble);
1397                 rt2800_register_write(rt2x00dev, AUTO_RSP_CFG, reg);
1398         }
1399
1400         if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1401                 rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
1402                 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_CTRL,
1403                                    erp->cts_protection ? 2 : 0);
1404                 rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
1405         }
1406
1407         if (changed & BSS_CHANGED_BASIC_RATES) {
1408                 rt2800_register_write(rt2x00dev, LEGACY_BASIC_RATE,
1409                                          erp->basic_rates);
1410                 rt2800_register_write(rt2x00dev, HT_BASIC_RATE, 0x00008003);
1411         }
1412
1413         if (changed & BSS_CHANGED_ERP_SLOT) {
1414                 rt2800_register_read(rt2x00dev, BKOFF_SLOT_CFG, &reg);
1415                 rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_SLOT_TIME,
1416                                    erp->slot_time);
1417                 rt2800_register_write(rt2x00dev, BKOFF_SLOT_CFG, reg);
1418
1419                 rt2800_register_read(rt2x00dev, XIFS_TIME_CFG, &reg);
1420                 rt2x00_set_field32(&reg, XIFS_TIME_CFG_EIFS, erp->eifs);
1421                 rt2800_register_write(rt2x00dev, XIFS_TIME_CFG, reg);
1422         }
1423
1424         if (changed & BSS_CHANGED_BEACON_INT) {
1425                 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
1426                 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_INTERVAL,
1427                                    erp->beacon_int * 16);
1428                 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
1429         }
1430
1431         if (changed & BSS_CHANGED_HT)
1432                 rt2800_config_ht_opmode(rt2x00dev, erp);
1433 }
1434 EXPORT_SYMBOL_GPL(rt2800_config_erp);
1435
1436 static void rt2800_set_ant_diversity(struct rt2x00_dev *rt2x00dev,
1437                                      enum antenna ant)
1438 {
1439         u32 reg;
1440         u8 eesk_pin = (ant == ANTENNA_A) ? 1 : 0;
1441         u8 gpio_bit3 = (ant == ANTENNA_A) ? 0 : 1;
1442
1443         if (rt2x00_is_pci(rt2x00dev)) {
1444                 rt2800_register_read(rt2x00dev, E2PROM_CSR, &reg);
1445                 rt2x00_set_field32(&reg, E2PROM_CSR_DATA_CLOCK, eesk_pin);
1446                 rt2800_register_write(rt2x00dev, E2PROM_CSR, reg);
1447         } else if (rt2x00_is_usb(rt2x00dev))
1448                 rt2800_mcu_request(rt2x00dev, MCU_ANT_SELECT, 0xff,
1449                                    eesk_pin, 0);
1450
1451         rt2800_register_read(rt2x00dev, GPIO_CTRL_CFG, &reg);
1452         rt2x00_set_field32(&reg, GPIO_CTRL_CFG_GPIOD_BIT3, 0);
1453         rt2x00_set_field32(&reg, GPIO_CTRL_CFG_BIT3, gpio_bit3);
1454         rt2800_register_write(rt2x00dev, GPIO_CTRL_CFG, reg);
1455 }
1456
1457 void rt2800_config_ant(struct rt2x00_dev *rt2x00dev, struct antenna_setup *ant)
1458 {
1459         u8 r1;
1460         u8 r3;
1461         u16 eeprom;
1462
1463         rt2800_bbp_read(rt2x00dev, 1, &r1);
1464         rt2800_bbp_read(rt2x00dev, 3, &r3);
1465
1466         /*
1467          * Configure the TX antenna.
1468          */
1469         switch (ant->tx_chain_num) {
1470         case 1:
1471                 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 0);
1472                 break;
1473         case 2:
1474                 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 2);
1475                 break;
1476         case 3:
1477                 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 0);
1478                 break;
1479         }
1480
1481         /*
1482          * Configure the RX antenna.
1483          */
1484         switch (ant->rx_chain_num) {
1485         case 1:
1486                 if (rt2x00_rt(rt2x00dev, RT3070) ||
1487                     rt2x00_rt(rt2x00dev, RT3090) ||
1488                     rt2x00_rt(rt2x00dev, RT3390)) {
1489                         rt2x00_eeprom_read(rt2x00dev,
1490                                            EEPROM_NIC_CONF1, &eeprom);
1491                         if (rt2x00_get_field16(eeprom,
1492                                                 EEPROM_NIC_CONF1_ANT_DIVERSITY))
1493                                 rt2800_set_ant_diversity(rt2x00dev,
1494                                                 rt2x00dev->default_ant.rx);
1495                 }
1496                 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 0);
1497                 break;
1498         case 2:
1499                 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 1);
1500                 break;
1501         case 3:
1502                 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 2);
1503                 break;
1504         }
1505
1506         rt2800_bbp_write(rt2x00dev, 3, r3);
1507         rt2800_bbp_write(rt2x00dev, 1, r1);
1508 }
1509 EXPORT_SYMBOL_GPL(rt2800_config_ant);
1510
1511 static void rt2800_config_lna_gain(struct rt2x00_dev *rt2x00dev,
1512                                    struct rt2x00lib_conf *libconf)
1513 {
1514         u16 eeprom;
1515         short lna_gain;
1516
1517         if (libconf->rf.channel <= 14) {
1518                 rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &eeprom);
1519                 lna_gain = rt2x00_get_field16(eeprom, EEPROM_LNA_BG);
1520         } else if (libconf->rf.channel <= 64) {
1521                 rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &eeprom);
1522                 lna_gain = rt2x00_get_field16(eeprom, EEPROM_LNA_A0);
1523         } else if (libconf->rf.channel <= 128) {
1524                 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &eeprom);
1525                 lna_gain = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG2_LNA_A1);
1526         } else {
1527                 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &eeprom);
1528                 lna_gain = rt2x00_get_field16(eeprom, EEPROM_RSSI_A2_LNA_A2);
1529         }
1530
1531         rt2x00dev->lna_gain = lna_gain;
1532 }
1533
1534 static void rt2800_config_channel_rf2xxx(struct rt2x00_dev *rt2x00dev,
1535                                          struct ieee80211_conf *conf,
1536                                          struct rf_channel *rf,
1537                                          struct channel_info *info)
1538 {
1539         rt2x00_set_field32(&rf->rf4, RF4_FREQ_OFFSET, rt2x00dev->freq_offset);
1540
1541         if (rt2x00dev->default_ant.tx_chain_num == 1)
1542                 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_TX1, 1);
1543
1544         if (rt2x00dev->default_ant.rx_chain_num == 1) {
1545                 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX1, 1);
1546                 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX2, 1);
1547         } else if (rt2x00dev->default_ant.rx_chain_num == 2)
1548                 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX2, 1);
1549
1550         if (rf->channel > 14) {
1551                 /*
1552                  * When TX power is below 0, we should increase it by 7 to
1553                  * make it a positive value (Minimum value is -7).
1554                  * However this means that values between 0 and 7 have
1555                  * double meaning, and we should set a 7DBm boost flag.
1556                  */
1557                 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_A_7DBM_BOOST,
1558                                    (info->default_power1 >= 0));
1559
1560                 if (info->default_power1 < 0)
1561                         info->default_power1 += 7;
1562
1563                 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_A, info->default_power1);
1564
1565                 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_A_7DBM_BOOST,
1566                                    (info->default_power2 >= 0));
1567
1568                 if (info->default_power2 < 0)
1569                         info->default_power2 += 7;
1570
1571                 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_A, info->default_power2);
1572         } else {
1573                 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_G, info->default_power1);
1574                 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_G, info->default_power2);
1575         }
1576
1577         rt2x00_set_field32(&rf->rf4, RF4_HT40, conf_is_ht40(conf));
1578
1579         rt2800_rf_write(rt2x00dev, 1, rf->rf1);
1580         rt2800_rf_write(rt2x00dev, 2, rf->rf2);
1581         rt2800_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
1582         rt2800_rf_write(rt2x00dev, 4, rf->rf4);
1583
1584         udelay(200);
1585
1586         rt2800_rf_write(rt2x00dev, 1, rf->rf1);
1587         rt2800_rf_write(rt2x00dev, 2, rf->rf2);
1588         rt2800_rf_write(rt2x00dev, 3, rf->rf3 | 0x00000004);
1589         rt2800_rf_write(rt2x00dev, 4, rf->rf4);
1590
1591         udelay(200);
1592
1593         rt2800_rf_write(rt2x00dev, 1, rf->rf1);
1594         rt2800_rf_write(rt2x00dev, 2, rf->rf2);
1595         rt2800_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
1596         rt2800_rf_write(rt2x00dev, 4, rf->rf4);
1597 }
1598
1599 static void rt2800_config_channel_rf3xxx(struct rt2x00_dev *rt2x00dev,
1600                                          struct ieee80211_conf *conf,
1601                                          struct rf_channel *rf,
1602                                          struct channel_info *info)
1603 {
1604         u8 rfcsr;
1605
1606         rt2800_rfcsr_write(rt2x00dev, 2, rf->rf1);
1607         rt2800_rfcsr_write(rt2x00dev, 3, rf->rf3);
1608
1609         rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
1610         rt2x00_set_field8(&rfcsr, RFCSR6_R1, rf->rf2);
1611         rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
1612
1613         rt2800_rfcsr_read(rt2x00dev, 12, &rfcsr);
1614         rt2x00_set_field8(&rfcsr, RFCSR12_TX_POWER, info->default_power1);
1615         rt2800_rfcsr_write(rt2x00dev, 12, rfcsr);
1616
1617         rt2800_rfcsr_read(rt2x00dev, 13, &rfcsr);
1618         rt2x00_set_field8(&rfcsr, RFCSR13_TX_POWER, info->default_power2);
1619         rt2800_rfcsr_write(rt2x00dev, 13, rfcsr);
1620
1621         rt2800_rfcsr_read(rt2x00dev, 23, &rfcsr);
1622         rt2x00_set_field8(&rfcsr, RFCSR23_FREQ_OFFSET, rt2x00dev->freq_offset);
1623         rt2800_rfcsr_write(rt2x00dev, 23, rfcsr);
1624
1625         rt2800_rfcsr_write(rt2x00dev, 24,
1626                               rt2x00dev->calibration[conf_is_ht40(conf)]);
1627
1628         rt2800_rfcsr_read(rt2x00dev, 7, &rfcsr);
1629         rt2x00_set_field8(&rfcsr, RFCSR7_RF_TUNING, 1);
1630         rt2800_rfcsr_write(rt2x00dev, 7, rfcsr);
1631 }
1632
1633
1634 #define RT5390_POWER_BOUND     0x27
1635 #define RT5390_FREQ_OFFSET_BOUND       0x5f
1636
1637 static void rt2800_config_channel_rf53xx(struct rt2x00_dev *rt2x00dev,
1638                                          struct ieee80211_conf *conf,
1639                                          struct rf_channel *rf,
1640                                          struct channel_info *info)
1641 {
1642         u8 rfcsr;
1643
1644         rt2800_rfcsr_write(rt2x00dev, 8, rf->rf1);
1645         rt2800_rfcsr_write(rt2x00dev, 9, rf->rf3);
1646         rt2800_rfcsr_read(rt2x00dev, 11, &rfcsr);
1647         rt2x00_set_field8(&rfcsr, RFCSR11_R, rf->rf2);
1648         rt2800_rfcsr_write(rt2x00dev, 11, rfcsr);
1649
1650         rt2800_rfcsr_read(rt2x00dev, 49, &rfcsr);
1651         if (info->default_power1 > RT5390_POWER_BOUND)
1652                 rt2x00_set_field8(&rfcsr, RFCSR49_TX, RT5390_POWER_BOUND);
1653         else
1654                 rt2x00_set_field8(&rfcsr, RFCSR49_TX, info->default_power1);
1655         rt2800_rfcsr_write(rt2x00dev, 49, rfcsr);
1656
1657         rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
1658         rt2x00_set_field8(&rfcsr, RFCSR1_RF_BLOCK_EN, 1);
1659         rt2x00_set_field8(&rfcsr, RFCSR1_PLL_PD, 1);
1660         rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 1);
1661         rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 1);
1662         rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
1663
1664         rt2800_rfcsr_read(rt2x00dev, 17, &rfcsr);
1665         if (rt2x00dev->freq_offset > RT5390_FREQ_OFFSET_BOUND)
1666                 rt2x00_set_field8(&rfcsr, RFCSR17_CODE,
1667                                   RT5390_FREQ_OFFSET_BOUND);
1668         else
1669                 rt2x00_set_field8(&rfcsr, RFCSR17_CODE, rt2x00dev->freq_offset);
1670         rt2800_rfcsr_write(rt2x00dev, 17, rfcsr);
1671
1672         if (rf->channel <= 14) {
1673                 int idx = rf->channel-1;
1674
1675                 if (test_bit(CAPABILITY_BT_COEXIST, &rt2x00dev->cap_flags)) {
1676                         if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F)) {
1677                                 /* r55/r59 value array of channel 1~14 */
1678                                 static const char r55_bt_rev[] = {0x83, 0x83,
1679                                         0x83, 0x73, 0x73, 0x63, 0x53, 0x53,
1680                                         0x53, 0x43, 0x43, 0x43, 0x43, 0x43};
1681                                 static const char r59_bt_rev[] = {0x0e, 0x0e,
1682                                         0x0e, 0x0e, 0x0e, 0x0b, 0x0a, 0x09,
1683                                         0x07, 0x07, 0x07, 0x07, 0x07, 0x07};
1684
1685                                 rt2800_rfcsr_write(rt2x00dev, 55,
1686                                                    r55_bt_rev[idx]);
1687                                 rt2800_rfcsr_write(rt2x00dev, 59,
1688                                                    r59_bt_rev[idx]);
1689                         } else {
1690                                 static const char r59_bt[] = {0x8b, 0x8b, 0x8b,
1691                                         0x8b, 0x8b, 0x8b, 0x8b, 0x8a, 0x89,
1692                                         0x88, 0x88, 0x86, 0x85, 0x84};
1693
1694                                 rt2800_rfcsr_write(rt2x00dev, 59, r59_bt[idx]);
1695                         }
1696                 } else {
1697                         if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F)) {
1698                                 static const char r55_nonbt_rev[] = {0x23, 0x23,
1699                                         0x23, 0x23, 0x13, 0x13, 0x03, 0x03,
1700                                         0x03, 0x03, 0x03, 0x03, 0x03, 0x03};
1701                                 static const char r59_nonbt_rev[] = {0x07, 0x07,
1702                                         0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
1703                                         0x07, 0x07, 0x06, 0x05, 0x04, 0x04};
1704
1705                                 rt2800_rfcsr_write(rt2x00dev, 55,
1706                                                    r55_nonbt_rev[idx]);
1707                                 rt2800_rfcsr_write(rt2x00dev, 59,
1708                                                    r59_nonbt_rev[idx]);
1709                         } else if (rt2x00_rt(rt2x00dev, RT5390)) {
1710                                 static const char r59_non_bt[] = {0x8f, 0x8f,
1711                                         0x8f, 0x8f, 0x8f, 0x8f, 0x8f, 0x8d,
1712                                         0x8a, 0x88, 0x88, 0x87, 0x87, 0x86};
1713
1714                                 rt2800_rfcsr_write(rt2x00dev, 59,
1715                                                    r59_non_bt[idx]);
1716                         }
1717                 }
1718         }
1719
1720         rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
1721         rt2x00_set_field8(&rfcsr, RFCSR30_TX_H20M, 0);
1722         rt2x00_set_field8(&rfcsr, RFCSR30_RX_H20M, 0);
1723         rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
1724
1725         rt2800_rfcsr_read(rt2x00dev, 3, &rfcsr);
1726         rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 1);
1727         rt2800_rfcsr_write(rt2x00dev, 3, rfcsr);
1728 }
1729
1730 static void rt2800_config_channel(struct rt2x00_dev *rt2x00dev,
1731                                   struct ieee80211_conf *conf,
1732                                   struct rf_channel *rf,
1733                                   struct channel_info *info)
1734 {
1735         u32 reg;
1736         unsigned int tx_pin;
1737         u8 bbp;
1738
1739         if (rf->channel <= 14) {
1740                 info->default_power1 = TXPOWER_G_TO_DEV(info->default_power1);
1741                 info->default_power2 = TXPOWER_G_TO_DEV(info->default_power2);
1742         } else {
1743                 info->default_power1 = TXPOWER_A_TO_DEV(info->default_power1);
1744                 info->default_power2 = TXPOWER_A_TO_DEV(info->default_power2);
1745         }
1746
1747         if (rt2x00_rf(rt2x00dev, RF2020) ||
1748             rt2x00_rf(rt2x00dev, RF3020) ||
1749             rt2x00_rf(rt2x00dev, RF3021) ||
1750             rt2x00_rf(rt2x00dev, RF3022) ||
1751             rt2x00_rf(rt2x00dev, RF3052) ||
1752             rt2x00_rf(rt2x00dev, RF3320))
1753                 rt2800_config_channel_rf3xxx(rt2x00dev, conf, rf, info);
1754         else if (rt2x00_rf(rt2x00dev, RF5390))
1755                 rt2800_config_channel_rf53xx(rt2x00dev, conf, rf, info);
1756         else
1757                 rt2800_config_channel_rf2xxx(rt2x00dev, conf, rf, info);
1758
1759         /*
1760          * Change BBP settings
1761          */
1762         rt2800_bbp_write(rt2x00dev, 62, 0x37 - rt2x00dev->lna_gain);
1763         rt2800_bbp_write(rt2x00dev, 63, 0x37 - rt2x00dev->lna_gain);
1764         rt2800_bbp_write(rt2x00dev, 64, 0x37 - rt2x00dev->lna_gain);
1765         rt2800_bbp_write(rt2x00dev, 86, 0);
1766
1767         if (rf->channel <= 14) {
1768                 if (!rt2x00_rt(rt2x00dev, RT5390)) {
1769                         if (test_bit(CAPABILITY_EXTERNAL_LNA_BG,
1770                                      &rt2x00dev->cap_flags)) {
1771                                 rt2800_bbp_write(rt2x00dev, 82, 0x62);
1772                                 rt2800_bbp_write(rt2x00dev, 75, 0x46);
1773                         } else {
1774                                 rt2800_bbp_write(rt2x00dev, 82, 0x84);
1775                                 rt2800_bbp_write(rt2x00dev, 75, 0x50);
1776                         }
1777                 }
1778         } else {
1779                 rt2800_bbp_write(rt2x00dev, 82, 0xf2);
1780
1781                 if (test_bit(CAPABILITY_EXTERNAL_LNA_A, &rt2x00dev->cap_flags))
1782                         rt2800_bbp_write(rt2x00dev, 75, 0x46);
1783                 else
1784                         rt2800_bbp_write(rt2x00dev, 75, 0x50);
1785         }
1786
1787         rt2800_register_read(rt2x00dev, TX_BAND_CFG, &reg);
1788         rt2x00_set_field32(&reg, TX_BAND_CFG_HT40_MINUS, conf_is_ht40_minus(conf));
1789         rt2x00_set_field32(&reg, TX_BAND_CFG_A, rf->channel > 14);
1790         rt2x00_set_field32(&reg, TX_BAND_CFG_BG, rf->channel <= 14);
1791         rt2800_register_write(rt2x00dev, TX_BAND_CFG, reg);
1792
1793         tx_pin = 0;
1794
1795         /* Turn on unused PA or LNA when not using 1T or 1R */
1796         if (rt2x00dev->default_ant.tx_chain_num == 2) {
1797                 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A1_EN, 1);
1798                 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G1_EN, 1);
1799         }
1800
1801         /* Turn on unused PA or LNA when not using 1T or 1R */
1802         if (rt2x00dev->default_ant.rx_chain_num == 2) {
1803                 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_A1_EN, 1);
1804                 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_G1_EN, 1);
1805         }
1806
1807         rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_A0_EN, 1);
1808         rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_G0_EN, 1);
1809         rt2x00_set_field32(&tx_pin, TX_PIN_CFG_RFTR_EN, 1);
1810         rt2x00_set_field32(&tx_pin, TX_PIN_CFG_TRSW_EN, 1);
1811         rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G0_EN, rf->channel <= 14);
1812         rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A0_EN, rf->channel > 14);
1813
1814         rt2800_register_write(rt2x00dev, TX_PIN_CFG, tx_pin);
1815
1816         rt2800_bbp_read(rt2x00dev, 4, &bbp);
1817         rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 2 * conf_is_ht40(conf));
1818         rt2800_bbp_write(rt2x00dev, 4, bbp);
1819
1820         rt2800_bbp_read(rt2x00dev, 3, &bbp);
1821         rt2x00_set_field8(&bbp, BBP3_HT40_MINUS, conf_is_ht40_minus(conf));
1822         rt2800_bbp_write(rt2x00dev, 3, bbp);
1823
1824         if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C)) {
1825                 if (conf_is_ht40(conf)) {
1826                         rt2800_bbp_write(rt2x00dev, 69, 0x1a);
1827                         rt2800_bbp_write(rt2x00dev, 70, 0x0a);
1828                         rt2800_bbp_write(rt2x00dev, 73, 0x16);
1829                 } else {
1830                         rt2800_bbp_write(rt2x00dev, 69, 0x16);
1831                         rt2800_bbp_write(rt2x00dev, 70, 0x08);
1832                         rt2800_bbp_write(rt2x00dev, 73, 0x11);
1833                 }
1834         }
1835
1836         msleep(1);
1837
1838         /*
1839          * Clear channel statistic counters
1840          */
1841         rt2800_register_read(rt2x00dev, CH_IDLE_STA, &reg);
1842         rt2800_register_read(rt2x00dev, CH_BUSY_STA, &reg);
1843         rt2800_register_read(rt2x00dev, CH_BUSY_STA_SEC, &reg);
1844 }
1845
1846 static int rt2800_get_gain_calibration_delta(struct rt2x00_dev *rt2x00dev)
1847 {
1848         u8 tssi_bounds[9];
1849         u8 current_tssi;
1850         u16 eeprom;
1851         u8 step;
1852         int i;
1853
1854         /*
1855          * Read TSSI boundaries for temperature compensation from
1856          * the EEPROM.
1857          *
1858          * Array idx               0    1    2    3    4    5    6    7    8
1859          * Matching Delta value   -4   -3   -2   -1    0   +1   +2   +3   +4
1860          * Example TSSI bounds  0xF0 0xD0 0xB5 0xA0 0x88 0x45 0x25 0x15 0x00
1861          */
1862         if (rt2x00dev->curr_band == IEEE80211_BAND_2GHZ) {
1863                 rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_BG1, &eeprom);
1864                 tssi_bounds[0] = rt2x00_get_field16(eeprom,
1865                                         EEPROM_TSSI_BOUND_BG1_MINUS4);
1866                 tssi_bounds[1] = rt2x00_get_field16(eeprom,
1867                                         EEPROM_TSSI_BOUND_BG1_MINUS3);
1868
1869                 rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_BG2, &eeprom);
1870                 tssi_bounds[2] = rt2x00_get_field16(eeprom,
1871                                         EEPROM_TSSI_BOUND_BG2_MINUS2);
1872                 tssi_bounds[3] = rt2x00_get_field16(eeprom,
1873                                         EEPROM_TSSI_BOUND_BG2_MINUS1);
1874
1875                 rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_BG3, &eeprom);
1876                 tssi_bounds[4] = rt2x00_get_field16(eeprom,
1877                                         EEPROM_TSSI_BOUND_BG3_REF);
1878                 tssi_bounds[5] = rt2x00_get_field16(eeprom,
1879                                         EEPROM_TSSI_BOUND_BG3_PLUS1);
1880
1881                 rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_BG4, &eeprom);
1882                 tssi_bounds[6] = rt2x00_get_field16(eeprom,
1883                                         EEPROM_TSSI_BOUND_BG4_PLUS2);
1884                 tssi_bounds[7] = rt2x00_get_field16(eeprom,
1885                                         EEPROM_TSSI_BOUND_BG4_PLUS3);
1886
1887                 rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_BG5, &eeprom);
1888                 tssi_bounds[8] = rt2x00_get_field16(eeprom,
1889                                         EEPROM_TSSI_BOUND_BG5_PLUS4);
1890
1891                 step = rt2x00_get_field16(eeprom,
1892                                           EEPROM_TSSI_BOUND_BG5_AGC_STEP);
1893         } else {
1894                 rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_A1, &eeprom);
1895                 tssi_bounds[0] = rt2x00_get_field16(eeprom,
1896                                         EEPROM_TSSI_BOUND_A1_MINUS4);
1897                 tssi_bounds[1] = rt2x00_get_field16(eeprom,
1898                                         EEPROM_TSSI_BOUND_A1_MINUS3);
1899
1900                 rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_A2, &eeprom);
1901                 tssi_bounds[2] = rt2x00_get_field16(eeprom,
1902                                         EEPROM_TSSI_BOUND_A2_MINUS2);
1903                 tssi_bounds[3] = rt2x00_get_field16(eeprom,
1904                                         EEPROM_TSSI_BOUND_A2_MINUS1);
1905
1906                 rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_A3, &eeprom);
1907                 tssi_bounds[4] = rt2x00_get_field16(eeprom,
1908                                         EEPROM_TSSI_BOUND_A3_REF);
1909                 tssi_bounds[5] = rt2x00_get_field16(eeprom,
1910                                         EEPROM_TSSI_BOUND_A3_PLUS1);
1911
1912                 rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_A4, &eeprom);
1913                 tssi_bounds[6] = rt2x00_get_field16(eeprom,
1914                                         EEPROM_TSSI_BOUND_A4_PLUS2);
1915                 tssi_bounds[7] = rt2x00_get_field16(eeprom,
1916                                         EEPROM_TSSI_BOUND_A4_PLUS3);
1917
1918                 rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_A5, &eeprom);
1919                 tssi_bounds[8] = rt2x00_get_field16(eeprom,
1920                                         EEPROM_TSSI_BOUND_A5_PLUS4);
1921
1922                 step = rt2x00_get_field16(eeprom,
1923                                           EEPROM_TSSI_BOUND_A5_AGC_STEP);
1924         }
1925
1926         /*
1927          * Check if temperature compensation is supported.
1928          */
1929         if (tssi_bounds[4] == 0xff)
1930                 return 0;
1931
1932         /*
1933          * Read current TSSI (BBP 49).
1934          */
1935         rt2800_bbp_read(rt2x00dev, 49, &current_tssi);
1936
1937         /*
1938          * Compare TSSI value (BBP49) with the compensation boundaries
1939          * from the EEPROM and increase or decrease tx power.
1940          */
1941         for (i = 0; i <= 3; i++) {
1942                 if (current_tssi > tssi_bounds[i])
1943                         break;
1944         }
1945
1946         if (i == 4) {
1947                 for (i = 8; i >= 5; i--) {
1948                         if (current_tssi < tssi_bounds[i])
1949                                 break;
1950                 }
1951         }
1952
1953         return (i - 4) * step;
1954 }
1955
1956 static int rt2800_get_txpower_bw_comp(struct rt2x00_dev *rt2x00dev,
1957                                       enum ieee80211_band band)
1958 {
1959         u16 eeprom;
1960         u8 comp_en;
1961         u8 comp_type;
1962         int comp_value = 0;
1963
1964         rt2x00_eeprom_read(rt2x00dev, EEPROM_TXPOWER_DELTA, &eeprom);
1965
1966         /*
1967          * HT40 compensation not required.
1968          */
1969         if (eeprom == 0xffff ||
1970             !test_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags))
1971                 return 0;
1972
1973         if (band == IEEE80211_BAND_2GHZ) {
1974                 comp_en = rt2x00_get_field16(eeprom,
1975                                  EEPROM_TXPOWER_DELTA_ENABLE_2G);
1976                 if (comp_en) {
1977                         comp_type = rt2x00_get_field16(eeprom,
1978                                            EEPROM_TXPOWER_DELTA_TYPE_2G);
1979                         comp_value = rt2x00_get_field16(eeprom,
1980                                             EEPROM_TXPOWER_DELTA_VALUE_2G);
1981                         if (!comp_type)
1982                                 comp_value = -comp_value;
1983                 }
1984         } else {
1985                 comp_en = rt2x00_get_field16(eeprom,
1986                                  EEPROM_TXPOWER_DELTA_ENABLE_5G);
1987                 if (comp_en) {
1988                         comp_type = rt2x00_get_field16(eeprom,
1989                                            EEPROM_TXPOWER_DELTA_TYPE_5G);
1990                         comp_value = rt2x00_get_field16(eeprom,
1991                                             EEPROM_TXPOWER_DELTA_VALUE_5G);
1992                         if (!comp_type)
1993                                 comp_value = -comp_value;
1994                 }
1995         }
1996
1997         return comp_value;
1998 }
1999
2000 static u8 rt2800_compensate_txpower(struct rt2x00_dev *rt2x00dev, int is_rate_b,
2001                                    enum ieee80211_band band, int power_level,
2002                                    u8 txpower, int delta)
2003 {
2004         u32 reg;
2005         u16 eeprom;
2006         u8 criterion;
2007         u8 eirp_txpower;
2008         u8 eirp_txpower_criterion;
2009         u8 reg_limit;
2010
2011         if (!((band == IEEE80211_BAND_5GHZ) && is_rate_b))
2012                 return txpower;
2013
2014         if (test_bit(CAPABILITY_POWER_LIMIT, &rt2x00dev->cap_flags)) {
2015                 /*
2016                  * Check if eirp txpower exceed txpower_limit.
2017                  * We use OFDM 6M as criterion and its eirp txpower
2018                  * is stored at EEPROM_EIRP_MAX_TX_POWER.
2019                  * .11b data rate need add additional 4dbm
2020                  * when calculating eirp txpower.
2021                  */
2022                 rt2800_register_read(rt2x00dev, TX_PWR_CFG_0, &reg);
2023                 criterion = rt2x00_get_field32(reg, TX_PWR_CFG_0_6MBS);
2024
2025                 rt2x00_eeprom_read(rt2x00dev,
2026                                    EEPROM_EIRP_MAX_TX_POWER, &eeprom);
2027
2028                 if (band == IEEE80211_BAND_2GHZ)
2029                         eirp_txpower_criterion = rt2x00_get_field16(eeprom,
2030                                                  EEPROM_EIRP_MAX_TX_POWER_2GHZ);
2031                 else
2032                         eirp_txpower_criterion = rt2x00_get_field16(eeprom,
2033                                                  EEPROM_EIRP_MAX_TX_POWER_5GHZ);
2034
2035                 eirp_txpower = eirp_txpower_criterion + (txpower - criterion) +
2036                                (is_rate_b ? 4 : 0) + delta;
2037
2038                 reg_limit = (eirp_txpower > power_level) ?
2039                                         (eirp_txpower - power_level) : 0;
2040         } else
2041                 reg_limit = 0;
2042
2043         return txpower + delta - reg_limit;
2044 }
2045
2046 static void rt2800_config_txpower(struct rt2x00_dev *rt2x00dev,
2047                                   enum ieee80211_band band,
2048                                   int power_level)
2049 {
2050         u8 txpower;
2051         u16 eeprom;
2052         int i, is_rate_b;
2053         u32 reg;
2054         u8 r1;
2055         u32 offset;
2056         int delta;
2057
2058         /*
2059          * Calculate HT40 compensation delta
2060          */
2061         delta = rt2800_get_txpower_bw_comp(rt2x00dev, band);
2062
2063         /*
2064          * calculate temperature compensation delta
2065          */
2066         delta += rt2800_get_gain_calibration_delta(rt2x00dev);
2067
2068         /*
2069          * set to normal bbp tx power control mode: +/- 0dBm
2070          */
2071         rt2800_bbp_read(rt2x00dev, 1, &r1);
2072         rt2x00_set_field8(&r1, BBP1_TX_POWER_CTRL, 0);
2073         rt2800_bbp_write(rt2x00dev, 1, r1);
2074         offset = TX_PWR_CFG_0;
2075
2076         for (i = 0; i < EEPROM_TXPOWER_BYRATE_SIZE; i += 2) {
2077                 /* just to be safe */
2078                 if (offset > TX_PWR_CFG_4)
2079                         break;
2080
2081                 rt2800_register_read(rt2x00dev, offset, &reg);
2082
2083                 /* read the next four txpower values */
2084                 rt2x00_eeprom_read(rt2x00dev, EEPROM_TXPOWER_BYRATE + i,
2085                                    &eeprom);
2086
2087                 is_rate_b = i ? 0 : 1;
2088                 /*
2089                  * TX_PWR_CFG_0: 1MBS, TX_PWR_CFG_1: 24MBS,
2090                  * TX_PWR_CFG_2: MCS4, TX_PWR_CFG_3: MCS12,
2091                  * TX_PWR_CFG_4: unknown
2092                  */
2093                 txpower = rt2x00_get_field16(eeprom,
2094                                              EEPROM_TXPOWER_BYRATE_RATE0);
2095                 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
2096                                              power_level, txpower, delta);
2097                 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE0, txpower);
2098
2099                 /*
2100                  * TX_PWR_CFG_0: 2MBS, TX_PWR_CFG_1: 36MBS,
2101                  * TX_PWR_CFG_2: MCS5, TX_PWR_CFG_3: MCS13,
2102                  * TX_PWR_CFG_4: unknown
2103                  */
2104                 txpower = rt2x00_get_field16(eeprom,
2105                                              EEPROM_TXPOWER_BYRATE_RATE1);
2106                 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
2107                                              power_level, txpower, delta);
2108                 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE1, txpower);
2109
2110                 /*
2111                  * TX_PWR_CFG_0: 5.5MBS, TX_PWR_CFG_1: 48MBS,
2112                  * TX_PWR_CFG_2: MCS6,  TX_PWR_CFG_3: MCS14,
2113                  * TX_PWR_CFG_4: unknown
2114                  */
2115                 txpower = rt2x00_get_field16(eeprom,
2116                                              EEPROM_TXPOWER_BYRATE_RATE2);
2117                 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
2118                                              power_level, txpower, delta);
2119                 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE2, txpower);
2120
2121                 /*
2122                  * TX_PWR_CFG_0: 11MBS, TX_PWR_CFG_1: 54MBS,
2123                  * TX_PWR_CFG_2: MCS7,  TX_PWR_CFG_3: MCS15,
2124                  * TX_PWR_CFG_4: unknown
2125                  */
2126                 txpower = rt2x00_get_field16(eeprom,
2127                                              EEPROM_TXPOWER_BYRATE_RATE3);
2128                 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
2129                                              power_level, txpower, delta);
2130                 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE3, txpower);
2131
2132                 /* read the next four txpower values */
2133                 rt2x00_eeprom_read(rt2x00dev, EEPROM_TXPOWER_BYRATE + i + 1,
2134                                    &eeprom);
2135
2136                 is_rate_b = 0;
2137                 /*
2138                  * TX_PWR_CFG_0: 6MBS, TX_PWR_CFG_1: MCS0,
2139                  * TX_PWR_CFG_2: MCS8, TX_PWR_CFG_3: unknown,
2140                  * TX_PWR_CFG_4: unknown
2141                  */
2142                 txpower = rt2x00_get_field16(eeprom,
2143                                              EEPROM_TXPOWER_BYRATE_RATE0);
2144                 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
2145                                              power_level, txpower, delta);
2146                 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE4, txpower);
2147
2148                 /*
2149                  * TX_PWR_CFG_0: 9MBS, TX_PWR_CFG_1: MCS1,
2150                  * TX_PWR_CFG_2: MCS9, TX_PWR_CFG_3: unknown,
2151                  * TX_PWR_CFG_4: unknown
2152                  */
2153                 txpower = rt2x00_get_field16(eeprom,
2154                                              EEPROM_TXPOWER_BYRATE_RATE1);
2155                 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
2156                                              power_level, txpower, delta);
2157                 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE5, txpower);
2158
2159                 /*
2160                  * TX_PWR_CFG_0: 12MBS, TX_PWR_CFG_1: MCS2,
2161                  * TX_PWR_CFG_2: MCS10, TX_PWR_CFG_3: unknown,
2162                  * TX_PWR_CFG_4: unknown
2163                  */
2164                 txpower = rt2x00_get_field16(eeprom,
2165                                              EEPROM_TXPOWER_BYRATE_RATE2);
2166                 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
2167                                              power_level, txpower, delta);
2168                 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE6, txpower);
2169
2170                 /*
2171                  * TX_PWR_CFG_0: 18MBS, TX_PWR_CFG_1: MCS3,
2172                  * TX_PWR_CFG_2: MCS11, TX_PWR_CFG_3: unknown,
2173                  * TX_PWR_CFG_4: unknown
2174                  */
2175                 txpower = rt2x00_get_field16(eeprom,
2176                                              EEPROM_TXPOWER_BYRATE_RATE3);
2177                 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
2178                                              power_level, txpower, delta);
2179                 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE7, txpower);
2180
2181                 rt2800_register_write(rt2x00dev, offset, reg);
2182
2183                 /* next TX_PWR_CFG register */
2184                 offset += 4;
2185         }
2186 }
2187
2188 void rt2800_gain_calibration(struct rt2x00_dev *rt2x00dev)
2189 {
2190         rt2800_config_txpower(rt2x00dev, rt2x00dev->curr_band,
2191                               rt2x00dev->tx_power);
2192 }
2193 EXPORT_SYMBOL_GPL(rt2800_gain_calibration);
2194
2195 static void rt2800_config_retry_limit(struct rt2x00_dev *rt2x00dev,
2196                                       struct rt2x00lib_conf *libconf)
2197 {
2198         u32 reg;
2199
2200         rt2800_register_read(rt2x00dev, TX_RTY_CFG, &reg);
2201         rt2x00_set_field32(&reg, TX_RTY_CFG_SHORT_RTY_LIMIT,
2202                            libconf->conf->short_frame_max_tx_count);
2203         rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_LIMIT,
2204                            libconf->conf->long_frame_max_tx_count);
2205         rt2800_register_write(rt2x00dev, TX_RTY_CFG, reg);
2206 }
2207
2208 static void rt2800_config_ps(struct rt2x00_dev *rt2x00dev,
2209                              struct rt2x00lib_conf *libconf)
2210 {
2211         enum dev_state state =
2212             (libconf->conf->flags & IEEE80211_CONF_PS) ?
2213                 STATE_SLEEP : STATE_AWAKE;
2214         u32 reg;
2215
2216         if (state == STATE_SLEEP) {
2217                 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, 0);
2218
2219                 rt2800_register_read(rt2x00dev, AUTOWAKEUP_CFG, &reg);
2220                 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTO_LEAD_TIME, 5);
2221                 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_TBCN_BEFORE_WAKE,
2222                                    libconf->conf->listen_interval - 1);
2223                 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTOWAKE, 1);
2224                 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, reg);
2225
2226                 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
2227         } else {
2228                 rt2800_register_read(rt2x00dev, AUTOWAKEUP_CFG, &reg);
2229                 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTO_LEAD_TIME, 0);
2230                 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_TBCN_BEFORE_WAKE, 0);
2231                 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTOWAKE, 0);
2232                 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, reg);
2233
2234                 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
2235         }
2236 }
2237
2238 void rt2800_config(struct rt2x00_dev *rt2x00dev,
2239                    struct rt2x00lib_conf *libconf,
2240                    const unsigned int flags)
2241 {
2242         /* Always recalculate LNA gain before changing configuration */
2243         rt2800_config_lna_gain(rt2x00dev, libconf);
2244
2245         if (flags & IEEE80211_CONF_CHANGE_CHANNEL) {
2246                 rt2800_config_channel(rt2x00dev, libconf->conf,
2247                                       &libconf->rf, &libconf->channel);
2248                 rt2800_config_txpower(rt2x00dev, libconf->conf->channel->band,
2249                                       libconf->conf->power_level);
2250         }
2251         if (flags & IEEE80211_CONF_CHANGE_POWER)
2252                 rt2800_config_txpower(rt2x00dev, libconf->conf->channel->band,
2253                                       libconf->conf->power_level);
2254         if (flags & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
2255                 rt2800_config_retry_limit(rt2x00dev, libconf);
2256         if (flags & IEEE80211_CONF_CHANGE_PS)
2257                 rt2800_config_ps(rt2x00dev, libconf);
2258 }
2259 EXPORT_SYMBOL_GPL(rt2800_config);
2260
2261 /*
2262  * Link tuning
2263  */
2264 void rt2800_link_stats(struct rt2x00_dev *rt2x00dev, struct link_qual *qual)
2265 {
2266         u32 reg;
2267
2268         /*
2269          * Update FCS error count from register.
2270          */
2271         rt2800_register_read(rt2x00dev, RX_STA_CNT0, &reg);
2272         qual->rx_failed = rt2x00_get_field32(reg, RX_STA_CNT0_CRC_ERR);
2273 }
2274 EXPORT_SYMBOL_GPL(rt2800_link_stats);
2275
2276 static u8 rt2800_get_default_vgc(struct rt2x00_dev *rt2x00dev)
2277 {
2278         if (rt2x00dev->curr_band == IEEE80211_BAND_2GHZ) {
2279                 if (rt2x00_rt(rt2x00dev, RT3070) ||
2280                     rt2x00_rt(rt2x00dev, RT3071) ||
2281                     rt2x00_rt(rt2x00dev, RT3090) ||
2282                     rt2x00_rt(rt2x00dev, RT3390) ||
2283                     rt2x00_rt(rt2x00dev, RT5390))
2284                         return 0x1c + (2 * rt2x00dev->lna_gain);
2285                 else
2286                         return 0x2e + rt2x00dev->lna_gain;
2287         }
2288
2289         if (!test_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags))
2290                 return 0x32 + (rt2x00dev->lna_gain * 5) / 3;
2291         else
2292                 return 0x3a + (rt2x00dev->lna_gain * 5) / 3;
2293 }
2294
2295 static inline void rt2800_set_vgc(struct rt2x00_dev *rt2x00dev,
2296                                   struct link_qual *qual, u8 vgc_level)
2297 {
2298         if (qual->vgc_level != vgc_level) {
2299                 rt2800_bbp_write(rt2x00dev, 66, vgc_level);
2300                 qual->vgc_level = vgc_level;
2301                 qual->vgc_level_reg = vgc_level;
2302         }
2303 }
2304
2305 void rt2800_reset_tuner(struct rt2x00_dev *rt2x00dev, struct link_qual *qual)
2306 {
2307         rt2800_set_vgc(rt2x00dev, qual, rt2800_get_default_vgc(rt2x00dev));
2308 }
2309 EXPORT_SYMBOL_GPL(rt2800_reset_tuner);
2310
2311 void rt2800_link_tuner(struct rt2x00_dev *rt2x00dev, struct link_qual *qual,
2312                        const u32 count)
2313 {
2314         if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C))
2315                 return;
2316
2317         /*
2318          * When RSSI is better then -80 increase VGC level with 0x10
2319          */
2320         rt2800_set_vgc(rt2x00dev, qual,
2321                        rt2800_get_default_vgc(rt2x00dev) +
2322                        ((qual->rssi > -80) * 0x10));
2323 }
2324 EXPORT_SYMBOL_GPL(rt2800_link_tuner);
2325
2326 /*
2327  * Initialization functions.
2328  */
2329 static int rt2800_init_registers(struct rt2x00_dev *rt2x00dev)
2330 {
2331         u32 reg;
2332         u16 eeprom;
2333         unsigned int i;
2334         int ret;
2335
2336         rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
2337         rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
2338         rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
2339         rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
2340         rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
2341         rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
2342         rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
2343
2344         ret = rt2800_drv_init_registers(rt2x00dev);
2345         if (ret)
2346                 return ret;
2347
2348         rt2800_register_read(rt2x00dev, BCN_OFFSET0, &reg);
2349         rt2x00_set_field32(&reg, BCN_OFFSET0_BCN0, 0xe0); /* 0x3800 */
2350         rt2x00_set_field32(&reg, BCN_OFFSET0_BCN1, 0xe8); /* 0x3a00 */
2351         rt2x00_set_field32(&reg, BCN_OFFSET0_BCN2, 0xf0); /* 0x3c00 */
2352         rt2x00_set_field32(&reg, BCN_OFFSET0_BCN3, 0xf8); /* 0x3e00 */
2353         rt2800_register_write(rt2x00dev, BCN_OFFSET0, reg);
2354
2355         rt2800_register_read(rt2x00dev, BCN_OFFSET1, &reg);
2356         rt2x00_set_field32(&reg, BCN_OFFSET1_BCN4, 0xc8); /* 0x3200 */
2357         rt2x00_set_field32(&reg, BCN_OFFSET1_BCN5, 0xd0); /* 0x3400 */
2358         rt2x00_set_field32(&reg, BCN_OFFSET1_BCN6, 0x77); /* 0x1dc0 */
2359         rt2x00_set_field32(&reg, BCN_OFFSET1_BCN7, 0x6f); /* 0x1bc0 */
2360         rt2800_register_write(rt2x00dev, BCN_OFFSET1, reg);
2361
2362         rt2800_register_write(rt2x00dev, LEGACY_BASIC_RATE, 0x0000013f);
2363         rt2800_register_write(rt2x00dev, HT_BASIC_RATE, 0x00008003);
2364
2365         rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, 0x00000000);
2366
2367         rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
2368         rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_INTERVAL, 1600);
2369         rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 0);
2370         rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_SYNC, 0);
2371         rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE, 0);
2372         rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
2373         rt2x00_set_field32(&reg, BCN_TIME_CFG_TX_TIME_COMPENSATE, 0);
2374         rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
2375
2376         rt2800_config_filter(rt2x00dev, FIF_ALLMULTI);
2377
2378         rt2800_register_read(rt2x00dev, BKOFF_SLOT_CFG, &reg);
2379         rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_SLOT_TIME, 9);
2380         rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_CC_DELAY_TIME, 2);
2381         rt2800_register_write(rt2x00dev, BKOFF_SLOT_CFG, reg);
2382
2383         if (rt2x00_rt(rt2x00dev, RT3071) ||
2384             rt2x00_rt(rt2x00dev, RT3090) ||
2385             rt2x00_rt(rt2x00dev, RT3390)) {
2386                 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
2387                 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
2388                 if (rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
2389                     rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E) ||
2390                     rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E)) {
2391                         rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &eeprom);
2392                         if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_DAC_TEST))
2393                                 rt2800_register_write(rt2x00dev, TX_SW_CFG2,
2394                                                       0x0000002c);
2395                         else
2396                                 rt2800_register_write(rt2x00dev, TX_SW_CFG2,
2397                                                       0x0000000f);
2398                 } else {
2399                         rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
2400                 }
2401         } else if (rt2x00_rt(rt2x00dev, RT3070)) {
2402                 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
2403
2404                 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F)) {
2405                         rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
2406                         rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x0000002c);
2407                 } else {
2408                         rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
2409                         rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
2410                 }
2411         } else if (rt2800_is_305x_soc(rt2x00dev)) {
2412                 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
2413                 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
2414                 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000030);
2415         } else if (rt2x00_rt(rt2x00dev, RT5390)) {
2416                 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000404);
2417                 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
2418                 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
2419         } else {
2420                 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000000);
2421                 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
2422         }
2423
2424         rt2800_register_read(rt2x00dev, TX_LINK_CFG, &reg);
2425         rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFB_LIFETIME, 32);
2426         rt2x00_set_field32(&reg, TX_LINK_CFG_MFB_ENABLE, 0);
2427         rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_UMFS_ENABLE, 0);
2428         rt2x00_set_field32(&reg, TX_LINK_CFG_TX_MRQ_EN, 0);
2429         rt2x00_set_field32(&reg, TX_LINK_CFG_TX_RDG_EN, 0);
2430         rt2x00_set_field32(&reg, TX_LINK_CFG_TX_CF_ACK_EN, 1);
2431         rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFB, 0);
2432         rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFS, 0);
2433         rt2800_register_write(rt2x00dev, TX_LINK_CFG, reg);
2434
2435         rt2800_register_read(rt2x00dev, TX_TIMEOUT_CFG, &reg);
2436         rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_MPDU_LIFETIME, 9);
2437         rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_RX_ACK_TIMEOUT, 32);
2438         rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_TX_OP_TIMEOUT, 10);
2439         rt2800_register_write(rt2x00dev, TX_TIMEOUT_CFG, reg);
2440
2441         rt2800_register_read(rt2x00dev, MAX_LEN_CFG, &reg);
2442         rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_MPDU, AGGREGATION_SIZE);
2443         if (rt2x00_rt_rev_gte(rt2x00dev, RT2872, REV_RT2872E) ||
2444             rt2x00_rt(rt2x00dev, RT2883) ||
2445             rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070E))
2446                 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_PSDU, 2);
2447         else
2448                 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_PSDU, 1);
2449         rt2x00_set_field32(&reg, MAX_LEN_CFG_MIN_PSDU, 0);
2450         rt2x00_set_field32(&reg, MAX_LEN_CFG_MIN_MPDU, 0);
2451         rt2800_register_write(rt2x00dev, MAX_LEN_CFG, reg);
2452
2453         rt2800_register_read(rt2x00dev, LED_CFG, &reg);
2454         rt2x00_set_field32(&reg, LED_CFG_ON_PERIOD, 70);
2455         rt2x00_set_field32(&reg, LED_CFG_OFF_PERIOD, 30);
2456         rt2x00_set_field32(&reg, LED_CFG_SLOW_BLINK_PERIOD, 3);
2457         rt2x00_set_field32(&reg, LED_CFG_R_LED_MODE, 3);
2458         rt2x00_set_field32(&reg, LED_CFG_G_LED_MODE, 3);
2459         rt2x00_set_field32(&reg, LED_CFG_Y_LED_MODE, 3);
2460         rt2x00_set_field32(&reg, LED_CFG_LED_POLAR, 1);
2461         rt2800_register_write(rt2x00dev, LED_CFG, reg);
2462
2463         rt2800_register_write(rt2x00dev, PBF_MAX_PCNT, 0x1f3fbf9f);
2464
2465         rt2800_register_read(rt2x00dev, TX_RTY_CFG, &reg);
2466         rt2x00_set_field32(&reg, TX_RTY_CFG_SHORT_RTY_LIMIT, 15);
2467         rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_LIMIT, 31);
2468         rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_THRE, 2000);
2469         rt2x00_set_field32(&reg, TX_RTY_CFG_NON_AGG_RTY_MODE, 0);
2470         rt2x00_set_field32(&reg, TX_RTY_CFG_AGG_RTY_MODE, 0);
2471         rt2x00_set_field32(&reg, TX_RTY_CFG_TX_AUTO_FB_ENABLE, 1);
2472         rt2800_register_write(rt2x00dev, TX_RTY_CFG, reg);
2473
2474         rt2800_register_read(rt2x00dev, AUTO_RSP_CFG, &reg);
2475         rt2x00_set_field32(&reg, AUTO_RSP_CFG_AUTORESPONDER, 1);
2476         rt2x00_set_field32(&reg, AUTO_RSP_CFG_BAC_ACK_POLICY, 1);
2477         rt2x00_set_field32(&reg, AUTO_RSP_CFG_CTS_40_MMODE, 0);
2478         rt2x00_set_field32(&reg, AUTO_RSP_CFG_CTS_40_MREF, 0);
2479         rt2x00_set_field32(&reg, AUTO_RSP_CFG_AR_PREAMBLE, 1);
2480         rt2x00_set_field32(&reg, AUTO_RSP_CFG_DUAL_CTS_EN, 0);
2481         rt2x00_set_field32(&reg, AUTO_RSP_CFG_ACK_CTS_PSM_BIT, 0);
2482         rt2800_register_write(rt2x00dev, AUTO_RSP_CFG, reg);
2483
2484         rt2800_register_read(rt2x00dev, CCK_PROT_CFG, &reg);
2485         rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_RATE, 3);
2486         rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_CTRL, 0);
2487         rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_NAV_SHORT, 1);
2488         rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_CCK, 1);
2489         rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
2490         rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_MM20, 1);
2491         rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_MM40, 0);
2492         rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_GF20, 1);
2493         rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_GF40, 0);
2494         rt2x00_set_field32(&reg, CCK_PROT_CFG_RTS_TH_EN, 1);
2495         rt2800_register_write(rt2x00dev, CCK_PROT_CFG, reg);
2496
2497         rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
2498         rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_RATE, 3);
2499         rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_CTRL, 0);
2500         rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_NAV_SHORT, 1);
2501         rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_CCK, 1);
2502         rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
2503         rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_MM20, 1);
2504         rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_MM40, 0);
2505         rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_GF20, 1);
2506         rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_GF40, 0);
2507         rt2x00_set_field32(&reg, OFDM_PROT_CFG_RTS_TH_EN, 1);
2508         rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
2509
2510         rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
2511         rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_RATE, 0x4004);
2512         rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_CTRL, 0);
2513         rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_NAV_SHORT, 1);
2514         rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_CCK, 1);
2515         rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
2516         rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_MM20, 1);
2517         rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_MM40, 0);
2518         rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_GF20, 1);
2519         rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_GF40, 0);
2520         rt2x00_set_field32(&reg, MM20_PROT_CFG_RTS_TH_EN, 0);
2521         rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
2522
2523         rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
2524         rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_RATE, 0x4084);
2525         rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_CTRL, 0);
2526         rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_NAV_SHORT, 1);
2527         rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_CCK, 1);
2528         rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
2529         rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_MM20, 1);
2530         rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_MM40, 1);
2531         rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_GF20, 1);
2532         rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_GF40, 1);
2533         rt2x00_set_field32(&reg, MM40_PROT_CFG_RTS_TH_EN, 0);
2534         rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
2535
2536         rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
2537         rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_RATE, 0x4004);
2538         rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_CTRL, 0);
2539         rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_NAV_SHORT, 1);
2540         rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_CCK, 1);
2541         rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
2542         rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_MM20, 1);
2543         rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_MM40, 0);
2544         rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_GF20, 1);
2545         rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_GF40, 0);
2546         rt2x00_set_field32(&reg, GF20_PROT_CFG_RTS_TH_EN, 0);
2547         rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
2548
2549         rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
2550         rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_RATE, 0x4084);
2551         rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_CTRL, 0);
2552         rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_NAV_SHORT, 1);
2553         rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_CCK, 1);
2554         rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
2555         rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_MM20, 1);
2556         rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_MM40, 1);
2557         rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_GF20, 1);
2558         rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_GF40, 1);
2559         rt2x00_set_field32(&reg, GF40_PROT_CFG_RTS_TH_EN, 0);
2560         rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
2561
2562         if (rt2x00_is_usb(rt2x00dev)) {
2563                 rt2800_register_write(rt2x00dev, PBF_CFG, 0xf40006);
2564
2565                 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
2566                 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
2567                 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
2568                 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
2569                 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
2570                 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_WP_DMA_BURST_SIZE, 3);
2571                 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 0);
2572                 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_BIG_ENDIAN, 0);
2573                 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_HDR_SCATTER, 0);
2574                 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_HDR_SEG_LEN, 0);
2575                 rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
2576         }
2577
2578         /*
2579          * The legacy driver also sets TXOP_CTRL_CFG_RESERVED_TRUN_EN to 1
2580          * although it is reserved.
2581          */
2582         rt2800_register_read(rt2x00dev, TXOP_CTRL_CFG, &reg);
2583         rt2x00_set_field32(&reg, TXOP_CTRL_CFG_TIMEOUT_TRUN_EN, 1);
2584         rt2x00_set_field32(&reg, TXOP_CTRL_CFG_AC_TRUN_EN, 1);
2585         rt2x00_set_field32(&reg, TXOP_CTRL_CFG_TXRATEGRP_TRUN_EN, 1);
2586         rt2x00_set_field32(&reg, TXOP_CTRL_CFG_USER_MODE_TRUN_EN, 1);
2587         rt2x00_set_field32(&reg, TXOP_CTRL_CFG_MIMO_PS_TRUN_EN, 1);
2588         rt2x00_set_field32(&reg, TXOP_CTRL_CFG_RESERVED_TRUN_EN, 1);
2589         rt2x00_set_field32(&reg, TXOP_CTRL_CFG_LSIG_TXOP_EN, 0);
2590         rt2x00_set_field32(&reg, TXOP_CTRL_CFG_EXT_CCA_EN, 0);
2591         rt2x00_set_field32(&reg, TXOP_CTRL_CFG_EXT_CCA_DLY, 88);
2592         rt2x00_set_field32(&reg, TXOP_CTRL_CFG_EXT_CWMIN, 0);
2593         rt2800_register_write(rt2x00dev, TXOP_CTRL_CFG, reg);
2594
2595         rt2800_register_write(rt2x00dev, TXOP_HLDR_ET, 0x00000002);
2596
2597         rt2800_register_read(rt2x00dev, TX_RTS_CFG, &reg);
2598         rt2x00_set_field32(&reg, TX_RTS_CFG_AUTO_RTS_RETRY_LIMIT, 32);
2599         rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_THRES,
2600                            IEEE80211_MAX_RTS_THRESHOLD);
2601         rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_FBK_EN, 0);
2602         rt2800_register_write(rt2x00dev, TX_RTS_CFG, reg);
2603
2604         rt2800_register_write(rt2x00dev, EXP_ACK_TIME, 0x002400ca);
2605
2606         /*
2607          * Usually the CCK SIFS time should be set to 10 and the OFDM SIFS
2608          * time should be set to 16. However, the original Ralink driver uses
2609          * 16 for both and indeed using a value of 10 for CCK SIFS results in
2610          * connection problems with 11g + CTS protection. Hence, use the same
2611          * defaults as the Ralink driver: 16 for both, CCK and OFDM SIFS.
2612          */
2613         rt2800_register_read(rt2x00dev, XIFS_TIME_CFG, &reg);
2614         rt2x00_set_field32(&reg, XIFS_TIME_CFG_CCKM_SIFS_TIME, 16);
2615         rt2x00_set_field32(&reg, XIFS_TIME_CFG_OFDM_SIFS_TIME, 16);
2616         rt2x00_set_field32(&reg, XIFS_TIME_CFG_OFDM_XIFS_TIME, 4);
2617         rt2x00_set_field32(&reg, XIFS_TIME_CFG_EIFS, 314);
2618         rt2x00_set_field32(&reg, XIFS_TIME_CFG_BB_RXEND_ENABLE, 1);
2619         rt2800_register_write(rt2x00dev, XIFS_TIME_CFG, reg);
2620
2621         rt2800_register_write(rt2x00dev, PWR_PIN_CFG, 0x00000003);
2622
2623         /*
2624          * ASIC will keep garbage value after boot, clear encryption keys.
2625          */
2626         for (i = 0; i < 4; i++)
2627                 rt2800_register_write(rt2x00dev,
2628                                          SHARED_KEY_MODE_ENTRY(i), 0);
2629
2630         for (i = 0; i < 256; i++) {
2631                 static const u32 wcid[2] = { 0xffffffff, 0x00ffffff };
2632                 rt2800_register_multiwrite(rt2x00dev, MAC_WCID_ENTRY(i),
2633                                               wcid, sizeof(wcid));
2634
2635                 rt2800_register_write(rt2x00dev, MAC_WCID_ATTR_ENTRY(i), 0);
2636                 rt2800_register_write(rt2x00dev, MAC_IVEIV_ENTRY(i), 0);
2637         }
2638
2639         /*
2640          * Clear all beacons
2641          */
2642         rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE0);
2643         rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE1);
2644         rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE2);
2645         rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE3);
2646         rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE4);
2647         rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE5);
2648         rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE6);
2649         rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE7);
2650
2651         if (rt2x00_is_usb(rt2x00dev)) {
2652                 rt2800_register_read(rt2x00dev, US_CYC_CNT, &reg);
2653                 rt2x00_set_field32(&reg, US_CYC_CNT_CLOCK_CYCLE, 30);
2654                 rt2800_register_write(rt2x00dev, US_CYC_CNT, reg);
2655         } else if (rt2x00_is_pcie(rt2x00dev)) {
2656                 rt2800_register_read(rt2x00dev, US_CYC_CNT, &reg);
2657                 rt2x00_set_field32(&reg, US_CYC_CNT_CLOCK_CYCLE, 125);
2658                 rt2800_register_write(rt2x00dev, US_CYC_CNT, reg);
2659         }
2660
2661         rt2800_register_read(rt2x00dev, HT_FBK_CFG0, &reg);
2662         rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS0FBK, 0);
2663         rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS1FBK, 0);
2664         rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS2FBK, 1);
2665         rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS3FBK, 2);
2666         rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS4FBK, 3);
2667         rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS5FBK, 4);
2668         rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS6FBK, 5);
2669         rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS7FBK, 6);
2670         rt2800_register_write(rt2x00dev, HT_FBK_CFG0, reg);
2671
2672         rt2800_register_read(rt2x00dev, HT_FBK_CFG1, &reg);
2673         rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS8FBK, 8);
2674         rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS9FBK, 8);
2675         rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS10FBK, 9);
2676         rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS11FBK, 10);
2677         rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS12FBK, 11);
2678         rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS13FBK, 12);
2679         rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS14FBK, 13);
2680         rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS15FBK, 14);
2681         rt2800_register_write(rt2x00dev, HT_FBK_CFG1, reg);
2682
2683         rt2800_register_read(rt2x00dev, LG_FBK_CFG0, &reg);
2684         rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS0FBK, 8);
2685         rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS1FBK, 8);
2686         rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS2FBK, 9);
2687         rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS3FBK, 10);
2688         rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS4FBK, 11);
2689         rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS5FBK, 12);
2690         rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS6FBK, 13);
2691         rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS7FBK, 14);
2692         rt2800_register_write(rt2x00dev, LG_FBK_CFG0, reg);
2693
2694         rt2800_register_read(rt2x00dev, LG_FBK_CFG1, &reg);
2695         rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS0FBK, 0);
2696         rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS1FBK, 0);
2697         rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS2FBK, 1);
2698         rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS3FBK, 2);
2699         rt2800_register_write(rt2x00dev, LG_FBK_CFG1, reg);
2700
2701         /*
2702          * Do not force the BA window size, we use the TXWI to set it
2703          */
2704         rt2800_register_read(rt2x00dev, AMPDU_BA_WINSIZE, &reg);
2705         rt2x00_set_field32(&reg, AMPDU_BA_WINSIZE_FORCE_WINSIZE_ENABLE, 0);
2706         rt2x00_set_field32(&reg, AMPDU_BA_WINSIZE_FORCE_WINSIZE, 0);
2707         rt2800_register_write(rt2x00dev, AMPDU_BA_WINSIZE, reg);
2708
2709         /*
2710          * We must clear the error counters.
2711          * These registers are cleared on read,
2712          * so we may pass a useless variable to store the value.
2713          */
2714         rt2800_register_read(rt2x00dev, RX_STA_CNT0, &reg);
2715         rt2800_register_read(rt2x00dev, RX_STA_CNT1, &reg);
2716         rt2800_register_read(rt2x00dev, RX_STA_CNT2, &reg);
2717         rt2800_register_read(rt2x00dev, TX_STA_CNT0, &reg);
2718         rt2800_register_read(rt2x00dev, TX_STA_CNT1, &reg);
2719         rt2800_register_read(rt2x00dev, TX_STA_CNT2, &reg);
2720
2721         /*
2722          * Setup leadtime for pre tbtt interrupt to 6ms
2723          */
2724         rt2800_register_read(rt2x00dev, INT_TIMER_CFG, &reg);
2725         rt2x00_set_field32(&reg, INT_TIMER_CFG_PRE_TBTT_TIMER, 6 << 4);
2726         rt2800_register_write(rt2x00dev, INT_TIMER_CFG, reg);
2727
2728         /*
2729          * Set up channel statistics timer
2730          */
2731         rt2800_register_read(rt2x00dev, CH_TIME_CFG, &reg);
2732         rt2x00_set_field32(&reg, CH_TIME_CFG_EIFS_BUSY, 1);
2733         rt2x00_set_field32(&reg, CH_TIME_CFG_NAV_BUSY, 1);
2734         rt2x00_set_field32(&reg, CH_TIME_CFG_RX_BUSY, 1);
2735         rt2x00_set_field32(&reg, CH_TIME_CFG_TX_BUSY, 1);
2736         rt2x00_set_field32(&reg, CH_TIME_CFG_TMR_EN, 1);
2737         rt2800_register_write(rt2x00dev, CH_TIME_CFG, reg);
2738
2739         return 0;
2740 }
2741
2742 static int rt2800_wait_bbp_rf_ready(struct rt2x00_dev *rt2x00dev)
2743 {
2744         unsigned int i;
2745         u32 reg;
2746
2747         for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
2748                 rt2800_register_read(rt2x00dev, MAC_STATUS_CFG, &reg);
2749                 if (!rt2x00_get_field32(reg, MAC_STATUS_CFG_BBP_RF_BUSY))
2750                         return 0;
2751
2752                 udelay(REGISTER_BUSY_DELAY);
2753         }
2754
2755         ERROR(rt2x00dev, "BBP/RF register access failed, aborting.\n");
2756         return -EACCES;
2757 }
2758
2759 static int rt2800_wait_bbp_ready(struct rt2x00_dev *rt2x00dev)
2760 {
2761         unsigned int i;
2762         u8 value;
2763
2764         /*
2765          * BBP was enabled after firmware was loaded,
2766          * but we need to reactivate it now.
2767          */
2768         rt2800_register_write(rt2x00dev, H2M_BBP_AGENT, 0);
2769         rt2800_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
2770         msleep(1);
2771
2772         for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
2773                 rt2800_bbp_read(rt2x00dev, 0, &value);
2774                 if ((value != 0xff) && (value != 0x00))
2775                         return 0;
2776                 udelay(REGISTER_BUSY_DELAY);
2777         }
2778
2779         ERROR(rt2x00dev, "BBP register access failed, aborting.\n");
2780         return -EACCES;
2781 }
2782
2783 static int rt2800_init_bbp(struct rt2x00_dev *rt2x00dev)
2784 {
2785         unsigned int i;
2786         u16 eeprom;
2787         u8 reg_id;
2788         u8 value;
2789
2790         if (unlikely(rt2800_wait_bbp_rf_ready(rt2x00dev) ||
2791                      rt2800_wait_bbp_ready(rt2x00dev)))
2792                 return -EACCES;
2793
2794         if (rt2x00_rt(rt2x00dev, RT5390)) {
2795                 rt2800_bbp_read(rt2x00dev, 4, &value);
2796                 rt2x00_set_field8(&value, BBP4_MAC_IF_CTRL, 1);
2797                 rt2800_bbp_write(rt2x00dev, 4, value);
2798         }
2799
2800         if (rt2800_is_305x_soc(rt2x00dev) ||
2801             rt2x00_rt(rt2x00dev, RT5390))
2802                 rt2800_bbp_write(rt2x00dev, 31, 0x08);
2803
2804         rt2800_bbp_write(rt2x00dev, 65, 0x2c);
2805         rt2800_bbp_write(rt2x00dev, 66, 0x38);
2806
2807         if (rt2x00_rt(rt2x00dev, RT5390))
2808                 rt2800_bbp_write(rt2x00dev, 68, 0x0b);
2809
2810         if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C)) {
2811                 rt2800_bbp_write(rt2x00dev, 69, 0x16);
2812                 rt2800_bbp_write(rt2x00dev, 73, 0x12);
2813         } else if (rt2x00_rt(rt2x00dev, RT5390)) {
2814                 rt2800_bbp_write(rt2x00dev, 69, 0x12);
2815                 rt2800_bbp_write(rt2x00dev, 73, 0x13);
2816                 rt2800_bbp_write(rt2x00dev, 75, 0x46);
2817                 rt2800_bbp_write(rt2x00dev, 76, 0x28);
2818                 rt2800_bbp_write(rt2x00dev, 77, 0x59);
2819         } else {
2820                 rt2800_bbp_write(rt2x00dev, 69, 0x12);
2821                 rt2800_bbp_write(rt2x00dev, 73, 0x10);
2822         }
2823
2824         rt2800_bbp_write(rt2x00dev, 70, 0x0a);
2825
2826         if (rt2x00_rt(rt2x00dev, RT3070) ||
2827             rt2x00_rt(rt2x00dev, RT3071) ||
2828             rt2x00_rt(rt2x00dev, RT3090) ||
2829             rt2x00_rt(rt2x00dev, RT3390) ||
2830             rt2x00_rt(rt2x00dev, RT5390)) {
2831                 rt2800_bbp_write(rt2x00dev, 79, 0x13);
2832                 rt2800_bbp_write(rt2x00dev, 80, 0x05);
2833                 rt2800_bbp_write(rt2x00dev, 81, 0x33);
2834         } else if (rt2800_is_305x_soc(rt2x00dev)) {
2835                 rt2800_bbp_write(rt2x00dev, 78, 0x0e);
2836                 rt2800_bbp_write(rt2x00dev, 80, 0x08);
2837         } else {
2838                 rt2800_bbp_write(rt2x00dev, 81, 0x37);
2839         }
2840
2841         rt2800_bbp_write(rt2x00dev, 82, 0x62);
2842         if (rt2x00_rt(rt2x00dev, RT5390))
2843                 rt2800_bbp_write(rt2x00dev, 83, 0x7a);
2844         else
2845                 rt2800_bbp_write(rt2x00dev, 83, 0x6a);
2846
2847         if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860D))
2848                 rt2800_bbp_write(rt2x00dev, 84, 0x19);
2849         else if (rt2x00_rt(rt2x00dev, RT5390))
2850                 rt2800_bbp_write(rt2x00dev, 84, 0x9a);
2851         else
2852                 rt2800_bbp_write(rt2x00dev, 84, 0x99);
2853
2854         if (rt2x00_rt(rt2x00dev, RT5390))
2855                 rt2800_bbp_write(rt2x00dev, 86, 0x38);
2856         else
2857                 rt2800_bbp_write(rt2x00dev, 86, 0x00);
2858
2859         rt2800_bbp_write(rt2x00dev, 91, 0x04);
2860
2861         if (rt2x00_rt(rt2x00dev, RT5390))
2862                 rt2800_bbp_write(rt2x00dev, 92, 0x02);
2863         else
2864                 rt2800_bbp_write(rt2x00dev, 92, 0x00);
2865
2866         if (rt2x00_rt_rev_gte(rt2x00dev, RT3070, REV_RT3070F) ||
2867             rt2x00_rt_rev_gte(rt2x00dev, RT3071, REV_RT3071E) ||
2868             rt2x00_rt_rev_gte(rt2x00dev, RT3090, REV_RT3090E) ||
2869             rt2x00_rt_rev_gte(rt2x00dev, RT3390, REV_RT3390E) ||
2870             rt2x00_rt(rt2x00dev, RT5390) ||
2871             rt2800_is_305x_soc(rt2x00dev))
2872                 rt2800_bbp_write(rt2x00dev, 103, 0xc0);
2873         else
2874                 rt2800_bbp_write(rt2x00dev, 103, 0x00);
2875
2876         if (rt2x00_rt(rt2x00dev, RT5390))
2877                 rt2800_bbp_write(rt2x00dev, 104, 0x92);
2878
2879         if (rt2800_is_305x_soc(rt2x00dev))
2880                 rt2800_bbp_write(rt2x00dev, 105, 0x01);
2881         else if (rt2x00_rt(rt2x00dev, RT5390))
2882                 rt2800_bbp_write(rt2x00dev, 105, 0x3c);
2883         else
2884                 rt2800_bbp_write(rt2x00dev, 105, 0x05);
2885
2886         if (rt2x00_rt(rt2x00dev, RT5390))
2887                 rt2800_bbp_write(rt2x00dev, 106, 0x03);
2888         else
2889                 rt2800_bbp_write(rt2x00dev, 106, 0x35);
2890
2891         if (rt2x00_rt(rt2x00dev, RT5390))
2892                 rt2800_bbp_write(rt2x00dev, 128, 0x12);
2893
2894         if (rt2x00_rt(rt2x00dev, RT3071) ||
2895             rt2x00_rt(rt2x00dev, RT3090) ||
2896             rt2x00_rt(rt2x00dev, RT3390) ||
2897             rt2x00_rt(rt2x00dev, RT5390)) {
2898                 rt2800_bbp_read(rt2x00dev, 138, &value);
2899
2900                 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &eeprom);
2901                 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_TXPATH) == 1)
2902                         value |= 0x20;
2903                 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RXPATH) == 1)
2904                         value &= ~0x02;
2905
2906                 rt2800_bbp_write(rt2x00dev, 138, value);
2907         }
2908
2909         if (rt2x00_rt(rt2x00dev, RT5390)) {
2910                 int ant, div_mode;
2911
2912                 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &eeprom);
2913                 div_mode = rt2x00_get_field16(eeprom,
2914                                               EEPROM_NIC_CONF1_ANT_DIVERSITY);
2915                 ant = (div_mode == 3) ? 1 : 0;
2916
2917                 /* check if this is a Bluetooth combo card */
2918                 if (test_bit(CAPABILITY_BT_COEXIST, &rt2x00dev->cap_flags)) {
2919                         u32 reg;
2920
2921                         rt2800_register_read(rt2x00dev, GPIO_CTRL_CFG, &reg);
2922                         rt2x00_set_field32(&reg, GPIO_CTRL_CFG_GPIOD_BIT3, 0);
2923                         rt2x00_set_field32(&reg, GPIO_CTRL_CFG_GPIOD_BIT6, 0);
2924                         rt2x00_set_field32(&reg, GPIO_CTRL_CFG_BIT3, 0);
2925                         rt2x00_set_field32(&reg, GPIO_CTRL_CFG_BIT6, 0);
2926                         if (ant == 0)
2927                                 rt2x00_set_field32(&reg, GPIO_CTRL_CFG_BIT3, 1);
2928                         else if (ant == 1)
2929                                 rt2x00_set_field32(&reg, GPIO_CTRL_CFG_BIT6, 1);
2930                         rt2800_register_write(rt2x00dev, GPIO_CTRL_CFG, reg);
2931                 }
2932
2933                 rt2800_bbp_read(rt2x00dev, 152, &value);
2934                 if (ant == 0)
2935                         rt2x00_set_field8(&value, BBP152_RX_DEFAULT_ANT, 1);
2936                 else
2937                         rt2x00_set_field8(&value, BBP152_RX_DEFAULT_ANT, 0);
2938                 rt2800_bbp_write(rt2x00dev, 152, value);
2939
2940                 /* Init frequency calibration */
2941                 rt2800_bbp_write(rt2x00dev, 142, 1);
2942                 rt2800_bbp_write(rt2x00dev, 143, 57);
2943         }
2944
2945         for (i = 0; i < EEPROM_BBP_SIZE; i++) {
2946                 rt2x00_eeprom_read(rt2x00dev, EEPROM_BBP_START + i, &eeprom);
2947
2948                 if (eeprom != 0xffff && eeprom != 0x0000) {
2949                         reg_id = rt2x00_get_field16(eeprom, EEPROM_BBP_REG_ID);
2950                         value = rt2x00_get_field16(eeprom, EEPROM_BBP_VALUE);
2951                         rt2800_bbp_write(rt2x00dev, reg_id, value);
2952                 }
2953         }
2954
2955         return 0;
2956 }
2957
2958 static u8 rt2800_init_rx_filter(struct rt2x00_dev *rt2x00dev,
2959                                 bool bw40, u8 rfcsr24, u8 filter_target)
2960 {
2961         unsigned int i;
2962         u8 bbp;
2963         u8 rfcsr;
2964         u8 passband;
2965         u8 stopband;
2966         u8 overtuned = 0;
2967
2968         rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
2969
2970         rt2800_bbp_read(rt2x00dev, 4, &bbp);
2971         rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 2 * bw40);
2972         rt2800_bbp_write(rt2x00dev, 4, bbp);
2973
2974         rt2800_rfcsr_read(rt2x00dev, 31, &rfcsr);
2975         rt2x00_set_field8(&rfcsr, RFCSR31_RX_H20M, bw40);
2976         rt2800_rfcsr_write(rt2x00dev, 31, rfcsr);
2977
2978         rt2800_rfcsr_read(rt2x00dev, 22, &rfcsr);
2979         rt2x00_set_field8(&rfcsr, RFCSR22_BASEBAND_LOOPBACK, 1);
2980         rt2800_rfcsr_write(rt2x00dev, 22, rfcsr);
2981
2982         /*
2983          * Set power & frequency of passband test tone
2984          */
2985         rt2800_bbp_write(rt2x00dev, 24, 0);
2986
2987         for (i = 0; i < 100; i++) {
2988                 rt2800_bbp_write(rt2x00dev, 25, 0x90);
2989                 msleep(1);
2990
2991                 rt2800_bbp_read(rt2x00dev, 55, &passband);
2992                 if (passband)
2993                         break;
2994         }
2995
2996         /*
2997          * Set power & frequency of stopband test tone
2998          */
2999         rt2800_bbp_write(rt2x00dev, 24, 0x06);
3000
3001         for (i = 0; i < 100; i++) {
3002                 rt2800_bbp_write(rt2x00dev, 25, 0x90);
3003                 msleep(1);
3004
3005                 rt2800_bbp_read(rt2x00dev, 55, &stopband);
3006
3007                 if ((passband - stopband) <= filter_target) {
3008                         rfcsr24++;
3009                         overtuned += ((passband - stopband) == filter_target);
3010                 } else
3011                         break;
3012
3013                 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
3014         }
3015
3016         rfcsr24 -= !!overtuned;
3017
3018         rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
3019         return rfcsr24;
3020 }
3021
3022 static int rt2800_init_rfcsr(struct rt2x00_dev *rt2x00dev)
3023 {
3024         u8 rfcsr;
3025         u8 bbp;
3026         u32 reg;
3027         u16 eeprom;
3028
3029         if (!rt2x00_rt(rt2x00dev, RT3070) &&
3030             !rt2x00_rt(rt2x00dev, RT3071) &&
3031             !rt2x00_rt(rt2x00dev, RT3090) &&
3032             !rt2x00_rt(rt2x00dev, RT3390) &&
3033             !rt2x00_rt(rt2x00dev, RT5390) &&
3034             !rt2800_is_305x_soc(rt2x00dev))
3035                 return 0;
3036
3037         /*
3038          * Init RF calibration.
3039          */
3040         if (rt2x00_rt(rt2x00dev, RT5390)) {
3041                 rt2800_rfcsr_read(rt2x00dev, 2, &rfcsr);
3042                 rt2x00_set_field8(&rfcsr, RFCSR2_RESCAL_EN, 1);
3043                 rt2800_rfcsr_write(rt2x00dev, 2, rfcsr);
3044                 msleep(1);
3045                 rt2x00_set_field8(&rfcsr, RFCSR2_RESCAL_EN, 0);
3046                 rt2800_rfcsr_write(rt2x00dev, 2, rfcsr);
3047         } else {
3048                 rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
3049                 rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 1);
3050                 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
3051                 msleep(1);
3052                 rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 0);
3053                 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
3054         }
3055
3056         if (rt2x00_rt(rt2x00dev, RT3070) ||
3057             rt2x00_rt(rt2x00dev, RT3071) ||
3058             rt2x00_rt(rt2x00dev, RT3090)) {
3059                 rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
3060                 rt2800_rfcsr_write(rt2x00dev, 5, 0x03);
3061                 rt2800_rfcsr_write(rt2x00dev, 6, 0x02);
3062                 rt2800_rfcsr_write(rt2x00dev, 7, 0x60);
3063                 rt2800_rfcsr_write(rt2x00dev, 9, 0x0f);
3064                 rt2800_rfcsr_write(rt2x00dev, 10, 0x41);
3065                 rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
3066                 rt2800_rfcsr_write(rt2x00dev, 12, 0x7b);
3067                 rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
3068                 rt2800_rfcsr_write(rt2x00dev, 15, 0x58);
3069                 rt2800_rfcsr_write(rt2x00dev, 16, 0xb3);
3070                 rt2800_rfcsr_write(rt2x00dev, 17, 0x92);
3071                 rt2800_rfcsr_write(rt2x00dev, 18, 0x2c);
3072                 rt2800_rfcsr_write(rt2x00dev, 19, 0x02);
3073                 rt2800_rfcsr_write(rt2x00dev, 20, 0xba);
3074                 rt2800_rfcsr_write(rt2x00dev, 21, 0xdb);
3075                 rt2800_rfcsr_write(rt2x00dev, 24, 0x16);
3076                 rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
3077                 rt2800_rfcsr_write(rt2x00dev, 29, 0x1f);
3078         } else if (rt2x00_rt(rt2x00dev, RT3390)) {
3079                 rt2800_rfcsr_write(rt2x00dev, 0, 0xa0);
3080                 rt2800_rfcsr_write(rt2x00dev, 1, 0xe1);
3081                 rt2800_rfcsr_write(rt2x00dev, 2, 0xf1);
3082                 rt2800_rfcsr_write(rt2x00dev, 3, 0x62);
3083                 rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
3084                 rt2800_rfcsr_write(rt2x00dev, 5, 0x8b);
3085                 rt2800_rfcsr_write(rt2x00dev, 6, 0x42);
3086                 rt2800_rfcsr_write(rt2x00dev, 7, 0x34);
3087                 rt2800_rfcsr_write(rt2x00dev, 8, 0x00);
3088                 rt2800_rfcsr_write(rt2x00dev, 9, 0xc0);
3089                 rt2800_rfcsr_write(rt2x00dev, 10, 0x61);
3090                 rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
3091                 rt2800_rfcsr_write(rt2x00dev, 12, 0x3b);
3092                 rt2800_rfcsr_write(rt2x00dev, 13, 0xe0);
3093                 rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
3094                 rt2800_rfcsr_write(rt2x00dev, 15, 0x53);
3095                 rt2800_rfcsr_write(rt2x00dev, 16, 0xe0);
3096                 rt2800_rfcsr_write(rt2x00dev, 17, 0x94);
3097                 rt2800_rfcsr_write(rt2x00dev, 18, 0x5c);
3098                 rt2800_rfcsr_write(rt2x00dev, 19, 0x4a);
3099                 rt2800_rfcsr_write(rt2x00dev, 20, 0xb2);
3100                 rt2800_rfcsr_write(rt2x00dev, 21, 0xf6);
3101                 rt2800_rfcsr_write(rt2x00dev, 22, 0x00);
3102                 rt2800_rfcsr_write(rt2x00dev, 23, 0x14);
3103                 rt2800_rfcsr_write(rt2x00dev, 24, 0x08);
3104                 rt2800_rfcsr_write(rt2x00dev, 25, 0x3d);
3105                 rt2800_rfcsr_write(rt2x00dev, 26, 0x85);
3106                 rt2800_rfcsr_write(rt2x00dev, 27, 0x00);
3107                 rt2800_rfcsr_write(rt2x00dev, 28, 0x41);
3108                 rt2800_rfcsr_write(rt2x00dev, 29, 0x8f);
3109                 rt2800_rfcsr_write(rt2x00dev, 30, 0x20);
3110                 rt2800_rfcsr_write(rt2x00dev, 31, 0x0f);
3111         } else if (rt2800_is_305x_soc(rt2x00dev)) {
3112                 rt2800_rfcsr_write(rt2x00dev, 0, 0x50);
3113                 rt2800_rfcsr_write(rt2x00dev, 1, 0x01);
3114                 rt2800_rfcsr_write(rt2x00dev, 2, 0xf7);
3115                 rt2800_rfcsr_write(rt2x00dev, 3, 0x75);
3116                 rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
3117                 rt2800_rfcsr_write(rt2x00dev, 5, 0x03);
3118                 rt2800_rfcsr_write(rt2x00dev, 6, 0x02);
3119                 rt2800_rfcsr_write(rt2x00dev, 7, 0x50);
3120                 rt2800_rfcsr_write(rt2x00dev, 8, 0x39);
3121                 rt2800_rfcsr_write(rt2x00dev, 9, 0x0f);
3122                 rt2800_rfcsr_write(rt2x00dev, 10, 0x60);
3123                 rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
3124                 rt2800_rfcsr_write(rt2x00dev, 12, 0x75);
3125                 rt2800_rfcsr_write(rt2x00dev, 13, 0x75);
3126                 rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
3127                 rt2800_rfcsr_write(rt2x00dev, 15, 0x58);
3128                 rt2800_rfcsr_write(rt2x00dev, 16, 0xb3);
3129                 rt2800_rfcsr_write(rt2x00dev, 17, 0x92);
3130                 rt2800_rfcsr_write(rt2x00dev, 18, 0x2c);
3131                 rt2800_rfcsr_write(rt2x00dev, 19, 0x02);
3132                 rt2800_rfcsr_write(rt2x00dev, 20, 0xba);
3133                 rt2800_rfcsr_write(rt2x00dev, 21, 0xdb);
3134                 rt2800_rfcsr_write(rt2x00dev, 22, 0x00);
3135                 rt2800_rfcsr_write(rt2x00dev, 23, 0x31);
3136                 rt2800_rfcsr_write(rt2x00dev, 24, 0x08);
3137                 rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
3138                 rt2800_rfcsr_write(rt2x00dev, 26, 0x25);
3139                 rt2800_rfcsr_write(rt2x00dev, 27, 0x23);
3140                 rt2800_rfcsr_write(rt2x00dev, 28, 0x13);
3141                 rt2800_rfcsr_write(rt2x00dev, 29, 0x83);
3142                 rt2800_rfcsr_write(rt2x00dev, 30, 0x00);
3143                 rt2800_rfcsr_write(rt2x00dev, 31, 0x00);
3144                 return 0;
3145         } else if (rt2x00_rt(rt2x00dev, RT5390)) {
3146                 rt2800_rfcsr_write(rt2x00dev, 1, 0x0f);
3147                 rt2800_rfcsr_write(rt2x00dev, 2, 0x80);
3148                 rt2800_rfcsr_write(rt2x00dev, 3, 0x88);
3149                 rt2800_rfcsr_write(rt2x00dev, 5, 0x10);
3150                 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
3151                         rt2800_rfcsr_write(rt2x00dev, 6, 0xe0);
3152                 else
3153                         rt2800_rfcsr_write(rt2x00dev, 6, 0xa0);
3154                 rt2800_rfcsr_write(rt2x00dev, 7, 0x00);
3155                 rt2800_rfcsr_write(rt2x00dev, 10, 0x53);
3156                 rt2800_rfcsr_write(rt2x00dev, 11, 0x4a);
3157                 rt2800_rfcsr_write(rt2x00dev, 12, 0xc6);
3158                 rt2800_rfcsr_write(rt2x00dev, 13, 0x9f);
3159                 rt2800_rfcsr_write(rt2x00dev, 14, 0x00);
3160                 rt2800_rfcsr_write(rt2x00dev, 15, 0x00);
3161                 rt2800_rfcsr_write(rt2x00dev, 16, 0x00);
3162                 rt2800_rfcsr_write(rt2x00dev, 18, 0x03);
3163                 rt2800_rfcsr_write(rt2x00dev, 19, 0x00);
3164
3165                 rt2800_rfcsr_write(rt2x00dev, 20, 0x00);
3166                 rt2800_rfcsr_write(rt2x00dev, 21, 0x00);
3167                 rt2800_rfcsr_write(rt2x00dev, 22, 0x20);
3168                 rt2800_rfcsr_write(rt2x00dev, 23, 0x00);
3169                 rt2800_rfcsr_write(rt2x00dev, 24, 0x00);
3170                 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
3171                         rt2800_rfcsr_write(rt2x00dev, 25, 0x80);
3172                 else
3173                         rt2800_rfcsr_write(rt2x00dev, 25, 0xc0);
3174                 rt2800_rfcsr_write(rt2x00dev, 26, 0x00);
3175                 rt2800_rfcsr_write(rt2x00dev, 27, 0x09);
3176                 rt2800_rfcsr_write(rt2x00dev, 28, 0x00);
3177                 rt2800_rfcsr_write(rt2x00dev, 29, 0x10);
3178
3179                 rt2800_rfcsr_write(rt2x00dev, 30, 0x00);
3180                 rt2800_rfcsr_write(rt2x00dev, 31, 0x80);
3181                 rt2800_rfcsr_write(rt2x00dev, 32, 0x80);
3182                 rt2800_rfcsr_write(rt2x00dev, 33, 0x00);
3183                 rt2800_rfcsr_write(rt2x00dev, 34, 0x07);
3184                 rt2800_rfcsr_write(rt2x00dev, 35, 0x12);
3185                 rt2800_rfcsr_write(rt2x00dev, 36, 0x00);
3186                 rt2800_rfcsr_write(rt2x00dev, 37, 0x08);
3187                 rt2800_rfcsr_write(rt2x00dev, 38, 0x85);
3188                 rt2800_rfcsr_write(rt2x00dev, 39, 0x1b);
3189
3190                 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
3191                         rt2800_rfcsr_write(rt2x00dev, 40, 0x0b);
3192                 else
3193                         rt2800_rfcsr_write(rt2x00dev, 40, 0x4b);
3194                 rt2800_rfcsr_write(rt2x00dev, 41, 0xbb);
3195                 rt2800_rfcsr_write(rt2x00dev, 42, 0xd2);
3196                 rt2800_rfcsr_write(rt2x00dev, 43, 0x9a);
3197                 rt2800_rfcsr_write(rt2x00dev, 44, 0x0e);
3198                 rt2800_rfcsr_write(rt2x00dev, 45, 0xa2);
3199                 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
3200                         rt2800_rfcsr_write(rt2x00dev, 46, 0x73);
3201                 else
3202                         rt2800_rfcsr_write(rt2x00dev, 46, 0x7b);
3203                 rt2800_rfcsr_write(rt2x00dev, 47, 0x00);
3204                 rt2800_rfcsr_write(rt2x00dev, 48, 0x10);
3205                 rt2800_rfcsr_write(rt2x00dev, 49, 0x94);
3206
3207                 rt2800_rfcsr_write(rt2x00dev, 52, 0x38);
3208                 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
3209                         rt2800_rfcsr_write(rt2x00dev, 53, 0x00);
3210                 else
3211                         rt2800_rfcsr_write(rt2x00dev, 53, 0x84);
3212                 rt2800_rfcsr_write(rt2x00dev, 54, 0x78);
3213                 rt2800_rfcsr_write(rt2x00dev, 55, 0x44);
3214                 rt2800_rfcsr_write(rt2x00dev, 56, 0x22);
3215                 rt2800_rfcsr_write(rt2x00dev, 57, 0x80);
3216                 rt2800_rfcsr_write(rt2x00dev, 58, 0x7f);
3217                 rt2800_rfcsr_write(rt2x00dev, 59, 0x63);
3218
3219                 rt2800_rfcsr_write(rt2x00dev, 60, 0x45);
3220                 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
3221                         rt2800_rfcsr_write(rt2x00dev, 61, 0xd1);
3222                 else
3223                         rt2800_rfcsr_write(rt2x00dev, 61, 0xdd);
3224                 rt2800_rfcsr_write(rt2x00dev, 62, 0x00);
3225                 rt2800_rfcsr_write(rt2x00dev, 63, 0x00);
3226         }
3227
3228         if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F)) {
3229                 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
3230                 rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
3231                 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 3);
3232                 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
3233         } else if (rt2x00_rt(rt2x00dev, RT3071) ||
3234                    rt2x00_rt(rt2x00dev, RT3090)) {
3235                 rt2800_rfcsr_write(rt2x00dev, 31, 0x14);
3236
3237                 rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
3238                 rt2x00_set_field8(&rfcsr, RFCSR6_R2, 1);
3239                 rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
3240
3241                 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
3242                 rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
3243                 if (rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
3244                     rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E)) {
3245                         rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &eeprom);
3246                         if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_DAC_TEST))
3247                                 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 3);
3248                         else
3249                                 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 0);
3250                 }
3251                 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
3252
3253                 rt2800_register_read(rt2x00dev, GPIO_SWITCH, &reg);
3254                 rt2x00_set_field32(&reg, GPIO_SWITCH_5, 0);
3255                 rt2800_register_write(rt2x00dev, GPIO_SWITCH, reg);
3256         } else if (rt2x00_rt(rt2x00dev, RT3390)) {
3257                 rt2800_register_read(rt2x00dev, GPIO_SWITCH, &reg);
3258                 rt2x00_set_field32(&reg, GPIO_SWITCH_5, 0);
3259                 rt2800_register_write(rt2x00dev, GPIO_SWITCH, reg);
3260         }
3261
3262         /*
3263          * Set RX Filter calibration for 20MHz and 40MHz
3264          */
3265         if (rt2x00_rt(rt2x00dev, RT3070)) {
3266                 rt2x00dev->calibration[0] =
3267                         rt2800_init_rx_filter(rt2x00dev, false, 0x07, 0x16);
3268                 rt2x00dev->calibration[1] =
3269                         rt2800_init_rx_filter(rt2x00dev, true, 0x27, 0x19);
3270         } else if (rt2x00_rt(rt2x00dev, RT3071) ||
3271                    rt2x00_rt(rt2x00dev, RT3090) ||
3272                    rt2x00_rt(rt2x00dev, RT3390)) {
3273                 rt2x00dev->calibration[0] =
3274                         rt2800_init_rx_filter(rt2x00dev, false, 0x07, 0x13);
3275                 rt2x00dev->calibration[1] =
3276                         rt2800_init_rx_filter(rt2x00dev, true, 0x27, 0x15);
3277         }
3278
3279         if (!rt2x00_rt(rt2x00dev, RT5390)) {
3280                 /*
3281                  * Set back to initial state
3282                  */
3283                 rt2800_bbp_write(rt2x00dev, 24, 0);
3284
3285                 rt2800_rfcsr_read(rt2x00dev, 22, &rfcsr);
3286                 rt2x00_set_field8(&rfcsr, RFCSR22_BASEBAND_LOOPBACK, 0);
3287                 rt2800_rfcsr_write(rt2x00dev, 22, rfcsr);
3288
3289                 /*
3290                  * Set BBP back to BW20
3291                  */
3292                 rt2800_bbp_read(rt2x00dev, 4, &bbp);
3293                 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 0);
3294                 rt2800_bbp_write(rt2x00dev, 4, bbp);
3295         }
3296
3297         if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F) ||
3298             rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
3299             rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E) ||
3300             rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E))
3301                 rt2800_rfcsr_write(rt2x00dev, 27, 0x03);
3302
3303         rt2800_register_read(rt2x00dev, OPT_14_CSR, &reg);
3304         rt2x00_set_field32(&reg, OPT_14_CSR_BIT0, 1);
3305         rt2800_register_write(rt2x00dev, OPT_14_CSR, reg);
3306
3307         if (!rt2x00_rt(rt2x00dev, RT5390)) {
3308                 rt2800_rfcsr_read(rt2x00dev, 17, &rfcsr);
3309                 rt2x00_set_field8(&rfcsr, RFCSR17_TX_LO1_EN, 0);
3310                 if (rt2x00_rt(rt2x00dev, RT3070) ||
3311                     rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
3312                     rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E) ||
3313                     rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E)) {
3314                         if (!test_bit(CAPABILITY_EXTERNAL_LNA_BG,
3315                                       &rt2x00dev->cap_flags))
3316                                 rt2x00_set_field8(&rfcsr, RFCSR17_R, 1);
3317                 }
3318                 rt2x00_eeprom_read(rt2x00dev, EEPROM_TXMIXER_GAIN_BG, &eeprom);
3319                 if (rt2x00_get_field16(eeprom, EEPROM_TXMIXER_GAIN_BG_VAL) >= 1)
3320                         rt2x00_set_field8(&rfcsr, RFCSR17_TXMIXER_GAIN,
3321                                         rt2x00_get_field16(eeprom,
3322                                                 EEPROM_TXMIXER_GAIN_BG_VAL));
3323                 rt2800_rfcsr_write(rt2x00dev, 17, rfcsr);
3324         }
3325
3326         if (rt2x00_rt(rt2x00dev, RT3090)) {
3327                 rt2800_bbp_read(rt2x00dev, 138, &bbp);
3328
3329                 /*  Turn off unused DAC1 and ADC1 to reduce power consumption */
3330                 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &eeprom);
3331                 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RXPATH) == 1)
3332                         rt2x00_set_field8(&bbp, BBP138_RX_ADC1, 0);
3333                 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_TXPATH) == 1)
3334                         rt2x00_set_field8(&bbp, BBP138_TX_DAC1, 1);
3335
3336                 rt2800_bbp_write(rt2x00dev, 138, bbp);
3337         }
3338
3339         if (rt2x00_rt(rt2x00dev, RT3071) ||
3340             rt2x00_rt(rt2x00dev, RT3090) ||
3341             rt2x00_rt(rt2x00dev, RT3390)) {
3342                 rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
3343                 rt2x00_set_field8(&rfcsr, RFCSR1_RF_BLOCK_EN, 1);
3344                 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 0);
3345                 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 0);
3346                 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 1);
3347                 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 1);
3348                 rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
3349
3350                 rt2800_rfcsr_read(rt2x00dev, 15, &rfcsr);
3351                 rt2x00_set_field8(&rfcsr, RFCSR15_TX_LO2_EN, 0);
3352                 rt2800_rfcsr_write(rt2x00dev, 15, rfcsr);
3353
3354                 rt2800_rfcsr_read(rt2x00dev, 20, &rfcsr);
3355                 rt2x00_set_field8(&rfcsr, RFCSR20_RX_LO1_EN, 0);
3356                 rt2800_rfcsr_write(rt2x00dev, 20, rfcsr);
3357
3358                 rt2800_rfcsr_read(rt2x00dev, 21, &rfcsr);
3359                 rt2x00_set_field8(&rfcsr, RFCSR21_RX_LO2_EN, 0);
3360                 rt2800_rfcsr_write(rt2x00dev, 21, rfcsr);
3361         }
3362
3363         if (rt2x00_rt(rt2x00dev, RT3070)) {
3364                 rt2800_rfcsr_read(rt2x00dev, 27, &rfcsr);
3365                 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F))
3366                         rt2x00_set_field8(&rfcsr, RFCSR27_R1, 3);
3367                 else
3368                         rt2x00_set_field8(&rfcsr, RFCSR27_R1, 0);
3369                 rt2x00_set_field8(&rfcsr, RFCSR27_R2, 0);
3370                 rt2x00_set_field8(&rfcsr, RFCSR27_R3, 0);
3371                 rt2x00_set_field8(&rfcsr, RFCSR27_R4, 0);
3372                 rt2800_rfcsr_write(rt2x00dev, 27, rfcsr);
3373         }
3374
3375         if (rt2x00_rt(rt2x00dev, RT5390)) {
3376                 rt2800_rfcsr_read(rt2x00dev, 38, &rfcsr);
3377                 rt2x00_set_field8(&rfcsr, RFCSR38_RX_LO1_EN, 0);
3378                 rt2800_rfcsr_write(rt2x00dev, 38, rfcsr);
3379
3380                 rt2800_rfcsr_read(rt2x00dev, 39, &rfcsr);
3381                 rt2x00_set_field8(&rfcsr, RFCSR39_RX_LO2_EN, 0);
3382                 rt2800_rfcsr_write(rt2x00dev, 39, rfcsr);
3383
3384                 rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
3385                 rt2x00_set_field8(&rfcsr, RFCSR30_RX_VCM, 2);
3386                 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
3387         }
3388
3389         return 0;
3390 }
3391
3392 int rt2800_enable_radio(struct rt2x00_dev *rt2x00dev)
3393 {
3394         u32 reg;
3395         u16 word;
3396
3397         /*
3398          * Initialize all registers.
3399          */
3400         if (unlikely(rt2800_wait_wpdma_ready(rt2x00dev) ||
3401                      rt2800_init_registers(rt2x00dev) ||
3402                      rt2800_init_bbp(rt2x00dev) ||
3403                      rt2800_init_rfcsr(rt2x00dev)))
3404                 return -EIO;
3405
3406         /*
3407          * Send signal to firmware during boot time.
3408          */
3409         rt2800_mcu_request(rt2x00dev, MCU_BOOT_SIGNAL, 0, 0, 0);
3410
3411         if (rt2x00_is_usb(rt2x00dev) &&
3412             (rt2x00_rt(rt2x00dev, RT3070) ||
3413              rt2x00_rt(rt2x00dev, RT3071) ||
3414              rt2x00_rt(rt2x00dev, RT3572))) {
3415                 udelay(200);
3416                 rt2800_mcu_request(rt2x00dev, MCU_CURRENT, 0, 0, 0);
3417                 udelay(10);
3418         }
3419
3420         /*
3421          * Enable RX.
3422          */
3423         rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
3424         rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 1);
3425         rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 0);
3426         rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
3427
3428         udelay(50);
3429
3430         rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
3431         rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 1);
3432         rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 1);
3433         rt2x00_set_field32(&reg, WPDMA_GLO_CFG_WP_DMA_BURST_SIZE, 2);
3434         rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
3435         rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
3436
3437         rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
3438         rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 1);
3439         rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 1);
3440         rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
3441
3442         /*
3443          * Initialize LED control
3444          */
3445         rt2x00_eeprom_read(rt2x00dev, EEPROM_LED_AG_CONF, &word);
3446         rt2800_mcu_request(rt2x00dev, MCU_LED_AG_CONF, 0xff,
3447                            word & 0xff, (word >> 8) & 0xff);
3448
3449         rt2x00_eeprom_read(rt2x00dev, EEPROM_LED_ACT_CONF, &word);
3450         rt2800_mcu_request(rt2x00dev, MCU_LED_ACT_CONF, 0xff,
3451                            word & 0xff, (word >> 8) & 0xff);
3452
3453         rt2x00_eeprom_read(rt2x00dev, EEPROM_LED_POLARITY, &word);
3454         rt2800_mcu_request(rt2x00dev, MCU_LED_LED_POLARITY, 0xff,
3455                            word & 0xff, (word >> 8) & 0xff);
3456
3457         return 0;
3458 }
3459 EXPORT_SYMBOL_GPL(rt2800_enable_radio);
3460
3461 void rt2800_disable_radio(struct rt2x00_dev *rt2x00dev)
3462 {
3463         u32 reg;
3464
3465         rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
3466         rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
3467         rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
3468         rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
3469
3470         /* Wait for DMA, ignore error */
3471         rt2800_wait_wpdma_ready(rt2x00dev);
3472
3473         rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
3474         rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 0);
3475         rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 0);
3476         rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
3477 }
3478 EXPORT_SYMBOL_GPL(rt2800_disable_radio);
3479
3480 int rt2800_efuse_detect(struct rt2x00_dev *rt2x00dev)
3481 {
3482         u32 reg;
3483
3484         rt2800_register_read(rt2x00dev, EFUSE_CTRL, &reg);
3485
3486         return rt2x00_get_field32(reg, EFUSE_CTRL_PRESENT);
3487 }
3488 EXPORT_SYMBOL_GPL(rt2800_efuse_detect);
3489
3490 static void rt2800_efuse_read(struct rt2x00_dev *rt2x00dev, unsigned int i)
3491 {
3492         u32 reg;
3493
3494         mutex_lock(&rt2x00dev->csr_mutex);
3495
3496         rt2800_register_read_lock(rt2x00dev, EFUSE_CTRL, &reg);
3497         rt2x00_set_field32(&reg, EFUSE_CTRL_ADDRESS_IN, i);
3498         rt2x00_set_field32(&reg, EFUSE_CTRL_MODE, 0);
3499         rt2x00_set_field32(&reg, EFUSE_CTRL_KICK, 1);
3500         rt2800_register_write_lock(rt2x00dev, EFUSE_CTRL, reg);
3501
3502         /* Wait until the EEPROM has been loaded */
3503         rt2800_regbusy_read(rt2x00dev, EFUSE_CTRL, EFUSE_CTRL_KICK, &reg);
3504
3505         /* Apparently the data is read from end to start */
3506         rt2800_register_read_lock(rt2x00dev, EFUSE_DATA3,
3507                                         (u32 *)&rt2x00dev->eeprom[i]);
3508         rt2800_register_read_lock(rt2x00dev, EFUSE_DATA2,
3509                                         (u32 *)&rt2x00dev->eeprom[i + 2]);
3510         rt2800_register_read_lock(rt2x00dev, EFUSE_DATA1,
3511                                         (u32 *)&rt2x00dev->eeprom[i + 4]);
3512         rt2800_register_read_lock(rt2x00dev, EFUSE_DATA0,
3513                                         (u32 *)&rt2x00dev->eeprom[i + 6]);
3514
3515         mutex_unlock(&rt2x00dev->csr_mutex);
3516 }
3517
3518 void rt2800_read_eeprom_efuse(struct rt2x00_dev *rt2x00dev)
3519 {
3520         unsigned int i;
3521
3522         for (i = 0; i < EEPROM_SIZE / sizeof(u16); i += 8)
3523                 rt2800_efuse_read(rt2x00dev, i);
3524 }
3525 EXPORT_SYMBOL_GPL(rt2800_read_eeprom_efuse);
3526
3527 int rt2800_validate_eeprom(struct rt2x00_dev *rt2x00dev)
3528 {
3529         u16 word;
3530         u8 *mac;
3531         u8 default_lna_gain;
3532
3533         /*
3534          * Start validation of the data that has been read.
3535          */
3536         mac = rt2x00_eeprom_addr(rt2x00dev, EEPROM_MAC_ADDR_0);
3537         if (!is_valid_ether_addr(mac)) {
3538                 random_ether_addr(mac);
3539                 EEPROM(rt2x00dev, "MAC: %pM\n", mac);
3540         }
3541
3542         rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &word);
3543         if (word == 0xffff) {
3544                 rt2x00_set_field16(&word, EEPROM_NIC_CONF0_RXPATH, 2);
3545                 rt2x00_set_field16(&word, EEPROM_NIC_CONF0_TXPATH, 1);
3546                 rt2x00_set_field16(&word, EEPROM_NIC_CONF0_RF_TYPE, RF2820);
3547                 rt2x00_eeprom_write(rt2x00dev, EEPROM_NIC_CONF0, word);
3548                 EEPROM(rt2x00dev, "Antenna: 0x%04x\n", word);
3549         } else if (rt2x00_rt(rt2x00dev, RT2860) ||
3550                    rt2x00_rt(rt2x00dev, RT2872)) {
3551                 /*
3552                  * There is a max of 2 RX streams for RT28x0 series
3553                  */
3554                 if (rt2x00_get_field16(word, EEPROM_NIC_CONF0_RXPATH) > 2)
3555                         rt2x00_set_field16(&word, EEPROM_NIC_CONF0_RXPATH, 2);
3556                 rt2x00_eeprom_write(rt2x00dev, EEPROM_NIC_CONF0, word);
3557         }
3558
3559         rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &word);
3560         if (word == 0xffff) {
3561                 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_HW_RADIO, 0);
3562                 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_EXTERNAL_TX_ALC, 0);
3563                 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_EXTERNAL_LNA_2G, 0);
3564                 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_EXTERNAL_LNA_5G, 0);
3565                 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_CARDBUS_ACCEL, 0);
3566                 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BW40M_SB_2G, 0);
3567                 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BW40M_SB_5G, 0);
3568                 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_WPS_PBC, 0);
3569                 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BW40M_2G, 0);
3570                 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BW40M_5G, 0);
3571                 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BROADBAND_EXT_LNA, 0);
3572                 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_ANT_DIVERSITY, 0);
3573                 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_INTERNAL_TX_ALC, 0);
3574                 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BT_COEXIST, 0);
3575                 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_DAC_TEST, 0);
3576                 rt2x00_eeprom_write(rt2x00dev, EEPROM_NIC_CONF1, word);
3577                 EEPROM(rt2x00dev, "NIC: 0x%04x\n", word);
3578         }
3579
3580         rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &word);
3581         if ((word & 0x00ff) == 0x00ff) {
3582                 rt2x00_set_field16(&word, EEPROM_FREQ_OFFSET, 0);
3583                 rt2x00_eeprom_write(rt2x00dev, EEPROM_FREQ, word);
3584                 EEPROM(rt2x00dev, "Freq: 0x%04x\n", word);
3585         }
3586         if ((word & 0xff00) == 0xff00) {
3587                 rt2x00_set_field16(&word, EEPROM_FREQ_LED_MODE,
3588                                    LED_MODE_TXRX_ACTIVITY);
3589                 rt2x00_set_field16(&word, EEPROM_FREQ_LED_POLARITY, 0);
3590                 rt2x00_eeprom_write(rt2x00dev, EEPROM_FREQ, word);
3591                 rt2x00_eeprom_write(rt2x00dev, EEPROM_LED_AG_CONF, 0x5555);
3592                 rt2x00_eeprom_write(rt2x00dev, EEPROM_LED_ACT_CONF, 0x2221);
3593                 rt2x00_eeprom_write(rt2x00dev, EEPROM_LED_POLARITY, 0xa9f8);
3594                 EEPROM(rt2x00dev, "Led Mode: 0x%04x\n", word);
3595         }
3596
3597         /*
3598          * During the LNA validation we are going to use
3599          * lna0 as correct value. Note that EEPROM_LNA
3600          * is never validated.
3601          */
3602         rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &word);
3603         default_lna_gain = rt2x00_get_field16(word, EEPROM_LNA_A0);
3604
3605         rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG, &word);
3606         if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG_OFFSET0)) > 10)
3607                 rt2x00_set_field16(&word, EEPROM_RSSI_BG_OFFSET0, 0);
3608         if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG_OFFSET1)) > 10)
3609                 rt2x00_set_field16(&word, EEPROM_RSSI_BG_OFFSET1, 0);
3610         rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_BG, word);
3611
3612         rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &word);
3613         if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG2_OFFSET2)) > 10)
3614                 rt2x00_set_field16(&word, EEPROM_RSSI_BG2_OFFSET2, 0);
3615         if (rt2x00_get_field16(word, EEPROM_RSSI_BG2_LNA_A1) == 0x00 ||
3616             rt2x00_get_field16(word, EEPROM_RSSI_BG2_LNA_A1) == 0xff)
3617                 rt2x00_set_field16(&word, EEPROM_RSSI_BG2_LNA_A1,
3618                                    default_lna_gain);
3619         rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_BG2, word);
3620
3621         rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A, &word);
3622         if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A_OFFSET0)) > 10)
3623                 rt2x00_set_field16(&word, EEPROM_RSSI_A_OFFSET0, 0);
3624         if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A_OFFSET1)) > 10)
3625                 rt2x00_set_field16(&word, EEPROM_RSSI_A_OFFSET1, 0);
3626         rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_A, word);
3627
3628         rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &word);
3629         if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A2_OFFSET2)) > 10)
3630                 rt2x00_set_field16(&word, EEPROM_RSSI_A2_OFFSET2, 0);
3631         if (rt2x00_get_field16(word, EEPROM_RSSI_A2_LNA_A2) == 0x00 ||
3632             rt2x00_get_field16(word, EEPROM_RSSI_A2_LNA_A2) == 0xff)
3633                 rt2x00_set_field16(&word, EEPROM_RSSI_A2_LNA_A2,
3634                                    default_lna_gain);
3635         rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_A2, word);
3636
3637         return 0;
3638 }
3639 EXPORT_SYMBOL_GPL(rt2800_validate_eeprom);
3640
3641 int rt2800_init_eeprom(struct rt2x00_dev *rt2x00dev)
3642 {
3643         u32 reg;
3644         u16 value;
3645         u16 eeprom;
3646
3647         /*
3648          * Read EEPROM word for configuration.
3649          */
3650         rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &eeprom);
3651
3652         /*
3653          * Identify RF chipset by EEPROM value
3654          * RT28xx/RT30xx: defined in "EEPROM_NIC_CONF0_RF_TYPE" field
3655          * RT53xx: defined in "EEPROM_CHIP_ID" field
3656          */
3657         rt2800_register_read(rt2x00dev, MAC_CSR0, &reg);
3658         if (rt2x00_get_field32(reg, MAC_CSR0_CHIPSET) == RT5390)
3659                 rt2x00_eeprom_read(rt2x00dev, EEPROM_CHIP_ID, &value);
3660         else
3661                 value = rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RF_TYPE);
3662
3663         rt2x00_set_chip(rt2x00dev, rt2x00_get_field32(reg, MAC_CSR0_CHIPSET),
3664                         value, rt2x00_get_field32(reg, MAC_CSR0_REVISION));
3665
3666         if (!rt2x00_rt(rt2x00dev, RT2860) &&
3667             !rt2x00_rt(rt2x00dev, RT2872) &&
3668             !rt2x00_rt(rt2x00dev, RT2883) &&
3669             !rt2x00_rt(rt2x00dev, RT3070) &&
3670             !rt2x00_rt(rt2x00dev, RT3071) &&
3671             !rt2x00_rt(rt2x00dev, RT3090) &&
3672             !rt2x00_rt(rt2x00dev, RT3390) &&
3673             !rt2x00_rt(rt2x00dev, RT3572) &&
3674             !rt2x00_rt(rt2x00dev, RT5390)) {
3675                 ERROR(rt2x00dev, "Invalid RT chipset detected.\n");
3676                 return -ENODEV;
3677         }
3678
3679         if (!rt2x00_rf(rt2x00dev, RF2820) &&
3680             !rt2x00_rf(rt2x00dev, RF2850) &&
3681             !rt2x00_rf(rt2x00dev, RF2720) &&
3682             !rt2x00_rf(rt2x00dev, RF2750) &&
3683             !rt2x00_rf(rt2x00dev, RF3020) &&
3684             !rt2x00_rf(rt2x00dev, RF2020) &&
3685             !rt2x00_rf(rt2x00dev, RF3021) &&
3686             !rt2x00_rf(rt2x00dev, RF3022) &&
3687             !rt2x00_rf(rt2x00dev, RF3052) &&
3688             !rt2x00_rf(rt2x00dev, RF3320) &&
3689             !rt2x00_rf(rt2x00dev, RF5390)) {
3690                 ERROR(rt2x00dev, "Invalid RF chipset detected.\n");
3691                 return -ENODEV;
3692         }
3693
3694         /*
3695          * Identify default antenna configuration.
3696          */
3697         rt2x00dev->default_ant.tx_chain_num =
3698             rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_TXPATH);
3699         rt2x00dev->default_ant.rx_chain_num =
3700             rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RXPATH);
3701
3702         rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &eeprom);
3703
3704         if (rt2x00_rt(rt2x00dev, RT3070) ||
3705             rt2x00_rt(rt2x00dev, RT3090) ||
3706             rt2x00_rt(rt2x00dev, RT3390)) {
3707                 value = rt2x00_get_field16(eeprom,
3708                                 EEPROM_NIC_CONF1_ANT_DIVERSITY);
3709                 switch (value) {
3710                 case 0:
3711                 case 1:
3712                 case 2:
3713                         rt2x00dev->default_ant.tx = ANTENNA_A;
3714                         rt2x00dev->default_ant.rx = ANTENNA_A;
3715                         break;
3716                 case 3:
3717                         rt2x00dev->default_ant.tx = ANTENNA_A;
3718                         rt2x00dev->default_ant.rx = ANTENNA_B;
3719                         break;
3720                 }
3721         } else {
3722                 rt2x00dev->default_ant.tx = ANTENNA_A;
3723                 rt2x00dev->default_ant.rx = ANTENNA_A;
3724         }
3725
3726         /*
3727          * Determine external LNA informations.
3728          */
3729         if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_EXTERNAL_LNA_5G))
3730                 __set_bit(CAPABILITY_EXTERNAL_LNA_A, &rt2x00dev->cap_flags);
3731         if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_EXTERNAL_LNA_2G))
3732                 __set_bit(CAPABILITY_EXTERNAL_LNA_BG, &rt2x00dev->cap_flags);
3733
3734         /*
3735          * Detect if this device has an hardware controlled radio.
3736          */
3737         if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_HW_RADIO))
3738                 __set_bit(CAPABILITY_HW_BUTTON, &rt2x00dev->cap_flags);
3739
3740         /*
3741          * Detect if this device has Bluetooth co-existence.
3742          */
3743         if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_BT_COEXIST))
3744                 __set_bit(CAPABILITY_BT_COEXIST, &rt2x00dev->cap_flags);
3745
3746         /*
3747          * Read frequency offset and RF programming sequence.
3748          */
3749         rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &eeprom);
3750         rt2x00dev->freq_offset = rt2x00_get_field16(eeprom, EEPROM_FREQ_OFFSET);
3751
3752         /*
3753          * Store led settings, for correct led behaviour.
3754          */
3755 #ifdef CONFIG_RT2X00_LIB_LEDS
3756         rt2800_init_led(rt2x00dev, &rt2x00dev->led_radio, LED_TYPE_RADIO);
3757         rt2800_init_led(rt2x00dev, &rt2x00dev->led_assoc, LED_TYPE_ASSOC);
3758         rt2800_init_led(rt2x00dev, &rt2x00dev->led_qual, LED_TYPE_QUALITY);
3759
3760         rt2x00dev->led_mcu_reg = eeprom;
3761 #endif /* CONFIG_RT2X00_LIB_LEDS */
3762
3763         /*
3764          * Check if support EIRP tx power limit feature.
3765          */
3766         rt2x00_eeprom_read(rt2x00dev, EEPROM_EIRP_MAX_TX_POWER, &eeprom);
3767
3768         if (rt2x00_get_field16(eeprom, EEPROM_EIRP_MAX_TX_POWER_2GHZ) <
3769                                         EIRP_MAX_TX_POWER_LIMIT)
3770                 __set_bit(CAPABILITY_POWER_LIMIT, &rt2x00dev->cap_flags);
3771
3772         return 0;
3773 }
3774 EXPORT_SYMBOL_GPL(rt2800_init_eeprom);
3775
3776 /*
3777  * RF value list for rt28xx
3778  * Supports: 2.4 GHz (all) & 5.2 GHz (RF2850 & RF2750)
3779  */
3780 static const struct rf_channel rf_vals[] = {
3781         { 1,  0x18402ecc, 0x184c0786, 0x1816b455, 0x1800510b },
3782         { 2,  0x18402ecc, 0x184c0786, 0x18168a55, 0x1800519f },
3783         { 3,  0x18402ecc, 0x184c078a, 0x18168a55, 0x1800518b },
3784         { 4,  0x18402ecc, 0x184c078a, 0x18168a55, 0x1800519f },
3785         { 5,  0x18402ecc, 0x184c078e, 0x18168a55, 0x1800518b },
3786         { 6,  0x18402ecc, 0x184c078e, 0x18168a55, 0x1800519f },
3787         { 7,  0x18402ecc, 0x184c0792, 0x18168a55, 0x1800518b },
3788         { 8,  0x18402ecc, 0x184c0792, 0x18168a55, 0x1800519f },
3789         { 9,  0x18402ecc, 0x184c0796, 0x18168a55, 0x1800518b },
3790         { 10, 0x18402ecc, 0x184c0796, 0x18168a55, 0x1800519f },
3791         { 11, 0x18402ecc, 0x184c079a, 0x18168a55, 0x1800518b },
3792         { 12, 0x18402ecc, 0x184c079a, 0x18168a55, 0x1800519f },
3793         { 13, 0x18402ecc, 0x184c079e, 0x18168a55, 0x1800518b },
3794         { 14, 0x18402ecc, 0x184c07a2, 0x18168a55, 0x18005193 },
3795
3796         /* 802.11 UNI / HyperLan 2 */
3797         { 36, 0x18402ecc, 0x184c099a, 0x18158a55, 0x180ed1a3 },
3798         { 38, 0x18402ecc, 0x184c099e, 0x18158a55, 0x180ed193 },
3799         { 40, 0x18402ec8, 0x184c0682, 0x18158a55, 0x180ed183 },
3800         { 44, 0x18402ec8, 0x184c0682, 0x18158a55, 0x180ed1a3 },
3801         { 46, 0x18402ec8, 0x184c0686, 0x18158a55, 0x180ed18b },
3802         { 48, 0x18402ec8, 0x184c0686, 0x18158a55, 0x180ed19b },
3803         { 52, 0x18402ec8, 0x184c068a, 0x18158a55, 0x180ed193 },
3804         { 54, 0x18402ec8, 0x184c068a, 0x18158a55, 0x180ed1a3 },
3805         { 56, 0x18402ec8, 0x184c068e, 0x18158a55, 0x180ed18b },
3806         { 60, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed183 },
3807         { 62, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed193 },
3808         { 64, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed1a3 },
3809
3810         /* 802.11 HyperLan 2 */
3811         { 100, 0x18402ec8, 0x184c06b2, 0x18178a55, 0x180ed783 },
3812         { 102, 0x18402ec8, 0x184c06b2, 0x18578a55, 0x180ed793 },
3813         { 104, 0x18402ec8, 0x185c06b2, 0x18578a55, 0x180ed1a3 },
3814         { 108, 0x18402ecc, 0x185c0a32, 0x18578a55, 0x180ed193 },
3815         { 110, 0x18402ecc, 0x184c0a36, 0x18178a55, 0x180ed183 },
3816         { 112, 0x18402ecc, 0x184c0a36, 0x18178a55, 0x180ed19b },
3817         { 116, 0x18402ecc, 0x184c0a3a, 0x18178a55, 0x180ed1a3 },
3818         { 118, 0x18402ecc, 0x184c0a3e, 0x18178a55, 0x180ed193 },
3819         { 120, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed183 },
3820         { 124, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed193 },
3821         { 126, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed15b },
3822         { 128, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed1a3 },
3823         { 132, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed18b },
3824         { 134, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed193 },
3825         { 136, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed19b },
3826         { 140, 0x18402ec4, 0x184c038a, 0x18178a55, 0x180ed183 },
3827
3828         /* 802.11 UNII */
3829         { 149, 0x18402ec4, 0x184c038a, 0x18178a55, 0x180ed1a7 },
3830         { 151, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed187 },
3831         { 153, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed18f },
3832         { 157, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed19f },
3833         { 159, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed1a7 },
3834         { 161, 0x18402ec4, 0x184c0392, 0x18178a55, 0x180ed187 },
3835         { 165, 0x18402ec4, 0x184c0392, 0x18178a55, 0x180ed197 },
3836         { 167, 0x18402ec4, 0x184c03d2, 0x18179855, 0x1815531f },
3837         { 169, 0x18402ec4, 0x184c03d2, 0x18179855, 0x18155327 },
3838         { 171, 0x18402ec4, 0x184c03d6, 0x18179855, 0x18155307 },
3839         { 173, 0x18402ec4, 0x184c03d6, 0x18179855, 0x1815530f },
3840
3841         /* 802.11 Japan */
3842         { 184, 0x15002ccc, 0x1500491e, 0x1509be55, 0x150c0a0b },
3843         { 188, 0x15002ccc, 0x15004922, 0x1509be55, 0x150c0a13 },
3844         { 192, 0x15002ccc, 0x15004926, 0x1509be55, 0x150c0a1b },
3845         { 196, 0x15002ccc, 0x1500492a, 0x1509be55, 0x150c0a23 },
3846         { 208, 0x15002ccc, 0x1500493a, 0x1509be55, 0x150c0a13 },
3847         { 212, 0x15002ccc, 0x1500493e, 0x1509be55, 0x150c0a1b },
3848         { 216, 0x15002ccc, 0x15004982, 0x1509be55, 0x150c0a23 },
3849 };
3850
3851 /*
3852  * RF value list for rt3xxx
3853  * Supports: 2.4 GHz (all) & 5.2 GHz (RF3052)
3854  */
3855 static const struct rf_channel rf_vals_3x[] = {
3856         {1,  241, 2, 2 },
3857         {2,  241, 2, 7 },
3858         {3,  242, 2, 2 },
3859         {4,  242, 2, 7 },
3860         {5,  243, 2, 2 },
3861         {6,  243, 2, 7 },
3862         {7,  244, 2, 2 },
3863         {8,  244, 2, 7 },
3864         {9,  245, 2, 2 },
3865         {10, 245, 2, 7 },
3866         {11, 246, 2, 2 },
3867         {12, 246, 2, 7 },
3868         {13, 247, 2, 2 },
3869         {14, 248, 2, 4 },
3870
3871         /* 802.11 UNI / HyperLan 2 */
3872         {36, 0x56, 0, 4},
3873         {38, 0x56, 0, 6},
3874         {40, 0x56, 0, 8},
3875         {44, 0x57, 0, 0},
3876         {46, 0x57, 0, 2},
3877         {48, 0x57, 0, 4},
3878         {52, 0x57, 0, 8},
3879         {54, 0x57, 0, 10},
3880         {56, 0x58, 0, 0},
3881         {60, 0x58, 0, 4},
3882         {62, 0x58, 0, 6},
3883         {64, 0x58, 0, 8},
3884
3885         /* 802.11 HyperLan 2 */
3886         {100, 0x5b, 0, 8},
3887         {102, 0x5b, 0, 10},
3888         {104, 0x5c, 0, 0},
3889         {108, 0x5c, 0, 4},
3890         {110, 0x5c, 0, 6},
3891         {112, 0x5c, 0, 8},
3892         {116, 0x5d, 0, 0},
3893         {118, 0x5d, 0, 2},
3894         {120, 0x5d, 0, 4},
3895         {124, 0x5d, 0, 8},
3896         {126, 0x5d, 0, 10},
3897         {128, 0x5e, 0, 0},
3898         {132, 0x5e, 0, 4},
3899         {134, 0x5e, 0, 6},
3900         {136, 0x5e, 0, 8},
3901         {140, 0x5f, 0, 0},
3902
3903         /* 802.11 UNII */
3904         {149, 0x5f, 0, 9},
3905         {151, 0x5f, 0, 11},
3906         {153, 0x60, 0, 1},
3907         {157, 0x60, 0, 5},
3908         {159, 0x60, 0, 7},
3909         {161, 0x60, 0, 9},
3910         {165, 0x61, 0, 1},
3911         {167, 0x61, 0, 3},
3912         {169, 0x61, 0, 5},
3913         {171, 0x61, 0, 7},
3914         {173, 0x61, 0, 9},
3915 };
3916
3917 int rt2800_probe_hw_mode(struct rt2x00_dev *rt2x00dev)
3918 {
3919         struct hw_mode_spec *spec = &rt2x00dev->spec;
3920         struct channel_info *info;
3921         char *default_power1;
3922         char *default_power2;
3923         unsigned int i;
3924         u16 eeprom;
3925
3926         /*
3927          * Disable powersaving as default on PCI devices.
3928          */
3929         if (rt2x00_is_pci(rt2x00dev) || rt2x00_is_soc(rt2x00dev))
3930                 rt2x00dev->hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
3931
3932         /*
3933          * Initialize all hw fields.
3934          */
3935         rt2x00dev->hw->flags =
3936             IEEE80211_HW_SIGNAL_DBM |
3937             IEEE80211_HW_SUPPORTS_PS |
3938             IEEE80211_HW_PS_NULLFUNC_STACK |
3939             IEEE80211_HW_AMPDU_AGGREGATION;
3940         /*
3941          * Don't set IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING for USB devices
3942          * unless we are capable of sending the buffered frames out after the
3943          * DTIM transmission using rt2x00lib_beacondone. This will send out
3944          * multicast and broadcast traffic immediately instead of buffering it
3945          * infinitly and thus dropping it after some time.
3946          */
3947         if (!rt2x00_is_usb(rt2x00dev))
3948                 rt2x00dev->hw->flags |=
3949                         IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING;
3950
3951         SET_IEEE80211_DEV(rt2x00dev->hw, rt2x00dev->dev);
3952         SET_IEEE80211_PERM_ADDR(rt2x00dev->hw,
3953                                 rt2x00_eeprom_addr(rt2x00dev,
3954                                                    EEPROM_MAC_ADDR_0));
3955
3956         /*
3957          * As rt2800 has a global fallback table we cannot specify
3958          * more then one tx rate per frame but since the hw will
3959          * try several rates (based on the fallback table) we should
3960          * initialize max_report_rates to the maximum number of rates
3961          * we are going to try. Otherwise mac80211 will truncate our
3962          * reported tx rates and the rc algortihm will end up with
3963          * incorrect data.
3964          */
3965         rt2x00dev->hw->max_rates = 1;
3966         rt2x00dev->hw->max_report_rates = 7;
3967         rt2x00dev->hw->max_rate_tries = 1;
3968
3969         rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &eeprom);
3970
3971         /*
3972          * Initialize hw_mode information.
3973          */
3974         spec->supported_bands = SUPPORT_BAND_2GHZ;
3975         spec->supported_rates = SUPPORT_RATE_CCK | SUPPORT_RATE_OFDM;
3976
3977         if (rt2x00_rf(rt2x00dev, RF2820) ||
3978             rt2x00_rf(rt2x00dev, RF2720)) {
3979                 spec->num_channels = 14;
3980                 spec->channels = rf_vals;
3981         } else if (rt2x00_rf(rt2x00dev, RF2850) ||
3982                    rt2x00_rf(rt2x00dev, RF2750)) {
3983                 spec->supported_bands |= SUPPORT_BAND_5GHZ;
3984                 spec->num_channels = ARRAY_SIZE(rf_vals);
3985                 spec->channels = rf_vals;
3986         } else if (rt2x00_rf(rt2x00dev, RF3020) ||
3987                    rt2x00_rf(rt2x00dev, RF2020) ||
3988                    rt2x00_rf(rt2x00dev, RF3021) ||
3989                    rt2x00_rf(rt2x00dev, RF3022) ||
3990                    rt2x00_rf(rt2x00dev, RF3320) ||
3991                    rt2x00_rf(rt2x00dev, RF5390)) {
3992                 spec->num_channels = 14;
3993                 spec->channels = rf_vals_3x;
3994         } else if (rt2x00_rf(rt2x00dev, RF3052)) {
3995                 spec->supported_bands |= SUPPORT_BAND_5GHZ;
3996                 spec->num_channels = ARRAY_SIZE(rf_vals_3x);
3997                 spec->channels = rf_vals_3x;
3998         }
3999
4000         /*
4001          * Initialize HT information.
4002          */
4003         if (!rt2x00_rf(rt2x00dev, RF2020))
4004                 spec->ht.ht_supported = true;
4005         else
4006                 spec->ht.ht_supported = false;
4007
4008         spec->ht.cap =
4009             IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
4010             IEEE80211_HT_CAP_GRN_FLD |
4011             IEEE80211_HT_CAP_SGI_20 |
4012             IEEE80211_HT_CAP_SGI_40;
4013
4014         if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_TXPATH) >= 2)
4015                 spec->ht.cap |= IEEE80211_HT_CAP_TX_STBC;
4016
4017         spec->ht.cap |=
4018             rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RXPATH) <<
4019                 IEEE80211_HT_CAP_RX_STBC_SHIFT;
4020
4021         spec->ht.ampdu_factor = 3;
4022         spec->ht.ampdu_density = 4;
4023         spec->ht.mcs.tx_params =
4024             IEEE80211_HT_MCS_TX_DEFINED |
4025             IEEE80211_HT_MCS_TX_RX_DIFF |
4026             ((rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_TXPATH) - 1) <<
4027                 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
4028
4029         switch (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RXPATH)) {
4030         case 3:
4031                 spec->ht.mcs.rx_mask[2] = 0xff;
4032         case 2:
4033                 spec->ht.mcs.rx_mask[1] = 0xff;
4034         case 1:
4035                 spec->ht.mcs.rx_mask[0] = 0xff;
4036                 spec->ht.mcs.rx_mask[4] = 0x1; /* MCS32 */
4037                 break;
4038         }
4039
4040         /*
4041          * Create channel information array
4042          */
4043         info = kcalloc(spec->num_channels, sizeof(*info), GFP_KERNEL);
4044         if (!info)
4045                 return -ENOMEM;
4046
4047         spec->channels_info = info;
4048
4049         default_power1 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_BG1);
4050         default_power2 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_BG2);
4051
4052         for (i = 0; i < 14; i++) {
4053                 info[i].default_power1 = default_power1[i];
4054                 info[i].default_power2 = default_power2[i];
4055         }
4056
4057         if (spec->num_channels > 14) {
4058                 default_power1 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_A1);
4059                 default_power2 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_A2);
4060
4061                 for (i = 14; i < spec->num_channels; i++) {
4062                         info[i].default_power1 = default_power1[i];
4063                         info[i].default_power2 = default_power2[i];
4064                 }
4065         }
4066
4067         return 0;
4068 }
4069 EXPORT_SYMBOL_GPL(rt2800_probe_hw_mode);
4070
4071 /*
4072  * IEEE80211 stack callback functions.
4073  */
4074 void rt2800_get_tkip_seq(struct ieee80211_hw *hw, u8 hw_key_idx, u32 *iv32,
4075                          u16 *iv16)
4076 {
4077         struct rt2x00_dev *rt2x00dev = hw->priv;
4078         struct mac_iveiv_entry iveiv_entry;
4079         u32 offset;
4080
4081         offset = MAC_IVEIV_ENTRY(hw_key_idx);
4082         rt2800_register_multiread(rt2x00dev, offset,
4083                                       &iveiv_entry, sizeof(iveiv_entry));
4084
4085         memcpy(iv16, &iveiv_entry.iv[0], sizeof(*iv16));
4086         memcpy(iv32, &iveiv_entry.iv[4], sizeof(*iv32));
4087 }
4088 EXPORT_SYMBOL_GPL(rt2800_get_tkip_seq);
4089
4090 int rt2800_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
4091 {
4092         struct rt2x00_dev *rt2x00dev = hw->priv;
4093         u32 reg;
4094         bool enabled = (value < IEEE80211_MAX_RTS_THRESHOLD);
4095
4096         rt2800_register_read(rt2x00dev, TX_RTS_CFG, &reg);
4097         rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_THRES, value);
4098         rt2800_register_write(rt2x00dev, TX_RTS_CFG, reg);
4099
4100         rt2800_register_read(rt2x00dev, CCK_PROT_CFG, &reg);
4101         rt2x00_set_field32(&reg, CCK_PROT_CFG_RTS_TH_EN, enabled);
4102         rt2800_register_write(rt2x00dev, CCK_PROT_CFG, reg);
4103
4104         rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
4105         rt2x00_set_field32(&reg, OFDM_PROT_CFG_RTS_TH_EN, enabled);
4106         rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
4107
4108         rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
4109         rt2x00_set_field32(&reg, MM20_PROT_CFG_RTS_TH_EN, enabled);
4110         rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
4111
4112         rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
4113         rt2x00_set_field32(&reg, MM40_PROT_CFG_RTS_TH_EN, enabled);
4114         rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
4115
4116         rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
4117         rt2x00_set_field32(&reg, GF20_PROT_CFG_RTS_TH_EN, enabled);
4118         rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
4119
4120         rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
4121         rt2x00_set_field32(&reg, GF40_PROT_CFG_RTS_TH_EN, enabled);
4122         rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
4123
4124         return 0;
4125 }
4126 EXPORT_SYMBOL_GPL(rt2800_set_rts_threshold);
4127
4128 int rt2800_conf_tx(struct ieee80211_hw *hw, u16 queue_idx,
4129                    const struct ieee80211_tx_queue_params *params)
4130 {
4131         struct rt2x00_dev *rt2x00dev = hw->priv;
4132         struct data_queue *queue;
4133         struct rt2x00_field32 field;
4134         int retval;
4135         u32 reg;
4136         u32 offset;
4137
4138         /*
4139          * First pass the configuration through rt2x00lib, that will
4140          * update the queue settings and validate the input. After that
4141          * we are free to update the registers based on the value
4142          * in the queue parameter.
4143          */
4144         retval = rt2x00mac_conf_tx(hw, queue_idx, params);
4145         if (retval)
4146                 return retval;
4147
4148         /*
4149          * We only need to perform additional register initialization
4150          * for WMM queues/
4151          */
4152         if (queue_idx >= 4)
4153                 return 0;
4154
4155         queue = rt2x00queue_get_tx_queue(rt2x00dev, queue_idx);
4156
4157         /* Update WMM TXOP register */
4158         offset = WMM_TXOP0_CFG + (sizeof(u32) * (!!(queue_idx & 2)));
4159         field.bit_offset = (queue_idx & 1) * 16;
4160         field.bit_mask = 0xffff << field.bit_offset;
4161
4162         rt2800_register_read(rt2x00dev, offset, &reg);
4163         rt2x00_set_field32(&reg, field, queue->txop);
4164         rt2800_register_write(rt2x00dev, offset, reg);
4165
4166         /* Update WMM registers */
4167         field.bit_offset = queue_idx * 4;
4168         field.bit_mask = 0xf << field.bit_offset;
4169
4170         rt2800_register_read(rt2x00dev, WMM_AIFSN_CFG, &reg);
4171         rt2x00_set_field32(&reg, field, queue->aifs);
4172         rt2800_register_write(rt2x00dev, WMM_AIFSN_CFG, reg);
4173
4174         rt2800_register_read(rt2x00dev, WMM_CWMIN_CFG, &reg);
4175         rt2x00_set_field32(&reg, field, queue->cw_min);
4176         rt2800_register_write(rt2x00dev, WMM_CWMIN_CFG, reg);
4177
4178         rt2800_register_read(rt2x00dev, WMM_CWMAX_CFG, &reg);
4179         rt2x00_set_field32(&reg, field, queue->cw_max);
4180         rt2800_register_write(rt2x00dev, WMM_CWMAX_CFG, reg);
4181
4182         /* Update EDCA registers */
4183         offset = EDCA_AC0_CFG + (sizeof(u32) * queue_idx);
4184
4185         rt2800_register_read(rt2x00dev, offset, &reg);
4186         rt2x00_set_field32(&reg, EDCA_AC0_CFG_TX_OP, queue->txop);
4187         rt2x00_set_field32(&reg, EDCA_AC0_CFG_AIFSN, queue->aifs);
4188         rt2x00_set_field32(&reg, EDCA_AC0_CFG_CWMIN, queue->cw_min);
4189         rt2x00_set_field32(&reg, EDCA_AC0_CFG_CWMAX, queue->cw_max);
4190         rt2800_register_write(rt2x00dev, offset, reg);
4191
4192         return 0;
4193 }
4194 EXPORT_SYMBOL_GPL(rt2800_conf_tx);
4195
4196 u64 rt2800_get_tsf(struct ieee80211_hw *hw)
4197 {
4198         struct rt2x00_dev *rt2x00dev = hw->priv;
4199         u64 tsf;
4200         u32 reg;
4201
4202         rt2800_register_read(rt2x00dev, TSF_TIMER_DW1, &reg);
4203         tsf = (u64) rt2x00_get_field32(reg, TSF_TIMER_DW1_HIGH_WORD) << 32;
4204         rt2800_register_read(rt2x00dev, TSF_TIMER_DW0, &reg);
4205         tsf |= rt2x00_get_field32(reg, TSF_TIMER_DW0_LOW_WORD);
4206
4207         return tsf;
4208 }
4209 EXPORT_SYMBOL_GPL(rt2800_get_tsf);
4210
4211 int rt2800_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4212                         enum ieee80211_ampdu_mlme_action action,
4213                         struct ieee80211_sta *sta, u16 tid, u16 *ssn,
4214                         u8 buf_size)
4215 {
4216         int ret = 0;
4217
4218         switch (action) {
4219         case IEEE80211_AMPDU_RX_START:
4220         case IEEE80211_AMPDU_RX_STOP:
4221                 /*
4222                  * The hw itself takes care of setting up BlockAck mechanisms.
4223                  * So, we only have to allow mac80211 to nagotiate a BlockAck
4224                  * agreement. Once that is done, the hw will BlockAck incoming
4225                  * AMPDUs without further setup.
4226                  */
4227                 break;
4228         case IEEE80211_AMPDU_TX_START:
4229                 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
4230                 break;
4231         case IEEE80211_AMPDU_TX_STOP:
4232                 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
4233                 break;
4234         case IEEE80211_AMPDU_TX_OPERATIONAL:
4235                 break;
4236         default:
4237                 WARNING((struct rt2x00_dev *)hw->priv, "Unknown AMPDU action\n");
4238         }
4239
4240         return ret;
4241 }
4242 EXPORT_SYMBOL_GPL(rt2800_ampdu_action);
4243
4244 int rt2800_get_survey(struct ieee80211_hw *hw, int idx,
4245                       struct survey_info *survey)
4246 {
4247         struct rt2x00_dev *rt2x00dev = hw->priv;
4248         struct ieee80211_conf *conf = &hw->conf;
4249         u32 idle, busy, busy_ext;
4250
4251         if (idx != 0)
4252                 return -ENOENT;
4253
4254         survey->channel = conf->channel;
4255
4256         rt2800_register_read(rt2x00dev, CH_IDLE_STA, &idle);
4257         rt2800_register_read(rt2x00dev, CH_BUSY_STA, &busy);
4258         rt2800_register_read(rt2x00dev, CH_BUSY_STA_SEC, &busy_ext);
4259
4260         if (idle || busy) {
4261                 survey->filled = SURVEY_INFO_CHANNEL_TIME |
4262                                  SURVEY_INFO_CHANNEL_TIME_BUSY |
4263                                  SURVEY_INFO_CHANNEL_TIME_EXT_BUSY;
4264
4265                 survey->channel_time = (idle + busy) / 1000;
4266                 survey->channel_time_busy = busy / 1000;
4267                 survey->channel_time_ext_busy = busy_ext / 1000;
4268         }
4269
4270         return 0;
4271
4272 }
4273 EXPORT_SYMBOL_GPL(rt2800_get_survey);
4274
4275 MODULE_AUTHOR(DRV_PROJECT ", Bartlomiej Zolnierkiewicz");
4276 MODULE_VERSION(DRV_VERSION);
4277 MODULE_DESCRIPTION("Ralink RT2800 library");
4278 MODULE_LICENSE("GPL");