blob: c6c3cd3c730f641b8764a94615224204a0514ae9 [file] [log] [blame]
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +01001/*
Ivo van Doorn96481b22010-08-06 20:47:57 +02002 Copyright (C) 2010 Willow Garage <http://www.willowgarage.com>
Ivo van Doorna5ea2f02010-06-14 22:13:15 +02003 Copyright (C) 2010 Ivo van Doorn <IvDoorn@gmail.com>
Gertjan van Wingerde9c9a0d12009-11-08 16:39:55 +01004 Copyright (C) 2009 Bartlomiej Zolnierkiewicz <bzolnier@gmail.com>
Gertjan van Wingerdecce5fc42009-11-10 22:42:40 +01005 Copyright (C) 2009 Gertjan van Wingerde <gwingerde@gmail.com>
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +01006
Gertjan van Wingerde9c9a0d12009-11-08 16:39:55 +01007 Based on the original rt2800pci.c and rt2800usb.c.
Gertjan van Wingerde9c9a0d12009-11-08 16:39:55 +01008 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>
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +010014 <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
Ivo van Doornf31c9a82010-07-11 12:30:37 +020037#include <linux/crc-ccitt.h>
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +010038#include <linux/kernel.h>
39#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
Stanislaw Gruszka341b1e92011-08-25 17:14:24 +020041#include <linux/sched.h>
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +010042
43#include "rt2x00.h"
44#include "rt2800lib.h"
45#include "rt2800.h"
46
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +010047/*
48 * Register access.
49 * All access to the CSR registers will go through the methods
50 * rt2800_register_read and rt2800_register_write.
51 * BBP and RF register require indirect register access,
52 * and use the CSR registers BBPCSR and RFCSR to achieve this.
53 * These indirect registers work with busy bits,
54 * and we will try maximal REGISTER_BUSY_COUNT times to access
55 * the register while taking a REGISTER_BUSY_DELAY us delay
56 * between each attampt. When the busy bit is still set at that time,
57 * the access attempt is considered to have failed,
58 * and we will print an error.
59 * The _lock versions must be used if you already hold the csr_mutex
60 */
61#define WAIT_FOR_BBP(__dev, __reg) \
62 rt2800_regbusy_read((__dev), BBP_CSR_CFG, BBP_CSR_CFG_BUSY, (__reg))
63#define WAIT_FOR_RFCSR(__dev, __reg) \
64 rt2800_regbusy_read((__dev), RF_CSR_CFG, RF_CSR_CFG_BUSY, (__reg))
65#define WAIT_FOR_RF(__dev, __reg) \
66 rt2800_regbusy_read((__dev), RF_CSR_CFG0, RF_CSR_CFG0_BUSY, (__reg))
67#define WAIT_FOR_MCU(__dev, __reg) \
68 rt2800_regbusy_read((__dev), H2M_MAILBOX_CSR, \
69 H2M_MAILBOX_CSR_OWNER, (__reg))
70
Helmut Schaabaff8002010-04-28 09:58:59 +020071static inline bool rt2800_is_305x_soc(struct rt2x00_dev *rt2x00dev)
72{
73 /* check for rt2872 on SoC */
74 if (!rt2x00_is_soc(rt2x00dev) ||
75 !rt2x00_rt(rt2x00dev, RT2872))
76 return false;
77
78 /* we know for sure that these rf chipsets are used on rt305x boards */
79 if (rt2x00_rf(rt2x00dev, RF3020) ||
80 rt2x00_rf(rt2x00dev, RF3021) ||
81 rt2x00_rf(rt2x00dev, RF3022))
82 return true;
83
84 NOTICE(rt2x00dev, "Unknown RF chipset on rt305x\n");
85 return false;
86}
87
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +010088static void rt2800_bbp_write(struct rt2x00_dev *rt2x00dev,
89 const unsigned int word, const u8 value)
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +010090{
91 u32 reg;
92
93 mutex_lock(&rt2x00dev->csr_mutex);
94
95 /*
96 * Wait until the BBP becomes available, afterwards we
97 * can safely write the new data into the register.
98 */
99 if (WAIT_FOR_BBP(rt2x00dev, &reg)) {
100 reg = 0;
101 rt2x00_set_field32(&reg, BBP_CSR_CFG_VALUE, value);
102 rt2x00_set_field32(&reg, BBP_CSR_CFG_REGNUM, word);
103 rt2x00_set_field32(&reg, BBP_CSR_CFG_BUSY, 1);
104 rt2x00_set_field32(&reg, BBP_CSR_CFG_READ_CONTROL, 0);
Ivo van Doornefc7d362010-06-29 21:49:26 +0200105 rt2x00_set_field32(&reg, BBP_CSR_CFG_BBP_RW_MODE, 1);
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100106
107 rt2800_register_write_lock(rt2x00dev, BBP_CSR_CFG, reg);
108 }
109
110 mutex_unlock(&rt2x00dev->csr_mutex);
111}
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100112
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +0100113static void rt2800_bbp_read(struct rt2x00_dev *rt2x00dev,
114 const unsigned int word, u8 *value)
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100115{
116 u32 reg;
117
118 mutex_lock(&rt2x00dev->csr_mutex);
119
120 /*
121 * Wait until the BBP becomes available, afterwards we
122 * can safely write the read request into the register.
123 * After the data has been written, we wait until hardware
124 * returns the correct value, if at any time the register
125 * doesn't become available in time, reg will be 0xffffffff
126 * which means we return 0xff to the caller.
127 */
128 if (WAIT_FOR_BBP(rt2x00dev, &reg)) {
129 reg = 0;
130 rt2x00_set_field32(&reg, BBP_CSR_CFG_REGNUM, word);
131 rt2x00_set_field32(&reg, BBP_CSR_CFG_BUSY, 1);
132 rt2x00_set_field32(&reg, BBP_CSR_CFG_READ_CONTROL, 1);
Ivo van Doornefc7d362010-06-29 21:49:26 +0200133 rt2x00_set_field32(&reg, BBP_CSR_CFG_BBP_RW_MODE, 1);
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100134
135 rt2800_register_write_lock(rt2x00dev, BBP_CSR_CFG, reg);
136
137 WAIT_FOR_BBP(rt2x00dev, &reg);
138 }
139
140 *value = rt2x00_get_field32(reg, BBP_CSR_CFG_VALUE);
141
142 mutex_unlock(&rt2x00dev->csr_mutex);
143}
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100144
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +0100145static void rt2800_rfcsr_write(struct rt2x00_dev *rt2x00dev,
146 const unsigned int word, const u8 value)
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100147{
148 u32 reg;
149
150 mutex_lock(&rt2x00dev->csr_mutex);
151
152 /*
153 * Wait until the RFCSR becomes available, afterwards we
154 * can safely write the new data into the register.
155 */
156 if (WAIT_FOR_RFCSR(rt2x00dev, &reg)) {
157 reg = 0;
158 rt2x00_set_field32(&reg, RF_CSR_CFG_DATA, value);
159 rt2x00_set_field32(&reg, RF_CSR_CFG_REGNUM, word);
160 rt2x00_set_field32(&reg, RF_CSR_CFG_WRITE, 1);
161 rt2x00_set_field32(&reg, RF_CSR_CFG_BUSY, 1);
162
163 rt2800_register_write_lock(rt2x00dev, RF_CSR_CFG, reg);
164 }
165
166 mutex_unlock(&rt2x00dev->csr_mutex);
167}
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100168
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +0100169static void rt2800_rfcsr_read(struct rt2x00_dev *rt2x00dev,
170 const unsigned int word, u8 *value)
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100171{
172 u32 reg;
173
174 mutex_lock(&rt2x00dev->csr_mutex);
175
176 /*
177 * Wait until the RFCSR becomes available, afterwards we
178 * can safely write the read request into the register.
179 * After the data has been written, we wait until hardware
180 * returns the correct value, if at any time the register
181 * doesn't become available in time, reg will be 0xffffffff
182 * which means we return 0xff to the caller.
183 */
184 if (WAIT_FOR_RFCSR(rt2x00dev, &reg)) {
185 reg = 0;
186 rt2x00_set_field32(&reg, RF_CSR_CFG_REGNUM, word);
187 rt2x00_set_field32(&reg, RF_CSR_CFG_WRITE, 0);
188 rt2x00_set_field32(&reg, RF_CSR_CFG_BUSY, 1);
189
190 rt2800_register_write_lock(rt2x00dev, RF_CSR_CFG, reg);
191
192 WAIT_FOR_RFCSR(rt2x00dev, &reg);
193 }
194
195 *value = rt2x00_get_field32(reg, RF_CSR_CFG_DATA);
196
197 mutex_unlock(&rt2x00dev->csr_mutex);
198}
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100199
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +0100200static void rt2800_rf_write(struct rt2x00_dev *rt2x00dev,
201 const unsigned int word, const u32 value)
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100202{
203 u32 reg;
204
205 mutex_lock(&rt2x00dev->csr_mutex);
206
207 /*
208 * Wait until the RF becomes available, afterwards we
209 * can safely write the new data into the register.
210 */
211 if (WAIT_FOR_RF(rt2x00dev, &reg)) {
212 reg = 0;
213 rt2x00_set_field32(&reg, RF_CSR_CFG0_REG_VALUE_BW, value);
214 rt2x00_set_field32(&reg, RF_CSR_CFG0_STANDBYMODE, 0);
215 rt2x00_set_field32(&reg, RF_CSR_CFG0_SEL, 0);
216 rt2x00_set_field32(&reg, RF_CSR_CFG0_BUSY, 1);
217
218 rt2800_register_write_lock(rt2x00dev, RF_CSR_CFG0, reg);
219 rt2x00_rf_write(rt2x00dev, word, value);
220 }
221
222 mutex_unlock(&rt2x00dev->csr_mutex);
223}
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100224
225void rt2800_mcu_request(struct rt2x00_dev *rt2x00dev,
226 const u8 command, const u8 token,
227 const u8 arg0, const u8 arg1)
228{
229 u32 reg;
230
Gertjan van Wingerdeee303e52009-11-23 22:44:49 +0100231 /*
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +0100232 * SOC devices don't support MCU requests.
Gertjan van Wingerdeee303e52009-11-23 22:44:49 +0100233 */
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +0100234 if (rt2x00_is_soc(rt2x00dev))
Gertjan van Wingerdeee303e52009-11-23 22:44:49 +0100235 return;
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +0100236
237 mutex_lock(&rt2x00dev->csr_mutex);
238
239 /*
240 * Wait until the MCU becomes available, afterwards we
241 * can safely write the new data into the register.
242 */
243 if (WAIT_FOR_MCU(rt2x00dev, &reg)) {
244 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_OWNER, 1);
245 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_CMD_TOKEN, token);
246 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_ARG0, arg0);
247 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_ARG1, arg1);
248 rt2800_register_write_lock(rt2x00dev, H2M_MAILBOX_CSR, reg);
249
250 reg = 0;
251 rt2x00_set_field32(&reg, HOST_CMD_CSR_HOST_COMMAND, command);
252 rt2800_register_write_lock(rt2x00dev, HOST_CMD_CSR, reg);
253 }
254
255 mutex_unlock(&rt2x00dev->csr_mutex);
256}
257EXPORT_SYMBOL_GPL(rt2800_mcu_request);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100258
Ivo van Doorn5ffddc42010-08-30 21:13:08 +0200259int rt2800_wait_csr_ready(struct rt2x00_dev *rt2x00dev)
260{
261 unsigned int i = 0;
262 u32 reg;
263
264 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
265 rt2800_register_read(rt2x00dev, MAC_CSR0, &reg);
266 if (reg && reg != ~0)
267 return 0;
268 msleep(1);
269 }
270
271 ERROR(rt2x00dev, "Unstable hardware.\n");
272 return -EBUSY;
273}
274EXPORT_SYMBOL_GPL(rt2800_wait_csr_ready);
275
Gertjan van Wingerde67a4c1e2009-12-30 11:36:32 +0100276int rt2800_wait_wpdma_ready(struct rt2x00_dev *rt2x00dev)
277{
278 unsigned int i;
279 u32 reg;
280
Helmut Schaa08e53102010-11-04 20:37:47 +0100281 /*
282 * Some devices are really slow to respond here. Wait a whole second
283 * before timing out.
284 */
Gertjan van Wingerde67a4c1e2009-12-30 11:36:32 +0100285 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
286 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
287 if (!rt2x00_get_field32(reg, WPDMA_GLO_CFG_TX_DMA_BUSY) &&
288 !rt2x00_get_field32(reg, WPDMA_GLO_CFG_RX_DMA_BUSY))
289 return 0;
290
Helmut Schaa08e53102010-11-04 20:37:47 +0100291 msleep(10);
Gertjan van Wingerde67a4c1e2009-12-30 11:36:32 +0100292 }
293
294 ERROR(rt2x00dev, "WPDMA TX/RX busy, aborting.\n");
295 return -EACCES;
296}
297EXPORT_SYMBOL_GPL(rt2800_wait_wpdma_ready);
298
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200299static bool rt2800_check_firmware_crc(const u8 *data, const size_t len)
300{
301 u16 fw_crc;
302 u16 crc;
303
304 /*
305 * The last 2 bytes in the firmware array are the crc checksum itself,
306 * this means that we should never pass those 2 bytes to the crc
307 * algorithm.
308 */
309 fw_crc = (data[len - 2] << 8 | data[len - 1]);
310
311 /*
312 * Use the crc ccitt algorithm.
313 * This will return the same value as the legacy driver which
314 * used bit ordering reversion on the both the firmware bytes
315 * before input input as well as on the final output.
316 * Obviously using crc ccitt directly is much more efficient.
317 */
318 crc = crc_ccitt(~0, data, len - 2);
319
320 /*
321 * There is a small difference between the crc-itu-t + bitrev and
322 * the crc-ccitt crc calculation. In the latter method the 2 bytes
323 * will be swapped, use swab16 to convert the crc to the correct
324 * value.
325 */
326 crc = swab16(crc);
327
328 return fw_crc == crc;
329}
330
331int rt2800_check_firmware(struct rt2x00_dev *rt2x00dev,
332 const u8 *data, const size_t len)
333{
334 size_t offset = 0;
335 size_t fw_len;
336 bool multiple;
337
338 /*
339 * PCI(e) & SOC devices require firmware with a length
340 * of 8kb. USB devices require firmware files with a length
341 * of 4kb. Certain USB chipsets however require different firmware,
342 * which Ralink only provides attached to the original firmware
343 * file. Thus for USB devices, firmware files have a length
344 * which is a multiple of 4kb.
345 */
346 if (rt2x00_is_usb(rt2x00dev)) {
347 fw_len = 4096;
348 multiple = true;
349 } else {
350 fw_len = 8192;
351 multiple = true;
352 }
353
354 /*
355 * Validate the firmware length
356 */
357 if (len != fw_len && (!multiple || (len % fw_len) != 0))
358 return FW_BAD_LENGTH;
359
360 /*
361 * Check if the chipset requires one of the upper parts
362 * of the firmware.
363 */
364 if (rt2x00_is_usb(rt2x00dev) &&
365 !rt2x00_rt(rt2x00dev, RT2860) &&
366 !rt2x00_rt(rt2x00dev, RT2872) &&
367 !rt2x00_rt(rt2x00dev, RT3070) &&
368 ((len / fw_len) == 1))
369 return FW_BAD_VERSION;
370
371 /*
372 * 8kb firmware files must be checked as if it were
373 * 2 separate firmware files.
374 */
375 while (offset < len) {
376 if (!rt2800_check_firmware_crc(data + offset, fw_len))
377 return FW_BAD_CRC;
378
379 offset += fw_len;
380 }
381
382 return FW_OK;
383}
384EXPORT_SYMBOL_GPL(rt2800_check_firmware);
385
386int rt2800_load_firmware(struct rt2x00_dev *rt2x00dev,
387 const u8 *data, const size_t len)
388{
389 unsigned int i;
390 u32 reg;
391
392 /*
Ivo van Doornb9eca242010-08-30 21:13:54 +0200393 * If driver doesn't wake up firmware here,
394 * rt2800_load_firmware will hang forever when interface is up again.
395 */
396 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, 0x00000000);
397
398 /*
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200399 * Wait for stable hardware.
400 */
Ivo van Doorn5ffddc42010-08-30 21:13:08 +0200401 if (rt2800_wait_csr_ready(rt2x00dev))
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200402 return -EBUSY;
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200403
Gabor Juhosadde5882011-03-03 11:46:45 +0100404 if (rt2x00_is_pci(rt2x00dev)) {
405 if (rt2x00_rt(rt2x00dev, RT5390)) {
406 rt2800_register_read(rt2x00dev, AUX_CTRL, &reg);
407 rt2x00_set_field32(&reg, AUX_CTRL_FORCE_PCIE_CLK, 1);
408 rt2x00_set_field32(&reg, AUX_CTRL_WAKE_PCIE_EN, 1);
409 rt2800_register_write(rt2x00dev, AUX_CTRL, reg);
410 }
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200411 rt2800_register_write(rt2x00dev, PWR_PIN_CFG, 0x00000002);
Gabor Juhosadde5882011-03-03 11:46:45 +0100412 }
Ivo van Doornf31c9a82010-07-11 12:30:37 +0200413
414 /*
415 * Disable DMA, will be reenabled later when enabling
416 * the radio.
417 */
418 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
419 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
420 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
421 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
422 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
423 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
424 rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
425
426 /*
427 * Write firmware to the device.
428 */
429 rt2800_drv_write_firmware(rt2x00dev, data, len);
430
431 /*
432 * Wait for device to stabilize.
433 */
434 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
435 rt2800_register_read(rt2x00dev, PBF_SYS_CTRL, &reg);
436 if (rt2x00_get_field32(reg, PBF_SYS_CTRL_READY))
437 break;
438 msleep(1);
439 }
440
441 if (i == REGISTER_BUSY_COUNT) {
442 ERROR(rt2x00dev, "PBF system register not ready.\n");
443 return -EBUSY;
444 }
445
446 /*
447 * Initialize firmware.
448 */
449 rt2800_register_write(rt2x00dev, H2M_BBP_AGENT, 0);
450 rt2800_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
451 msleep(1);
452
453 return 0;
454}
455EXPORT_SYMBOL_GPL(rt2800_load_firmware);
456
Ivo van Doorn0c5879b2010-08-06 20:47:20 +0200457void rt2800_write_tx_data(struct queue_entry *entry,
458 struct txentry_desc *txdesc)
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200459{
Ivo van Doorn0c5879b2010-08-06 20:47:20 +0200460 __le32 *txwi = rt2800_drv_get_txwi(entry);
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200461 u32 word;
462
463 /*
464 * Initialize TX Info descriptor
465 */
466 rt2x00_desc_read(txwi, 0, &word);
467 rt2x00_set_field32(&word, TXWI_W0_FRAG,
468 test_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags));
Ivo van Doorn84804cd2010-08-06 20:46:19 +0200469 rt2x00_set_field32(&word, TXWI_W0_MIMO_PS,
470 test_bit(ENTRY_TXD_HT_MIMO_PS, &txdesc->flags));
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200471 rt2x00_set_field32(&word, TXWI_W0_CF_ACK, 0);
472 rt2x00_set_field32(&word, TXWI_W0_TS,
473 test_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags));
474 rt2x00_set_field32(&word, TXWI_W0_AMPDU,
475 test_bit(ENTRY_TXD_HT_AMPDU, &txdesc->flags));
Helmut Schaa26a1d072011-03-03 19:42:35 +0100476 rt2x00_set_field32(&word, TXWI_W0_MPDU_DENSITY,
477 txdesc->u.ht.mpdu_density);
478 rt2x00_set_field32(&word, TXWI_W0_TX_OP, txdesc->u.ht.txop);
479 rt2x00_set_field32(&word, TXWI_W0_MCS, txdesc->u.ht.mcs);
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200480 rt2x00_set_field32(&word, TXWI_W0_BW,
481 test_bit(ENTRY_TXD_HT_BW_40, &txdesc->flags));
482 rt2x00_set_field32(&word, TXWI_W0_SHORT_GI,
483 test_bit(ENTRY_TXD_HT_SHORT_GI, &txdesc->flags));
Helmut Schaa26a1d072011-03-03 19:42:35 +0100484 rt2x00_set_field32(&word, TXWI_W0_STBC, txdesc->u.ht.stbc);
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200485 rt2x00_set_field32(&word, TXWI_W0_PHYMODE, txdesc->rate_mode);
486 rt2x00_desc_write(txwi, 0, word);
487
488 rt2x00_desc_read(txwi, 1, &word);
489 rt2x00_set_field32(&word, TXWI_W1_ACK,
490 test_bit(ENTRY_TXD_ACK, &txdesc->flags));
491 rt2x00_set_field32(&word, TXWI_W1_NSEQ,
492 test_bit(ENTRY_TXD_GENERATE_SEQ, &txdesc->flags));
Helmut Schaa26a1d072011-03-03 19:42:35 +0100493 rt2x00_set_field32(&word, TXWI_W1_BW_WIN_SIZE, txdesc->u.ht.ba_size);
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200494 rt2x00_set_field32(&word, TXWI_W1_WIRELESS_CLI_ID,
495 test_bit(ENTRY_TXD_ENCRYPT, &txdesc->flags) ?
496 txdesc->key_idx : 0xff);
497 rt2x00_set_field32(&word, TXWI_W1_MPDU_TOTAL_BYTE_COUNT,
498 txdesc->length);
Helmut Schaa2b23cda2010-11-04 20:38:15 +0100499 rt2x00_set_field32(&word, TXWI_W1_PACKETID_QUEUE, entry->queue->qid);
Ivo van Doornbc8a9792010-10-02 11:32:43 +0200500 rt2x00_set_field32(&word, TXWI_W1_PACKETID_ENTRY, (entry->entry_idx % 3) + 1);
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200501 rt2x00_desc_write(txwi, 1, word);
502
503 /*
504 * Always write 0 to IV/EIV fields, hardware will insert the IV
505 * from the IVEIV register when TXD_W3_WIV is set to 0.
506 * When TXD_W3_WIV is set to 1 it will use the IV data
507 * from the descriptor. The TXWI_W1_WIRELESS_CLI_ID indicates which
508 * crypto entry in the registers should be used to encrypt the frame.
509 */
510 _rt2x00_desc_write(txwi, 2, 0 /* skbdesc->iv[0] */);
511 _rt2x00_desc_write(txwi, 3, 0 /* skbdesc->iv[1] */);
512}
Ivo van Doorn0c5879b2010-08-06 20:47:20 +0200513EXPORT_SYMBOL_GPL(rt2800_write_tx_data);
Gertjan van Wingerde59679b92010-05-08 23:40:21 +0200514
Helmut Schaaff6133b2010-10-09 13:34:11 +0200515static int rt2800_agc_to_rssi(struct rt2x00_dev *rt2x00dev, u32 rxwi_w2)
Gertjan van Wingerde2de64dd2010-05-08 23:40:22 +0200516{
Ivo van Doorn74861922010-07-11 12:23:50 +0200517 int rssi0 = rt2x00_get_field32(rxwi_w2, RXWI_W2_RSSI0);
518 int rssi1 = rt2x00_get_field32(rxwi_w2, RXWI_W2_RSSI1);
519 int rssi2 = rt2x00_get_field32(rxwi_w2, RXWI_W2_RSSI2);
520 u16 eeprom;
521 u8 offset0;
522 u8 offset1;
523 u8 offset2;
524
Ivo van Doorne5ef5ba2010-08-06 20:49:27 +0200525 if (rt2x00dev->curr_band == IEEE80211_BAND_2GHZ) {
Ivo van Doorn74861922010-07-11 12:23:50 +0200526 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG, &eeprom);
527 offset0 = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG_OFFSET0);
528 offset1 = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG_OFFSET1);
529 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &eeprom);
530 offset2 = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG2_OFFSET2);
531 } else {
532 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A, &eeprom);
533 offset0 = rt2x00_get_field16(eeprom, EEPROM_RSSI_A_OFFSET0);
534 offset1 = rt2x00_get_field16(eeprom, EEPROM_RSSI_A_OFFSET1);
535 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &eeprom);
536 offset2 = rt2x00_get_field16(eeprom, EEPROM_RSSI_A2_OFFSET2);
537 }
538
539 /*
540 * Convert the value from the descriptor into the RSSI value
541 * If the value in the descriptor is 0, it is considered invalid
542 * and the default (extremely low) rssi value is assumed
543 */
544 rssi0 = (rssi0) ? (-12 - offset0 - rt2x00dev->lna_gain - rssi0) : -128;
545 rssi1 = (rssi1) ? (-12 - offset1 - rt2x00dev->lna_gain - rssi1) : -128;
546 rssi2 = (rssi2) ? (-12 - offset2 - rt2x00dev->lna_gain - rssi2) : -128;
547
548 /*
549 * mac80211 only accepts a single RSSI value. Calculating the
550 * average doesn't deliver a fair answer either since -60:-60 would
551 * be considered equally good as -50:-70 while the second is the one
552 * which gives less energy...
553 */
554 rssi0 = max(rssi0, rssi1);
555 return max(rssi0, rssi2);
556}
557
558void rt2800_process_rxwi(struct queue_entry *entry,
559 struct rxdone_entry_desc *rxdesc)
560{
561 __le32 *rxwi = (__le32 *) entry->skb->data;
Gertjan van Wingerde2de64dd2010-05-08 23:40:22 +0200562 u32 word;
563
564 rt2x00_desc_read(rxwi, 0, &word);
565
566 rxdesc->cipher = rt2x00_get_field32(word, RXWI_W0_UDF);
567 rxdesc->size = rt2x00_get_field32(word, RXWI_W0_MPDU_TOTAL_BYTE_COUNT);
568
569 rt2x00_desc_read(rxwi, 1, &word);
570
571 if (rt2x00_get_field32(word, RXWI_W1_SHORT_GI))
572 rxdesc->flags |= RX_FLAG_SHORT_GI;
573
574 if (rt2x00_get_field32(word, RXWI_W1_BW))
575 rxdesc->flags |= RX_FLAG_40MHZ;
576
577 /*
578 * Detect RX rate, always use MCS as signal type.
579 */
580 rxdesc->dev_flags |= RXDONE_SIGNAL_MCS;
581 rxdesc->signal = rt2x00_get_field32(word, RXWI_W1_MCS);
582 rxdesc->rate_mode = rt2x00_get_field32(word, RXWI_W1_PHYMODE);
583
584 /*
585 * Mask of 0x8 bit to remove the short preamble flag.
586 */
587 if (rxdesc->rate_mode == RATE_MODE_CCK)
588 rxdesc->signal &= ~0x8;
589
590 rt2x00_desc_read(rxwi, 2, &word);
591
Ivo van Doorn74861922010-07-11 12:23:50 +0200592 /*
593 * Convert descriptor AGC value to RSSI value.
594 */
595 rxdesc->rssi = rt2800_agc_to_rssi(entry->queue->rt2x00dev, word);
Gertjan van Wingerde2de64dd2010-05-08 23:40:22 +0200596
597 /*
598 * Remove RXWI descriptor from start of buffer.
599 */
Ivo van Doorn74861922010-07-11 12:23:50 +0200600 skb_pull(entry->skb, RXWI_DESC_SIZE);
Gertjan van Wingerde2de64dd2010-05-08 23:40:22 +0200601}
602EXPORT_SYMBOL_GPL(rt2800_process_rxwi);
603
Ivo van Doorn36138842010-08-30 21:13:30 +0200604static bool rt2800_txdone_entry_check(struct queue_entry *entry, u32 reg)
605{
606 __le32 *txwi;
607 u32 word;
608 int wcid, ack, pid;
609 int tx_wcid, tx_ack, tx_pid;
610
Stanislaw Gruszka341b1e92011-08-25 17:14:24 +0200611 if (test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags) ||
612 !test_bit(ENTRY_DATA_STATUS_PENDING, &entry->flags)) {
613 WARNING(entry->queue->rt2x00dev,
614 "Data pending for entry %u in queue %u\n",
615 entry->entry_idx, entry->queue->qid);
616 cond_resched();
617 return false;
618 }
619
Ivo van Doorn36138842010-08-30 21:13:30 +0200620 wcid = rt2x00_get_field32(reg, TX_STA_FIFO_WCID);
621 ack = rt2x00_get_field32(reg, TX_STA_FIFO_TX_ACK_REQUIRED);
622 pid = rt2x00_get_field32(reg, TX_STA_FIFO_PID_TYPE);
623
624 /*
625 * This frames has returned with an IO error,
626 * so the status report is not intended for this
627 * frame.
628 */
629 if (test_bit(ENTRY_DATA_IO_FAILED, &entry->flags)) {
630 rt2x00lib_txdone_noinfo(entry, TXDONE_FAILURE);
631 return false;
632 }
633
634 /*
635 * Validate if this TX status report is intended for
636 * this entry by comparing the WCID/ACK/PID fields.
637 */
638 txwi = rt2800_drv_get_txwi(entry);
639
640 rt2x00_desc_read(txwi, 1, &word);
641 tx_wcid = rt2x00_get_field32(word, TXWI_W1_WIRELESS_CLI_ID);
642 tx_ack = rt2x00_get_field32(word, TXWI_W1_ACK);
643 tx_pid = rt2x00_get_field32(word, TXWI_W1_PACKETID);
644
645 if ((wcid != tx_wcid) || (ack != tx_ack) || (pid != tx_pid)) {
646 WARNING(entry->queue->rt2x00dev,
647 "TX status report missed for queue %d entry %d\n",
648 entry->queue->qid, entry->entry_idx);
649 rt2x00lib_txdone_noinfo(entry, TXDONE_UNKNOWN);
650 return false;
651 }
652
653 return true;
654}
655
Helmut Schaa14433332010-10-02 11:27:03 +0200656void rt2800_txdone_entry(struct queue_entry *entry, u32 status)
657{
658 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
Helmut Schaab34793e2010-10-02 11:34:56 +0200659 struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
Helmut Schaa14433332010-10-02 11:27:03 +0200660 struct txdone_entry_desc txdesc;
661 u32 word;
662 u16 mcs, real_mcs;
Helmut Schaab34793e2010-10-02 11:34:56 +0200663 int aggr, ampdu;
Helmut Schaa14433332010-10-02 11:27:03 +0200664 __le32 *txwi;
665
666 /*
667 * Obtain the status about this packet.
668 */
669 txdesc.flags = 0;
670 txwi = rt2800_drv_get_txwi(entry);
671 rt2x00_desc_read(txwi, 0, &word);
Helmut Schaab34793e2010-10-02 11:34:56 +0200672
Helmut Schaa14433332010-10-02 11:27:03 +0200673 mcs = rt2x00_get_field32(word, TXWI_W0_MCS);
Helmut Schaab34793e2010-10-02 11:34:56 +0200674 ampdu = rt2x00_get_field32(word, TXWI_W0_AMPDU);
675
Helmut Schaa14433332010-10-02 11:27:03 +0200676 real_mcs = rt2x00_get_field32(status, TX_STA_FIFO_MCS);
Helmut Schaab34793e2010-10-02 11:34:56 +0200677 aggr = rt2x00_get_field32(status, TX_STA_FIFO_TX_AGGRE);
678
679 /*
680 * If a frame was meant to be sent as a single non-aggregated MPDU
681 * but ended up in an aggregate the used tx rate doesn't correlate
682 * with the one specified in the TXWI as the whole aggregate is sent
683 * with the same rate.
684 *
685 * For example: two frames are sent to rt2x00, the first one sets
686 * AMPDU=1 and requests MCS7 whereas the second frame sets AMDPU=0
687 * and requests MCS15. If the hw aggregates both frames into one
688 * AMDPU the tx status for both frames will contain MCS7 although
689 * the frame was sent successfully.
690 *
691 * Hence, replace the requested rate with the real tx rate to not
692 * confuse the rate control algortihm by providing clearly wrong
693 * data.
694 */
Helmut Schaa5356d962011-03-03 19:40:33 +0100695 if (unlikely(aggr == 1 && ampdu == 0 && real_mcs != mcs)) {
Helmut Schaab34793e2010-10-02 11:34:56 +0200696 skbdesc->tx_rate_idx = real_mcs;
697 mcs = real_mcs;
698 }
Helmut Schaa14433332010-10-02 11:27:03 +0200699
Helmut Schaaf16d2db2011-03-28 13:35:21 +0200700 if (aggr == 1 || ampdu == 1)
701 __set_bit(TXDONE_AMPDU, &txdesc.flags);
702
Helmut Schaa14433332010-10-02 11:27:03 +0200703 /*
704 * Ralink has a retry mechanism using a global fallback
705 * table. We setup this fallback table to try the immediate
706 * lower rate for all rates. In the TX_STA_FIFO, the MCS field
707 * always contains the MCS used for the last transmission, be
708 * it successful or not.
709 */
710 if (rt2x00_get_field32(status, TX_STA_FIFO_TX_SUCCESS)) {
711 /*
712 * Transmission succeeded. The number of retries is
713 * mcs - real_mcs
714 */
715 __set_bit(TXDONE_SUCCESS, &txdesc.flags);
716 txdesc.retry = ((mcs > real_mcs) ? mcs - real_mcs : 0);
717 } else {
718 /*
719 * Transmission failed. The number of retries is
720 * always 7 in this case (for a total number of 8
721 * frames sent).
722 */
723 __set_bit(TXDONE_FAILURE, &txdesc.flags);
724 txdesc.retry = rt2x00dev->long_retry;
725 }
726
727 /*
728 * the frame was retried at least once
729 * -> hw used fallback rates
730 */
731 if (txdesc.retry)
732 __set_bit(TXDONE_FALLBACK, &txdesc.flags);
733
734 rt2x00lib_txdone(entry, &txdesc);
735}
736EXPORT_SYMBOL_GPL(rt2800_txdone_entry);
737
Ivo van Doorn96481b22010-08-06 20:47:57 +0200738void rt2800_txdone(struct rt2x00_dev *rt2x00dev)
739{
740 struct data_queue *queue;
741 struct queue_entry *entry;
Ivo van Doorn96481b22010-08-06 20:47:57 +0200742 u32 reg;
Johannes Stezenbach0e0d39e2011-04-18 15:29:12 +0200743 u8 qid;
Ivo van Doorn96481b22010-08-06 20:47:57 +0200744
Johannes Stezenbach0e0d39e2011-04-18 15:29:12 +0200745 while (kfifo_get(&rt2x00dev->txstatus_fifo, &reg)) {
Ivo van Doorn96481b22010-08-06 20:47:57 +0200746
Johannes Stezenbach0e0d39e2011-04-18 15:29:12 +0200747 /* TX_STA_FIFO_PID_QUEUE is a 2-bit field, thus
748 * qid is guaranteed to be one of the TX QIDs
Ivo van Doorn96481b22010-08-06 20:47:57 +0200749 */
Johannes Stezenbach0e0d39e2011-04-18 15:29:12 +0200750 qid = rt2x00_get_field32(reg, TX_STA_FIFO_PID_QUEUE);
751 queue = rt2x00queue_get_tx_queue(rt2x00dev, qid);
752 if (unlikely(!queue)) {
753 WARNING(rt2x00dev, "Got TX status for an unavailable "
754 "queue %u, dropping\n", qid);
Ivo van Doorn96481b22010-08-06 20:47:57 +0200755 continue;
Johannes Stezenbach0e0d39e2011-04-18 15:29:12 +0200756 }
Ivo van Doorn96481b22010-08-06 20:47:57 +0200757
758 /*
759 * Inside each queue, we process each entry in a chronological
760 * order. We first check that the queue is not empty.
761 */
762 entry = NULL;
763 while (!rt2x00queue_empty(queue)) {
764 entry = rt2x00queue_get_entry(queue, Q_INDEX_DONE);
Ivo van Doorn36138842010-08-30 21:13:30 +0200765 if (rt2800_txdone_entry_check(entry, reg))
Ivo van Doorn96481b22010-08-06 20:47:57 +0200766 break;
Ivo van Doorn96481b22010-08-06 20:47:57 +0200767 }
768
769 if (!entry || rt2x00queue_empty(queue))
770 break;
771
Helmut Schaa14433332010-10-02 11:27:03 +0200772 rt2800_txdone_entry(entry, reg);
Ivo van Doorn96481b22010-08-06 20:47:57 +0200773 }
774}
775EXPORT_SYMBOL_GPL(rt2800_txdone);
776
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200777void rt2800_write_beacon(struct queue_entry *entry, struct txentry_desc *txdesc)
778{
779 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
780 struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
781 unsigned int beacon_base;
Wolfgang Kufner739fd942010-12-13 12:39:12 +0100782 unsigned int padding_len;
Seth Forsheed76dfc62011-02-14 08:52:25 -0600783 u32 orig_reg, reg;
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200784
785 /*
786 * Disable beaconing while we are reloading the beacon data,
787 * otherwise we might be sending out invalid data.
788 */
789 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
Seth Forsheed76dfc62011-02-14 08:52:25 -0600790 orig_reg = reg;
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200791 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
792 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
793
794 /*
795 * Add space for the TXWI in front of the skb.
796 */
Stanislaw Gruszkae10eea62011-07-30 13:32:56 +0200797 memset(skb_push(entry->skb, TXWI_DESC_SIZE), 0, TXWI_DESC_SIZE);
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200798
799 /*
800 * Register descriptor details in skb frame descriptor.
801 */
802 skbdesc->flags |= SKBDESC_DESC_IN_SKB;
803 skbdesc->desc = entry->skb->data;
804 skbdesc->desc_len = TXWI_DESC_SIZE;
805
806 /*
807 * Add the TXWI for the beacon to the skb.
808 */
Ivo van Doorn0c5879b2010-08-06 20:47:20 +0200809 rt2800_write_tx_data(entry, txdesc);
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200810
811 /*
812 * Dump beacon to userspace through debugfs.
813 */
814 rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_BEACON, entry->skb);
815
816 /*
Wolfgang Kufner739fd942010-12-13 12:39:12 +0100817 * Write entire beacon with TXWI and padding to register.
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200818 */
Wolfgang Kufner739fd942010-12-13 12:39:12 +0100819 padding_len = roundup(entry->skb->len, 4) - entry->skb->len;
Seth Forsheed76dfc62011-02-14 08:52:25 -0600820 if (padding_len && skb_pad(entry->skb, padding_len)) {
821 ERROR(rt2x00dev, "Failure padding beacon, aborting\n");
822 /* skb freed by skb_pad() on failure */
823 entry->skb = NULL;
824 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, orig_reg);
825 return;
826 }
827
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200828 beacon_base = HW_BEACON_OFFSET(entry->entry_idx);
Wolfgang Kufner739fd942010-12-13 12:39:12 +0100829 rt2800_register_multiwrite(rt2x00dev, beacon_base, entry->skb->data,
830 entry->skb->len + padding_len);
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200831
832 /*
833 * Enable beaconing again.
834 */
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200835 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 1);
836 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
837
838 /*
839 * Clean up beacon skb.
840 */
841 dev_kfree_skb_any(entry->skb);
842 entry->skb = NULL;
843}
Ivo van Doorn50e888e2010-07-11 12:26:12 +0200844EXPORT_SYMBOL_GPL(rt2800_write_beacon);
Gertjan van Wingerdef0194b22010-06-03 10:51:53 +0200845
Helmut Schaa69cf36a2011-01-30 13:16:03 +0100846static inline void rt2800_clear_beacon_register(struct rt2x00_dev *rt2x00dev,
847 unsigned int beacon_base)
Helmut Schaafdb87252010-06-29 21:48:06 +0200848{
849 int i;
850
851 /*
852 * For the Beacon base registers we only need to clear
853 * the whole TXWI which (when set to 0) will invalidate
854 * the entire beacon.
855 */
856 for (i = 0; i < TXWI_DESC_SIZE; i += sizeof(__le32))
857 rt2800_register_write(rt2x00dev, beacon_base + i, 0);
858}
859
Helmut Schaa69cf36a2011-01-30 13:16:03 +0100860void rt2800_clear_beacon(struct queue_entry *entry)
861{
862 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
863 u32 reg;
864
865 /*
866 * Disable beaconing while we are reloading the beacon data,
867 * otherwise we might be sending out invalid data.
868 */
869 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
870 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
871 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
872
873 /*
874 * Clear beacon.
875 */
876 rt2800_clear_beacon_register(rt2x00dev,
877 HW_BEACON_OFFSET(entry->entry_idx));
878
879 /*
880 * Enabled beaconing again.
881 */
882 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 1);
883 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
884}
885EXPORT_SYMBOL_GPL(rt2800_clear_beacon);
886
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100887#ifdef CONFIG_RT2X00_LIB_DEBUGFS
888const struct rt2x00debug rt2800_rt2x00debug = {
889 .owner = THIS_MODULE,
890 .csr = {
891 .read = rt2800_register_read,
892 .write = rt2800_register_write,
893 .flags = RT2X00DEBUGFS_OFFSET,
894 .word_base = CSR_REG_BASE,
895 .word_size = sizeof(u32),
896 .word_count = CSR_REG_SIZE / sizeof(u32),
897 },
898 .eeprom = {
899 .read = rt2x00_eeprom_read,
900 .write = rt2x00_eeprom_write,
901 .word_base = EEPROM_BASE,
902 .word_size = sizeof(u16),
903 .word_count = EEPROM_SIZE / sizeof(u16),
904 },
905 .bbp = {
906 .read = rt2800_bbp_read,
907 .write = rt2800_bbp_write,
908 .word_base = BBP_BASE,
909 .word_size = sizeof(u8),
910 .word_count = BBP_SIZE / sizeof(u8),
911 },
912 .rf = {
913 .read = rt2x00_rf_read,
914 .write = rt2800_rf_write,
915 .word_base = RF_BASE,
916 .word_size = sizeof(u32),
917 .word_count = RF_SIZE / sizeof(u32),
918 },
919};
920EXPORT_SYMBOL_GPL(rt2800_rt2x00debug);
921#endif /* CONFIG_RT2X00_LIB_DEBUGFS */
922
923int rt2800_rfkill_poll(struct rt2x00_dev *rt2x00dev)
924{
925 u32 reg;
926
927 rt2800_register_read(rt2x00dev, GPIO_CTRL_CFG, &reg);
928 return rt2x00_get_field32(reg, GPIO_CTRL_CFG_BIT2);
929}
930EXPORT_SYMBOL_GPL(rt2800_rfkill_poll);
931
932#ifdef CONFIG_RT2X00_LIB_LEDS
933static void rt2800_brightness_set(struct led_classdev *led_cdev,
934 enum led_brightness brightness)
935{
936 struct rt2x00_led *led =
937 container_of(led_cdev, struct rt2x00_led, led_dev);
938 unsigned int enabled = brightness != LED_OFF;
939 unsigned int bg_mode =
940 (enabled && led->rt2x00dev->curr_band == IEEE80211_BAND_2GHZ);
941 unsigned int polarity =
942 rt2x00_get_field16(led->rt2x00dev->led_mcu_reg,
943 EEPROM_FREQ_LED_POLARITY);
944 unsigned int ledmode =
945 rt2x00_get_field16(led->rt2x00dev->led_mcu_reg,
946 EEPROM_FREQ_LED_MODE);
Layne Edwards44704e52011-04-18 15:26:00 +0200947 u32 reg;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100948
Layne Edwards44704e52011-04-18 15:26:00 +0200949 /* Check for SoC (SOC devices don't support MCU requests) */
950 if (rt2x00_is_soc(led->rt2x00dev)) {
951 rt2800_register_read(led->rt2x00dev, LED_CFG, &reg);
952
953 /* Set LED Polarity */
954 rt2x00_set_field32(&reg, LED_CFG_LED_POLAR, polarity);
955
956 /* Set LED Mode */
957 if (led->type == LED_TYPE_RADIO) {
958 rt2x00_set_field32(&reg, LED_CFG_G_LED_MODE,
959 enabled ? 3 : 0);
960 } else if (led->type == LED_TYPE_ASSOC) {
961 rt2x00_set_field32(&reg, LED_CFG_Y_LED_MODE,
962 enabled ? 3 : 0);
963 } else if (led->type == LED_TYPE_QUALITY) {
964 rt2x00_set_field32(&reg, LED_CFG_R_LED_MODE,
965 enabled ? 3 : 0);
966 }
967
968 rt2800_register_write(led->rt2x00dev, LED_CFG, reg);
969
970 } else {
971 if (led->type == LED_TYPE_RADIO) {
972 rt2800_mcu_request(led->rt2x00dev, MCU_LED, 0xff, ledmode,
973 enabled ? 0x20 : 0);
974 } else if (led->type == LED_TYPE_ASSOC) {
975 rt2800_mcu_request(led->rt2x00dev, MCU_LED, 0xff, ledmode,
976 enabled ? (bg_mode ? 0x60 : 0xa0) : 0x20);
977 } else if (led->type == LED_TYPE_QUALITY) {
978 /*
979 * The brightness is divided into 6 levels (0 - 5),
980 * The specs tell us the following levels:
981 * 0, 1 ,3, 7, 15, 31
982 * to determine the level in a simple way we can simply
983 * work with bitshifting:
984 * (1 << level) - 1
985 */
986 rt2800_mcu_request(led->rt2x00dev, MCU_LED_STRENGTH, 0xff,
987 (1 << brightness / (LED_FULL / 6)) - 1,
988 polarity);
989 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +0100990 }
991}
992
993static int rt2800_blink_set(struct led_classdev *led_cdev,
994 unsigned long *delay_on, unsigned long *delay_off)
995{
996 struct rt2x00_led *led =
997 container_of(led_cdev, struct rt2x00_led, led_dev);
998 u32 reg;
999
1000 rt2800_register_read(led->rt2x00dev, LED_CFG, &reg);
1001 rt2x00_set_field32(&reg, LED_CFG_ON_PERIOD, *delay_on);
1002 rt2x00_set_field32(&reg, LED_CFG_OFF_PERIOD, *delay_off);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001003 rt2800_register_write(led->rt2x00dev, LED_CFG, reg);
1004
1005 return 0;
1006}
1007
Gertjan van Wingerdeb3579d62009-12-30 11:36:34 +01001008static void rt2800_init_led(struct rt2x00_dev *rt2x00dev,
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001009 struct rt2x00_led *led, enum led_type type)
1010{
1011 led->rt2x00dev = rt2x00dev;
1012 led->type = type;
1013 led->led_dev.brightness_set = rt2800_brightness_set;
1014 led->led_dev.blink_set = rt2800_blink_set;
1015 led->flags = LED_INITIALIZED;
1016}
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001017#endif /* CONFIG_RT2X00_LIB_LEDS */
1018
1019/*
1020 * Configuration handlers.
1021 */
1022static void rt2800_config_wcid_attr(struct rt2x00_dev *rt2x00dev,
1023 struct rt2x00lib_crypto *crypto,
1024 struct ieee80211_key_conf *key)
1025{
1026 struct mac_wcid_entry wcid_entry;
1027 struct mac_iveiv_entry iveiv_entry;
1028 u32 offset;
1029 u32 reg;
1030
1031 offset = MAC_WCID_ATTR_ENTRY(key->hw_key_idx);
1032
Ivo van Doorne4a0ab32010-06-14 22:14:19 +02001033 if (crypto->cmd == SET_KEY) {
1034 rt2800_register_read(rt2x00dev, offset, &reg);
1035 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_KEYTAB,
1036 !!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE));
1037 /*
1038 * Both the cipher as the BSS Idx numbers are split in a main
1039 * value of 3 bits, and a extended field for adding one additional
1040 * bit to the value.
1041 */
1042 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_CIPHER,
1043 (crypto->cipher & 0x7));
1044 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_CIPHER_EXT,
1045 (crypto->cipher & 0x8) >> 3);
1046 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_BSS_IDX,
1047 (crypto->bssidx & 0x7));
1048 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_BSS_IDX_EXT,
1049 (crypto->bssidx & 0x8) >> 3);
1050 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_RX_WIUDF, crypto->cipher);
1051 rt2800_register_write(rt2x00dev, offset, reg);
1052 } else {
1053 rt2800_register_write(rt2x00dev, offset, 0);
1054 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001055
1056 offset = MAC_IVEIV_ENTRY(key->hw_key_idx);
1057
1058 memset(&iveiv_entry, 0, sizeof(iveiv_entry));
1059 if ((crypto->cipher == CIPHER_TKIP) ||
1060 (crypto->cipher == CIPHER_TKIP_NO_MIC) ||
1061 (crypto->cipher == CIPHER_AES))
1062 iveiv_entry.iv[3] |= 0x20;
1063 iveiv_entry.iv[3] |= key->keyidx << 6;
1064 rt2800_register_multiwrite(rt2x00dev, offset,
1065 &iveiv_entry, sizeof(iveiv_entry));
1066
1067 offset = MAC_WCID_ENTRY(key->hw_key_idx);
1068
1069 memset(&wcid_entry, 0, sizeof(wcid_entry));
1070 if (crypto->cmd == SET_KEY)
Gertjan van Wingerde10026f72011-01-30 13:23:03 +01001071 memcpy(wcid_entry.mac, crypto->address, ETH_ALEN);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001072 rt2800_register_multiwrite(rt2x00dev, offset,
1073 &wcid_entry, sizeof(wcid_entry));
1074}
1075
1076int rt2800_config_shared_key(struct rt2x00_dev *rt2x00dev,
1077 struct rt2x00lib_crypto *crypto,
1078 struct ieee80211_key_conf *key)
1079{
1080 struct hw_key_entry key_entry;
1081 struct rt2x00_field32 field;
1082 u32 offset;
1083 u32 reg;
1084
1085 if (crypto->cmd == SET_KEY) {
1086 key->hw_key_idx = (4 * crypto->bssidx) + key->keyidx;
1087
1088 memcpy(key_entry.key, crypto->key,
1089 sizeof(key_entry.key));
1090 memcpy(key_entry.tx_mic, crypto->tx_mic,
1091 sizeof(key_entry.tx_mic));
1092 memcpy(key_entry.rx_mic, crypto->rx_mic,
1093 sizeof(key_entry.rx_mic));
1094
1095 offset = SHARED_KEY_ENTRY(key->hw_key_idx);
1096 rt2800_register_multiwrite(rt2x00dev, offset,
1097 &key_entry, sizeof(key_entry));
1098 }
1099
1100 /*
1101 * The cipher types are stored over multiple registers
1102 * starting with SHARED_KEY_MODE_BASE each word will have
1103 * 32 bits and contains the cipher types for 2 bssidx each.
1104 * Using the correct defines correctly will cause overhead,
1105 * so just calculate the correct offset.
1106 */
1107 field.bit_offset = 4 * (key->hw_key_idx % 8);
1108 field.bit_mask = 0x7 << field.bit_offset;
1109
1110 offset = SHARED_KEY_MODE_ENTRY(key->hw_key_idx / 8);
1111
1112 rt2800_register_read(rt2x00dev, offset, &reg);
1113 rt2x00_set_field32(&reg, field,
1114 (crypto->cmd == SET_KEY) * crypto->cipher);
1115 rt2800_register_write(rt2x00dev, offset, reg);
1116
1117 /*
1118 * Update WCID information
1119 */
1120 rt2800_config_wcid_attr(rt2x00dev, crypto, key);
1121
1122 return 0;
1123}
1124EXPORT_SYMBOL_GPL(rt2800_config_shared_key);
1125
Helmut Schaa1ed38112011-03-03 19:44:33 +01001126static inline int rt2800_find_pairwise_keyslot(struct rt2x00_dev *rt2x00dev)
1127{
1128 int idx;
1129 u32 offset, reg;
1130
1131 /*
1132 * Search for the first free pairwise key entry and return the
1133 * corresponding index.
1134 *
1135 * Make sure the WCID starts _after_ the last possible shared key
1136 * entry (>32).
1137 *
1138 * Since parts of the pairwise key table might be shared with
1139 * the beacon frame buffers 6 & 7 we should only write into the
1140 * first 222 entries.
1141 */
1142 for (idx = 33; idx <= 222; idx++) {
1143 offset = MAC_WCID_ATTR_ENTRY(idx);
1144 rt2800_register_read(rt2x00dev, offset, &reg);
1145 if (!reg)
1146 return idx;
1147 }
1148 return -1;
1149}
1150
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001151int rt2800_config_pairwise_key(struct rt2x00_dev *rt2x00dev,
1152 struct rt2x00lib_crypto *crypto,
1153 struct ieee80211_key_conf *key)
1154{
1155 struct hw_key_entry key_entry;
1156 u32 offset;
Helmut Schaa1ed38112011-03-03 19:44:33 +01001157 int idx;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001158
1159 if (crypto->cmd == SET_KEY) {
Helmut Schaa1ed38112011-03-03 19:44:33 +01001160 idx = rt2800_find_pairwise_keyslot(rt2x00dev);
1161 if (idx < 0)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001162 return -ENOSPC;
Helmut Schaa1ed38112011-03-03 19:44:33 +01001163 key->hw_key_idx = idx;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001164
1165 memcpy(key_entry.key, crypto->key,
1166 sizeof(key_entry.key));
1167 memcpy(key_entry.tx_mic, crypto->tx_mic,
1168 sizeof(key_entry.tx_mic));
1169 memcpy(key_entry.rx_mic, crypto->rx_mic,
1170 sizeof(key_entry.rx_mic));
1171
1172 offset = PAIRWISE_KEY_ENTRY(key->hw_key_idx);
1173 rt2800_register_multiwrite(rt2x00dev, offset,
1174 &key_entry, sizeof(key_entry));
1175 }
1176
1177 /*
1178 * Update WCID information
1179 */
1180 rt2800_config_wcid_attr(rt2x00dev, crypto, key);
1181
1182 return 0;
1183}
1184EXPORT_SYMBOL_GPL(rt2800_config_pairwise_key);
1185
1186void rt2800_config_filter(struct rt2x00_dev *rt2x00dev,
1187 const unsigned int filter_flags)
1188{
1189 u32 reg;
1190
1191 /*
1192 * Start configuration steps.
1193 * Note that the version error will always be dropped
1194 * and broadcast frames will always be accepted since
1195 * there is no filter for it at this time.
1196 */
1197 rt2800_register_read(rt2x00dev, RX_FILTER_CFG, &reg);
1198 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CRC_ERROR,
1199 !(filter_flags & FIF_FCSFAIL));
1200 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_PHY_ERROR,
1201 !(filter_flags & FIF_PLCPFAIL));
1202 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_NOT_TO_ME,
1203 !(filter_flags & FIF_PROMISC_IN_BSS));
1204 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_NOT_MY_BSSD, 0);
1205 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_VER_ERROR, 1);
1206 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_MULTICAST,
1207 !(filter_flags & FIF_ALLMULTI));
1208 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BROADCAST, 0);
1209 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_DUPLICATE, 1);
1210 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CF_END_ACK,
1211 !(filter_flags & FIF_CONTROL));
1212 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CF_END,
1213 !(filter_flags & FIF_CONTROL));
1214 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_ACK,
1215 !(filter_flags & FIF_CONTROL));
1216 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CTS,
1217 !(filter_flags & FIF_CONTROL));
1218 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_RTS,
1219 !(filter_flags & FIF_CONTROL));
1220 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_PSPOLL,
1221 !(filter_flags & FIF_PSPOLL));
1222 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BA, 1);
1223 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BAR, 0);
1224 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CNTL,
1225 !(filter_flags & FIF_CONTROL));
1226 rt2800_register_write(rt2x00dev, RX_FILTER_CFG, reg);
1227}
1228EXPORT_SYMBOL_GPL(rt2800_config_filter);
1229
1230void rt2800_config_intf(struct rt2x00_dev *rt2x00dev, struct rt2x00_intf *intf,
1231 struct rt2x00intf_conf *conf, const unsigned int flags)
1232{
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001233 u32 reg;
Helmut Schaafa8b4b22010-11-04 20:42:36 +01001234 bool update_bssid = false;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001235
1236 if (flags & CONFIG_UPDATE_TYPE) {
1237 /*
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001238 * Enable synchronisation.
1239 */
1240 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001241 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_SYNC, conf->sync);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001242 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
Helmut Schaa15a533c2011-04-18 15:28:04 +02001243
1244 if (conf->sync == TSF_SYNC_AP_NONE) {
1245 /*
1246 * Tune beacon queue transmit parameters for AP mode
1247 */
1248 rt2800_register_read(rt2x00dev, TBTT_SYNC_CFG, &reg);
1249 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_CWMIN, 0);
1250 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_AIFSN, 1);
1251 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_EXP_WIN, 32);
1252 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_TBTT_ADJUST, 0);
1253 rt2800_register_write(rt2x00dev, TBTT_SYNC_CFG, reg);
1254 } else {
1255 rt2800_register_read(rt2x00dev, TBTT_SYNC_CFG, &reg);
1256 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_CWMIN, 4);
1257 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_AIFSN, 2);
1258 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_BCN_EXP_WIN, 32);
1259 rt2x00_set_field32(&reg, TBTT_SYNC_CFG_TBTT_ADJUST, 16);
1260 rt2800_register_write(rt2x00dev, TBTT_SYNC_CFG, reg);
1261 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001262 }
1263
1264 if (flags & CONFIG_UPDATE_MAC) {
Helmut Schaafa8b4b22010-11-04 20:42:36 +01001265 if (flags & CONFIG_UPDATE_TYPE &&
1266 conf->sync == TSF_SYNC_AP_NONE) {
1267 /*
1268 * The BSSID register has to be set to our own mac
1269 * address in AP mode.
1270 */
1271 memcpy(conf->bssid, conf->mac, sizeof(conf->mac));
1272 update_bssid = true;
1273 }
1274
Ivo van Doornc600c822010-08-30 21:14:15 +02001275 if (!is_zero_ether_addr((const u8 *)conf->mac)) {
1276 reg = le32_to_cpu(conf->mac[1]);
1277 rt2x00_set_field32(&reg, MAC_ADDR_DW1_UNICAST_TO_ME_MASK, 0xff);
1278 conf->mac[1] = cpu_to_le32(reg);
1279 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001280
1281 rt2800_register_multiwrite(rt2x00dev, MAC_ADDR_DW0,
1282 conf->mac, sizeof(conf->mac));
1283 }
1284
Helmut Schaafa8b4b22010-11-04 20:42:36 +01001285 if ((flags & CONFIG_UPDATE_BSSID) || update_bssid) {
Ivo van Doornc600c822010-08-30 21:14:15 +02001286 if (!is_zero_ether_addr((const u8 *)conf->bssid)) {
1287 reg = le32_to_cpu(conf->bssid[1]);
1288 rt2x00_set_field32(&reg, MAC_BSSID_DW1_BSS_ID_MASK, 3);
1289 rt2x00_set_field32(&reg, MAC_BSSID_DW1_BSS_BCN_NUM, 7);
1290 conf->bssid[1] = cpu_to_le32(reg);
1291 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001292
1293 rt2800_register_multiwrite(rt2x00dev, MAC_BSSID_DW0,
1294 conf->bssid, sizeof(conf->bssid));
1295 }
1296}
1297EXPORT_SYMBOL_GPL(rt2800_config_intf);
1298
Helmut Schaa87c19152010-10-02 11:28:34 +02001299static void rt2800_config_ht_opmode(struct rt2x00_dev *rt2x00dev,
1300 struct rt2x00lib_erp *erp)
1301{
1302 bool any_sta_nongf = !!(erp->ht_opmode &
1303 IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
1304 u8 protection = erp->ht_opmode & IEEE80211_HT_OP_MODE_PROTECTION;
1305 u8 mm20_mode, mm40_mode, gf20_mode, gf40_mode;
1306 u16 mm20_rate, mm40_rate, gf20_rate, gf40_rate;
1307 u32 reg;
1308
1309 /* default protection rate for HT20: OFDM 24M */
1310 mm20_rate = gf20_rate = 0x4004;
1311
1312 /* default protection rate for HT40: duplicate OFDM 24M */
1313 mm40_rate = gf40_rate = 0x4084;
1314
1315 switch (protection) {
1316 case IEEE80211_HT_OP_MODE_PROTECTION_NONE:
1317 /*
1318 * All STAs in this BSS are HT20/40 but there might be
1319 * STAs not supporting greenfield mode.
1320 * => Disable protection for HT transmissions.
1321 */
1322 mm20_mode = mm40_mode = gf20_mode = gf40_mode = 0;
1323
1324 break;
1325 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
1326 /*
1327 * All STAs in this BSS are HT20 or HT20/40 but there
1328 * might be STAs not supporting greenfield mode.
1329 * => Protect all HT40 transmissions.
1330 */
1331 mm20_mode = gf20_mode = 0;
1332 mm40_mode = gf40_mode = 2;
1333
1334 break;
1335 case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:
1336 /*
1337 * Nonmember protection:
1338 * According to 802.11n we _should_ protect all
1339 * HT transmissions (but we don't have to).
1340 *
1341 * But if cts_protection is enabled we _shall_ protect
1342 * all HT transmissions using a CCK rate.
1343 *
1344 * And if any station is non GF we _shall_ protect
1345 * GF transmissions.
1346 *
1347 * We decide to protect everything
1348 * -> fall through to mixed mode.
1349 */
1350 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
1351 /*
1352 * Legacy STAs are present
1353 * => Protect all HT transmissions.
1354 */
1355 mm20_mode = mm40_mode = gf20_mode = gf40_mode = 2;
1356
1357 /*
1358 * If erp protection is needed we have to protect HT
1359 * transmissions with CCK 11M long preamble.
1360 */
1361 if (erp->cts_protection) {
1362 /* don't duplicate RTS/CTS in CCK mode */
1363 mm20_rate = mm40_rate = 0x0003;
1364 gf20_rate = gf40_rate = 0x0003;
1365 }
1366 break;
1367 };
1368
1369 /* check for STAs not supporting greenfield mode */
1370 if (any_sta_nongf)
1371 gf20_mode = gf40_mode = 2;
1372
1373 /* Update HT protection config */
1374 rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
1375 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_RATE, mm20_rate);
1376 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_CTRL, mm20_mode);
1377 rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
1378
1379 rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
1380 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_RATE, mm40_rate);
1381 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_CTRL, mm40_mode);
1382 rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
1383
1384 rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
1385 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_RATE, gf20_rate);
1386 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_CTRL, gf20_mode);
1387 rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
1388
1389 rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
1390 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_RATE, gf40_rate);
1391 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_CTRL, gf40_mode);
1392 rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
1393}
1394
Helmut Schaa02044642010-09-08 20:56:32 +02001395void rt2800_config_erp(struct rt2x00_dev *rt2x00dev, struct rt2x00lib_erp *erp,
1396 u32 changed)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001397{
1398 u32 reg;
1399
Helmut Schaa02044642010-09-08 20:56:32 +02001400 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1401 rt2800_register_read(rt2x00dev, AUTO_RSP_CFG, &reg);
1402 rt2x00_set_field32(&reg, AUTO_RSP_CFG_BAC_ACK_POLICY,
1403 !!erp->short_preamble);
1404 rt2x00_set_field32(&reg, AUTO_RSP_CFG_AR_PREAMBLE,
1405 !!erp->short_preamble);
1406 rt2800_register_write(rt2x00dev, AUTO_RSP_CFG, reg);
1407 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001408
Helmut Schaa02044642010-09-08 20:56:32 +02001409 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1410 rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
1411 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_CTRL,
1412 erp->cts_protection ? 2 : 0);
1413 rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
1414 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001415
Helmut Schaa02044642010-09-08 20:56:32 +02001416 if (changed & BSS_CHANGED_BASIC_RATES) {
1417 rt2800_register_write(rt2x00dev, LEGACY_BASIC_RATE,
1418 erp->basic_rates);
1419 rt2800_register_write(rt2x00dev, HT_BASIC_RATE, 0x00008003);
1420 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001421
Helmut Schaa02044642010-09-08 20:56:32 +02001422 if (changed & BSS_CHANGED_ERP_SLOT) {
1423 rt2800_register_read(rt2x00dev, BKOFF_SLOT_CFG, &reg);
1424 rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_SLOT_TIME,
1425 erp->slot_time);
1426 rt2800_register_write(rt2x00dev, BKOFF_SLOT_CFG, reg);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001427
Helmut Schaa02044642010-09-08 20:56:32 +02001428 rt2800_register_read(rt2x00dev, XIFS_TIME_CFG, &reg);
1429 rt2x00_set_field32(&reg, XIFS_TIME_CFG_EIFS, erp->eifs);
1430 rt2800_register_write(rt2x00dev, XIFS_TIME_CFG, reg);
1431 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001432
Helmut Schaa02044642010-09-08 20:56:32 +02001433 if (changed & BSS_CHANGED_BEACON_INT) {
1434 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
1435 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_INTERVAL,
1436 erp->beacon_int * 16);
1437 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
1438 }
Helmut Schaa87c19152010-10-02 11:28:34 +02001439
1440 if (changed & BSS_CHANGED_HT)
1441 rt2800_config_ht_opmode(rt2x00dev, erp);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001442}
1443EXPORT_SYMBOL_GPL(rt2800_config_erp);
1444
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001445static void rt2800_set_ant_diversity(struct rt2x00_dev *rt2x00dev,
1446 enum antenna ant)
1447{
1448 u32 reg;
1449 u8 eesk_pin = (ant == ANTENNA_A) ? 1 : 0;
1450 u8 gpio_bit3 = (ant == ANTENNA_A) ? 0 : 1;
1451
1452 if (rt2x00_is_pci(rt2x00dev)) {
1453 rt2800_register_read(rt2x00dev, E2PROM_CSR, &reg);
1454 rt2x00_set_field32(&reg, E2PROM_CSR_DATA_CLOCK, eesk_pin);
1455 rt2800_register_write(rt2x00dev, E2PROM_CSR, reg);
1456 } else if (rt2x00_is_usb(rt2x00dev))
1457 rt2800_mcu_request(rt2x00dev, MCU_ANT_SELECT, 0xff,
1458 eesk_pin, 0);
1459
1460 rt2800_register_read(rt2x00dev, GPIO_CTRL_CFG, &reg);
Shiang Tufe591472011-02-20 13:57:22 +01001461 rt2x00_set_field32(&reg, GPIO_CTRL_CFG_GPIOD_BIT3, 0);
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001462 rt2x00_set_field32(&reg, GPIO_CTRL_CFG_BIT3, gpio_bit3);
1463 rt2800_register_write(rt2x00dev, GPIO_CTRL_CFG, reg);
1464}
1465
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001466void rt2800_config_ant(struct rt2x00_dev *rt2x00dev, struct antenna_setup *ant)
1467{
1468 u8 r1;
1469 u8 r3;
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001470 u16 eeprom;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001471
1472 rt2800_bbp_read(rt2x00dev, 1, &r1);
1473 rt2800_bbp_read(rt2x00dev, 3, &r3);
1474
1475 /*
1476 * Configure the TX antenna.
1477 */
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001478 switch (ant->tx_chain_num) {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001479 case 1:
1480 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 0);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001481 break;
1482 case 2:
1483 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 2);
1484 break;
1485 case 3:
Ivo van Doorne22557f2010-06-29 21:49:05 +02001486 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 0);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001487 break;
1488 }
1489
1490 /*
1491 * Configure the RX antenna.
1492 */
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001493 switch (ant->rx_chain_num) {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001494 case 1:
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001495 if (rt2x00_rt(rt2x00dev, RT3070) ||
1496 rt2x00_rt(rt2x00dev, RT3090) ||
1497 rt2x00_rt(rt2x00dev, RT3390)) {
1498 rt2x00_eeprom_read(rt2x00dev,
1499 EEPROM_NIC_CONF1, &eeprom);
1500 if (rt2x00_get_field16(eeprom,
1501 EEPROM_NIC_CONF1_ANT_DIVERSITY))
1502 rt2800_set_ant_diversity(rt2x00dev,
1503 rt2x00dev->default_ant.rx);
1504 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001505 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 0);
1506 break;
1507 case 2:
1508 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 1);
1509 break;
1510 case 3:
1511 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 2);
1512 break;
1513 }
1514
1515 rt2800_bbp_write(rt2x00dev, 3, r3);
1516 rt2800_bbp_write(rt2x00dev, 1, r1);
1517}
1518EXPORT_SYMBOL_GPL(rt2800_config_ant);
1519
1520static void rt2800_config_lna_gain(struct rt2x00_dev *rt2x00dev,
1521 struct rt2x00lib_conf *libconf)
1522{
1523 u16 eeprom;
1524 short lna_gain;
1525
1526 if (libconf->rf.channel <= 14) {
1527 rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &eeprom);
1528 lna_gain = rt2x00_get_field16(eeprom, EEPROM_LNA_BG);
1529 } else if (libconf->rf.channel <= 64) {
1530 rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &eeprom);
1531 lna_gain = rt2x00_get_field16(eeprom, EEPROM_LNA_A0);
1532 } else if (libconf->rf.channel <= 128) {
1533 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &eeprom);
1534 lna_gain = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG2_LNA_A1);
1535 } else {
1536 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &eeprom);
1537 lna_gain = rt2x00_get_field16(eeprom, EEPROM_RSSI_A2_LNA_A2);
1538 }
1539
1540 rt2x00dev->lna_gain = lna_gain;
1541}
1542
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +02001543static void rt2800_config_channel_rf2xxx(struct rt2x00_dev *rt2x00dev,
1544 struct ieee80211_conf *conf,
1545 struct rf_channel *rf,
1546 struct channel_info *info)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001547{
1548 rt2x00_set_field32(&rf->rf4, RF4_FREQ_OFFSET, rt2x00dev->freq_offset);
1549
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001550 if (rt2x00dev->default_ant.tx_chain_num == 1)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001551 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_TX1, 1);
1552
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001553 if (rt2x00dev->default_ant.rx_chain_num == 1) {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001554 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX1, 1);
1555 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX2, 1);
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001556 } else if (rt2x00dev->default_ant.rx_chain_num == 2)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001557 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX2, 1);
1558
1559 if (rf->channel > 14) {
1560 /*
1561 * When TX power is below 0, we should increase it by 7 to
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001562 * make it a positive value (Minimum value is -7).
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001563 * However this means that values between 0 and 7 have
1564 * double meaning, and we should set a 7DBm boost flag.
1565 */
1566 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_A_7DBM_BOOST,
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001567 (info->default_power1 >= 0));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001568
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001569 if (info->default_power1 < 0)
1570 info->default_power1 += 7;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001571
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001572 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_A, info->default_power1);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001573
1574 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_A_7DBM_BOOST,
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001575 (info->default_power2 >= 0));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001576
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001577 if (info->default_power2 < 0)
1578 info->default_power2 += 7;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001579
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001580 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_A, info->default_power2);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001581 } else {
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001582 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_G, info->default_power1);
1583 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_G, info->default_power2);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001584 }
1585
1586 rt2x00_set_field32(&rf->rf4, RF4_HT40, conf_is_ht40(conf));
1587
1588 rt2800_rf_write(rt2x00dev, 1, rf->rf1);
1589 rt2800_rf_write(rt2x00dev, 2, rf->rf2);
1590 rt2800_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
1591 rt2800_rf_write(rt2x00dev, 4, rf->rf4);
1592
1593 udelay(200);
1594
1595 rt2800_rf_write(rt2x00dev, 1, rf->rf1);
1596 rt2800_rf_write(rt2x00dev, 2, rf->rf2);
1597 rt2800_rf_write(rt2x00dev, 3, rf->rf3 | 0x00000004);
1598 rt2800_rf_write(rt2x00dev, 4, rf->rf4);
1599
1600 udelay(200);
1601
1602 rt2800_rf_write(rt2x00dev, 1, rf->rf1);
1603 rt2800_rf_write(rt2x00dev, 2, rf->rf2);
1604 rt2800_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
1605 rt2800_rf_write(rt2x00dev, 4, rf->rf4);
1606}
1607
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +02001608static void rt2800_config_channel_rf3xxx(struct rt2x00_dev *rt2x00dev,
1609 struct ieee80211_conf *conf,
1610 struct rf_channel *rf,
1611 struct channel_info *info)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001612{
1613 u8 rfcsr;
1614
1615 rt2800_rfcsr_write(rt2x00dev, 2, rf->rf1);
Gertjan van Wingerde41a26172009-11-09 22:59:04 +01001616 rt2800_rfcsr_write(rt2x00dev, 3, rf->rf3);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001617
1618 rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
Gertjan van Wingerdefab799c2010-04-11 14:31:08 +02001619 rt2x00_set_field8(&rfcsr, RFCSR6_R1, rf->rf2);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001620 rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
1621
1622 rt2800_rfcsr_read(rt2x00dev, 12, &rfcsr);
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001623 rt2x00_set_field8(&rfcsr, RFCSR12_TX_POWER, info->default_power1);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001624 rt2800_rfcsr_write(rt2x00dev, 12, rfcsr);
1625
Helmut Schaa5a673962010-04-23 15:54:43 +02001626 rt2800_rfcsr_read(rt2x00dev, 13, &rfcsr);
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001627 rt2x00_set_field8(&rfcsr, RFCSR13_TX_POWER, info->default_power2);
Helmut Schaa5a673962010-04-23 15:54:43 +02001628 rt2800_rfcsr_write(rt2x00dev, 13, rfcsr);
1629
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001630 rt2800_rfcsr_read(rt2x00dev, 23, &rfcsr);
1631 rt2x00_set_field8(&rfcsr, RFCSR23_FREQ_OFFSET, rt2x00dev->freq_offset);
1632 rt2800_rfcsr_write(rt2x00dev, 23, rfcsr);
1633
1634 rt2800_rfcsr_write(rt2x00dev, 24,
1635 rt2x00dev->calibration[conf_is_ht40(conf)]);
1636
Gertjan van Wingerde71976902010-03-24 21:42:36 +01001637 rt2800_rfcsr_read(rt2x00dev, 7, &rfcsr);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001638 rt2x00_set_field8(&rfcsr, RFCSR7_RF_TUNING, 1);
Gertjan van Wingerde71976902010-03-24 21:42:36 +01001639 rt2800_rfcsr_write(rt2x00dev, 7, rfcsr);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001640}
1641
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01001642
1643#define RT5390_POWER_BOUND 0x27
1644#define RT5390_FREQ_OFFSET_BOUND 0x5f
1645
1646static void rt2800_config_channel_rf53xx(struct rt2x00_dev *rt2x00dev,
Gabor Juhosadde5882011-03-03 11:46:45 +01001647 struct ieee80211_conf *conf,
1648 struct rf_channel *rf,
1649 struct channel_info *info)
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01001650{
Gabor Juhosadde5882011-03-03 11:46:45 +01001651 u8 rfcsr;
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01001652
Gabor Juhosadde5882011-03-03 11:46:45 +01001653 rt2800_rfcsr_write(rt2x00dev, 8, rf->rf1);
1654 rt2800_rfcsr_write(rt2x00dev, 9, rf->rf3);
1655 rt2800_rfcsr_read(rt2x00dev, 11, &rfcsr);
1656 rt2x00_set_field8(&rfcsr, RFCSR11_R, rf->rf2);
1657 rt2800_rfcsr_write(rt2x00dev, 11, rfcsr);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01001658
Gabor Juhosadde5882011-03-03 11:46:45 +01001659 rt2800_rfcsr_read(rt2x00dev, 49, &rfcsr);
1660 if (info->default_power1 > RT5390_POWER_BOUND)
1661 rt2x00_set_field8(&rfcsr, RFCSR49_TX, RT5390_POWER_BOUND);
1662 else
1663 rt2x00_set_field8(&rfcsr, RFCSR49_TX, info->default_power1);
1664 rt2800_rfcsr_write(rt2x00dev, 49, rfcsr);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01001665
Gabor Juhosadde5882011-03-03 11:46:45 +01001666 rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
1667 rt2x00_set_field8(&rfcsr, RFCSR1_RF_BLOCK_EN, 1);
1668 rt2x00_set_field8(&rfcsr, RFCSR1_PLL_PD, 1);
1669 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 1);
1670 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 1);
1671 rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01001672
Gabor Juhosadde5882011-03-03 11:46:45 +01001673 rt2800_rfcsr_read(rt2x00dev, 17, &rfcsr);
1674 if (rt2x00dev->freq_offset > RT5390_FREQ_OFFSET_BOUND)
1675 rt2x00_set_field8(&rfcsr, RFCSR17_CODE,
1676 RT5390_FREQ_OFFSET_BOUND);
1677 else
1678 rt2x00_set_field8(&rfcsr, RFCSR17_CODE, rt2x00dev->freq_offset);
1679 rt2800_rfcsr_write(rt2x00dev, 17, rfcsr);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01001680
Gabor Juhosadde5882011-03-03 11:46:45 +01001681 if (rf->channel <= 14) {
1682 int idx = rf->channel-1;
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01001683
Gertjan van Wingerdefdbc7b02011-04-30 17:15:37 +02001684 if (test_bit(CAPABILITY_BT_COEXIST, &rt2x00dev->cap_flags)) {
Gabor Juhosadde5882011-03-03 11:46:45 +01001685 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F)) {
1686 /* r55/r59 value array of channel 1~14 */
1687 static const char r55_bt_rev[] = {0x83, 0x83,
1688 0x83, 0x73, 0x73, 0x63, 0x53, 0x53,
1689 0x53, 0x43, 0x43, 0x43, 0x43, 0x43};
1690 static const char r59_bt_rev[] = {0x0e, 0x0e,
1691 0x0e, 0x0e, 0x0e, 0x0b, 0x0a, 0x09,
1692 0x07, 0x07, 0x07, 0x07, 0x07, 0x07};
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01001693
Gabor Juhosadde5882011-03-03 11:46:45 +01001694 rt2800_rfcsr_write(rt2x00dev, 55,
1695 r55_bt_rev[idx]);
1696 rt2800_rfcsr_write(rt2x00dev, 59,
1697 r59_bt_rev[idx]);
1698 } else {
1699 static const char r59_bt[] = {0x8b, 0x8b, 0x8b,
1700 0x8b, 0x8b, 0x8b, 0x8b, 0x8a, 0x89,
1701 0x88, 0x88, 0x86, 0x85, 0x84};
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01001702
Gabor Juhosadde5882011-03-03 11:46:45 +01001703 rt2800_rfcsr_write(rt2x00dev, 59, r59_bt[idx]);
1704 }
1705 } else {
1706 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F)) {
1707 static const char r55_nonbt_rev[] = {0x23, 0x23,
1708 0x23, 0x23, 0x13, 0x13, 0x03, 0x03,
1709 0x03, 0x03, 0x03, 0x03, 0x03, 0x03};
1710 static const char r59_nonbt_rev[] = {0x07, 0x07,
1711 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
1712 0x07, 0x07, 0x06, 0x05, 0x04, 0x04};
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01001713
Gabor Juhosadde5882011-03-03 11:46:45 +01001714 rt2800_rfcsr_write(rt2x00dev, 55,
1715 r55_nonbt_rev[idx]);
1716 rt2800_rfcsr_write(rt2x00dev, 59,
1717 r59_nonbt_rev[idx]);
1718 } else if (rt2x00_rt(rt2x00dev, RT5390)) {
1719 static const char r59_non_bt[] = {0x8f, 0x8f,
1720 0x8f, 0x8f, 0x8f, 0x8f, 0x8f, 0x8d,
1721 0x8a, 0x88, 0x88, 0x87, 0x87, 0x86};
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01001722
Gabor Juhosadde5882011-03-03 11:46:45 +01001723 rt2800_rfcsr_write(rt2x00dev, 59,
1724 r59_non_bt[idx]);
1725 }
1726 }
1727 }
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01001728
Gabor Juhosadde5882011-03-03 11:46:45 +01001729 rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
1730 rt2x00_set_field8(&rfcsr, RFCSR30_TX_H20M, 0);
1731 rt2x00_set_field8(&rfcsr, RFCSR30_RX_H20M, 0);
1732 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01001733
Gabor Juhosadde5882011-03-03 11:46:45 +01001734 rt2800_rfcsr_read(rt2x00dev, 3, &rfcsr);
1735 rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 1);
1736 rt2800_rfcsr_write(rt2x00dev, 3, rfcsr);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01001737}
1738
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001739static void rt2800_config_channel(struct rt2x00_dev *rt2x00dev,
1740 struct ieee80211_conf *conf,
1741 struct rf_channel *rf,
1742 struct channel_info *info)
1743{
1744 u32 reg;
1745 unsigned int tx_pin;
1746 u8 bbp;
1747
Ivo van Doorn46323e12010-08-23 19:55:43 +02001748 if (rf->channel <= 14) {
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001749 info->default_power1 = TXPOWER_G_TO_DEV(info->default_power1);
1750 info->default_power2 = TXPOWER_G_TO_DEV(info->default_power2);
Ivo van Doorn46323e12010-08-23 19:55:43 +02001751 } else {
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02001752 info->default_power1 = TXPOWER_A_TO_DEV(info->default_power1);
1753 info->default_power2 = TXPOWER_A_TO_DEV(info->default_power2);
Ivo van Doorn46323e12010-08-23 19:55:43 +02001754 }
1755
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +02001756 if (rt2x00_rf(rt2x00dev, RF2020) ||
1757 rt2x00_rf(rt2x00dev, RF3020) ||
1758 rt2x00_rf(rt2x00dev, RF3021) ||
Ivo van Doorn46323e12010-08-23 19:55:43 +02001759 rt2x00_rf(rt2x00dev, RF3022) ||
Gertjan van Wingerdef93bc9b2010-11-13 19:09:50 +01001760 rt2x00_rf(rt2x00dev, RF3052) ||
1761 rt2x00_rf(rt2x00dev, RF3320))
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +02001762 rt2800_config_channel_rf3xxx(rt2x00dev, conf, rf, info);
Gertjan van Wingerdeaca355b2011-05-04 21:41:36 +02001763 else if (rt2x00_rf(rt2x00dev, RF5370) ||
1764 rt2x00_rf(rt2x00dev, RF5390))
Gabor Juhosadde5882011-03-03 11:46:45 +01001765 rt2800_config_channel_rf53xx(rt2x00dev, conf, rf, info);
Gertjan van Wingerdefa6f6322009-11-09 22:59:58 +01001766 else
Gertjan van Wingerde06855ef2010-04-11 14:31:07 +02001767 rt2800_config_channel_rf2xxx(rt2x00dev, conf, rf, info);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001768
1769 /*
1770 * Change BBP settings
1771 */
1772 rt2800_bbp_write(rt2x00dev, 62, 0x37 - rt2x00dev->lna_gain);
1773 rt2800_bbp_write(rt2x00dev, 63, 0x37 - rt2x00dev->lna_gain);
1774 rt2800_bbp_write(rt2x00dev, 64, 0x37 - rt2x00dev->lna_gain);
1775 rt2800_bbp_write(rt2x00dev, 86, 0);
1776
1777 if (rf->channel <= 14) {
Gabor Juhosadde5882011-03-03 11:46:45 +01001778 if (!rt2x00_rt(rt2x00dev, RT5390)) {
Ivo van Doorn7dab73b2011-04-18 15:27:06 +02001779 if (test_bit(CAPABILITY_EXTERNAL_LNA_BG,
1780 &rt2x00dev->cap_flags)) {
Gabor Juhosadde5882011-03-03 11:46:45 +01001781 rt2800_bbp_write(rt2x00dev, 82, 0x62);
1782 rt2800_bbp_write(rt2x00dev, 75, 0x46);
1783 } else {
1784 rt2800_bbp_write(rt2x00dev, 82, 0x84);
1785 rt2800_bbp_write(rt2x00dev, 75, 0x50);
1786 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001787 }
1788 } else {
1789 rt2800_bbp_write(rt2x00dev, 82, 0xf2);
1790
Ivo van Doorn7dab73b2011-04-18 15:27:06 +02001791 if (test_bit(CAPABILITY_EXTERNAL_LNA_A, &rt2x00dev->cap_flags))
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001792 rt2800_bbp_write(rt2x00dev, 75, 0x46);
1793 else
1794 rt2800_bbp_write(rt2x00dev, 75, 0x50);
1795 }
1796
1797 rt2800_register_read(rt2x00dev, TX_BAND_CFG, &reg);
Gertjan van Wingerdea21ee722010-05-03 22:43:04 +02001798 rt2x00_set_field32(&reg, TX_BAND_CFG_HT40_MINUS, conf_is_ht40_minus(conf));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001799 rt2x00_set_field32(&reg, TX_BAND_CFG_A, rf->channel > 14);
1800 rt2x00_set_field32(&reg, TX_BAND_CFG_BG, rf->channel <= 14);
1801 rt2800_register_write(rt2x00dev, TX_BAND_CFG, reg);
1802
1803 tx_pin = 0;
1804
1805 /* Turn on unused PA or LNA when not using 1T or 1R */
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001806 if (rt2x00dev->default_ant.tx_chain_num == 2) {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001807 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A1_EN, 1);
1808 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G1_EN, 1);
1809 }
1810
1811 /* Turn on unused PA or LNA when not using 1T or 1R */
RA-Jay Hungd96aa642011-02-20 13:54:52 +01001812 if (rt2x00dev->default_ant.rx_chain_num == 2) {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001813 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_A1_EN, 1);
1814 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_G1_EN, 1);
1815 }
1816
1817 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_A0_EN, 1);
1818 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_G0_EN, 1);
1819 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_RFTR_EN, 1);
1820 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_TRSW_EN, 1);
1821 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G0_EN, rf->channel <= 14);
1822 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A0_EN, rf->channel > 14);
1823
1824 rt2800_register_write(rt2x00dev, TX_PIN_CFG, tx_pin);
1825
1826 rt2800_bbp_read(rt2x00dev, 4, &bbp);
1827 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 2 * conf_is_ht40(conf));
1828 rt2800_bbp_write(rt2x00dev, 4, bbp);
1829
1830 rt2800_bbp_read(rt2x00dev, 3, &bbp);
Gertjan van Wingerdea21ee722010-05-03 22:43:04 +02001831 rt2x00_set_field8(&bbp, BBP3_HT40_MINUS, conf_is_ht40_minus(conf));
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001832 rt2800_bbp_write(rt2x00dev, 3, bbp);
1833
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02001834 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C)) {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001835 if (conf_is_ht40(conf)) {
1836 rt2800_bbp_write(rt2x00dev, 69, 0x1a);
1837 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
1838 rt2800_bbp_write(rt2x00dev, 73, 0x16);
1839 } else {
1840 rt2800_bbp_write(rt2x00dev, 69, 0x16);
1841 rt2800_bbp_write(rt2x00dev, 70, 0x08);
1842 rt2800_bbp_write(rt2x00dev, 73, 0x11);
1843 }
1844 }
1845
1846 msleep(1);
Helmut Schaa977206d2010-12-13 12:31:58 +01001847
1848 /*
1849 * Clear channel statistic counters
1850 */
1851 rt2800_register_read(rt2x00dev, CH_IDLE_STA, &reg);
1852 rt2800_register_read(rt2x00dev, CH_BUSY_STA, &reg);
1853 rt2800_register_read(rt2x00dev, CH_BUSY_STA_SEC, &reg);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01001854}
1855
Helmut Schaa9e33a352011-03-28 13:33:40 +02001856static int rt2800_get_gain_calibration_delta(struct rt2x00_dev *rt2x00dev)
1857{
1858 u8 tssi_bounds[9];
1859 u8 current_tssi;
1860 u16 eeprom;
1861 u8 step;
1862 int i;
1863
1864 /*
1865 * Read TSSI boundaries for temperature compensation from
1866 * the EEPROM.
1867 *
1868 * Array idx 0 1 2 3 4 5 6 7 8
1869 * Matching Delta value -4 -3 -2 -1 0 +1 +2 +3 +4
1870 * Example TSSI bounds 0xF0 0xD0 0xB5 0xA0 0x88 0x45 0x25 0x15 0x00
1871 */
1872 if (rt2x00dev->curr_band == IEEE80211_BAND_2GHZ) {
1873 rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_BG1, &eeprom);
1874 tssi_bounds[0] = rt2x00_get_field16(eeprom,
1875 EEPROM_TSSI_BOUND_BG1_MINUS4);
1876 tssi_bounds[1] = rt2x00_get_field16(eeprom,
1877 EEPROM_TSSI_BOUND_BG1_MINUS3);
1878
1879 rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_BG2, &eeprom);
1880 tssi_bounds[2] = rt2x00_get_field16(eeprom,
1881 EEPROM_TSSI_BOUND_BG2_MINUS2);
1882 tssi_bounds[3] = rt2x00_get_field16(eeprom,
1883 EEPROM_TSSI_BOUND_BG2_MINUS1);
1884
1885 rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_BG3, &eeprom);
1886 tssi_bounds[4] = rt2x00_get_field16(eeprom,
1887 EEPROM_TSSI_BOUND_BG3_REF);
1888 tssi_bounds[5] = rt2x00_get_field16(eeprom,
1889 EEPROM_TSSI_BOUND_BG3_PLUS1);
1890
1891 rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_BG4, &eeprom);
1892 tssi_bounds[6] = rt2x00_get_field16(eeprom,
1893 EEPROM_TSSI_BOUND_BG4_PLUS2);
1894 tssi_bounds[7] = rt2x00_get_field16(eeprom,
1895 EEPROM_TSSI_BOUND_BG4_PLUS3);
1896
1897 rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_BG5, &eeprom);
1898 tssi_bounds[8] = rt2x00_get_field16(eeprom,
1899 EEPROM_TSSI_BOUND_BG5_PLUS4);
1900
1901 step = rt2x00_get_field16(eeprom,
1902 EEPROM_TSSI_BOUND_BG5_AGC_STEP);
1903 } else {
1904 rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_A1, &eeprom);
1905 tssi_bounds[0] = rt2x00_get_field16(eeprom,
1906 EEPROM_TSSI_BOUND_A1_MINUS4);
1907 tssi_bounds[1] = rt2x00_get_field16(eeprom,
1908 EEPROM_TSSI_BOUND_A1_MINUS3);
1909
1910 rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_A2, &eeprom);
1911 tssi_bounds[2] = rt2x00_get_field16(eeprom,
1912 EEPROM_TSSI_BOUND_A2_MINUS2);
1913 tssi_bounds[3] = rt2x00_get_field16(eeprom,
1914 EEPROM_TSSI_BOUND_A2_MINUS1);
1915
1916 rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_A3, &eeprom);
1917 tssi_bounds[4] = rt2x00_get_field16(eeprom,
1918 EEPROM_TSSI_BOUND_A3_REF);
1919 tssi_bounds[5] = rt2x00_get_field16(eeprom,
1920 EEPROM_TSSI_BOUND_A3_PLUS1);
1921
1922 rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_A4, &eeprom);
1923 tssi_bounds[6] = rt2x00_get_field16(eeprom,
1924 EEPROM_TSSI_BOUND_A4_PLUS2);
1925 tssi_bounds[7] = rt2x00_get_field16(eeprom,
1926 EEPROM_TSSI_BOUND_A4_PLUS3);
1927
1928 rt2x00_eeprom_read(rt2x00dev, EEPROM_TSSI_BOUND_A5, &eeprom);
1929 tssi_bounds[8] = rt2x00_get_field16(eeprom,
1930 EEPROM_TSSI_BOUND_A5_PLUS4);
1931
1932 step = rt2x00_get_field16(eeprom,
1933 EEPROM_TSSI_BOUND_A5_AGC_STEP);
1934 }
1935
1936 /*
1937 * Check if temperature compensation is supported.
1938 */
1939 if (tssi_bounds[4] == 0xff)
1940 return 0;
1941
1942 /*
1943 * Read current TSSI (BBP 49).
1944 */
1945 rt2800_bbp_read(rt2x00dev, 49, &current_tssi);
1946
1947 /*
1948 * Compare TSSI value (BBP49) with the compensation boundaries
1949 * from the EEPROM and increase or decrease tx power.
1950 */
1951 for (i = 0; i <= 3; i++) {
1952 if (current_tssi > tssi_bounds[i])
1953 break;
1954 }
1955
1956 if (i == 4) {
1957 for (i = 8; i >= 5; i--) {
1958 if (current_tssi < tssi_bounds[i])
1959 break;
1960 }
1961 }
1962
1963 return (i - 4) * step;
1964}
1965
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01001966static int rt2800_get_txpower_bw_comp(struct rt2x00_dev *rt2x00dev,
1967 enum ieee80211_band band)
1968{
1969 u16 eeprom;
1970 u8 comp_en;
1971 u8 comp_type;
Helmut Schaa75faae82011-03-28 13:31:30 +02001972 int comp_value = 0;
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01001973
1974 rt2x00_eeprom_read(rt2x00dev, EEPROM_TXPOWER_DELTA, &eeprom);
1975
Helmut Schaa75faae82011-03-28 13:31:30 +02001976 /*
1977 * HT40 compensation not required.
1978 */
1979 if (eeprom == 0xffff ||
1980 !test_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags))
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01001981 return 0;
1982
1983 if (band == IEEE80211_BAND_2GHZ) {
1984 comp_en = rt2x00_get_field16(eeprom,
1985 EEPROM_TXPOWER_DELTA_ENABLE_2G);
1986 if (comp_en) {
1987 comp_type = rt2x00_get_field16(eeprom,
1988 EEPROM_TXPOWER_DELTA_TYPE_2G);
1989 comp_value = rt2x00_get_field16(eeprom,
1990 EEPROM_TXPOWER_DELTA_VALUE_2G);
1991 if (!comp_type)
1992 comp_value = -comp_value;
1993 }
1994 } else {
1995 comp_en = rt2x00_get_field16(eeprom,
1996 EEPROM_TXPOWER_DELTA_ENABLE_5G);
1997 if (comp_en) {
1998 comp_type = rt2x00_get_field16(eeprom,
1999 EEPROM_TXPOWER_DELTA_TYPE_5G);
2000 comp_value = rt2x00_get_field16(eeprom,
2001 EEPROM_TXPOWER_DELTA_VALUE_5G);
2002 if (!comp_type)
2003 comp_value = -comp_value;
2004 }
2005 }
2006
2007 return comp_value;
2008}
2009
Helmut Schaafa71a162011-03-28 13:32:32 +02002010static u8 rt2800_compensate_txpower(struct rt2x00_dev *rt2x00dev, int is_rate_b,
2011 enum ieee80211_band band, int power_level,
2012 u8 txpower, int delta)
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002013{
2014 u32 reg;
2015 u16 eeprom;
2016 u8 criterion;
2017 u8 eirp_txpower;
2018 u8 eirp_txpower_criterion;
2019 u8 reg_limit;
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002020
2021 if (!((band == IEEE80211_BAND_5GHZ) && is_rate_b))
2022 return txpower;
2023
Ivo van Doorn7dab73b2011-04-18 15:27:06 +02002024 if (test_bit(CAPABILITY_POWER_LIMIT, &rt2x00dev->cap_flags)) {
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002025 /*
2026 * Check if eirp txpower exceed txpower_limit.
2027 * We use OFDM 6M as criterion and its eirp txpower
2028 * is stored at EEPROM_EIRP_MAX_TX_POWER.
2029 * .11b data rate need add additional 4dbm
2030 * when calculating eirp txpower.
2031 */
2032 rt2800_register_read(rt2x00dev, TX_PWR_CFG_0, &reg);
2033 criterion = rt2x00_get_field32(reg, TX_PWR_CFG_0_6MBS);
2034
2035 rt2x00_eeprom_read(rt2x00dev,
2036 EEPROM_EIRP_MAX_TX_POWER, &eeprom);
2037
2038 if (band == IEEE80211_BAND_2GHZ)
2039 eirp_txpower_criterion = rt2x00_get_field16(eeprom,
2040 EEPROM_EIRP_MAX_TX_POWER_2GHZ);
2041 else
2042 eirp_txpower_criterion = rt2x00_get_field16(eeprom,
2043 EEPROM_EIRP_MAX_TX_POWER_5GHZ);
2044
2045 eirp_txpower = eirp_txpower_criterion + (txpower - criterion) +
Helmut Schaa2af242e2011-03-28 13:32:01 +02002046 (is_rate_b ? 4 : 0) + delta;
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002047
2048 reg_limit = (eirp_txpower > power_level) ?
2049 (eirp_txpower - power_level) : 0;
2050 } else
2051 reg_limit = 0;
2052
Helmut Schaa2af242e2011-03-28 13:32:01 +02002053 return txpower + delta - reg_limit;
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002054}
2055
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002056static void rt2800_config_txpower(struct rt2x00_dev *rt2x00dev,
Helmut Schaa9e33a352011-03-28 13:33:40 +02002057 enum ieee80211_band band,
2058 int power_level)
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002059{
Helmut Schaa5e846002010-07-11 12:23:09 +02002060 u8 txpower;
Helmut Schaa5e846002010-07-11 12:23:09 +02002061 u16 eeprom;
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002062 int i, is_rate_b;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002063 u32 reg;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002064 u8 r1;
Helmut Schaa5e846002010-07-11 12:23:09 +02002065 u32 offset;
Helmut Schaa2af242e2011-03-28 13:32:01 +02002066 int delta;
2067
2068 /*
2069 * Calculate HT40 compensation delta
2070 */
2071 delta = rt2800_get_txpower_bw_comp(rt2x00dev, band);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002072
Helmut Schaa5e846002010-07-11 12:23:09 +02002073 /*
Helmut Schaa9e33a352011-03-28 13:33:40 +02002074 * calculate temperature compensation delta
2075 */
2076 delta += rt2800_get_gain_calibration_delta(rt2x00dev);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002077
Helmut Schaa5e846002010-07-11 12:23:09 +02002078 /*
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002079 * set to normal bbp tx power control mode: +/- 0dBm
Helmut Schaa5e846002010-07-11 12:23:09 +02002080 */
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002081 rt2800_bbp_read(rt2x00dev, 1, &r1);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002082 rt2x00_set_field8(&r1, BBP1_TX_POWER_CTRL, 0);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002083 rt2800_bbp_write(rt2x00dev, 1, r1);
Helmut Schaa5e846002010-07-11 12:23:09 +02002084 offset = TX_PWR_CFG_0;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002085
Helmut Schaa5e846002010-07-11 12:23:09 +02002086 for (i = 0; i < EEPROM_TXPOWER_BYRATE_SIZE; i += 2) {
2087 /* just to be safe */
2088 if (offset > TX_PWR_CFG_4)
2089 break;
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002090
Helmut Schaa5e846002010-07-11 12:23:09 +02002091 rt2800_register_read(rt2x00dev, offset, &reg);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002092
Helmut Schaa5e846002010-07-11 12:23:09 +02002093 /* read the next four txpower values */
2094 rt2x00_eeprom_read(rt2x00dev, EEPROM_TXPOWER_BYRATE + i,
2095 &eeprom);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002096
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002097 is_rate_b = i ? 0 : 1;
2098 /*
2099 * TX_PWR_CFG_0: 1MBS, TX_PWR_CFG_1: 24MBS,
Helmut Schaa5e846002010-07-11 12:23:09 +02002100 * TX_PWR_CFG_2: MCS4, TX_PWR_CFG_3: MCS12,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002101 * TX_PWR_CFG_4: unknown
2102 */
Helmut Schaa5e846002010-07-11 12:23:09 +02002103 txpower = rt2x00_get_field16(eeprom,
2104 EEPROM_TXPOWER_BYRATE_RATE0);
Helmut Schaafa71a162011-03-28 13:32:32 +02002105 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02002106 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002107 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE0, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02002108
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002109 /*
2110 * TX_PWR_CFG_0: 2MBS, TX_PWR_CFG_1: 36MBS,
Helmut Schaa5e846002010-07-11 12:23:09 +02002111 * TX_PWR_CFG_2: MCS5, TX_PWR_CFG_3: MCS13,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002112 * TX_PWR_CFG_4: unknown
2113 */
Helmut Schaa5e846002010-07-11 12:23:09 +02002114 txpower = rt2x00_get_field16(eeprom,
2115 EEPROM_TXPOWER_BYRATE_RATE1);
Helmut Schaafa71a162011-03-28 13:32:32 +02002116 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02002117 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002118 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE1, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02002119
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002120 /*
2121 * TX_PWR_CFG_0: 5.5MBS, TX_PWR_CFG_1: 48MBS,
Helmut Schaa5e846002010-07-11 12:23:09 +02002122 * TX_PWR_CFG_2: MCS6, TX_PWR_CFG_3: MCS14,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002123 * TX_PWR_CFG_4: unknown
2124 */
Helmut Schaa5e846002010-07-11 12:23:09 +02002125 txpower = rt2x00_get_field16(eeprom,
2126 EEPROM_TXPOWER_BYRATE_RATE2);
Helmut Schaafa71a162011-03-28 13:32:32 +02002127 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02002128 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002129 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE2, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02002130
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002131 /*
2132 * TX_PWR_CFG_0: 11MBS, TX_PWR_CFG_1: 54MBS,
Helmut Schaa5e846002010-07-11 12:23:09 +02002133 * TX_PWR_CFG_2: MCS7, TX_PWR_CFG_3: MCS15,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002134 * TX_PWR_CFG_4: unknown
2135 */
Helmut Schaa5e846002010-07-11 12:23:09 +02002136 txpower = rt2x00_get_field16(eeprom,
2137 EEPROM_TXPOWER_BYRATE_RATE3);
Helmut Schaafa71a162011-03-28 13:32:32 +02002138 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02002139 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002140 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE3, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02002141
2142 /* read the next four txpower values */
2143 rt2x00_eeprom_read(rt2x00dev, EEPROM_TXPOWER_BYRATE + i + 1,
2144 &eeprom);
2145
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002146 is_rate_b = 0;
2147 /*
2148 * TX_PWR_CFG_0: 6MBS, TX_PWR_CFG_1: MCS0,
Helmut Schaa5e846002010-07-11 12:23:09 +02002149 * TX_PWR_CFG_2: MCS8, TX_PWR_CFG_3: unknown,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002150 * TX_PWR_CFG_4: unknown
2151 */
Helmut Schaa5e846002010-07-11 12:23:09 +02002152 txpower = rt2x00_get_field16(eeprom,
2153 EEPROM_TXPOWER_BYRATE_RATE0);
Helmut Schaafa71a162011-03-28 13:32:32 +02002154 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02002155 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002156 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE4, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02002157
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002158 /*
2159 * TX_PWR_CFG_0: 9MBS, TX_PWR_CFG_1: MCS1,
Helmut Schaa5e846002010-07-11 12:23:09 +02002160 * TX_PWR_CFG_2: MCS9, TX_PWR_CFG_3: unknown,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002161 * TX_PWR_CFG_4: unknown
2162 */
Helmut Schaa5e846002010-07-11 12:23:09 +02002163 txpower = rt2x00_get_field16(eeprom,
2164 EEPROM_TXPOWER_BYRATE_RATE1);
Helmut Schaafa71a162011-03-28 13:32:32 +02002165 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02002166 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002167 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE5, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02002168
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002169 /*
2170 * TX_PWR_CFG_0: 12MBS, TX_PWR_CFG_1: MCS2,
Helmut Schaa5e846002010-07-11 12:23:09 +02002171 * TX_PWR_CFG_2: MCS10, TX_PWR_CFG_3: unknown,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002172 * TX_PWR_CFG_4: unknown
2173 */
Helmut Schaa5e846002010-07-11 12:23:09 +02002174 txpower = rt2x00_get_field16(eeprom,
2175 EEPROM_TXPOWER_BYRATE_RATE2);
Helmut Schaafa71a162011-03-28 13:32:32 +02002176 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02002177 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002178 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE6, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02002179
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002180 /*
2181 * TX_PWR_CFG_0: 18MBS, TX_PWR_CFG_1: MCS3,
Helmut Schaa5e846002010-07-11 12:23:09 +02002182 * TX_PWR_CFG_2: MCS11, TX_PWR_CFG_3: unknown,
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002183 * TX_PWR_CFG_4: unknown
2184 */
Helmut Schaa5e846002010-07-11 12:23:09 +02002185 txpower = rt2x00_get_field16(eeprom,
2186 EEPROM_TXPOWER_BYRATE_RATE3);
Helmut Schaafa71a162011-03-28 13:32:32 +02002187 txpower = rt2800_compensate_txpower(rt2x00dev, is_rate_b, band,
Helmut Schaa2af242e2011-03-28 13:32:01 +02002188 power_level, txpower, delta);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002189 rt2x00_set_field32(&reg, TX_PWR_CFG_RATE7, txpower);
Helmut Schaa5e846002010-07-11 12:23:09 +02002190
2191 rt2800_register_write(rt2x00dev, offset, reg);
2192
2193 /* next TX_PWR_CFG register */
2194 offset += 4;
2195 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002196}
2197
Helmut Schaa9e33a352011-03-28 13:33:40 +02002198void rt2800_gain_calibration(struct rt2x00_dev *rt2x00dev)
2199{
2200 rt2800_config_txpower(rt2x00dev, rt2x00dev->curr_band,
2201 rt2x00dev->tx_power);
2202}
2203EXPORT_SYMBOL_GPL(rt2800_gain_calibration);
2204
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002205static void rt2800_config_retry_limit(struct rt2x00_dev *rt2x00dev,
2206 struct rt2x00lib_conf *libconf)
2207{
2208 u32 reg;
2209
2210 rt2800_register_read(rt2x00dev, TX_RTY_CFG, &reg);
2211 rt2x00_set_field32(&reg, TX_RTY_CFG_SHORT_RTY_LIMIT,
2212 libconf->conf->short_frame_max_tx_count);
2213 rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_LIMIT,
2214 libconf->conf->long_frame_max_tx_count);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002215 rt2800_register_write(rt2x00dev, TX_RTY_CFG, reg);
2216}
2217
2218static void rt2800_config_ps(struct rt2x00_dev *rt2x00dev,
2219 struct rt2x00lib_conf *libconf)
2220{
2221 enum dev_state state =
2222 (libconf->conf->flags & IEEE80211_CONF_PS) ?
2223 STATE_SLEEP : STATE_AWAKE;
2224 u32 reg;
2225
2226 if (state == STATE_SLEEP) {
2227 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, 0);
2228
2229 rt2800_register_read(rt2x00dev, AUTOWAKEUP_CFG, &reg);
2230 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTO_LEAD_TIME, 5);
2231 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_TBCN_BEFORE_WAKE,
2232 libconf->conf->listen_interval - 1);
2233 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTOWAKE, 1);
2234 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, reg);
2235
2236 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
2237 } else {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002238 rt2800_register_read(rt2x00dev, AUTOWAKEUP_CFG, &reg);
2239 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTO_LEAD_TIME, 0);
2240 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_TBCN_BEFORE_WAKE, 0);
2241 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTOWAKE, 0);
2242 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, reg);
Gertjan van Wingerde57318582010-03-30 23:50:23 +02002243
2244 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002245 }
2246}
2247
2248void rt2800_config(struct rt2x00_dev *rt2x00dev,
2249 struct rt2x00lib_conf *libconf,
2250 const unsigned int flags)
2251{
2252 /* Always recalculate LNA gain before changing configuration */
2253 rt2800_config_lna_gain(rt2x00dev, libconf);
2254
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002255 if (flags & IEEE80211_CONF_CHANGE_CHANNEL) {
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002256 rt2800_config_channel(rt2x00dev, libconf->conf,
2257 &libconf->rf, &libconf->channel);
Helmut Schaa9e33a352011-03-28 13:33:40 +02002258 rt2800_config_txpower(rt2x00dev, libconf->conf->channel->band,
2259 libconf->conf->power_level);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01002260 }
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002261 if (flags & IEEE80211_CONF_CHANGE_POWER)
Helmut Schaa9e33a352011-03-28 13:33:40 +02002262 rt2800_config_txpower(rt2x00dev, libconf->conf->channel->band,
2263 libconf->conf->power_level);
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002264 if (flags & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
2265 rt2800_config_retry_limit(rt2x00dev, libconf);
2266 if (flags & IEEE80211_CONF_CHANGE_PS)
2267 rt2800_config_ps(rt2x00dev, libconf);
2268}
2269EXPORT_SYMBOL_GPL(rt2800_config);
2270
2271/*
2272 * Link tuning
2273 */
2274void rt2800_link_stats(struct rt2x00_dev *rt2x00dev, struct link_qual *qual)
2275{
2276 u32 reg;
2277
2278 /*
2279 * Update FCS error count from register.
2280 */
2281 rt2800_register_read(rt2x00dev, RX_STA_CNT0, &reg);
2282 qual->rx_failed = rt2x00_get_field32(reg, RX_STA_CNT0_CRC_ERR);
2283}
2284EXPORT_SYMBOL_GPL(rt2800_link_stats);
2285
2286static u8 rt2800_get_default_vgc(struct rt2x00_dev *rt2x00dev)
2287{
2288 if (rt2x00dev->curr_band == IEEE80211_BAND_2GHZ) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002289 if (rt2x00_rt(rt2x00dev, RT3070) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002290 rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002291 rt2x00_rt(rt2x00dev, RT3090) ||
Gabor Juhosadde5882011-03-03 11:46:45 +01002292 rt2x00_rt(rt2x00dev, RT3390) ||
2293 rt2x00_rt(rt2x00dev, RT5390))
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002294 return 0x1c + (2 * rt2x00dev->lna_gain);
2295 else
2296 return 0x2e + rt2x00dev->lna_gain;
2297 }
2298
2299 if (!test_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags))
2300 return 0x32 + (rt2x00dev->lna_gain * 5) / 3;
2301 else
2302 return 0x3a + (rt2x00dev->lna_gain * 5) / 3;
2303}
2304
2305static inline void rt2800_set_vgc(struct rt2x00_dev *rt2x00dev,
2306 struct link_qual *qual, u8 vgc_level)
2307{
2308 if (qual->vgc_level != vgc_level) {
2309 rt2800_bbp_write(rt2x00dev, 66, vgc_level);
2310 qual->vgc_level = vgc_level;
2311 qual->vgc_level_reg = vgc_level;
2312 }
2313}
2314
2315void rt2800_reset_tuner(struct rt2x00_dev *rt2x00dev, struct link_qual *qual)
2316{
2317 rt2800_set_vgc(rt2x00dev, qual, rt2800_get_default_vgc(rt2x00dev));
2318}
2319EXPORT_SYMBOL_GPL(rt2800_reset_tuner);
2320
2321void rt2800_link_tuner(struct rt2x00_dev *rt2x00dev, struct link_qual *qual,
2322 const u32 count)
2323{
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02002324 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C))
Bartlomiej Zolnierkiewiczf4450612009-11-04 18:36:40 +01002325 return;
2326
2327 /*
2328 * When RSSI is better then -80 increase VGC level with 0x10
2329 */
2330 rt2800_set_vgc(rt2x00dev, qual,
2331 rt2800_get_default_vgc(rt2x00dev) +
2332 ((qual->rssi > -80) * 0x10));
2333}
2334EXPORT_SYMBOL_GPL(rt2800_link_tuner);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002335
2336/*
2337 * Initialization functions.
2338 */
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02002339static int rt2800_init_registers(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002340{
2341 u32 reg;
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002342 u16 eeprom;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002343 unsigned int i;
Gertjan van Wingerdee3a896b2010-06-03 10:52:04 +02002344 int ret;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002345
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002346 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
2347 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
2348 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
2349 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
2350 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
2351 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
2352 rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
2353
Gertjan van Wingerdee3a896b2010-06-03 10:52:04 +02002354 ret = rt2800_drv_init_registers(rt2x00dev);
2355 if (ret)
2356 return ret;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002357
2358 rt2800_register_read(rt2x00dev, BCN_OFFSET0, &reg);
2359 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN0, 0xe0); /* 0x3800 */
2360 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN1, 0xe8); /* 0x3a00 */
2361 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN2, 0xf0); /* 0x3c00 */
2362 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN3, 0xf8); /* 0x3e00 */
2363 rt2800_register_write(rt2x00dev, BCN_OFFSET0, reg);
2364
2365 rt2800_register_read(rt2x00dev, BCN_OFFSET1, &reg);
2366 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN4, 0xc8); /* 0x3200 */
2367 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN5, 0xd0); /* 0x3400 */
2368 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN6, 0x77); /* 0x1dc0 */
2369 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN7, 0x6f); /* 0x1bc0 */
2370 rt2800_register_write(rt2x00dev, BCN_OFFSET1, reg);
2371
2372 rt2800_register_write(rt2x00dev, LEGACY_BASIC_RATE, 0x0000013f);
2373 rt2800_register_write(rt2x00dev, HT_BASIC_RATE, 0x00008003);
2374
2375 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, 0x00000000);
2376
2377 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
Helmut Schaa8544df32010-07-11 12:29:49 +02002378 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_INTERVAL, 1600);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002379 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 0);
2380 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_SYNC, 0);
2381 rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE, 0);
2382 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
2383 rt2x00_set_field32(&reg, BCN_TIME_CFG_TX_TIME_COMPENSATE, 0);
2384 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
2385
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002386 rt2800_config_filter(rt2x00dev, FIF_ALLMULTI);
2387
2388 rt2800_register_read(rt2x00dev, BKOFF_SLOT_CFG, &reg);
2389 rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_SLOT_TIME, 9);
2390 rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_CC_DELAY_TIME, 2);
2391 rt2800_register_write(rt2x00dev, BKOFF_SLOT_CFG, reg);
2392
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002393 if (rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002394 rt2x00_rt(rt2x00dev, RT3090) ||
2395 rt2x00_rt(rt2x00dev, RT3390)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002396 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
2397 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002398 if (rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002399 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E) ||
2400 rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E)) {
RA-Jay Hung38c8a562010-12-13 12:31:27 +01002401 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &eeprom);
2402 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_DAC_TEST))
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002403 rt2800_register_write(rt2x00dev, TX_SW_CFG2,
2404 0x0000002c);
2405 else
2406 rt2800_register_write(rt2x00dev, TX_SW_CFG2,
2407 0x0000000f);
2408 } else {
2409 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
2410 }
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002411 } else if (rt2x00_rt(rt2x00dev, RT3070)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002412 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002413
2414 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F)) {
2415 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
2416 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x0000002c);
2417 } else {
2418 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
2419 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
2420 }
Helmut Schaac295a812010-06-03 10:52:13 +02002421 } else if (rt2800_is_305x_soc(rt2x00dev)) {
2422 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
2423 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
Helmut Schaa961636b2011-04-18 15:28:27 +02002424 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000030);
Gabor Juhosadde5882011-03-03 11:46:45 +01002425 } else if (rt2x00_rt(rt2x00dev, RT5390)) {
2426 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000404);
2427 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
2428 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002429 } else {
2430 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000000);
2431 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
2432 }
2433
2434 rt2800_register_read(rt2x00dev, TX_LINK_CFG, &reg);
2435 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFB_LIFETIME, 32);
2436 rt2x00_set_field32(&reg, TX_LINK_CFG_MFB_ENABLE, 0);
2437 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_UMFS_ENABLE, 0);
2438 rt2x00_set_field32(&reg, TX_LINK_CFG_TX_MRQ_EN, 0);
2439 rt2x00_set_field32(&reg, TX_LINK_CFG_TX_RDG_EN, 0);
2440 rt2x00_set_field32(&reg, TX_LINK_CFG_TX_CF_ACK_EN, 1);
2441 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFB, 0);
2442 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFS, 0);
2443 rt2800_register_write(rt2x00dev, TX_LINK_CFG, reg);
2444
2445 rt2800_register_read(rt2x00dev, TX_TIMEOUT_CFG, &reg);
2446 rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_MPDU_LIFETIME, 9);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002447 rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_RX_ACK_TIMEOUT, 32);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002448 rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_TX_OP_TIMEOUT, 10);
2449 rt2800_register_write(rt2x00dev, TX_TIMEOUT_CFG, reg);
2450
2451 rt2800_register_read(rt2x00dev, MAX_LEN_CFG, &reg);
2452 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_MPDU, AGGREGATION_SIZE);
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02002453 if (rt2x00_rt_rev_gte(rt2x00dev, RT2872, REV_RT2872E) ||
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01002454 rt2x00_rt(rt2x00dev, RT2883) ||
Gertjan van Wingerde8d0c9b62010-04-11 14:31:10 +02002455 rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070E))
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002456 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_PSDU, 2);
2457 else
2458 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_PSDU, 1);
2459 rt2x00_set_field32(&reg, MAX_LEN_CFG_MIN_PSDU, 0);
2460 rt2x00_set_field32(&reg, MAX_LEN_CFG_MIN_MPDU, 0);
2461 rt2800_register_write(rt2x00dev, MAX_LEN_CFG, reg);
2462
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002463 rt2800_register_read(rt2x00dev, LED_CFG, &reg);
2464 rt2x00_set_field32(&reg, LED_CFG_ON_PERIOD, 70);
2465 rt2x00_set_field32(&reg, LED_CFG_OFF_PERIOD, 30);
2466 rt2x00_set_field32(&reg, LED_CFG_SLOW_BLINK_PERIOD, 3);
2467 rt2x00_set_field32(&reg, LED_CFG_R_LED_MODE, 3);
2468 rt2x00_set_field32(&reg, LED_CFG_G_LED_MODE, 3);
2469 rt2x00_set_field32(&reg, LED_CFG_Y_LED_MODE, 3);
2470 rt2x00_set_field32(&reg, LED_CFG_LED_POLAR, 1);
2471 rt2800_register_write(rt2x00dev, LED_CFG, reg);
2472
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002473 rt2800_register_write(rt2x00dev, PBF_MAX_PCNT, 0x1f3fbf9f);
2474
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002475 rt2800_register_read(rt2x00dev, TX_RTY_CFG, &reg);
2476 rt2x00_set_field32(&reg, TX_RTY_CFG_SHORT_RTY_LIMIT, 15);
2477 rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_LIMIT, 31);
2478 rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_THRE, 2000);
2479 rt2x00_set_field32(&reg, TX_RTY_CFG_NON_AGG_RTY_MODE, 0);
2480 rt2x00_set_field32(&reg, TX_RTY_CFG_AGG_RTY_MODE, 0);
2481 rt2x00_set_field32(&reg, TX_RTY_CFG_TX_AUTO_FB_ENABLE, 1);
2482 rt2800_register_write(rt2x00dev, TX_RTY_CFG, reg);
2483
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002484 rt2800_register_read(rt2x00dev, AUTO_RSP_CFG, &reg);
2485 rt2x00_set_field32(&reg, AUTO_RSP_CFG_AUTORESPONDER, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002486 rt2x00_set_field32(&reg, AUTO_RSP_CFG_BAC_ACK_POLICY, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002487 rt2x00_set_field32(&reg, AUTO_RSP_CFG_CTS_40_MMODE, 0);
2488 rt2x00_set_field32(&reg, AUTO_RSP_CFG_CTS_40_MREF, 0);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002489 rt2x00_set_field32(&reg, AUTO_RSP_CFG_AR_PREAMBLE, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002490 rt2x00_set_field32(&reg, AUTO_RSP_CFG_DUAL_CTS_EN, 0);
2491 rt2x00_set_field32(&reg, AUTO_RSP_CFG_ACK_CTS_PSM_BIT, 0);
2492 rt2800_register_write(rt2x00dev, AUTO_RSP_CFG, reg);
2493
2494 rt2800_register_read(rt2x00dev, CCK_PROT_CFG, &reg);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002495 rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_RATE, 3);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002496 rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_CTRL, 0);
Shiang Tu6f492b62011-02-20 13:56:54 +01002497 rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_NAV_SHORT, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002498 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_CCK, 1);
2499 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
2500 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_MM20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002501 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_MM40, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002502 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_GF20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002503 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_GF40, 0);
2504 rt2x00_set_field32(&reg, CCK_PROT_CFG_RTS_TH_EN, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002505 rt2800_register_write(rt2x00dev, CCK_PROT_CFG, reg);
2506
2507 rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002508 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_RATE, 3);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002509 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_CTRL, 0);
Shiang Tu6f492b62011-02-20 13:56:54 +01002510 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_NAV_SHORT, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002511 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_CCK, 1);
2512 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
2513 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_MM20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002514 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_MM40, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002515 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_GF20, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002516 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_GF40, 0);
2517 rt2x00_set_field32(&reg, OFDM_PROT_CFG_RTS_TH_EN, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002518 rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
2519
2520 rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
2521 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_RATE, 0x4004);
2522 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_CTRL, 0);
Shiang Tu6f492b62011-02-20 13:56:54 +01002523 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_NAV_SHORT, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002524 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_CCK, 1);
2525 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
2526 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_MM20, 1);
2527 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_MM40, 0);
2528 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_GF20, 1);
2529 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_GF40, 0);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002530 rt2x00_set_field32(&reg, MM20_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002531 rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
2532
2533 rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
2534 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_RATE, 0x4084);
Helmut Schaad13a97f2010-10-02 11:29:08 +02002535 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_CTRL, 0);
Shiang Tu6f492b62011-02-20 13:56:54 +01002536 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_NAV_SHORT, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002537 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_CCK, 1);
2538 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
2539 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_MM20, 1);
2540 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_MM40, 1);
2541 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_GF20, 1);
2542 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_GF40, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002543 rt2x00_set_field32(&reg, MM40_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002544 rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
2545
2546 rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
2547 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_RATE, 0x4004);
2548 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_CTRL, 0);
Shiang Tu6f492b62011-02-20 13:56:54 +01002549 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_NAV_SHORT, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002550 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_CCK, 1);
2551 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
2552 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_MM20, 1);
2553 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_MM40, 0);
2554 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_GF20, 1);
2555 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_GF40, 0);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002556 rt2x00_set_field32(&reg, GF20_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002557 rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
2558
2559 rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
2560 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_RATE, 0x4084);
2561 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_CTRL, 0);
Shiang Tu6f492b62011-02-20 13:56:54 +01002562 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_NAV_SHORT, 1);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002563 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_CCK, 1);
2564 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
2565 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_MM20, 1);
2566 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_MM40, 1);
2567 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_GF20, 1);
2568 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_GF40, 1);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002569 rt2x00_set_field32(&reg, GF40_PROT_CFG_RTS_TH_EN, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002570 rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
2571
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +01002572 if (rt2x00_is_usb(rt2x00dev)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002573 rt2800_register_write(rt2x00dev, PBF_CFG, 0xf40006);
2574
2575 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
2576 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
2577 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
2578 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
2579 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
2580 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_WP_DMA_BURST_SIZE, 3);
2581 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 0);
2582 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_BIG_ENDIAN, 0);
2583 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_HDR_SCATTER, 0);
2584 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_HDR_SEG_LEN, 0);
2585 rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
2586 }
2587
Helmut Schaa961621a2010-11-04 20:36:59 +01002588 /*
2589 * The legacy driver also sets TXOP_CTRL_CFG_RESERVED_TRUN_EN to 1
2590 * although it is reserved.
2591 */
2592 rt2800_register_read(rt2x00dev, TXOP_CTRL_CFG, &reg);
2593 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_TIMEOUT_TRUN_EN, 1);
2594 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_AC_TRUN_EN, 1);
2595 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_TXRATEGRP_TRUN_EN, 1);
2596 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_USER_MODE_TRUN_EN, 1);
2597 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_MIMO_PS_TRUN_EN, 1);
2598 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_RESERVED_TRUN_EN, 1);
2599 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_LSIG_TXOP_EN, 0);
2600 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_EXT_CCA_EN, 0);
2601 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_EXT_CCA_DLY, 88);
2602 rt2x00_set_field32(&reg, TXOP_CTRL_CFG_EXT_CWMIN, 0);
2603 rt2800_register_write(rt2x00dev, TXOP_CTRL_CFG, reg);
2604
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002605 rt2800_register_write(rt2x00dev, TXOP_HLDR_ET, 0x00000002);
2606
2607 rt2800_register_read(rt2x00dev, TX_RTS_CFG, &reg);
2608 rt2x00_set_field32(&reg, TX_RTS_CFG_AUTO_RTS_RETRY_LIMIT, 32);
2609 rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_THRES,
2610 IEEE80211_MAX_RTS_THRESHOLD);
2611 rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_FBK_EN, 0);
2612 rt2800_register_write(rt2x00dev, TX_RTS_CFG, reg);
2613
2614 rt2800_register_write(rt2x00dev, EXP_ACK_TIME, 0x002400ca);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002615
Helmut Schaaa21c2ab2010-05-06 12:29:04 +02002616 /*
2617 * Usually the CCK SIFS time should be set to 10 and the OFDM SIFS
2618 * time should be set to 16. However, the original Ralink driver uses
2619 * 16 for both and indeed using a value of 10 for CCK SIFS results in
2620 * connection problems with 11g + CTS protection. Hence, use the same
2621 * defaults as the Ralink driver: 16 for both, CCK and OFDM SIFS.
2622 */
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002623 rt2800_register_read(rt2x00dev, XIFS_TIME_CFG, &reg);
Helmut Schaaa21c2ab2010-05-06 12:29:04 +02002624 rt2x00_set_field32(&reg, XIFS_TIME_CFG_CCKM_SIFS_TIME, 16);
2625 rt2x00_set_field32(&reg, XIFS_TIME_CFG_OFDM_SIFS_TIME, 16);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002626 rt2x00_set_field32(&reg, XIFS_TIME_CFG_OFDM_XIFS_TIME, 4);
2627 rt2x00_set_field32(&reg, XIFS_TIME_CFG_EIFS, 314);
2628 rt2x00_set_field32(&reg, XIFS_TIME_CFG_BB_RXEND_ENABLE, 1);
2629 rt2800_register_write(rt2x00dev, XIFS_TIME_CFG, reg);
2630
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002631 rt2800_register_write(rt2x00dev, PWR_PIN_CFG, 0x00000003);
2632
2633 /*
2634 * ASIC will keep garbage value after boot, clear encryption keys.
2635 */
2636 for (i = 0; i < 4; i++)
2637 rt2800_register_write(rt2x00dev,
2638 SHARED_KEY_MODE_ENTRY(i), 0);
2639
2640 for (i = 0; i < 256; i++) {
Joe Perchesf4e16e42010-11-20 18:39:01 -08002641 static const u32 wcid[2] = { 0xffffffff, 0x00ffffff };
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002642 rt2800_register_multiwrite(rt2x00dev, MAC_WCID_ENTRY(i),
2643 wcid, sizeof(wcid));
2644
Helmut Schaa1ed38112011-03-03 19:44:33 +01002645 rt2800_register_write(rt2x00dev, MAC_WCID_ATTR_ENTRY(i), 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002646 rt2800_register_write(rt2x00dev, MAC_IVEIV_ENTRY(i), 0);
2647 }
2648
2649 /*
2650 * Clear all beacons
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002651 */
Helmut Schaa69cf36a2011-01-30 13:16:03 +01002652 rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE0);
2653 rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE1);
2654 rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE2);
2655 rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE3);
2656 rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE4);
2657 rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE5);
2658 rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE6);
2659 rt2800_clear_beacon_register(rt2x00dev, HW_BEACON_BASE7);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002660
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +01002661 if (rt2x00_is_usb(rt2x00dev)) {
Gertjan van Wingerde785c3c02010-06-03 10:51:59 +02002662 rt2800_register_read(rt2x00dev, US_CYC_CNT, &reg);
2663 rt2x00_set_field32(&reg, US_CYC_CNT_CLOCK_CYCLE, 30);
2664 rt2800_register_write(rt2x00dev, US_CYC_CNT, reg);
RA-Jay Hungc6fcc0e2011-01-30 13:21:22 +01002665 } else if (rt2x00_is_pcie(rt2x00dev)) {
2666 rt2800_register_read(rt2x00dev, US_CYC_CNT, &reg);
2667 rt2x00_set_field32(&reg, US_CYC_CNT_CLOCK_CYCLE, 125);
2668 rt2800_register_write(rt2x00dev, US_CYC_CNT, reg);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002669 }
2670
2671 rt2800_register_read(rt2x00dev, HT_FBK_CFG0, &reg);
2672 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS0FBK, 0);
2673 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS1FBK, 0);
2674 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS2FBK, 1);
2675 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS3FBK, 2);
2676 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS4FBK, 3);
2677 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS5FBK, 4);
2678 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS6FBK, 5);
2679 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS7FBK, 6);
2680 rt2800_register_write(rt2x00dev, HT_FBK_CFG0, reg);
2681
2682 rt2800_register_read(rt2x00dev, HT_FBK_CFG1, &reg);
2683 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS8FBK, 8);
2684 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS9FBK, 8);
2685 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS10FBK, 9);
2686 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS11FBK, 10);
2687 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS12FBK, 11);
2688 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS13FBK, 12);
2689 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS14FBK, 13);
2690 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS15FBK, 14);
2691 rt2800_register_write(rt2x00dev, HT_FBK_CFG1, reg);
2692
2693 rt2800_register_read(rt2x00dev, LG_FBK_CFG0, &reg);
2694 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS0FBK, 8);
2695 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS1FBK, 8);
2696 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS2FBK, 9);
2697 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS3FBK, 10);
2698 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS4FBK, 11);
2699 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS5FBK, 12);
2700 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS6FBK, 13);
2701 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS7FBK, 14);
2702 rt2800_register_write(rt2x00dev, LG_FBK_CFG0, reg);
2703
2704 rt2800_register_read(rt2x00dev, LG_FBK_CFG1, &reg);
2705 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS0FBK, 0);
2706 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS1FBK, 0);
2707 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS2FBK, 1);
2708 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS3FBK, 2);
2709 rt2800_register_write(rt2x00dev, LG_FBK_CFG1, reg);
2710
2711 /*
Helmut Schaa47ee3eb2010-09-08 20:56:04 +02002712 * Do not force the BA window size, we use the TXWI to set it
2713 */
2714 rt2800_register_read(rt2x00dev, AMPDU_BA_WINSIZE, &reg);
2715 rt2x00_set_field32(&reg, AMPDU_BA_WINSIZE_FORCE_WINSIZE_ENABLE, 0);
2716 rt2x00_set_field32(&reg, AMPDU_BA_WINSIZE_FORCE_WINSIZE, 0);
2717 rt2800_register_write(rt2x00dev, AMPDU_BA_WINSIZE, reg);
2718
2719 /*
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002720 * We must clear the error counters.
2721 * These registers are cleared on read,
2722 * so we may pass a useless variable to store the value.
2723 */
2724 rt2800_register_read(rt2x00dev, RX_STA_CNT0, &reg);
2725 rt2800_register_read(rt2x00dev, RX_STA_CNT1, &reg);
2726 rt2800_register_read(rt2x00dev, RX_STA_CNT2, &reg);
2727 rt2800_register_read(rt2x00dev, TX_STA_CNT0, &reg);
2728 rt2800_register_read(rt2x00dev, TX_STA_CNT1, &reg);
2729 rt2800_register_read(rt2x00dev, TX_STA_CNT2, &reg);
2730
Helmut Schaa9f926fb2010-07-11 12:28:23 +02002731 /*
2732 * Setup leadtime for pre tbtt interrupt to 6ms
2733 */
2734 rt2800_register_read(rt2x00dev, INT_TIMER_CFG, &reg);
2735 rt2x00_set_field32(&reg, INT_TIMER_CFG_PRE_TBTT_TIMER, 6 << 4);
2736 rt2800_register_write(rt2x00dev, INT_TIMER_CFG, reg);
2737
Helmut Schaa977206d2010-12-13 12:31:58 +01002738 /*
2739 * Set up channel statistics timer
2740 */
2741 rt2800_register_read(rt2x00dev, CH_TIME_CFG, &reg);
2742 rt2x00_set_field32(&reg, CH_TIME_CFG_EIFS_BUSY, 1);
2743 rt2x00_set_field32(&reg, CH_TIME_CFG_NAV_BUSY, 1);
2744 rt2x00_set_field32(&reg, CH_TIME_CFG_RX_BUSY, 1);
2745 rt2x00_set_field32(&reg, CH_TIME_CFG_TX_BUSY, 1);
2746 rt2x00_set_field32(&reg, CH_TIME_CFG_TMR_EN, 1);
2747 rt2800_register_write(rt2x00dev, CH_TIME_CFG, reg);
2748
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002749 return 0;
2750}
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002751
2752static int rt2800_wait_bbp_rf_ready(struct rt2x00_dev *rt2x00dev)
2753{
2754 unsigned int i;
2755 u32 reg;
2756
2757 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
2758 rt2800_register_read(rt2x00dev, MAC_STATUS_CFG, &reg);
2759 if (!rt2x00_get_field32(reg, MAC_STATUS_CFG_BBP_RF_BUSY))
2760 return 0;
2761
2762 udelay(REGISTER_BUSY_DELAY);
2763 }
2764
2765 ERROR(rt2x00dev, "BBP/RF register access failed, aborting.\n");
2766 return -EACCES;
2767}
2768
2769static int rt2800_wait_bbp_ready(struct rt2x00_dev *rt2x00dev)
2770{
2771 unsigned int i;
2772 u8 value;
2773
2774 /*
2775 * BBP was enabled after firmware was loaded,
2776 * but we need to reactivate it now.
2777 */
2778 rt2800_register_write(rt2x00dev, H2M_BBP_AGENT, 0);
2779 rt2800_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
2780 msleep(1);
2781
2782 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
2783 rt2800_bbp_read(rt2x00dev, 0, &value);
2784 if ((value != 0xff) && (value != 0x00))
2785 return 0;
2786 udelay(REGISTER_BUSY_DELAY);
2787 }
2788
2789 ERROR(rt2x00dev, "BBP register access failed, aborting.\n");
2790 return -EACCES;
2791}
2792
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02002793static int rt2800_init_bbp(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002794{
2795 unsigned int i;
2796 u16 eeprom;
2797 u8 reg_id;
2798 u8 value;
2799
2800 if (unlikely(rt2800_wait_bbp_rf_ready(rt2x00dev) ||
2801 rt2800_wait_bbp_ready(rt2x00dev)))
2802 return -EACCES;
2803
Gabor Juhosadde5882011-03-03 11:46:45 +01002804 if (rt2x00_rt(rt2x00dev, RT5390)) {
2805 rt2800_bbp_read(rt2x00dev, 4, &value);
2806 rt2x00_set_field8(&value, BBP4_MAC_IF_CTRL, 1);
2807 rt2800_bbp_write(rt2x00dev, 4, value);
2808 }
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002809
Gabor Juhosadde5882011-03-03 11:46:45 +01002810 if (rt2800_is_305x_soc(rt2x00dev) ||
2811 rt2x00_rt(rt2x00dev, RT5390))
Helmut Schaabaff8002010-04-28 09:58:59 +02002812 rt2800_bbp_write(rt2x00dev, 31, 0x08);
2813
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002814 rt2800_bbp_write(rt2x00dev, 65, 0x2c);
2815 rt2800_bbp_write(rt2x00dev, 66, 0x38);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002816
Gabor Juhosadde5882011-03-03 11:46:45 +01002817 if (rt2x00_rt(rt2x00dev, RT5390))
2818 rt2800_bbp_write(rt2x00dev, 68, 0x0b);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002819
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002820 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860C)) {
2821 rt2800_bbp_write(rt2x00dev, 69, 0x16);
2822 rt2800_bbp_write(rt2x00dev, 73, 0x12);
Gabor Juhosadde5882011-03-03 11:46:45 +01002823 } else if (rt2x00_rt(rt2x00dev, RT5390)) {
2824 rt2800_bbp_write(rt2x00dev, 69, 0x12);
2825 rt2800_bbp_write(rt2x00dev, 73, 0x13);
2826 rt2800_bbp_write(rt2x00dev, 75, 0x46);
2827 rt2800_bbp_write(rt2x00dev, 76, 0x28);
2828 rt2800_bbp_write(rt2x00dev, 77, 0x59);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002829 } else {
2830 rt2800_bbp_write(rt2x00dev, 69, 0x12);
2831 rt2800_bbp_write(rt2x00dev, 73, 0x10);
2832 }
2833
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002834 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002835
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002836 if (rt2x00_rt(rt2x00dev, RT3070) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002837 rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002838 rt2x00_rt(rt2x00dev, RT3090) ||
Gabor Juhosadde5882011-03-03 11:46:45 +01002839 rt2x00_rt(rt2x00dev, RT3390) ||
2840 rt2x00_rt(rt2x00dev, RT5390)) {
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002841 rt2800_bbp_write(rt2x00dev, 79, 0x13);
2842 rt2800_bbp_write(rt2x00dev, 80, 0x05);
2843 rt2800_bbp_write(rt2x00dev, 81, 0x33);
Helmut Schaabaff8002010-04-28 09:58:59 +02002844 } else if (rt2800_is_305x_soc(rt2x00dev)) {
2845 rt2800_bbp_write(rt2x00dev, 78, 0x0e);
2846 rt2800_bbp_write(rt2x00dev, 80, 0x08);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002847 } else {
2848 rt2800_bbp_write(rt2x00dev, 81, 0x37);
2849 }
2850
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002851 rt2800_bbp_write(rt2x00dev, 82, 0x62);
Gabor Juhosadde5882011-03-03 11:46:45 +01002852 if (rt2x00_rt(rt2x00dev, RT5390))
2853 rt2800_bbp_write(rt2x00dev, 83, 0x7a);
2854 else
2855 rt2800_bbp_write(rt2x00dev, 83, 0x6a);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002856
Gertjan van Wingerde5ed8f452010-06-03 10:51:57 +02002857 if (rt2x00_rt_rev(rt2x00dev, RT2860, REV_RT2860D))
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002858 rt2800_bbp_write(rt2x00dev, 84, 0x19);
Gabor Juhosadde5882011-03-03 11:46:45 +01002859 else if (rt2x00_rt(rt2x00dev, RT5390))
2860 rt2800_bbp_write(rt2x00dev, 84, 0x9a);
Gertjan van Wingerdea9dce142010-04-11 14:31:11 +02002861 else
2862 rt2800_bbp_write(rt2x00dev, 84, 0x99);
2863
Gabor Juhosadde5882011-03-03 11:46:45 +01002864 if (rt2x00_rt(rt2x00dev, RT5390))
2865 rt2800_bbp_write(rt2x00dev, 86, 0x38);
2866 else
2867 rt2800_bbp_write(rt2x00dev, 86, 0x00);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002868
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002869 rt2800_bbp_write(rt2x00dev, 91, 0x04);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002870
Gabor Juhosadde5882011-03-03 11:46:45 +01002871 if (rt2x00_rt(rt2x00dev, RT5390))
2872 rt2800_bbp_write(rt2x00dev, 92, 0x02);
2873 else
2874 rt2800_bbp_write(rt2x00dev, 92, 0x00);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002875
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002876 if (rt2x00_rt_rev_gte(rt2x00dev, RT3070, REV_RT3070F) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002877 rt2x00_rt_rev_gte(rt2x00dev, RT3071, REV_RT3071E) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002878 rt2x00_rt_rev_gte(rt2x00dev, RT3090, REV_RT3090E) ||
Helmut Schaabaff8002010-04-28 09:58:59 +02002879 rt2x00_rt_rev_gte(rt2x00dev, RT3390, REV_RT3390E) ||
Gabor Juhosadde5882011-03-03 11:46:45 +01002880 rt2x00_rt(rt2x00dev, RT5390) ||
Helmut Schaabaff8002010-04-28 09:58:59 +02002881 rt2800_is_305x_soc(rt2x00dev))
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02002882 rt2800_bbp_write(rt2x00dev, 103, 0xc0);
2883 else
2884 rt2800_bbp_write(rt2x00dev, 103, 0x00);
2885
Gabor Juhosadde5882011-03-03 11:46:45 +01002886 if (rt2x00_rt(rt2x00dev, RT5390))
2887 rt2800_bbp_write(rt2x00dev, 104, 0x92);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002888
Helmut Schaabaff8002010-04-28 09:58:59 +02002889 if (rt2800_is_305x_soc(rt2x00dev))
2890 rt2800_bbp_write(rt2x00dev, 105, 0x01);
Gabor Juhosadde5882011-03-03 11:46:45 +01002891 else if (rt2x00_rt(rt2x00dev, RT5390))
2892 rt2800_bbp_write(rt2x00dev, 105, 0x3c);
Helmut Schaabaff8002010-04-28 09:58:59 +02002893 else
2894 rt2800_bbp_write(rt2x00dev, 105, 0x05);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002895
Gabor Juhosadde5882011-03-03 11:46:45 +01002896 if (rt2x00_rt(rt2x00dev, RT5390))
2897 rt2800_bbp_write(rt2x00dev, 106, 0x03);
2898 else
2899 rt2800_bbp_write(rt2x00dev, 106, 0x35);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002900
Gabor Juhosadde5882011-03-03 11:46:45 +01002901 if (rt2x00_rt(rt2x00dev, RT5390))
2902 rt2800_bbp_write(rt2x00dev, 128, 0x12);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002903
Gertjan van Wingerde64522952010-04-11 14:31:14 +02002904 if (rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02002905 rt2x00_rt(rt2x00dev, RT3090) ||
Gabor Juhosadde5882011-03-03 11:46:45 +01002906 rt2x00_rt(rt2x00dev, RT3390) ||
2907 rt2x00_rt(rt2x00dev, RT5390)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002908 rt2800_bbp_read(rt2x00dev, 138, &value);
2909
RA-Jay Hung38c8a562010-12-13 12:31:27 +01002910 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &eeprom);
2911 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_TXPATH) == 1)
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002912 value |= 0x20;
RA-Jay Hung38c8a562010-12-13 12:31:27 +01002913 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RXPATH) == 1)
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02002914 value &= ~0x02;
2915
2916 rt2800_bbp_write(rt2x00dev, 138, value);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002917 }
2918
Gabor Juhosadde5882011-03-03 11:46:45 +01002919 if (rt2x00_rt(rt2x00dev, RT5390)) {
2920 int ant, div_mode;
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002921
Gabor Juhosadde5882011-03-03 11:46:45 +01002922 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &eeprom);
2923 div_mode = rt2x00_get_field16(eeprom,
2924 EEPROM_NIC_CONF1_ANT_DIVERSITY);
2925 ant = (div_mode == 3) ? 1 : 0;
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002926
Gabor Juhosadde5882011-03-03 11:46:45 +01002927 /* check if this is a Bluetooth combo card */
Gertjan van Wingerdefdbc7b02011-04-30 17:15:37 +02002928 if (test_bit(CAPABILITY_BT_COEXIST, &rt2x00dev->cap_flags)) {
Gabor Juhosadde5882011-03-03 11:46:45 +01002929 u32 reg;
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002930
Gabor Juhosadde5882011-03-03 11:46:45 +01002931 rt2800_register_read(rt2x00dev, GPIO_CTRL_CFG, &reg);
2932 rt2x00_set_field32(&reg, GPIO_CTRL_CFG_GPIOD_BIT3, 0);
2933 rt2x00_set_field32(&reg, GPIO_CTRL_CFG_GPIOD_BIT6, 0);
2934 rt2x00_set_field32(&reg, GPIO_CTRL_CFG_BIT3, 0);
2935 rt2x00_set_field32(&reg, GPIO_CTRL_CFG_BIT6, 0);
2936 if (ant == 0)
2937 rt2x00_set_field32(&reg, GPIO_CTRL_CFG_BIT3, 1);
2938 else if (ant == 1)
2939 rt2x00_set_field32(&reg, GPIO_CTRL_CFG_BIT6, 1);
2940 rt2800_register_write(rt2x00dev, GPIO_CTRL_CFG, reg);
2941 }
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002942
Gabor Juhosadde5882011-03-03 11:46:45 +01002943 rt2800_bbp_read(rt2x00dev, 152, &value);
2944 if (ant == 0)
2945 rt2x00_set_field8(&value, BBP152_RX_DEFAULT_ANT, 1);
2946 else
2947 rt2x00_set_field8(&value, BBP152_RX_DEFAULT_ANT, 0);
2948 rt2800_bbp_write(rt2x00dev, 152, value);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01002949
Gabor Juhosadde5882011-03-03 11:46:45 +01002950 /* Init frequency calibration */
2951 rt2800_bbp_write(rt2x00dev, 142, 1);
2952 rt2800_bbp_write(rt2x00dev, 143, 57);
2953 }
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002954
2955 for (i = 0; i < EEPROM_BBP_SIZE; i++) {
2956 rt2x00_eeprom_read(rt2x00dev, EEPROM_BBP_START + i, &eeprom);
2957
2958 if (eeprom != 0xffff && eeprom != 0x0000) {
2959 reg_id = rt2x00_get_field16(eeprom, EEPROM_BBP_REG_ID);
2960 value = rt2x00_get_field16(eeprom, EEPROM_BBP_VALUE);
2961 rt2800_bbp_write(rt2x00dev, reg_id, value);
2962 }
2963 }
2964
2965 return 0;
2966}
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002967
2968static u8 rt2800_init_rx_filter(struct rt2x00_dev *rt2x00dev,
2969 bool bw40, u8 rfcsr24, u8 filter_target)
2970{
2971 unsigned int i;
2972 u8 bbp;
2973 u8 rfcsr;
2974 u8 passband;
2975 u8 stopband;
2976 u8 overtuned = 0;
2977
2978 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
2979
2980 rt2800_bbp_read(rt2x00dev, 4, &bbp);
2981 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 2 * bw40);
2982 rt2800_bbp_write(rt2x00dev, 4, bbp);
2983
RA-Jay Hung80d184e2011-01-10 11:28:10 +01002984 rt2800_rfcsr_read(rt2x00dev, 31, &rfcsr);
2985 rt2x00_set_field8(&rfcsr, RFCSR31_RX_H20M, bw40);
2986 rt2800_rfcsr_write(rt2x00dev, 31, rfcsr);
2987
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01002988 rt2800_rfcsr_read(rt2x00dev, 22, &rfcsr);
2989 rt2x00_set_field8(&rfcsr, RFCSR22_BASEBAND_LOOPBACK, 1);
2990 rt2800_rfcsr_write(rt2x00dev, 22, rfcsr);
2991
2992 /*
2993 * Set power & frequency of passband test tone
2994 */
2995 rt2800_bbp_write(rt2x00dev, 24, 0);
2996
2997 for (i = 0; i < 100; i++) {
2998 rt2800_bbp_write(rt2x00dev, 25, 0x90);
2999 msleep(1);
3000
3001 rt2800_bbp_read(rt2x00dev, 55, &passband);
3002 if (passband)
3003 break;
3004 }
3005
3006 /*
3007 * Set power & frequency of stopband test tone
3008 */
3009 rt2800_bbp_write(rt2x00dev, 24, 0x06);
3010
3011 for (i = 0; i < 100; i++) {
3012 rt2800_bbp_write(rt2x00dev, 25, 0x90);
3013 msleep(1);
3014
3015 rt2800_bbp_read(rt2x00dev, 55, &stopband);
3016
3017 if ((passband - stopband) <= filter_target) {
3018 rfcsr24++;
3019 overtuned += ((passband - stopband) == filter_target);
3020 } else
3021 break;
3022
3023 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
3024 }
3025
3026 rfcsr24 -= !!overtuned;
3027
3028 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
3029 return rfcsr24;
3030}
3031
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02003032static int rt2800_init_rfcsr(struct rt2x00_dev *rt2x00dev)
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003033{
3034 u8 rfcsr;
3035 u8 bbp;
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02003036 u32 reg;
3037 u16 eeprom;
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003038
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02003039 if (!rt2x00_rt(rt2x00dev, RT3070) &&
Gertjan van Wingerde64522952010-04-11 14:31:14 +02003040 !rt2x00_rt(rt2x00dev, RT3071) &&
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02003041 !rt2x00_rt(rt2x00dev, RT3090) &&
Helmut Schaa23812382010-04-26 13:48:45 +02003042 !rt2x00_rt(rt2x00dev, RT3390) &&
Gabor Juhosadde5882011-03-03 11:46:45 +01003043 !rt2x00_rt(rt2x00dev, RT5390) &&
Helmut Schaabaff8002010-04-28 09:58:59 +02003044 !rt2800_is_305x_soc(rt2x00dev))
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003045 return 0;
3046
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003047 /*
3048 * Init RF calibration.
3049 */
Gabor Juhosadde5882011-03-03 11:46:45 +01003050 if (rt2x00_rt(rt2x00dev, RT5390)) {
3051 rt2800_rfcsr_read(rt2x00dev, 2, &rfcsr);
3052 rt2x00_set_field8(&rfcsr, RFCSR2_RESCAL_EN, 1);
3053 rt2800_rfcsr_write(rt2x00dev, 2, rfcsr);
3054 msleep(1);
3055 rt2x00_set_field8(&rfcsr, RFCSR2_RESCAL_EN, 0);
3056 rt2800_rfcsr_write(rt2x00dev, 2, rfcsr);
3057 } else {
3058 rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
3059 rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 1);
3060 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
3061 msleep(1);
3062 rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 0);
3063 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
3064 }
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003065
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02003066 if (rt2x00_rt(rt2x00dev, RT3070) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02003067 rt2x00_rt(rt2x00dev, RT3071) ||
3068 rt2x00_rt(rt2x00dev, RT3090)) {
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003069 rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
3070 rt2800_rfcsr_write(rt2x00dev, 5, 0x03);
3071 rt2800_rfcsr_write(rt2x00dev, 6, 0x02);
RA-Jay Hung80d184e2011-01-10 11:28:10 +01003072 rt2800_rfcsr_write(rt2x00dev, 7, 0x60);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003073 rt2800_rfcsr_write(rt2x00dev, 9, 0x0f);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02003074 rt2800_rfcsr_write(rt2x00dev, 10, 0x41);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003075 rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
3076 rt2800_rfcsr_write(rt2x00dev, 12, 0x7b);
3077 rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
3078 rt2800_rfcsr_write(rt2x00dev, 15, 0x58);
3079 rt2800_rfcsr_write(rt2x00dev, 16, 0xb3);
3080 rt2800_rfcsr_write(rt2x00dev, 17, 0x92);
3081 rt2800_rfcsr_write(rt2x00dev, 18, 0x2c);
3082 rt2800_rfcsr_write(rt2x00dev, 19, 0x02);
3083 rt2800_rfcsr_write(rt2x00dev, 20, 0xba);
3084 rt2800_rfcsr_write(rt2x00dev, 21, 0xdb);
3085 rt2800_rfcsr_write(rt2x00dev, 24, 0x16);
3086 rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003087 rt2800_rfcsr_write(rt2x00dev, 29, 0x1f);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02003088 } else if (rt2x00_rt(rt2x00dev, RT3390)) {
3089 rt2800_rfcsr_write(rt2x00dev, 0, 0xa0);
3090 rt2800_rfcsr_write(rt2x00dev, 1, 0xe1);
3091 rt2800_rfcsr_write(rt2x00dev, 2, 0xf1);
3092 rt2800_rfcsr_write(rt2x00dev, 3, 0x62);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003093 rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02003094 rt2800_rfcsr_write(rt2x00dev, 5, 0x8b);
3095 rt2800_rfcsr_write(rt2x00dev, 6, 0x42);
3096 rt2800_rfcsr_write(rt2x00dev, 7, 0x34);
3097 rt2800_rfcsr_write(rt2x00dev, 8, 0x00);
3098 rt2800_rfcsr_write(rt2x00dev, 9, 0xc0);
3099 rt2800_rfcsr_write(rt2x00dev, 10, 0x61);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003100 rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02003101 rt2800_rfcsr_write(rt2x00dev, 12, 0x3b);
3102 rt2800_rfcsr_write(rt2x00dev, 13, 0xe0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003103 rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02003104 rt2800_rfcsr_write(rt2x00dev, 15, 0x53);
3105 rt2800_rfcsr_write(rt2x00dev, 16, 0xe0);
3106 rt2800_rfcsr_write(rt2x00dev, 17, 0x94);
3107 rt2800_rfcsr_write(rt2x00dev, 18, 0x5c);
3108 rt2800_rfcsr_write(rt2x00dev, 19, 0x4a);
3109 rt2800_rfcsr_write(rt2x00dev, 20, 0xb2);
3110 rt2800_rfcsr_write(rt2x00dev, 21, 0xf6);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003111 rt2800_rfcsr_write(rt2x00dev, 22, 0x00);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02003112 rt2800_rfcsr_write(rt2x00dev, 23, 0x14);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003113 rt2800_rfcsr_write(rt2x00dev, 24, 0x08);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02003114 rt2800_rfcsr_write(rt2x00dev, 25, 0x3d);
3115 rt2800_rfcsr_write(rt2x00dev, 26, 0x85);
3116 rt2800_rfcsr_write(rt2x00dev, 27, 0x00);
3117 rt2800_rfcsr_write(rt2x00dev, 28, 0x41);
3118 rt2800_rfcsr_write(rt2x00dev, 29, 0x8f);
3119 rt2800_rfcsr_write(rt2x00dev, 30, 0x20);
3120 rt2800_rfcsr_write(rt2x00dev, 31, 0x0f);
Helmut Schaabaff8002010-04-28 09:58:59 +02003121 } else if (rt2800_is_305x_soc(rt2x00dev)) {
Helmut Schaa23812382010-04-26 13:48:45 +02003122 rt2800_rfcsr_write(rt2x00dev, 0, 0x50);
3123 rt2800_rfcsr_write(rt2x00dev, 1, 0x01);
3124 rt2800_rfcsr_write(rt2x00dev, 2, 0xf7);
3125 rt2800_rfcsr_write(rt2x00dev, 3, 0x75);
3126 rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
3127 rt2800_rfcsr_write(rt2x00dev, 5, 0x03);
3128 rt2800_rfcsr_write(rt2x00dev, 6, 0x02);
3129 rt2800_rfcsr_write(rt2x00dev, 7, 0x50);
3130 rt2800_rfcsr_write(rt2x00dev, 8, 0x39);
3131 rt2800_rfcsr_write(rt2x00dev, 9, 0x0f);
3132 rt2800_rfcsr_write(rt2x00dev, 10, 0x60);
3133 rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
3134 rt2800_rfcsr_write(rt2x00dev, 12, 0x75);
3135 rt2800_rfcsr_write(rt2x00dev, 13, 0x75);
3136 rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
3137 rt2800_rfcsr_write(rt2x00dev, 15, 0x58);
3138 rt2800_rfcsr_write(rt2x00dev, 16, 0xb3);
3139 rt2800_rfcsr_write(rt2x00dev, 17, 0x92);
3140 rt2800_rfcsr_write(rt2x00dev, 18, 0x2c);
3141 rt2800_rfcsr_write(rt2x00dev, 19, 0x02);
3142 rt2800_rfcsr_write(rt2x00dev, 20, 0xba);
3143 rt2800_rfcsr_write(rt2x00dev, 21, 0xdb);
3144 rt2800_rfcsr_write(rt2x00dev, 22, 0x00);
3145 rt2800_rfcsr_write(rt2x00dev, 23, 0x31);
3146 rt2800_rfcsr_write(rt2x00dev, 24, 0x08);
3147 rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
3148 rt2800_rfcsr_write(rt2x00dev, 26, 0x25);
3149 rt2800_rfcsr_write(rt2x00dev, 27, 0x23);
3150 rt2800_rfcsr_write(rt2x00dev, 28, 0x13);
3151 rt2800_rfcsr_write(rt2x00dev, 29, 0x83);
Helmut Schaabaff8002010-04-28 09:58:59 +02003152 rt2800_rfcsr_write(rt2x00dev, 30, 0x00);
3153 rt2800_rfcsr_write(rt2x00dev, 31, 0x00);
3154 return 0;
Gabor Juhosadde5882011-03-03 11:46:45 +01003155 } else if (rt2x00_rt(rt2x00dev, RT5390)) {
3156 rt2800_rfcsr_write(rt2x00dev, 1, 0x0f);
3157 rt2800_rfcsr_write(rt2x00dev, 2, 0x80);
3158 rt2800_rfcsr_write(rt2x00dev, 3, 0x88);
3159 rt2800_rfcsr_write(rt2x00dev, 5, 0x10);
3160 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
3161 rt2800_rfcsr_write(rt2x00dev, 6, 0xe0);
3162 else
3163 rt2800_rfcsr_write(rt2x00dev, 6, 0xa0);
3164 rt2800_rfcsr_write(rt2x00dev, 7, 0x00);
3165 rt2800_rfcsr_write(rt2x00dev, 10, 0x53);
3166 rt2800_rfcsr_write(rt2x00dev, 11, 0x4a);
3167 rt2800_rfcsr_write(rt2x00dev, 12, 0xc6);
3168 rt2800_rfcsr_write(rt2x00dev, 13, 0x9f);
3169 rt2800_rfcsr_write(rt2x00dev, 14, 0x00);
3170 rt2800_rfcsr_write(rt2x00dev, 15, 0x00);
3171 rt2800_rfcsr_write(rt2x00dev, 16, 0x00);
3172 rt2800_rfcsr_write(rt2x00dev, 18, 0x03);
3173 rt2800_rfcsr_write(rt2x00dev, 19, 0x00);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01003174
Gabor Juhosadde5882011-03-03 11:46:45 +01003175 rt2800_rfcsr_write(rt2x00dev, 20, 0x00);
3176 rt2800_rfcsr_write(rt2x00dev, 21, 0x00);
3177 rt2800_rfcsr_write(rt2x00dev, 22, 0x20);
3178 rt2800_rfcsr_write(rt2x00dev, 23, 0x00);
3179 rt2800_rfcsr_write(rt2x00dev, 24, 0x00);
3180 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
3181 rt2800_rfcsr_write(rt2x00dev, 25, 0x80);
3182 else
3183 rt2800_rfcsr_write(rt2x00dev, 25, 0xc0);
3184 rt2800_rfcsr_write(rt2x00dev, 26, 0x00);
3185 rt2800_rfcsr_write(rt2x00dev, 27, 0x09);
3186 rt2800_rfcsr_write(rt2x00dev, 28, 0x00);
3187 rt2800_rfcsr_write(rt2x00dev, 29, 0x10);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01003188
Gabor Juhosadde5882011-03-03 11:46:45 +01003189 rt2800_rfcsr_write(rt2x00dev, 30, 0x00);
3190 rt2800_rfcsr_write(rt2x00dev, 31, 0x80);
3191 rt2800_rfcsr_write(rt2x00dev, 32, 0x80);
3192 rt2800_rfcsr_write(rt2x00dev, 33, 0x00);
3193 rt2800_rfcsr_write(rt2x00dev, 34, 0x07);
3194 rt2800_rfcsr_write(rt2x00dev, 35, 0x12);
3195 rt2800_rfcsr_write(rt2x00dev, 36, 0x00);
3196 rt2800_rfcsr_write(rt2x00dev, 37, 0x08);
3197 rt2800_rfcsr_write(rt2x00dev, 38, 0x85);
3198 rt2800_rfcsr_write(rt2x00dev, 39, 0x1b);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01003199
Gabor Juhosadde5882011-03-03 11:46:45 +01003200 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
3201 rt2800_rfcsr_write(rt2x00dev, 40, 0x0b);
3202 else
3203 rt2800_rfcsr_write(rt2x00dev, 40, 0x4b);
3204 rt2800_rfcsr_write(rt2x00dev, 41, 0xbb);
3205 rt2800_rfcsr_write(rt2x00dev, 42, 0xd2);
3206 rt2800_rfcsr_write(rt2x00dev, 43, 0x9a);
3207 rt2800_rfcsr_write(rt2x00dev, 44, 0x0e);
3208 rt2800_rfcsr_write(rt2x00dev, 45, 0xa2);
3209 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
3210 rt2800_rfcsr_write(rt2x00dev, 46, 0x73);
3211 else
3212 rt2800_rfcsr_write(rt2x00dev, 46, 0x7b);
3213 rt2800_rfcsr_write(rt2x00dev, 47, 0x00);
3214 rt2800_rfcsr_write(rt2x00dev, 48, 0x10);
3215 rt2800_rfcsr_write(rt2x00dev, 49, 0x94);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01003216
Gabor Juhosadde5882011-03-03 11:46:45 +01003217 rt2800_rfcsr_write(rt2x00dev, 52, 0x38);
3218 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
3219 rt2800_rfcsr_write(rt2x00dev, 53, 0x00);
3220 else
3221 rt2800_rfcsr_write(rt2x00dev, 53, 0x84);
3222 rt2800_rfcsr_write(rt2x00dev, 54, 0x78);
3223 rt2800_rfcsr_write(rt2x00dev, 55, 0x44);
3224 rt2800_rfcsr_write(rt2x00dev, 56, 0x22);
3225 rt2800_rfcsr_write(rt2x00dev, 57, 0x80);
3226 rt2800_rfcsr_write(rt2x00dev, 58, 0x7f);
3227 rt2800_rfcsr_write(rt2x00dev, 59, 0x63);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01003228
Gabor Juhosadde5882011-03-03 11:46:45 +01003229 rt2800_rfcsr_write(rt2x00dev, 60, 0x45);
3230 if (rt2x00_rt_rev_gte(rt2x00dev, RT5390, REV_RT5390F))
3231 rt2800_rfcsr_write(rt2x00dev, 61, 0xd1);
3232 else
3233 rt2800_rfcsr_write(rt2x00dev, 61, 0xdd);
3234 rt2800_rfcsr_write(rt2x00dev, 62, 0x00);
3235 rt2800_rfcsr_write(rt2x00dev, 63, 0x00);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02003236 }
3237
3238 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F)) {
3239 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
3240 rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
3241 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 3);
3242 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02003243 } else if (rt2x00_rt(rt2x00dev, RT3071) ||
3244 rt2x00_rt(rt2x00dev, RT3090)) {
RA-Jay Hung80d184e2011-01-10 11:28:10 +01003245 rt2800_rfcsr_write(rt2x00dev, 31, 0x14);
3246
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02003247 rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
3248 rt2x00_set_field8(&rfcsr, RFCSR6_R2, 1);
3249 rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
3250
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02003251 rt2800_register_read(rt2x00dev, LDO_CFG0, &reg);
3252 rt2x00_set_field32(&reg, LDO_CFG0_BGSEL, 1);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02003253 if (rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
3254 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E)) {
RA-Jay Hung38c8a562010-12-13 12:31:27 +01003255 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &eeprom);
3256 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_DAC_TEST))
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02003257 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 3);
3258 else
3259 rt2x00_set_field32(&reg, LDO_CFG0_LDO_CORE_VLEVEL, 0);
3260 }
3261 rt2800_register_write(rt2x00dev, LDO_CFG0, reg);
RA-Jay Hung80d184e2011-01-10 11:28:10 +01003262
3263 rt2800_register_read(rt2x00dev, GPIO_SWITCH, &reg);
3264 rt2x00_set_field32(&reg, GPIO_SWITCH_5, 0);
3265 rt2800_register_write(rt2x00dev, GPIO_SWITCH, reg);
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02003266 } else if (rt2x00_rt(rt2x00dev, RT3390)) {
3267 rt2800_register_read(rt2x00dev, GPIO_SWITCH, &reg);
3268 rt2x00_set_field32(&reg, GPIO_SWITCH_5, 0);
3269 rt2800_register_write(rt2x00dev, GPIO_SWITCH, reg);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003270 }
3271
3272 /*
3273 * Set RX Filter calibration for 20MHz and 40MHz
3274 */
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02003275 if (rt2x00_rt(rt2x00dev, RT3070)) {
3276 rt2x00dev->calibration[0] =
3277 rt2800_init_rx_filter(rt2x00dev, false, 0x07, 0x16);
3278 rt2x00dev->calibration[1] =
3279 rt2800_init_rx_filter(rt2x00dev, true, 0x27, 0x19);
Gertjan van Wingerde64522952010-04-11 14:31:14 +02003280 } else if (rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02003281 rt2x00_rt(rt2x00dev, RT3090) ||
3282 rt2x00_rt(rt2x00dev, RT3390)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02003283 rt2x00dev->calibration[0] =
3284 rt2800_init_rx_filter(rt2x00dev, false, 0x07, 0x13);
3285 rt2x00dev->calibration[1] =
3286 rt2800_init_rx_filter(rt2x00dev, true, 0x27, 0x15);
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02003287 }
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003288
Gabor Juhosadde5882011-03-03 11:46:45 +01003289 if (!rt2x00_rt(rt2x00dev, RT5390)) {
3290 /*
3291 * Set back to initial state
3292 */
3293 rt2800_bbp_write(rt2x00dev, 24, 0);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003294
Gabor Juhosadde5882011-03-03 11:46:45 +01003295 rt2800_rfcsr_read(rt2x00dev, 22, &rfcsr);
3296 rt2x00_set_field8(&rfcsr, RFCSR22_BASEBAND_LOOPBACK, 0);
3297 rt2800_rfcsr_write(rt2x00dev, 22, rfcsr);
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003298
Gabor Juhosadde5882011-03-03 11:46:45 +01003299 /*
3300 * Set BBP back to BW20
3301 */
3302 rt2800_bbp_read(rt2x00dev, 4, &bbp);
3303 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 0);
3304 rt2800_bbp_write(rt2x00dev, 4, bbp);
3305 }
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003306
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02003307 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F) ||
Gertjan van Wingerde64522952010-04-11 14:31:14 +02003308 rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02003309 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E) ||
3310 rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E))
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02003311 rt2800_rfcsr_write(rt2x00dev, 27, 0x03);
3312
3313 rt2800_register_read(rt2x00dev, OPT_14_CSR, &reg);
3314 rt2x00_set_field32(&reg, OPT_14_CSR_BIT0, 1);
3315 rt2800_register_write(rt2x00dev, OPT_14_CSR, reg);
3316
Gabor Juhosadde5882011-03-03 11:46:45 +01003317 if (!rt2x00_rt(rt2x00dev, RT5390)) {
3318 rt2800_rfcsr_read(rt2x00dev, 17, &rfcsr);
3319 rt2x00_set_field8(&rfcsr, RFCSR17_TX_LO1_EN, 0);
3320 if (rt2x00_rt(rt2x00dev, RT3070) ||
3321 rt2x00_rt_rev_lt(rt2x00dev, RT3071, REV_RT3071E) ||
3322 rt2x00_rt_rev_lt(rt2x00dev, RT3090, REV_RT3090E) ||
3323 rt2x00_rt_rev_lt(rt2x00dev, RT3390, REV_RT3390E)) {
Ivo van Doorn7dab73b2011-04-18 15:27:06 +02003324 if (!test_bit(CAPABILITY_EXTERNAL_LNA_BG,
3325 &rt2x00dev->cap_flags))
Gabor Juhosadde5882011-03-03 11:46:45 +01003326 rt2x00_set_field8(&rfcsr, RFCSR17_R, 1);
3327 }
3328 rt2x00_eeprom_read(rt2x00dev, EEPROM_TXMIXER_GAIN_BG, &eeprom);
3329 if (rt2x00_get_field16(eeprom, EEPROM_TXMIXER_GAIN_BG_VAL) >= 1)
3330 rt2x00_set_field8(&rfcsr, RFCSR17_TXMIXER_GAIN,
3331 rt2x00_get_field16(eeprom,
3332 EEPROM_TXMIXER_GAIN_BG_VAL));
3333 rt2800_rfcsr_write(rt2x00dev, 17, rfcsr);
3334 }
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02003335
Gertjan van Wingerde64522952010-04-11 14:31:14 +02003336 if (rt2x00_rt(rt2x00dev, RT3090)) {
3337 rt2800_bbp_read(rt2x00dev, 138, &bbp);
3338
RA-Jay Hung80d184e2011-01-10 11:28:10 +01003339 /* Turn off unused DAC1 and ADC1 to reduce power consumption */
RA-Jay Hung38c8a562010-12-13 12:31:27 +01003340 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &eeprom);
3341 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RXPATH) == 1)
Gertjan van Wingerde64522952010-04-11 14:31:14 +02003342 rt2x00_set_field8(&bbp, BBP138_RX_ADC1, 0);
RA-Jay Hung38c8a562010-12-13 12:31:27 +01003343 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_TXPATH) == 1)
Gertjan van Wingerde64522952010-04-11 14:31:14 +02003344 rt2x00_set_field8(&bbp, BBP138_TX_DAC1, 1);
3345
3346 rt2800_bbp_write(rt2x00dev, 138, bbp);
3347 }
3348
3349 if (rt2x00_rt(rt2x00dev, RT3071) ||
Gertjan van Wingerdecc78e902010-04-11 14:31:15 +02003350 rt2x00_rt(rt2x00dev, RT3090) ||
3351 rt2x00_rt(rt2x00dev, RT3390)) {
Gertjan van Wingerded5385bf2010-04-11 14:31:13 +02003352 rt2800_rfcsr_read(rt2x00dev, 1, &rfcsr);
3353 rt2x00_set_field8(&rfcsr, RFCSR1_RF_BLOCK_EN, 1);
3354 rt2x00_set_field8(&rfcsr, RFCSR1_RX0_PD, 0);
3355 rt2x00_set_field8(&rfcsr, RFCSR1_TX0_PD, 0);
3356 rt2x00_set_field8(&rfcsr, RFCSR1_RX1_PD, 1);
3357 rt2x00_set_field8(&rfcsr, RFCSR1_TX1_PD, 1);
3358 rt2800_rfcsr_write(rt2x00dev, 1, rfcsr);
3359
3360 rt2800_rfcsr_read(rt2x00dev, 15, &rfcsr);
3361 rt2x00_set_field8(&rfcsr, RFCSR15_TX_LO2_EN, 0);
3362 rt2800_rfcsr_write(rt2x00dev, 15, rfcsr);
3363
3364 rt2800_rfcsr_read(rt2x00dev, 20, &rfcsr);
3365 rt2x00_set_field8(&rfcsr, RFCSR20_RX_LO1_EN, 0);
3366 rt2800_rfcsr_write(rt2x00dev, 20, rfcsr);
3367
3368 rt2800_rfcsr_read(rt2x00dev, 21, &rfcsr);
3369 rt2x00_set_field8(&rfcsr, RFCSR21_RX_LO2_EN, 0);
3370 rt2800_rfcsr_write(rt2x00dev, 21, rfcsr);
3371 }
3372
RA-Jay Hung80d184e2011-01-10 11:28:10 +01003373 if (rt2x00_rt(rt2x00dev, RT3070)) {
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02003374 rt2800_rfcsr_read(rt2x00dev, 27, &rfcsr);
RA-Jay Hung80d184e2011-01-10 11:28:10 +01003375 if (rt2x00_rt_rev_lt(rt2x00dev, RT3070, REV_RT3070F))
Gertjan van Wingerde8cdd15e2010-04-11 14:31:12 +02003376 rt2x00_set_field8(&rfcsr, RFCSR27_R1, 3);
3377 else
3378 rt2x00_set_field8(&rfcsr, RFCSR27_R1, 0);
3379 rt2x00_set_field8(&rfcsr, RFCSR27_R2, 0);
3380 rt2x00_set_field8(&rfcsr, RFCSR27_R3, 0);
3381 rt2x00_set_field8(&rfcsr, RFCSR27_R4, 0);
3382 rt2800_rfcsr_write(rt2x00dev, 27, rfcsr);
3383 }
3384
Gabor Juhosadde5882011-03-03 11:46:45 +01003385 if (rt2x00_rt(rt2x00dev, RT5390)) {
3386 rt2800_rfcsr_read(rt2x00dev, 38, &rfcsr);
3387 rt2x00_set_field8(&rfcsr, RFCSR38_RX_LO1_EN, 0);
3388 rt2800_rfcsr_write(rt2x00dev, 38, rfcsr);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01003389
Gabor Juhosadde5882011-03-03 11:46:45 +01003390 rt2800_rfcsr_read(rt2x00dev, 39, &rfcsr);
3391 rt2x00_set_field8(&rfcsr, RFCSR39_RX_LO2_EN, 0);
3392 rt2800_rfcsr_write(rt2x00dev, 39, rfcsr);
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01003393
Gabor Juhosadde5882011-03-03 11:46:45 +01003394 rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
3395 rt2x00_set_field8(&rfcsr, RFCSR30_RX_VCM, 2);
3396 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
3397 }
RA-Shiang Tu60687ba2011-02-20 13:57:46 +01003398
Bartlomiej Zolnierkiewiczfcf51542009-11-04 18:36:57 +01003399 return 0;
3400}
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02003401
3402int rt2800_enable_radio(struct rt2x00_dev *rt2x00dev)
3403{
3404 u32 reg;
3405 u16 word;
3406
3407 /*
3408 * Initialize all registers.
3409 */
3410 if (unlikely(rt2800_wait_wpdma_ready(rt2x00dev) ||
3411 rt2800_init_registers(rt2x00dev) ||
3412 rt2800_init_bbp(rt2x00dev) ||
3413 rt2800_init_rfcsr(rt2x00dev)))
3414 return -EIO;
3415
3416 /*
3417 * Send signal to firmware during boot time.
3418 */
3419 rt2800_mcu_request(rt2x00dev, MCU_BOOT_SIGNAL, 0, 0, 0);
3420
3421 if (rt2x00_is_usb(rt2x00dev) &&
3422 (rt2x00_rt(rt2x00dev, RT3070) ||
3423 rt2x00_rt(rt2x00dev, RT3071) ||
3424 rt2x00_rt(rt2x00dev, RT3572))) {
3425 udelay(200);
3426 rt2800_mcu_request(rt2x00dev, MCU_CURRENT, 0, 0, 0);
3427 udelay(10);
3428 }
3429
3430 /*
3431 * Enable RX.
3432 */
3433 rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
3434 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 1);
3435 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 0);
3436 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
3437
3438 udelay(50);
3439
3440 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
3441 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 1);
3442 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 1);
3443 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_WP_DMA_BURST_SIZE, 2);
3444 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
3445 rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
3446
3447 rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
3448 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 1);
3449 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 1);
3450 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
3451
3452 /*
3453 * Initialize LED control
3454 */
RA-Jay Hung38c8a562010-12-13 12:31:27 +01003455 rt2x00_eeprom_read(rt2x00dev, EEPROM_LED_AG_CONF, &word);
3456 rt2800_mcu_request(rt2x00dev, MCU_LED_AG_CONF, 0xff,
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02003457 word & 0xff, (word >> 8) & 0xff);
3458
RA-Jay Hung38c8a562010-12-13 12:31:27 +01003459 rt2x00_eeprom_read(rt2x00dev, EEPROM_LED_ACT_CONF, &word);
3460 rt2800_mcu_request(rt2x00dev, MCU_LED_ACT_CONF, 0xff,
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02003461 word & 0xff, (word >> 8) & 0xff);
3462
RA-Jay Hung38c8a562010-12-13 12:31:27 +01003463 rt2x00_eeprom_read(rt2x00dev, EEPROM_LED_POLARITY, &word);
3464 rt2800_mcu_request(rt2x00dev, MCU_LED_LED_POLARITY, 0xff,
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02003465 word & 0xff, (word >> 8) & 0xff);
3466
3467 return 0;
3468}
3469EXPORT_SYMBOL_GPL(rt2800_enable_radio);
3470
3471void rt2800_disable_radio(struct rt2x00_dev *rt2x00dev)
3472{
3473 u32 reg;
3474
3475 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
3476 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02003477 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02003478 rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
3479
3480 /* Wait for DMA, ignore error */
3481 rt2800_wait_wpdma_ready(rt2x00dev);
3482
3483 rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
3484 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 0);
3485 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 0);
3486 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
Ivo van Doornb9a07ae2010-08-23 19:55:22 +02003487}
3488EXPORT_SYMBOL_GPL(rt2800_disable_radio);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01003489
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01003490int rt2800_efuse_detect(struct rt2x00_dev *rt2x00dev)
3491{
3492 u32 reg;
3493
3494 rt2800_register_read(rt2x00dev, EFUSE_CTRL, &reg);
3495
3496 return rt2x00_get_field32(reg, EFUSE_CTRL_PRESENT);
3497}
3498EXPORT_SYMBOL_GPL(rt2800_efuse_detect);
3499
3500static void rt2800_efuse_read(struct rt2x00_dev *rt2x00dev, unsigned int i)
3501{
3502 u32 reg;
3503
Gertjan van Wingerde31a4cf12009-11-14 20:20:36 +01003504 mutex_lock(&rt2x00dev->csr_mutex);
3505
3506 rt2800_register_read_lock(rt2x00dev, EFUSE_CTRL, &reg);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01003507 rt2x00_set_field32(&reg, EFUSE_CTRL_ADDRESS_IN, i);
3508 rt2x00_set_field32(&reg, EFUSE_CTRL_MODE, 0);
3509 rt2x00_set_field32(&reg, EFUSE_CTRL_KICK, 1);
Gertjan van Wingerde31a4cf12009-11-14 20:20:36 +01003510 rt2800_register_write_lock(rt2x00dev, EFUSE_CTRL, reg);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01003511
3512 /* Wait until the EEPROM has been loaded */
3513 rt2800_regbusy_read(rt2x00dev, EFUSE_CTRL, EFUSE_CTRL_KICK, &reg);
3514
3515 /* Apparently the data is read from end to start */
Gertjan van Wingerde31a4cf12009-11-14 20:20:36 +01003516 rt2800_register_read_lock(rt2x00dev, EFUSE_DATA3,
3517 (u32 *)&rt2x00dev->eeprom[i]);
3518 rt2800_register_read_lock(rt2x00dev, EFUSE_DATA2,
3519 (u32 *)&rt2x00dev->eeprom[i + 2]);
3520 rt2800_register_read_lock(rt2x00dev, EFUSE_DATA1,
3521 (u32 *)&rt2x00dev->eeprom[i + 4]);
3522 rt2800_register_read_lock(rt2x00dev, EFUSE_DATA0,
3523 (u32 *)&rt2x00dev->eeprom[i + 6]);
3524
3525 mutex_unlock(&rt2x00dev->csr_mutex);
Bartlomiej Zolnierkiewicz30e84032009-11-08 14:39:48 +01003526}
3527
3528void rt2800_read_eeprom_efuse(struct rt2x00_dev *rt2x00dev)
3529{
3530 unsigned int i;
3531
3532 for (i = 0; i < EEPROM_SIZE / sizeof(u16); i += 8)
3533 rt2800_efuse_read(rt2x00dev, i);
3534}
3535EXPORT_SYMBOL_GPL(rt2800_read_eeprom_efuse);
3536
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01003537int rt2800_validate_eeprom(struct rt2x00_dev *rt2x00dev)
3538{
3539 u16 word;
3540 u8 *mac;
3541 u8 default_lna_gain;
3542
3543 /*
3544 * Start validation of the data that has been read.
3545 */
3546 mac = rt2x00_eeprom_addr(rt2x00dev, EEPROM_MAC_ADDR_0);
3547 if (!is_valid_ether_addr(mac)) {
3548 random_ether_addr(mac);
3549 EEPROM(rt2x00dev, "MAC: %pM\n", mac);
3550 }
3551
RA-Jay Hung38c8a562010-12-13 12:31:27 +01003552 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01003553 if (word == 0xffff) {
RA-Jay Hung38c8a562010-12-13 12:31:27 +01003554 rt2x00_set_field16(&word, EEPROM_NIC_CONF0_RXPATH, 2);
3555 rt2x00_set_field16(&word, EEPROM_NIC_CONF0_TXPATH, 1);
3556 rt2x00_set_field16(&word, EEPROM_NIC_CONF0_RF_TYPE, RF2820);
3557 rt2x00_eeprom_write(rt2x00dev, EEPROM_NIC_CONF0, word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01003558 EEPROM(rt2x00dev, "Antenna: 0x%04x\n", word);
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01003559 } else if (rt2x00_rt(rt2x00dev, RT2860) ||
Gertjan van Wingerdee148b4c2010-04-11 14:31:09 +02003560 rt2x00_rt(rt2x00dev, RT2872)) {
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01003561 /*
3562 * There is a max of 2 RX streams for RT28x0 series
3563 */
RA-Jay Hung38c8a562010-12-13 12:31:27 +01003564 if (rt2x00_get_field16(word, EEPROM_NIC_CONF0_RXPATH) > 2)
3565 rt2x00_set_field16(&word, EEPROM_NIC_CONF0_RXPATH, 2);
3566 rt2x00_eeprom_write(rt2x00dev, EEPROM_NIC_CONF0, word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01003567 }
3568
RA-Jay Hung38c8a562010-12-13 12:31:27 +01003569 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01003570 if (word == 0xffff) {
RA-Jay Hung38c8a562010-12-13 12:31:27 +01003571 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_HW_RADIO, 0);
3572 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_EXTERNAL_TX_ALC, 0);
3573 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_EXTERNAL_LNA_2G, 0);
3574 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_EXTERNAL_LNA_5G, 0);
3575 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_CARDBUS_ACCEL, 0);
3576 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BW40M_SB_2G, 0);
3577 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BW40M_SB_5G, 0);
3578 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_WPS_PBC, 0);
3579 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BW40M_2G, 0);
3580 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BW40M_5G, 0);
3581 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BROADBAND_EXT_LNA, 0);
3582 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_ANT_DIVERSITY, 0);
3583 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_INTERNAL_TX_ALC, 0);
3584 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_BT_COEXIST, 0);
3585 rt2x00_set_field16(&word, EEPROM_NIC_CONF1_DAC_TEST, 0);
3586 rt2x00_eeprom_write(rt2x00dev, EEPROM_NIC_CONF1, word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01003587 EEPROM(rt2x00dev, "NIC: 0x%04x\n", word);
3588 }
3589
3590 rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &word);
3591 if ((word & 0x00ff) == 0x00ff) {
3592 rt2x00_set_field16(&word, EEPROM_FREQ_OFFSET, 0);
Gertjan van Wingerdeec2d1792010-06-29 21:44:50 +02003593 rt2x00_eeprom_write(rt2x00dev, EEPROM_FREQ, word);
3594 EEPROM(rt2x00dev, "Freq: 0x%04x\n", word);
3595 }
3596 if ((word & 0xff00) == 0xff00) {
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01003597 rt2x00_set_field16(&word, EEPROM_FREQ_LED_MODE,
3598 LED_MODE_TXRX_ACTIVITY);
3599 rt2x00_set_field16(&word, EEPROM_FREQ_LED_POLARITY, 0);
3600 rt2x00_eeprom_write(rt2x00dev, EEPROM_FREQ, word);
RA-Jay Hung38c8a562010-12-13 12:31:27 +01003601 rt2x00_eeprom_write(rt2x00dev, EEPROM_LED_AG_CONF, 0x5555);
3602 rt2x00_eeprom_write(rt2x00dev, EEPROM_LED_ACT_CONF, 0x2221);
3603 rt2x00_eeprom_write(rt2x00dev, EEPROM_LED_POLARITY, 0xa9f8);
Gertjan van Wingerdeec2d1792010-06-29 21:44:50 +02003604 EEPROM(rt2x00dev, "Led Mode: 0x%04x\n", word);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01003605 }
3606
3607 /*
3608 * During the LNA validation we are going to use
3609 * lna0 as correct value. Note that EEPROM_LNA
3610 * is never validated.
3611 */
3612 rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &word);
3613 default_lna_gain = rt2x00_get_field16(word, EEPROM_LNA_A0);
3614
3615 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG, &word);
3616 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG_OFFSET0)) > 10)
3617 rt2x00_set_field16(&word, EEPROM_RSSI_BG_OFFSET0, 0);
3618 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG_OFFSET1)) > 10)
3619 rt2x00_set_field16(&word, EEPROM_RSSI_BG_OFFSET1, 0);
3620 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_BG, word);
3621
3622 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &word);
3623 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG2_OFFSET2)) > 10)
3624 rt2x00_set_field16(&word, EEPROM_RSSI_BG2_OFFSET2, 0);
3625 if (rt2x00_get_field16(word, EEPROM_RSSI_BG2_LNA_A1) == 0x00 ||
3626 rt2x00_get_field16(word, EEPROM_RSSI_BG2_LNA_A1) == 0xff)
3627 rt2x00_set_field16(&word, EEPROM_RSSI_BG2_LNA_A1,
3628 default_lna_gain);
3629 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_BG2, word);
3630
3631 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A, &word);
3632 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A_OFFSET0)) > 10)
3633 rt2x00_set_field16(&word, EEPROM_RSSI_A_OFFSET0, 0);
3634 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A_OFFSET1)) > 10)
3635 rt2x00_set_field16(&word, EEPROM_RSSI_A_OFFSET1, 0);
3636 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_A, word);
3637
3638 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &word);
3639 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A2_OFFSET2)) > 10)
3640 rt2x00_set_field16(&word, EEPROM_RSSI_A2_OFFSET2, 0);
3641 if (rt2x00_get_field16(word, EEPROM_RSSI_A2_LNA_A2) == 0x00 ||
3642 rt2x00_get_field16(word, EEPROM_RSSI_A2_LNA_A2) == 0xff)
3643 rt2x00_set_field16(&word, EEPROM_RSSI_A2_LNA_A2,
3644 default_lna_gain);
3645 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_A2, word);
3646
3647 return 0;
3648}
3649EXPORT_SYMBOL_GPL(rt2800_validate_eeprom);
3650
3651int rt2800_init_eeprom(struct rt2x00_dev *rt2x00dev)
3652{
3653 u32 reg;
3654 u16 value;
3655 u16 eeprom;
3656
3657 /*
3658 * Read EEPROM word for configuration.
3659 */
RA-Jay Hung38c8a562010-12-13 12:31:27 +01003660 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &eeprom);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01003661
3662 /*
Gabor Juhosadde5882011-03-03 11:46:45 +01003663 * Identify RF chipset by EEPROM value
3664 * RT28xx/RT30xx: defined in "EEPROM_NIC_CONF0_RF_TYPE" field
3665 * RT53xx: defined in "EEPROM_CHIP_ID" field
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01003666 */
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01003667 rt2800_register_read(rt2x00dev, MAC_CSR0, &reg);
Gabor Juhosadde5882011-03-03 11:46:45 +01003668 if (rt2x00_get_field32(reg, MAC_CSR0_CHIPSET) == RT5390)
3669 rt2x00_eeprom_read(rt2x00dev, EEPROM_CHIP_ID, &value);
3670 else
3671 value = rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RF_TYPE);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01003672
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01003673 rt2x00_set_chip(rt2x00dev, rt2x00_get_field32(reg, MAC_CSR0_CHIPSET),
3674 value, rt2x00_get_field32(reg, MAC_CSR0_REVISION));
Gertjan van Wingerde714fa662010-02-13 20:55:48 +01003675
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01003676 if (!rt2x00_rt(rt2x00dev, RT2860) &&
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01003677 !rt2x00_rt(rt2x00dev, RT2872) &&
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01003678 !rt2x00_rt(rt2x00dev, RT2883) &&
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01003679 !rt2x00_rt(rt2x00dev, RT3070) &&
3680 !rt2x00_rt(rt2x00dev, RT3071) &&
3681 !rt2x00_rt(rt2x00dev, RT3090) &&
3682 !rt2x00_rt(rt2x00dev, RT3390) &&
Gabor Juhosadde5882011-03-03 11:46:45 +01003683 !rt2x00_rt(rt2x00dev, RT3572) &&
3684 !rt2x00_rt(rt2x00dev, RT5390)) {
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +01003685 ERROR(rt2x00dev, "Invalid RT chipset detected.\n");
3686 return -ENODEV;
3687 }
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01003688
Gertjan van Wingerde5122d892009-12-23 00:03:25 +01003689 if (!rt2x00_rf(rt2x00dev, RF2820) &&
3690 !rt2x00_rf(rt2x00dev, RF2850) &&
3691 !rt2x00_rf(rt2x00dev, RF2720) &&
3692 !rt2x00_rf(rt2x00dev, RF2750) &&
3693 !rt2x00_rf(rt2x00dev, RF3020) &&
3694 !rt2x00_rf(rt2x00dev, RF2020) &&
3695 !rt2x00_rf(rt2x00dev, RF3021) &&
Gertjan van Wingerde6c0fe262009-12-30 11:36:31 +01003696 !rt2x00_rf(rt2x00dev, RF3022) &&
Gertjan van Wingerdef93bc9b2010-11-13 19:09:50 +01003697 !rt2x00_rf(rt2x00dev, RF3052) &&
Gabor Juhosadde5882011-03-03 11:46:45 +01003698 !rt2x00_rf(rt2x00dev, RF3320) &&
Gertjan van Wingerdeaca355b2011-05-04 21:41:36 +02003699 !rt2x00_rf(rt2x00dev, RF5370) &&
Gabor Juhosadde5882011-03-03 11:46:45 +01003700 !rt2x00_rf(rt2x00dev, RF5390)) {
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01003701 ERROR(rt2x00dev, "Invalid RF chipset detected.\n");
3702 return -ENODEV;
3703 }
3704
3705 /*
3706 * Identify default antenna configuration.
3707 */
RA-Jay Hungd96aa642011-02-20 13:54:52 +01003708 rt2x00dev->default_ant.tx_chain_num =
RA-Jay Hung38c8a562010-12-13 12:31:27 +01003709 rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_TXPATH);
RA-Jay Hungd96aa642011-02-20 13:54:52 +01003710 rt2x00dev->default_ant.rx_chain_num =
RA-Jay Hung38c8a562010-12-13 12:31:27 +01003711 rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RXPATH);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01003712
RA-Jay Hungd96aa642011-02-20 13:54:52 +01003713 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF1, &eeprom);
3714
3715 if (rt2x00_rt(rt2x00dev, RT3070) ||
3716 rt2x00_rt(rt2x00dev, RT3090) ||
3717 rt2x00_rt(rt2x00dev, RT3390)) {
3718 value = rt2x00_get_field16(eeprom,
3719 EEPROM_NIC_CONF1_ANT_DIVERSITY);
3720 switch (value) {
3721 case 0:
3722 case 1:
3723 case 2:
3724 rt2x00dev->default_ant.tx = ANTENNA_A;
3725 rt2x00dev->default_ant.rx = ANTENNA_A;
3726 break;
3727 case 3:
3728 rt2x00dev->default_ant.tx = ANTENNA_A;
3729 rt2x00dev->default_ant.rx = ANTENNA_B;
3730 break;
3731 }
3732 } else {
3733 rt2x00dev->default_ant.tx = ANTENNA_A;
3734 rt2x00dev->default_ant.rx = ANTENNA_A;
3735 }
3736
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01003737 /*
Gertjan van Wingerde9328fda2011-04-30 17:15:13 +02003738 * Determine external LNA informations.
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01003739 */
RA-Jay Hung38c8a562010-12-13 12:31:27 +01003740 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_EXTERNAL_LNA_5G))
Ivo van Doorn7dab73b2011-04-18 15:27:06 +02003741 __set_bit(CAPABILITY_EXTERNAL_LNA_A, &rt2x00dev->cap_flags);
RA-Jay Hung38c8a562010-12-13 12:31:27 +01003742 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_EXTERNAL_LNA_2G))
Ivo van Doorn7dab73b2011-04-18 15:27:06 +02003743 __set_bit(CAPABILITY_EXTERNAL_LNA_BG, &rt2x00dev->cap_flags);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01003744
3745 /*
3746 * Detect if this device has an hardware controlled radio.
3747 */
RA-Jay Hung38c8a562010-12-13 12:31:27 +01003748 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_HW_RADIO))
Ivo van Doorn7dab73b2011-04-18 15:27:06 +02003749 __set_bit(CAPABILITY_HW_BUTTON, &rt2x00dev->cap_flags);
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01003750
3751 /*
Gertjan van Wingerdefdbc7b02011-04-30 17:15:37 +02003752 * Detect if this device has Bluetooth co-existence.
3753 */
3754 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF1_BT_COEXIST))
3755 __set_bit(CAPABILITY_BT_COEXIST, &rt2x00dev->cap_flags);
3756
3757 /*
Gertjan van Wingerde9328fda2011-04-30 17:15:13 +02003758 * Read frequency offset and RF programming sequence.
3759 */
3760 rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &eeprom);
3761 rt2x00dev->freq_offset = rt2x00_get_field16(eeprom, EEPROM_FREQ_OFFSET);
3762
3763 /*
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01003764 * Store led settings, for correct led behaviour.
3765 */
3766#ifdef CONFIG_RT2X00_LIB_LEDS
3767 rt2800_init_led(rt2x00dev, &rt2x00dev->led_radio, LED_TYPE_RADIO);
3768 rt2800_init_led(rt2x00dev, &rt2x00dev->led_assoc, LED_TYPE_ASSOC);
3769 rt2800_init_led(rt2x00dev, &rt2x00dev->led_qual, LED_TYPE_QUALITY);
3770
Gertjan van Wingerde9328fda2011-04-30 17:15:13 +02003771 rt2x00dev->led_mcu_reg = eeprom;
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01003772#endif /* CONFIG_RT2X00_LIB_LEDS */
3773
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003774 /*
3775 * Check if support EIRP tx power limit feature.
3776 */
3777 rt2x00_eeprom_read(rt2x00dev, EEPROM_EIRP_MAX_TX_POWER, &eeprom);
3778
3779 if (rt2x00_get_field16(eeprom, EEPROM_EIRP_MAX_TX_POWER_2GHZ) <
3780 EIRP_MAX_TX_POWER_LIMIT)
Ivo van Doorn7dab73b2011-04-18 15:27:06 +02003781 __set_bit(CAPABILITY_POWER_LIMIT, &rt2x00dev->cap_flags);
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01003782
Bartlomiej Zolnierkiewicz38bd7b82009-11-08 14:39:01 +01003783 return 0;
3784}
3785EXPORT_SYMBOL_GPL(rt2800_init_eeprom);
3786
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01003787/*
Ivo van Doorn55f93212010-05-06 14:45:46 +02003788 * RF value list for rt28xx
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01003789 * Supports: 2.4 GHz (all) & 5.2 GHz (RF2850 & RF2750)
3790 */
3791static const struct rf_channel rf_vals[] = {
3792 { 1, 0x18402ecc, 0x184c0786, 0x1816b455, 0x1800510b },
3793 { 2, 0x18402ecc, 0x184c0786, 0x18168a55, 0x1800519f },
3794 { 3, 0x18402ecc, 0x184c078a, 0x18168a55, 0x1800518b },
3795 { 4, 0x18402ecc, 0x184c078a, 0x18168a55, 0x1800519f },
3796 { 5, 0x18402ecc, 0x184c078e, 0x18168a55, 0x1800518b },
3797 { 6, 0x18402ecc, 0x184c078e, 0x18168a55, 0x1800519f },
3798 { 7, 0x18402ecc, 0x184c0792, 0x18168a55, 0x1800518b },
3799 { 8, 0x18402ecc, 0x184c0792, 0x18168a55, 0x1800519f },
3800 { 9, 0x18402ecc, 0x184c0796, 0x18168a55, 0x1800518b },
3801 { 10, 0x18402ecc, 0x184c0796, 0x18168a55, 0x1800519f },
3802 { 11, 0x18402ecc, 0x184c079a, 0x18168a55, 0x1800518b },
3803 { 12, 0x18402ecc, 0x184c079a, 0x18168a55, 0x1800519f },
3804 { 13, 0x18402ecc, 0x184c079e, 0x18168a55, 0x1800518b },
3805 { 14, 0x18402ecc, 0x184c07a2, 0x18168a55, 0x18005193 },
3806
3807 /* 802.11 UNI / HyperLan 2 */
3808 { 36, 0x18402ecc, 0x184c099a, 0x18158a55, 0x180ed1a3 },
3809 { 38, 0x18402ecc, 0x184c099e, 0x18158a55, 0x180ed193 },
3810 { 40, 0x18402ec8, 0x184c0682, 0x18158a55, 0x180ed183 },
3811 { 44, 0x18402ec8, 0x184c0682, 0x18158a55, 0x180ed1a3 },
3812 { 46, 0x18402ec8, 0x184c0686, 0x18158a55, 0x180ed18b },
3813 { 48, 0x18402ec8, 0x184c0686, 0x18158a55, 0x180ed19b },
3814 { 52, 0x18402ec8, 0x184c068a, 0x18158a55, 0x180ed193 },
3815 { 54, 0x18402ec8, 0x184c068a, 0x18158a55, 0x180ed1a3 },
3816 { 56, 0x18402ec8, 0x184c068e, 0x18158a55, 0x180ed18b },
3817 { 60, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed183 },
3818 { 62, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed193 },
3819 { 64, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed1a3 },
3820
3821 /* 802.11 HyperLan 2 */
3822 { 100, 0x18402ec8, 0x184c06b2, 0x18178a55, 0x180ed783 },
3823 { 102, 0x18402ec8, 0x184c06b2, 0x18578a55, 0x180ed793 },
3824 { 104, 0x18402ec8, 0x185c06b2, 0x18578a55, 0x180ed1a3 },
3825 { 108, 0x18402ecc, 0x185c0a32, 0x18578a55, 0x180ed193 },
3826 { 110, 0x18402ecc, 0x184c0a36, 0x18178a55, 0x180ed183 },
3827 { 112, 0x18402ecc, 0x184c0a36, 0x18178a55, 0x180ed19b },
3828 { 116, 0x18402ecc, 0x184c0a3a, 0x18178a55, 0x180ed1a3 },
3829 { 118, 0x18402ecc, 0x184c0a3e, 0x18178a55, 0x180ed193 },
3830 { 120, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed183 },
3831 { 124, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed193 },
3832 { 126, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed15b },
3833 { 128, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed1a3 },
3834 { 132, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed18b },
3835 { 134, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed193 },
3836 { 136, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed19b },
3837 { 140, 0x18402ec4, 0x184c038a, 0x18178a55, 0x180ed183 },
3838
3839 /* 802.11 UNII */
3840 { 149, 0x18402ec4, 0x184c038a, 0x18178a55, 0x180ed1a7 },
3841 { 151, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed187 },
3842 { 153, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed18f },
3843 { 157, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed19f },
3844 { 159, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed1a7 },
3845 { 161, 0x18402ec4, 0x184c0392, 0x18178a55, 0x180ed187 },
3846 { 165, 0x18402ec4, 0x184c0392, 0x18178a55, 0x180ed197 },
3847 { 167, 0x18402ec4, 0x184c03d2, 0x18179855, 0x1815531f },
3848 { 169, 0x18402ec4, 0x184c03d2, 0x18179855, 0x18155327 },
3849 { 171, 0x18402ec4, 0x184c03d6, 0x18179855, 0x18155307 },
3850 { 173, 0x18402ec4, 0x184c03d6, 0x18179855, 0x1815530f },
3851
3852 /* 802.11 Japan */
3853 { 184, 0x15002ccc, 0x1500491e, 0x1509be55, 0x150c0a0b },
3854 { 188, 0x15002ccc, 0x15004922, 0x1509be55, 0x150c0a13 },
3855 { 192, 0x15002ccc, 0x15004926, 0x1509be55, 0x150c0a1b },
3856 { 196, 0x15002ccc, 0x1500492a, 0x1509be55, 0x150c0a23 },
3857 { 208, 0x15002ccc, 0x1500493a, 0x1509be55, 0x150c0a13 },
3858 { 212, 0x15002ccc, 0x1500493e, 0x1509be55, 0x150c0a1b },
3859 { 216, 0x15002ccc, 0x15004982, 0x1509be55, 0x150c0a23 },
3860};
3861
3862/*
Ivo van Doorn55f93212010-05-06 14:45:46 +02003863 * RF value list for rt3xxx
3864 * Supports: 2.4 GHz (all) & 5.2 GHz (RF3052)
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01003865 */
Ivo van Doorn55f93212010-05-06 14:45:46 +02003866static const struct rf_channel rf_vals_3x[] = {
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01003867 {1, 241, 2, 2 },
3868 {2, 241, 2, 7 },
3869 {3, 242, 2, 2 },
3870 {4, 242, 2, 7 },
3871 {5, 243, 2, 2 },
3872 {6, 243, 2, 7 },
3873 {7, 244, 2, 2 },
3874 {8, 244, 2, 7 },
3875 {9, 245, 2, 2 },
3876 {10, 245, 2, 7 },
3877 {11, 246, 2, 2 },
3878 {12, 246, 2, 7 },
3879 {13, 247, 2, 2 },
3880 {14, 248, 2, 4 },
Ivo van Doorn55f93212010-05-06 14:45:46 +02003881
3882 /* 802.11 UNI / HyperLan 2 */
3883 {36, 0x56, 0, 4},
3884 {38, 0x56, 0, 6},
3885 {40, 0x56, 0, 8},
3886 {44, 0x57, 0, 0},
3887 {46, 0x57, 0, 2},
3888 {48, 0x57, 0, 4},
3889 {52, 0x57, 0, 8},
3890 {54, 0x57, 0, 10},
3891 {56, 0x58, 0, 0},
3892 {60, 0x58, 0, 4},
3893 {62, 0x58, 0, 6},
3894 {64, 0x58, 0, 8},
3895
3896 /* 802.11 HyperLan 2 */
3897 {100, 0x5b, 0, 8},
3898 {102, 0x5b, 0, 10},
3899 {104, 0x5c, 0, 0},
3900 {108, 0x5c, 0, 4},
3901 {110, 0x5c, 0, 6},
3902 {112, 0x5c, 0, 8},
3903 {116, 0x5d, 0, 0},
3904 {118, 0x5d, 0, 2},
3905 {120, 0x5d, 0, 4},
3906 {124, 0x5d, 0, 8},
3907 {126, 0x5d, 0, 10},
3908 {128, 0x5e, 0, 0},
3909 {132, 0x5e, 0, 4},
3910 {134, 0x5e, 0, 6},
3911 {136, 0x5e, 0, 8},
3912 {140, 0x5f, 0, 0},
3913
3914 /* 802.11 UNII */
3915 {149, 0x5f, 0, 9},
3916 {151, 0x5f, 0, 11},
3917 {153, 0x60, 0, 1},
3918 {157, 0x60, 0, 5},
3919 {159, 0x60, 0, 7},
3920 {161, 0x60, 0, 9},
3921 {165, 0x61, 0, 1},
3922 {167, 0x61, 0, 3},
3923 {169, 0x61, 0, 5},
3924 {171, 0x61, 0, 7},
3925 {173, 0x61, 0, 9},
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01003926};
3927
3928int rt2800_probe_hw_mode(struct rt2x00_dev *rt2x00dev)
3929{
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01003930 struct hw_mode_spec *spec = &rt2x00dev->spec;
3931 struct channel_info *info;
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02003932 char *default_power1;
3933 char *default_power2;
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01003934 unsigned int i;
3935 u16 eeprom;
3936
3937 /*
Gertjan van Wingerde93b6bd22009-12-14 20:33:55 +01003938 * Disable powersaving as default on PCI devices.
3939 */
Gertjan van Wingerdecea90e52010-02-13 20:55:47 +01003940 if (rt2x00_is_pci(rt2x00dev) || rt2x00_is_soc(rt2x00dev))
Gertjan van Wingerde93b6bd22009-12-14 20:33:55 +01003941 rt2x00dev->hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
3942
3943 /*
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01003944 * Initialize all hw fields.
3945 */
3946 rt2x00dev->hw->flags =
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01003947 IEEE80211_HW_SIGNAL_DBM |
3948 IEEE80211_HW_SUPPORTS_PS |
Helmut Schaa1df90802010-06-29 21:38:12 +02003949 IEEE80211_HW_PS_NULLFUNC_STACK |
3950 IEEE80211_HW_AMPDU_AGGREGATION;
Helmut Schaa5a5b6ed2010-10-02 11:31:33 +02003951 /*
3952 * Don't set IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING for USB devices
3953 * unless we are capable of sending the buffered frames out after the
3954 * DTIM transmission using rt2x00lib_beacondone. This will send out
3955 * multicast and broadcast traffic immediately instead of buffering it
3956 * infinitly and thus dropping it after some time.
3957 */
3958 if (!rt2x00_is_usb(rt2x00dev))
3959 rt2x00dev->hw->flags |=
3960 IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING;
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01003961
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01003962 SET_IEEE80211_DEV(rt2x00dev->hw, rt2x00dev->dev);
3963 SET_IEEE80211_PERM_ADDR(rt2x00dev->hw,
3964 rt2x00_eeprom_addr(rt2x00dev,
3965 EEPROM_MAC_ADDR_0));
3966
Helmut Schaa3f2bee22010-06-14 22:12:01 +02003967 /*
3968 * As rt2800 has a global fallback table we cannot specify
3969 * more then one tx rate per frame but since the hw will
3970 * try several rates (based on the fallback table) we should
Helmut Schaaba3b9e52010-10-02 11:32:16 +02003971 * initialize max_report_rates to the maximum number of rates
Helmut Schaa3f2bee22010-06-14 22:12:01 +02003972 * we are going to try. Otherwise mac80211 will truncate our
3973 * reported tx rates and the rc algortihm will end up with
3974 * incorrect data.
3975 */
Helmut Schaaba3b9e52010-10-02 11:32:16 +02003976 rt2x00dev->hw->max_rates = 1;
3977 rt2x00dev->hw->max_report_rates = 7;
Helmut Schaa3f2bee22010-06-14 22:12:01 +02003978 rt2x00dev->hw->max_rate_tries = 1;
3979
RA-Jay Hung38c8a562010-12-13 12:31:27 +01003980 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC_CONF0, &eeprom);
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01003981
3982 /*
3983 * Initialize hw_mode information.
3984 */
3985 spec->supported_bands = SUPPORT_BAND_2GHZ;
3986 spec->supported_rates = SUPPORT_RATE_CCK | SUPPORT_RATE_OFDM;
3987
Gertjan van Wingerde5122d892009-12-23 00:03:25 +01003988 if (rt2x00_rf(rt2x00dev, RF2820) ||
Ivo van Doorn55f93212010-05-06 14:45:46 +02003989 rt2x00_rf(rt2x00dev, RF2720)) {
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01003990 spec->num_channels = 14;
3991 spec->channels = rf_vals;
Ivo van Doorn55f93212010-05-06 14:45:46 +02003992 } else if (rt2x00_rf(rt2x00dev, RF2850) ||
3993 rt2x00_rf(rt2x00dev, RF2750)) {
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01003994 spec->supported_bands |= SUPPORT_BAND_5GHZ;
3995 spec->num_channels = ARRAY_SIZE(rf_vals);
3996 spec->channels = rf_vals;
Gertjan van Wingerde5122d892009-12-23 00:03:25 +01003997 } else if (rt2x00_rf(rt2x00dev, RF3020) ||
3998 rt2x00_rf(rt2x00dev, RF2020) ||
3999 rt2x00_rf(rt2x00dev, RF3021) ||
Gertjan van Wingerdef93bc9b2010-11-13 19:09:50 +01004000 rt2x00_rf(rt2x00dev, RF3022) ||
Gabor Juhosadde5882011-03-03 11:46:45 +01004001 rt2x00_rf(rt2x00dev, RF3320) ||
Gertjan van Wingerdeaca355b2011-05-04 21:41:36 +02004002 rt2x00_rf(rt2x00dev, RF5370) ||
Gabor Juhosadde5882011-03-03 11:46:45 +01004003 rt2x00_rf(rt2x00dev, RF5390)) {
Ivo van Doorn55f93212010-05-06 14:45:46 +02004004 spec->num_channels = 14;
4005 spec->channels = rf_vals_3x;
4006 } else if (rt2x00_rf(rt2x00dev, RF3052)) {
4007 spec->supported_bands |= SUPPORT_BAND_5GHZ;
4008 spec->num_channels = ARRAY_SIZE(rf_vals_3x);
4009 spec->channels = rf_vals_3x;
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01004010 }
4011
4012 /*
4013 * Initialize HT information.
4014 */
Gertjan van Wingerde5122d892009-12-23 00:03:25 +01004015 if (!rt2x00_rf(rt2x00dev, RF2020))
Gertjan van Wingerde38a522e2009-11-23 22:44:47 +01004016 spec->ht.ht_supported = true;
4017 else
4018 spec->ht.ht_supported = false;
4019
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01004020 spec->ht.cap =
Gertjan van Wingerde06443e42010-06-03 10:52:08 +02004021 IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01004022 IEEE80211_HT_CAP_GRN_FLD |
4023 IEEE80211_HT_CAP_SGI_20 |
Ivo van Doornaa674632010-06-29 21:48:37 +02004024 IEEE80211_HT_CAP_SGI_40;
Helmut Schaa22cabaa2010-06-03 10:52:10 +02004025
RA-Jay Hung38c8a562010-12-13 12:31:27 +01004026 if (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_TXPATH) >= 2)
Helmut Schaa22cabaa2010-06-03 10:52:10 +02004027 spec->ht.cap |= IEEE80211_HT_CAP_TX_STBC;
4028
Ivo van Doornaa674632010-06-29 21:48:37 +02004029 spec->ht.cap |=
RA-Jay Hung38c8a562010-12-13 12:31:27 +01004030 rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RXPATH) <<
Ivo van Doornaa674632010-06-29 21:48:37 +02004031 IEEE80211_HT_CAP_RX_STBC_SHIFT;
4032
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01004033 spec->ht.ampdu_factor = 3;
4034 spec->ht.ampdu_density = 4;
4035 spec->ht.mcs.tx_params =
4036 IEEE80211_HT_MCS_TX_DEFINED |
4037 IEEE80211_HT_MCS_TX_RX_DIFF |
RA-Jay Hung38c8a562010-12-13 12:31:27 +01004038 ((rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_TXPATH) - 1) <<
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01004039 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
4040
RA-Jay Hung38c8a562010-12-13 12:31:27 +01004041 switch (rt2x00_get_field16(eeprom, EEPROM_NIC_CONF0_RXPATH)) {
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01004042 case 3:
4043 spec->ht.mcs.rx_mask[2] = 0xff;
4044 case 2:
4045 spec->ht.mcs.rx_mask[1] = 0xff;
4046 case 1:
4047 spec->ht.mcs.rx_mask[0] = 0xff;
4048 spec->ht.mcs.rx_mask[4] = 0x1; /* MCS32 */
4049 break;
4050 }
4051
4052 /*
4053 * Create channel information array
4054 */
Joe Perchesbaeb2ff2010-08-11 07:02:48 +00004055 info = kcalloc(spec->num_channels, sizeof(*info), GFP_KERNEL);
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01004056 if (!info)
4057 return -ENOMEM;
4058
4059 spec->channels_info = info;
4060
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02004061 default_power1 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_BG1);
4062 default_power2 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_BG2);
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01004063
4064 for (i = 0; i < 14; i++) {
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004065 info[i].default_power1 = default_power1[i];
4066 info[i].default_power2 = default_power2[i];
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01004067 }
4068
4069 if (spec->num_channels > 14) {
Ivo van Doorn8d1331b2010-08-23 19:56:07 +02004070 default_power1 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_A1);
4071 default_power2 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_A2);
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01004072
4073 for (i = 14; i < spec->num_channels; i++) {
RA-Jay Hunge90c54b2011-02-20 13:55:25 +01004074 info[i].default_power1 = default_power1[i];
4075 info[i].default_power2 = default_power2[i];
Bartlomiej Zolnierkiewicz4da29332009-11-08 14:39:32 +01004076 }
4077 }
4078
4079 return 0;
4080}
4081EXPORT_SYMBOL_GPL(rt2800_probe_hw_mode);
4082
4083/*
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01004084 * IEEE80211 stack callback functions.
4085 */
Helmut Schaae7836192010-07-11 12:28:54 +02004086void rt2800_get_tkip_seq(struct ieee80211_hw *hw, u8 hw_key_idx, u32 *iv32,
4087 u16 *iv16)
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01004088{
4089 struct rt2x00_dev *rt2x00dev = hw->priv;
4090 struct mac_iveiv_entry iveiv_entry;
4091 u32 offset;
4092
4093 offset = MAC_IVEIV_ENTRY(hw_key_idx);
4094 rt2800_register_multiread(rt2x00dev, offset,
4095 &iveiv_entry, sizeof(iveiv_entry));
4096
Julia Lawall855da5e2009-12-13 17:07:45 +01004097 memcpy(iv16, &iveiv_entry.iv[0], sizeof(*iv16));
4098 memcpy(iv32, &iveiv_entry.iv[4], sizeof(*iv32));
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01004099}
Helmut Schaae7836192010-07-11 12:28:54 +02004100EXPORT_SYMBOL_GPL(rt2800_get_tkip_seq);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01004101
Helmut Schaae7836192010-07-11 12:28:54 +02004102int rt2800_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01004103{
4104 struct rt2x00_dev *rt2x00dev = hw->priv;
4105 u32 reg;
4106 bool enabled = (value < IEEE80211_MAX_RTS_THRESHOLD);
4107
4108 rt2800_register_read(rt2x00dev, TX_RTS_CFG, &reg);
4109 rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_THRES, value);
4110 rt2800_register_write(rt2x00dev, TX_RTS_CFG, reg);
4111
4112 rt2800_register_read(rt2x00dev, CCK_PROT_CFG, &reg);
4113 rt2x00_set_field32(&reg, CCK_PROT_CFG_RTS_TH_EN, enabled);
4114 rt2800_register_write(rt2x00dev, CCK_PROT_CFG, reg);
4115
4116 rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
4117 rt2x00_set_field32(&reg, OFDM_PROT_CFG_RTS_TH_EN, enabled);
4118 rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
4119
4120 rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
4121 rt2x00_set_field32(&reg, MM20_PROT_CFG_RTS_TH_EN, enabled);
4122 rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
4123
4124 rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
4125 rt2x00_set_field32(&reg, MM40_PROT_CFG_RTS_TH_EN, enabled);
4126 rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
4127
4128 rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
4129 rt2x00_set_field32(&reg, GF20_PROT_CFG_RTS_TH_EN, enabled);
4130 rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
4131
4132 rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
4133 rt2x00_set_field32(&reg, GF40_PROT_CFG_RTS_TH_EN, enabled);
4134 rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
4135
4136 return 0;
4137}
Helmut Schaae7836192010-07-11 12:28:54 +02004138EXPORT_SYMBOL_GPL(rt2800_set_rts_threshold);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01004139
Helmut Schaae7836192010-07-11 12:28:54 +02004140int rt2800_conf_tx(struct ieee80211_hw *hw, u16 queue_idx,
4141 const struct ieee80211_tx_queue_params *params)
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01004142{
4143 struct rt2x00_dev *rt2x00dev = hw->priv;
4144 struct data_queue *queue;
4145 struct rt2x00_field32 field;
4146 int retval;
4147 u32 reg;
4148 u32 offset;
4149
4150 /*
4151 * First pass the configuration through rt2x00lib, that will
4152 * update the queue settings and validate the input. After that
4153 * we are free to update the registers based on the value
4154 * in the queue parameter.
4155 */
4156 retval = rt2x00mac_conf_tx(hw, queue_idx, params);
4157 if (retval)
4158 return retval;
4159
4160 /*
4161 * We only need to perform additional register initialization
4162 * for WMM queues/
4163 */
4164 if (queue_idx >= 4)
4165 return 0;
4166
Helmut Schaa11f818e2011-03-03 19:38:55 +01004167 queue = rt2x00queue_get_tx_queue(rt2x00dev, queue_idx);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01004168
4169 /* Update WMM TXOP register */
4170 offset = WMM_TXOP0_CFG + (sizeof(u32) * (!!(queue_idx & 2)));
4171 field.bit_offset = (queue_idx & 1) * 16;
4172 field.bit_mask = 0xffff << field.bit_offset;
4173
4174 rt2800_register_read(rt2x00dev, offset, &reg);
4175 rt2x00_set_field32(&reg, field, queue->txop);
4176 rt2800_register_write(rt2x00dev, offset, reg);
4177
4178 /* Update WMM registers */
4179 field.bit_offset = queue_idx * 4;
4180 field.bit_mask = 0xf << field.bit_offset;
4181
4182 rt2800_register_read(rt2x00dev, WMM_AIFSN_CFG, &reg);
4183 rt2x00_set_field32(&reg, field, queue->aifs);
4184 rt2800_register_write(rt2x00dev, WMM_AIFSN_CFG, reg);
4185
4186 rt2800_register_read(rt2x00dev, WMM_CWMIN_CFG, &reg);
4187 rt2x00_set_field32(&reg, field, queue->cw_min);
4188 rt2800_register_write(rt2x00dev, WMM_CWMIN_CFG, reg);
4189
4190 rt2800_register_read(rt2x00dev, WMM_CWMAX_CFG, &reg);
4191 rt2x00_set_field32(&reg, field, queue->cw_max);
4192 rt2800_register_write(rt2x00dev, WMM_CWMAX_CFG, reg);
4193
4194 /* Update EDCA registers */
4195 offset = EDCA_AC0_CFG + (sizeof(u32) * queue_idx);
4196
4197 rt2800_register_read(rt2x00dev, offset, &reg);
4198 rt2x00_set_field32(&reg, EDCA_AC0_CFG_TX_OP, queue->txop);
4199 rt2x00_set_field32(&reg, EDCA_AC0_CFG_AIFSN, queue->aifs);
4200 rt2x00_set_field32(&reg, EDCA_AC0_CFG_CWMIN, queue->cw_min);
4201 rt2x00_set_field32(&reg, EDCA_AC0_CFG_CWMAX, queue->cw_max);
4202 rt2800_register_write(rt2x00dev, offset, reg);
4203
4204 return 0;
4205}
Helmut Schaae7836192010-07-11 12:28:54 +02004206EXPORT_SYMBOL_GPL(rt2800_conf_tx);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01004207
Helmut Schaae7836192010-07-11 12:28:54 +02004208u64 rt2800_get_tsf(struct ieee80211_hw *hw)
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01004209{
4210 struct rt2x00_dev *rt2x00dev = hw->priv;
4211 u64 tsf;
4212 u32 reg;
4213
4214 rt2800_register_read(rt2x00dev, TSF_TIMER_DW1, &reg);
4215 tsf = (u64) rt2x00_get_field32(reg, TSF_TIMER_DW1_HIGH_WORD) << 32;
4216 rt2800_register_read(rt2x00dev, TSF_TIMER_DW0, &reg);
4217 tsf |= rt2x00_get_field32(reg, TSF_TIMER_DW0_LOW_WORD);
4218
4219 return tsf;
4220}
Helmut Schaae7836192010-07-11 12:28:54 +02004221EXPORT_SYMBOL_GPL(rt2800_get_tsf);
Bartlomiej Zolnierkiewicz2ce33992009-11-04 18:37:05 +01004222
Helmut Schaae7836192010-07-11 12:28:54 +02004223int rt2800_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4224 enum ieee80211_ampdu_mlme_action action,
Johannes Berg0b01f032011-01-18 13:51:05 +01004225 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
4226 u8 buf_size)
Helmut Schaa1df90802010-06-29 21:38:12 +02004227{
Helmut Schaa1df90802010-06-29 21:38:12 +02004228 int ret = 0;
4229
4230 switch (action) {
4231 case IEEE80211_AMPDU_RX_START:
4232 case IEEE80211_AMPDU_RX_STOP:
Helmut Schaa58ed8262010-10-02 11:33:17 +02004233 /*
4234 * The hw itself takes care of setting up BlockAck mechanisms.
4235 * So, we only have to allow mac80211 to nagotiate a BlockAck
4236 * agreement. Once that is done, the hw will BlockAck incoming
4237 * AMPDUs without further setup.
4238 */
Helmut Schaa1df90802010-06-29 21:38:12 +02004239 break;
4240 case IEEE80211_AMPDU_TX_START:
4241 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
4242 break;
4243 case IEEE80211_AMPDU_TX_STOP:
4244 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
4245 break;
4246 case IEEE80211_AMPDU_TX_OPERATIONAL:
4247 break;
4248 default:
Ivo van Doorn4e9e58c2010-06-29 21:49:50 +02004249 WARNING((struct rt2x00_dev *)hw->priv, "Unknown AMPDU action\n");
Helmut Schaa1df90802010-06-29 21:38:12 +02004250 }
4251
4252 return ret;
4253}
Helmut Schaae7836192010-07-11 12:28:54 +02004254EXPORT_SYMBOL_GPL(rt2800_ampdu_action);
Ivo van Doorna5ea2f02010-06-14 22:13:15 +02004255
Helmut Schaa977206d2010-12-13 12:31:58 +01004256int rt2800_get_survey(struct ieee80211_hw *hw, int idx,
4257 struct survey_info *survey)
4258{
4259 struct rt2x00_dev *rt2x00dev = hw->priv;
4260 struct ieee80211_conf *conf = &hw->conf;
4261 u32 idle, busy, busy_ext;
4262
4263 if (idx != 0)
4264 return -ENOENT;
4265
4266 survey->channel = conf->channel;
4267
4268 rt2800_register_read(rt2x00dev, CH_IDLE_STA, &idle);
4269 rt2800_register_read(rt2x00dev, CH_BUSY_STA, &busy);
4270 rt2800_register_read(rt2x00dev, CH_BUSY_STA_SEC, &busy_ext);
4271
4272 if (idle || busy) {
4273 survey->filled = SURVEY_INFO_CHANNEL_TIME |
4274 SURVEY_INFO_CHANNEL_TIME_BUSY |
4275 SURVEY_INFO_CHANNEL_TIME_EXT_BUSY;
4276
4277 survey->channel_time = (idle + busy) / 1000;
4278 survey->channel_time_busy = busy / 1000;
4279 survey->channel_time_ext_busy = busy_ext / 1000;
4280 }
4281
4282 return 0;
4283
4284}
4285EXPORT_SYMBOL_GPL(rt2800_get_survey);
4286
Ivo van Doorna5ea2f02010-06-14 22:13:15 +02004287MODULE_AUTHOR(DRV_PROJECT ", Bartlomiej Zolnierkiewicz");
4288MODULE_VERSION(DRV_VERSION);
4289MODULE_DESCRIPTION("Ralink RT2800 library");
4290MODULE_LICENSE("GPL");